2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
16 #include <sys/types.h>
18 #include <urcu/compiler.h>
21 #include <common/compat/errno.h>
22 #include <common/common.h>
23 #include <common/sessiond-comm/sessiond-comm.h>
25 #include "buffer-registry.h"
27 #include "health-sessiond.h"
29 #include "ust-consumer.h"
30 #include "lttng-ust-ctl.h"
31 #include "lttng-ust-error.h"
34 #include "lttng-sessiond.h"
35 #include "notification-thread-commands.h"
38 struct lttng_ht
*ust_app_ht
;
39 struct lttng_ht
*ust_app_ht_by_sock
;
40 struct lttng_ht
*ust_app_ht_by_notify_sock
;
43 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
45 /* Next available channel key. Access under next_channel_key_lock. */
46 static uint64_t _next_channel_key
;
47 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
49 /* Next available session ID. Access under next_session_id_lock. */
50 static uint64_t _next_session_id
;
51 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
54 * Return the incremented value of next_channel_key.
56 static uint64_t get_next_channel_key(void)
60 pthread_mutex_lock(&next_channel_key_lock
);
61 ret
= ++_next_channel_key
;
62 pthread_mutex_unlock(&next_channel_key_lock
);
67 * Return the atomically incremented value of next_session_id.
69 static uint64_t get_next_session_id(void)
73 pthread_mutex_lock(&next_session_id_lock
);
74 ret
= ++_next_session_id
;
75 pthread_mutex_unlock(&next_session_id_lock
);
79 static void copy_channel_attr_to_ustctl(
80 struct ustctl_consumer_channel_attr
*attr
,
81 struct lttng_ust_channel_attr
*uattr
)
83 /* Copy event attributes since the layout is different. */
84 attr
->subbuf_size
= uattr
->subbuf_size
;
85 attr
->num_subbuf
= uattr
->num_subbuf
;
86 attr
->overwrite
= uattr
->overwrite
;
87 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
88 attr
->read_timer_interval
= uattr
->read_timer_interval
;
89 attr
->output
= uattr
->output
;
90 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
94 * Match function for the hash table lookup.
96 * It matches an ust app event based on three attributes which are the event
97 * name, the filter bytecode and the loglevel.
99 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
101 struct ust_app_event
*event
;
102 const struct ust_app_ht_key
*key
;
103 int ev_loglevel_value
;
108 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
110 ev_loglevel_value
= event
->attr
.loglevel
;
112 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
115 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
119 /* Event loglevel. */
120 if (ev_loglevel_value
!= key
->loglevel_type
) {
121 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
122 && key
->loglevel_type
== 0 &&
123 ev_loglevel_value
== -1) {
125 * Match is accepted. This is because on event creation, the
126 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
127 * -1 are accepted for this loglevel type since 0 is the one set by
128 * the API when receiving an enable event.
135 /* One of the filters is NULL, fail. */
136 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
140 if (key
->filter
&& event
->filter
) {
141 /* Both filters exists, check length followed by the bytecode. */
142 if (event
->filter
->len
!= key
->filter
->len
||
143 memcmp(event
->filter
->data
, key
->filter
->data
,
144 event
->filter
->len
) != 0) {
149 /* One of the exclusions is NULL, fail. */
150 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
154 if (key
->exclusion
&& event
->exclusion
) {
155 /* Both exclusions exists, check count followed by the names. */
156 if (event
->exclusion
->count
!= key
->exclusion
->count
||
157 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
158 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
172 * Unique add of an ust app event in the given ht. This uses the custom
173 * ht_match_ust_app_event match function and the event name as hash.
175 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
176 struct ust_app_event
*event
)
178 struct cds_lfht_node
*node_ptr
;
179 struct ust_app_ht_key key
;
183 assert(ua_chan
->events
);
186 ht
= ua_chan
->events
;
187 key
.name
= event
->attr
.name
;
188 key
.filter
= event
->filter
;
189 key
.loglevel_type
= event
->attr
.loglevel
;
190 key
.exclusion
= event
->exclusion
;
192 node_ptr
= cds_lfht_add_unique(ht
->ht
,
193 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
194 ht_match_ust_app_event
, &key
, &event
->node
.node
);
195 assert(node_ptr
== &event
->node
.node
);
199 * Close the notify socket from the given RCU head object. This MUST be called
200 * through a call_rcu().
202 static void close_notify_sock_rcu(struct rcu_head
*head
)
205 struct ust_app_notify_sock_obj
*obj
=
206 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
208 /* Must have a valid fd here. */
209 assert(obj
->fd
>= 0);
211 ret
= close(obj
->fd
);
213 ERR("close notify sock %d RCU", obj
->fd
);
215 lttng_fd_put(LTTNG_FD_APPS
, 1);
221 * Return the session registry according to the buffer type of the given
224 * A registry per UID object MUST exists before calling this function or else
225 * it assert() if not found. RCU read side lock must be acquired.
227 static struct ust_registry_session
*get_session_registry(
228 struct ust_app_session
*ua_sess
)
230 struct ust_registry_session
*registry
= NULL
;
234 switch (ua_sess
->buffer_type
) {
235 case LTTNG_BUFFER_PER_PID
:
237 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
241 registry
= reg_pid
->registry
->reg
.ust
;
244 case LTTNG_BUFFER_PER_UID
:
246 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
247 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
248 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
252 registry
= reg_uid
->registry
->reg
.ust
;
264 * Delete ust context safely. RCU read lock must be held before calling
268 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
276 pthread_mutex_lock(&app
->sock_lock
);
277 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
278 pthread_mutex_unlock(&app
->sock_lock
);
279 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
280 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
281 sock
, ua_ctx
->obj
->handle
, ret
);
289 * Delete ust app event safely. RCU read lock must be held before calling
293 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
300 free(ua_event
->filter
);
301 if (ua_event
->exclusion
!= NULL
)
302 free(ua_event
->exclusion
);
303 if (ua_event
->obj
!= NULL
) {
304 pthread_mutex_lock(&app
->sock_lock
);
305 ret
= ustctl_release_object(sock
, ua_event
->obj
);
306 pthread_mutex_unlock(&app
->sock_lock
);
307 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
308 ERR("UST app sock %d release event obj failed with ret %d",
317 * Release ust data object of the given stream.
319 * Return 0 on success or else a negative value.
321 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
329 pthread_mutex_lock(&app
->sock_lock
);
330 ret
= ustctl_release_object(sock
, stream
->obj
);
331 pthread_mutex_unlock(&app
->sock_lock
);
332 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
333 ERR("UST app sock %d release stream obj failed with ret %d",
336 lttng_fd_put(LTTNG_FD_APPS
, 2);
344 * Delete ust app stream safely. RCU read lock must be held before calling
348 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
353 (void) release_ust_app_stream(sock
, stream
, app
);
358 * We need to execute ht_destroy outside of RCU read-side critical
359 * section and outside of call_rcu thread, so we postpone its execution
360 * using ht_cleanup_push. It is simpler than to change the semantic of
361 * the many callers of delete_ust_app_session().
364 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
366 struct ust_app_channel
*ua_chan
=
367 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
369 ht_cleanup_push(ua_chan
->ctx
);
370 ht_cleanup_push(ua_chan
->events
);
375 * Extract the lost packet or discarded events counter when the channel is
376 * being deleted and store the value in the parent channel so we can
377 * access it from lttng list and at stop/destroy.
379 * The session list lock must be held by the caller.
382 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
384 uint64_t discarded
= 0, lost
= 0;
385 struct ltt_session
*session
;
386 struct ltt_ust_channel
*uchan
;
388 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
393 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
394 if (!session
|| !session
->ust_session
) {
396 * Not finding the session is not an error because there are
397 * multiple ways the channels can be torn down.
399 * 1) The session daemon can initiate the destruction of the
400 * ust app session after receiving a destroy command or
401 * during its shutdown/teardown.
402 * 2) The application, since we are in per-pid tracing, is
403 * unregistering and tearing down its ust app session.
405 * Both paths are protected by the session list lock which
406 * ensures that the accounting of lost packets and discarded
407 * events is done exactly once. The session is then unpublished
408 * from the session list, resulting in this condition.
413 if (ua_chan
->attr
.overwrite
) {
414 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
415 ua_chan
->key
, session
->ust_session
->consumer
,
418 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
419 ua_chan
->key
, session
->ust_session
->consumer
,
422 uchan
= trace_ust_find_channel_by_name(
423 session
->ust_session
->domain_global
.channels
,
426 ERR("Missing UST channel to store discarded counters");
430 uchan
->per_pid_closed_app_discarded
+= discarded
;
431 uchan
->per_pid_closed_app_lost
+= lost
;
436 session_put(session
);
441 * Delete ust app channel safely. RCU read lock must be held before calling
444 * The session list lock must be held by the caller.
447 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
451 struct lttng_ht_iter iter
;
452 struct ust_app_event
*ua_event
;
453 struct ust_app_ctx
*ua_ctx
;
454 struct ust_app_stream
*stream
, *stmp
;
455 struct ust_registry_session
*registry
;
459 DBG3("UST app deleting channel %s", ua_chan
->name
);
462 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
463 cds_list_del(&stream
->list
);
464 delete_ust_app_stream(sock
, stream
, app
);
468 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
469 cds_list_del(&ua_ctx
->list
);
470 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
472 delete_ust_app_ctx(sock
, ua_ctx
, app
);
476 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
478 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
480 delete_ust_app_event(sock
, ua_event
, app
);
483 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
484 /* Wipe and free registry from session registry. */
485 registry
= get_session_registry(ua_chan
->session
);
487 ust_registry_channel_del_free(registry
, ua_chan
->key
,
491 * A negative socket can be used by the caller when
492 * cleaning-up a ua_chan in an error path. Skip the
493 * accounting in this case.
496 save_per_pid_lost_discarded_counters(ua_chan
);
500 if (ua_chan
->obj
!= NULL
) {
501 /* Remove channel from application UST object descriptor. */
502 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
503 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
505 pthread_mutex_lock(&app
->sock_lock
);
506 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
507 pthread_mutex_unlock(&app
->sock_lock
);
508 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
509 ERR("UST app sock %d release channel obj failed with ret %d",
512 lttng_fd_put(LTTNG_FD_APPS
, 1);
515 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
518 int ust_app_register_done(struct ust_app
*app
)
522 pthread_mutex_lock(&app
->sock_lock
);
523 ret
= ustctl_register_done(app
->sock
);
524 pthread_mutex_unlock(&app
->sock_lock
);
528 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
533 pthread_mutex_lock(&app
->sock_lock
);
538 ret
= ustctl_release_object(sock
, data
);
540 pthread_mutex_unlock(&app
->sock_lock
);
546 * Push metadata to consumer socket.
548 * RCU read-side lock must be held to guarantee existance of socket.
549 * Must be called with the ust app session lock held.
550 * Must be called with the registry lock held.
552 * On success, return the len of metadata pushed or else a negative value.
553 * Returning a -EPIPE return value means we could not send the metadata,
554 * but it can be caused by recoverable errors (e.g. the application has
555 * terminated concurrently).
557 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
558 struct consumer_socket
*socket
, int send_zero_data
)
561 char *metadata_str
= NULL
;
562 size_t len
, offset
, new_metadata_len_sent
;
564 uint64_t metadata_key
, metadata_version
;
569 metadata_key
= registry
->metadata_key
;
572 * Means that no metadata was assigned to the session. This can
573 * happens if no start has been done previously.
579 offset
= registry
->metadata_len_sent
;
580 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
581 new_metadata_len_sent
= registry
->metadata_len
;
582 metadata_version
= registry
->metadata_version
;
584 DBG3("No metadata to push for metadata key %" PRIu64
,
585 registry
->metadata_key
);
587 if (send_zero_data
) {
588 DBG("No metadata to push");
594 /* Allocate only what we have to send. */
595 metadata_str
= zmalloc(len
);
597 PERROR("zmalloc ust app metadata string");
601 /* Copy what we haven't sent out. */
602 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
605 pthread_mutex_unlock(®istry
->lock
);
607 * We need to unlock the registry while we push metadata to
608 * break a circular dependency between the consumerd metadata
609 * lock and the sessiond registry lock. Indeed, pushing metadata
610 * to the consumerd awaits that it gets pushed all the way to
611 * relayd, but doing so requires grabbing the metadata lock. If
612 * a concurrent metadata request is being performed by
613 * consumerd, this can try to grab the registry lock on the
614 * sessiond while holding the metadata lock on the consumer
615 * daemon. Those push and pull schemes are performed on two
616 * different bidirectionnal communication sockets.
618 ret
= consumer_push_metadata(socket
, metadata_key
,
619 metadata_str
, len
, offset
, metadata_version
);
620 pthread_mutex_lock(®istry
->lock
);
623 * There is an acceptable race here between the registry
624 * metadata key assignment and the creation on the
625 * consumer. The session daemon can concurrently push
626 * metadata for this registry while being created on the
627 * consumer since the metadata key of the registry is
628 * assigned *before* it is setup to avoid the consumer
629 * to ask for metadata that could possibly be not found
630 * in the session daemon.
632 * The metadata will get pushed either by the session
633 * being stopped or the consumer requesting metadata if
634 * that race is triggered.
636 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
639 ERR("Error pushing metadata to consumer");
645 * Metadata may have been concurrently pushed, since
646 * we're not holding the registry lock while pushing to
647 * consumer. This is handled by the fact that we send
648 * the metadata content, size, and the offset at which
649 * that metadata belongs. This may arrive out of order
650 * on the consumer side, and the consumer is able to
651 * deal with overlapping fragments. The consumer
652 * supports overlapping fragments, which must be
653 * contiguous starting from offset 0. We keep the
654 * largest metadata_len_sent value of the concurrent
657 registry
->metadata_len_sent
=
658 max_t(size_t, registry
->metadata_len_sent
,
659 new_metadata_len_sent
);
668 * On error, flag the registry that the metadata is
669 * closed. We were unable to push anything and this
670 * means that either the consumer is not responding or
671 * the metadata cache has been destroyed on the
674 registry
->metadata_closed
= 1;
682 * For a given application and session, push metadata to consumer.
683 * Either sock or consumer is required : if sock is NULL, the default
684 * socket to send the metadata is retrieved from consumer, if sock
685 * is not NULL we use it to send the metadata.
686 * RCU read-side lock must be held while calling this function,
687 * therefore ensuring existance of registry. It also ensures existance
688 * of socket throughout this function.
690 * Return 0 on success else a negative error.
691 * Returning a -EPIPE return value means we could not send the metadata,
692 * but it can be caused by recoverable errors (e.g. the application has
693 * terminated concurrently).
695 static int push_metadata(struct ust_registry_session
*registry
,
696 struct consumer_output
*consumer
)
700 struct consumer_socket
*socket
;
705 pthread_mutex_lock(®istry
->lock
);
706 if (registry
->metadata_closed
) {
711 /* Get consumer socket to use to push the metadata.*/
712 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
719 ret
= ust_app_push_metadata(registry
, socket
, 0);
724 pthread_mutex_unlock(®istry
->lock
);
728 pthread_mutex_unlock(®istry
->lock
);
733 * Send to the consumer a close metadata command for the given session. Once
734 * done, the metadata channel is deleted and the session metadata pointer is
735 * nullified. The session lock MUST be held unless the application is
736 * in the destroy path.
738 * Do not hold the registry lock while communicating with the consumerd, because
739 * doing so causes inter-process deadlocks between consumerd and sessiond with
740 * the metadata request notification.
742 * Return 0 on success else a negative value.
744 static int close_metadata(struct ust_registry_session
*registry
,
745 struct consumer_output
*consumer
)
748 struct consumer_socket
*socket
;
749 uint64_t metadata_key
;
750 bool registry_was_already_closed
;
757 pthread_mutex_lock(®istry
->lock
);
758 metadata_key
= registry
->metadata_key
;
759 registry_was_already_closed
= registry
->metadata_closed
;
760 if (metadata_key
!= 0) {
762 * Metadata closed. Even on error this means that the consumer
763 * is not responding or not found so either way a second close
764 * should NOT be emit for this registry.
766 registry
->metadata_closed
= 1;
768 pthread_mutex_unlock(®istry
->lock
);
770 if (metadata_key
== 0 || registry_was_already_closed
) {
775 /* Get consumer socket to use to push the metadata.*/
776 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
783 ret
= consumer_close_metadata(socket
, metadata_key
);
794 * We need to execute ht_destroy outside of RCU read-side critical
795 * section and outside of call_rcu thread, so we postpone its execution
796 * using ht_cleanup_push. It is simpler than to change the semantic of
797 * the many callers of delete_ust_app_session().
800 void delete_ust_app_session_rcu(struct rcu_head
*head
)
802 struct ust_app_session
*ua_sess
=
803 caa_container_of(head
, struct ust_app_session
, rcu_head
);
805 ht_cleanup_push(ua_sess
->channels
);
810 * Delete ust app session safely. RCU read lock must be held before calling
813 * The session list lock must be held by the caller.
816 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
820 struct lttng_ht_iter iter
;
821 struct ust_app_channel
*ua_chan
;
822 struct ust_registry_session
*registry
;
826 pthread_mutex_lock(&ua_sess
->lock
);
828 assert(!ua_sess
->deleted
);
829 ua_sess
->deleted
= true;
831 registry
= get_session_registry(ua_sess
);
832 /* Registry can be null on error path during initialization. */
834 /* Push metadata for application before freeing the application. */
835 (void) push_metadata(registry
, ua_sess
->consumer
);
838 * Don't ask to close metadata for global per UID buffers. Close
839 * metadata only on destroy trace session in this case. Also, the
840 * previous push metadata could have flag the metadata registry to
841 * close so don't send a close command if closed.
843 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
844 /* And ask to close it for this session registry. */
845 (void) close_metadata(registry
, ua_sess
->consumer
);
849 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
851 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
853 delete_ust_app_channel(sock
, ua_chan
, app
);
856 /* In case of per PID, the registry is kept in the session. */
857 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
858 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
861 * Registry can be null on error path during
864 buffer_reg_pid_remove(reg_pid
);
865 buffer_reg_pid_destroy(reg_pid
);
869 if (ua_sess
->handle
!= -1) {
870 pthread_mutex_lock(&app
->sock_lock
);
871 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
872 pthread_mutex_unlock(&app
->sock_lock
);
873 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
874 ERR("UST app sock %d release session handle failed with ret %d",
877 /* Remove session from application UST object descriptor. */
878 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
879 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
883 pthread_mutex_unlock(&ua_sess
->lock
);
885 consumer_output_put(ua_sess
->consumer
);
887 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
891 * Delete a traceable application structure from the global list. Never call
892 * this function outside of a call_rcu call.
894 * RCU read side lock should _NOT_ be held when calling this function.
897 void delete_ust_app(struct ust_app
*app
)
900 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
903 * The session list lock must be held during this function to guarantee
904 * the existence of ua_sess.
907 /* Delete ust app sessions info */
912 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
914 /* Free every object in the session and the session. */
916 delete_ust_app_session(sock
, ua_sess
, app
);
920 ht_cleanup_push(app
->sessions
);
921 ht_cleanup_push(app
->ust_sessions_objd
);
922 ht_cleanup_push(app
->ust_objd
);
925 * Wait until we have deleted the application from the sock hash table
926 * before closing this socket, otherwise an application could re-use the
927 * socket ID and race with the teardown, using the same hash table entry.
929 * It's OK to leave the close in call_rcu. We want it to stay unique for
930 * all RCU readers that could run concurrently with unregister app,
931 * therefore we _need_ to only close that socket after a grace period. So
932 * it should stay in this RCU callback.
934 * This close() is a very important step of the synchronization model so
935 * every modification to this function must be carefully reviewed.
941 lttng_fd_put(LTTNG_FD_APPS
, 1);
943 DBG2("UST app pid %d deleted", app
->pid
);
945 session_unlock_list();
949 * URCU intermediate call to delete an UST app.
952 void delete_ust_app_rcu(struct rcu_head
*head
)
954 struct lttng_ht_node_ulong
*node
=
955 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
956 struct ust_app
*app
=
957 caa_container_of(node
, struct ust_app
, pid_n
);
959 DBG3("Call RCU deleting app PID %d", app
->pid
);
964 * Delete the session from the application ht and delete the data structure by
965 * freeing every object inside and releasing them.
967 * The session list lock must be held by the caller.
969 static void destroy_app_session(struct ust_app
*app
,
970 struct ust_app_session
*ua_sess
)
973 struct lttng_ht_iter iter
;
978 iter
.iter
.node
= &ua_sess
->node
.node
;
979 ret
= lttng_ht_del(app
->sessions
, &iter
);
981 /* Already scheduled for teardown. */
985 /* Once deleted, free the data structure. */
986 delete_ust_app_session(app
->sock
, ua_sess
, app
);
993 * Alloc new UST app session.
996 struct ust_app_session
*alloc_ust_app_session(void)
998 struct ust_app_session
*ua_sess
;
1000 /* Init most of the default value by allocating and zeroing */
1001 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1002 if (ua_sess
== NULL
) {
1007 ua_sess
->handle
= -1;
1008 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1009 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1010 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1019 * Alloc new UST app channel.
1022 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1023 struct ust_app_session
*ua_sess
,
1024 struct lttng_ust_channel_attr
*attr
)
1026 struct ust_app_channel
*ua_chan
;
1028 /* Init most of the default value by allocating and zeroing */
1029 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1030 if (ua_chan
== NULL
) {
1035 /* Setup channel name */
1036 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1037 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1039 ua_chan
->enabled
= 1;
1040 ua_chan
->handle
= -1;
1041 ua_chan
->session
= ua_sess
;
1042 ua_chan
->key
= get_next_channel_key();
1043 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1044 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1045 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1047 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1048 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1050 /* Copy attributes */
1052 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1053 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1054 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1055 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1056 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1057 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1058 ua_chan
->attr
.output
= attr
->output
;
1059 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1061 /* By default, the channel is a per cpu channel. */
1062 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1064 DBG3("UST app channel %s allocated", ua_chan
->name
);
1073 * Allocate and initialize a UST app stream.
1075 * Return newly allocated stream pointer or NULL on error.
1077 struct ust_app_stream
*ust_app_alloc_stream(void)
1079 struct ust_app_stream
*stream
= NULL
;
1081 stream
= zmalloc(sizeof(*stream
));
1082 if (stream
== NULL
) {
1083 PERROR("zmalloc ust app stream");
1087 /* Zero could be a valid value for a handle so flag it to -1. */
1088 stream
->handle
= -1;
1095 * Alloc new UST app event.
1098 struct ust_app_event
*alloc_ust_app_event(char *name
,
1099 struct lttng_ust_event
*attr
)
1101 struct ust_app_event
*ua_event
;
1103 /* Init most of the default value by allocating and zeroing */
1104 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1105 if (ua_event
== NULL
) {
1106 PERROR("Failed to allocate ust_app_event structure");
1110 ua_event
->enabled
= 1;
1111 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1112 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1113 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1115 /* Copy attributes */
1117 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1120 DBG3("UST app event %s allocated", ua_event
->name
);
1129 * Alloc new UST app context.
1132 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1134 struct ust_app_ctx
*ua_ctx
;
1136 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1137 if (ua_ctx
== NULL
) {
1141 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1144 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1145 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1146 char *provider_name
= NULL
, *ctx_name
= NULL
;
1148 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1149 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1150 if (!provider_name
|| !ctx_name
) {
1151 free(provider_name
);
1156 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1157 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1161 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1169 * Allocate a filter and copy the given original filter.
1171 * Return allocated filter or NULL on error.
1173 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1174 struct lttng_filter_bytecode
*orig_f
)
1176 struct lttng_filter_bytecode
*filter
= NULL
;
1178 /* Copy filter bytecode */
1179 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1181 PERROR("zmalloc alloc filter bytecode");
1185 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1192 * Create a liblttng-ust filter bytecode from given bytecode.
1194 * Return allocated filter or NULL on error.
1196 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1197 struct lttng_filter_bytecode
*orig_f
)
1199 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1201 /* Copy filter bytecode */
1202 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1204 PERROR("zmalloc alloc ust filter bytecode");
1208 assert(sizeof(struct lttng_filter_bytecode
) ==
1209 sizeof(struct lttng_ust_filter_bytecode
));
1210 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1216 * Find an ust_app using the sock and return it. RCU read side lock must be
1217 * held before calling this helper function.
1219 struct ust_app
*ust_app_find_by_sock(int sock
)
1221 struct lttng_ht_node_ulong
*node
;
1222 struct lttng_ht_iter iter
;
1224 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1225 node
= lttng_ht_iter_get_node_ulong(&iter
);
1227 DBG2("UST app find by sock %d not found", sock
);
1231 return caa_container_of(node
, struct ust_app
, sock_n
);
1238 * Find an ust_app using the notify sock and return it. RCU read side lock must
1239 * be held before calling this helper function.
1241 static struct ust_app
*find_app_by_notify_sock(int sock
)
1243 struct lttng_ht_node_ulong
*node
;
1244 struct lttng_ht_iter iter
;
1246 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1248 node
= lttng_ht_iter_get_node_ulong(&iter
);
1250 DBG2("UST app find by notify sock %d not found", sock
);
1254 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1261 * Lookup for an ust app event based on event name, filter bytecode and the
1264 * Return an ust_app_event object or NULL on error.
1266 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1267 const char *name
, const struct lttng_filter_bytecode
*filter
,
1269 const struct lttng_event_exclusion
*exclusion
)
1271 struct lttng_ht_iter iter
;
1272 struct lttng_ht_node_str
*node
;
1273 struct ust_app_event
*event
= NULL
;
1274 struct ust_app_ht_key key
;
1279 /* Setup key for event lookup. */
1281 key
.filter
= filter
;
1282 key
.loglevel_type
= loglevel_value
;
1283 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1284 key
.exclusion
= exclusion
;
1286 /* Lookup using the event name as hash and a custom match fct. */
1287 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1288 ht_match_ust_app_event
, &key
, &iter
.iter
);
1289 node
= lttng_ht_iter_get_node_str(&iter
);
1294 event
= caa_container_of(node
, struct ust_app_event
, node
);
1301 * Create the channel context on the tracer.
1303 * Called with UST app session lock held.
1306 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1307 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1311 health_code_update();
1313 pthread_mutex_lock(&app
->sock_lock
);
1314 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1315 ua_chan
->obj
, &ua_ctx
->obj
);
1316 pthread_mutex_unlock(&app
->sock_lock
);
1318 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1319 ERR("UST app create channel context failed for app (pid: %d) "
1320 "with ret %d", app
->pid
, ret
);
1323 * This is normal behavior, an application can die during the
1324 * creation process. Don't report an error so the execution can
1325 * continue normally.
1328 DBG3("UST app add context failed. Application is dead.");
1333 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1335 DBG2("UST app context handle %d created successfully for channel %s",
1336 ua_ctx
->handle
, ua_chan
->name
);
1339 health_code_update();
1344 * Set the filter on the tracer.
1347 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1348 struct ust_app
*app
)
1351 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1353 health_code_update();
1355 if (!ua_event
->filter
) {
1360 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1361 if (!ust_bytecode
) {
1362 ret
= -LTTNG_ERR_NOMEM
;
1365 pthread_mutex_lock(&app
->sock_lock
);
1366 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1368 pthread_mutex_unlock(&app
->sock_lock
);
1370 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1371 ERR("UST app event %s filter failed for app (pid: %d) "
1372 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1375 * This is normal behavior, an application can die during the
1376 * creation process. Don't report an error so the execution can
1377 * continue normally.
1380 DBG3("UST app filter event failed. Application is dead.");
1385 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1388 health_code_update();
1394 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1395 struct lttng_event_exclusion
*exclusion
)
1397 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1398 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1399 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1401 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1402 if (!ust_exclusion
) {
1407 assert(sizeof(struct lttng_event_exclusion
) ==
1408 sizeof(struct lttng_ust_event_exclusion
));
1409 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1411 return ust_exclusion
;
1415 * Set event exclusions on the tracer.
1418 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1419 struct ust_app
*app
)
1422 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1424 health_code_update();
1426 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1431 ust_exclusion
= create_ust_exclusion_from_exclusion(
1432 ua_event
->exclusion
);
1433 if (!ust_exclusion
) {
1434 ret
= -LTTNG_ERR_NOMEM
;
1437 pthread_mutex_lock(&app
->sock_lock
);
1438 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1439 pthread_mutex_unlock(&app
->sock_lock
);
1441 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1442 ERR("UST app event %s exclusions failed for app (pid: %d) "
1443 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1446 * This is normal behavior, an application can die during the
1447 * creation process. Don't report an error so the execution can
1448 * continue normally.
1451 DBG3("UST app event exclusion failed. Application is dead.");
1456 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1459 health_code_update();
1460 free(ust_exclusion
);
1465 * Disable the specified event on to UST tracer for the UST session.
1467 static int disable_ust_event(struct ust_app
*app
,
1468 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1472 health_code_update();
1474 pthread_mutex_lock(&app
->sock_lock
);
1475 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1476 pthread_mutex_unlock(&app
->sock_lock
);
1478 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1479 ERR("UST app event %s disable failed for app (pid: %d) "
1480 "and session handle %d with ret %d",
1481 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1484 * This is normal behavior, an application can die during the
1485 * creation process. Don't report an error so the execution can
1486 * continue normally.
1489 DBG3("UST app disable event failed. Application is dead.");
1494 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1495 ua_event
->attr
.name
, app
->pid
);
1498 health_code_update();
1503 * Disable the specified channel on to UST tracer for the UST session.
1505 static int disable_ust_channel(struct ust_app
*app
,
1506 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1510 health_code_update();
1512 pthread_mutex_lock(&app
->sock_lock
);
1513 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1514 pthread_mutex_unlock(&app
->sock_lock
);
1516 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1517 ERR("UST app channel %s disable failed for app (pid: %d) "
1518 "and session handle %d with ret %d",
1519 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1522 * This is normal behavior, an application can die during the
1523 * creation process. Don't report an error so the execution can
1524 * continue normally.
1527 DBG3("UST app disable channel failed. Application is dead.");
1532 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1533 ua_chan
->name
, app
->pid
);
1536 health_code_update();
1541 * Enable the specified channel on to UST tracer for the UST session.
1543 static int enable_ust_channel(struct ust_app
*app
,
1544 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1548 health_code_update();
1550 pthread_mutex_lock(&app
->sock_lock
);
1551 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1552 pthread_mutex_unlock(&app
->sock_lock
);
1554 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1555 ERR("UST app channel %s enable failed for app (pid: %d) "
1556 "and session handle %d with ret %d",
1557 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1560 * This is normal behavior, an application can die during the
1561 * creation process. Don't report an error so the execution can
1562 * continue normally.
1565 DBG3("UST app enable channel failed. Application is dead.");
1570 ua_chan
->enabled
= 1;
1572 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1573 ua_chan
->name
, app
->pid
);
1576 health_code_update();
1581 * Enable the specified event on to UST tracer for the UST session.
1583 static int enable_ust_event(struct ust_app
*app
,
1584 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1588 health_code_update();
1590 pthread_mutex_lock(&app
->sock_lock
);
1591 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1592 pthread_mutex_unlock(&app
->sock_lock
);
1594 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1595 ERR("UST app event %s enable failed for app (pid: %d) "
1596 "and session handle %d with ret %d",
1597 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1600 * This is normal behavior, an application can die during the
1601 * creation process. Don't report an error so the execution can
1602 * continue normally.
1605 DBG3("UST app enable event failed. Application is dead.");
1610 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1611 ua_event
->attr
.name
, app
->pid
);
1614 health_code_update();
1619 * Send channel and stream buffer to application.
1621 * Return 0 on success. On error, a negative value is returned.
1623 static int send_channel_pid_to_ust(struct ust_app
*app
,
1624 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1627 struct ust_app_stream
*stream
, *stmp
;
1633 health_code_update();
1635 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1638 /* Send channel to the application. */
1639 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1640 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1641 ret
= -ENOTCONN
; /* Caused by app exiting. */
1643 } else if (ret
< 0) {
1647 health_code_update();
1649 /* Send all streams to application. */
1650 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1651 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1652 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1653 ret
= -ENOTCONN
; /* Caused by app exiting. */
1655 } else if (ret
< 0) {
1658 /* We don't need the stream anymore once sent to the tracer. */
1659 cds_list_del(&stream
->list
);
1660 delete_ust_app_stream(-1, stream
, app
);
1662 /* Flag the channel that it is sent to the application. */
1663 ua_chan
->is_sent
= 1;
1666 health_code_update();
1671 * Create the specified event onto the UST tracer for a UST session.
1673 * Should be called with session mutex held.
1676 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1677 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1681 health_code_update();
1683 /* Create UST event on tracer */
1684 pthread_mutex_lock(&app
->sock_lock
);
1685 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1687 pthread_mutex_unlock(&app
->sock_lock
);
1689 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1691 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1692 ua_event
->attr
.name
, app
->pid
, ret
);
1695 * This is normal behavior, an application can die during the
1696 * creation process. Don't report an error so the execution can
1697 * continue normally.
1700 DBG3("UST app create event failed. Application is dead.");
1705 ua_event
->handle
= ua_event
->obj
->handle
;
1707 DBG2("UST app event %s created successfully for pid:%d",
1708 ua_event
->attr
.name
, app
->pid
);
1710 health_code_update();
1712 /* Set filter if one is present. */
1713 if (ua_event
->filter
) {
1714 ret
= set_ust_event_filter(ua_event
, app
);
1720 /* Set exclusions for the event */
1721 if (ua_event
->exclusion
) {
1722 ret
= set_ust_event_exclusion(ua_event
, app
);
1728 /* If event not enabled, disable it on the tracer */
1729 if (ua_event
->enabled
) {
1731 * We now need to explicitly enable the event, since it
1732 * is now disabled at creation.
1734 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1737 * If we hit an EPERM, something is wrong with our enable call. If
1738 * we get an EEXIST, there is a problem on the tracer side since we
1742 case -LTTNG_UST_ERR_PERM
:
1743 /* Code flow problem */
1745 case -LTTNG_UST_ERR_EXIST
:
1746 /* It's OK for our use case. */
1757 health_code_update();
1762 * Copy data between an UST app event and a LTT event.
1764 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1765 struct ltt_ust_event
*uevent
)
1767 size_t exclusion_alloc_size
;
1769 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1770 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1772 ua_event
->enabled
= uevent
->enabled
;
1774 /* Copy event attributes */
1775 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1777 /* Copy filter bytecode */
1778 if (uevent
->filter
) {
1779 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1780 /* Filter might be NULL here in case of ENONEM. */
1783 /* Copy exclusion data */
1784 if (uevent
->exclusion
) {
1785 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1786 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1787 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1788 if (ua_event
->exclusion
== NULL
) {
1791 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1792 exclusion_alloc_size
);
1798 * Copy data between an UST app channel and a LTT channel.
1800 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1801 struct ltt_ust_channel
*uchan
)
1803 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1805 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1806 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1808 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1809 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1811 /* Copy event attributes since the layout is different. */
1812 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1813 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1814 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1815 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1816 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1817 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1818 ua_chan
->attr
.output
= uchan
->attr
.output
;
1819 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
1822 * Note that the attribute channel type is not set since the channel on the
1823 * tracing registry side does not have this information.
1826 ua_chan
->enabled
= uchan
->enabled
;
1827 ua_chan
->tracing_channel_id
= uchan
->id
;
1829 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1833 * Copy data between a UST app session and a regular LTT session.
1835 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1836 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1838 struct tm
*timeinfo
;
1841 char tmp_shm_path
[PATH_MAX
];
1843 timeinfo
= localtime(&app
->registration_time
);
1844 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1846 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1848 ua_sess
->tracing_id
= usess
->id
;
1849 ua_sess
->id
= get_next_session_id();
1850 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
1851 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
1852 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
1853 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
1854 ua_sess
->buffer_type
= usess
->buffer_type
;
1855 ua_sess
->bits_per_long
= app
->bits_per_long
;
1857 /* There is only one consumer object per session possible. */
1858 consumer_output_get(usess
->consumer
);
1859 ua_sess
->consumer
= usess
->consumer
;
1861 ua_sess
->output_traces
= usess
->output_traces
;
1862 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1863 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1864 &usess
->metadata_attr
);
1866 switch (ua_sess
->buffer_type
) {
1867 case LTTNG_BUFFER_PER_PID
:
1868 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1869 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1872 case LTTNG_BUFFER_PER_UID
:
1873 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1874 DEFAULT_UST_TRACE_UID_PATH
,
1875 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
1876 app
->bits_per_long
);
1883 PERROR("asprintf UST shadow copy session");
1888 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1889 sizeof(ua_sess
->root_shm_path
));
1890 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1891 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1892 sizeof(ua_sess
->shm_path
));
1893 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1894 if (ua_sess
->shm_path
[0]) {
1895 switch (ua_sess
->buffer_type
) {
1896 case LTTNG_BUFFER_PER_PID
:
1897 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1898 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1899 app
->name
, app
->pid
, datetime
);
1901 case LTTNG_BUFFER_PER_UID
:
1902 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1903 "/" DEFAULT_UST_TRACE_UID_PATH
,
1904 app
->uid
, app
->bits_per_long
);
1911 PERROR("sprintf UST shadow copy session");
1915 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1916 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1917 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1922 consumer_output_put(ua_sess
->consumer
);
1926 * Lookup sesison wrapper.
1929 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
1930 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1932 /* Get right UST app session from app */
1933 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1937 * Return ust app session from the app session hashtable using the UST session
1940 static struct ust_app_session
*lookup_session_by_app(
1941 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
1943 struct lttng_ht_iter iter
;
1944 struct lttng_ht_node_u64
*node
;
1946 __lookup_session_by_app(usess
, app
, &iter
);
1947 node
= lttng_ht_iter_get_node_u64(&iter
);
1952 return caa_container_of(node
, struct ust_app_session
, node
);
1959 * Setup buffer registry per PID for the given session and application. If none
1960 * is found, a new one is created, added to the global registry and
1961 * initialized. If regp is valid, it's set with the newly created object.
1963 * Return 0 on success or else a negative value.
1965 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1966 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1969 struct buffer_reg_pid
*reg_pid
;
1976 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1979 * This is the create channel path meaning that if there is NO
1980 * registry available, we have to create one for this session.
1982 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
1983 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1991 /* Initialize registry. */
1992 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1993 app
->bits_per_long
, app
->uint8_t_alignment
,
1994 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1995 app
->uint64_t_alignment
, app
->long_alignment
,
1996 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
1997 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
1998 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
1999 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2000 ua_sess
->tracing_id
,
2004 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2005 * destroy the buffer registry, because it is always expected
2006 * that if the buffer registry can be found, its ust registry is
2009 buffer_reg_pid_destroy(reg_pid
);
2013 buffer_reg_pid_add(reg_pid
);
2015 DBG3("UST app buffer registry per PID created successfully");
2027 * Setup buffer registry per UID for the given session and application. If none
2028 * is found, a new one is created, added to the global registry and
2029 * initialized. If regp is valid, it's set with the newly created object.
2031 * Return 0 on success or else a negative value.
2033 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2034 struct ust_app_session
*ua_sess
,
2035 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2038 struct buffer_reg_uid
*reg_uid
;
2045 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2048 * This is the create channel path meaning that if there is NO
2049 * registry available, we have to create one for this session.
2051 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2052 LTTNG_DOMAIN_UST
, ®_uid
,
2053 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2061 /* Initialize registry. */
2062 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2063 app
->bits_per_long
, app
->uint8_t_alignment
,
2064 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2065 app
->uint64_t_alignment
, app
->long_alignment
,
2066 app
->byte_order
, app
->version
.major
,
2067 app
->version
.minor
, reg_uid
->root_shm_path
,
2068 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2069 ua_sess
->tracing_id
, app
->uid
);
2072 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2073 * destroy the buffer registry, because it is always expected
2074 * that if the buffer registry can be found, its ust registry is
2077 buffer_reg_uid_destroy(reg_uid
, NULL
);
2080 /* Add node to teardown list of the session. */
2081 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2083 buffer_reg_uid_add(reg_uid
);
2085 DBG3("UST app buffer registry per UID created successfully");
2096 * Create a session on the tracer side for the given app.
2098 * On success, ua_sess_ptr is populated with the session pointer or else left
2099 * untouched. If the session was created, is_created is set to 1. On error,
2100 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2103 * Returns 0 on success or else a negative code which is either -ENOMEM or
2104 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2106 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2107 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2110 int ret
, created
= 0;
2111 struct ust_app_session
*ua_sess
;
2115 assert(ua_sess_ptr
);
2117 health_code_update();
2119 ua_sess
= lookup_session_by_app(usess
, app
);
2120 if (ua_sess
== NULL
) {
2121 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2122 app
->pid
, usess
->id
);
2123 ua_sess
= alloc_ust_app_session();
2124 if (ua_sess
== NULL
) {
2125 /* Only malloc can failed so something is really wrong */
2129 shadow_copy_session(ua_sess
, usess
, app
);
2133 switch (usess
->buffer_type
) {
2134 case LTTNG_BUFFER_PER_PID
:
2135 /* Init local registry. */
2136 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2138 delete_ust_app_session(-1, ua_sess
, app
);
2142 case LTTNG_BUFFER_PER_UID
:
2143 /* Look for a global registry. If none exists, create one. */
2144 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2146 delete_ust_app_session(-1, ua_sess
, app
);
2156 health_code_update();
2158 if (ua_sess
->handle
== -1) {
2159 pthread_mutex_lock(&app
->sock_lock
);
2160 ret
= ustctl_create_session(app
->sock
);
2161 pthread_mutex_unlock(&app
->sock_lock
);
2163 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2164 ERR("Creating session for app pid %d with ret %d",
2167 DBG("UST app creating session failed. Application is dead");
2169 * This is normal behavior, an application can die during the
2170 * creation process. Don't report an error so the execution can
2171 * continue normally. This will get flagged ENOTCONN and the
2172 * caller will handle it.
2176 delete_ust_app_session(-1, ua_sess
, app
);
2177 if (ret
!= -ENOMEM
) {
2179 * Tracer is probably gone or got an internal error so let's
2180 * behave like it will soon unregister or not usable.
2187 ua_sess
->handle
= ret
;
2189 /* Add ust app session to app's HT */
2190 lttng_ht_node_init_u64(&ua_sess
->node
,
2191 ua_sess
->tracing_id
);
2192 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2193 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2194 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2195 &ua_sess
->ust_objd_node
);
2197 DBG2("UST app session created successfully with handle %d", ret
);
2200 *ua_sess_ptr
= ua_sess
;
2202 *is_created
= created
;
2205 /* Everything went well. */
2209 health_code_update();
2214 * Match function for a hash table lookup of ust_app_ctx.
2216 * It matches an ust app context based on the context type and, in the case
2217 * of perf counters, their name.
2219 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2221 struct ust_app_ctx
*ctx
;
2222 const struct lttng_ust_context_attr
*key
;
2227 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2231 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2236 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2237 if (strncmp(key
->u
.perf_counter
.name
,
2238 ctx
->ctx
.u
.perf_counter
.name
,
2239 sizeof(key
->u
.perf_counter
.name
))) {
2243 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2244 if (strcmp(key
->u
.app_ctx
.provider_name
,
2245 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2246 strcmp(key
->u
.app_ctx
.ctx_name
,
2247 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2263 * Lookup for an ust app context from an lttng_ust_context.
2265 * Must be called while holding RCU read side lock.
2266 * Return an ust_app_ctx object or NULL on error.
2269 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2270 struct lttng_ust_context_attr
*uctx
)
2272 struct lttng_ht_iter iter
;
2273 struct lttng_ht_node_ulong
*node
;
2274 struct ust_app_ctx
*app_ctx
= NULL
;
2279 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2280 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2281 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2282 node
= lttng_ht_iter_get_node_ulong(&iter
);
2287 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2294 * Create a context for the channel on the tracer.
2296 * Called with UST app session lock held and a RCU read side lock.
2299 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2300 struct lttng_ust_context_attr
*uctx
,
2301 struct ust_app
*app
)
2304 struct ust_app_ctx
*ua_ctx
;
2306 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2308 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2314 ua_ctx
= alloc_ust_app_ctx(uctx
);
2315 if (ua_ctx
== NULL
) {
2321 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2322 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2323 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2325 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2335 * Enable on the tracer side a ust app event for the session and channel.
2337 * Called with UST app session lock held.
2340 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2341 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2345 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2350 ua_event
->enabled
= 1;
2357 * Disable on the tracer side a ust app event for the session and channel.
2359 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2360 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2364 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2369 ua_event
->enabled
= 0;
2376 * Lookup ust app channel for session and disable it on the tracer side.
2379 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2380 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2384 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2389 ua_chan
->enabled
= 0;
2396 * Lookup ust app channel for session and enable it on the tracer side. This
2397 * MUST be called with a RCU read side lock acquired.
2399 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2400 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2403 struct lttng_ht_iter iter
;
2404 struct lttng_ht_node_str
*ua_chan_node
;
2405 struct ust_app_channel
*ua_chan
;
2407 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2408 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2409 if (ua_chan_node
== NULL
) {
2410 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2411 uchan
->name
, ua_sess
->tracing_id
);
2415 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2417 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2427 * Ask the consumer to create a channel and get it if successful.
2429 * Called with UST app session lock held.
2431 * Return 0 on success or else a negative value.
2433 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2434 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2435 int bitness
, struct ust_registry_session
*registry
,
2436 uint64_t trace_archive_id
)
2439 unsigned int nb_fd
= 0;
2440 struct consumer_socket
*socket
;
2448 health_code_update();
2450 /* Get the right consumer socket for the application. */
2451 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2457 health_code_update();
2459 /* Need one fd for the channel. */
2460 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2462 ERR("Exhausted number of available FD upon create channel");
2467 * Ask consumer to create channel. The consumer will return the number of
2468 * stream we have to expect.
2470 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2471 registry
, usess
->current_trace_chunk
);
2477 * Compute the number of fd needed before receiving them. It must be 2 per
2478 * stream (2 being the default value here).
2480 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2482 /* Reserve the amount of file descriptor we need. */
2483 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2485 ERR("Exhausted number of available FD upon create channel");
2486 goto error_fd_get_stream
;
2489 health_code_update();
2492 * Now get the channel from the consumer. This call wil populate the stream
2493 * list of that channel and set the ust objects.
2495 if (usess
->consumer
->enabled
) {
2496 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2506 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2507 error_fd_get_stream
:
2509 * Initiate a destroy channel on the consumer since we had an error
2510 * handling it on our side. The return value is of no importance since we
2511 * already have a ret value set by the previous error that we need to
2514 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2516 lttng_fd_put(LTTNG_FD_APPS
, 1);
2518 health_code_update();
2524 * Duplicate the ust data object of the ust app stream and save it in the
2525 * buffer registry stream.
2527 * Return 0 on success or else a negative value.
2529 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2530 struct ust_app_stream
*stream
)
2537 /* Reserve the amount of file descriptor we need. */
2538 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2540 ERR("Exhausted number of available FD upon duplicate stream");
2544 /* Duplicate object for stream once the original is in the registry. */
2545 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2546 reg_stream
->obj
.ust
);
2548 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2549 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2550 lttng_fd_put(LTTNG_FD_APPS
, 2);
2553 stream
->handle
= stream
->obj
->handle
;
2560 * Duplicate the ust data object of the ust app. channel and save it in the
2561 * buffer registry channel.
2563 * Return 0 on success or else a negative value.
2565 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2566 struct ust_app_channel
*ua_chan
)
2573 /* Need two fds for the channel. */
2574 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2576 ERR("Exhausted number of available FD upon duplicate channel");
2580 /* Duplicate object for stream once the original is in the registry. */
2581 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2583 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2584 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2587 ua_chan
->handle
= ua_chan
->obj
->handle
;
2592 lttng_fd_put(LTTNG_FD_APPS
, 1);
2598 * For a given channel buffer registry, setup all streams of the given ust
2599 * application channel.
2601 * Return 0 on success or else a negative value.
2603 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2604 struct ust_app_channel
*ua_chan
,
2605 struct ust_app
*app
)
2608 struct ust_app_stream
*stream
, *stmp
;
2613 DBG2("UST app setup buffer registry stream");
2615 /* Send all streams to application. */
2616 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2617 struct buffer_reg_stream
*reg_stream
;
2619 ret
= buffer_reg_stream_create(®_stream
);
2625 * Keep original pointer and nullify it in the stream so the delete
2626 * stream call does not release the object.
2628 reg_stream
->obj
.ust
= stream
->obj
;
2630 buffer_reg_stream_add(reg_stream
, reg_chan
);
2632 /* We don't need the streams anymore. */
2633 cds_list_del(&stream
->list
);
2634 delete_ust_app_stream(-1, stream
, app
);
2642 * Create a buffer registry channel for the given session registry and
2643 * application channel object. If regp pointer is valid, it's set with the
2644 * created object. Important, the created object is NOT added to the session
2645 * registry hash table.
2647 * Return 0 on success else a negative value.
2649 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2650 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2653 struct buffer_reg_channel
*reg_chan
= NULL
;
2658 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2660 /* Create buffer registry channel. */
2661 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2666 reg_chan
->consumer_key
= ua_chan
->key
;
2667 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2668 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2670 /* Create and add a channel registry to session. */
2671 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2672 ua_chan
->tracing_channel_id
);
2676 buffer_reg_channel_add(reg_sess
, reg_chan
);
2685 /* Safe because the registry channel object was not added to any HT. */
2686 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2692 * Setup buffer registry channel for the given session registry and application
2693 * channel object. If regp pointer is valid, it's set with the created object.
2695 * Return 0 on success else a negative value.
2697 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2698 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2699 struct ust_app
*app
)
2706 assert(ua_chan
->obj
);
2708 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2710 /* Setup all streams for the registry. */
2711 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2716 reg_chan
->obj
.ust
= ua_chan
->obj
;
2717 ua_chan
->obj
= NULL
;
2722 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2723 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2728 * Send buffer registry channel to the application.
2730 * Return 0 on success else a negative value.
2732 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2733 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2734 struct ust_app_channel
*ua_chan
)
2737 struct buffer_reg_stream
*reg_stream
;
2744 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2746 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2751 /* Send channel to the application. */
2752 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2753 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2754 ret
= -ENOTCONN
; /* Caused by app exiting. */
2756 } else if (ret
< 0) {
2760 health_code_update();
2762 /* Send all streams to application. */
2763 pthread_mutex_lock(®_chan
->stream_list_lock
);
2764 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2765 struct ust_app_stream stream
;
2767 ret
= duplicate_stream_object(reg_stream
, &stream
);
2769 goto error_stream_unlock
;
2772 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2774 (void) release_ust_app_stream(-1, &stream
, app
);
2775 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2776 ret
= -ENOTCONN
; /* Caused by app exiting. */
2778 goto error_stream_unlock
;
2782 * The return value is not important here. This function will output an
2785 (void) release_ust_app_stream(-1, &stream
, app
);
2787 ua_chan
->is_sent
= 1;
2789 error_stream_unlock
:
2790 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2796 * Create and send to the application the created buffers with per UID buffers.
2798 * This MUST be called with a RCU read side lock acquired.
2799 * The session list lock and the session's lock must be acquired.
2801 * Return 0 on success else a negative value.
2803 static int create_channel_per_uid(struct ust_app
*app
,
2804 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2805 struct ust_app_channel
*ua_chan
)
2808 struct buffer_reg_uid
*reg_uid
;
2809 struct buffer_reg_channel
*reg_chan
;
2810 struct ltt_session
*session
= NULL
;
2811 enum lttng_error_code notification_ret
;
2812 struct ust_registry_channel
*chan_reg
;
2819 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2821 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2823 * The session creation handles the creation of this global registry
2824 * object. If none can be find, there is a code flow problem or a
2829 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2835 /* Create the buffer registry channel object. */
2836 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2838 ERR("Error creating the UST channel \"%s\" registry instance",
2843 session
= session_find_by_id(ua_sess
->tracing_id
);
2845 assert(pthread_mutex_trylock(&session
->lock
));
2846 assert(session_trylock_list());
2849 * Create the buffers on the consumer side. This call populates the
2850 * ust app channel object with all streams and data object.
2852 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2853 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
2854 session
->most_recent_chunk_id
.value
);
2856 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2860 * Let's remove the previously created buffer registry channel so
2861 * it's not visible anymore in the session registry.
2863 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2864 ua_chan
->tracing_channel_id
, false);
2865 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2866 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2871 * Setup the streams and add it to the session registry.
2873 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2874 ua_chan
, reg_chan
, app
);
2876 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
2880 /* Notify the notification subsystem of the channel's creation. */
2881 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2882 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2883 ua_chan
->tracing_channel_id
);
2885 chan_reg
->consumer_key
= ua_chan
->key
;
2887 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2889 notification_ret
= notification_thread_command_add_channel(
2890 notification_thread_handle
, session
->name
,
2891 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2892 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2894 ua_chan
->key
, LTTNG_DOMAIN_UST
,
2895 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2896 if (notification_ret
!= LTTNG_OK
) {
2897 ret
= - (int) notification_ret
;
2898 ERR("Failed to add channel to notification thread");
2903 /* Send buffers to the application. */
2904 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2906 if (ret
!= -ENOTCONN
) {
2907 ERR("Error sending channel to application");
2914 session_put(session
);
2920 * Create and send to the application the created buffers with per PID buffers.
2922 * Called with UST app session lock held.
2923 * The session list lock and the session's lock must be acquired.
2925 * Return 0 on success else a negative value.
2927 static int create_channel_per_pid(struct ust_app
*app
,
2928 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2929 struct ust_app_channel
*ua_chan
)
2932 struct ust_registry_session
*registry
;
2933 enum lttng_error_code cmd_ret
;
2934 struct ltt_session
*session
= NULL
;
2935 uint64_t chan_reg_key
;
2936 struct ust_registry_channel
*chan_reg
;
2943 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2947 registry
= get_session_registry(ua_sess
);
2948 /* The UST app session lock is held, registry shall not be null. */
2951 /* Create and add a new channel registry to session. */
2952 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2954 ERR("Error creating the UST channel \"%s\" registry instance",
2959 session
= session_find_by_id(ua_sess
->tracing_id
);
2962 assert(pthread_mutex_trylock(&session
->lock
));
2963 assert(session_trylock_list());
2965 /* Create and get channel on the consumer side. */
2966 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2967 app
->bits_per_long
, registry
,
2968 session
->most_recent_chunk_id
.value
);
2970 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2972 goto error_remove_from_registry
;
2975 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2977 if (ret
!= -ENOTCONN
) {
2978 ERR("Error sending channel to application");
2980 goto error_remove_from_registry
;
2983 chan_reg_key
= ua_chan
->key
;
2984 pthread_mutex_lock(®istry
->lock
);
2985 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
2987 chan_reg
->consumer_key
= ua_chan
->key
;
2988 pthread_mutex_unlock(®istry
->lock
);
2990 cmd_ret
= notification_thread_command_add_channel(
2991 notification_thread_handle
, session
->name
,
2992 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2993 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2995 ua_chan
->key
, LTTNG_DOMAIN_UST
,
2996 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2997 if (cmd_ret
!= LTTNG_OK
) {
2998 ret
= - (int) cmd_ret
;
2999 ERR("Failed to add channel to notification thread");
3000 goto error_remove_from_registry
;
3003 error_remove_from_registry
:
3005 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3010 session_put(session
);
3016 * From an already allocated ust app channel, create the channel buffers if
3017 * needed and send them to the application. This MUST be called with a RCU read
3018 * side lock acquired.
3020 * Called with UST app session lock held.
3022 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3023 * the application exited concurrently.
3025 static int ust_app_channel_send(struct ust_app
*app
,
3026 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3027 struct ust_app_channel
*ua_chan
)
3033 assert(usess
->active
);
3037 /* Handle buffer type before sending the channel to the application. */
3038 switch (usess
->buffer_type
) {
3039 case LTTNG_BUFFER_PER_UID
:
3041 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3047 case LTTNG_BUFFER_PER_PID
:
3049 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3061 /* Initialize ust objd object using the received handle and add it. */
3062 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3063 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3065 /* If channel is not enabled, disable it on the tracer */
3066 if (!ua_chan
->enabled
) {
3067 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3078 * Create UST app channel and return it through ua_chanp if not NULL.
3080 * Called with UST app session lock and RCU read-side lock held.
3082 * Return 0 on success or else a negative value.
3084 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3085 struct ltt_ust_channel
*uchan
,
3086 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3087 struct ust_app_channel
**ua_chanp
)
3090 struct lttng_ht_iter iter
;
3091 struct lttng_ht_node_str
*ua_chan_node
;
3092 struct ust_app_channel
*ua_chan
;
3094 /* Lookup channel in the ust app session */
3095 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3096 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3097 if (ua_chan_node
!= NULL
) {
3098 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3102 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3103 if (ua_chan
== NULL
) {
3104 /* Only malloc can fail here */
3108 shadow_copy_channel(ua_chan
, uchan
);
3110 /* Set channel type. */
3111 ua_chan
->attr
.type
= type
;
3113 /* Only add the channel if successful on the tracer side. */
3114 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3117 *ua_chanp
= ua_chan
;
3120 /* Everything went well. */
3128 * Create UST app event and create it on the tracer side.
3130 * Called with ust app session mutex held.
3133 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3134 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3135 struct ust_app
*app
)
3138 struct ust_app_event
*ua_event
;
3140 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3141 if (ua_event
== NULL
) {
3142 /* Only failure mode of alloc_ust_app_event(). */
3146 shadow_copy_event(ua_event
, uevent
);
3148 /* Create it on the tracer side */
3149 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3152 * Not found previously means that it does not exist on the
3153 * tracer. If the application reports that the event existed,
3154 * it means there is a bug in the sessiond or lttng-ust
3155 * (or corruption, etc.)
3157 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3158 ERR("Tracer for application reported that an event being created already existed: "
3159 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3161 app
->pid
, app
->ppid
, app
->uid
,
3167 add_unique_ust_app_event(ua_chan
, ua_event
);
3169 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3176 /* Valid. Calling here is already in a read side lock */
3177 delete_ust_app_event(-1, ua_event
, app
);
3182 * Create UST metadata and open it on the tracer side.
3184 * Called with UST app session lock held and RCU read side lock.
3186 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3187 struct ust_app
*app
, struct consumer_output
*consumer
)
3190 struct ust_app_channel
*metadata
;
3191 struct consumer_socket
*socket
;
3192 struct ust_registry_session
*registry
;
3193 struct ltt_session
*session
= NULL
;
3199 registry
= get_session_registry(ua_sess
);
3200 /* The UST app session is held registry shall not be null. */
3203 pthread_mutex_lock(®istry
->lock
);
3205 /* Metadata already exists for this registry or it was closed previously */
3206 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3211 /* Allocate UST metadata */
3212 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3214 /* malloc() failed */
3219 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3221 /* Need one fd for the channel. */
3222 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3224 ERR("Exhausted number of available FD upon create metadata");