2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
16 #include <sys/types.h>
18 #include <urcu/compiler.h>
21 #include <common/compat/errno.h>
22 #include <common/common.h>
23 #include <common/sessiond-comm/sessiond-comm.h>
25 #include "buffer-registry.h"
27 #include "health-sessiond.h"
29 #include "ust-consumer.h"
30 #include "lttng-ust-ctl.h"
31 #include "lttng-ust-error.h"
34 #include "lttng-sessiond.h"
35 #include "notification-thread-commands.h"
38 struct lttng_ht
*ust_app_ht
;
39 struct lttng_ht
*ust_app_ht_by_sock
;
40 struct lttng_ht
*ust_app_ht_by_notify_sock
;
43 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
45 /* Next available channel key. Access under next_channel_key_lock. */
46 static uint64_t _next_channel_key
;
47 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
49 /* Next available session ID. Access under next_session_id_lock. */
50 static uint64_t _next_session_id
;
51 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
54 * Return the incremented value of next_channel_key.
56 static uint64_t get_next_channel_key(void)
60 pthread_mutex_lock(&next_channel_key_lock
);
61 ret
= ++_next_channel_key
;
62 pthread_mutex_unlock(&next_channel_key_lock
);
67 * Return the atomically incremented value of next_session_id.
69 static uint64_t get_next_session_id(void)
73 pthread_mutex_lock(&next_session_id_lock
);
74 ret
= ++_next_session_id
;
75 pthread_mutex_unlock(&next_session_id_lock
);
79 static void copy_channel_attr_to_ustctl(
80 struct ustctl_consumer_channel_attr
*attr
,
81 struct lttng_ust_channel_attr
*uattr
)
83 /* Copy event attributes since the layout is different. */
84 attr
->subbuf_size
= uattr
->subbuf_size
;
85 attr
->num_subbuf
= uattr
->num_subbuf
;
86 attr
->overwrite
= uattr
->overwrite
;
87 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
88 attr
->read_timer_interval
= uattr
->read_timer_interval
;
89 attr
->output
= uattr
->output
;
90 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
94 * Match function for the hash table lookup.
96 * It matches an ust app event based on three attributes which are the event
97 * name, the filter bytecode and the loglevel.
99 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
101 struct ust_app_event
*event
;
102 const struct ust_app_ht_key
*key
;
103 int ev_loglevel_value
;
108 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
110 ev_loglevel_value
= event
->attr
.loglevel
;
112 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
115 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
119 /* Event loglevel. */
120 if (ev_loglevel_value
!= key
->loglevel_type
) {
121 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
122 && key
->loglevel_type
== 0 &&
123 ev_loglevel_value
== -1) {
125 * Match is accepted. This is because on event creation, the
126 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
127 * -1 are accepted for this loglevel type since 0 is the one set by
128 * the API when receiving an enable event.
135 /* One of the filters is NULL, fail. */
136 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
140 if (key
->filter
&& event
->filter
) {
141 /* Both filters exists, check length followed by the bytecode. */
142 if (event
->filter
->len
!= key
->filter
->len
||
143 memcmp(event
->filter
->data
, key
->filter
->data
,
144 event
->filter
->len
) != 0) {
149 /* One of the exclusions is NULL, fail. */
150 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
154 if (key
->exclusion
&& event
->exclusion
) {
155 /* Both exclusions exists, check count followed by the names. */
156 if (event
->exclusion
->count
!= key
->exclusion
->count
||
157 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
158 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
172 * Unique add of an ust app event in the given ht. This uses the custom
173 * ht_match_ust_app_event match function and the event name as hash.
175 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
176 struct ust_app_event
*event
)
178 struct cds_lfht_node
*node_ptr
;
179 struct ust_app_ht_key key
;
183 assert(ua_chan
->events
);
186 ht
= ua_chan
->events
;
187 key
.name
= event
->attr
.name
;
188 key
.filter
= event
->filter
;
189 key
.loglevel_type
= event
->attr
.loglevel
;
190 key
.exclusion
= event
->exclusion
;
192 node_ptr
= cds_lfht_add_unique(ht
->ht
,
193 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
194 ht_match_ust_app_event
, &key
, &event
->node
.node
);
195 assert(node_ptr
== &event
->node
.node
);
199 * Close the notify socket from the given RCU head object. This MUST be called
200 * through a call_rcu().
202 static void close_notify_sock_rcu(struct rcu_head
*head
)
205 struct ust_app_notify_sock_obj
*obj
=
206 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
208 /* Must have a valid fd here. */
209 assert(obj
->fd
>= 0);
211 ret
= close(obj
->fd
);
213 ERR("close notify sock %d RCU", obj
->fd
);
215 lttng_fd_put(LTTNG_FD_APPS
, 1);
221 * Return the session registry according to the buffer type of the given
224 * A registry per UID object MUST exists before calling this function or else
225 * it assert() if not found. RCU read side lock must be acquired.
227 static struct ust_registry_session
*get_session_registry(
228 struct ust_app_session
*ua_sess
)
230 struct ust_registry_session
*registry
= NULL
;
234 switch (ua_sess
->buffer_type
) {
235 case LTTNG_BUFFER_PER_PID
:
237 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
241 registry
= reg_pid
->registry
->reg
.ust
;
244 case LTTNG_BUFFER_PER_UID
:
246 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
247 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
248 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
252 registry
= reg_uid
->registry
->reg
.ust
;
264 * Delete ust context safely. RCU read lock must be held before calling
268 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
276 pthread_mutex_lock(&app
->sock_lock
);
277 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
278 pthread_mutex_unlock(&app
->sock_lock
);
279 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
280 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
281 sock
, ua_ctx
->obj
->handle
, ret
);
289 * Delete ust app event safely. RCU read lock must be held before calling
293 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
300 free(ua_event
->filter
);
301 if (ua_event
->exclusion
!= NULL
)
302 free(ua_event
->exclusion
);
303 if (ua_event
->obj
!= NULL
) {
304 pthread_mutex_lock(&app
->sock_lock
);
305 ret
= ustctl_release_object(sock
, ua_event
->obj
);
306 pthread_mutex_unlock(&app
->sock_lock
);
307 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
308 ERR("UST app sock %d release event obj failed with ret %d",
317 * Release ust data object of the given stream.
319 * Return 0 on success or else a negative value.
321 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
329 pthread_mutex_lock(&app
->sock_lock
);
330 ret
= ustctl_release_object(sock
, stream
->obj
);
331 pthread_mutex_unlock(&app
->sock_lock
);
332 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
333 ERR("UST app sock %d release stream obj failed with ret %d",
336 lttng_fd_put(LTTNG_FD_APPS
, 2);
344 * Delete ust app stream safely. RCU read lock must be held before calling
348 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
353 (void) release_ust_app_stream(sock
, stream
, app
);
358 * We need to execute ht_destroy outside of RCU read-side critical
359 * section and outside of call_rcu thread, so we postpone its execution
360 * using ht_cleanup_push. It is simpler than to change the semantic of
361 * the many callers of delete_ust_app_session().
364 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
366 struct ust_app_channel
*ua_chan
=
367 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
369 ht_cleanup_push(ua_chan
->ctx
);
370 ht_cleanup_push(ua_chan
->events
);
375 * Extract the lost packet or discarded events counter when the channel is
376 * being deleted and store the value in the parent channel so we can
377 * access it from lttng list and at stop/destroy.
379 * The session list lock must be held by the caller.
382 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
384 uint64_t discarded
= 0, lost
= 0;
385 struct ltt_session
*session
;
386 struct ltt_ust_channel
*uchan
;
388 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
393 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
394 if (!session
|| !session
->ust_session
) {
396 * Not finding the session is not an error because there are
397 * multiple ways the channels can be torn down.
399 * 1) The session daemon can initiate the destruction of the
400 * ust app session after receiving a destroy command or
401 * during its shutdown/teardown.
402 * 2) The application, since we are in per-pid tracing, is
403 * unregistering and tearing down its ust app session.
405 * Both paths are protected by the session list lock which
406 * ensures that the accounting of lost packets and discarded
407 * events is done exactly once. The session is then unpublished
408 * from the session list, resulting in this condition.
413 if (ua_chan
->attr
.overwrite
) {
414 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
415 ua_chan
->key
, session
->ust_session
->consumer
,
418 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
419 ua_chan
->key
, session
->ust_session
->consumer
,
422 uchan
= trace_ust_find_channel_by_name(
423 session
->ust_session
->domain_global
.channels
,
426 ERR("Missing UST channel to store discarded counters");
430 uchan
->per_pid_closed_app_discarded
+= discarded
;
431 uchan
->per_pid_closed_app_lost
+= lost
;
436 session_put(session
);
441 * Delete ust app channel safely. RCU read lock must be held before calling
444 * The session list lock must be held by the caller.
447 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
451 struct lttng_ht_iter iter
;
452 struct ust_app_event
*ua_event
;
453 struct ust_app_ctx
*ua_ctx
;
454 struct ust_app_stream
*stream
, *stmp
;
455 struct ust_registry_session
*registry
;
459 DBG3("UST app deleting channel %s", ua_chan
->name
);
462 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
463 cds_list_del(&stream
->list
);
464 delete_ust_app_stream(sock
, stream
, app
);
468 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
469 cds_list_del(&ua_ctx
->list
);
470 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
472 delete_ust_app_ctx(sock
, ua_ctx
, app
);
476 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
478 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
480 delete_ust_app_event(sock
, ua_event
, app
);
483 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
484 /* Wipe and free registry from session registry. */
485 registry
= get_session_registry(ua_chan
->session
);
487 ust_registry_channel_del_free(registry
, ua_chan
->key
,
491 * A negative socket can be used by the caller when
492 * cleaning-up a ua_chan in an error path. Skip the
493 * accounting in this case.
496 save_per_pid_lost_discarded_counters(ua_chan
);
500 if (ua_chan
->obj
!= NULL
) {
501 /* Remove channel from application UST object descriptor. */
502 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
503 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
505 pthread_mutex_lock(&app
->sock_lock
);
506 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
507 pthread_mutex_unlock(&app
->sock_lock
);
508 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
509 ERR("UST app sock %d release channel obj failed with ret %d",
512 lttng_fd_put(LTTNG_FD_APPS
, 1);
515 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
518 int ust_app_register_done(struct ust_app
*app
)
522 pthread_mutex_lock(&app
->sock_lock
);
523 ret
= ustctl_register_done(app
->sock
);
524 pthread_mutex_unlock(&app
->sock_lock
);
528 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
533 pthread_mutex_lock(&app
->sock_lock
);
538 ret
= ustctl_release_object(sock
, data
);
540 pthread_mutex_unlock(&app
->sock_lock
);
546 * Push metadata to consumer socket.
548 * RCU read-side lock must be held to guarantee existance of socket.
549 * Must be called with the ust app session lock held.
550 * Must be called with the registry lock held.
552 * On success, return the len of metadata pushed or else a negative value.
553 * Returning a -EPIPE return value means we could not send the metadata,
554 * but it can be caused by recoverable errors (e.g. the application has
555 * terminated concurrently).
557 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
558 struct consumer_socket
*socket
, int send_zero_data
)
561 char *metadata_str
= NULL
;
562 size_t len
, offset
, new_metadata_len_sent
;
564 uint64_t metadata_key
, metadata_version
;
569 metadata_key
= registry
->metadata_key
;
572 * Means that no metadata was assigned to the session. This can
573 * happens if no start has been done previously.
579 offset
= registry
->metadata_len_sent
;
580 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
581 new_metadata_len_sent
= registry
->metadata_len
;
582 metadata_version
= registry
->metadata_version
;
584 DBG3("No metadata to push for metadata key %" PRIu64
,
585 registry
->metadata_key
);
587 if (send_zero_data
) {
588 DBG("No metadata to push");
594 /* Allocate only what we have to send. */
595 metadata_str
= zmalloc(len
);
597 PERROR("zmalloc ust app metadata string");
601 /* Copy what we haven't sent out. */
602 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
605 pthread_mutex_unlock(®istry
->lock
);
607 * We need to unlock the registry while we push metadata to
608 * break a circular dependency between the consumerd metadata
609 * lock and the sessiond registry lock. Indeed, pushing metadata
610 * to the consumerd awaits that it gets pushed all the way to
611 * relayd, but doing so requires grabbing the metadata lock. If
612 * a concurrent metadata request is being performed by
613 * consumerd, this can try to grab the registry lock on the
614 * sessiond while holding the metadata lock on the consumer
615 * daemon. Those push and pull schemes are performed on two
616 * different bidirectionnal communication sockets.
618 ret
= consumer_push_metadata(socket
, metadata_key
,
619 metadata_str
, len
, offset
, metadata_version
);
620 pthread_mutex_lock(®istry
->lock
);
623 * There is an acceptable race here between the registry
624 * metadata key assignment and the creation on the
625 * consumer. The session daemon can concurrently push
626 * metadata for this registry while being created on the
627 * consumer since the metadata key of the registry is
628 * assigned *before* it is setup to avoid the consumer
629 * to ask for metadata that could possibly be not found
630 * in the session daemon.
632 * The metadata will get pushed either by the session
633 * being stopped or the consumer requesting metadata if
634 * that race is triggered.
636 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
639 ERR("Error pushing metadata to consumer");
645 * Metadata may have been concurrently pushed, since
646 * we're not holding the registry lock while pushing to
647 * consumer. This is handled by the fact that we send
648 * the metadata content, size, and the offset at which
649 * that metadata belongs. This may arrive out of order
650 * on the consumer side, and the consumer is able to
651 * deal with overlapping fragments. The consumer
652 * supports overlapping fragments, which must be
653 * contiguous starting from offset 0. We keep the
654 * largest metadata_len_sent value of the concurrent
657 registry
->metadata_len_sent
=
658 max_t(size_t, registry
->metadata_len_sent
,
659 new_metadata_len_sent
);
668 * On error, flag the registry that the metadata is
669 * closed. We were unable to push anything and this
670 * means that either the consumer is not responding or
671 * the metadata cache has been destroyed on the
674 registry
->metadata_closed
= 1;
682 * For a given application and session, push metadata to consumer.
683 * Either sock or consumer is required : if sock is NULL, the default
684 * socket to send the metadata is retrieved from consumer, if sock
685 * is not NULL we use it to send the metadata.
686 * RCU read-side lock must be held while calling this function,
687 * therefore ensuring existance of registry. It also ensures existance
688 * of socket throughout this function.
690 * Return 0 on success else a negative error.
691 * Returning a -EPIPE return value means we could not send the metadata,
692 * but it can be caused by recoverable errors (e.g. the application has
693 * terminated concurrently).
695 static int push_metadata(struct ust_registry_session
*registry
,
696 struct consumer_output
*consumer
)
700 struct consumer_socket
*socket
;
705 pthread_mutex_lock(®istry
->lock
);
706 if (registry
->metadata_closed
) {
711 /* Get consumer socket to use to push the metadata.*/
712 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
719 ret
= ust_app_push_metadata(registry
, socket
, 0);
724 pthread_mutex_unlock(®istry
->lock
);
728 pthread_mutex_unlock(®istry
->lock
);
733 * Send to the consumer a close metadata command for the given session. Once
734 * done, the metadata channel is deleted and the session metadata pointer is
735 * nullified. The session lock MUST be held unless the application is
736 * in the destroy path.
738 * Do not hold the registry lock while communicating with the consumerd, because
739 * doing so causes inter-process deadlocks between consumerd and sessiond with
740 * the metadata request notification.
742 * Return 0 on success else a negative value.
744 static int close_metadata(struct ust_registry_session
*registry
,
745 struct consumer_output
*consumer
)
748 struct consumer_socket
*socket
;
749 uint64_t metadata_key
;
750 bool registry_was_already_closed
;
757 pthread_mutex_lock(®istry
->lock
);
758 metadata_key
= registry
->metadata_key
;
759 registry_was_already_closed
= registry
->metadata_closed
;
760 if (metadata_key
!= 0) {
762 * Metadata closed. Even on error this means that the consumer
763 * is not responding or not found so either way a second close
764 * should NOT be emit for this registry.
766 registry
->metadata_closed
= 1;
768 pthread_mutex_unlock(®istry
->lock
);
770 if (metadata_key
== 0 || registry_was_already_closed
) {
775 /* Get consumer socket to use to push the metadata.*/
776 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
783 ret
= consumer_close_metadata(socket
, metadata_key
);
794 * We need to execute ht_destroy outside of RCU read-side critical
795 * section and outside of call_rcu thread, so we postpone its execution
796 * using ht_cleanup_push. It is simpler than to change the semantic of
797 * the many callers of delete_ust_app_session().
800 void delete_ust_app_session_rcu(struct rcu_head
*head
)
802 struct ust_app_session
*ua_sess
=
803 caa_container_of(head
, struct ust_app_session
, rcu_head
);
805 ht_cleanup_push(ua_sess
->channels
);
810 * Delete ust app session safely. RCU read lock must be held before calling
813 * The session list lock must be held by the caller.
816 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
820 struct lttng_ht_iter iter
;
821 struct ust_app_channel
*ua_chan
;
822 struct ust_registry_session
*registry
;
826 pthread_mutex_lock(&ua_sess
->lock
);
828 assert(!ua_sess
->deleted
);
829 ua_sess
->deleted
= true;
831 registry
= get_session_registry(ua_sess
);
832 /* Registry can be null on error path during initialization. */
834 /* Push metadata for application before freeing the application. */
835 (void) push_metadata(registry
, ua_sess
->consumer
);
838 * Don't ask to close metadata for global per UID buffers. Close
839 * metadata only on destroy trace session in this case. Also, the
840 * previous push metadata could have flag the metadata registry to
841 * close so don't send a close command if closed.
843 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
844 /* And ask to close it for this session registry. */
845 (void) close_metadata(registry
, ua_sess
->consumer
);
849 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
851 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
853 delete_ust_app_channel(sock
, ua_chan
, app
);
856 /* In case of per PID, the registry is kept in the session. */
857 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
858 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
861 * Registry can be null on error path during
864 buffer_reg_pid_remove(reg_pid
);
865 buffer_reg_pid_destroy(reg_pid
);
869 if (ua_sess
->handle
!= -1) {
870 pthread_mutex_lock(&app
->sock_lock
);
871 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
872 pthread_mutex_unlock(&app
->sock_lock
);
873 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
874 ERR("UST app sock %d release session handle failed with ret %d",
877 /* Remove session from application UST object descriptor. */
878 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
879 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
883 pthread_mutex_unlock(&ua_sess
->lock
);
885 consumer_output_put(ua_sess
->consumer
);
887 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
891 * Delete a traceable application structure from the global list. Never call
892 * this function outside of a call_rcu call.
894 * RCU read side lock should _NOT_ be held when calling this function.
897 void delete_ust_app(struct ust_app
*app
)
900 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
903 * The session list lock must be held during this function to guarantee
904 * the existence of ua_sess.
907 /* Delete ust app sessions info */
912 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
914 /* Free every object in the session and the session. */
916 delete_ust_app_session(sock
, ua_sess
, app
);
920 ht_cleanup_push(app
->sessions
);
921 ht_cleanup_push(app
->ust_sessions_objd
);
922 ht_cleanup_push(app
->ust_objd
);
925 * Wait until we have deleted the application from the sock hash table
926 * before closing this socket, otherwise an application could re-use the
927 * socket ID and race with the teardown, using the same hash table entry.
929 * It's OK to leave the close in call_rcu. We want it to stay unique for
930 * all RCU readers that could run concurrently with unregister app,
931 * therefore we _need_ to only close that socket after a grace period. So
932 * it should stay in this RCU callback.
934 * This close() is a very important step of the synchronization model so
935 * every modification to this function must be carefully reviewed.
941 lttng_fd_put(LTTNG_FD_APPS
, 1);
943 DBG2("UST app pid %d deleted", app
->pid
);
945 session_unlock_list();
949 * URCU intermediate call to delete an UST app.
952 void delete_ust_app_rcu(struct rcu_head
*head
)
954 struct lttng_ht_node_ulong
*node
=
955 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
956 struct ust_app
*app
=
957 caa_container_of(node
, struct ust_app
, pid_n
);
959 DBG3("Call RCU deleting app PID %d", app
->pid
);
964 * Delete the session from the application ht and delete the data structure by
965 * freeing every object inside and releasing them.
967 * The session list lock must be held by the caller.
969 static void destroy_app_session(struct ust_app
*app
,
970 struct ust_app_session
*ua_sess
)
973 struct lttng_ht_iter iter
;
978 iter
.iter
.node
= &ua_sess
->node
.node
;
979 ret
= lttng_ht_del(app
->sessions
, &iter
);
981 /* Already scheduled for teardown. */
985 /* Once deleted, free the data structure. */
986 delete_ust_app_session(app
->sock
, ua_sess
, app
);
993 * Alloc new UST app session.
996 struct ust_app_session
*alloc_ust_app_session(void)
998 struct ust_app_session
*ua_sess
;
1000 /* Init most of the default value by allocating and zeroing */
1001 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1002 if (ua_sess
== NULL
) {
1007 ua_sess
->handle
= -1;
1008 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1009 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1010 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1019 * Alloc new UST app channel.
1022 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1023 struct ust_app_session
*ua_sess
,
1024 struct lttng_ust_channel_attr
*attr
)
1026 struct ust_app_channel
*ua_chan
;
1028 /* Init most of the default value by allocating and zeroing */
1029 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1030 if (ua_chan
== NULL
) {
1035 /* Setup channel name */
1036 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1037 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1039 ua_chan
->enabled
= 1;
1040 ua_chan
->handle
= -1;
1041 ua_chan
->session
= ua_sess
;
1042 ua_chan
->key
= get_next_channel_key();
1043 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1044 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1045 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1047 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1048 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1050 /* Copy attributes */
1052 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1053 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1054 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1055 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1056 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1057 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1058 ua_chan
->attr
.output
= attr
->output
;
1059 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1061 /* By default, the channel is a per cpu channel. */
1062 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1064 DBG3("UST app channel %s allocated", ua_chan
->name
);
1073 * Allocate and initialize a UST app stream.
1075 * Return newly allocated stream pointer or NULL on error.
1077 struct ust_app_stream
*ust_app_alloc_stream(void)
1079 struct ust_app_stream
*stream
= NULL
;
1081 stream
= zmalloc(sizeof(*stream
));
1082 if (stream
== NULL
) {
1083 PERROR("zmalloc ust app stream");
1087 /* Zero could be a valid value for a handle so flag it to -1. */
1088 stream
->handle
= -1;
1095 * Alloc new UST app event.
1098 struct ust_app_event
*alloc_ust_app_event(char *name
,
1099 struct lttng_ust_event
*attr
)
1101 struct ust_app_event
*ua_event
;
1103 /* Init most of the default value by allocating and zeroing */
1104 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1105 if (ua_event
== NULL
) {
1106 PERROR("Failed to allocate ust_app_event structure");
1110 ua_event
->enabled
= 1;
1111 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1112 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1113 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1115 /* Copy attributes */
1117 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1120 DBG3("UST app event %s allocated", ua_event
->name
);
1129 * Alloc new UST app context.
1132 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1134 struct ust_app_ctx
*ua_ctx
;
1136 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1137 if (ua_ctx
== NULL
) {
1141 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1144 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1145 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1146 char *provider_name
= NULL
, *ctx_name
= NULL
;
1148 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1149 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1150 if (!provider_name
|| !ctx_name
) {
1151 free(provider_name
);
1156 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1157 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1161 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1169 * Allocate a filter and copy the given original filter.
1171 * Return allocated filter or NULL on error.
1173 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1174 struct lttng_filter_bytecode
*orig_f
)
1176 struct lttng_filter_bytecode
*filter
= NULL
;
1178 /* Copy filter bytecode */
1179 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1181 PERROR("zmalloc alloc filter bytecode");
1185 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1192 * Create a liblttng-ust filter bytecode from given bytecode.
1194 * Return allocated filter or NULL on error.
1196 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1197 const struct lttng_filter_bytecode
*orig_f
)
1199 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1201 /* Copy filter bytecode */
1202 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1204 PERROR("zmalloc alloc ust filter bytecode");
1208 assert(sizeof(struct lttng_filter_bytecode
) ==
1209 sizeof(struct lttng_ust_filter_bytecode
));
1210 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1216 * Find an ust_app using the sock and return it. RCU read side lock must be
1217 * held before calling this helper function.
1219 struct ust_app
*ust_app_find_by_sock(int sock
)
1221 struct lttng_ht_node_ulong
*node
;
1222 struct lttng_ht_iter iter
;
1224 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1225 node
= lttng_ht_iter_get_node_ulong(&iter
);
1227 DBG2("UST app find by sock %d not found", sock
);
1231 return caa_container_of(node
, struct ust_app
, sock_n
);
1238 * Find an ust_app using the notify sock and return it. RCU read side lock must
1239 * be held before calling this helper function.
1241 static struct ust_app
*find_app_by_notify_sock(int sock
)
1243 struct lttng_ht_node_ulong
*node
;
1244 struct lttng_ht_iter iter
;
1246 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1248 node
= lttng_ht_iter_get_node_ulong(&iter
);
1250 DBG2("UST app find by notify sock %d not found", sock
);
1254 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1261 * Lookup for an ust app event based on event name, filter bytecode and the
1264 * Return an ust_app_event object or NULL on error.
1266 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1267 const char *name
, const struct lttng_filter_bytecode
*filter
,
1269 const struct lttng_event_exclusion
*exclusion
)
1271 struct lttng_ht_iter iter
;
1272 struct lttng_ht_node_str
*node
;
1273 struct ust_app_event
*event
= NULL
;
1274 struct ust_app_ht_key key
;
1279 /* Setup key for event lookup. */
1281 key
.filter
= filter
;
1282 key
.loglevel_type
= loglevel_value
;
1283 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1284 key
.exclusion
= exclusion
;
1286 /* Lookup using the event name as hash and a custom match fct. */
1287 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1288 ht_match_ust_app_event
, &key
, &iter
.iter
);
1289 node
= lttng_ht_iter_get_node_str(&iter
);
1294 event
= caa_container_of(node
, struct ust_app_event
, node
);
1301 * Create the channel context on the tracer.
1303 * Called with UST app session lock held.
1306 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1307 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1311 health_code_update();
1313 pthread_mutex_lock(&app
->sock_lock
);
1314 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1315 ua_chan
->obj
, &ua_ctx
->obj
);
1316 pthread_mutex_unlock(&app
->sock_lock
);
1318 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1319 ERR("UST app create channel context failed for app (pid: %d) "
1320 "with ret %d", app
->pid
, ret
);
1323 * This is normal behavior, an application can die during the
1324 * creation process. Don't report an error so the execution can
1325 * continue normally.
1328 DBG3("UST app add context failed. Application is dead.");
1333 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1335 DBG2("UST app context handle %d created successfully for channel %s",
1336 ua_ctx
->handle
, ua_chan
->name
);
1339 health_code_update();
1344 * Set the filter on the tracer.
1346 static int set_ust_object_filter(struct ust_app
*app
,
1347 const struct lttng_filter_bytecode
*bytecode
,
1348 struct lttng_ust_object_data
*ust_object
)
1351 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1353 health_code_update();
1355 ust_bytecode
= create_ust_bytecode_from_bytecode(bytecode
);
1356 if (!ust_bytecode
) {
1357 ret
= -LTTNG_ERR_NOMEM
;
1360 pthread_mutex_lock(&app
->sock_lock
);
1361 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1363 pthread_mutex_unlock(&app
->sock_lock
);
1365 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1366 ERR("UST app set object filter failed for object %p of app (pid: %d) "
1367 "with ret %d", ust_object
, app
->pid
, ret
);
1370 * This is normal behavior, an application can die during the
1371 * creation process. Don't report an error so the execution can
1372 * continue normally.
1375 DBG3("Failed to set UST app object filter. Application is dead.");
1380 DBG2("UST filter successfully set for object %p", ust_object
);
1383 health_code_update();
1389 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1390 const struct lttng_event_exclusion
*exclusion
)
1392 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1393 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1394 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1396 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1397 if (!ust_exclusion
) {
1402 assert(sizeof(struct lttng_event_exclusion
) ==
1403 sizeof(struct lttng_ust_event_exclusion
));
1404 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1406 return ust_exclusion
;
1410 * Set event exclusions on the tracer.
1412 static int set_ust_object_exclusions(struct ust_app
*app
,
1413 const struct lttng_event_exclusion
*exclusions
,
1414 struct lttng_ust_object_data
*ust_object
)
1417 struct lttng_ust_event_exclusion
*ust_exclusions
= NULL
;
1419 assert(exclusions
&& exclusions
->count
> 0);
1421 health_code_update();
1423 ust_exclusions
= create_ust_exclusion_from_exclusion(
1425 if (!ust_exclusions
) {
1426 ret
= -LTTNG_ERR_NOMEM
;
1429 pthread_mutex_lock(&app
->sock_lock
);
1430 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1431 pthread_mutex_unlock(&app
->sock_lock
);
1433 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1434 ERR("Failed to set UST app exclusions for object %p of app (pid: %d) "
1435 "with ret %d", ust_object
, app
->pid
, ret
);
1438 * This is normal behavior, an application can die during the
1439 * creation process. Don't report an error so the execution can
1440 * continue normally.
1443 DBG3("Failed to set UST app object exclusions. Application is dead.");
1448 DBG2("UST exclusions set successfully for object %p", ust_object
);
1451 health_code_update();
1452 free(ust_exclusions
);
1457 * Disable the specified event on to UST tracer for the UST session.
1459 static int disable_ust_event(struct ust_app
*app
,
1460 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1464 health_code_update();
1466 pthread_mutex_lock(&app
->sock_lock
);
1467 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1468 pthread_mutex_unlock(&app
->sock_lock
);
1470 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1471 ERR("UST app event %s disable failed for app (pid: %d) "
1472 "and session handle %d with ret %d",
1473 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1476 * This is normal behavior, an application can die during the
1477 * creation process. Don't report an error so the execution can
1478 * continue normally.
1481 DBG3("UST app disable event failed. Application is dead.");
1486 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1487 ua_event
->attr
.name
, app
->pid
);
1490 health_code_update();
1495 * Disable the specified channel on to UST tracer for the UST session.
1497 static int disable_ust_channel(struct ust_app
*app
,
1498 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1502 health_code_update();
1504 pthread_mutex_lock(&app
->sock_lock
);
1505 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1506 pthread_mutex_unlock(&app
->sock_lock
);
1508 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1509 ERR("UST app channel %s disable failed for app (pid: %d) "
1510 "and session handle %d with ret %d",
1511 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1514 * This is normal behavior, an application can die during the
1515 * creation process. Don't report an error so the execution can
1516 * continue normally.
1519 DBG3("UST app disable channel failed. Application is dead.");
1524 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1525 ua_chan
->name
, app
->pid
);
1528 health_code_update();
1533 * Enable the specified channel on to UST tracer for the UST session.
1535 static int enable_ust_channel(struct ust_app
*app
,
1536 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1540 health_code_update();
1542 pthread_mutex_lock(&app
->sock_lock
);
1543 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1544 pthread_mutex_unlock(&app
->sock_lock
);
1546 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1547 ERR("UST app channel %s enable failed for app (pid: %d) "
1548 "and session handle %d with ret %d",
1549 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1552 * This is normal behavior, an application can die during the
1553 * creation process. Don't report an error so the execution can
1554 * continue normally.
1557 DBG3("UST app enable channel failed. Application is dead.");
1562 ua_chan
->enabled
= 1;
1564 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1565 ua_chan
->name
, app
->pid
);
1568 health_code_update();
1573 * Enable the specified event on to UST tracer for the UST session.
1575 static int enable_ust_event(struct ust_app
*app
,
1576 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1580 health_code_update();
1582 pthread_mutex_lock(&app
->sock_lock
);
1583 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1584 pthread_mutex_unlock(&app
->sock_lock
);
1586 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1587 ERR("UST app event %s enable failed for app (pid: %d) "
1588 "and session handle %d with ret %d",
1589 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1592 * This is normal behavior, an application can die during the
1593 * creation process. Don't report an error so the execution can
1594 * continue normally.
1597 DBG3("UST app enable event failed. Application is dead.");
1602 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1603 ua_event
->attr
.name
, app
->pid
);
1606 health_code_update();
1611 * Send channel and stream buffer to application.
1613 * Return 0 on success. On error, a negative value is returned.
1615 static int send_channel_pid_to_ust(struct ust_app
*app
,
1616 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1619 struct ust_app_stream
*stream
, *stmp
;
1625 health_code_update();
1627 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1630 /* Send channel to the application. */
1631 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1632 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1633 ret
= -ENOTCONN
; /* Caused by app exiting. */
1635 } else if (ret
< 0) {
1639 health_code_update();
1641 /* Send all streams to application. */
1642 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1643 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1644 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1645 ret
= -ENOTCONN
; /* Caused by app exiting. */
1647 } else if (ret
< 0) {
1650 /* We don't need the stream anymore once sent to the tracer. */
1651 cds_list_del(&stream
->list
);
1652 delete_ust_app_stream(-1, stream
, app
);
1654 /* Flag the channel that it is sent to the application. */
1655 ua_chan
->is_sent
= 1;
1658 health_code_update();
1663 * Create the specified event onto the UST tracer for a UST session.
1665 * Should be called with session mutex held.
1668 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1669 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1673 health_code_update();
1675 /* Create UST event on tracer */
1676 pthread_mutex_lock(&app
->sock_lock
);
1677 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1679 pthread_mutex_unlock(&app
->sock_lock
);
1681 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_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
1712 /* Set exclusions for the event */
1713 if (ua_event
->exclusion
) {
1714 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
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 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1797 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1798 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1800 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1801 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1803 /* Copy event attributes since the layout is different. */
1804 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1805 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1806 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1807 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1808 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1809 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1810 ua_chan
->attr
.output
= uchan
->attr
.output
;
1811 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
1814 * Note that the attribute channel type is not set since the channel on the
1815 * tracing registry side does not have this information.
1818 ua_chan
->enabled
= uchan
->enabled
;
1819 ua_chan
->tracing_channel_id
= uchan
->id
;
1821 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1825 * Copy data between a UST app session and a regular LTT session.
1827 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1828 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1830 struct tm
*timeinfo
;
1833 char tmp_shm_path
[PATH_MAX
];
1835 timeinfo
= localtime(&app
->registration_time
);
1836 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1838 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1840 ua_sess
->tracing_id
= usess
->id
;
1841 ua_sess
->id
= get_next_session_id();
1842 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
1843 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
1844 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
1845 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
1846 ua_sess
->buffer_type
= usess
->buffer_type
;
1847 ua_sess
->bits_per_long
= app
->bits_per_long
;
1849 /* There is only one consumer object per session possible. */
1850 consumer_output_get(usess
->consumer
);
1851 ua_sess
->consumer
= usess
->consumer
;
1853 ua_sess
->output_traces
= usess
->output_traces
;
1854 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1855 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1856 &usess
->metadata_attr
);
1858 switch (ua_sess
->buffer_type
) {
1859 case LTTNG_BUFFER_PER_PID
:
1860 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1861 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1864 case LTTNG_BUFFER_PER_UID
:
1865 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1866 DEFAULT_UST_TRACE_UID_PATH
,
1867 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
1868 app
->bits_per_long
);
1875 PERROR("asprintf UST shadow copy session");
1880 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1881 sizeof(ua_sess
->root_shm_path
));
1882 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1883 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1884 sizeof(ua_sess
->shm_path
));
1885 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1886 if (ua_sess
->shm_path
[0]) {
1887 switch (ua_sess
->buffer_type
) {
1888 case LTTNG_BUFFER_PER_PID
:
1889 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1890 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1891 app
->name
, app
->pid
, datetime
);
1893 case LTTNG_BUFFER_PER_UID
:
1894 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1895 "/" DEFAULT_UST_TRACE_UID_PATH
,
1896 app
->uid
, app
->bits_per_long
);
1903 PERROR("sprintf UST shadow copy session");
1907 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1908 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1909 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1914 consumer_output_put(ua_sess
->consumer
);
1918 * Lookup sesison wrapper.
1921 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
1922 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1924 /* Get right UST app session from app */
1925 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1929 * Return ust app session from the app session hashtable using the UST session
1932 static struct ust_app_session
*lookup_session_by_app(
1933 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
1935 struct lttng_ht_iter iter
;
1936 struct lttng_ht_node_u64
*node
;
1938 __lookup_session_by_app(usess
, app
, &iter
);
1939 node
= lttng_ht_iter_get_node_u64(&iter
);
1944 return caa_container_of(node
, struct ust_app_session
, node
);
1951 * Setup buffer registry per PID for the given session and application. If none
1952 * is found, a new one is created, added to the global registry and
1953 * initialized. If regp is valid, it's set with the newly created object.
1955 * Return 0 on success or else a negative value.
1957 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1958 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1961 struct buffer_reg_pid
*reg_pid
;
1968 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1971 * This is the create channel path meaning that if there is NO
1972 * registry available, we have to create one for this session.
1974 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
1975 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1983 /* Initialize registry. */
1984 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1985 app
->bits_per_long
, app
->uint8_t_alignment
,
1986 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1987 app
->uint64_t_alignment
, app
->long_alignment
,
1988 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
1989 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
1990 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
1991 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
1992 ua_sess
->tracing_id
,
1996 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1997 * destroy the buffer registry, because it is always expected
1998 * that if the buffer registry can be found, its ust registry is
2001 buffer_reg_pid_destroy(reg_pid
);
2005 buffer_reg_pid_add(reg_pid
);
2007 DBG3("UST app buffer registry per PID created successfully");
2019 * Setup buffer registry per UID for the given session and application. If none
2020 * is found, a new one is created, added to the global registry and
2021 * initialized. If regp is valid, it's set with the newly created object.
2023 * Return 0 on success or else a negative value.
2025 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2026 struct ust_app_session
*ua_sess
,
2027 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2030 struct buffer_reg_uid
*reg_uid
;
2037 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
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_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2044 LTTNG_DOMAIN_UST
, ®_uid
,
2045 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2053 /* Initialize registry. */
2054 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2055 app
->bits_per_long
, app
->uint8_t_alignment
,
2056 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2057 app
->uint64_t_alignment
, app
->long_alignment
,
2058 app
->byte_order
, app
->version
.major
,
2059 app
->version
.minor
, reg_uid
->root_shm_path
,
2060 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2061 ua_sess
->tracing_id
, app
->uid
);
2064 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2065 * destroy the buffer registry, because it is always expected
2066 * that if the buffer registry can be found, its ust registry is
2069 buffer_reg_uid_destroy(reg_uid
, NULL
);
2072 /* Add node to teardown list of the session. */
2073 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2075 buffer_reg_uid_add(reg_uid
);
2077 DBG3("UST app buffer registry per UID created successfully");
2088 * Create a session on the tracer side for the given app.
2090 * On success, ua_sess_ptr is populated with the session pointer or else left
2091 * untouched. If the session was created, is_created is set to 1. On error,
2092 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2095 * Returns 0 on success or else a negative code which is either -ENOMEM or
2096 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2098 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2099 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2102 int ret
, created
= 0;
2103 struct ust_app_session
*ua_sess
;
2107 assert(ua_sess_ptr
);
2109 health_code_update();
2111 ua_sess
= lookup_session_by_app(usess
, app
);
2112 if (ua_sess
== NULL
) {
2113 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2114 app
->pid
, usess
->id
);
2115 ua_sess
= alloc_ust_app_session();
2116 if (ua_sess
== NULL
) {
2117 /* Only malloc can failed so something is really wrong */
2121 shadow_copy_session(ua_sess
, usess
, app
);
2125 switch (usess
->buffer_type
) {
2126 case LTTNG_BUFFER_PER_PID
:
2127 /* Init local registry. */
2128 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2130 delete_ust_app_session(-1, ua_sess
, app
);
2134 case LTTNG_BUFFER_PER_UID
:
2135 /* Look for a global registry. If none exists, create one. */
2136 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2138 delete_ust_app_session(-1, ua_sess
, app
);
2148 health_code_update();
2150 if (ua_sess
->handle
== -1) {
2151 pthread_mutex_lock(&app
->sock_lock
);
2152 ret
= ustctl_create_session(app
->sock
);
2153 pthread_mutex_unlock(&app
->sock_lock
);
2155 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2156 ERR("Creating session for app pid %d with ret %d",
2159 DBG("UST app creating session failed. Application is dead");
2161 * This is normal behavior, an application can die during the
2162 * creation process. Don't report an error so the execution can
2163 * continue normally. This will get flagged ENOTCONN and the
2164 * caller will handle it.
2168 delete_ust_app_session(-1, ua_sess
, app
);
2169 if (ret
!= -ENOMEM
) {
2171 * Tracer is probably gone or got an internal error so let's
2172 * behave like it will soon unregister or not usable.
2179 ua_sess
->handle
= ret
;
2181 /* Add ust app session to app's HT */
2182 lttng_ht_node_init_u64(&ua_sess
->node
,
2183 ua_sess
->tracing_id
);
2184 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2185 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2186 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2187 &ua_sess
->ust_objd_node
);
2189 DBG2("UST app session created successfully with handle %d", ret
);
2192 *ua_sess_ptr
= ua_sess
;
2194 *is_created
= created
;
2197 /* Everything went well. */
2201 health_code_update();
2206 * Match function for a hash table lookup of ust_app_ctx.
2208 * It matches an ust app context based on the context type and, in the case
2209 * of perf counters, their name.
2211 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2213 struct ust_app_ctx
*ctx
;
2214 const struct lttng_ust_context_attr
*key
;
2219 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2223 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2228 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2229 if (strncmp(key
->u
.perf_counter
.name
,
2230 ctx
->ctx
.u
.perf_counter
.name
,
2231 sizeof(key
->u
.perf_counter
.name
))) {
2235 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2236 if (strcmp(key
->u
.app_ctx
.provider_name
,
2237 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2238 strcmp(key
->u
.app_ctx
.ctx_name
,
2239 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2255 * Lookup for an ust app context from an lttng_ust_context.
2257 * Must be called while holding RCU read side lock.
2258 * Return an ust_app_ctx object or NULL on error.
2261 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2262 struct lttng_ust_context_attr
*uctx
)
2264 struct lttng_ht_iter iter
;
2265 struct lttng_ht_node_ulong
*node
;
2266 struct ust_app_ctx
*app_ctx
= NULL
;
2271 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2272 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2273 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2274 node
= lttng_ht_iter_get_node_ulong(&iter
);
2279 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2286 * Create a context for the channel on the tracer.
2288 * Called with UST app session lock held and a RCU read side lock.
2291 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2292 struct lttng_ust_context_attr
*uctx
,
2293 struct ust_app
*app
)
2296 struct ust_app_ctx
*ua_ctx
;
2298 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2300 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2306 ua_ctx
= alloc_ust_app_ctx(uctx
);
2307 if (ua_ctx
== NULL
) {
2313 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2314 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2315 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2317 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2327 * Enable on the tracer side a ust app event for the session and channel.
2329 * Called with UST app session lock held.
2332 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2333 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2337 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2342 ua_event
->enabled
= 1;
2349 * Disable on the tracer side a ust app event for the session and channel.
2351 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2352 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2356 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2361 ua_event
->enabled
= 0;
2368 * Lookup ust app channel for session and disable it on the tracer side.
2371 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2372 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2376 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2381 ua_chan
->enabled
= 0;
2388 * Lookup ust app channel for session and enable it on the tracer side. This
2389 * MUST be called with a RCU read side lock acquired.
2391 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2392 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2395 struct lttng_ht_iter iter
;
2396 struct lttng_ht_node_str
*ua_chan_node
;
2397 struct ust_app_channel
*ua_chan
;
2399 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2400 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2401 if (ua_chan_node
== NULL
) {
2402 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2403 uchan
->name
, ua_sess
->tracing_id
);
2407 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2409 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2419 * Ask the consumer to create a channel and get it if successful.
2421 * Called with UST app session lock held.
2423 * Return 0 on success or else a negative value.
2425 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2426 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2427 int bitness
, struct ust_registry_session
*registry
,
2428 uint64_t trace_archive_id
)
2431 unsigned int nb_fd
= 0;
2432 struct consumer_socket
*socket
;
2440 health_code_update();
2442 /* Get the right consumer socket for the application. */
2443 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2449 health_code_update();
2451 /* Need one fd for the channel. */
2452 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2454 ERR("Exhausted number of available FD upon create channel");
2459 * Ask consumer to create channel. The consumer will return the number of
2460 * stream we have to expect.
2462 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2463 registry
, usess
->current_trace_chunk
);
2469 * Compute the number of fd needed before receiving them. It must be 2 per
2470 * stream (2 being the default value here).
2472 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2474 /* Reserve the amount of file descriptor we need. */
2475 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2477 ERR("Exhausted number of available FD upon create channel");
2478 goto error_fd_get_stream
;
2481 health_code_update();
2484 * Now get the channel from the consumer. This call wil populate the stream
2485 * list of that channel and set the ust objects.
2487 if (usess
->consumer
->enabled
) {
2488 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2498 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2499 error_fd_get_stream
:
2501 * Initiate a destroy channel on the consumer since we had an error
2502 * handling it on our side. The return value is of no importance since we
2503 * already have a ret value set by the previous error that we need to
2506 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2508 lttng_fd_put(LTTNG_FD_APPS
, 1);
2510 health_code_update();
2516 * Duplicate the ust data object of the ust app stream and save it in the
2517 * buffer registry stream.
2519 * Return 0 on success or else a negative value.
2521 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2522 struct ust_app_stream
*stream
)
2529 /* Reserve the amount of file descriptor we need. */
2530 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2532 ERR("Exhausted number of available FD upon duplicate stream");
2536 /* Duplicate object for stream once the original is in the registry. */
2537 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2538 reg_stream
->obj
.ust
);
2540 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2541 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2542 lttng_fd_put(LTTNG_FD_APPS
, 2);
2545 stream
->handle
= stream
->obj
->handle
;
2552 * Duplicate the ust data object of the ust app. channel and save it in the
2553 * buffer registry channel.
2555 * Return 0 on success or else a negative value.
2557 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2558 struct ust_app_channel
*ua_chan
)
2565 /* Need two fds for the channel. */
2566 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2568 ERR("Exhausted number of available FD upon duplicate channel");
2572 /* Duplicate object for stream once the original is in the registry. */
2573 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2575 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2576 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2579 ua_chan
->handle
= ua_chan
->obj
->handle
;
2584 lttng_fd_put(LTTNG_FD_APPS
, 1);
2590 * For a given channel buffer registry, setup all streams of the given ust
2591 * application channel.
2593 * Return 0 on success or else a negative value.
2595 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2596 struct ust_app_channel
*ua_chan
,
2597 struct ust_app
*app
)
2600 struct ust_app_stream
*stream
, *stmp
;
2605 DBG2("UST app setup buffer registry stream");
2607 /* Send all streams to application. */
2608 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2609 struct buffer_reg_stream
*reg_stream
;
2611 ret
= buffer_reg_stream_create(®_stream
);
2617 * Keep original pointer and nullify it in the stream so the delete
2618 * stream call does not release the object.
2620 reg_stream
->obj
.ust
= stream
->obj
;
2622 buffer_reg_stream_add(reg_stream
, reg_chan
);
2624 /* We don't need the streams anymore. */
2625 cds_list_del(&stream
->list
);
2626 delete_ust_app_stream(-1, stream
, app
);
2634 * Create a buffer registry channel for the given session registry and
2635 * application channel object. If regp pointer is valid, it's set with the
2636 * created object. Important, the created object is NOT added to the session
2637 * registry hash table.
2639 * Return 0 on success else a negative value.
2641 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2642 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2645 struct buffer_reg_channel
*reg_chan
= NULL
;
2650 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2652 /* Create buffer registry channel. */
2653 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2658 reg_chan
->consumer_key
= ua_chan
->key
;
2659 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2660 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2662 /* Create and add a channel registry to session. */
2663 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2664 ua_chan
->tracing_channel_id
);
2668 buffer_reg_channel_add(reg_sess
, reg_chan
);
2677 /* Safe because the registry channel object was not added to any HT. */
2678 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2684 * Setup buffer registry channel for the given session registry and application
2685 * channel object. If regp pointer is valid, it's set with the created object.
2687 * Return 0 on success else a negative value.
2689 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2690 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2691 struct ust_app
*app
)
2698 assert(ua_chan
->obj
);
2700 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2702 /* Setup all streams for the registry. */
2703 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2708 reg_chan
->obj
.ust
= ua_chan
->obj
;
2709 ua_chan
->obj
= NULL
;
2714 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2715 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2720 * Send buffer registry channel to the application.
2722 * Return 0 on success else a negative value.
2724 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2725 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2726 struct ust_app_channel
*ua_chan
)
2729 struct buffer_reg_stream
*reg_stream
;
2736 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2738 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2743 /* Send channel to the application. */
2744 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2745 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2746 ret
= -ENOTCONN
; /* Caused by app exiting. */
2748 } else if (ret
< 0) {
2752 health_code_update();
2754 /* Send all streams to application. */
2755 pthread_mutex_lock(®_chan
->stream_list_lock
);
2756 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2757 struct ust_app_stream stream
;
2759 ret
= duplicate_stream_object(reg_stream
, &stream
);
2761 goto error_stream_unlock
;
2764 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2766 (void) release_ust_app_stream(-1, &stream
, app
);
2767 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2768 ret
= -ENOTCONN
; /* Caused by app exiting. */
2770 goto error_stream_unlock
;
2774 * The return value is not important here. This function will output an
2777 (void) release_ust_app_stream(-1, &stream
, app
);
2779 ua_chan
->is_sent
= 1;
2781 error_stream_unlock
:
2782 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2788 * Create and send to the application the created buffers with per UID buffers.
2790 * This MUST be called with a RCU read side lock acquired.
2791 * The session list lock and the session's lock must be acquired.
2793 * Return 0 on success else a negative value.
2795 static int create_channel_per_uid(struct ust_app
*app
,
2796 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2797 struct ust_app_channel
*ua_chan
)
2800 struct buffer_reg_uid
*reg_uid
;
2801 struct buffer_reg_channel
*reg_chan
;
2802 struct ltt_session
*session
= NULL
;
2803 enum lttng_error_code notification_ret
;
2804 struct ust_registry_channel
*chan_reg
;
2811 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2813 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2815 * The session creation handles the creation of this global registry
2816 * object. If none can be find, there is a code flow problem or a
2821 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2827 /* Create the buffer registry channel object. */
2828 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2830 ERR("Error creating the UST channel \"%s\" registry instance",
2835 session
= session_find_by_id(ua_sess
->tracing_id
);
2837 assert(pthread_mutex_trylock(&session
->lock
));
2838 assert(session_trylock_list());
2841 * Create the buffers on the consumer side. This call populates the
2842 * ust app channel object with all streams and data object.
2844 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2845 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
2846 session
->most_recent_chunk_id
.value
);
2848 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2852 * Let's remove the previously created buffer registry channel so
2853 * it's not visible anymore in the session registry.
2855 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2856 ua_chan
->tracing_channel_id
, false);
2857 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2858 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2863 * Setup the streams and add it to the session registry.
2865 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2866 ua_chan
, reg_chan
, app
);
2868 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
2872 /* Notify the notification subsystem of the channel's creation. */
2873 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2874 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2875 ua_chan
->tracing_channel_id
);
2877 chan_reg
->consumer_key
= ua_chan
->key
;
2879 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2881 notification_ret
= notification_thread_command_add_channel(
2882 notification_thread_handle
, session
->name
,
2883 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2884 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2886 ua_chan
->key
, LTTNG_DOMAIN_UST
,
2887 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2888 if (notification_ret
!= LTTNG_OK
) {
2889 ret
= - (int) notification_ret
;
2890 ERR("Failed to add channel to notification thread");
2895 /* Send buffers to the application. */
2896 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2898 if (ret
!= -ENOTCONN
) {
2899 ERR("Error sending channel to application");
2906 session_put(session
);
2912 * Create and send to the application the created buffers with per PID buffers.
2914 * Called with UST app session lock held.
2915 * The session list lock and the session's lock must be acquired.
2917 * Return 0 on success else a negative value.
2919 static int create_channel_per_pid(struct ust_app
*app
,
2920 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2921 struct ust_app_channel
*ua_chan
)
2924 struct ust_registry_session
*registry
;
2925 enum lttng_error_code cmd_ret
;
2926 struct ltt_session
*session
= NULL
;
2927 uint64_t chan_reg_key
;
2928 struct ust_registry_channel
*chan_reg
;
2935 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2939 registry
= get_session_registry(ua_sess
);
2940 /* The UST app session lock is held, registry shall not be null. */
2943 /* Create and add a new channel registry to session. */
2944 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2946 ERR("Error creating the UST channel \"%s\" registry instance",
2951 session
= session_find_by_id(ua_sess
->tracing_id
);
2954 assert(pthread_mutex_trylock(&session
->lock
));
2955 assert(session_trylock_list());
2957 /* Create and get channel on the consumer side. */
2958 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2959 app
->bits_per_long
, registry
,
2960 session
->most_recent_chunk_id
.value
);
2962 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2964 goto error_remove_from_registry
;
2967 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2969 if (ret
!= -ENOTCONN
) {
2970 ERR("Error sending channel to application");
2972 goto error_remove_from_registry
;
2975 chan_reg_key
= ua_chan
->key
;
2976 pthread_mutex_lock(®istry
->lock
);
2977 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
2979 chan_reg
->consumer_key
= ua_chan
->key
;
2980 pthread_mutex_unlock(®istry
->lock
);
2982 cmd_ret
= notification_thread_command_add_channel(
2983 notification_thread_handle
, session
->name
,
2984 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2985 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2987 ua_chan
->key
, LTTNG_DOMAIN_UST
,
2988 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2989 if (cmd_ret
!= LTTNG_OK
) {
2990 ret
= - (int) cmd_ret
;
2991 ERR("Failed to add channel to notification thread");
2992 goto error_remove_from_registry
;
2995 error_remove_from_registry
:
2997 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3002 session_put(session
);
3008 * From an already allocated ust app channel, create the channel buffers if
3009 * needed and send them to the application. This MUST be called with a RCU read
3010 * side lock acquired.
3012 * Called with UST app session lock held.
3014 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3015 * the application exited concurrently.
3017 static int ust_app_channel_send(struct ust_app
*app
,
3018 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3019 struct ust_app_channel
*ua_chan
)
3025 assert(usess
->active
);
3029 /* Handle buffer type before sending the channel to the application. */
3030 switch (usess
->buffer_type
) {
3031 case LTTNG_BUFFER_PER_UID
:
3033 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3039 case LTTNG_BUFFER_PER_PID
:
3041 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3053 /* Initialize ust objd object using the received handle and add it. */
3054 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3055 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3057 /* If channel is not enabled, disable it on the tracer */
3058 if (!ua_chan
->enabled
) {
3059 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3070 * Create UST app channel and return it through ua_chanp if not NULL.
3072 * Called with UST app session lock and RCU read-side lock held.
3074 * Return 0 on success or else a negative value.
3076 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3077 struct ltt_ust_channel
*uchan
,
3078 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3079 struct ust_app_channel
**ua_chanp
)
3082 struct lttng_ht_iter iter
;
3083 struct lttng_ht_node_str
*ua_chan_node
;
3084 struct ust_app_channel
*ua_chan
;
3086 /* Lookup channel in the ust app session */
3087 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3088 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3089 if (ua_chan_node
!= NULL
) {
3090 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3094 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3095 if (ua_chan
== NULL
) {
3096 /* Only malloc can fail here */
3100 shadow_copy_channel(ua_chan
, uchan
);
3102 /* Set channel type. */
3103 ua_chan
->attr
.type
= type
;
3105 /* Only add the channel if successful on the tracer side. */
3106 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3109 *ua_chanp
= ua_chan
;
3112 /* Everything went well. */
3120 * Create UST app event and create it on the tracer side.
3122 * Called with ust app session mutex held.
3125 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3126 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3127 struct ust_app
*app
)
3130 struct ust_app_event
*ua_event
;
3132 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3133 if (ua_event
== NULL
) {
3134 /* Only failure mode of alloc_ust_app_event(). */
3138 shadow_copy_event(ua_event
, uevent
);
3140 /* Create it on the tracer side */
3141 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3144 * Not found previously means that it does not exist on the
3145 * tracer. If the application reports that the event existed,
3146 * it means there is a bug in the sessiond or lttng-ust
3147 * (or corruption, etc.)
3149 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3150 ERR("Tracer for application reported that an event being created already existed: "
3151 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3153 app
->pid
, app
->ppid
, app
->uid
,
3159 add_unique_ust_app_event(ua_chan
, ua_event
);
3161 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3168 /* Valid. Calling here is already in a read side lock */
3169 delete_ust_app_event(-1, ua_event
, app
);
3174 * Create UST metadata and open it on the tracer side.
3176 * Called with UST app session lock held and RCU read side lock.
3178 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3179 struct ust_app
*app
, struct consumer_output
*consumer
)
3182 struct ust_app_channel
*metadata
;
3183 struct consumer_socket
*socket
;
3184 struct ust_registry_session
*registry
;
3185 struct ltt_session
*session
= NULL
;
3191 registry
= get_session_registry(ua_sess
);
3192 /* The UST app session is held registry shall not be null. */
3195 pthread_mutex_lock(®istry
->lock
);
3197 /* Metadata already exists for this registry or it was closed previously */
3198 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3203 /* Allocate UST metadata */
3204 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3206 /* malloc() failed */
3211 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3213 /* Need one fd for the channel. */
3214 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3216 ERR("Exhausted number of available FD upon create metadata");
3220 /* Get the right consumer socket for the application. */
3221 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3224 goto error_consumer
;
3228 * Keep metadata key so we can identify it on the consumer side. Assign it
3229 * to the registry *before* we ask the consumer so we avoid the race of the
3230 * consumer requesting the metadata and the ask_channel call on our side
3231 * did not returned yet.
3233 registry
->metadata_key
= metadata
->key
;
3235 session
= session_find_by_id(ua_sess
->tracing_id
);
3238 assert(pthread_mutex_trylock(&session
->lock
));
3239 assert(session_trylock_list());
3242 * Ask the metadata channel creation to the consumer. The metadata object
3243 * will be created by the consumer and kept their. However, the stream is
3244 * never added or monitored until we do a first push metadata to the
3247 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3248 registry
, session
->current_trace_chunk
);
3250 /* Nullify the metadata key so we don't try to close it later on. */
3251 registry
->metadata_key
= 0;
3252 goto error_consumer
;
3256 * The setup command will make the metadata stream be sent to the relayd,
3257 * if applicable, and the thread managing the metadatas. This is important
3258 * because after this point, if an error occurs, the only way the stream
3259 * can be deleted is to be monitored in the consumer.
3261 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3263 /* Nullify the metadata key so we don't try to close it later on. */
3264 registry
->metadata_key
= 0;
3265 goto error_consumer
;
3268 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3269 metadata
->key
, app
->pid
);
3272 lttng_fd_put(LTTNG_FD_APPS
, 1);
3273 delete_ust_app_channel(-1, metadata
, app
);
3275 pthread_mutex_unlock(®istry
->lock
);
3277 session_put(session
);
3283 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3284 * acquired before calling this function.
3286 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3288 struct ust_app
*app
= NULL
;
3289 struct lttng_ht_node_ulong
*node
;
3290 struct lttng_ht_iter iter
;
3292 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3293 node
= lttng_ht_iter_get_node_ulong(&iter
);
3295 DBG2("UST app no found with pid %d", pid
);
3299 DBG2("Found UST app by pid %d", pid
);
3301 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3308 * Allocate and init an UST app object using the registration information and
3309 * the command socket. This is called when the command socket connects to the
3312 * The object is returned on success or else NULL.
3314 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3316 struct ust_app
*lta
= NULL
;
3321 DBG3("UST app creating application for socket %d", sock
);
3323 if ((msg
->bits_per_long
== 64 &&
3324 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3325 || (msg
->bits_per_long
== 32 &&
3326 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3327 ERR("Registration failed: application \"%s\" (pid: %d) has "
3328 "%d-bit long, but no consumerd for this size is available.\n",
3329 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3333 lta
= zmalloc(sizeof(struct ust_app
));
3339 lta
->ppid
= msg
->ppid
;
3340 lta
->uid
= msg
->uid
;
3341 lta
->gid
= msg
->gid
;
3343 lta
->bits_per_long
= msg
->bits_per_long
;
3344 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3345 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3346 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3347 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3348 lta
->long_alignment
= msg
->long_alignment
;
3349 lta
->byte_order
= msg
->byte_order
;
3351 lta
->v_major
= msg
->major
;
3352 lta
->v_minor
= msg
->minor
;
3353 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3354 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3355 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3356 lta
->notify_sock
= -1;
3358 /* Copy name and make sure it's NULL terminated. */
3359 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3360 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3363 * Before this can be called, when receiving the registration information,
3364 * the application compatibility is checked. So, at this point, the
3365 * application can work with this session daemon.
3367 lta
->compatible
= 1;
3369 lta
->pid
= msg
->pid
;
3370 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3372 pthread_mutex_init(<a
->sock_lock
, NULL
);
3373 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3375 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3381 * For a given application object, add it to every hash table.
3383 void ust_app_add(struct ust_app
*app
)
3386 assert(app
->notify_sock
>= 0);
3388 app
->registration_time
= time(NULL
);
3393 * On a re-registration, we want to kick out the previous registration of
3396 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3399 * The socket _should_ be unique until _we_ call close. So, a add_unique
3400 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3401 * already in the table.
3403 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3405 /* Add application to the notify socket hash table. */
3406 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3407 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3409 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3410 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3411 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3418 * Set the application version into the object.
3420 * Return 0 on success else a negative value either an errno code or a
3421 * LTTng-UST error code.
3423 int ust_app_version(struct ust_app
*app
)
3429 pthread_mutex_lock(&app
->sock_lock
);
3430 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3431 pthread_mutex_unlock(&app
->sock_lock
);
3433 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3434 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3436 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3444 * Unregister app by removing it from the global traceable app list and freeing
3447 * The socket is already closed at this point so no close to sock.
3449 void ust_app_unregister(int sock
)
3451 struct ust_app
*lta
;
3452 struct lttng_ht_node_ulong
*node
;
3453 struct lttng_ht_iter ust_app_sock_iter
;
3454 struct lttng_ht_iter iter
;
3455 struct ust_app_session
*ua_sess
;
3460 /* Get the node reference for a call_rcu */
3461 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3462 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3465 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3466 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3469 * For per-PID buffers, perform "push metadata" and flush all
3470 * application streams before removing app from hash tables,
3471 * ensuring proper behavior of data_pending check.
3472 * Remove sessions so they are not visible during deletion.
3474 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3476 struct ust_registry_session
*registry
;
3478 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3480 /* The session was already removed so scheduled for teardown. */
3484 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3485 (void) ust_app_flush_app_session(lta
, ua_sess
);
3489 * Add session to list for teardown. This is safe since at this point we
3490 * are the only one using this list.
3492 pthread_mutex_lock(&ua_sess
->lock
);
3494 if (ua_sess
->deleted
) {
3495 pthread_mutex_unlock(&ua_sess
->lock
);
3500 * Normally, this is done in the delete session process which is
3501 * executed in the call rcu below. However, upon registration we can't
3502 * afford to wait for the grace period before pushing data or else the
3503 * data pending feature can race between the unregistration and stop
3504 * command where the data pending command is sent *before* the grace
3507 * The close metadata below nullifies the metadata pointer in the
3508 * session so the delete session will NOT push/close a second time.
3510 registry
= get_session_registry(ua_sess
);
3512 /* Push metadata for application before freeing the application. */
3513 (void) push_metadata(registry
, ua_sess
->consumer
);
3516 * Don't ask to close metadata for global per UID buffers. Close
3517 * metadata only on destroy trace session in this case. Also, the
3518 * previous push metadata could have flag the metadata registry to
3519 * close so don't send a close command if closed.
3521 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3522 /* And ask to close it for this session registry. */
3523 (void) close_metadata(registry
, ua_sess
->consumer
);
3526 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3528 pthread_mutex_unlock(&ua_sess
->lock
);
3531 /* Remove application from PID hash table */
3532 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3536 * Remove application from notify hash table. The thread handling the
3537 * notify socket could have deleted the node so ignore on error because
3538 * either way it's valid. The close of that socket is handled by the
3539 * apps_notify_thread.
3541 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3542 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3545 * Ignore return value since the node might have been removed before by an
3546 * add replace during app registration because the PID can be reassigned by
3549 iter
.iter
.node
= <a
->pid_n
.node
;
3550 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3552 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3557 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3564 * Fill events array with all events name of all registered apps.
3566 int ust_app_list_events(struct lttng_event
**events
)
3569 size_t nbmem
, count
= 0;
3570 struct lttng_ht_iter iter
;
3571 struct ust_app
*app
;
3572 struct lttng_event
*tmp_event
;
3574 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3575 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3576 if (tmp_event
== NULL
) {
3577 PERROR("zmalloc ust app events");
3584 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3585 struct lttng_ust_tracepoint_iter uiter
;
3587 health_code_update();
3589 if (!app
->compatible
) {
3591 * TODO: In time, we should notice the caller of this error by
3592 * telling him that this is a version error.
3596 pthread_mutex_lock(&app
->sock_lock
);
3597 handle
= ustctl_tracepoint_list(app
->sock
);
3599 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3600 ERR("UST app list events getting handle failed for app pid %d",
3603 pthread_mutex_unlock(&app
->sock_lock
);
3607 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3608 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3609 /* Handle ustctl error. */
3613 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3614 ERR("UST app tp list get failed for app %d with ret %d",
3617 DBG3("UST app tp list get failed. Application is dead");
3619 * This is normal behavior, an application can die during the
3620 * creation process. Don't report an error so the execution can
3621 * continue normally. Continue normal execution.
3626 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3627 if (release_ret
< 0 &&
3628 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3629 release_ret
!= -EPIPE
) {
3630 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3632 pthread_mutex_unlock(&app
->sock_lock
);
3636 health_code_update();
3637 if (count
>= nbmem
) {
3638 /* In case the realloc fails, we free the memory */
3639 struct lttng_event
*new_tmp_event
;
3642 new_nbmem
= nbmem
<< 1;
3643 DBG2("Reallocating event list from %zu to %zu entries",
3645 new_tmp_event
= realloc(tmp_event
,
3646 new_nbmem
* sizeof(struct lttng_event
));
3647 if (new_tmp_event
== NULL
) {
3650 PERROR("realloc ust app events");
3653 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3654 if (release_ret
< 0 &&
3655 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3656 release_ret
!= -EPIPE
) {
3657 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3659 pthread_mutex_unlock(&app
->sock_lock
);
3662 /* Zero the new memory */
3663 memset(new_tmp_event
+ nbmem
, 0,
3664 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3666 tmp_event
= new_tmp_event
;
3668 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3669 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3670 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3671 tmp_event
[count
].pid
= app
->pid
;
3672 tmp_event
[count
].enabled
= -1;
3675 ret
= ustctl_release_handle(app
->sock
, handle
);
3676 pthread_mutex_unlock(&app
->sock_lock
);
3677 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3678 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3683 *events
= tmp_event
;
3685 DBG2("UST app list events done (%zu events)", count
);
3690 health_code_update();
3695 * Fill events array with all events name of all registered apps.
3697 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3700 size_t nbmem
, count
= 0;
3701 struct lttng_ht_iter iter
;
3702 struct ust_app
*app
;
3703 struct lttng_event_field
*tmp_event
;
3705 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3706 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3707 if (tmp_event
== NULL
) {
3708 PERROR("zmalloc ust app event fields");
3715 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3716 struct lttng_ust_field_iter uiter
;
3718 health_code_update();
3720 if (!app
->compatible
) {
3722 * TODO: In time, we should notice the caller of this error by
3723 * telling him that this is a version error.
3727 pthread_mutex_lock(&app
->sock_lock
);
3728 handle
= ustctl_tracepoint_field_list(app
->sock
);
3730 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3731 ERR("UST app list field getting handle failed for app pid %d",
3734 pthread_mutex_unlock(&app
->sock_lock
);
3738 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3739 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3740 /* Handle ustctl error. */
3744 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3745 ERR("UST app tp list field failed for app %d with ret %d",
3748 DBG3("UST app tp list field failed. Application is dead");
3750 * This is normal behavior, an application can die during the
3751 * creation process. Don't report an error so the execution can
3752 * continue normally. Reset list and count for next app.
3757 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3758 pthread_mutex_unlock(&app
->sock_lock
);
3759 if (release_ret
< 0 &&
3760 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3761 release_ret
!= -EPIPE
) {
3762 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3767 health_code_update();
3768 if (count
>= nbmem
) {
3769 /* In case the realloc fails, we free the memory */
3770 struct lttng_event_field
*new_tmp_event
;
3773 new_nbmem
= nbmem
<< 1;
3774 DBG2("Reallocating event field list from %zu to %zu entries",
3776 new_tmp_event
= realloc(tmp_event
,
3777 new_nbmem
* sizeof(struct lttng_event_field
));
3778 if (new_tmp_event
== NULL
) {
3781 PERROR("realloc ust app event fields");
3784 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3785 pthread_mutex_unlock(&app
->sock_lock
);
3787 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3788 release_ret
!= -EPIPE
) {
3789 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3793 /* Zero the new memory */
3794 memset(new_tmp_event
+ nbmem
, 0,
3795 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3797 tmp_event
= new_tmp_event
;
3800 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3801 /* Mapping between these enums matches 1 to 1. */
3802 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3803 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3805 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3806 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3807 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3808 tmp_event
[count
].event
.pid
= app
->pid
;
3809 tmp_event
[count
].event
.enabled
= -1;
3812 ret
= ustctl_release_handle(app
->sock
, handle
);
3813 pthread_mutex_unlock(&app
->sock_lock
);
3815 ret
!= -LTTNG_UST_ERR_EXITING
&&
3817 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3822 *fields
= tmp_event
;
3824 DBG2("UST app list event fields done (%zu events)", count
);
3829 health_code_update();
3834 * Free and clean all traceable apps of the global list.
3836 * Should _NOT_ be called with RCU read-side lock held.
3838 void ust_app_clean_list(void)
3841 struct ust_app
*app
;
3842 struct lttng_ht_iter iter
;
3844 DBG2("UST app cleaning registered apps hash table");
3848 /* Cleanup notify socket hash table */
3849 if (ust_app_ht_by_notify_sock
) {
3850 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3851 notify_sock_n
.node
) {
3852 struct cds_lfht_node
*node
;
3853 struct ust_app
*app
;
3855 node
= cds_lfht_iter_get_node(&iter
.iter
);
3860 app
= container_of(node
, struct ust_app
,
3861 notify_sock_n
.node
);
3862 ust_app_notify_sock_unregister(app
->notify_sock
);
3867 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3868 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3870 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3874 /* Cleanup socket hash table */
3875 if (ust_app_ht_by_sock
) {
3876 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3878 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3885 /* Destroy is done only when the ht is empty */
3887 ht_cleanup_push(ust_app_ht
);
3889 if (ust_app_ht_by_sock
) {
3890 ht_cleanup_push(ust_app_ht_by_sock
);
3892 if (ust_app_ht_by_notify_sock
) {
3893 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3898 * Init UST app hash table.
3900 int ust_app_ht_alloc(void)
3902 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3906 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3907 if (!ust_app_ht_by_sock
) {
3910 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3911 if (!ust_app_ht_by_notify_sock
) {
3918 * For a specific UST session, disable the channel for all registered apps.
3920 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3921 struct ltt_ust_channel
*uchan
)
3924 struct lttng_ht_iter iter
;
3925 struct lttng_ht_node_str
*ua_chan_node
;
3926 struct ust_app
*app
;
3927 struct ust_app_session
*ua_sess
;
3928 struct ust_app_channel
*ua_chan
;
3930 assert(usess
->active
);
3931 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3932 uchan
->name
, usess
->id
);
3936 /* For every registered applications */
3937 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3938 struct lttng_ht_iter uiter
;
3939 if (!app
->compatible
) {
3941 * TODO: In time, we should notice the caller of this error by
3942 * telling him that this is a version error.
3946 ua_sess
= lookup_session_by_app(usess
, app
);
3947 if (ua_sess
== NULL
) {
3952 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3953 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3954 /* If the session if found for the app, the channel must be there */
3955 assert(ua_chan_node
);
3957 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3958 /* The channel must not be already disabled */
3959 assert(ua_chan
->enabled
== 1);
3961 /* Disable channel onto application */
3962 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3964 /* XXX: We might want to report this error at some point... */
3974 * For a specific UST session, enable the channel for all registered apps.
3976 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3977 struct ltt_ust_channel
*uchan
)
3980 struct lttng_ht_iter iter
;
3981 struct ust_app
*app
;
3982 struct ust_app_session
*ua_sess
;
3984 assert(usess
->active
);
3985 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3986 uchan
->name
, usess
->id
);
3990 /* For every registered applications */
3991 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3992 if (!app
->compatible
) {
3994 * TODO: In time, we should notice the caller of this error by
3995 * telling him that this is a version error.
3999 ua_sess
= lookup_session_by_app(usess
, app
);
4000 if (ua_sess
== NULL
) {
4004 /* Enable channel onto application */
4005 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4007 /* XXX: We might want to report this error at some point... */
4017 * Disable an event in a channel and for a specific session.
4019 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4020 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4023 struct lttng_ht_iter iter
, uiter
;
4024 struct lttng_ht_node_str
*ua_chan_node
;
4025 struct ust_app
*app
;
4026 struct ust_app_session
*ua_sess
;
4027 struct ust_app_channel
*ua_chan
;
4028 struct ust_app_event
*ua_event
;
4030 assert(usess
->active
);
4031 DBG("UST app disabling event %s for all apps in channel "
4032 "%s for session id %" PRIu64
,
4033 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4037 /* For all registered applications */
4038 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4039 if (!app
->compatible
) {
4041 * TODO: In time, we should notice the caller of this error by
4042 * telling him that this is a version error.
4046 ua_sess
= lookup_session_by_app(usess
, app
);
4047 if (ua_sess
== NULL
) {
4052 /* Lookup channel in the ust app session */
4053 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4054 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4055 if (ua_chan_node
== NULL
) {
4056 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4057 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4060 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4062 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4063 uevent
->filter
, uevent
->attr
.loglevel
,
4065 if (ua_event
== NULL
) {
4066 DBG2("Event %s not found in channel %s for app pid %d."
4067 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4071 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4073 /* XXX: Report error someday... */
4082 /* The ua_sess lock must be held by the caller. */
4084 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4085 struct ust_app_session
*ua_sess
,
4086 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4087 struct ust_app_channel
**_ua_chan
)
4090 struct ust_app_channel
*ua_chan
= NULL
;
4093 ASSERT_LOCKED(ua_sess
->lock
);
4095 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4096 sizeof(uchan
->name
))) {
4097 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4101 struct ltt_ust_context
*uctx
= NULL
;
4104 * Create channel onto application and synchronize its
4107 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4108 LTTNG_UST_CHAN_PER_CPU
, usess
,
4114 ret
= ust_app_channel_send(app
, usess
,
4121 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4122 ret
= create_ust_app_channel_context(ua_chan
,
4135 * The application's socket is not valid. Either a bad socket
4136 * or a timeout on it. We can't inform the caller that for a
4137 * specific app, the session failed so lets continue here.
4139 ret
= 0; /* Not an error. */
4147 if (ret
== 0 && _ua_chan
) {
4149 * Only return the application's channel on success. Note
4150 * that the channel can still be part of the application's
4151 * channel hashtable on error.
4153 *_ua_chan
= ua_chan
;
4159 * Enable event for a specific session and channel on the tracer.
4161 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4162 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4165 struct lttng_ht_iter iter
, uiter
;
4166 struct lttng_ht_node_str
*ua_chan_node
;
4167 struct ust_app
*app
;
4168 struct ust_app_session
*ua_sess
;
4169 struct ust_app_channel
*ua_chan
;
4170 struct ust_app_event
*ua_event
;
4172 assert(usess
->active
);
4173 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4174 uevent
->attr
.name
, usess
->id
);
4177 * NOTE: At this point, this function is called only if the session and
4178 * channel passed are already created for all apps. and enabled on the
4184 /* For all registered applications */
4185 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4186 if (!app
->compatible
) {
4188 * TODO: In time, we should notice the caller of this error by
4189 * telling him that this is a version error.
4193 ua_sess
= lookup_session_by_app(usess
, app
);
4195 /* The application has problem or is probably dead. */
4199 pthread_mutex_lock(&ua_sess
->lock
);
4201 if (ua_sess
->deleted
) {
4202 pthread_mutex_unlock(&ua_sess
->lock
);
4206 /* Lookup channel in the ust app session */
4207 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4208 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4210 * It is possible that the channel cannot be found is
4211 * the channel/event creation occurs concurrently with
4212 * an application exit.
4214 if (!ua_chan_node
) {
4215 pthread_mutex_unlock(&ua_sess
->lock
);
4219 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4221 /* Get event node */
4222 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4223 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4224 if (ua_event
== NULL
) {
4225 DBG3("UST app enable event %s not found for app PID %d."
4226 "Skipping app", uevent
->attr
.name
, app
->pid
);
4230 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4232 pthread_mutex_unlock(&ua_sess
->lock
);
4236 pthread_mutex_unlock(&ua_sess
->lock
);
4245 * For a specific existing UST session and UST channel, creates the event for
4246 * all registered apps.
4248 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4249 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4252 struct lttng_ht_iter iter
, uiter
;
4253 struct lttng_ht_node_str
*ua_chan_node
;
4254 struct ust_app
*app
;
4255 struct ust_app_session
*ua_sess
;
4256 struct ust_app_channel
*ua_chan
;
4258 assert(usess
->active
);
4259 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4260 uevent
->attr
.name
, usess
->id
);
4264 /* For all registered applications */
4265 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4266 if (!app
->compatible
) {
4268 * TODO: In time, we should notice the caller of this error by
4269 * telling him that this is a version error.
4273 ua_sess
= lookup_session_by_app(usess
, app
);
4275 /* The application has problem or is probably dead. */
4279 pthread_mutex_lock(&ua_sess
->lock
);
4281 if (ua_sess
->deleted
) {
4282 pthread_mutex_unlock(&ua_sess
->lock
);
4286 /* Lookup channel in the ust app session */
4287 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4288 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4289 /* If the channel is not found, there is a code flow error */
4290 assert(ua_chan_node
);
4292 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4294 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4295 pthread_mutex_unlock(&ua_sess
->lock
);
4297 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4298 /* Possible value at this point: -ENOMEM. If so, we stop! */
4301 DBG2("UST app event %s already exist on app PID %d",
4302 uevent
->attr
.name
, app
->pid
);
4312 * Start tracing for a specific UST session and app.
4314 * Called with UST app session lock held.
4318 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4321 struct ust_app_session
*ua_sess
;
4323 DBG("Starting tracing for ust app pid %d", app
->pid
);
4327 if (!app
->compatible
) {
4331 ua_sess
= lookup_session_by_app(usess
, app
);
4332 if (ua_sess
== NULL
) {
4333 /* The session is in teardown process. Ignore and continue. */
4337 pthread_mutex_lock(&ua_sess
->lock
);
4339 if (ua_sess
->deleted
) {
4340 pthread_mutex_unlock(&ua_sess
->lock
);
4344 if (ua_sess
->enabled
) {
4345 pthread_mutex_unlock(&ua_sess
->lock
);
4349 /* Upon restart, we skip the setup, already done */
4350 if (ua_sess
->started
) {
4354 health_code_update();
4357 /* This starts the UST tracing */
4358 pthread_mutex_lock(&app
->sock_lock
);
4359 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4360 pthread_mutex_unlock(&app
->sock_lock
);
4362 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4363 ERR("Error starting tracing for app pid: %d (ret: %d)",
4366 DBG("UST app start session failed. Application is dead.");
4368 * This is normal behavior, an application can die during the
4369 * creation process. Don't report an error so the execution can
4370 * continue normally.
4372 pthread_mutex_unlock(&ua_sess
->lock
);
4378 /* Indicate that the session has been started once */
4379 ua_sess
->started
= 1;
4380 ua_sess
->enabled
= 1;
4382 pthread_mutex_unlock(&ua_sess
->lock
);
4384 health_code_update();
4386 /* Quiescent wait after starting trace */
4387 pthread_mutex_lock(&app
->sock_lock
);
4388 ret
= ustctl_wait_quiescent(app
->sock
);
4389 pthread_mutex_unlock(&app
->sock_lock
);
4390 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4391 ERR("UST app wait quiescent failed for app pid %d ret %d",
4397 health_code_update();
4401 pthread_mutex_unlock(&ua_sess
->lock
);
4403 health_code_update();
4408 * Stop tracing for a specific UST session and app.
4411 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4414 struct ust_app_session
*ua_sess
;
4415 struct ust_registry_session
*registry
;
4417 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4421 if (!app
->compatible
) {
4422 goto end_no_session
;
4425 ua_sess
= lookup_session_by_app(usess
, app
);
4426 if (ua_sess
== NULL
) {
4427 goto end_no_session
;
4430 pthread_mutex_lock(&ua_sess
->lock
);
4432 if (ua_sess
->deleted
) {
4433 pthread_mutex_unlock(&ua_sess
->lock
);
4434 goto end_no_session
;
4438 * If started = 0, it means that stop trace has been called for a session
4439 * that was never started. It's possible since we can have a fail start
4440 * from either the application manager thread or the command thread. Simply
4441 * indicate that this is a stop error.
4443 if (!ua_sess
->started
) {
4444 goto error_rcu_unlock
;
4447 health_code_update();
4449 /* This inhibits UST tracing */
4450 pthread_mutex_lock(&app
->sock_lock
);
4451 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4452 pthread_mutex_unlock(&app
->sock_lock
);
4454 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4455 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4458 DBG("UST app stop session failed. Application is dead.");
4460 * This is normal behavior, an application can die during the
4461 * creation process. Don't report an error so the execution can
4462 * continue normally.
4466 goto error_rcu_unlock
;
4469 health_code_update();
4470 ua_sess
->enabled
= 0;
4472 /* Quiescent wait after stopping trace */
4473 pthread_mutex_lock(&app
->sock_lock
);
4474 ret
= ustctl_wait_quiescent(app
->sock
);
4475 pthread_mutex_unlock(&app
->sock_lock
);
4476 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4477 ERR("UST app wait quiescent failed for app pid %d ret %d",
4481 health_code_update();
4483 registry
= get_session_registry(ua_sess
);
4485 /* The UST app session is held registry shall not be null. */
4488 /* Push metadata for application before freeing the application. */
4489 (void) push_metadata(registry
, ua_sess
->consumer
);
4492 pthread_mutex_unlock(&ua_sess
->lock
);
4495 health_code_update();
4499 pthread_mutex_unlock(&ua_sess
->lock
);
4501 health_code_update();
4506 int ust_app_flush_app_session(struct ust_app
*app
,
4507 struct ust_app_session
*ua_sess
)
4509 int ret
, retval
= 0;
4510 struct lttng_ht_iter iter
;
4511 struct ust_app_channel
*ua_chan
;
4512 struct consumer_socket
*socket
;
4514 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4518 if (!app
->compatible
) {
4519 goto end_not_compatible
;
4522 pthread_mutex_lock(&ua_sess
->lock
);
4524 if (ua_sess
->deleted
) {
4528 health_code_update();
4530 /* Flushing buffers */
4531 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4534 /* Flush buffers and push metadata. */
4535 switch (ua_sess
->buffer_type
) {
4536 case LTTNG_BUFFER_PER_PID
:
4537 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4539 health_code_update();
4540 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4542 ERR("Error flushing consumer channel");
4548 case LTTNG_BUFFER_PER_UID
:
4554 health_code_update();
4557 pthread_mutex_unlock(&ua_sess
->lock
);
4561 health_code_update();
4566 * Flush buffers for all applications for a specific UST session.
4567 * Called with UST session lock held.
4570 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4575 DBG("Flushing session buffers for all ust apps");
4579 /* Flush buffers and push metadata. */
4580 switch (usess
->buffer_type
) {
4581 case LTTNG_BUFFER_PER_UID
:
4583 struct buffer_reg_uid
*reg
;
4584 struct lttng_ht_iter iter
;
4586 /* Flush all per UID buffers associated to that session. */
4587 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4588 struct ust_registry_session
*ust_session_reg
;
4589 struct buffer_reg_channel
*reg_chan
;
4590 struct consumer_socket
*socket
;
4592 /* Get consumer socket to use to push the metadata.*/
4593 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4596 /* Ignore request if no consumer is found for the session. */
4600 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4601 reg_chan
, node
.node
) {
4603 * The following call will print error values so the return
4604 * code is of little importance because whatever happens, we
4605 * have to try them all.
4607 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4610 ust_session_reg
= reg
->registry
->reg
.ust
;
4611 /* Push metadata. */
4612 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4616 case LTTNG_BUFFER_PER_PID
:
4618 struct ust_app_session
*ua_sess
;
4619 struct lttng_ht_iter iter
;
4620 struct ust_app
*app
;
4622 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4623 ua_sess
= lookup_session_by_app(usess
, app
);
4624 if (ua_sess
== NULL
) {
4627 (void) ust_app_flush_app_session(app
, ua_sess
);
4638 health_code_update();
4643 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
4644 struct ust_app_session
*ua_sess
)
4647 struct lttng_ht_iter iter
;
4648 struct ust_app_channel
*ua_chan
;
4649 struct consumer_socket
*socket
;
4651 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
4655 if (!app
->compatible
) {
4656 goto end_not_compatible
;
4659 pthread_mutex_lock(&ua_sess
->lock
);
4661 if (ua_sess
->deleted
) {
4665 health_code_update();
4667 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4670 ERR("Failed to find consumer (%" PRIu32
") socket",
4671 app
->bits_per_long
);
4676 /* Clear quiescent state. */
4677 switch (ua_sess
->buffer_type
) {
4678 case LTTNG_BUFFER_PER_PID
:
4679 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
4680 ua_chan
, node
.node
) {
4681 health_code_update();
4682 ret
= consumer_clear_quiescent_channel(socket
,
4685 ERR("Error clearing quiescent state for consumer channel");
4691 case LTTNG_BUFFER_PER_UID
:
4698 health_code_update();
4701 pthread_mutex_unlock(&ua_sess
->lock
);
4705 health_code_update();
4710 * Clear quiescent state in each stream for all applications for a
4711 * specific UST session.
4712 * Called with UST session lock held.
4715 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
4720 DBG("Clearing stream quiescent state for all ust apps");
4724 switch (usess
->buffer_type
) {
4725 case LTTNG_BUFFER_PER_UID
:
4727 struct lttng_ht_iter iter
;
4728 struct buffer_reg_uid
*reg
;
4731 * Clear quiescent for all per UID buffers associated to
4734 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4735 struct consumer_socket
*socket
;
4736 struct buffer_reg_channel
*reg_chan
;
4738 /* Get associated consumer socket.*/
4739 socket
= consumer_find_socket_by_bitness(
4740 reg
->bits_per_long
, usess
->consumer
);
4743 * Ignore request if no consumer is found for
4749 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
4750 &iter
.iter
, reg_chan
, node
.node
) {
4752 * The following call will print error values so
4753 * the return code is of little importance
4754 * because whatever happens, we have to try them
4757 (void) consumer_clear_quiescent_channel(socket
,
4758 reg_chan
->consumer_key
);
4763 case LTTNG_BUFFER_PER_PID
:
4765 struct ust_app_session
*ua_sess
;
4766 struct lttng_ht_iter iter
;
4767 struct ust_app
*app
;
4769 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
4771 ua_sess
= lookup_session_by_app(usess
, app
);
4772 if (ua_sess
== NULL
) {
4775 (void) ust_app_clear_quiescent_app_session(app
,
4787 health_code_update();
4792 * Destroy a specific UST session in apps.
4794 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4797 struct ust_app_session
*ua_sess
;
4798 struct lttng_ht_iter iter
;
4799 struct lttng_ht_node_u64
*node
;
4801 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4805 if (!app
->compatible
) {
4809 __lookup_session_by_app(usess
, app
, &iter
);
4810 node
= lttng_ht_iter_get_node_u64(&iter
);
4812 /* Session is being or is deleted. */
4815 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4817 health_code_update();
4818 destroy_app_session(app
, ua_sess
);
4820 health_code_update();
4822 /* Quiescent wait after stopping trace */
4823 pthread_mutex_lock(&app
->sock_lock
);
4824 ret
= ustctl_wait_quiescent(app
->sock
);
4825 pthread_mutex_unlock(&app
->sock_lock
);
4826 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4827 ERR("UST app wait quiescent failed for app pid %d ret %d",
4832 health_code_update();
4837 * Start tracing for the UST session.
4839 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4841 struct lttng_ht_iter iter
;
4842 struct ust_app
*app
;
4844 DBG("Starting all UST traces");
4847 * Even though the start trace might fail, flag this session active so
4848 * other application coming in are started by default.
4855 * In a start-stop-start use-case, we need to clear the quiescent state
4856 * of each channel set by the prior stop command, thus ensuring that a
4857 * following stop or destroy is sure to grab a timestamp_end near those
4858 * operations, even if the packet is empty.
4860 (void) ust_app_clear_quiescent_session(usess
);
4862 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4863 ust_app_global_update(usess
, app
);
4872 * Start tracing for the UST session.
4873 * Called with UST session lock held.
4875 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4878 struct lttng_ht_iter iter
;
4879 struct ust_app
*app
;
4881 DBG("Stopping all UST traces");
4884 * Even though the stop trace might fail, flag this session inactive so
4885 * other application coming in are not started by default.
4891 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4892 ret
= ust_app_stop_trace(usess
, app
);
4894 /* Continue to next apps even on error */
4899 (void) ust_app_flush_session(usess
);
4907 * Destroy app UST session.
4909 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4912 struct lttng_ht_iter iter
;
4913 struct ust_app
*app
;
4915 DBG("Destroy all UST traces");
4919 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4920 ret
= destroy_trace(usess
, app
);
4922 /* Continue to next apps even on error */
4932 /* The ua_sess lock must be held by the caller. */
4934 int find_or_create_ust_app_channel(
4935 struct ltt_ust_session
*usess
,
4936 struct ust_app_session
*ua_sess
,
4937 struct ust_app
*app
,
4938 struct ltt_ust_channel
*uchan
,
4939 struct ust_app_channel
**ua_chan
)
4942 struct lttng_ht_iter iter
;
4943 struct lttng_ht_node_str
*ua_chan_node
;
4945 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
4946 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4948 *ua_chan
= caa_container_of(ua_chan_node
,
4949 struct ust_app_channel
, node
);
4953 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
4962 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
4963 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
4964 struct ust_app
*app
)
4967 struct ust_app_event
*ua_event
= NULL
;
4969 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4970 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4972 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4977 if (ua_event
->enabled
!= uevent
->enabled
) {
4978 ret
= uevent
->enabled
?
4979 enable_ust_app_event(ua_sess
, ua_event
, app
) :
4980 disable_ust_app_event(ua_sess
, ua_event
, app
);
4989 * The caller must ensure that the application is compatible and is tracked
4990 * by the process attribute trackers.
4993 void ust_app_synchronize(struct ltt_ust_session
*usess
,
4994 struct ust_app
*app
)
4997 struct cds_lfht_iter uchan_iter
;
4998 struct ltt_ust_channel
*uchan
;
4999 struct ust_app_session
*ua_sess
= NULL
;
5002 * The application's configuration should only be synchronized for
5005 assert(usess
->active
);
5007 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5009 /* Tracer is probably gone or ENOMEM. */
5014 pthread_mutex_lock(&ua_sess
->lock
);
5015 if (ua_sess
->deleted
) {
5016 pthread_mutex_unlock(&ua_sess
->lock
);
5022 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5024 struct ust_app_channel
*ua_chan
;
5025 struct cds_lfht_iter uevent_iter
;
5026 struct ltt_ust_event
*uevent
;
5029 * Search for a matching ust_app_channel. If none is found,
5030 * create it. Creating the channel will cause the ua_chan
5031 * structure to be allocated, the channel buffers to be
5032 * allocated (if necessary) and sent to the application, and
5033 * all enabled contexts will be added to the channel.
5035 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5036 app
, uchan
, &ua_chan
);
5038 /* Tracer is probably gone or ENOMEM. */
5043 /* ua_chan will be NULL for the metadata channel */
5047 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5049 ret
= ust_app_channel_synchronize_event(ua_chan
,
5050 uevent
, ua_sess
, app
);
5056 if (ua_chan
->enabled
!= uchan
->enabled
) {
5057 ret
= uchan
->enabled
?
5058 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5059 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5067 * Create the metadata for the application. This returns gracefully if a
5068 * metadata was already set for the session.
5070 * The metadata channel must be created after the data channels as the
5071 * consumer daemon assumes this ordering. When interacting with a relay
5072 * daemon, the consumer will use this assumption to send the
5073 * "STREAMS_SENT" message to the relay daemon.
5075 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5083 pthread_mutex_unlock(&ua_sess
->lock
);
5084 /* Everything went well at this point. */
5089 pthread_mutex_unlock(&ua_sess
->lock
);
5092 destroy_app_session(app
, ua_sess
);
5098 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5100 struct ust_app_session
*ua_sess
;
5102 ua_sess
= lookup_session_by_app(usess
, app
);
5103 if (ua_sess
== NULL
) {
5106 destroy_app_session(app
, ua_sess
);
5110 * Add channels/events from UST global domain to registered apps at sock.
5112 * Called with session lock held.
5113 * Called with RCU read-side lock held.
5115 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5118 assert(usess
->active
);
5120 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5121 app
->sock
, usess
->id
);
5123 if (!app
->compatible
) {
5126 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5128 trace_ust_id_tracker_lookup(
5129 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5131 trace_ust_id_tracker_lookup(
5132 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5135 * Synchronize the application's internal tracing configuration
5136 * and start tracing.
5138 ust_app_synchronize(usess
, app
);
5139 ust_app_start_trace(usess
, app
);
5141 ust_app_global_destroy(usess
, app
);
5146 * Called with session lock held.
5148 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5150 struct lttng_ht_iter iter
;
5151 struct ust_app
*app
;
5154 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5155 ust_app_global_update(usess
, app
);
5161 * Add context to a specific channel for global UST domain.
5163 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5164 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5167 struct lttng_ht_node_str
*ua_chan_node
;
5168 struct lttng_ht_iter iter
, uiter
;
5169 struct ust_app_channel
*ua_chan
= NULL
;
5170 struct ust_app_session
*ua_sess
;
5171 struct ust_app
*app
;
5173 assert(usess
->active
);
5176 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5177 if (!app
->compatible
) {
5179 * TODO: In time, we should notice the caller of this error by
5180 * telling him that this is a version error.
5184 ua_sess
= lookup_session_by_app(usess
, app
);
5185 if (ua_sess
== NULL
) {
5189 pthread_mutex_lock(&ua_sess
->lock
);
5191 if (ua_sess
->deleted
) {
5192 pthread_mutex_unlock(&ua_sess
->lock
);
5196 /* Lookup channel in the ust app session */
5197 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5198 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5199 if (ua_chan_node
== NULL
) {
5202 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5204 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5209 pthread_mutex_unlock(&ua_sess
->lock
);
5217 * Receive registration and populate the given msg structure.
5219 * On success return 0 else a negative value returned by the ustctl call.
5221 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5224 uint32_t pid
, ppid
, uid
, gid
;
5228 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5229 &pid
, &ppid
, &uid
, &gid
,
5230 &msg
->bits_per_long
,
5231 &msg
->uint8_t_alignment
,
5232 &msg
->uint16_t_alignment
,
5233 &msg
->uint32_t_alignment
,
5234 &msg
->uint64_t_alignment
,
5235 &msg
->long_alignment
,
5242 case LTTNG_UST_ERR_EXITING
:
5243 DBG3("UST app recv reg message failed. Application died");
5245 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5246 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5247 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5248 LTTNG_UST_ABI_MINOR_VERSION
);
5251 ERR("UST app recv reg message failed with ret %d", ret
);
5256 msg
->pid
= (pid_t
) pid
;
5257 msg
->ppid
= (pid_t
) ppid
;
5258 msg
->uid
= (uid_t
) uid
;
5259 msg
->gid
= (gid_t
) gid
;
5266 * Return a ust app session object using the application object and the
5267 * session object descriptor has a key. If not found, NULL is returned.
5268 * A RCU read side lock MUST be acquired when calling this function.
5270 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5273 struct lttng_ht_node_ulong
*node
;
5274 struct lttng_ht_iter iter
;
5275 struct ust_app_session
*ua_sess
= NULL
;
5279 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5280 node
= lttng_ht_iter_get_node_ulong(&iter
);
5282 DBG2("UST app session find by objd %d not found", objd
);
5286 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5293 * Return a ust app channel object using the application object and the channel
5294 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5295 * lock MUST be acquired before calling this function.
5297 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5300 struct lttng_ht_node_ulong
*node
;
5301 struct lttng_ht_iter iter
;
5302 struct ust_app_channel
*ua_chan
= NULL
;
5306 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5307 node
= lttng_ht_iter_get_node_ulong(&iter
);
5309 DBG2("UST app channel find by objd %d not found", objd
);
5313 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5320 * Reply to a register channel notification from an application on the notify
5321 * socket. The channel metadata is also created.
5323 * The session UST registry lock is acquired in this function.
5325 * On success 0 is returned else a negative value.
5327 static int reply_ust_register_channel(int sock
, int cobjd
,
5328 size_t nr_fields
, struct ustctl_field
*fields
)
5330 int ret
, ret_code
= 0;
5332 uint64_t chan_reg_key
;
5333 enum ustctl_channel_header type
;
5334 struct ust_app
*app
;
5335 struct ust_app_channel
*ua_chan
;
5336 struct ust_app_session
*ua_sess
;
5337 struct ust_registry_session
*registry
;
5338 struct ust_registry_channel
*chan_reg
;
5342 /* Lookup application. If not found, there is a code flow error. */
5343 app
= find_app_by_notify_sock(sock
);
5345 DBG("Application socket %d is being torn down. Abort event notify",
5348 goto error_rcu_unlock
;
5351 /* Lookup channel by UST object descriptor. */
5352 ua_chan
= find_channel_by_objd(app
, cobjd
);
5354 DBG("Application channel is being torn down. Abort event notify");
5356 goto error_rcu_unlock
;
5359 assert(ua_chan
->session
);
5360 ua_sess
= ua_chan
->session
;
5362 /* Get right session registry depending on the session buffer type. */
5363 registry
= get_session_registry(ua_sess
);
5365 DBG("Application session is being torn down. Abort event notify");
5367 goto error_rcu_unlock
;
5370 /* Depending on the buffer type, a different channel key is used. */
5371 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5372 chan_reg_key
= ua_chan
->tracing_channel_id
;
5374 chan_reg_key
= ua_chan
->key
;
5377 pthread_mutex_lock(®istry
->lock
);
5379 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5382 if (!chan_reg
->register_done
) {
5384 * TODO: eventually use the registry event count for
5385 * this channel to better guess header type for per-pid
5388 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5389 chan_reg
->nr_ctx_fields
= nr_fields
;
5390 chan_reg
->ctx_fields
= fields
;
5392 chan_reg
->header_type
= type
;
5394 /* Get current already assigned values. */
5395 type
= chan_reg
->header_type
;
5397 /* Channel id is set during the object creation. */
5398 chan_id
= chan_reg
->chan_id
;
5400 /* Append to metadata */
5401 if (!chan_reg
->metadata_dumped
) {
5402 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5404 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5410 DBG3("UST app replying to register channel key %" PRIu64
5411 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5414 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5416 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5417 ERR("UST app reply channel failed with ret %d", ret
);
5419 DBG3("UST app reply channel failed. Application died");
5424 /* This channel registry registration is completed. */
5425 chan_reg
->register_done
= 1;
5428 pthread_mutex_unlock(®istry
->lock
);
5436 * Add event to the UST channel registry. When the event is added to the
5437 * registry, the metadata is also created. Once done, this replies to the
5438 * application with the appropriate error code.
5440 * The session UST registry lock is acquired in the function.
5442 * On success 0 is returned else a negative value.
5444 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5445 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5446 int loglevel_value
, char *model_emf_uri
)
5449 uint32_t event_id
= 0;
5450 uint64_t chan_reg_key
;
5451 struct ust_app
*app
;
5452 struct ust_app_channel
*ua_chan
;
5453 struct ust_app_session
*ua_sess
;
5454 struct ust_registry_session
*registry
;
5458 /* Lookup application. If not found, there is a code flow error. */
5459 app
= find_app_by_notify_sock(sock
);
5461 DBG("Application socket %d is being torn down. Abort event notify",
5464 goto error_rcu_unlock
;
5467 /* Lookup channel by UST object descriptor. */
5468 ua_chan
= find_channel_by_objd(app
, cobjd
);
5470 DBG("Application channel is being torn down. Abort event notify");
5472 goto error_rcu_unlock
;
5475 assert(ua_chan
->session
);
5476 ua_sess
= ua_chan
->session
;
5478 registry
= get_session_registry(ua_sess
);
5480 DBG("Application session is being torn down. Abort event notify");
5482 goto error_rcu_unlock
;
5485 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5486 chan_reg_key
= ua_chan
->tracing_channel_id
;
5488 chan_reg_key
= ua_chan
->key
;
5491 pthread_mutex_lock(®istry
->lock
);
5494 * From this point on, this call acquires the ownership of the sig, fields
5495 * and model_emf_uri meaning any free are done inside it if needed. These
5496 * three variables MUST NOT be read/write after this.
5498 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5499 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5500 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5504 model_emf_uri
= NULL
;
5507 * The return value is returned to ustctl so in case of an error, the
5508 * application can be notified. In case of an error, it's important not to
5509 * return a negative error or else the application will get closed.
5511 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5513 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5514 ERR("UST app reply event failed with ret %d", ret
);
5516 DBG3("UST app reply event failed. Application died");
5519 * No need to wipe the create event since the application socket will
5520 * get close on error hence cleaning up everything by itself.
5525 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5529 pthread_mutex_unlock(®istry
->lock
);
5534 free(model_emf_uri
);
5539 * Add enum to the UST session registry. Once done, this replies to the
5540 * application with the appropriate error code.
5542 * The session UST registry lock is acquired within this function.
5544 * On success 0 is returned else a negative value.
5546 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
5547 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
5549 int ret
= 0, ret_code
;
5550 struct ust_app
*app
;
5551 struct ust_app_session
*ua_sess
;
5552 struct ust_registry_session
*registry
;
5553 uint64_t enum_id
= -1ULL;
5557 /* Lookup application. If not found, there is a code flow error. */
5558 app
= find_app_by_notify_sock(sock
);
5560 /* Return an error since this is not an error */
5561 DBG("Application socket %d is being torn down. Aborting enum registration",
5564 goto error_rcu_unlock
;
5567 /* Lookup session by UST object descriptor. */
5568 ua_sess
= find_session_by_objd(app
, sobjd
);
5570 /* Return an error since this is not an error */
5571 DBG("Application session is being torn down (session not found). Aborting enum registration.");
5573 goto error_rcu_unlock
;
5576 registry
= get_session_registry(ua_sess
);
5578 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
5580 goto error_rcu_unlock
;
5583 pthread_mutex_lock(®istry
->lock
);
5586 * From this point on, the callee acquires the ownership of
5587 * entries. The variable entries MUST NOT be read/written after
5590 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
5591 entries
, nr_entries
, &enum_id
);
5595 * The return value is returned to ustctl so in case of an error, the
5596 * application can be notified. In case of an error, it's important not to
5597 * return a negative error or else the application will get closed.
5599 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
5601 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5602 ERR("UST app reply enum failed with ret %d", ret
);
5604 DBG3("UST app reply enum failed. Application died");
5607 * No need to wipe the create enum since the application socket will
5608 * get close on error hence cleaning up everything by itself.
5613 DBG3("UST registry enum %s added successfully or already found", name
);
5616 pthread_mutex_unlock(®istry
->lock
);
5623 * Handle application notification through the given notify socket.
5625 * Return 0 on success or else a negative value.
5627 int ust_app_recv_notify(int sock
)
5630 enum ustctl_notify_cmd cmd
;
5632 DBG3("UST app receiving notify from sock %d", sock
);
5634 ret
= ustctl_recv_notify(sock
, &cmd
);
5636 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5637 ERR("UST app recv notify failed with ret %d", ret
);
5639 DBG3("UST app recv notify failed. Application died");
5645 case USTCTL_NOTIFY_CMD_EVENT
:
5647 int sobjd
, cobjd
, loglevel_value
;
5648 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5650 struct ustctl_field
*fields
;
5652 DBG2("UST app ustctl register event received");
5654 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5655 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5658 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5659 ERR("UST app recv event failed with ret %d", ret
);
5661 DBG3("UST app recv event failed. Application died");
5667 * Add event to the UST registry coming from the notify socket. This
5668 * call will free if needed the sig, fields and model_emf_uri. This
5669 * code path loses the ownsership of these variables and transfer them
5670 * to the this function.
5672 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5673 fields
, loglevel_value
, model_emf_uri
);
5680 case USTCTL_NOTIFY_CMD_CHANNEL
:
5684 struct ustctl_field
*fields
;
5686 DBG2("UST app ustctl register channel received");
5688 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5691 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5692 ERR("UST app recv channel failed with ret %d", ret
);
5694 DBG3("UST app recv channel failed. Application died");
5700 * The fields ownership are transfered to this function call meaning
5701 * that if needed it will be freed. After this, it's invalid to access
5702 * fields or clean it up.
5704 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
5712 case USTCTL_NOTIFY_CMD_ENUM
:
5715 char name
[LTTNG_UST_SYM_NAME_LEN
];
5717 struct ustctl_enum_entry
*entries
;
5719 DBG2("UST app ustctl register enum received");
5721 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
5722 &entries
, &nr_entries
);
5724 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5725 ERR("UST app recv enum failed with ret %d", ret
);
5727 DBG3("UST app recv enum failed. Application died");
5732 /* Callee assumes ownership of entries */
5733 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
5734 entries
, nr_entries
);
5742 /* Should NEVER happen. */
5751 * Once the notify socket hangs up, this is called. First, it tries to find the
5752 * corresponding application. On failure, the call_rcu to close the socket is
5753 * executed. If an application is found, it tries to delete it from the notify
5754 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5756 * Note that an object needs to be allocated here so on ENOMEM failure, the
5757 * call RCU is not done but the rest of the cleanup is.
5759 void ust_app_notify_sock_unregister(int sock
)
5762 struct lttng_ht_iter iter
;
5763 struct ust_app
*app
;
5764 struct ust_app_notify_sock_obj
*obj
;
5770 obj
= zmalloc(sizeof(*obj
));
5773 * An ENOMEM is kind of uncool. If this strikes we continue the
5774 * procedure but the call_rcu will not be called. In this case, we
5775 * accept the fd leak rather than possibly creating an unsynchronized
5776 * state between threads.
5778 * TODO: The notify object should be created once the notify socket is
5779 * registered and stored independantely from the ust app object. The
5780 * tricky part is to synchronize the teardown of the application and
5781 * this notify object. Let's keep that in mind so we can avoid this
5782 * kind of shenanigans with ENOMEM in the teardown path.
5789 DBG("UST app notify socket unregister %d", sock
);
5792 * Lookup application by notify socket. If this fails, this means that the
5793 * hash table delete has already been done by the application
5794 * unregistration process so we can safely close the notify socket in a
5797 app
= find_app_by_notify_sock(sock
);
5802 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5805 * Whatever happens here either we fail or succeed, in both cases we have
5806 * to close the socket after a grace period to continue to the call RCU
5807 * here. If the deletion is successful, the application is not visible
5808 * anymore by other threads and is it fails it means that it was already
5809 * deleted from the hash table so either way we just have to close the
5812 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5818 * Close socket after a grace period to avoid for the socket to be reused
5819 * before the application object is freed creating potential race between
5820 * threads trying to add unique in the global hash table.
5823 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5828 * Destroy a ust app data structure and free its memory.
5830 void ust_app_destroy(struct ust_app
*app
)
5836 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5840 * Take a snapshot for a given UST session. The snapshot is sent to the given
5843 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5845 enum lttng_error_code
ust_app_snapshot_record(
5846 const struct ltt_ust_session
*usess
,
5847 const struct consumer_output
*output
, int wait
,
5848 uint64_t nb_packets_per_stream
)
5851 enum lttng_error_code status
= LTTNG_OK
;
5852 struct lttng_ht_iter iter
;
5853 struct ust_app
*app
;
5854 char *trace_path
= NULL
;
5861 switch (usess
->buffer_type
) {
5862 case LTTNG_BUFFER_PER_UID
:
5864 struct buffer_reg_uid
*reg
;
5866 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5867 struct buffer_reg_channel
*reg_chan
;
5868 struct consumer_socket
*socket
;
5869 char pathname
[PATH_MAX
];
5870 size_t consumer_path_offset
= 0;
5872 if (!reg
->registry
->reg
.ust
->metadata_key
) {
5873 /* Skip since no metadata is present */
5877 /* Get consumer socket to use to push the metadata.*/
5878 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5881 status
= LTTNG_ERR_INVALID
;
5885 memset(pathname
, 0, sizeof(pathname
));
5886 ret
= snprintf(pathname
, sizeof(pathname
),
5887 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5888 reg
->uid
, reg
->bits_per_long
);
5890 PERROR("snprintf snapshot path");
5891 status
= LTTNG_ERR_INVALID
;
5894 /* Free path allowed on previous iteration. */
5896 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
5897 &consumer_path_offset
);
5899 status
= LTTNG_ERR_INVALID
;
5902 /* Add the UST default trace dir to path. */
5903 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5904 reg_chan
, node
.node
) {
5905 status
= consumer_snapshot_channel(socket
,
5906 reg_chan
->consumer_key
,
5907 output
, 0, usess
->uid
,
5908 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
5909 nb_packets_per_stream
);
5910 if (status
!= LTTNG_OK
) {
5914 status
= consumer_snapshot_channel(socket
,
5915 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5916 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
5918 if (status
!= LTTNG_OK
) {
5924 case LTTNG_BUFFER_PER_PID
:
5926 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5927 struct consumer_socket
*socket
;
5928 struct lttng_ht_iter chan_iter
;
5929 struct ust_app_channel
*ua_chan
;
5930 struct ust_app_session
*ua_sess
;
5931 struct ust_registry_session
*registry
;
5932 char pathname
[PATH_MAX
];
5933 size_t consumer_path_offset
= 0;
5935 ua_sess
= lookup_session_by_app(usess
, app
);
5937 /* Session not associated with this app. */
5941 /* Get the right consumer socket for the application. */
5942 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5945 status
= LTTNG_ERR_INVALID
;
5949 /* Add the UST default trace dir to path. */
5950 memset(pathname
, 0, sizeof(pathname
));
5951 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5954 status
= LTTNG_ERR_INVALID
;
5955 PERROR("snprintf snapshot path");
5958 /* Free path allowed on previous iteration. */
5960 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
5961 &consumer_path_offset
);
5963 status
= LTTNG_ERR_INVALID
;
5966 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5967 ua_chan
, node
.node
) {
5968 status
= consumer_snapshot_channel(socket
,
5969 ua_chan
->key
, output
, 0,
5970 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
5971 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
5972 &trace_path
[consumer_path_offset
], wait
,
5973 nb_packets_per_stream
);
5977 case LTTNG_ERR_CHAN_NOT_FOUND
:
5984 registry
= get_session_registry(ua_sess
);
5986 DBG("Application session is being torn down. Skip application.");
5989 status
= consumer_snapshot_channel(socket
,
5990 registry
->metadata_key
, output
, 1,
5991 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
5992 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
5993 &trace_path
[consumer_path_offset
], wait
, 0);
5997 case LTTNG_ERR_CHAN_NOT_FOUND
:
6017 * Return the size taken by one more packet per stream.
6019 uint64_t ust_app_get_size_one_more_packet_per_stream(
6020 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6022 uint64_t tot_size
= 0;
6023 struct ust_app
*app
;
6024 struct lttng_ht_iter iter
;
6028 switch (usess
->buffer_type
) {
6029 case LTTNG_BUFFER_PER_UID
:
6031 struct buffer_reg_uid
*reg
;
6033 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6034 struct buffer_reg_channel
*reg_chan
;
6037 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6038 reg_chan
, node
.node
) {
6039 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6041 * Don't take channel into account if we
6042 * already grab all its packets.
6046 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6052 case LTTNG_BUFFER_PER_PID
:
6055 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6056 struct ust_app_channel
*ua_chan
;
6057 struct ust_app_session
*ua_sess
;
6058 struct lttng_ht_iter chan_iter
;
6060 ua_sess
= lookup_session_by_app(usess
, app
);
6062 /* Session not associated with this app. */
6066 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6067 ua_chan
, node
.node
) {
6068 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6070 * Don't take channel into account if we
6071 * already grab all its packets.
6075 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6089 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6090 struct cds_list_head
*buffer_reg_uid_list
,
6091 struct consumer_output
*consumer
, uint64_t uchan_id
,
6092 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6095 uint64_t consumer_chan_key
;
6100 ret
= buffer_reg_uid_consumer_channel_key(
6101 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6109 ret
= consumer_get_lost_packets(ust_session_id
,
6110 consumer_chan_key
, consumer
, lost
);
6112 ret
= consumer_get_discarded_events(ust_session_id
,
6113 consumer_chan_key
, consumer
, discarded
);
6120 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6121 struct ltt_ust_channel
*uchan
,
6122 struct consumer_output
*consumer
, int overwrite
,
6123 uint64_t *discarded
, uint64_t *lost
)
6126 struct lttng_ht_iter iter
;
6127 struct lttng_ht_node_str
*ua_chan_node
;
6128 struct ust_app
*app
;
6129 struct ust_app_session
*ua_sess
;
6130 struct ust_app_channel
*ua_chan
;
6137 * Iterate over every registered applications. Sum counters for
6138 * all applications containing requested session and channel.
6140 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6141 struct lttng_ht_iter uiter
;
6143 ua_sess
= lookup_session_by_app(usess
, app
);
6144 if (ua_sess
== NULL
) {
6149 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6150 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6151 /* If the session is found for the app, the channel must be there */
6152 assert(ua_chan_node
);
6154 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6159 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6166 uint64_t _discarded
;
6168 ret
= consumer_get_discarded_events(usess
->id
,
6169 ua_chan
->key
, consumer
, &_discarded
);
6173 (*discarded
) += _discarded
;
6182 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6183 struct ust_app
*app
)
6186 struct ust_app_session
*ua_sess
;
6188 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6192 ua_sess
= lookup_session_by_app(usess
, app
);
6193 if (ua_sess
== NULL
) {
6194 /* The session is in teardown process. Ignore and continue. */
6198 pthread_mutex_lock(&ua_sess
->lock
);
6200 if (ua_sess
->deleted
) {
6204 pthread_mutex_lock(&app
->sock_lock
);
6205 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6206 pthread_mutex_unlock(&app
->sock_lock
);
6209 pthread_mutex_unlock(&ua_sess
->lock
);
6213 health_code_update();
6218 * Regenerate the statedump for each app in the session.
6220 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6223 struct lttng_ht_iter iter
;
6224 struct ust_app
*app
;
6226 DBG("Regenerating the metadata for all UST apps");
6230 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6231 if (!app
->compatible
) {
6235 ret
= ust_app_regenerate_statedump(usess
, app
);
6237 /* Continue to the next app even on error */
6248 * Rotate all the channels of a session.
6250 * Return LTTNG_OK on success or else an LTTng error code.
6252 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
6255 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6256 struct lttng_ht_iter iter
;
6257 struct ust_app
*app
;
6258 struct ltt_ust_session
*usess
= session
->ust_session
;
6264 switch (usess
->buffer_type
) {
6265 case LTTNG_BUFFER_PER_UID
:
6267 struct buffer_reg_uid
*reg
;
6269 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6270 struct buffer_reg_channel
*reg_chan
;
6271 struct consumer_socket
*socket
;
6273 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6274 /* Skip since no metadata is present */
6278 /* Get consumer socket to use to push the metadata.*/
6279 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6282 cmd_ret
= LTTNG_ERR_INVALID
;
6286 /* Rotate the data channels. */
6287 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6288 reg_chan
, node
.node
) {
6289 ret
= consumer_rotate_channel(socket
,
6290 reg_chan
->consumer_key
,
6291 usess
->uid
, usess
->gid
,
6293 /* is_metadata_channel */ false);
6295 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6300 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6302 ret
= consumer_rotate_channel(socket
,
6303 reg
->registry
->reg
.ust
->metadata_key
,
6304 usess
->uid
, usess
->gid
,
6306 /* is_metadata_channel */ true);
6308 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6314 case LTTNG_BUFFER_PER_PID
:
6316 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6317 struct consumer_socket
*socket
;
6318 struct lttng_ht_iter chan_iter
;
6319 struct ust_app_channel
*ua_chan
;
6320 struct ust_app_session
*ua_sess
;
6321 struct ust_registry_session
*registry
;
6323 ua_sess
= lookup_session_by_app(usess
, app
);
6325 /* Session not associated with this app. */
6329 /* Get the right consumer socket for the application. */
6330 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6333 cmd_ret
= LTTNG_ERR_INVALID
;
6337 registry
= get_session_registry(ua_sess
);
6339 DBG("Application session is being torn down. Skip application.");
6343 /* Rotate the data channels. */
6344 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6345 ua_chan
, node
.node
) {
6346 ret
= consumer_rotate_channel(socket
,
6348 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6349 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6351 /* is_metadata_channel */ false);
6353 /* Per-PID buffer and application going away. */
6354 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6356 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6361 /* Rotate the metadata channel. */
6362 (void) push_metadata(registry
, usess
->consumer
);
6363 ret
= consumer_rotate_channel(socket
,
6364 registry
->metadata_key
,
6365 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6366 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6368 /* is_metadata_channel */ true);
6370 /* Per-PID buffer and application going away. */
6371 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6373 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6391 enum lttng_error_code
ust_app_create_channel_subdirectories(
6392 const struct ltt_ust_session
*usess
)
6394 enum lttng_error_code ret
= LTTNG_OK
;
6395 struct lttng_ht_iter iter
;
6396 enum lttng_trace_chunk_status chunk_status
;
6397 char *pathname_index
;
6400 assert(usess
->current_trace_chunk
);
6403 switch (usess
->buffer_type
) {
6404 case LTTNG_BUFFER_PER_UID
:
6406 struct buffer_reg_uid
*reg
;
6408 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6409 fmt_ret
= asprintf(&pathname_index
,
6410 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
6411 reg
->uid
, reg
->bits_per_long
);
6413 ERR("Failed to format channel index directory");
6414 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6419 * Create the index subdirectory which will take care
6420 * of implicitly creating the channel's path.
6422 chunk_status
= lttng_trace_chunk_create_subdirectory(
6423 usess
->current_trace_chunk
,
6425 free(pathname_index
);
6426 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6427 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6433 case LTTNG_BUFFER_PER_PID
:
6435 struct ust_app
*app
;
6438 * Create the toplevel ust/ directory in case no apps are running.
6440 chunk_status
= lttng_trace_chunk_create_subdirectory(
6441 usess
->current_trace_chunk
,
6442 DEFAULT_UST_TRACE_DIR
);
6443 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6444 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6448 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
6450 struct ust_app_session
*ua_sess
;
6451 struct ust_registry_session
*registry
;
6453 ua_sess
= lookup_session_by_app(usess
, app
);
6455 /* Session not associated with this app. */
6459 registry
= get_session_registry(ua_sess
);
6461 DBG("Application session is being torn down. Skip application.");
6465 fmt_ret
= asprintf(&pathname_index
,
6466 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
6469 ERR("Failed to format channel index directory");
6470 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6474 * Create the index subdirectory which will take care
6475 * of implicitly creating the channel's path.
6477 chunk_status
= lttng_trace_chunk_create_subdirectory(
6478 usess
->current_trace_chunk
,
6480 free(pathname_index
);
6481 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6482 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6499 * Clear all the channels of a session.
6501 * Return LTTNG_OK on success or else an LTTng error code.
6503 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
6506 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6507 struct lttng_ht_iter iter
;
6508 struct ust_app
*app
;
6509 struct ltt_ust_session
*usess
= session
->ust_session
;
6515 if (usess
->active
) {
6516 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
6517 cmd_ret
= LTTNG_ERR_FATAL
;
6521 switch (usess
->buffer_type
) {
6522 case LTTNG_BUFFER_PER_UID
:
6524 struct buffer_reg_uid
*reg
;
6526 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6527 struct buffer_reg_channel
*reg_chan
;
6528 struct consumer_socket
*socket
;
6530 /* Get consumer socket to use to push the metadata.*/
6531 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6534 cmd_ret
= LTTNG_ERR_INVALID
;
6538 /* Clear the data channels. */
6539 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6540 reg_chan
, node
.node
) {
6541 ret
= consumer_clear_channel(socket
,
6542 reg_chan
->consumer_key
);
6548 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6551 * Clear the metadata channel.
6552 * Metadata channel is not cleared per se but we still need to
6553 * perform a rotation operation on it behind the scene.
6555 ret
= consumer_clear_channel(socket
,
6556 reg
->registry
->reg
.ust
->metadata_key
);
6563 case LTTNG_BUFFER_PER_PID
:
6565 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6566 struct consumer_socket
*socket
;
6567 struct lttng_ht_iter chan_iter
;
6568 struct ust_app_channel
*ua_chan
;
6569 struct ust_app_session
*ua_sess
;
6570 struct ust_registry_session
*registry
;
6572 ua_sess
= lookup_session_by_app(usess
, app
);
6574 /* Session not associated with this app. */
6578 /* Get the right consumer socket for the application. */
6579 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6582 cmd_ret
= LTTNG_ERR_INVALID
;
6586 registry
= get_session_registry(ua_sess
);
6588 DBG("Application session is being torn down. Skip application.");
6592 /* Clear the data channels. */
6593 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6594 ua_chan
, node
.node
) {
6595 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
6597 /* Per-PID buffer and application going away. */
6598 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6605 (void) push_metadata(registry
, usess
->consumer
);
6608 * Clear the metadata channel.
6609 * Metadata channel is not cleared per se but we still need to
6610 * perform rotation operation on it behind the scene.
6612 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
6614 /* Per-PID buffer and application going away. */
6615 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6633 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
6634 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
6637 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
6647 * This function skips the metadata channel as the begin/end timestamps of a
6648 * metadata packet are useless.
6650 * Moreover, opening a packet after a "clear" will cause problems for live
6651 * sessions as it will introduce padding that was not part of the first trace
6652 * chunk. The relay daemon expects the content of the metadata stream of
6653 * successive metadata trace chunks to be strict supersets of one another.
6655 * For example, flushing a packet at the beginning of the metadata stream of
6656 * a trace chunk resulting from a "clear" session command will cause the
6657 * size of the metadata stream of the new trace chunk to not match the size of
6658 * the metadata stream of the original chunk. This will confuse the relay
6659 * daemon as the same "offset" in a metadata stream will no longer point
6660 * to the same content.
6662 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
6664 enum lttng_error_code ret
= LTTNG_OK
;
6665 struct lttng_ht_iter iter
;
6666 struct ltt_ust_session
*usess
= session
->ust_session
;
6672 switch (usess
->buffer_type
) {
6673 case LTTNG_BUFFER_PER_UID
:
6675 struct buffer_reg_uid
*reg
;
6677 cds_list_for_each_entry (
6678 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6679 struct buffer_reg_channel
*reg_chan
;
6680 struct consumer_socket
*socket
;
6682 socket
= consumer_find_socket_by_bitness(
6683 reg
->bits_per_long
, usess
->consumer
);
6685 ret
= LTTNG_ERR_FATAL
;
6689 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
6690 &iter
.iter
, reg_chan
, node
.node
) {
6691 const int open_ret
=
6692 consumer_open_channel_packets(
6694 reg_chan
->consumer_key
);
6697 ret
= LTTNG_ERR_UNK
;
6704 case LTTNG_BUFFER_PER_PID
:
6706 struct ust_app
*app
;
6708 cds_lfht_for_each_entry (
6709 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6710 struct consumer_socket
*socket
;
6711 struct lttng_ht_iter chan_iter
;
6712 struct ust_app_channel
*ua_chan
;
6713 struct ust_app_session
*ua_sess
;
6714 struct ust_registry_session
*registry
;
6716 ua_sess
= lookup_session_by_app(usess
, app
);
6718 /* Session not associated with this app. */
6722 /* Get the right consumer socket for the application. */
6723 socket
= consumer_find_socket_by_bitness(
6724 app
->bits_per_long
, usess
->consumer
);
6726 ret
= LTTNG_ERR_FATAL
;
6730 registry
= get_session_registry(ua_sess
);
6732 DBG("Application session is being torn down. Skip application.");
6736 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
6737 &chan_iter
.iter
, ua_chan
, node
.node
) {
6738 const int open_ret
=
6739 consumer_open_channel_packets(
6745 * Per-PID buffer and application going
6748 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6752 ret
= LTTNG_ERR_UNK
;