2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
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 struct lttng_ht
*ust_app_ht
;
39 struct lttng_ht
*ust_app_ht_by_sock
;
40 struct lttng_ht
*ust_app_ht_by_notify_sock
;
43 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
45 /* Next available channel key. Access under next_channel_key_lock. */
46 static uint64_t _next_channel_key
;
47 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
49 /* Next available session ID. Access under next_session_id_lock. */
50 static uint64_t _next_session_id
;
51 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
54 * Return the incremented value of next_channel_key.
56 static uint64_t get_next_channel_key(void)
60 pthread_mutex_lock(&next_channel_key_lock
);
61 ret
= ++_next_channel_key
;
62 pthread_mutex_unlock(&next_channel_key_lock
);
67 * Return the atomically incremented value of next_session_id.
69 static uint64_t get_next_session_id(void)
73 pthread_mutex_lock(&next_session_id_lock
);
74 ret
= ++_next_session_id
;
75 pthread_mutex_unlock(&next_session_id_lock
);
79 static void copy_channel_attr_to_ustctl(
80 struct ustctl_consumer_channel_attr
*attr
,
81 struct lttng_ust_channel_attr
*uattr
)
83 /* Copy event attributes since the layout is different. */
84 attr
->subbuf_size
= uattr
->subbuf_size
;
85 attr
->num_subbuf
= uattr
->num_subbuf
;
86 attr
->overwrite
= uattr
->overwrite
;
87 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
88 attr
->read_timer_interval
= uattr
->read_timer_interval
;
89 attr
->output
= uattr
->output
;
90 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
94 * Match function for the hash table lookup.
96 * It matches an ust app event based on three attributes which are the event
97 * name, the filter bytecode and the loglevel.
99 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
101 struct ust_app_event
*event
;
102 const struct ust_app_ht_key
*key
;
103 int ev_loglevel_value
;
108 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
110 ev_loglevel_value
= event
->attr
.loglevel
;
112 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
115 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
119 /* Event loglevel. */
120 if (ev_loglevel_value
!= key
->loglevel_type
) {
121 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
122 && key
->loglevel_type
== 0 &&
123 ev_loglevel_value
== -1) {
125 * Match is accepted. This is because on event creation, the
126 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
127 * -1 are accepted for this loglevel type since 0 is the one set by
128 * the API when receiving an enable event.
135 /* One of the filters is NULL, fail. */
136 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
140 if (key
->filter
&& event
->filter
) {
141 /* Both filters exists, check length followed by the bytecode. */
142 if (event
->filter
->len
!= key
->filter
->len
||
143 memcmp(event
->filter
->data
, key
->filter
->data
,
144 event
->filter
->len
) != 0) {
149 /* One of the exclusions is NULL, fail. */
150 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
154 if (key
->exclusion
&& event
->exclusion
) {
155 /* Both exclusions exists, check count followed by the names. */
156 if (event
->exclusion
->count
!= key
->exclusion
->count
||
157 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
158 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
172 * Unique add of an ust app event in the given ht. This uses the custom
173 * ht_match_ust_app_event match function and the event name as hash.
175 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
176 struct ust_app_event
*event
)
178 struct cds_lfht_node
*node_ptr
;
179 struct ust_app_ht_key key
;
183 assert(ua_chan
->events
);
186 ht
= ua_chan
->events
;
187 key
.name
= event
->attr
.name
;
188 key
.filter
= event
->filter
;
189 key
.loglevel_type
= event
->attr
.loglevel
;
190 key
.exclusion
= event
->exclusion
;
192 node_ptr
= cds_lfht_add_unique(ht
->ht
,
193 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
194 ht_match_ust_app_event
, &key
, &event
->node
.node
);
195 assert(node_ptr
== &event
->node
.node
);
199 * Close the notify socket from the given RCU head object. This MUST be called
200 * through a call_rcu().
202 static void close_notify_sock_rcu(struct rcu_head
*head
)
205 struct ust_app_notify_sock_obj
*obj
=
206 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
208 /* Must have a valid fd here. */
209 assert(obj
->fd
>= 0);
211 ret
= close(obj
->fd
);
213 ERR("close notify sock %d RCU", obj
->fd
);
215 lttng_fd_put(LTTNG_FD_APPS
, 1);
221 * Return the session registry according to the buffer type of the given
224 * A registry per UID object MUST exists before calling this function or else
225 * it assert() if not found. RCU read side lock must be acquired.
227 static struct ust_registry_session
*get_session_registry(
228 struct ust_app_session
*ua_sess
)
230 struct ust_registry_session
*registry
= NULL
;
234 switch (ua_sess
->buffer_type
) {
235 case LTTNG_BUFFER_PER_PID
:
237 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
241 registry
= reg_pid
->registry
->reg
.ust
;
244 case LTTNG_BUFFER_PER_UID
:
246 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
247 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
248 ua_sess
->real_credentials
.uid
);
252 registry
= reg_uid
->registry
->reg
.ust
;
264 * Delete ust context safely. RCU read lock must be held before calling
268 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
276 pthread_mutex_lock(&app
->sock_lock
);
277 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
278 pthread_mutex_unlock(&app
->sock_lock
);
280 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
281 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
282 app
->pid
, app
->sock
);
283 } else if (ret
== -EAGAIN
) {
284 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
285 app
->pid
, app
->sock
);
287 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
288 ua_ctx
->obj
->handle
, ret
,
289 app
->pid
, app
->sock
);
298 * Delete ust app event safely. RCU read lock must be held before calling
302 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
309 free(ua_event
->filter
);
310 if (ua_event
->exclusion
!= NULL
)
311 free(ua_event
->exclusion
);
312 if (ua_event
->obj
!= NULL
) {
313 pthread_mutex_lock(&app
->sock_lock
);
314 ret
= ustctl_release_object(sock
, ua_event
->obj
);
315 pthread_mutex_unlock(&app
->sock_lock
);
317 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
318 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
319 app
->pid
, app
->sock
);
320 } else if (ret
== -EAGAIN
) {
321 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
322 app
->pid
, app
->sock
);
324 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
325 ret
, app
->pid
, app
->sock
);
334 * Release ust data object of the given stream.
336 * Return 0 on success or else a negative value.
338 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
346 pthread_mutex_lock(&app
->sock_lock
);
347 ret
= ustctl_release_object(sock
, stream
->obj
);
348 pthread_mutex_unlock(&app
->sock_lock
);
350 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
351 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
352 app
->pid
, app
->sock
);
353 } else if (ret
== -EAGAIN
) {
354 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
355 app
->pid
, app
->sock
);
357 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
358 ret
, app
->pid
, app
->sock
);
361 lttng_fd_put(LTTNG_FD_APPS
, 2);
369 * Delete ust app stream safely. RCU read lock must be held before calling
373 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
378 (void) release_ust_app_stream(sock
, stream
, app
);
383 * We need to execute ht_destroy outside of RCU read-side critical
384 * section and outside of call_rcu thread, so we postpone its execution
385 * using ht_cleanup_push. It is simpler than to change the semantic of
386 * the many callers of delete_ust_app_session().
389 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
391 struct ust_app_channel
*ua_chan
=
392 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
394 ht_cleanup_push(ua_chan
->ctx
);
395 ht_cleanup_push(ua_chan
->events
);
400 * Extract the lost packet or discarded events counter when the channel is
401 * being deleted and store the value in the parent channel so we can
402 * access it from lttng list and at stop/destroy.
404 * The session list lock must be held by the caller.
407 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
409 uint64_t discarded
= 0, lost
= 0;
410 struct ltt_session
*session
;
411 struct ltt_ust_channel
*uchan
;
413 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
418 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
419 if (!session
|| !session
->ust_session
) {
421 * Not finding the session is not an error because there are
422 * multiple ways the channels can be torn down.
424 * 1) The session daemon can initiate the destruction of the
425 * ust app session after receiving a destroy command or
426 * during its shutdown/teardown.
427 * 2) The application, since we are in per-pid tracing, is
428 * unregistering and tearing down its ust app session.
430 * Both paths are protected by the session list lock which
431 * ensures that the accounting of lost packets and discarded
432 * events is done exactly once. The session is then unpublished
433 * from the session list, resulting in this condition.
438 if (ua_chan
->attr
.overwrite
) {
439 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
440 ua_chan
->key
, session
->ust_session
->consumer
,
443 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
444 ua_chan
->key
, session
->ust_session
->consumer
,
447 uchan
= trace_ust_find_channel_by_name(
448 session
->ust_session
->domain_global
.channels
,
451 ERR("Missing UST channel to store discarded counters");
455 uchan
->per_pid_closed_app_discarded
+= discarded
;
456 uchan
->per_pid_closed_app_lost
+= lost
;
461 session_put(session
);
466 * Delete ust app channel safely. RCU read lock must be held before calling
469 * The session list lock must be held by the caller.
472 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
476 struct lttng_ht_iter iter
;
477 struct ust_app_event
*ua_event
;
478 struct ust_app_ctx
*ua_ctx
;
479 struct ust_app_stream
*stream
, *stmp
;
480 struct ust_registry_session
*registry
;
484 DBG3("UST app deleting channel %s", ua_chan
->name
);
487 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
488 cds_list_del(&stream
->list
);
489 delete_ust_app_stream(sock
, stream
, app
);
493 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
494 cds_list_del(&ua_ctx
->list
);
495 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
497 delete_ust_app_ctx(sock
, ua_ctx
, app
);
501 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
503 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
505 delete_ust_app_event(sock
, ua_event
, app
);
508 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
509 /* Wipe and free registry from session registry. */
510 registry
= get_session_registry(ua_chan
->session
);
512 ust_registry_channel_del_free(registry
, ua_chan
->key
,
516 * A negative socket can be used by the caller when
517 * cleaning-up a ua_chan in an error path. Skip the
518 * accounting in this case.
521 save_per_pid_lost_discarded_counters(ua_chan
);
525 if (ua_chan
->obj
!= NULL
) {
526 /* Remove channel from application UST object descriptor. */
527 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
528 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
530 pthread_mutex_lock(&app
->sock_lock
);
531 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
532 pthread_mutex_unlock(&app
->sock_lock
);
534 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
535 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
536 ua_chan
->name
, app
->pid
,
538 } else if (ret
== -EAGAIN
) {
539 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
540 ua_chan
->name
, app
->pid
,
543 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
544 ua_chan
->name
, ret
, app
->pid
,
548 lttng_fd_put(LTTNG_FD_APPS
, 1);
551 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
554 int ust_app_register_done(struct ust_app
*app
)
558 pthread_mutex_lock(&app
->sock_lock
);
559 ret
= ustctl_register_done(app
->sock
);
560 pthread_mutex_unlock(&app
->sock_lock
);
564 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
569 pthread_mutex_lock(&app
->sock_lock
);
574 ret
= ustctl_release_object(sock
, data
);
576 pthread_mutex_unlock(&app
->sock_lock
);
582 * Push metadata to consumer socket.
584 * RCU read-side lock must be held to guarantee existance of socket.
585 * Must be called with the ust app session lock held.
586 * Must be called with the registry lock held.
588 * On success, return the len of metadata pushed or else a negative value.
589 * Returning a -EPIPE return value means we could not send the metadata,
590 * but it can be caused by recoverable errors (e.g. the application has
591 * terminated concurrently).
593 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
594 struct consumer_socket
*socket
, int send_zero_data
)
597 char *metadata_str
= NULL
;
598 size_t len
, offset
, new_metadata_len_sent
;
600 uint64_t metadata_key
, metadata_version
;
605 metadata_key
= registry
->metadata_key
;
608 * Means that no metadata was assigned to the session. This can
609 * happens if no start has been done previously.
615 offset
= registry
->metadata_len_sent
;
616 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
617 new_metadata_len_sent
= registry
->metadata_len
;
618 metadata_version
= registry
->metadata_version
;
620 DBG3("No metadata to push for metadata key %" PRIu64
,
621 registry
->metadata_key
);
623 if (send_zero_data
) {
624 DBG("No metadata to push");
630 /* Allocate only what we have to send. */
631 metadata_str
= zmalloc(len
);
633 PERROR("zmalloc ust app metadata string");
637 /* Copy what we haven't sent out. */
638 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
641 pthread_mutex_unlock(®istry
->lock
);
643 * We need to unlock the registry while we push metadata to
644 * break a circular dependency between the consumerd metadata
645 * lock and the sessiond registry lock. Indeed, pushing metadata
646 * to the consumerd awaits that it gets pushed all the way to
647 * relayd, but doing so requires grabbing the metadata lock. If
648 * a concurrent metadata request is being performed by
649 * consumerd, this can try to grab the registry lock on the
650 * sessiond while holding the metadata lock on the consumer
651 * daemon. Those push and pull schemes are performed on two
652 * different bidirectionnal communication sockets.
654 ret
= consumer_push_metadata(socket
, metadata_key
,
655 metadata_str
, len
, offset
, metadata_version
);
656 pthread_mutex_lock(®istry
->lock
);
659 * There is an acceptable race here between the registry
660 * metadata key assignment and the creation on the
661 * consumer. The session daemon can concurrently push
662 * metadata for this registry while being created on the
663 * consumer since the metadata key of the registry is
664 * assigned *before* it is setup to avoid the consumer
665 * to ask for metadata that could possibly be not found
666 * in the session daemon.
668 * The metadata will get pushed either by the session
669 * being stopped or the consumer requesting metadata if
670 * that race is triggered.
672 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
675 ERR("Error pushing metadata to consumer");
681 * Metadata may have been concurrently pushed, since
682 * we're not holding the registry lock while pushing to
683 * consumer. This is handled by the fact that we send
684 * the metadata content, size, and the offset at which
685 * that metadata belongs. This may arrive out of order
686 * on the consumer side, and the consumer is able to
687 * deal with overlapping fragments. The consumer
688 * supports overlapping fragments, which must be
689 * contiguous starting from offset 0. We keep the
690 * largest metadata_len_sent value of the concurrent
693 registry
->metadata_len_sent
=
694 max_t(size_t, registry
->metadata_len_sent
,
695 new_metadata_len_sent
);
704 * On error, flag the registry that the metadata is
705 * closed. We were unable to push anything and this
706 * means that either the consumer is not responding or
707 * the metadata cache has been destroyed on the
710 registry
->metadata_closed
= 1;
718 * For a given application and session, push metadata to consumer.
719 * Either sock or consumer is required : if sock is NULL, the default
720 * socket to send the metadata is retrieved from consumer, if sock
721 * is not NULL we use it to send the metadata.
722 * RCU read-side lock must be held while calling this function,
723 * therefore ensuring existance of registry. It also ensures existance
724 * of socket throughout this function.
726 * Return 0 on success else a negative error.
727 * Returning a -EPIPE return value means we could not send the metadata,
728 * but it can be caused by recoverable errors (e.g. the application has
729 * terminated concurrently).
731 static int push_metadata(struct ust_registry_session
*registry
,
732 struct consumer_output
*consumer
)
736 struct consumer_socket
*socket
;
741 pthread_mutex_lock(®istry
->lock
);
742 if (registry
->metadata_closed
) {
747 /* Get consumer socket to use to push the metadata.*/
748 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
755 ret
= ust_app_push_metadata(registry
, socket
, 0);
760 pthread_mutex_unlock(®istry
->lock
);
764 pthread_mutex_unlock(®istry
->lock
);
769 * Send to the consumer a close metadata command for the given session. Once
770 * done, the metadata channel is deleted and the session metadata pointer is
771 * nullified. The session lock MUST be held unless the application is
772 * in the destroy path.
774 * Do not hold the registry lock while communicating with the consumerd, because
775 * doing so causes inter-process deadlocks between consumerd and sessiond with
776 * the metadata request notification.
778 * Return 0 on success else a negative value.
780 static int close_metadata(struct ust_registry_session
*registry
,
781 struct consumer_output
*consumer
)
784 struct consumer_socket
*socket
;
785 uint64_t metadata_key
;
786 bool registry_was_already_closed
;
793 pthread_mutex_lock(®istry
->lock
);
794 metadata_key
= registry
->metadata_key
;
795 registry_was_already_closed
= registry
->metadata_closed
;
796 if (metadata_key
!= 0) {
798 * Metadata closed. Even on error this means that the consumer
799 * is not responding or not found so either way a second close
800 * should NOT be emit for this registry.
802 registry
->metadata_closed
= 1;
804 pthread_mutex_unlock(®istry
->lock
);
806 if (metadata_key
== 0 || registry_was_already_closed
) {
811 /* Get consumer socket to use to push the metadata.*/
812 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
819 ret
= consumer_close_metadata(socket
, metadata_key
);
830 * We need to execute ht_destroy outside of RCU read-side critical
831 * section and outside of call_rcu thread, so we postpone its execution
832 * using ht_cleanup_push. It is simpler than to change the semantic of
833 * the many callers of delete_ust_app_session().
836 void delete_ust_app_session_rcu(struct rcu_head
*head
)
838 struct ust_app_session
*ua_sess
=
839 caa_container_of(head
, struct ust_app_session
, rcu_head
);
841 ht_cleanup_push(ua_sess
->channels
);
846 * Delete ust app session safely. RCU read lock must be held before calling
849 * The session list lock must be held by the caller.
852 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
856 struct lttng_ht_iter iter
;
857 struct ust_app_channel
*ua_chan
;
858 struct ust_registry_session
*registry
;
862 pthread_mutex_lock(&ua_sess
->lock
);
864 assert(!ua_sess
->deleted
);
865 ua_sess
->deleted
= true;
867 registry
= get_session_registry(ua_sess
);
868 /* Registry can be null on error path during initialization. */
870 /* Push metadata for application before freeing the application. */
871 (void) push_metadata(registry
, ua_sess
->consumer
);
874 * Don't ask to close metadata for global per UID buffers. Close
875 * metadata only on destroy trace session in this case. Also, the
876 * previous push metadata could have flag the metadata registry to
877 * close so don't send a close command if closed.
879 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
880 /* And ask to close it for this session registry. */
881 (void) close_metadata(registry
, ua_sess
->consumer
);
885 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
887 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
889 delete_ust_app_channel(sock
, ua_chan
, app
);
892 /* In case of per PID, the registry is kept in the session. */
893 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
894 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
897 * Registry can be null on error path during
900 buffer_reg_pid_remove(reg_pid
);
901 buffer_reg_pid_destroy(reg_pid
);
905 if (ua_sess
->handle
!= -1) {
906 pthread_mutex_lock(&app
->sock_lock
);
907 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
908 pthread_mutex_unlock(&app
->sock_lock
);
910 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
911 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
912 app
->pid
, app
->sock
);
913 } else if (ret
== -EAGAIN
) {
914 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
915 app
->pid
, app
->sock
);
917 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
918 ret
, app
->pid
, app
->sock
);
922 /* Remove session from application UST object descriptor. */
923 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
924 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
928 pthread_mutex_unlock(&ua_sess
->lock
);
930 consumer_output_put(ua_sess
->consumer
);
932 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
936 * Delete a traceable application structure from the global list. Never call
937 * this function outside of a call_rcu call.
939 * RCU read side lock should _NOT_ be held when calling this function.
942 void delete_ust_app(struct ust_app
*app
)
945 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
948 * The session list lock must be held during this function to guarantee
949 * the existence of ua_sess.
952 /* Delete ust app sessions info */
957 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
959 /* Free every object in the session and the session. */
961 delete_ust_app_session(sock
, ua_sess
, app
);
965 ht_cleanup_push(app
->sessions
);
966 ht_cleanup_push(app
->ust_sessions_objd
);
967 ht_cleanup_push(app
->ust_objd
);
970 * Wait until we have deleted the application from the sock hash table
971 * before closing this socket, otherwise an application could re-use the
972 * socket ID and race with the teardown, using the same hash table entry.
974 * It's OK to leave the close in call_rcu. We want it to stay unique for
975 * all RCU readers that could run concurrently with unregister app,
976 * therefore we _need_ to only close that socket after a grace period. So
977 * it should stay in this RCU callback.
979 * This close() is a very important step of the synchronization model so
980 * every modification to this function must be carefully reviewed.
986 lttng_fd_put(LTTNG_FD_APPS
, 1);
988 DBG2("UST app pid %d deleted", app
->pid
);
990 session_unlock_list();
994 * URCU intermediate call to delete an UST app.
997 void delete_ust_app_rcu(struct rcu_head
*head
)
999 struct lttng_ht_node_ulong
*node
=
1000 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1001 struct ust_app
*app
=
1002 caa_container_of(node
, struct ust_app
, pid_n
);
1004 DBG3("Call RCU deleting app PID %d", app
->pid
);
1005 delete_ust_app(app
);
1009 * Delete the session from the application ht and delete the data structure by
1010 * freeing every object inside and releasing them.
1012 * The session list lock must be held by the caller.
1014 static void destroy_app_session(struct ust_app
*app
,
1015 struct ust_app_session
*ua_sess
)
1018 struct lttng_ht_iter iter
;
1023 iter
.iter
.node
= &ua_sess
->node
.node
;
1024 ret
= lttng_ht_del(app
->sessions
, &iter
);
1026 /* Already scheduled for teardown. */
1030 /* Once deleted, free the data structure. */
1031 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1038 * Alloc new UST app session.
1041 struct ust_app_session
*alloc_ust_app_session(void)
1043 struct ust_app_session
*ua_sess
;
1045 /* Init most of the default value by allocating and zeroing */
1046 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1047 if (ua_sess
== NULL
) {
1052 ua_sess
->handle
= -1;
1053 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1054 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1055 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1064 * Alloc new UST app channel.
1067 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1068 struct ust_app_session
*ua_sess
,
1069 struct lttng_ust_channel_attr
*attr
)
1071 struct ust_app_channel
*ua_chan
;
1073 /* Init most of the default value by allocating and zeroing */
1074 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1075 if (ua_chan
== NULL
) {
1080 /* Setup channel name */
1081 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1082 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1084 ua_chan
->enabled
= 1;
1085 ua_chan
->handle
= -1;
1086 ua_chan
->session
= ua_sess
;
1087 ua_chan
->key
= get_next_channel_key();
1088 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1089 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1090 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1092 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1093 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1095 /* Copy attributes */
1097 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1098 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1099 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1100 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1101 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1102 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1103 ua_chan
->attr
.output
= attr
->output
;
1104 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1106 /* By default, the channel is a per cpu channel. */
1107 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1109 DBG3("UST app channel %s allocated", ua_chan
->name
);
1118 * Allocate and initialize a UST app stream.
1120 * Return newly allocated stream pointer or NULL on error.
1122 struct ust_app_stream
*ust_app_alloc_stream(void)
1124 struct ust_app_stream
*stream
= NULL
;
1126 stream
= zmalloc(sizeof(*stream
));
1127 if (stream
== NULL
) {
1128 PERROR("zmalloc ust app stream");
1132 /* Zero could be a valid value for a handle so flag it to -1. */
1133 stream
->handle
= -1;
1140 * Alloc new UST app event.
1143 struct ust_app_event
*alloc_ust_app_event(char *name
,
1144 struct lttng_ust_event
*attr
)
1146 struct ust_app_event
*ua_event
;
1148 /* Init most of the default value by allocating and zeroing */
1149 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1150 if (ua_event
== NULL
) {
1151 PERROR("Failed to allocate ust_app_event structure");
1155 ua_event
->enabled
= 1;
1156 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1157 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1158 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1160 /* Copy attributes */
1162 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1165 DBG3("UST app event %s allocated", ua_event
->name
);
1174 * Alloc new UST app context.
1177 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1179 struct ust_app_ctx
*ua_ctx
;
1181 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1182 if (ua_ctx
== NULL
) {
1186 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1189 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1190 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1191 char *provider_name
= NULL
, *ctx_name
= NULL
;
1193 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1194 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1195 if (!provider_name
|| !ctx_name
) {
1196 free(provider_name
);
1201 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1202 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1206 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1214 * Allocate a filter and copy the given original filter.
1216 * Return allocated filter or NULL on error.
1218 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1219 struct lttng_filter_bytecode
*orig_f
)
1221 struct lttng_filter_bytecode
*filter
= NULL
;
1223 /* Copy filter bytecode */
1224 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1226 PERROR("zmalloc alloc filter bytecode");
1230 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1237 * Create a liblttng-ust filter bytecode from given bytecode.
1239 * Return allocated filter or NULL on error.
1241 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1242 struct lttng_filter_bytecode
*orig_f
)
1244 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1246 /* Copy filter bytecode */
1247 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1249 PERROR("zmalloc alloc ust filter bytecode");
1253 assert(sizeof(struct lttng_filter_bytecode
) ==
1254 sizeof(struct lttng_ust_filter_bytecode
));
1255 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1261 * Find an ust_app using the sock and return it. RCU read side lock must be
1262 * held before calling this helper function.
1264 struct ust_app
*ust_app_find_by_sock(int sock
)
1266 struct lttng_ht_node_ulong
*node
;
1267 struct lttng_ht_iter iter
;
1269 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1270 node
= lttng_ht_iter_get_node_ulong(&iter
);
1272 DBG2("UST app find by sock %d not found", sock
);
1276 return caa_container_of(node
, struct ust_app
, sock_n
);
1283 * Find an ust_app using the notify sock and return it. RCU read side lock must
1284 * be held before calling this helper function.
1286 static struct ust_app
*find_app_by_notify_sock(int sock
)
1288 struct lttng_ht_node_ulong
*node
;
1289 struct lttng_ht_iter iter
;
1291 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1293 node
= lttng_ht_iter_get_node_ulong(&iter
);
1295 DBG2("UST app find by notify sock %d not found", sock
);
1299 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1306 * Lookup for an ust app event based on event name, filter bytecode and the
1309 * Return an ust_app_event object or NULL on error.
1311 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1312 const char *name
, const struct lttng_filter_bytecode
*filter
,
1314 const struct lttng_event_exclusion
*exclusion
)
1316 struct lttng_ht_iter iter
;
1317 struct lttng_ht_node_str
*node
;
1318 struct ust_app_event
*event
= NULL
;
1319 struct ust_app_ht_key key
;
1324 /* Setup key for event lookup. */
1326 key
.filter
= filter
;
1327 key
.loglevel_type
= loglevel_value
;
1328 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1329 key
.exclusion
= exclusion
;
1331 /* Lookup using the event name as hash and a custom match fct. */
1332 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1333 ht_match_ust_app_event
, &key
, &iter
.iter
);
1334 node
= lttng_ht_iter_get_node_str(&iter
);
1339 event
= caa_container_of(node
, struct ust_app_event
, node
);
1346 * Create the channel context on the tracer.
1348 * Called with UST app session lock held.
1351 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1352 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1356 health_code_update();
1358 pthread_mutex_lock(&app
->sock_lock
);
1359 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1360 ua_chan
->obj
, &ua_ctx
->obj
);
1361 pthread_mutex_unlock(&app
->sock_lock
);
1363 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1365 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1366 app
->pid
, app
->sock
);
1367 } else if (ret
== -EAGAIN
) {
1369 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1370 app
->pid
, app
->sock
);
1372 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1373 ret
, app
->pid
, app
->sock
);
1378 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1380 DBG2("UST app context handle %d created successfully for channel %s",
1381 ua_ctx
->handle
, ua_chan
->name
);
1384 health_code_update();
1389 * Set the filter on the tracer.
1392 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1393 struct ust_app
*app
)
1396 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1398 health_code_update();
1400 if (!ua_event
->filter
) {
1405 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1406 if (!ust_bytecode
) {
1407 ret
= -LTTNG_ERR_NOMEM
;
1410 pthread_mutex_lock(&app
->sock_lock
);
1411 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1413 pthread_mutex_unlock(&app
->sock_lock
);
1415 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1417 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1418 app
->pid
, app
->sock
);
1419 } else if (ret
== -EAGAIN
) {
1421 DBG3("UST app set filter failed. Communication timeout: pid = %d, sock = %d",
1422 app
->pid
, app
->sock
);
1424 ERR("UST app event set filter failed with ret %d: pid = %d, sock = %d",
1431 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1434 health_code_update();
1440 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1441 struct lttng_event_exclusion
*exclusion
)
1443 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1444 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1445 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1447 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1448 if (!ust_exclusion
) {
1453 assert(sizeof(struct lttng_event_exclusion
) ==
1454 sizeof(struct lttng_ust_event_exclusion
));
1455 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1457 return ust_exclusion
;
1461 * Set event exclusions on the tracer.
1464 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1465 struct ust_app
*app
)
1468 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1470 health_code_update();
1472 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1477 ust_exclusion
= create_ust_exclusion_from_exclusion(
1478 ua_event
->exclusion
);
1479 if (!ust_exclusion
) {
1480 ret
= -LTTNG_ERR_NOMEM
;
1483 pthread_mutex_lock(&app
->sock_lock
);
1484 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1485 pthread_mutex_unlock(&app
->sock_lock
);
1487 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1489 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1490 app
->pid
, app
->sock
);
1491 } else if (ret
== -EAGAIN
) {
1493 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1494 app
->pid
, app
->sock
);
1496 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, event = %s",
1497 ret
, app
->pid
, app
->sock
, ua_event
->name
);
1502 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1505 health_code_update();
1506 free(ust_exclusion
);
1511 * Disable the specified event on to UST tracer for the UST session.
1513 static int disable_ust_event(struct ust_app
*app
,
1514 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1518 health_code_update();
1520 pthread_mutex_lock(&app
->sock_lock
);
1521 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1522 pthread_mutex_unlock(&app
->sock_lock
);
1524 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1526 DBG3("UST app disable event failed. Application is dead: pid = %d, sock = %d",
1527 app
->pid
, app
->sock
);
1528 } else if (ret
== -EAGAIN
) {
1530 WARN("UST app disable event failed. Communication time out: pid = %d, sock = %d",
1531 app
->pid
, app
->sock
);
1533 ERR("UST app disable event failed with ret %d: pid = %d, sock = %d , name = %s",
1534 ret
, app
->pid
, app
->sock
, ua_event
->attr
.name
);
1539 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1540 ua_event
->attr
.name
, app
->pid
);
1543 health_code_update();
1548 * Disable the specified channel on to UST tracer for the UST session.
1550 static int disable_ust_channel(struct ust_app
*app
,
1551 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1555 health_code_update();
1557 pthread_mutex_lock(&app
->sock_lock
);
1558 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1559 pthread_mutex_unlock(&app
->sock_lock
);
1561 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1563 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1564 app
->pid
, app
->sock
);
1565 } else if (ret
== -EAGAIN
) {
1567 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1568 app
->pid
, app
->sock
);
1570 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1571 ua_chan
->name
, ua_sess
->handle
, ret
,
1572 app
->pid
, app
->sock
);
1577 DBG2("UST app channel %s disabled successfully for app: pid = %d",
1578 ua_chan
->name
, app
->pid
);
1581 health_code_update();
1586 * Enable the specified channel on to UST tracer for the UST session.
1588 static int enable_ust_channel(struct ust_app
*app
,
1589 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1593 health_code_update();
1595 pthread_mutex_lock(&app
->sock_lock
);
1596 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1597 pthread_mutex_unlock(&app
->sock_lock
);
1599 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1601 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1602 ua_chan
->name
, app
->pid
, app
->sock
);
1603 } else if (ret
== -EAGAIN
) {
1605 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1606 ua_chan
->name
, app
->pid
, app
->sock
);
1608 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1609 ua_chan
->name
, ua_sess
->handle
, ret
,
1610 app
->pid
, app
->sock
);
1615 ua_chan
->enabled
= 1;
1617 DBG2("UST app channel %s enabled successfully for app: pid = %d",
1618 ua_chan
->name
, app
->pid
);
1621 health_code_update();
1626 * Enable the specified event on to UST tracer for the UST session.
1628 static int enable_ust_event(struct ust_app
*app
,
1629 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1633 health_code_update();
1635 pthread_mutex_lock(&app
->sock_lock
);
1636 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1637 pthread_mutex_unlock(&app
->sock_lock
);
1639 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1641 DBG3("UST app enable event failed. Application is dead: pid = %d, sock = %d",
1642 app
->pid
, app
->sock
);
1643 } else if (ret
== -EAGAIN
) {
1645 WARN("UST app enable event failed. Communication time out: pid = %d, sock = %d",
1646 app
->pid
, app
->sock
);
1648 ERR("UST app enable event failed with ret %d: pid = %d, sock = %d, event = %s",
1649 ret
, app
->pid
, app
->sock
, ua_event
->attr
.name
);
1654 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1655 ua_event
->attr
.name
, app
->pid
);
1658 health_code_update();
1663 * Send channel and stream buffer to application.
1665 * Return 0 on success. On error, a negative value is returned.
1667 static int send_channel_pid_to_ust(struct ust_app
*app
,
1668 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1671 struct ust_app_stream
*stream
, *stmp
;
1677 health_code_update();
1679 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1682 /* Send channel to the application. */
1683 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1684 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1685 ret
= -ENOTCONN
; /* Caused by app exiting. */
1687 } else if (ret
== -EAGAIN
) {
1688 /* Caused by timeout. */
1689 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
"\".",
1690 app
->pid
, ua_chan
->name
, ua_sess
->tracing_id
);
1691 /* Treat this the same way as an application that is exiting. */
1694 } else if (ret
< 0) {
1698 health_code_update();
1700 /* Send all streams to application. */
1701 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1702 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1703 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1704 ret
= -ENOTCONN
; /* Caused by app exiting. */
1706 } else if (ret
== -EAGAIN
) {
1707 /* Caused by timeout. */
1708 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
"\".",
1709 app
->pid
, stream
->name
, ua_chan
->name
,
1710 ua_sess
->tracing_id
);
1712 * Treat this the same way as an application that is
1716 } else if (ret
< 0) {
1719 /* We don't need the stream anymore once sent to the tracer. */
1720 cds_list_del(&stream
->list
);
1721 delete_ust_app_stream(-1, stream
, app
);
1723 /* Flag the channel that it is sent to the application. */
1724 ua_chan
->is_sent
= 1;
1727 health_code_update();
1732 * Create the specified event onto the UST tracer for a UST session.
1734 * Should be called with session mutex held.
1737 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1738 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1742 health_code_update();
1744 /* Create UST event on tracer */
1745 pthread_mutex_lock(&app
->sock_lock
);
1746 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1748 pthread_mutex_unlock(&app
->sock_lock
);
1750 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1752 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
1753 app
->pid
, app
->sock
);
1754 } else if (ret
== -EAGAIN
) {
1756 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
1757 app
->pid
, app
->sock
);
1759 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
1760 ua_event
->attr
.name
, ret
, app
->pid
,
1766 ua_event
->handle
= ua_event
->obj
->handle
;
1768 DBG2("UST app event %s created successfully for pid:%d",
1769 ua_event
->attr
.name
, app
->pid
);
1771 health_code_update();
1773 /* Set filter if one is present. */
1774 if (ua_event
->filter
) {
1775 ret
= set_ust_event_filter(ua_event
, app
);
1781 /* Set exclusions for the event */
1782 if (ua_event
->exclusion
) {
1783 ret
= set_ust_event_exclusion(ua_event
, app
);
1789 /* If event not enabled, disable it on the tracer */
1790 if (ua_event
->enabled
) {
1792 * We now need to explicitly enable the event, since it
1793 * is now disabled at creation.
1795 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1798 * If we hit an EPERM, something is wrong with our enable call. If
1799 * we get an EEXIST, there is a problem on the tracer side since we
1803 case -LTTNG_UST_ERR_PERM
:
1804 /* Code flow problem */
1806 case -LTTNG_UST_ERR_EXIST
:
1807 /* It's OK for our use case. */
1818 health_code_update();
1823 * Copy data between an UST app event and a LTT event.
1825 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1826 struct ltt_ust_event
*uevent
)
1828 size_t exclusion_alloc_size
;
1830 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1831 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1833 ua_event
->enabled
= uevent
->enabled
;
1835 /* Copy event attributes */
1836 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1838 /* Copy filter bytecode */
1839 if (uevent
->filter
) {
1840 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1841 /* Filter might be NULL here in case of ENONEM. */
1844 /* Copy exclusion data */
1845 if (uevent
->exclusion
) {
1846 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1847 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1848 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1849 if (ua_event
->exclusion
== NULL
) {
1852 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1853 exclusion_alloc_size
);
1859 * Copy data between an UST app channel and a LTT channel.
1861 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1862 struct ltt_ust_channel
*uchan
)
1864 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1866 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1867 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1869 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1870 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1872 /* Copy event attributes since the layout is different. */
1873 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1874 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1875 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1876 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1877 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1878 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1879 ua_chan
->attr
.output
= uchan
->attr
.output
;
1880 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
1883 * Note that the attribute channel type is not set since the channel on the
1884 * tracing registry side does not have this information.
1887 ua_chan
->enabled
= uchan
->enabled
;
1888 ua_chan
->tracing_channel_id
= uchan
->id
;
1890 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1894 * Copy data between a UST app session and a regular LTT session.
1896 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1897 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1899 struct tm
*timeinfo
;
1902 char tmp_shm_path
[PATH_MAX
];
1904 timeinfo
= localtime(&app
->registration_time
);
1905 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1907 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1909 ua_sess
->tracing_id
= usess
->id
;
1910 ua_sess
->id
= get_next_session_id();
1911 ua_sess
->real_credentials
.uid
= app
->uid
;
1912 ua_sess
->real_credentials
.gid
= app
->gid
;
1913 ua_sess
->effective_credentials
.uid
= usess
->uid
;
1914 ua_sess
->effective_credentials
.gid
= usess
->gid
;
1915 ua_sess
->buffer_type
= usess
->buffer_type
;
1916 ua_sess
->bits_per_long
= app
->bits_per_long
;
1918 /* There is only one consumer object per session possible. */
1919 consumer_output_get(usess
->consumer
);
1920 ua_sess
->consumer
= usess
->consumer
;
1922 ua_sess
->output_traces
= usess
->output_traces
;
1923 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1924 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1925 &usess
->metadata_attr
);
1927 switch (ua_sess
->buffer_type
) {
1928 case LTTNG_BUFFER_PER_PID
:
1929 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1930 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1933 case LTTNG_BUFFER_PER_UID
:
1934 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1935 DEFAULT_UST_TRACE_UID_PATH
,
1936 ua_sess
->real_credentials
.uid
,
1937 app
->bits_per_long
);
1944 PERROR("asprintf UST shadow copy session");
1949 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1950 sizeof(ua_sess
->root_shm_path
));
1951 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1952 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1953 sizeof(ua_sess
->shm_path
));
1954 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1955 if (ua_sess
->shm_path
[0]) {
1956 switch (ua_sess
->buffer_type
) {
1957 case LTTNG_BUFFER_PER_PID
:
1958 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1959 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1960 app
->name
, app
->pid
, datetime
);
1962 case LTTNG_BUFFER_PER_UID
:
1963 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1964 "/" DEFAULT_UST_TRACE_UID_PATH
,
1965 app
->uid
, app
->bits_per_long
);
1972 PERROR("sprintf UST shadow copy session");
1976 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1977 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1978 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1983 consumer_output_put(ua_sess
->consumer
);
1987 * Lookup sesison wrapper.
1990 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
1991 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1993 /* Get right UST app session from app */
1994 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1998 * Return ust app session from the app session hashtable using the UST session
2001 static struct ust_app_session
*lookup_session_by_app(
2002 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2004 struct lttng_ht_iter iter
;
2005 struct lttng_ht_node_u64
*node
;
2007 __lookup_session_by_app(usess
, app
, &iter
);
2008 node
= lttng_ht_iter_get_node_u64(&iter
);
2013 return caa_container_of(node
, struct ust_app_session
, node
);
2020 * Setup buffer registry per PID for the given session and application. If none
2021 * is found, a new one is created, added to the global registry and
2022 * initialized. If regp is valid, it's set with the newly created object.
2024 * Return 0 on success or else a negative value.
2026 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2027 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2030 struct buffer_reg_pid
*reg_pid
;
2037 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2040 * This is the create channel path meaning that if there is NO
2041 * registry available, we have to create one for this session.
2043 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2044 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2052 /* Initialize registry. */
2053 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2054 app
->bits_per_long
, app
->uint8_t_alignment
,
2055 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2056 app
->uint64_t_alignment
, app
->long_alignment
,
2057 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2058 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2059 ua_sess
->effective_credentials
.uid
,
2060 ua_sess
->effective_credentials
.gid
, ua_sess
->tracing_id
,
2064 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2065 * destroy the buffer registry, because it is always expected
2066 * that if the buffer registry can be found, its ust registry is
2069 buffer_reg_pid_destroy(reg_pid
);
2073 buffer_reg_pid_add(reg_pid
);
2075 DBG3("UST app buffer registry per PID created successfully");
2087 * Setup buffer registry per UID for the given session and application. If none
2088 * is found, a new one is created, added to the global registry and
2089 * initialized. If regp is valid, it's set with the newly created object.
2091 * Return 0 on success or else a negative value.
2093 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2094 struct ust_app_session
*ua_sess
,
2095 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2098 struct buffer_reg_uid
*reg_uid
;
2105 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2108 * This is the create channel path meaning that if there is NO
2109 * registry available, we have to create one for this session.
2111 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2112 LTTNG_DOMAIN_UST
, ®_uid
,
2113 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2121 /* Initialize registry. */
2122 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2123 app
->bits_per_long
, app
->uint8_t_alignment
,
2124 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2125 app
->uint64_t_alignment
, app
->long_alignment
,
2126 app
->byte_order
, app
->version
.major
,
2127 app
->version
.minor
, reg_uid
->root_shm_path
,
2128 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2129 ua_sess
->tracing_id
, app
->uid
);
2132 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2133 * destroy the buffer registry, because it is always expected
2134 * that if the buffer registry can be found, its ust registry is
2137 buffer_reg_uid_destroy(reg_uid
, NULL
);
2140 /* Add node to teardown list of the session. */
2141 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2143 buffer_reg_uid_add(reg_uid
);
2145 DBG3("UST app buffer registry per UID created successfully");
2156 * Create a session on the tracer side for the given app.
2158 * On success, ua_sess_ptr is populated with the session pointer or else left
2159 * untouched. If the session was created, is_created is set to 1. On error,
2160 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2163 * Returns 0 on success or else a negative code which is either -ENOMEM or
2164 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2166 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2167 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2170 int ret
, created
= 0;
2171 struct ust_app_session
*ua_sess
;
2175 assert(ua_sess_ptr
);
2177 health_code_update();
2179 ua_sess
= lookup_session_by_app(usess
, app
);
2180 if (ua_sess
== NULL
) {
2181 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2182 app
->pid
, usess
->id
);
2183 ua_sess
= alloc_ust_app_session();
2184 if (ua_sess
== NULL
) {
2185 /* Only malloc can failed so something is really wrong */
2189 shadow_copy_session(ua_sess
, usess
, app
);
2193 switch (usess
->buffer_type
) {
2194 case LTTNG_BUFFER_PER_PID
:
2195 /* Init local registry. */
2196 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2198 delete_ust_app_session(-1, ua_sess
, app
);
2202 case LTTNG_BUFFER_PER_UID
:
2203 /* Look for a global registry. If none exists, create one. */
2204 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2206 delete_ust_app_session(-1, ua_sess
, app
);
2216 health_code_update();
2218 if (ua_sess
->handle
== -1) {
2219 pthread_mutex_lock(&app
->sock_lock
);
2220 ret
= ustctl_create_session(app
->sock
);
2221 pthread_mutex_unlock(&app
->sock_lock
);
2223 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2224 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2225 app
->pid
, app
->sock
);
2227 } else if (ret
== -EAGAIN
) {
2228 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2229 app
->pid
, app
->sock
);
2232 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2233 ret
, app
->pid
, app
->sock
);
2235 delete_ust_app_session(-1, ua_sess
, app
);
2236 if (ret
!= -ENOMEM
) {
2238 * Tracer is probably gone or got an internal error so let's
2239 * behave like it will soon unregister or not usable.
2246 ua_sess
->handle
= ret
;
2248 /* Add ust app session to app's HT */
2249 lttng_ht_node_init_u64(&ua_sess
->node
,
2250 ua_sess
->tracing_id
);
2251 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2252 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2253 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2254 &ua_sess
->ust_objd_node
);
2256 DBG2("UST app session created successfully with handle %d", ret
);
2259 *ua_sess_ptr
= ua_sess
;
2261 *is_created
= created
;
2264 /* Everything went well. */
2268 health_code_update();
2273 * Match function for a hash table lookup of ust_app_ctx.
2275 * It matches an ust app context based on the context type and, in the case
2276 * of perf counters, their name.
2278 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2280 struct ust_app_ctx
*ctx
;
2281 const struct lttng_ust_context_attr
*key
;
2286 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2290 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2295 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2296 if (strncmp(key
->u
.perf_counter
.name
,
2297 ctx
->ctx
.u
.perf_counter
.name
,
2298 sizeof(key
->u
.perf_counter
.name
))) {
2302 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2303 if (strcmp(key
->u
.app_ctx
.provider_name
,
2304 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2305 strcmp(key
->u
.app_ctx
.ctx_name
,
2306 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2322 * Lookup for an ust app context from an lttng_ust_context.
2324 * Must be called while holding RCU read side lock.
2325 * Return an ust_app_ctx object or NULL on error.
2328 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2329 struct lttng_ust_context_attr
*uctx
)
2331 struct lttng_ht_iter iter
;
2332 struct lttng_ht_node_ulong
*node
;
2333 struct ust_app_ctx
*app_ctx
= NULL
;
2338 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2339 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2340 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2341 node
= lttng_ht_iter_get_node_ulong(&iter
);
2346 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2353 * Create a context for the channel on the tracer.
2355 * Called with UST app session lock held and a RCU read side lock.
2358 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2359 struct lttng_ust_context_attr
*uctx
,
2360 struct ust_app
*app
)
2363 struct ust_app_ctx
*ua_ctx
;
2365 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2367 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2373 ua_ctx
= alloc_ust_app_ctx(uctx
);
2374 if (ua_ctx
== NULL
) {
2380 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2381 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2382 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2384 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2394 * Enable on the tracer side a ust app event for the session and channel.
2396 * Called with UST app session lock held.
2399 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2400 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2404 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2409 ua_event
->enabled
= 1;
2416 * Disable on the tracer side a ust app event for the session and channel.
2418 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2419 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2423 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2428 ua_event
->enabled
= 0;
2435 * Lookup ust app channel for session and disable it on the tracer side.
2438 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2439 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2443 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2448 ua_chan
->enabled
= 0;
2455 * Lookup ust app channel for session and enable it on the tracer side. This
2456 * MUST be called with a RCU read side lock acquired.
2458 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2459 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2462 struct lttng_ht_iter iter
;
2463 struct lttng_ht_node_str
*ua_chan_node
;
2464 struct ust_app_channel
*ua_chan
;
2466 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2467 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2468 if (ua_chan_node
== NULL
) {
2469 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2470 uchan
->name
, ua_sess
->tracing_id
);
2474 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2476 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2486 * Ask the consumer to create a channel and get it if successful.
2488 * Called with UST app session lock held.
2490 * Return 0 on success or else a negative value.
2492 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2493 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2494 int bitness
, struct ust_registry_session
*registry
,
2495 uint64_t trace_archive_id
)
2498 unsigned int nb_fd
= 0;
2499 struct consumer_socket
*socket
;
2507 health_code_update();
2509 /* Get the right consumer socket for the application. */
2510 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2516 health_code_update();
2518 /* Need one fd for the channel. */
2519 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2521 ERR("Exhausted number of available FD upon create channel");
2526 * Ask consumer to create channel. The consumer will return the number of
2527 * stream we have to expect.
2529 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2530 registry
, usess
->current_trace_chunk
);
2536 * Compute the number of fd needed before receiving them. It must be 2 per
2537 * stream (2 being the default value here).
2539 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2541 /* Reserve the amount of file descriptor we need. */
2542 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2544 ERR("Exhausted number of available FD upon create channel");
2545 goto error_fd_get_stream
;
2548 health_code_update();
2551 * Now get the channel from the consumer. This call will populate the stream
2552 * list of that channel and set the ust objects.
2554 if (usess
->consumer
->enabled
) {
2555 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2565 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2566 error_fd_get_stream
:
2568 * Initiate a destroy channel on the consumer since we had an error
2569 * handling it on our side. The return value is of no importance since we
2570 * already have a ret value set by the previous error that we need to
2573 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2575 lttng_fd_put(LTTNG_FD_APPS
, 1);
2577 health_code_update();
2583 * Duplicate the ust data object of the ust app stream and save it in the
2584 * buffer registry stream.
2586 * Return 0 on success or else a negative value.
2588 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2589 struct ust_app_stream
*stream
)
2596 /* Reserve the amount of file descriptor we need. */
2597 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2599 ERR("Exhausted number of available FD upon duplicate stream");
2603 /* Duplicate object for stream once the original is in the registry. */
2604 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2605 reg_stream
->obj
.ust
);
2607 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2608 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2609 lttng_fd_put(LTTNG_FD_APPS
, 2);
2612 stream
->handle
= stream
->obj
->handle
;
2619 * Duplicate the ust data object of the ust app. channel and save it in the
2620 * buffer registry channel.
2622 * Return 0 on success or else a negative value.
2624 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2625 struct ust_app_channel
*ua_chan
)
2632 /* Need two fds for the channel. */
2633 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2635 ERR("Exhausted number of available FD upon duplicate channel");
2639 /* Duplicate object for stream once the original is in the registry. */
2640 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2642 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2643 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2646 ua_chan
->handle
= ua_chan
->obj
->handle
;
2651 lttng_fd_put(LTTNG_FD_APPS
, 1);
2657 * For a given channel buffer registry, setup all streams of the given ust
2658 * application channel.
2660 * Return 0 on success or else a negative value.
2662 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2663 struct ust_app_channel
*ua_chan
,
2664 struct ust_app
*app
)
2667 struct ust_app_stream
*stream
, *stmp
;
2672 DBG2("UST app setup buffer registry stream");
2674 /* Send all streams to application. */
2675 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2676 struct buffer_reg_stream
*reg_stream
;
2678 ret
= buffer_reg_stream_create(®_stream
);
2684 * Keep original pointer and nullify it in the stream so the delete
2685 * stream call does not release the object.
2687 reg_stream
->obj
.ust
= stream
->obj
;
2689 buffer_reg_stream_add(reg_stream
, reg_chan
);
2691 /* We don't need the streams anymore. */
2692 cds_list_del(&stream
->list
);
2693 delete_ust_app_stream(-1, stream
, app
);
2701 * Create a buffer registry channel for the given session registry and
2702 * application channel object. If regp pointer is valid, it's set with the
2703 * created object. Important, the created object is NOT added to the session
2704 * registry hash table.
2706 * Return 0 on success else a negative value.
2708 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2709 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2712 struct buffer_reg_channel
*reg_chan
= NULL
;
2717 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2719 /* Create buffer registry channel. */
2720 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2725 reg_chan
->consumer_key
= ua_chan
->key
;
2726 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2727 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2729 /* Create and add a channel registry to session. */
2730 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2731 ua_chan
->tracing_channel_id
);
2735 buffer_reg_channel_add(reg_sess
, reg_chan
);
2744 /* Safe because the registry channel object was not added to any HT. */
2745 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2751 * Setup buffer registry channel for the given session registry and application
2752 * channel object. If regp pointer is valid, it's set with the created object.
2754 * Return 0 on success else a negative value.
2756 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2757 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2758 struct ust_app
*app
)
2765 assert(ua_chan
->obj
);
2767 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2769 /* Setup all streams for the registry. */
2770 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2775 reg_chan
->obj
.ust
= ua_chan
->obj
;
2776 ua_chan
->obj
= NULL
;
2781 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2782 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2787 * Send buffer registry channel to the application.
2789 * Return 0 on success else a negative value.
2791 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2792 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2793 struct ust_app_channel
*ua_chan
)
2796 struct buffer_reg_stream
*reg_stream
;
2803 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2805 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2810 /* Send channel to the application. */
2811 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2812 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2813 ret
= -ENOTCONN
; /* Caused by app exiting. */
2815 } else if (ret
== -EAGAIN
) {
2816 /* Caused by timeout. */
2817 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
"\".",
2818 app
->pid
, ua_chan
->name
, ua_sess
->tracing_id
);
2819 /* Treat this the same way as an application that is exiting. */
2822 } else if (ret
< 0) {
2826 health_code_update();
2828 /* Send all streams to application. */
2829 pthread_mutex_lock(®_chan
->stream_list_lock
);
2830 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2831 struct ust_app_stream stream
;
2833 ret
= duplicate_stream_object(reg_stream
, &stream
);
2835 goto error_stream_unlock
;
2838 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2840 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2841 ret
= -ENOTCONN
; /* Caused by app exiting. */
2842 } else if (ret
== -EAGAIN
) {
2844 * Caused by timeout.
2845 * Treat this the same way as an application
2848 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
"\".",
2849 app
->pid
, stream
.name
,
2851 ua_sess
->tracing_id
);
2854 (void) release_ust_app_stream(-1, &stream
, app
);
2855 goto error_stream_unlock
;
2859 * The return value is not important here. This function will output an
2862 (void) release_ust_app_stream(-1, &stream
, app
);
2864 ua_chan
->is_sent
= 1;
2866 error_stream_unlock
:
2867 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2873 * Create and send to the application the created buffers with per UID buffers.
2875 * This MUST be called with a RCU read side lock acquired.
2876 * The session list lock and the session's lock must be acquired.
2878 * Return 0 on success else a negative value.
2880 static int create_channel_per_uid(struct ust_app
*app
,
2881 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2882 struct ust_app_channel
*ua_chan
)
2885 struct buffer_reg_uid
*reg_uid
;
2886 struct buffer_reg_channel
*reg_chan
;
2887 struct ltt_session
*session
= NULL
;
2888 enum lttng_error_code notification_ret
;
2889 struct ust_registry_channel
*chan_reg
;
2896 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2898 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2900 * The session creation handles the creation of this global registry
2901 * object. If none can be find, there is a code flow problem or a
2906 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2912 /* Create the buffer registry channel object. */
2913 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2915 ERR("Error creating the UST channel \"%s\" registry instance",
2920 session
= session_find_by_id(ua_sess
->tracing_id
);
2922 assert(pthread_mutex_trylock(&session
->lock
));
2923 assert(session_trylock_list());
2926 * Create the buffers on the consumer side. This call populates the
2927 * ust app channel object with all streams and data object.
2929 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2930 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
2931 session
->most_recent_chunk_id
.value
);
2933 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2937 * Let's remove the previously created buffer registry channel so
2938 * it's not visible anymore in the session registry.
2940 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2941 ua_chan
->tracing_channel_id
, false);
2942 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2943 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2948 * Setup the streams and add it to the session registry.
2950 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2951 ua_chan
, reg_chan
, app
);
2953 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
2957 /* Notify the notification subsystem of the channel's creation. */
2958 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2959 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2960 ua_chan
->tracing_channel_id
);
2962 chan_reg
->consumer_key
= ua_chan
->key
;
2964 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2966 notification_ret
= notification_thread_command_add_channel(
2967 notification_thread_handle
, session
->name
,
2968 ua_sess
->effective_credentials
.uid
,
2969 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
2970 ua_chan
->key
, LTTNG_DOMAIN_UST
,
2971 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2972 if (notification_ret
!= LTTNG_OK
) {
2973 ret
= - (int) notification_ret
;
2974 ERR("Failed to add channel to notification thread");
2979 /* Send buffers to the application. */
2980 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2982 if (ret
!= -ENOTCONN
) {
2983 ERR("Error sending channel to application");
2990 session_put(session
);
2996 * Create and send to the application the created buffers with per PID buffers.
2998 * Called with UST app session lock held.
2999 * The session list lock and the session's lock must be acquired.
3001 * Return 0 on success else a negative value.
3003 static int create_channel_per_pid(struct ust_app
*app
,
3004 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3005 struct ust_app_channel
*ua_chan
)
3008 struct ust_registry_session
*registry
;
3009 enum lttng_error_code cmd_ret
;
3010 struct ltt_session
*session
= NULL
;
3011 uint64_t chan_reg_key
;
3012 struct ust_registry_channel
*chan_reg
;
3019 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3023 registry
= get_session_registry(ua_sess
);
3024 /* The UST app session lock is held, registry shall not be null. */
3027 /* Create and add a new channel registry to session. */
3028 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3030 ERR("Error creating the UST channel \"%s\" registry instance",
3035 session
= session_find_by_id(ua_sess
->tracing_id
);
3038 assert(pthread_mutex_trylock(&session
->lock
));
3039 assert(session_trylock_list());
3041 /* Create and get channel on the consumer side. */
3042 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3043 app
->bits_per_long
, registry
,
3044 session
->most_recent_chunk_id
.value
);
3046 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3048 goto error_remove_from_registry
;
3051 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3053 if (ret
!= -ENOTCONN
) {
3054 ERR("Error sending channel to application");
3056 goto error_remove_from_registry
;
3059 chan_reg_key
= ua_chan
->key
;
3060 pthread_mutex_lock(®istry
->lock
);
3061 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3063 chan_reg
->consumer_key
= ua_chan
->key
;
3064 pthread_mutex_unlock(®istry
->lock
);
3066 cmd_ret
= notification_thread_command_add_channel(
3067 notification_thread_handle
, session
->name
,
3068 ua_sess
->effective_credentials
.uid
,
3069 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
3070 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3071 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3072 if (cmd_ret
!= LTTNG_OK
) {
3073 ret
= - (int) cmd_ret
;
3074 ERR("Failed to add channel to notification thread");
3075 goto error_remove_from_registry
;
3078 error_remove_from_registry
:
3080 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3085 session_put(session
);
3091 * From an already allocated ust app channel, create the channel buffers if
3092 * needed and send them to the application. This MUST be called with a RCU read
3093 * side lock acquired.
3095 * Called with UST app session lock held.
3097 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3098 * the application exited concurrently.
3100 static int ust_app_channel_send(struct ust_app
*app
,
3101 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3102 struct ust_app_channel
*ua_chan
)
3108 assert(usess
->active
);
3112 /* Handle buffer type before sending the channel to the application. */
3113 switch (usess
->buffer_type
) {
3114 case LTTNG_BUFFER_PER_UID
:
3116 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3122 case LTTNG_BUFFER_PER_PID
:
3124 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3136 /* Initialize ust objd object using the received handle and add it. */
3137 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3138 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3140 /* If channel is not enabled, disable it on the tracer */
3141 if (!ua_chan
->enabled
) {
3142 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3153 * Create UST app channel and return it through ua_chanp if not NULL.
3155 * Called with UST app session lock and RCU read-side lock held.
3157 * Return 0 on success or else a negative value.
3159 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3160 struct ltt_ust_channel
*uchan
,
3161 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3162 struct ust_app_channel
**ua_chanp
)
3165 struct lttng_ht_iter iter
;
3166 struct lttng_ht_node_str
*ua_chan_node
;
3167 struct ust_app_channel
*ua_chan
;
3169 /* Lookup channel in the ust app session */
3170 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3171 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3172 if (ua_chan_node
!= NULL
) {
3173 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3177 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3178 if (ua_chan
== NULL
) {
3179 /* Only malloc can fail here */
3183 shadow_copy_channel(ua_chan
, uchan
);
3185 /* Set channel type. */
3186 ua_chan
->attr
.type
= type
;
3188 /* Only add the channel if successful on the tracer side. */
3189 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3192 *ua_chanp
= ua_chan
;
3195 /* Everything went well. */
3203 * Create UST app event and create it on the tracer side.
3205 * Called with ust app session mutex held.
3208 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3209 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3210 struct ust_app
*app
)
3213 struct ust_app_event
*ua_event
;
3215 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3216 if (ua_event
== NULL
) {
3217 /* Only failure mode of alloc_ust_app_event(). */
3221 shadow_copy_event(ua_event
, uevent
);
3223 /* Create it on the tracer side */
3224 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3227 * Not found previously means that it does not exist on the
3228 * tracer. If the application reports that the event existed,
3229 * it means there is a bug in the sessiond or lttng-ust
3230 * (or corruption, etc.)
3232 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3233 ERR("Tracer for application reported that an event being created already existed: "
3234 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3236 app
->pid
, app
->ppid
, app
->uid
,
3242 add_unique_ust_app_event(ua_chan
, ua_event
);
3244 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3251 /* Valid. Calling here is already in a read side lock */
3252 delete_ust_app_event(-1, ua_event
, app
);
3257 * Create UST metadata and open it on the tracer side.
3259 * Called with UST app session lock held and RCU read side lock.
3261 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3262 struct ust_app
*app
, struct consumer_output
*consumer
)
3265 struct ust_app_channel
*metadata
;
3266 struct consumer_socket
*socket
;
3267 struct ust_registry_session
*registry
;
3268 struct ltt_session
*session
= NULL
;
3274 registry
= get_session_registry(ua_sess
);
3275 /* The UST app session is held registry shall not be null. */
3278 pthread_mutex_lock(®istry
->lock
);
3280 /* Metadata already exists for this registry or it was closed previously */
3281 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3286 /* Allocate UST metadata */
3287 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3289 /* malloc() failed */
3294 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3296 /* Need one fd for the channel. */
3297 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3299 ERR("Exhausted number of available FD upon create metadata");
3303 /* Get the right consumer socket for the application. */
3304 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3307 goto error_consumer
;
3311 * Keep metadata key so we can identify it on the consumer side. Assign it
3312 * to the registry *before* we ask the consumer so we avoid the race of the
3313 * consumer requesting the metadata and the ask_channel call on our side
3314 * did not returned yet.
3316 registry
->metadata_key
= metadata
->key
;
3318 session
= session_find_by_id(ua_sess
->tracing_id
);
3321 assert(pthread_mutex_trylock(&session
->lock
));
3322 assert(session_trylock_list());
3325 * Ask the metadata channel creation to the consumer. The metadata object
3326 * will be created by the consumer and kept their. However, the stream is
3327 * never added or monitored until we do a first push metadata to the
3330 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3331 registry
, session
->current_trace_chunk
);
3333 /* Nullify the metadata key so we don't try to close it later on. */
3334 registry
->metadata_key
= 0;
3335 goto error_consumer
;
3339 * The setup command will make the metadata stream be sent to the relayd,
3340 * if applicable, and the thread managing the metadatas. This is important
3341 * because after this point, if an error occurs, the only way the stream
3342 * can be deleted is to be monitored in the consumer.
3344 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3346 /* Nullify the metadata key so we don't try to close it later on. */
3347 registry
->metadata_key
= 0;
3348 goto error_consumer
;
3351 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3352 metadata
->key
, app
->pid
);
3355 lttng_fd_put(LTTNG_FD_APPS
, 1);
3356 delete_ust_app_channel(-1, metadata
, app
);
3358 pthread_mutex_unlock(®istry
->lock
);
3360 session_put(session
);
3366 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3367 * acquired before calling this function.
3369 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3371 struct ust_app
*app
= NULL
;
3372 struct lttng_ht_node_ulong
*node
;
3373 struct lttng_ht_iter iter
;
3375 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3376 node
= lttng_ht_iter_get_node_ulong(&iter
);
3378 DBG2("UST app no found with pid %d", pid
);
3382 DBG2("Found UST app by pid %d", pid
);
3384 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3391 * Allocate and init an UST app object using the registration information and
3392 * the command socket. This is called when the command socket connects to the
3395 * The object is returned on success or else NULL.
3397 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3399 struct ust_app
*lta
= NULL
;
3404 DBG3("UST app creating application for socket %d", sock
);
3406 if ((msg
->bits_per_long
== 64 &&
3407 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3408 || (msg
->bits_per_long
== 32 &&
3409 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3410 ERR("Registration failed: application \"%s\" (pid: %d) has "
3411 "%d-bit long, but no consumerd for this size is available.\n",
3412 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3416 lta
= zmalloc(sizeof(struct ust_app
));
3422 lta
->ppid
= msg
->ppid
;
3423 lta
->uid
= msg
->uid
;
3424 lta
->gid
= msg
->gid
;
3426 lta
->bits_per_long
= msg
->bits_per_long
;
3427 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3428 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3429 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3430 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3431 lta
->long_alignment
= msg
->long_alignment
;
3432 lta
->byte_order
= msg
->byte_order
;
3434 lta
->v_major
= msg
->major
;
3435 lta
->v_minor
= msg
->minor
;
3436 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3437 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3438 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3439 lta
->notify_sock
= -1;
3441 /* Copy name and make sure it's NULL terminated. */
3442 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3443 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3446 * Before this can be called, when receiving the registration information,
3447 * the application compatibility is checked. So, at this point, the
3448 * application can work with this session daemon.
3450 lta
->compatible
= 1;
3452 lta
->pid
= msg
->pid
;
3453 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3455 pthread_mutex_init(<a
->sock_lock
, NULL
);
3456 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3458 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3464 * For a given application object, add it to every hash table.
3466 void ust_app_add(struct ust_app
*app
)
3469 assert(app
->notify_sock
>= 0);
3471 app
->registration_time
= time(NULL
);
3476 * On a re-registration, we want to kick out the previous registration of
3479 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3482 * The socket _should_ be unique until _we_ call close. So, a add_unique
3483 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3484 * already in the table.
3486 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3488 /* Add application to the notify socket hash table. */
3489 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3490 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3492 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
3493 "notify_sock =%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3494 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3501 * Set the application version into the object.
3503 * Return 0 on success else a negative value either an errno code or a
3504 * LTTng-UST error code.
3506 int ust_app_version(struct ust_app
*app
)
3512 pthread_mutex_lock(&app
->sock_lock
);
3513 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3514 pthread_mutex_unlock(&app
->sock_lock
);
3516 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
3517 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
3518 app
->pid
, app
->sock
);
3519 } else if (ret
== -EAGAIN
) {
3520 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
3521 app
->pid
, app
->sock
);
3523 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
3524 ret
, app
->pid
, app
->sock
);
3532 * Unregister app by removing it from the global traceable app list and freeing
3535 * The socket is already closed at this point so no close to sock.
3537 void ust_app_unregister(int sock
)
3539 struct ust_app
*lta
;
3540 struct lttng_ht_node_ulong
*node
;
3541 struct lttng_ht_iter ust_app_sock_iter
;
3542 struct lttng_ht_iter iter
;
3543 struct ust_app_session
*ua_sess
;
3548 /* Get the node reference for a call_rcu */
3549 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3550 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3553 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3554 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3557 * For per-PID buffers, perform "push metadata" and flush all
3558 * application streams before removing app from hash tables,
3559 * ensuring proper behavior of data_pending check.
3560 * Remove sessions so they are not visible during deletion.
3562 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3564 struct ust_registry_session
*registry
;
3566 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3568 /* The session was already removed so scheduled for teardown. */
3572 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3573 (void) ust_app_flush_app_session(lta
, ua_sess
);
3577 * Add session to list for teardown. This is safe since at this point we
3578 * are the only one using this list.
3580 pthread_mutex_lock(&ua_sess
->lock
);
3582 if (ua_sess
->deleted
) {
3583 pthread_mutex_unlock(&ua_sess
->lock
);
3588 * Normally, this is done in the delete session process which is
3589 * executed in the call rcu below. However, upon registration we can't
3590 * afford to wait for the grace period before pushing data or else the
3591 * data pending feature can race between the unregistration and stop
3592 * command where the data pending command is sent *before* the grace
3595 * The close metadata below nullifies the metadata pointer in the
3596 * session so the delete session will NOT push/close a second time.
3598 registry
= get_session_registry(ua_sess
);
3600 /* Push metadata for application before freeing the application. */
3601 (void) push_metadata(registry
, ua_sess
->consumer
);
3604 * Don't ask to close metadata for global per UID buffers. Close
3605 * metadata only on destroy trace session in this case. Also, the
3606 * previous push metadata could have flag the metadata registry to
3607 * close so don't send a close command if closed.
3609 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3610 /* And ask to close it for this session registry. */
3611 (void) close_metadata(registry
, ua_sess
->consumer
);
3614 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3616 pthread_mutex_unlock(&ua_sess
->lock
);
3619 /* Remove application from PID hash table */
3620 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3624 * Remove application from notify hash table. The thread handling the
3625 * notify socket could have deleted the node so ignore on error because
3626 * either way it's valid. The close of that socket is handled by the
3627 * apps_notify_thread.
3629 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3630 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3633 * Ignore return value since the node might have been removed before by an
3634 * add replace during app registration because the PID can be reassigned by
3637 iter
.iter
.node
= <a
->pid_n
.node
;
3638 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3640 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3645 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3652 * Fill events array with all events name of all registered apps.
3654 int ust_app_list_events(struct lttng_event
**events
)
3657 size_t nbmem
, count
= 0;
3658 struct lttng_ht_iter iter
;
3659 struct ust_app
*app
;
3660 struct lttng_event
*tmp_event
;
3662 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3663 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3664 if (tmp_event
== NULL
) {
3665 PERROR("zmalloc ust app events");
3672 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3673 struct lttng_ust_tracepoint_iter uiter
;
3675 health_code_update();
3677 if (!app
->compatible
) {
3679 * TODO: In time, we should notice the caller of this error by
3680 * telling him that this is a version error.
3684 pthread_mutex_lock(&app
->sock_lock
);
3685 handle
= ustctl_tracepoint_list(app
->sock
);
3687 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3688 ERR("UST app list events getting handle failed for app pid %d",
3691 pthread_mutex_unlock(&app
->sock_lock
);
3695 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3696 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3697 /* Handle ustctl error. */
3701 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3702 ERR("UST app tp list get failed for app %d with ret %d",
3705 DBG3("UST app tp list get failed. Application is dead");
3709 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3710 if (release_ret
< 0 &&
3711 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3712 release_ret
!= -EPIPE
) {
3713 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3715 pthread_mutex_unlock(&app
->sock_lock
);
3719 health_code_update();
3720 if (count
>= nbmem
) {
3721 /* In case the realloc fails, we free the memory */
3722 struct lttng_event
*new_tmp_event
;
3725 new_nbmem
= nbmem
<< 1;
3726 DBG2("Reallocating event list from %zu to %zu entries",
3728 new_tmp_event
= realloc(tmp_event
,
3729 new_nbmem
* sizeof(struct lttng_event
));
3730 if (new_tmp_event
== NULL
) {
3733 PERROR("realloc ust app events");
3736 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3737 if (release_ret
< 0 &&
3738 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3739 release_ret
!= -EPIPE
) {
3740 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3742 pthread_mutex_unlock(&app
->sock_lock
);
3745 /* Zero the new memory */
3746 memset(new_tmp_event
+ nbmem
, 0,
3747 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3749 tmp_event
= new_tmp_event
;
3751 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3752 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3753 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3754 tmp_event
[count
].pid
= app
->pid
;
3755 tmp_event
[count
].enabled
= -1;
3758 ret
= ustctl_release_handle(app
->sock
, handle
);
3759 pthread_mutex_unlock(&app
->sock_lock
);
3761 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3762 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
3763 app
->pid
, app
->sock
);
3764 } else if (ret
== -EAGAIN
) {
3765 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
3766 app
->pid
, app
->sock
);
3768 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
3769 ret
, app
->pid
, app
->sock
);
3775 *events
= tmp_event
;
3777 DBG2("UST app list events done (%zu events)", count
);
3782 health_code_update();
3787 * Fill events array with all events name of all registered apps.
3789 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3792 size_t nbmem
, count
= 0;
3793 struct lttng_ht_iter iter
;
3794 struct ust_app
*app
;
3795 struct lttng_event_field
*tmp_event
;
3797 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3798 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3799 if (tmp_event
== NULL
) {
3800 PERROR("zmalloc ust app event fields");
3807 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3808 struct lttng_ust_field_iter uiter
;
3810 health_code_update();
3812 if (!app
->compatible
) {
3814 * TODO: In time, we should notice the caller of this error by
3815 * telling him that this is a version error.
3819 pthread_mutex_lock(&app
->sock_lock
);
3820 handle
= ustctl_tracepoint_field_list(app
->sock
);
3822 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3823 ERR("UST app list field getting handle failed for app pid %d",
3826 pthread_mutex_unlock(&app
->sock_lock
);
3830 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3831 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3832 /* Handle ustctl error. */
3836 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3837 ERR("UST app tp list field failed for app %d with ret %d",
3840 DBG3("UST app tp list field failed. Application is dead");
3844 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3845 pthread_mutex_unlock(&app
->sock_lock
);
3846 if (release_ret
< 0 &&
3847 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3848 release_ret
!= -EPIPE
) {
3849 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3854 health_code_update();
3855 if (count
>= nbmem
) {
3856 /* In case the realloc fails, we free the memory */
3857 struct lttng_event_field
*new_tmp_event
;
3860 new_nbmem
= nbmem
<< 1;
3861 DBG2("Reallocating event field list from %zu to %zu entries",
3863 new_tmp_event
= realloc(tmp_event
,
3864 new_nbmem
* sizeof(struct lttng_event_field
));
3865 if (new_tmp_event
== NULL
) {
3868 PERROR("realloc ust app event fields");
3871 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3872 pthread_mutex_unlock(&app
->sock_lock
);
3874 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3875 release_ret
!= -EPIPE
) {
3876 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3880 /* Zero the new memory */
3881 memset(new_tmp_event
+ nbmem
, 0,
3882 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3884 tmp_event
= new_tmp_event
;
3887 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3888 /* Mapping between these enums matches 1 to 1. */
3889 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3890 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3892 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3893 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3894 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3895 tmp_event
[count
].event
.pid
= app
->pid
;
3896 tmp_event
[count
].event
.enabled
= -1;
3899 ret
= ustctl_release_handle(app
->sock
, handle
);
3900 pthread_mutex_unlock(&app
->sock_lock
);
3902 ret
!= -LTTNG_UST_ERR_EXITING
&&
3904 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3909 *fields
= tmp_event
;
3911 DBG2("UST app list event fields done (%zu events)", count
);
3916 health_code_update();
3921 * Free and clean all traceable apps of the global list.
3923 * Should _NOT_ be called with RCU read-side lock held.
3925 void ust_app_clean_list(void)
3928 struct ust_app
*app
;
3929 struct lttng_ht_iter iter
;
3931 DBG2("UST app cleaning registered apps hash table");
3935 /* Cleanup notify socket hash table */
3936 if (ust_app_ht_by_notify_sock
) {
3937 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3938 notify_sock_n
.node
) {
3939 struct cds_lfht_node
*node
;
3940 struct ust_app
*app
;
3942 node
= cds_lfht_iter_get_node(&iter
.iter
);
3947 app
= container_of(node
, struct ust_app
,
3948 notify_sock_n
.node
);
3949 ust_app_notify_sock_unregister(app
->notify_sock
);
3954 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3955 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3957 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3961 /* Cleanup socket hash table */
3962 if (ust_app_ht_by_sock
) {
3963 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3965 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3972 /* Destroy is done only when the ht is empty */
3974 ht_cleanup_push(ust_app_ht
);
3976 if (ust_app_ht_by_sock
) {
3977 ht_cleanup_push(ust_app_ht_by_sock
);
3979 if (ust_app_ht_by_notify_sock
) {
3980 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3985 * Init UST app hash table.
3987 int ust_app_ht_alloc(void)
3989 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3993 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3994 if (!ust_app_ht_by_sock
) {
3997 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3998 if (!ust_app_ht_by_notify_sock
) {
4005 * For a specific UST session, disable the channel for all registered apps.
4007 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4008 struct ltt_ust_channel
*uchan
)
4011 struct lttng_ht_iter iter
;
4012 struct lttng_ht_node_str
*ua_chan_node
;
4013 struct ust_app
*app
;
4014 struct ust_app_session
*ua_sess
;
4015 struct ust_app_channel
*ua_chan
;
4017 assert(usess
->active
);
4018 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4019 uchan
->name
, usess
->id
);
4023 /* For every registered applications */
4024 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4025 struct lttng_ht_iter uiter
;
4026 if (!app
->compatible
) {
4028 * TODO: In time, we should notice the caller of this error by
4029 * telling him that this is a version error.
4033 ua_sess
= lookup_session_by_app(usess
, app
);
4034 if (ua_sess
== NULL
) {
4039 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4040 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4041 /* If the session if found for the app, the channel must be there */
4042 assert(ua_chan_node
);
4044 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4045 /* The channel must not be already disabled */
4046 assert(ua_chan
->enabled
== 1);
4048 /* Disable channel onto application */
4049 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4051 /* XXX: We might want to report this error at some point... */
4061 * For a specific UST session, enable the channel for all registered apps.
4063 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4064 struct ltt_ust_channel
*uchan
)
4067 struct lttng_ht_iter iter
;
4068 struct ust_app
*app
;
4069 struct ust_app_session
*ua_sess
;
4071 assert(usess
->active
);
4072 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4073 uchan
->name
, usess
->id
);
4077 /* For every registered applications */
4078 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4079 if (!app
->compatible
) {
4081 * TODO: In time, we should notice the caller of this error by
4082 * telling him that this is a version error.
4086 ua_sess
= lookup_session_by_app(usess
, app
);
4087 if (ua_sess
== NULL
) {
4091 /* Enable channel onto application */
4092 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4094 /* XXX: We might want to report this error at some point... */
4104 * Disable an event in a channel and for a specific session.
4106 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4107 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4110 struct lttng_ht_iter iter
, uiter
;
4111 struct lttng_ht_node_str
*ua_chan_node
;
4112 struct ust_app
*app
;
4113 struct ust_app_session
*ua_sess
;
4114 struct ust_app_channel
*ua_chan
;
4115 struct ust_app_event
*ua_event
;
4117 assert(usess
->active
);
4118 DBG("UST app disabling event %s for all apps in channel "
4119 "%s for session id %" PRIu64
,
4120 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4124 /* For all registered applications */
4125 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4126 if (!app
->compatible
) {
4128 * TODO: In time, we should notice the caller of this error by
4129 * telling him that this is a version error.
4133 ua_sess
= lookup_session_by_app(usess
, app
);
4134 if (ua_sess
== NULL
) {
4139 /* Lookup channel in the ust app session */
4140 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4141 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4142 if (ua_chan_node
== NULL
) {
4143 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4144 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4147 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4149 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4150 uevent
->filter
, uevent
->attr
.loglevel
,
4152 if (ua_event
== NULL
) {
4153 DBG2("Event %s not found in channel %s for app pid %d."
4154 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4158 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4160 /* XXX: Report error someday... */
4169 /* The ua_sess lock must be held by the caller. */
4171 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4172 struct ust_app_session
*ua_sess
,
4173 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4174 struct ust_app_channel
**_ua_chan
)
4177 struct ust_app_channel
*ua_chan
= NULL
;
4180 ASSERT_LOCKED(ua_sess
->lock
);
4182 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4183 sizeof(uchan
->name
))) {
4184 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4188 struct ltt_ust_context
*uctx
= NULL
;
4191 * Create channel onto application and synchronize its
4194 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4195 LTTNG_UST_CHAN_PER_CPU
, usess
,
4201 ret
= ust_app_channel_send(app
, usess
,
4208 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4209 ret
= create_ust_app_channel_context(ua_chan
,
4222 * The application's socket is not valid. Either a bad socket
4223 * or a timeout on it. We can't inform the caller that for a
4224 * specific app, the session failed so lets continue here.
4226 ret
= 0; /* Not an error. */
4234 if (ret
== 0 && _ua_chan
) {
4236 * Only return the application's channel on success. Note
4237 * that the channel can still be part of the application's
4238 * channel hashtable on error.
4240 *_ua_chan
= ua_chan
;
4246 * Enable event for a specific session and channel on the tracer.
4248 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4249 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4252 struct lttng_ht_iter iter
, uiter
;
4253 struct lttng_ht_node_str
*ua_chan_node
;
4254 struct ust_app
*app
;
4255 struct ust_app_session
*ua_sess
;
4256 struct ust_app_channel
*ua_chan
;
4257 struct ust_app_event
*ua_event
;
4259 assert(usess
->active
);
4260 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4261 uevent
->attr
.name
, usess
->id
);
4264 * NOTE: At this point, this function is called only if the session and
4265 * channel passed are already created for all apps. and enabled on the
4271 /* For all registered applications */
4272 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4273 if (!app
->compatible
) {
4275 * TODO: In time, we should notice the caller of this error by
4276 * telling him that this is a version error.
4280 ua_sess
= lookup_session_by_app(usess
, app
);
4282 /* The application has problem or is probably dead. */
4286 pthread_mutex_lock(&ua_sess
->lock
);
4288 if (ua_sess
->deleted
) {
4289 pthread_mutex_unlock(&ua_sess
->lock
);
4293 /* Lookup channel in the ust app session */
4294 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4295 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4297 * It is possible that the channel cannot be found is
4298 * the channel/event creation occurs concurrently with
4299 * an application exit.
4301 if (!ua_chan_node
) {
4302 pthread_mutex_unlock(&ua_sess
->lock
);
4306 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4308 /* Get event node */
4309 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4310 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4311 if (ua_event
== NULL
) {
4312 DBG3("UST app enable event %s not found for app PID %d."
4313 "Skipping app", uevent
->attr
.name
, app
->pid
);
4317 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4319 pthread_mutex_unlock(&ua_sess
->lock
);
4323 pthread_mutex_unlock(&ua_sess
->lock
);
4332 * For a specific existing UST session and UST channel, creates the event for
4333 * all registered apps.
4335 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4336 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4339 struct lttng_ht_iter iter
, uiter
;
4340 struct lttng_ht_node_str
*ua_chan_node
;
4341 struct ust_app
*app
;
4342 struct ust_app_session
*ua_sess
;
4343 struct ust_app_channel
*ua_chan
;
4345 assert(usess
->active
);
4346 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4347 uevent
->attr
.name
, usess
->id
);
4351 /* For all registered applications */
4352 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4353 if (!app
->compatible
) {
4355 * TODO: In time, we should notice the caller of this error by
4356 * telling him that this is a version error.
4360 ua_sess
= lookup_session_by_app(usess
, app
);
4362 /* The application has problem or is probably dead. */
4366 pthread_mutex_lock(&ua_sess
->lock
);
4368 if (ua_sess
->deleted
) {
4369 pthread_mutex_unlock(&ua_sess
->lock
);
4373 /* Lookup channel in the ust app session */
4374 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4375 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4376 /* If the channel is not found, there is a code flow error */
4377 assert(ua_chan_node
);
4379 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4381 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4382 pthread_mutex_unlock(&ua_sess
->lock
);
4384 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4385 /* Possible value at this point: -ENOMEM. If so, we stop! */
4388 DBG2("UST app event %s already exist on app PID %d",
4389 uevent
->attr
.name
, app
->pid
);
4399 * Start tracing for a specific UST session and app.
4401 * Called with UST app session lock held.
4405 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4408 struct ust_app_session
*ua_sess
;
4410 DBG("Starting tracing for ust app pid %d", app
->pid
);
4414 if (!app
->compatible
) {
4418 ua_sess
= lookup_session_by_app(usess
, app
);
4419 if (ua_sess
== NULL
) {
4420 /* The session is in teardown process. Ignore and continue. */
4424 pthread_mutex_lock(&ua_sess
->lock
);
4426 if (ua_sess
->deleted
) {
4427 pthread_mutex_unlock(&ua_sess
->lock
);
4431 if (ua_sess
->enabled
) {
4432 pthread_mutex_unlock(&ua_sess
->lock
);
4436 /* Upon restart, we skip the setup, already done */
4437 if (ua_sess
->started
) {
4441 health_code_update();
4444 /* This starts the UST tracing */
4445 pthread_mutex_lock(&app
->sock_lock
);
4446 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4447 pthread_mutex_unlock(&app
->sock_lock
);
4449 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4450 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
4451 app
->pid
, app
->sock
);
4452 pthread_mutex_unlock(&ua_sess
->lock
);
4454 } else if (ret
== -EAGAIN
) {
4455 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
4456 app
->pid
, app
->sock
);
4457 pthread_mutex_unlock(&ua_sess
->lock
);
4461 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
4462 ret
, app
->pid
, app
->sock
);
4467 /* Indicate that the session has been started once */
4468 ua_sess
->started
= 1;
4469 ua_sess
->enabled
= 1;
4471 pthread_mutex_unlock(&ua_sess
->lock
);
4473 health_code_update();
4475 /* Quiescent wait after starting trace */
4476 pthread_mutex_lock(&app
->sock_lock
);
4477 ret
= ustctl_wait_quiescent(app
->sock
);
4478 pthread_mutex_unlock(&app
->sock_lock
);
4480 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4481 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
4482 app
->pid
, app
->sock
);
4483 } else if (ret
== -EAGAIN
) {
4484 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
4485 app
->pid
, app
->sock
);
4487 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
4488 ret
, app
->pid
, app
->sock
);
4494 health_code_update();
4498 pthread_mutex_unlock(&ua_sess
->lock
);
4500 health_code_update();
4505 * Stop tracing for a specific UST session and app.
4508 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4511 struct ust_app_session
*ua_sess
;
4512 struct ust_registry_session
*registry
;
4514 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4518 if (!app
->compatible
) {
4519 goto end_no_session
;
4522 ua_sess
= lookup_session_by_app(usess
, app
);
4523 if (ua_sess
== NULL
) {
4524 goto end_no_session
;
4527 pthread_mutex_lock(&ua_sess
->lock
);
4529 if (ua_sess
->deleted
) {
4530 pthread_mutex_unlock(&ua_sess
->lock
);
4531 goto end_no_session
;
4535 * If started = 0, it means that stop trace has been called for a session
4536 * that was never started. It's possible since we can have a fail start
4537 * from either the application manager thread or the command thread. Simply
4538 * indicate that this is a stop error.
4540 if (!ua_sess
->started
) {
4541 goto error_rcu_unlock
;
4544 health_code_update();
4546 /* This inhibits UST tracing */
4547 pthread_mutex_lock(&app
->sock_lock
);
4548 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4549 pthread_mutex_unlock(&app
->sock_lock
);
4551 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4552 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
4553 app
->pid
, app
->sock
);
4555 } else if (ret
== -EAGAIN
) {
4556 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
4557 app
->pid
, app
->sock
);
4561 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
4562 ret
, app
->pid
, app
->sock
);
4564 goto error_rcu_unlock
;
4567 health_code_update();
4568 ua_sess
->enabled
= 0;
4570 /* Quiescent wait after stopping trace */
4571 pthread_mutex_lock(&app
->sock_lock
);
4572 ret
= ustctl_wait_quiescent(app
->sock
);
4573 pthread_mutex_unlock(&app
->sock_lock
);
4575 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4576 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d)",
4577 app
->pid
, app
->sock
);
4578 } else if (ret
== -EAGAIN
) {
4579 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d)",
4580 app
->pid
, app
->sock
);
4582 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d)",
4583 ret
, app
->pid
, app
->sock
);
4587 health_code_update();
4589 registry
= get_session_registry(ua_sess
);
4591 /* The UST app session is held registry shall not be null. */
4594 /* Push metadata for application before freeing the application. */
4595 (void) push_metadata(registry
, ua_sess
->consumer
);
4598 pthread_mutex_unlock(&ua_sess
->lock
);
4601 health_code_update();
4605 pthread_mutex_unlock(&ua_sess
->lock
);
4607 health_code_update();
4612 int ust_app_flush_app_session(struct ust_app
*app
,
4613 struct ust_app_session
*ua_sess
)
4615 int ret
, retval
= 0;
4616 struct lttng_ht_iter iter
;
4617 struct ust_app_channel
*ua_chan
;
4618 struct consumer_socket
*socket
;
4620 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4624 if (!app
->compatible
) {
4625 goto end_not_compatible
;
4628 pthread_mutex_lock(&ua_sess
->lock
);
4630 if (ua_sess
->deleted
) {
4634 health_code_update();
4636 /* Flushing buffers */
4637 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4640 /* Flush buffers and push metadata. */
4641 switch (ua_sess
->buffer_type
) {
4642 case LTTNG_BUFFER_PER_PID
:
4643 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4645 health_code_update();
4646 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4648 ERR("Error flushing consumer channel");
4654 case LTTNG_BUFFER_PER_UID
:
4660 health_code_update();
4663 pthread_mutex_unlock(&ua_sess
->lock
);
4667 health_code_update();
4672 * Flush buffers for all applications for a specific UST session.
4673 * Called with UST session lock held.
4676 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4681 DBG("Flushing session buffers for all ust apps");
4685 /* Flush buffers and push metadata. */
4686 switch (usess
->buffer_type
) {
4687 case LTTNG_BUFFER_PER_UID
:
4689 struct buffer_reg_uid
*reg
;
4690 struct lttng_ht_iter iter
;
4692 /* Flush all per UID buffers associated to that session. */
4693 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4694 struct ust_registry_session
*ust_session_reg
;
4695 struct buffer_reg_channel
*reg_chan
;
4696 struct consumer_socket
*socket
;
4698 /* Get consumer socket to use to push the metadata.*/
4699 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4702 /* Ignore request if no consumer is found for the session. */
4706 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4707 reg_chan
, node
.node
) {
4709 * The following call will print error values so the return
4710 * code is of little importance because whatever happens, we
4711 * have to try them all.
4713 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4716 ust_session_reg
= reg
->registry
->reg
.ust
;
4717 /* Push metadata. */
4718 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4722 case LTTNG_BUFFER_PER_PID
:
4724 struct ust_app_session
*ua_sess
;
4725 struct lttng_ht_iter iter
;
4726 struct ust_app
*app
;
4728 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4729 ua_sess
= lookup_session_by_app(usess
, app
);
4730 if (ua_sess
== NULL
) {
4733 (void) ust_app_flush_app_session(app
, ua_sess
);
4744 health_code_update();
4749 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
4750 struct ust_app_session
*ua_sess
)
4753 struct lttng_ht_iter iter
;
4754 struct ust_app_channel
*ua_chan
;
4755 struct consumer_socket
*socket
;
4757 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
4761 if (!app
->compatible
) {
4762 goto end_not_compatible
;
4765 pthread_mutex_lock(&ua_sess
->lock
);
4767 if (ua_sess
->deleted
) {
4771 health_code_update();
4773 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4776 ERR("Failed to find consumer (%" PRIu32
") socket",
4777 app
->bits_per_long
);
4782 /* Clear quiescent state. */
4783 switch (ua_sess
->buffer_type
) {
4784 case LTTNG_BUFFER_PER_PID
:
4785 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
4786 ua_chan
, node
.node
) {
4787 health_code_update();
4788 ret
= consumer_clear_quiescent_channel(socket
,
4791 ERR("Error clearing quiescent state for consumer channel");
4797 case LTTNG_BUFFER_PER_UID
:
4804 health_code_update();
4807 pthread_mutex_unlock(&ua_sess
->lock
);
4811 health_code_update();
4816 * Clear quiescent state in each stream for all applications for a
4817 * specific UST session.
4818 * Called with UST session lock held.
4821 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
4826 DBG("Clearing stream quiescent state for all ust apps");
4830 switch (usess
->buffer_type
) {
4831 case LTTNG_BUFFER_PER_UID
:
4833 struct lttng_ht_iter iter
;
4834 struct buffer_reg_uid
*reg
;
4837 * Clear quiescent for all per UID buffers associated to
4840 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4841 struct consumer_socket
*socket
;
4842 struct buffer_reg_channel
*reg_chan
;
4844 /* Get associated consumer socket.*/
4845 socket
= consumer_find_socket_by_bitness(
4846 reg
->bits_per_long
, usess
->consumer
);
4849 * Ignore request if no consumer is found for
4855 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
4856 &iter
.iter
, reg_chan
, node
.node
) {
4858 * The following call will print error values so
4859 * the return code is of little importance
4860 * because whatever happens, we have to try them
4863 (void) consumer_clear_quiescent_channel(socket
,
4864 reg_chan
->consumer_key
);
4869 case LTTNG_BUFFER_PER_PID
:
4871 struct ust_app_session
*ua_sess
;
4872 struct lttng_ht_iter iter
;
4873 struct ust_app
*app
;
4875 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
4877 ua_sess
= lookup_session_by_app(usess
, app
);
4878 if (ua_sess
== NULL
) {
4881 (void) ust_app_clear_quiescent_app_session(app
,
4893 health_code_update();
4898 * Destroy a specific UST session in apps.
4900 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4903 struct ust_app_session
*ua_sess
;
4904 struct lttng_ht_iter iter
;
4905 struct lttng_ht_node_u64
*node
;
4907 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4911 if (!app
->compatible
) {
4915 __lookup_session_by_app(usess
, app
, &iter
);
4916 node
= lttng_ht_iter_get_node_u64(&iter
);
4918 /* Session is being or is deleted. */
4921 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4923 health_code_update();
4924 destroy_app_session(app
, ua_sess
);
4926 health_code_update();
4928 /* Quiescent wait after stopping trace */
4929 pthread_mutex_lock(&app
->sock_lock
);
4930 ret
= ustctl_wait_quiescent(app
->sock
);
4931 pthread_mutex_unlock(&app
->sock_lock
);
4933 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4934 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d)",
4935 app
->pid
, app
->sock
);
4936 } else if (ret
== -EAGAIN
) {
4937 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d)",
4938 app
->pid
, app
->sock
);
4940 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d)",
4941 ret
, app
->pid
, app
->sock
);
4946 health_code_update();
4951 * Start tracing for the UST session.
4953 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4955 struct lttng_ht_iter iter
;
4956 struct ust_app
*app
;
4958 DBG("Starting all UST traces");
4961 * Even though the start trace might fail, flag this session active so
4962 * other application coming in are started by default.
4969 * In a start-stop-start use-case, we need to clear the quiescent state
4970 * of each channel set by the prior stop command, thus ensuring that a
4971 * following stop or destroy is sure to grab a timestamp_end near those
4972 * operations, even if the packet is empty.
4974 (void) ust_app_clear_quiescent_session(usess
);
4976 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4977 ust_app_global_update(usess
, app
);
4986 * Start tracing for the UST session.
4987 * Called with UST session lock held.
4989 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4992 struct lttng_ht_iter iter
;
4993 struct ust_app
*app
;
4995 DBG("Stopping all UST traces");
4998 * Even though the stop trace might fail, flag this session inactive so
4999 * other application coming in are not started by default.
5005 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5006 ret
= ust_app_stop_trace(usess
, app
);
5008 /* Continue to next apps even on error */
5013 (void) ust_app_flush_session(usess
);
5021 * Destroy app UST session.
5023 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5026 struct lttng_ht_iter iter
;
5027 struct ust_app
*app
;
5029 DBG("Destroy all UST traces");
5033 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5034 ret
= destroy_trace(usess
, app
);
5036 /* Continue to next apps even on error */
5046 /* The ua_sess lock must be held by the caller. */
5048 int find_or_create_ust_app_channel(
5049 struct ltt_ust_session
*usess
,
5050 struct ust_app_session
*ua_sess
,
5051 struct ust_app
*app
,
5052 struct ltt_ust_channel
*uchan
,
5053 struct ust_app_channel
**ua_chan
)
5056 struct lttng_ht_iter iter
;
5057 struct lttng_ht_node_str
*ua_chan_node
;
5059 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5060 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5062 *ua_chan
= caa_container_of(ua_chan_node
,
5063 struct ust_app_channel
, node
);
5067 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5076 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5077 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5078 struct ust_app
*app
)
5081 struct ust_app_event
*ua_event
= NULL
;
5083 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5084 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5086 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5091 if (ua_event
->enabled
!= uevent
->enabled
) {
5092 ret
= uevent
->enabled
?
5093 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5094 disable_ust_app_event(ua_sess
, ua_event
, app
);
5103 * The caller must ensure that the application is compatible and is tracked
5104 * by the process attribute trackers.
5107 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5108 struct ust_app
*app
)
5111 struct cds_lfht_iter uchan_iter
;
5112 struct ltt_ust_channel
*uchan
;
5113 struct ust_app_session
*ua_sess
= NULL
;
5116 * The application's configuration should only be synchronized for
5119 assert(usess
->active
);
5121 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5123 /* Tracer is probably gone or ENOMEM. */
5128 pthread_mutex_lock(&ua_sess
->lock
);
5129 if (ua_sess
->deleted
) {
5130 pthread_mutex_unlock(&ua_sess
->lock
);
5136 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5138 struct ust_app_channel
*ua_chan
;
5139 struct cds_lfht_iter uevent_iter
;
5140 struct ltt_ust_event
*uevent
;
5143 * Search for a matching ust_app_channel. If none is found,
5144 * create it. Creating the channel will cause the ua_chan
5145 * structure to be allocated, the channel buffers to be
5146 * allocated (if necessary) and sent to the application, and
5147 * all enabled contexts will be added to the channel.
5149 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5150 app
, uchan
, &ua_chan
);
5152 /* Tracer is probably gone or ENOMEM. */
5157 /* ua_chan will be NULL for the metadata channel */
5161 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5163 ret
= ust_app_channel_synchronize_event(ua_chan
,
5164 uevent
, ua_sess
, app
);
5170 if (ua_chan
->enabled
!= uchan
->enabled
) {
5171 ret
= uchan
->enabled
?
5172 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5173 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5181 * Create the metadata for the application. This returns gracefully if a
5182 * metadata was already set for the session.
5184 * The metadata channel must be created after the data channels as the
5185 * consumer daemon assumes this ordering. When interacting with a relay
5186 * daemon, the consumer will use this assumption to send the
5187 * "STREAMS_SENT" message to the relay daemon.
5189 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5197 pthread_mutex_unlock(&ua_sess
->lock
);
5198 /* Everything went well at this point. */
5203 pthread_mutex_unlock(&ua_sess
->lock
);
5206 destroy_app_session(app
, ua_sess
);
5212 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5214 struct ust_app_session
*ua_sess
;
5216 ua_sess
= lookup_session_by_app(usess
, app
);
5217 if (ua_sess
== NULL
) {
5220 destroy_app_session(app
, ua_sess
);
5224 * Add channels/events from UST global domain to registered apps at sock.
5226 * Called with session lock held.
5227 * Called with RCU read-side lock held.
5229 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5232 assert(usess
->active
);
5234 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5235 app
->sock
, usess
->id
);
5237 if (!app
->compatible
) {
5240 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5242 trace_ust_id_tracker_lookup(
5243 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5245 trace_ust_id_tracker_lookup(
5246 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5249 * Synchronize the application's internal tracing configuration
5250 * and start tracing.
5252 ust_app_synchronize(usess
, app
);
5253 ust_app_start_trace(usess
, app
);
5255 ust_app_global_destroy(usess
, app
);
5260 * Called with session lock held.
5262 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5264 struct lttng_ht_iter iter
;
5265 struct ust_app
*app
;
5268 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5269 ust_app_global_update(usess
, app
);
5275 * Add context to a specific channel for global UST domain.
5277 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5278 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5281 struct lttng_ht_node_str
*ua_chan_node
;
5282 struct lttng_ht_iter iter
, uiter
;
5283 struct ust_app_channel
*ua_chan
= NULL
;
5284 struct ust_app_session
*ua_sess
;
5285 struct ust_app
*app
;
5287 assert(usess
->active
);
5290 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5291 if (!app
->compatible
) {
5293 * TODO: In time, we should notice the caller of this error by
5294 * telling him that this is a version error.
5298 ua_sess
= lookup_session_by_app(usess
, app
);
5299 if (ua_sess
== NULL
) {
5303 pthread_mutex_lock(&ua_sess
->lock
);
5305 if (ua_sess
->deleted
) {
5306 pthread_mutex_unlock(&ua_sess
->lock
);
5310 /* Lookup channel in the ust app session */
5311 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5312 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5313 if (ua_chan_node
== NULL
) {
5316 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5318 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5323 pthread_mutex_unlock(&ua_sess
->lock
);
5331 * Receive registration and populate the given msg structure.
5333 * On success return 0 else a negative value returned by the ustctl call.
5335 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5338 uint32_t pid
, ppid
, uid
, gid
;
5342 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5343 &pid
, &ppid
, &uid
, &gid
,
5344 &msg
->bits_per_long
,
5345 &msg
->uint8_t_alignment
,
5346 &msg
->uint16_t_alignment
,
5347 &msg
->uint32_t_alignment
,
5348 &msg
->uint64_t_alignment
,
5349 &msg
->long_alignment
,
5356 case LTTNG_UST_ERR_EXITING
:
5357 DBG3("UST app recv reg message failed. Application died");
5359 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5360 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5361 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5362 LTTNG_UST_ABI_MINOR_VERSION
);
5365 ERR("UST app recv reg message failed with ret %d", ret
);
5370 msg
->pid
= (pid_t
) pid
;
5371 msg
->ppid
= (pid_t
) ppid
;
5372 msg
->uid
= (uid_t
) uid
;
5373 msg
->gid
= (gid_t
) gid
;
5380 * Return a ust app session object using the application object and the
5381 * session object descriptor has a key. If not found, NULL is returned.
5382 * A RCU read side lock MUST be acquired when calling this function.
5384 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5387 struct lttng_ht_node_ulong
*node
;
5388 struct lttng_ht_iter iter
;
5389 struct ust_app_session
*ua_sess
= NULL
;
5393 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5394 node
= lttng_ht_iter_get_node_ulong(&iter
);
5396 DBG2("UST app session find by objd %d not found", objd
);
5400 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5407 * Return a ust app channel object using the application object and the channel
5408 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5409 * lock MUST be acquired before calling this function.
5411 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5414 struct lttng_ht_node_ulong
*node
;
5415 struct lttng_ht_iter iter
;
5416 struct ust_app_channel
*ua_chan
= NULL
;
5420 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5421 node
= lttng_ht_iter_get_node_ulong(&iter
);
5423 DBG2("UST app channel find by objd %d not found", objd
);
5427 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5434 * Reply to a register channel notification from an application on the notify
5435 * socket. The channel metadata is also created.
5437 * The session UST registry lock is acquired in this function.
5439 * On success 0 is returned else a negative value.
5441 static int reply_ust_register_channel(int sock
, int cobjd
,
5442 size_t nr_fields
, struct ustctl_field
*fields
)
5444 int ret
, ret_code
= 0;
5446 uint64_t chan_reg_key
;
5447 enum ustctl_channel_header type
;
5448 struct ust_app
*app
;
5449 struct ust_app_channel
*ua_chan
;
5450 struct ust_app_session
*ua_sess
;
5451 struct ust_registry_session
*registry
;
5452 struct ust_registry_channel
*chan_reg
;
5456 /* Lookup application. If not found, there is a code flow error. */
5457 app
= find_app_by_notify_sock(sock
);
5459 DBG("Application socket %d is being torn down. Abort event notify",
5462 goto error_rcu_unlock
;
5465 /* Lookup channel by UST object descriptor. */
5466 ua_chan
= find_channel_by_objd(app
, cobjd
);
5468 DBG("Application channel is being torn down. Abort event notify");
5470 goto error_rcu_unlock
;
5473 assert(ua_chan
->session
);
5474 ua_sess
= ua_chan
->session
;
5476 /* Get right session registry depending on the session buffer type. */
5477 registry
= get_session_registry(ua_sess
);
5479 DBG("Application session is being torn down. Abort event notify");
5481 goto error_rcu_unlock
;
5484 /* Depending on the buffer type, a different channel key is used. */
5485 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5486 chan_reg_key
= ua_chan
->tracing_channel_id
;
5488 chan_reg_key
= ua_chan
->key
;
5491 pthread_mutex_lock(®istry
->lock
);
5493 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5496 if (!chan_reg
->register_done
) {
5498 * TODO: eventually use the registry event count for
5499 * this channel to better guess header type for per-pid
5502 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5503 chan_reg
->nr_ctx_fields
= nr_fields
;
5504 chan_reg
->ctx_fields
= fields
;
5506 chan_reg
->header_type
= type
;
5508 /* Get current already assigned values. */
5509 type
= chan_reg
->header_type
;
5511 /* Channel id is set during the object creation. */
5512 chan_id
= chan_reg
->chan_id
;
5514 /* Append to metadata */
5515 if (!chan_reg
->metadata_dumped
) {
5516 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5518 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5524 DBG3("UST app replying to register channel key %" PRIu64
5525 " with id %u, type = %d, ret = %d", chan_reg_key
, chan_id
, type
,
5528 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5530 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5531 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
5532 app
->pid
, app
->sock
);
5533 } else if (ret
== -EAGAIN
) {
5534 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
5535 app
->pid
, app
->sock
);
5537 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
5538 ret
, app
->pid
, app
->sock
);
5543 /* This channel registry registration is completed. */
5544 chan_reg
->register_done
= 1;
5547 pthread_mutex_unlock(®istry
->lock
);
5555 * Add event to the UST channel registry. When the event is added to the
5556 * registry, the metadata is also created. Once done, this replies to the
5557 * application with the appropriate error code.
5559 * The session UST registry lock is acquired in the function.
5561 * On success 0 is returned else a negative value.
5563 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5564 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5565 int loglevel_value
, char *model_emf_uri
)
5568 uint32_t event_id
= 0;
5569 uint64_t chan_reg_key
;
5570 struct ust_app
*app
;
5571 struct ust_app_channel
*ua_chan
;
5572 struct ust_app_session
*ua_sess
;
5573 struct ust_registry_session
*registry
;
5577 /* Lookup application. If not found, there is a code flow error. */
5578 app
= find_app_by_notify_sock(sock
);
5580 DBG("Application socket %d is being torn down. Abort event notify",
5583 goto error_rcu_unlock
;
5586 /* Lookup channel by UST object descriptor. */
5587 ua_chan
= find_channel_by_objd(app
, cobjd
);
5589 DBG("Application channel is being torn down. Abort event notify");
5591 goto error_rcu_unlock
;
5594 assert(ua_chan
->session
);
5595 ua_sess
= ua_chan
->session
;
5597 registry
= get_session_registry(ua_sess
);
5599 DBG("Application session is being torn down. Abort event notify");
5601 goto error_rcu_unlock
;
5604 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5605 chan_reg_key
= ua_chan
->tracing_channel_id
;
5607 chan_reg_key
= ua_chan
->key
;
5610 pthread_mutex_lock(®istry
->lock
);
5613 * From this point on, this call acquires the ownership of the sig, fields
5614 * and model_emf_uri meaning any free are done inside it if needed. These
5615 * three variables MUST NOT be read/write after this.
5617 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5618 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5619 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5623 model_emf_uri
= NULL
;
5626 * The return value is returned to ustctl so in case of an error, the
5627 * application can be notified. In case of an error, it's important not to
5628 * return a negative error or else the application will get closed.
5630 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5632 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5633 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
5634 app
->pid
, app
->sock
);
5635 } else if (ret
== -EAGAIN
) {
5636 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
5637 app
->pid
, app
->sock
);
5639 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
5640 ret
, app
->pid
, app
->sock
);
5643 * No need to wipe the create event since the application socket will
5644 * get close on error hence cleaning up everything by itself.
5649 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5653 pthread_mutex_unlock(®istry
->lock
);
5658 free(model_emf_uri
);
5663 * Add enum to the UST session registry. Once done, this replies to the
5664 * application with the appropriate error code.
5666 * The session UST registry lock is acquired within this function.
5668 * On success 0 is returned else a negative value.
5670 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
5671 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
5673 int ret
= 0, ret_code
;
5674 struct ust_app
*app
;
5675 struct ust_app_session
*ua_sess
;
5676 struct ust_registry_session
*registry
;
5677 uint64_t enum_id
= -1ULL;
5681 /* Lookup application. If not found, there is a code flow error. */
5682 app
= find_app_by_notify_sock(sock
);
5684 /* Return an error since this is not an error */
5685 DBG("Application socket %d is being torn down. Aborting enum registration",
5689 goto error_rcu_unlock
;
5692 /* Lookup session by UST object descriptor. */
5693 ua_sess
= find_session_by_objd(app
, sobjd
);
5695 /* Return an error since this is not an error */
5696 DBG("Application session is being torn down (session not found Aborting enum registration.");
5698 goto error_rcu_unlock
;
5701 registry
= get_session_registry(ua_sess
);
5703 DBG("Application session is being torn down (registry not found Aborting enum registration.");
5705 goto error_rcu_unlock
;
5708 pthread_mutex_lock(®istry
->lock
);
5711 * From this point on, the callee acquires the ownership of
5712 * entries. The variable entries MUST NOT be read/written after
5715 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
5716 entries
, nr_entries
, &enum_id
);
5720 * The return value is returned to ustctl so in case of an error, the
5721 * application can be notified. In case of an error, it's important not to
5722 * return a negative error or else the application will get closed.
5724 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
5726 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5727 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
5728 app
->pid
, app
->sock
);
5729 } else if (ret
== -EAGAIN
) {
5730 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
5731 app
->pid
, app
->sock
);
5733 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
5734 ret
, app
->pid
, app
->sock
);
5737 * No need to wipe the create enum since the application socket will
5738 * get close on error hence cleaning up everything by itself.
5743 DBG3("UST registry enum %s added successfully or already found", name
);
5746 pthread_mutex_unlock(®istry
->lock
);
5753 * Handle application notification through the given notify socket.
5755 * Return 0 on success or else a negative value.
5757 int ust_app_recv_notify(int sock
)
5760 enum ustctl_notify_cmd cmd
;
5762 DBG3("UST app receiving notify from sock %d", sock
);
5764 ret
= ustctl_recv_notify(sock
, &cmd
);
5766 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5767 DBG3("UST app recv notify failed. Application died: sock = %d",
5769 } else if (ret
== -EAGAIN
) {
5770 WARN("UST app recv notify failed. Communication time out: sock = %d",
5773 ERR("UST app recv notify failed with ret %d: sock = %d",
5780 case USTCTL_NOTIFY_CMD_EVENT
:
5782 int sobjd
, cobjd
, loglevel_value
;
5783 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5785 struct ustctl_field
*fields
;
5787 DBG2("UST app ustctl register event received");
5789 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5790 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5793 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5794 DBG3("UST app recv event failed. Application died: sock = %d",
5796 } else if (ret
== -EAGAIN
) {
5797 WARN("UST app recv event failed. Communication time out: sock = %d",
5800 ERR("UST app recv event failed with ret %d: sock = %d",
5807 * Add event to the UST registry coming from the notify socket. This
5808 * call will free if needed the sig, fields and model_emf_uri. This
5809 * code path loses the ownsership of these variables and transfer them
5810 * to the this function.
5812 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5813 fields
, loglevel_value
, model_emf_uri
);
5820 case USTCTL_NOTIFY_CMD_CHANNEL
:
5824 struct ustctl_field
*fields
;
5826 DBG2("UST app ustctl register channel received");
5828 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5831 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5832 DBG3("UST app recv channel failed. Application died: sock = %d",
5834 } else if (ret
== -EAGAIN
) {
5835 WARN("UST app recv channel failed. Communication time out: sock = %d",
5838 ERR("UST app recv channel failed with ret %d: sock = %d)",
5845 * The fields ownership are transfered to this function call meaning
5846 * that if needed it will be freed. After this, it's invalid to access
5847 * fields or clean it up.
5849 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
5857 case USTCTL_NOTIFY_CMD_ENUM
:
5860 char name
[LTTNG_UST_SYM_NAME_LEN
];
5862 struct ustctl_enum_entry
*entries
;
5864 DBG2("UST app ustctl register enum received");
5866 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
5867 &entries
, &nr_entries
);
5869 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5870 DBG3("UST app recv enum failed. Application died: sock = %d",
5872 } else if (ret
== -EAGAIN
) {
5873 WARN("UST app recv enum failed. Communication time out: sock = %d",
5876 ERR("UST app recv enum failed with ret %d: sock = %d",
5882 /* Callee assumes ownership of entries */
5883 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
5884 entries
, nr_entries
);
5892 /* Should NEVER happen. */
5901 * Once the notify socket hangs up, this is called. First, it tries to find the
5902 * corresponding application. On failure, the call_rcu to close the socket is
5903 * executed. If an application is found, it tries to delete it from the notify
5904 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5906 * Note that an object needs to be allocated here so on ENOMEM failure, the
5907 * call RCU is not done but the rest of the cleanup is.
5909 void ust_app_notify_sock_unregister(int sock
)
5912 struct lttng_ht_iter iter
;
5913 struct ust_app
*app
;
5914 struct ust_app_notify_sock_obj
*obj
;
5920 obj
= zmalloc(sizeof(*obj
));
5923 * An ENOMEM is kind of uncool. If this strikes we continue the
5924 * procedure but the call_rcu will not be called. In this case, we
5925 * accept the fd leak rather than possibly creating an unsynchronized
5926 * state between threads.
5928 * TODO: The notify object should be created once the notify socket is
5929 * registered and stored independantely from the ust app object. The
5930 * tricky part is to synchronize the teardown of the application and
5931 * this notify object. Let's keep that in mind so we can avoid this
5932 * kind of shenanigans with ENOMEM in the teardown path.
5939 DBG("UST app notify socket unregister %d", sock
);
5942 * Lookup application by notify socket. If this fails, this means that the
5943 * hash table delete has already been done by the application
5944 * unregistration process so we can safely close the notify socket in a
5947 app
= find_app_by_notify_sock(sock
);
5952 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5955 * Whatever happens here either we fail or succeed, in both cases we have
5956 * to close the socket after a grace period to continue to the call RCU
5957 * here. If the deletion is successful, the application is not visible
5958 * anymore by other threads and is it fails it means that it was already
5959 * deleted from the hash table so either way we just have to close the
5962 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5968 * Close socket after a grace period to avoid for the socket to be reused
5969 * before the application object is freed creating potential race between
5970 * threads trying to add unique in the global hash table.
5973 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5978 * Destroy a ust app data structure and free its memory.
5980 void ust_app_destroy(struct ust_app
*app
)
5986 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5990 * Take a snapshot for a given UST session. The snapshot is sent to the given
5993 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5995 enum lttng_error_code
ust_app_snapshot_record(
5996 const struct ltt_ust_session
*usess
,
5997 const struct consumer_output
*output
, int wait
,
5998 uint64_t nb_packets_per_stream
)
6001 enum lttng_error_code status
= LTTNG_OK
;
6002 struct lttng_ht_iter iter
;
6003 struct ust_app
*app
;
6004 char *trace_path
= NULL
;
6011 switch (usess
->buffer_type
) {
6012 case LTTNG_BUFFER_PER_UID
:
6014 struct buffer_reg_uid
*reg
;
6016 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6017 struct buffer_reg_channel
*reg_chan
;
6018 struct consumer_socket
*socket
;
6019 char pathname
[PATH_MAX
];
6020 size_t consumer_path_offset
= 0;
6022 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6023 /* Skip since no metadata is present */
6027 /* Get consumer socket to use to push the metadata.*/
6028 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6031 status
= LTTNG_ERR_INVALID
;
6035 memset(pathname
, 0, sizeof(pathname
));
6036 ret
= snprintf(pathname
, sizeof(pathname
),
6037 DEFAULT_UST_TRACE_UID_PATH
,
6038 reg
->uid
, reg
->bits_per_long
);
6040 PERROR("snprintf snapshot path");
6041 status
= LTTNG_ERR_INVALID
;
6044 /* Free path allowed on previous iteration. */
6046 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6047 &consumer_path_offset
);
6049 status
= LTTNG_ERR_INVALID
;
6052 /* Add the UST default trace dir to path. */
6053 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6054 reg_chan
, node
.node
) {
6055 status
= consumer_snapshot_channel(socket
,
6056 reg_chan
->consumer_key
,
6057 output
, 0, usess
->uid
,
6058 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
6059 nb_packets_per_stream
);
6060 if (status
!= LTTNG_OK
) {
6064 status
= consumer_snapshot_channel(socket
,
6065 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6066 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6068 if (status
!= LTTNG_OK
) {
6074 case LTTNG_BUFFER_PER_PID
:
6076 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6077 struct consumer_socket
*socket
;
6078 struct lttng_ht_iter chan_iter
;
6079 struct ust_app_channel
*ua_chan
;
6080 struct ust_app_session
*ua_sess
;
6081 struct ust_registry_session
*registry
;
6082 char pathname
[PATH_MAX
];
6083 size_t consumer_path_offset
= 0;
6085 ua_sess
= lookup_session_by_app(usess
, app
);
6087 /* Session not associated with this app. */
6091 /* Get the right consumer socket for the application. */
6092 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6095 status
= LTTNG_ERR_INVALID
;
6099 /* Add the UST default trace dir to path. */
6100 memset(pathname
, 0, sizeof(pathname
));
6101 ret
= snprintf(pathname
, sizeof(pathname
), "%s",
6104 status
= LTTNG_ERR_INVALID
;
6105 PERROR("snprintf snapshot path");
6108 /* Free path allowed on previous iteration. */
6110 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6111 &consumer_path_offset
);
6113 status
= LTTNG_ERR_INVALID
;
6116 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6117 ua_chan
, node
.node
) {
6118 status
= consumer_snapshot_channel(socket
,
6119 ua_chan
->key
, output
, 0,
6120 ua_sess
->effective_credentials
6122 ua_sess
->effective_credentials
6124 &trace_path
[consumer_path_offset
], wait
,
6125 nb_packets_per_stream
);
6129 case LTTNG_ERR_CHAN_NOT_FOUND
:
6136 registry
= get_session_registry(ua_sess
);
6138 DBG("Application session is being torn down. Skip application.");
6141 status
= consumer_snapshot_channel(socket
,
6142 registry
->metadata_key
, output
, 1,
6143 ua_sess
->effective_credentials
.uid
,
6144 ua_sess
->effective_credentials
.gid
,
6145 &trace_path
[consumer_path_offset
], wait
, 0);
6149 case LTTNG_ERR_CHAN_NOT_FOUND
:
6169 * Return the size taken by one more packet per stream.
6171 uint64_t ust_app_get_size_one_more_packet_per_stream(
6172 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6174 uint64_t tot_size
= 0;
6175 struct ust_app
*app
;
6176 struct lttng_ht_iter iter
;
6180 switch (usess
->buffer_type
) {
6181 case LTTNG_BUFFER_PER_UID
:
6183 struct buffer_reg_uid
*reg
;
6185 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6186 struct buffer_reg_channel
*reg_chan
;
6189 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6190 reg_chan
, node
.node
) {
6191 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6193 * Don't take channel into account if we
6194 * already grab all its packets.
6198 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6204 case LTTNG_BUFFER_PER_PID
:
6207 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6208 struct ust_app_channel
*ua_chan
;
6209 struct ust_app_session
*ua_sess
;
6210 struct lttng_ht_iter chan_iter
;
6212 ua_sess
= lookup_session_by_app(usess
, app
);
6214 /* Session not associated with this app. */
6218 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6219 ua_chan
, node
.node
) {
6220 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6222 * Don't take channel into account if we
6223 * already grab all its packets.
6227 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6241 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6242 struct cds_list_head
*buffer_reg_uid_list
,
6243 struct consumer_output
*consumer
, uint64_t uchan_id
,
6244 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6247 uint64_t consumer_chan_key
;
6252 ret
= buffer_reg_uid_consumer_channel_key(
6253 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6261 ret
= consumer_get_lost_packets(ust_session_id
,
6262 consumer_chan_key
, consumer
, lost
);
6264 ret
= consumer_get_discarded_events(ust_session_id
,
6265 consumer_chan_key
, consumer
, discarded
);
6272 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6273 struct ltt_ust_channel
*uchan
,
6274 struct consumer_output
*consumer
, int overwrite
,
6275 uint64_t *discarded
, uint64_t *lost
)
6278 struct lttng_ht_iter iter
;
6279 struct lttng_ht_node_str
*ua_chan_node
;
6280 struct ust_app
*app
;
6281 struct ust_app_session
*ua_sess
;
6282 struct ust_app_channel
*ua_chan
;
6289 * Iterate over every registered applications. Sum counters for
6290 * all applications containing requested session and channel.
6292 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6293 struct lttng_ht_iter uiter
;
6295 ua_sess
= lookup_session_by_app(usess
, app
);
6296 if (ua_sess
== NULL
) {
6301 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6302 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6303 /* If the session is found for the app, the channel must be there */
6304 assert(ua_chan_node
);
6306 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6311 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6318 uint64_t _discarded
;
6320 ret
= consumer_get_discarded_events(usess
->id
,
6321 ua_chan
->key
, consumer
, &_discarded
);
6325 (*discarded
) += _discarded
;
6334 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6335 struct ust_app
*app
)
6338 struct ust_app_session
*ua_sess
;
6340 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6344 ua_sess
= lookup_session_by_app(usess
, app
);
6345 if (ua_sess
== NULL
) {
6346 /* The session is in teardown process. Ignore and continue. */
6350 pthread_mutex_lock(&ua_sess
->lock
);
6352 if (ua_sess
->deleted
) {
6356 pthread_mutex_lock(&app
->sock_lock
);
6357 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6358 pthread_mutex_unlock(&app
->sock_lock
);
6361 pthread_mutex_unlock(&ua_sess
->lock
);
6365 health_code_update();
6370 * Regenerate the statedump for each app in the session.
6372 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6375 struct lttng_ht_iter iter
;
6376 struct ust_app
*app
;
6378 DBG("Regenerating the metadata for all UST apps");
6382 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6383 if (!app
->compatible
) {
6387 ret
= ust_app_regenerate_statedump(usess
, app
);
6389 /* Continue to the next app even on error */
6400 * Rotate all the channels of a session.
6402 * Return LTTNG_OK on success or else an LTTng error code.
6404 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
6407 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6408 struct lttng_ht_iter iter
;
6409 struct ust_app
*app
;
6410 struct ltt_ust_session
*usess
= session
->ust_session
;
6416 switch (usess
->buffer_type
) {
6417 case LTTNG_BUFFER_PER_UID
:
6419 struct buffer_reg_uid
*reg
;
6421 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6422 struct buffer_reg_channel
*reg_chan
;
6423 struct consumer_socket
*socket
;
6425 /* Get consumer socket to use to push the metadata.*/
6426 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6429 cmd_ret
= LTTNG_ERR_INVALID
;
6433 /* Rotate the data channels. */
6434 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6435 reg_chan
, node
.node
) {
6436 ret
= consumer_rotate_channel(socket
,
6437 reg_chan
->consumer_key
,
6438 usess
->uid
, usess
->gid
,
6440 /* is_metadata_channel */ false);
6442 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6448 * The metadata channel might not be present.
6450 * Consumer stream allocation can be done
6451 * asynchronously and can fail on intermediary
6452 * operations (i.e add context) and lead to data
6453 * channels created with no metadata channel.
6455 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6456 /* Skip since no metadata is present. */
6460 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6462 ret
= consumer_rotate_channel(socket
,
6463 reg
->registry
->reg
.ust
->metadata_key
,
6464 usess
->uid
, usess
->gid
,
6466 /* is_metadata_channel */ true);
6468 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6474 case LTTNG_BUFFER_PER_PID
:
6476 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6477 struct consumer_socket
*socket
;
6478 struct lttng_ht_iter chan_iter
;
6479 struct ust_app_channel
*ua_chan
;
6480 struct ust_app_session
*ua_sess
;
6481 struct ust_registry_session
*registry
;
6483 ua_sess
= lookup_session_by_app(usess
, app
);
6485 /* Session not associated with this app. */
6489 /* Get the right consumer socket for the application. */
6490 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6493 cmd_ret
= LTTNG_ERR_INVALID
;
6497 registry
= get_session_registry(ua_sess
);
6499 DBG("Application session is being torn down. Skip application.");
6503 /* Rotate the data channels. */
6504 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6505 ua_chan
, node
.node
) {
6506 ret
= consumer_rotate_channel(socket
,
6508 ua_sess
->effective_credentials
6510 ua_sess
->effective_credentials
6513 /* is_metadata_channel */ false);
6515 /* Per-PID buffer and application going away. */
6516 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6518 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6523 /* Rotate the metadata channel. */
6524 (void) push_metadata(registry
, usess
->consumer
);
6525 ret
= consumer_rotate_channel(socket
,
6526 registry
->metadata_key
,
6527 ua_sess
->effective_credentials
.uid
,
6528 ua_sess
->effective_credentials
.gid
,
6530 /* is_metadata_channel */ true);
6532 /* Per-PID buffer and application going away. */
6533 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6535 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6553 enum lttng_error_code
ust_app_create_channel_subdirectories(
6554 const struct ltt_ust_session
*usess
)
6556 enum lttng_error_code ret
= LTTNG_OK
;
6557 struct lttng_ht_iter iter
;
6558 enum lttng_trace_chunk_status chunk_status
;
6559 char *pathname_index
;
6562 assert(usess
->current_trace_chunk
);
6565 switch (usess
->buffer_type
) {
6566 case LTTNG_BUFFER_PER_UID
:
6568 struct buffer_reg_uid
*reg
;
6570 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6571 fmt_ret
= asprintf(&pathname_index
,
6572 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
6573 reg
->uid
, reg
->bits_per_long
);
6575 ERR("Failed to format channel index directory");
6576 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6581 * Create the index subdirectory which will take care
6582 * of implicitly creating the channel's path.
6584 chunk_status
= lttng_trace_chunk_create_subdirectory(
6585 usess
->current_trace_chunk
,
6587 free(pathname_index
);
6588 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6589 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6595 case LTTNG_BUFFER_PER_PID
:
6597 struct ust_app
*app
;
6600 * Create the toplevel ust/ directory in case no apps are running.
6602 chunk_status
= lttng_trace_chunk_create_subdirectory(
6603 usess
->current_trace_chunk
,
6604 DEFAULT_UST_TRACE_DIR
);
6605 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6606 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6610 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
6612 struct ust_app_session
*ua_sess
;
6613 struct ust_registry_session
*registry
;
6615 ua_sess
= lookup_session_by_app(usess
, app
);
6617 /* Session not associated with this app. */
6621 registry
= get_session_registry(ua_sess
);
6623 DBG("Application session is being torn down. Skip application.");
6627 fmt_ret
= asprintf(&pathname_index
,
6628 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
6631 ERR("Failed to format channel index directory");
6632 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6636 * Create the index subdirectory which will take care
6637 * of implicitly creating the channel's path.
6639 chunk_status
= lttng_trace_chunk_create_subdirectory(
6640 usess
->current_trace_chunk
,
6642 free(pathname_index
);
6643 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6644 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6661 * Clear all the channels of a session.
6663 * Return LTTNG_OK on success or else an LTTng error code.
6665 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
6668 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6669 struct lttng_ht_iter iter
;
6670 struct ust_app
*app
;
6671 struct ltt_ust_session
*usess
= session
->ust_session
;
6677 if (usess
->active
) {
6678 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
6679 cmd_ret
= LTTNG_ERR_FATAL
;
6683 switch (usess
->buffer_type
) {
6684 case LTTNG_BUFFER_PER_UID
:
6686 struct buffer_reg_uid
*reg
;
6688 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6689 struct buffer_reg_channel
*reg_chan
;
6690 struct consumer_socket
*socket
;
6692 /* Get consumer socket to use to push the metadata.*/
6693 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6696 cmd_ret
= LTTNG_ERR_INVALID
;
6700 /* Clear the data channels. */
6701 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6702 reg_chan
, node
.node
) {
6703 ret
= consumer_clear_channel(socket
,
6704 reg_chan
->consumer_key
);
6710 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6713 * Clear the metadata channel.
6714 * Metadata channel is not cleared per se but we still need to
6715 * perform a rotation operation on it behind the scene.
6717 ret
= consumer_clear_channel(socket
,
6718 reg
->registry
->reg
.ust
->metadata_key
);
6725 case LTTNG_BUFFER_PER_PID
:
6727 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6728 struct consumer_socket
*socket
;
6729 struct lttng_ht_iter chan_iter
;
6730 struct ust_app_channel
*ua_chan
;
6731 struct ust_app_session
*ua_sess
;
6732 struct ust_registry_session
*registry
;
6734 ua_sess
= lookup_session_by_app(usess
, app
);
6736 /* Session not associated with this app. */
6740 /* Get the right consumer socket for the application. */
6741 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6744 cmd_ret
= LTTNG_ERR_INVALID
;
6748 registry
= get_session_registry(ua_sess
);
6750 DBG("Application session is being torn down. Skip application.");
6754 /* Clear the data channels. */
6755 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6756 ua_chan
, node
.node
) {
6757 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
6759 /* Per-PID buffer and application going away. */
6760 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6767 (void) push_metadata(registry
, usess
->consumer
);
6770 * Clear the metadata channel.
6771 * Metadata channel is not cleared per se but we still need to
6772 * perform rotation operation on it behind the scene.
6774 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
6776 /* Per-PID buffer and application going away. */
6777 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6795 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
6796 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
6799 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
6809 * This function skips the metadata channel as the begin/end timestamps of a
6810 * metadata packet are useless.
6812 * Moreover, opening a packet after a "clear" will cause problems for live
6813 * sessions as it will introduce padding that was not part of the first trace
6814 * chunk. The relay daemon expects the content of the metadata stream of
6815 * successive metadata trace chunks to be strict supersets of one another.
6817 * For example, flushing a packet at the beginning of the metadata stream of
6818 * a trace chunk resulting from a "clear" session command will cause the
6819 * size of the metadata stream of the new trace chunk to not match the size of
6820 * the metadata stream of the original chunk. This will confuse the relay
6821 * daemon as the same "offset" in a metadata stream will no longer point
6822 * to the same content.
6824 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
6826 enum lttng_error_code ret
= LTTNG_OK
;
6827 struct lttng_ht_iter iter
;
6828 struct ltt_ust_session
*usess
= session
->ust_session
;
6834 switch (usess
->buffer_type
) {
6835 case LTTNG_BUFFER_PER_UID
:
6837 struct buffer_reg_uid
*reg
;
6839 cds_list_for_each_entry (
6840 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6841 struct buffer_reg_channel
*reg_chan
;
6842 struct consumer_socket
*socket
;
6844 socket
= consumer_find_socket_by_bitness(
6845 reg
->bits_per_long
, usess
->consumer
);
6847 ret
= LTTNG_ERR_FATAL
;
6851 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
6852 &iter
.iter
, reg_chan
, node
.node
) {
6853 const int open_ret
=
6854 consumer_open_channel_packets(
6856 reg_chan
->consumer_key
);
6859 ret
= LTTNG_ERR_UNK
;
6866 case LTTNG_BUFFER_PER_PID
:
6868 struct ust_app
*app
;
6870 cds_lfht_for_each_entry (
6871 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6872 struct consumer_socket
*socket
;
6873 struct lttng_ht_iter chan_iter
;
6874 struct ust_app_channel
*ua_chan
;
6875 struct ust_app_session
*ua_sess
;
6876 struct ust_registry_session
*registry
;
6878 ua_sess
= lookup_session_by_app(usess
, app
);
6880 /* Session not associated with this app. */
6884 /* Get the right consumer socket for the application. */
6885 socket
= consumer_find_socket_by_bitness(
6886 app
->bits_per_long
, usess
->consumer
);
6888 ret
= LTTNG_ERR_FATAL
;
6892 registry
= get_session_registry(ua_sess
);
6894 DBG("Application session is being torn down. Skip application.");
6898 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
6899 &chan_iter
.iter
, ua_chan
, node
.node
) {
6900 const int open_ret
=
6901 consumer_open_channel_packets(
6907 * Per-PID buffer and application going
6910 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6914 ret
= LTTNG_ERR_UNK
;