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 * This could be NULL if the event notifier setup failed (e.g the app
926 * was killed or the tracer does not support this feature).
928 if (app
->event_notifier_group
.object
) {
929 enum lttng_error_code ret_code
;
930 const int event_notifier_read_fd
= lttng_pipe_get_readfd(
931 app
->event_notifier_group
.event_pipe
);
933 ret_code
= notification_thread_command_remove_tracer_event_source(
934 notification_thread_handle
,
935 event_notifier_read_fd
);
936 if (ret_code
!= LTTNG_OK
) {
937 ERR("Failed to remove application tracer event source from notification thread");
940 ustctl_release_object(sock
, app
->event_notifier_group
.object
);
941 free(app
->event_notifier_group
.object
);
944 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
947 * Wait until we have deleted the application from the sock hash table
948 * before closing this socket, otherwise an application could re-use the
949 * socket ID and race with the teardown, using the same hash table entry.
951 * It's OK to leave the close in call_rcu. We want it to stay unique for
952 * all RCU readers that could run concurrently with unregister app,
953 * therefore we _need_ to only close that socket after a grace period. So
954 * it should stay in this RCU callback.
956 * This close() is a very important step of the synchronization model so
957 * every modification to this function must be carefully reviewed.
963 lttng_fd_put(LTTNG_FD_APPS
, 1);
965 DBG2("UST app pid %d deleted", app
->pid
);
967 session_unlock_list();
971 * URCU intermediate call to delete an UST app.
974 void delete_ust_app_rcu(struct rcu_head
*head
)
976 struct lttng_ht_node_ulong
*node
=
977 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
978 struct ust_app
*app
=
979 caa_container_of(node
, struct ust_app
, pid_n
);
981 DBG3("Call RCU deleting app PID %d", app
->pid
);
986 * Delete the session from the application ht and delete the data structure by
987 * freeing every object inside and releasing them.
989 * The session list lock must be held by the caller.
991 static void destroy_app_session(struct ust_app
*app
,
992 struct ust_app_session
*ua_sess
)
995 struct lttng_ht_iter iter
;
1000 iter
.iter
.node
= &ua_sess
->node
.node
;
1001 ret
= lttng_ht_del(app
->sessions
, &iter
);
1003 /* Already scheduled for teardown. */
1007 /* Once deleted, free the data structure. */
1008 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1015 * Alloc new UST app session.
1018 struct ust_app_session
*alloc_ust_app_session(void)
1020 struct ust_app_session
*ua_sess
;
1022 /* Init most of the default value by allocating and zeroing */
1023 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1024 if (ua_sess
== NULL
) {
1029 ua_sess
->handle
= -1;
1030 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1031 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1032 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1041 * Alloc new UST app channel.
1044 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1045 struct ust_app_session
*ua_sess
,
1046 struct lttng_ust_channel_attr
*attr
)
1048 struct ust_app_channel
*ua_chan
;
1050 /* Init most of the default value by allocating and zeroing */
1051 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1052 if (ua_chan
== NULL
) {
1057 /* Setup channel name */
1058 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1059 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1061 ua_chan
->enabled
= 1;
1062 ua_chan
->handle
= -1;
1063 ua_chan
->session
= ua_sess
;
1064 ua_chan
->key
= get_next_channel_key();
1065 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1066 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1067 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1069 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1070 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1072 /* Copy attributes */
1074 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1075 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1076 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1077 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1078 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1079 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1080 ua_chan
->attr
.output
= attr
->output
;
1081 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1083 /* By default, the channel is a per cpu channel. */
1084 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1086 DBG3("UST app channel %s allocated", ua_chan
->name
);
1095 * Allocate and initialize a UST app stream.
1097 * Return newly allocated stream pointer or NULL on error.
1099 struct ust_app_stream
*ust_app_alloc_stream(void)
1101 struct ust_app_stream
*stream
= NULL
;
1103 stream
= zmalloc(sizeof(*stream
));
1104 if (stream
== NULL
) {
1105 PERROR("zmalloc ust app stream");
1109 /* Zero could be a valid value for a handle so flag it to -1. */
1110 stream
->handle
= -1;
1117 * Alloc new UST app event.
1120 struct ust_app_event
*alloc_ust_app_event(char *name
,
1121 struct lttng_ust_event
*attr
)
1123 struct ust_app_event
*ua_event
;
1125 /* Init most of the default value by allocating and zeroing */
1126 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1127 if (ua_event
== NULL
) {
1128 PERROR("Failed to allocate ust_app_event structure");
1132 ua_event
->enabled
= 1;
1133 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1134 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1135 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1137 /* Copy attributes */
1139 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1142 DBG3("UST app event %s allocated", ua_event
->name
);
1151 * Alloc new UST app context.
1154 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1156 struct ust_app_ctx
*ua_ctx
;
1158 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1159 if (ua_ctx
== NULL
) {
1163 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1166 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1167 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1168 char *provider_name
= NULL
, *ctx_name
= NULL
;
1170 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1171 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1172 if (!provider_name
|| !ctx_name
) {
1173 free(provider_name
);
1178 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1179 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1183 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1191 * Allocate a filter and copy the given original filter.
1193 * Return allocated filter or NULL on error.
1195 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1196 struct lttng_filter_bytecode
*orig_f
)
1198 struct lttng_filter_bytecode
*filter
= NULL
;
1200 /* Copy filter bytecode */
1201 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1203 PERROR("zmalloc alloc filter bytecode");
1207 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1214 * Create a liblttng-ust filter bytecode from given bytecode.
1216 * Return allocated filter or NULL on error.
1218 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1219 const struct lttng_filter_bytecode
*orig_f
)
1221 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1223 /* Copy filter bytecode */
1224 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1226 PERROR("zmalloc alloc ust filter bytecode");
1230 assert(sizeof(struct lttng_filter_bytecode
) ==
1231 sizeof(struct lttng_ust_filter_bytecode
));
1232 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1238 * Find an ust_app using the sock and return it. RCU read side lock must be
1239 * held before calling this helper function.
1241 struct ust_app
*ust_app_find_by_sock(int sock
)
1243 struct lttng_ht_node_ulong
*node
;
1244 struct lttng_ht_iter iter
;
1246 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1247 node
= lttng_ht_iter_get_node_ulong(&iter
);
1249 DBG2("UST app find by sock %d not found", sock
);
1253 return caa_container_of(node
, struct ust_app
, sock_n
);
1260 * Find an ust_app using the notify sock and return it. RCU read side lock must
1261 * be held before calling this helper function.
1263 static struct ust_app
*find_app_by_notify_sock(int sock
)
1265 struct lttng_ht_node_ulong
*node
;
1266 struct lttng_ht_iter iter
;
1268 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1270 node
= lttng_ht_iter_get_node_ulong(&iter
);
1272 DBG2("UST app find by notify sock %d not found", sock
);
1276 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1283 * Lookup for an ust app event based on event name, filter bytecode and the
1286 * Return an ust_app_event object or NULL on error.
1288 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1289 const char *name
, const struct lttng_filter_bytecode
*filter
,
1291 const struct lttng_event_exclusion
*exclusion
)
1293 struct lttng_ht_iter iter
;
1294 struct lttng_ht_node_str
*node
;
1295 struct ust_app_event
*event
= NULL
;
1296 struct ust_app_ht_key key
;
1301 /* Setup key for event lookup. */
1303 key
.filter
= filter
;
1304 key
.loglevel_type
= loglevel_value
;
1305 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1306 key
.exclusion
= exclusion
;
1308 /* Lookup using the event name as hash and a custom match fct. */
1309 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1310 ht_match_ust_app_event
, &key
, &iter
.iter
);
1311 node
= lttng_ht_iter_get_node_str(&iter
);
1316 event
= caa_container_of(node
, struct ust_app_event
, node
);
1323 * Create the channel context on the tracer.
1325 * Called with UST app session lock held.
1328 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1329 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1333 health_code_update();
1335 pthread_mutex_lock(&app
->sock_lock
);
1336 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1337 ua_chan
->obj
, &ua_ctx
->obj
);
1338 pthread_mutex_unlock(&app
->sock_lock
);
1340 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1341 ERR("UST app create channel context failed for app (pid: %d) "
1342 "with ret %d", app
->pid
, ret
);
1345 * This is normal behavior, an application can die during the
1346 * creation process. Don't report an error so the execution can
1347 * continue normally.
1350 DBG3("UST app add context failed. Application is dead.");
1355 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1357 DBG2("UST app context handle %d created successfully for channel %s",
1358 ua_ctx
->handle
, ua_chan
->name
);
1361 health_code_update();
1366 * Set the filter on the tracer.
1368 static int set_ust_object_filter(struct ust_app
*app
,
1369 const struct lttng_filter_bytecode
*bytecode
,
1370 struct lttng_ust_object_data
*ust_object
)
1373 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1375 health_code_update();
1377 ust_bytecode
= create_ust_bytecode_from_bytecode(bytecode
);
1378 if (!ust_bytecode
) {
1379 ret
= -LTTNG_ERR_NOMEM
;
1382 pthread_mutex_lock(&app
->sock_lock
);
1383 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1385 pthread_mutex_unlock(&app
->sock_lock
);
1387 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1388 ERR("UST app set object filter failed for object %p of app (pid: %d) "
1389 "with ret %d", ust_object
, app
->pid
, ret
);
1392 * This is normal behavior, an application can die during the
1393 * creation process. Don't report an error so the execution can
1394 * continue normally.
1397 DBG3("Failed to set UST app object filter. Application is dead.");
1402 DBG2("UST filter successfully set for object %p", ust_object
);
1405 health_code_update();
1411 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1412 const struct lttng_event_exclusion
*exclusion
)
1414 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1415 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1416 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1418 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1419 if (!ust_exclusion
) {
1424 assert(sizeof(struct lttng_event_exclusion
) ==
1425 sizeof(struct lttng_ust_event_exclusion
));
1426 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1428 return ust_exclusion
;
1432 * Set event exclusions on the tracer.
1434 static int set_ust_object_exclusions(struct ust_app
*app
,
1435 const struct lttng_event_exclusion
*exclusions
,
1436 struct lttng_ust_object_data
*ust_object
)
1439 struct lttng_ust_event_exclusion
*ust_exclusions
= NULL
;
1441 assert(exclusions
&& exclusions
->count
> 0);
1443 health_code_update();
1445 ust_exclusions
= create_ust_exclusion_from_exclusion(
1447 if (!ust_exclusions
) {
1448 ret
= -LTTNG_ERR_NOMEM
;
1451 pthread_mutex_lock(&app
->sock_lock
);
1452 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1453 pthread_mutex_unlock(&app
->sock_lock
);
1455 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1456 ERR("Failed to set UST app exclusions for object %p of app (pid: %d) "
1457 "with ret %d", ust_object
, app
->pid
, ret
);
1460 * This is normal behavior, an application can die during the
1461 * creation process. Don't report an error so the execution can
1462 * continue normally.
1465 DBG3("Failed to set UST app object exclusions. Application is dead.");
1470 DBG2("UST exclusions set successfully for object %p", ust_object
);
1473 health_code_update();
1474 free(ust_exclusions
);
1479 * Disable the specified event on to UST tracer for the UST session.
1481 static int disable_ust_object(struct ust_app
*app
,
1482 struct lttng_ust_object_data
*object
)
1486 health_code_update();
1488 pthread_mutex_lock(&app
->sock_lock
);
1489 ret
= ustctl_disable(app
->sock
, object
);
1490 pthread_mutex_unlock(&app
->sock_lock
);
1492 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1493 ERR("Failed to disable UST app object %p app (pid: %d) with ret %d",
1494 object
, app
->pid
, ret
);
1497 * This is normal behavior, an application can die during the
1498 * creation process. Don't report an error so the execution can
1499 * continue normally.
1502 DBG3("Failed to disable UST app object. Application is dead.");
1507 DBG2("UST app object %p disabled successfully for app (pid: %d)",
1511 health_code_update();
1516 * Disable the specified channel on to UST tracer for the UST session.
1518 static int disable_ust_channel(struct ust_app
*app
,
1519 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1523 health_code_update();
1525 pthread_mutex_lock(&app
->sock_lock
);
1526 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1527 pthread_mutex_unlock(&app
->sock_lock
);
1529 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1530 ERR("UST app channel %s disable failed for app (pid: %d) "
1531 "and session handle %d with ret %d",
1532 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1535 * This is normal behavior, an application can die during the
1536 * creation process. Don't report an error so the execution can
1537 * continue normally.
1540 DBG3("UST app disable channel failed. Application is dead.");
1545 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1546 ua_chan
->name
, app
->pid
);
1549 health_code_update();
1554 * Enable the specified channel on to UST tracer for the UST session.
1556 static int enable_ust_channel(struct ust_app
*app
,
1557 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1561 health_code_update();
1563 pthread_mutex_lock(&app
->sock_lock
);
1564 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1565 pthread_mutex_unlock(&app
->sock_lock
);
1567 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1568 ERR("UST app channel %s enable failed for app (pid: %d) "
1569 "and session handle %d with ret %d",
1570 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1573 * This is normal behavior, an application can die during the
1574 * creation process. Don't report an error so the execution can
1575 * continue normally.
1578 DBG3("UST app enable channel failed. Application is dead.");
1583 ua_chan
->enabled
= 1;
1585 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1586 ua_chan
->name
, app
->pid
);
1589 health_code_update();
1594 * Enable the specified event on to UST tracer for the UST session.
1596 static int enable_ust_object(
1597 struct ust_app
*app
, struct lttng_ust_object_data
*ust_object
)
1601 health_code_update();
1603 pthread_mutex_lock(&app
->sock_lock
);
1604 ret
= ustctl_enable(app
->sock
, ust_object
);
1605 pthread_mutex_unlock(&app
->sock_lock
);
1607 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1608 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
1609 ust_object
, app
->pid
, ret
);
1612 * This is normal behavior, an application can die during the
1613 * creation process. Don't report an error so the execution can
1614 * continue normally.
1617 DBG3("Failed to enable UST app object. Application is dead.");
1622 DBG2("UST app object %p enabled successfully for app (pid: %d)",
1623 ust_object
, app
->pid
);
1626 health_code_update();
1631 * Send channel and stream buffer to application.
1633 * Return 0 on success. On error, a negative value is returned.
1635 static int send_channel_pid_to_ust(struct ust_app
*app
,
1636 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1639 struct ust_app_stream
*stream
, *stmp
;
1645 health_code_update();
1647 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1650 /* Send channel to the application. */
1651 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1652 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1653 ret
= -ENOTCONN
; /* Caused by app exiting. */
1655 } else if (ret
< 0) {
1659 health_code_update();
1661 /* Send all streams to application. */
1662 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1663 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1664 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1665 ret
= -ENOTCONN
; /* Caused by app exiting. */
1667 } else if (ret
< 0) {
1670 /* We don't need the stream anymore once sent to the tracer. */
1671 cds_list_del(&stream
->list
);
1672 delete_ust_app_stream(-1, stream
, app
);
1674 /* Flag the channel that it is sent to the application. */
1675 ua_chan
->is_sent
= 1;
1678 health_code_update();
1683 * Create the specified event onto the UST tracer for a UST session.
1685 * Should be called with session mutex held.
1688 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1689 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1693 health_code_update();
1695 /* Create UST event on tracer */
1696 pthread_mutex_lock(&app
->sock_lock
);
1697 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1699 pthread_mutex_unlock(&app
->sock_lock
);
1701 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1703 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1704 ua_event
->attr
.name
, app
->pid
, ret
);
1707 * This is normal behavior, an application can die during the
1708 * creation process. Don't report an error so the execution can
1709 * continue normally.
1712 DBG3("UST app create event failed. Application is dead.");
1717 ua_event
->handle
= ua_event
->obj
->handle
;
1719 DBG2("UST app event %s created successfully for pid:%d object: %p",
1720 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
1722 health_code_update();
1724 /* Set filter if one is present. */
1725 if (ua_event
->filter
) {
1726 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
1732 /* Set exclusions for the event */
1733 if (ua_event
->exclusion
) {
1734 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
1740 /* If event not enabled, disable it on the tracer */
1741 if (ua_event
->enabled
) {
1743 * We now need to explicitly enable the event, since it
1744 * is now disabled at creation.
1746 ret
= enable_ust_object(app
, ua_event
->obj
);
1749 * If we hit an EPERM, something is wrong with our enable call. If
1750 * we get an EEXIST, there is a problem on the tracer side since we
1754 case -LTTNG_UST_ERR_PERM
:
1755 /* Code flow problem */
1757 case -LTTNG_UST_ERR_EXIST
:
1758 /* It's OK for our use case. */
1769 health_code_update();
1774 * Copy data between an UST app event and a LTT event.
1776 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1777 struct ltt_ust_event
*uevent
)
1779 size_t exclusion_alloc_size
;
1781 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1782 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1784 ua_event
->enabled
= uevent
->enabled
;
1786 /* Copy event attributes */
1787 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1789 /* Copy filter bytecode */
1790 if (uevent
->filter
) {
1791 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1792 /* Filter might be NULL here in case of ENONEM. */
1795 /* Copy exclusion data */
1796 if (uevent
->exclusion
) {
1797 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1798 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1799 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1800 if (ua_event
->exclusion
== NULL
) {
1803 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1804 exclusion_alloc_size
);
1810 * Copy data between an UST app channel and a LTT channel.
1812 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1813 struct ltt_ust_channel
*uchan
)
1815 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1817 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1818 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1820 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1821 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1823 /* Copy event attributes since the layout is different. */
1824 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1825 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1826 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1827 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1828 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1829 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1830 ua_chan
->attr
.output
= uchan
->attr
.output
;
1831 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
1834 * Note that the attribute channel type is not set since the channel on the
1835 * tracing registry side does not have this information.
1838 ua_chan
->enabled
= uchan
->enabled
;
1839 ua_chan
->tracing_channel_id
= uchan
->id
;
1841 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1845 * Copy data between a UST app session and a regular LTT session.
1847 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1848 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1850 struct tm
*timeinfo
;
1853 char tmp_shm_path
[PATH_MAX
];
1855 timeinfo
= localtime(&app
->registration_time
);
1856 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1858 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1860 ua_sess
->tracing_id
= usess
->id
;
1861 ua_sess
->id
= get_next_session_id();
1862 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
1863 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
1864 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
1865 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
1866 ua_sess
->buffer_type
= usess
->buffer_type
;
1867 ua_sess
->bits_per_long
= app
->bits_per_long
;
1869 /* There is only one consumer object per session possible. */
1870 consumer_output_get(usess
->consumer
);
1871 ua_sess
->consumer
= usess
->consumer
;
1873 ua_sess
->output_traces
= usess
->output_traces
;
1874 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1875 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1876 &usess
->metadata_attr
);
1878 switch (ua_sess
->buffer_type
) {
1879 case LTTNG_BUFFER_PER_PID
:
1880 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1881 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1884 case LTTNG_BUFFER_PER_UID
:
1885 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1886 DEFAULT_UST_TRACE_UID_PATH
,
1887 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
1888 app
->bits_per_long
);
1895 PERROR("asprintf UST shadow copy session");
1900 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1901 sizeof(ua_sess
->root_shm_path
));
1902 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1903 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1904 sizeof(ua_sess
->shm_path
));
1905 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1906 if (ua_sess
->shm_path
[0]) {
1907 switch (ua_sess
->buffer_type
) {
1908 case LTTNG_BUFFER_PER_PID
:
1909 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1910 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1911 app
->name
, app
->pid
, datetime
);
1913 case LTTNG_BUFFER_PER_UID
:
1914 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1915 "/" DEFAULT_UST_TRACE_UID_PATH
,
1916 app
->uid
, app
->bits_per_long
);
1923 PERROR("sprintf UST shadow copy session");
1927 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1928 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1929 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1934 consumer_output_put(ua_sess
->consumer
);
1938 * Lookup sesison wrapper.
1941 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
1942 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1944 /* Get right UST app session from app */
1945 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1949 * Return ust app session from the app session hashtable using the UST session
1952 static struct ust_app_session
*lookup_session_by_app(
1953 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
1955 struct lttng_ht_iter iter
;
1956 struct lttng_ht_node_u64
*node
;
1958 __lookup_session_by_app(usess
, app
, &iter
);
1959 node
= lttng_ht_iter_get_node_u64(&iter
);
1964 return caa_container_of(node
, struct ust_app_session
, node
);
1971 * Setup buffer registry per PID for the given session and application. If none
1972 * is found, a new one is created, added to the global registry and
1973 * initialized. If regp is valid, it's set with the newly created object.
1975 * Return 0 on success or else a negative value.
1977 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1978 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1981 struct buffer_reg_pid
*reg_pid
;
1988 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1991 * This is the create channel path meaning that if there is NO
1992 * registry available, we have to create one for this session.
1994 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
1995 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2003 /* Initialize registry. */
2004 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2005 app
->bits_per_long
, app
->uint8_t_alignment
,
2006 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2007 app
->uint64_t_alignment
, app
->long_alignment
,
2008 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2009 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2010 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2011 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2012 ua_sess
->tracing_id
,
2016 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2017 * destroy the buffer registry, because it is always expected
2018 * that if the buffer registry can be found, its ust registry is
2021 buffer_reg_pid_destroy(reg_pid
);
2025 buffer_reg_pid_add(reg_pid
);
2027 DBG3("UST app buffer registry per PID created successfully");
2039 * Setup buffer registry per UID for the given session and application. If none
2040 * is found, a new one is created, added to the global registry and
2041 * initialized. If regp is valid, it's set with the newly created object.
2043 * Return 0 on success or else a negative value.
2045 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2046 struct ust_app_session
*ua_sess
,
2047 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2050 struct buffer_reg_uid
*reg_uid
;
2057 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2060 * This is the create channel path meaning that if there is NO
2061 * registry available, we have to create one for this session.
2063 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2064 LTTNG_DOMAIN_UST
, ®_uid
,
2065 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2073 /* Initialize registry. */
2074 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2075 app
->bits_per_long
, app
->uint8_t_alignment
,
2076 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2077 app
->uint64_t_alignment
, app
->long_alignment
,
2078 app
->byte_order
, app
->version
.major
,
2079 app
->version
.minor
, reg_uid
->root_shm_path
,
2080 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2081 ua_sess
->tracing_id
, app
->uid
);
2084 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2085 * destroy the buffer registry, because it is always expected
2086 * that if the buffer registry can be found, its ust registry is
2089 buffer_reg_uid_destroy(reg_uid
, NULL
);
2092 /* Add node to teardown list of the session. */
2093 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2095 buffer_reg_uid_add(reg_uid
);
2097 DBG3("UST app buffer registry per UID created successfully");
2108 * Create a session on the tracer side for the given app.
2110 * On success, ua_sess_ptr is populated with the session pointer or else left
2111 * untouched. If the session was created, is_created is set to 1. On error,
2112 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2115 * Returns 0 on success or else a negative code which is either -ENOMEM or
2116 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2118 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2119 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2122 int ret
, created
= 0;
2123 struct ust_app_session
*ua_sess
;
2127 assert(ua_sess_ptr
);
2129 health_code_update();
2131 ua_sess
= lookup_session_by_app(usess
, app
);
2132 if (ua_sess
== NULL
) {
2133 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2134 app
->pid
, usess
->id
);
2135 ua_sess
= alloc_ust_app_session();
2136 if (ua_sess
== NULL
) {
2137 /* Only malloc can failed so something is really wrong */
2141 shadow_copy_session(ua_sess
, usess
, app
);
2145 switch (usess
->buffer_type
) {
2146 case LTTNG_BUFFER_PER_PID
:
2147 /* Init local registry. */
2148 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2150 delete_ust_app_session(-1, ua_sess
, app
);
2154 case LTTNG_BUFFER_PER_UID
:
2155 /* Look for a global registry. If none exists, create one. */
2156 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2158 delete_ust_app_session(-1, ua_sess
, app
);
2168 health_code_update();
2170 if (ua_sess
->handle
== -1) {
2171 pthread_mutex_lock(&app
->sock_lock
);
2172 ret
= ustctl_create_session(app
->sock
);
2173 pthread_mutex_unlock(&app
->sock_lock
);
2175 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2176 ERR("Creating session for app pid %d with ret %d",
2179 DBG("UST app creating session failed. Application is dead");
2181 * This is normal behavior, an application can die during the
2182 * creation process. Don't report an error so the execution can
2183 * continue normally. This will get flagged ENOTCONN and the
2184 * caller will handle it.
2188 delete_ust_app_session(-1, ua_sess
, app
);
2189 if (ret
!= -ENOMEM
) {
2191 * Tracer is probably gone or got an internal error so let's
2192 * behave like it will soon unregister or not usable.
2199 ua_sess
->handle
= ret
;
2201 /* Add ust app session to app's HT */
2202 lttng_ht_node_init_u64(&ua_sess
->node
,
2203 ua_sess
->tracing_id
);
2204 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2205 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2206 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2207 &ua_sess
->ust_objd_node
);
2209 DBG2("UST app session created successfully with handle %d", ret
);
2212 *ua_sess_ptr
= ua_sess
;
2214 *is_created
= created
;
2217 /* Everything went well. */
2221 health_code_update();
2226 * Match function for a hash table lookup of ust_app_ctx.
2228 * It matches an ust app context based on the context type and, in the case
2229 * of perf counters, their name.
2231 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2233 struct ust_app_ctx
*ctx
;
2234 const struct lttng_ust_context_attr
*key
;
2239 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2243 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2248 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2249 if (strncmp(key
->u
.perf_counter
.name
,
2250 ctx
->ctx
.u
.perf_counter
.name
,
2251 sizeof(key
->u
.perf_counter
.name
))) {
2255 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2256 if (strcmp(key
->u
.app_ctx
.provider_name
,
2257 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2258 strcmp(key
->u
.app_ctx
.ctx_name
,
2259 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2275 * Lookup for an ust app context from an lttng_ust_context.
2277 * Must be called while holding RCU read side lock.
2278 * Return an ust_app_ctx object or NULL on error.
2281 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2282 struct lttng_ust_context_attr
*uctx
)
2284 struct lttng_ht_iter iter
;
2285 struct lttng_ht_node_ulong
*node
;
2286 struct ust_app_ctx
*app_ctx
= NULL
;
2291 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2292 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2293 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2294 node
= lttng_ht_iter_get_node_ulong(&iter
);
2299 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2306 * Create a context for the channel on the tracer.
2308 * Called with UST app session lock held and a RCU read side lock.
2311 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2312 struct lttng_ust_context_attr
*uctx
,
2313 struct ust_app
*app
)
2316 struct ust_app_ctx
*ua_ctx
;
2318 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2320 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2326 ua_ctx
= alloc_ust_app_ctx(uctx
);
2327 if (ua_ctx
== NULL
) {
2333 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2334 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2335 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2337 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2347 * Enable on the tracer side a ust app event for the session and channel.
2349 * Called with UST app session lock held.
2352 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2353 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2357 ret
= enable_ust_object(app
, ua_event
->obj
);
2362 ua_event
->enabled
= 1;
2369 * Disable on the tracer side a ust app event for the session and channel.
2371 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2372 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2376 ret
= disable_ust_object(app
, ua_event
->obj
);
2381 ua_event
->enabled
= 0;
2388 * Lookup ust app channel for session and disable it on the tracer side.
2391 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2392 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2396 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2401 ua_chan
->enabled
= 0;
2408 * Lookup ust app channel for session and enable it on the tracer side. This
2409 * MUST be called with a RCU read side lock acquired.
2411 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2412 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2415 struct lttng_ht_iter iter
;
2416 struct lttng_ht_node_str
*ua_chan_node
;
2417 struct ust_app_channel
*ua_chan
;
2419 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2420 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2421 if (ua_chan_node
== NULL
) {
2422 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2423 uchan
->name
, ua_sess
->tracing_id
);
2427 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2429 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2439 * Ask the consumer to create a channel and get it if successful.
2441 * Called with UST app session lock held.
2443 * Return 0 on success or else a negative value.
2445 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2446 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2447 int bitness
, struct ust_registry_session
*registry
,
2448 uint64_t trace_archive_id
)
2451 unsigned int nb_fd
= 0;
2452 struct consumer_socket
*socket
;
2460 health_code_update();
2462 /* Get the right consumer socket for the application. */
2463 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2469 health_code_update();
2471 /* Need one fd for the channel. */
2472 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2474 ERR("Exhausted number of available FD upon create channel");
2479 * Ask consumer to create channel. The consumer will return the number of
2480 * stream we have to expect.
2482 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2483 registry
, usess
->current_trace_chunk
);
2489 * Compute the number of fd needed before receiving them. It must be 2 per
2490 * stream (2 being the default value here).
2492 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2494 /* Reserve the amount of file descriptor we need. */
2495 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2497 ERR("Exhausted number of available FD upon create channel");
2498 goto error_fd_get_stream
;
2501 health_code_update();
2504 * Now get the channel from the consumer. This call wil populate the stream
2505 * list of that channel and set the ust objects.
2507 if (usess
->consumer
->enabled
) {
2508 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2518 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2519 error_fd_get_stream
:
2521 * Initiate a destroy channel on the consumer since we had an error
2522 * handling it on our side. The return value is of no importance since we
2523 * already have a ret value set by the previous error that we need to
2526 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2528 lttng_fd_put(LTTNG_FD_APPS
, 1);
2530 health_code_update();
2536 * Duplicate the ust data object of the ust app stream and save it in the
2537 * buffer registry stream.
2539 * Return 0 on success or else a negative value.
2541 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2542 struct ust_app_stream
*stream
)
2549 /* Reserve the amount of file descriptor we need. */
2550 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2552 ERR("Exhausted number of available FD upon duplicate stream");
2556 /* Duplicate object for stream once the original is in the registry. */
2557 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2558 reg_stream
->obj
.ust
);
2560 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2561 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2562 lttng_fd_put(LTTNG_FD_APPS
, 2);
2565 stream
->handle
= stream
->obj
->handle
;
2572 * Duplicate the ust data object of the ust app. channel and save it in the
2573 * buffer registry channel.
2575 * Return 0 on success or else a negative value.
2577 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2578 struct ust_app_channel
*ua_chan
)
2585 /* Need two fds for the channel. */
2586 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2588 ERR("Exhausted number of available FD upon duplicate channel");
2592 /* Duplicate object for stream once the original is in the registry. */
2593 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2595 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2596 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2599 ua_chan
->handle
= ua_chan
->obj
->handle
;
2604 lttng_fd_put(LTTNG_FD_APPS
, 1);
2610 * For a given channel buffer registry, setup all streams of the given ust
2611 * application channel.
2613 * Return 0 on success or else a negative value.
2615 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2616 struct ust_app_channel
*ua_chan
,
2617 struct ust_app
*app
)
2620 struct ust_app_stream
*stream
, *stmp
;
2625 DBG2("UST app setup buffer registry stream");
2627 /* Send all streams to application. */
2628 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2629 struct buffer_reg_stream
*reg_stream
;
2631 ret
= buffer_reg_stream_create(®_stream
);
2637 * Keep original pointer and nullify it in the stream so the delete
2638 * stream call does not release the object.
2640 reg_stream
->obj
.ust
= stream
->obj
;
2642 buffer_reg_stream_add(reg_stream
, reg_chan
);
2644 /* We don't need the streams anymore. */
2645 cds_list_del(&stream
->list
);
2646 delete_ust_app_stream(-1, stream
, app
);
2654 * Create a buffer registry channel for the given session registry and
2655 * application channel object. If regp pointer is valid, it's set with the
2656 * created object. Important, the created object is NOT added to the session
2657 * registry hash table.
2659 * Return 0 on success else a negative value.
2661 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2662 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2665 struct buffer_reg_channel
*reg_chan
= NULL
;
2670 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2672 /* Create buffer registry channel. */
2673 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2678 reg_chan
->consumer_key
= ua_chan
->key
;
2679 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2680 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2682 /* Create and add a channel registry to session. */
2683 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2684 ua_chan
->tracing_channel_id
);
2688 buffer_reg_channel_add(reg_sess
, reg_chan
);
2697 /* Safe because the registry channel object was not added to any HT. */
2698 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2704 * Setup buffer registry channel for the given session registry and application
2705 * channel object. If regp pointer is valid, it's set with the created object.
2707 * Return 0 on success else a negative value.
2709 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2710 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2711 struct ust_app
*app
)
2718 assert(ua_chan
->obj
);
2720 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2722 /* Setup all streams for the registry. */
2723 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2728 reg_chan
->obj
.ust
= ua_chan
->obj
;
2729 ua_chan
->obj
= NULL
;
2734 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2735 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2740 * Send buffer registry channel to the application.
2742 * Return 0 on success else a negative value.
2744 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2745 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2746 struct ust_app_channel
*ua_chan
)
2749 struct buffer_reg_stream
*reg_stream
;
2756 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2758 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2763 /* Send channel to the application. */
2764 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2765 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2766 ret
= -ENOTCONN
; /* Caused by app exiting. */
2768 } else if (ret
< 0) {
2772 health_code_update();
2774 /* Send all streams to application. */
2775 pthread_mutex_lock(®_chan
->stream_list_lock
);
2776 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2777 struct ust_app_stream stream
;
2779 ret
= duplicate_stream_object(reg_stream
, &stream
);
2781 goto error_stream_unlock
;
2784 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2786 (void) release_ust_app_stream(-1, &stream
, app
);
2787 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2788 ret
= -ENOTCONN
; /* Caused by app exiting. */
2790 goto error_stream_unlock
;
2794 * The return value is not important here. This function will output an
2797 (void) release_ust_app_stream(-1, &stream
, app
);
2799 ua_chan
->is_sent
= 1;
2801 error_stream_unlock
:
2802 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2808 * Create and send to the application the created buffers with per UID buffers.
2810 * This MUST be called with a RCU read side lock acquired.
2811 * The session list lock and the session's lock must be acquired.
2813 * Return 0 on success else a negative value.
2815 static int create_channel_per_uid(struct ust_app
*app
,
2816 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2817 struct ust_app_channel
*ua_chan
)
2820 struct buffer_reg_uid
*reg_uid
;
2821 struct buffer_reg_channel
*reg_chan
;
2822 struct ltt_session
*session
= NULL
;
2823 enum lttng_error_code notification_ret
;
2824 struct ust_registry_channel
*chan_reg
;
2831 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2833 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2835 * The session creation handles the creation of this global registry
2836 * object. If none can be find, there is a code flow problem or a
2841 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2847 /* Create the buffer registry channel object. */
2848 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2850 ERR("Error creating the UST channel \"%s\" registry instance",
2855 session
= session_find_by_id(ua_sess
->tracing_id
);
2857 assert(pthread_mutex_trylock(&session
->lock
));
2858 assert(session_trylock_list());
2861 * Create the buffers on the consumer side. This call populates the
2862 * ust app channel object with all streams and data object.
2864 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2865 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
2866 session
->most_recent_chunk_id
.value
);
2868 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2872 * Let's remove the previously created buffer registry channel so
2873 * it's not visible anymore in the session registry.
2875 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2876 ua_chan
->tracing_channel_id
, false);
2877 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2878 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2883 * Setup the streams and add it to the session registry.
2885 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2886 ua_chan
, reg_chan
, app
);
2888 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
2892 /* Notify the notification subsystem of the channel's creation. */
2893 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2894 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2895 ua_chan
->tracing_channel_id
);
2897 chan_reg
->consumer_key
= ua_chan
->key
;
2899 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2901 notification_ret
= notification_thread_command_add_channel(
2902 notification_thread_handle
, session
->name
,
2903 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2904 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2906 ua_chan
->key
, LTTNG_DOMAIN_UST
,
2907 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2908 if (notification_ret
!= LTTNG_OK
) {
2909 ret
= - (int) notification_ret
;
2910 ERR("Failed to add channel to notification thread");
2915 /* Send buffers to the application. */
2916 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2918 if (ret
!= -ENOTCONN
) {
2919 ERR("Error sending channel to application");
2926 session_put(session
);
2932 * Create and send to the application the created buffers with per PID buffers.
2934 * Called with UST app session lock held.
2935 * The session list lock and the session's lock must be acquired.
2937 * Return 0 on success else a negative value.
2939 static int create_channel_per_pid(struct ust_app
*app
,
2940 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2941 struct ust_app_channel
*ua_chan
)
2944 struct ust_registry_session
*registry
;
2945 enum lttng_error_code cmd_ret
;
2946 struct ltt_session
*session
= NULL
;
2947 uint64_t chan_reg_key
;
2948 struct ust_registry_channel
*chan_reg
;
2955 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2959 registry
= get_session_registry(ua_sess
);
2960 /* The UST app session lock is held, registry shall not be null. */
2963 /* Create and add a new channel registry to session. */
2964 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2966 ERR("Error creating the UST channel \"%s\" registry instance",
2971 session
= session_find_by_id(ua_sess
->tracing_id
);
2974 assert(pthread_mutex_trylock(&session
->lock
));
2975 assert(session_trylock_list());
2977 /* Create and get channel on the consumer side. */
2978 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2979 app
->bits_per_long
, registry
,
2980 session
->most_recent_chunk_id
.value
);
2982 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2984 goto error_remove_from_registry
;
2987 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2989 if (ret
!= -ENOTCONN
) {
2990 ERR("Error sending channel to application");
2992 goto error_remove_from_registry
;
2995 chan_reg_key
= ua_chan
->key
;
2996 pthread_mutex_lock(®istry
->lock
);
2997 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
2999 chan_reg
->consumer_key
= ua_chan
->key
;
3000 pthread_mutex_unlock(®istry
->lock
);
3002 cmd_ret
= notification_thread_command_add_channel(
3003 notification_thread_handle
, session
->name
,
3004 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3005 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3007 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3008 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3009 if (cmd_ret
!= LTTNG_OK
) {
3010 ret
= - (int) cmd_ret
;
3011 ERR("Failed to add channel to notification thread");
3012 goto error_remove_from_registry
;
3015 error_remove_from_registry
:
3017 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3022 session_put(session
);
3028 * From an already allocated ust app channel, create the channel buffers if
3029 * needed and send them to the application. This MUST be called with a RCU read
3030 * side lock acquired.
3032 * Called with UST app session lock held.
3034 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3035 * the application exited concurrently.
3037 static int ust_app_channel_send(struct ust_app
*app
,
3038 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3039 struct ust_app_channel
*ua_chan
)
3045 assert(usess
->active
);
3049 /* Handle buffer type before sending the channel to the application. */
3050 switch (usess
->buffer_type
) {
3051 case LTTNG_BUFFER_PER_UID
:
3053 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3059 case LTTNG_BUFFER_PER_PID
:
3061 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3073 /* Initialize ust objd object using the received handle and add it. */
3074 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3075 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3077 /* If channel is not enabled, disable it on the tracer */
3078 if (!ua_chan
->enabled
) {
3079 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3090 * Create UST app channel and return it through ua_chanp if not NULL.
3092 * Called with UST app session lock and RCU read-side lock held.
3094 * Return 0 on success or else a negative value.
3096 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3097 struct ltt_ust_channel
*uchan
,
3098 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3099 struct ust_app_channel
**ua_chanp
)
3102 struct lttng_ht_iter iter
;
3103 struct lttng_ht_node_str
*ua_chan_node
;
3104 struct ust_app_channel
*ua_chan
;
3106 /* Lookup channel in the ust app session */
3107 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3108 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3109 if (ua_chan_node
!= NULL
) {
3110 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3114 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3115 if (ua_chan
== NULL
) {
3116 /* Only malloc can fail here */
3120 shadow_copy_channel(ua_chan
, uchan
);
3122 /* Set channel type. */
3123 ua_chan
->attr
.type
= type
;
3125 /* Only add the channel if successful on the tracer side. */
3126 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3129 *ua_chanp
= ua_chan
;
3132 /* Everything went well. */
3140 * Create UST app event and create it on the tracer side.
3142 * Called with ust app session mutex held.
3145 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3146 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3147 struct ust_app
*app
)
3150 struct ust_app_event
*ua_event
;
3152 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3153 if (ua_event
== NULL
) {
3154 /* Only failure mode of alloc_ust_app_event(). */
3158 shadow_copy_event(ua_event
, uevent
);
3160 /* Create it on the tracer side */
3161 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3164 * Not found previously means that it does not exist on the
3165 * tracer. If the application reports that the event existed,
3166 * it means there is a bug in the sessiond or lttng-ust
3167 * (or corruption, etc.)
3169 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3170 ERR("Tracer for application reported that an event being created already existed: "
3171 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3173 app
->pid
, app
->ppid
, app
->uid
,
3179 add_unique_ust_app_event(ua_chan
, ua_event
);
3181 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3188 /* Valid. Calling here is already in a read side lock */
3189 delete_ust_app_event(-1, ua_event
, app
);
3194 * Create UST metadata and open it on the tracer side.
3196 * Called with UST app session lock held and RCU read side lock.
3198 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3199 struct ust_app
*app
, struct consumer_output
*consumer
)
3202 struct ust_app_channel
*metadata
;
3203 struct consumer_socket
*socket
;
3204 struct ust_registry_session
*registry
;
3205 struct ltt_session
*session
= NULL
;
3211 registry
= get_session_registry(ua_sess
);
3212 /* The UST app session is held registry shall not be null. */
3215 pthread_mutex_lock(®istry
->lock
);
3217 /* Metadata already exists for this registry or it was closed previously */
3218 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3223 /* Allocate UST metadata */
3224 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3226 /* malloc() failed */
3231 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3233 /* Need one fd for the channel. */
3234 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3236 ERR("Exhausted number of available FD upon create metadata");
3240 /* Get the right consumer socket for the application. */
3241 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3244 goto error_consumer
;
3248 * Keep metadata key so we can identify it on the consumer side. Assign it
3249 * to the registry *before* we ask the consumer so we avoid the race of the
3250 * consumer requesting the metadata and the ask_channel call on our side
3251 * did not returned yet.
3253 registry
->metadata_key
= metadata
->key
;
3255 session
= session_find_by_id(ua_sess
->tracing_id
);
3258 assert(pthread_mutex_trylock(&session
->lock
));
3259 assert(session_trylock_list());
3262 * Ask the metadata channel creation to the consumer. The metadata object
3263 * will be created by the consumer and kept their. However, the stream is
3264 * never added or monitored until we do a first push metadata to the
3267 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3268 registry
, session
->current_trace_chunk
);
3270 /* Nullify the metadata key so we don't try to close it later on. */
3271 registry
->metadata_key
= 0;
3272 goto error_consumer
;
3276 * The setup command will make the metadata stream be sent to the relayd,
3277 * if applicable, and the thread managing the metadatas. This is important
3278 * because after this point, if an error occurs, the only way the stream
3279 * can be deleted is to be monitored in the consumer.
3281 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3283 /* Nullify the metadata key so we don't try to close it later on. */
3284 registry
->metadata_key
= 0;
3285 goto error_consumer
;
3288 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3289 metadata
->key
, app
->pid
);
3292 lttng_fd_put(LTTNG_FD_APPS
, 1);
3293 delete_ust_app_channel(-1, metadata
, app
);
3295 pthread_mutex_unlock(®istry
->lock
);
3297 session_put(session
);
3303 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3304 * acquired before calling this function.
3306 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3308 struct ust_app
*app
= NULL
;
3309 struct lttng_ht_node_ulong
*node
;
3310 struct lttng_ht_iter iter
;
3312 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3313 node
= lttng_ht_iter_get_node_ulong(&iter
);
3315 DBG2("UST app no found with pid %d", pid
);
3319 DBG2("Found UST app by pid %d", pid
);
3321 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3328 * Allocate and init an UST app object using the registration information and
3329 * the command socket. This is called when the command socket connects to the
3332 * The object is returned on success or else NULL.
3334 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3336 struct ust_app
*lta
= NULL
;
3337 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3342 DBG3("UST app creating application for socket %d", sock
);
3344 if ((msg
->bits_per_long
== 64 &&
3345 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3346 || (msg
->bits_per_long
== 32 &&
3347 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3348 ERR("Registration failed: application \"%s\" (pid: %d) has "
3349 "%d-bit long, but no consumerd for this size is available.\n",
3350 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3354 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3355 if (!event_notifier_event_source_pipe
) {
3356 PERROR("Failed to open application event source pipe: '%s' (ppid = %d)",
3357 msg
->name
, msg
->ppid
);
3361 lta
= zmalloc(sizeof(struct ust_app
));
3364 goto error_free_pipe
;
3367 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
3369 lta
->ppid
= msg
->ppid
;
3370 lta
->uid
= msg
->uid
;
3371 lta
->gid
= msg
->gid
;
3373 lta
->bits_per_long
= msg
->bits_per_long
;
3374 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3375 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3376 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3377 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3378 lta
->long_alignment
= msg
->long_alignment
;
3379 lta
->byte_order
= msg
->byte_order
;
3381 lta
->v_major
= msg
->major
;
3382 lta
->v_minor
= msg
->minor
;
3383 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3384 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3385 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3386 lta
->notify_sock
= -1;
3388 /* Copy name and make sure it's NULL terminated. */
3389 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3390 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3393 * Before this can be called, when receiving the registration information,
3394 * the application compatibility is checked. So, at this point, the
3395 * application can work with this session daemon.
3397 lta
->compatible
= 1;
3399 lta
->pid
= msg
->pid
;
3400 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3402 pthread_mutex_init(<a
->sock_lock
, NULL
);
3403 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3405 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3409 lttng_pipe_destroy(event_notifier_event_source_pipe
);
3415 * For a given application object, add it to every hash table.
3417 void ust_app_add(struct ust_app
*app
)
3420 assert(app
->notify_sock
>= 0);
3422 app
->registration_time
= time(NULL
);
3427 * On a re-registration, we want to kick out the previous registration of
3430 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3433 * The socket _should_ be unique until _we_ call close. So, a add_unique
3434 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3435 * already in the table.
3437 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3439 /* Add application to the notify socket hash table. */
3440 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3441 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3443 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3444 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3445 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3452 * Set the application version into the object.
3454 * Return 0 on success else a negative value either an errno code or a
3455 * LTTng-UST error code.
3457 int ust_app_version(struct ust_app
*app
)
3463 pthread_mutex_lock(&app
->sock_lock
);
3464 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3465 pthread_mutex_unlock(&app
->sock_lock
);
3467 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3468 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3470 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3478 * Setup the base event notifier group.
3480 * Return 0 on success else a negative value either an errno code or a
3481 * LTTng-UST error code.
3483 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
3486 int event_pipe_write_fd
;
3487 struct lttng_ust_object_data
*event_notifier_group
= NULL
;
3488 enum lttng_error_code lttng_ret
;
3492 /* Get the write side of the pipe. */
3493 event_pipe_write_fd
= lttng_pipe_get_writefd(
3494 app
->event_notifier_group
.event_pipe
);
3496 pthread_mutex_lock(&app
->sock_lock
);
3497 ret
= ustctl_create_event_notifier_group(app
->sock
,
3498 event_pipe_write_fd
, &event_notifier_group
);
3499 pthread_mutex_unlock(&app
->sock_lock
);
3501 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3502 ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d",
3503 ret
, app
->sock
, event_pipe_write_fd
);
3505 DBG("Failed to create application event notifier group (application is dead): app socket fd = %d",
3512 lttng_ret
= notification_thread_command_add_tracer_event_source(
3513 notification_thread_handle
,
3514 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
),
3516 if (lttng_ret
!= LTTNG_OK
) {
3517 ERR("Failed to add tracer event source to notification thread");
3522 /* Assign handle only when the complete setup is valid. */
3523 app
->event_notifier_group
.object
= event_notifier_group
;
3527 ustctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
3528 free(app
->event_notifier_group
.object
);
3533 * Unregister app by removing it from the global traceable app list and freeing
3536 * The socket is already closed at this point so no close to sock.
3538 void ust_app_unregister(int sock
)
3540 struct ust_app
*lta
;
3541 struct lttng_ht_node_ulong
*node
;
3542 struct lttng_ht_iter ust_app_sock_iter
;
3543 struct lttng_ht_iter iter
;
3544 struct ust_app_session
*ua_sess
;
3549 /* Get the node reference for a call_rcu */
3550 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3551 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3554 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3555 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3558 * For per-PID buffers, perform "push metadata" and flush all
3559 * application streams before removing app from hash tables,
3560 * ensuring proper behavior of data_pending check.
3561 * Remove sessions so they are not visible during deletion.
3563 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3565 struct ust_registry_session
*registry
;
3567 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3569 /* The session was already removed so scheduled for teardown. */
3573 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3574 (void) ust_app_flush_app_session(lta
, ua_sess
);
3578 * Add session to list for teardown. This is safe since at this point we
3579 * are the only one using this list.
3581 pthread_mutex_lock(&ua_sess
->lock
);
3583 if (ua_sess
->deleted
) {
3584 pthread_mutex_unlock(&ua_sess
->lock
);
3589 * Normally, this is done in the delete session process which is
3590 * executed in the call rcu below. However, upon registration we can't
3591 * afford to wait for the grace period before pushing data or else the
3592 * data pending feature can race between the unregistration and stop
3593 * command where the data pending command is sent *before* the grace
3596 * The close metadata below nullifies the metadata pointer in the
3597 * session so the delete session will NOT push/close a second time.
3599 registry
= get_session_registry(ua_sess
);
3601 /* Push metadata for application before freeing the application. */
3602 (void) push_metadata(registry
, ua_sess
->consumer
);
3605 * Don't ask to close metadata for global per UID buffers. Close
3606 * metadata only on destroy trace session in this case. Also, the
3607 * previous push metadata could have flag the metadata registry to
3608 * close so don't send a close command if closed.
3610 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3611 /* And ask to close it for this session registry. */
3612 (void) close_metadata(registry
, ua_sess
->consumer
);
3615 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3617 pthread_mutex_unlock(&ua_sess
->lock
);
3620 /* Remove application from PID hash table */
3621 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3625 * Remove application from notify hash table. The thread handling the
3626 * notify socket could have deleted the node so ignore on error because
3627 * either way it's valid. The close of that socket is handled by the
3628 * apps_notify_thread.
3630 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3631 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3634 * Ignore return value since the node might have been removed before by an
3635 * add replace during app registration because the PID can be reassigned by
3638 iter
.iter
.node
= <a
->pid_n
.node
;
3639 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3641 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3646 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3653 * Fill events array with all events name of all registered apps.
3655 int ust_app_list_events(struct lttng_event
**events
)
3658 size_t nbmem
, count
= 0;
3659 struct lttng_ht_iter iter
;
3660 struct ust_app
*app
;
3661 struct lttng_event
*tmp_event
;
3663 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3664 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3665 if (tmp_event
== NULL
) {
3666 PERROR("zmalloc ust app events");
3673 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3674 struct lttng_ust_tracepoint_iter uiter
;
3676 health_code_update();
3678 if (!app
->compatible
) {
3680 * TODO: In time, we should notice the caller of this error by
3681 * telling him that this is a version error.
3685 pthread_mutex_lock(&app
->sock_lock
);
3686 handle
= ustctl_tracepoint_list(app
->sock
);
3688 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3689 ERR("UST app list events getting handle failed for app pid %d",
3692 pthread_mutex_unlock(&app
->sock_lock
);
3696 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3697 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3698 /* Handle ustctl error. */
3702 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3703 ERR("UST app tp list get failed for app %d with ret %d",
3706 DBG3("UST app tp list get failed. Application is dead");
3708 * This is normal behavior, an application can die during the
3709 * creation process. Don't report an error so the execution can
3710 * continue normally. Continue normal execution.
3715 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3716 if (release_ret
< 0 &&
3717 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3718 release_ret
!= -EPIPE
) {
3719 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3721 pthread_mutex_unlock(&app
->sock_lock
);
3725 health_code_update();
3726 if (count
>= nbmem
) {
3727 /* In case the realloc fails, we free the memory */
3728 struct lttng_event
*new_tmp_event
;
3731 new_nbmem
= nbmem
<< 1;
3732 DBG2("Reallocating event list from %zu to %zu entries",
3734 new_tmp_event
= realloc(tmp_event
,
3735 new_nbmem
* sizeof(struct lttng_event
));
3736 if (new_tmp_event
== NULL
) {
3739 PERROR("realloc ust app events");
3742 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3743 if (release_ret
< 0 &&
3744 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3745 release_ret
!= -EPIPE
) {
3746 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3748 pthread_mutex_unlock(&app
->sock_lock
);
3751 /* Zero the new memory */
3752 memset(new_tmp_event
+ nbmem
, 0,
3753 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3755 tmp_event
= new_tmp_event
;
3757 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3758 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3759 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3760 tmp_event
[count
].pid
= app
->pid
;
3761 tmp_event
[count
].enabled
= -1;
3764 ret
= ustctl_release_handle(app
->sock
, handle
);
3765 pthread_mutex_unlock(&app
->sock_lock
);
3766 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3767 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3772 *events
= tmp_event
;
3774 DBG2("UST app list events done (%zu events)", count
);
3779 health_code_update();
3784 * Fill events array with all events name of all registered apps.
3786 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3789 size_t nbmem
, count
= 0;
3790 struct lttng_ht_iter iter
;
3791 struct ust_app
*app
;
3792 struct lttng_event_field
*tmp_event
;
3794 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3795 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3796 if (tmp_event
== NULL
) {
3797 PERROR("zmalloc ust app event fields");
3804 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3805 struct lttng_ust_field_iter uiter
;
3807 health_code_update();
3809 if (!app
->compatible
) {
3811 * TODO: In time, we should notice the caller of this error by
3812 * telling him that this is a version error.
3816 pthread_mutex_lock(&app
->sock_lock
);
3817 handle
= ustctl_tracepoint_field_list(app
->sock
);
3819 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3820 ERR("UST app list field getting handle failed for app pid %d",
3823 pthread_mutex_unlock(&app
->sock_lock
);
3827 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3828 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3829 /* Handle ustctl error. */
3833 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3834 ERR("UST app tp list field failed for app %d with ret %d",
3837 DBG3("UST app tp list field failed. Application is dead");
3839 * This is normal behavior, an application can die during the
3840 * creation process. Don't report an error so the execution can
3841 * continue normally. Reset list and count for next app.
3846 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3847 pthread_mutex_unlock(&app
->sock_lock
);
3848 if (release_ret
< 0 &&
3849 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3850 release_ret
!= -EPIPE
) {
3851 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3856 health_code_update();
3857 if (count
>= nbmem
) {
3858 /* In case the realloc fails, we free the memory */
3859 struct lttng_event_field
*new_tmp_event
;
3862 new_nbmem
= nbmem
<< 1;
3863 DBG2("Reallocating event field list from %zu to %zu entries",
3865 new_tmp_event
= realloc(tmp_event
,
3866 new_nbmem
* sizeof(struct lttng_event_field
));
3867 if (new_tmp_event
== NULL
) {
3870 PERROR("realloc ust app event fields");
3873 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3874 pthread_mutex_unlock(&app
->sock_lock
);
3876 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3877 release_ret
!= -EPIPE
) {
3878 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3882 /* Zero the new memory */
3883 memset(new_tmp_event
+ nbmem
, 0,
3884 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3886 tmp_event
= new_tmp_event
;
3889 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3890 /* Mapping between these enums matches 1 to 1. */
3891 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3892 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3894 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3895 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3896 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3897 tmp_event
[count
].event
.pid
= app
->pid
;
3898 tmp_event
[count
].event
.enabled
= -1;
3901 ret
= ustctl_release_handle(app
->sock
, handle
);
3902 pthread_mutex_unlock(&app
->sock_lock
);
3904 ret
!= -LTTNG_UST_ERR_EXITING
&&
3906 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3911 *fields
= tmp_event
;
3913 DBG2("UST app list event fields done (%zu events)", count
);
3918 health_code_update();
3923 * Free and clean all traceable apps of the global list.
3925 * Should _NOT_ be called with RCU read-side lock held.
3927 void ust_app_clean_list(void)
3930 struct ust_app
*app
;
3931 struct lttng_ht_iter iter
;
3933 DBG2("UST app cleaning registered apps hash table");
3937 /* Cleanup notify socket hash table */
3938 if (ust_app_ht_by_notify_sock
) {
3939 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3940 notify_sock_n
.node
) {
3941 struct cds_lfht_node
*node
;
3942 struct ust_app
*app
;
3944 node
= cds_lfht_iter_get_node(&iter
.iter
);
3949 app
= container_of(node
, struct ust_app
,
3950 notify_sock_n
.node
);
3951 ust_app_notify_sock_unregister(app
->notify_sock
);
3956 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3957 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3959 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3963 /* Cleanup socket hash table */
3964 if (ust_app_ht_by_sock
) {
3965 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3967 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3974 /* Destroy is done only when the ht is empty */
3976 ht_cleanup_push(ust_app_ht
);
3978 if (ust_app_ht_by_sock
) {
3979 ht_cleanup_push(ust_app_ht_by_sock
);
3981 if (ust_app_ht_by_notify_sock
) {
3982 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3987 * Init UST app hash table.
3989 int ust_app_ht_alloc(void)
3991 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3995 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3996 if (!ust_app_ht_by_sock
) {
3999 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4000 if (!ust_app_ht_by_notify_sock
) {
4007 * For a specific UST session, disable the channel for all registered apps.
4009 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4010 struct ltt_ust_channel
*uchan
)
4013 struct lttng_ht_iter iter
;
4014 struct lttng_ht_node_str
*ua_chan_node
;
4015 struct ust_app
*app
;
4016 struct ust_app_session
*ua_sess
;
4017 struct ust_app_channel
*ua_chan
;
4019 assert(usess
->active
);
4020 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4021 uchan
->name
, usess
->id
);
4025 /* For every registered applications */
4026 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4027 struct lttng_ht_iter uiter
;
4028 if (!app
->compatible
) {
4030 * TODO: In time, we should notice the caller of this error by
4031 * telling him that this is a version error.
4035 ua_sess
= lookup_session_by_app(usess
, app
);
4036 if (ua_sess
== NULL
) {
4041 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4042 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4043 /* If the session if found for the app, the channel must be there */
4044 assert(ua_chan_node
);
4046 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4047 /* The channel must not be already disabled */
4048 assert(ua_chan
->enabled
== 1);
4050 /* Disable channel onto application */
4051 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4053 /* XXX: We might want to report this error at some point... */
4063 * For a specific UST session, enable the channel for all registered apps.
4065 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4066 struct ltt_ust_channel
*uchan
)
4069 struct lttng_ht_iter iter
;
4070 struct ust_app
*app
;
4071 struct ust_app_session
*ua_sess
;
4073 assert(usess
->active
);
4074 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4075 uchan
->name
, usess
->id
);
4079 /* For every registered applications */
4080 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4081 if (!app
->compatible
) {
4083 * TODO: In time, we should notice the caller of this error by
4084 * telling him that this is a version error.
4088 ua_sess
= lookup_session_by_app(usess
, app
);
4089 if (ua_sess
== NULL
) {
4093 /* Enable channel onto application */
4094 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4096 /* XXX: We might want to report this error at some point... */
4106 * Disable an event in a channel and for a specific session.
4108 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4109 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4112 struct lttng_ht_iter iter
, uiter
;
4113 struct lttng_ht_node_str
*ua_chan_node
;
4114 struct ust_app
*app
;
4115 struct ust_app_session
*ua_sess
;
4116 struct ust_app_channel
*ua_chan
;
4117 struct ust_app_event
*ua_event
;
4119 assert(usess
->active
);
4120 DBG("UST app disabling event %s for all apps in channel "
4121 "%s for session id %" PRIu64
,
4122 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4126 /* For all registered applications */
4127 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4128 if (!app
->compatible
) {
4130 * TODO: In time, we should notice the caller of this error by
4131 * telling him that this is a version error.
4135 ua_sess
= lookup_session_by_app(usess
, app
);
4136 if (ua_sess
== NULL
) {
4141 /* Lookup channel in the ust app session */
4142 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4143 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4144 if (ua_chan_node
== NULL
) {
4145 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4146 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4149 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4151 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4152 uevent
->filter
, uevent
->attr
.loglevel
,
4154 if (ua_event
== NULL
) {
4155 DBG2("Event %s not found in channel %s for app pid %d."
4156 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4160 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4162 /* XXX: Report error someday... */
4171 /* The ua_sess lock must be held by the caller. */
4173 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4174 struct ust_app_session
*ua_sess
,
4175 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4176 struct ust_app_channel
**_ua_chan
)
4179 struct ust_app_channel
*ua_chan
= NULL
;
4182 ASSERT_LOCKED(ua_sess
->lock
);
4184 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4185 sizeof(uchan
->name
))) {
4186 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4190 struct ltt_ust_context
*uctx
= NULL
;
4193 * Create channel onto application and synchronize its
4196 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4197 LTTNG_UST_CHAN_PER_CPU
, usess
,
4203 ret
= ust_app_channel_send(app
, usess
,
4210 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4211 ret
= create_ust_app_channel_context(ua_chan
,
4224 * The application's socket is not valid. Either a bad socket
4225 * or a timeout on it. We can't inform the caller that for a
4226 * specific app, the session failed so lets continue here.
4228 ret
= 0; /* Not an error. */
4236 if (ret
== 0 && _ua_chan
) {
4238 * Only return the application's channel on success. Note
4239 * that the channel can still be part of the application's
4240 * channel hashtable on error.
4242 *_ua_chan
= ua_chan
;
4248 * Enable event for a specific session and channel on the tracer.
4250 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4251 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4254 struct lttng_ht_iter iter
, uiter
;
4255 struct lttng_ht_node_str
*ua_chan_node
;
4256 struct ust_app
*app
;
4257 struct ust_app_session
*ua_sess
;
4258 struct ust_app_channel
*ua_chan
;
4259 struct ust_app_event
*ua_event
;
4261 assert(usess
->active
);
4262 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4263 uevent
->attr
.name
, usess
->id
);
4266 * NOTE: At this point, this function is called only if the session and
4267 * channel passed are already created for all apps. and enabled on the
4273 /* For all registered applications */
4274 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4275 if (!app
->compatible
) {
4277 * TODO: In time, we should notice the caller of this error by
4278 * telling him that this is a version error.
4282 ua_sess
= lookup_session_by_app(usess
, app
);
4284 /* The application has problem or is probably dead. */
4288 pthread_mutex_lock(&ua_sess
->lock
);
4290 if (ua_sess
->deleted
) {
4291 pthread_mutex_unlock(&ua_sess
->lock
);
4295 /* Lookup channel in the ust app session */
4296 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4297 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4299 * It is possible that the channel cannot be found is
4300 * the channel/event creation occurs concurrently with
4301 * an application exit.
4303 if (!ua_chan_node
) {
4304 pthread_mutex_unlock(&ua_sess
->lock
);
4308 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4310 /* Get event node */
4311 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4312 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4313 if (ua_event
== NULL
) {
4314 DBG3("UST app enable event %s not found for app PID %d."
4315 "Skipping app", uevent
->attr
.name
, app
->pid
);
4319 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4321 pthread_mutex_unlock(&ua_sess
->lock
);
4325 pthread_mutex_unlock(&ua_sess
->lock
);
4334 * For a specific existing UST session and UST channel, creates the event for
4335 * all registered apps.
4337 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4338 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4341 struct lttng_ht_iter iter
, uiter
;
4342 struct lttng_ht_node_str
*ua_chan_node
;
4343 struct ust_app
*app
;
4344 struct ust_app_session
*ua_sess
;
4345 struct ust_app_channel
*ua_chan
;
4347 assert(usess
->active
);
4348 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4349 uevent
->attr
.name
, usess
->id
);
4353 /* For all registered applications */
4354 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4355 if (!app
->compatible
) {
4357 * TODO: In time, we should notice the caller of this error by
4358 * telling him that this is a version error.
4362 ua_sess
= lookup_session_by_app(usess
, app
);
4364 /* The application has problem or is probably dead. */
4368 pthread_mutex_lock(&ua_sess
->lock
);
4370 if (ua_sess
->deleted
) {
4371 pthread_mutex_unlock(&ua_sess
->lock
);
4375 /* Lookup channel in the ust app session */
4376 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4377 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4378 /* If the channel is not found, there is a code flow error */
4379 assert(ua_chan_node
);
4381 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4383 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4384 pthread_mutex_unlock(&ua_sess
->lock
);
4386 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4387 /* Possible value at this point: -ENOMEM. If so, we stop! */
4390 DBG2("UST app event %s already exist on app PID %d",
4391 uevent
->attr
.name
, app
->pid
);
4401 * Start tracing for a specific UST session and app.
4403 * Called with UST app session lock held.
4407 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4410 struct ust_app_session
*ua_sess
;
4412 DBG("Starting tracing for ust app pid %d", app
->pid
);
4416 if (!app
->compatible
) {
4420 ua_sess
= lookup_session_by_app(usess
, app
);
4421 if (ua_sess
== NULL
) {
4422 /* The session is in teardown process. Ignore and continue. */
4426 pthread_mutex_lock(&ua_sess
->lock
);
4428 if (ua_sess
->deleted
) {
4429 pthread_mutex_unlock(&ua_sess
->lock
);
4433 if (ua_sess
->enabled
) {
4434 pthread_mutex_unlock(&ua_sess
->lock
);
4438 /* Upon restart, we skip the setup, already done */
4439 if (ua_sess
->started
) {
4443 health_code_update();
4446 /* This starts the UST tracing */
4447 pthread_mutex_lock(&app
->sock_lock
);
4448 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4449 pthread_mutex_unlock(&app
->sock_lock
);
4451 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4452 ERR("Error starting tracing for app pid: %d (ret: %d)",
4455 DBG("UST app start session failed. Application is dead.");
4457 * This is normal behavior, an application can die during the
4458 * creation process. Don't report an error so the execution can
4459 * continue normally.
4461 pthread_mutex_unlock(&ua_sess
->lock
);
4467 /* Indicate that the session has been started once */
4468 ua_sess
->started
= 1;
4469 ua_sess
->enabled
= 1;
4471 pthread_mutex_unlock(&ua_sess
->lock
);
4473 health_code_update();
4475 /* Quiescent wait after starting trace */
4476 pthread_mutex_lock(&app
->sock_lock
);
4477 ret
= ustctl_wait_quiescent(app
->sock
);
4478 pthread_mutex_unlock(&app
->sock_lock
);
4479 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4480 ERR("UST app wait quiescent failed for app pid %d ret %d",
4486 health_code_update();
4490 pthread_mutex_unlock(&ua_sess
->lock
);
4492 health_code_update();
4497 * Stop tracing for a specific UST session and app.
4500 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4503 struct ust_app_session
*ua_sess
;
4504 struct ust_registry_session
*registry
;
4506 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4510 if (!app
->compatible
) {
4511 goto end_no_session
;
4514 ua_sess
= lookup_session_by_app(usess
, app
);
4515 if (ua_sess
== NULL
) {
4516 goto end_no_session
;
4519 pthread_mutex_lock(&ua_sess
->lock
);
4521 if (ua_sess
->deleted
) {
4522 pthread_mutex_unlock(&ua_sess
->lock
);
4523 goto end_no_session
;
4527 * If started = 0, it means that stop trace has been called for a session
4528 * that was never started. It's possible since we can have a fail start
4529 * from either the application manager thread or the command thread. Simply
4530 * indicate that this is a stop error.
4532 if (!ua_sess
->started
) {
4533 goto error_rcu_unlock
;
4536 health_code_update();
4538 /* This inhibits UST tracing */
4539 pthread_mutex_lock(&app
->sock_lock
);
4540 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4541 pthread_mutex_unlock(&app
->sock_lock
);
4543 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4544 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4547 DBG("UST app stop session failed. Application is dead.");
4549 * This is normal behavior, an application can die during the
4550 * creation process. Don't report an error so the execution can
4551 * continue normally.
4555 goto error_rcu_unlock
;
4558 health_code_update();
4559 ua_sess
->enabled
= 0;
4561 /* Quiescent wait after stopping trace */
4562 pthread_mutex_lock(&app
->sock_lock
);
4563 ret
= ustctl_wait_quiescent(app
->sock
);
4564 pthread_mutex_unlock(&app
->sock_lock
);
4565 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4566 ERR("UST app wait quiescent failed for app pid %d ret %d",
4570 health_code_update();
4572 registry
= get_session_registry(ua_sess
);
4574 /* The UST app session is held registry shall not be null. */
4577 /* Push metadata for application before freeing the application. */
4578 (void) push_metadata(registry
, ua_sess
->consumer
);
4581 pthread_mutex_unlock(&ua_sess
->lock
);
4584 health_code_update();
4588 pthread_mutex_unlock(&ua_sess
->lock
);
4590 health_code_update();
4595 int ust_app_flush_app_session(struct ust_app
*app
,
4596 struct ust_app_session
*ua_sess
)
4598 int ret
, retval
= 0;
4599 struct lttng_ht_iter iter
;
4600 struct ust_app_channel
*ua_chan
;
4601 struct consumer_socket
*socket
;
4603 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4607 if (!app
->compatible
) {
4608 goto end_not_compatible
;
4611 pthread_mutex_lock(&ua_sess
->lock
);
4613 if (ua_sess
->deleted
) {
4617 health_code_update();
4619 /* Flushing buffers */
4620 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4623 /* Flush buffers and push metadata. */
4624 switch (ua_sess
->buffer_type
) {
4625 case LTTNG_BUFFER_PER_PID
:
4626 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4628 health_code_update();
4629 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4631 ERR("Error flushing consumer channel");
4637 case LTTNG_BUFFER_PER_UID
:
4643 health_code_update();
4646 pthread_mutex_unlock(&ua_sess
->lock
);
4650 health_code_update();
4655 * Flush buffers for all applications for a specific UST session.
4656 * Called with UST session lock held.
4659 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4664 DBG("Flushing session buffers for all ust apps");
4668 /* Flush buffers and push metadata. */
4669 switch (usess
->buffer_type
) {
4670 case LTTNG_BUFFER_PER_UID
:
4672 struct buffer_reg_uid
*reg
;
4673 struct lttng_ht_iter iter
;
4675 /* Flush all per UID buffers associated to that session. */
4676 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4677 struct ust_registry_session
*ust_session_reg
;
4678 struct buffer_reg_channel
*reg_chan
;
4679 struct consumer_socket
*socket
;
4681 /* Get consumer socket to use to push the metadata.*/
4682 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4685 /* Ignore request if no consumer is found for the session. */
4689 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4690 reg_chan
, node
.node
) {
4692 * The following call will print error values so the return
4693 * code is of little importance because whatever happens, we
4694 * have to try them all.
4696 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4699 ust_session_reg
= reg
->registry
->reg
.ust
;
4700 /* Push metadata. */
4701 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4705 case LTTNG_BUFFER_PER_PID
:
4707 struct ust_app_session
*ua_sess
;
4708 struct lttng_ht_iter iter
;
4709 struct ust_app
*app
;
4711 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4712 ua_sess
= lookup_session_by_app(usess
, app
);
4713 if (ua_sess
== NULL
) {
4716 (void) ust_app_flush_app_session(app
, ua_sess
);
4727 health_code_update();
4732 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
4733 struct ust_app_session
*ua_sess
)
4736 struct lttng_ht_iter iter
;
4737 struct ust_app_channel
*ua_chan
;
4738 struct consumer_socket
*socket
;
4740 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
4744 if (!app
->compatible
) {
4745 goto end_not_compatible
;
4748 pthread_mutex_lock(&ua_sess
->lock
);
4750 if (ua_sess
->deleted
) {
4754 health_code_update();
4756 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4759 ERR("Failed to find consumer (%" PRIu32
") socket",
4760 app
->bits_per_long
);
4765 /* Clear quiescent state. */
4766 switch (ua_sess
->buffer_type
) {
4767 case LTTNG_BUFFER_PER_PID
:
4768 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
4769 ua_chan
, node
.node
) {
4770 health_code_update();
4771 ret
= consumer_clear_quiescent_channel(socket
,
4774 ERR("Error clearing quiescent state for consumer channel");
4780 case LTTNG_BUFFER_PER_UID
:
4787 health_code_update();
4790 pthread_mutex_unlock(&ua_sess
->lock
);
4794 health_code_update();
4799 * Clear quiescent state in each stream for all applications for a
4800 * specific UST session.
4801 * Called with UST session lock held.
4804 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
4809 DBG("Clearing stream quiescent state for all ust apps");
4813 switch (usess
->buffer_type
) {
4814 case LTTNG_BUFFER_PER_UID
:
4816 struct lttng_ht_iter iter
;
4817 struct buffer_reg_uid
*reg
;
4820 * Clear quiescent for all per UID buffers associated to
4823 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4824 struct consumer_socket
*socket
;
4825 struct buffer_reg_channel
*reg_chan
;
4827 /* Get associated consumer socket.*/
4828 socket
= consumer_find_socket_by_bitness(
4829 reg
->bits_per_long
, usess
->consumer
);
4832 * Ignore request if no consumer is found for
4838 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
4839 &iter
.iter
, reg_chan
, node
.node
) {
4841 * The following call will print error values so
4842 * the return code is of little importance
4843 * because whatever happens, we have to try them
4846 (void) consumer_clear_quiescent_channel(socket
,
4847 reg_chan
->consumer_key
);
4852 case LTTNG_BUFFER_PER_PID
:
4854 struct ust_app_session
*ua_sess
;
4855 struct lttng_ht_iter iter
;
4856 struct ust_app
*app
;
4858 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
4860 ua_sess
= lookup_session_by_app(usess
, app
);
4861 if (ua_sess
== NULL
) {
4864 (void) ust_app_clear_quiescent_app_session(app
,
4876 health_code_update();
4881 * Destroy a specific UST session in apps.
4883 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4886 struct ust_app_session
*ua_sess
;
4887 struct lttng_ht_iter iter
;
4888 struct lttng_ht_node_u64
*node
;
4890 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4894 if (!app
->compatible
) {
4898 __lookup_session_by_app(usess
, app
, &iter
);
4899 node
= lttng_ht_iter_get_node_u64(&iter
);
4901 /* Session is being or is deleted. */
4904 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4906 health_code_update();
4907 destroy_app_session(app
, ua_sess
);
4909 health_code_update();
4911 /* Quiescent wait after stopping trace */
4912 pthread_mutex_lock(&app
->sock_lock
);
4913 ret
= ustctl_wait_quiescent(app
->sock
);
4914 pthread_mutex_unlock(&app
->sock_lock
);
4915 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4916 ERR("UST app wait quiescent failed for app pid %d ret %d",
4921 health_code_update();
4926 * Start tracing for the UST session.
4928 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4930 struct lttng_ht_iter iter
;
4931 struct ust_app
*app
;
4933 DBG("Starting all UST traces");
4936 * Even though the start trace might fail, flag this session active so
4937 * other application coming in are started by default.
4944 * In a start-stop-start use-case, we need to clear the quiescent state
4945 * of each channel set by the prior stop command, thus ensuring that a
4946 * following stop or destroy is sure to grab a timestamp_end near those
4947 * operations, even if the packet is empty.
4949 (void) ust_app_clear_quiescent_session(usess
);
4951 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4952 ust_app_global_update(usess
, app
);
4961 * Start tracing for the UST session.
4962 * Called with UST session lock held.
4964 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4967 struct lttng_ht_iter iter
;
4968 struct ust_app
*app
;
4970 DBG("Stopping all UST traces");
4973 * Even though the stop trace might fail, flag this session inactive so
4974 * other application coming in are not started by default.
4980 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4981 ret
= ust_app_stop_trace(usess
, app
);
4983 /* Continue to next apps even on error */
4988 (void) ust_app_flush_session(usess
);
4996 * Destroy app UST session.
4998 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5001 struct lttng_ht_iter iter
;
5002 struct ust_app
*app
;
5004 DBG("Destroy all UST traces");
5008 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5009 ret
= destroy_trace(usess
, app
);
5011 /* Continue to next apps even on error */
5021 /* The ua_sess lock must be held by the caller. */
5023 int find_or_create_ust_app_channel(
5024 struct ltt_ust_session
*usess
,
5025 struct ust_app_session
*ua_sess
,
5026 struct ust_app
*app
,
5027 struct ltt_ust_channel
*uchan
,
5028 struct ust_app_channel
**ua_chan
)
5031 struct lttng_ht_iter iter
;
5032 struct lttng_ht_node_str
*ua_chan_node
;
5034 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5035 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5037 *ua_chan
= caa_container_of(ua_chan_node
,
5038 struct ust_app_channel
, node
);
5042 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5051 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5052 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5053 struct ust_app
*app
)
5056 struct ust_app_event
*ua_event
= NULL
;
5058 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5059 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5061 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5066 if (ua_event
->enabled
!= uevent
->enabled
) {
5067 ret
= uevent
->enabled
?
5068 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5069 disable_ust_app_event(ua_sess
, ua_event
, app
);
5078 * The caller must ensure that the application is compatible and is tracked
5079 * by the process attribute trackers.
5082 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5083 struct ust_app
*app
)
5086 struct cds_lfht_iter uchan_iter
;
5087 struct ltt_ust_channel
*uchan
;
5088 struct ust_app_session
*ua_sess
= NULL
;
5091 * The application's configuration should only be synchronized for
5094 assert(usess
->active
);
5096 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5098 /* Tracer is probably gone or ENOMEM. */
5103 pthread_mutex_lock(&ua_sess
->lock
);
5104 if (ua_sess
->deleted
) {
5105 pthread_mutex_unlock(&ua_sess
->lock
);
5111 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5113 struct ust_app_channel
*ua_chan
;
5114 struct cds_lfht_iter uevent_iter
;
5115 struct ltt_ust_event
*uevent
;
5118 * Search for a matching ust_app_channel. If none is found,
5119 * create it. Creating the channel will cause the ua_chan
5120 * structure to be allocated, the channel buffers to be
5121 * allocated (if necessary) and sent to the application, and
5122 * all enabled contexts will be added to the channel.
5124 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5125 app
, uchan
, &ua_chan
);
5127 /* Tracer is probably gone or ENOMEM. */
5132 /* ua_chan will be NULL for the metadata channel */
5136 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5138 ret
= ust_app_channel_synchronize_event(ua_chan
,
5139 uevent
, ua_sess
, app
);
5145 if (ua_chan
->enabled
!= uchan
->enabled
) {
5146 ret
= uchan
->enabled
?
5147 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5148 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5156 * Create the metadata for the application. This returns gracefully if a
5157 * metadata was already set for the session.
5159 * The metadata channel must be created after the data channels as the
5160 * consumer daemon assumes this ordering. When interacting with a relay
5161 * daemon, the consumer will use this assumption to send the
5162 * "STREAMS_SENT" message to the relay daemon.
5164 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5172 pthread_mutex_unlock(&ua_sess
->lock
);
5173 /* Everything went well at this point. */
5178 pthread_mutex_unlock(&ua_sess
->lock
);
5181 destroy_app_session(app
, ua_sess
);
5187 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5189 struct ust_app_session
*ua_sess
;
5191 ua_sess
= lookup_session_by_app(usess
, app
);
5192 if (ua_sess
== NULL
) {
5195 destroy_app_session(app
, ua_sess
);
5199 * Add channels/events from UST global domain to registered apps at sock.
5201 * Called with session lock held.
5202 * Called with RCU read-side lock held.
5204 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5207 assert(usess
->active
);
5209 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5210 app
->sock
, usess
->id
);
5212 if (!app
->compatible
) {
5215 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5217 trace_ust_id_tracker_lookup(
5218 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5220 trace_ust_id_tracker_lookup(
5221 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5224 * Synchronize the application's internal tracing configuration
5225 * and start tracing.
5227 ust_app_synchronize(usess
, app
);
5228 ust_app_start_trace(usess
, app
);
5230 ust_app_global_destroy(usess
, app
);
5235 * Called with session lock held.
5237 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5239 struct lttng_ht_iter iter
;
5240 struct ust_app
*app
;
5243 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5244 ust_app_global_update(usess
, app
);
5250 * Add context to a specific channel for global UST domain.
5252 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5253 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5256 struct lttng_ht_node_str
*ua_chan_node
;
5257 struct lttng_ht_iter iter
, uiter
;
5258 struct ust_app_channel
*ua_chan
= NULL
;
5259 struct ust_app_session
*ua_sess
;
5260 struct ust_app
*app
;
5262 assert(usess
->active
);
5265 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5266 if (!app
->compatible
) {
5268 * TODO: In time, we should notice the caller of this error by
5269 * telling him that this is a version error.
5273 ua_sess
= lookup_session_by_app(usess
, app
);
5274 if (ua_sess
== NULL
) {
5278 pthread_mutex_lock(&ua_sess
->lock
);
5280 if (ua_sess
->deleted
) {
5281 pthread_mutex_unlock(&ua_sess
->lock
);
5285 /* Lookup channel in the ust app session */
5286 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5287 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5288 if (ua_chan_node
== NULL
) {
5291 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5293 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5298 pthread_mutex_unlock(&ua_sess
->lock
);
5306 * Receive registration and populate the given msg structure.
5308 * On success return 0 else a negative value returned by the ustctl call.
5310 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5313 uint32_t pid
, ppid
, uid
, gid
;
5317 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5318 &pid
, &ppid
, &uid
, &gid
,
5319 &msg
->bits_per_long
,
5320 &msg
->uint8_t_alignment
,
5321 &msg
->uint16_t_alignment
,
5322 &msg
->uint32_t_alignment
,
5323 &msg
->uint64_t_alignment
,
5324 &msg
->long_alignment
,
5331 case LTTNG_UST_ERR_EXITING
:
5332 DBG3("UST app recv reg message failed. Application died");
5334 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5335 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5336 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5337 LTTNG_UST_ABI_MINOR_VERSION
);
5340 ERR("UST app recv reg message failed with ret %d", ret
);
5345 msg
->pid
= (pid_t
) pid
;
5346 msg
->ppid
= (pid_t
) ppid
;
5347 msg
->uid
= (uid_t
) uid
;
5348 msg
->gid
= (gid_t
) gid
;
5355 * Return a ust app session object using the application object and the
5356 * session object descriptor has a key. If not found, NULL is returned.
5357 * A RCU read side lock MUST be acquired when calling this function.
5359 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5362 struct lttng_ht_node_ulong
*node
;
5363 struct lttng_ht_iter iter
;
5364 struct ust_app_session
*ua_sess
= NULL
;
5368 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5369 node
= lttng_ht_iter_get_node_ulong(&iter
);
5371 DBG2("UST app session find by objd %d not found", objd
);
5375 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5382 * Return a ust app channel object using the application object and the channel
5383 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5384 * lock MUST be acquired before calling this function.
5386 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5389 struct lttng_ht_node_ulong
*node
;
5390 struct lttng_ht_iter iter
;
5391 struct ust_app_channel
*ua_chan
= NULL
;
5395 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5396 node
= lttng_ht_iter_get_node_ulong(&iter
);
5398 DBG2("UST app channel find by objd %d not found", objd
);
5402 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5409 * Reply to a register channel notification from an application on the notify
5410 * socket. The channel metadata is also created.
5412 * The session UST registry lock is acquired in this function.
5414 * On success 0 is returned else a negative value.
5416 static int reply_ust_register_channel(int sock
, int cobjd
,
5417 size_t nr_fields
, struct ustctl_field
*fields
)
5419 int ret
, ret_code
= 0;
5421 uint64_t chan_reg_key
;
5422 enum ustctl_channel_header type
;
5423 struct ust_app
*app
;
5424 struct ust_app_channel
*ua_chan
;
5425 struct ust_app_session
*ua_sess
;
5426 struct ust_registry_session
*registry
;
5427 struct ust_registry_channel
*chan_reg
;
5431 /* Lookup application. If not found, there is a code flow error. */
5432 app
= find_app_by_notify_sock(sock
);
5434 DBG("Application socket %d is being torn down. Abort event notify",
5437 goto error_rcu_unlock
;
5440 /* Lookup channel by UST object descriptor. */
5441 ua_chan
= find_channel_by_objd(app
, cobjd
);
5443 DBG("Application channel is being torn down. Abort event notify");
5445 goto error_rcu_unlock
;
5448 assert(ua_chan
->session
);
5449 ua_sess
= ua_chan
->session
;
5451 /* Get right session registry depending on the session buffer type. */
5452 registry
= get_session_registry(ua_sess
);
5454 DBG("Application session is being torn down. Abort event notify");
5456 goto error_rcu_unlock
;
5459 /* Depending on the buffer type, a different channel key is used. */
5460 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5461 chan_reg_key
= ua_chan
->tracing_channel_id
;
5463 chan_reg_key
= ua_chan
->key
;
5466 pthread_mutex_lock(®istry
->lock
);
5468 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5471 if (!chan_reg
->register_done
) {
5473 * TODO: eventually use the registry event count for
5474 * this channel to better guess header type for per-pid
5477 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5478 chan_reg
->nr_ctx_fields
= nr_fields
;
5479 chan_reg
->ctx_fields
= fields
;
5481 chan_reg
->header_type
= type
;
5483 /* Get current already assigned values. */
5484 type
= chan_reg
->header_type
;
5486 /* Channel id is set during the object creation. */
5487 chan_id
= chan_reg
->chan_id
;
5489 /* Append to metadata */
5490 if (!chan_reg
->metadata_dumped
) {
5491 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5493 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5499 DBG3("UST app replying to register channel key %" PRIu64
5500 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5503 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5505 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5506 ERR("UST app reply channel failed with ret %d", ret
);
5508 DBG3("UST app reply channel failed. Application died");
5513 /* This channel registry registration is completed. */
5514 chan_reg
->register_done
= 1;
5517 pthread_mutex_unlock(®istry
->lock
);
5525 * Add event to the UST channel registry. When the event is added to the
5526 * registry, the metadata is also created. Once done, this replies to the
5527 * application with the appropriate error code.
5529 * The session UST registry lock is acquired in the function.
5531 * On success 0 is returned else a negative value.
5533 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5534 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5535 int loglevel_value
, char *model_emf_uri
)
5538 uint32_t event_id
= 0;
5539 uint64_t chan_reg_key
;
5540 struct ust_app
*app
;
5541 struct ust_app_channel
*ua_chan
;
5542 struct ust_app_session
*ua_sess
;
5543 struct ust_registry_session
*registry
;
5547 /* Lookup application. If not found, there is a code flow error. */
5548 app
= find_app_by_notify_sock(sock
);
5550 DBG("Application socket %d is being torn down. Abort event notify",
5553 goto error_rcu_unlock
;
5556 /* Lookup channel by UST object descriptor. */
5557 ua_chan
= find_channel_by_objd(app
, cobjd
);
5559 DBG("Application channel is being torn down. Abort event notify");
5561 goto error_rcu_unlock
;
5564 assert(ua_chan
->session
);
5565 ua_sess
= ua_chan
->session
;
5567 registry
= get_session_registry(ua_sess
);
5569 DBG("Application session is being torn down. Abort event notify");
5571 goto error_rcu_unlock
;
5574 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5575 chan_reg_key
= ua_chan
->tracing_channel_id
;
5577 chan_reg_key
= ua_chan
->key
;
5580 pthread_mutex_lock(®istry
->lock
);
5583 * From this point on, this call acquires the ownership of the sig, fields
5584 * and model_emf_uri meaning any free are done inside it if needed. These
5585 * three variables MUST NOT be read/write after this.
5587 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5588 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5589 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5593 model_emf_uri
= NULL
;
5596 * The return value is returned to ustctl so in case of an error, the
5597 * application can be notified. In case of an error, it's important not to
5598 * return a negative error or else the application will get closed.
5600 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5602 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5603 ERR("UST app reply event failed with ret %d", ret
);
5605 DBG3("UST app reply event failed. Application died");
5608 * No need to wipe the create event since the application socket will
5609 * get close on error hence cleaning up everything by itself.
5614 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5618 pthread_mutex_unlock(®istry
->lock
);
5623 free(model_emf_uri
);
5628 * Add enum to the UST session registry. Once done, this replies to the
5629 * application with the appropriate error code.
5631 * The session UST registry lock is acquired within this function.
5633 * On success 0 is returned else a negative value.
5635 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
5636 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
5638 int ret
= 0, ret_code
;
5639 struct ust_app
*app
;
5640 struct ust_app_session
*ua_sess
;
5641 struct ust_registry_session
*registry
;
5642 uint64_t enum_id
= -1ULL;
5646 /* Lookup application. If not found, there is a code flow error. */
5647 app
= find_app_by_notify_sock(sock
);
5649 /* Return an error since this is not an error */
5650 DBG("Application socket %d is being torn down. Aborting enum registration",
5653 goto error_rcu_unlock
;
5656 /* Lookup session by UST object descriptor. */
5657 ua_sess
= find_session_by_objd(app
, sobjd
);
5659 /* Return an error since this is not an error */
5660 DBG("Application session is being torn down (session not found). Aborting enum registration.");
5662 goto error_rcu_unlock
;
5665 registry
= get_session_registry(ua_sess
);
5667 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
5669 goto error_rcu_unlock
;
5672 pthread_mutex_lock(®istry
->lock
);
5675 * From this point on, the callee acquires the ownership of
5676 * entries. The variable entries MUST NOT be read/written after
5679 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
5680 entries
, nr_entries
, &enum_id
);
5684 * The return value is returned to ustctl so in case of an error, the
5685 * application can be notified. In case of an error, it's important not to
5686 * return a negative error or else the application will get closed.
5688 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
5690 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5691 ERR("UST app reply enum failed with ret %d", ret
);
5693 DBG3("UST app reply enum failed. Application died");
5696 * No need to wipe the create enum since the application socket will
5697 * get close on error hence cleaning up everything by itself.
5702 DBG3("UST registry enum %s added successfully or already found", name
);
5705 pthread_mutex_unlock(®istry
->lock
);
5712 * Handle application notification through the given notify socket.
5714 * Return 0 on success or else a negative value.
5716 int ust_app_recv_notify(int sock
)
5719 enum ustctl_notify_cmd cmd
;
5721 DBG3("UST app receiving notify from sock %d", sock
);
5723 ret
= ustctl_recv_notify(sock
, &cmd
);
5725 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5726 ERR("UST app recv notify failed with ret %d", ret
);
5728 DBG3("UST app recv notify failed. Application died");
5734 case USTCTL_NOTIFY_CMD_EVENT
:
5736 int sobjd
, cobjd
, loglevel_value
;
5737 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5739 struct ustctl_field
*fields
;
5741 DBG2("UST app ustctl register event received");
5743 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5744 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5747 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5748 ERR("UST app recv event failed with ret %d", ret
);
5750 DBG3("UST app recv event failed. Application died");
5756 * Add event to the UST registry coming from the notify socket. This
5757 * call will free if needed the sig, fields and model_emf_uri. This
5758 * code path loses the ownsership of these variables and transfer them
5759 * to the this function.
5761 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5762 fields
, loglevel_value
, model_emf_uri
);
5769 case USTCTL_NOTIFY_CMD_CHANNEL
:
5773 struct ustctl_field
*fields
;
5775 DBG2("UST app ustctl register channel received");
5777 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5780 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5781 ERR("UST app recv channel failed with ret %d", ret
);
5783 DBG3("UST app recv channel failed. Application died");
5789 * The fields ownership are transfered to this function call meaning
5790 * that if needed it will be freed. After this, it's invalid to access
5791 * fields or clean it up.
5793 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
5801 case USTCTL_NOTIFY_CMD_ENUM
:
5804 char name
[LTTNG_UST_SYM_NAME_LEN
];
5806 struct ustctl_enum_entry
*entries
;
5808 DBG2("UST app ustctl register enum received");
5810 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
5811 &entries
, &nr_entries
);
5813 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5814 ERR("UST app recv enum failed with ret %d", ret
);
5816 DBG3("UST app recv enum failed. Application died");
5821 /* Callee assumes ownership of entries */
5822 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
5823 entries
, nr_entries
);
5831 /* Should NEVER happen. */
5840 * Once the notify socket hangs up, this is called. First, it tries to find the
5841 * corresponding application. On failure, the call_rcu to close the socket is
5842 * executed. If an application is found, it tries to delete it from the notify
5843 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5845 * Note that an object needs to be allocated here so on ENOMEM failure, the
5846 * call RCU is not done but the rest of the cleanup is.
5848 void ust_app_notify_sock_unregister(int sock
)
5851 struct lttng_ht_iter iter
;
5852 struct ust_app
*app
;
5853 struct ust_app_notify_sock_obj
*obj
;
5859 obj
= zmalloc(sizeof(*obj
));
5862 * An ENOMEM is kind of uncool. If this strikes we continue the
5863 * procedure but the call_rcu will not be called. In this case, we
5864 * accept the fd leak rather than possibly creating an unsynchronized
5865 * state between threads.
5867 * TODO: The notify object should be created once the notify socket is
5868 * registered and stored independantely from the ust app object. The
5869 * tricky part is to synchronize the teardown of the application and
5870 * this notify object. Let's keep that in mind so we can avoid this
5871 * kind of shenanigans with ENOMEM in the teardown path.
5878 DBG("UST app notify socket unregister %d", sock
);
5881 * Lookup application by notify socket. If this fails, this means that the
5882 * hash table delete has already been done by the application
5883 * unregistration process so we can safely close the notify socket in a
5886 app
= find_app_by_notify_sock(sock
);
5891 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5894 * Whatever happens here either we fail or succeed, in both cases we have
5895 * to close the socket after a grace period to continue to the call RCU
5896 * here. If the deletion is successful, the application is not visible
5897 * anymore by other threads and is it fails it means that it was already
5898 * deleted from the hash table so either way we just have to close the
5901 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5907 * Close socket after a grace period to avoid for the socket to be reused
5908 * before the application object is freed creating potential race between
5909 * threads trying to add unique in the global hash table.
5912 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5917 * Destroy a ust app data structure and free its memory.
5919 void ust_app_destroy(struct ust_app
*app
)
5925 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5929 * Take a snapshot for a given UST session. The snapshot is sent to the given
5932 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5934 enum lttng_error_code
ust_app_snapshot_record(
5935 const struct ltt_ust_session
*usess
,
5936 const struct consumer_output
*output
, int wait
,
5937 uint64_t nb_packets_per_stream
)
5940 enum lttng_error_code status
= LTTNG_OK
;
5941 struct lttng_ht_iter iter
;
5942 struct ust_app
*app
;
5943 char *trace_path
= NULL
;
5950 switch (usess
->buffer_type
) {
5951 case LTTNG_BUFFER_PER_UID
:
5953 struct buffer_reg_uid
*reg
;
5955 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5956 struct buffer_reg_channel
*reg_chan
;
5957 struct consumer_socket
*socket
;
5958 char pathname
[PATH_MAX
];
5959 size_t consumer_path_offset
= 0;
5961 if (!reg
->registry
->reg
.ust
->metadata_key
) {
5962 /* Skip since no metadata is present */
5966 /* Get consumer socket to use to push the metadata.*/
5967 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5970 status
= LTTNG_ERR_INVALID
;
5974 memset(pathname
, 0, sizeof(pathname
));
5975 ret
= snprintf(pathname
, sizeof(pathname
),
5976 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5977 reg
->uid
, reg
->bits_per_long
);
5979 PERROR("snprintf snapshot path");
5980 status
= LTTNG_ERR_INVALID
;
5983 /* Free path allowed on previous iteration. */
5985 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
5986 &consumer_path_offset
);
5988 status
= LTTNG_ERR_INVALID
;
5991 /* Add the UST default trace dir to path. */
5992 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5993 reg_chan
, node
.node
) {
5994 status
= consumer_snapshot_channel(socket
,
5995 reg_chan
->consumer_key
,
5996 output
, 0, usess
->uid
,
5997 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
5998 nb_packets_per_stream
);
5999 if (status
!= LTTNG_OK
) {
6003 status
= consumer_snapshot_channel(socket
,
6004 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6005 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6007 if (status
!= LTTNG_OK
) {
6013 case LTTNG_BUFFER_PER_PID
:
6015 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6016 struct consumer_socket
*socket
;
6017 struct lttng_ht_iter chan_iter
;
6018 struct ust_app_channel
*ua_chan
;
6019 struct ust_app_session
*ua_sess
;
6020 struct ust_registry_session
*registry
;
6021 char pathname
[PATH_MAX
];
6022 size_t consumer_path_offset
= 0;
6024 ua_sess
= lookup_session_by_app(usess
, app
);
6026 /* Session not associated with this app. */
6030 /* Get the right consumer socket for the application. */
6031 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6034 status
= LTTNG_ERR_INVALID
;
6038 /* Add the UST default trace dir to path. */
6039 memset(pathname
, 0, sizeof(pathname
));
6040 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
6043 status
= LTTNG_ERR_INVALID
;
6044 PERROR("snprintf snapshot path");
6047 /* Free path allowed on previous iteration. */
6049 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6050 &consumer_path_offset
);
6052 status
= LTTNG_ERR_INVALID
;
6055 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6056 ua_chan
, node
.node
) {
6057 status
= consumer_snapshot_channel(socket
,
6058 ua_chan
->key
, output
, 0,
6059 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6060 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6061 &trace_path
[consumer_path_offset
], wait
,
6062 nb_packets_per_stream
);
6066 case LTTNG_ERR_CHAN_NOT_FOUND
:
6073 registry
= get_session_registry(ua_sess
);
6075 DBG("Application session is being torn down. Skip application.");
6078 status
= consumer_snapshot_channel(socket
,
6079 registry
->metadata_key
, output
, 1,
6080 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6081 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6082 &trace_path
[consumer_path_offset
], wait
, 0);
6086 case LTTNG_ERR_CHAN_NOT_FOUND
:
6106 * Return the size taken by one more packet per stream.
6108 uint64_t ust_app_get_size_one_more_packet_per_stream(
6109 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6111 uint64_t tot_size
= 0;
6112 struct ust_app
*app
;
6113 struct lttng_ht_iter iter
;
6117 switch (usess
->buffer_type
) {
6118 case LTTNG_BUFFER_PER_UID
:
6120 struct buffer_reg_uid
*reg
;
6122 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6123 struct buffer_reg_channel
*reg_chan
;
6126 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6127 reg_chan
, node
.node
) {
6128 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6130 * Don't take channel into account if we
6131 * already grab all its packets.
6135 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6141 case LTTNG_BUFFER_PER_PID
:
6144 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6145 struct ust_app_channel
*ua_chan
;
6146 struct ust_app_session
*ua_sess
;
6147 struct lttng_ht_iter chan_iter
;
6149 ua_sess
= lookup_session_by_app(usess
, app
);
6151 /* Session not associated with this app. */
6155 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6156 ua_chan
, node
.node
) {
6157 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6159 * Don't take channel into account if we
6160 * already grab all its packets.
6164 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6178 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6179 struct cds_list_head
*buffer_reg_uid_list
,
6180 struct consumer_output
*consumer
, uint64_t uchan_id
,
6181 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6184 uint64_t consumer_chan_key
;
6189 ret
= buffer_reg_uid_consumer_channel_key(
6190 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6198 ret
= consumer_get_lost_packets(ust_session_id
,
6199 consumer_chan_key
, consumer
, lost
);
6201 ret
= consumer_get_discarded_events(ust_session_id
,
6202 consumer_chan_key
, consumer
, discarded
);
6209 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6210 struct ltt_ust_channel
*uchan
,
6211 struct consumer_output
*consumer
, int overwrite
,
6212 uint64_t *discarded
, uint64_t *lost
)
6215 struct lttng_ht_iter iter
;
6216 struct lttng_ht_node_str
*ua_chan_node
;
6217 struct ust_app
*app
;
6218 struct ust_app_session
*ua_sess
;
6219 struct ust_app_channel
*ua_chan
;
6226 * Iterate over every registered applications. Sum counters for
6227 * all applications containing requested session and channel.
6229 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6230 struct lttng_ht_iter uiter
;
6232 ua_sess
= lookup_session_by_app(usess
, app
);
6233 if (ua_sess
== NULL
) {
6238 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6239 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6240 /* If the session is found for the app, the channel must be there */
6241 assert(ua_chan_node
);
6243 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6248 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6255 uint64_t _discarded
;
6257 ret
= consumer_get_discarded_events(usess
->id
,
6258 ua_chan
->key
, consumer
, &_discarded
);
6262 (*discarded
) += _discarded
;
6271 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6272 struct ust_app
*app
)
6275 struct ust_app_session
*ua_sess
;
6277 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6281 ua_sess
= lookup_session_by_app(usess
, app
);
6282 if (ua_sess
== NULL
) {
6283 /* The session is in teardown process. Ignore and continue. */
6287 pthread_mutex_lock(&ua_sess
->lock
);
6289 if (ua_sess
->deleted
) {
6293 pthread_mutex_lock(&app
->sock_lock
);
6294 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6295 pthread_mutex_unlock(&app
->sock_lock
);
6298 pthread_mutex_unlock(&ua_sess
->lock
);
6302 health_code_update();
6307 * Regenerate the statedump for each app in the session.
6309 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6312 struct lttng_ht_iter iter
;
6313 struct ust_app
*app
;
6315 DBG("Regenerating the metadata for all UST apps");
6319 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6320 if (!app
->compatible
) {
6324 ret
= ust_app_regenerate_statedump(usess
, app
);
6326 /* Continue to the next app even on error */
6337 * Rotate all the channels of a session.
6339 * Return LTTNG_OK on success or else an LTTng error code.
6341 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
6344 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6345 struct lttng_ht_iter iter
;
6346 struct ust_app
*app
;
6347 struct ltt_ust_session
*usess
= session
->ust_session
;
6353 switch (usess
->buffer_type
) {
6354 case LTTNG_BUFFER_PER_UID
:
6356 struct buffer_reg_uid
*reg
;
6358 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6359 struct buffer_reg_channel
*reg_chan
;
6360 struct consumer_socket
*socket
;
6362 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6363 /* Skip since no metadata is present */
6367 /* Get consumer socket to use to push the metadata.*/
6368 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6371 cmd_ret
= LTTNG_ERR_INVALID
;
6375 /* Rotate the data channels. */
6376 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6377 reg_chan
, node
.node
) {
6378 ret
= consumer_rotate_channel(socket
,
6379 reg_chan
->consumer_key
,
6380 usess
->uid
, usess
->gid
,
6382 /* is_metadata_channel */ false);
6384 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6389 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6391 ret
= consumer_rotate_channel(socket
,
6392 reg
->registry
->reg
.ust
->metadata_key
,
6393 usess
->uid
, usess
->gid
,
6395 /* is_metadata_channel */ true);
6397 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6403 case LTTNG_BUFFER_PER_PID
:
6405 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6406 struct consumer_socket
*socket
;
6407 struct lttng_ht_iter chan_iter
;
6408 struct ust_app_channel
*ua_chan
;
6409 struct ust_app_session
*ua_sess
;
6410 struct ust_registry_session
*registry
;
6412 ua_sess
= lookup_session_by_app(usess
, app
);
6414 /* Session not associated with this app. */
6418 /* Get the right consumer socket for the application. */
6419 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6422 cmd_ret
= LTTNG_ERR_INVALID
;
6426 registry
= get_session_registry(ua_sess
);
6428 DBG("Application session is being torn down. Skip application.");
6432 /* Rotate the data channels. */
6433 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6434 ua_chan
, node
.node
) {
6435 ret
= consumer_rotate_channel(socket
,
6437 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6438 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6440 /* is_metadata_channel */ false);
6442 /* Per-PID buffer and application going away. */
6443 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6445 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6450 /* Rotate the metadata channel. */
6451 (void) push_metadata(registry
, usess
->consumer
);
6452 ret
= consumer_rotate_channel(socket
,
6453 registry
->metadata_key
,
6454 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6455 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6457 /* is_metadata_channel */ true);
6459 /* Per-PID buffer and application going away. */
6460 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6462 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6480 enum lttng_error_code
ust_app_create_channel_subdirectories(
6481 const struct ltt_ust_session
*usess
)
6483 enum lttng_error_code ret
= LTTNG_OK
;
6484 struct lttng_ht_iter iter
;
6485 enum lttng_trace_chunk_status chunk_status
;
6486 char *pathname_index
;
6489 assert(usess
->current_trace_chunk
);
6492 switch (usess
->buffer_type
) {
6493 case LTTNG_BUFFER_PER_UID
:
6495 struct buffer_reg_uid
*reg
;
6497 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6498 fmt_ret
= asprintf(&pathname_index
,
6499 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
6500 reg
->uid
, reg
->bits_per_long
);
6502 ERR("Failed to format channel index directory");
6503 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6508 * Create the index subdirectory which will take care
6509 * of implicitly creating the channel's path.
6511 chunk_status
= lttng_trace_chunk_create_subdirectory(
6512 usess
->current_trace_chunk
,
6514 free(pathname_index
);
6515 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6516 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6522 case LTTNG_BUFFER_PER_PID
:
6524 struct ust_app
*app
;
6527 * Create the toplevel ust/ directory in case no apps are running.
6529 chunk_status
= lttng_trace_chunk_create_subdirectory(
6530 usess
->current_trace_chunk
,
6531 DEFAULT_UST_TRACE_DIR
);
6532 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6533 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6537 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
6539 struct ust_app_session
*ua_sess
;
6540 struct ust_registry_session
*registry
;
6542 ua_sess
= lookup_session_by_app(usess
, app
);
6544 /* Session not associated with this app. */
6548 registry
= get_session_registry(ua_sess
);
6550 DBG("Application session is being torn down. Skip application.");
6554 fmt_ret
= asprintf(&pathname_index
,
6555 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
6558 ERR("Failed to format channel index directory");
6559 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6563 * Create the index subdirectory which will take care
6564 * of implicitly creating the channel's path.
6566 chunk_status
= lttng_trace_chunk_create_subdirectory(
6567 usess
->current_trace_chunk
,
6569 free(pathname_index
);
6570 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6571 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6588 * Clear all the channels of a session.
6590 * Return LTTNG_OK on success or else an LTTng error code.
6592 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
6595 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6596 struct lttng_ht_iter iter
;
6597 struct ust_app
*app
;
6598 struct ltt_ust_session
*usess
= session
->ust_session
;
6604 if (usess
->active
) {
6605 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
6606 cmd_ret
= LTTNG_ERR_FATAL
;
6610 switch (usess
->buffer_type
) {
6611 case LTTNG_BUFFER_PER_UID
:
6613 struct buffer_reg_uid
*reg
;
6615 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6616 struct buffer_reg_channel
*reg_chan
;
6617 struct consumer_socket
*socket
;
6619 /* Get consumer socket to use to push the metadata.*/
6620 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6623 cmd_ret
= LTTNG_ERR_INVALID
;
6627 /* Clear the data channels. */
6628 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6629 reg_chan
, node
.node
) {
6630 ret
= consumer_clear_channel(socket
,
6631 reg_chan
->consumer_key
);
6637 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6640 * Clear the metadata channel.
6641 * Metadata channel is not cleared per se but we still need to
6642 * perform a rotation operation on it behind the scene.
6644 ret
= consumer_clear_channel(socket
,
6645 reg
->registry
->reg
.ust
->metadata_key
);
6652 case LTTNG_BUFFER_PER_PID
:
6654 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6655 struct consumer_socket
*socket
;
6656 struct lttng_ht_iter chan_iter
;
6657 struct ust_app_channel
*ua_chan
;
6658 struct ust_app_session
*ua_sess
;
6659 struct ust_registry_session
*registry
;
6661 ua_sess
= lookup_session_by_app(usess
, app
);
6663 /* Session not associated with this app. */
6667 /* Get the right consumer socket for the application. */
6668 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6671 cmd_ret
= LTTNG_ERR_INVALID
;
6675 registry
= get_session_registry(ua_sess
);
6677 DBG("Application session is being torn down. Skip application.");
6681 /* Clear the data channels. */
6682 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6683 ua_chan
, node
.node
) {
6684 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
6686 /* Per-PID buffer and application going away. */
6687 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6694 (void) push_metadata(registry
, usess
->consumer
);
6697 * Clear the metadata channel.
6698 * Metadata channel is not cleared per se but we still need to
6699 * perform rotation operation on it behind the scene.
6701 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
6703 /* Per-PID buffer and application going away. */
6704 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6722 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
6723 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
6726 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
6736 * This function skips the metadata channel as the begin/end timestamps of a
6737 * metadata packet are useless.
6739 * Moreover, opening a packet after a "clear" will cause problems for live
6740 * sessions as it will introduce padding that was not part of the first trace
6741 * chunk. The relay daemon expects the content of the metadata stream of
6742 * successive metadata trace chunks to be strict supersets of one another.
6744 * For example, flushing a packet at the beginning of the metadata stream of
6745 * a trace chunk resulting from a "clear" session command will cause the
6746 * size of the metadata stream of the new trace chunk to not match the size of
6747 * the metadata stream of the original chunk. This will confuse the relay
6748 * daemon as the same "offset" in a metadata stream will no longer point
6749 * to the same content.
6751 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
6753 enum lttng_error_code ret
= LTTNG_OK
;
6754 struct lttng_ht_iter iter
;
6755 struct ltt_ust_session
*usess
= session
->ust_session
;
6761 switch (usess
->buffer_type
) {
6762 case LTTNG_BUFFER_PER_UID
:
6764 struct buffer_reg_uid
*reg
;
6766 cds_list_for_each_entry (
6767 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6768 struct buffer_reg_channel
*reg_chan
;
6769 struct consumer_socket
*socket
;
6771 socket
= consumer_find_socket_by_bitness(
6772 reg
->bits_per_long
, usess
->consumer
);
6774 ret
= LTTNG_ERR_FATAL
;
6778 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
6779 &iter
.iter
, reg_chan
, node
.node
) {
6780 const int open_ret
=
6781 consumer_open_channel_packets(
6783 reg_chan
->consumer_key
);
6786 ret
= LTTNG_ERR_UNK
;
6793 case LTTNG_BUFFER_PER_PID
:
6795 struct ust_app
*app
;
6797 cds_lfht_for_each_entry (
6798 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6799 struct consumer_socket
*socket
;
6800 struct lttng_ht_iter chan_iter
;
6801 struct ust_app_channel
*ua_chan
;
6802 struct ust_app_session
*ua_sess
;
6803 struct ust_registry_session
*registry
;
6805 ua_sess
= lookup_session_by_app(usess
, app
);
6807 /* Session not associated with this app. */
6811 /* Get the right consumer socket for the application. */
6812 socket
= consumer_find_socket_by_bitness(
6813 app
->bits_per_long
, usess
->consumer
);
6815 ret
= LTTNG_ERR_FATAL
;
6819 registry
= get_session_registry(ua_sess
);
6821 DBG("Application session is being torn down. Skip application.");
6825 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
6826 &chan_iter
.iter
, ua_chan
, node
.node
) {
6827 const int open_ret
=
6828 consumer_open_channel_packets(
6834 * Per-PID buffer and application going
6837 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6841 ret
= LTTNG_ERR_UNK
;