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_object(struct ust_app
*app
,
1460 struct lttng_ust_object_data
*object
)
1464 health_code_update();
1466 pthread_mutex_lock(&app
->sock_lock
);
1467 ret
= ustctl_disable(app
->sock
, object
);
1468 pthread_mutex_unlock(&app
->sock_lock
);
1470 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1471 ERR("Failed to disable UST app object %p app (pid: %d) with ret %d",
1472 object
, app
->pid
, ret
);
1475 * This is normal behavior, an application can die during the
1476 * creation process. Don't report an error so the execution can
1477 * continue normally.
1480 DBG3("Failed to disable UST app object. Application is dead.");
1485 DBG2("UST app object %p disabled successfully for app (pid: %d)",
1489 health_code_update();
1494 * Disable the specified channel on to UST tracer for the UST session.
1496 static int disable_ust_channel(struct ust_app
*app
,
1497 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1501 health_code_update();
1503 pthread_mutex_lock(&app
->sock_lock
);
1504 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1505 pthread_mutex_unlock(&app
->sock_lock
);
1507 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1508 ERR("UST app channel %s disable failed for app (pid: %d) "
1509 "and session handle %d with ret %d",
1510 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1513 * This is normal behavior, an application can die during the
1514 * creation process. Don't report an error so the execution can
1515 * continue normally.
1518 DBG3("UST app disable channel failed. Application is dead.");
1523 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1524 ua_chan
->name
, app
->pid
);
1527 health_code_update();
1532 * Enable the specified channel on to UST tracer for the UST session.
1534 static int enable_ust_channel(struct ust_app
*app
,
1535 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1539 health_code_update();
1541 pthread_mutex_lock(&app
->sock_lock
);
1542 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1543 pthread_mutex_unlock(&app
->sock_lock
);
1545 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1546 ERR("UST app channel %s enable failed for app (pid: %d) "
1547 "and session handle %d with ret %d",
1548 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1551 * This is normal behavior, an application can die during the
1552 * creation process. Don't report an error so the execution can
1553 * continue normally.
1556 DBG3("UST app enable channel failed. Application is dead.");
1561 ua_chan
->enabled
= 1;
1563 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1564 ua_chan
->name
, app
->pid
);
1567 health_code_update();
1572 * Enable the specified event on to UST tracer for the UST session.
1574 static int enable_ust_object(
1575 struct ust_app
*app
, struct lttng_ust_object_data
*ust_object
)
1579 health_code_update();
1581 pthread_mutex_lock(&app
->sock_lock
);
1582 ret
= ustctl_enable(app
->sock
, ust_object
);
1583 pthread_mutex_unlock(&app
->sock_lock
);
1585 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1586 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
1587 ust_object
, app
->pid
, ret
);
1590 * This is normal behavior, an application can die during the
1591 * creation process. Don't report an error so the execution can
1592 * continue normally.
1595 DBG3("Failed to enable UST app object. Application is dead.");
1600 DBG2("UST app object %p enabled successfully for app (pid: %d)",
1601 ust_object
, app
->pid
);
1604 health_code_update();
1609 * Send channel and stream buffer to application.
1611 * Return 0 on success. On error, a negative value is returned.
1613 static int send_channel_pid_to_ust(struct ust_app
*app
,
1614 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1617 struct ust_app_stream
*stream
, *stmp
;
1623 health_code_update();
1625 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1628 /* Send channel to the application. */
1629 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1630 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1631 ret
= -ENOTCONN
; /* Caused by app exiting. */
1633 } else if (ret
< 0) {
1637 health_code_update();
1639 /* Send all streams to application. */
1640 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1641 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1642 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1643 ret
= -ENOTCONN
; /* Caused by app exiting. */
1645 } else if (ret
< 0) {
1648 /* We don't need the stream anymore once sent to the tracer. */
1649 cds_list_del(&stream
->list
);
1650 delete_ust_app_stream(-1, stream
, app
);
1652 /* Flag the channel that it is sent to the application. */
1653 ua_chan
->is_sent
= 1;
1656 health_code_update();
1661 * Create the specified event onto the UST tracer for a UST session.
1663 * Should be called with session mutex held.
1666 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1667 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1671 health_code_update();
1673 /* Create UST event on tracer */
1674 pthread_mutex_lock(&app
->sock_lock
);
1675 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1677 pthread_mutex_unlock(&app
->sock_lock
);
1679 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1681 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1682 ua_event
->attr
.name
, app
->pid
, ret
);
1685 * This is normal behavior, an application can die during the
1686 * creation process. Don't report an error so the execution can
1687 * continue normally.
1690 DBG3("UST app create event failed. Application is dead.");
1695 ua_event
->handle
= ua_event
->obj
->handle
;
1697 DBG2("UST app event %s created successfully for pid:%d object: %p",
1698 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
1700 health_code_update();
1702 /* Set filter if one is present. */
1703 if (ua_event
->filter
) {
1704 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
1710 /* Set exclusions for the event */
1711 if (ua_event
->exclusion
) {
1712 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
1718 /* If event not enabled, disable it on the tracer */
1719 if (ua_event
->enabled
) {
1721 * We now need to explicitly enable the event, since it
1722 * is now disabled at creation.
1724 ret
= enable_ust_object(app
, ua_event
->obj
);
1727 * If we hit an EPERM, something is wrong with our enable call. If
1728 * we get an EEXIST, there is a problem on the tracer side since we
1732 case -LTTNG_UST_ERR_PERM
:
1733 /* Code flow problem */
1735 case -LTTNG_UST_ERR_EXIST
:
1736 /* It's OK for our use case. */
1747 health_code_update();
1752 * Copy data between an UST app event and a LTT event.
1754 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1755 struct ltt_ust_event
*uevent
)
1757 size_t exclusion_alloc_size
;
1759 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1760 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1762 ua_event
->enabled
= uevent
->enabled
;
1764 /* Copy event attributes */
1765 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1767 /* Copy filter bytecode */
1768 if (uevent
->filter
) {
1769 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1770 /* Filter might be NULL here in case of ENONEM. */
1773 /* Copy exclusion data */
1774 if (uevent
->exclusion
) {
1775 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1776 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1777 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1778 if (ua_event
->exclusion
== NULL
) {
1781 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1782 exclusion_alloc_size
);
1788 * Copy data between an UST app channel and a LTT channel.
1790 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1791 struct ltt_ust_channel
*uchan
)
1793 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1795 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1796 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1798 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1799 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1801 /* Copy event attributes since the layout is different. */
1802 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1803 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1804 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1805 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1806 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1807 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1808 ua_chan
->attr
.output
= uchan
->attr
.output
;
1809 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
1812 * Note that the attribute channel type is not set since the channel on the
1813 * tracing registry side does not have this information.
1816 ua_chan
->enabled
= uchan
->enabled
;
1817 ua_chan
->tracing_channel_id
= uchan
->id
;
1819 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1823 * Copy data between a UST app session and a regular LTT session.
1825 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1826 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1828 struct tm
*timeinfo
;
1831 char tmp_shm_path
[PATH_MAX
];
1833 timeinfo
= localtime(&app
->registration_time
);
1834 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1836 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1838 ua_sess
->tracing_id
= usess
->id
;
1839 ua_sess
->id
= get_next_session_id();
1840 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
1841 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
1842 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
1843 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
1844 ua_sess
->buffer_type
= usess
->buffer_type
;
1845 ua_sess
->bits_per_long
= app
->bits_per_long
;
1847 /* There is only one consumer object per session possible. */
1848 consumer_output_get(usess
->consumer
);
1849 ua_sess
->consumer
= usess
->consumer
;
1851 ua_sess
->output_traces
= usess
->output_traces
;
1852 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1853 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1854 &usess
->metadata_attr
);
1856 switch (ua_sess
->buffer_type
) {
1857 case LTTNG_BUFFER_PER_PID
:
1858 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1859 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1862 case LTTNG_BUFFER_PER_UID
:
1863 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1864 DEFAULT_UST_TRACE_UID_PATH
,
1865 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
1866 app
->bits_per_long
);
1873 PERROR("asprintf UST shadow copy session");
1878 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1879 sizeof(ua_sess
->root_shm_path
));
1880 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1881 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1882 sizeof(ua_sess
->shm_path
));
1883 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1884 if (ua_sess
->shm_path
[0]) {
1885 switch (ua_sess
->buffer_type
) {
1886 case LTTNG_BUFFER_PER_PID
:
1887 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1888 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1889 app
->name
, app
->pid
, datetime
);
1891 case LTTNG_BUFFER_PER_UID
:
1892 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1893 "/" DEFAULT_UST_TRACE_UID_PATH
,
1894 app
->uid
, app
->bits_per_long
);
1901 PERROR("sprintf UST shadow copy session");
1905 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1906 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1907 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1912 consumer_output_put(ua_sess
->consumer
);
1916 * Lookup sesison wrapper.
1919 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
1920 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1922 /* Get right UST app session from app */
1923 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1927 * Return ust app session from the app session hashtable using the UST session
1930 static struct ust_app_session
*lookup_session_by_app(
1931 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
1933 struct lttng_ht_iter iter
;
1934 struct lttng_ht_node_u64
*node
;
1936 __lookup_session_by_app(usess
, app
, &iter
);
1937 node
= lttng_ht_iter_get_node_u64(&iter
);
1942 return caa_container_of(node
, struct ust_app_session
, node
);
1949 * Setup buffer registry per PID for the given session and application. If none
1950 * is found, a new one is created, added to the global registry and
1951 * initialized. If regp is valid, it's set with the newly created object.
1953 * Return 0 on success or else a negative value.
1955 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1956 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1959 struct buffer_reg_pid
*reg_pid
;
1966 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1969 * This is the create channel path meaning that if there is NO
1970 * registry available, we have to create one for this session.
1972 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
1973 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1981 /* Initialize registry. */
1982 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1983 app
->bits_per_long
, app
->uint8_t_alignment
,
1984 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1985 app
->uint64_t_alignment
, app
->long_alignment
,
1986 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
1987 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
1988 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
1989 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
1990 ua_sess
->tracing_id
,
1994 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1995 * destroy the buffer registry, because it is always expected
1996 * that if the buffer registry can be found, its ust registry is
1999 buffer_reg_pid_destroy(reg_pid
);
2003 buffer_reg_pid_add(reg_pid
);
2005 DBG3("UST app buffer registry per PID created successfully");
2017 * Setup buffer registry per UID for the given session and application. If none
2018 * is found, a new one is created, added to the global registry and
2019 * initialized. If regp is valid, it's set with the newly created object.
2021 * Return 0 on success or else a negative value.
2023 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2024 struct ust_app_session
*ua_sess
,
2025 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2028 struct buffer_reg_uid
*reg_uid
;
2035 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2038 * This is the create channel path meaning that if there is NO
2039 * registry available, we have to create one for this session.
2041 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2042 LTTNG_DOMAIN_UST
, ®_uid
,
2043 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2051 /* Initialize registry. */
2052 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2053 app
->bits_per_long
, app
->uint8_t_alignment
,
2054 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2055 app
->uint64_t_alignment
, app
->long_alignment
,
2056 app
->byte_order
, app
->version
.major
,
2057 app
->version
.minor
, reg_uid
->root_shm_path
,
2058 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2059 ua_sess
->tracing_id
, app
->uid
);
2062 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2063 * destroy the buffer registry, because it is always expected
2064 * that if the buffer registry can be found, its ust registry is
2067 buffer_reg_uid_destroy(reg_uid
, NULL
);
2070 /* Add node to teardown list of the session. */
2071 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2073 buffer_reg_uid_add(reg_uid
);
2075 DBG3("UST app buffer registry per UID created successfully");
2086 * Create a session on the tracer side for the given app.
2088 * On success, ua_sess_ptr is populated with the session pointer or else left
2089 * untouched. If the session was created, is_created is set to 1. On error,
2090 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2093 * Returns 0 on success or else a negative code which is either -ENOMEM or
2094 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2096 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2097 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2100 int ret
, created
= 0;
2101 struct ust_app_session
*ua_sess
;
2105 assert(ua_sess_ptr
);
2107 health_code_update();
2109 ua_sess
= lookup_session_by_app(usess
, app
);
2110 if (ua_sess
== NULL
) {
2111 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2112 app
->pid
, usess
->id
);
2113 ua_sess
= alloc_ust_app_session();
2114 if (ua_sess
== NULL
) {
2115 /* Only malloc can failed so something is really wrong */
2119 shadow_copy_session(ua_sess
, usess
, app
);
2123 switch (usess
->buffer_type
) {
2124 case LTTNG_BUFFER_PER_PID
:
2125 /* Init local registry. */
2126 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2128 delete_ust_app_session(-1, ua_sess
, app
);
2132 case LTTNG_BUFFER_PER_UID
:
2133 /* Look for a global registry. If none exists, create one. */
2134 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2136 delete_ust_app_session(-1, ua_sess
, app
);
2146 health_code_update();
2148 if (ua_sess
->handle
== -1) {
2149 pthread_mutex_lock(&app
->sock_lock
);
2150 ret
= ustctl_create_session(app
->sock
);
2151 pthread_mutex_unlock(&app
->sock_lock
);
2153 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2154 ERR("Creating session for app pid %d with ret %d",
2157 DBG("UST app creating session failed. Application is dead");
2159 * This is normal behavior, an application can die during the
2160 * creation process. Don't report an error so the execution can
2161 * continue normally. This will get flagged ENOTCONN and the
2162 * caller will handle it.
2166 delete_ust_app_session(-1, ua_sess
, app
);
2167 if (ret
!= -ENOMEM
) {
2169 * Tracer is probably gone or got an internal error so let's
2170 * behave like it will soon unregister or not usable.
2177 ua_sess
->handle
= ret
;
2179 /* Add ust app session to app's HT */
2180 lttng_ht_node_init_u64(&ua_sess
->node
,
2181 ua_sess
->tracing_id
);
2182 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2183 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2184 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2185 &ua_sess
->ust_objd_node
);
2187 DBG2("UST app session created successfully with handle %d", ret
);
2190 *ua_sess_ptr
= ua_sess
;
2192 *is_created
= created
;
2195 /* Everything went well. */
2199 health_code_update();
2204 * Match function for a hash table lookup of ust_app_ctx.
2206 * It matches an ust app context based on the context type and, in the case
2207 * of perf counters, their name.
2209 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2211 struct ust_app_ctx
*ctx
;
2212 const struct lttng_ust_context_attr
*key
;
2217 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2221 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2226 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2227 if (strncmp(key
->u
.perf_counter
.name
,
2228 ctx
->ctx
.u
.perf_counter
.name
,
2229 sizeof(key
->u
.perf_counter
.name
))) {
2233 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2234 if (strcmp(key
->u
.app_ctx
.provider_name
,
2235 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2236 strcmp(key
->u
.app_ctx
.ctx_name
,
2237 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2253 * Lookup for an ust app context from an lttng_ust_context.
2255 * Must be called while holding RCU read side lock.
2256 * Return an ust_app_ctx object or NULL on error.
2259 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2260 struct lttng_ust_context_attr
*uctx
)
2262 struct lttng_ht_iter iter
;
2263 struct lttng_ht_node_ulong
*node
;
2264 struct ust_app_ctx
*app_ctx
= NULL
;
2269 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2270 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2271 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2272 node
= lttng_ht_iter_get_node_ulong(&iter
);
2277 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2284 * Create a context for the channel on the tracer.
2286 * Called with UST app session lock held and a RCU read side lock.
2289 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2290 struct lttng_ust_context_attr
*uctx
,
2291 struct ust_app
*app
)
2294 struct ust_app_ctx
*ua_ctx
;
2296 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2298 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2304 ua_ctx
= alloc_ust_app_ctx(uctx
);
2305 if (ua_ctx
== NULL
) {
2311 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2312 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2313 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2315 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2325 * Enable on the tracer side a ust app event for the session and channel.
2327 * Called with UST app session lock held.
2330 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2331 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2335 ret
= enable_ust_object(app
, ua_event
->obj
);
2340 ua_event
->enabled
= 1;
2347 * Disable on the tracer side a ust app event for the session and channel.
2349 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2350 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2354 ret
= disable_ust_object(app
, ua_event
->obj
);
2359 ua_event
->enabled
= 0;
2366 * Lookup ust app channel for session and disable it on the tracer side.
2369 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2370 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2374 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2379 ua_chan
->enabled
= 0;
2386 * Lookup ust app channel for session and enable it on the tracer side. This
2387 * MUST be called with a RCU read side lock acquired.
2389 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2390 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2393 struct lttng_ht_iter iter
;
2394 struct lttng_ht_node_str
*ua_chan_node
;
2395 struct ust_app_channel
*ua_chan
;
2397 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2398 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2399 if (ua_chan_node
== NULL
) {
2400 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2401 uchan
->name
, ua_sess
->tracing_id
);
2405 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2407 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2417 * Ask the consumer to create a channel and get it if successful.
2419 * Called with UST app session lock held.
2421 * Return 0 on success or else a negative value.
2423 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2424 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2425 int bitness
, struct ust_registry_session
*registry
,
2426 uint64_t trace_archive_id
)
2429 unsigned int nb_fd
= 0;
2430 struct consumer_socket
*socket
;
2438 health_code_update();
2440 /* Get the right consumer socket for the application. */
2441 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2447 health_code_update();
2449 /* Need one fd for the channel. */
2450 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2452 ERR("Exhausted number of available FD upon create channel");
2457 * Ask consumer to create channel. The consumer will return the number of
2458 * stream we have to expect.
2460 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2461 registry
, usess
->current_trace_chunk
);
2467 * Compute the number of fd needed before receiving them. It must be 2 per
2468 * stream (2 being the default value here).
2470 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2472 /* Reserve the amount of file descriptor we need. */
2473 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2475 ERR("Exhausted number of available FD upon create channel");
2476 goto error_fd_get_stream
;
2479 health_code_update();
2482 * Now get the channel from the consumer. This call wil populate the stream
2483 * list of that channel and set the ust objects.
2485 if (usess
->consumer
->enabled
) {
2486 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2496 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2497 error_fd_get_stream
:
2499 * Initiate a destroy channel on the consumer since we had an error
2500 * handling it on our side. The return value is of no importance since we
2501 * already have a ret value set by the previous error that we need to
2504 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2506 lttng_fd_put(LTTNG_FD_APPS
, 1);
2508 health_code_update();
2514 * Duplicate the ust data object of the ust app stream and save it in the
2515 * buffer registry stream.
2517 * Return 0 on success or else a negative value.
2519 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2520 struct ust_app_stream
*stream
)
2527 /* Reserve the amount of file descriptor we need. */
2528 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2530 ERR("Exhausted number of available FD upon duplicate stream");
2534 /* Duplicate object for stream once the original is in the registry. */
2535 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2536 reg_stream
->obj
.ust
);
2538 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2539 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2540 lttng_fd_put(LTTNG_FD_APPS
, 2);
2543 stream
->handle
= stream
->obj
->handle
;
2550 * Duplicate the ust data object of the ust app. channel and save it in the
2551 * buffer registry channel.
2553 * Return 0 on success or else a negative value.
2555 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2556 struct ust_app_channel
*ua_chan
)
2563 /* Need two fds for the channel. */
2564 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2566 ERR("Exhausted number of available FD upon duplicate channel");
2570 /* Duplicate object for stream once the original is in the registry. */
2571 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2573 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2574 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2577 ua_chan
->handle
= ua_chan
->obj
->handle
;
2582 lttng_fd_put(LTTNG_FD_APPS
, 1);
2588 * For a given channel buffer registry, setup all streams of the given ust
2589 * application channel.
2591 * Return 0 on success or else a negative value.
2593 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2594 struct ust_app_channel
*ua_chan
,
2595 struct ust_app
*app
)
2598 struct ust_app_stream
*stream
, *stmp
;
2603 DBG2("UST app setup buffer registry stream");
2605 /* Send all streams to application. */
2606 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2607 struct buffer_reg_stream
*reg_stream
;
2609 ret
= buffer_reg_stream_create(®_stream
);
2615 * Keep original pointer and nullify it in the stream so the delete
2616 * stream call does not release the object.
2618 reg_stream
->obj
.ust
= stream
->obj
;
2620 buffer_reg_stream_add(reg_stream
, reg_chan
);
2622 /* We don't need the streams anymore. */
2623 cds_list_del(&stream
->list
);
2624 delete_ust_app_stream(-1, stream
, app
);
2632 * Create a buffer registry channel for the given session registry and
2633 * application channel object. If regp pointer is valid, it's set with the
2634 * created object. Important, the created object is NOT added to the session
2635 * registry hash table.
2637 * Return 0 on success else a negative value.
2639 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2640 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2643 struct buffer_reg_channel
*reg_chan
= NULL
;
2648 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2650 /* Create buffer registry channel. */
2651 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2656 reg_chan
->consumer_key
= ua_chan
->key
;
2657 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2658 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2660 /* Create and add a channel registry to session. */
2661 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2662 ua_chan
->tracing_channel_id
);
2666 buffer_reg_channel_add(reg_sess
, reg_chan
);
2675 /* Safe because the registry channel object was not added to any HT. */
2676 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2682 * Setup buffer registry channel for the given session registry and application
2683 * channel object. If regp pointer is valid, it's set with the created object.
2685 * Return 0 on success else a negative value.
2687 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2688 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2689 struct ust_app
*app
)
2696 assert(ua_chan
->obj
);
2698 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2700 /* Setup all streams for the registry. */
2701 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2706 reg_chan
->obj
.ust
= ua_chan
->obj
;
2707 ua_chan
->obj
= NULL
;
2712 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2713 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2718 * Send buffer registry channel to the application.
2720 * Return 0 on success else a negative value.
2722 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2723 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2724 struct ust_app_channel
*ua_chan
)
2727 struct buffer_reg_stream
*reg_stream
;
2734 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2736 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2741 /* Send channel to the application. */
2742 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2743 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2744 ret
= -ENOTCONN
; /* Caused by app exiting. */
2746 } else if (ret
< 0) {
2750 health_code_update();
2752 /* Send all streams to application. */
2753 pthread_mutex_lock(®_chan
->stream_list_lock
);
2754 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2755 struct ust_app_stream stream
;
2757 ret
= duplicate_stream_object(reg_stream
, &stream
);
2759 goto error_stream_unlock
;
2762 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2764 (void) release_ust_app_stream(-1, &stream
, app
);
2765 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2766 ret
= -ENOTCONN
; /* Caused by app exiting. */
2768 goto error_stream_unlock
;
2772 * The return value is not important here. This function will output an
2775 (void) release_ust_app_stream(-1, &stream
, app
);
2777 ua_chan
->is_sent
= 1;
2779 error_stream_unlock
:
2780 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2786 * Create and send to the application the created buffers with per UID buffers.
2788 * This MUST be called with a RCU read side lock acquired.
2789 * The session list lock and the session's lock must be acquired.
2791 * Return 0 on success else a negative value.
2793 static int create_channel_per_uid(struct ust_app
*app
,
2794 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2795 struct ust_app_channel
*ua_chan
)
2798 struct buffer_reg_uid
*reg_uid
;
2799 struct buffer_reg_channel
*reg_chan
;
2800 struct ltt_session
*session
= NULL
;
2801 enum lttng_error_code notification_ret
;
2802 struct ust_registry_channel
*chan_reg
;
2809 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2811 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2813 * The session creation handles the creation of this global registry
2814 * object. If none can be find, there is a code flow problem or a
2819 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2825 /* Create the buffer registry channel object. */
2826 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2828 ERR("Error creating the UST channel \"%s\" registry instance",
2833 session
= session_find_by_id(ua_sess
->tracing_id
);
2835 assert(pthread_mutex_trylock(&session
->lock
));
2836 assert(session_trylock_list());
2839 * Create the buffers on the consumer side. This call populates the
2840 * ust app channel object with all streams and data object.
2842 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2843 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
2844 session
->most_recent_chunk_id
.value
);
2846 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2850 * Let's remove the previously created buffer registry channel so
2851 * it's not visible anymore in the session registry.
2853 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2854 ua_chan
->tracing_channel_id
, false);
2855 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2856 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2861 * Setup the streams and add it to the session registry.
2863 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2864 ua_chan
, reg_chan
, app
);
2866 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
2870 /* Notify the notification subsystem of the channel's creation. */
2871 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2872 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2873 ua_chan
->tracing_channel_id
);
2875 chan_reg
->consumer_key
= ua_chan
->key
;
2877 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2879 notification_ret
= notification_thread_command_add_channel(
2880 notification_thread_handle
, session
->name
,
2881 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2882 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2884 ua_chan
->key
, LTTNG_DOMAIN_UST
,
2885 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2886 if (notification_ret
!= LTTNG_OK
) {
2887 ret
= - (int) notification_ret
;
2888 ERR("Failed to add channel to notification thread");
2893 /* Send buffers to the application. */
2894 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2896 if (ret
!= -ENOTCONN
) {
2897 ERR("Error sending channel to application");
2904 session_put(session
);
2910 * Create and send to the application the created buffers with per PID buffers.
2912 * Called with UST app session lock held.
2913 * The session list lock and the session's lock must be acquired.
2915 * Return 0 on success else a negative value.
2917 static int create_channel_per_pid(struct ust_app
*app
,
2918 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2919 struct ust_app_channel
*ua_chan
)
2922 struct ust_registry_session
*registry
;
2923 enum lttng_error_code cmd_ret
;
2924 struct ltt_session
*session
= NULL
;
2925 uint64_t chan_reg_key
;
2926 struct ust_registry_channel
*chan_reg
;
2933 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2937 registry
= get_session_registry(ua_sess
);
2938 /* The UST app session lock is held, registry shall not be null. */
2941 /* Create and add a new channel registry to session. */
2942 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2944 ERR("Error creating the UST channel \"%s\" registry instance",
2949 session
= session_find_by_id(ua_sess
->tracing_id
);
2952 assert(pthread_mutex_trylock(&session
->lock
));
2953 assert(session_trylock_list());
2955 /* Create and get channel on the consumer side. */
2956 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2957 app
->bits_per_long
, registry
,
2958 session
->most_recent_chunk_id
.value
);
2960 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2962 goto error_remove_from_registry
;
2965 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2967 if (ret
!= -ENOTCONN
) {
2968 ERR("Error sending channel to application");
2970 goto error_remove_from_registry
;
2973 chan_reg_key
= ua_chan
->key
;
2974 pthread_mutex_lock(®istry
->lock
);
2975 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
2977 chan_reg
->consumer_key
= ua_chan
->key
;
2978 pthread_mutex_unlock(®istry
->lock
);
2980 cmd_ret
= notification_thread_command_add_channel(
2981 notification_thread_handle
, session
->name
,
2982 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2983 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2985 ua_chan
->key
, LTTNG_DOMAIN_UST
,
2986 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2987 if (cmd_ret
!= LTTNG_OK
) {
2988 ret
= - (int) cmd_ret
;
2989 ERR("Failed to add channel to notification thread");
2990 goto error_remove_from_registry
;
2993 error_remove_from_registry
:
2995 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3000 session_put(session
);
3006 * From an already allocated ust app channel, create the channel buffers if
3007 * needed and send them to the application. This MUST be called with a RCU read
3008 * side lock acquired.
3010 * Called with UST app session lock held.
3012 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3013 * the application exited concurrently.
3015 static int ust_app_channel_send(struct ust_app
*app
,
3016 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3017 struct ust_app_channel
*ua_chan
)
3023 assert(usess
->active
);
3027 /* Handle buffer type before sending the channel to the application. */
3028 switch (usess
->buffer_type
) {
3029 case LTTNG_BUFFER_PER_UID
:
3031 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3037 case LTTNG_BUFFER_PER_PID
:
3039 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3051 /* Initialize ust objd object using the received handle and add it. */
3052 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3053 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3055 /* If channel is not enabled, disable it on the tracer */
3056 if (!ua_chan
->enabled
) {
3057 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3068 * Create UST app channel and return it through ua_chanp if not NULL.
3070 * Called with UST app session lock and RCU read-side lock held.
3072 * Return 0 on success or else a negative value.
3074 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3075 struct ltt_ust_channel
*uchan
,
3076 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3077 struct ust_app_channel
**ua_chanp
)
3080 struct lttng_ht_iter iter
;
3081 struct lttng_ht_node_str
*ua_chan_node
;
3082 struct ust_app_channel
*ua_chan
;
3084 /* Lookup channel in the ust app session */
3085 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3086 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3087 if (ua_chan_node
!= NULL
) {
3088 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3092 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3093 if (ua_chan
== NULL
) {
3094 /* Only malloc can fail here */
3098 shadow_copy_channel(ua_chan
, uchan
);
3100 /* Set channel type. */
3101 ua_chan
->attr
.type
= type
;
3103 /* Only add the channel if successful on the tracer side. */
3104 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3107 *ua_chanp
= ua_chan
;
3110 /* Everything went well. */
3118 * Create UST app event and create it on the tracer side.
3120 * Called with ust app session mutex held.
3123 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3124 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3125 struct ust_app
*app
)
3128 struct ust_app_event
*ua_event
;
3130 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3131 if (ua_event
== NULL
) {
3132 /* Only failure mode of alloc_ust_app_event(). */
3136 shadow_copy_event(ua_event
, uevent
);
3138 /* Create it on the tracer side */
3139 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3142 * Not found previously means that it does not exist on the
3143 * tracer. If the application reports that the event existed,
3144 * it means there is a bug in the sessiond or lttng-ust
3145 * (or corruption, etc.)
3147 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3148 ERR("Tracer for application reported that an event being created already existed: "
3149 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3151 app
->pid
, app
->ppid
, app
->uid
,
3157 add_unique_ust_app_event(ua_chan
, ua_event
);
3159 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3166 /* Valid. Calling here is already in a read side lock */
3167 delete_ust_app_event(-1, ua_event
, app
);
3172 * Create UST metadata and open it on the tracer side.
3174 * Called with UST app session lock held and RCU read side lock.
3176 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3177 struct ust_app
*app
, struct consumer_output
*consumer
)
3180 struct ust_app_channel
*metadata
;
3181 struct consumer_socket
*socket
;
3182 struct ust_registry_session
*registry
;
3183 struct ltt_session
*session
= NULL
;
3189 registry
= get_session_registry(ua_sess
);
3190 /* The UST app session is held registry shall not be null. */
3193 pthread_mutex_lock(®istry
->lock
);
3195 /* Metadata already exists for this registry or it was closed previously */
3196 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3201 /* Allocate UST metadata */
3202 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3204 /* malloc() failed */
3209 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3211 /* Need one fd for the channel. */
3212 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3214 ERR("Exhausted number of available FD upon create metadata");
3218 /* Get the right consumer socket for the application. */
3219 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3222 goto error_consumer
;
3226 * Keep metadata key so we can identify it on the consumer side. Assign it
3227 * to the registry *before* we ask the consumer so we avoid the race of the
3228 * consumer requesting the metadata and the ask_channel call on our side
3229 * did not returned yet.
3231 registry
->metadata_key
= metadata
->key
;
3233 session
= session_find_by_id(ua_sess
->tracing_id
);
3236 assert(pthread_mutex_trylock(&session
->lock
));
3237 assert(session_trylock_list());
3240 * Ask the metadata channel creation to the consumer. The metadata object
3241 * will be created by the consumer and kept their. However, the stream is
3242 * never added or monitored until we do a first push metadata to the
3245 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3246 registry
, session
->current_trace_chunk
);
3248 /* Nullify the metadata key so we don't try to close it later on. */
3249 registry
->metadata_key
= 0;
3250 goto error_consumer
;
3254 * The setup command will make the metadata stream be sent to the relayd,
3255 * if applicable, and the thread managing the metadatas. This is important
3256 * because after this point, if an error occurs, the only way the stream
3257 * can be deleted is to be monitored in the consumer.
3259 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3261 /* Nullify the metadata key so we don't try to close it later on. */
3262 registry
->metadata_key
= 0;
3263 goto error_consumer
;
3266 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3267 metadata
->key
, app
->pid
);
3270 lttng_fd_put(LTTNG_FD_APPS
, 1);
3271 delete_ust_app_channel(-1, metadata
, app
);
3273 pthread_mutex_unlock(®istry
->lock
);
3275 session_put(session
);
3281 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3282 * acquired before calling this function.
3284 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3286 struct ust_app
*app
= NULL
;
3287 struct lttng_ht_node_ulong
*node
;
3288 struct lttng_ht_iter iter
;
3290 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3291 node
= lttng_ht_iter_get_node_ulong(&iter
);
3293 DBG2("UST app no found with pid %d", pid
);
3297 DBG2("Found UST app by pid %d", pid
);
3299 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3306 * Allocate and init an UST app object using the registration information and
3307 * the command socket. This is called when the command socket connects to the
3310 * The object is returned on success or else NULL.
3312 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3314 struct ust_app
*lta
= NULL
;
3319 DBG3("UST app creating application for socket %d", sock
);
3321 if ((msg
->bits_per_long
== 64 &&
3322 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3323 || (msg
->bits_per_long
== 32 &&
3324 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3325 ERR("Registration failed: application \"%s\" (pid: %d) has "
3326 "%d-bit long, but no consumerd for this size is available.\n",
3327 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3331 lta
= zmalloc(sizeof(struct ust_app
));
3337 lta
->ppid
= msg
->ppid
;
3338 lta
->uid
= msg
->uid
;
3339 lta
->gid
= msg
->gid
;
3341 lta
->bits_per_long
= msg
->bits_per_long
;
3342 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3343 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3344 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3345 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3346 lta
->long_alignment
= msg
->long_alignment
;
3347 lta
->byte_order
= msg
->byte_order
;
3349 lta
->v_major
= msg
->major
;
3350 lta
->v_minor
= msg
->minor
;
3351 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3352 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3353 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3354 lta
->notify_sock
= -1;
3356 /* Copy name and make sure it's NULL terminated. */
3357 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3358 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3361 * Before this can be called, when receiving the registration information,
3362 * the application compatibility is checked. So, at this point, the
3363 * application can work with this session daemon.
3365 lta
->compatible
= 1;
3367 lta
->pid
= msg
->pid
;
3368 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3370 pthread_mutex_init(<a
->sock_lock
, NULL
);
3371 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3373 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3379 * For a given application object, add it to every hash table.
3381 void ust_app_add(struct ust_app
*app
)
3384 assert(app
->notify_sock
>= 0);
3386 app
->registration_time
= time(NULL
);
3391 * On a re-registration, we want to kick out the previous registration of
3394 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3397 * The socket _should_ be unique until _we_ call close. So, a add_unique
3398 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3399 * already in the table.
3401 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3403 /* Add application to the notify socket hash table. */
3404 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3405 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3407 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3408 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3409 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3416 * Set the application version into the object.
3418 * Return 0 on success else a negative value either an errno code or a
3419 * LTTng-UST error code.
3421 int ust_app_version(struct ust_app
*app
)
3427 pthread_mutex_lock(&app
->sock_lock
);
3428 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3429 pthread_mutex_unlock(&app
->sock_lock
);
3431 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3432 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3434 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3442 * Unregister app by removing it from the global traceable app list and freeing
3445 * The socket is already closed at this point so no close to sock.
3447 void ust_app_unregister(int sock
)
3449 struct ust_app
*lta
;
3450 struct lttng_ht_node_ulong
*node
;
3451 struct lttng_ht_iter ust_app_sock_iter
;
3452 struct lttng_ht_iter iter
;
3453 struct ust_app_session
*ua_sess
;
3458 /* Get the node reference for a call_rcu */
3459 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3460 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3463 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3464 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3467 * For per-PID buffers, perform "push metadata" and flush all
3468 * application streams before removing app from hash tables,
3469 * ensuring proper behavior of data_pending check.
3470 * Remove sessions so they are not visible during deletion.
3472 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3474 struct ust_registry_session
*registry
;
3476 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3478 /* The session was already removed so scheduled for teardown. */
3482 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3483 (void) ust_app_flush_app_session(lta
, ua_sess
);
3487 * Add session to list for teardown. This is safe since at this point we
3488 * are the only one using this list.
3490 pthread_mutex_lock(&ua_sess
->lock
);
3492 if (ua_sess
->deleted
) {
3493 pthread_mutex_unlock(&ua_sess
->lock
);
3498 * Normally, this is done in the delete session process which is
3499 * executed in the call rcu below. However, upon registration we can't
3500 * afford to wait for the grace period before pushing data or else the
3501 * data pending feature can race between the unregistration and stop
3502 * command where the data pending command is sent *before* the grace
3505 * The close metadata below nullifies the metadata pointer in the
3506 * session so the delete session will NOT push/close a second time.
3508 registry
= get_session_registry(ua_sess
);
3510 /* Push metadata for application before freeing the application. */
3511 (void) push_metadata(registry
, ua_sess
->consumer
);
3514 * Don't ask to close metadata for global per UID buffers. Close
3515 * metadata only on destroy trace session in this case. Also, the
3516 * previous push metadata could have flag the metadata registry to
3517 * close so don't send a close command if closed.
3519 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3520 /* And ask to close it for this session registry. */
3521 (void) close_metadata(registry
, ua_sess
->consumer
);
3524 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3526 pthread_mutex_unlock(&ua_sess
->lock
);
3529 /* Remove application from PID hash table */
3530 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3534 * Remove application from notify hash table. The thread handling the
3535 * notify socket could have deleted the node so ignore on error because
3536 * either way it's valid. The close of that socket is handled by the
3537 * apps_notify_thread.
3539 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3540 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3543 * Ignore return value since the node might have been removed before by an
3544 * add replace during app registration because the PID can be reassigned by
3547 iter
.iter
.node
= <a
->pid_n
.node
;
3548 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3550 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3555 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3562 * Fill events array with all events name of all registered apps.
3564 int ust_app_list_events(struct lttng_event
**events
)
3567 size_t nbmem
, count
= 0;
3568 struct lttng_ht_iter iter
;
3569 struct ust_app
*app
;
3570 struct lttng_event
*tmp_event
;
3572 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3573 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3574 if (tmp_event
== NULL
) {
3575 PERROR("zmalloc ust app events");
3582 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3583 struct lttng_ust_tracepoint_iter uiter
;
3585 health_code_update();
3587 if (!app
->compatible
) {
3589 * TODO: In time, we should notice the caller of this error by
3590 * telling him that this is a version error.
3594 pthread_mutex_lock(&app
->sock_lock
);
3595 handle
= ustctl_tracepoint_list(app
->sock
);
3597 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3598 ERR("UST app list events getting handle failed for app pid %d",
3601 pthread_mutex_unlock(&app
->sock_lock
);
3605 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3606 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3607 /* Handle ustctl error. */
3611 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3612 ERR("UST app tp list get failed for app %d with ret %d",
3615 DBG3("UST app tp list get failed. Application is dead");
3617 * This is normal behavior, an application can die during the
3618 * creation process. Don't report an error so the execution can
3619 * continue normally. Continue normal execution.
3624 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3625 if (release_ret
< 0 &&
3626 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3627 release_ret
!= -EPIPE
) {
3628 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3630 pthread_mutex_unlock(&app
->sock_lock
);
3634 health_code_update();
3635 if (count
>= nbmem
) {
3636 /* In case the realloc fails, we free the memory */
3637 struct lttng_event
*new_tmp_event
;
3640 new_nbmem
= nbmem
<< 1;
3641 DBG2("Reallocating event list from %zu to %zu entries",
3643 new_tmp_event
= realloc(tmp_event
,
3644 new_nbmem
* sizeof(struct lttng_event
));
3645 if (new_tmp_event
== NULL
) {
3648 PERROR("realloc ust app events");
3651 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3652 if (release_ret
< 0 &&
3653 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3654 release_ret
!= -EPIPE
) {
3655 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3657 pthread_mutex_unlock(&app
->sock_lock
);
3660 /* Zero the new memory */
3661 memset(new_tmp_event
+ nbmem
, 0,
3662 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3664 tmp_event
= new_tmp_event
;
3666 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3667 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3668 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3669 tmp_event
[count
].pid
= app
->pid
;
3670 tmp_event
[count
].enabled
= -1;
3673 ret
= ustctl_release_handle(app
->sock
, handle
);
3674 pthread_mutex_unlock(&app
->sock_lock
);
3675 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3676 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3681 *events
= tmp_event
;
3683 DBG2("UST app list events done (%zu events)", count
);
3688 health_code_update();
3693 * Fill events array with all events name of all registered apps.
3695 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3698 size_t nbmem
, count
= 0;
3699 struct lttng_ht_iter iter
;
3700 struct ust_app
*app
;
3701 struct lttng_event_field
*tmp_event
;
3703 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3704 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3705 if (tmp_event
== NULL
) {
3706 PERROR("zmalloc ust app event fields");
3713 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3714 struct lttng_ust_field_iter uiter
;
3716 health_code_update();
3718 if (!app
->compatible
) {
3720 * TODO: In time, we should notice the caller of this error by
3721 * telling him that this is a version error.
3725 pthread_mutex_lock(&app
->sock_lock
);
3726 handle
= ustctl_tracepoint_field_list(app
->sock
);
3728 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3729 ERR("UST app list field getting handle failed for app pid %d",
3732 pthread_mutex_unlock(&app
->sock_lock
);
3736 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3737 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3738 /* Handle ustctl error. */
3742 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3743 ERR("UST app tp list field failed for app %d with ret %d",
3746 DBG3("UST app tp list field failed. Application is dead");
3748 * This is normal behavior, an application can die during the
3749 * creation process. Don't report an error so the execution can
3750 * continue normally. Reset list and count for next app.
3755 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3756 pthread_mutex_unlock(&app
->sock_lock
);
3757 if (release_ret
< 0 &&
3758 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3759 release_ret
!= -EPIPE
) {
3760 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3765 health_code_update();
3766 if (count
>= nbmem
) {
3767 /* In case the realloc fails, we free the memory */
3768 struct lttng_event_field
*new_tmp_event
;
3771 new_nbmem
= nbmem
<< 1;
3772 DBG2("Reallocating event field list from %zu to %zu entries",
3774 new_tmp_event
= realloc(tmp_event
,
3775 new_nbmem
* sizeof(struct lttng_event_field
));
3776 if (new_tmp_event
== NULL
) {
3779 PERROR("realloc ust app event fields");
3782 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3783 pthread_mutex_unlock(&app
->sock_lock
);
3785 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3786 release_ret
!= -EPIPE
) {
3787 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3791 /* Zero the new memory */
3792 memset(new_tmp_event
+ nbmem
, 0,
3793 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3795 tmp_event
= new_tmp_event
;
3798 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3799 /* Mapping between these enums matches 1 to 1. */
3800 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3801 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3803 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3804 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3805 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3806 tmp_event
[count
].event
.pid
= app
->pid
;
3807 tmp_event
[count
].event
.enabled
= -1;
3810 ret
= ustctl_release_handle(app
->sock
, handle
);
3811 pthread_mutex_unlock(&app
->sock_lock
);
3813 ret
!= -LTTNG_UST_ERR_EXITING
&&
3815 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3820 *fields
= tmp_event
;
3822 DBG2("UST app list event fields done (%zu events)", count
);
3827 health_code_update();
3832 * Free and clean all traceable apps of the global list.
3834 * Should _NOT_ be called with RCU read-side lock held.
3836 void ust_app_clean_list(void)
3839 struct ust_app
*app
;
3840 struct lttng_ht_iter iter
;
3842 DBG2("UST app cleaning registered apps hash table");
3846 /* Cleanup notify socket hash table */
3847 if (ust_app_ht_by_notify_sock
) {
3848 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3849 notify_sock_n
.node
) {
3850 struct cds_lfht_node
*node
;
3851 struct ust_app
*app
;
3853 node
= cds_lfht_iter_get_node(&iter
.iter
);
3858 app
= container_of(node
, struct ust_app
,
3859 notify_sock_n
.node
);
3860 ust_app_notify_sock_unregister(app
->notify_sock
);
3865 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3866 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3868 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3872 /* Cleanup socket hash table */
3873 if (ust_app_ht_by_sock
) {
3874 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3876 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3883 /* Destroy is done only when the ht is empty */
3885 ht_cleanup_push(ust_app_ht
);
3887 if (ust_app_ht_by_sock
) {
3888 ht_cleanup_push(ust_app_ht_by_sock
);
3890 if (ust_app_ht_by_notify_sock
) {
3891 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3896 * Init UST app hash table.
3898 int ust_app_ht_alloc(void)
3900 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3904 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3905 if (!ust_app_ht_by_sock
) {
3908 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3909 if (!ust_app_ht_by_notify_sock
) {
3916 * For a specific UST session, disable the channel for all registered apps.
3918 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3919 struct ltt_ust_channel
*uchan
)
3922 struct lttng_ht_iter iter
;
3923 struct lttng_ht_node_str
*ua_chan_node
;
3924 struct ust_app
*app
;
3925 struct ust_app_session
*ua_sess
;
3926 struct ust_app_channel
*ua_chan
;
3928 assert(usess
->active
);
3929 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3930 uchan
->name
, usess
->id
);
3934 /* For every registered applications */
3935 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3936 struct lttng_ht_iter uiter
;
3937 if (!app
->compatible
) {
3939 * TODO: In time, we should notice the caller of this error by
3940 * telling him that this is a version error.
3944 ua_sess
= lookup_session_by_app(usess
, app
);
3945 if (ua_sess
== NULL
) {
3950 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3951 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3952 /* If the session if found for the app, the channel must be there */
3953 assert(ua_chan_node
);
3955 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3956 /* The channel must not be already disabled */
3957 assert(ua_chan
->enabled
== 1);
3959 /* Disable channel onto application */
3960 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3962 /* XXX: We might want to report this error at some point... */
3972 * For a specific UST session, enable the channel for all registered apps.
3974 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3975 struct ltt_ust_channel
*uchan
)
3978 struct lttng_ht_iter iter
;
3979 struct ust_app
*app
;
3980 struct ust_app_session
*ua_sess
;
3982 assert(usess
->active
);
3983 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3984 uchan
->name
, usess
->id
);
3988 /* For every registered applications */
3989 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3990 if (!app
->compatible
) {
3992 * TODO: In time, we should notice the caller of this error by
3993 * telling him that this is a version error.
3997 ua_sess
= lookup_session_by_app(usess
, app
);
3998 if (ua_sess
== NULL
) {
4002 /* Enable channel onto application */
4003 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4005 /* XXX: We might want to report this error at some point... */
4015 * Disable an event in a channel and for a specific session.
4017 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4018 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4021 struct lttng_ht_iter iter
, uiter
;
4022 struct lttng_ht_node_str
*ua_chan_node
;
4023 struct ust_app
*app
;
4024 struct ust_app_session
*ua_sess
;
4025 struct ust_app_channel
*ua_chan
;
4026 struct ust_app_event
*ua_event
;
4028 assert(usess
->active
);
4029 DBG("UST app disabling event %s for all apps in channel "
4030 "%s for session id %" PRIu64
,
4031 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4035 /* For all registered applications */
4036 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4037 if (!app
->compatible
) {
4039 * TODO: In time, we should notice the caller of this error by
4040 * telling him that this is a version error.
4044 ua_sess
= lookup_session_by_app(usess
, app
);
4045 if (ua_sess
== NULL
) {
4050 /* Lookup channel in the ust app session */
4051 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4052 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4053 if (ua_chan_node
== NULL
) {
4054 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4055 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4058 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4060 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4061 uevent
->filter
, uevent
->attr
.loglevel
,
4063 if (ua_event
== NULL
) {
4064 DBG2("Event %s not found in channel %s for app pid %d."
4065 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4069 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4071 /* XXX: Report error someday... */
4080 /* The ua_sess lock must be held by the caller. */
4082 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4083 struct ust_app_session
*ua_sess
,
4084 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4085 struct ust_app_channel
**_ua_chan
)
4088 struct ust_app_channel
*ua_chan
= NULL
;
4091 ASSERT_LOCKED(ua_sess
->lock
);
4093 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4094 sizeof(uchan
->name
))) {
4095 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4099 struct ltt_ust_context
*uctx
= NULL
;
4102 * Create channel onto application and synchronize its
4105 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4106 LTTNG_UST_CHAN_PER_CPU
, usess
,
4112 ret
= ust_app_channel_send(app
, usess
,
4119 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4120 ret
= create_ust_app_channel_context(ua_chan
,
4133 * The application's socket is not valid. Either a bad socket
4134 * or a timeout on it. We can't inform the caller that for a
4135 * specific app, the session failed so lets continue here.
4137 ret
= 0; /* Not an error. */
4145 if (ret
== 0 && _ua_chan
) {
4147 * Only return the application's channel on success. Note
4148 * that the channel can still be part of the application's
4149 * channel hashtable on error.
4151 *_ua_chan
= ua_chan
;
4157 * Enable event for a specific session and channel on the tracer.
4159 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4160 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4163 struct lttng_ht_iter iter
, uiter
;
4164 struct lttng_ht_node_str
*ua_chan_node
;
4165 struct ust_app
*app
;
4166 struct ust_app_session
*ua_sess
;
4167 struct ust_app_channel
*ua_chan
;
4168 struct ust_app_event
*ua_event
;
4170 assert(usess
->active
);
4171 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4172 uevent
->attr
.name
, usess
->id
);
4175 * NOTE: At this point, this function is called only if the session and
4176 * channel passed are already created for all apps. and enabled on the
4182 /* For all registered applications */
4183 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4184 if (!app
->compatible
) {
4186 * TODO: In time, we should notice the caller of this error by
4187 * telling him that this is a version error.
4191 ua_sess
= lookup_session_by_app(usess
, app
);
4193 /* The application has problem or is probably dead. */
4197 pthread_mutex_lock(&ua_sess
->lock
);
4199 if (ua_sess
->deleted
) {
4200 pthread_mutex_unlock(&ua_sess
->lock
);
4204 /* Lookup channel in the ust app session */
4205 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4206 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4208 * It is possible that the channel cannot be found is
4209 * the channel/event creation occurs concurrently with
4210 * an application exit.
4212 if (!ua_chan_node
) {
4213 pthread_mutex_unlock(&ua_sess
->lock
);
4217 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4219 /* Get event node */
4220 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4221 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4222 if (ua_event
== NULL
) {
4223 DBG3("UST app enable event %s not found for app PID %d."
4224 "Skipping app", uevent
->attr
.name
, app
->pid
);
4228 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4230 pthread_mutex_unlock(&ua_sess
->lock
);
4234 pthread_mutex_unlock(&ua_sess
->lock
);
4243 * For a specific existing UST session and UST channel, creates the event for
4244 * all registered apps.
4246 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4247 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4250 struct lttng_ht_iter iter
, uiter
;
4251 struct lttng_ht_node_str
*ua_chan_node
;
4252 struct ust_app
*app
;
4253 struct ust_app_session
*ua_sess
;
4254 struct ust_app_channel
*ua_chan
;
4256 assert(usess
->active
);
4257 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4258 uevent
->attr
.name
, usess
->id
);
4262 /* For all registered applications */
4263 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4264 if (!app
->compatible
) {
4266 * TODO: In time, we should notice the caller of this error by
4267 * telling him that this is a version error.
4271 ua_sess
= lookup_session_by_app(usess
, app
);
4273 /* The application has problem or is probably dead. */
4277 pthread_mutex_lock(&ua_sess
->lock
);
4279 if (ua_sess
->deleted
) {
4280 pthread_mutex_unlock(&ua_sess
->lock
);
4284 /* Lookup channel in the ust app session */
4285 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4286 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4287 /* If the channel is not found, there is a code flow error */
4288 assert(ua_chan_node
);
4290 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4292 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4293 pthread_mutex_unlock(&ua_sess
->lock
);
4295 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4296 /* Possible value at this point: -ENOMEM. If so, we stop! */
4299 DBG2("UST app event %s already exist on app PID %d",
4300 uevent
->attr
.name
, app
->pid
);
4310 * Start tracing for a specific UST session and app.
4312 * Called with UST app session lock held.
4316 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4319 struct ust_app_session
*ua_sess
;
4321 DBG("Starting tracing for ust app pid %d", app
->pid
);
4325 if (!app
->compatible
) {
4329 ua_sess
= lookup_session_by_app(usess
, app
);
4330 if (ua_sess
== NULL
) {
4331 /* The session is in teardown process. Ignore and continue. */
4335 pthread_mutex_lock(&ua_sess
->lock
);
4337 if (ua_sess
->deleted
) {
4338 pthread_mutex_unlock(&ua_sess
->lock
);
4342 if (ua_sess
->enabled
) {
4343 pthread_mutex_unlock(&ua_sess
->lock
);
4347 /* Upon restart, we skip the setup, already done */
4348 if (ua_sess
->started
) {
4352 health_code_update();
4355 /* This starts the UST tracing */
4356 pthread_mutex_lock(&app
->sock_lock
);
4357 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4358 pthread_mutex_unlock(&app
->sock_lock
);
4360 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4361 ERR("Error starting tracing for app pid: %d (ret: %d)",
4364 DBG("UST app start session failed. Application is dead.");
4366 * This is normal behavior, an application can die during the
4367 * creation process. Don't report an error so the execution can
4368 * continue normally.
4370 pthread_mutex_unlock(&ua_sess
->lock
);
4376 /* Indicate that the session has been started once */
4377 ua_sess
->started
= 1;
4378 ua_sess
->enabled
= 1;
4380 pthread_mutex_unlock(&ua_sess
->lock
);
4382 health_code_update();
4384 /* Quiescent wait after starting trace */
4385 pthread_mutex_lock(&app
->sock_lock
);
4386 ret
= ustctl_wait_quiescent(app
->sock
);
4387 pthread_mutex_unlock(&app
->sock_lock
);
4388 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4389 ERR("UST app wait quiescent failed for app pid %d ret %d",
4395 health_code_update();
4399 pthread_mutex_unlock(&ua_sess
->lock
);
4401 health_code_update();
4406 * Stop tracing for a specific UST session and app.
4409 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4412 struct ust_app_session
*ua_sess
;
4413 struct ust_registry_session
*registry
;
4415 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4419 if (!app
->compatible
) {
4420 goto end_no_session
;
4423 ua_sess
= lookup_session_by_app(usess
, app
);
4424 if (ua_sess
== NULL
) {
4425 goto end_no_session
;
4428 pthread_mutex_lock(&ua_sess
->lock
);
4430 if (ua_sess
->deleted
) {
4431 pthread_mutex_unlock(&ua_sess
->lock
);
4432 goto end_no_session
;
4436 * If started = 0, it means that stop trace has been called for a session
4437 * that was never started. It's possible since we can have a fail start
4438 * from either the application manager thread or the command thread. Simply
4439 * indicate that this is a stop error.
4441 if (!ua_sess
->started
) {
4442 goto error_rcu_unlock
;
4445 health_code_update();
4447 /* This inhibits UST tracing */
4448 pthread_mutex_lock(&app
->sock_lock
);
4449 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4450 pthread_mutex_unlock(&app
->sock_lock
);
4452 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4453 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4456 DBG("UST app stop session failed. Application is dead.");
4458 * This is normal behavior, an application can die during the
4459 * creation process. Don't report an error so the execution can
4460 * continue normally.
4464 goto error_rcu_unlock
;
4467 health_code_update();
4468 ua_sess
->enabled
= 0;
4470 /* Quiescent wait after stopping trace */
4471 pthread_mutex_lock(&app
->sock_lock
);
4472 ret
= ustctl_wait_quiescent(app
->sock
);
4473 pthread_mutex_unlock(&app
->sock_lock
);
4474 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4475 ERR("UST app wait quiescent failed for app pid %d ret %d",
4479 health_code_update();
4481 registry
= get_session_registry(ua_sess
);
4483 /* The UST app session is held registry shall not be null. */
4486 /* Push metadata for application before freeing the application. */
4487 (void) push_metadata(registry
, ua_sess
->consumer
);
4490 pthread_mutex_unlock(&ua_sess
->lock
);
4493 health_code_update();
4497 pthread_mutex_unlock(&ua_sess
->lock
);
4499 health_code_update();
4504 int ust_app_flush_app_session(struct ust_app
*app
,
4505 struct ust_app_session
*ua_sess
)
4507 int ret
, retval
= 0;
4508 struct lttng_ht_iter iter
;
4509 struct ust_app_channel
*ua_chan
;
4510 struct consumer_socket
*socket
;
4512 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4516 if (!app
->compatible
) {
4517 goto end_not_compatible
;
4520 pthread_mutex_lock(&ua_sess
->lock
);
4522 if (ua_sess
->deleted
) {
4526 health_code_update();
4528 /* Flushing buffers */
4529 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4532 /* Flush buffers and push metadata. */
4533 switch (ua_sess
->buffer_type
) {
4534 case LTTNG_BUFFER_PER_PID
:
4535 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4537 health_code_update();
4538 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4540 ERR("Error flushing consumer channel");
4546 case LTTNG_BUFFER_PER_UID
:
4552 health_code_update();
4555 pthread_mutex_unlock(&ua_sess
->lock
);
4559 health_code_update();
4564 * Flush buffers for all applications for a specific UST session.
4565 * Called with UST session lock held.
4568 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4573 DBG("Flushing session buffers for all ust apps");
4577 /* Flush buffers and push metadata. */
4578 switch (usess
->buffer_type
) {
4579 case LTTNG_BUFFER_PER_UID
:
4581 struct buffer_reg_uid
*reg
;
4582 struct lttng_ht_iter iter
;
4584 /* Flush all per UID buffers associated to that session. */
4585 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4586 struct ust_registry_session
*ust_session_reg
;
4587 struct buffer_reg_channel
*reg_chan
;
4588 struct consumer_socket
*socket
;
4590 /* Get consumer socket to use to push the metadata.*/
4591 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4594 /* Ignore request if no consumer is found for the session. */
4598 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4599 reg_chan
, node
.node
) {
4601 * The following call will print error values so the return
4602 * code is of little importance because whatever happens, we
4603 * have to try them all.
4605 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4608 ust_session_reg
= reg
->registry
->reg
.ust
;
4609 /* Push metadata. */
4610 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4614 case LTTNG_BUFFER_PER_PID
:
4616 struct ust_app_session
*ua_sess
;
4617 struct lttng_ht_iter iter
;
4618 struct ust_app
*app
;
4620 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4621 ua_sess
= lookup_session_by_app(usess
, app
);
4622 if (ua_sess
== NULL
) {
4625 (void) ust_app_flush_app_session(app
, ua_sess
);
4636 health_code_update();
4641 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
4642 struct ust_app_session
*ua_sess
)
4645 struct lttng_ht_iter iter
;
4646 struct ust_app_channel
*ua_chan
;
4647 struct consumer_socket
*socket
;
4649 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
4653 if (!app
->compatible
) {
4654 goto end_not_compatible
;
4657 pthread_mutex_lock(&ua_sess
->lock
);
4659 if (ua_sess
->deleted
) {
4663 health_code_update();
4665 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4668 ERR("Failed to find consumer (%" PRIu32
") socket",
4669 app
->bits_per_long
);
4674 /* Clear quiescent state. */
4675 switch (ua_sess
->buffer_type
) {
4676 case LTTNG_BUFFER_PER_PID
:
4677 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
4678 ua_chan
, node
.node
) {
4679 health_code_update();
4680 ret
= consumer_clear_quiescent_channel(socket
,
4683 ERR("Error clearing quiescent state for consumer channel");
4689 case LTTNG_BUFFER_PER_UID
:
4696 health_code_update();
4699 pthread_mutex_unlock(&ua_sess
->lock
);
4703 health_code_update();
4708 * Clear quiescent state in each stream for all applications for a
4709 * specific UST session.
4710 * Called with UST session lock held.
4713 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
4718 DBG("Clearing stream quiescent state for all ust apps");
4722 switch (usess
->buffer_type
) {
4723 case LTTNG_BUFFER_PER_UID
:
4725 struct lttng_ht_iter iter
;
4726 struct buffer_reg_uid
*reg
;
4729 * Clear quiescent for all per UID buffers associated to
4732 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4733 struct consumer_socket
*socket
;
4734 struct buffer_reg_channel
*reg_chan
;
4736 /* Get associated consumer socket.*/
4737 socket
= consumer_find_socket_by_bitness(
4738 reg
->bits_per_long
, usess
->consumer
);
4741 * Ignore request if no consumer is found for
4747 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
4748 &iter
.iter
, reg_chan
, node
.node
) {
4750 * The following call will print error values so
4751 * the return code is of little importance
4752 * because whatever happens, we have to try them
4755 (void) consumer_clear_quiescent_channel(socket
,
4756 reg_chan
->consumer_key
);
4761 case LTTNG_BUFFER_PER_PID
:
4763 struct ust_app_session
*ua_sess
;
4764 struct lttng_ht_iter iter
;
4765 struct ust_app
*app
;
4767 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
4769 ua_sess
= lookup_session_by_app(usess
, app
);
4770 if (ua_sess
== NULL
) {
4773 (void) ust_app_clear_quiescent_app_session(app
,
4785 health_code_update();
4790 * Destroy a specific UST session in apps.
4792 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4795 struct ust_app_session
*ua_sess
;
4796 struct lttng_ht_iter iter
;
4797 struct lttng_ht_node_u64
*node
;
4799 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4803 if (!app
->compatible
) {
4807 __lookup_session_by_app(usess
, app
, &iter
);
4808 node
= lttng_ht_iter_get_node_u64(&iter
);
4810 /* Session is being or is deleted. */
4813 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4815 health_code_update();
4816 destroy_app_session(app
, ua_sess
);
4818 health_code_update();
4820 /* Quiescent wait after stopping trace */
4821 pthread_mutex_lock(&app
->sock_lock
);
4822 ret
= ustctl_wait_quiescent(app
->sock
);
4823 pthread_mutex_unlock(&app
->sock_lock
);
4824 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4825 ERR("UST app wait quiescent failed for app pid %d ret %d",
4830 health_code_update();
4835 * Start tracing for the UST session.
4837 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4839 struct lttng_ht_iter iter
;
4840 struct ust_app
*app
;
4842 DBG("Starting all UST traces");
4845 * Even though the start trace might fail, flag this session active so
4846 * other application coming in are started by default.
4853 * In a start-stop-start use-case, we need to clear the quiescent state
4854 * of each channel set by the prior stop command, thus ensuring that a
4855 * following stop or destroy is sure to grab a timestamp_end near those
4856 * operations, even if the packet is empty.
4858 (void) ust_app_clear_quiescent_session(usess
);
4860 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4861 ust_app_global_update(usess
, app
);
4870 * Start tracing for the UST session.
4871 * Called with UST session lock held.
4873 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4876 struct lttng_ht_iter iter
;
4877 struct ust_app
*app
;
4879 DBG("Stopping all UST traces");
4882 * Even though the stop trace might fail, flag this session inactive so
4883 * other application coming in are not started by default.
4889 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4890 ret
= ust_app_stop_trace(usess
, app
);
4892 /* Continue to next apps even on error */
4897 (void) ust_app_flush_session(usess
);
4905 * Destroy app UST session.
4907 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4910 struct lttng_ht_iter iter
;
4911 struct ust_app
*app
;
4913 DBG("Destroy all UST traces");
4917 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4918 ret
= destroy_trace(usess
, app
);
4920 /* Continue to next apps even on error */
4930 /* The ua_sess lock must be held by the caller. */
4932 int find_or_create_ust_app_channel(
4933 struct ltt_ust_session
*usess
,
4934 struct ust_app_session
*ua_sess
,
4935 struct ust_app
*app
,
4936 struct ltt_ust_channel
*uchan
,
4937 struct ust_app_channel
**ua_chan
)
4940 struct lttng_ht_iter iter
;
4941 struct lttng_ht_node_str
*ua_chan_node
;
4943 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
4944 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4946 *ua_chan
= caa_container_of(ua_chan_node
,
4947 struct ust_app_channel
, node
);
4951 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
4960 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
4961 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
4962 struct ust_app
*app
)
4965 struct ust_app_event
*ua_event
= NULL
;
4967 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4968 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4970 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4975 if (ua_event
->enabled
!= uevent
->enabled
) {
4976 ret
= uevent
->enabled
?
4977 enable_ust_app_event(ua_sess
, ua_event
, app
) :
4978 disable_ust_app_event(ua_sess
, ua_event
, app
);
4987 * The caller must ensure that the application is compatible and is tracked
4988 * by the process attribute trackers.
4991 void ust_app_synchronize(struct ltt_ust_session
*usess
,
4992 struct ust_app
*app
)
4995 struct cds_lfht_iter uchan_iter
;
4996 struct ltt_ust_channel
*uchan
;
4997 struct ust_app_session
*ua_sess
= NULL
;
5000 * The application's configuration should only be synchronized for
5003 assert(usess
->active
);
5005 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5007 /* Tracer is probably gone or ENOMEM. */
5012 pthread_mutex_lock(&ua_sess
->lock
);
5013 if (ua_sess
->deleted
) {
5014 pthread_mutex_unlock(&ua_sess
->lock
);
5020 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5022 struct ust_app_channel
*ua_chan
;
5023 struct cds_lfht_iter uevent_iter
;
5024 struct ltt_ust_event
*uevent
;
5027 * Search for a matching ust_app_channel. If none is found,
5028 * create it. Creating the channel will cause the ua_chan
5029 * structure to be allocated, the channel buffers to be
5030 * allocated (if necessary) and sent to the application, and
5031 * all enabled contexts will be added to the channel.
5033 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5034 app
, uchan
, &ua_chan
);
5036 /* Tracer is probably gone or ENOMEM. */
5041 /* ua_chan will be NULL for the metadata channel */
5045 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5047 ret
= ust_app_channel_synchronize_event(ua_chan
,
5048 uevent
, ua_sess
, app
);
5054 if (ua_chan
->enabled
!= uchan
->enabled
) {
5055 ret
= uchan
->enabled
?
5056 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5057 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5065 * Create the metadata for the application. This returns gracefully if a
5066 * metadata was already set for the session.
5068 * The metadata channel must be created after the data channels as the
5069 * consumer daemon assumes this ordering. When interacting with a relay
5070 * daemon, the consumer will use this assumption to send the
5071 * "STREAMS_SENT" message to the relay daemon.
5073 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5081 pthread_mutex_unlock(&ua_sess
->lock
);
5082 /* Everything went well at this point. */
5087 pthread_mutex_unlock(&ua_sess
->lock
);
5090 destroy_app_session(app
, ua_sess
);
5096 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5098 struct ust_app_session
*ua_sess
;
5100 ua_sess
= lookup_session_by_app(usess
, app
);
5101 if (ua_sess
== NULL
) {
5104 destroy_app_session(app
, ua_sess
);
5108 * Add channels/events from UST global domain to registered apps at sock.
5110 * Called with session lock held.
5111 * Called with RCU read-side lock held.
5113 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5116 assert(usess
->active
);
5118 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5119 app
->sock
, usess
->id
);
5121 if (!app
->compatible
) {
5124 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5126 trace_ust_id_tracker_lookup(
5127 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5129 trace_ust_id_tracker_lookup(
5130 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5133 * Synchronize the application's internal tracing configuration
5134 * and start tracing.
5136 ust_app_synchronize(usess
, app
);
5137 ust_app_start_trace(usess
, app
);
5139 ust_app_global_destroy(usess
, app
);
5144 * Called with session lock held.
5146 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5148 struct lttng_ht_iter iter
;
5149 struct ust_app
*app
;
5152 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5153 ust_app_global_update(usess
, app
);
5159 * Add context to a specific channel for global UST domain.
5161 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5162 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5165 struct lttng_ht_node_str
*ua_chan_node
;
5166 struct lttng_ht_iter iter
, uiter
;
5167 struct ust_app_channel
*ua_chan
= NULL
;
5168 struct ust_app_session
*ua_sess
;
5169 struct ust_app
*app
;
5171 assert(usess
->active
);
5174 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5175 if (!app
->compatible
) {
5177 * TODO: In time, we should notice the caller of this error by
5178 * telling him that this is a version error.
5182 ua_sess
= lookup_session_by_app(usess
, app
);
5183 if (ua_sess
== NULL
) {
5187 pthread_mutex_lock(&ua_sess
->lock
);
5189 if (ua_sess
->deleted
) {
5190 pthread_mutex_unlock(&ua_sess
->lock
);
5194 /* Lookup channel in the ust app session */
5195 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5196 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5197 if (ua_chan_node
== NULL
) {
5200 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5202 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5207 pthread_mutex_unlock(&ua_sess
->lock
);
5215 * Receive registration and populate the given msg structure.
5217 * On success return 0 else a negative value returned by the ustctl call.
5219 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5222 uint32_t pid
, ppid
, uid
, gid
;
5226 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5227 &pid
, &ppid
, &uid
, &gid
,
5228 &msg
->bits_per_long
,
5229 &msg
->uint8_t_alignment
,
5230 &msg
->uint16_t_alignment
,
5231 &msg
->uint32_t_alignment
,
5232 &msg
->uint64_t_alignment
,
5233 &msg
->long_alignment
,
5240 case LTTNG_UST_ERR_EXITING
:
5241 DBG3("UST app recv reg message failed. Application died");
5243 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5244 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5245 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5246 LTTNG_UST_ABI_MINOR_VERSION
);
5249 ERR("UST app recv reg message failed with ret %d", ret
);
5254 msg
->pid
= (pid_t
) pid
;
5255 msg
->ppid
= (pid_t
) ppid
;
5256 msg
->uid
= (uid_t
) uid
;
5257 msg
->gid
= (gid_t
) gid
;
5264 * Return a ust app session object using the application object and the
5265 * session object descriptor has a key. If not found, NULL is returned.
5266 * A RCU read side lock MUST be acquired when calling this function.
5268 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5271 struct lttng_ht_node_ulong
*node
;
5272 struct lttng_ht_iter iter
;
5273 struct ust_app_session
*ua_sess
= NULL
;
5277 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5278 node
= lttng_ht_iter_get_node_ulong(&iter
);
5280 DBG2("UST app session find by objd %d not found", objd
);
5284 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5291 * Return a ust app channel object using the application object and the channel
5292 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5293 * lock MUST be acquired before calling this function.
5295 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5298 struct lttng_ht_node_ulong
*node
;
5299 struct lttng_ht_iter iter
;
5300 struct ust_app_channel
*ua_chan
= NULL
;
5304 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5305 node
= lttng_ht_iter_get_node_ulong(&iter
);
5307 DBG2("UST app channel find by objd %d not found", objd
);
5311 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5318 * Reply to a register channel notification from an application on the notify
5319 * socket. The channel metadata is also created.
5321 * The session UST registry lock is acquired in this function.
5323 * On success 0 is returned else a negative value.
5325 static int reply_ust_register_channel(int sock
, int cobjd
,
5326 size_t nr_fields
, struct ustctl_field
*fields
)
5328 int ret
, ret_code
= 0;
5330 uint64_t chan_reg_key
;
5331 enum ustctl_channel_header type
;
5332 struct ust_app
*app
;
5333 struct ust_app_channel
*ua_chan
;
5334 struct ust_app_session
*ua_sess
;
5335 struct ust_registry_session
*registry
;
5336 struct ust_registry_channel
*chan_reg
;
5340 /* Lookup application. If not found, there is a code flow error. */
5341 app
= find_app_by_notify_sock(sock
);
5343 DBG("Application socket %d is being torn down. Abort event notify",
5346 goto error_rcu_unlock
;
5349 /* Lookup channel by UST object descriptor. */
5350 ua_chan
= find_channel_by_objd(app
, cobjd
);
5352 DBG("Application channel is being torn down. Abort event notify");
5354 goto error_rcu_unlock
;
5357 assert(ua_chan
->session
);
5358 ua_sess
= ua_chan
->session
;
5360 /* Get right session registry depending on the session buffer type. */
5361 registry
= get_session_registry(ua_sess
);
5363 DBG("Application session is being torn down. Abort event notify");
5365 goto error_rcu_unlock
;
5368 /* Depending on the buffer type, a different channel key is used. */
5369 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5370 chan_reg_key
= ua_chan
->tracing_channel_id
;
5372 chan_reg_key
= ua_chan
->key
;
5375 pthread_mutex_lock(®istry
->lock
);
5377 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5380 if (!chan_reg
->register_done
) {
5382 * TODO: eventually use the registry event count for
5383 * this channel to better guess header type for per-pid
5386 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5387 chan_reg
->nr_ctx_fields
= nr_fields
;
5388 chan_reg
->ctx_fields
= fields
;
5390 chan_reg
->header_type
= type
;
5392 /* Get current already assigned values. */
5393 type
= chan_reg
->header_type
;
5395 /* Channel id is set during the object creation. */
5396 chan_id
= chan_reg
->chan_id
;
5398 /* Append to metadata */
5399 if (!chan_reg
->metadata_dumped
) {
5400 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5402 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5408 DBG3("UST app replying to register channel key %" PRIu64
5409 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5412 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5414 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5415 ERR("UST app reply channel failed with ret %d", ret
);
5417 DBG3("UST app reply channel failed. Application died");
5422 /* This channel registry registration is completed. */
5423 chan_reg
->register_done
= 1;
5426 pthread_mutex_unlock(®istry
->lock
);
5434 * Add event to the UST channel registry. When the event is added to the
5435 * registry, the metadata is also created. Once done, this replies to the
5436 * application with the appropriate error code.
5438 * The session UST registry lock is acquired in the function.
5440 * On success 0 is returned else a negative value.
5442 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5443 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5444 int loglevel_value
, char *model_emf_uri
)
5447 uint32_t event_id
= 0;
5448 uint64_t chan_reg_key
;
5449 struct ust_app
*app
;
5450 struct ust_app_channel
*ua_chan
;
5451 struct ust_app_session
*ua_sess
;
5452 struct ust_registry_session
*registry
;
5456 /* Lookup application. If not found, there is a code flow error. */
5457 app
= find_app_by_notify_sock(sock
);
5459 DBG("Application socket %d is being torn down. Abort event notify",
5462 goto error_rcu_unlock
;
5465 /* Lookup channel by UST object descriptor. */
5466 ua_chan
= find_channel_by_objd(app
, cobjd
);
5468 DBG("Application channel is being torn down. Abort event notify");
5470 goto error_rcu_unlock
;
5473 assert(ua_chan
->session
);
5474 ua_sess
= ua_chan
->session
;
5476 registry
= get_session_registry(ua_sess
);
5478 DBG("Application session is being torn down. Abort event notify");
5480 goto error_rcu_unlock
;
5483 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5484 chan_reg_key
= ua_chan
->tracing_channel_id
;
5486 chan_reg_key
= ua_chan
->key
;
5489 pthread_mutex_lock(®istry
->lock
);
5492 * From this point on, this call acquires the ownership of the sig, fields
5493 * and model_emf_uri meaning any free are done inside it if needed. These
5494 * three variables MUST NOT be read/write after this.
5496 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5497 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5498 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5502 model_emf_uri
= NULL
;
5505 * The return value is returned to ustctl so in case of an error, the
5506 * application can be notified. In case of an error, it's important not to
5507 * return a negative error or else the application will get closed.
5509 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5511 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5512 ERR("UST app reply event failed with ret %d", ret
);
5514 DBG3("UST app reply event failed. Application died");
5517 * No need to wipe the create event since the application socket will
5518 * get close on error hence cleaning up everything by itself.
5523 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5527 pthread_mutex_unlock(®istry
->lock
);
5532 free(model_emf_uri
);
5537 * Add enum to the UST session registry. Once done, this replies to the
5538 * application with the appropriate error code.
5540 * The session UST registry lock is acquired within this function.
5542 * On success 0 is returned else a negative value.
5544 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
5545 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
5547 int ret
= 0, ret_code
;
5548 struct ust_app
*app
;
5549 struct ust_app_session
*ua_sess
;
5550 struct ust_registry_session
*registry
;
5551 uint64_t enum_id
= -1ULL;
5555 /* Lookup application. If not found, there is a code flow error. */
5556 app
= find_app_by_notify_sock(sock
);
5558 /* Return an error since this is not an error */
5559 DBG("Application socket %d is being torn down. Aborting enum registration",
5562 goto error_rcu_unlock
;
5565 /* Lookup session by UST object descriptor. */
5566 ua_sess
= find_session_by_objd(app
, sobjd
);
5568 /* Return an error since this is not an error */
5569 DBG("Application session is being torn down (session not found). Aborting enum registration.");
5571 goto error_rcu_unlock
;
5574 registry
= get_session_registry(ua_sess
);
5576 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
5578 goto error_rcu_unlock
;
5581 pthread_mutex_lock(®istry
->lock
);
5584 * From this point on, the callee acquires the ownership of
5585 * entries. The variable entries MUST NOT be read/written after
5588 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
5589 entries
, nr_entries
, &enum_id
);
5593 * The return value is returned to ustctl so in case of an error, the
5594 * application can be notified. In case of an error, it's important not to
5595 * return a negative error or else the application will get closed.
5597 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
5599 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5600 ERR("UST app reply enum failed with ret %d", ret
);
5602 DBG3("UST app reply enum failed. Application died");
5605 * No need to wipe the create enum since the application socket will
5606 * get close on error hence cleaning up everything by itself.
5611 DBG3("UST registry enum %s added successfully or already found", name
);
5614 pthread_mutex_unlock(®istry
->lock
);
5621 * Handle application notification through the given notify socket.
5623 * Return 0 on success or else a negative value.
5625 int ust_app_recv_notify(int sock
)
5628 enum ustctl_notify_cmd cmd
;
5630 DBG3("UST app receiving notify from sock %d", sock
);
5632 ret
= ustctl_recv_notify(sock
, &cmd
);
5634 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5635 ERR("UST app recv notify failed with ret %d", ret
);
5637 DBG3("UST app recv notify failed. Application died");
5643 case USTCTL_NOTIFY_CMD_EVENT
:
5645 int sobjd
, cobjd
, loglevel_value
;
5646 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5648 struct ustctl_field
*fields
;
5650 DBG2("UST app ustctl register event received");
5652 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5653 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5656 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5657 ERR("UST app recv event failed with ret %d", ret
);
5659 DBG3("UST app recv event failed. Application died");
5665 * Add event to the UST registry coming from the notify socket. This
5666 * call will free if needed the sig, fields and model_emf_uri. This
5667 * code path loses the ownsership of these variables and transfer them
5668 * to the this function.
5670 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5671 fields
, loglevel_value
, model_emf_uri
);
5678 case USTCTL_NOTIFY_CMD_CHANNEL
:
5682 struct ustctl_field
*fields
;
5684 DBG2("UST app ustctl register channel received");
5686 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5689 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5690 ERR("UST app recv channel failed with ret %d", ret
);
5692 DBG3("UST app recv channel failed. Application died");
5698 * The fields ownership are transfered to this function call meaning
5699 * that if needed it will be freed. After this, it's invalid to access
5700 * fields or clean it up.
5702 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
5710 case USTCTL_NOTIFY_CMD_ENUM
:
5713 char name
[LTTNG_UST_SYM_NAME_LEN
];
5715 struct ustctl_enum_entry
*entries
;
5717 DBG2("UST app ustctl register enum received");
5719 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
5720 &entries
, &nr_entries
);
5722 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5723 ERR("UST app recv enum failed with ret %d", ret
);
5725 DBG3("UST app recv enum failed. Application died");
5730 /* Callee assumes ownership of entries */
5731 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
5732 entries
, nr_entries
);
5740 /* Should NEVER happen. */
5749 * Once the notify socket hangs up, this is called. First, it tries to find the
5750 * corresponding application. On failure, the call_rcu to close the socket is
5751 * executed. If an application is found, it tries to delete it from the notify
5752 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5754 * Note that an object needs to be allocated here so on ENOMEM failure, the
5755 * call RCU is not done but the rest of the cleanup is.
5757 void ust_app_notify_sock_unregister(int sock
)
5760 struct lttng_ht_iter iter
;
5761 struct ust_app
*app
;
5762 struct ust_app_notify_sock_obj
*obj
;
5768 obj
= zmalloc(sizeof(*obj
));
5771 * An ENOMEM is kind of uncool. If this strikes we continue the
5772 * procedure but the call_rcu will not be called. In this case, we
5773 * accept the fd leak rather than possibly creating an unsynchronized
5774 * state between threads.
5776 * TODO: The notify object should be created once the notify socket is
5777 * registered and stored independantely from the ust app object. The
5778 * tricky part is to synchronize the teardown of the application and
5779 * this notify object. Let's keep that in mind so we can avoid this
5780 * kind of shenanigans with ENOMEM in the teardown path.
5787 DBG("UST app notify socket unregister %d", sock
);
5790 * Lookup application by notify socket. If this fails, this means that the
5791 * hash table delete has already been done by the application
5792 * unregistration process so we can safely close the notify socket in a
5795 app
= find_app_by_notify_sock(sock
);
5800 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5803 * Whatever happens here either we fail or succeed, in both cases we have
5804 * to close the socket after a grace period to continue to the call RCU
5805 * here. If the deletion is successful, the application is not visible
5806 * anymore by other threads and is it fails it means that it was already
5807 * deleted from the hash table so either way we just have to close the
5810 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5816 * Close socket after a grace period to avoid for the socket to be reused
5817 * before the application object is freed creating potential race between
5818 * threads trying to add unique in the global hash table.
5821 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5826 * Destroy a ust app data structure and free its memory.
5828 void ust_app_destroy(struct ust_app
*app
)
5834 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5838 * Take a snapshot for a given UST session. The snapshot is sent to the given
5841 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5843 enum lttng_error_code
ust_app_snapshot_record(
5844 const struct ltt_ust_session
*usess
,
5845 const struct consumer_output
*output
, int wait
,
5846 uint64_t nb_packets_per_stream
)
5849 enum lttng_error_code status
= LTTNG_OK
;
5850 struct lttng_ht_iter iter
;
5851 struct ust_app
*app
;
5852 char *trace_path
= NULL
;
5859 switch (usess
->buffer_type
) {
5860 case LTTNG_BUFFER_PER_UID
:
5862 struct buffer_reg_uid
*reg
;
5864 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5865 struct buffer_reg_channel
*reg_chan
;
5866 struct consumer_socket
*socket
;
5867 char pathname
[PATH_MAX
];
5868 size_t consumer_path_offset
= 0;
5870 if (!reg
->registry
->reg
.ust
->metadata_key
) {
5871 /* Skip since no metadata is present */
5875 /* Get consumer socket to use to push the metadata.*/
5876 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5879 status
= LTTNG_ERR_INVALID
;
5883 memset(pathname
, 0, sizeof(pathname
));
5884 ret
= snprintf(pathname
, sizeof(pathname
),
5885 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5886 reg
->uid
, reg
->bits_per_long
);
5888 PERROR("snprintf snapshot path");
5889 status
= LTTNG_ERR_INVALID
;
5892 /* Free path allowed on previous iteration. */
5894 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
5895 &consumer_path_offset
);
5897 status
= LTTNG_ERR_INVALID
;
5900 /* Add the UST default trace dir to path. */
5901 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5902 reg_chan
, node
.node
) {
5903 status
= consumer_snapshot_channel(socket
,
5904 reg_chan
->consumer_key
,
5905 output
, 0, usess
->uid
,
5906 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
5907 nb_packets_per_stream
);
5908 if (status
!= LTTNG_OK
) {
5912 status
= consumer_snapshot_channel(socket
,
5913 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5914 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
5916 if (status
!= LTTNG_OK
) {
5922 case LTTNG_BUFFER_PER_PID
:
5924 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5925 struct consumer_socket
*socket
;
5926 struct lttng_ht_iter chan_iter
;
5927 struct ust_app_channel
*ua_chan
;
5928 struct ust_app_session
*ua_sess
;
5929 struct ust_registry_session
*registry
;
5930 char pathname
[PATH_MAX
];
5931 size_t consumer_path_offset
= 0;
5933 ua_sess
= lookup_session_by_app(usess
, app
);
5935 /* Session not associated with this app. */
5939 /* Get the right consumer socket for the application. */
5940 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5943 status
= LTTNG_ERR_INVALID
;
5947 /* Add the UST default trace dir to path. */
5948 memset(pathname
, 0, sizeof(pathname
));
5949 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5952 status
= LTTNG_ERR_INVALID
;
5953 PERROR("snprintf snapshot path");
5956 /* Free path allowed on previous iteration. */
5958 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
5959 &consumer_path_offset
);
5961 status
= LTTNG_ERR_INVALID
;
5964 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5965 ua_chan
, node
.node
) {
5966 status
= consumer_snapshot_channel(socket
,
5967 ua_chan
->key
, output
, 0,
5968 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
5969 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
5970 &trace_path
[consumer_path_offset
], wait
,
5971 nb_packets_per_stream
);
5975 case LTTNG_ERR_CHAN_NOT_FOUND
:
5982 registry
= get_session_registry(ua_sess
);
5984 DBG("Application session is being torn down. Skip application.");
5987 status
= consumer_snapshot_channel(socket
,
5988 registry
->metadata_key
, output
, 1,
5989 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
5990 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
5991 &trace_path
[consumer_path_offset
], wait
, 0);
5995 case LTTNG_ERR_CHAN_NOT_FOUND
:
6015 * Return the size taken by one more packet per stream.
6017 uint64_t ust_app_get_size_one_more_packet_per_stream(
6018 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6020 uint64_t tot_size
= 0;
6021 struct ust_app
*app
;
6022 struct lttng_ht_iter iter
;
6026 switch (usess
->buffer_type
) {
6027 case LTTNG_BUFFER_PER_UID
:
6029 struct buffer_reg_uid
*reg
;
6031 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6032 struct buffer_reg_channel
*reg_chan
;
6035 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6036 reg_chan
, node
.node
) {
6037 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6039 * Don't take channel into account if we
6040 * already grab all its packets.
6044 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6050 case LTTNG_BUFFER_PER_PID
:
6053 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6054 struct ust_app_channel
*ua_chan
;
6055 struct ust_app_session
*ua_sess
;
6056 struct lttng_ht_iter chan_iter
;
6058 ua_sess
= lookup_session_by_app(usess
, app
);
6060 /* Session not associated with this app. */
6064 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6065 ua_chan
, node
.node
) {
6066 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6068 * Don't take channel into account if we
6069 * already grab all its packets.
6073 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6087 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6088 struct cds_list_head
*buffer_reg_uid_list
,
6089 struct consumer_output
*consumer
, uint64_t uchan_id
,
6090 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6093 uint64_t consumer_chan_key
;
6098 ret
= buffer_reg_uid_consumer_channel_key(
6099 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6107 ret
= consumer_get_lost_packets(ust_session_id
,
6108 consumer_chan_key
, consumer
, lost
);
6110 ret
= consumer_get_discarded_events(ust_session_id
,
6111 consumer_chan_key
, consumer
, discarded
);
6118 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6119 struct ltt_ust_channel
*uchan
,
6120 struct consumer_output
*consumer
, int overwrite
,
6121 uint64_t *discarded
, uint64_t *lost
)
6124 struct lttng_ht_iter iter
;
6125 struct lttng_ht_node_str
*ua_chan_node
;
6126 struct ust_app
*app
;
6127 struct ust_app_session
*ua_sess
;
6128 struct ust_app_channel
*ua_chan
;
6135 * Iterate over every registered applications. Sum counters for
6136 * all applications containing requested session and channel.
6138 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6139 struct lttng_ht_iter uiter
;
6141 ua_sess
= lookup_session_by_app(usess
, app
);
6142 if (ua_sess
== NULL
) {
6147 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6148 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6149 /* If the session is found for the app, the channel must be there */
6150 assert(ua_chan_node
);
6152 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6157 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6164 uint64_t _discarded
;
6166 ret
= consumer_get_discarded_events(usess
->id
,
6167 ua_chan
->key
, consumer
, &_discarded
);
6171 (*discarded
) += _discarded
;
6180 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6181 struct ust_app
*app
)
6184 struct ust_app_session
*ua_sess
;
6186 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6190 ua_sess
= lookup_session_by_app(usess
, app
);
6191 if (ua_sess
== NULL
) {
6192 /* The session is in teardown process. Ignore and continue. */
6196 pthread_mutex_lock(&ua_sess
->lock
);
6198 if (ua_sess
->deleted
) {
6202 pthread_mutex_lock(&app
->sock_lock
);
6203 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6204 pthread_mutex_unlock(&app
->sock_lock
);
6207 pthread_mutex_unlock(&ua_sess
->lock
);
6211 health_code_update();
6216 * Regenerate the statedump for each app in the session.
6218 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6221 struct lttng_ht_iter iter
;
6222 struct ust_app
*app
;
6224 DBG("Regenerating the metadata for all UST apps");
6228 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6229 if (!app
->compatible
) {
6233 ret
= ust_app_regenerate_statedump(usess
, app
);
6235 /* Continue to the next app even on error */
6246 * Rotate all the channels of a session.
6248 * Return LTTNG_OK on success or else an LTTng error code.
6250 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
6253 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6254 struct lttng_ht_iter iter
;
6255 struct ust_app
*app
;
6256 struct ltt_ust_session
*usess
= session
->ust_session
;
6262 switch (usess
->buffer_type
) {
6263 case LTTNG_BUFFER_PER_UID
:
6265 struct buffer_reg_uid
*reg
;
6267 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6268 struct buffer_reg_channel
*reg_chan
;
6269 struct consumer_socket
*socket
;
6271 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6272 /* Skip since no metadata is present */
6276 /* Get consumer socket to use to push the metadata.*/
6277 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6280 cmd_ret
= LTTNG_ERR_INVALID
;
6284 /* Rotate the data channels. */
6285 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6286 reg_chan
, node
.node
) {
6287 ret
= consumer_rotate_channel(socket
,
6288 reg_chan
->consumer_key
,
6289 usess
->uid
, usess
->gid
,
6291 /* is_metadata_channel */ false);
6293 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6298 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6300 ret
= consumer_rotate_channel(socket
,
6301 reg
->registry
->reg
.ust
->metadata_key
,
6302 usess
->uid
, usess
->gid
,
6304 /* is_metadata_channel */ true);
6306 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6312 case LTTNG_BUFFER_PER_PID
:
6314 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6315 struct consumer_socket
*socket
;
6316 struct lttng_ht_iter chan_iter
;
6317 struct ust_app_channel
*ua_chan
;
6318 struct ust_app_session
*ua_sess
;
6319 struct ust_registry_session
*registry
;
6321 ua_sess
= lookup_session_by_app(usess
, app
);
6323 /* Session not associated with this app. */
6327 /* Get the right consumer socket for the application. */
6328 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6331 cmd_ret
= LTTNG_ERR_INVALID
;
6335 registry
= get_session_registry(ua_sess
);
6337 DBG("Application session is being torn down. Skip application.");
6341 /* Rotate the data channels. */
6342 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6343 ua_chan
, node
.node
) {
6344 ret
= consumer_rotate_channel(socket
,
6346 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6347 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6349 /* is_metadata_channel */ false);
6351 /* Per-PID buffer and application going away. */
6352 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6354 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6359 /* Rotate the metadata channel. */
6360 (void) push_metadata(registry
, usess
->consumer
);
6361 ret
= consumer_rotate_channel(socket
,
6362 registry
->metadata_key
,
6363 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6364 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6366 /* is_metadata_channel */ true);
6368 /* Per-PID buffer and application going away. */
6369 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6371 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6389 enum lttng_error_code
ust_app_create_channel_subdirectories(
6390 const struct ltt_ust_session
*usess
)
6392 enum lttng_error_code ret
= LTTNG_OK
;
6393 struct lttng_ht_iter iter
;
6394 enum lttng_trace_chunk_status chunk_status
;
6395 char *pathname_index
;
6398 assert(usess
->current_trace_chunk
);
6401 switch (usess
->buffer_type
) {
6402 case LTTNG_BUFFER_PER_UID
:
6404 struct buffer_reg_uid
*reg
;
6406 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6407 fmt_ret
= asprintf(&pathname_index
,
6408 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
6409 reg
->uid
, reg
->bits_per_long
);
6411 ERR("Failed to format channel index directory");
6412 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6417 * Create the index subdirectory which will take care
6418 * of implicitly creating the channel's path.
6420 chunk_status
= lttng_trace_chunk_create_subdirectory(
6421 usess
->current_trace_chunk
,
6423 free(pathname_index
);
6424 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6425 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6431 case LTTNG_BUFFER_PER_PID
:
6433 struct ust_app
*app
;
6436 * Create the toplevel ust/ directory in case no apps are running.
6438 chunk_status
= lttng_trace_chunk_create_subdirectory(
6439 usess
->current_trace_chunk
,
6440 DEFAULT_UST_TRACE_DIR
);
6441 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6442 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6446 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
6448 struct ust_app_session
*ua_sess
;
6449 struct ust_registry_session
*registry
;
6451 ua_sess
= lookup_session_by_app(usess
, app
);
6453 /* Session not associated with this app. */
6457 registry
= get_session_registry(ua_sess
);
6459 DBG("Application session is being torn down. Skip application.");
6463 fmt_ret
= asprintf(&pathname_index
,
6464 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
6467 ERR("Failed to format channel index directory");
6468 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6472 * Create the index subdirectory which will take care
6473 * of implicitly creating the channel's path.
6475 chunk_status
= lttng_trace_chunk_create_subdirectory(
6476 usess
->current_trace_chunk
,
6478 free(pathname_index
);
6479 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6480 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6497 * Clear all the channels of a session.
6499 * Return LTTNG_OK on success or else an LTTng error code.
6501 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
6504 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6505 struct lttng_ht_iter iter
;
6506 struct ust_app
*app
;
6507 struct ltt_ust_session
*usess
= session
->ust_session
;
6513 if (usess
->active
) {
6514 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
6515 cmd_ret
= LTTNG_ERR_FATAL
;
6519 switch (usess
->buffer_type
) {
6520 case LTTNG_BUFFER_PER_UID
:
6522 struct buffer_reg_uid
*reg
;
6524 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6525 struct buffer_reg_channel
*reg_chan
;
6526 struct consumer_socket
*socket
;
6528 /* Get consumer socket to use to push the metadata.*/
6529 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6532 cmd_ret
= LTTNG_ERR_INVALID
;
6536 /* Clear the data channels. */
6537 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6538 reg_chan
, node
.node
) {
6539 ret
= consumer_clear_channel(socket
,
6540 reg_chan
->consumer_key
);
6546 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6549 * Clear the metadata channel.
6550 * Metadata channel is not cleared per se but we still need to
6551 * perform a rotation operation on it behind the scene.
6553 ret
= consumer_clear_channel(socket
,
6554 reg
->registry
->reg
.ust
->metadata_key
);
6561 case LTTNG_BUFFER_PER_PID
:
6563 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6564 struct consumer_socket
*socket
;
6565 struct lttng_ht_iter chan_iter
;
6566 struct ust_app_channel
*ua_chan
;
6567 struct ust_app_session
*ua_sess
;
6568 struct ust_registry_session
*registry
;
6570 ua_sess
= lookup_session_by_app(usess
, app
);
6572 /* Session not associated with this app. */
6576 /* Get the right consumer socket for the application. */
6577 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6580 cmd_ret
= LTTNG_ERR_INVALID
;
6584 registry
= get_session_registry(ua_sess
);
6586 DBG("Application session is being torn down. Skip application.");
6590 /* Clear the data channels. */
6591 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6592 ua_chan
, node
.node
) {
6593 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
6595 /* Per-PID buffer and application going away. */
6596 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6603 (void) push_metadata(registry
, usess
->consumer
);
6606 * Clear the metadata channel.
6607 * Metadata channel is not cleared per se but we still need to
6608 * perform rotation operation on it behind the scene.
6610 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
6612 /* Per-PID buffer and application going away. */
6613 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6631 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
6632 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
6635 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
6645 * This function skips the metadata channel as the begin/end timestamps of a
6646 * metadata packet are useless.
6648 * Moreover, opening a packet after a "clear" will cause problems for live
6649 * sessions as it will introduce padding that was not part of the first trace
6650 * chunk. The relay daemon expects the content of the metadata stream of
6651 * successive metadata trace chunks to be strict supersets of one another.
6653 * For example, flushing a packet at the beginning of the metadata stream of
6654 * a trace chunk resulting from a "clear" session command will cause the
6655 * size of the metadata stream of the new trace chunk to not match the size of
6656 * the metadata stream of the original chunk. This will confuse the relay
6657 * daemon as the same "offset" in a metadata stream will no longer point
6658 * to the same content.
6660 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
6662 enum lttng_error_code ret
= LTTNG_OK
;
6663 struct lttng_ht_iter iter
;
6664 struct ltt_ust_session
*usess
= session
->ust_session
;
6670 switch (usess
->buffer_type
) {
6671 case LTTNG_BUFFER_PER_UID
:
6673 struct buffer_reg_uid
*reg
;
6675 cds_list_for_each_entry (
6676 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6677 struct buffer_reg_channel
*reg_chan
;
6678 struct consumer_socket
*socket
;
6680 socket
= consumer_find_socket_by_bitness(
6681 reg
->bits_per_long
, usess
->consumer
);
6683 ret
= LTTNG_ERR_FATAL
;
6687 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
6688 &iter
.iter
, reg_chan
, node
.node
) {
6689 const int open_ret
=
6690 consumer_open_channel_packets(
6692 reg_chan
->consumer_key
);
6695 ret
= LTTNG_ERR_UNK
;
6702 case LTTNG_BUFFER_PER_PID
:
6704 struct ust_app
*app
;
6706 cds_lfht_for_each_entry (
6707 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6708 struct consumer_socket
*socket
;
6709 struct lttng_ht_iter chan_iter
;
6710 struct ust_app_channel
*ua_chan
;
6711 struct ust_app_session
*ua_sess
;
6712 struct ust_registry_session
*registry
;
6714 ua_sess
= lookup_session_by_app(usess
, app
);
6716 /* Session not associated with this app. */
6720 /* Get the right consumer socket for the application. */
6721 socket
= consumer_find_socket_by_bitness(
6722 app
->bits_per_long
, usess
->consumer
);
6724 ret
= LTTNG_ERR_FATAL
;
6728 registry
= get_session_registry(ua_sess
);
6730 DBG("Application session is being torn down. Skip application.");
6734 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
6735 &chan_iter
.iter
, ua_chan
, node
.node
) {
6736 const int open_ret
=
6737 consumer_open_channel_packets(
6743 * Per-PID buffer and application going
6746 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6750 ret
= LTTNG_ERR_UNK
;