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>
30 #include <lttng/ust-error.h>
33 #include <common/common.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
36 #include "buffer-registry.h"
38 #include "health-sessiond.h"
40 #include "ust-consumer.h"
44 #include "lttng-sessiond.h"
45 #include "notification-thread-commands.h"
49 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
51 /* Next available channel key. Access under next_channel_key_lock. */
52 static uint64_t _next_channel_key
;
53 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
55 /* Next available session ID. Access under next_session_id_lock. */
56 static uint64_t _next_session_id
;
57 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
60 * Return the incremented value of next_channel_key.
62 static uint64_t get_next_channel_key(void)
66 pthread_mutex_lock(&next_channel_key_lock
);
67 ret
= ++_next_channel_key
;
68 pthread_mutex_unlock(&next_channel_key_lock
);
73 * Return the atomically incremented value of next_session_id.
75 static uint64_t get_next_session_id(void)
79 pthread_mutex_lock(&next_session_id_lock
);
80 ret
= ++_next_session_id
;
81 pthread_mutex_unlock(&next_session_id_lock
);
85 static void copy_channel_attr_to_ustctl(
86 struct ustctl_consumer_channel_attr
*attr
,
87 struct lttng_ust_channel_attr
*uattr
)
89 /* Copy event attributes since the layout is different. */
90 attr
->subbuf_size
= uattr
->subbuf_size
;
91 attr
->num_subbuf
= uattr
->num_subbuf
;
92 attr
->overwrite
= uattr
->overwrite
;
93 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
94 attr
->read_timer_interval
= uattr
->read_timer_interval
;
95 attr
->output
= uattr
->output
;
96 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
100 * Match function for the hash table lookup.
102 * It matches an ust app event based on three attributes which are the event
103 * name, the filter bytecode and the loglevel.
105 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
107 struct ust_app_event
*event
;
108 const struct ust_app_ht_key
*key
;
109 int ev_loglevel_value
;
114 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
116 ev_loglevel_value
= event
->attr
.loglevel
;
118 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
121 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
125 /* Event loglevel. */
126 if (ev_loglevel_value
!= key
->loglevel_type
) {
127 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
128 && key
->loglevel_type
== 0 &&
129 ev_loglevel_value
== -1) {
131 * Match is accepted. This is because on event creation, the
132 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
133 * -1 are accepted for this loglevel type since 0 is the one set by
134 * the API when receiving an enable event.
141 /* One of the filters is NULL, fail. */
142 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
146 if (key
->filter
&& event
->filter
) {
147 /* Both filters exists, check length followed by the bytecode. */
148 if (event
->filter
->len
!= key
->filter
->len
||
149 memcmp(event
->filter
->data
, key
->filter
->data
,
150 event
->filter
->len
) != 0) {
155 /* One of the exclusions is NULL, fail. */
156 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
160 if (key
->exclusion
&& event
->exclusion
) {
161 /* Both exclusions exists, check count followed by the names. */
162 if (event
->exclusion
->count
!= key
->exclusion
->count
||
163 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
164 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
178 * Unique add of an ust app event in the given ht. This uses the custom
179 * ht_match_ust_app_event match function and the event name as hash.
181 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
182 struct ust_app_event
*event
)
184 struct cds_lfht_node
*node_ptr
;
185 struct ust_app_ht_key key
;
189 assert(ua_chan
->events
);
192 ht
= ua_chan
->events
;
193 key
.name
= event
->attr
.name
;
194 key
.filter
= event
->filter
;
195 key
.loglevel_type
= event
->attr
.loglevel
;
196 key
.exclusion
= event
->exclusion
;
198 node_ptr
= cds_lfht_add_unique(ht
->ht
,
199 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
200 ht_match_ust_app_event
, &key
, &event
->node
.node
);
201 assert(node_ptr
== &event
->node
.node
);
205 * Close the notify socket from the given RCU head object. This MUST be called
206 * through a call_rcu().
208 static void close_notify_sock_rcu(struct rcu_head
*head
)
211 struct ust_app_notify_sock_obj
*obj
=
212 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
214 /* Must have a valid fd here. */
215 assert(obj
->fd
>= 0);
217 ret
= close(obj
->fd
);
219 ERR("close notify sock %d RCU", obj
->fd
);
221 lttng_fd_put(LTTNG_FD_APPS
, 1);
227 * Return the session registry according to the buffer type of the given
230 * A registry per UID object MUST exists before calling this function or else
231 * it assert() if not found. RCU read side lock must be acquired.
233 static struct ust_registry_session
*get_session_registry(
234 struct ust_app_session
*ua_sess
)
236 struct ust_registry_session
*registry
= NULL
;
240 switch (ua_sess
->buffer_type
) {
241 case LTTNG_BUFFER_PER_PID
:
243 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
247 registry
= reg_pid
->registry
->reg
.ust
;
250 case LTTNG_BUFFER_PER_UID
:
252 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
253 ua_sess
->tracing_id
, ua_sess
->bits_per_long
, ua_sess
->uid
);
257 registry
= reg_uid
->registry
->reg
.ust
;
269 * Delete ust context safely. RCU read lock must be held before calling
273 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
281 pthread_mutex_lock(&app
->sock_lock
);
282 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
283 pthread_mutex_unlock(&app
->sock_lock
);
284 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
285 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
286 sock
, ua_ctx
->obj
->handle
, ret
);
294 * Delete ust app event safely. RCU read lock must be held before calling
298 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
305 free(ua_event
->filter
);
306 if (ua_event
->exclusion
!= NULL
)
307 free(ua_event
->exclusion
);
308 if (ua_event
->obj
!= NULL
) {
309 pthread_mutex_lock(&app
->sock_lock
);
310 ret
= ustctl_release_object(sock
, ua_event
->obj
);
311 pthread_mutex_unlock(&app
->sock_lock
);
312 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
313 ERR("UST app sock %d release event obj failed with ret %d",
322 * Release ust data object of the given stream.
324 * Return 0 on success or else a negative value.
326 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
334 pthread_mutex_lock(&app
->sock_lock
);
335 ret
= ustctl_release_object(sock
, stream
->obj
);
336 pthread_mutex_unlock(&app
->sock_lock
);
337 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
338 ERR("UST app sock %d release stream obj failed with ret %d",
341 lttng_fd_put(LTTNG_FD_APPS
, 2);
349 * Delete ust app stream safely. RCU read lock must be held before calling
353 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
358 (void) release_ust_app_stream(sock
, stream
, app
);
363 * We need to execute ht_destroy outside of RCU read-side critical
364 * section and outside of call_rcu thread, so we postpone its execution
365 * using ht_cleanup_push. It is simpler than to change the semantic of
366 * the many callers of delete_ust_app_session().
369 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
371 struct ust_app_channel
*ua_chan
=
372 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
374 ht_cleanup_push(ua_chan
->ctx
);
375 ht_cleanup_push(ua_chan
->events
);
380 * Extract the lost packet or discarded events counter when the channel is
381 * being deleted and store the value in the parent channel so we can
382 * access it from lttng list and at stop/destroy.
384 * The session list lock must be held by the caller.
387 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
389 uint64_t discarded
= 0, lost
= 0;
390 struct ltt_session
*session
;
391 struct ltt_ust_channel
*uchan
;
393 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
398 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
399 if (!session
|| !session
->ust_session
) {
401 * Not finding the session is not an error because there are
402 * multiple ways the channels can be torn down.
404 * 1) The session daemon can initiate the destruction of the
405 * ust app session after receiving a destroy command or
406 * during its shutdown/teardown.
407 * 2) The application, since we are in per-pid tracing, is
408 * unregistering and tearing down its ust app session.
410 * Both paths are protected by the session list lock which
411 * ensures that the accounting of lost packets and discarded
412 * events is done exactly once. The session is then unpublished
413 * from the session list, resulting in this condition.
418 if (ua_chan
->attr
.overwrite
) {
419 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
420 ua_chan
->key
, session
->ust_session
->consumer
,
423 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
424 ua_chan
->key
, session
->ust_session
->consumer
,
427 uchan
= trace_ust_find_channel_by_name(
428 session
->ust_session
->domain_global
.channels
,
431 ERR("Missing UST channel to store discarded counters");
435 uchan
->per_pid_closed_app_discarded
+= discarded
;
436 uchan
->per_pid_closed_app_lost
+= lost
;
443 * Delete ust app channel safely. RCU read lock must be held before calling
446 * The session list lock must be held by the caller.
449 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
453 struct lttng_ht_iter iter
;
454 struct ust_app_event
*ua_event
;
455 struct ust_app_ctx
*ua_ctx
;
456 struct ust_app_stream
*stream
, *stmp
;
457 struct ust_registry_session
*registry
;
461 DBG3("UST app deleting channel %s", ua_chan
->name
);
464 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
465 cds_list_del(&stream
->list
);
466 delete_ust_app_stream(sock
, stream
, app
);
470 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
471 cds_list_del(&ua_ctx
->list
);
472 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
474 delete_ust_app_ctx(sock
, ua_ctx
, app
);
478 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
480 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
482 delete_ust_app_event(sock
, ua_event
, app
);
485 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
486 /* Wipe and free registry from session registry. */
487 registry
= get_session_registry(ua_chan
->session
);
489 ust_registry_channel_del_free(registry
, ua_chan
->key
,
493 save_per_pid_lost_discarded_counters(ua_chan
);
497 if (ua_chan
->obj
!= NULL
) {
498 /* Remove channel from application UST object descriptor. */
499 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
500 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
502 pthread_mutex_lock(&app
->sock_lock
);
503 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
504 pthread_mutex_unlock(&app
->sock_lock
);
505 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
506 ERR("UST app sock %d release channel obj failed with ret %d",
509 lttng_fd_put(LTTNG_FD_APPS
, 1);
512 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
515 int ust_app_register_done(struct ust_app
*app
)
519 pthread_mutex_lock(&app
->sock_lock
);
520 ret
= ustctl_register_done(app
->sock
);
521 pthread_mutex_unlock(&app
->sock_lock
);
525 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
530 pthread_mutex_lock(&app
->sock_lock
);
535 ret
= ustctl_release_object(sock
, data
);
537 pthread_mutex_unlock(&app
->sock_lock
);
543 * Push metadata to consumer socket.
545 * RCU read-side lock must be held to guarantee existance of socket.
546 * Must be called with the ust app session lock held.
547 * Must be called with the registry lock held.
549 * On success, return the len of metadata pushed or else a negative value.
550 * Returning a -EPIPE return value means we could not send the metadata,
551 * but it can be caused by recoverable errors (e.g. the application has
552 * terminated concurrently).
554 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
555 struct consumer_socket
*socket
, int send_zero_data
)
558 char *metadata_str
= NULL
;
559 size_t len
, offset
, new_metadata_len_sent
;
561 uint64_t metadata_key
, metadata_version
;
566 metadata_key
= registry
->metadata_key
;
569 * Means that no metadata was assigned to the session. This can
570 * happens if no start has been done previously.
576 offset
= registry
->metadata_len_sent
;
577 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
578 new_metadata_len_sent
= registry
->metadata_len
;
579 metadata_version
= registry
->metadata_version
;
581 DBG3("No metadata to push for metadata key %" PRIu64
,
582 registry
->metadata_key
);
584 if (send_zero_data
) {
585 DBG("No metadata to push");
591 /* Allocate only what we have to send. */
592 metadata_str
= zmalloc(len
);
594 PERROR("zmalloc ust app metadata string");
598 /* Copy what we haven't sent out. */
599 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
602 pthread_mutex_unlock(®istry
->lock
);
604 * We need to unlock the registry while we push metadata to
605 * break a circular dependency between the consumerd metadata
606 * lock and the sessiond registry lock. Indeed, pushing metadata
607 * to the consumerd awaits that it gets pushed all the way to
608 * relayd, but doing so requires grabbing the metadata lock. If
609 * a concurrent metadata request is being performed by
610 * consumerd, this can try to grab the registry lock on the
611 * sessiond while holding the metadata lock on the consumer
612 * daemon. Those push and pull schemes are performed on two
613 * different bidirectionnal communication sockets.
615 ret
= consumer_push_metadata(socket
, metadata_key
,
616 metadata_str
, len
, offset
, metadata_version
);
617 pthread_mutex_lock(®istry
->lock
);
620 * There is an acceptable race here between the registry
621 * metadata key assignment and the creation on the
622 * consumer. The session daemon can concurrently push
623 * metadata for this registry while being created on the
624 * consumer since the metadata key of the registry is
625 * assigned *before* it is setup to avoid the consumer
626 * to ask for metadata that could possibly be not found
627 * in the session daemon.
629 * The metadata will get pushed either by the session
630 * being stopped or the consumer requesting metadata if
631 * that race is triggered.
633 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
636 ERR("Error pushing metadata to consumer");
642 * Metadata may have been concurrently pushed, since
643 * we're not holding the registry lock while pushing to
644 * consumer. This is handled by the fact that we send
645 * the metadata content, size, and the offset at which
646 * that metadata belongs. This may arrive out of order
647 * on the consumer side, and the consumer is able to
648 * deal with overlapping fragments. The consumer
649 * supports overlapping fragments, which must be
650 * contiguous starting from offset 0. We keep the
651 * largest metadata_len_sent value of the concurrent
654 registry
->metadata_len_sent
=
655 max_t(size_t, registry
->metadata_len_sent
,
656 new_metadata_len_sent
);
665 * On error, flag the registry that the metadata is
666 * closed. We were unable to push anything and this
667 * means that either the consumer is not responding or
668 * the metadata cache has been destroyed on the
671 registry
->metadata_closed
= 1;
679 * For a given application and session, push metadata to consumer.
680 * Either sock or consumer is required : if sock is NULL, the default
681 * socket to send the metadata is retrieved from consumer, if sock
682 * is not NULL we use it to send the metadata.
683 * RCU read-side lock must be held while calling this function,
684 * therefore ensuring existance of registry. It also ensures existance
685 * of socket throughout this function.
687 * Return 0 on success else a negative error.
688 * Returning a -EPIPE return value means we could not send the metadata,
689 * but it can be caused by recoverable errors (e.g. the application has
690 * terminated concurrently).
692 static int push_metadata(struct ust_registry_session
*registry
,
693 struct consumer_output
*consumer
)
697 struct consumer_socket
*socket
;
702 pthread_mutex_lock(®istry
->lock
);
703 if (registry
->metadata_closed
) {
708 /* Get consumer socket to use to push the metadata.*/
709 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
716 ret
= ust_app_push_metadata(registry
, socket
, 0);
721 pthread_mutex_unlock(®istry
->lock
);
725 pthread_mutex_unlock(®istry
->lock
);
730 * Send to the consumer a close metadata command for the given session. Once
731 * done, the metadata channel is deleted and the session metadata pointer is
732 * nullified. The session lock MUST be held unless the application is
733 * in the destroy path.
735 * Return 0 on success else a negative value.
737 static int close_metadata(struct ust_registry_session
*registry
,
738 struct consumer_output
*consumer
)
741 struct consumer_socket
*socket
;
748 pthread_mutex_lock(®istry
->lock
);
750 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
755 /* Get consumer socket to use to push the metadata.*/
756 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
763 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
770 * Metadata closed. Even on error this means that the consumer is not
771 * responding or not found so either way a second close should NOT be emit
774 registry
->metadata_closed
= 1;
776 pthread_mutex_unlock(®istry
->lock
);
782 * We need to execute ht_destroy outside of RCU read-side critical
783 * section and outside of call_rcu thread, so we postpone its execution
784 * using ht_cleanup_push. It is simpler than to change the semantic of
785 * the many callers of delete_ust_app_session().
788 void delete_ust_app_session_rcu(struct rcu_head
*head
)
790 struct ust_app_session
*ua_sess
=
791 caa_container_of(head
, struct ust_app_session
, rcu_head
);
793 ht_cleanup_push(ua_sess
->channels
);
798 * Delete ust app session safely. RCU read lock must be held before calling
801 * The session list lock must be held by the caller.
804 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
808 struct lttng_ht_iter iter
;
809 struct ust_app_channel
*ua_chan
;
810 struct ust_registry_session
*registry
;
814 pthread_mutex_lock(&ua_sess
->lock
);
816 assert(!ua_sess
->deleted
);
817 ua_sess
->deleted
= true;
819 registry
= get_session_registry(ua_sess
);
820 /* Registry can be null on error path during initialization. */
822 /* Push metadata for application before freeing the application. */
823 (void) push_metadata(registry
, ua_sess
->consumer
);
826 * Don't ask to close metadata for global per UID buffers. Close
827 * metadata only on destroy trace session in this case. Also, the
828 * previous push metadata could have flag the metadata registry to
829 * close so don't send a close command if closed.
831 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
832 /* And ask to close it for this session registry. */
833 (void) close_metadata(registry
, ua_sess
->consumer
);
837 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
839 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
841 delete_ust_app_channel(sock
, ua_chan
, app
);
844 /* In case of per PID, the registry is kept in the session. */
845 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
846 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
849 * Registry can be null on error path during
852 buffer_reg_pid_remove(reg_pid
);
853 buffer_reg_pid_destroy(reg_pid
);
857 if (ua_sess
->handle
!= -1) {
858 pthread_mutex_lock(&app
->sock_lock
);
859 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
860 pthread_mutex_unlock(&app
->sock_lock
);
861 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
862 ERR("UST app sock %d release session handle failed with ret %d",
865 /* Remove session from application UST object descriptor. */
866 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
867 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
871 pthread_mutex_unlock(&ua_sess
->lock
);
873 consumer_output_put(ua_sess
->consumer
);
875 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
879 * Delete a traceable application structure from the global list. Never call
880 * this function outside of a call_rcu call.
882 * RCU read side lock should _NOT_ be held when calling this function.
885 void delete_ust_app(struct ust_app
*app
)
888 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
891 * The session list lock must be held during this function to guarantee
892 * the existence of ua_sess.
895 /* Delete ust app sessions info */
900 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
902 /* Free every object in the session and the session. */
904 delete_ust_app_session(sock
, ua_sess
, app
);
908 ht_cleanup_push(app
->sessions
);
909 ht_cleanup_push(app
->ust_sessions_objd
);
910 ht_cleanup_push(app
->ust_objd
);
913 * Wait until we have deleted the application from the sock hash table
914 * before closing this socket, otherwise an application could re-use the
915 * socket ID and race with the teardown, using the same hash table entry.
917 * It's OK to leave the close in call_rcu. We want it to stay unique for
918 * all RCU readers that could run concurrently with unregister app,
919 * therefore we _need_ to only close that socket after a grace period. So
920 * it should stay in this RCU callback.
922 * This close() is a very important step of the synchronization model so
923 * every modification to this function must be carefully reviewed.
929 lttng_fd_put(LTTNG_FD_APPS
, 1);
931 DBG2("UST app pid %d deleted", app
->pid
);
933 session_unlock_list();
937 * URCU intermediate call to delete an UST app.
940 void delete_ust_app_rcu(struct rcu_head
*head
)
942 struct lttng_ht_node_ulong
*node
=
943 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
944 struct ust_app
*app
=
945 caa_container_of(node
, struct ust_app
, pid_n
);
947 DBG3("Call RCU deleting app PID %d", app
->pid
);
952 * Delete the session from the application ht and delete the data structure by
953 * freeing every object inside and releasing them.
955 * The session list lock must be held by the caller.
957 static void destroy_app_session(struct ust_app
*app
,
958 struct ust_app_session
*ua_sess
)
961 struct lttng_ht_iter iter
;
966 iter
.iter
.node
= &ua_sess
->node
.node
;
967 ret
= lttng_ht_del(app
->sessions
, &iter
);
969 /* Already scheduled for teardown. */
973 /* Once deleted, free the data structure. */
974 delete_ust_app_session(app
->sock
, ua_sess
, app
);
981 * Alloc new UST app session.
984 struct ust_app_session
*alloc_ust_app_session(void)
986 struct ust_app_session
*ua_sess
;
988 /* Init most of the default value by allocating and zeroing */
989 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
990 if (ua_sess
== NULL
) {
995 ua_sess
->handle
= -1;
996 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
997 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
998 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1007 * Alloc new UST app channel.
1010 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
1011 struct ust_app_session
*ua_sess
,
1012 struct lttng_ust_channel_attr
*attr
)
1014 struct ust_app_channel
*ua_chan
;
1016 /* Init most of the default value by allocating and zeroing */
1017 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1018 if (ua_chan
== NULL
) {
1023 /* Setup channel name */
1024 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1025 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1027 ua_chan
->enabled
= 1;
1028 ua_chan
->handle
= -1;
1029 ua_chan
->session
= ua_sess
;
1030 ua_chan
->key
= get_next_channel_key();
1031 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1032 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1033 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1035 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1036 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1038 /* Copy attributes */
1040 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1041 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1042 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1043 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1044 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1045 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1046 ua_chan
->attr
.output
= attr
->output
;
1047 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1049 /* By default, the channel is a per cpu channel. */
1050 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1052 DBG3("UST app channel %s allocated", ua_chan
->name
);
1061 * Allocate and initialize a UST app stream.
1063 * Return newly allocated stream pointer or NULL on error.
1065 struct ust_app_stream
*ust_app_alloc_stream(void)
1067 struct ust_app_stream
*stream
= NULL
;
1069 stream
= zmalloc(sizeof(*stream
));
1070 if (stream
== NULL
) {
1071 PERROR("zmalloc ust app stream");
1075 /* Zero could be a valid value for a handle so flag it to -1. */
1076 stream
->handle
= -1;
1083 * Alloc new UST app event.
1086 struct ust_app_event
*alloc_ust_app_event(char *name
,
1087 struct lttng_ust_event
*attr
)
1089 struct ust_app_event
*ua_event
;
1091 /* Init most of the default value by allocating and zeroing */
1092 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1093 if (ua_event
== NULL
) {
1098 ua_event
->enabled
= 1;
1099 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1100 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1101 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1103 /* Copy attributes */
1105 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1108 DBG3("UST app event %s allocated", ua_event
->name
);
1117 * Alloc new UST app context.
1120 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1122 struct ust_app_ctx
*ua_ctx
;
1124 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1125 if (ua_ctx
== NULL
) {
1129 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1132 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1133 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1134 char *provider_name
= NULL
, *ctx_name
= NULL
;
1136 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1137 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1138 if (!provider_name
|| !ctx_name
) {
1139 free(provider_name
);
1144 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1145 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1149 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1157 * Allocate a filter and copy the given original filter.
1159 * Return allocated filter or NULL on error.
1161 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1162 struct lttng_filter_bytecode
*orig_f
)
1164 struct lttng_filter_bytecode
*filter
= NULL
;
1166 /* Copy filter bytecode */
1167 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1169 PERROR("zmalloc alloc filter bytecode");
1173 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1180 * Create a liblttng-ust filter bytecode from given bytecode.
1182 * Return allocated filter or NULL on error.
1184 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1185 struct lttng_filter_bytecode
*orig_f
)
1187 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1189 /* Copy filter bytecode */
1190 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1192 PERROR("zmalloc alloc ust filter bytecode");
1196 assert(sizeof(struct lttng_filter_bytecode
) ==
1197 sizeof(struct lttng_ust_filter_bytecode
));
1198 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1204 * Find an ust_app using the sock and return it. RCU read side lock must be
1205 * held before calling this helper function.
1207 struct ust_app
*ust_app_find_by_sock(int sock
)
1209 struct lttng_ht_node_ulong
*node
;
1210 struct lttng_ht_iter iter
;
1212 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1213 node
= lttng_ht_iter_get_node_ulong(&iter
);
1215 DBG2("UST app find by sock %d not found", sock
);
1219 return caa_container_of(node
, struct ust_app
, sock_n
);
1226 * Find an ust_app using the notify sock and return it. RCU read side lock must
1227 * be held before calling this helper function.
1229 static struct ust_app
*find_app_by_notify_sock(int sock
)
1231 struct lttng_ht_node_ulong
*node
;
1232 struct lttng_ht_iter iter
;
1234 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1236 node
= lttng_ht_iter_get_node_ulong(&iter
);
1238 DBG2("UST app find by notify sock %d not found", sock
);
1242 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1249 * Lookup for an ust app event based on event name, filter bytecode and the
1252 * Return an ust_app_event object or NULL on error.
1254 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1255 char *name
, struct lttng_filter_bytecode
*filter
,
1257 const struct lttng_event_exclusion
*exclusion
)
1259 struct lttng_ht_iter iter
;
1260 struct lttng_ht_node_str
*node
;
1261 struct ust_app_event
*event
= NULL
;
1262 struct ust_app_ht_key key
;
1267 /* Setup key for event lookup. */
1269 key
.filter
= filter
;
1270 key
.loglevel_type
= loglevel_value
;
1271 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1272 key
.exclusion
= exclusion
;
1274 /* Lookup using the event name as hash and a custom match fct. */
1275 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1276 ht_match_ust_app_event
, &key
, &iter
.iter
);
1277 node
= lttng_ht_iter_get_node_str(&iter
);
1282 event
= caa_container_of(node
, struct ust_app_event
, node
);
1289 * Create the channel context on the tracer.
1291 * Called with UST app session lock held.
1294 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1295 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1299 health_code_update();
1301 pthread_mutex_lock(&app
->sock_lock
);
1302 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1303 ua_chan
->obj
, &ua_ctx
->obj
);
1304 pthread_mutex_unlock(&app
->sock_lock
);
1306 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1307 ERR("UST app create channel context failed for app (pid: %d) "
1308 "with ret %d", app
->pid
, ret
);
1311 * This is normal behavior, an application can die during the
1312 * creation process. Don't report an error so the execution can
1313 * continue normally.
1316 DBG3("UST app disable event failed. Application is dead.");
1321 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1323 DBG2("UST app context handle %d created successfully for channel %s",
1324 ua_ctx
->handle
, ua_chan
->name
);
1327 health_code_update();
1332 * Set the filter on the tracer.
1335 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1336 struct ust_app
*app
)
1339 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1341 health_code_update();
1343 if (!ua_event
->filter
) {
1348 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1349 if (!ust_bytecode
) {
1350 ret
= -LTTNG_ERR_NOMEM
;
1353 pthread_mutex_lock(&app
->sock_lock
);
1354 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1356 pthread_mutex_unlock(&app
->sock_lock
);
1358 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1359 ERR("UST app event %s filter failed for app (pid: %d) "
1360 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1363 * This is normal behavior, an application can die during the
1364 * creation process. Don't report an error so the execution can
1365 * continue normally.
1368 DBG3("UST app filter event failed. Application is dead.");
1373 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1376 health_code_update();
1382 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1383 struct lttng_event_exclusion
*exclusion
)
1385 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1386 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1387 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1389 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1390 if (!ust_exclusion
) {
1395 assert(sizeof(struct lttng_event_exclusion
) ==
1396 sizeof(struct lttng_ust_event_exclusion
));
1397 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1399 return ust_exclusion
;
1403 * Set event exclusions on the tracer.
1406 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1407 struct ust_app
*app
)
1410 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1412 health_code_update();
1414 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1419 ust_exclusion
= create_ust_exclusion_from_exclusion(
1420 ua_event
->exclusion
);
1421 if (!ust_exclusion
) {
1422 ret
= -LTTNG_ERR_NOMEM
;
1425 pthread_mutex_lock(&app
->sock_lock
);
1426 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1427 pthread_mutex_unlock(&app
->sock_lock
);
1429 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1430 ERR("UST app event %s exclusions failed for app (pid: %d) "
1431 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1434 * This is normal behavior, an application can die during the
1435 * creation process. Don't report an error so the execution can
1436 * continue normally.
1439 DBG3("UST app event exclusion failed. Application is dead.");
1444 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1447 health_code_update();
1448 free(ust_exclusion
);
1453 * Disable the specified event on to UST tracer for the UST session.
1455 static int disable_ust_event(struct ust_app
*app
,
1456 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1460 health_code_update();
1462 pthread_mutex_lock(&app
->sock_lock
);
1463 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1464 pthread_mutex_unlock(&app
->sock_lock
);
1466 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1467 ERR("UST app event %s disable failed for app (pid: %d) "
1468 "and session handle %d with ret %d",
1469 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1472 * This is normal behavior, an application can die during the
1473 * creation process. Don't report an error so the execution can
1474 * continue normally.
1477 DBG3("UST app disable event failed. Application is dead.");
1482 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1483 ua_event
->attr
.name
, app
->pid
);
1486 health_code_update();
1491 * Disable the specified channel on to UST tracer for the UST session.
1493 static int disable_ust_channel(struct ust_app
*app
,
1494 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1498 health_code_update();
1500 pthread_mutex_lock(&app
->sock_lock
);
1501 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1502 pthread_mutex_unlock(&app
->sock_lock
);
1504 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1505 ERR("UST app channel %s disable failed for app (pid: %d) "
1506 "and session handle %d with ret %d",
1507 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1510 * This is normal behavior, an application can die during the
1511 * creation process. Don't report an error so the execution can
1512 * continue normally.
1515 DBG3("UST app disable channel failed. Application is dead.");
1520 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1521 ua_chan
->name
, app
->pid
);
1524 health_code_update();
1529 * Enable the specified channel on to UST tracer for the UST session.
1531 static int enable_ust_channel(struct ust_app
*app
,
1532 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1536 health_code_update();
1538 pthread_mutex_lock(&app
->sock_lock
);
1539 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1540 pthread_mutex_unlock(&app
->sock_lock
);
1542 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1543 ERR("UST app channel %s enable failed for app (pid: %d) "
1544 "and session handle %d with ret %d",
1545 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1548 * This is normal behavior, an application can die during the
1549 * creation process. Don't report an error so the execution can
1550 * continue normally.
1553 DBG3("UST app enable channel failed. Application is dead.");
1558 ua_chan
->enabled
= 1;
1560 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1561 ua_chan
->name
, app
->pid
);
1564 health_code_update();
1569 * Enable the specified event on to UST tracer for the UST session.
1571 static int enable_ust_event(struct ust_app
*app
,
1572 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1576 health_code_update();
1578 pthread_mutex_lock(&app
->sock_lock
);
1579 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1580 pthread_mutex_unlock(&app
->sock_lock
);
1582 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1583 ERR("UST app event %s enable failed for app (pid: %d) "
1584 "and session handle %d with ret %d",
1585 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1588 * This is normal behavior, an application can die during the
1589 * creation process. Don't report an error so the execution can
1590 * continue normally.
1593 DBG3("UST app enable event failed. Application is dead.");
1598 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1599 ua_event
->attr
.name
, app
->pid
);
1602 health_code_update();
1607 * Send channel and stream buffer to application.
1609 * Return 0 on success. On error, a negative value is returned.
1611 static int send_channel_pid_to_ust(struct ust_app
*app
,
1612 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1615 struct ust_app_stream
*stream
, *stmp
;
1621 health_code_update();
1623 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1626 /* Send channel to the application. */
1627 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1628 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1629 ret
= -ENOTCONN
; /* Caused by app exiting. */
1631 } else if (ret
< 0) {
1635 health_code_update();
1637 /* Send all streams to application. */
1638 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1639 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1640 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1641 ret
= -ENOTCONN
; /* Caused by app exiting. */
1643 } else if (ret
< 0) {
1646 /* We don't need the stream anymore once sent to the tracer. */
1647 cds_list_del(&stream
->list
);
1648 delete_ust_app_stream(-1, stream
, app
);
1650 /* Flag the channel that it is sent to the application. */
1651 ua_chan
->is_sent
= 1;
1654 health_code_update();
1659 * Create the specified event onto the UST tracer for a UST session.
1661 * Should be called with session mutex held.
1664 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1665 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1669 health_code_update();
1671 /* Create UST event on tracer */
1672 pthread_mutex_lock(&app
->sock_lock
);
1673 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1675 pthread_mutex_unlock(&app
->sock_lock
);
1677 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1678 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1679 ua_event
->attr
.name
, app
->pid
, ret
);
1682 * This is normal behavior, an application can die during the
1683 * creation process. Don't report an error so the execution can
1684 * continue normally.
1687 DBG3("UST app create event failed. Application is dead.");
1692 ua_event
->handle
= ua_event
->obj
->handle
;
1694 DBG2("UST app event %s created successfully for pid:%d",
1695 ua_event
->attr
.name
, app
->pid
);
1697 health_code_update();
1699 /* Set filter if one is present. */
1700 if (ua_event
->filter
) {
1701 ret
= set_ust_event_filter(ua_event
, app
);
1707 /* Set exclusions for the event */
1708 if (ua_event
->exclusion
) {
1709 ret
= set_ust_event_exclusion(ua_event
, app
);
1715 /* If event not enabled, disable it on the tracer */
1716 if (ua_event
->enabled
) {
1718 * We now need to explicitly enable the event, since it
1719 * is now disabled at creation.
1721 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1724 * If we hit an EPERM, something is wrong with our enable call. If
1725 * we get an EEXIST, there is a problem on the tracer side since we
1729 case -LTTNG_UST_ERR_PERM
:
1730 /* Code flow problem */
1732 case -LTTNG_UST_ERR_EXIST
:
1733 /* It's OK for our use case. */
1744 health_code_update();
1749 * Copy data between an UST app event and a LTT event.
1751 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1752 struct ltt_ust_event
*uevent
)
1754 size_t exclusion_alloc_size
;
1756 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1757 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1759 ua_event
->enabled
= uevent
->enabled
;
1761 /* Copy event attributes */
1762 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1764 /* Copy filter bytecode */
1765 if (uevent
->filter
) {
1766 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1767 /* Filter might be NULL here in case of ENONEM. */
1770 /* Copy exclusion data */
1771 if (uevent
->exclusion
) {
1772 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1773 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1774 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1775 if (ua_event
->exclusion
== NULL
) {
1778 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1779 exclusion_alloc_size
);
1785 * Copy data between an UST app channel and a LTT channel.
1787 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1788 struct ltt_ust_channel
*uchan
)
1790 struct lttng_ht_iter iter
;
1791 struct ltt_ust_event
*uevent
;
1792 struct ltt_ust_context
*uctx
;
1793 struct ust_app_event
*ua_event
;
1795 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1797 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1798 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1800 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1801 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1803 /* Copy event attributes since the layout is different. */
1804 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1805 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1806 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1807 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1808 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1809 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1810 ua_chan
->attr
.output
= uchan
->attr
.output
;
1811 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
1814 * Note that the attribute channel type is not set since the channel on the
1815 * tracing registry side does not have this information.
1818 ua_chan
->enabled
= uchan
->enabled
;
1819 ua_chan
->tracing_channel_id
= uchan
->id
;
1821 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1822 struct ust_app_ctx
*ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1824 if (ua_ctx
== NULL
) {
1827 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1828 (unsigned long) ua_ctx
->ctx
.ctx
);
1829 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1830 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1833 /* Copy all events from ltt ust channel to ust app channel */
1834 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1835 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1836 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1837 if (ua_event
== NULL
) {
1838 DBG2("UST event %s not found on shadow copy channel",
1840 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1841 if (ua_event
== NULL
) {
1844 shadow_copy_event(ua_event
, uevent
);
1845 add_unique_ust_app_event(ua_chan
, ua_event
);
1849 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1853 * Copy data between a UST app session and a regular LTT session.
1855 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1856 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1858 struct lttng_ht_node_str
*ua_chan_node
;
1859 struct lttng_ht_iter iter
;
1860 struct ltt_ust_channel
*uchan
;
1861 struct ust_app_channel
*ua_chan
;
1863 struct tm
*timeinfo
;
1866 char tmp_shm_path
[PATH_MAX
];
1868 /* Get date and time for unique app path */
1870 timeinfo
= localtime(&rawtime
);
1871 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1873 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1875 ua_sess
->tracing_id
= usess
->id
;
1876 ua_sess
->id
= get_next_session_id();
1877 ua_sess
->uid
= app
->uid
;
1878 ua_sess
->gid
= app
->gid
;
1879 ua_sess
->euid
= usess
->uid
;
1880 ua_sess
->egid
= usess
->gid
;
1881 ua_sess
->buffer_type
= usess
->buffer_type
;
1882 ua_sess
->bits_per_long
= app
->bits_per_long
;
1884 /* There is only one consumer object per session possible. */
1885 consumer_output_get(usess
->consumer
);
1886 ua_sess
->consumer
= usess
->consumer
;
1888 ua_sess
->output_traces
= usess
->output_traces
;
1889 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1890 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1891 &usess
->metadata_attr
);
1893 switch (ua_sess
->buffer_type
) {
1894 case LTTNG_BUFFER_PER_PID
:
1895 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1896 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1899 case LTTNG_BUFFER_PER_UID
:
1900 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1901 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1908 PERROR("asprintf UST shadow copy session");
1913 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1914 sizeof(ua_sess
->root_shm_path
));
1915 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1916 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1917 sizeof(ua_sess
->shm_path
));
1918 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1919 if (ua_sess
->shm_path
[0]) {
1920 switch (ua_sess
->buffer_type
) {
1921 case LTTNG_BUFFER_PER_PID
:
1922 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1923 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1924 app
->name
, app
->pid
, datetime
);
1926 case LTTNG_BUFFER_PER_UID
:
1927 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1928 DEFAULT_UST_TRACE_UID_PATH
,
1929 app
->uid
, app
->bits_per_long
);
1936 PERROR("sprintf UST shadow copy session");
1940 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1941 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1942 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1945 /* Iterate over all channels in global domain. */
1946 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1948 struct lttng_ht_iter uiter
;
1950 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1951 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1952 if (ua_chan_node
!= NULL
) {
1953 /* Session exist. Contiuing. */
1957 DBG2("Channel %s not found on shadow session copy, creating it",
1959 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
,
1961 if (ua_chan
== NULL
) {
1962 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1965 shadow_copy_channel(ua_chan
, uchan
);
1967 * The concept of metadata channel does not exist on the tracing
1968 * registry side of the session daemon so this can only be a per CPU
1969 * channel and not metadata.
1971 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1973 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1978 consumer_output_put(ua_sess
->consumer
);
1982 * Lookup sesison wrapper.
1985 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1986 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1988 /* Get right UST app session from app */
1989 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1993 * Return ust app session from the app session hashtable using the UST session
1996 static struct ust_app_session
*lookup_session_by_app(
1997 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1999 struct lttng_ht_iter iter
;
2000 struct lttng_ht_node_u64
*node
;
2002 __lookup_session_by_app(usess
, app
, &iter
);
2003 node
= lttng_ht_iter_get_node_u64(&iter
);
2008 return caa_container_of(node
, struct ust_app_session
, node
);
2015 * Setup buffer registry per PID for the given session and application. If none
2016 * is found, a new one is created, added to the global registry and
2017 * initialized. If regp is valid, it's set with the newly created object.
2019 * Return 0 on success or else a negative value.
2021 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2022 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2025 struct buffer_reg_pid
*reg_pid
;
2032 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2035 * This is the create channel path meaning that if there is NO
2036 * registry available, we have to create one for this session.
2038 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2039 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2047 /* Initialize registry. */
2048 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2049 app
->bits_per_long
, app
->uint8_t_alignment
,
2050 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2051 app
->uint64_t_alignment
, app
->long_alignment
,
2052 app
->byte_order
, app
->version
.major
,
2053 app
->version
.minor
, reg_pid
->root_shm_path
,
2055 ua_sess
->euid
, ua_sess
->egid
);
2058 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2059 * destroy the buffer registry, because it is always expected
2060 * that if the buffer registry can be found, its ust registry is
2063 buffer_reg_pid_destroy(reg_pid
);
2067 buffer_reg_pid_add(reg_pid
);
2069 DBG3("UST app buffer registry per PID created successfully");
2081 * Setup buffer registry per UID for the given session and application. If none
2082 * is found, a new one is created, added to the global registry and
2083 * initialized. If regp is valid, it's set with the newly created object.
2085 * Return 0 on success or else a negative value.
2087 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2088 struct ust_app_session
*ua_sess
,
2089 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2092 struct buffer_reg_uid
*reg_uid
;
2099 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2102 * This is the create channel path meaning that if there is NO
2103 * registry available, we have to create one for this session.
2105 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2106 LTTNG_DOMAIN_UST
, ®_uid
,
2107 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2115 /* Initialize registry. */
2116 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2117 app
->bits_per_long
, app
->uint8_t_alignment
,
2118 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2119 app
->uint64_t_alignment
, app
->long_alignment
,
2120 app
->byte_order
, app
->version
.major
,
2121 app
->version
.minor
, reg_uid
->root_shm_path
,
2122 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
2125 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2126 * destroy the buffer registry, because it is always expected
2127 * that if the buffer registry can be found, its ust registry is
2130 buffer_reg_uid_destroy(reg_uid
, NULL
);
2133 /* Add node to teardown list of the session. */
2134 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2136 buffer_reg_uid_add(reg_uid
);
2138 DBG3("UST app buffer registry per UID created successfully");
2149 * Create a session on the tracer side for the given app.
2151 * On success, ua_sess_ptr is populated with the session pointer or else left
2152 * untouched. If the session was created, is_created is set to 1. On error,
2153 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2156 * Returns 0 on success or else a negative code which is either -ENOMEM or
2157 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2159 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2160 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2163 int ret
, created
= 0;
2164 struct ust_app_session
*ua_sess
;
2168 assert(ua_sess_ptr
);
2170 health_code_update();
2172 ua_sess
= lookup_session_by_app(usess
, app
);
2173 if (ua_sess
== NULL
) {
2174 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2175 app
->pid
, usess
->id
);
2176 ua_sess
= alloc_ust_app_session();
2177 if (ua_sess
== NULL
) {
2178 /* Only malloc can failed so something is really wrong */
2182 shadow_copy_session(ua_sess
, usess
, app
);
2186 switch (usess
->buffer_type
) {
2187 case LTTNG_BUFFER_PER_PID
:
2188 /* Init local registry. */
2189 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2191 delete_ust_app_session(-1, ua_sess
, app
);
2195 case LTTNG_BUFFER_PER_UID
:
2196 /* Look for a global registry. If none exists, create one. */
2197 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2199 delete_ust_app_session(-1, ua_sess
, app
);
2209 health_code_update();
2211 if (ua_sess
->handle
== -1) {
2212 pthread_mutex_lock(&app
->sock_lock
);
2213 ret
= ustctl_create_session(app
->sock
);
2214 pthread_mutex_unlock(&app
->sock_lock
);
2216 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2217 ERR("Creating session for app pid %d with ret %d",
2220 DBG("UST app creating session failed. Application is dead");
2222 * This is normal behavior, an application can die during the
2223 * creation process. Don't report an error so the execution can
2224 * continue normally. This will get flagged ENOTCONN and the
2225 * caller will handle it.
2229 delete_ust_app_session(-1, ua_sess
, app
);
2230 if (ret
!= -ENOMEM
) {
2232 * Tracer is probably gone or got an internal error so let's
2233 * behave like it will soon unregister or not usable.
2240 ua_sess
->handle
= ret
;
2242 /* Add ust app session to app's HT */
2243 lttng_ht_node_init_u64(&ua_sess
->node
,
2244 ua_sess
->tracing_id
);
2245 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2246 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2247 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2248 &ua_sess
->ust_objd_node
);
2250 DBG2("UST app session created successfully with handle %d", ret
);
2253 *ua_sess_ptr
= ua_sess
;
2255 *is_created
= created
;
2258 /* Everything went well. */
2262 health_code_update();
2267 * Match function for a hash table lookup of ust_app_ctx.
2269 * It matches an ust app context based on the context type and, in the case
2270 * of perf counters, their name.
2272 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2274 struct ust_app_ctx
*ctx
;
2275 const struct lttng_ust_context_attr
*key
;
2280 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2284 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2289 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2290 if (strncmp(key
->u
.perf_counter
.name
,
2291 ctx
->ctx
.u
.perf_counter
.name
,
2292 sizeof(key
->u
.perf_counter
.name
))) {
2296 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2297 if (strcmp(key
->u
.app_ctx
.provider_name
,
2298 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2299 strcmp(key
->u
.app_ctx
.ctx_name
,
2300 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2316 * Lookup for an ust app context from an lttng_ust_context.
2318 * Must be called while holding RCU read side lock.
2319 * Return an ust_app_ctx object or NULL on error.
2322 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2323 struct lttng_ust_context_attr
*uctx
)
2325 struct lttng_ht_iter iter
;
2326 struct lttng_ht_node_ulong
*node
;
2327 struct ust_app_ctx
*app_ctx
= NULL
;
2332 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2333 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2334 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2335 node
= lttng_ht_iter_get_node_ulong(&iter
);
2340 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2347 * Create a context for the channel on the tracer.
2349 * Called with UST app session lock held and a RCU read side lock.
2352 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2353 struct lttng_ust_context_attr
*uctx
,
2354 struct ust_app
*app
)
2357 struct ust_app_ctx
*ua_ctx
;
2359 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2361 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2367 ua_ctx
= alloc_ust_app_ctx(uctx
);
2368 if (ua_ctx
== NULL
) {
2374 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2375 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2376 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2378 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2388 * Enable on the tracer side a ust app event for the session and channel.
2390 * Called with UST app session lock held.
2393 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2394 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2398 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2403 ua_event
->enabled
= 1;
2410 * Disable on the tracer side a ust app event for the session and channel.
2412 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2413 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2417 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2422 ua_event
->enabled
= 0;
2429 * Lookup ust app channel for session and disable it on the tracer side.
2432 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2433 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2437 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2442 ua_chan
->enabled
= 0;
2449 * Lookup ust app channel for session and enable it on the tracer side. This
2450 * MUST be called with a RCU read side lock acquired.
2452 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2453 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2456 struct lttng_ht_iter iter
;
2457 struct lttng_ht_node_str
*ua_chan_node
;
2458 struct ust_app_channel
*ua_chan
;
2460 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2461 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2462 if (ua_chan_node
== NULL
) {
2463 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2464 uchan
->name
, ua_sess
->tracing_id
);
2468 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2470 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2480 * Ask the consumer to create a channel and get it if successful.
2482 * Called with UST app session lock held.
2484 * Return 0 on success or else a negative value.
2486 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2487 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2488 int bitness
, struct ust_registry_session
*registry
,
2489 uint64_t trace_archive_id
)
2492 unsigned int nb_fd
= 0;
2493 struct consumer_socket
*socket
;
2501 health_code_update();
2503 /* Get the right consumer socket for the application. */
2504 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2510 health_code_update();
2512 /* Need one fd for the channel. */
2513 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2515 ERR("Exhausted number of available FD upon create channel");
2520 * Ask consumer to create channel. The consumer will return the number of
2521 * stream we have to expect.
2523 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2524 registry
, trace_archive_id
);
2530 * Compute the number of fd needed before receiving them. It must be 2 per
2531 * stream (2 being the default value here).
2533 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2535 /* Reserve the amount of file descriptor we need. */
2536 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2538 ERR("Exhausted number of available FD upon create channel");
2539 goto error_fd_get_stream
;
2542 health_code_update();
2545 * Now get the channel from the consumer. This call wil populate the stream
2546 * list of that channel and set the ust objects.
2548 if (usess
->consumer
->enabled
) {
2549 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2559 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2560 error_fd_get_stream
:
2562 * Initiate a destroy channel on the consumer since we had an error
2563 * handling it on our side. The return value is of no importance since we
2564 * already have a ret value set by the previous error that we need to
2567 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2569 lttng_fd_put(LTTNG_FD_APPS
, 1);
2571 health_code_update();
2577 * Duplicate the ust data object of the ust app stream and save it in the
2578 * buffer registry stream.
2580 * Return 0 on success or else a negative value.
2582 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2583 struct ust_app_stream
*stream
)
2590 /* Reserve the amount of file descriptor we need. */
2591 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2593 ERR("Exhausted number of available FD upon duplicate stream");
2597 /* Duplicate object for stream once the original is in the registry. */
2598 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2599 reg_stream
->obj
.ust
);
2601 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2602 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2603 lttng_fd_put(LTTNG_FD_APPS
, 2);
2606 stream
->handle
= stream
->obj
->handle
;
2613 * Duplicate the ust data object of the ust app. channel and save it in the
2614 * buffer registry channel.
2616 * Return 0 on success or else a negative value.
2618 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2619 struct ust_app_channel
*ua_chan
)
2626 /* Need two fds for the channel. */
2627 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2629 ERR("Exhausted number of available FD upon duplicate channel");
2633 /* Duplicate object for stream once the original is in the registry. */
2634 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2636 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2637 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2640 ua_chan
->handle
= ua_chan
->obj
->handle
;
2645 lttng_fd_put(LTTNG_FD_APPS
, 1);
2651 * For a given channel buffer registry, setup all streams of the given ust
2652 * application channel.
2654 * Return 0 on success or else a negative value.
2656 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2657 struct ust_app_channel
*ua_chan
,
2658 struct ust_app
*app
)
2661 struct ust_app_stream
*stream
, *stmp
;
2666 DBG2("UST app setup buffer registry stream");
2668 /* Send all streams to application. */
2669 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2670 struct buffer_reg_stream
*reg_stream
;
2672 ret
= buffer_reg_stream_create(®_stream
);
2678 * Keep original pointer and nullify it in the stream so the delete
2679 * stream call does not release the object.
2681 reg_stream
->obj
.ust
= stream
->obj
;
2683 buffer_reg_stream_add(reg_stream
, reg_chan
);
2685 /* We don't need the streams anymore. */
2686 cds_list_del(&stream
->list
);
2687 delete_ust_app_stream(-1, stream
, app
);
2695 * Create a buffer registry channel for the given session registry and
2696 * application channel object. If regp pointer is valid, it's set with the
2697 * created object. Important, the created object is NOT added to the session
2698 * registry hash table.
2700 * Return 0 on success else a negative value.
2702 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2703 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2706 struct buffer_reg_channel
*reg_chan
= NULL
;
2711 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2713 /* Create buffer registry channel. */
2714 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2719 reg_chan
->consumer_key
= ua_chan
->key
;
2720 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2721 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2723 /* Create and add a channel registry to session. */
2724 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2725 ua_chan
->tracing_channel_id
);
2729 buffer_reg_channel_add(reg_sess
, reg_chan
);
2738 /* Safe because the registry channel object was not added to any HT. */
2739 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2745 * Setup buffer registry channel for the given session registry and application
2746 * channel object. If regp pointer is valid, it's set with the created object.
2748 * Return 0 on success else a negative value.
2750 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2751 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2752 struct ust_app
*app
)
2759 assert(ua_chan
->obj
);
2761 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2763 /* Setup all streams for the registry. */
2764 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2769 reg_chan
->obj
.ust
= ua_chan
->obj
;
2770 ua_chan
->obj
= NULL
;
2775 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2776 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2781 * Send buffer registry channel to the application.
2783 * Return 0 on success else a negative value.
2785 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2786 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2787 struct ust_app_channel
*ua_chan
)
2790 struct buffer_reg_stream
*reg_stream
;
2797 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2799 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2804 /* Send channel to the application. */
2805 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2806 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2807 ret
= -ENOTCONN
; /* Caused by app exiting. */
2809 } else if (ret
< 0) {
2813 health_code_update();
2815 /* Send all streams to application. */
2816 pthread_mutex_lock(®_chan
->stream_list_lock
);
2817 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2818 struct ust_app_stream stream
;
2820 ret
= duplicate_stream_object(reg_stream
, &stream
);
2822 goto error_stream_unlock
;
2825 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2827 (void) release_ust_app_stream(-1, &stream
, app
);
2828 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2829 ret
= -ENOTCONN
; /* Caused by app exiting. */
2831 goto error_stream_unlock
;
2835 * The return value is not important here. This function will output an
2838 (void) release_ust_app_stream(-1, &stream
, app
);
2840 ua_chan
->is_sent
= 1;
2842 error_stream_unlock
:
2843 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2849 * Create and send to the application the created buffers with per UID buffers.
2851 * This MUST be called with a RCU read side lock acquired.
2852 * The session list lock and the session's lock must be acquired.
2854 * Return 0 on success else a negative value.
2856 static int create_channel_per_uid(struct ust_app
*app
,
2857 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2858 struct ust_app_channel
*ua_chan
)
2861 struct buffer_reg_uid
*reg_uid
;
2862 struct buffer_reg_channel
*reg_chan
;
2863 struct ltt_session
*session
;
2864 enum lttng_error_code notification_ret
;
2865 struct ust_registry_channel
*chan_reg
;
2872 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2874 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2876 * The session creation handles the creation of this global registry
2877 * object. If none can be find, there is a code flow problem or a
2882 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2888 /* Create the buffer registry channel object. */
2889 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2891 ERR("Error creating the UST channel \"%s\" registry instance",
2896 session
= session_find_by_id(ua_sess
->tracing_id
);
2898 assert(pthread_mutex_trylock(&session
->lock
));
2899 assert(session_trylock_list());
2902 * Create the buffers on the consumer side. This call populates the
2903 * ust app channel object with all streams and data object.
2905 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2906 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
2907 session
->current_archive_id
);
2909 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2913 * Let's remove the previously created buffer registry channel so
2914 * it's not visible anymore in the session registry.
2916 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2917 ua_chan
->tracing_channel_id
, false);
2918 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2919 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2924 * Setup the streams and add it to the session registry.
2926 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2927 ua_chan
, reg_chan
, app
);
2929 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
2933 /* Notify the notification subsystem of the channel's creation. */
2934 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2935 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2936 ua_chan
->tracing_channel_id
);
2938 chan_reg
->consumer_key
= ua_chan
->key
;
2940 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2942 notification_ret
= notification_thread_command_add_channel(
2943 notification_thread_handle
, session
->name
,
2944 ua_sess
->euid
, ua_sess
->egid
,
2948 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2949 if (notification_ret
!= LTTNG_OK
) {
2950 ret
= - (int) notification_ret
;
2951 ERR("Failed to add channel to notification thread");
2956 /* Send buffers to the application. */
2957 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2959 if (ret
!= -ENOTCONN
) {
2960 ERR("Error sending channel to application");
2970 * Create and send to the application the created buffers with per PID buffers.
2972 * Called with UST app session lock held.
2973 * The session list lock and the session's lock must be acquired.
2975 * Return 0 on success else a negative value.
2977 static int create_channel_per_pid(struct ust_app
*app
,
2978 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2979 struct ust_app_channel
*ua_chan
)
2982 struct ust_registry_session
*registry
;
2983 enum lttng_error_code cmd_ret
;
2984 struct ltt_session
*session
;
2985 uint64_t chan_reg_key
;
2986 struct ust_registry_channel
*chan_reg
;
2993 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2997 registry
= get_session_registry(ua_sess
);
2998 /* The UST app session lock is held, registry shall not be null. */
3001 /* Create and add a new channel registry to session. */
3002 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3004 ERR("Error creating the UST channel \"%s\" registry instance",
3009 session
= session_find_by_id(ua_sess
->tracing_id
);
3012 assert(pthread_mutex_trylock(&session
->lock
));
3013 assert(session_trylock_list());
3015 /* Create and get channel on the consumer side. */
3016 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3017 app
->bits_per_long
, registry
,
3018 session
->current_archive_id
);
3020 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3025 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3027 if (ret
!= -ENOTCONN
) {
3028 ERR("Error sending channel to application");
3033 chan_reg_key
= ua_chan
->key
;
3034 pthread_mutex_lock(®istry
->lock
);
3035 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3037 chan_reg
->consumer_key
= ua_chan
->key
;
3038 pthread_mutex_unlock(®istry
->lock
);
3040 cmd_ret
= notification_thread_command_add_channel(
3041 notification_thread_handle
, session
->name
,
3042 ua_sess
->euid
, ua_sess
->egid
,
3046 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3047 if (cmd_ret
!= LTTNG_OK
) {
3048 ret
= - (int) cmd_ret
;
3049 ERR("Failed to add channel to notification thread");
3059 * From an already allocated ust app channel, create the channel buffers if
3060 * need and send it to the application. This MUST be called with a RCU read
3061 * side lock acquired.
3063 * Called with UST app session lock held.
3065 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3066 * the application exited concurrently.
3068 static int do_create_channel(struct ust_app
*app
,
3069 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3070 struct ust_app_channel
*ua_chan
)
3079 /* Handle buffer type before sending the channel to the application. */
3080 switch (usess
->buffer_type
) {
3081 case LTTNG_BUFFER_PER_UID
:
3083 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3089 case LTTNG_BUFFER_PER_PID
:
3091 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3103 /* Initialize ust objd object using the received handle and add it. */
3104 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3105 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3107 /* If channel is not enabled, disable it on the tracer */
3108 if (!ua_chan
->enabled
) {
3109 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3120 * Create UST app channel and create it on the tracer. Set ua_chanp of the
3121 * newly created channel if not NULL.
3123 * Called with UST app session lock and RCU read-side lock held.
3125 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3126 * the application exited concurrently.
3128 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
3129 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
3130 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3131 struct ust_app_channel
**ua_chanp
)
3134 struct lttng_ht_iter iter
;
3135 struct lttng_ht_node_str
*ua_chan_node
;
3136 struct ust_app_channel
*ua_chan
;
3138 /* Lookup channel in the ust app session */
3139 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3140 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3141 if (ua_chan_node
!= NULL
) {
3142 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3146 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3147 if (ua_chan
== NULL
) {
3148 /* Only malloc can fail here */
3152 shadow_copy_channel(ua_chan
, uchan
);
3154 /* Set channel type. */
3155 ua_chan
->attr
.type
= type
;
3157 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
3162 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
3165 /* Only add the channel if successful on the tracer side. */
3166 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3169 *ua_chanp
= ua_chan
;
3172 /* Everything went well. */
3176 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
3182 * Create UST app event and create it on the tracer side.
3184 * Called with ust app session mutex held.
3187 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3188 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3189 struct ust_app
*app
)
3192 struct ust_app_event
*ua_event
;
3194 /* Get event node */
3195 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3196 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3197 if (ua_event
!= NULL
) {
3202 /* Does not exist so create one */
3203 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3204 if (ua_event
== NULL
) {
3205 /* Only malloc can failed so something is really wrong */
3209 shadow_copy_event(ua_event
, uevent
);
3211 /* Create it on the tracer side */
3212 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3214 /* Not found previously means that it does not exist on the tracer */
3215 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
3219 add_unique_ust_app_event(ua_chan
, ua_event
);
3221 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3228 /* Valid. Calling here is already in a read side lock */
3229 delete_ust_app_event(-1, ua_event
, app
);
3234 * Create UST metadata and open it on the tracer side.
3236 * Called with UST app session lock held and RCU read side lock.
3238 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3239 struct ust_app
*app
, struct consumer_output
*consumer
)
3242 struct ust_app_channel
*metadata
;
3243 struct consumer_socket
*socket
;
3244 struct ust_registry_session
*registry
;
3245 struct ltt_session
*session
;
3251 registry
= get_session_registry(ua_sess
);
3252 /* The UST app session is held registry shall not be null. */
3255 pthread_mutex_lock(®istry
->lock
);
3257 /* Metadata already exists for this registry or it was closed previously */
3258 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3263 /* Allocate UST metadata */
3264 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3266 /* malloc() failed */
3271 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3273 /* Need one fd for the channel. */
3274 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3276 ERR("Exhausted number of available FD upon create metadata");
3280 /* Get the right consumer socket for the application. */
3281 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3284 goto error_consumer
;
3288 * Keep metadata key so we can identify it on the consumer side. Assign it
3289 * to the registry *before* we ask the consumer so we avoid the race of the
3290 * consumer requesting the metadata and the ask_channel call on our side
3291 * did not returned yet.
3293 registry
->metadata_key
= metadata
->key
;
3295 session
= session_find_by_id(ua_sess
->tracing_id
);
3298 assert(pthread_mutex_trylock(&session
->lock
));
3299 assert(session_trylock_list());
3302 * Ask the metadata channel creation to the consumer. The metadata object
3303 * will be created by the consumer and kept their. However, the stream is
3304 * never added or monitored until we do a first push metadata to the
3307 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3308 registry
, session
->current_archive_id
);
3310 /* Nullify the metadata key so we don't try to close it later on. */
3311 registry
->metadata_key
= 0;
3312 goto error_consumer
;
3316 * The setup command will make the metadata stream be sent to the relayd,
3317 * if applicable, and the thread managing the metadatas. This is important
3318 * because after this point, if an error occurs, the only way the stream
3319 * can be deleted is to be monitored in the consumer.
3321 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3323 /* Nullify the metadata key so we don't try to close it later on. */
3324 registry
->metadata_key
= 0;
3325 goto error_consumer
;
3328 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3329 metadata
->key
, app
->pid
);
3332 lttng_fd_put(LTTNG_FD_APPS
, 1);
3333 delete_ust_app_channel(-1, metadata
, app
);
3335 pthread_mutex_unlock(®istry
->lock
);
3340 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3341 * acquired before calling this function.
3343 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3345 struct ust_app
*app
= NULL
;
3346 struct lttng_ht_node_ulong
*node
;
3347 struct lttng_ht_iter iter
;
3349 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3350 node
= lttng_ht_iter_get_node_ulong(&iter
);
3352 DBG2("UST app no found with pid %d", pid
);
3356 DBG2("Found UST app by pid %d", pid
);
3358 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3365 * Allocate and init an UST app object using the registration information and
3366 * the command socket. This is called when the command socket connects to the
3369 * The object is returned on success or else NULL.
3371 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3373 struct ust_app
*lta
= NULL
;
3378 DBG3("UST app creating application for socket %d", sock
);
3380 if ((msg
->bits_per_long
== 64 &&
3381 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3382 || (msg
->bits_per_long
== 32 &&
3383 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3384 ERR("Registration failed: application \"%s\" (pid: %d) has "
3385 "%d-bit long, but no consumerd for this size is available.\n",
3386 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3390 lta
= zmalloc(sizeof(struct ust_app
));
3396 lta
->ppid
= msg
->ppid
;
3397 lta
->uid
= msg
->uid
;
3398 lta
->gid
= msg
->gid
;
3400 lta
->bits_per_long
= msg
->bits_per_long
;
3401 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3402 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3403 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3404 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3405 lta
->long_alignment
= msg
->long_alignment
;
3406 lta
->byte_order
= msg
->byte_order
;
3408 lta
->v_major
= msg
->major
;
3409 lta
->v_minor
= msg
->minor
;
3410 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3411 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3412 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3413 lta
->notify_sock
= -1;
3415 /* Copy name and make sure it's NULL terminated. */
3416 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3417 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3420 * Before this can be called, when receiving the registration information,
3421 * the application compatibility is checked. So, at this point, the
3422 * application can work with this session daemon.
3424 lta
->compatible
= 1;
3426 lta
->pid
= msg
->pid
;
3427 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3429 pthread_mutex_init(<a
->sock_lock
, NULL
);
3430 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3432 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3438 * For a given application object, add it to every hash table.
3440 void ust_app_add(struct ust_app
*app
)
3443 assert(app
->notify_sock
>= 0);
3448 * On a re-registration, we want to kick out the previous registration of
3451 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3454 * The socket _should_ be unique until _we_ call close. So, a add_unique
3455 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3456 * already in the table.
3458 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3460 /* Add application to the notify socket hash table. */
3461 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3462 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3464 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3465 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3466 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3473 * Set the application version into the object.
3475 * Return 0 on success else a negative value either an errno code or a
3476 * LTTng-UST error code.
3478 int ust_app_version(struct ust_app
*app
)
3484 pthread_mutex_lock(&app
->sock_lock
);
3485 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3486 pthread_mutex_unlock(&app
->sock_lock
);
3488 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3489 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3491 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3499 * Unregister app by removing it from the global traceable app list and freeing
3502 * The socket is already closed at this point so no close to sock.
3504 void ust_app_unregister(int sock
)
3506 struct ust_app
*lta
;
3507 struct lttng_ht_node_ulong
*node
;
3508 struct lttng_ht_iter ust_app_sock_iter
;
3509 struct lttng_ht_iter iter
;
3510 struct ust_app_session
*ua_sess
;
3515 /* Get the node reference for a call_rcu */
3516 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3517 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3520 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3521 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3524 * For per-PID buffers, perform "push metadata" and flush all
3525 * application streams before removing app from hash tables,
3526 * ensuring proper behavior of data_pending check.
3527 * Remove sessions so they are not visible during deletion.
3529 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3531 struct ust_registry_session
*registry
;
3533 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3535 /* The session was already removed so scheduled for teardown. */
3539 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3540 (void) ust_app_flush_app_session(lta
, ua_sess
);
3544 * Add session to list for teardown. This is safe since at this point we
3545 * are the only one using this list.
3547 pthread_mutex_lock(&ua_sess
->lock
);
3549 if (ua_sess
->deleted
) {
3550 pthread_mutex_unlock(&ua_sess
->lock
);
3555 * Normally, this is done in the delete session process which is
3556 * executed in the call rcu below. However, upon registration we can't
3557 * afford to wait for the grace period before pushing data or else the
3558 * data pending feature can race between the unregistration and stop
3559 * command where the data pending command is sent *before* the grace
3562 * The close metadata below nullifies the metadata pointer in the
3563 * session so the delete session will NOT push/close a second time.
3565 registry
= get_session_registry(ua_sess
);
3567 /* Push metadata for application before freeing the application. */
3568 (void) push_metadata(registry
, ua_sess
->consumer
);
3571 * Don't ask to close metadata for global per UID buffers. Close
3572 * metadata only on destroy trace session in this case. Also, the
3573 * previous push metadata could have flag the metadata registry to
3574 * close so don't send a close command if closed.
3576 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3577 /* And ask to close it for this session registry. */
3578 (void) close_metadata(registry
, ua_sess
->consumer
);
3581 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3583 pthread_mutex_unlock(&ua_sess
->lock
);
3586 /* Remove application from PID hash table */
3587 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3591 * Remove application from notify hash table. The thread handling the
3592 * notify socket could have deleted the node so ignore on error because
3593 * either way it's valid. The close of that socket is handled by the
3594 * apps_notify_thread.
3596 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3597 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3600 * Ignore return value since the node might have been removed before by an
3601 * add replace during app registration because the PID can be reassigned by
3604 iter
.iter
.node
= <a
->pid_n
.node
;
3605 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3607 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3612 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3619 * Fill events array with all events name of all registered apps.
3621 int ust_app_list_events(struct lttng_event
**events
)
3624 size_t nbmem
, count
= 0;
3625 struct lttng_ht_iter iter
;
3626 struct ust_app
*app
;
3627 struct lttng_event
*tmp_event
;
3629 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3630 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3631 if (tmp_event
== NULL
) {
3632 PERROR("zmalloc ust app events");
3639 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3640 struct lttng_ust_tracepoint_iter uiter
;
3642 health_code_update();
3644 if (!app
->compatible
) {
3646 * TODO: In time, we should notice the caller of this error by
3647 * telling him that this is a version error.
3651 pthread_mutex_lock(&app
->sock_lock
);
3652 handle
= ustctl_tracepoint_list(app
->sock
);
3654 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3655 ERR("UST app list events getting handle failed for app pid %d",
3658 pthread_mutex_unlock(&app
->sock_lock
);
3662 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3663 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3664 /* Handle ustctl error. */
3668 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3669 ERR("UST app tp list get failed for app %d with ret %d",
3672 DBG3("UST app tp list get failed. Application is dead");
3674 * This is normal behavior, an application can die during the
3675 * creation process. Don't report an error so the execution can
3676 * continue normally. Continue normal execution.
3681 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3682 if (release_ret
< 0 &&
3683 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3684 release_ret
!= -EPIPE
) {
3685 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3687 pthread_mutex_unlock(&app
->sock_lock
);
3691 health_code_update();
3692 if (count
>= nbmem
) {
3693 /* In case the realloc fails, we free the memory */
3694 struct lttng_event
*new_tmp_event
;
3697 new_nbmem
= nbmem
<< 1;
3698 DBG2("Reallocating event list from %zu to %zu entries",
3700 new_tmp_event
= realloc(tmp_event
,
3701 new_nbmem
* sizeof(struct lttng_event
));
3702 if (new_tmp_event
== NULL
) {
3705 PERROR("realloc ust app events");
3708 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3709 if (release_ret
< 0 &&
3710 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3711 release_ret
!= -EPIPE
) {
3712 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3714 pthread_mutex_unlock(&app
->sock_lock
);
3717 /* Zero the new memory */
3718 memset(new_tmp_event
+ nbmem
, 0,
3719 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3721 tmp_event
= new_tmp_event
;