2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
29 #include <urcu/compiler.h>
32 #include <common/common.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
35 #include "buffer-registry.h"
37 #include "health-sessiond.h"
39 #include "ust-consumer.h"
40 #include "lttng-ust-ctl.h"
41 #include "lttng-ust-error.h"
44 #include "lttng-sessiond.h"
45 #include "notification-thread-commands.h"
48 struct lttng_ht
*ust_app_ht
;
49 struct lttng_ht
*ust_app_ht_by_sock
;
50 struct lttng_ht
*ust_app_ht_by_notify_sock
;
53 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
55 /* Next available channel key. Access under next_channel_key_lock. */
56 static uint64_t _next_channel_key
;
57 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
59 /* Next available session ID. Access under next_session_id_lock. */
60 static uint64_t _next_session_id
;
61 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
64 * Return the incremented value of next_channel_key.
66 static uint64_t get_next_channel_key(void)
70 pthread_mutex_lock(&next_channel_key_lock
);
71 ret
= ++_next_channel_key
;
72 pthread_mutex_unlock(&next_channel_key_lock
);
77 * Return the atomically incremented value of next_session_id.
79 static uint64_t get_next_session_id(void)
83 pthread_mutex_lock(&next_session_id_lock
);
84 ret
= ++_next_session_id
;
85 pthread_mutex_unlock(&next_session_id_lock
);
89 static void copy_channel_attr_to_ustctl(
90 struct ustctl_consumer_channel_attr
*attr
,
91 struct lttng_ust_channel_attr
*uattr
)
93 /* Copy event attributes since the layout is different. */
94 attr
->subbuf_size
= uattr
->subbuf_size
;
95 attr
->num_subbuf
= uattr
->num_subbuf
;
96 attr
->overwrite
= uattr
->overwrite
;
97 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
98 attr
->read_timer_interval
= uattr
->read_timer_interval
;
99 attr
->output
= uattr
->output
;
100 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
104 * Match function for the hash table lookup.
106 * It matches an ust app event based on three attributes which are the event
107 * name, the filter bytecode and the loglevel.
109 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
111 struct ust_app_event
*event
;
112 const struct ust_app_ht_key
*key
;
113 int ev_loglevel_value
;
118 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
120 ev_loglevel_value
= event
->attr
.loglevel
;
122 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
125 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
129 /* Event loglevel. */
130 if (ev_loglevel_value
!= key
->loglevel_type
) {
131 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
132 && key
->loglevel_type
== 0 &&
133 ev_loglevel_value
== -1) {
135 * Match is accepted. This is because on event creation, the
136 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
137 * -1 are accepted for this loglevel type since 0 is the one set by
138 * the API when receiving an enable event.
145 /* One of the filters is NULL, fail. */
146 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
150 if (key
->filter
&& event
->filter
) {
151 /* Both filters exists, check length followed by the bytecode. */
152 if (event
->filter
->len
!= key
->filter
->len
||
153 memcmp(event
->filter
->data
, key
->filter
->data
,
154 event
->filter
->len
) != 0) {
159 /* One of the exclusions is NULL, fail. */
160 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
164 if (key
->exclusion
&& event
->exclusion
) {
165 /* Both exclusions exists, check count followed by the names. */
166 if (event
->exclusion
->count
!= key
->exclusion
->count
||
167 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
168 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
182 * Unique add of an ust app event in the given ht. This uses the custom
183 * ht_match_ust_app_event match function and the event name as hash.
185 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
186 struct ust_app_event
*event
)
188 struct cds_lfht_node
*node_ptr
;
189 struct ust_app_ht_key key
;
193 assert(ua_chan
->events
);
196 ht
= ua_chan
->events
;
197 key
.name
= event
->attr
.name
;
198 key
.filter
= event
->filter
;
199 key
.loglevel_type
= event
->attr
.loglevel
;
200 key
.exclusion
= event
->exclusion
;
202 node_ptr
= cds_lfht_add_unique(ht
->ht
,
203 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
204 ht_match_ust_app_event
, &key
, &event
->node
.node
);
205 assert(node_ptr
== &event
->node
.node
);
209 * Close the notify socket from the given RCU head object. This MUST be called
210 * through a call_rcu().
212 static void close_notify_sock_rcu(struct rcu_head
*head
)
215 struct ust_app_notify_sock_obj
*obj
=
216 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
218 /* Must have a valid fd here. */
219 assert(obj
->fd
>= 0);
221 ret
= close(obj
->fd
);
223 ERR("close notify sock %d RCU", obj
->fd
);
225 lttng_fd_put(LTTNG_FD_APPS
, 1);
231 * Return the session registry according to the buffer type of the given
234 * A registry per UID object MUST exists before calling this function or else
235 * it assert() if not found. RCU read side lock must be acquired.
237 static struct ust_registry_session
*get_session_registry(
238 struct ust_app_session
*ua_sess
)
240 struct ust_registry_session
*registry
= NULL
;
244 switch (ua_sess
->buffer_type
) {
245 case LTTNG_BUFFER_PER_PID
:
247 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
251 registry
= reg_pid
->registry
->reg
.ust
;
254 case LTTNG_BUFFER_PER_UID
:
256 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
257 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
258 ua_sess
->real_credentials
.uid
);
262 registry
= reg_uid
->registry
->reg
.ust
;
274 * Delete ust context safely. RCU read lock must be held before calling
278 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
286 pthread_mutex_lock(&app
->sock_lock
);
287 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
288 pthread_mutex_unlock(&app
->sock_lock
);
290 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
291 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
292 app
->pid
, app
->sock
);
293 } else if (ret
== -EAGAIN
) {
294 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
295 app
->pid
, app
->sock
);
297 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
298 ua_ctx
->obj
->handle
, ret
,
299 app
->pid
, app
->sock
);
308 * Delete ust app event safely. RCU read lock must be held before calling
312 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
319 free(ua_event
->filter
);
320 if (ua_event
->exclusion
!= NULL
)
321 free(ua_event
->exclusion
);
322 if (ua_event
->obj
!= NULL
) {
323 pthread_mutex_lock(&app
->sock_lock
);
324 ret
= ustctl_release_object(sock
, ua_event
->obj
);
325 pthread_mutex_unlock(&app
->sock_lock
);
327 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
328 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
329 app
->pid
, app
->sock
);
330 } else if (ret
== -EAGAIN
) {
331 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
332 app
->pid
, app
->sock
);
334 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
335 ret
, app
->pid
, app
->sock
);
344 * Release ust data object of the given stream.
346 * Return 0 on success or else a negative value.
348 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
356 pthread_mutex_lock(&app
->sock_lock
);
357 ret
= ustctl_release_object(sock
, stream
->obj
);
358 pthread_mutex_unlock(&app
->sock_lock
);
360 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
361 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
362 app
->pid
, app
->sock
);
363 } else if (ret
== -EAGAIN
) {
364 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
365 app
->pid
, app
->sock
);
367 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
368 ret
, app
->pid
, app
->sock
);
371 lttng_fd_put(LTTNG_FD_APPS
, 2);
379 * Delete ust app stream safely. RCU read lock must be held before calling
383 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
388 (void) release_ust_app_stream(sock
, stream
, app
);
393 * We need to execute ht_destroy outside of RCU read-side critical
394 * section and outside of call_rcu thread, so we postpone its execution
395 * using ht_cleanup_push. It is simpler than to change the semantic of
396 * the many callers of delete_ust_app_session().
399 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
401 struct ust_app_channel
*ua_chan
=
402 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
404 ht_cleanup_push(ua_chan
->ctx
);
405 ht_cleanup_push(ua_chan
->events
);
410 * Extract the lost packet or discarded events counter when the channel is
411 * being deleted and store the value in the parent channel so we can
412 * access it from lttng list and at stop/destroy.
414 * The session list lock must be held by the caller.
417 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
419 uint64_t discarded
= 0, lost
= 0;
420 struct ltt_session
*session
;
421 struct ltt_ust_channel
*uchan
;
423 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
428 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
429 if (!session
|| !session
->ust_session
) {
431 * Not finding the session is not an error because there are
432 * multiple ways the channels can be torn down.
434 * 1) The session daemon can initiate the destruction of the
435 * ust app session after receiving a destroy command or
436 * during its shutdown/teardown.
437 * 2) The application, since we are in per-pid tracing, is
438 * unregistering and tearing down its ust app session.
440 * Both paths are protected by the session list lock which
441 * ensures that the accounting of lost packets and discarded
442 * events is done exactly once. The session is then unpublished
443 * from the session list, resulting in this condition.
448 if (ua_chan
->attr
.overwrite
) {
449 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
450 ua_chan
->key
, session
->ust_session
->consumer
,
453 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
454 ua_chan
->key
, session
->ust_session
->consumer
,
457 uchan
= trace_ust_find_channel_by_name(
458 session
->ust_session
->domain_global
.channels
,
461 ERR("Missing UST channel to store discarded counters");
465 uchan
->per_pid_closed_app_discarded
+= discarded
;
466 uchan
->per_pid_closed_app_lost
+= lost
;
471 session_put(session
);
476 * Delete ust app channel safely. RCU read lock must be held before calling
479 * The session list lock must be held by the caller.
482 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
486 struct lttng_ht_iter iter
;
487 struct ust_app_event
*ua_event
;
488 struct ust_app_ctx
*ua_ctx
;
489 struct ust_app_stream
*stream
, *stmp
;
490 struct ust_registry_session
*registry
;
494 DBG3("UST app deleting channel %s", ua_chan
->name
);
497 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
498 cds_list_del(&stream
->list
);
499 delete_ust_app_stream(sock
, stream
, app
);
503 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
504 cds_list_del(&ua_ctx
->list
);
505 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
507 delete_ust_app_ctx(sock
, ua_ctx
, app
);
511 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
513 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
515 delete_ust_app_event(sock
, ua_event
, app
);
518 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
519 /* Wipe and free registry from session registry. */
520 registry
= get_session_registry(ua_chan
->session
);
522 ust_registry_channel_del_free(registry
, ua_chan
->key
,
526 * A negative socket can be used by the caller when
527 * cleaning-up a ua_chan in an error path. Skip the
528 * accounting in this case.
531 save_per_pid_lost_discarded_counters(ua_chan
);
535 if (ua_chan
->obj
!= NULL
) {
536 /* Remove channel from application UST object descriptor. */
537 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
538 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
540 pthread_mutex_lock(&app
->sock_lock
);
541 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
542 pthread_mutex_unlock(&app
->sock_lock
);
544 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
545 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
546 ua_chan
->name
, app
->pid
,
548 } else if (ret
== -EAGAIN
) {
549 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
550 ua_chan
->name
, app
->pid
,
553 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
554 ua_chan
->name
, ret
, app
->pid
,
558 lttng_fd_put(LTTNG_FD_APPS
, 1);
561 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
564 int ust_app_register_done(struct ust_app
*app
)
568 pthread_mutex_lock(&app
->sock_lock
);
569 ret
= ustctl_register_done(app
->sock
);
570 pthread_mutex_unlock(&app
->sock_lock
);
574 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
579 pthread_mutex_lock(&app
->sock_lock
);
584 ret
= ustctl_release_object(sock
, data
);
586 pthread_mutex_unlock(&app
->sock_lock
);
592 * Push metadata to consumer socket.
594 * RCU read-side lock must be held to guarantee existance of socket.
595 * Must be called with the ust app session lock held.
596 * Must be called with the registry lock held.
598 * On success, return the len of metadata pushed or else a negative value.
599 * Returning a -EPIPE return value means we could not send the metadata,
600 * but it can be caused by recoverable errors (e.g. the application has
601 * terminated concurrently).
603 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
604 struct consumer_socket
*socket
, int send_zero_data
)
607 char *metadata_str
= NULL
;
608 size_t len
, offset
, new_metadata_len_sent
;
610 uint64_t metadata_key
, metadata_version
;
615 metadata_key
= registry
->metadata_key
;
618 * Means that no metadata was assigned to the session. This can
619 * happens if no start has been done previously.
625 offset
= registry
->metadata_len_sent
;
626 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
627 new_metadata_len_sent
= registry
->metadata_len
;
628 metadata_version
= registry
->metadata_version
;
630 DBG3("No metadata to push for metadata key %" PRIu64
,
631 registry
->metadata_key
);
633 if (send_zero_data
) {
634 DBG("No metadata to push");
640 /* Allocate only what we have to send. */
641 metadata_str
= zmalloc(len
);
643 PERROR("zmalloc ust app metadata string");
647 /* Copy what we haven't sent out. */
648 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
651 pthread_mutex_unlock(®istry
->lock
);
653 * We need to unlock the registry while we push metadata to
654 * break a circular dependency between the consumerd metadata
655 * lock and the sessiond registry lock. Indeed, pushing metadata
656 * to the consumerd awaits that it gets pushed all the way to
657 * relayd, but doing so requires grabbing the metadata lock. If
658 * a concurrent metadata request is being performed by
659 * consumerd, this can try to grab the registry lock on the
660 * sessiond while holding the metadata lock on the consumer
661 * daemon. Those push and pull schemes are performed on two
662 * different bidirectionnal communication sockets.
664 ret
= consumer_push_metadata(socket
, metadata_key
,
665 metadata_str
, len
, offset
, metadata_version
);
666 pthread_mutex_lock(®istry
->lock
);
669 * There is an acceptable race here between the registry
670 * metadata key assignment and the creation on the
671 * consumer. The session daemon can concurrently push
672 * metadata for this registry while being created on the
673 * consumer since the metadata key of the registry is
674 * assigned *before* it is setup to avoid the consumer
675 * to ask for metadata that could possibly be not found
676 * in the session daemon.
678 * The metadata will get pushed either by the session
679 * being stopped or the consumer requesting metadata if
680 * that race is triggered.
682 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
685 ERR("Error pushing metadata to consumer");
691 * Metadata may have been concurrently pushed, since
692 * we're not holding the registry lock while pushing to
693 * consumer. This is handled by the fact that we send
694 * the metadata content, size, and the offset at which
695 * that metadata belongs. This may arrive out of order
696 * on the consumer side, and the consumer is able to
697 * deal with overlapping fragments. The consumer
698 * supports overlapping fragments, which must be
699 * contiguous starting from offset 0. We keep the
700 * largest metadata_len_sent value of the concurrent
703 registry
->metadata_len_sent
=
704 max_t(size_t, registry
->metadata_len_sent
,
705 new_metadata_len_sent
);
714 * On error, flag the registry that the metadata is
715 * closed. We were unable to push anything and this
716 * means that either the consumer is not responding or
717 * the metadata cache has been destroyed on the
720 registry
->metadata_closed
= 1;
728 * For a given application and session, push metadata to consumer.
729 * Either sock or consumer is required : if sock is NULL, the default
730 * socket to send the metadata is retrieved from consumer, if sock
731 * is not NULL we use it to send the metadata.
732 * RCU read-side lock must be held while calling this function,
733 * therefore ensuring existance of registry. It also ensures existance
734 * of socket throughout this function.
736 * Return 0 on success else a negative error.
737 * Returning a -EPIPE return value means we could not send the metadata,
738 * but it can be caused by recoverable errors (e.g. the application has
739 * terminated concurrently).
741 static int push_metadata(struct ust_registry_session
*registry
,
742 struct consumer_output
*consumer
)
746 struct consumer_socket
*socket
;
751 pthread_mutex_lock(®istry
->lock
);
752 if (registry
->metadata_closed
) {
757 /* Get consumer socket to use to push the metadata.*/
758 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
765 ret
= ust_app_push_metadata(registry
, socket
, 0);
770 pthread_mutex_unlock(®istry
->lock
);
774 pthread_mutex_unlock(®istry
->lock
);
779 * Send to the consumer a close metadata command for the given session. Once
780 * done, the metadata channel is deleted and the session metadata pointer is
781 * nullified. The session lock MUST be held unless the application is
782 * in the destroy path.
784 * Do not hold the registry lock while communicating with the consumerd, because
785 * doing so causes inter-process deadlocks between consumerd and sessiond with
786 * the metadata request notification.
788 * Return 0 on success else a negative value.
790 static int close_metadata(struct ust_registry_session
*registry
,
791 struct consumer_output
*consumer
)
794 struct consumer_socket
*socket
;
795 uint64_t metadata_key
;
796 bool registry_was_already_closed
;
803 pthread_mutex_lock(®istry
->lock
);
804 metadata_key
= registry
->metadata_key
;
805 registry_was_already_closed
= registry
->metadata_closed
;
806 if (metadata_key
!= 0) {
808 * Metadata closed. Even on error this means that the consumer
809 * is not responding or not found so either way a second close
810 * should NOT be emit for this registry.
812 registry
->metadata_closed
= 1;
814 pthread_mutex_unlock(®istry
->lock
);
816 if (metadata_key
== 0 || registry_was_already_closed
) {
821 /* Get consumer socket to use to push the metadata.*/
822 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
829 ret
= consumer_close_metadata(socket
, metadata_key
);
840 * We need to execute ht_destroy outside of RCU read-side critical
841 * section and outside of call_rcu thread, so we postpone its execution
842 * using ht_cleanup_push. It is simpler than to change the semantic of
843 * the many callers of delete_ust_app_session().
846 void delete_ust_app_session_rcu(struct rcu_head
*head
)
848 struct ust_app_session
*ua_sess
=
849 caa_container_of(head
, struct ust_app_session
, rcu_head
);
851 ht_cleanup_push(ua_sess
->channels
);
856 * Delete ust app session safely. RCU read lock must be held before calling
859 * The session list lock must be held by the caller.
862 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
866 struct lttng_ht_iter iter
;
867 struct ust_app_channel
*ua_chan
;
868 struct ust_registry_session
*registry
;
872 pthread_mutex_lock(&ua_sess
->lock
);
874 assert(!ua_sess
->deleted
);
875 ua_sess
->deleted
= true;
877 registry
= get_session_registry(ua_sess
);
878 /* Registry can be null on error path during initialization. */
880 /* Push metadata for application before freeing the application. */
881 (void) push_metadata(registry
, ua_sess
->consumer
);
884 * Don't ask to close metadata for global per UID buffers. Close
885 * metadata only on destroy trace session in this case. Also, the
886 * previous push metadata could have flag the metadata registry to
887 * close so don't send a close command if closed.
889 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
890 /* And ask to close it for this session registry. */
891 (void) close_metadata(registry
, ua_sess
->consumer
);
895 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
897 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
899 delete_ust_app_channel(sock
, ua_chan
, app
);
902 /* In case of per PID, the registry is kept in the session. */
903 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
904 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
907 * Registry can be null on error path during
910 buffer_reg_pid_remove(reg_pid
);
911 buffer_reg_pid_destroy(reg_pid
);
915 if (ua_sess
->handle
!= -1) {
916 pthread_mutex_lock(&app
->sock_lock
);
917 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
918 pthread_mutex_unlock(&app
->sock_lock
);
920 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
921 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
922 app
->pid
, app
->sock
);
923 } else if (ret
== -EAGAIN
) {
924 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
925 app
->pid
, app
->sock
);
927 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
928 ret
, app
->pid
, app
->sock
);
932 /* Remove session from application UST object descriptor. */
933 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
934 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
938 pthread_mutex_unlock(&ua_sess
->lock
);
940 consumer_output_put(ua_sess
->consumer
);
942 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
946 * Delete a traceable application structure from the global list. Never call
947 * this function outside of a call_rcu call.
949 * RCU read side lock should _NOT_ be held when calling this function.
952 void delete_ust_app(struct ust_app
*app
)
955 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
958 * The session list lock must be held during this function to guarantee
959 * the existence of ua_sess.
962 /* Delete ust app sessions info */
967 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
969 /* Free every object in the session and the session. */
971 delete_ust_app_session(sock
, ua_sess
, app
);
975 ht_cleanup_push(app
->sessions
);
976 ht_cleanup_push(app
->ust_sessions_objd
);
977 ht_cleanup_push(app
->ust_objd
);
980 * Wait until we have deleted the application from the sock hash table
981 * before closing this socket, otherwise an application could re-use the
982 * socket ID and race with the teardown, using the same hash table entry.
984 * It's OK to leave the close in call_rcu. We want it to stay unique for
985 * all RCU readers that could run concurrently with unregister app,
986 * therefore we _need_ to only close that socket after a grace period. So
987 * it should stay in this RCU callback.
989 * This close() is a very important step of the synchronization model so
990 * every modification to this function must be carefully reviewed.
996 lttng_fd_put(LTTNG_FD_APPS
, 1);
998 DBG2("UST app pid %d deleted", app
->pid
);
1000 session_unlock_list();
1004 * URCU intermediate call to delete an UST app.
1007 void delete_ust_app_rcu(struct rcu_head
*head
)
1009 struct lttng_ht_node_ulong
*node
=
1010 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1011 struct ust_app
*app
=
1012 caa_container_of(node
, struct ust_app
, pid_n
);
1014 DBG3("Call RCU deleting app PID %d", app
->pid
);
1015 delete_ust_app(app
);
1019 * Delete the session from the application ht and delete the data structure by
1020 * freeing every object inside and releasing them.
1022 * The session list lock must be held by the caller.
1024 static void destroy_app_session(struct ust_app
*app
,
1025 struct ust_app_session
*ua_sess
)
1028 struct lttng_ht_iter iter
;
1033 iter
.iter
.node
= &ua_sess
->node
.node
;
1034 ret
= lttng_ht_del(app
->sessions
, &iter
);
1036 /* Already scheduled for teardown. */
1040 /* Once deleted, free the data structure. */
1041 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1048 * Alloc new UST app session.
1051 struct ust_app_session
*alloc_ust_app_session(void)
1053 struct ust_app_session
*ua_sess
;
1055 /* Init most of the default value by allocating and zeroing */
1056 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1057 if (ua_sess
== NULL
) {
1062 ua_sess
->handle
= -1;
1063 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1064 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1065 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1074 * Alloc new UST app channel.
1077 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
1078 struct ust_app_session
*ua_sess
,
1079 struct lttng_ust_channel_attr
*attr
)
1081 struct ust_app_channel
*ua_chan
;
1083 /* Init most of the default value by allocating and zeroing */
1084 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1085 if (ua_chan
== NULL
) {
1090 /* Setup channel name */
1091 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1092 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1094 ua_chan
->enabled
= 1;
1095 ua_chan
->handle
= -1;
1096 ua_chan
->session
= ua_sess
;
1097 ua_chan
->key
= get_next_channel_key();
1098 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1099 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1100 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1102 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1103 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1105 /* Copy attributes */
1107 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1108 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1109 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1110 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1111 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1112 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1113 ua_chan
->attr
.output
= attr
->output
;
1114 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1116 /* By default, the channel is a per cpu channel. */
1117 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1119 DBG3("UST app channel %s allocated", ua_chan
->name
);
1128 * Allocate and initialize a UST app stream.
1130 * Return newly allocated stream pointer or NULL on error.
1132 struct ust_app_stream
*ust_app_alloc_stream(void)
1134 struct ust_app_stream
*stream
= NULL
;
1136 stream
= zmalloc(sizeof(*stream
));
1137 if (stream
== NULL
) {
1138 PERROR("zmalloc ust app stream");
1142 /* Zero could be a valid value for a handle so flag it to -1. */
1143 stream
->handle
= -1;
1150 * Alloc new UST app event.
1153 struct ust_app_event
*alloc_ust_app_event(char *name
,
1154 struct lttng_ust_event
*attr
)
1156 struct ust_app_event
*ua_event
;
1158 /* Init most of the default value by allocating and zeroing */
1159 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1160 if (ua_event
== NULL
) {
1161 PERROR("Failed to allocate ust_app_event structure");
1165 ua_event
->enabled
= 1;
1166 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1167 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1168 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1170 /* Copy attributes */
1172 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1175 DBG3("UST app event %s allocated", ua_event
->name
);
1184 * Alloc new UST app context.
1187 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1189 struct ust_app_ctx
*ua_ctx
;
1191 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1192 if (ua_ctx
== NULL
) {
1196 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1199 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1200 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1201 char *provider_name
= NULL
, *ctx_name
= NULL
;
1203 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1204 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1205 if (!provider_name
|| !ctx_name
) {
1206 free(provider_name
);
1211 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1212 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1216 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1224 * Allocate a filter and copy the given original filter.
1226 * Return allocated filter or NULL on error.
1228 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1229 struct lttng_filter_bytecode
*orig_f
)
1231 struct lttng_filter_bytecode
*filter
= NULL
;
1233 /* Copy filter bytecode */
1234 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1236 PERROR("zmalloc alloc filter bytecode");
1240 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1247 * Create a liblttng-ust filter bytecode from given bytecode.
1249 * Return allocated filter or NULL on error.
1251 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1252 struct lttng_filter_bytecode
*orig_f
)
1254 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1256 /* Copy filter bytecode */
1257 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1259 PERROR("zmalloc alloc ust filter bytecode");
1263 assert(sizeof(struct lttng_filter_bytecode
) ==
1264 sizeof(struct lttng_ust_filter_bytecode
));
1265 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1271 * Find an ust_app using the sock and return it. RCU read side lock must be
1272 * held before calling this helper function.
1274 struct ust_app
*ust_app_find_by_sock(int sock
)
1276 struct lttng_ht_node_ulong
*node
;
1277 struct lttng_ht_iter iter
;
1279 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1280 node
= lttng_ht_iter_get_node_ulong(&iter
);
1282 DBG2("UST app find by sock %d not found", sock
);
1286 return caa_container_of(node
, struct ust_app
, sock_n
);
1293 * Find an ust_app using the notify sock and return it. RCU read side lock must
1294 * be held before calling this helper function.
1296 static struct ust_app
*find_app_by_notify_sock(int sock
)
1298 struct lttng_ht_node_ulong
*node
;
1299 struct lttng_ht_iter iter
;
1301 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1303 node
= lttng_ht_iter_get_node_ulong(&iter
);
1305 DBG2("UST app find by notify sock %d not found", sock
);
1309 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1316 * Lookup for an ust app event based on event name, filter bytecode and the
1319 * Return an ust_app_event object or NULL on error.
1321 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1322 const char *name
, const struct lttng_filter_bytecode
*filter
,
1324 const struct lttng_event_exclusion
*exclusion
)
1326 struct lttng_ht_iter iter
;
1327 struct lttng_ht_node_str
*node
;
1328 struct ust_app_event
*event
= NULL
;
1329 struct ust_app_ht_key key
;
1334 /* Setup key for event lookup. */
1336 key
.filter
= filter
;
1337 key
.loglevel_type
= loglevel_value
;
1338 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1339 key
.exclusion
= exclusion
;
1341 /* Lookup using the event name as hash and a custom match fct. */
1342 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1343 ht_match_ust_app_event
, &key
, &iter
.iter
);
1344 node
= lttng_ht_iter_get_node_str(&iter
);
1349 event
= caa_container_of(node
, struct ust_app_event
, node
);
1356 * Create the channel context on the tracer.
1358 * Called with UST app session lock held.
1361 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1362 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1366 health_code_update();
1368 pthread_mutex_lock(&app
->sock_lock
);
1369 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1370 ua_chan
->obj
, &ua_ctx
->obj
);
1371 pthread_mutex_unlock(&app
->sock_lock
);
1373 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1375 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1376 app
->pid
, app
->sock
);
1377 } else if (ret
== -EAGAIN
) {
1379 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1380 app
->pid
, app
->sock
);
1382 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1383 ret
, app
->pid
, app
->sock
);
1388 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1390 DBG2("UST app context handle %d created successfully for channel %s",
1391 ua_ctx
->handle
, ua_chan
->name
);
1394 health_code_update();
1399 * Set the filter on the tracer.
1402 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1403 struct ust_app
*app
)
1406 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1408 health_code_update();
1410 if (!ua_event
->filter
) {
1415 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1416 if (!ust_bytecode
) {
1417 ret
= -LTTNG_ERR_NOMEM
;
1420 pthread_mutex_lock(&app
->sock_lock
);
1421 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1423 pthread_mutex_unlock(&app
->sock_lock
);
1425 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1427 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1428 app
->pid
, app
->sock
);
1429 } else if (ret
== -EAGAIN
) {
1431 DBG3("UST app set filter failed. Communication timeout: pid = %d, sock = %d",
1432 app
->pid
, app
->sock
);
1434 ERR("UST app event set filter failed with ret %d: pid = %d, sock = %d",
1441 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1444 health_code_update();
1450 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1451 struct lttng_event_exclusion
*exclusion
)
1453 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1454 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1455 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1457 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1458 if (!ust_exclusion
) {
1463 assert(sizeof(struct lttng_event_exclusion
) ==
1464 sizeof(struct lttng_ust_event_exclusion
));
1465 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1467 return ust_exclusion
;
1471 * Set event exclusions on the tracer.
1474 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1475 struct ust_app
*app
)
1478 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1480 health_code_update();
1482 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1487 ust_exclusion
= create_ust_exclusion_from_exclusion(
1488 ua_event
->exclusion
);
1489 if (!ust_exclusion
) {
1490 ret
= -LTTNG_ERR_NOMEM
;
1493 pthread_mutex_lock(&app
->sock_lock
);
1494 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1495 pthread_mutex_unlock(&app
->sock_lock
);
1497 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1499 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1500 app
->pid
, app
->sock
);
1501 } else if (ret
== -EAGAIN
) {
1503 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1504 app
->pid
, app
->sock
);
1506 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, event = %s",
1507 ret
, app
->pid
, app
->sock
, ua_event
->name
);
1512 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1515 health_code_update();
1516 free(ust_exclusion
);
1521 * Disable the specified event on to UST tracer for the UST session.
1523 static int disable_ust_event(struct ust_app
*app
,
1524 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1528 health_code_update();
1530 pthread_mutex_lock(&app
->sock_lock
);
1531 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1532 pthread_mutex_unlock(&app
->sock_lock
);
1534 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1536 DBG3("UST app disable event failed. Application is dead: pid = %d, sock = %d",
1537 app
->pid
, app
->sock
);
1538 } else if (ret
== -EAGAIN
) {
1540 WARN("UST app disable event failed. Communication time out: pid = %d, sock = %d",
1541 app
->pid
, app
->sock
);
1543 ERR("UST app disable event failed with ret %d: pid = %d, sock = %d , name = %s",
1544 ret
, app
->pid
, app
->sock
, ua_event
->attr
.name
);
1549 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1550 ua_event
->attr
.name
, app
->pid
);
1553 health_code_update();
1558 * Disable the specified channel on to UST tracer for the UST session.
1560 static int disable_ust_channel(struct ust_app
*app
,
1561 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1565 health_code_update();
1567 pthread_mutex_lock(&app
->sock_lock
);
1568 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1569 pthread_mutex_unlock(&app
->sock_lock
);
1571 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1573 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1574 app
->pid
, app
->sock
);
1575 } else if (ret
== -EAGAIN
) {
1577 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1578 app
->pid
, app
->sock
);
1580 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1581 ua_chan
->name
, ua_sess
->handle
, ret
,
1582 app
->pid
, app
->sock
);
1587 DBG2("UST app channel %s disabled successfully for app: pid = %d",
1588 ua_chan
->name
, app
->pid
);
1591 health_code_update();
1596 * Enable the specified channel on to UST tracer for the UST session.
1598 static int enable_ust_channel(struct ust_app
*app
,
1599 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1603 health_code_update();
1605 pthread_mutex_lock(&app
->sock_lock
);
1606 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1607 pthread_mutex_unlock(&app
->sock_lock
);
1609 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1611 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1612 ua_chan
->name
, app
->pid
, app
->sock
);
1613 } else if (ret
== -EAGAIN
) {
1615 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1616 ua_chan
->name
, app
->pid
, app
->sock
);
1618 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1619 ua_chan
->name
, ua_sess
->handle
, ret
,
1620 app
->pid
, app
->sock
);
1625 ua_chan
->enabled
= 1;
1627 DBG2("UST app channel %s enabled successfully for app: pid = %d",
1628 ua_chan
->name
, app
->pid
);
1631 health_code_update();
1636 * Enable the specified event on to UST tracer for the UST session.
1638 static int enable_ust_event(struct ust_app
*app
,
1639 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1643 health_code_update();
1645 pthread_mutex_lock(&app
->sock_lock
);
1646 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1647 pthread_mutex_unlock(&app
->sock_lock
);
1649 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1651 DBG3("UST app enable event failed. Application is dead: pid = %d, sock = %d",
1652 app
->pid
, app
->sock
);
1653 } else if (ret
== -EAGAIN
) {
1655 WARN("UST app enable event failed. Communication time out: pid = %d, sock = %d",
1656 app
->pid
, app
->sock
);
1658 ERR("UST app enable event failed with ret %d: pid = %d, sock = %d, event = %s",
1659 ret
, app
->pid
, app
->sock
, ua_event
->attr
.name
);
1664 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1665 ua_event
->attr
.name
, app
->pid
);
1668 health_code_update();
1673 * Send channel and stream buffer to application.
1675 * Return 0 on success. On error, a negative value is returned.
1677 static int send_channel_pid_to_ust(struct ust_app
*app
,
1678 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1681 struct ust_app_stream
*stream
, *stmp
;
1687 health_code_update();
1689 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1692 /* Send channel to the application. */
1693 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1694 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1695 ret
= -ENOTCONN
; /* Caused by app exiting. */
1697 } else if (ret
== -EAGAIN
) {
1698 /* Caused by timeout. */
1699 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
"\".",
1700 app
->pid
, ua_chan
->name
, ua_sess
->tracing_id
);
1701 /* Treat this the same way as an application that is exiting. */
1704 } else if (ret
< 0) {
1708 health_code_update();
1710 /* Send all streams to application. */
1711 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1712 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1713 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1714 ret
= -ENOTCONN
; /* Caused by app exiting. */
1716 } else if (ret
== -EAGAIN
) {
1717 /* Caused by timeout. */
1718 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
"\".",
1719 app
->pid
, stream
->name
, ua_chan
->name
,
1720 ua_sess
->tracing_id
);
1722 * Treat this the same way as an application that is
1726 } else if (ret
< 0) {
1729 /* We don't need the stream anymore once sent to the tracer. */
1730 cds_list_del(&stream
->list
);
1731 delete_ust_app_stream(-1, stream
, app
);
1733 /* Flag the channel that it is sent to the application. */
1734 ua_chan
->is_sent
= 1;
1737 health_code_update();
1742 * Create the specified event onto the UST tracer for a UST session.
1744 * Should be called with session mutex held.
1747 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1748 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1752 health_code_update();
1754 /* Create UST event on tracer */
1755 pthread_mutex_lock(&app
->sock_lock
);
1756 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1758 pthread_mutex_unlock(&app
->sock_lock
);
1760 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1762 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
1763 app
->pid
, app
->sock
);
1764 } else if (ret
== -EAGAIN
) {
1766 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
1767 app
->pid
, app
->sock
);
1769 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
1770 ua_event
->attr
.name
, ret
, app
->pid
,
1776 ua_event
->handle
= ua_event
->obj
->handle
;
1778 DBG2("UST app event %s created successfully for pid:%d",
1779 ua_event
->attr
.name
, app
->pid
);
1781 health_code_update();
1783 /* Set filter if one is present. */
1784 if (ua_event
->filter
) {
1785 ret
= set_ust_event_filter(ua_event
, app
);
1791 /* Set exclusions for the event */
1792 if (ua_event
->exclusion
) {
1793 ret
= set_ust_event_exclusion(ua_event
, app
);
1799 /* If event not enabled, disable it on the tracer */
1800 if (ua_event
->enabled
) {
1802 * We now need to explicitly enable the event, since it
1803 * is now disabled at creation.
1805 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1808 * If we hit an EPERM, something is wrong with our enable call. If
1809 * we get an EEXIST, there is a problem on the tracer side since we
1813 case -LTTNG_UST_ERR_PERM
:
1814 /* Code flow problem */
1816 case -LTTNG_UST_ERR_EXIST
:
1817 /* It's OK for our use case. */
1828 health_code_update();
1833 * Copy data between an UST app event and a LTT event.
1835 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1836 struct ltt_ust_event
*uevent
)
1838 size_t exclusion_alloc_size
;
1840 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1841 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1843 ua_event
->enabled
= uevent
->enabled
;
1845 /* Copy event attributes */
1846 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1848 /* Copy filter bytecode */
1849 if (uevent
->filter
) {
1850 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1851 /* Filter might be NULL here in case of ENONEM. */
1854 /* Copy exclusion data */
1855 if (uevent
->exclusion
) {
1856 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1857 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1858 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1859 if (ua_event
->exclusion
== NULL
) {
1862 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1863 exclusion_alloc_size
);
1869 * Copy data between an UST app channel and a LTT channel.
1871 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1872 struct ltt_ust_channel
*uchan
)
1874 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1876 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1877 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1879 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1880 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1882 /* Copy event attributes since the layout is different. */
1883 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1884 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1885 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1886 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1887 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1888 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1889 ua_chan
->attr
.output
= uchan
->attr
.output
;
1890 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
1893 * Note that the attribute channel type is not set since the channel on the
1894 * tracing registry side does not have this information.
1897 ua_chan
->enabled
= uchan
->enabled
;
1898 ua_chan
->tracing_channel_id
= uchan
->id
;
1900 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1904 * Copy data between a UST app session and a regular LTT session.
1906 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1907 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1909 struct tm
*timeinfo
;
1912 char tmp_shm_path
[PATH_MAX
];
1914 timeinfo
= localtime(&app
->registration_time
);
1915 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1917 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1919 ua_sess
->tracing_id
= usess
->id
;
1920 ua_sess
->id
= get_next_session_id();
1921 ua_sess
->real_credentials
.uid
= app
->uid
;
1922 ua_sess
->real_credentials
.gid
= app
->gid
;
1923 ua_sess
->effective_credentials
.uid
= usess
->uid
;
1924 ua_sess
->effective_credentials
.gid
= usess
->gid
;
1925 ua_sess
->buffer_type
= usess
->buffer_type
;
1926 ua_sess
->bits_per_long
= app
->bits_per_long
;
1928 /* There is only one consumer object per session possible. */
1929 consumer_output_get(usess
->consumer
);
1930 ua_sess
->consumer
= usess
->consumer
;
1932 ua_sess
->output_traces
= usess
->output_traces
;
1933 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1934 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1935 &usess
->metadata_attr
);
1937 switch (ua_sess
->buffer_type
) {
1938 case LTTNG_BUFFER_PER_PID
:
1939 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1940 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1943 case LTTNG_BUFFER_PER_UID
:
1944 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1945 DEFAULT_UST_TRACE_UID_PATH
,
1946 ua_sess
->real_credentials
.uid
,
1947 app
->bits_per_long
);
1954 PERROR("asprintf UST shadow copy session");
1959 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1960 sizeof(ua_sess
->root_shm_path
));
1961 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1962 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1963 sizeof(ua_sess
->shm_path
));
1964 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1965 if (ua_sess
->shm_path
[0]) {
1966 switch (ua_sess
->buffer_type
) {
1967 case LTTNG_BUFFER_PER_PID
:
1968 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1969 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1970 app
->name
, app
->pid
, datetime
);
1972 case LTTNG_BUFFER_PER_UID
:
1973 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1974 DEFAULT_UST_TRACE_UID_PATH
,
1975 app
->uid
, app
->bits_per_long
);
1982 PERROR("sprintf UST shadow copy session");
1986 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1987 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1988 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1993 consumer_output_put(ua_sess
->consumer
);
1997 * Lookup sesison wrapper.
2000 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2001 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2003 /* Get right UST app session from app */
2004 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2008 * Return ust app session from the app session hashtable using the UST session
2011 static struct ust_app_session
*lookup_session_by_app(
2012 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2014 struct lttng_ht_iter iter
;
2015 struct lttng_ht_node_u64
*node
;
2017 __lookup_session_by_app(usess
, app
, &iter
);
2018 node
= lttng_ht_iter_get_node_u64(&iter
);
2023 return caa_container_of(node
, struct ust_app_session
, node
);
2030 * Setup buffer registry per PID for the given session and application. If none
2031 * is found, a new one is created, added to the global registry and
2032 * initialized. If regp is valid, it's set with the newly created object.
2034 * Return 0 on success or else a negative value.
2036 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2037 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2040 struct buffer_reg_pid
*reg_pid
;
2047 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2050 * This is the create channel path meaning that if there is NO
2051 * registry available, we have to create one for this session.
2053 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2054 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2062 /* Initialize registry. */
2063 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2064 app
->bits_per_long
, app
->uint8_t_alignment
,
2065 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2066 app
->uint64_t_alignment
, app
->long_alignment
,
2067 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2068 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2069 ua_sess
->effective_credentials
.uid
,
2070 ua_sess
->effective_credentials
.gid
, ua_sess
->tracing_id
,
2074 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2075 * destroy the buffer registry, because it is always expected
2076 * that if the buffer registry can be found, its ust registry is
2079 buffer_reg_pid_destroy(reg_pid
);
2083 buffer_reg_pid_add(reg_pid
);
2085 DBG3("UST app buffer registry per PID created successfully");
2097 * Setup buffer registry per UID for the given session and application. If none
2098 * is found, a new one is created, added to the global registry and
2099 * initialized. If regp is valid, it's set with the newly created object.
2101 * Return 0 on success or else a negative value.
2103 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2104 struct ust_app_session
*ua_sess
,
2105 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2108 struct buffer_reg_uid
*reg_uid
;
2115 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2118 * This is the create channel path meaning that if there is NO
2119 * registry available, we have to create one for this session.
2121 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2122 LTTNG_DOMAIN_UST
, ®_uid
,
2123 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2131 /* Initialize registry. */
2132 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2133 app
->bits_per_long
, app
->uint8_t_alignment
,
2134 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2135 app
->uint64_t_alignment
, app
->long_alignment
,
2136 app
->byte_order
, app
->version
.major
,
2137 app
->version
.minor
, reg_uid
->root_shm_path
,
2138 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2139 ua_sess
->tracing_id
, app
->uid
);
2142 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2143 * destroy the buffer registry, because it is always expected
2144 * that if the buffer registry can be found, its ust registry is
2147 buffer_reg_uid_destroy(reg_uid
, NULL
);
2150 /* Add node to teardown list of the session. */
2151 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2153 buffer_reg_uid_add(reg_uid
);
2155 DBG3("UST app buffer registry per UID created successfully");
2166 * Create a session on the tracer side for the given app.
2168 * On success, ua_sess_ptr is populated with the session pointer or else left
2169 * untouched. If the session was created, is_created is set to 1. On error,
2170 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2173 * Returns 0 on success or else a negative code which is either -ENOMEM or
2174 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2176 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2177 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2180 int ret
, created
= 0;
2181 struct ust_app_session
*ua_sess
;
2185 assert(ua_sess_ptr
);
2187 health_code_update();
2189 ua_sess
= lookup_session_by_app(usess
, app
);
2190 if (ua_sess
== NULL
) {
2191 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2192 app
->pid
, usess
->id
);
2193 ua_sess
= alloc_ust_app_session();
2194 if (ua_sess
== NULL
) {
2195 /* Only malloc can failed so something is really wrong */
2199 shadow_copy_session(ua_sess
, usess
, app
);
2203 switch (usess
->buffer_type
) {
2204 case LTTNG_BUFFER_PER_PID
:
2205 /* Init local registry. */
2206 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2208 delete_ust_app_session(-1, ua_sess
, app
);
2212 case LTTNG_BUFFER_PER_UID
:
2213 /* Look for a global registry. If none exists, create one. */
2214 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2216 delete_ust_app_session(-1, ua_sess
, app
);
2226 health_code_update();
2228 if (ua_sess
->handle
== -1) {
2229 pthread_mutex_lock(&app
->sock_lock
);
2230 ret
= ustctl_create_session(app
->sock
);
2231 pthread_mutex_unlock(&app
->sock_lock
);
2233 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2234 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2235 app
->pid
, app
->sock
);
2237 } else if (ret
== -EAGAIN
) {
2238 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2239 app
->pid
, app
->sock
);
2242 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2243 ret
, app
->pid
, app
->sock
);
2245 delete_ust_app_session(-1, ua_sess
, app
);
2246 if (ret
!= -ENOMEM
) {
2248 * Tracer is probably gone or got an internal error so let's
2249 * behave like it will soon unregister or not usable.
2256 ua_sess
->handle
= ret
;
2258 /* Add ust app session to app's HT */
2259 lttng_ht_node_init_u64(&ua_sess
->node
,
2260 ua_sess
->tracing_id
);
2261 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2262 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2263 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2264 &ua_sess
->ust_objd_node
);
2266 DBG2("UST app session created successfully with handle %d", ret
);
2269 *ua_sess_ptr
= ua_sess
;
2271 *is_created
= created
;
2274 /* Everything went well. */
2278 health_code_update();
2283 * Match function for a hash table lookup of ust_app_ctx.
2285 * It matches an ust app context based on the context type and, in the case
2286 * of perf counters, their name.
2288 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2290 struct ust_app_ctx
*ctx
;
2291 const struct lttng_ust_context_attr
*key
;
2296 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2300 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2305 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2306 if (strncmp(key
->u
.perf_counter
.name
,
2307 ctx
->ctx
.u
.perf_counter
.name
,
2308 sizeof(key
->u
.perf_counter
.name
))) {
2312 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2313 if (strcmp(key
->u
.app_ctx
.provider_name
,
2314 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2315 strcmp(key
->u
.app_ctx
.ctx_name
,
2316 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2332 * Lookup for an ust app context from an lttng_ust_context.
2334 * Must be called while holding RCU read side lock.
2335 * Return an ust_app_ctx object or NULL on error.
2338 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2339 struct lttng_ust_context_attr
*uctx
)
2341 struct lttng_ht_iter iter
;
2342 struct lttng_ht_node_ulong
*node
;
2343 struct ust_app_ctx
*app_ctx
= NULL
;
2348 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2349 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2350 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2351 node
= lttng_ht_iter_get_node_ulong(&iter
);
2356 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2363 * Create a context for the channel on the tracer.
2365 * Called with UST app session lock held and a RCU read side lock.
2368 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2369 struct lttng_ust_context_attr
*uctx
,
2370 struct ust_app
*app
)
2373 struct ust_app_ctx
*ua_ctx
;
2375 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2377 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2383 ua_ctx
= alloc_ust_app_ctx(uctx
);
2384 if (ua_ctx
== NULL
) {
2390 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2391 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2392 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2394 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2404 * Enable on the tracer side a ust app event for the session and channel.
2406 * Called with UST app session lock held.
2409 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2410 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2414 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2419 ua_event
->enabled
= 1;
2426 * Disable on the tracer side a ust app event for the session and channel.
2428 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2429 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2433 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2438 ua_event
->enabled
= 0;
2445 * Lookup ust app channel for session and disable it on the tracer side.
2448 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2449 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2453 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2458 ua_chan
->enabled
= 0;
2465 * Lookup ust app channel for session and enable it on the tracer side. This
2466 * MUST be called with a RCU read side lock acquired.
2468 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2469 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2472 struct lttng_ht_iter iter
;
2473 struct lttng_ht_node_str
*ua_chan_node
;
2474 struct ust_app_channel
*ua_chan
;
2476 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2477 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2478 if (ua_chan_node
== NULL
) {
2479 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2480 uchan
->name
, ua_sess
->tracing_id
);
2484 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2486 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2496 * Ask the consumer to create a channel and get it if successful.
2498 * Called with UST app session lock held.
2500 * Return 0 on success or else a negative value.
2502 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2503 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2504 int bitness
, struct ust_registry_session
*registry
,
2505 uint64_t trace_archive_id
)
2508 unsigned int nb_fd
= 0;
2509 struct consumer_socket
*socket
;
2517 health_code_update();
2519 /* Get the right consumer socket for the application. */
2520 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2526 health_code_update();
2528 /* Need one fd for the channel. */
2529 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2531 ERR("Exhausted number of available FD upon create channel");
2536 * Ask consumer to create channel. The consumer will return the number of
2537 * stream we have to expect.
2539 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2540 registry
, usess
->current_trace_chunk
);
2546 * Compute the number of fd needed before receiving them. It must be 2 per
2547 * stream (2 being the default value here).
2549 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2551 /* Reserve the amount of file descriptor we need. */
2552 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2554 ERR("Exhausted number of available FD upon create channel");
2555 goto error_fd_get_stream
;
2558 health_code_update();
2561 * Now get the channel from the consumer. This call will populate the stream
2562 * list of that channel and set the ust objects.
2564 if (usess
->consumer
->enabled
) {
2565 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2575 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2576 error_fd_get_stream
:
2578 * Initiate a destroy channel on the consumer since we had an error
2579 * handling it on our side. The return value is of no importance since we
2580 * already have a ret value set by the previous error that we need to
2583 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2585 lttng_fd_put(LTTNG_FD_APPS
, 1);
2587 health_code_update();
2593 * Duplicate the ust data object of the ust app stream and save it in the
2594 * buffer registry stream.
2596 * Return 0 on success or else a negative value.
2598 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2599 struct ust_app_stream
*stream
)
2606 /* Reserve the amount of file descriptor we need. */
2607 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2609 ERR("Exhausted number of available FD upon duplicate stream");
2613 /* Duplicate object for stream once the original is in the registry. */
2614 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2615 reg_stream
->obj
.ust
);
2617 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2618 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2619 lttng_fd_put(LTTNG_FD_APPS
, 2);
2622 stream
->handle
= stream
->obj
->handle
;
2629 * Duplicate the ust data object of the ust app. channel and save it in the
2630 * buffer registry channel.
2632 * Return 0 on success or else a negative value.
2634 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2635 struct ust_app_channel
*ua_chan
)
2642 /* Need two fds for the channel. */
2643 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2645 ERR("Exhausted number of available FD upon duplicate channel");
2649 /* Duplicate object for stream once the original is in the registry. */
2650 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2652 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2653 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2656 ua_chan
->handle
= ua_chan
->obj
->handle
;
2661 lttng_fd_put(LTTNG_FD_APPS
, 1);
2667 * For a given channel buffer registry, setup all streams of the given ust
2668 * application channel.
2670 * Return 0 on success or else a negative value.
2672 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2673 struct ust_app_channel
*ua_chan
,
2674 struct ust_app
*app
)
2677 struct ust_app_stream
*stream
, *stmp
;
2682 DBG2("UST app setup buffer registry stream");
2684 /* Send all streams to application. */
2685 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2686 struct buffer_reg_stream
*reg_stream
;
2688 ret
= buffer_reg_stream_create(®_stream
);
2694 * Keep original pointer and nullify it in the stream so the delete
2695 * stream call does not release the object.
2697 reg_stream
->obj
.ust
= stream
->obj
;
2699 buffer_reg_stream_add(reg_stream
, reg_chan
);
2701 /* We don't need the streams anymore. */
2702 cds_list_del(&stream
->list
);
2703 delete_ust_app_stream(-1, stream
, app
);
2711 * Create a buffer registry channel for the given session registry and
2712 * application channel object. If regp pointer is valid, it's set with the
2713 * created object. Important, the created object is NOT added to the session
2714 * registry hash table.
2716 * Return 0 on success else a negative value.
2718 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2719 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2722 struct buffer_reg_channel
*reg_chan
= NULL
;
2727 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2729 /* Create buffer registry channel. */
2730 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2735 reg_chan
->consumer_key
= ua_chan
->key
;
2736 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2737 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2739 /* Create and add a channel registry to session. */
2740 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2741 ua_chan
->tracing_channel_id
);
2745 buffer_reg_channel_add(reg_sess
, reg_chan
);
2754 /* Safe because the registry channel object was not added to any HT. */
2755 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2761 * Setup buffer registry channel for the given session registry and application
2762 * channel object. If regp pointer is valid, it's set with the created object.
2764 * Return 0 on success else a negative value.
2766 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2767 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2768 struct ust_app
*app
)
2775 assert(ua_chan
->obj
);
2777 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2779 /* Setup all streams for the registry. */
2780 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2785 reg_chan
->obj
.ust
= ua_chan
->obj
;
2786 ua_chan
->obj
= NULL
;
2791 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2792 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2797 * Send buffer registry channel to the application.
2799 * Return 0 on success else a negative value.
2801 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2802 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2803 struct ust_app_channel
*ua_chan
)
2806 struct buffer_reg_stream
*reg_stream
;
2813 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2815 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2820 /* Send channel to the application. */
2821 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2822 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2823 ret
= -ENOTCONN
; /* Caused by app exiting. */
2825 } else if (ret
== -EAGAIN
) {
2826 /* Caused by timeout. */
2827 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
"\".",
2828 app
->pid
, ua_chan
->name
, ua_sess
->tracing_id
);
2829 /* Treat this the same way as an application that is exiting. */
2832 } else if (ret
< 0) {
2836 health_code_update();
2838 /* Send all streams to application. */
2839 pthread_mutex_lock(®_chan
->stream_list_lock
);
2840 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2841 struct ust_app_stream stream
;
2843 ret
= duplicate_stream_object(reg_stream
, &stream
);
2845 goto error_stream_unlock
;
2848 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2850 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2851 ret
= -ENOTCONN
; /* Caused by app exiting. */
2852 } else if (ret
== -EAGAIN
) {
2854 * Caused by timeout.
2855 * Treat this the same way as an application
2858 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
"\".",
2859 app
->pid
, stream
.name
,
2861 ua_sess
->tracing_id
);
2864 (void) release_ust_app_stream(-1, &stream
, app
);
2865 goto error_stream_unlock
;
2869 * The return value is not important here. This function will output an
2872 (void) release_ust_app_stream(-1, &stream
, app
);
2874 ua_chan
->is_sent
= 1;
2876 error_stream_unlock
:
2877 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2883 * Create and send to the application the created buffers with per UID buffers.
2885 * This MUST be called with a RCU read side lock acquired.
2886 * The session list lock and the session's lock must be acquired.
2888 * Return 0 on success else a negative value.
2890 static int create_channel_per_uid(struct ust_app
*app
,
2891 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2892 struct ust_app_channel
*ua_chan
)
2895 struct buffer_reg_uid
*reg_uid
;
2896 struct buffer_reg_channel
*reg_chan
;
2897 struct ltt_session
*session
= NULL
;
2898 enum lttng_error_code notification_ret
;
2899 struct ust_registry_channel
*chan_reg
;
2906 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2908 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2910 * The session creation handles the creation of this global registry
2911 * object. If none can be find, there is a code flow problem or a
2916 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2922 /* Create the buffer registry channel object. */
2923 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2925 ERR("Error creating the UST channel \"%s\" registry instance",
2930 session
= session_find_by_id(ua_sess
->tracing_id
);
2932 assert(pthread_mutex_trylock(&session
->lock
));
2933 assert(session_trylock_list());
2936 * Create the buffers on the consumer side. This call populates the
2937 * ust app channel object with all streams and data object.
2939 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2940 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
2941 session
->most_recent_chunk_id
.value
);
2943 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2947 * Let's remove the previously created buffer registry channel so
2948 * it's not visible anymore in the session registry.
2950 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2951 ua_chan
->tracing_channel_id
, false);
2952 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2953 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2958 * Setup the streams and add it to the session registry.
2960 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2961 ua_chan
, reg_chan
, app
);
2963 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
2967 /* Notify the notification subsystem of the channel's creation. */
2968 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2969 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2970 ua_chan
->tracing_channel_id
);
2972 chan_reg
->consumer_key
= ua_chan
->key
;
2974 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2976 notification_ret
= notification_thread_command_add_channel(
2977 notification_thread_handle
, session
->name
,
2978 ua_sess
->effective_credentials
.uid
,
2979 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
2980 ua_chan
->key
, LTTNG_DOMAIN_UST
,
2981 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2982 if (notification_ret
!= LTTNG_OK
) {
2983 ret
= - (int) notification_ret
;
2984 ERR("Failed to add channel to notification thread");
2989 /* Send buffers to the application. */
2990 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2992 if (ret
!= -ENOTCONN
) {
2993 ERR("Error sending channel to application");
3000 session_put(session
);
3006 * Create and send to the application the created buffers with per PID buffers.
3008 * Called with UST app session lock held.
3009 * The session list lock and the session's lock must be acquired.
3011 * Return 0 on success else a negative value.
3013 static int create_channel_per_pid(struct ust_app
*app
,
3014 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3015 struct ust_app_channel
*ua_chan
)
3018 struct ust_registry_session
*registry
;
3019 enum lttng_error_code cmd_ret
;
3020 struct ltt_session
*session
= NULL
;
3021 uint64_t chan_reg_key
;
3022 struct ust_registry_channel
*chan_reg
;
3029 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3033 registry
= get_session_registry(ua_sess
);
3034 /* The UST app session lock is held, registry shall not be null. */
3037 /* Create and add a new channel registry to session. */
3038 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3040 ERR("Error creating the UST channel \"%s\" registry instance",
3045 session
= session_find_by_id(ua_sess
->tracing_id
);
3048 assert(pthread_mutex_trylock(&session
->lock
));
3049 assert(session_trylock_list());
3051 /* Create and get channel on the consumer side. */
3052 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3053 app
->bits_per_long
, registry
,
3054 session
->most_recent_chunk_id
.value
);
3056 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3058 goto error_remove_from_registry
;
3061 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3063 if (ret
!= -ENOTCONN
) {
3064 ERR("Error sending channel to application");
3066 goto error_remove_from_registry
;
3069 chan_reg_key
= ua_chan
->key
;
3070 pthread_mutex_lock(®istry
->lock
);
3071 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3073 chan_reg
->consumer_key
= ua_chan
->key
;
3074 pthread_mutex_unlock(®istry
->lock
);
3076 cmd_ret
= notification_thread_command_add_channel(
3077 notification_thread_handle
, session
->name
,
3078 ua_sess
->effective_credentials
.uid
,
3079 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
3080 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3081 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3082 if (cmd_ret
!= LTTNG_OK
) {
3083 ret
= - (int) cmd_ret
;
3084 ERR("Failed to add channel to notification thread");
3085 goto error_remove_from_registry
;
3088 error_remove_from_registry
:
3090 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3095 session_put(session
);
3101 * From an already allocated ust app channel, create the channel buffers if
3102 * needed and send them to the application. This MUST be called with a RCU read
3103 * side lock acquired.
3105 * Called with UST app session lock held.
3107 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3108 * the application exited concurrently.
3110 static int ust_app_channel_send(struct ust_app
*app
,
3111 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3112 struct ust_app_channel
*ua_chan
)
3118 assert(usess
->active
);
3122 /* Handle buffer type before sending the channel to the application. */
3123 switch (usess
->buffer_type
) {
3124 case LTTNG_BUFFER_PER_UID
:
3126 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3132 case LTTNG_BUFFER_PER_PID
:
3134 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3146 /* Initialize ust objd object using the received handle and add it. */
3147 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3148 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3150 /* If channel is not enabled, disable it on the tracer */
3151 if (!ua_chan
->enabled
) {
3152 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3163 * Create UST app channel and return it through ua_chanp if not NULL.
3165 * Called with UST app session lock and RCU read-side lock held.
3167 * Return 0 on success or else a negative value.
3169 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3170 struct ltt_ust_channel
*uchan
,
3171 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3172 struct ust_app_channel
**ua_chanp
)
3175 struct lttng_ht_iter iter
;
3176 struct lttng_ht_node_str
*ua_chan_node
;
3177 struct ust_app_channel
*ua_chan
;
3179 /* Lookup channel in the ust app session */
3180 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3181 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3182 if (ua_chan_node
!= NULL
) {
3183 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3187 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3188 if (ua_chan
== NULL
) {
3189 /* Only malloc can fail here */
3193 shadow_copy_channel(ua_chan
, uchan
);
3195 /* Set channel type. */
3196 ua_chan
->attr
.type
= type
;
3198 /* Only add the channel if successful on the tracer side. */
3199 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3202 *ua_chanp
= ua_chan
;
3205 /* Everything went well. */
3213 * Create UST app event and create it on the tracer side.
3215 * Called with ust app session mutex held.
3218 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3219 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3220 struct ust_app
*app
)
3223 struct ust_app_event
*ua_event
;
3225 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3226 if (ua_event
== NULL
) {
3227 /* Only failure mode of alloc_ust_app_event(). */
3231 shadow_copy_event(ua_event
, uevent
);
3233 /* Create it on the tracer side */
3234 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3237 * Not found previously means that it does not exist on the
3238 * tracer. If the application reports that the event existed,
3239 * it means there is a bug in the sessiond or lttng-ust
3240 * (or corruption, etc.)
3242 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3243 ERR("Tracer for application reported that an event being created already existed: "
3244 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3246 app
->pid
, app
->ppid
, app
->uid
,
3252 add_unique_ust_app_event(ua_chan
, ua_event
);
3254 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3261 /* Valid. Calling here is already in a read side lock */
3262 delete_ust_app_event(-1, ua_event
, app
);
3267 * Create UST metadata and open it on the tracer side.
3269 * Called with UST app session lock held and RCU read side lock.
3271 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3272 struct ust_app
*app
, struct consumer_output
*consumer
)
3275 struct ust_app_channel
*metadata
;
3276 struct consumer_socket
*socket
;
3277 struct ust_registry_session
*registry
;
3278 struct ltt_session
*session
= NULL
;
3284 registry
= get_session_registry(ua_sess
);
3285 /* The UST app session is held registry shall not be null. */
3288 pthread_mutex_lock(®istry
->lock
);
3290 /* Metadata already exists for this registry or it was closed previously */
3291 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3296 /* Allocate UST metadata */
3297 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3299 /* malloc() failed */
3304 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3306 /* Need one fd for the channel. */
3307 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3309 ERR("Exhausted number of available FD upon create metadata");
3313 /* Get the right consumer socket for the application. */
3314 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3317 goto error_consumer
;
3321 * Keep metadata key so we can identify it on the consumer side. Assign it
3322 * to the registry *before* we ask the consumer so we avoid the race of the
3323 * consumer requesting the metadata and the ask_channel call on our side
3324 * did not returned yet.
3326 registry
->metadata_key
= metadata
->key
;
3328 session
= session_find_by_id(ua_sess
->tracing_id
);
3331 assert(pthread_mutex_trylock(&session
->lock
));
3332 assert(session_trylock_list());
3335 * Ask the metadata channel creation to the consumer. The metadata object
3336 * will be created by the consumer and kept their. However, the stream is
3337 * never added or monitored until we do a first push metadata to the
3340 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3341 registry
, session
->current_trace_chunk
);
3343 /* Nullify the metadata key so we don't try to close it later on. */
3344 registry
->metadata_key
= 0;
3345 goto error_consumer
;
3349 * The setup command will make the metadata stream be sent to the relayd,
3350 * if applicable, and the thread managing the metadatas. This is important
3351 * because after this point, if an error occurs, the only way the stream
3352 * can be deleted is to be monitored in the consumer.
3354 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3356 /* Nullify the metadata key so we don't try to close it later on. */
3357 registry
->metadata_key
= 0;
3358 goto error_consumer
;
3361 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3362 metadata
->key
, app
->pid
);
3365 lttng_fd_put(LTTNG_FD_APPS
, 1);
3366 delete_ust_app_channel(-1, metadata
, app
);
3368 pthread_mutex_unlock(®istry
->lock
);
3370 session_put(session
);
3376 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3377 * acquired before calling this function.
3379 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3381 struct ust_app
*app
= NULL
;
3382 struct lttng_ht_node_ulong
*node
;
3383 struct lttng_ht_iter iter
;
3385 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3386 node
= lttng_ht_iter_get_node_ulong(&iter
);
3388 DBG2("UST app no found with pid %d", pid
);
3392 DBG2("Found UST app by pid %d", pid
);
3394 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3401 * Allocate and init an UST app object using the registration information and
3402 * the command socket. This is called when the command socket connects to the
3405 * The object is returned on success or else NULL.
3407 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3409 struct ust_app
*lta
= NULL
;
3414 DBG3("UST app creating application for socket %d", sock
);
3416 if ((msg
->bits_per_long
== 64 &&
3417 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3418 || (msg
->bits_per_long
== 32 &&
3419 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3420 ERR("Registration failed: application \"%s\" (pid: %d) has "
3421 "%d-bit long, but no consumerd for this size is available.\n",
3422 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3426 lta
= zmalloc(sizeof(struct ust_app
));
3432 lta
->ppid
= msg
->ppid
;
3433 lta
->uid
= msg
->uid
;
3434 lta
->gid
= msg
->gid
;
3436 lta
->bits_per_long
= msg
->bits_per_long
;
3437 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3438 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3439 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3440 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3441 lta
->long_alignment
= msg
->long_alignment
;
3442 lta
->byte_order
= msg
->byte_order
;
3444 lta
->v_major
= msg
->major
;
3445 lta
->v_minor
= msg
->minor
;
3446 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3447 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3448 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3449 lta
->notify_sock
= -1;
3451 /* Copy name and make sure it's NULL terminated. */
3452 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3453 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3456 * Before this can be called, when receiving the registration information,
3457 * the application compatibility is checked. So, at this point, the
3458 * application can work with this session daemon.
3460 lta
->compatible
= 1;
3462 lta
->pid
= msg
->pid
;
3463 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3465 pthread_mutex_init(<a
->sock_lock
, NULL
);
3466 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3468 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3474 * For a given application object, add it to every hash table.
3476 void ust_app_add(struct ust_app
*app
)
3479 assert(app
->notify_sock
>= 0);
3481 app
->registration_time
= time(NULL
);
3486 * On a re-registration, we want to kick out the previous registration of
3489 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3492 * The socket _should_ be unique until _we_ call close. So, a add_unique
3493 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3494 * already in the table.
3496 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3498 /* Add application to the notify socket hash table. */
3499 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3500 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3502 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
3503 "notify_sock =%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3504 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3511 * Set the application version into the object.
3513 * Return 0 on success else a negative value either an errno code or a
3514 * LTTng-UST error code.
3516 int ust_app_version(struct ust_app
*app
)
3522 pthread_mutex_lock(&app
->sock_lock
);
3523 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3524 pthread_mutex_unlock(&app
->sock_lock
);
3526 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
3527 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
3528 app
->pid
, app
->sock
);
3529 } else if (ret
== -EAGAIN
) {
3530 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
3531 app
->pid
, app
->sock
);
3533 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
3534 ret
, app
->pid
, app
->sock
);
3542 * Unregister app by removing it from the global traceable app list and freeing
3545 * The socket is already closed at this point so no close to sock.
3547 void ust_app_unregister(int sock
)
3549 struct ust_app
*lta
;
3550 struct lttng_ht_node_ulong
*node
;
3551 struct lttng_ht_iter ust_app_sock_iter
;
3552 struct lttng_ht_iter iter
;
3553 struct ust_app_session
*ua_sess
;
3558 /* Get the node reference for a call_rcu */
3559 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3560 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3563 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3564 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3567 * For per-PID buffers, perform "push metadata" and flush all
3568 * application streams before removing app from hash tables,
3569 * ensuring proper behavior of data_pending check.
3570 * Remove sessions so they are not visible during deletion.
3572 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3574 struct ust_registry_session
*registry
;
3576 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3578 /* The session was already removed so scheduled for teardown. */
3582 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3583 (void) ust_app_flush_app_session(lta
, ua_sess
);
3587 * Add session to list for teardown. This is safe since at this point we
3588 * are the only one using this list.
3590 pthread_mutex_lock(&ua_sess
->lock
);
3592 if (ua_sess
->deleted
) {
3593 pthread_mutex_unlock(&ua_sess
->lock
);
3598 * Normally, this is done in the delete session process which is
3599 * executed in the call rcu below. However, upon registration we can't
3600 * afford to wait for the grace period before pushing data or else the
3601 * data pending feature can race between the unregistration and stop
3602 * command where the data pending command is sent *before* the grace
3605 * The close metadata below nullifies the metadata pointer in the
3606 * session so the delete session will NOT push/close a second time.
3608 registry
= get_session_registry(ua_sess
);
3610 /* Push metadata for application before freeing the application. */
3611 (void) push_metadata(registry
, ua_sess
->consumer
);
3614 * Don't ask to close metadata for global per UID buffers. Close
3615 * metadata only on destroy trace session in this case. Also, the
3616 * previous push metadata could have flag the metadata registry to
3617 * close so don't send a close command if closed.
3619 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3620 /* And ask to close it for this session registry. */
3621 (void) close_metadata(registry
, ua_sess
->consumer
);
3624 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3626 pthread_mutex_unlock(&ua_sess
->lock
);
3629 /* Remove application from PID hash table */
3630 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3634 * Remove application from notify hash table. The thread handling the
3635 * notify socket could have deleted the node so ignore on error because
3636 * either way it's valid. The close of that socket is handled by the
3637 * apps_notify_thread.
3639 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3640 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3643 * Ignore return value since the node might have been removed before by an
3644 * add replace during app registration because the PID can be reassigned by
3647 iter
.iter
.node
= <a
->pid_n
.node
;
3648 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3650 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3655 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3662 * Fill events array with all events name of all registered apps.
3664 int ust_app_list_events(struct lttng_event
**events
)
3667 size_t nbmem
, count
= 0;
3668 struct lttng_ht_iter iter
;
3669 struct ust_app
*app
;
3670 struct lttng_event
*tmp_event
;
3672 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3673 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3674 if (tmp_event
== NULL
) {
3675 PERROR("zmalloc ust app events");
3682 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3683 struct lttng_ust_tracepoint_iter uiter
;
3685 health_code_update();
3687 if (!app
->compatible
) {
3689 * TODO: In time, we should notice the caller of this error by
3690 * telling him that this is a version error.
3694 pthread_mutex_lock(&app
->sock_lock
);
3695 handle
= ustctl_tracepoint_list(app
->sock
);
3697 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3698 ERR("UST app list events getting handle failed for app pid %d",
3701 pthread_mutex_unlock(&app
->sock_lock
);
3705 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3706 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3707 /* Handle ustctl error. */
3711 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3712 ERR("UST app tp list get failed for app %d with ret %d",
3715 DBG3("UST app tp list get failed. Application is dead");
3719 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3720 if (release_ret
< 0 &&
3721 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3722 release_ret
!= -EPIPE
) {
3723 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3725 pthread_mutex_unlock(&app
->sock_lock
);
3729 health_code_update();
3730 if (count
>= nbmem
) {
3731 /* In case the realloc fails, we free the memory */
3732 struct lttng_event
*new_tmp_event
;
3735 new_nbmem
= nbmem
<< 1;
3736 DBG2("Reallocating event list from %zu to %zu entries",
3738 new_tmp_event
= realloc(tmp_event
,
3739 new_nbmem
* sizeof(struct lttng_event
));
3740 if (new_tmp_event
== NULL
) {
3743 PERROR("realloc ust app events");
3746 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3747 if (release_ret
< 0 &&
3748 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3749 release_ret
!= -EPIPE
) {
3750 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3752 pthread_mutex_unlock(&app
->sock_lock
);
3755 /* Zero the new memory */
3756 memset(new_tmp_event
+ nbmem
, 0,
3757 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3759 tmp_event
= new_tmp_event
;
3761 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3762 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3763 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3764 tmp_event
[count
].pid
= app
->pid
;
3765 tmp_event
[count
].enabled
= -1;
3768 ret
= ustctl_release_handle(app
->sock
, handle
);
3769 pthread_mutex_unlock(&app
->sock_lock
);
3771 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3772 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
3773 app
->pid
, app
->sock
);
3774 } else if (ret
== -EAGAIN
) {
3775 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
3776 app
->pid
, app
->sock
);
3778 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
3779 ret
, app
->pid
, app
->sock
);
3785 *events
= tmp_event
;
3787 DBG2("UST app list events done (%zu events)", count
);
3792 health_code_update();
3797 * Fill events array with all events name of all registered apps.
3799 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3802 size_t nbmem
, count
= 0;
3803 struct lttng_ht_iter iter
;
3804 struct ust_app
*app
;
3805 struct lttng_event_field
*tmp_event
;
3807 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3808 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3809 if (tmp_event
== NULL
) {
3810 PERROR("zmalloc ust app event fields");
3817 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3818 struct lttng_ust_field_iter uiter
;
3820 health_code_update();
3822 if (!app
->compatible
) {
3824 * TODO: In time, we should notice the caller of this error by
3825 * telling him that this is a version error.
3829 pthread_mutex_lock(&app
->sock_lock
);
3830 handle
= ustctl_tracepoint_field_list(app
->sock
);
3832 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3833 ERR("UST app list field getting handle failed for app pid %d",
3836 pthread_mutex_unlock(&app
->sock_lock
);
3840 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3841 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3842 /* Handle ustctl error. */
3846 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3847 ERR("UST app tp list field failed for app %d with ret %d",
3850 DBG3("UST app tp list field failed. Application is dead");
3854 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3855 pthread_mutex_unlock(&app
->sock_lock
);
3856 if (release_ret
< 0 &&
3857 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3858 release_ret
!= -EPIPE
) {
3859 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3864 health_code_update();
3865 if (count
>= nbmem
) {
3866 /* In case the realloc fails, we free the memory */
3867 struct lttng_event_field
*new_tmp_event
;
3870 new_nbmem
= nbmem
<< 1;
3871 DBG2("Reallocating event field list from %zu to %zu entries",
3873 new_tmp_event
= realloc(tmp_event
,
3874 new_nbmem
* sizeof(struct lttng_event_field
));
3875 if (new_tmp_event
== NULL
) {
3878 PERROR("realloc ust app event fields");
3881 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3882 pthread_mutex_unlock(&app
->sock_lock
);
3884 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3885 release_ret
!= -EPIPE
) {
3886 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3890 /* Zero the new memory */
3891 memset(new_tmp_event
+ nbmem
, 0,
3892 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3894 tmp_event
= new_tmp_event
;
3897 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3898 /* Mapping between these enums matches 1 to 1. */
3899 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3900 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3902 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3903 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3904 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3905 tmp_event
[count
].event
.pid
= app
->pid
;
3906 tmp_event
[count
].event
.enabled
= -1;
3909 ret
= ustctl_release_handle(app
->sock
, handle
);
3910 pthread_mutex_unlock(&app
->sock_lock
);
3912 ret
!= -LTTNG_UST_ERR_EXITING
&&
3914 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3919 *fields
= tmp_event
;
3921 DBG2("UST app list event fields done (%zu events)", count
);
3926 health_code_update();
3931 * Free and clean all traceable apps of the global list.
3933 * Should _NOT_ be called with RCU read-side lock held.
3935 void ust_app_clean_list(void)
3938 struct ust_app
*app
;
3939 struct lttng_ht_iter iter
;
3941 DBG2("UST app cleaning registered apps hash table");
3945 /* Cleanup notify socket hash table */
3946 if (ust_app_ht_by_notify_sock
) {
3947 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3948 notify_sock_n
.node
) {
3949 struct cds_lfht_node
*node
;
3950 struct ust_app
*app
;
3952 node
= cds_lfht_iter_get_node(&iter
.iter
);
3957 app
= container_of(node
, struct ust_app
,
3958 notify_sock_n
.node
);
3959 ust_app_notify_sock_unregister(app
->notify_sock
);
3964 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3965 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3967 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3971 /* Cleanup socket hash table */
3972 if (ust_app_ht_by_sock
) {
3973 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3975 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3982 /* Destroy is done only when the ht is empty */
3984 ht_cleanup_push(ust_app_ht
);
3986 if (ust_app_ht_by_sock
) {
3987 ht_cleanup_push(ust_app_ht_by_sock
);
3989 if (ust_app_ht_by_notify_sock
) {
3990 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3995 * Init UST app hash table.
3997 int ust_app_ht_alloc(void)
3999 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4003 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4004 if (!ust_app_ht_by_sock
) {
4007 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4008 if (!ust_app_ht_by_notify_sock
) {
4015 * For a specific UST session, disable the channel for all registered apps.
4017 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4018 struct ltt_ust_channel
*uchan
)
4021 struct lttng_ht_iter iter
;
4022 struct lttng_ht_node_str
*ua_chan_node
;
4023 struct ust_app
*app
;
4024 struct ust_app_session
*ua_sess
;
4025 struct ust_app_channel
*ua_chan
;
4027 assert(usess
->active
);
4028 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4029 uchan
->name
, usess
->id
);
4033 /* For every registered applications */
4034 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4035 struct lttng_ht_iter uiter
;
4036 if (!app
->compatible
) {
4038 * TODO: In time, we should notice the caller of this error by
4039 * telling him that this is a version error.
4043 ua_sess
= lookup_session_by_app(usess
, app
);
4044 if (ua_sess
== NULL
) {
4049 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4050 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4051 /* If the session if found for the app, the channel must be there */
4052 assert(ua_chan_node
);
4054 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4055 /* The channel must not be already disabled */
4056 assert(ua_chan
->enabled
== 1);
4058 /* Disable channel onto application */
4059 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4061 /* XXX: We might want to report this error at some point... */
4071 * For a specific UST session, enable the channel for all registered apps.
4073 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4074 struct ltt_ust_channel
*uchan
)
4077 struct lttng_ht_iter iter
;
4078 struct ust_app
*app
;
4079 struct ust_app_session
*ua_sess
;
4081 assert(usess
->active
);
4082 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4083 uchan
->name
, usess
->id
);
4087 /* For every registered applications */
4088 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4089 if (!app
->compatible
) {
4091 * TODO: In time, we should notice the caller of this error by
4092 * telling him that this is a version error.
4096 ua_sess
= lookup_session_by_app(usess
, app
);
4097 if (ua_sess
== NULL
) {
4101 /* Enable channel onto application */
4102 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4104 /* XXX: We might want to report this error at some point... */
4114 * Disable an event in a channel and for a specific session.
4116 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4117 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4120 struct lttng_ht_iter iter
, uiter
;
4121 struct lttng_ht_node_str
*ua_chan_node
;
4122 struct ust_app
*app
;
4123 struct ust_app_session
*ua_sess
;
4124 struct ust_app_channel
*ua_chan
;
4125 struct ust_app_event
*ua_event
;
4127 assert(usess
->active
);
4128 DBG("UST app disabling event %s for all apps in channel "
4129 "%s for session id %" PRIu64
,
4130 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4134 /* For all registered applications */
4135 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4136 if (!app
->compatible
) {
4138 * TODO: In time, we should notice the caller of this error by
4139 * telling him that this is a version error.
4143 ua_sess
= lookup_session_by_app(usess
, app
);
4144 if (ua_sess
== NULL
) {
4149 /* Lookup channel in the ust app session */
4150 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4151 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4152 if (ua_chan_node
== NULL
) {
4153 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4154 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4157 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4159 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4160 uevent
->filter
, uevent
->attr
.loglevel
,
4162 if (ua_event
== NULL
) {
4163 DBG2("Event %s not found in channel %s for app pid %d."
4164 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4168 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4170 /* XXX: Report error someday... */
4179 /* The ua_sess lock must be held by the caller. */
4181 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4182 struct ust_app_session
*ua_sess
,
4183 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4184 struct ust_app_channel
**_ua_chan
)
4187 struct ust_app_channel
*ua_chan
= NULL
;
4190 ASSERT_LOCKED(ua_sess
->lock
);
4192 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4193 sizeof(uchan
->name
))) {
4194 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4198 struct ltt_ust_context
*uctx
= NULL
;
4201 * Create channel onto application and synchronize its
4204 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4205 LTTNG_UST_CHAN_PER_CPU
, usess
,
4211 ret
= ust_app_channel_send(app
, usess
,
4218 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4219 ret
= create_ust_app_channel_context(ua_chan
,
4232 * The application's socket is not valid. Either a bad socket
4233 * or a timeout on it. We can't inform the caller that for a
4234 * specific app, the session failed so lets continue here.
4236 ret
= 0; /* Not an error. */
4244 if (ret
== 0 && _ua_chan
) {
4246 * Only return the application's channel on success. Note
4247 * that the channel can still be part of the application's
4248 * channel hashtable on error.
4250 *_ua_chan
= ua_chan
;
4256 * For a specific UST session, create the channel for all registered apps.
4258 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
4259 struct ltt_ust_channel
*uchan
)
4262 struct cds_lfht_iter iter
;
4263 struct ust_app
*app
;
4266 assert(usess
->active
);
4269 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
4270 uchan
->name
, usess
->id
);
4273 /* For every registered applications */
4274 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
, app
, pid_n
.node
) {
4275 struct ust_app_session
*ua_sess
;
4276 int session_was_created
= 0;
4278 if (!app
->compatible
||
4279 !trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4280 goto error_rcu_unlock
;
4284 * Create session on the tracer side and add it to app session HT. Note
4285 * that if session exist, it will simply return a pointer to the ust
4288 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
,
4289 &session_was_created
);
4294 * The application's socket is not valid. Either a bad
4295 * socket or a timeout on it. We can't inform the caller
4296 * that for a specific app, the session failed so lets
4297 * continue here; it is not an error.
4300 goto error_rcu_unlock
;
4303 goto error_rcu_unlock
;
4307 if (ua_sess
->deleted
) {
4310 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, NULL
);
4312 if (session_was_created
) {
4313 destroy_app_session(app
, ua_sess
);
4315 /* Continue to the next application. */
4325 * Enable event for a specific session and channel on the tracer.
4327 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4328 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4331 struct lttng_ht_iter iter
, uiter
;
4332 struct lttng_ht_node_str
*ua_chan_node
;
4333 struct ust_app
*app
;
4334 struct ust_app_session
*ua_sess
;
4335 struct ust_app_channel
*ua_chan
;
4336 struct ust_app_event
*ua_event
;
4338 assert(usess
->active
);
4339 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4340 uevent
->attr
.name
, usess
->id
);
4343 * NOTE: At this point, this function is called only if the session and
4344 * channel passed are already created for all apps. and enabled on the
4350 /* For all registered applications */
4351 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4352 if (!app
->compatible
) {
4354 * TODO: In time, we should notice the caller of this error by
4355 * telling him that this is a version error.
4359 ua_sess
= lookup_session_by_app(usess
, app
);
4361 /* The application has problem or is probably dead. */
4365 pthread_mutex_lock(&ua_sess
->lock
);
4367 if (ua_sess
->deleted
) {
4368 pthread_mutex_unlock(&ua_sess
->lock
);
4372 /* Lookup channel in the ust app session */
4373 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4374 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4376 * It is possible that the channel cannot be found is
4377 * the channel/event creation occurs concurrently with
4378 * an application exit.
4380 if (!ua_chan_node
) {
4381 pthread_mutex_unlock(&ua_sess
->lock
);
4385 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4387 /* Get event node */
4388 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4389 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4390 if (ua_event
== NULL
) {
4391 DBG3("UST app enable event %s not found for app PID %d."
4392 "Skipping app", uevent
->attr
.name
, app
->pid
);
4396 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4398 pthread_mutex_unlock(&ua_sess
->lock
);
4402 pthread_mutex_unlock(&ua_sess
->lock
);
4411 * For a specific existing UST session and UST channel, creates the event for
4412 * all registered apps.
4414 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4415 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4418 struct lttng_ht_iter iter
, uiter
;
4419 struct lttng_ht_node_str
*ua_chan_node
;
4420 struct ust_app
*app
;
4421 struct ust_app_session
*ua_sess
;
4422 struct ust_app_channel
*ua_chan
;
4424 assert(usess
->active
);
4425 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4426 uevent
->attr
.name
, usess
->id
);
4430 /* For all registered applications */
4431 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4432 if (!app
->compatible
) {
4434 * TODO: In time, we should notice the caller of this error by
4435 * telling him that this is a version error.
4439 ua_sess
= lookup_session_by_app(usess
, app
);
4441 /* The application has problem or is probably dead. */
4445 pthread_mutex_lock(&ua_sess
->lock
);
4447 if (ua_sess
->deleted
) {
4448 pthread_mutex_unlock(&ua_sess
->lock
);
4452 /* Lookup channel in the ust app session */
4453 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4454 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4455 /* If the channel is not found, there is a code flow error */
4456 assert(ua_chan_node
);
4458 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4460 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4461 pthread_mutex_unlock(&ua_sess
->lock
);
4463 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4464 /* Possible value at this point: -ENOMEM. If so, we stop! */
4467 DBG2("UST app event %s already exist on app PID %d",
4468 uevent
->attr
.name
, app
->pid
);
4478 * Start tracing for a specific UST session and app.
4480 * Called with UST app session lock held.
4484 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4487 struct ust_app_session
*ua_sess
;
4489 DBG("Starting tracing for ust app pid %d", app
->pid
);
4493 if (!app
->compatible
) {
4497 ua_sess
= lookup_session_by_app(usess
, app
);
4498 if (ua_sess
== NULL
) {
4499 /* The session is in teardown process. Ignore and continue. */
4503 pthread_mutex_lock(&ua_sess
->lock
);
4505 if (ua_sess
->deleted
) {
4506 pthread_mutex_unlock(&ua_sess
->lock
);
4510 /* Upon restart, we skip the setup, already done */
4511 if (ua_sess
->started
) {
4515 health_code_update();
4518 /* This start the UST tracing */
4519 pthread_mutex_lock(&app
->sock_lock
);
4520 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4521 pthread_mutex_unlock(&app
->sock_lock
);
4523 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4524 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
4525 app
->pid
, app
->sock
);
4526 pthread_mutex_unlock(&ua_sess
->lock
);
4528 } else if (ret
== -EAGAIN
) {
4529 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
4530 app
->pid
, app
->sock
);
4531 pthread_mutex_unlock(&ua_sess
->lock
);
4535 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
4536 ret
, app
->pid
, app
->sock
);
4541 /* Indicate that the session has been started once */
4542 ua_sess
->started
= 1;
4544 pthread_mutex_unlock(&ua_sess
->lock
);
4546 health_code_update();
4548 /* Quiescent wait after starting trace */
4549 pthread_mutex_lock(&app
->sock_lock
);
4550 ret
= ustctl_wait_quiescent(app
->sock
);
4551 pthread_mutex_unlock(&app
->sock_lock
);
4553 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4554 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
4555 app
->pid
, app
->sock
);
4556 } else if (ret
== -EAGAIN
) {
4557 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
4558 app
->pid
, app
->sock
);
4560 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
4561 ret
, app
->pid
, app
->sock
);
4567 health_code_update();
4571 pthread_mutex_unlock(&ua_sess
->lock
);
4573 health_code_update();
4578 * Stop tracing for a specific UST session and app.
4581 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4584 struct ust_app_session
*ua_sess
;
4585 struct ust_registry_session
*registry
;
4587 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4591 if (!app
->compatible
) {
4592 goto end_no_session
;
4595 ua_sess
= lookup_session_by_app(usess
, app
);
4596 if (ua_sess
== NULL
) {
4597 goto end_no_session
;
4600 pthread_mutex_lock(&ua_sess
->lock
);
4602 if (ua_sess
->deleted
) {
4603 pthread_mutex_unlock(&ua_sess
->lock
);
4604 goto end_no_session
;
4608 * If started = 0, it means that stop trace has been called for a session
4609 * that was never started. It's possible since we can have a fail start
4610 * from either the application manager thread or the command thread. Simply
4611 * indicate that this is a stop error.
4613 if (!ua_sess
->started
) {
4614 goto error_rcu_unlock
;
4617 health_code_update();
4619 /* This inhibits UST tracing */
4620 pthread_mutex_lock(&app
->sock_lock
);
4621 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4622 pthread_mutex_unlock(&app
->sock_lock
);
4624 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4625 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
4626 app
->pid
, app
->sock
);
4628 } else if (ret
== -EAGAIN
) {
4629 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
4630 app
->pid
, app
->sock
);
4634 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
4635 ret
, app
->pid
, app
->sock
);
4637 goto error_rcu_unlock
;
4640 health_code_update();
4642 /* Quiescent wait after stopping trace */
4643 pthread_mutex_lock(&app
->sock_lock
);
4644 ret
= ustctl_wait_quiescent(app
->sock
);
4645 pthread_mutex_unlock(&app
->sock_lock
);
4647 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4648 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d)",
4649 app
->pid
, app
->sock
);
4650 } else if (ret
== -EAGAIN
) {
4651 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d)",
4652 app
->pid
, app
->sock
);
4654 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d)",
4655 ret
, app
->pid
, app
->sock
);
4659 health_code_update();
4661 registry
= get_session_registry(ua_sess
);
4663 /* The UST app session is held registry shall not be null. */
4666 /* Push metadata for application before freeing the application. */
4667 (void) push_metadata(registry
, ua_sess
->consumer
);
4670 pthread_mutex_unlock(&ua_sess
->lock
);
4673 health_code_update();
4677 pthread_mutex_unlock(&ua_sess
->lock
);
4679 health_code_update();
4684 int ust_app_flush_app_session(struct ust_app
*app
,
4685 struct ust_app_session
*ua_sess
)
4687 int ret
, retval
= 0;
4688 struct lttng_ht_iter iter
;
4689 struct ust_app_channel
*ua_chan
;
4690 struct consumer_socket
*socket
;
4692 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4696 if (!app
->compatible
) {
4697 goto end_not_compatible
;
4700 pthread_mutex_lock(&ua_sess
->lock
);
4702 if (ua_sess
->deleted
) {
4706 health_code_update();
4708 /* Flushing buffers */
4709 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4712 /* Flush buffers and push metadata. */
4713 switch (ua_sess
->buffer_type
) {
4714 case LTTNG_BUFFER_PER_PID
:
4715 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4717 health_code_update();
4718 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4720 ERR("Error flushing consumer channel");
4726 case LTTNG_BUFFER_PER_UID
:
4732 health_code_update();
4735 pthread_mutex_unlock(&ua_sess
->lock
);
4739 health_code_update();
4744 * Flush buffers for all applications for a specific UST session.
4745 * Called with UST session lock held.
4748 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4753 DBG("Flushing session buffers for all ust apps");
4757 /* Flush buffers and push metadata. */
4758 switch (usess
->buffer_type
) {
4759 case LTTNG_BUFFER_PER_UID
:
4761 struct buffer_reg_uid
*reg
;
4762 struct lttng_ht_iter iter
;
4764 /* Flush all per UID buffers associated to that session. */
4765 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4766 struct ust_registry_session
*ust_session_reg
;
4767 struct buffer_reg_channel
*reg_chan
;
4768 struct consumer_socket
*socket
;
4770 /* Get consumer socket to use to push the metadata.*/
4771 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4774 /* Ignore request if no consumer is found for the session. */
4778 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4779 reg_chan
, node
.node
) {
4781 * The following call will print error values so the return
4782 * code is of little importance because whatever happens, we
4783 * have to try them all.
4785 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4788 ust_session_reg
= reg
->registry
->reg
.ust
;
4789 /* Push metadata. */
4790 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4794 case LTTNG_BUFFER_PER_PID
:
4796 struct ust_app_session
*ua_sess
;
4797 struct lttng_ht_iter iter
;
4798 struct ust_app
*app
;
4800 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4801 ua_sess
= lookup_session_by_app(usess
, app
);
4802 if (ua_sess
== NULL
) {
4805 (void) ust_app_flush_app_session(app
, ua_sess
);
4816 health_code_update();
4821 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
4822 struct ust_app_session
*ua_sess
)
4825 struct lttng_ht_iter iter
;
4826 struct ust_app_channel
*ua_chan
;
4827 struct consumer_socket
*socket
;
4829 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
4833 if (!app
->compatible
) {
4834 goto end_not_compatible
;
4837 pthread_mutex_lock(&ua_sess
->lock
);
4839 if (ua_sess
->deleted
) {
4843 health_code_update();
4845 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4848 ERR("Failed to find consumer (%" PRIu32
") socket",
4849 app
->bits_per_long
);
4854 /* Clear quiescent state. */
4855 switch (ua_sess
->buffer_type
) {
4856 case LTTNG_BUFFER_PER_PID
:
4857 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
4858 ua_chan
, node
.node
) {
4859 health_code_update();
4860 ret
= consumer_clear_quiescent_channel(socket
,
4863 ERR("Error clearing quiescent state for consumer channel");
4869 case LTTNG_BUFFER_PER_UID
:
4876 health_code_update();
4879 pthread_mutex_unlock(&ua_sess
->lock
);
4883 health_code_update();
4888 * Clear quiescent state in each stream for all applications for a
4889 * specific UST session.
4890 * Called with UST session lock held.
4893 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
4898 DBG("Clearing stream quiescent state for all ust apps");
4902 switch (usess
->buffer_type
) {
4903 case LTTNG_BUFFER_PER_UID
:
4905 struct lttng_ht_iter iter
;
4906 struct buffer_reg_uid
*reg
;
4909 * Clear quiescent for all per UID buffers associated to
4912 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4913 struct consumer_socket
*socket
;
4914 struct buffer_reg_channel
*reg_chan
;
4916 /* Get associated consumer socket.*/
4917 socket
= consumer_find_socket_by_bitness(
4918 reg
->bits_per_long
, usess
->consumer
);
4921 * Ignore request if no consumer is found for
4927 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
4928 &iter
.iter
, reg_chan
, node
.node
) {
4930 * The following call will print error values so
4931 * the return code is of little importance
4932 * because whatever happens, we have to try them
4935 (void) consumer_clear_quiescent_channel(socket
,
4936 reg_chan
->consumer_key
);
4941 case LTTNG_BUFFER_PER_PID
:
4943 struct ust_app_session
*ua_sess
;
4944 struct lttng_ht_iter iter
;
4945 struct ust_app
*app
;
4947 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
4949 ua_sess
= lookup_session_by_app(usess
, app
);
4950 if (ua_sess
== NULL
) {
4953 (void) ust_app_clear_quiescent_app_session(app
,
4965 health_code_update();
4970 * Destroy a specific UST session in apps.
4972 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4975 struct ust_app_session
*ua_sess
;
4976 struct lttng_ht_iter iter
;
4977 struct lttng_ht_node_u64
*node
;
4979 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4983 if (!app
->compatible
) {
4987 __lookup_session_by_app(usess
, app
, &iter
);
4988 node
= lttng_ht_iter_get_node_u64(&iter
);
4990 /* Session is being or is deleted. */
4993 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4995 health_code_update();
4996 destroy_app_session(app
, ua_sess
);
4998 health_code_update();
5000 /* Quiescent wait after stopping trace */
5001 pthread_mutex_lock(&app
->sock_lock
);
5002 ret
= ustctl_wait_quiescent(app
->sock
);
5003 pthread_mutex_unlock(&app
->sock_lock
);
5005 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5006 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d)",
5007 app
->pid
, app
->sock
);
5008 } else if (ret
== -EAGAIN
) {
5009 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d)",
5010 app
->pid
, app
->sock
);
5012 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d)",
5013 ret
, app
->pid
, app
->sock
);
5018 health_code_update();
5023 * Start tracing for the UST session.
5025 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5027 struct lttng_ht_iter iter
;
5028 struct ust_app
*app
;
5030 DBG("Starting all UST traces");
5033 * Even though the start trace might fail, flag this session active so
5034 * other application coming in are started by default.
5041 * In a start-stop-start use-case, we need to clear the quiescent state
5042 * of each channel set by the prior stop command, thus ensuring that a
5043 * following stop or destroy is sure to grab a timestamp_end near those
5044 * operations, even if the packet is empty.
5046 (void) ust_app_clear_quiescent_session(usess
);
5048 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5049 ust_app_global_update(usess
, app
);
5058 * Start tracing for the UST session.
5059 * Called with UST session lock held.
5061 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5064 struct lttng_ht_iter iter
;
5065 struct ust_app
*app
;
5067 DBG("Stopping all UST traces");
5070 * Even though the stop trace might fail, flag this session inactive so
5071 * other application coming in are not started by default.
5077 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5078 ret
= ust_app_stop_trace(usess
, app
);
5080 /* Continue to next apps even on error */
5085 (void) ust_app_flush_session(usess
);
5093 * Destroy app UST session.
5095 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5098 struct lttng_ht_iter iter
;
5099 struct ust_app
*app
;
5101 DBG("Destroy all UST traces");
5105 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5106 ret
= destroy_trace(usess
, app
);
5108 /* Continue to next apps even on error */
5118 /* The ua_sess lock must be held by the caller. */
5120 int find_or_create_ust_app_channel(
5121 struct ltt_ust_session
*usess
,
5122 struct ust_app_session
*ua_sess
,
5123 struct ust_app
*app
,
5124 struct ltt_ust_channel
*uchan
,
5125 struct ust_app_channel
**ua_chan
)
5128 struct lttng_ht_iter iter
;
5129 struct lttng_ht_node_str
*ua_chan_node
;
5131 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5132 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5134 *ua_chan
= caa_container_of(ua_chan_node
,
5135 struct ust_app_channel
, node
);
5139 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5148 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5149 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5150 struct ust_app
*app
)
5153 struct ust_app_event
*ua_event
= NULL
;
5155 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5156 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5158 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5163 if (ua_event
->enabled
!= uevent
->enabled
) {
5164 ret
= uevent
->enabled
?
5165 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5166 disable_ust_app_event(ua_sess
, ua_event
, app
);
5175 * The caller must ensure that the application is compatible and is tracked
5176 * by the PID tracker.
5179 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5180 struct ust_app
*app
)
5183 struct cds_lfht_iter uchan_iter
;
5184 struct ltt_ust_channel
*uchan
;
5185 struct ust_app_session
*ua_sess
= NULL
;
5188 * The application's configuration should only be synchronized for
5191 assert(usess
->active
);
5193 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5195 /* Tracer is probably gone or ENOMEM. */
5200 pthread_mutex_lock(&ua_sess
->lock
);
5201 if (ua_sess
->deleted
) {
5202 pthread_mutex_unlock(&ua_sess
->lock
);
5208 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5210 struct ust_app_channel
*ua_chan
;
5211 struct cds_lfht_iter uevent_iter
;
5212 struct ltt_ust_event
*uevent
;
5215 * Search for a matching ust_app_channel. If none is found,
5216 * create it. Creating the channel will cause the ua_chan
5217 * structure to be allocated, the channel buffers to be
5218 * allocated (if necessary) and sent to the application, and
5219 * all enabled contexts will be added to the channel.
5221 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5222 app
, uchan
, &ua_chan
);
5224 /* Tracer is probably gone or ENOMEM. */
5229 /* ua_chan will be NULL for the metadata channel */
5233 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5235 ret
= ust_app_channel_synchronize_event(ua_chan
,
5236 uevent
, ua_sess
, app
);
5242 if (ua_chan
->enabled
!= uchan
->enabled
) {
5243 ret
= uchan
->enabled
?
5244 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5245 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5253 * Create the metadata for the application. This returns gracefully if a
5254 * metadata was already set for the session.
5256 * The metadata channel must be created after the data channels as the
5257 * consumer daemon assumes this ordering. When interacting with a relay
5258 * daemon, the consumer will use this assumption to send the
5259 * "STREAMS_SENT" message to the relay daemon.
5261 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5269 pthread_mutex_unlock(&ua_sess
->lock
);
5270 /* Everything went well at this point. */
5275 pthread_mutex_unlock(&ua_sess
->lock
);
5278 destroy_app_session(app
, ua_sess
);
5284 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5286 struct ust_app_session
*ua_sess
;
5288 ua_sess
= lookup_session_by_app(usess
, app
);
5289 if (ua_sess
== NULL
) {
5292 destroy_app_session(app
, ua_sess
);
5296 * Add channels/events from UST global domain to registered apps at sock.
5298 * Called with session lock held.
5299 * Called with RCU read-side lock held.
5301 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5304 assert(usess
->active
);
5306 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5307 app
->sock
, usess
->id
);
5309 if (!app
->compatible
) {
5312 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
5314 * Synchronize the application's internal tracing configuration
5315 * and start tracing.
5317 ust_app_synchronize(usess
, app
);
5318 ust_app_start_trace(usess
, app
);
5320 ust_app_global_destroy(usess
, app
);
5325 * Called with session lock held.
5327 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5329 struct lttng_ht_iter iter
;
5330 struct ust_app
*app
;
5333 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5334 ust_app_global_update(usess
, app
);
5340 * Add context to a specific channel for global UST domain.
5342 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5343 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5346 struct lttng_ht_node_str
*ua_chan_node
;
5347 struct lttng_ht_iter iter
, uiter
;
5348 struct ust_app_channel
*ua_chan
= NULL
;
5349 struct ust_app_session
*ua_sess
;
5350 struct ust_app
*app
;
5352 assert(usess
->active
);
5355 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5356 if (!app
->compatible
) {
5358 * TODO: In time, we should notice the caller of this error by
5359 * telling him that this is a version error.
5363 ua_sess
= lookup_session_by_app(usess
, app
);
5364 if (ua_sess
== NULL
) {
5368 pthread_mutex_lock(&ua_sess
->lock
);
5370 if (ua_sess
->deleted
) {
5371 pthread_mutex_unlock(&ua_sess
->lock
);
5375 /* Lookup channel in the ust app session */
5376 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5377 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5378 if (ua_chan_node
== NULL
) {
5381 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5383 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5388 pthread_mutex_unlock(&ua_sess
->lock
);
5396 * Receive registration and populate the given msg structure.
5398 * On success return 0 else a negative value returned by the ustctl call.
5400 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5403 uint32_t pid
, ppid
, uid
, gid
;
5407 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5408 &pid
, &ppid
, &uid
, &gid
,
5409 &msg
->bits_per_long
,
5410 &msg
->uint8_t_alignment
,
5411 &msg
->uint16_t_alignment
,
5412 &msg
->uint32_t_alignment
,
5413 &msg
->uint64_t_alignment
,
5414 &msg
->long_alignment
,
5421 case LTTNG_UST_ERR_EXITING
:
5422 DBG3("UST app recv reg message failed. Application died");
5424 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5425 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5426 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5427 LTTNG_UST_ABI_MINOR_VERSION
);
5430 ERR("UST app recv reg message failed with ret %d", ret
);
5435 msg
->pid
= (pid_t
) pid
;
5436 msg
->ppid
= (pid_t
) ppid
;
5437 msg
->uid
= (uid_t
) uid
;
5438 msg
->gid
= (gid_t
) gid
;
5445 * Return a ust app session object using the application object and the
5446 * session object descriptor has a key. If not found, NULL is returned.
5447 * A RCU read side lock MUST be acquired when calling this function.
5449 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5452 struct lttng_ht_node_ulong
*node
;
5453 struct lttng_ht_iter iter
;
5454 struct ust_app_session
*ua_sess
= NULL
;
5458 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5459 node
= lttng_ht_iter_get_node_ulong(&iter
);
5461 DBG2("UST app session find by objd %d not found", objd
);
5465 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5472 * Return a ust app channel object using the application object and the channel
5473 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5474 * lock MUST be acquired before calling this function.
5476 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5479 struct lttng_ht_node_ulong
*node
;
5480 struct lttng_ht_iter iter
;
5481 struct ust_app_channel
*ua_chan
= NULL
;
5485 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5486 node
= lttng_ht_iter_get_node_ulong(&iter
);
5488 DBG2("UST app channel find by objd %d not found", objd
);
5492 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5499 * Reply to a register channel notification from an application on the notify
5500 * socket. The channel metadata is also created.
5502 * The session UST registry lock is acquired in this function.
5504 * On success 0 is returned else a negative value.
5506 static int reply_ust_register_channel(int sock
, int cobjd
,
5507 size_t nr_fields
, struct ustctl_field
*fields
)
5509 int ret
, ret_code
= 0;
5511 uint64_t chan_reg_key
;
5512 enum ustctl_channel_header type
;
5513 struct ust_app
*app
;
5514 struct ust_app_channel
*ua_chan
;
5515 struct ust_app_session
*ua_sess
;
5516 struct ust_registry_session
*registry
;
5517 struct ust_registry_channel
*chan_reg
;
5521 /* Lookup application. If not found, there is a code flow error. */
5522 app
= find_app_by_notify_sock(sock
);
5524 DBG("Application socket %d is being torn down. Abort event notify",
5527 goto error_rcu_unlock
;
5530 /* Lookup channel by UST object descriptor. */
5531 ua_chan
= find_channel_by_objd(app
, cobjd
);
5533 DBG("Application channel is being torn down. Abort event notify");
5535 goto error_rcu_unlock
;
5538 assert(ua_chan
->session
);
5539 ua_sess
= ua_chan
->session
;
5541 /* Get right session registry depending on the session buffer type. */
5542 registry
= get_session_registry(ua_sess
);
5544 DBG("Application session is being torn down. Abort event notify");
5546 goto error_rcu_unlock
;
5549 /* Depending on the buffer type, a different channel key is used. */
5550 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5551 chan_reg_key
= ua_chan
->tracing_channel_id
;
5553 chan_reg_key
= ua_chan
->key
;
5556 pthread_mutex_lock(®istry
->lock
);
5558 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5561 if (!chan_reg
->register_done
) {
5563 * TODO: eventually use the registry event count for
5564 * this channel to better guess header type for per-pid
5567 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5568 chan_reg
->nr_ctx_fields
= nr_fields
;
5569 chan_reg
->ctx_fields
= fields
;
5571 chan_reg
->header_type
= type
;
5573 /* Get current already assigned values. */
5574 type
= chan_reg
->header_type
;
5576 /* Channel id is set during the object creation. */
5577 chan_id
= chan_reg
->chan_id
;
5579 /* Append to metadata */
5580 if (!chan_reg
->metadata_dumped
) {
5581 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5583 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5589 DBG3("UST app replying to register channel key %" PRIu64
5590 " with id %u, type = %d, ret = %d", chan_reg_key
, chan_id
, type
,
5593 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5595 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5596 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
5597 app
->pid
, app
->sock
);
5598 } else if (ret
== -EAGAIN
) {
5599 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
5600 app
->pid
, app
->sock
);
5602 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
5603 ret
, app
->pid
, app
->sock
);
5608 /* This channel registry registration is completed. */
5609 chan_reg
->register_done
= 1;
5612 pthread_mutex_unlock(®istry
->lock
);
5620 * Add event to the UST channel registry. When the event is added to the
5621 * registry, the metadata is also created. Once done, this replies to the
5622 * application with the appropriate error code.
5624 * The session UST registry lock is acquired in the function.
5626 * On success 0 is returned else a negative value.
5628 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5629 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5630 int loglevel_value
, char *model_emf_uri
)
5633 uint32_t event_id
= 0;
5634 uint64_t chan_reg_key
;
5635 struct ust_app
*app
;
5636 struct ust_app_channel
*ua_chan
;
5637 struct ust_app_session
*ua_sess
;
5638 struct ust_registry_session
*registry
;
5642 /* Lookup application. If not found, there is a code flow error. */
5643 app
= find_app_by_notify_sock(sock
);
5645 DBG("Application socket %d is being torn down. Abort event notify",
5648 goto error_rcu_unlock
;
5651 /* Lookup channel by UST object descriptor. */
5652 ua_chan
= find_channel_by_objd(app
, cobjd
);
5654 DBG("Application channel is being torn down. Abort event notify");
5656 goto error_rcu_unlock
;
5659 assert(ua_chan
->session
);
5660 ua_sess
= ua_chan
->session
;
5662 registry
= get_session_registry(ua_sess
);
5664 DBG("Application session is being torn down. Abort event notify");
5666 goto error_rcu_unlock
;
5669 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5670 chan_reg_key
= ua_chan
->tracing_channel_id
;
5672 chan_reg_key
= ua_chan
->key
;
5675 pthread_mutex_lock(®istry
->lock
);
5678 * From this point on, this call acquires the ownership of the sig, fields
5679 * and model_emf_uri meaning any free are done inside it if needed. These
5680 * three variables MUST NOT be read/write after this.
5682 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5683 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5684 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5688 model_emf_uri
= NULL
;
5691 * The return value is returned to ustctl so in case of an error, the
5692 * application can be notified. In case of an error, it's important not to
5693 * return a negative error or else the application will get closed.
5695 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5697 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5698 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
5699 app
->pid
, app
->sock
);
5700 } else if (ret
== -EAGAIN
) {
5701 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
5702 app
->pid
, app
->sock
);
5704 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
5705 ret
, app
->pid
, app
->sock
);
5708 * No need to wipe the create event since the application socket will
5709 * get close on error hence cleaning up everything by itself.
5714 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5718 pthread_mutex_unlock(®istry
->lock
);
5723 free(model_emf_uri
);
5728 * Add enum to the UST session registry. Once done, this replies to the
5729 * application with the appropriate error code.
5731 * The session UST registry lock is acquired within this function.
5733 * On success 0 is returned else a negative value.
5735 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
5736 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
5738 int ret
= 0, ret_code
;
5739 struct ust_app
*app
;
5740 struct ust_app_session
*ua_sess
;
5741 struct ust_registry_session
*registry
;
5742 uint64_t enum_id
= -1ULL;
5746 /* Lookup application. If not found, there is a code flow error. */
5747 app
= find_app_by_notify_sock(sock
);
5749 /* Return an error since this is not an error */
5750 DBG("Application socket %d is being torn down. Aborting enum registration",
5754 goto error_rcu_unlock
;
5757 /* Lookup session by UST object descriptor. */
5758 ua_sess
= find_session_by_objd(app
, sobjd
);
5760 /* Return an error since this is not an error */
5761 DBG("Application session is being torn down (session not found Aborting enum registration.");
5763 goto error_rcu_unlock
;
5766 registry
= get_session_registry(ua_sess
);
5768 DBG("Application session is being torn down (registry not found Aborting enum registration.");
5770 goto error_rcu_unlock
;
5773 pthread_mutex_lock(®istry
->lock
);
5776 * From this point on, the callee acquires the ownership of
5777 * entries. The variable entries MUST NOT be read/written after
5780 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
5781 entries
, nr_entries
, &enum_id
);
5785 * The return value is returned to ustctl so in case of an error, the
5786 * application can be notified. In case of an error, it's important not to
5787 * return a negative error or else the application will get closed.
5789 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
5791 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5792 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
5793 app
->pid
, app
->sock
);
5794 } else if (ret
== -EAGAIN
) {
5795 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
5796 app
->pid
, app
->sock
);
5798 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
5799 ret
, app
->pid
, app
->sock
);
5802 * No need to wipe the create enum since the application socket will
5803 * get close on error hence cleaning up everything by itself.
5808 DBG3("UST registry enum %s added successfully or already found", name
);
5811 pthread_mutex_unlock(®istry
->lock
);
5818 * Handle application notification through the given notify socket.
5820 * Return 0 on success or else a negative value.
5822 int ust_app_recv_notify(int sock
)
5825 enum ustctl_notify_cmd cmd
;
5827 DBG3("UST app receiving notify from sock %d", sock
);
5829 ret
= ustctl_recv_notify(sock
, &cmd
);
5831 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5832 DBG3("UST app recv notify failed. Application died: sock = %d",
5834 } else if (ret
== -EAGAIN
) {
5835 WARN("UST app recv notify failed. Communication time out: sock = %d",
5838 ERR("UST app recv notify failed with ret %d: sock = %d",
5845 case USTCTL_NOTIFY_CMD_EVENT
:
5847 int sobjd
, cobjd
, loglevel_value
;
5848 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5850 struct ustctl_field
*fields
;
5852 DBG2("UST app ustctl register event received");
5854 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5855 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5858 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5859 DBG3("UST app recv event failed. Application died: sock = %d",
5861 } else if (ret
== -EAGAIN
) {
5862 WARN("UST app recv event failed. Communication time out: sock = %d",
5865 ERR("UST app recv event failed with ret %d: sock = %d",
5872 * Add event to the UST registry coming from the notify socket. This
5873 * call will free if needed the sig, fields and model_emf_uri. This
5874 * code path loses the ownsership of these variables and transfer them
5875 * to the this function.
5877 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5878 fields
, loglevel_value
, model_emf_uri
);
5885 case USTCTL_NOTIFY_CMD_CHANNEL
:
5889 struct ustctl_field
*fields
;
5891 DBG2("UST app ustctl register channel received");
5893 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5896 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5897 DBG3("UST app recv channel failed. Application died: sock = %d",
5899 } else if (ret
== -EAGAIN
) {
5900 WARN("UST app recv channel failed. Communication time out: sock = %d",
5903 ERR("UST app recv channel failed with ret %d: sock = %d)",
5910 * The fields ownership are transfered to this function call meaning
5911 * that if needed it will be freed. After this, it's invalid to access
5912 * fields or clean it up.
5914 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
5922 case USTCTL_NOTIFY_CMD_ENUM
:
5925 char name
[LTTNG_UST_SYM_NAME_LEN
];
5927 struct ustctl_enum_entry
*entries
;
5929 DBG2("UST app ustctl register enum received");
5931 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
5932 &entries
, &nr_entries
);
5934 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5935 DBG3("UST app recv enum failed. Application died: sock = %d",
5937 } else if (ret
== -EAGAIN
) {
5938 WARN("UST app recv enum failed. Communication time out: sock = %d",
5941 ERR("UST app recv enum failed with ret %d: sock = %d",
5947 /* Callee assumes ownership of entries */
5948 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
5949 entries
, nr_entries
);
5957 /* Should NEVER happen. */
5966 * Once the notify socket hangs up, this is called. First, it tries to find the
5967 * corresponding application. On failure, the call_rcu to close the socket is
5968 * executed. If an application is found, it tries to delete it from the notify
5969 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5971 * Note that an object needs to be allocated here so on ENOMEM failure, the
5972 * call RCU is not done but the rest of the cleanup is.
5974 void ust_app_notify_sock_unregister(int sock
)
5977 struct lttng_ht_iter iter
;
5978 struct ust_app
*app
;
5979 struct ust_app_notify_sock_obj
*obj
;
5985 obj
= zmalloc(sizeof(*obj
));
5988 * An ENOMEM is kind of uncool. If this strikes we continue the
5989 * procedure but the call_rcu will not be called. In this case, we
5990 * accept the fd leak rather than possibly creating an unsynchronized
5991 * state between threads.
5993 * TODO: The notify object should be created once the notify socket is
5994 * registered and stored independantely from the ust app object. The
5995 * tricky part is to synchronize the teardown of the application and
5996 * this notify object. Let's keep that in mind so we can avoid this
5997 * kind of shenanigans with ENOMEM in the teardown path.
6004 DBG("UST app notify socket unregister %d", sock
);
6007 * Lookup application by notify socket. If this fails, this means that the
6008 * hash table delete has already been done by the application
6009 * unregistration process so we can safely close the notify socket in a
6012 app
= find_app_by_notify_sock(sock
);
6017 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6020 * Whatever happens here either we fail or succeed, in both cases we have
6021 * to close the socket after a grace period to continue to the call RCU
6022 * here. If the deletion is successful, the application is not visible
6023 * anymore by other threads and is it fails it means that it was already
6024 * deleted from the hash table so either way we just have to close the
6027 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6033 * Close socket after a grace period to avoid for the socket to be reused
6034 * before the application object is freed creating potential race between
6035 * threads trying to add unique in the global hash table.
6038 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6043 * Destroy a ust app data structure and free its memory.
6045 void ust_app_destroy(struct ust_app
*app
)
6051 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6055 * Take a snapshot for a given UST session. The snapshot is sent to the given
6058 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6060 enum lttng_error_code
ust_app_snapshot_record(
6061 const struct ltt_ust_session
*usess
,
6062 const struct consumer_output
*output
, int wait
,
6063 uint64_t nb_packets_per_stream
)
6066 enum lttng_error_code status
= LTTNG_OK
;
6067 struct lttng_ht_iter iter
;
6068 struct ust_app
*app
;
6069 char *trace_path
= NULL
;
6076 switch (usess
->buffer_type
) {
6077 case LTTNG_BUFFER_PER_UID
:
6079 struct buffer_reg_uid
*reg
;
6081 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6082 struct buffer_reg_channel
*reg_chan
;
6083 struct consumer_socket
*socket
;
6084 char pathname
[PATH_MAX
];
6086 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6087 /* Skip since no metadata is present */
6091 /* Get consumer socket to use to push the metadata.*/
6092 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6095 status
= LTTNG_ERR_INVALID
;
6099 memset(pathname
, 0, sizeof(pathname
));
6101 * DEFAULT_UST_TRACE_UID_PATH already contains a path
6104 ret
= snprintf(pathname
, sizeof(pathname
),
6105 DEFAULT_UST_TRACE_DIR DEFAULT_UST_TRACE_UID_PATH
,
6106 reg
->uid
, reg
->bits_per_long
);
6108 PERROR("snprintf snapshot path");
6109 status
= LTTNG_ERR_INVALID
;
6112 /* Free path allowed on previous iteration. */
6114 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
);
6116 status
= LTTNG_ERR_INVALID
;
6119 /* Add the UST default trace dir to path. */
6120 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6121 reg_chan
, node
.node
) {
6122 status
= consumer_snapshot_channel(socket
,
6123 reg_chan
->consumer_key
,
6124 output
, 0, usess
->uid
,
6125 usess
->gid
, trace_path
, wait
,
6126 nb_packets_per_stream
);
6127 if (status
!= LTTNG_OK
) {
6131 status
= consumer_snapshot_channel(socket
,
6132 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6133 usess
->uid
, usess
->gid
, trace_path
, wait
, 0);
6134 if (status
!= LTTNG_OK
) {
6140 case LTTNG_BUFFER_PER_PID
:
6142 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6143 struct consumer_socket
*socket
;
6144 struct lttng_ht_iter chan_iter
;
6145 struct ust_app_channel
*ua_chan
;
6146 struct ust_app_session
*ua_sess
;
6147 struct ust_registry_session
*registry
;
6148 char pathname
[PATH_MAX
];
6150 ua_sess
= lookup_session_by_app(usess
, app
);
6152 /* Session not associated with this app. */
6156 /* Get the right consumer socket for the application. */
6157 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6160 status
= LTTNG_ERR_INVALID
;
6164 /* Add the UST default trace dir to path. */
6165 memset(pathname
, 0, sizeof(pathname
));
6166 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"%s",
6169 status
= LTTNG_ERR_INVALID
;
6170 PERROR("snprintf snapshot path");
6173 /* Free path allowed on previous iteration. */
6175 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
);
6177 status
= LTTNG_ERR_INVALID
;
6180 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6181 ua_chan
, node
.node
) {
6182 status
= consumer_snapshot_channel(socket
,
6183 ua_chan
->key
, output
, 0,
6184 ua_sess
->effective_credentials
6186 ua_sess
->effective_credentials
6189 nb_packets_per_stream
);
6193 case LTTNG_ERR_CHAN_NOT_FOUND
:
6200 registry
= get_session_registry(ua_sess
);
6202 DBG("Application session is being torn down. Skip application.");
6205 status
= consumer_snapshot_channel(socket
,
6206 registry
->metadata_key
, output
, 1,
6207 ua_sess
->effective_credentials
.uid
,
6208 ua_sess
->effective_credentials
.gid
,
6209 trace_path
, wait
, 0);
6213 case LTTNG_ERR_CHAN_NOT_FOUND
:
6233 * Return the size taken by one more packet per stream.
6235 uint64_t ust_app_get_size_one_more_packet_per_stream(
6236 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6238 uint64_t tot_size
= 0;
6239 struct ust_app
*app
;
6240 struct lttng_ht_iter iter
;
6244 switch (usess
->buffer_type
) {
6245 case LTTNG_BUFFER_PER_UID
:
6247 struct buffer_reg_uid
*reg
;
6249 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6250 struct buffer_reg_channel
*reg_chan
;
6253 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6254 reg_chan
, node
.node
) {
6255 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6257 * Don't take channel into account if we
6258 * already grab all its packets.
6262 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6268 case LTTNG_BUFFER_PER_PID
:
6271 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6272 struct ust_app_channel
*ua_chan
;
6273 struct ust_app_session
*ua_sess
;
6274 struct lttng_ht_iter chan_iter
;
6276 ua_sess
= lookup_session_by_app(usess
, app
);
6278 /* Session not associated with this app. */
6282 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6283 ua_chan
, node
.node
) {
6284 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6286 * Don't take channel into account if we
6287 * already grab all its packets.
6291 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6305 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6306 struct cds_list_head
*buffer_reg_uid_list
,
6307 struct consumer_output
*consumer
, uint64_t uchan_id
,
6308 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6311 uint64_t consumer_chan_key
;
6316 ret
= buffer_reg_uid_consumer_channel_key(
6317 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6325 ret
= consumer_get_lost_packets(ust_session_id
,
6326 consumer_chan_key
, consumer
, lost
);
6328 ret
= consumer_get_discarded_events(ust_session_id
,
6329 consumer_chan_key
, consumer
, discarded
);
6336 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6337 struct ltt_ust_channel
*uchan
,
6338 struct consumer_output
*consumer
, int overwrite
,
6339 uint64_t *discarded
, uint64_t *lost
)
6342 struct lttng_ht_iter iter
;
6343 struct lttng_ht_node_str
*ua_chan_node
;
6344 struct ust_app
*app
;
6345 struct ust_app_session
*ua_sess
;
6346 struct ust_app_channel
*ua_chan
;
6353 * Iterate over every registered applications. Sum counters for
6354 * all applications containing requested session and channel.
6356 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6357 struct lttng_ht_iter uiter
;
6359 ua_sess
= lookup_session_by_app(usess
, app
);
6360 if (ua_sess
== NULL
) {
6365 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6366 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6367 /* If the session is found for the app, the channel must be there */
6368 assert(ua_chan_node
);
6370 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6375 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6382 uint64_t _discarded
;
6384 ret
= consumer_get_discarded_events(usess
->id
,
6385 ua_chan
->key
, consumer
, &_discarded
);
6389 (*discarded
) += _discarded
;
6398 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6399 struct ust_app
*app
)
6402 struct ust_app_session
*ua_sess
;
6404 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6408 ua_sess
= lookup_session_by_app(usess
, app
);
6409 if (ua_sess
== NULL
) {
6410 /* The session is in teardown process. Ignore and continue. */
6414 pthread_mutex_lock(&ua_sess
->lock
);
6416 if (ua_sess
->deleted
) {
6420 pthread_mutex_lock(&app
->sock_lock
);
6421 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6422 pthread_mutex_unlock(&app
->sock_lock
);
6425 pthread_mutex_unlock(&ua_sess
->lock
);
6429 health_code_update();
6434 * Regenerate the statedump for each app in the session.
6436 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6439 struct lttng_ht_iter iter
;
6440 struct ust_app
*app
;
6442 DBG("Regenerating the metadata for all UST apps");
6446 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6447 if (!app
->compatible
) {
6451 ret
= ust_app_regenerate_statedump(usess
, app
);
6453 /* Continue to the next app even on error */
6464 * Rotate all the channels of a session.
6466 * Return LTTNG_OK on success or else an LTTng error code.
6468 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
6471 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6472 struct lttng_ht_iter iter
;
6473 struct ust_app
*app
;
6474 struct ltt_ust_session
*usess
= session
->ust_session
;
6480 switch (usess
->buffer_type
) {
6481 case LTTNG_BUFFER_PER_UID
:
6483 struct buffer_reg_uid
*reg
;
6485 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6486 struct buffer_reg_channel
*reg_chan
;
6487 struct consumer_socket
*socket
;
6489 /* Get consumer socket to use to push the metadata.*/
6490 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6493 cmd_ret
= LTTNG_ERR_INVALID
;
6497 /* Rotate the data channels. */
6498 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6499 reg_chan
, node
.node
) {
6500 ret
= consumer_rotate_channel(socket
,
6501 reg_chan
->consumer_key
,
6502 usess
->uid
, usess
->gid
,
6504 /* is_metadata_channel */ false);
6506 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6512 * The metadata channel might not be present.
6514 * Consumer stream allocation can be done
6515 * asynchronously and can fail on intermediary
6516 * operations (i.e add context) and lead to data
6517 * channels created with no metadata channel.
6519 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6520 /* Skip since no metadata is present. */
6524 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6526 ret
= consumer_rotate_channel(socket
,
6527 reg
->registry
->reg
.ust
->metadata_key
,
6528 usess
->uid
, usess
->gid
,
6530 /* is_metadata_channel */ true);
6532 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6538 case LTTNG_BUFFER_PER_PID
:
6540 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6541 struct consumer_socket
*socket
;
6542 struct lttng_ht_iter chan_iter
;
6543 struct ust_app_channel
*ua_chan
;
6544 struct ust_app_session
*ua_sess
;
6545 struct ust_registry_session
*registry
;
6547 ua_sess
= lookup_session_by_app(usess
, app
);
6549 /* Session not associated with this app. */
6553 /* Get the right consumer socket for the application. */
6554 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6557 cmd_ret
= LTTNG_ERR_INVALID
;
6561 registry
= get_session_registry(ua_sess
);
6563 DBG("Application session is being torn down. Skip application.");
6567 /* Rotate the data channels. */
6568 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6569 ua_chan
, node
.node
) {
6570 ret
= consumer_rotate_channel(socket
,
6572 ua_sess
->effective_credentials
6574 ua_sess
->effective_credentials
6577 /* is_metadata_channel */ false);
6579 /* Per-PID buffer and application going away. */
6580 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6582 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6587 /* Rotate the metadata channel. */
6588 (void) push_metadata(registry
, usess
->consumer
);
6589 ret
= consumer_rotate_channel(socket
,
6590 registry
->metadata_key
,
6591 ua_sess
->effective_credentials
.uid
,
6592 ua_sess
->effective_credentials
.gid
,
6594 /* is_metadata_channel */ true);
6596 /* Per-PID buffer and application going away. */
6597 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6599 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6617 enum lttng_error_code
ust_app_create_channel_subdirectories(
6618 const struct ltt_ust_session
*usess
)
6620 enum lttng_error_code ret
= LTTNG_OK
;
6621 struct lttng_ht_iter iter
;
6622 enum lttng_trace_chunk_status chunk_status
;
6623 char *pathname_index
;
6626 assert(usess
->current_trace_chunk
);
6629 switch (usess
->buffer_type
) {
6630 case LTTNG_BUFFER_PER_UID
:
6632 struct buffer_reg_uid
*reg
;
6634 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6635 fmt_ret
= asprintf(&pathname_index
,
6636 DEFAULT_UST_TRACE_DIR DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
6637 reg
->uid
, reg
->bits_per_long
);
6639 ERR("Failed to format channel index directory");
6640 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6645 * Create the index subdirectory which will take care
6646 * of implicitly creating the channel's path.
6648 chunk_status
= lttng_trace_chunk_create_subdirectory(
6649 usess
->current_trace_chunk
,
6651 free(pathname_index
);
6652 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6653 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6659 case LTTNG_BUFFER_PER_PID
:
6661 struct ust_app
*app
;
6664 * Create the toplevel ust/ directory in case no apps are running.
6666 chunk_status
= lttng_trace_chunk_create_subdirectory(
6667 usess
->current_trace_chunk
,
6668 DEFAULT_UST_TRACE_DIR
);
6669 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6670 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6674 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
6676 struct ust_app_session
*ua_sess
;
6677 struct ust_registry_session
*registry
;
6679 ua_sess
= lookup_session_by_app(usess
, app
);
6681 /* Session not associated with this app. */
6685 registry
= get_session_registry(ua_sess
);
6687 DBG("Application session is being torn down. Skip application.");
6691 fmt_ret
= asprintf(&pathname_index
,
6692 DEFAULT_UST_TRACE_DIR
"%s/" DEFAULT_INDEX_DIR
,
6695 ERR("Failed to format channel index directory");
6696 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6700 * Create the index subdirectory which will take care
6701 * of implicitly creating the channel's path.
6703 chunk_status
= lttng_trace_chunk_create_subdirectory(
6704 usess
->current_trace_chunk
,
6706 free(pathname_index
);
6707 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6708 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;