2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
17 #include <sys/types.h>
19 #include <urcu/compiler.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 #include "ust-field-utils.h"
40 struct lttng_ht
*ust_app_ht
;
41 struct lttng_ht
*ust_app_ht_by_sock
;
42 struct lttng_ht
*ust_app_ht_by_notify_sock
;
45 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
47 /* Next available channel key. Access under next_channel_key_lock. */
48 static uint64_t _next_channel_key
;
49 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
51 /* Next available session ID. Access under next_session_id_lock. */
52 static uint64_t _next_session_id
;
53 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
56 * Return the incremented value of next_channel_key.
58 static uint64_t get_next_channel_key(void)
62 pthread_mutex_lock(&next_channel_key_lock
);
63 ret
= ++_next_channel_key
;
64 pthread_mutex_unlock(&next_channel_key_lock
);
69 * Return the atomically incremented value of next_session_id.
71 static uint64_t get_next_session_id(void)
75 pthread_mutex_lock(&next_session_id_lock
);
76 ret
= ++_next_session_id
;
77 pthread_mutex_unlock(&next_session_id_lock
);
81 static void copy_channel_attr_to_ustctl(
82 struct ustctl_consumer_channel_attr
*attr
,
83 struct lttng_ust_channel_attr
*uattr
)
85 /* Copy event attributes since the layout is different. */
86 attr
->subbuf_size
= uattr
->subbuf_size
;
87 attr
->num_subbuf
= uattr
->num_subbuf
;
88 attr
->overwrite
= uattr
->overwrite
;
89 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
90 attr
->read_timer_interval
= uattr
->read_timer_interval
;
91 attr
->output
= uattr
->output
;
92 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
96 * Match function for the hash table lookup.
98 * It matches an ust app event based on three attributes which are the event
99 * name, the filter bytecode and the loglevel.
101 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
103 struct ust_app_event
*event
;
104 const struct ust_app_ht_key
*key
;
105 int ev_loglevel_value
;
110 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
112 ev_loglevel_value
= event
->attr
.loglevel
;
114 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
117 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
121 /* Event loglevel. */
122 if (ev_loglevel_value
!= key
->loglevel_type
) {
123 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
124 && key
->loglevel_type
== 0 &&
125 ev_loglevel_value
== -1) {
127 * Match is accepted. This is because on event creation, the
128 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
129 * -1 are accepted for this loglevel type since 0 is the one set by
130 * the API when receiving an enable event.
137 /* One of the filters is NULL, fail. */
138 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
142 if (key
->filter
&& event
->filter
) {
143 /* Both filters exists, check length followed by the bytecode. */
144 if (event
->filter
->len
!= key
->filter
->len
||
145 memcmp(event
->filter
->data
, key
->filter
->data
,
146 event
->filter
->len
) != 0) {
151 /* One of the exclusions is NULL, fail. */
152 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
156 if (key
->exclusion
&& event
->exclusion
) {
157 /* Both exclusions exists, check count followed by the names. */
158 if (event
->exclusion
->count
!= key
->exclusion
->count
||
159 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
160 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
174 * Unique add of an ust app event in the given ht. This uses the custom
175 * ht_match_ust_app_event match function and the event name as hash.
177 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
178 struct ust_app_event
*event
)
180 struct cds_lfht_node
*node_ptr
;
181 struct ust_app_ht_key key
;
185 assert(ua_chan
->events
);
188 ht
= ua_chan
->events
;
189 key
.name
= event
->attr
.name
;
190 key
.filter
= event
->filter
;
191 key
.loglevel_type
= event
->attr
.loglevel
;
192 key
.exclusion
= event
->exclusion
;
194 node_ptr
= cds_lfht_add_unique(ht
->ht
,
195 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
196 ht_match_ust_app_event
, &key
, &event
->node
.node
);
197 assert(node_ptr
== &event
->node
.node
);
201 * Close the notify socket from the given RCU head object. This MUST be called
202 * through a call_rcu().
204 static void close_notify_sock_rcu(struct rcu_head
*head
)
207 struct ust_app_notify_sock_obj
*obj
=
208 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
210 /* Must have a valid fd here. */
211 assert(obj
->fd
>= 0);
213 ret
= close(obj
->fd
);
215 ERR("close notify sock %d RCU", obj
->fd
);
217 lttng_fd_put(LTTNG_FD_APPS
, 1);
223 * Return the session registry according to the buffer type of the given
226 * A registry per UID object MUST exists before calling this function or else
227 * it assert() if not found. RCU read side lock must be acquired.
229 static struct ust_registry_session
*get_session_registry(
230 struct ust_app_session
*ua_sess
)
232 struct ust_registry_session
*registry
= NULL
;
236 switch (ua_sess
->buffer_type
) {
237 case LTTNG_BUFFER_PER_PID
:
239 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
243 registry
= reg_pid
->registry
->reg
.ust
;
246 case LTTNG_BUFFER_PER_UID
:
248 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
249 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
250 ua_sess
->real_credentials
.uid
);
254 registry
= reg_uid
->registry
->reg
.ust
;
266 * Delete ust context safely. RCU read lock must be held before calling
270 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
278 pthread_mutex_lock(&app
->sock_lock
);
279 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
280 pthread_mutex_unlock(&app
->sock_lock
);
282 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
283 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
284 app
->pid
, app
->sock
);
285 } else if (ret
== -EAGAIN
) {
286 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
287 app
->pid
, app
->sock
);
289 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
290 ua_ctx
->obj
->handle
, ret
,
291 app
->pid
, app
->sock
);
300 * Delete ust app event safely. RCU read lock must be held before calling
304 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
311 free(ua_event
->filter
);
312 if (ua_event
->exclusion
!= NULL
)
313 free(ua_event
->exclusion
);
314 if (ua_event
->obj
!= NULL
) {
315 pthread_mutex_lock(&app
->sock_lock
);
316 ret
= ustctl_release_object(sock
, ua_event
->obj
);
317 pthread_mutex_unlock(&app
->sock_lock
);
319 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
320 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
321 app
->pid
, app
->sock
);
322 } else if (ret
== -EAGAIN
) {
323 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
324 app
->pid
, app
->sock
);
326 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
327 ret
, app
->pid
, app
->sock
);
336 * Release ust data object of the given stream.
338 * Return 0 on success or else a negative value.
340 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
348 pthread_mutex_lock(&app
->sock_lock
);
349 ret
= ustctl_release_object(sock
, stream
->obj
);
350 pthread_mutex_unlock(&app
->sock_lock
);
352 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
353 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
354 app
->pid
, app
->sock
);
355 } else if (ret
== -EAGAIN
) {
356 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
357 app
->pid
, app
->sock
);
359 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
360 ret
, app
->pid
, app
->sock
);
363 lttng_fd_put(LTTNG_FD_APPS
, 2);
371 * Delete ust app stream safely. RCU read lock must be held before calling
375 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
380 (void) release_ust_app_stream(sock
, stream
, app
);
385 * We need to execute ht_destroy outside of RCU read-side critical
386 * section and outside of call_rcu thread, so we postpone its execution
387 * using ht_cleanup_push. It is simpler than to change the semantic of
388 * the many callers of delete_ust_app_session().
391 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
393 struct ust_app_channel
*ua_chan
=
394 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
396 ht_cleanup_push(ua_chan
->ctx
);
397 ht_cleanup_push(ua_chan
->events
);
402 * Extract the lost packet or discarded events counter when the channel is
403 * being deleted and store the value in the parent channel so we can
404 * access it from lttng list and at stop/destroy.
406 * The session list lock must be held by the caller.
409 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
411 uint64_t discarded
= 0, lost
= 0;
412 struct ltt_session
*session
;
413 struct ltt_ust_channel
*uchan
;
415 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
420 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
421 if (!session
|| !session
->ust_session
) {
423 * Not finding the session is not an error because there are
424 * multiple ways the channels can be torn down.
426 * 1) The session daemon can initiate the destruction of the
427 * ust app session after receiving a destroy command or
428 * during its shutdown/teardown.
429 * 2) The application, since we are in per-pid tracing, is
430 * unregistering and tearing down its ust app session.
432 * Both paths are protected by the session list lock which
433 * ensures that the accounting of lost packets and discarded
434 * events is done exactly once. The session is then unpublished
435 * from the session list, resulting in this condition.
440 if (ua_chan
->attr
.overwrite
) {
441 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
442 ua_chan
->key
, session
->ust_session
->consumer
,
445 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
446 ua_chan
->key
, session
->ust_session
->consumer
,
449 uchan
= trace_ust_find_channel_by_name(
450 session
->ust_session
->domain_global
.channels
,
453 ERR("Missing UST channel to store discarded counters");
457 uchan
->per_pid_closed_app_discarded
+= discarded
;
458 uchan
->per_pid_closed_app_lost
+= lost
;
463 session_put(session
);
468 * Delete ust app channel safely. RCU read lock must be held before calling
471 * The session list lock must be held by the caller.
474 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
478 struct lttng_ht_iter iter
;
479 struct ust_app_event
*ua_event
;
480 struct ust_app_ctx
*ua_ctx
;
481 struct ust_app_stream
*stream
, *stmp
;
482 struct ust_registry_session
*registry
;
486 DBG3("UST app deleting channel %s", ua_chan
->name
);
489 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
490 cds_list_del(&stream
->list
);
491 delete_ust_app_stream(sock
, stream
, app
);
495 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
496 cds_list_del(&ua_ctx
->list
);
497 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
499 delete_ust_app_ctx(sock
, ua_ctx
, app
);
503 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
505 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
507 delete_ust_app_event(sock
, ua_event
, app
);
510 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
511 /* Wipe and free registry from session registry. */
512 registry
= get_session_registry(ua_chan
->session
);
514 ust_registry_channel_del_free(registry
, ua_chan
->key
,
518 * A negative socket can be used by the caller when
519 * cleaning-up a ua_chan in an error path. Skip the
520 * accounting in this case.
523 save_per_pid_lost_discarded_counters(ua_chan
);
527 if (ua_chan
->obj
!= NULL
) {
528 /* Remove channel from application UST object descriptor. */
529 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
530 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
532 pthread_mutex_lock(&app
->sock_lock
);
533 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
534 pthread_mutex_unlock(&app
->sock_lock
);
536 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
537 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
538 ua_chan
->name
, app
->pid
,
540 } else if (ret
== -EAGAIN
) {
541 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
542 ua_chan
->name
, app
->pid
,
545 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
546 ua_chan
->name
, ret
, app
->pid
,
550 lttng_fd_put(LTTNG_FD_APPS
, 1);
553 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
556 int ust_app_register_done(struct ust_app
*app
)
560 pthread_mutex_lock(&app
->sock_lock
);
561 ret
= ustctl_register_done(app
->sock
);
562 pthread_mutex_unlock(&app
->sock_lock
);
566 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
571 pthread_mutex_lock(&app
->sock_lock
);
576 ret
= ustctl_release_object(sock
, data
);
578 pthread_mutex_unlock(&app
->sock_lock
);
584 * Push metadata to consumer socket.
586 * RCU read-side lock must be held to guarantee existance of socket.
587 * Must be called with the ust app session lock held.
588 * Must be called with the registry lock held.
590 * On success, return the len of metadata pushed or else a negative value.
591 * Returning a -EPIPE return value means we could not send the metadata,
592 * but it can be caused by recoverable errors (e.g. the application has
593 * terminated concurrently).
595 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
596 struct consumer_socket
*socket
, int send_zero_data
)
599 char *metadata_str
= NULL
;
600 size_t len
, offset
, new_metadata_len_sent
;
602 uint64_t metadata_key
, metadata_version
;
607 metadata_key
= registry
->metadata_key
;
610 * Means that no metadata was assigned to the session. This can
611 * happens if no start has been done previously.
617 offset
= registry
->metadata_len_sent
;
618 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
619 new_metadata_len_sent
= registry
->metadata_len
;
620 metadata_version
= registry
->metadata_version
;
622 DBG3("No metadata to push for metadata key %" PRIu64
,
623 registry
->metadata_key
);
625 if (send_zero_data
) {
626 DBG("No metadata to push");
632 /* Allocate only what we have to send. */
633 metadata_str
= zmalloc(len
);
635 PERROR("zmalloc ust app metadata string");
639 /* Copy what we haven't sent out. */
640 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
643 pthread_mutex_unlock(®istry
->lock
);
645 * We need to unlock the registry while we push metadata to
646 * break a circular dependency between the consumerd metadata
647 * lock and the sessiond registry lock. Indeed, pushing metadata
648 * to the consumerd awaits that it gets pushed all the way to
649 * relayd, but doing so requires grabbing the metadata lock. If
650 * a concurrent metadata request is being performed by
651 * consumerd, this can try to grab the registry lock on the
652 * sessiond while holding the metadata lock on the consumer
653 * daemon. Those push and pull schemes are performed on two
654 * different bidirectionnal communication sockets.
656 ret
= consumer_push_metadata(socket
, metadata_key
,
657 metadata_str
, len
, offset
, metadata_version
);
658 pthread_mutex_lock(®istry
->lock
);
661 * There is an acceptable race here between the registry
662 * metadata key assignment and the creation on the
663 * consumer. The session daemon can concurrently push
664 * metadata for this registry while being created on the
665 * consumer since the metadata key of the registry is
666 * assigned *before* it is setup to avoid the consumer
667 * to ask for metadata that could possibly be not found
668 * in the session daemon.
670 * The metadata will get pushed either by the session
671 * being stopped or the consumer requesting metadata if
672 * that race is triggered.
674 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
677 ERR("Error pushing metadata to consumer");
683 * Metadata may have been concurrently pushed, since
684 * we're not holding the registry lock while pushing to
685 * consumer. This is handled by the fact that we send
686 * the metadata content, size, and the offset at which
687 * that metadata belongs. This may arrive out of order
688 * on the consumer side, and the consumer is able to
689 * deal with overlapping fragments. The consumer
690 * supports overlapping fragments, which must be
691 * contiguous starting from offset 0. We keep the
692 * largest metadata_len_sent value of the concurrent
695 registry
->metadata_len_sent
=
696 max_t(size_t, registry
->metadata_len_sent
,
697 new_metadata_len_sent
);
706 * On error, flag the registry that the metadata is
707 * closed. We were unable to push anything and this
708 * means that either the consumer is not responding or
709 * the metadata cache has been destroyed on the
712 registry
->metadata_closed
= 1;
720 * For a given application and session, push metadata to consumer.
721 * Either sock or consumer is required : if sock is NULL, the default
722 * socket to send the metadata is retrieved from consumer, if sock
723 * is not NULL we use it to send the metadata.
724 * RCU read-side lock must be held while calling this function,
725 * therefore ensuring existance of registry. It also ensures existance
726 * of socket throughout this function.
728 * Return 0 on success else a negative error.
729 * Returning a -EPIPE return value means we could not send the metadata,
730 * but it can be caused by recoverable errors (e.g. the application has
731 * terminated concurrently).
733 static int push_metadata(struct ust_registry_session
*registry
,
734 struct consumer_output
*consumer
)
738 struct consumer_socket
*socket
;
743 pthread_mutex_lock(®istry
->lock
);
744 if (registry
->metadata_closed
) {
749 /* Get consumer socket to use to push the metadata.*/
750 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
757 ret
= ust_app_push_metadata(registry
, socket
, 0);
762 pthread_mutex_unlock(®istry
->lock
);
766 pthread_mutex_unlock(®istry
->lock
);
771 * Send to the consumer a close metadata command for the given session. Once
772 * done, the metadata channel is deleted and the session metadata pointer is
773 * nullified. The session lock MUST be held unless the application is
774 * in the destroy path.
776 * Do not hold the registry lock while communicating with the consumerd, because
777 * doing so causes inter-process deadlocks between consumerd and sessiond with
778 * the metadata request notification.
780 * Return 0 on success else a negative value.
782 static int close_metadata(struct ust_registry_session
*registry
,
783 struct consumer_output
*consumer
)
786 struct consumer_socket
*socket
;
787 uint64_t metadata_key
;
788 bool registry_was_already_closed
;
795 pthread_mutex_lock(®istry
->lock
);
796 metadata_key
= registry
->metadata_key
;
797 registry_was_already_closed
= registry
->metadata_closed
;
798 if (metadata_key
!= 0) {
800 * Metadata closed. Even on error this means that the consumer
801 * is not responding or not found so either way a second close
802 * should NOT be emit for this registry.
804 registry
->metadata_closed
= 1;
806 pthread_mutex_unlock(®istry
->lock
);
808 if (metadata_key
== 0 || registry_was_already_closed
) {
813 /* Get consumer socket to use to push the metadata.*/
814 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
821 ret
= consumer_close_metadata(socket
, metadata_key
);
832 * We need to execute ht_destroy outside of RCU read-side critical
833 * section and outside of call_rcu thread, so we postpone its execution
834 * using ht_cleanup_push. It is simpler than to change the semantic of
835 * the many callers of delete_ust_app_session().
838 void delete_ust_app_session_rcu(struct rcu_head
*head
)
840 struct ust_app_session
*ua_sess
=
841 caa_container_of(head
, struct ust_app_session
, rcu_head
);
843 ht_cleanup_push(ua_sess
->channels
);
848 * Delete ust app session safely. RCU read lock must be held before calling
851 * The session list lock must be held by the caller.
854 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
858 struct lttng_ht_iter iter
;
859 struct ust_app_channel
*ua_chan
;
860 struct ust_registry_session
*registry
;
864 pthread_mutex_lock(&ua_sess
->lock
);
866 assert(!ua_sess
->deleted
);
867 ua_sess
->deleted
= true;
869 registry
= get_session_registry(ua_sess
);
870 /* Registry can be null on error path during initialization. */
872 /* Push metadata for application before freeing the application. */
873 (void) push_metadata(registry
, ua_sess
->consumer
);
876 * Don't ask to close metadata for global per UID buffers. Close
877 * metadata only on destroy trace session in this case. Also, the
878 * previous push metadata could have flag the metadata registry to
879 * close so don't send a close command if closed.
881 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
882 /* And ask to close it for this session registry. */
883 (void) close_metadata(registry
, ua_sess
->consumer
);
887 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
889 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
891 delete_ust_app_channel(sock
, ua_chan
, app
);
894 /* In case of per PID, the registry is kept in the session. */
895 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
896 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
899 * Registry can be null on error path during
902 buffer_reg_pid_remove(reg_pid
);
903 buffer_reg_pid_destroy(reg_pid
);
907 if (ua_sess
->handle
!= -1) {
908 pthread_mutex_lock(&app
->sock_lock
);
909 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
910 pthread_mutex_unlock(&app
->sock_lock
);
912 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
913 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
914 app
->pid
, app
->sock
);
915 } else if (ret
== -EAGAIN
) {
916 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
917 app
->pid
, app
->sock
);
919 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
920 ret
, app
->pid
, app
->sock
);
924 /* Remove session from application UST object descriptor. */
925 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
926 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
930 pthread_mutex_unlock(&ua_sess
->lock
);
932 consumer_output_put(ua_sess
->consumer
);
934 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
938 * Delete a traceable application structure from the global list. Never call
939 * this function outside of a call_rcu call.
941 * RCU read side lock should _NOT_ be held when calling this function.
944 void delete_ust_app(struct ust_app
*app
)
947 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
950 * The session list lock must be held during this function to guarantee
951 * the existence of ua_sess.
954 /* Delete ust app sessions info */
959 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
961 /* Free every object in the session and the session. */
963 delete_ust_app_session(sock
, ua_sess
, app
);
967 ht_cleanup_push(app
->sessions
);
968 ht_cleanup_push(app
->ust_sessions_objd
);
969 ht_cleanup_push(app
->ust_objd
);
972 * Wait until we have deleted the application from the sock hash table
973 * before closing this socket, otherwise an application could re-use the
974 * socket ID and race with the teardown, using the same hash table entry.
976 * It's OK to leave the close in call_rcu. We want it to stay unique for
977 * all RCU readers that could run concurrently with unregister app,
978 * therefore we _need_ to only close that socket after a grace period. So
979 * it should stay in this RCU callback.
981 * This close() is a very important step of the synchronization model so
982 * every modification to this function must be carefully reviewed.
988 lttng_fd_put(LTTNG_FD_APPS
, 1);
990 DBG2("UST app pid %d deleted", app
->pid
);
992 session_unlock_list();
996 * URCU intermediate call to delete an UST app.
999 void delete_ust_app_rcu(struct rcu_head
*head
)
1001 struct lttng_ht_node_ulong
*node
=
1002 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1003 struct ust_app
*app
=
1004 caa_container_of(node
, struct ust_app
, pid_n
);
1006 DBG3("Call RCU deleting app PID %d", app
->pid
);
1007 delete_ust_app(app
);
1011 * Delete the session from the application ht and delete the data structure by
1012 * freeing every object inside and releasing them.
1014 * The session list lock must be held by the caller.
1016 static void destroy_app_session(struct ust_app
*app
,
1017 struct ust_app_session
*ua_sess
)
1020 struct lttng_ht_iter iter
;
1025 iter
.iter
.node
= &ua_sess
->node
.node
;
1026 ret
= lttng_ht_del(app
->sessions
, &iter
);
1028 /* Already scheduled for teardown. */
1032 /* Once deleted, free the data structure. */
1033 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1040 * Alloc new UST app session.
1043 struct ust_app_session
*alloc_ust_app_session(void)
1045 struct ust_app_session
*ua_sess
;
1047 /* Init most of the default value by allocating and zeroing */
1048 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1049 if (ua_sess
== NULL
) {
1054 ua_sess
->handle
= -1;
1055 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1056 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1057 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1066 * Alloc new UST app channel.
1069 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1070 struct ust_app_session
*ua_sess
,
1071 struct lttng_ust_channel_attr
*attr
)
1073 struct ust_app_channel
*ua_chan
;
1075 /* Init most of the default value by allocating and zeroing */
1076 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1077 if (ua_chan
== NULL
) {
1082 /* Setup channel name */
1083 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1084 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1086 ua_chan
->enabled
= 1;
1087 ua_chan
->handle
= -1;
1088 ua_chan
->session
= ua_sess
;
1089 ua_chan
->key
= get_next_channel_key();
1090 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1091 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1092 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1094 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1095 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1097 /* Copy attributes */
1099 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1100 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1101 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1102 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1103 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1104 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1105 ua_chan
->attr
.output
= attr
->output
;
1106 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1108 /* By default, the channel is a per cpu channel. */
1109 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1111 DBG3("UST app channel %s allocated", ua_chan
->name
);
1120 * Allocate and initialize a UST app stream.
1122 * Return newly allocated stream pointer or NULL on error.
1124 struct ust_app_stream
*ust_app_alloc_stream(void)
1126 struct ust_app_stream
*stream
= NULL
;
1128 stream
= zmalloc(sizeof(*stream
));
1129 if (stream
== NULL
) {
1130 PERROR("zmalloc ust app stream");
1134 /* Zero could be a valid value for a handle so flag it to -1. */
1135 stream
->handle
= -1;
1142 * Alloc new UST app event.
1145 struct ust_app_event
*alloc_ust_app_event(char *name
,
1146 struct lttng_ust_event
*attr
)
1148 struct ust_app_event
*ua_event
;
1150 /* Init most of the default value by allocating and zeroing */
1151 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1152 if (ua_event
== NULL
) {
1153 PERROR("Failed to allocate ust_app_event structure");
1157 ua_event
->enabled
= 1;
1158 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1159 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1160 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1162 /* Copy attributes */
1164 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1167 DBG3("UST app event %s allocated", ua_event
->name
);
1176 * Alloc new UST app context.
1179 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1181 struct ust_app_ctx
*ua_ctx
;
1183 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1184 if (ua_ctx
== NULL
) {
1188 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1191 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1192 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1193 char *provider_name
= NULL
, *ctx_name
= NULL
;
1195 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1196 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1197 if (!provider_name
|| !ctx_name
) {
1198 free(provider_name
);
1203 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1204 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1208 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1216 * Allocate a filter and copy the given original filter.
1218 * Return allocated filter or NULL on error.
1220 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1221 struct lttng_filter_bytecode
*orig_f
)
1223 struct lttng_filter_bytecode
*filter
= NULL
;
1225 /* Copy filter bytecode */
1226 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1228 PERROR("zmalloc alloc filter bytecode");
1232 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1239 * Create a liblttng-ust filter bytecode from given bytecode.
1241 * Return allocated filter or NULL on error.
1243 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1244 struct lttng_filter_bytecode
*orig_f
)
1246 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1248 /* Copy filter bytecode */
1249 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1251 PERROR("zmalloc alloc ust filter bytecode");
1255 assert(sizeof(struct lttng_filter_bytecode
) ==
1256 sizeof(struct lttng_ust_filter_bytecode
));
1257 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1263 * Find an ust_app using the sock and return it. RCU read side lock must be
1264 * held before calling this helper function.
1266 struct ust_app
*ust_app_find_by_sock(int sock
)
1268 struct lttng_ht_node_ulong
*node
;
1269 struct lttng_ht_iter iter
;
1271 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1272 node
= lttng_ht_iter_get_node_ulong(&iter
);
1274 DBG2("UST app find by sock %d not found", sock
);
1278 return caa_container_of(node
, struct ust_app
, sock_n
);
1285 * Find an ust_app using the notify sock and return it. RCU read side lock must
1286 * be held before calling this helper function.
1288 static struct ust_app
*find_app_by_notify_sock(int sock
)
1290 struct lttng_ht_node_ulong
*node
;
1291 struct lttng_ht_iter iter
;
1293 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1295 node
= lttng_ht_iter_get_node_ulong(&iter
);
1297 DBG2("UST app find by notify sock %d not found", sock
);
1301 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1308 * Lookup for an ust app event based on event name, filter bytecode and the
1311 * Return an ust_app_event object or NULL on error.
1313 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1314 const char *name
, const struct lttng_filter_bytecode
*filter
,
1316 const struct lttng_event_exclusion
*exclusion
)
1318 struct lttng_ht_iter iter
;
1319 struct lttng_ht_node_str
*node
;
1320 struct ust_app_event
*event
= NULL
;
1321 struct ust_app_ht_key key
;
1326 /* Setup key for event lookup. */
1328 key
.filter
= filter
;
1329 key
.loglevel_type
= loglevel_value
;
1330 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1331 key
.exclusion
= exclusion
;
1333 /* Lookup using the event name as hash and a custom match fct. */
1334 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1335 ht_match_ust_app_event
, &key
, &iter
.iter
);
1336 node
= lttng_ht_iter_get_node_str(&iter
);
1341 event
= caa_container_of(node
, struct ust_app_event
, node
);
1348 * Create the channel context on the tracer.
1350 * Called with UST app session lock held.
1353 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1354 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1358 health_code_update();
1360 pthread_mutex_lock(&app
->sock_lock
);
1361 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1362 ua_chan
->obj
, &ua_ctx
->obj
);
1363 pthread_mutex_unlock(&app
->sock_lock
);
1365 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1367 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1368 app
->pid
, app
->sock
);
1369 } else if (ret
== -EAGAIN
) {
1371 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1372 app
->pid
, app
->sock
);
1374 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1375 ret
, app
->pid
, app
->sock
);
1380 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1382 DBG2("UST app context handle %d created successfully for channel %s",
1383 ua_ctx
->handle
, ua_chan
->name
);
1386 health_code_update();
1391 * Set the filter on the tracer.
1394 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1395 struct ust_app
*app
)
1398 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1400 health_code_update();
1402 if (!ua_event
->filter
) {
1407 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1408 if (!ust_bytecode
) {
1409 ret
= -LTTNG_ERR_NOMEM
;
1412 pthread_mutex_lock(&app
->sock_lock
);
1413 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1415 pthread_mutex_unlock(&app
->sock_lock
);
1417 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1419 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1420 app
->pid
, app
->sock
);
1421 } else if (ret
== -EAGAIN
) {
1423 DBG3("UST app set filter failed. Communication timeout: pid = %d, sock = %d",
1424 app
->pid
, app
->sock
);
1426 ERR("UST app event set filter failed with ret %d: pid = %d, sock = %d",
1433 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1436 health_code_update();
1442 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1443 struct lttng_event_exclusion
*exclusion
)
1445 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1446 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1447 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1449 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1450 if (!ust_exclusion
) {
1455 assert(sizeof(struct lttng_event_exclusion
) ==
1456 sizeof(struct lttng_ust_event_exclusion
));
1457 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1459 return ust_exclusion
;
1463 * Set event exclusions on the tracer.
1466 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1467 struct ust_app
*app
)
1470 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1472 health_code_update();
1474 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1479 ust_exclusion
= create_ust_exclusion_from_exclusion(
1480 ua_event
->exclusion
);
1481 if (!ust_exclusion
) {
1482 ret
= -LTTNG_ERR_NOMEM
;
1485 pthread_mutex_lock(&app
->sock_lock
);
1486 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1487 pthread_mutex_unlock(&app
->sock_lock
);
1489 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1491 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1492 app
->pid
, app
->sock
);
1493 } else if (ret
== -EAGAIN
) {
1495 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1496 app
->pid
, app
->sock
);
1498 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, event = %s",
1499 ret
, app
->pid
, app
->sock
, ua_event
->name
);
1504 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1507 health_code_update();
1508 free(ust_exclusion
);
1513 * Disable the specified event on to UST tracer for the UST session.
1515 static int disable_ust_event(struct ust_app
*app
,
1516 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1520 health_code_update();
1522 pthread_mutex_lock(&app
->sock_lock
);
1523 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1524 pthread_mutex_unlock(&app
->sock_lock
);
1526 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1528 DBG3("UST app disable event failed. Application is dead: pid = %d, sock = %d",
1529 app
->pid
, app
->sock
);
1530 } else if (ret
== -EAGAIN
) {
1532 WARN("UST app disable event failed. Communication time out: pid = %d, sock = %d",
1533 app
->pid
, app
->sock
);
1535 ERR("UST app disable event failed with ret %d: pid = %d, sock = %d , name = %s",
1536 ret
, app
->pid
, app
->sock
, ua_event
->attr
.name
);
1541 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1542 ua_event
->attr
.name
, app
->pid
);
1545 health_code_update();
1550 * Disable the specified channel on to UST tracer for the UST session.
1552 static int disable_ust_channel(struct ust_app
*app
,
1553 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1557 health_code_update();
1559 pthread_mutex_lock(&app
->sock_lock
);
1560 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1561 pthread_mutex_unlock(&app
->sock_lock
);
1563 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1565 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1566 app
->pid
, app
->sock
);
1567 } else if (ret
== -EAGAIN
) {
1569 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1570 app
->pid
, app
->sock
);
1572 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1573 ua_chan
->name
, ua_sess
->handle
, ret
,
1574 app
->pid
, app
->sock
);
1579 DBG2("UST app channel %s disabled successfully for app: pid = %d",
1580 ua_chan
->name
, app
->pid
);
1583 health_code_update();
1588 * Enable the specified channel on to UST tracer for the UST session.
1590 static int enable_ust_channel(struct ust_app
*app
,
1591 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1595 health_code_update();
1597 pthread_mutex_lock(&app
->sock_lock
);
1598 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1599 pthread_mutex_unlock(&app
->sock_lock
);
1601 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1603 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1604 ua_chan
->name
, app
->pid
, app
->sock
);
1605 } else if (ret
== -EAGAIN
) {
1607 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1608 ua_chan
->name
, app
->pid
, app
->sock
);
1610 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1611 ua_chan
->name
, ua_sess
->handle
, ret
,
1612 app
->pid
, app
->sock
);
1617 ua_chan
->enabled
= 1;
1619 DBG2("UST app channel %s enabled successfully for app: pid = %d",
1620 ua_chan
->name
, app
->pid
);
1623 health_code_update();
1628 * Enable the specified event on to UST tracer for the UST session.
1630 static int enable_ust_event(struct ust_app
*app
,
1631 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1635 health_code_update();
1637 pthread_mutex_lock(&app
->sock_lock
);
1638 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1639 pthread_mutex_unlock(&app
->sock_lock
);
1641 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1643 DBG3("UST app enable event failed. Application is dead: pid = %d, sock = %d",
1644 app
->pid
, app
->sock
);
1645 } else if (ret
== -EAGAIN
) {
1647 WARN("UST app enable event failed. Communication time out: pid = %d, sock = %d",
1648 app
->pid
, app
->sock
);
1650 ERR("UST app enable event failed with ret %d: pid = %d, sock = %d, event = %s",
1651 ret
, app
->pid
, app
->sock
, ua_event
->attr
.name
);
1656 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1657 ua_event
->attr
.name
, app
->pid
);
1660 health_code_update();
1665 * Send channel and stream buffer to application.
1667 * Return 0 on success. On error, a negative value is returned.
1669 static int send_channel_pid_to_ust(struct ust_app
*app
,
1670 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1673 struct ust_app_stream
*stream
, *stmp
;
1679 health_code_update();
1681 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1684 /* Send channel to the application. */
1685 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1686 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1687 ret
= -ENOTCONN
; /* Caused by app exiting. */
1689 } else if (ret
== -EAGAIN
) {
1690 /* Caused by timeout. */
1691 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
"\".",
1692 app
->pid
, ua_chan
->name
, ua_sess
->tracing_id
);
1693 /* Treat this the same way as an application that is exiting. */
1696 } else if (ret
< 0) {
1700 health_code_update();
1702 /* Send all streams to application. */
1703 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1704 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1705 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1706 ret
= -ENOTCONN
; /* Caused by app exiting. */
1708 } else if (ret
== -EAGAIN
) {
1709 /* Caused by timeout. */
1710 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
"\".",
1711 app
->pid
, stream
->name
, ua_chan
->name
,
1712 ua_sess
->tracing_id
);
1714 * Treat this the same way as an application that is
1718 } else if (ret
< 0) {
1721 /* We don't need the stream anymore once sent to the tracer. */
1722 cds_list_del(&stream
->list
);
1723 delete_ust_app_stream(-1, stream
, app
);
1725 /* Flag the channel that it is sent to the application. */
1726 ua_chan
->is_sent
= 1;
1729 health_code_update();
1734 * Create the specified event onto the UST tracer for a UST session.
1736 * Should be called with session mutex held.
1739 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1740 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1744 health_code_update();
1746 /* Create UST event on tracer */
1747 pthread_mutex_lock(&app
->sock_lock
);
1748 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1750 pthread_mutex_unlock(&app
->sock_lock
);
1752 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1754 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
1755 app
->pid
, app
->sock
);
1756 } else if (ret
== -EAGAIN
) {
1758 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
1759 app
->pid
, app
->sock
);
1761 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
1762 ua_event
->attr
.name
, ret
, app
->pid
,
1768 ua_event
->handle
= ua_event
->obj
->handle
;
1770 DBG2("UST app event %s created successfully for pid:%d",
1771 ua_event
->attr
.name
, app
->pid
);
1773 health_code_update();
1775 /* Set filter if one is present. */
1776 if (ua_event
->filter
) {
1777 ret
= set_ust_event_filter(ua_event
, app
);
1783 /* Set exclusions for the event */
1784 if (ua_event
->exclusion
) {
1785 ret
= set_ust_event_exclusion(ua_event
, app
);
1791 /* If event not enabled, disable it on the tracer */
1792 if (ua_event
->enabled
) {
1794 * We now need to explicitly enable the event, since it
1795 * is now disabled at creation.
1797 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1800 * If we hit an EPERM, something is wrong with our enable call. If
1801 * we get an EEXIST, there is a problem on the tracer side since we
1805 case -LTTNG_UST_ERR_PERM
:
1806 /* Code flow problem */
1808 case -LTTNG_UST_ERR_EXIST
:
1809 /* It's OK for our use case. */
1820 health_code_update();
1825 * Copy data between an UST app event and a LTT event.
1827 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1828 struct ltt_ust_event
*uevent
)
1830 size_t exclusion_alloc_size
;
1832 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1833 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1835 ua_event
->enabled
= uevent
->enabled
;
1837 /* Copy event attributes */
1838 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1840 /* Copy filter bytecode */
1841 if (uevent
->filter
) {
1842 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1843 /* Filter might be NULL here in case of ENONEM. */
1846 /* Copy exclusion data */
1847 if (uevent
->exclusion
) {
1848 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1849 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1850 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1851 if (ua_event
->exclusion
== NULL
) {
1854 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1855 exclusion_alloc_size
);
1861 * Copy data between an UST app channel and a LTT channel.
1863 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1864 struct ltt_ust_channel
*uchan
)
1866 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1868 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1869 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1871 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1872 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1874 /* Copy event attributes since the layout is different. */
1875 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1876 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1877 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1878 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1879 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1880 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1881 ua_chan
->attr
.output
= uchan
->attr
.output
;
1882 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
1885 * Note that the attribute channel type is not set since the channel on the
1886 * tracing registry side does not have this information.
1889 ua_chan
->enabled
= uchan
->enabled
;
1890 ua_chan
->tracing_channel_id
= uchan
->id
;
1892 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1896 * Copy data between a UST app session and a regular LTT session.
1898 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1899 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1901 struct tm
*timeinfo
;
1904 char tmp_shm_path
[PATH_MAX
];
1906 timeinfo
= localtime(&app
->registration_time
);
1907 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1909 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1911 ua_sess
->tracing_id
= usess
->id
;
1912 ua_sess
->id
= get_next_session_id();
1913 ua_sess
->real_credentials
.uid
= app
->uid
;
1914 ua_sess
->real_credentials
.gid
= app
->gid
;
1915 ua_sess
->effective_credentials
.uid
= usess
->uid
;
1916 ua_sess
->effective_credentials
.gid
= usess
->gid
;
1917 ua_sess
->buffer_type
= usess
->buffer_type
;
1918 ua_sess
->bits_per_long
= app
->bits_per_long
;
1920 /* There is only one consumer object per session possible. */
1921 consumer_output_get(usess
->consumer
);
1922 ua_sess
->consumer
= usess
->consumer
;
1924 ua_sess
->output_traces
= usess
->output_traces
;
1925 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1926 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1927 &usess
->metadata_attr
);
1929 switch (ua_sess
->buffer_type
) {
1930 case LTTNG_BUFFER_PER_PID
:
1931 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1932 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1935 case LTTNG_BUFFER_PER_UID
:
1936 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1937 DEFAULT_UST_TRACE_UID_PATH
,
1938 ua_sess
->real_credentials
.uid
,
1939 app
->bits_per_long
);
1946 PERROR("asprintf UST shadow copy session");
1951 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1952 sizeof(ua_sess
->root_shm_path
));
1953 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1954 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1955 sizeof(ua_sess
->shm_path
));
1956 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1957 if (ua_sess
->shm_path
[0]) {
1958 switch (ua_sess
->buffer_type
) {
1959 case LTTNG_BUFFER_PER_PID
:
1960 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1961 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1962 app
->name
, app
->pid
, datetime
);
1964 case LTTNG_BUFFER_PER_UID
:
1965 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1966 "/" DEFAULT_UST_TRACE_UID_PATH
,
1967 app
->uid
, app
->bits_per_long
);
1974 PERROR("sprintf UST shadow copy session");
1978 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1979 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1980 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1985 consumer_output_put(ua_sess
->consumer
);
1989 * Lookup sesison wrapper.
1992 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
1993 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1995 /* Get right UST app session from app */
1996 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2000 * Return ust app session from the app session hashtable using the UST session
2003 static struct ust_app_session
*lookup_session_by_app(
2004 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2006 struct lttng_ht_iter iter
;
2007 struct lttng_ht_node_u64
*node
;
2009 __lookup_session_by_app(usess
, app
, &iter
);
2010 node
= lttng_ht_iter_get_node_u64(&iter
);
2015 return caa_container_of(node
, struct ust_app_session
, node
);
2022 * Setup buffer registry per PID for the given session and application. If none
2023 * is found, a new one is created, added to the global registry and
2024 * initialized. If regp is valid, it's set with the newly created object.
2026 * Return 0 on success or else a negative value.
2028 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2029 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2032 struct buffer_reg_pid
*reg_pid
;
2039 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2042 * This is the create channel path meaning that if there is NO
2043 * registry available, we have to create one for this session.
2045 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2046 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2054 /* Initialize registry. */
2055 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2056 app
->bits_per_long
, app
->uint8_t_alignment
,
2057 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2058 app
->uint64_t_alignment
, app
->long_alignment
,
2059 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2060 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2061 ua_sess
->effective_credentials
.uid
,
2062 ua_sess
->effective_credentials
.gid
, ua_sess
->tracing_id
,
2066 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2067 * destroy the buffer registry, because it is always expected
2068 * that if the buffer registry can be found, its ust registry is
2071 buffer_reg_pid_destroy(reg_pid
);
2075 buffer_reg_pid_add(reg_pid
);
2077 DBG3("UST app buffer registry per PID created successfully");
2089 * Setup buffer registry per UID for the given session and application. If none
2090 * is found, a new one is created, added to the global registry and
2091 * initialized. If regp is valid, it's set with the newly created object.
2093 * Return 0 on success or else a negative value.
2095 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2096 struct ust_app_session
*ua_sess
,
2097 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2100 struct buffer_reg_uid
*reg_uid
;
2107 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2110 * This is the create channel path meaning that if there is NO
2111 * registry available, we have to create one for this session.
2113 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2114 LTTNG_DOMAIN_UST
, ®_uid
,
2115 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2123 /* Initialize registry. */
2124 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2125 app
->bits_per_long
, app
->uint8_t_alignment
,
2126 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2127 app
->uint64_t_alignment
, app
->long_alignment
,
2128 app
->byte_order
, app
->version
.major
,
2129 app
->version
.minor
, reg_uid
->root_shm_path
,
2130 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2131 ua_sess
->tracing_id
, app
->uid
);
2134 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2135 * destroy the buffer registry, because it is always expected
2136 * that if the buffer registry can be found, its ust registry is
2139 buffer_reg_uid_destroy(reg_uid
, NULL
);
2142 /* Add node to teardown list of the session. */
2143 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2145 buffer_reg_uid_add(reg_uid
);
2147 DBG3("UST app buffer registry per UID created successfully");
2158 * Create a session on the tracer side for the given app.
2160 * On success, ua_sess_ptr is populated with the session pointer or else left
2161 * untouched. If the session was created, is_created is set to 1. On error,
2162 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2165 * Returns 0 on success or else a negative code which is either -ENOMEM or
2166 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2168 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2169 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2172 int ret
, created
= 0;
2173 struct ust_app_session
*ua_sess
;
2177 assert(ua_sess_ptr
);
2179 health_code_update();
2181 ua_sess
= lookup_session_by_app(usess
, app
);
2182 if (ua_sess
== NULL
) {
2183 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2184 app
->pid
, usess
->id
);
2185 ua_sess
= alloc_ust_app_session();
2186 if (ua_sess
== NULL
) {
2187 /* Only malloc can failed so something is really wrong */
2191 shadow_copy_session(ua_sess
, usess
, app
);
2195 switch (usess
->buffer_type
) {
2196 case LTTNG_BUFFER_PER_PID
:
2197 /* Init local registry. */
2198 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2200 delete_ust_app_session(-1, ua_sess
, app
);
2204 case LTTNG_BUFFER_PER_UID
:
2205 /* Look for a global registry. If none exists, create one. */
2206 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2208 delete_ust_app_session(-1, ua_sess
, app
);
2218 health_code_update();
2220 if (ua_sess
->handle
== -1) {
2221 pthread_mutex_lock(&app
->sock_lock
);
2222 ret
= ustctl_create_session(app
->sock
);
2223 pthread_mutex_unlock(&app
->sock_lock
);
2225 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2226 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2227 app
->pid
, app
->sock
);
2229 } else if (ret
== -EAGAIN
) {
2230 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2231 app
->pid
, app
->sock
);
2234 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2235 ret
, app
->pid
, app
->sock
);
2237 delete_ust_app_session(-1, ua_sess
, app
);
2238 if (ret
!= -ENOMEM
) {
2240 * Tracer is probably gone or got an internal error so let's
2241 * behave like it will soon unregister or not usable.
2248 ua_sess
->handle
= ret
;
2250 /* Add ust app session to app's HT */
2251 lttng_ht_node_init_u64(&ua_sess
->node
,
2252 ua_sess
->tracing_id
);
2253 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2254 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2255 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2256 &ua_sess
->ust_objd_node
);
2258 DBG2("UST app session created successfully with handle %d", ret
);
2261 *ua_sess_ptr
= ua_sess
;
2263 *is_created
= created
;
2266 /* Everything went well. */
2270 health_code_update();
2275 * Match function for a hash table lookup of ust_app_ctx.
2277 * It matches an ust app context based on the context type and, in the case
2278 * of perf counters, their name.
2280 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2282 struct ust_app_ctx
*ctx
;
2283 const struct lttng_ust_context_attr
*key
;
2288 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2292 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2297 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2298 if (strncmp(key
->u
.perf_counter
.name
,
2299 ctx
->ctx
.u
.perf_counter
.name
,
2300 sizeof(key
->u
.perf_counter
.name
))) {
2304 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2305 if (strcmp(key
->u
.app_ctx
.provider_name
,
2306 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2307 strcmp(key
->u
.app_ctx
.ctx_name
,
2308 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2324 * Lookup for an ust app context from an lttng_ust_context.
2326 * Must be called while holding RCU read side lock.
2327 * Return an ust_app_ctx object or NULL on error.
2330 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2331 struct lttng_ust_context_attr
*uctx
)
2333 struct lttng_ht_iter iter
;
2334 struct lttng_ht_node_ulong
*node
;
2335 struct ust_app_ctx
*app_ctx
= NULL
;
2340 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2341 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2342 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2343 node
= lttng_ht_iter_get_node_ulong(&iter
);
2348 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2355 * Create a context for the channel on the tracer.
2357 * Called with UST app session lock held and a RCU read side lock.
2360 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2361 struct lttng_ust_context_attr
*uctx
,
2362 struct ust_app
*app
)
2365 struct ust_app_ctx
*ua_ctx
;
2367 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2369 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2375 ua_ctx
= alloc_ust_app_ctx(uctx
);
2376 if (ua_ctx
== NULL
) {
2382 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2383 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2384 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2386 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2396 * Enable on the tracer side a ust app event for the session and channel.
2398 * Called with UST app session lock held.
2401 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2402 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2406 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2411 ua_event
->enabled
= 1;
2418 * Disable on the tracer side a ust app event for the session and channel.
2420 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2421 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2425 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2430 ua_event
->enabled
= 0;
2437 * Lookup ust app channel for session and disable it on the tracer side.
2440 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2441 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2445 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2450 ua_chan
->enabled
= 0;
2457 * Lookup ust app channel for session and enable it on the tracer side. This
2458 * MUST be called with a RCU read side lock acquired.
2460 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2461 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2464 struct lttng_ht_iter iter
;
2465 struct lttng_ht_node_str
*ua_chan_node
;
2466 struct ust_app_channel
*ua_chan
;
2468 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2469 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2470 if (ua_chan_node
== NULL
) {
2471 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2472 uchan
->name
, ua_sess
->tracing_id
);
2476 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2478 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2488 * Ask the consumer to create a channel and get it if successful.
2490 * Called with UST app session lock held.
2492 * Return 0 on success or else a negative value.
2494 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2495 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2496 int bitness
, struct ust_registry_session
*registry
,
2497 uint64_t trace_archive_id
)
2500 unsigned int nb_fd
= 0;
2501 struct consumer_socket
*socket
;
2509 health_code_update();
2511 /* Get the right consumer socket for the application. */
2512 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2518 health_code_update();
2520 /* Need one fd for the channel. */
2521 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2523 ERR("Exhausted number of available FD upon create channel");
2528 * Ask consumer to create channel. The consumer will return the number of
2529 * stream we have to expect.
2531 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2532 registry
, usess
->current_trace_chunk
);
2538 * Compute the number of fd needed before receiving them. It must be 2 per
2539 * stream (2 being the default value here).
2541 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2543 /* Reserve the amount of file descriptor we need. */
2544 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2546 ERR("Exhausted number of available FD upon create channel");
2547 goto error_fd_get_stream
;
2550 health_code_update();
2553 * Now get the channel from the consumer. This call will populate the stream
2554 * list of that channel and set the ust objects.
2556 if (usess
->consumer
->enabled
) {
2557 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2567 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2568 error_fd_get_stream
:
2570 * Initiate a destroy channel on the consumer since we had an error
2571 * handling it on our side. The return value is of no importance since we
2572 * already have a ret value set by the previous error that we need to
2575 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2577 lttng_fd_put(LTTNG_FD_APPS
, 1);
2579 health_code_update();
2585 * Duplicate the ust data object of the ust app stream and save it in the
2586 * buffer registry stream.
2588 * Return 0 on success or else a negative value.
2590 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2591 struct ust_app_stream
*stream
)
2598 /* Reserve the amount of file descriptor we need. */
2599 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2601 ERR("Exhausted number of available FD upon duplicate stream");
2605 /* Duplicate object for stream once the original is in the registry. */
2606 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2607 reg_stream
->obj
.ust
);
2609 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2610 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2611 lttng_fd_put(LTTNG_FD_APPS
, 2);
2614 stream
->handle
= stream
->obj
->handle
;
2621 * Duplicate the ust data object of the ust app. channel and save it in the
2622 * buffer registry channel.
2624 * Return 0 on success or else a negative value.
2626 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2627 struct ust_app_channel
*ua_chan
)
2634 /* Need two fds for the channel. */
2635 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2637 ERR("Exhausted number of available FD upon duplicate channel");
2641 /* Duplicate object for stream once the original is in the registry. */
2642 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2644 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2645 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2648 ua_chan
->handle
= ua_chan
->obj
->handle
;
2653 lttng_fd_put(LTTNG_FD_APPS
, 1);
2659 * For a given channel buffer registry, setup all streams of the given ust
2660 * application channel.
2662 * Return 0 on success or else a negative value.
2664 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2665 struct ust_app_channel
*ua_chan
,
2666 struct ust_app
*app
)
2669 struct ust_app_stream
*stream
, *stmp
;
2674 DBG2("UST app setup buffer registry stream");
2676 /* Send all streams to application. */
2677 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2678 struct buffer_reg_stream
*reg_stream
;
2680 ret
= buffer_reg_stream_create(®_stream
);
2686 * Keep original pointer and nullify it in the stream so the delete
2687 * stream call does not release the object.
2689 reg_stream
->obj
.ust
= stream
->obj
;
2691 buffer_reg_stream_add(reg_stream
, reg_chan
);
2693 /* We don't need the streams anymore. */
2694 cds_list_del(&stream
->list
);
2695 delete_ust_app_stream(-1, stream
, app
);
2703 * Create a buffer registry channel for the given session registry and
2704 * application channel object. If regp pointer is valid, it's set with the
2705 * created object. Important, the created object is NOT added to the session
2706 * registry hash table.
2708 * Return 0 on success else a negative value.
2710 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2711 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2714 struct buffer_reg_channel
*reg_chan
= NULL
;
2719 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2721 /* Create buffer registry channel. */
2722 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2727 reg_chan
->consumer_key
= ua_chan
->key
;
2728 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2729 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2731 /* Create and add a channel registry to session. */
2732 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2733 ua_chan
->tracing_channel_id
);
2737 buffer_reg_channel_add(reg_sess
, reg_chan
);
2746 /* Safe because the registry channel object was not added to any HT. */
2747 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2753 * Setup buffer registry channel for the given session registry and application
2754 * channel object. If regp pointer is valid, it's set with the created object.
2756 * Return 0 on success else a negative value.
2758 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2759 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2760 struct ust_app
*app
)
2767 assert(ua_chan
->obj
);
2769 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2771 /* Setup all streams for the registry. */
2772 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2777 reg_chan
->obj
.ust
= ua_chan
->obj
;
2778 ua_chan
->obj
= NULL
;
2783 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2784 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2789 * Send buffer registry channel to the application.
2791 * Return 0 on success else a negative value.
2793 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2794 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2795 struct ust_app_channel
*ua_chan
)
2798 struct buffer_reg_stream
*reg_stream
;
2805 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2807 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2812 /* Send channel to the application. */
2813 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2814 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2815 ret
= -ENOTCONN
; /* Caused by app exiting. */
2817 } else if (ret
== -EAGAIN
) {
2818 /* Caused by timeout. */
2819 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
"\".",
2820 app
->pid
, ua_chan
->name
, ua_sess
->tracing_id
);
2821 /* Treat this the same way as an application that is exiting. */
2824 } else if (ret
< 0) {
2828 health_code_update();
2830 /* Send all streams to application. */
2831 pthread_mutex_lock(®_chan
->stream_list_lock
);
2832 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2833 struct ust_app_stream stream
;
2835 ret
= duplicate_stream_object(reg_stream
, &stream
);
2837 goto error_stream_unlock
;
2840 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2842 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2843 ret
= -ENOTCONN
; /* Caused by app exiting. */
2844 } else if (ret
== -EAGAIN
) {
2846 * Caused by timeout.
2847 * Treat this the same way as an application
2850 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
"\".",
2851 app
->pid
, stream
.name
,
2853 ua_sess
->tracing_id
);
2856 (void) release_ust_app_stream(-1, &stream
, app
);
2857 goto error_stream_unlock
;
2861 * The return value is not important here. This function will output an
2864 (void) release_ust_app_stream(-1, &stream
, app
);
2866 ua_chan
->is_sent
= 1;
2868 error_stream_unlock
:
2869 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2875 * Create and send to the application the created buffers with per UID buffers.
2877 * This MUST be called with a RCU read side lock acquired.
2878 * The session list lock and the session's lock must be acquired.
2880 * Return 0 on success else a negative value.
2882 static int create_channel_per_uid(struct ust_app
*app
,
2883 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2884 struct ust_app_channel
*ua_chan
)
2887 struct buffer_reg_uid
*reg_uid
;
2888 struct buffer_reg_channel
*reg_chan
;
2889 struct ltt_session
*session
= NULL
;
2890 enum lttng_error_code notification_ret
;
2891 struct ust_registry_channel
*chan_reg
;
2898 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2900 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2902 * The session creation handles the creation of this global registry
2903 * object. If none can be find, there is a code flow problem or a
2908 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2914 /* Create the buffer registry channel object. */
2915 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2917 ERR("Error creating the UST channel \"%s\" registry instance",
2922 session
= session_find_by_id(ua_sess
->tracing_id
);
2924 assert(pthread_mutex_trylock(&session
->lock
));
2925 assert(session_trylock_list());
2928 * Create the buffers on the consumer side. This call populates the
2929 * ust app channel object with all streams and data object.
2931 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2932 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
2933 session
->most_recent_chunk_id
.value
);
2935 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2939 * Let's remove the previously created buffer registry channel so
2940 * it's not visible anymore in the session registry.
2942 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2943 ua_chan
->tracing_channel_id
, false);
2944 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2945 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2950 * Setup the streams and add it to the session registry.
2952 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2953 ua_chan
, reg_chan
, app
);
2955 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
2959 /* Notify the notification subsystem of the channel's creation. */
2960 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2961 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2962 ua_chan
->tracing_channel_id
);
2964 chan_reg
->consumer_key
= ua_chan
->key
;
2966 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2968 notification_ret
= notification_thread_command_add_channel(
2969 notification_thread_handle
, session
->name
,
2970 ua_sess
->effective_credentials
.uid
,
2971 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
2972 ua_chan
->key
, LTTNG_DOMAIN_UST
,
2973 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2974 if (notification_ret
!= LTTNG_OK
) {
2975 ret
= - (int) notification_ret
;
2976 ERR("Failed to add channel to notification thread");
2981 /* Send buffers to the application. */
2982 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2984 if (ret
!= -ENOTCONN
) {
2985 ERR("Error sending channel to application");
2992 session_put(session
);
2998 * Create and send to the application the created buffers with per PID buffers.
3000 * Called with UST app session lock held.
3001 * The session list lock and the session's lock must be acquired.
3003 * Return 0 on success else a negative value.
3005 static int create_channel_per_pid(struct ust_app
*app
,
3006 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3007 struct ust_app_channel
*ua_chan
)
3010 struct ust_registry_session
*registry
;
3011 enum lttng_error_code cmd_ret
;
3012 struct ltt_session
*session
= NULL
;
3013 uint64_t chan_reg_key
;
3014 struct ust_registry_channel
*chan_reg
;
3021 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3025 registry
= get_session_registry(ua_sess
);
3026 /* The UST app session lock is held, registry shall not be null. */
3029 /* Create and add a new channel registry to session. */
3030 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3032 ERR("Error creating the UST channel \"%s\" registry instance",
3037 session
= session_find_by_id(ua_sess
->tracing_id
);
3040 assert(pthread_mutex_trylock(&session
->lock
));
3041 assert(session_trylock_list());
3043 /* Create and get channel on the consumer side. */
3044 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3045 app
->bits_per_long
, registry
,
3046 session
->most_recent_chunk_id
.value
);
3048 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3050 goto error_remove_from_registry
;
3053 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3055 if (ret
!= -ENOTCONN
) {
3056 ERR("Error sending channel to application");
3058 goto error_remove_from_registry
;
3061 chan_reg_key
= ua_chan
->key
;
3062 pthread_mutex_lock(®istry
->lock
);
3063 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3065 chan_reg
->consumer_key
= ua_chan
->key
;
3066 pthread_mutex_unlock(®istry
->lock
);
3068 cmd_ret
= notification_thread_command_add_channel(
3069 notification_thread_handle
, session
->name
,
3070 ua_sess
->effective_credentials
.uid
,
3071 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
3072 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3073 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3074 if (cmd_ret
!= LTTNG_OK
) {
3075 ret
= - (int) cmd_ret
;
3076 ERR("Failed to add channel to notification thread");
3077 goto error_remove_from_registry
;
3080 error_remove_from_registry
:
3082 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3087 session_put(session
);
3093 * From an already allocated ust app channel, create the channel buffers if
3094 * needed and send them to the application. This MUST be called with a RCU read
3095 * side lock acquired.
3097 * Called with UST app session lock held.
3099 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3100 * the application exited concurrently.
3102 static int ust_app_channel_send(struct ust_app
*app
,
3103 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3104 struct ust_app_channel
*ua_chan
)
3110 assert(usess
->active
);
3114 /* Handle buffer type before sending the channel to the application. */
3115 switch (usess
->buffer_type
) {
3116 case LTTNG_BUFFER_PER_UID
:
3118 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3124 case LTTNG_BUFFER_PER_PID
:
3126 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3138 /* Initialize ust objd object using the received handle and add it. */
3139 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3140 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3142 /* If channel is not enabled, disable it on the tracer */
3143 if (!ua_chan
->enabled
) {
3144 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3155 * Create UST app channel and return it through ua_chanp if not NULL.
3157 * Called with UST app session lock and RCU read-side lock held.
3159 * Return 0 on success or else a negative value.
3161 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3162 struct ltt_ust_channel
*uchan
,
3163 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3164 struct ust_app_channel
**ua_chanp
)
3167 struct lttng_ht_iter iter
;
3168 struct lttng_ht_node_str
*ua_chan_node
;
3169 struct ust_app_channel
*ua_chan
;
3171 /* Lookup channel in the ust app session */
3172 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3173 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3174 if (ua_chan_node
!= NULL
) {
3175 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3179 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3180 if (ua_chan
== NULL
) {
3181 /* Only malloc can fail here */
3185 shadow_copy_channel(ua_chan
, uchan
);
3187 /* Set channel type. */
3188 ua_chan
->attr
.type
= type
;
3190 /* Only add the channel if successful on the tracer side. */
3191 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3194 *ua_chanp
= ua_chan
;
3197 /* Everything went well. */
3205 * Create UST app event and create it on the tracer side.
3207 * Called with ust app session mutex held.
3210 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3211 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3212 struct ust_app
*app
)
3215 struct ust_app_event
*ua_event
;
3217 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3218 if (ua_event
== NULL
) {
3219 /* Only failure mode of alloc_ust_app_event(). */
3223 shadow_copy_event(ua_event
, uevent
);
3225 /* Create it on the tracer side */
3226 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3229 * Not found previously means that it does not exist on the
3230 * tracer. If the application reports that the event existed,
3231 * it means there is a bug in the sessiond or lttng-ust
3232 * (or corruption, etc.)
3234 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3235 ERR("Tracer for application reported that an event being created already existed: "
3236 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3238 app
->pid
, app
->ppid
, app
->uid
,
3244 add_unique_ust_app_event(ua_chan
, ua_event
);
3246 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3253 /* Valid. Calling here is already in a read side lock */
3254 delete_ust_app_event(-1, ua_event
, app
);
3259 * Create UST metadata and open it on the tracer side.
3261 * Called with UST app session lock held and RCU read side lock.
3263 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3264 struct ust_app
*app
, struct consumer_output
*consumer
)
3267 struct ust_app_channel
*metadata
;
3268 struct consumer_socket
*socket
;
3269 struct ust_registry_session
*registry
;
3270 struct ltt_session
*session
= NULL
;
3276 registry
= get_session_registry(ua_sess
);
3277 /* The UST app session is held registry shall not be null. */
3280 pthread_mutex_lock(®istry
->lock
);
3282 /* Metadata already exists for this registry or it was closed previously */
3283 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3288 /* Allocate UST metadata */
3289 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3291 /* malloc() failed */
3296 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3298 /* Need one fd for the channel. */
3299 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3301 ERR("Exhausted number of available FD upon create metadata");
3305 /* Get the right consumer socket for the application. */
3306 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3309 goto error_consumer
;
3313 * Keep metadata key so we can identify it on the consumer side. Assign it
3314 * to the registry *before* we ask the consumer so we avoid the race of the
3315 * consumer requesting the metadata and the ask_channel call on our side
3316 * did not returned yet.
3318 registry
->metadata_key
= metadata
->key
;
3320 session
= session_find_by_id(ua_sess
->tracing_id
);
3323 assert(pthread_mutex_trylock(&session
->lock
));
3324 assert(session_trylock_list());
3327 * Ask the metadata channel creation to the consumer. The metadata object
3328 * will be created by the consumer and kept their. However, the stream is
3329 * never added or monitored until we do a first push metadata to the
3332 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3333 registry
, session
->current_trace_chunk
);
3335 /* Nullify the metadata key so we don't try to close it later on. */
3336 registry
->metadata_key
= 0;
3337 goto error_consumer
;
3341 * The setup command will make the metadata stream be sent to the relayd,
3342 * if applicable, and the thread managing the metadatas. This is important
3343 * because after this point, if an error occurs, the only way the stream
3344 * can be deleted is to be monitored in the consumer.
3346 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3348 /* Nullify the metadata key so we don't try to close it later on. */
3349 registry
->metadata_key
= 0;
3350 goto error_consumer
;
3353 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3354 metadata
->key
, app
->pid
);
3357 lttng_fd_put(LTTNG_FD_APPS
, 1);
3358 delete_ust_app_channel(-1, metadata
, app
);
3360 pthread_mutex_unlock(®istry
->lock
);
3362 session_put(session
);
3368 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3369 * acquired before calling this function.
3371 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3373 struct ust_app
*app
= NULL
;
3374 struct lttng_ht_node_ulong
*node
;
3375 struct lttng_ht_iter iter
;
3377 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3378 node
= lttng_ht_iter_get_node_ulong(&iter
);
3380 DBG2("UST app no found with pid %d", pid
);
3384 DBG2("Found UST app by pid %d", pid
);
3386 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3393 * Allocate and init an UST app object using the registration information and
3394 * the command socket. This is called when the command socket connects to the
3397 * The object is returned on success or else NULL.
3399 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3401 struct ust_app
*lta
= NULL
;
3406 DBG3("UST app creating application for socket %d", sock
);
3408 if ((msg
->bits_per_long
== 64 &&
3409 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3410 || (msg
->bits_per_long
== 32 &&
3411 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3412 ERR("Registration failed: application \"%s\" (pid: %d) has "
3413 "%d-bit long, but no consumerd for this size is available.\n",
3414 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3418 lta
= zmalloc(sizeof(struct ust_app
));
3424 lta
->ppid
= msg
->ppid
;
3425 lta
->uid
= msg
->uid
;
3426 lta
->gid
= msg
->gid
;
3428 lta
->bits_per_long
= msg
->bits_per_long
;
3429 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3430 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3431 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3432 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3433 lta
->long_alignment
= msg
->long_alignment
;
3434 lta
->byte_order
= msg
->byte_order
;
3436 lta
->v_major
= msg
->major
;
3437 lta
->v_minor
= msg
->minor
;
3438 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3439 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3440 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3441 lta
->notify_sock
= -1;
3443 /* Copy name and make sure it's NULL terminated. */
3444 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3445 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3448 * Before this can be called, when receiving the registration information,
3449 * the application compatibility is checked. So, at this point, the
3450 * application can work with this session daemon.
3452 lta
->compatible
= 1;
3454 lta
->pid
= msg
->pid
;
3455 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3457 pthread_mutex_init(<a
->sock_lock
, NULL
);
3458 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3460 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3466 * For a given application object, add it to every hash table.
3468 void ust_app_add(struct ust_app
*app
)
3471 assert(app
->notify_sock
>= 0);
3473 app
->registration_time
= time(NULL
);
3478 * On a re-registration, we want to kick out the previous registration of
3481 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3484 * The socket _should_ be unique until _we_ call close. So, a add_unique
3485 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3486 * already in the table.
3488 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3490 /* Add application to the notify socket hash table. */
3491 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3492 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3494 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
3495 "notify_sock =%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3496 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3503 * Set the application version into the object.
3505 * Return 0 on success else a negative value either an errno code or a
3506 * LTTng-UST error code.
3508 int ust_app_version(struct ust_app
*app
)
3514 pthread_mutex_lock(&app
->sock_lock
);
3515 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3516 pthread_mutex_unlock(&app
->sock_lock
);
3518 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
3519 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
3520 app
->pid
, app
->sock
);
3521 } else if (ret
== -EAGAIN
) {
3522 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
3523 app
->pid
, app
->sock
);
3525 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
3526 ret
, app
->pid
, app
->sock
);
3534 * Unregister app by removing it from the global traceable app list and freeing
3537 * The socket is already closed at this point so no close to sock.
3539 void ust_app_unregister(int sock
)
3541 struct ust_app
*lta
;
3542 struct lttng_ht_node_ulong
*node
;
3543 struct lttng_ht_iter ust_app_sock_iter
;
3544 struct lttng_ht_iter iter
;
3545 struct ust_app_session
*ua_sess
;
3550 /* Get the node reference for a call_rcu */
3551 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3552 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3555 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3556 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3559 * For per-PID buffers, perform "push metadata" and flush all
3560 * application streams before removing app from hash tables,
3561 * ensuring proper behavior of data_pending check.
3562 * Remove sessions so they are not visible during deletion.
3564 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3566 struct ust_registry_session
*registry
;
3568 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3570 /* The session was already removed so scheduled for teardown. */
3574 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3575 (void) ust_app_flush_app_session(lta
, ua_sess
);
3579 * Add session to list for teardown. This is safe since at this point we
3580 * are the only one using this list.
3582 pthread_mutex_lock(&ua_sess
->lock
);
3584 if (ua_sess
->deleted
) {
3585 pthread_mutex_unlock(&ua_sess
->lock
);
3590 * Normally, this is done in the delete session process which is
3591 * executed in the call rcu below. However, upon registration we can't
3592 * afford to wait for the grace period before pushing data or else the
3593 * data pending feature can race between the unregistration and stop
3594 * command where the data pending command is sent *before* the grace
3597 * The close metadata below nullifies the metadata pointer in the
3598 * session so the delete session will NOT push/close a second time.
3600 registry
= get_session_registry(ua_sess
);
3602 /* Push metadata for application before freeing the application. */
3603 (void) push_metadata(registry
, ua_sess
->consumer
);
3606 * Don't ask to close metadata for global per UID buffers. Close
3607 * metadata only on destroy trace session in this case. Also, the
3608 * previous push metadata could have flag the metadata registry to
3609 * close so don't send a close command if closed.
3611 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3612 /* And ask to close it for this session registry. */
3613 (void) close_metadata(registry
, ua_sess
->consumer
);
3616 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3618 pthread_mutex_unlock(&ua_sess
->lock
);
3621 /* Remove application from PID hash table */
3622 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3626 * Remove application from notify hash table. The thread handling the
3627 * notify socket could have deleted the node so ignore on error because
3628 * either way it's valid. The close of that socket is handled by the
3629 * apps_notify_thread.
3631 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3632 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3635 * Ignore return value since the node might have been removed before by an
3636 * add replace during app registration because the PID can be reassigned by
3639 iter
.iter
.node
= <a
->pid_n
.node
;
3640 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3642 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3647 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3654 * Fill events array with all events name of all registered apps.
3656 int ust_app_list_events(struct lttng_event
**events
)
3659 size_t nbmem
, count
= 0;
3660 struct lttng_ht_iter iter
;
3661 struct ust_app
*app
;
3662 struct lttng_event
*tmp_event
;
3664 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3665 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3666 if (tmp_event
== NULL
) {
3667 PERROR("zmalloc ust app events");
3674 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3675 struct lttng_ust_tracepoint_iter uiter
;
3677 health_code_update();
3679 if (!app
->compatible
) {
3681 * TODO: In time, we should notice the caller of this error by
3682 * telling him that this is a version error.
3686 pthread_mutex_lock(&app
->sock_lock
);
3687 handle
= ustctl_tracepoint_list(app
->sock
);
3689 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3690 ERR("UST app list events getting handle failed for app pid %d",
3693 pthread_mutex_unlock(&app
->sock_lock
);
3697 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3698 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3699 /* Handle ustctl error. */
3703 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3704 ERR("UST app tp list get failed for app %d with ret %d",
3707 DBG3("UST app tp list get failed. Application is dead");
3711 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3712 if (release_ret
< 0 &&
3713 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3714 release_ret
!= -EPIPE
) {
3715 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3717 pthread_mutex_unlock(&app
->sock_lock
);
3721 health_code_update();
3722 if (count
>= nbmem
) {
3723 /* In case the realloc fails, we free the memory */
3724 struct lttng_event
*new_tmp_event
;
3727 new_nbmem
= nbmem
<< 1;
3728 DBG2("Reallocating event list from %zu to %zu entries",
3730 new_tmp_event
= realloc(tmp_event
,
3731 new_nbmem
* sizeof(struct lttng_event
));
3732 if (new_tmp_event
== NULL
) {
3735 PERROR("realloc ust app events");
3738 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3739 if (release_ret
< 0 &&
3740 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3741 release_ret
!= -EPIPE
) {
3742 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3744 pthread_mutex_unlock(&app
->sock_lock
);
3747 /* Zero the new memory */
3748 memset(new_tmp_event
+ nbmem
, 0,
3749 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3751 tmp_event
= new_tmp_event
;
3753 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3754 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3755 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3756 tmp_event
[count
].pid
= app
->pid
;
3757 tmp_event
[count
].enabled
= -1;
3760 ret
= ustctl_release_handle(app
->sock
, handle
);
3761 pthread_mutex_unlock(&app
->sock_lock
);
3763 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3764 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
3765 app
->pid
, app
->sock
);
3766 } else if (ret
== -EAGAIN
) {
3767 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
3768 app
->pid
, app
->sock
);
3770 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
3771 ret
, app
->pid
, app
->sock
);
3777 *events
= tmp_event
;
3779 DBG2("UST app list events done (%zu events)", count
);
3784 health_code_update();
3789 * Fill events array with all events name of all registered apps.
3791 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3794 size_t nbmem
, count
= 0;
3795 struct lttng_ht_iter iter
;
3796 struct ust_app
*app
;
3797 struct lttng_event_field
*tmp_event
;
3799 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3800 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3801 if (tmp_event
== NULL
) {
3802 PERROR("zmalloc ust app event fields");
3809 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3810 struct lttng_ust_field_iter uiter
;
3812 health_code_update();
3814 if (!app
->compatible
) {
3816 * TODO: In time, we should notice the caller of this error by
3817 * telling him that this is a version error.
3821 pthread_mutex_lock(&app
->sock_lock
);
3822 handle
= ustctl_tracepoint_field_list(app
->sock
);
3824 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3825 ERR("UST app list field getting handle failed for app pid %d",
3828 pthread_mutex_unlock(&app
->sock_lock
);
3832 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3833 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3834 /* Handle ustctl error. */
3838 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3839 ERR("UST app tp list field failed for app %d with ret %d",
3842 DBG3("UST app tp list field failed. Application is dead");
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 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
4453 app
->pid
, app
->sock
);
4454 pthread_mutex_unlock(&ua_sess
->lock
);
4456 } else if (ret
== -EAGAIN
) {
4457 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
4458 app
->pid
, app
->sock
);
4459 pthread_mutex_unlock(&ua_sess
->lock
);
4463 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
4464 ret
, app
->pid
, app
->sock
);
4469 /* Indicate that the session has been started once */
4470 ua_sess
->started
= 1;
4471 ua_sess
->enabled
= 1;
4473 pthread_mutex_unlock(&ua_sess
->lock
);
4475 health_code_update();
4477 /* Quiescent wait after starting trace */
4478 pthread_mutex_lock(&app
->sock_lock
);
4479 ret
= ustctl_wait_quiescent(app
->sock
);
4480 pthread_mutex_unlock(&app
->sock_lock
);
4482 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4483 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
4484 app
->pid
, app
->sock
);
4485 } else if (ret
== -EAGAIN
) {
4486 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
4487 app
->pid
, app
->sock
);
4489 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
4490 ret
, app
->pid
, app
->sock
);
4496 health_code_update();
4500 pthread_mutex_unlock(&ua_sess
->lock
);
4502 health_code_update();
4507 * Stop tracing for a specific UST session and app.
4510 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4513 struct ust_app_session
*ua_sess
;
4514 struct ust_registry_session
*registry
;
4516 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4520 if (!app
->compatible
) {
4521 goto end_no_session
;
4524 ua_sess
= lookup_session_by_app(usess
, app
);
4525 if (ua_sess
== NULL
) {
4526 goto end_no_session
;
4529 pthread_mutex_lock(&ua_sess
->lock
);
4531 if (ua_sess
->deleted
) {
4532 pthread_mutex_unlock(&ua_sess
->lock
);
4533 goto end_no_session
;
4537 * If started = 0, it means that stop trace has been called for a session
4538 * that was never started. It's possible since we can have a fail start
4539 * from either the application manager thread or the command thread. Simply
4540 * indicate that this is a stop error.
4542 if (!ua_sess
->started
) {
4543 goto error_rcu_unlock
;
4546 health_code_update();
4548 /* This inhibits UST tracing */
4549 pthread_mutex_lock(&app
->sock_lock
);
4550 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4551 pthread_mutex_unlock(&app
->sock_lock
);
4553 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4554 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
4555 app
->pid
, app
->sock
);
4557 } else if (ret
== -EAGAIN
) {
4558 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
4559 app
->pid
, app
->sock
);
4563 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
4564 ret
, app
->pid
, app
->sock
);
4566 goto error_rcu_unlock
;
4569 health_code_update();
4570 ua_sess
->enabled
= 0;
4572 /* Quiescent wait after stopping trace */
4573 pthread_mutex_lock(&app
->sock_lock
);
4574 ret
= ustctl_wait_quiescent(app
->sock
);
4575 pthread_mutex_unlock(&app
->sock_lock
);
4577 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4578 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d)",
4579 app
->pid
, app
->sock
);
4580 } else if (ret
== -EAGAIN
) {
4581 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d)",
4582 app
->pid
, app
->sock
);
4584 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d)",
4585 ret
, app
->pid
, app
->sock
);
4589 health_code_update();
4591 registry
= get_session_registry(ua_sess
);
4593 /* The UST app session is held registry shall not be null. */
4596 /* Push metadata for application before freeing the application. */
4597 (void) push_metadata(registry
, ua_sess
->consumer
);
4600 pthread_mutex_unlock(&ua_sess
->lock
);
4603 health_code_update();
4607 pthread_mutex_unlock(&ua_sess
->lock
);
4609 health_code_update();
4614 int ust_app_flush_app_session(struct ust_app
*app
,
4615 struct ust_app_session
*ua_sess
)
4617 int ret
, retval
= 0;
4618 struct lttng_ht_iter iter
;
4619 struct ust_app_channel
*ua_chan
;
4620 struct consumer_socket
*socket
;
4622 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4626 if (!app
->compatible
) {
4627 goto end_not_compatible
;
4630 pthread_mutex_lock(&ua_sess
->lock
);
4632 if (ua_sess
->deleted
) {
4636 health_code_update();
4638 /* Flushing buffers */
4639 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4642 /* Flush buffers and push metadata. */
4643 switch (ua_sess
->buffer_type
) {
4644 case LTTNG_BUFFER_PER_PID
:
4645 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4647 health_code_update();
4648 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4650 ERR("Error flushing consumer channel");
4656 case LTTNG_BUFFER_PER_UID
:
4662 health_code_update();
4665 pthread_mutex_unlock(&ua_sess
->lock
);
4669 health_code_update();
4674 * Flush buffers for all applications for a specific UST session.
4675 * Called with UST session lock held.
4678 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4683 DBG("Flushing session buffers for all ust apps");
4687 /* Flush buffers and push metadata. */
4688 switch (usess
->buffer_type
) {
4689 case LTTNG_BUFFER_PER_UID
:
4691 struct buffer_reg_uid
*reg
;
4692 struct lttng_ht_iter iter
;
4694 /* Flush all per UID buffers associated to that session. */
4695 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4696 struct ust_registry_session
*ust_session_reg
;
4697 struct buffer_reg_channel
*reg_chan
;
4698 struct consumer_socket
*socket
;
4700 /* Get consumer socket to use to push the metadata.*/
4701 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4704 /* Ignore request if no consumer is found for the session. */
4708 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4709 reg_chan
, node
.node
) {
4711 * The following call will print error values so the return
4712 * code is of little importance because whatever happens, we
4713 * have to try them all.
4715 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4718 ust_session_reg
= reg
->registry
->reg
.ust
;
4719 /* Push metadata. */
4720 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4724 case LTTNG_BUFFER_PER_PID
:
4726 struct ust_app_session
*ua_sess
;
4727 struct lttng_ht_iter iter
;
4728 struct ust_app
*app
;
4730 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4731 ua_sess
= lookup_session_by_app(usess
, app
);
4732 if (ua_sess
== NULL
) {
4735 (void) ust_app_flush_app_session(app
, ua_sess
);
4746 health_code_update();
4751 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
4752 struct ust_app_session
*ua_sess
)
4755 struct lttng_ht_iter iter
;
4756 struct ust_app_channel
*ua_chan
;
4757 struct consumer_socket
*socket
;
4759 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
4763 if (!app
->compatible
) {
4764 goto end_not_compatible
;
4767 pthread_mutex_lock(&ua_sess
->lock
);
4769 if (ua_sess
->deleted
) {
4773 health_code_update();
4775 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4778 ERR("Failed to find consumer (%" PRIu32
") socket",
4779 app
->bits_per_long
);
4784 /* Clear quiescent state. */
4785 switch (ua_sess
->buffer_type
) {
4786 case LTTNG_BUFFER_PER_PID
:
4787 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
4788 ua_chan
, node
.node
) {
4789 health_code_update();
4790 ret
= consumer_clear_quiescent_channel(socket
,
4793 ERR("Error clearing quiescent state for consumer channel");
4799 case LTTNG_BUFFER_PER_UID
:
4806 health_code_update();
4809 pthread_mutex_unlock(&ua_sess
->lock
);
4813 health_code_update();
4818 * Clear quiescent state in each stream for all applications for a
4819 * specific UST session.
4820 * Called with UST session lock held.
4823 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
4828 DBG("Clearing stream quiescent state for all ust apps");
4832 switch (usess
->buffer_type
) {
4833 case LTTNG_BUFFER_PER_UID
:
4835 struct lttng_ht_iter iter
;
4836 struct buffer_reg_uid
*reg
;
4839 * Clear quiescent for all per UID buffers associated to
4842 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4843 struct consumer_socket
*socket
;
4844 struct buffer_reg_channel
*reg_chan
;
4846 /* Get associated consumer socket.*/
4847 socket
= consumer_find_socket_by_bitness(
4848 reg
->bits_per_long
, usess
->consumer
);
4851 * Ignore request if no consumer is found for
4857 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
4858 &iter
.iter
, reg_chan
, node
.node
) {
4860 * The following call will print error values so
4861 * the return code is of little importance
4862 * because whatever happens, we have to try them
4865 (void) consumer_clear_quiescent_channel(socket
,
4866 reg_chan
->consumer_key
);
4871 case LTTNG_BUFFER_PER_PID
:
4873 struct ust_app_session
*ua_sess
;
4874 struct lttng_ht_iter iter
;
4875 struct ust_app
*app
;
4877 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
4879 ua_sess
= lookup_session_by_app(usess
, app
);
4880 if (ua_sess
== NULL
) {
4883 (void) ust_app_clear_quiescent_app_session(app
,
4895 health_code_update();
4900 * Destroy a specific UST session in apps.
4902 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4905 struct ust_app_session
*ua_sess
;
4906 struct lttng_ht_iter iter
;
4907 struct lttng_ht_node_u64
*node
;
4909 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4913 if (!app
->compatible
) {
4917 __lookup_session_by_app(usess
, app
, &iter
);
4918 node
= lttng_ht_iter_get_node_u64(&iter
);
4920 /* Session is being or is deleted. */
4923 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4925 health_code_update();
4926 destroy_app_session(app
, ua_sess
);
4928 health_code_update();
4930 /* Quiescent wait after stopping trace */
4931 pthread_mutex_lock(&app
->sock_lock
);
4932 ret
= ustctl_wait_quiescent(app
->sock
);
4933 pthread_mutex_unlock(&app
->sock_lock
);
4935 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4936 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d)",
4937 app
->pid
, app
->sock
);
4938 } else if (ret
== -EAGAIN
) {
4939 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d)",
4940 app
->pid
, app
->sock
);
4942 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d)",
4943 ret
, app
->pid
, app
->sock
);
4948 health_code_update();
4953 * Start tracing for the UST session.
4955 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4957 struct lttng_ht_iter iter
;
4958 struct ust_app
*app
;
4960 DBG("Starting all UST traces");
4963 * Even though the start trace might fail, flag this session active so
4964 * other application coming in are started by default.
4971 * In a start-stop-start use-case, we need to clear the quiescent state
4972 * of each channel set by the prior stop command, thus ensuring that a
4973 * following stop or destroy is sure to grab a timestamp_end near those
4974 * operations, even if the packet is empty.
4976 (void) ust_app_clear_quiescent_session(usess
);
4978 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4979 ust_app_global_update(usess
, app
);
4988 * Start tracing for the UST session.
4989 * Called with UST session lock held.
4991 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4994 struct lttng_ht_iter iter
;
4995 struct ust_app
*app
;
4997 DBG("Stopping all UST traces");
5000 * Even though the stop trace might fail, flag this session inactive so
5001 * other application coming in are not started by default.
5007 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5008 ret
= ust_app_stop_trace(usess
, app
);
5010 /* Continue to next apps even on error */
5015 (void) ust_app_flush_session(usess
);
5023 * Destroy app UST session.
5025 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5028 struct lttng_ht_iter iter
;
5029 struct ust_app
*app
;
5031 DBG("Destroy all UST traces");
5035 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5036 ret
= destroy_trace(usess
, app
);
5038 /* Continue to next apps even on error */
5048 /* The ua_sess lock must be held by the caller. */
5050 int find_or_create_ust_app_channel(
5051 struct ltt_ust_session
*usess
,
5052 struct ust_app_session
*ua_sess
,
5053 struct ust_app
*app
,
5054 struct ltt_ust_channel
*uchan
,
5055 struct ust_app_channel
**ua_chan
)
5058 struct lttng_ht_iter iter
;
5059 struct lttng_ht_node_str
*ua_chan_node
;
5061 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5062 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5064 *ua_chan
= caa_container_of(ua_chan_node
,
5065 struct ust_app_channel
, node
);
5069 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5078 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5079 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5080 struct ust_app
*app
)
5083 struct ust_app_event
*ua_event
= NULL
;
5085 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5086 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5088 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5093 if (ua_event
->enabled
!= uevent
->enabled
) {
5094 ret
= uevent
->enabled
?
5095 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5096 disable_ust_app_event(ua_sess
, ua_event
, app
);
5105 * The caller must ensure that the application is compatible and is tracked
5106 * by the process attribute trackers.
5109 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5110 struct ust_app
*app
)
5113 struct cds_lfht_iter uchan_iter
;
5114 struct ltt_ust_channel
*uchan
;
5115 struct ust_app_session
*ua_sess
= NULL
;
5118 * The application's configuration should only be synchronized for
5121 assert(usess
->active
);
5123 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5125 /* Tracer is probably gone or ENOMEM. */
5130 pthread_mutex_lock(&ua_sess
->lock
);
5131 if (ua_sess
->deleted
) {
5132 pthread_mutex_unlock(&ua_sess
->lock
);
5138 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5140 struct ust_app_channel
*ua_chan
;
5141 struct cds_lfht_iter uevent_iter
;
5142 struct ltt_ust_event
*uevent
;
5145 * Search for a matching ust_app_channel. If none is found,
5146 * create it. Creating the channel will cause the ua_chan
5147 * structure to be allocated, the channel buffers to be
5148 * allocated (if necessary) and sent to the application, and
5149 * all enabled contexts will be added to the channel.
5151 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5152 app
, uchan
, &ua_chan
);
5154 /* Tracer is probably gone or ENOMEM. */
5159 /* ua_chan will be NULL for the metadata channel */
5163 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5165 ret
= ust_app_channel_synchronize_event(ua_chan
,
5166 uevent
, ua_sess
, app
);
5172 if (ua_chan
->enabled
!= uchan
->enabled
) {
5173 ret
= uchan
->enabled
?
5174 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5175 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5183 * Create the metadata for the application. This returns gracefully if a
5184 * metadata was already set for the session.
5186 * The metadata channel must be created after the data channels as the
5187 * consumer daemon assumes this ordering. When interacting with a relay
5188 * daemon, the consumer will use this assumption to send the
5189 * "STREAMS_SENT" message to the relay daemon.
5191 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5199 pthread_mutex_unlock(&ua_sess
->lock
);
5200 /* Everything went well at this point. */
5205 pthread_mutex_unlock(&ua_sess
->lock
);
5208 destroy_app_session(app
, ua_sess
);
5214 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5216 struct ust_app_session
*ua_sess
;
5218 ua_sess
= lookup_session_by_app(usess
, app
);
5219 if (ua_sess
== NULL
) {
5222 destroy_app_session(app
, ua_sess
);
5226 * Add channels/events from UST global domain to registered apps at sock.
5228 * Called with session lock held.
5229 * Called with RCU read-side lock held.
5231 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5234 assert(usess
->active
);
5236 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5237 app
->sock
, usess
->id
);
5239 if (!app
->compatible
) {
5242 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5244 trace_ust_id_tracker_lookup(
5245 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5247 trace_ust_id_tracker_lookup(
5248 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5251 * Synchronize the application's internal tracing configuration
5252 * and start tracing.
5254 ust_app_synchronize(usess
, app
);
5255 ust_app_start_trace(usess
, app
);
5257 ust_app_global_destroy(usess
, app
);
5262 * Called with session lock held.
5264 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5266 struct lttng_ht_iter iter
;
5267 struct ust_app
*app
;
5270 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5271 ust_app_global_update(usess
, app
);
5277 * Add context to a specific channel for global UST domain.
5279 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5280 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5283 struct lttng_ht_node_str
*ua_chan_node
;
5284 struct lttng_ht_iter iter
, uiter
;
5285 struct ust_app_channel
*ua_chan
= NULL
;
5286 struct ust_app_session
*ua_sess
;
5287 struct ust_app
*app
;
5289 assert(usess
->active
);
5292 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5293 if (!app
->compatible
) {
5295 * TODO: In time, we should notice the caller of this error by
5296 * telling him that this is a version error.
5300 ua_sess
= lookup_session_by_app(usess
, app
);
5301 if (ua_sess
== NULL
) {
5305 pthread_mutex_lock(&ua_sess
->lock
);
5307 if (ua_sess
->deleted
) {
5308 pthread_mutex_unlock(&ua_sess
->lock
);
5312 /* Lookup channel in the ust app session */
5313 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5314 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5315 if (ua_chan_node
== NULL
) {
5318 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5320 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5325 pthread_mutex_unlock(&ua_sess
->lock
);
5333 * Receive registration and populate the given msg structure.
5335 * On success return 0 else a negative value returned by the ustctl call.
5337 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5340 uint32_t pid
, ppid
, uid
, gid
;
5344 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5345 &pid
, &ppid
, &uid
, &gid
,
5346 &msg
->bits_per_long
,
5347 &msg
->uint8_t_alignment
,
5348 &msg
->uint16_t_alignment
,
5349 &msg
->uint32_t_alignment
,
5350 &msg
->uint64_t_alignment
,
5351 &msg
->long_alignment
,
5358 case LTTNG_UST_ERR_EXITING
:
5359 DBG3("UST app recv reg message failed. Application died");
5361 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5362 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5363 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5364 LTTNG_UST_ABI_MINOR_VERSION
);
5367 ERR("UST app recv reg message failed with ret %d", ret
);
5372 msg
->pid
= (pid_t
) pid
;
5373 msg
->ppid
= (pid_t
) ppid
;
5374 msg
->uid
= (uid_t
) uid
;
5375 msg
->gid
= (gid_t
) gid
;
5382 * Return a ust app session object using the application object and the
5383 * session object descriptor has a key. If not found, NULL is returned.
5384 * A RCU read side lock MUST be acquired when calling this function.
5386 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5389 struct lttng_ht_node_ulong
*node
;
5390 struct lttng_ht_iter iter
;
5391 struct ust_app_session
*ua_sess
= NULL
;
5395 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5396 node
= lttng_ht_iter_get_node_ulong(&iter
);
5398 DBG2("UST app session find by objd %d not found", objd
);
5402 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5409 * Return a ust app channel object using the application object and the channel
5410 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5411 * lock MUST be acquired before calling this function.
5413 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5416 struct lttng_ht_node_ulong
*node
;
5417 struct lttng_ht_iter iter
;
5418 struct ust_app_channel
*ua_chan
= NULL
;
5422 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5423 node
= lttng_ht_iter_get_node_ulong(&iter
);
5425 DBG2("UST app channel find by objd %d not found", objd
);
5429 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5436 * Reply to a register channel notification from an application on the notify
5437 * socket. The channel metadata is also created.
5439 * The session UST registry lock is acquired in this function.
5441 * On success 0 is returned else a negative value.
5443 static int reply_ust_register_channel(int sock
, int cobjd
,
5444 size_t nr_fields
, struct ustctl_field
*fields
)
5446 int ret
, ret_code
= 0;
5448 uint64_t chan_reg_key
;
5449 enum ustctl_channel_header type
;
5450 struct ust_app
*app
;
5451 struct ust_app_channel
*ua_chan
;
5452 struct ust_app_session
*ua_sess
;
5453 struct ust_registry_session
*registry
;
5454 struct ust_registry_channel
*chan_reg
;
5458 /* Lookup application. If not found, there is a code flow error. */
5459 app
= find_app_by_notify_sock(sock
);
5461 DBG("Application socket %d is being torn down. Abort event notify",
5464 goto error_rcu_unlock
;
5467 /* Lookup channel by UST object descriptor. */
5468 ua_chan
= find_channel_by_objd(app
, cobjd
);
5470 DBG("Application channel is being torn down. Abort event notify");
5472 goto error_rcu_unlock
;
5475 assert(ua_chan
->session
);
5476 ua_sess
= ua_chan
->session
;
5478 /* Get right session registry depending on the session buffer type. */
5479 registry
= get_session_registry(ua_sess
);
5481 DBG("Application session is being torn down. Abort event notify");
5483 goto error_rcu_unlock
;
5486 /* Depending on the buffer type, a different channel key is used. */
5487 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5488 chan_reg_key
= ua_chan
->tracing_channel_id
;
5490 chan_reg_key
= ua_chan
->key
;
5493 pthread_mutex_lock(®istry
->lock
);
5495 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5498 /* Channel id is set during the object creation. */
5499 chan_id
= chan_reg
->chan_id
;
5501 if (!chan_reg
->register_done
) {
5503 * TODO: eventually use the registry event count for
5504 * this channel to better guess header type for per-pid
5507 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5508 chan_reg
->nr_ctx_fields
= nr_fields
;
5509 chan_reg
->ctx_fields
= fields
;
5511 chan_reg
->header_type
= type
;
5513 /* Get current already assigned values. */
5514 type
= chan_reg
->header_type
;
5517 * Validate that the context fields match between
5518 * registry and newcoming application.
5520 if (!match_lttng_ust_ctl_field_array(chan_reg
->ctx_fields
,
5521 chan_reg
->nr_ctx_fields
,
5522 fields
, nr_fields
)) {
5523 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
5524 app
->pid
, app
->sock
);
5530 /* Append to metadata */
5531 if (!chan_reg
->metadata_dumped
) {
5532 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5534 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5540 DBG3("UST app replying to register channel key %" PRIu64
5541 " with id %u, type = %d, ret = %d", chan_reg_key
, chan_id
, type
,
5544 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5546 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5547 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
5548 app
->pid
, app
->sock
);
5549 } else if (ret
== -EAGAIN
) {
5550 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
5551 app
->pid
, app
->sock
);
5553 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
5554 ret
, app
->pid
, app
->sock
);
5559 /* This channel registry registration is completed. */
5560 chan_reg
->register_done
= 1;
5563 pthread_mutex_unlock(®istry
->lock
);
5571 * Add event to the UST channel registry. When the event is added to the
5572 * registry, the metadata is also created. Once done, this replies to the
5573 * application with the appropriate error code.
5575 * The session UST registry lock is acquired in the function.
5577 * On success 0 is returned else a negative value.
5579 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5580 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5581 int loglevel_value
, char *model_emf_uri
)
5584 uint32_t event_id
= 0;
5585 uint64_t chan_reg_key
;
5586 struct ust_app
*app
;
5587 struct ust_app_channel
*ua_chan
;
5588 struct ust_app_session
*ua_sess
;
5589 struct ust_registry_session
*registry
;
5593 /* Lookup application. If not found, there is a code flow error. */
5594 app
= find_app_by_notify_sock(sock
);
5596 DBG("Application socket %d is being torn down. Abort event notify",
5599 goto error_rcu_unlock
;
5602 /* Lookup channel by UST object descriptor. */
5603 ua_chan
= find_channel_by_objd(app
, cobjd
);
5605 DBG("Application channel is being torn down. Abort event notify");
5607 goto error_rcu_unlock
;
5610 assert(ua_chan
->session
);
5611 ua_sess
= ua_chan
->session
;
5613 registry
= get_session_registry(ua_sess
);
5615 DBG("Application session is being torn down. Abort event notify");
5617 goto error_rcu_unlock
;
5620 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5621 chan_reg_key
= ua_chan
->tracing_channel_id
;
5623 chan_reg_key
= ua_chan
->key
;
5626 pthread_mutex_lock(®istry
->lock
);
5629 * From this point on, this call acquires the ownership of the sig, fields
5630 * and model_emf_uri meaning any free are done inside it if needed. These
5631 * three variables MUST NOT be read/write after this.
5633 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5634 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5635 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5639 model_emf_uri
= NULL
;
5642 * The return value is returned to ustctl so in case of an error, the
5643 * application can be notified. In case of an error, it's important not to
5644 * return a negative error or else the application will get closed.
5646 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5648 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5649 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
5650 app
->pid
, app
->sock
);
5651 } else if (ret
== -EAGAIN
) {
5652 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
5653 app
->pid
, app
->sock
);
5655 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
5656 ret
, app
->pid
, app
->sock
);
5659 * No need to wipe the create event since the application socket will
5660 * get close on error hence cleaning up everything by itself.
5665 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5669 pthread_mutex_unlock(®istry
->lock
);
5674 free(model_emf_uri
);
5679 * Add enum to the UST session registry. Once done, this replies to the
5680 * application with the appropriate error code.
5682 * The session UST registry lock is acquired within this function.
5684 * On success 0 is returned else a negative value.
5686 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
5687 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
5689 int ret
= 0, ret_code
;
5690 struct ust_app
*app
;
5691 struct ust_app_session
*ua_sess
;
5692 struct ust_registry_session
*registry
;
5693 uint64_t enum_id
= -1ULL;
5697 /* Lookup application. If not found, there is a code flow error. */
5698 app
= find_app_by_notify_sock(sock
);
5700 /* Return an error since this is not an error */
5701 DBG("Application socket %d is being torn down. Aborting enum registration",
5705 goto error_rcu_unlock
;
5708 /* Lookup session by UST object descriptor. */
5709 ua_sess
= find_session_by_objd(app
, sobjd
);
5711 /* Return an error since this is not an error */
5712 DBG("Application session is being torn down (session not found Aborting enum registration.");
5714 goto error_rcu_unlock
;
5717 registry
= get_session_registry(ua_sess
);
5719 DBG("Application session is being torn down (registry not found Aborting enum registration.");
5721 goto error_rcu_unlock
;
5724 pthread_mutex_lock(®istry
->lock
);
5727 * From this point on, the callee acquires the ownership of
5728 * entries. The variable entries MUST NOT be read/written after
5731 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
5732 entries
, nr_entries
, &enum_id
);
5736 * The return value is returned to ustctl so in case of an error, the
5737 * application can be notified. In case of an error, it's important not to
5738 * return a negative error or else the application will get closed.
5740 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
5742 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5743 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
5744 app
->pid
, app
->sock
);
5745 } else if (ret
== -EAGAIN
) {
5746 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
5747 app
->pid
, app
->sock
);
5749 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
5750 ret
, app
->pid
, app
->sock
);
5753 * No need to wipe the create enum since the application socket will
5754 * get close on error hence cleaning up everything by itself.
5759 DBG3("UST registry enum %s added successfully or already found", name
);
5762 pthread_mutex_unlock(®istry
->lock
);
5769 * Handle application notification through the given notify socket.
5771 * Return 0 on success or else a negative value.
5773 int ust_app_recv_notify(int sock
)
5776 enum ustctl_notify_cmd cmd
;
5778 DBG3("UST app receiving notify from sock %d", sock
);
5780 ret
= ustctl_recv_notify(sock
, &cmd
);
5782 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5783 DBG3("UST app recv notify failed. Application died: sock = %d",
5785 } else if (ret
== -EAGAIN
) {
5786 WARN("UST app recv notify failed. Communication time out: sock = %d",
5789 ERR("UST app recv notify failed with ret %d: sock = %d",
5796 case USTCTL_NOTIFY_CMD_EVENT
:
5798 int sobjd
, cobjd
, loglevel_value
;
5799 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5801 struct ustctl_field
*fields
;
5803 DBG2("UST app ustctl register event received");
5805 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5806 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5809 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5810 DBG3("UST app recv event failed. Application died: sock = %d",
5812 } else if (ret
== -EAGAIN
) {
5813 WARN("UST app recv event failed. Communication time out: sock = %d",
5816 ERR("UST app recv event failed with ret %d: sock = %d",
5823 * Add event to the UST registry coming from the notify socket. This
5824 * call will free if needed the sig, fields and model_emf_uri. This
5825 * code path loses the ownsership of these variables and transfer them
5826 * to the this function.
5828 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5829 fields
, loglevel_value
, model_emf_uri
);
5836 case USTCTL_NOTIFY_CMD_CHANNEL
:
5840 struct ustctl_field
*fields
;
5842 DBG2("UST app ustctl register channel received");
5844 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5847 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5848 DBG3("UST app recv channel failed. Application died: sock = %d",
5850 } else if (ret
== -EAGAIN
) {
5851 WARN("UST app recv channel failed. Communication time out: sock = %d",
5854 ERR("UST app recv channel failed with ret %d: sock = %d)",
5861 * The fields ownership are transfered to this function call meaning
5862 * that if needed it will be freed. After this, it's invalid to access
5863 * fields or clean it up.
5865 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
5873 case USTCTL_NOTIFY_CMD_ENUM
:
5876 char name
[LTTNG_UST_SYM_NAME_LEN
];
5878 struct ustctl_enum_entry
*entries
;
5880 DBG2("UST app ustctl register enum received");
5882 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
5883 &entries
, &nr_entries
);
5885 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5886 DBG3("UST app recv enum failed. Application died: sock = %d",
5888 } else if (ret
== -EAGAIN
) {
5889 WARN("UST app recv enum failed. Communication time out: sock = %d",
5892 ERR("UST app recv enum failed with ret %d: sock = %d",
5898 /* Callee assumes ownership of entries */
5899 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
5900 entries
, nr_entries
);
5908 /* Should NEVER happen. */
5917 * Once the notify socket hangs up, this is called. First, it tries to find the
5918 * corresponding application. On failure, the call_rcu to close the socket is
5919 * executed. If an application is found, it tries to delete it from the notify
5920 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5922 * Note that an object needs to be allocated here so on ENOMEM failure, the
5923 * call RCU is not done but the rest of the cleanup is.
5925 void ust_app_notify_sock_unregister(int sock
)
5928 struct lttng_ht_iter iter
;
5929 struct ust_app
*app
;
5930 struct ust_app_notify_sock_obj
*obj
;
5936 obj
= zmalloc(sizeof(*obj
));
5939 * An ENOMEM is kind of uncool. If this strikes we continue the
5940 * procedure but the call_rcu will not be called. In this case, we
5941 * accept the fd leak rather than possibly creating an unsynchronized
5942 * state between threads.
5944 * TODO: The notify object should be created once the notify socket is
5945 * registered and stored independantely from the ust app object. The
5946 * tricky part is to synchronize the teardown of the application and
5947 * this notify object. Let's keep that in mind so we can avoid this
5948 * kind of shenanigans with ENOMEM in the teardown path.
5955 DBG("UST app notify socket unregister %d", sock
);
5958 * Lookup application by notify socket. If this fails, this means that the
5959 * hash table delete has already been done by the application
5960 * unregistration process so we can safely close the notify socket in a
5963 app
= find_app_by_notify_sock(sock
);
5968 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5971 * Whatever happens here either we fail or succeed, in both cases we have
5972 * to close the socket after a grace period to continue to the call RCU
5973 * here. If the deletion is successful, the application is not visible
5974 * anymore by other threads and is it fails it means that it was already
5975 * deleted from the hash table so either way we just have to close the
5978 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5984 * Close socket after a grace period to avoid for the socket to be reused
5985 * before the application object is freed creating potential race between
5986 * threads trying to add unique in the global hash table.
5989 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5994 * Destroy a ust app data structure and free its memory.
5996 void ust_app_destroy(struct ust_app
*app
)
6002 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6006 * Take a snapshot for a given UST session. The snapshot is sent to the given
6009 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6011 enum lttng_error_code
ust_app_snapshot_record(
6012 const struct ltt_ust_session
*usess
,
6013 const struct consumer_output
*output
, int wait
,
6014 uint64_t nb_packets_per_stream
)
6017 enum lttng_error_code status
= LTTNG_OK
;
6018 struct lttng_ht_iter iter
;
6019 struct ust_app
*app
;
6020 char *trace_path
= NULL
;
6027 switch (usess
->buffer_type
) {
6028 case LTTNG_BUFFER_PER_UID
:
6030 struct buffer_reg_uid
*reg
;
6032 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6033 struct buffer_reg_channel
*reg_chan
;
6034 struct consumer_socket
*socket
;
6035 char pathname
[PATH_MAX
];
6036 size_t consumer_path_offset
= 0;
6038 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6039 /* Skip since no metadata is present */
6043 /* Get consumer socket to use to push the metadata.*/
6044 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6047 status
= LTTNG_ERR_INVALID
;
6051 memset(pathname
, 0, sizeof(pathname
));
6052 ret
= snprintf(pathname
, sizeof(pathname
),
6053 DEFAULT_UST_TRACE_UID_PATH
,
6054 reg
->uid
, reg
->bits_per_long
);
6056 PERROR("snprintf snapshot path");
6057 status
= LTTNG_ERR_INVALID
;
6060 /* Free path allowed on previous iteration. */
6062 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6063 &consumer_path_offset
);
6065 status
= LTTNG_ERR_INVALID
;
6068 /* Add the UST default trace dir to path. */
6069 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6070 reg_chan
, node
.node
) {
6071 status
= consumer_snapshot_channel(socket
,
6072 reg_chan
->consumer_key
,
6073 output
, 0, usess
->uid
,
6074 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
6075 nb_packets_per_stream
);
6076 if (status
!= LTTNG_OK
) {
6080 status
= consumer_snapshot_channel(socket
,
6081 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6082 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6084 if (status
!= LTTNG_OK
) {
6090 case LTTNG_BUFFER_PER_PID
:
6092 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6093 struct consumer_socket
*socket
;
6094 struct lttng_ht_iter chan_iter
;
6095 struct ust_app_channel
*ua_chan
;
6096 struct ust_app_session
*ua_sess
;
6097 struct ust_registry_session
*registry
;
6098 char pathname
[PATH_MAX
];
6099 size_t consumer_path_offset
= 0;
6101 ua_sess
= lookup_session_by_app(usess
, app
);
6103 /* Session not associated with this app. */
6107 /* Get the right consumer socket for the application. */
6108 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6111 status
= LTTNG_ERR_INVALID
;
6115 /* Add the UST default trace dir to path. */
6116 memset(pathname
, 0, sizeof(pathname
));
6117 ret
= snprintf(pathname
, sizeof(pathname
), "%s",
6120 status
= LTTNG_ERR_INVALID
;
6121 PERROR("snprintf snapshot path");
6124 /* Free path allowed on previous iteration. */
6126 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6127 &consumer_path_offset
);
6129 status
= LTTNG_ERR_INVALID
;
6132 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6133 ua_chan
, node
.node
) {
6134 status
= consumer_snapshot_channel(socket
,
6135 ua_chan
->key
, output
, 0,
6136 ua_sess
->effective_credentials
6138 ua_sess
->effective_credentials
6140 &trace_path
[consumer_path_offset
], wait
,
6141 nb_packets_per_stream
);
6145 case LTTNG_ERR_CHAN_NOT_FOUND
:
6152 registry
= get_session_registry(ua_sess
);
6154 DBG("Application session is being torn down. Skip application.");
6157 status
= consumer_snapshot_channel(socket
,
6158 registry
->metadata_key
, output
, 1,
6159 ua_sess
->effective_credentials
.uid
,
6160 ua_sess
->effective_credentials
.gid
,
6161 &trace_path
[consumer_path_offset
], wait
, 0);
6165 case LTTNG_ERR_CHAN_NOT_FOUND
:
6185 * Return the size taken by one more packet per stream.
6187 uint64_t ust_app_get_size_one_more_packet_per_stream(
6188 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6190 uint64_t tot_size
= 0;
6191 struct ust_app
*app
;
6192 struct lttng_ht_iter iter
;
6196 switch (usess
->buffer_type
) {
6197 case LTTNG_BUFFER_PER_UID
:
6199 struct buffer_reg_uid
*reg
;
6201 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6202 struct buffer_reg_channel
*reg_chan
;
6205 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6206 reg_chan
, node
.node
) {
6207 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6209 * Don't take channel into account if we
6210 * already grab all its packets.
6214 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6220 case LTTNG_BUFFER_PER_PID
:
6223 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6224 struct ust_app_channel
*ua_chan
;
6225 struct ust_app_session
*ua_sess
;
6226 struct lttng_ht_iter chan_iter
;
6228 ua_sess
= lookup_session_by_app(usess
, app
);
6230 /* Session not associated with this app. */
6234 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6235 ua_chan
, node
.node
) {
6236 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6238 * Don't take channel into account if we
6239 * already grab all its packets.
6243 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6257 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6258 struct cds_list_head
*buffer_reg_uid_list
,
6259 struct consumer_output
*consumer
, uint64_t uchan_id
,
6260 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6263 uint64_t consumer_chan_key
;
6268 ret
= buffer_reg_uid_consumer_channel_key(
6269 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6277 ret
= consumer_get_lost_packets(ust_session_id
,
6278 consumer_chan_key
, consumer
, lost
);
6280 ret
= consumer_get_discarded_events(ust_session_id
,
6281 consumer_chan_key
, consumer
, discarded
);
6288 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6289 struct ltt_ust_channel
*uchan
,
6290 struct consumer_output
*consumer
, int overwrite
,
6291 uint64_t *discarded
, uint64_t *lost
)
6294 struct lttng_ht_iter iter
;
6295 struct lttng_ht_node_str
*ua_chan_node
;
6296 struct ust_app
*app
;
6297 struct ust_app_session
*ua_sess
;
6298 struct ust_app_channel
*ua_chan
;
6305 * Iterate over every registered applications. Sum counters for
6306 * all applications containing requested session and channel.
6308 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6309 struct lttng_ht_iter uiter
;
6311 ua_sess
= lookup_session_by_app(usess
, app
);
6312 if (ua_sess
== NULL
) {
6317 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6318 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6319 /* If the session is found for the app, the channel must be there */
6320 assert(ua_chan_node
);
6322 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6327 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6334 uint64_t _discarded
;
6336 ret
= consumer_get_discarded_events(usess
->id
,
6337 ua_chan
->key
, consumer
, &_discarded
);
6341 (*discarded
) += _discarded
;
6350 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6351 struct ust_app
*app
)
6354 struct ust_app_session
*ua_sess
;
6356 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6360 ua_sess
= lookup_session_by_app(usess
, app
);
6361 if (ua_sess
== NULL
) {
6362 /* The session is in teardown process. Ignore and continue. */
6366 pthread_mutex_lock(&ua_sess
->lock
);
6368 if (ua_sess
->deleted
) {
6372 pthread_mutex_lock(&app
->sock_lock
);
6373 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6374 pthread_mutex_unlock(&app
->sock_lock
);
6377 pthread_mutex_unlock(&ua_sess
->lock
);
6381 health_code_update();
6386 * Regenerate the statedump for each app in the session.
6388 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6391 struct lttng_ht_iter iter
;
6392 struct ust_app
*app
;
6394 DBG("Regenerating the metadata for all UST apps");
6398 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6399 if (!app
->compatible
) {
6403 ret
= ust_app_regenerate_statedump(usess
, app
);
6405 /* Continue to the next app even on error */
6416 * Rotate all the channels of a session.
6418 * Return LTTNG_OK on success or else an LTTng error code.
6420 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
6423 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6424 struct lttng_ht_iter iter
;
6425 struct ust_app
*app
;
6426 struct ltt_ust_session
*usess
= session
->ust_session
;
6432 switch (usess
->buffer_type
) {
6433 case LTTNG_BUFFER_PER_UID
:
6435 struct buffer_reg_uid
*reg
;
6437 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6438 struct buffer_reg_channel
*reg_chan
;
6439 struct consumer_socket
*socket
;
6441 /* Get consumer socket to use to push the metadata.*/
6442 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6445 cmd_ret
= LTTNG_ERR_INVALID
;
6449 /* Rotate the data channels. */
6450 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6451 reg_chan
, node
.node
) {
6452 ret
= consumer_rotate_channel(socket
,
6453 reg_chan
->consumer_key
,
6454 usess
->uid
, usess
->gid
,
6456 /* is_metadata_channel */ false);
6458 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6464 * The metadata channel might not be present.
6466 * Consumer stream allocation can be done
6467 * asynchronously and can fail on intermediary
6468 * operations (i.e add context) and lead to data
6469 * channels created with no metadata channel.
6471 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6472 /* Skip since no metadata is present. */
6476 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6478 ret
= consumer_rotate_channel(socket
,
6479 reg
->registry
->reg
.ust
->metadata_key
,
6480 usess
->uid
, usess
->gid
,
6482 /* is_metadata_channel */ true);
6484 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6490 case LTTNG_BUFFER_PER_PID
:
6492 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6493 struct consumer_socket
*socket
;
6494 struct lttng_ht_iter chan_iter
;
6495 struct ust_app_channel
*ua_chan
;
6496 struct ust_app_session
*ua_sess
;
6497 struct ust_registry_session
*registry
;
6499 ua_sess
= lookup_session_by_app(usess
, app
);
6501 /* Session not associated with this app. */
6505 /* Get the right consumer socket for the application. */
6506 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6509 cmd_ret
= LTTNG_ERR_INVALID
;
6513 registry
= get_session_registry(ua_sess
);
6515 DBG("Application session is being torn down. Skip application.");
6519 /* Rotate the data channels. */
6520 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6521 ua_chan
, node
.node
) {
6522 ret
= consumer_rotate_channel(socket
,
6524 ua_sess
->effective_credentials
6526 ua_sess
->effective_credentials
6529 /* is_metadata_channel */ false);
6531 /* Per-PID buffer and application going away. */
6532 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6534 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6539 /* Rotate the metadata channel. */
6540 (void) push_metadata(registry
, usess
->consumer
);
6541 ret
= consumer_rotate_channel(socket
,
6542 registry
->metadata_key
,
6543 ua_sess
->effective_credentials
.uid
,
6544 ua_sess
->effective_credentials
.gid
,
6546 /* is_metadata_channel */ true);
6548 /* Per-PID buffer and application going away. */
6549 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6551 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6569 enum lttng_error_code
ust_app_create_channel_subdirectories(
6570 const struct ltt_ust_session
*usess
)
6572 enum lttng_error_code ret
= LTTNG_OK
;
6573 struct lttng_ht_iter iter
;
6574 enum lttng_trace_chunk_status chunk_status
;
6575 char *pathname_index
;
6578 assert(usess
->current_trace_chunk
);
6581 switch (usess
->buffer_type
) {
6582 case LTTNG_BUFFER_PER_UID
:
6584 struct buffer_reg_uid
*reg
;
6586 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6587 fmt_ret
= asprintf(&pathname_index
,
6588 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
6589 reg
->uid
, reg
->bits_per_long
);
6591 ERR("Failed to format channel index directory");
6592 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6597 * Create the index subdirectory which will take care
6598 * of implicitly creating the channel's path.
6600 chunk_status
= lttng_trace_chunk_create_subdirectory(
6601 usess
->current_trace_chunk
,
6603 free(pathname_index
);
6604 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6605 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6611 case LTTNG_BUFFER_PER_PID
:
6613 struct ust_app
*app
;
6616 * Create the toplevel ust/ directory in case no apps are running.
6618 chunk_status
= lttng_trace_chunk_create_subdirectory(
6619 usess
->current_trace_chunk
,
6620 DEFAULT_UST_TRACE_DIR
);
6621 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6622 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6626 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
6628 struct ust_app_session
*ua_sess
;
6629 struct ust_registry_session
*registry
;
6631 ua_sess
= lookup_session_by_app(usess
, app
);
6633 /* Session not associated with this app. */
6637 registry
= get_session_registry(ua_sess
);
6639 DBG("Application session is being torn down. Skip application.");
6643 fmt_ret
= asprintf(&pathname_index
,
6644 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
6647 ERR("Failed to format channel index directory");
6648 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6652 * Create the index subdirectory which will take care
6653 * of implicitly creating the channel's path.
6655 chunk_status
= lttng_trace_chunk_create_subdirectory(
6656 usess
->current_trace_chunk
,
6658 free(pathname_index
);
6659 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6660 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6677 * Clear all the channels of a session.
6679 * Return LTTNG_OK on success or else an LTTng error code.
6681 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
6684 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6685 struct lttng_ht_iter iter
;
6686 struct ust_app
*app
;
6687 struct ltt_ust_session
*usess
= session
->ust_session
;
6693 if (usess
->active
) {
6694 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
6695 cmd_ret
= LTTNG_ERR_FATAL
;
6699 switch (usess
->buffer_type
) {
6700 case LTTNG_BUFFER_PER_UID
:
6702 struct buffer_reg_uid
*reg
;
6704 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6705 struct buffer_reg_channel
*reg_chan
;
6706 struct consumer_socket
*socket
;
6708 /* Get consumer socket to use to push the metadata.*/
6709 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6712 cmd_ret
= LTTNG_ERR_INVALID
;
6716 /* Clear the data channels. */
6717 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6718 reg_chan
, node
.node
) {
6719 ret
= consumer_clear_channel(socket
,
6720 reg_chan
->consumer_key
);
6726 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6729 * Clear the metadata channel.
6730 * Metadata channel is not cleared per se but we still need to
6731 * perform a rotation operation on it behind the scene.
6733 ret
= consumer_clear_channel(socket
,
6734 reg
->registry
->reg
.ust
->metadata_key
);
6741 case LTTNG_BUFFER_PER_PID
:
6743 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6744 struct consumer_socket
*socket
;
6745 struct lttng_ht_iter chan_iter
;
6746 struct ust_app_channel
*ua_chan
;
6747 struct ust_app_session
*ua_sess
;
6748 struct ust_registry_session
*registry
;
6750 ua_sess
= lookup_session_by_app(usess
, app
);
6752 /* Session not associated with this app. */
6756 /* Get the right consumer socket for the application. */
6757 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6760 cmd_ret
= LTTNG_ERR_INVALID
;
6764 registry
= get_session_registry(ua_sess
);
6766 DBG("Application session is being torn down. Skip application.");
6770 /* Clear the data channels. */
6771 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6772 ua_chan
, node
.node
) {
6773 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
6775 /* Per-PID buffer and application going away. */
6776 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6783 (void) push_metadata(registry
, usess
->consumer
);
6786 * Clear the metadata channel.
6787 * Metadata channel is not cleared per se but we still need to
6788 * perform rotation operation on it behind the scene.
6790 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
6792 /* Per-PID buffer and application going away. */
6793 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6811 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
6812 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
6815 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
6825 * This function skips the metadata channel as the begin/end timestamps of a
6826 * metadata packet are useless.
6828 * Moreover, opening a packet after a "clear" will cause problems for live
6829 * sessions as it will introduce padding that was not part of the first trace
6830 * chunk. The relay daemon expects the content of the metadata stream of
6831 * successive metadata trace chunks to be strict supersets of one another.
6833 * For example, flushing a packet at the beginning of the metadata stream of
6834 * a trace chunk resulting from a "clear" session command will cause the
6835 * size of the metadata stream of the new trace chunk to not match the size of
6836 * the metadata stream of the original chunk. This will confuse the relay
6837 * daemon as the same "offset" in a metadata stream will no longer point
6838 * to the same content.
6840 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
6842 enum lttng_error_code ret
= LTTNG_OK
;
6843 struct lttng_ht_iter iter
;
6844 struct ltt_ust_session
*usess
= session
->ust_session
;
6850 switch (usess
->buffer_type
) {
6851 case LTTNG_BUFFER_PER_UID
:
6853 struct buffer_reg_uid
*reg
;
6855 cds_list_for_each_entry (
6856 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6857 struct buffer_reg_channel
*reg_chan
;
6858 struct consumer_socket
*socket
;
6860 socket
= consumer_find_socket_by_bitness(
6861 reg
->bits_per_long
, usess
->consumer
);
6863 ret
= LTTNG_ERR_FATAL
;
6867 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
6868 &iter
.iter
, reg_chan
, node
.node
) {
6869 const int open_ret
=
6870 consumer_open_channel_packets(
6872 reg_chan
->consumer_key
);
6875 ret
= LTTNG_ERR_UNK
;
6882 case LTTNG_BUFFER_PER_PID
:
6884 struct ust_app
*app
;
6886 cds_lfht_for_each_entry (
6887 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6888 struct consumer_socket
*socket
;
6889 struct lttng_ht_iter chan_iter
;
6890 struct ust_app_channel
*ua_chan
;
6891 struct ust_app_session
*ua_sess
;
6892 struct ust_registry_session
*registry
;
6894 ua_sess
= lookup_session_by_app(usess
, app
);
6896 /* Session not associated with this app. */
6900 /* Get the right consumer socket for the application. */
6901 socket
= consumer_find_socket_by_bitness(
6902 app
->bits_per_long
, usess
->consumer
);
6904 ret
= LTTNG_ERR_FATAL
;
6908 registry
= get_session_registry(ua_sess
);
6910 DBG("Application session is being torn down. Skip application.");
6914 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
6915 &chan_iter
.iter
, ua_chan
, node
.node
) {
6916 const int open_ret
=
6917 consumer_open_channel_packets(
6923 * Per-PID buffer and application going
6926 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6930 ret
= LTTNG_ERR_UNK
;