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
,
493 * A negative socket can be used by the caller when
494 * cleaning-up a ua_chan in an error path. Skip the
495 * accounting in this case.
498 save_per_pid_lost_discarded_counters(ua_chan
);
502 if (ua_chan
->obj
!= NULL
) {
503 /* Remove channel from application UST object descriptor. */
504 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
505 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
507 pthread_mutex_lock(&app
->sock_lock
);
508 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
509 pthread_mutex_unlock(&app
->sock_lock
);
510 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
511 ERR("UST app sock %d release channel obj failed with ret %d",
514 lttng_fd_put(LTTNG_FD_APPS
, 1);
517 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
520 int ust_app_register_done(struct ust_app
*app
)
524 pthread_mutex_lock(&app
->sock_lock
);
525 ret
= ustctl_register_done(app
->sock
);
526 pthread_mutex_unlock(&app
->sock_lock
);
530 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
535 pthread_mutex_lock(&app
->sock_lock
);
540 ret
= ustctl_release_object(sock
, data
);
542 pthread_mutex_unlock(&app
->sock_lock
);
548 * Push metadata to consumer socket.
550 * RCU read-side lock must be held to guarantee existance of socket.
551 * Must be called with the ust app session lock held.
552 * Must be called with the registry lock held.
554 * On success, return the len of metadata pushed or else a negative value.
555 * Returning a -EPIPE return value means we could not send the metadata,
556 * but it can be caused by recoverable errors (e.g. the application has
557 * terminated concurrently).
559 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
560 struct consumer_socket
*socket
, int send_zero_data
)
563 char *metadata_str
= NULL
;
564 size_t len
, offset
, new_metadata_len_sent
;
566 uint64_t metadata_key
, metadata_version
;
571 metadata_key
= registry
->metadata_key
;
574 * Means that no metadata was assigned to the session. This can
575 * happens if no start has been done previously.
581 offset
= registry
->metadata_len_sent
;
582 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
583 new_metadata_len_sent
= registry
->metadata_len
;
584 metadata_version
= registry
->metadata_version
;
586 DBG3("No metadata to push for metadata key %" PRIu64
,
587 registry
->metadata_key
);
589 if (send_zero_data
) {
590 DBG("No metadata to push");
596 /* Allocate only what we have to send. */
597 metadata_str
= zmalloc(len
);
599 PERROR("zmalloc ust app metadata string");
603 /* Copy what we haven't sent out. */
604 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
607 pthread_mutex_unlock(®istry
->lock
);
609 * We need to unlock the registry while we push metadata to
610 * break a circular dependency between the consumerd metadata
611 * lock and the sessiond registry lock. Indeed, pushing metadata
612 * to the consumerd awaits that it gets pushed all the way to
613 * relayd, but doing so requires grabbing the metadata lock. If
614 * a concurrent metadata request is being performed by
615 * consumerd, this can try to grab the registry lock on the
616 * sessiond while holding the metadata lock on the consumer
617 * daemon. Those push and pull schemes are performed on two
618 * different bidirectionnal communication sockets.
620 ret
= consumer_push_metadata(socket
, metadata_key
,
621 metadata_str
, len
, offset
, metadata_version
);
622 pthread_mutex_lock(®istry
->lock
);
625 * There is an acceptable race here between the registry
626 * metadata key assignment and the creation on the
627 * consumer. The session daemon can concurrently push
628 * metadata for this registry while being created on the
629 * consumer since the metadata key of the registry is
630 * assigned *before* it is setup to avoid the consumer
631 * to ask for metadata that could possibly be not found
632 * in the session daemon.
634 * The metadata will get pushed either by the session
635 * being stopped or the consumer requesting metadata if
636 * that race is triggered.
638 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
641 ERR("Error pushing metadata to consumer");
647 * Metadata may have been concurrently pushed, since
648 * we're not holding the registry lock while pushing to
649 * consumer. This is handled by the fact that we send
650 * the metadata content, size, and the offset at which
651 * that metadata belongs. This may arrive out of order
652 * on the consumer side, and the consumer is able to
653 * deal with overlapping fragments. The consumer
654 * supports overlapping fragments, which must be
655 * contiguous starting from offset 0. We keep the
656 * largest metadata_len_sent value of the concurrent
659 registry
->metadata_len_sent
=
660 max_t(size_t, registry
->metadata_len_sent
,
661 new_metadata_len_sent
);
670 * On error, flag the registry that the metadata is
671 * closed. We were unable to push anything and this
672 * means that either the consumer is not responding or
673 * the metadata cache has been destroyed on the
676 registry
->metadata_closed
= 1;
684 * For a given application and session, push metadata to consumer.
685 * Either sock or consumer is required : if sock is NULL, the default
686 * socket to send the metadata is retrieved from consumer, if sock
687 * is not NULL we use it to send the metadata.
688 * RCU read-side lock must be held while calling this function,
689 * therefore ensuring existance of registry. It also ensures existance
690 * of socket throughout this function.
692 * Return 0 on success else a negative error.
693 * Returning a -EPIPE return value means we could not send the metadata,
694 * but it can be caused by recoverable errors (e.g. the application has
695 * terminated concurrently).
697 static int push_metadata(struct ust_registry_session
*registry
,
698 struct consumer_output
*consumer
)
702 struct consumer_socket
*socket
;
707 pthread_mutex_lock(®istry
->lock
);
708 if (registry
->metadata_closed
) {
713 /* Get consumer socket to use to push the metadata.*/
714 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
721 ret
= ust_app_push_metadata(registry
, socket
, 0);
726 pthread_mutex_unlock(®istry
->lock
);
730 pthread_mutex_unlock(®istry
->lock
);
735 * Send to the consumer a close metadata command for the given session. Once
736 * done, the metadata channel is deleted and the session metadata pointer is
737 * nullified. The session lock MUST be held unless the application is
738 * in the destroy path.
740 * Return 0 on success else a negative value.
742 static int close_metadata(struct ust_registry_session
*registry
,
743 struct consumer_output
*consumer
)
746 struct consumer_socket
*socket
;
753 pthread_mutex_lock(®istry
->lock
);
755 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
760 /* Get consumer socket to use to push the metadata.*/
761 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
768 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
775 * Metadata closed. Even on error this means that the consumer is not
776 * responding or not found so either way a second close should NOT be emit
779 registry
->metadata_closed
= 1;
781 pthread_mutex_unlock(®istry
->lock
);
787 * We need to execute ht_destroy outside of RCU read-side critical
788 * section and outside of call_rcu thread, so we postpone its execution
789 * using ht_cleanup_push. It is simpler than to change the semantic of
790 * the many callers of delete_ust_app_session().
793 void delete_ust_app_session_rcu(struct rcu_head
*head
)
795 struct ust_app_session
*ua_sess
=
796 caa_container_of(head
, struct ust_app_session
, rcu_head
);
798 ht_cleanup_push(ua_sess
->channels
);
803 * Delete ust app session safely. RCU read lock must be held before calling
806 * The session list lock must be held by the caller.
809 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
813 struct lttng_ht_iter iter
;
814 struct ust_app_channel
*ua_chan
;
815 struct ust_registry_session
*registry
;
819 pthread_mutex_lock(&ua_sess
->lock
);
821 assert(!ua_sess
->deleted
);
822 ua_sess
->deleted
= true;
824 registry
= get_session_registry(ua_sess
);
825 /* Registry can be null on error path during initialization. */
827 /* Push metadata for application before freeing the application. */
828 (void) push_metadata(registry
, ua_sess
->consumer
);
831 * Don't ask to close metadata for global per UID buffers. Close
832 * metadata only on destroy trace session in this case. Also, the
833 * previous push metadata could have flag the metadata registry to
834 * close so don't send a close command if closed.
836 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
837 /* And ask to close it for this session registry. */
838 (void) close_metadata(registry
, ua_sess
->consumer
);
842 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
844 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
846 delete_ust_app_channel(sock
, ua_chan
, app
);
849 /* In case of per PID, the registry is kept in the session. */
850 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
851 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
854 * Registry can be null on error path during
857 buffer_reg_pid_remove(reg_pid
);
858 buffer_reg_pid_destroy(reg_pid
);
862 if (ua_sess
->handle
!= -1) {
863 pthread_mutex_lock(&app
->sock_lock
);
864 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
865 pthread_mutex_unlock(&app
->sock_lock
);
866 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
867 ERR("UST app sock %d release session handle failed with ret %d",
870 /* Remove session from application UST object descriptor. */
871 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
872 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
876 pthread_mutex_unlock(&ua_sess
->lock
);
878 consumer_output_put(ua_sess
->consumer
);
880 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
884 * Delete a traceable application structure from the global list. Never call
885 * this function outside of a call_rcu call.
887 * RCU read side lock should _NOT_ be held when calling this function.
890 void delete_ust_app(struct ust_app
*app
)
893 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
896 * The session list lock must be held during this function to guarantee
897 * the existence of ua_sess.
900 /* Delete ust app sessions info */
905 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
907 /* Free every object in the session and the session. */
909 delete_ust_app_session(sock
, ua_sess
, app
);
913 ht_cleanup_push(app
->sessions
);
914 ht_cleanup_push(app
->ust_sessions_objd
);
915 ht_cleanup_push(app
->ust_objd
);
918 * Wait until we have deleted the application from the sock hash table
919 * before closing this socket, otherwise an application could re-use the
920 * socket ID and race with the teardown, using the same hash table entry.
922 * It's OK to leave the close in call_rcu. We want it to stay unique for
923 * all RCU readers that could run concurrently with unregister app,
924 * therefore we _need_ to only close that socket after a grace period. So
925 * it should stay in this RCU callback.
927 * This close() is a very important step of the synchronization model so
928 * every modification to this function must be carefully reviewed.
934 lttng_fd_put(LTTNG_FD_APPS
, 1);
936 DBG2("UST app pid %d deleted", app
->pid
);
938 session_unlock_list();
942 * URCU intermediate call to delete an UST app.
945 void delete_ust_app_rcu(struct rcu_head
*head
)
947 struct lttng_ht_node_ulong
*node
=
948 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
949 struct ust_app
*app
=
950 caa_container_of(node
, struct ust_app
, pid_n
);
952 DBG3("Call RCU deleting app PID %d", app
->pid
);
957 * Delete the session from the application ht and delete the data structure by
958 * freeing every object inside and releasing them.
960 * The session list lock must be held by the caller.
962 static void destroy_app_session(struct ust_app
*app
,
963 struct ust_app_session
*ua_sess
)
966 struct lttng_ht_iter iter
;
971 iter
.iter
.node
= &ua_sess
->node
.node
;
972 ret
= lttng_ht_del(app
->sessions
, &iter
);
974 /* Already scheduled for teardown. */
978 /* Once deleted, free the data structure. */
979 delete_ust_app_session(app
->sock
, ua_sess
, app
);
986 * Alloc new UST app session.
989 struct ust_app_session
*alloc_ust_app_session(void)
991 struct ust_app_session
*ua_sess
;
993 /* Init most of the default value by allocating and zeroing */
994 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
995 if (ua_sess
== NULL
) {
1000 ua_sess
->handle
= -1;
1001 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1002 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1003 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1012 * Alloc new UST app channel.
1015 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
1016 struct ust_app_session
*ua_sess
,
1017 struct lttng_ust_channel_attr
*attr
)
1019 struct ust_app_channel
*ua_chan
;
1021 /* Init most of the default value by allocating and zeroing */
1022 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1023 if (ua_chan
== NULL
) {
1028 /* Setup channel name */
1029 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1030 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1032 ua_chan
->enabled
= 1;
1033 ua_chan
->handle
= -1;
1034 ua_chan
->session
= ua_sess
;
1035 ua_chan
->key
= get_next_channel_key();
1036 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1037 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1038 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1040 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1041 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1043 /* Copy attributes */
1045 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1046 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1047 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1048 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1049 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1050 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1051 ua_chan
->attr
.output
= attr
->output
;
1052 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1054 /* By default, the channel is a per cpu channel. */
1055 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1057 DBG3("UST app channel %s allocated", ua_chan
->name
);
1066 * Allocate and initialize a UST app stream.
1068 * Return newly allocated stream pointer or NULL on error.
1070 struct ust_app_stream
*ust_app_alloc_stream(void)
1072 struct ust_app_stream
*stream
= NULL
;
1074 stream
= zmalloc(sizeof(*stream
));
1075 if (stream
== NULL
) {
1076 PERROR("zmalloc ust app stream");
1080 /* Zero could be a valid value for a handle so flag it to -1. */
1081 stream
->handle
= -1;
1088 * Alloc new UST app event.
1091 struct ust_app_event
*alloc_ust_app_event(char *name
,
1092 struct lttng_ust_event
*attr
)
1094 struct ust_app_event
*ua_event
;
1096 /* Init most of the default value by allocating and zeroing */
1097 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1098 if (ua_event
== NULL
) {
1103 ua_event
->enabled
= 1;
1104 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1105 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1106 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1108 /* Copy attributes */
1110 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1113 DBG3("UST app event %s allocated", ua_event
->name
);
1122 * Alloc new UST app context.
1125 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1127 struct ust_app_ctx
*ua_ctx
;
1129 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1130 if (ua_ctx
== NULL
) {
1134 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1137 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1138 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1139 char *provider_name
= NULL
, *ctx_name
= NULL
;
1141 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1142 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1143 if (!provider_name
|| !ctx_name
) {
1144 free(provider_name
);
1149 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1150 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1154 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1162 * Allocate a filter and copy the given original filter.
1164 * Return allocated filter or NULL on error.
1166 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1167 struct lttng_filter_bytecode
*orig_f
)
1169 struct lttng_filter_bytecode
*filter
= NULL
;
1171 /* Copy filter bytecode */
1172 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1174 PERROR("zmalloc alloc filter bytecode");
1178 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1185 * Create a liblttng-ust filter bytecode from given bytecode.
1187 * Return allocated filter or NULL on error.
1189 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1190 struct lttng_filter_bytecode
*orig_f
)
1192 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1194 /* Copy filter bytecode */
1195 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1197 PERROR("zmalloc alloc ust filter bytecode");
1201 assert(sizeof(struct lttng_filter_bytecode
) ==
1202 sizeof(struct lttng_ust_filter_bytecode
));
1203 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1209 * Find an ust_app using the sock and return it. RCU read side lock must be
1210 * held before calling this helper function.
1212 struct ust_app
*ust_app_find_by_sock(int sock
)
1214 struct lttng_ht_node_ulong
*node
;
1215 struct lttng_ht_iter iter
;
1217 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1218 node
= lttng_ht_iter_get_node_ulong(&iter
);
1220 DBG2("UST app find by sock %d not found", sock
);
1224 return caa_container_of(node
, struct ust_app
, sock_n
);
1231 * Find an ust_app using the notify sock and return it. RCU read side lock must
1232 * be held before calling this helper function.
1234 static struct ust_app
*find_app_by_notify_sock(int sock
)
1236 struct lttng_ht_node_ulong
*node
;
1237 struct lttng_ht_iter iter
;
1239 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1241 node
= lttng_ht_iter_get_node_ulong(&iter
);
1243 DBG2("UST app find by notify sock %d not found", sock
);
1247 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1254 * Lookup for an ust app event based on event name, filter bytecode and the
1257 * Return an ust_app_event object or NULL on error.
1259 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1260 char *name
, struct lttng_filter_bytecode
*filter
,
1262 const struct lttng_event_exclusion
*exclusion
)
1264 struct lttng_ht_iter iter
;
1265 struct lttng_ht_node_str
*node
;
1266 struct ust_app_event
*event
= NULL
;
1267 struct ust_app_ht_key key
;
1272 /* Setup key for event lookup. */
1274 key
.filter
= filter
;
1275 key
.loglevel_type
= loglevel_value
;
1276 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1277 key
.exclusion
= exclusion
;
1279 /* Lookup using the event name as hash and a custom match fct. */
1280 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1281 ht_match_ust_app_event
, &key
, &iter
.iter
);
1282 node
= lttng_ht_iter_get_node_str(&iter
);
1287 event
= caa_container_of(node
, struct ust_app_event
, node
);
1294 * Create the channel context on the tracer.
1296 * Called with UST app session lock held.
1299 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1300 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1304 health_code_update();
1306 pthread_mutex_lock(&app
->sock_lock
);
1307 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1308 ua_chan
->obj
, &ua_ctx
->obj
);
1309 pthread_mutex_unlock(&app
->sock_lock
);
1311 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1312 ERR("UST app create channel context failed for app (pid: %d) "
1313 "with ret %d", app
->pid
, ret
);
1316 * This is normal behavior, an application can die during the
1317 * creation process. Don't report an error so the execution can
1318 * continue normally.
1321 DBG3("UST app disable event failed. Application is dead.");
1326 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1328 DBG2("UST app context handle %d created successfully for channel %s",
1329 ua_ctx
->handle
, ua_chan
->name
);
1332 health_code_update();
1337 * Set the filter on the tracer.
1340 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1341 struct ust_app
*app
)
1344 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1346 health_code_update();
1348 if (!ua_event
->filter
) {
1353 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1354 if (!ust_bytecode
) {
1355 ret
= -LTTNG_ERR_NOMEM
;
1358 pthread_mutex_lock(&app
->sock_lock
);
1359 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1361 pthread_mutex_unlock(&app
->sock_lock
);
1363 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1364 ERR("UST app event %s filter failed for app (pid: %d) "
1365 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1368 * This is normal behavior, an application can die during the
1369 * creation process. Don't report an error so the execution can
1370 * continue normally.
1373 DBG3("UST app filter event failed. Application is dead.");
1378 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1381 health_code_update();
1387 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1388 struct lttng_event_exclusion
*exclusion
)
1390 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1391 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1392 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1394 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1395 if (!ust_exclusion
) {
1400 assert(sizeof(struct lttng_event_exclusion
) ==
1401 sizeof(struct lttng_ust_event_exclusion
));
1402 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1404 return ust_exclusion
;
1408 * Set event exclusions on the tracer.
1411 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1412 struct ust_app
*app
)
1415 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1417 health_code_update();
1419 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1424 ust_exclusion
= create_ust_exclusion_from_exclusion(
1425 ua_event
->exclusion
);
1426 if (!ust_exclusion
) {
1427 ret
= -LTTNG_ERR_NOMEM
;
1430 pthread_mutex_lock(&app
->sock_lock
);
1431 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1432 pthread_mutex_unlock(&app
->sock_lock
);
1434 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1435 ERR("UST app event %s exclusions failed for app (pid: %d) "
1436 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1439 * This is normal behavior, an application can die during the
1440 * creation process. Don't report an error so the execution can
1441 * continue normally.
1444 DBG3("UST app event exclusion failed. Application is dead.");
1449 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1452 health_code_update();
1453 free(ust_exclusion
);
1458 * Disable the specified event on to UST tracer for the UST session.
1460 static int disable_ust_event(struct ust_app
*app
,
1461 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1465 health_code_update();
1467 pthread_mutex_lock(&app
->sock_lock
);
1468 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1469 pthread_mutex_unlock(&app
->sock_lock
);
1471 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1472 ERR("UST app event %s disable failed for app (pid: %d) "
1473 "and session handle %d with ret %d",
1474 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1477 * This is normal behavior, an application can die during the
1478 * creation process. Don't report an error so the execution can
1479 * continue normally.
1482 DBG3("UST app disable event failed. Application is dead.");
1487 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1488 ua_event
->attr
.name
, app
->pid
);
1491 health_code_update();
1496 * Disable the specified channel on to UST tracer for the UST session.
1498 static int disable_ust_channel(struct ust_app
*app
,
1499 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1503 health_code_update();
1505 pthread_mutex_lock(&app
->sock_lock
);
1506 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1507 pthread_mutex_unlock(&app
->sock_lock
);
1509 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1510 ERR("UST app channel %s disable failed for app (pid: %d) "
1511 "and session handle %d with ret %d",
1512 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1515 * This is normal behavior, an application can die during the
1516 * creation process. Don't report an error so the execution can
1517 * continue normally.
1520 DBG3("UST app disable channel failed. Application is dead.");
1525 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1526 ua_chan
->name
, app
->pid
);
1529 health_code_update();
1534 * Enable the specified channel on to UST tracer for the UST session.
1536 static int enable_ust_channel(struct ust_app
*app
,
1537 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1541 health_code_update();
1543 pthread_mutex_lock(&app
->sock_lock
);
1544 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1545 pthread_mutex_unlock(&app
->sock_lock
);
1547 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1548 ERR("UST app channel %s enable failed for app (pid: %d) "
1549 "and session handle %d with ret %d",
1550 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1553 * This is normal behavior, an application can die during the
1554 * creation process. Don't report an error so the execution can
1555 * continue normally.
1558 DBG3("UST app enable channel failed. Application is dead.");
1563 ua_chan
->enabled
= 1;
1565 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1566 ua_chan
->name
, app
->pid
);
1569 health_code_update();
1574 * Enable the specified event on to UST tracer for the UST session.
1576 static int enable_ust_event(struct ust_app
*app
,
1577 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1581 health_code_update();
1583 pthread_mutex_lock(&app
->sock_lock
);
1584 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1585 pthread_mutex_unlock(&app
->sock_lock
);
1587 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1588 ERR("UST app event %s enable failed for app (pid: %d) "
1589 "and session handle %d with ret %d",
1590 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1593 * This is normal behavior, an application can die during the
1594 * creation process. Don't report an error so the execution can
1595 * continue normally.
1598 DBG3("UST app enable event failed. Application is dead.");
1603 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1604 ua_event
->attr
.name
, app
->pid
);
1607 health_code_update();
1612 * Send channel and stream buffer to application.
1614 * Return 0 on success. On error, a negative value is returned.
1616 static int send_channel_pid_to_ust(struct ust_app
*app
,
1617 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1620 struct ust_app_stream
*stream
, *stmp
;
1626 health_code_update();
1628 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1631 /* Send channel to the application. */
1632 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1633 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1634 ret
= -ENOTCONN
; /* Caused by app exiting. */
1636 } else if (ret
< 0) {
1640 health_code_update();
1642 /* Send all streams to application. */
1643 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1644 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1645 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1646 ret
= -ENOTCONN
; /* Caused by app exiting. */
1648 } else if (ret
< 0) {
1651 /* We don't need the stream anymore once sent to the tracer. */
1652 cds_list_del(&stream
->list
);
1653 delete_ust_app_stream(-1, stream
, app
);
1655 /* Flag the channel that it is sent to the application. */
1656 ua_chan
->is_sent
= 1;
1659 health_code_update();
1664 * Create the specified event onto the UST tracer for a UST session.
1666 * Should be called with session mutex held.
1669 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1670 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1674 health_code_update();
1676 /* Create UST event on tracer */
1677 pthread_mutex_lock(&app
->sock_lock
);
1678 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1680 pthread_mutex_unlock(&app
->sock_lock
);
1682 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1683 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1684 ua_event
->attr
.name
, app
->pid
, ret
);
1687 * This is normal behavior, an application can die during the
1688 * creation process. Don't report an error so the execution can
1689 * continue normally.
1692 DBG3("UST app create event failed. Application is dead.");
1697 ua_event
->handle
= ua_event
->obj
->handle
;
1699 DBG2("UST app event %s created successfully for pid:%d",
1700 ua_event
->attr
.name
, app
->pid
);
1702 health_code_update();
1704 /* Set filter if one is present. */
1705 if (ua_event
->filter
) {
1706 ret
= set_ust_event_filter(ua_event
, app
);
1712 /* Set exclusions for the event */
1713 if (ua_event
->exclusion
) {
1714 ret
= set_ust_event_exclusion(ua_event
, app
);
1720 /* If event not enabled, disable it on the tracer */
1721 if (ua_event
->enabled
) {
1723 * We now need to explicitly enable the event, since it
1724 * is now disabled at creation.
1726 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1729 * If we hit an EPERM, something is wrong with our enable call. If
1730 * we get an EEXIST, there is a problem on the tracer side since we
1734 case -LTTNG_UST_ERR_PERM
:
1735 /* Code flow problem */
1737 case -LTTNG_UST_ERR_EXIST
:
1738 /* It's OK for our use case. */
1749 health_code_update();
1754 * Copy data between an UST app event and a LTT event.
1756 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1757 struct ltt_ust_event
*uevent
)
1759 size_t exclusion_alloc_size
;
1761 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1762 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1764 ua_event
->enabled
= uevent
->enabled
;
1766 /* Copy event attributes */
1767 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1769 /* Copy filter bytecode */
1770 if (uevent
->filter
) {
1771 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1772 /* Filter might be NULL here in case of ENONEM. */
1775 /* Copy exclusion data */
1776 if (uevent
->exclusion
) {
1777 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1778 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1779 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1780 if (ua_event
->exclusion
== NULL
) {
1783 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1784 exclusion_alloc_size
);
1790 * Copy data between an UST app channel and a LTT channel.
1792 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1793 struct ltt_ust_channel
*uchan
)
1795 struct lttng_ht_iter iter
;
1796 struct ltt_ust_event
*uevent
;
1797 struct ltt_ust_context
*uctx
;
1798 struct ust_app_event
*ua_event
;
1800 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1802 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1803 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1805 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1806 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1808 /* Copy event attributes since the layout is different. */
1809 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1810 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1811 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1812 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1813 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1814 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1815 ua_chan
->attr
.output
= uchan
->attr
.output
;
1816 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
1819 * Note that the attribute channel type is not set since the channel on the
1820 * tracing registry side does not have this information.
1823 ua_chan
->enabled
= uchan
->enabled
;
1824 ua_chan
->tracing_channel_id
= uchan
->id
;
1826 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1827 struct ust_app_ctx
*ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1829 if (ua_ctx
== NULL
) {
1832 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1833 (unsigned long) ua_ctx
->ctx
.ctx
);
1834 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1835 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1838 /* Copy all events from ltt ust channel to ust app channel */
1839 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1840 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1841 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1842 if (ua_event
== NULL
) {
1843 DBG2("UST event %s not found on shadow copy channel",
1845 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1846 if (ua_event
== NULL
) {
1849 shadow_copy_event(ua_event
, uevent
);
1850 add_unique_ust_app_event(ua_chan
, ua_event
);
1854 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1858 * Copy data between a UST app session and a regular LTT session.
1860 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1861 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1863 struct lttng_ht_node_str
*ua_chan_node
;
1864 struct lttng_ht_iter iter
;
1865 struct ltt_ust_channel
*uchan
;
1866 struct ust_app_channel
*ua_chan
;
1868 struct tm
*timeinfo
;
1871 char tmp_shm_path
[PATH_MAX
];
1873 /* Get date and time for unique app path */
1875 timeinfo
= localtime(&rawtime
);
1876 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1878 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1880 ua_sess
->tracing_id
= usess
->id
;
1881 ua_sess
->id
= get_next_session_id();
1882 ua_sess
->uid
= app
->uid
;
1883 ua_sess
->gid
= app
->gid
;
1884 ua_sess
->euid
= usess
->uid
;
1885 ua_sess
->egid
= usess
->gid
;
1886 ua_sess
->buffer_type
= usess
->buffer_type
;
1887 ua_sess
->bits_per_long
= app
->bits_per_long
;
1889 /* There is only one consumer object per session possible. */
1890 consumer_output_get(usess
->consumer
);
1891 ua_sess
->consumer
= usess
->consumer
;
1893 ua_sess
->output_traces
= usess
->output_traces
;
1894 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1895 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1896 &usess
->metadata_attr
);
1898 switch (ua_sess
->buffer_type
) {
1899 case LTTNG_BUFFER_PER_PID
:
1900 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1901 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1904 case LTTNG_BUFFER_PER_UID
:
1905 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1906 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1913 PERROR("asprintf UST shadow copy session");
1918 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1919 sizeof(ua_sess
->root_shm_path
));
1920 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1921 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1922 sizeof(ua_sess
->shm_path
));
1923 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1924 if (ua_sess
->shm_path
[0]) {
1925 switch (ua_sess
->buffer_type
) {
1926 case LTTNG_BUFFER_PER_PID
:
1927 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1928 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1929 app
->name
, app
->pid
, datetime
);
1931 case LTTNG_BUFFER_PER_UID
:
1932 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1933 DEFAULT_UST_TRACE_UID_PATH
,
1934 app
->uid
, app
->bits_per_long
);
1941 PERROR("sprintf UST shadow copy session");
1945 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1946 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1947 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1950 /* Iterate over all channels in global domain. */
1951 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1953 struct lttng_ht_iter uiter
;
1955 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1956 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1957 if (ua_chan_node
!= NULL
) {
1958 /* Session exist. Contiuing. */
1962 DBG2("Channel %s not found on shadow session copy, creating it",
1964 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
,
1966 if (ua_chan
== NULL
) {
1967 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1970 shadow_copy_channel(ua_chan
, uchan
);
1972 * The concept of metadata channel does not exist on the tracing
1973 * registry side of the session daemon so this can only be a per CPU
1974 * channel and not metadata.
1976 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1978 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1983 consumer_output_put(ua_sess
->consumer
);
1987 * Lookup sesison wrapper.
1990 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1991 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1993 /* Get right UST app session from app */
1994 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1998 * Return ust app session from the app session hashtable using the UST session
2001 static struct ust_app_session
*lookup_session_by_app(
2002 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2004 struct lttng_ht_iter iter
;
2005 struct lttng_ht_node_u64
*node
;
2007 __lookup_session_by_app(usess
, app
, &iter
);
2008 node
= lttng_ht_iter_get_node_u64(&iter
);
2013 return caa_container_of(node
, struct ust_app_session
, node
);
2020 * Setup buffer registry per PID for the given session and application. If none
2021 * is found, a new one is created, added to the global registry and
2022 * initialized. If regp is valid, it's set with the newly created object.
2024 * Return 0 on success or else a negative value.
2026 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2027 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2030 struct buffer_reg_pid
*reg_pid
;
2037 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2040 * This is the create channel path meaning that if there is NO
2041 * registry available, we have to create one for this session.
2043 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2044 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2052 /* Initialize registry. */
2053 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2054 app
->bits_per_long
, app
->uint8_t_alignment
,
2055 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2056 app
->uint64_t_alignment
, app
->long_alignment
,
2057 app
->byte_order
, app
->version
.major
,
2058 app
->version
.minor
, reg_pid
->root_shm_path
,
2060 ua_sess
->euid
, ua_sess
->egid
);
2063 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2064 * destroy the buffer registry, because it is always expected
2065 * that if the buffer registry can be found, its ust registry is
2068 buffer_reg_pid_destroy(reg_pid
);
2072 buffer_reg_pid_add(reg_pid
);
2074 DBG3("UST app buffer registry per PID created successfully");
2086 * Setup buffer registry per UID for the given session and application. If none
2087 * is found, a new one is created, added to the global registry and
2088 * initialized. If regp is valid, it's set with the newly created object.
2090 * Return 0 on success or else a negative value.
2092 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2093 struct ust_app_session
*ua_sess
,
2094 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2097 struct buffer_reg_uid
*reg_uid
;
2104 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2107 * This is the create channel path meaning that if there is NO
2108 * registry available, we have to create one for this session.
2110 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2111 LTTNG_DOMAIN_UST
, ®_uid
,
2112 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2120 /* Initialize registry. */
2121 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2122 app
->bits_per_long
, app
->uint8_t_alignment
,
2123 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2124 app
->uint64_t_alignment
, app
->long_alignment
,
2125 app
->byte_order
, app
->version
.major
,
2126 app
->version
.minor
, reg_uid
->root_shm_path
,
2127 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
2130 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2131 * destroy the buffer registry, because it is always expected
2132 * that if the buffer registry can be found, its ust registry is
2135 buffer_reg_uid_destroy(reg_uid
, NULL
);
2138 /* Add node to teardown list of the session. */
2139 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2141 buffer_reg_uid_add(reg_uid
);
2143 DBG3("UST app buffer registry per UID created successfully");
2154 * Create a session on the tracer side for the given app.
2156 * On success, ua_sess_ptr is populated with the session pointer or else left
2157 * untouched. If the session was created, is_created is set to 1. On error,
2158 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2161 * Returns 0 on success or else a negative code which is either -ENOMEM or
2162 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2164 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2165 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2168 int ret
, created
= 0;
2169 struct ust_app_session
*ua_sess
;
2173 assert(ua_sess_ptr
);
2175 health_code_update();
2177 ua_sess
= lookup_session_by_app(usess
, app
);
2178 if (ua_sess
== NULL
) {
2179 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2180 app
->pid
, usess
->id
);
2181 ua_sess
= alloc_ust_app_session();
2182 if (ua_sess
== NULL
) {
2183 /* Only malloc can failed so something is really wrong */
2187 shadow_copy_session(ua_sess
, usess
, app
);
2191 switch (usess
->buffer_type
) {
2192 case LTTNG_BUFFER_PER_PID
:
2193 /* Init local registry. */
2194 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2196 delete_ust_app_session(-1, ua_sess
, app
);
2200 case LTTNG_BUFFER_PER_UID
:
2201 /* Look for a global registry. If none exists, create one. */
2202 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2204 delete_ust_app_session(-1, ua_sess
, app
);
2214 health_code_update();
2216 if (ua_sess
->handle
== -1) {
2217 pthread_mutex_lock(&app
->sock_lock
);
2218 ret
= ustctl_create_session(app
->sock
);
2219 pthread_mutex_unlock(&app
->sock_lock
);
2221 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2222 ERR("Creating session for app pid %d with ret %d",
2225 DBG("UST app creating session failed. Application is dead");
2227 * This is normal behavior, an application can die during the
2228 * creation process. Don't report an error so the execution can
2229 * continue normally. This will get flagged ENOTCONN and the
2230 * caller will handle it.
2234 delete_ust_app_session(-1, ua_sess
, app
);
2235 if (ret
!= -ENOMEM
) {
2237 * Tracer is probably gone or got an internal error so let's
2238 * behave like it will soon unregister or not usable.
2245 ua_sess
->handle
= ret
;
2247 /* Add ust app session to app's HT */
2248 lttng_ht_node_init_u64(&ua_sess
->node
,
2249 ua_sess
->tracing_id
);
2250 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2251 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2252 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2253 &ua_sess
->ust_objd_node
);
2255 DBG2("UST app session created successfully with handle %d", ret
);
2258 *ua_sess_ptr
= ua_sess
;
2260 *is_created
= created
;
2263 /* Everything went well. */
2267 health_code_update();
2272 * Match function for a hash table lookup of ust_app_ctx.
2274 * It matches an ust app context based on the context type and, in the case
2275 * of perf counters, their name.
2277 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2279 struct ust_app_ctx
*ctx
;
2280 const struct lttng_ust_context_attr
*key
;
2285 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2289 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2294 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2295 if (strncmp(key
->u
.perf_counter
.name
,
2296 ctx
->ctx
.u
.perf_counter
.name
,
2297 sizeof(key
->u
.perf_counter
.name
))) {
2301 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2302 if (strcmp(key
->u
.app_ctx
.provider_name
,
2303 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2304 strcmp(key
->u
.app_ctx
.ctx_name
,
2305 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2321 * Lookup for an ust app context from an lttng_ust_context.
2323 * Must be called while holding RCU read side lock.
2324 * Return an ust_app_ctx object or NULL on error.
2327 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2328 struct lttng_ust_context_attr
*uctx
)
2330 struct lttng_ht_iter iter
;
2331 struct lttng_ht_node_ulong
*node
;
2332 struct ust_app_ctx
*app_ctx
= NULL
;
2337 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2338 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2339 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2340 node
= lttng_ht_iter_get_node_ulong(&iter
);
2345 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2352 * Create a context for the channel on the tracer.
2354 * Called with UST app session lock held and a RCU read side lock.
2357 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2358 struct lttng_ust_context_attr
*uctx
,
2359 struct ust_app
*app
)
2362 struct ust_app_ctx
*ua_ctx
;
2364 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2366 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2372 ua_ctx
= alloc_ust_app_ctx(uctx
);
2373 if (ua_ctx
== NULL
) {
2379 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2380 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2381 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2383 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2393 * Enable on the tracer side a ust app event for the session and channel.
2395 * Called with UST app session lock held.
2398 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2399 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2403 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2408 ua_event
->enabled
= 1;
2415 * Disable on the tracer side a ust app event for the session and channel.
2417 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2418 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2422 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2427 ua_event
->enabled
= 0;
2434 * Lookup ust app channel for session and disable it on the tracer side.
2437 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2438 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2442 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2447 ua_chan
->enabled
= 0;
2454 * Lookup ust app channel for session and enable it on the tracer side. This
2455 * MUST be called with a RCU read side lock acquired.
2457 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2458 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2461 struct lttng_ht_iter iter
;
2462 struct lttng_ht_node_str
*ua_chan_node
;
2463 struct ust_app_channel
*ua_chan
;
2465 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2466 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2467 if (ua_chan_node
== NULL
) {
2468 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2469 uchan
->name
, ua_sess
->tracing_id
);
2473 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2475 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2485 * Ask the consumer to create a channel and get it if successful.
2487 * Called with UST app session lock held.
2489 * Return 0 on success or else a negative value.
2491 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2492 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2493 int bitness
, struct ust_registry_session
*registry
,
2494 uint64_t trace_archive_id
)
2497 unsigned int nb_fd
= 0;
2498 struct consumer_socket
*socket
;
2506 health_code_update();
2508 /* Get the right consumer socket for the application. */
2509 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2515 health_code_update();
2517 /* Need one fd for the channel. */
2518 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2520 ERR("Exhausted number of available FD upon create channel");
2525 * Ask consumer to create channel. The consumer will return the number of
2526 * stream we have to expect.
2528 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2529 registry
, trace_archive_id
);
2535 * Compute the number of fd needed before receiving them. It must be 2 per
2536 * stream (2 being the default value here).
2538 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2540 /* Reserve the amount of file descriptor we need. */
2541 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2543 ERR("Exhausted number of available FD upon create channel");
2544 goto error_fd_get_stream
;
2547 health_code_update();
2550 * Now get the channel from the consumer. This call wil populate the stream
2551 * list of that channel and set the ust objects.
2553 if (usess
->consumer
->enabled
) {
2554 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2564 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2565 error_fd_get_stream
:
2567 * Initiate a destroy channel on the consumer since we had an error
2568 * handling it on our side. The return value is of no importance since we
2569 * already have a ret value set by the previous error that we need to
2572 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2574 lttng_fd_put(LTTNG_FD_APPS
, 1);
2576 health_code_update();
2582 * Duplicate the ust data object of the ust app stream and save it in the
2583 * buffer registry stream.
2585 * Return 0 on success or else a negative value.
2587 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2588 struct ust_app_stream
*stream
)
2595 /* Reserve the amount of file descriptor we need. */
2596 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2598 ERR("Exhausted number of available FD upon duplicate stream");
2602 /* Duplicate object for stream once the original is in the registry. */
2603 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2604 reg_stream
->obj
.ust
);
2606 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2607 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2608 lttng_fd_put(LTTNG_FD_APPS
, 2);
2611 stream
->handle
= stream
->obj
->handle
;
2618 * Duplicate the ust data object of the ust app. channel and save it in the
2619 * buffer registry channel.
2621 * Return 0 on success or else a negative value.
2623 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2624 struct ust_app_channel
*ua_chan
)
2631 /* Need two fds for the channel. */
2632 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2634 ERR("Exhausted number of available FD upon duplicate channel");
2638 /* Duplicate object for stream once the original is in the registry. */
2639 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2641 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2642 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2645 ua_chan
->handle
= ua_chan
->obj
->handle
;
2650 lttng_fd_put(LTTNG_FD_APPS
, 1);
2656 * For a given channel buffer registry, setup all streams of the given ust
2657 * application channel.
2659 * Return 0 on success or else a negative value.
2661 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2662 struct ust_app_channel
*ua_chan
,
2663 struct ust_app
*app
)
2666 struct ust_app_stream
*stream
, *stmp
;
2671 DBG2("UST app setup buffer registry stream");
2673 /* Send all streams to application. */
2674 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2675 struct buffer_reg_stream
*reg_stream
;
2677 ret
= buffer_reg_stream_create(®_stream
);
2683 * Keep original pointer and nullify it in the stream so the delete
2684 * stream call does not release the object.
2686 reg_stream
->obj
.ust
= stream
->obj
;
2688 buffer_reg_stream_add(reg_stream
, reg_chan
);
2690 /* We don't need the streams anymore. */
2691 cds_list_del(&stream
->list
);
2692 delete_ust_app_stream(-1, stream
, app
);
2700 * Create a buffer registry channel for the given session registry and
2701 * application channel object. If regp pointer is valid, it's set with the
2702 * created object. Important, the created object is NOT added to the session
2703 * registry hash table.
2705 * Return 0 on success else a negative value.
2707 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2708 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2711 struct buffer_reg_channel
*reg_chan
= NULL
;
2716 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2718 /* Create buffer registry channel. */
2719 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2724 reg_chan
->consumer_key
= ua_chan
->key
;
2725 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2726 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2728 /* Create and add a channel registry to session. */
2729 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2730 ua_chan
->tracing_channel_id
);
2734 buffer_reg_channel_add(reg_sess
, reg_chan
);
2743 /* Safe because the registry channel object was not added to any HT. */
2744 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2750 * Setup buffer registry channel for the given session registry and application
2751 * channel object. If regp pointer is valid, it's set with the created object.
2753 * Return 0 on success else a negative value.
2755 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2756 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2757 struct ust_app
*app
)
2764 assert(ua_chan
->obj
);
2766 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2768 /* Setup all streams for the registry. */
2769 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2774 reg_chan
->obj
.ust
= ua_chan
->obj
;
2775 ua_chan
->obj
= NULL
;
2780 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2781 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2786 * Send buffer registry channel to the application.
2788 * Return 0 on success else a negative value.
2790 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2791 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2792 struct ust_app_channel
*ua_chan
)
2795 struct buffer_reg_stream
*reg_stream
;
2802 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2804 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2809 /* Send channel to the application. */
2810 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2811 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2812 ret
= -ENOTCONN
; /* Caused by app exiting. */
2814 } else if (ret
< 0) {
2818 health_code_update();
2820 /* Send all streams to application. */
2821 pthread_mutex_lock(®_chan
->stream_list_lock
);
2822 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2823 struct ust_app_stream stream
;
2825 ret
= duplicate_stream_object(reg_stream
, &stream
);
2827 goto error_stream_unlock
;
2830 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2832 (void) release_ust_app_stream(-1, &stream
, app
);
2833 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2834 ret
= -ENOTCONN
; /* Caused by app exiting. */
2836 goto error_stream_unlock
;
2840 * The return value is not important here. This function will output an
2843 (void) release_ust_app_stream(-1, &stream
, app
);
2845 ua_chan
->is_sent
= 1;
2847 error_stream_unlock
:
2848 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2854 * Create and send to the application the created buffers with per UID buffers.
2856 * This MUST be called with a RCU read side lock acquired.
2857 * The session list lock and the session's lock must be acquired.
2859 * Return 0 on success else a negative value.
2861 static int create_channel_per_uid(struct ust_app
*app
,
2862 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2863 struct ust_app_channel
*ua_chan
)
2866 struct buffer_reg_uid
*reg_uid
;
2867 struct buffer_reg_channel
*reg_chan
;
2868 struct ltt_session
*session
;
2869 enum lttng_error_code notification_ret
;
2870 struct ust_registry_channel
*chan_reg
;
2877 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2879 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2881 * The session creation handles the creation of this global registry
2882 * object. If none can be find, there is a code flow problem or a
2887 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2893 /* Create the buffer registry channel object. */
2894 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2896 ERR("Error creating the UST channel \"%s\" registry instance",
2901 session
= session_find_by_id(ua_sess
->tracing_id
);
2903 assert(pthread_mutex_trylock(&session
->lock
));
2904 assert(session_trylock_list());
2907 * Create the buffers on the consumer side. This call populates the
2908 * ust app channel object with all streams and data object.
2910 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2911 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
2912 session
->current_archive_id
);
2914 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2918 * Let's remove the previously created buffer registry channel so
2919 * it's not visible anymore in the session registry.
2921 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2922 ua_chan
->tracing_channel_id
, false);
2923 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2924 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2929 * Setup the streams and add it to the session registry.
2931 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2932 ua_chan
, reg_chan
, app
);
2934 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
2938 /* Notify the notification subsystem of the channel's creation. */
2939 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2940 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2941 ua_chan
->tracing_channel_id
);
2943 chan_reg
->consumer_key
= ua_chan
->key
;
2945 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2947 notification_ret
= notification_thread_command_add_channel(
2948 notification_thread_handle
, session
->name
,
2949 ua_sess
->euid
, ua_sess
->egid
,
2953 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2954 if (notification_ret
!= LTTNG_OK
) {
2955 ret
= - (int) notification_ret
;
2956 ERR("Failed to add channel to notification thread");
2961 /* Send buffers to the application. */
2962 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2964 if (ret
!= -ENOTCONN
) {
2965 ERR("Error sending channel to application");
2975 * Create and send to the application the created buffers with per PID buffers.
2977 * Called with UST app session lock held.
2978 * The session list lock and the session's lock must be acquired.
2980 * Return 0 on success else a negative value.
2982 static int create_channel_per_pid(struct ust_app
*app
,
2983 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2984 struct ust_app_channel
*ua_chan
)
2987 struct ust_registry_session
*registry
;
2988 enum lttng_error_code cmd_ret
;
2989 struct ltt_session
*session
;
2990 uint64_t chan_reg_key
;
2991 struct ust_registry_channel
*chan_reg
;
2998 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3002 registry
= get_session_registry(ua_sess
);
3003 /* The UST app session lock is held, registry shall not be null. */
3006 /* Create and add a new channel registry to session. */
3007 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3009 ERR("Error creating the UST channel \"%s\" registry instance",
3014 session
= session_find_by_id(ua_sess
->tracing_id
);
3017 assert(pthread_mutex_trylock(&session
->lock
));
3018 assert(session_trylock_list());
3020 /* Create and get channel on the consumer side. */
3021 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3022 app
->bits_per_long
, registry
,
3023 session
->current_archive_id
);
3025 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3027 goto error_remove_from_registry
;
3030 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3032 if (ret
!= -ENOTCONN
) {
3033 ERR("Error sending channel to application");
3035 goto error_remove_from_registry
;
3038 chan_reg_key
= ua_chan
->key
;
3039 pthread_mutex_lock(®istry
->lock
);
3040 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3042 chan_reg
->consumer_key
= ua_chan
->key
;
3043 pthread_mutex_unlock(®istry
->lock
);
3045 cmd_ret
= notification_thread_command_add_channel(
3046 notification_thread_handle
, session
->name
,
3047 ua_sess
->euid
, ua_sess
->egid
,
3051 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3052 if (cmd_ret
!= LTTNG_OK
) {
3053 ret
= - (int) cmd_ret
;
3054 ERR("Failed to add channel to notification thread");
3055 goto error_remove_from_registry
;
3058 error_remove_from_registry
:
3060 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3068 * From an already allocated ust app channel, create the channel buffers if
3069 * need and send it to the application. This MUST be called with a RCU read
3070 * side lock acquired.
3072 * Called with UST app session lock held.
3074 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3075 * the application exited concurrently.
3077 static int do_create_channel(struct ust_app
*app
,
3078 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3079 struct ust_app_channel
*ua_chan
)
3088 /* Handle buffer type before sending the channel to the application. */
3089 switch (usess
->buffer_type
) {
3090 case LTTNG_BUFFER_PER_UID
:
3092 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3098 case LTTNG_BUFFER_PER_PID
:
3100 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3112 /* Initialize ust objd object using the received handle and add it. */
3113 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3114 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3116 /* If channel is not enabled, disable it on the tracer */
3117 if (!ua_chan
->enabled
) {
3118 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3129 * Create UST app channel and create it on the tracer. Set ua_chanp of the
3130 * newly created channel if not NULL.
3132 * Called with UST app session lock and RCU read-side lock held.
3134 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3135 * the application exited concurrently.
3137 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
3138 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
3139 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3140 struct ust_app_channel
**ua_chanp
)
3143 struct lttng_ht_iter iter
;
3144 struct lttng_ht_node_str
*ua_chan_node
;
3145 struct ust_app_channel
*ua_chan
;
3147 /* Lookup channel in the ust app session */
3148 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3149 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3150 if (ua_chan_node
!= NULL
) {
3151 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3155 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3156 if (ua_chan
== NULL
) {
3157 /* Only malloc can fail here */
3161 shadow_copy_channel(ua_chan
, uchan
);
3163 /* Set channel type. */
3164 ua_chan
->attr
.type
= type
;
3166 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
3171 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
3174 /* Only add the channel if successful on the tracer side. */
3175 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3178 *ua_chanp
= ua_chan
;
3181 /* Everything went well. */
3185 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
3191 * Create UST app event and create it on the tracer side.
3193 * Called with ust app session mutex held.
3196 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3197 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3198 struct ust_app
*app
)
3201 struct ust_app_event
*ua_event
;
3203 /* Get event node */
3204 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3205 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3206 if (ua_event
!= NULL
) {
3211 /* Does not exist so create one */
3212 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3213 if (ua_event
== NULL
) {
3214 /* Only malloc can failed so something is really wrong */
3218 shadow_copy_event(ua_event
, uevent
);
3220 /* Create it on the tracer side */
3221 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3223 /* Not found previously means that it does not exist on the tracer */
3224 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
3228 add_unique_ust_app_event(ua_chan
, ua_event
);
3230 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3237 /* Valid. Calling here is already in a read side lock */
3238 delete_ust_app_event(-1, ua_event
, app
);
3243 * Create UST metadata and open it on the tracer side.
3245 * Called with UST app session lock held and RCU read side lock.
3247 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3248 struct ust_app
*app
, struct consumer_output
*consumer
)
3251 struct ust_app_channel
*metadata
;
3252 struct consumer_socket
*socket
;
3253 struct ust_registry_session
*registry
;
3254 struct ltt_session
*session
;
3260 registry
= get_session_registry(ua_sess
);
3261 /* The UST app session is held registry shall not be null. */
3264 pthread_mutex_lock(®istry
->lock
);
3266 /* Metadata already exists for this registry or it was closed previously */
3267 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3272 /* Allocate UST metadata */
3273 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3275 /* malloc() failed */
3280 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3282 /* Need one fd for the channel. */
3283 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3285 ERR("Exhausted number of available FD upon create metadata");
3289 /* Get the right consumer socket for the application. */
3290 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3293 goto error_consumer
;
3297 * Keep metadata key so we can identify it on the consumer side. Assign it
3298 * to the registry *before* we ask the consumer so we avoid the race of the
3299 * consumer requesting the metadata and the ask_channel call on our side
3300 * did not returned yet.
3302 registry
->metadata_key
= metadata
->key
;
3304 session
= session_find_by_id(ua_sess
->tracing_id
);
3307 assert(pthread_mutex_trylock(&session
->lock
));
3308 assert(session_trylock_list());
3311 * Ask the metadata channel creation to the consumer. The metadata object
3312 * will be created by the consumer and kept their. However, the stream is
3313 * never added or monitored until we do a first push metadata to the
3316 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3317 registry
, session
->current_archive_id
);
3319 /* Nullify the metadata key so we don't try to close it later on. */
3320 registry
->metadata_key
= 0;
3321 goto error_consumer
;
3325 * The setup command will make the metadata stream be sent to the relayd,
3326 * if applicable, and the thread managing the metadatas. This is important
3327 * because after this point, if an error occurs, the only way the stream
3328 * can be deleted is to be monitored in the consumer.
3330 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3332 /* Nullify the metadata key so we don't try to close it later on. */
3333 registry
->metadata_key
= 0;
3334 goto error_consumer
;
3337 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3338 metadata
->key
, app
->pid
);
3341 lttng_fd_put(LTTNG_FD_APPS
, 1);
3342 delete_ust_app_channel(-1, metadata
, app
);
3344 pthread_mutex_unlock(®istry
->lock
);
3349 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3350 * acquired before calling this function.
3352 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3354 struct ust_app
*app
= NULL
;
3355 struct lttng_ht_node_ulong
*node
;
3356 struct lttng_ht_iter iter
;
3358 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3359 node
= lttng_ht_iter_get_node_ulong(&iter
);
3361 DBG2("UST app no found with pid %d", pid
);
3365 DBG2("Found UST app by pid %d", pid
);
3367 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3374 * Allocate and init an UST app object using the registration information and
3375 * the command socket. This is called when the command socket connects to the
3378 * The object is returned on success or else NULL.
3380 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3382 struct ust_app
*lta
= NULL
;
3387 DBG3("UST app creating application for socket %d", sock
);
3389 if ((msg
->bits_per_long
== 64 &&
3390 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3391 || (msg
->bits_per_long
== 32 &&
3392 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3393 ERR("Registration failed: application \"%s\" (pid: %d) has "
3394 "%d-bit long, but no consumerd for this size is available.\n",
3395 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3399 lta
= zmalloc(sizeof(struct ust_app
));
3405 lta
->ppid
= msg
->ppid
;
3406 lta
->uid
= msg
->uid
;
3407 lta
->gid
= msg
->gid
;
3409 lta
->bits_per_long
= msg
->bits_per_long
;
3410 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3411 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3412 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3413 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3414 lta
->long_alignment
= msg
->long_alignment
;
3415 lta
->byte_order
= msg
->byte_order
;
3417 lta
->v_major
= msg
->major
;
3418 lta
->v_minor
= msg
->minor
;
3419 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3420 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3421 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3422 lta
->notify_sock
= -1;
3424 /* Copy name and make sure it's NULL terminated. */
3425 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3426 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3429 * Before this can be called, when receiving the registration information,
3430 * the application compatibility is checked. So, at this point, the
3431 * application can work with this session daemon.
3433 lta
->compatible
= 1;
3435 lta
->pid
= msg
->pid
;
3436 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3438 pthread_mutex_init(<a
->sock_lock
, NULL
);
3439 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3441 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3447 * For a given application object, add it to every hash table.
3449 void ust_app_add(struct ust_app
*app
)
3452 assert(app
->notify_sock
>= 0);
3457 * On a re-registration, we want to kick out the previous registration of
3460 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3463 * The socket _should_ be unique until _we_ call close. So, a add_unique
3464 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3465 * already in the table.
3467 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3469 /* Add application to the notify socket hash table. */
3470 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3471 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3473 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3474 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3475 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3482 * Set the application version into the object.
3484 * Return 0 on success else a negative value either an errno code or a
3485 * LTTng-UST error code.
3487 int ust_app_version(struct ust_app
*app
)
3493 pthread_mutex_lock(&app
->sock_lock
);
3494 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3495 pthread_mutex_unlock(&app
->sock_lock
);
3497 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3498 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3500 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3508 * Unregister app by removing it from the global traceable app list and freeing
3511 * The socket is already closed at this point so no close to sock.
3513 void ust_app_unregister(int sock
)
3515 struct ust_app
*lta
;
3516 struct lttng_ht_node_ulong
*node
;
3517 struct lttng_ht_iter ust_app_sock_iter
;
3518 struct lttng_ht_iter iter
;
3519 struct ust_app_session
*ua_sess
;
3524 /* Get the node reference for a call_rcu */
3525 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3526 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3529 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3530 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3533 * For per-PID buffers, perform "push metadata" and flush all
3534 * application streams before removing app from hash tables,
3535 * ensuring proper behavior of data_pending check.
3536 * Remove sessions so they are not visible during deletion.
3538 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3540 struct ust_registry_session
*registry
;
3542 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3544 /* The session was already removed so scheduled for teardown. */
3548 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3549 (void) ust_app_flush_app_session(lta
, ua_sess
);
3553 * Add session to list for teardown. This is safe since at this point we
3554 * are the only one using this list.
3556 pthread_mutex_lock(&ua_sess
->lock
);
3558 if (ua_sess
->deleted
) {
3559 pthread_mutex_unlock(&ua_sess
->lock
);
3564 * Normally, this is done in the delete session process which is
3565 * executed in the call rcu below. However, upon registration we can't
3566 * afford to wait for the grace period before pushing data or else the
3567 * data pending feature can race between the unregistration and stop
3568 * command where the data pending command is sent *before* the grace
3571 * The close metadata below nullifies the metadata pointer in the
3572 * session so the delete session will NOT push/close a second time.
3574 registry
= get_session_registry(ua_sess
);
3576 /* Push metadata for application before freeing the application. */
3577 (void) push_metadata(registry
, ua_sess
->consumer
);
3580 * Don't ask to close metadata for global per UID buffers. Close
3581 * metadata only on destroy trace session in this case. Also, the
3582 * previous push metadata could have flag the metadata registry to
3583 * close so don't send a close command if closed.
3585 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3586 /* And ask to close it for this session registry. */
3587 (void) close_metadata(registry
, ua_sess
->consumer
);
3590 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3592 pthread_mutex_unlock(&ua_sess
->lock
);
3595 /* Remove application from PID hash table */
3596 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3600 * Remove application from notify hash table. The thread handling the
3601 * notify socket could have deleted the node so ignore on error because
3602 * either way it's valid. The close of that socket is handled by the
3603 * apps_notify_thread.
3605 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3606 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3609 * Ignore return value since the node might have been removed before by an
3610 * add replace during app registration because the PID can be reassigned by
3613 iter
.iter
.node
= <a
->pid_n
.node
;
3614 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3616 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3621 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3628 * Fill events array with all events name of all registered apps.
3630 int ust_app_list_events(struct lttng_event
**events
)
3633 size_t nbmem
, count
= 0;
3634 struct lttng_ht_iter iter
;
3635 struct ust_app
*app
;
3636 struct lttng_event
*tmp_event
;
3638 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3639 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3640 if (tmp_event
== NULL
) {
3641 PERROR("zmalloc ust app events");
3648 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3649 struct lttng_ust_tracepoint_iter uiter
;
3651 health_code_update();
3653 if (!app
->compatible
) {
3655 * TODO: In time, we should notice the caller of this error by
3656 * telling him that this is a version error.
3660 pthread_mutex_lock(&app
->sock_lock
);
3661 handle
= ustctl_tracepoint_list(app
->sock
);
3663 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3664 ERR("UST app list events getting handle failed for app pid %d",
3667 pthread_mutex_unlock(&app
->sock_lock
);
3671 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3672 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3673 /* Handle ustctl error. */
3677 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3678 ERR("UST app tp list get failed for app %d with ret %d",
3681 DBG3("UST app tp list get failed. Application is dead");
3683 * This is normal behavior, an application can die during the
3684 * creation process. Don't report an error so the execution can
3685 * continue normally. Continue normal execution.
3690 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3691 if (release_ret
< 0 &&
3692 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3693 release_ret
!= -EPIPE
) {
3694 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3696 pthread_mutex_unlock(&app
->sock_lock
);
3700 health_code_update();
3701 if (count
>= nbmem
) {
3702 /* In case the realloc fails, we free the memory */
3703 struct lttng_event
*new_tmp_event
;
3706 new_nbmem
= nbmem
<< 1;
3707 DBG2("Reallocating event list from %zu to %zu entries",
3709 new_tmp_event
= realloc(tmp_event
,
3710 new_nbmem
* sizeof(struct lttng_event
));
3711 if (new_tmp_event
== NULL
) {
3714 PERROR("realloc ust app events");
3717 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3718 if (release_ret
< 0 &&
3719 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3720 release_ret
!= -EPIPE
) {
3721 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3723 pthread_mutex_unlock(&app
->sock_lock
);
3726 /* Zero the new memory */
3727 memset(new_tmp_event
+ nbmem
, 0,
3728 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3730 tmp_event
= new_tmp_event
;
3732 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3733 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3734 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3735 tmp_event
[count
].pid
= app
->pid
;
3736 tmp_event
[count
].enabled
= -1;
3739 ret
= ustctl_release_handle(app
->sock
, handle
);
3740 pthread_mutex_unlock(&app
->sock_lock
);
3741 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3742 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3747 *events
= tmp_event
;
3749 DBG2("UST app list events done (%zu events)", count
);
3754 health_code_update();
3759 * Fill events array with all events name of all registered apps.
3761 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3764 size_t nbmem
, count
= 0;
3765 struct lttng_ht_iter iter
;
3766 struct ust_app
*app
;
3767 struct lttng_event_field
*tmp_event
;
3769 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3770 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3771 if (tmp_event
== NULL
) {
3772 PERROR("zmalloc ust app event fields");
3779 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3780 struct lttng_ust_field_iter uiter
;
3782 health_code_update();
3784 if (!app
->compatible
) {
3786 * TODO: In time, we should notice the caller of this error by
3787 * telling him that this is a version error.
3791 pthread_mutex_lock(&app
->sock_lock
);
3792 handle
= ustctl_tracepoint_field_list(app
->sock
);
3794 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3795 ERR("UST app list field getting handle failed for app pid %d",
3798 pthread_mutex_unlock(&app
->sock_lock
);
3802 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3803 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3804 /* Handle ustctl error. */
3808 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3809 ERR("UST app tp list field failed for app %d with ret %d",
3812 DBG3("UST app tp list field failed. Application is dead");
3814 * This is normal behavior, an application can die during the
3815 * creation process. Don't report an error so the execution can
3816 * continue normally. Reset list and count for next app.
3821 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3822 pthread_mutex_unlock(&app
->sock_lock
);
3823 if (release_ret
< 0 &&
3824 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3825 release_ret
!= -EPIPE
) {
3826 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3831 health_code_update();
3832 if (count
>= nbmem
) {
3833 /* In case the realloc fails, we free the memory */
3834 struct lttng_event_field
*new_tmp_event
;
3837 new_nbmem
= nbmem
<< 1;
3838 DBG2("Reallocating event field list from %zu to %zu entries",
3840 new_tmp_event
= realloc(tmp_event
,
3841 new_nbmem
* sizeof(struct lttng_event_field
));
3842 if (new_tmp_event
== NULL
) {
3845 PERROR("realloc ust app event fields");
3848 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3849 pthread_mutex_unlock(&app
->sock_lock
);
3851 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3852 release_ret
!= -EPIPE
) {
3853 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3857 /* Zero the new memory */
3858 memset(new_tmp_event
+ nbmem
, 0,
3859 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3861 tmp_event
= new_tmp_event
;
3864 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3865 /* Mapping between these enums matches 1 to 1. */
3866 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3867 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3869 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3870 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3871 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3872 tmp_event
[count
].event
.pid
= app
->pid
;
3873 tmp_event
[count
].event
.enabled
= -1;
3876 ret
= ustctl_release_handle(app
->sock
, handle
);
3877 pthread_mutex_unlock(&app
->sock_lock
);
3879 ret
!= -LTTNG_UST_ERR_EXITING
&&
3881 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3886 *fields
= tmp_event
;
3888 DBG2("UST app list event fields done (%zu events)", count
);
3893 health_code_update();
3898 * Free and clean all traceable apps of the global list.
3900 * Should _NOT_ be called with RCU read-side lock held.
3902 void ust_app_clean_list(void)
3905 struct ust_app
*app
;
3906 struct lttng_ht_iter iter
;
3908 DBG2("UST app cleaning registered apps hash table");
3913 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3914 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3916 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3920 /* Cleanup socket hash table */
3921 if (ust_app_ht_by_sock
) {
3922 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3924 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3929 /* Cleanup notify socket hash table */
3930 if (ust_app_ht_by_notify_sock
) {
3931 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3932 notify_sock_n
.node
) {
3933 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3939 /* Destroy is done only when the ht is empty */
3941 ht_cleanup_push(ust_app_ht
);
3943 if (ust_app_ht_by_sock
) {
3944 ht_cleanup_push(ust_app_ht_by_sock
);
3946 if (ust_app_ht_by_notify_sock
) {
3947 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3952 * Init UST app hash table.
3954 int ust_app_ht_alloc(void)
3956 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3960 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3961 if (!ust_app_ht_by_sock
) {
3964 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3965 if (!ust_app_ht_by_notify_sock
) {
3972 * For a specific UST session, disable the channel for all registered apps.
3974 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3975 struct ltt_ust_channel
*uchan
)
3978 struct lttng_ht_iter iter
;
3979 struct lttng_ht_node_str
*ua_chan_node
;
3980 struct ust_app
*app
;
3981 struct ust_app_session
*ua_sess
;
3982 struct ust_app_channel
*ua_chan
;
3984 if (usess
== NULL
|| uchan
== NULL
) {
3985 ERR("Disabling UST global channel with NULL values");
3990 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3991 uchan
->name
, usess
->id
);
3995 /* For every registered applications */
3996 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3997 struct lttng_ht_iter uiter
;
3998 if (!app
->compatible
) {
4000 * TODO: In time, we should notice the caller of this error by
4001 * telling him that this is a version error.
4005 ua_sess
= lookup_session_by_app(usess
, app
);
4006 if (ua_sess
== NULL
) {
4011 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4012 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4013 /* If the session if found for the app, the channel must be there */
4014 assert(ua_chan_node
);
4016 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4017 /* The channel must not be already disabled */
4018 assert(ua_chan
->enabled
== 1);
4020 /* Disable channel onto application */
4021 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4023 /* XXX: We might want to report this error at some point... */
4035 * For a specific UST session, enable the channel for all registered apps.
4037 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4038 struct ltt_ust_channel
*uchan
)
4041 struct lttng_ht_iter iter
;
4042 struct ust_app
*app
;
4043 struct ust_app_session
*ua_sess
;
4045 if (usess
== NULL
|| uchan
== NULL
) {
4046 ERR("Adding UST global channel to NULL values");
4051 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4052 uchan
->name
, usess
->id
);
4056 /* For every registered applications */
4057 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4058 if (!app
->compatible
) {
4060 * TODO: In time, we should notice the caller of this error by
4061 * telling him that this is a version error.
4065 ua_sess
= lookup_session_by_app(usess
, app
);
4066 if (ua_sess
== NULL
) {
4070 /* Enable channel onto application */
4071 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4073 /* XXX: We might want to report this error at some point... */
4085 * Disable an event in a channel and for a specific session.
4087 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4088 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4091 struct lttng_ht_iter iter
, uiter
;
4092 struct lttng_ht_node_str
*ua_chan_node
;
4093 struct ust_app
*app
;
4094 struct ust_app_session
*ua_sess
;
4095 struct ust_app_channel
*ua_chan
;
4096 struct ust_app_event
*ua_event
;
4098 DBG("UST app disabling event %s for all apps in channel "
4099 "%s for session id %" PRIu64
,
4100 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4104 /* For all registered applications */
4105 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4106 if (!app
->compatible
) {
4108 * TODO: In time, we should notice the caller of this error by
4109 * telling him that this is a version error.
4113 ua_sess
= lookup_session_by_app(usess
, app
);
4114 if (ua_sess
== NULL
) {
4119 /* Lookup channel in the ust app session */
4120 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4121 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4122 if (ua_chan_node
== NULL
) {
4123 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4124 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4127 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4129 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4130 uevent
->filter
, uevent
->attr
.loglevel
,
4132 if (ua_event
== NULL
) {
4133 DBG2("Event %s not found in channel %s for app pid %d."
4134 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4138 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4140 /* XXX: Report error someday... */
4151 * For a specific UST session, create the channel for all registered apps.
4153 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
4154 struct ltt_ust_channel
*uchan
)
4156 int ret
= 0, created
;
4157 struct lttng_ht_iter iter
;
4158 struct ust_app
*app
;
4159 struct ust_app_session
*ua_sess
= NULL
;
4161 /* Very wrong code flow */
4165 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
4166 uchan
->name
, usess
->id
);
4170 /* For every registered applications */
4171 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4172 if (!app
->compatible
) {
4174 * TODO: In time, we should notice the caller of this error by
4175 * telling him that this is a version error.
4179 if (!trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4185 * Create session on the tracer side and add it to app session HT. Note
4186 * that if session exist, it will simply return a pointer to the ust
4189 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, &created
);
4194 * The application's socket is not valid. Either a bad socket
4195 * or a timeout on it. We can't inform the caller that for a
4196 * specific app, the session failed so lets continue here.
4198 ret
= 0; /* Not an error. */
4202 goto error_rcu_unlock
;
4207 pthread_mutex_lock(&ua_sess
->lock
);
4209 if (ua_sess
->deleted
) {
4210 pthread_mutex_unlock(&ua_sess
->lock
);
4214 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4215 sizeof(uchan
->name
))) {
4216 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
4219 /* Create channel onto application. We don't need the chan ref. */
4220 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
4221 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
4223 pthread_mutex_unlock(&ua_sess
->lock
);
4225 /* Cleanup the created session if it's the case. */
4227 destroy_app_session(app
, ua_sess
);
4232 * The application's socket is not valid. Either a bad socket
4233 * or a timeout on it. We can't inform the caller that for a
4234 * specific app, the session failed so lets continue here.
4236 ret
= 0; /* Not an error. */
4240 goto error_rcu_unlock
;
4251 * Enable event for a specific session and channel on the tracer.
4253 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4254 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4257 struct lttng_ht_iter iter
, uiter
;
4258 struct lttng_ht_node_str
*ua_chan_node
;
4259 struct ust_app
*app
;
4260 struct ust_app_session
*ua_sess
;
4261 struct ust_app_channel
*ua_chan
;
4262 struct ust_app_event
*ua_event
;
4264 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4265 uevent
->attr
.name
, usess
->id
);
4268 * NOTE: At this point, this function is called only if the session and
4269 * channel passed are already created for all apps. and enabled on the
4275 /* For all registered applications */
4276 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4277 if (!app
->compatible
) {
4279 * TODO: In time, we should notice the caller of this error by
4280 * telling him that this is a version error.
4284 ua_sess
= lookup_session_by_app(usess
, app
);
4286 /* The application has problem or is probably dead. */
4290 pthread_mutex_lock(&ua_sess
->lock
);
4292 if (ua_sess
->deleted
) {
4293 pthread_mutex_unlock(&ua_sess
->lock
);
4297 /* Lookup channel in the ust app session */
4298 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4299 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4301 * It is possible that the channel cannot be found is
4302 * the channel/event creation occurs concurrently with
4303 * an application exit.
4305 if (!ua_chan_node
) {
4306 pthread_mutex_unlock(&ua_sess
->lock
);
4310 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4312 /* Get event node */
4313 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4314 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4315 if (ua_event
== NULL
) {
4316 DBG3("UST app enable event %s not found for app PID %d."
4317 "Skipping app", uevent
->attr
.name
, app
->pid
);
4321 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4323 pthread_mutex_unlock(&ua_sess
->lock
);
4327 pthread_mutex_unlock(&ua_sess
->lock
);
4336 * For a specific existing UST session and UST channel, creates the event for
4337 * all registered apps.
4339 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4340 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4343 struct lttng_ht_iter iter
, uiter
;
4344 struct lttng_ht_node_str
*ua_chan_node
;
4345 struct ust_app
*app
;
4346 struct ust_app_session
*ua_sess
;
4347 struct ust_app_channel
*ua_chan
;
4349 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4350 uevent
->attr
.name
, usess
->id
);
4354 /* For all registered applications */
4355 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4356 if (!app
->compatible
) {
4358 * TODO: In time, we should notice the caller of this error by
4359 * telling him that this is a version error.
4363 ua_sess
= lookup_session_by_app(usess
, app
);
4365 /* The application has problem or is probably dead. */
4369 pthread_mutex_lock(&ua_sess
->lock
);
4371 if (ua_sess
->deleted
) {
4372 pthread_mutex_unlock(&ua_sess
->lock
);
4376 /* Lookup channel in the ust app session */
4377 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4378 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4379 /* If the channel is not found, there is a code flow error */
4380 assert(ua_chan_node
);
4382 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4384 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4385 pthread_mutex_unlock(&ua_sess
->lock
);
4387 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4388 /* Possible value at this point: -ENOMEM. If so, we stop! */
4391 DBG2("UST app event %s already exist on app PID %d",
4392 uevent
->attr
.name
, app
->pid
);
4403 * Start tracing for a specific UST session and app.
4405 * Called with UST app session lock held.
4409 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4412 struct ust_app_session
*ua_sess
;
4414 DBG("Starting tracing for ust app pid %d", app
->pid
);
4418 if (!app
->compatible
) {
4422 ua_sess
= lookup_session_by_app(usess
, app
);
4423 if (ua_sess
== NULL
) {
4424 /* The session is in teardown process. Ignore and continue. */
4428 pthread_mutex_lock(&ua_sess
->lock
);
4430 if (ua_sess
->deleted
) {
4431 pthread_mutex_unlock(&ua_sess
->lock
);
4435 /* Upon restart, we skip the setup, already done */
4436 if (ua_sess
->started
) {
4440 /* Create directories if consumer is LOCAL and has a path defined. */
4441 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
4442 usess
->consumer
->dst
.session_root_path
[0] != '\0') {
4445 tmp_path
= zmalloc(LTTNG_PATH_MAX
);
4447 ERR("Alloc tmp_path");
4450 ret
= snprintf(tmp_path
, LTTNG_PATH_MAX
, "%s%s%s",
4451 usess
->consumer
->dst
.session_root_path
,
4452 usess
->consumer
->chunk_path
,
4453 usess
->consumer
->subdir
);
4454 if (ret
>= LTTNG_PATH_MAX
) {
4455 ERR("Local destination path exceeds the maximal allowed length of %i bytes (needs %i bytes) with path = \"%s%s%s\"",
4456 LTTNG_PATH_MAX
, ret
,
4457 usess
->consumer
->dst
.session_root_path
,
4458 usess
->consumer
->chunk_path
,
4459 usess
->consumer
->subdir
);
4464 DBG("Creating directory path for local tracing: \"%s\"",
4466 ret
= run_as_mkdir_recursive(tmp_path
, S_IRWXU
| S_IRWXG
,
4467 ua_sess
->euid
, ua_sess
->egid
);
4470 if (errno
!= EEXIST
) {
4471 ERR("Trace directory creation error");
4478 * Create the metadata for the application. This returns gracefully if a
4479 * metadata was already set for the session.
4481 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4486 health_code_update();
4489 /* This start the UST tracing */
4490 pthread_mutex_lock(&app
->sock_lock
);
4491 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4492 pthread_mutex_unlock(&app
->sock_lock
);
4494 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4495 ERR("Error starting tracing for app pid: %d (ret: %d)",
4498 DBG("UST app start session failed. Application is dead.");
4500 * This is normal behavior, an application can die during the
4501 * creation process. Don't report an error so the execution can
4502 * continue normally.
4504 pthread_mutex_unlock(&ua_sess
->lock
);
4510 /* Indicate that the session has been started once */
4511 ua_sess
->started
= 1;
4513 pthread_mutex_unlock(&ua_sess
->lock
);
4515 health_code_update();
4517 /* Quiescent wait after starting trace */
4518 pthread_mutex_lock(&app
->sock_lock
);
4519 ret
= ustctl_wait_quiescent(app
->sock
);
4520 pthread_mutex_unlock(&app
->sock_lock
);
4521 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4522 ERR("UST app wait quiescent failed for app pid %d ret %d",
4528 health_code_update();
4532 pthread_mutex_unlock(&ua_sess
->lock
);
4534 health_code_update();
4539 * Stop tracing for a specific UST session and app.
4542 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4545 struct ust_app_session
*ua_sess
;
4546 struct ust_registry_session
*registry
;
4548 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4552 if (!app
->compatible
) {
4553 goto end_no_session
;
4556 ua_sess
= lookup_session_by_app(usess
, app
);
4557 if (ua_sess
== NULL
) {
4558 goto end_no_session
;
4561 pthread_mutex_lock(&ua_sess
->lock
);
4563 if (ua_sess
->deleted
) {
4564 pthread_mutex_unlock(&ua_sess
->lock
);
4565 goto end_no_session
;
4569 * If started = 0, it means that stop trace has been called for a session
4570 * that was never started. It's possible since we can have a fail start
4571 * from either the application manager thread or the command thread. Simply
4572 * indicate that this is a stop error.
4574 if (!ua_sess
->started
) {
4575 goto error_rcu_unlock
;
4578 health_code_update();
4580 /* This inhibits UST tracing */
4581 pthread_mutex_lock(&app
->sock_lock
);
4582 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4583 pthread_mutex_unlock(&app
->sock_lock
);
4585 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4586 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4589 DBG("UST app stop session failed. Application is dead.");
4591 * This is normal behavior, an application can die during the
4592 * creation process. Don't report an error so the execution can
4593 * continue normally.
4597 goto error_rcu_unlock
;
4600 health_code_update();
4602 /* Quiescent wait after stopping trace */
4603 pthread_mutex_lock(&app
->sock_lock
);
4604 ret
= ustctl_wait_quiescent(app
->sock
);
4605 pthread_mutex_unlock(&app
->sock_lock
);
4606 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4607 ERR("UST app wait quiescent failed for app pid %d ret %d",
4611 health_code_update();
4613 registry
= get_session_registry(ua_sess
);
4615 /* The UST app session is held registry shall not be null. */
4618 /* Push metadata for application before freeing the application. */
4619 (void) push_metadata(registry
, ua_sess
->consumer
);
4622 pthread_mutex_unlock(&ua_sess
->lock
);
4625 health_code_update();
4629 pthread_mutex_unlock(&ua_sess
->lock
);
4631 health_code_update();
4636 int ust_app_flush_app_session(struct ust_app
*app
,
4637 struct ust_app_session
*ua_sess
)
4639 int ret
, retval
= 0;
4640 struct lttng_ht_iter iter
;
4641 struct ust_app_channel
*ua_chan
;
4642 struct consumer_socket
*socket
;
4644 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4648 if (!app
->compatible
) {
4649 goto end_not_compatible
;
4652 pthread_mutex_lock(&ua_sess
->lock
);
4654 if (ua_sess
->deleted
) {
4658 health_code_update();
4660 /* Flushing buffers */
4661 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4664 /* Flush buffers and push metadata. */
4665 switch (ua_sess
->buffer_type
) {
4666 case LTTNG_BUFFER_PER_PID
:
4667 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4669 health_code_update();
4670 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4672 ERR("Error flushing consumer channel");
4678 case LTTNG_BUFFER_PER_UID
:
4684 health_code_update();
4687 pthread_mutex_unlock(&ua_sess
->lock
);
4691 health_code_update();
4696 * Flush buffers for all applications for a specific UST session.
4697 * Called with UST session lock held.
4700 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4705 DBG("Flushing session buffers for all ust apps");
4709 /* Flush buffers and push metadata. */
4710 switch (usess
->buffer_type
) {
4711 case LTTNG_BUFFER_PER_UID
:
4713 struct buffer_reg_uid
*reg
;
4714 struct lttng_ht_iter iter
;
4716 /* Flush all per UID buffers associated to that session. */
4717 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4718 struct ust_registry_session
*ust_session_reg
;
4719 struct buffer_reg_channel
*reg_chan
;
4720 struct consumer_socket
*socket
;
4722 /* Get consumer socket to use to push the metadata.*/
4723 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4726 /* Ignore request if no consumer is found for the session. */
4730 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4731 reg_chan
, node
.node
) {
4733 * The following call will print error values so the return
4734 * code is of little importance because whatever happens, we
4735 * have to try them all.
4737 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4740 ust_session_reg
= reg
->registry
->reg
.ust
;
4741 /* Push metadata. */
4742 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4746 case LTTNG_BUFFER_PER_PID
:
4748 struct ust_app_session
*ua_sess
;
4749 struct lttng_ht_iter iter
;
4750 struct ust_app
*app
;
4752 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4753 ua_sess
= lookup_session_by_app(usess
, app
);
4754 if (ua_sess
== NULL
) {
4757 (void) ust_app_flush_app_session(app
, ua_sess
);
4768 health_code_update();
4773 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
4774 struct ust_app_session
*ua_sess
)
4777 struct lttng_ht_iter iter
;
4778 struct ust_app_channel
*ua_chan
;
4779 struct consumer_socket
*socket
;
4781 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
4785 if (!app
->compatible
) {
4786 goto end_not_compatible
;
4789 pthread_mutex_lock(&ua_sess
->lock
);
4791 if (ua_sess
->deleted
) {
4795 health_code_update();
4797 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4800 ERR("Failed to find consumer (%" PRIu32
") socket",
4801 app
->bits_per_long
);
4806 /* Clear quiescent state. */
4807 switch (ua_sess
->buffer_type
) {
4808 case LTTNG_BUFFER_PER_PID
:
4809 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
4810 ua_chan
, node
.node
) {
4811 health_code_update();
4812 ret
= consumer_clear_quiescent_channel(socket
,
4815 ERR("Error clearing quiescent state for consumer channel");
4821 case LTTNG_BUFFER_PER_UID
:
4828 health_code_update();
4831 pthread_mutex_unlock(&ua_sess
->lock
);
4835 health_code_update();
4840 * Clear quiescent state in each stream for all applications for a
4841 * specific UST session.
4842 * Called with UST session lock held.
4845 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
4850 DBG("Clearing stream quiescent state for all ust apps");
4854 switch (usess
->buffer_type
) {
4855 case LTTNG_BUFFER_PER_UID
:
4857 struct lttng_ht_iter iter
;
4858 struct buffer_reg_uid
*reg
;
4861 * Clear quiescent for all per UID buffers associated to
4864 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4865 struct consumer_socket
*socket
;
4866 struct buffer_reg_channel
*reg_chan
;
4868 /* Get associated consumer socket.*/
4869 socket
= consumer_find_socket_by_bitness(
4870 reg
->bits_per_long
, usess
->consumer
);
4873 * Ignore request if no consumer is found for
4879 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
4880 &iter
.iter
, reg_chan
, node
.node
) {
4882 * The following call will print error values so
4883 * the return code is of little importance
4884 * because whatever happens, we have to try them
4887 (void) consumer_clear_quiescent_channel(socket
,
4888 reg_chan
->consumer_key
);
4893 case LTTNG_BUFFER_PER_PID
:
4895 struct ust_app_session
*ua_sess
;
4896 struct lttng_ht_iter iter
;
4897 struct ust_app
*app
;
4899 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
4901 ua_sess
= lookup_session_by_app(usess
, app
);
4902 if (ua_sess
== NULL
) {
4905 (void) ust_app_clear_quiescent_app_session(app
,
4917 health_code_update();
4922 * Destroy a specific UST session in apps.
4924 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4927 struct ust_app_session
*ua_sess
;
4928 struct lttng_ht_iter iter
;
4929 struct lttng_ht_node_u64
*node
;
4931 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4935 if (!app
->compatible
) {
4939 __lookup_session_by_app(usess
, app
, &iter
);
4940 node
= lttng_ht_iter_get_node_u64(&iter
);
4942 /* Session is being or is deleted. */
4945 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4947 health_code_update();
4948 destroy_app_session(app
, ua_sess
);
4950 health_code_update();
4952 /* Quiescent wait after stopping trace */
4953 pthread_mutex_lock(&app
->sock_lock
);
4954 ret
= ustctl_wait_quiescent(app
->sock
);
4955 pthread_mutex_unlock(&app
->sock_lock
);
4956 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4957 ERR("UST app wait quiescent failed for app pid %d ret %d",
4962 health_code_update();
4967 * Start tracing for the UST session.
4969 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4972 struct lttng_ht_iter iter
;
4973 struct ust_app
*app
;
4975 DBG("Starting all UST traces");
4980 * In a start-stop-start use-case, we need to clear the quiescent state
4981 * of each channel set by the prior stop command, thus ensuring that a
4982 * following stop or destroy is sure to grab a timestamp_end near those
4983 * operations, even if the packet is empty.
4985 (void) ust_app_clear_quiescent_session(usess
);
4987 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4988 ret
= ust_app_start_trace(usess
, app
);
4990 /* Continue to next apps even on error */
5001 * Start tracing for the UST session.
5002 * Called with UST session lock held.
5004 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5007 struct lttng_ht_iter iter
;
5008 struct ust_app
*app
;
5010 DBG("Stopping all UST traces");
5014 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5015 ret
= ust_app_stop_trace(usess
, app
);
5017 /* Continue to next apps even on error */
5022 (void) ust_app_flush_session(usess
);
5030 * Destroy app UST session.
5032 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5035 struct lttng_ht_iter iter
;
5036 struct ust_app
*app
;
5038 DBG("Destroy all UST traces");
5042 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5043 ret
= destroy_trace(usess
, app
);
5045 /* Continue to next apps even on error */
5056 void ust_app_global_create(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5059 struct lttng_ht_iter iter
, uiter
;
5060 struct ust_app_session
*ua_sess
= NULL
;
5061 struct ust_app_channel
*ua_chan
;
5062 struct ust_app_event
*ua_event
;
5063 struct ust_app_ctx
*ua_ctx
;
5066 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, &is_created
);
5068 /* Tracer is probably gone or ENOMEM. */
5072 /* App session already created. */
5077 pthread_mutex_lock(&ua_sess
->lock
);
5079 if (ua_sess
->deleted
) {
5080 pthread_mutex_unlock(&ua_sess
->lock
);
5085 * We can iterate safely here over all UST app session since the create ust
5086 * app session above made a shadow copy of the UST global domain from the
5089 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5091 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
5092 if (ret
< 0 && ret
!= -ENOTCONN
) {
5094 * Stop everything. On error, the application
5095 * failed, no more file descriptor are available
5096 * or ENOMEM so stopping here is the only thing
5097 * we can do for now. The only exception is
5098 * -ENOTCONN, which indicates that the application
5105 * Add context using the list so they are enabled in the same order the
5108 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
5109 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
5116 /* For each events */
5117 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
5119 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
5126 pthread_mutex_unlock(&ua_sess
->lock
);
5128 if (usess
->active
) {
5129 ret
= ust_app_start_trace(usess
, app
);
5134 DBG2("UST trace started for app pid %d", app
->pid
);
5137 /* Everything went well at this point. */
5141 pthread_mutex_unlock(&ua_sess
->lock
);
5144 destroy_app_session(app
, ua_sess
);
5150 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5152 struct ust_app_session
*ua_sess
;
5154 ua_sess
= lookup_session_by_app(usess
, app
);
5155 if (ua_sess
== NULL
) {
5158 destroy_app_session(app
, ua_sess
);
5162 * Add channels/events from UST global domain to registered apps at sock.
5164 * Called with session lock held.
5165 * Called with RCU read-side lock held.
5167 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5171 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5172 app
->sock
, usess
->id
);
5174 if (!app
->compatible
) {
5178 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
5179 ust_app_global_create(usess
, app
);
5181 ust_app_global_destroy(usess
, app
);
5186 * Called with session lock held.
5188 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5190 struct lttng_ht_iter iter
;
5191 struct ust_app
*app
;
5194 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5195 ust_app_global_update(usess
, app
);
5201 * Add context to a specific channel for global UST domain.
5203 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5204 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5207 struct lttng_ht_node_str
*ua_chan_node
;
5208 struct lttng_ht_iter iter
, uiter
;
5209 struct ust_app_channel
*ua_chan
= NULL
;
5210 struct ust_app_session
*ua_sess
;
5211 struct ust_app
*app
;
5215 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5216 if (!app
->compatible
) {
5218 * TODO: In time, we should notice the caller of this error by
5219 * telling him that this is a version error.
5223 ua_sess
= lookup_session_by_app(usess
, app
);
5224 if (ua_sess
== NULL
) {
5228 pthread_mutex_lock(&ua_sess
->lock
);
5230 if (ua_sess
->deleted
) {
5231 pthread_mutex_unlock(&ua_sess
->lock
);
5235 /* Lookup channel in the ust app session */
5236 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5237 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5238 if (ua_chan_node
== NULL
) {
5241 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5243 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5248 pthread_mutex_unlock(&ua_sess
->lock
);
5256 * Enable event for a channel from a UST session for a specific PID.
5258 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
5259 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
5262 struct lttng_ht_iter iter
;
5263 struct lttng_ht_node_str
*ua_chan_node
;
5264 struct ust_app
*app
;
5265 struct ust_app_session
*ua_sess
;
5266 struct ust_app_channel
*ua_chan
;
5267 struct ust_app_event
*ua_event
;
5269 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
5273 app
= ust_app_find_by_pid(pid
);
5275 ERR("UST app enable event per PID %d not found", pid
);
5280 if (!app
->compatible
) {
5285 ua_sess
= lookup_session_by_app(usess
, app
);
5287 /* The application has problem or is probably dead. */
5292 pthread_mutex_lock(&ua_sess
->lock
);
5294 if (ua_sess
->deleted
) {
5299 /* Lookup channel in the ust app session */
5300 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
5301 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5302 /* If the channel is not found, there is a code flow error */
5303 assert(ua_chan_node
);
5305 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5307 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5308 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5309 if (ua_event
== NULL
) {
5310 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5315 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
5322 pthread_mutex_unlock(&ua_sess
->lock
);
5329 * Receive registration and populate the given msg structure.
5331 * On success return 0 else a negative value returned by the ustctl call.
5333 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5336 uint32_t pid
, ppid
, uid
, gid
;
5340 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5341 &pid
, &ppid
, &uid
, &gid
,
5342 &msg
->bits_per_long
,
5343 &msg
->uint8_t_alignment
,
5344 &msg
->uint16_t_alignment
,
5345 &msg
->uint32_t_alignment
,
5346 &msg
->uint64_t_alignment
,
5347 &msg
->long_alignment
,
5354 case LTTNG_UST_ERR_EXITING
:
5355 DBG3("UST app recv reg message failed. Application died");
5357 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5358 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5359 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5360 LTTNG_UST_ABI_MINOR_VERSION
);
5363 ERR("UST app recv reg message failed with ret %d", ret
);
5368 msg
->pid
= (pid_t
) pid
;
5369 msg
->ppid
= (pid_t
) ppid
;
5370 msg
->uid
= (uid_t
) uid
;
5371 msg
->gid
= (gid_t
) gid
;
5378 * Return a ust app session object using the application object and the
5379 * session object descriptor has a key. If not found, NULL is returned.
5380 * A RCU read side lock MUST be acquired when calling this function.
5382 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5385 struct lttng_ht_node_ulong
*node
;
5386 struct lttng_ht_iter iter
;
5387 struct ust_app_session
*ua_sess
= NULL
;
5391 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5392 node
= lttng_ht_iter_get_node_ulong(&iter
);
5394 DBG2("UST app session find by objd %d not found", objd
);
5398 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5405 * Return a ust app channel object using the application object and the channel
5406 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5407 * lock MUST be acquired before calling this function.
5409 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5412 struct lttng_ht_node_ulong
*node
;
5413 struct lttng_ht_iter iter
;
5414 struct ust_app_channel
*ua_chan
= NULL
;
5418 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5419 node
= lttng_ht_iter_get_node_ulong(&iter
);
5421 DBG2("UST app channel find by objd %d not found", objd
);
5425 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5432 * Reply to a register channel notification from an application on the notify
5433 * socket. The channel metadata is also created.
5435 * The session UST registry lock is acquired in this function.
5437 * On success 0 is returned else a negative value.
5439 static int reply_ust_register_channel(int sock
, int cobjd
,
5440 size_t nr_fields
, struct ustctl_field
*fields
)
5442 int ret
, ret_code
= 0;
5444 uint64_t chan_reg_key
;
5445 enum ustctl_channel_header type
;
5446 struct ust_app
*app
;
5447 struct ust_app_channel
*ua_chan
;
5448 struct ust_app_session
*ua_sess
;
5449 struct ust_registry_session
*registry
;
5450 struct ust_registry_channel
*chan_reg
;
5454 /* Lookup application. If not found, there is a code flow error. */
5455 app
= find_app_by_notify_sock(sock
);
5457 DBG("Application socket %d is being torn down. Abort event notify",
5460 goto error_rcu_unlock
;
5463 /* Lookup channel by UST object descriptor. */
5464 ua_chan
= find_channel_by_objd(app
, cobjd
);
5466 DBG("Application channel is being torn down. Abort event notify");
5468 goto error_rcu_unlock
;
5471 assert(ua_chan
->session
);
5472 ua_sess
= ua_chan
->session
;
5474 /* Get right session registry depending on the session buffer type. */
5475 registry
= get_session_registry(ua_sess
);
5477 DBG("Application session is being torn down. Abort event notify");
5479 goto error_rcu_unlock
;
5482 /* Depending on the buffer type, a different channel key is used. */
5483 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5484 chan_reg_key
= ua_chan
->tracing_channel_id
;
5486 chan_reg_key
= ua_chan
->key
;
5489 pthread_mutex_lock(®istry
->lock
);
5491 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5494 if (!chan_reg
->register_done
) {
5496 * TODO: eventually use the registry event count for
5497 * this channel to better guess header type for per-pid
5500 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5501 chan_reg
->nr_ctx_fields
= nr_fields
;
5502 chan_reg
->ctx_fields
= fields
;
5504 chan_reg
->header_type
= type
;
5506 /* Get current already assigned values. */
5507 type
= chan_reg
->header_type
;
5509 /* Channel id is set during the object creation. */
5510 chan_id
= chan_reg
->chan_id
;
5512 /* Append to metadata */
5513 if (!chan_reg
->metadata_dumped
) {
5514 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5516 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5522 DBG3("UST app replying to register channel key %" PRIu64
5523 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5526 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5528 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5529 ERR("UST app reply channel failed with ret %d", ret
);
5531 DBG3("UST app reply channel failed. Application died");
5536 /* This channel registry registration is completed. */
5537 chan_reg
->register_done
= 1;
5540 pthread_mutex_unlock(®istry
->lock
);
5548 * Add event to the UST channel registry. When the event is added to the
5549 * registry, the metadata is also created. Once done, this replies to the
5550 * application with the appropriate error code.
5552 * The session UST registry lock is acquired in the function.
5554 * On success 0 is returned else a negative value.
5556 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5557 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5558 int loglevel_value
, char *model_emf_uri
)
5561 uint32_t event_id
= 0;
5562 uint64_t chan_reg_key
;
5563 struct ust_app
*app
;
5564 struct ust_app_channel
*ua_chan
;
5565 struct ust_app_session
*ua_sess
;
5566 struct ust_registry_session
*registry
;
5570 /* Lookup application. If not found, there is a code flow error. */
5571 app
= find_app_by_notify_sock(sock
);
5573 DBG("Application socket %d is being torn down. Abort event notify",
5576 goto error_rcu_unlock
;
5579 /* Lookup channel by UST object descriptor. */
5580 ua_chan
= find_channel_by_objd(app
, cobjd
);
5582 DBG("Application channel is being torn down. Abort event notify");
5584 goto error_rcu_unlock
;
5587 assert(ua_chan
->session
);
5588 ua_sess
= ua_chan
->session
;
5590 registry
= get_session_registry(ua_sess
);
5592 DBG("Application session is being torn down. Abort event notify");
5594 goto error_rcu_unlock
;
5597 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5598 chan_reg_key
= ua_chan
->tracing_channel_id
;
5600 chan_reg_key
= ua_chan
->key
;
5603 pthread_mutex_lock(®istry
->lock
);
5606 * From this point on, this call acquires the ownership of the sig, fields
5607 * and model_emf_uri meaning any free are done inside it if needed. These
5608 * three variables MUST NOT be read/write after this.
5610 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5611 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5612 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5616 model_emf_uri
= NULL
;
5619 * The return value is returned to ustctl so in case of an error, the
5620 * application can be notified. In case of an error, it's important not to
5621 * return a negative error or else the application will get closed.
5623 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5625 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5626 ERR("UST app reply event failed with ret %d", ret
);
5628 DBG3("UST app reply event failed. Application died");
5631 * No need to wipe the create event since the application socket will
5632 * get close on error hence cleaning up everything by itself.
5637 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5641 pthread_mutex_unlock(®istry
->lock
);
5646 free(model_emf_uri
);
5651 * Add enum to the UST session registry. Once done, this replies to the
5652 * application with the appropriate error code.
5654 * The session UST registry lock is acquired within this function.
5656 * On success 0 is returned else a negative value.
5658 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
5659 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
5661 int ret
= 0, ret_code
;
5662 struct ust_app
*app
;
5663 struct ust_app_session
*ua_sess
;
5664 struct ust_registry_session
*registry
;
5665 uint64_t enum_id
= -1ULL;
5669 /* Lookup application. If not found, there is a code flow error. */
5670 app
= find_app_by_notify_sock(sock
);
5672 /* Return an error since this is not an error */
5673 DBG("Application socket %d is being torn down. Aborting enum registration",
5676 goto error_rcu_unlock
;
5679 /* Lookup session by UST object descriptor. */
5680 ua_sess
= find_session_by_objd(app
, sobjd
);
5682 /* Return an error since this is not an error */
5683 DBG("Application session is being torn down (session not found). Aborting enum registration.");
5685 goto error_rcu_unlock
;
5688 registry
= get_session_registry(ua_sess
);
5690 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
5692 goto error_rcu_unlock
;
5695 pthread_mutex_lock(®istry
->lock
);
5698 * From this point on, the callee acquires the ownership of
5699 * entries. The variable entries MUST NOT be read/written after
5702 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
5703 entries
, nr_entries
, &enum_id
);
5707 * The return value is returned to ustctl so in case of an error, the
5708 * application can be notified. In case of an error, it's important not to
5709 * return a negative error or else the application will get closed.
5711 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
5713 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5714 ERR("UST app reply enum failed with ret %d", ret
);
5716 DBG3("UST app reply enum failed. Application died");
5719 * No need to wipe the create enum since the application socket will
5720 * get close on error hence cleaning up everything by itself.
5725 DBG3("UST registry enum %s added successfully or already found", name
);
5728 pthread_mutex_unlock(®istry
->lock
);
5735 * Handle application notification through the given notify socket.
5737 * Return 0 on success or else a negative value.
5739 int ust_app_recv_notify(int sock
)
5742 enum ustctl_notify_cmd cmd
;
5744 DBG3("UST app receiving notify from sock %d", sock
);
5746 ret
= ustctl_recv_notify(sock
, &cmd
);
5748 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5749 ERR("UST app recv notify failed with ret %d", ret
);
5751 DBG3("UST app recv notify failed. Application died");
5757 case USTCTL_NOTIFY_CMD_EVENT
:
5759 int sobjd
, cobjd
, loglevel_value
;
5760 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5762 struct ustctl_field
*fields
;
5764 DBG2("UST app ustctl register event received");
5766 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5767 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5770 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5771 ERR("UST app recv event failed with ret %d", ret
);
5773 DBG3("UST app recv event failed. Application died");
5779 * Add event to the UST registry coming from the notify socket. This
5780 * call will free if needed the sig, fields and model_emf_uri. This
5781 * code path loses the ownsership of these variables and transfer them
5782 * to the this function.
5784 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5785 fields
, loglevel_value
, model_emf_uri
);
5792 case USTCTL_NOTIFY_CMD_CHANNEL
:
5796 struct ustctl_field
*fields
;
5798 DBG2("UST app ustctl register channel received");
5800 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5803 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5804 ERR("UST app recv channel failed with ret %d", ret
);
5806 DBG3("UST app recv channel failed. Application died");
5812 * The fields ownership are transfered to this function call meaning
5813 * that if needed it will be freed. After this, it's invalid to access
5814 * fields or clean it up.
5816 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
5824 case USTCTL_NOTIFY_CMD_ENUM
:
5827 char name
[LTTNG_UST_SYM_NAME_LEN
];
5829 struct ustctl_enum_entry
*entries
;
5831 DBG2("UST app ustctl register enum received");
5833 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
5834 &entries
, &nr_entries
);
5836 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5837 ERR("UST app recv enum failed with ret %d", ret
);
5839 DBG3("UST app recv enum failed. Application died");
5844 /* Callee assumes ownership of entries */
5845 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
5846 entries
, nr_entries
);
5854 /* Should NEVER happen. */
5863 * Once the notify socket hangs up, this is called. First, it tries to find the
5864 * corresponding application. On failure, the call_rcu to close the socket is
5865 * executed. If an application is found, it tries to delete it from the notify
5866 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5868 * Note that an object needs to be allocated here so on ENOMEM failure, the
5869 * call RCU is not done but the rest of the cleanup is.
5871 void ust_app_notify_sock_unregister(int sock
)
5874 struct lttng_ht_iter iter
;
5875 struct ust_app
*app
;
5876 struct ust_app_notify_sock_obj
*obj
;
5882 obj
= zmalloc(sizeof(*obj
));
5885 * An ENOMEM is kind of uncool. If this strikes we continue the
5886 * procedure but the call_rcu will not be called. In this case, we
5887 * accept the fd leak rather than possibly creating an unsynchronized
5888 * state between threads.
5890 * TODO: The notify object should be created once the notify socket is
5891 * registered and stored independantely from the ust app object. The
5892 * tricky part is to synchronize the teardown of the application and
5893 * this notify object. Let's keep that in mind so we can avoid this
5894 * kind of shenanigans with ENOMEM in the teardown path.
5901 DBG("UST app notify socket unregister %d", sock
);
5904 * Lookup application by notify socket. If this fails, this means that the
5905 * hash table delete has already been done by the application
5906 * unregistration process so we can safely close the notify socket in a
5909 app
= find_app_by_notify_sock(sock
);
5914 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5917 * Whatever happens here either we fail or succeed, in both cases we have
5918 * to close the socket after a grace period to continue to the call RCU
5919 * here. If the deletion is successful, the application is not visible
5920 * anymore by other threads and is it fails it means that it was already
5921 * deleted from the hash table so either way we just have to close the
5924 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5930 * Close socket after a grace period to avoid for the socket to be reused
5931 * before the application object is freed creating potential race between
5932 * threads trying to add unique in the global hash table.
5935 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5940 * Destroy a ust app data structure and free its memory.
5942 void ust_app_destroy(struct ust_app
*app
)
5948 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5952 * Take a snapshot for a given UST session. The snapshot is sent to the given
5955 * Return 0 on success or else a negative value.
5957 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5958 struct snapshot_output
*output
, int wait
,
5959 uint64_t nb_packets_per_stream
)
5962 struct lttng_ht_iter iter
;
5963 struct ust_app
*app
;
5964 char pathname
[PATH_MAX
];
5965 struct ltt_session
*session
;
5966 uint64_t trace_archive_id
;
5973 session
= session_find_by_id(usess
->id
);
5975 assert(pthread_mutex_trylock(&session
->lock
));
5976 assert(session_trylock_list());
5977 trace_archive_id
= session
->current_archive_id
;
5979 switch (usess
->buffer_type
) {
5980 case LTTNG_BUFFER_PER_UID
:
5982 struct buffer_reg_uid
*reg
;
5984 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5985 struct buffer_reg_channel
*reg_chan
;
5986 struct consumer_socket
*socket
;
5988 if (!reg
->registry
->reg
.ust
->metadata_key
) {
5989 /* Skip since no metadata is present */
5993 /* Get consumer socket to use to push the metadata.*/
5994 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6001 memset(pathname
, 0, sizeof(pathname
));
6002 ret
= snprintf(pathname
, sizeof(pathname
),
6003 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
6004 reg
->uid
, reg
->bits_per_long
);
6006 PERROR("snprintf snapshot path");
6010 /* Add the UST default trace dir to path. */
6011 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6012 reg_chan
, node
.node
) {
6013 ret
= consumer_snapshot_channel(socket
,
6014 reg_chan
->consumer_key
,
6015 output
, 0, usess
->uid
,
6016 usess
->gid
, pathname
, wait
,
6017 nb_packets_per_stream
,
6023 ret
= consumer_snapshot_channel(socket
,
6024 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6025 usess
->uid
, usess
->gid
, pathname
, wait
, 0,
6033 case LTTNG_BUFFER_PER_PID
:
6035 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6036 struct consumer_socket
*socket
;
6037 struct lttng_ht_iter chan_iter
;
6038 struct ust_app_channel
*ua_chan
;
6039 struct ust_app_session
*ua_sess
;
6040 struct ust_registry_session
*registry
;
6042 ua_sess
= lookup_session_by_app(usess
, app
);
6044 /* Session not associated with this app. */
6048 /* Get the right consumer socket for the application. */
6049 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6056 /* Add the UST default trace dir to path. */
6057 memset(pathname
, 0, sizeof(pathname
));
6058 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
6061 PERROR("snprintf snapshot path");
6065 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6066 ua_chan
, node
.node
) {
6067 ret
= consumer_snapshot_channel(socket
,
6068 ua_chan
->key
, output
,
6069 0, ua_sess
->euid
, ua_sess
->egid
,
6071 nb_packets_per_stream
,
6074 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6081 registry
= get_session_registry(ua_sess
);
6083 DBG("Application session is being torn down. Skip application.");
6086 ret
= consumer_snapshot_channel(socket
,
6087 registry
->metadata_key
, output
,
6088 1, ua_sess
->euid
, ua_sess
->egid
,
6092 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6111 * Return the size taken by one more packet per stream.
6113 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
6114 uint64_t cur_nr_packets
)
6116 uint64_t tot_size
= 0;
6117 struct ust_app
*app
;
6118 struct lttng_ht_iter iter
;
6122 switch (usess
->buffer_type
) {
6123 case LTTNG_BUFFER_PER_UID
:
6125 struct buffer_reg_uid
*reg
;
6127 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6128 struct buffer_reg_channel
*reg_chan
;
6131 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6132 reg_chan
, node
.node
) {
6133 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6135 * Don't take channel into account if we
6136 * already grab all its packets.
6140 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6146 case LTTNG_BUFFER_PER_PID
:
6149 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6150 struct ust_app_channel
*ua_chan
;
6151 struct ust_app_session
*ua_sess
;
6152 struct lttng_ht_iter chan_iter
;
6154 ua_sess
= lookup_session_by_app(usess
, app
);
6156 /* Session not associated with this app. */
6160 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6161 ua_chan
, node
.node
) {
6162 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6164 * Don't take channel into account if we
6165 * already grab all its packets.
6169 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6183 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6184 struct cds_list_head
*buffer_reg_uid_list
,
6185 struct consumer_output
*consumer
, uint64_t uchan_id
,
6186 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6189 uint64_t consumer_chan_key
;
6194 ret
= buffer_reg_uid_consumer_channel_key(
6195 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6203 ret
= consumer_get_lost_packets(ust_session_id
,
6204 consumer_chan_key
, consumer
, lost
);
6206 ret
= consumer_get_discarded_events(ust_session_id
,
6207 consumer_chan_key
, consumer
, discarded
);
6214 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6215 struct ltt_ust_channel
*uchan
,
6216 struct consumer_output
*consumer
, int overwrite
,
6217 uint64_t *discarded
, uint64_t *lost
)
6220 struct lttng_ht_iter iter
;
6221 struct lttng_ht_node_str
*ua_chan_node
;
6222 struct ust_app
*app
;
6223 struct ust_app_session
*ua_sess
;
6224 struct ust_app_channel
*ua_chan
;
6231 * Iterate over every registered applications. Sum counters for
6232 * all applications containing requested session and channel.
6234 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6235 struct lttng_ht_iter uiter
;
6237 ua_sess
= lookup_session_by_app(usess
, app
);
6238 if (ua_sess
== NULL
) {
6243 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6244 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6245 /* If the session is found for the app, the channel must be there */
6246 assert(ua_chan_node
);
6248 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6253 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6260 uint64_t _discarded
;
6262 ret
= consumer_get_discarded_events(usess
->id
,
6263 ua_chan
->key
, consumer
, &_discarded
);
6267 (*discarded
) += _discarded
;
6276 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6277 struct ust_app
*app
)
6280 struct ust_app_session
*ua_sess
;
6282 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6286 ua_sess
= lookup_session_by_app(usess
, app
);
6287 if (ua_sess
== NULL
) {
6288 /* The session is in teardown process. Ignore and continue. */
6292 pthread_mutex_lock(&ua_sess
->lock
);
6294 if (ua_sess
->deleted
) {
6298 pthread_mutex_lock(&app
->sock_lock
);
6299 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6300 pthread_mutex_unlock(&app
->sock_lock
);
6303 pthread_mutex_unlock(&ua_sess
->lock
);
6307 health_code_update();
6312 * Regenerate the statedump for each app in the session.
6314 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6317 struct lttng_ht_iter iter
;
6318 struct ust_app
*app
;
6320 DBG("Regenerating the metadata for all UST apps");
6324 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6325 if (!app
->compatible
) {
6329 ret
= ust_app_regenerate_statedump(usess
, app
);
6331 /* Continue to the next app even on error */
6342 * Rotate all the channels of a session.
6344 * Return LTTNG_OK on success or else an LTTng error code.
6346 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
6349 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6350 struct lttng_ht_iter iter
;
6351 struct ust_app
*app
;
6352 struct ltt_ust_session
*usess
= session
->ust_session
;
6353 char pathname
[LTTNG_PATH_MAX
];
6359 switch (usess
->buffer_type
) {
6360 case LTTNG_BUFFER_PER_UID
:
6362 struct buffer_reg_uid
*reg
;
6364 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6365 struct buffer_reg_channel
*reg_chan
;
6366 struct consumer_socket
*socket
;
6368 /* Get consumer socket to use to push the metadata.*/
6369 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6372 cmd_ret
= LTTNG_ERR_INVALID
;
6376 ret
= snprintf(pathname
, sizeof(pathname
),
6377 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
6378 reg
->uid
, reg
->bits_per_long
);
6379 if (ret
< 0 || ret
== sizeof(pathname
)) {
6380 PERROR("Failed to format rotation path");
6381 cmd_ret
= LTTNG_ERR_INVALID
;
6385 /* Rotate the data channels. */
6386 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6387 reg_chan
, node
.node
) {
6388 ret
= consumer_rotate_channel(socket
,
6389 reg_chan
->consumer_key
,
6390 usess
->uid
, usess
->gid
,
6391 usess
->consumer
, pathname
,
6392 /* is_metadata_channel */ false,
6393 session
->current_archive_id
);
6395 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6400 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6402 ret
= consumer_rotate_channel(socket
,
6403 reg
->registry
->reg
.ust
->metadata_key
,
6404 usess
->uid
, usess
->gid
,
6405 usess
->consumer
, pathname
,
6406 /* is_metadata_channel */ true,
6407 session
->current_archive_id
);
6409 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6415 case LTTNG_BUFFER_PER_PID
:
6417 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6418 struct consumer_socket
*socket
;
6419 struct lttng_ht_iter chan_iter
;
6420 struct ust_app_channel
*ua_chan
;
6421 struct ust_app_session
*ua_sess
;
6422 struct ust_registry_session
*registry
;
6424 ua_sess
= lookup_session_by_app(usess
, app
);
6426 /* Session not associated with this app. */
6429 ret
= snprintf(pathname
, sizeof(pathname
),
6430 DEFAULT_UST_TRACE_DIR
"/%s",
6432 if (ret
< 0 || ret
== sizeof(pathname
)) {
6433 PERROR("Failed to format rotation path");
6434 cmd_ret
= LTTNG_ERR_INVALID
;
6438 /* Get the right consumer socket for the application. */
6439 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6442 cmd_ret
= LTTNG_ERR_INVALID
;
6446 registry
= get_session_registry(ua_sess
);
6448 DBG("Application session is being torn down. Skip application.");
6453 /* Rotate the data channels. */
6454 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6455 ua_chan
, node
.node
) {
6456 ret
= consumer_rotate_channel(socket
, ua_chan
->key
,
6457 ua_sess
->euid
, ua_sess
->egid
,
6458 ua_sess
->consumer
, pathname
,
6459 /* is_metadata_channel */ false,
6460 session
->current_archive_id
);
6462 /* Per-PID buffer and application going away. */
6463 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6465 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
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
);
6478 /* Per-PID buffer and application going away. */
6479 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6481 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;