2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
28 #include <urcu/compiler.h>
29 #include <lttng/ust-error.h>
32 #include <common/common.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
35 #include "buffer-registry.h"
39 #include "ust-consumer.h"
43 /* Next available channel key. */
44 static unsigned long next_channel_key
;
45 static unsigned long next_session_id
;
48 * Return the atomically incremented value of next_channel_key.
50 static inline unsigned long get_next_channel_key(void)
52 return uatomic_add_return(&next_channel_key
, 1);
56 * Return the atomically incremented value of next_session_id.
58 static inline unsigned long get_next_session_id(void)
60 return uatomic_add_return(&next_session_id
, 1);
63 static void copy_channel_attr_to_ustctl(
64 struct ustctl_consumer_channel_attr
*attr
,
65 struct lttng_ust_channel_attr
*uattr
)
67 /* Copy event attributes since the layout is different. */
68 attr
->subbuf_size
= uattr
->subbuf_size
;
69 attr
->num_subbuf
= uattr
->num_subbuf
;
70 attr
->overwrite
= uattr
->overwrite
;
71 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
72 attr
->read_timer_interval
= uattr
->read_timer_interval
;
73 attr
->output
= uattr
->output
;
77 * Match function for the hash table lookup.
79 * It matches an ust app event based on three attributes which are the event
80 * name, the filter bytecode and the loglevel.
82 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
84 struct ust_app_event
*event
;
85 const struct ust_app_ht_key
*key
;
90 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
93 /* Match the 3 elements of the key: name, filter and loglevel. */
96 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
100 /* Event loglevel. */
101 if (event
->attr
.loglevel
!= key
->loglevel
) {
102 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
103 && key
->loglevel
== 0 && event
->attr
.loglevel
== -1) {
105 * Match is accepted. This is because on event creation, the
106 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
107 * -1 are accepted for this loglevel type since 0 is the one set by
108 * the API when receiving an enable event.
115 /* One of the filters is NULL, fail. */
116 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
120 if (key
->filter
&& event
->filter
) {
121 /* Both filters exists, check length followed by the bytecode. */
122 if (event
->filter
->len
!= key
->filter
->len
||
123 memcmp(event
->filter
->data
, key
->filter
->data
,
124 event
->filter
->len
) != 0) {
137 * Unique add of an ust app event in the given ht. This uses the custom
138 * ht_match_ust_app_event match function and the event name as hash.
140 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
141 struct ust_app_event
*event
)
143 struct cds_lfht_node
*node_ptr
;
144 struct ust_app_ht_key key
;
148 assert(ua_chan
->events
);
151 ht
= ua_chan
->events
;
152 key
.name
= event
->attr
.name
;
153 key
.filter
= event
->filter
;
154 key
.loglevel
= event
->attr
.loglevel
;
156 node_ptr
= cds_lfht_add_unique(ht
->ht
,
157 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
158 ht_match_ust_app_event
, &key
, &event
->node
.node
);
159 assert(node_ptr
== &event
->node
.node
);
163 * Close the notify socket from the given RCU head object. This MUST be called
164 * through a call_rcu().
166 static void close_notify_sock_rcu(struct rcu_head
*head
)
169 struct ust_app_notify_sock_obj
*obj
=
170 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
172 /* Must have a valid fd here. */
173 assert(obj
->fd
>= 0);
175 ret
= close(obj
->fd
);
177 ERR("close notify sock %d RCU", obj
->fd
);
179 lttng_fd_put(LTTNG_FD_APPS
, 1);
185 * Return the session registry according to the buffer type of the given
188 * A registry per UID object MUST exists before calling this function or else
189 * it assert() if not found. RCU read side lock must be acquired.
191 static struct ust_registry_session
*get_session_registry(
192 struct ust_app_session
*ua_sess
)
194 struct ust_registry_session
*registry
= NULL
;
198 switch (ua_sess
->buffer_type
) {
199 case LTTNG_BUFFER_PER_PID
:
201 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
205 registry
= reg_pid
->registry
->reg
.ust
;
208 case LTTNG_BUFFER_PER_UID
:
210 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
211 ua_sess
->tracing_id
, ua_sess
->bits_per_long
, ua_sess
->uid
);
215 registry
= reg_uid
->registry
->reg
.ust
;
227 * Delete ust context safely. RCU read lock must be held before calling
231 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
238 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
239 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
240 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
241 sock
, ua_ctx
->obj
->handle
, ret
);
249 * Delete ust app event safely. RCU read lock must be held before calling
253 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
259 free(ua_event
->filter
);
261 if (ua_event
->obj
!= NULL
) {
262 ret
= ustctl_release_object(sock
, ua_event
->obj
);
263 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
264 ERR("UST app sock %d release event obj failed with ret %d",
273 * Release ust data object of the given stream.
275 * Return 0 on success or else a negative value.
277 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
284 ret
= ustctl_release_object(sock
, stream
->obj
);
285 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
286 ERR("UST app sock %d release stream obj failed with ret %d",
289 lttng_fd_put(LTTNG_FD_APPS
, 2);
297 * Delete ust app stream safely. RCU read lock must be held before calling
301 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
305 (void) release_ust_app_stream(sock
, stream
);
310 * We need to execute ht_destroy outside of RCU read-side critical
311 * section and outside of call_rcu thread, so we postpone its execution
312 * using ht_cleanup_push. It is simpler than to change the semantic of
313 * the many callers of delete_ust_app_session().
316 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
318 struct ust_app_channel
*ua_chan
=
319 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
321 ht_cleanup_push(ua_chan
->ctx
);
322 ht_cleanup_push(ua_chan
->events
);
327 * Delete ust app channel safely. RCU read lock must be held before calling
331 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
335 struct lttng_ht_iter iter
;
336 struct ust_app_event
*ua_event
;
337 struct ust_app_ctx
*ua_ctx
;
338 struct ust_app_stream
*stream
, *stmp
;
339 struct ust_registry_session
*registry
;
343 DBG3("UST app deleting channel %s", ua_chan
->name
);
346 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
347 cds_list_del(&stream
->list
);
348 delete_ust_app_stream(sock
, stream
);
352 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
353 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
355 delete_ust_app_ctx(sock
, ua_ctx
);
359 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
361 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
363 delete_ust_app_event(sock
, ua_event
);
366 /* Wipe and free registry from session registry. */
367 registry
= get_session_registry(ua_chan
->session
);
369 ust_registry_channel_del_free(registry
, ua_chan
->key
);
372 if (ua_chan
->obj
!= NULL
) {
373 /* Remove channel from application UST object descriptor. */
374 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
375 lttng_ht_del(app
->ust_objd
, &iter
);
376 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
377 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
378 ERR("UST app sock %d release channel obj failed with ret %d",
381 lttng_fd_put(LTTNG_FD_APPS
, 1);
384 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
388 * Push metadata to consumer socket.
390 * The socket lock MUST be acquired.
391 * The ust app session lock MUST be acquired.
393 * On success, return the len of metadata pushed or else a negative value.
395 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
396 struct consumer_socket
*socket
, int send_zero_data
)
399 char *metadata_str
= NULL
;
407 * On a push metadata error either the consumer is dead or the metadata
408 * channel has been destroyed because its endpoint might have died (e.g:
409 * relayd). If so, the metadata closed flag is set to 1 so we deny pushing
410 * metadata again which is not valid anymore on the consumer side.
412 * The ust app session mutex locked allows us to make this check without
415 if (registry
->metadata_closed
) {
419 pthread_mutex_lock(®istry
->lock
);
421 offset
= registry
->metadata_len_sent
;
422 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
424 DBG3("No metadata to push for metadata key %" PRIu64
,
425 registry
->metadata_key
);
427 if (send_zero_data
) {
428 DBG("No metadata to push");
434 /* Allocate only what we have to send. */
435 metadata_str
= zmalloc(len
);
437 PERROR("zmalloc ust app metadata string");
441 /* Copy what we haven't send out. */
442 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
443 registry
->metadata_len_sent
+= len
;
446 pthread_mutex_unlock(®istry
->lock
);
447 ret
= consumer_push_metadata(socket
, registry
->metadata_key
,
448 metadata_str
, len
, offset
);
459 pthread_mutex_unlock(®istry
->lock
);
466 * For a given application and session, push metadata to consumer. The session
467 * lock MUST be acquired here before calling this.
468 * Either sock or consumer is required : if sock is NULL, the default
469 * socket to send the metadata is retrieved from consumer, if sock
470 * is not NULL we use it to send the metadata.
472 * Return 0 on success else a negative error.
474 static int push_metadata(struct ust_registry_session
*registry
,
475 struct consumer_output
*consumer
)
479 struct consumer_socket
*socket
;
487 * Means that no metadata was assigned to the session. This can happens if
488 * no start has been done previously.
490 if (!registry
->metadata_key
) {
495 /* Get consumer socket to use to push the metadata.*/
496 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
500 goto error_rcu_unlock
;
504 * TODO: Currently, we hold the socket lock around sampling of the next
505 * metadata segment to ensure we send metadata over the consumer socket in
506 * the correct order. This makes the registry lock nest inside the socket
509 * Please note that this is a temporary measure: we should move this lock
510 * back into ust_consumer_push_metadata() when the consumer gets the
511 * ability to reorder the metadata it receives.
513 pthread_mutex_lock(socket
->lock
);
514 ret
= ust_app_push_metadata(registry
, socket
, 0);
515 pthread_mutex_unlock(socket
->lock
);
518 goto error_rcu_unlock
;
526 * On error, flag the registry that the metadata is closed. We were unable
527 * to push anything and this means that either the consumer is not
528 * responding or the metadata cache has been destroyed on the consumer.
530 registry
->metadata_closed
= 1;
537 * Send to the consumer a close metadata command for the given session. Once
538 * done, the metadata channel is deleted and the session metadata pointer is
539 * nullified. The session lock MUST be acquired here unless the application is
540 * in the destroy path.
542 * Return 0 on success else a negative value.
544 static int close_metadata(struct ust_registry_session
*registry
,
545 struct consumer_output
*consumer
)
548 struct consumer_socket
*socket
;
555 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
560 /* Get consumer socket to use to push the metadata.*/
561 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
568 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
575 * Metadata closed. Even on error this means that the consumer is not
576 * responding or not found so either way a second close should NOT be emit
579 registry
->metadata_closed
= 1;
586 * We need to execute ht_destroy outside of RCU read-side critical
587 * section and outside of call_rcu thread, so we postpone its execution
588 * using ht_cleanup_push. It is simpler than to change the semantic of
589 * the many callers of delete_ust_app_session().
592 void delete_ust_app_session_rcu(struct rcu_head
*head
)
594 struct ust_app_session
*ua_sess
=
595 caa_container_of(head
, struct ust_app_session
, rcu_head
);
597 ht_cleanup_push(ua_sess
->channels
);
602 * Delete ust app session safely. RCU read lock must be held before calling
606 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
610 struct lttng_ht_iter iter
;
611 struct ust_app_channel
*ua_chan
;
612 struct ust_registry_session
*registry
;
616 pthread_mutex_lock(&ua_sess
->lock
);
618 registry
= get_session_registry(ua_sess
);
619 if (registry
&& !registry
->metadata_closed
) {
620 /* Push metadata for application before freeing the application. */
621 (void) push_metadata(registry
, ua_sess
->consumer
);
624 * Don't ask to close metadata for global per UID buffers. Close
625 * metadata only on destroy trace session in this case. Also, the
626 * previous push metadata could have flag the metadata registry to
627 * close so don't send a close command if closed.
629 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
&&
630 !registry
->metadata_closed
) {
631 /* And ask to close it for this session registry. */
632 (void) close_metadata(registry
, ua_sess
->consumer
);
636 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
638 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
640 delete_ust_app_channel(sock
, ua_chan
, app
);
643 /* In case of per PID, the registry is kept in the session. */
644 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
645 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
647 buffer_reg_pid_remove(reg_pid
);
648 buffer_reg_pid_destroy(reg_pid
);
652 if (ua_sess
->handle
!= -1) {
653 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
654 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
655 ERR("UST app sock %d release session handle failed with ret %d",
659 pthread_mutex_unlock(&ua_sess
->lock
);
661 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
665 * Delete a traceable application structure from the global list. Never call
666 * this function outside of a call_rcu call.
668 * RCU read side lock should _NOT_ be held when calling this function.
671 void delete_ust_app(struct ust_app
*app
)
674 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
676 /* Delete ust app sessions info */
681 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
683 /* Free every object in the session and the session. */
685 delete_ust_app_session(sock
, ua_sess
, app
);
689 ht_cleanup_push(app
->sessions
);
690 ht_cleanup_push(app
->ust_objd
);
693 * Wait until we have deleted the application from the sock hash table
694 * before closing this socket, otherwise an application could re-use the
695 * socket ID and race with the teardown, using the same hash table entry.
697 * It's OK to leave the close in call_rcu. We want it to stay unique for
698 * all RCU readers that could run concurrently with unregister app,
699 * therefore we _need_ to only close that socket after a grace period. So
700 * it should stay in this RCU callback.
702 * This close() is a very important step of the synchronization model so
703 * every modification to this function must be carefully reviewed.
709 lttng_fd_put(LTTNG_FD_APPS
, 1);
711 DBG2("UST app pid %d deleted", app
->pid
);
716 * URCU intermediate call to delete an UST app.
719 void delete_ust_app_rcu(struct rcu_head
*head
)
721 struct lttng_ht_node_ulong
*node
=
722 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
723 struct ust_app
*app
=
724 caa_container_of(node
, struct ust_app
, pid_n
);
726 DBG3("Call RCU deleting app PID %d", app
->pid
);
731 * Delete the session from the application ht and delete the data structure by
732 * freeing every object inside and releasing them.
734 static void destroy_app_session(struct ust_app
*app
,
735 struct ust_app_session
*ua_sess
)
738 struct lttng_ht_iter iter
;
743 iter
.iter
.node
= &ua_sess
->node
.node
;
744 ret
= lttng_ht_del(app
->sessions
, &iter
);
746 /* Already scheduled for teardown. */
750 /* Once deleted, free the data structure. */
751 delete_ust_app_session(app
->sock
, ua_sess
, app
);
758 * Alloc new UST app session.
761 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
763 struct ust_app_session
*ua_sess
;
765 /* Init most of the default value by allocating and zeroing */
766 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
767 if (ua_sess
== NULL
) {
772 ua_sess
->handle
= -1;
773 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
774 pthread_mutex_init(&ua_sess
->lock
, NULL
);
783 * Alloc new UST app channel.
786 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
787 struct ust_app_session
*ua_sess
,
788 struct lttng_ust_channel_attr
*attr
)
790 struct ust_app_channel
*ua_chan
;
792 /* Init most of the default value by allocating and zeroing */
793 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
794 if (ua_chan
== NULL
) {
799 /* Setup channel name */
800 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
801 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
803 ua_chan
->enabled
= 1;
804 ua_chan
->handle
= -1;
805 ua_chan
->session
= ua_sess
;
806 ua_chan
->key
= get_next_channel_key();
807 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
808 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
809 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
811 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
813 /* Copy attributes */
815 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
816 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
817 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
818 ua_chan
->attr
.overwrite
= attr
->overwrite
;
819 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
820 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
821 ua_chan
->attr
.output
= attr
->output
;
823 /* By default, the channel is a per cpu channel. */
824 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
826 DBG3("UST app channel %s allocated", ua_chan
->name
);
835 * Allocate and initialize a UST app stream.
837 * Return newly allocated stream pointer or NULL on error.
839 struct ust_app_stream
*ust_app_alloc_stream(void)
841 struct ust_app_stream
*stream
= NULL
;
843 stream
= zmalloc(sizeof(*stream
));
844 if (stream
== NULL
) {
845 PERROR("zmalloc ust app stream");
849 /* Zero could be a valid value for a handle so flag it to -1. */
857 * Alloc new UST app event.
860 struct ust_app_event
*alloc_ust_app_event(char *name
,
861 struct lttng_ust_event
*attr
)
863 struct ust_app_event
*ua_event
;
865 /* Init most of the default value by allocating and zeroing */
866 ua_event
= zmalloc(sizeof(struct ust_app_event
));
867 if (ua_event
== NULL
) {
872 ua_event
->enabled
= 1;
873 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
874 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
875 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
877 /* Copy attributes */
879 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
882 DBG3("UST app event %s allocated", ua_event
->name
);
891 * Alloc new UST app context.
894 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
896 struct ust_app_ctx
*ua_ctx
;
898 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
899 if (ua_ctx
== NULL
) {
904 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
907 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
914 * Allocate a filter and copy the given original filter.
916 * Return allocated filter or NULL on error.
918 static struct lttng_ust_filter_bytecode
*alloc_copy_ust_app_filter(
919 struct lttng_ust_filter_bytecode
*orig_f
)
921 struct lttng_ust_filter_bytecode
*filter
= NULL
;
923 /* Copy filter bytecode */
924 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
926 PERROR("zmalloc alloc ust app filter");
930 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
937 * Find an ust_app using the sock and return it. RCU read side lock must be
938 * held before calling this helper function.
941 struct ust_app
*find_app_by_sock(int sock
)
943 struct lttng_ht_node_ulong
*node
;
944 struct lttng_ht_iter iter
;
946 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
947 node
= lttng_ht_iter_get_node_ulong(&iter
);
949 DBG2("UST app find by sock %d not found", sock
);
953 return caa_container_of(node
, struct ust_app
, sock_n
);
960 * Find an ust_app using the notify sock and return it. RCU read side lock must
961 * be held before calling this helper function.
963 static struct ust_app
*find_app_by_notify_sock(int sock
)
965 struct lttng_ht_node_ulong
*node
;
966 struct lttng_ht_iter iter
;
968 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
970 node
= lttng_ht_iter_get_node_ulong(&iter
);
972 DBG2("UST app find by notify sock %d not found", sock
);
976 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
983 * Lookup for an ust app event based on event name, filter bytecode and the
986 * Return an ust_app_event object or NULL on error.
988 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
989 char *name
, struct lttng_ust_filter_bytecode
*filter
, int loglevel
)
991 struct lttng_ht_iter iter
;
992 struct lttng_ht_node_str
*node
;
993 struct ust_app_event
*event
= NULL
;
994 struct ust_app_ht_key key
;
999 /* Setup key for event lookup. */
1001 key
.filter
= filter
;
1002 key
.loglevel
= loglevel
;
1004 /* Lookup using the event name as hash and a custom match fct. */
1005 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1006 ht_match_ust_app_event
, &key
, &iter
.iter
);
1007 node
= lttng_ht_iter_get_node_str(&iter
);
1012 event
= caa_container_of(node
, struct ust_app_event
, node
);
1019 * Create the channel context on the tracer.
1021 * Called with UST app session lock held.
1024 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1025 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1029 health_code_update();
1031 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1032 ua_chan
->obj
, &ua_ctx
->obj
);
1034 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1035 ERR("UST app create channel context failed for app (pid: %d) "
1036 "with ret %d", app
->pid
, ret
);
1038 DBG3("UST app disable event failed. Application is dead.");
1043 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1045 DBG2("UST app context handle %d created successfully for channel %s",
1046 ua_ctx
->handle
, ua_chan
->name
);
1049 health_code_update();
1054 * Set the filter on the tracer.
1057 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1058 struct ust_app
*app
)
1062 health_code_update();
1064 if (!ua_event
->filter
) {
1069 ret
= ustctl_set_filter(app
->sock
, ua_event
->filter
,
1072 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1073 ERR("UST app event %s filter failed for app (pid: %d) "
1074 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1076 DBG3("UST app filter event failed. Application is dead.");
1081 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1084 health_code_update();
1089 * Disable the specified event on to UST tracer for the UST session.
1091 static int disable_ust_event(struct ust_app
*app
,
1092 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1096 health_code_update();
1098 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1100 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1101 ERR("UST app event %s disable failed for app (pid: %d) "
1102 "and session handle %d with ret %d",
1103 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1105 DBG3("UST app disable event failed. Application is dead.");
1110 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1111 ua_event
->attr
.name
, app
->pid
);
1114 health_code_update();
1119 * Disable the specified channel on to UST tracer for the UST session.
1121 static int disable_ust_channel(struct ust_app
*app
,
1122 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1126 health_code_update();
1128 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1130 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1131 ERR("UST app channel %s disable failed for app (pid: %d) "
1132 "and session handle %d with ret %d",
1133 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1135 DBG3("UST app disable channel failed. Application is dead.");
1140 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1141 ua_chan
->name
, app
->pid
);
1144 health_code_update();
1149 * Enable the specified channel on to UST tracer for the UST session.
1151 static int enable_ust_channel(struct ust_app
*app
,
1152 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1156 health_code_update();
1158 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1160 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1161 ERR("UST app channel %s enable failed for app (pid: %d) "
1162 "and session handle %d with ret %d",
1163 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1165 DBG3("UST app enable channel failed. Application is dead.");
1170 ua_chan
->enabled
= 1;
1172 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1173 ua_chan
->name
, app
->pid
);
1176 health_code_update();
1181 * Enable the specified event on to UST tracer for the UST session.
1183 static int enable_ust_event(struct ust_app
*app
,
1184 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1188 health_code_update();
1190 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1192 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1193 ERR("UST app event %s enable failed for app (pid: %d) "
1194 "and session handle %d with ret %d",
1195 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1197 DBG3("UST app enable event failed. Application is dead.");
1202 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1203 ua_event
->attr
.name
, app
->pid
);
1206 health_code_update();
1211 * Send channel and stream buffer to application.
1213 * Return 0 on success. On error, a negative value is returned.
1215 static int send_channel_pid_to_ust(struct ust_app
*app
,
1216 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1219 struct ust_app_stream
*stream
, *stmp
;
1225 health_code_update();
1227 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1230 /* Send channel to the application. */
1231 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1236 health_code_update();
1238 /* Send all streams to application. */
1239 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1240 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1244 /* We don't need the stream anymore once sent to the tracer. */
1245 cds_list_del(&stream
->list
);
1246 delete_ust_app_stream(-1, stream
);
1248 /* Flag the channel that it is sent to the application. */
1249 ua_chan
->is_sent
= 1;
1252 health_code_update();
1257 * Create the specified event onto the UST tracer for a UST session.
1259 * Should be called with session mutex held.
1262 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1263 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1267 health_code_update();
1269 /* Create UST event on tracer */
1270 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1273 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1274 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1275 ua_event
->attr
.name
, app
->pid
, ret
);
1277 DBG3("UST app create event failed. Application is dead.");
1282 ua_event
->handle
= ua_event
->obj
->handle
;
1284 DBG2("UST app event %s created successfully for pid:%d",
1285 ua_event
->attr
.name
, app
->pid
);
1287 health_code_update();
1289 /* Set filter if one is present. */
1290 if (ua_event
->filter
) {
1291 ret
= set_ust_event_filter(ua_event
, app
);
1297 /* If event not enabled, disable it on the tracer */
1298 if (ua_event
->enabled
== 0) {
1299 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1302 * If we hit an EPERM, something is wrong with our disable call. If
1303 * we get an EEXIST, there is a problem on the tracer side since we
1307 case -LTTNG_UST_ERR_PERM
:
1308 /* Code flow problem */
1310 case -LTTNG_UST_ERR_EXIST
:
1311 /* It's OK for our use case. */
1322 health_code_update();
1327 * Copy data between an UST app event and a LTT event.
1329 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1330 struct ltt_ust_event
*uevent
)
1332 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1333 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1335 ua_event
->enabled
= uevent
->enabled
;
1337 /* Copy event attributes */
1338 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1340 /* Copy filter bytecode */
1341 if (uevent
->filter
) {
1342 ua_event
->filter
= alloc_copy_ust_app_filter(uevent
->filter
);
1343 /* Filter might be NULL here in case of ENONEM. */
1348 * Copy data between an UST app channel and a LTT channel.
1350 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1351 struct ltt_ust_channel
*uchan
)
1353 struct lttng_ht_iter iter
;
1354 struct ltt_ust_event
*uevent
;
1355 struct ltt_ust_context
*uctx
;
1356 struct ust_app_event
*ua_event
;
1357 struct ust_app_ctx
*ua_ctx
;
1359 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1361 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1362 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1364 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1365 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1367 /* Copy event attributes since the layout is different. */
1368 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1369 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1370 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1371 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1372 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1373 ua_chan
->attr
.output
= uchan
->attr
.output
;
1375 * Note that the attribute channel type is not set since the channel on the
1376 * tracing registry side does not have this information.
1379 ua_chan
->enabled
= uchan
->enabled
;
1380 ua_chan
->tracing_channel_id
= uchan
->id
;
1382 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
1383 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1384 if (ua_ctx
== NULL
) {
1387 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1388 (unsigned long) ua_ctx
->ctx
.ctx
);
1389 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1392 /* Copy all events from ltt ust channel to ust app channel */
1393 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1394 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1395 uevent
->filter
, uevent
->attr
.loglevel
);
1396 if (ua_event
== NULL
) {
1397 DBG2("UST event %s not found on shadow copy channel",
1399 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1400 if (ua_event
== NULL
) {
1403 shadow_copy_event(ua_event
, uevent
);
1404 add_unique_ust_app_event(ua_chan
, ua_event
);
1408 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1412 * Copy data between a UST app session and a regular LTT session.
1414 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1415 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1417 struct lttng_ht_node_str
*ua_chan_node
;
1418 struct lttng_ht_iter iter
;
1419 struct ltt_ust_channel
*uchan
;
1420 struct ust_app_channel
*ua_chan
;
1422 struct tm
*timeinfo
;
1426 /* Get date and time for unique app path */
1428 timeinfo
= localtime(&rawtime
);
1429 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1431 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1433 ua_sess
->tracing_id
= usess
->id
;
1434 ua_sess
->id
= get_next_session_id();
1435 ua_sess
->uid
= app
->uid
;
1436 ua_sess
->gid
= app
->gid
;
1437 ua_sess
->euid
= usess
->uid
;
1438 ua_sess
->egid
= usess
->gid
;
1439 ua_sess
->buffer_type
= usess
->buffer_type
;
1440 ua_sess
->bits_per_long
= app
->bits_per_long
;
1441 /* There is only one consumer object per session possible. */
1442 ua_sess
->consumer
= usess
->consumer
;
1444 switch (ua_sess
->buffer_type
) {
1445 case LTTNG_BUFFER_PER_PID
:
1446 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1447 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1450 case LTTNG_BUFFER_PER_UID
:
1451 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1452 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1459 PERROR("asprintf UST shadow copy session");
1464 /* Iterate over all channels in global domain. */
1465 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1467 struct lttng_ht_iter uiter
;
1469 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1470 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1471 if (ua_chan_node
!= NULL
) {
1472 /* Session exist. Contiuing. */
1476 DBG2("Channel %s not found on shadow session copy, creating it",
1478 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1479 if (ua_chan
== NULL
) {
1480 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1483 shadow_copy_channel(ua_chan
, uchan
);
1485 * The concept of metadata channel does not exist on the tracing
1486 * registry side of the session daemon so this can only be a per CPU
1487 * channel and not metadata.
1489 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1491 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1499 * Lookup sesison wrapper.
1502 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1503 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1505 /* Get right UST app session from app */
1506 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
1510 * Return ust app session from the app session hashtable using the UST session
1513 static struct ust_app_session
*lookup_session_by_app(
1514 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1516 struct lttng_ht_iter iter
;
1517 struct lttng_ht_node_ulong
*node
;
1519 __lookup_session_by_app(usess
, app
, &iter
);
1520 node
= lttng_ht_iter_get_node_ulong(&iter
);
1525 return caa_container_of(node
, struct ust_app_session
, node
);
1532 * Setup buffer registry per PID for the given session and application. If none
1533 * is found, a new one is created, added to the global registry and
1534 * initialized. If regp is valid, it's set with the newly created object.
1536 * Return 0 on success or else a negative value.
1538 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1539 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1542 struct buffer_reg_pid
*reg_pid
;
1549 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1552 * This is the create channel path meaning that if there is NO
1553 * registry available, we have to create one for this session.
1555 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
);
1559 buffer_reg_pid_add(reg_pid
);
1564 /* Initialize registry. */
1565 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1566 app
->bits_per_long
, app
->uint8_t_alignment
,
1567 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1568 app
->uint64_t_alignment
, app
->long_alignment
,
1569 app
->byte_order
, app
->version
.major
,
1570 app
->version
.minor
);
1575 DBG3("UST app buffer registry per PID created successfully");
1587 * Setup buffer registry per UID for the given session and application. If none
1588 * is found, a new one is created, added to the global registry and
1589 * initialized. If regp is valid, it's set with the newly created object.
1591 * Return 0 on success or else a negative value.
1593 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
1594 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
1597 struct buffer_reg_uid
*reg_uid
;
1604 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
1607 * This is the create channel path meaning that if there is NO
1608 * registry available, we have to create one for this session.
1610 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
1611 LTTNG_DOMAIN_UST
, ®_uid
);
1615 buffer_reg_uid_add(reg_uid
);
1620 /* Initialize registry. */
1621 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
1622 app
->bits_per_long
, app
->uint8_t_alignment
,
1623 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1624 app
->uint64_t_alignment
, app
->long_alignment
,
1625 app
->byte_order
, app
->version
.major
,
1626 app
->version
.minor
);
1630 /* Add node to teardown list of the session. */
1631 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
1633 DBG3("UST app buffer registry per UID created successfully");
1645 * Create a session on the tracer side for the given app.
1647 * On success, ua_sess_ptr is populated with the session pointer or else left
1648 * untouched. If the session was created, is_created is set to 1. On error,
1649 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1652 * Returns 0 on success or else a negative code which is either -ENOMEM or
1653 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1655 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1656 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1659 int ret
, created
= 0;
1660 struct ust_app_session
*ua_sess
;
1664 assert(ua_sess_ptr
);
1666 health_code_update();
1668 ua_sess
= lookup_session_by_app(usess
, app
);
1669 if (ua_sess
== NULL
) {
1670 DBG2("UST app pid: %d session id %d not found, creating it",
1671 app
->pid
, usess
->id
);
1672 ua_sess
= alloc_ust_app_session(app
);
1673 if (ua_sess
== NULL
) {
1674 /* Only malloc can failed so something is really wrong */
1678 shadow_copy_session(ua_sess
, usess
, app
);
1682 switch (usess
->buffer_type
) {
1683 case LTTNG_BUFFER_PER_PID
:
1684 /* Init local registry. */
1685 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
1690 case LTTNG_BUFFER_PER_UID
:
1691 /* Look for a global registry. If none exists, create one. */
1692 ret
= setup_buffer_reg_uid(usess
, app
, NULL
);
1703 health_code_update();
1705 if (ua_sess
->handle
== -1) {
1706 ret
= ustctl_create_session(app
->sock
);
1708 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1709 ERR("Creating session for app pid %d with ret %d",
1712 DBG("UST app creating session failed. Application is dead");
1714 delete_ust_app_session(-1, ua_sess
, app
);
1715 if (ret
!= -ENOMEM
) {
1717 * Tracer is probably gone or got an internal error so let's
1718 * behave like it will soon unregister or not usable.
1725 ua_sess
->handle
= ret
;
1727 /* Add ust app session to app's HT */
1728 lttng_ht_node_init_ulong(&ua_sess
->node
,
1729 (unsigned long) ua_sess
->tracing_id
);
1730 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
1732 DBG2("UST app session created successfully with handle %d", ret
);
1735 *ua_sess_ptr
= ua_sess
;
1737 *is_created
= created
;
1740 /* Everything went well. */
1744 health_code_update();
1749 * Create a context for the channel on the tracer.
1751 * Called with UST app session lock held and a RCU read side lock.
1754 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
1755 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
1756 struct ust_app
*app
)
1759 struct lttng_ht_iter iter
;
1760 struct lttng_ht_node_ulong
*node
;
1761 struct ust_app_ctx
*ua_ctx
;
1763 DBG2("UST app adding context to channel %s", ua_chan
->name
);
1765 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
1766 node
= lttng_ht_iter_get_node_ulong(&iter
);
1772 ua_ctx
= alloc_ust_app_ctx(uctx
);
1773 if (ua_ctx
== NULL
) {
1779 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
1780 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1782 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
1792 * Enable on the tracer side a ust app event for the session and channel.
1794 * Called with UST app session lock held.
1797 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
1798 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1802 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1807 ua_event
->enabled
= 1;
1814 * Disable on the tracer side a ust app event for the session and channel.
1816 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1817 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1821 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1826 ua_event
->enabled
= 0;
1833 * Lookup ust app channel for session and disable it on the tracer side.
1836 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1837 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1841 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1846 ua_chan
->enabled
= 0;
1853 * Lookup ust app channel for session and enable it on the tracer side. This
1854 * MUST be called with a RCU read side lock acquired.
1856 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1857 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1860 struct lttng_ht_iter iter
;
1861 struct lttng_ht_node_str
*ua_chan_node
;
1862 struct ust_app_channel
*ua_chan
;
1864 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1865 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1866 if (ua_chan_node
== NULL
) {
1867 DBG2("Unable to find channel %s in ust session id %u",
1868 uchan
->name
, ua_sess
->tracing_id
);
1872 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1874 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1884 * Ask the consumer to create a channel and get it if successful.
1886 * Return 0 on success or else a negative value.
1888 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
1889 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
1890 int bitness
, struct ust_registry_session
*registry
)
1893 unsigned int nb_fd
= 0;
1894 struct consumer_socket
*socket
;
1902 health_code_update();
1904 /* Get the right consumer socket for the application. */
1905 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
1911 health_code_update();
1913 /* Need one fd for the channel. */
1914 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1916 ERR("Exhausted number of available FD upon create channel");
1921 * Ask consumer to create channel. The consumer will return the number of
1922 * stream we have to expect.
1924 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
1931 * Compute the number of fd needed before receiving them. It must be 2 per
1932 * stream (2 being the default value here).
1934 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
1936 /* Reserve the amount of file descriptor we need. */
1937 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
1939 ERR("Exhausted number of available FD upon create channel");
1940 goto error_fd_get_stream
;
1943 health_code_update();
1946 * Now get the channel from the consumer. This call wil populate the stream
1947 * list of that channel and set the ust objects.
1949 ret
= ust_consumer_get_channel(socket
, ua_chan
);
1958 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
1959 error_fd_get_stream
:
1961 * Initiate a destroy channel on the consumer since we had an error
1962 * handling it on our side. The return value is of no importance since we
1963 * already have a ret value set by the previous error that we need to
1966 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
1968 lttng_fd_put(LTTNG_FD_APPS
, 1);
1970 health_code_update();
1976 * Duplicate the ust data object of the ust app stream and save it in the
1977 * buffer registry stream.
1979 * Return 0 on success or else a negative value.
1981 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
1982 struct ust_app_stream
*stream
)
1989 /* Reserve the amount of file descriptor we need. */
1990 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
1992 ERR("Exhausted number of available FD upon duplicate stream");
1996 /* Duplicate object for stream once the original is in the registry. */
1997 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
1998 reg_stream
->obj
.ust
);
2000 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2001 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2002 lttng_fd_put(LTTNG_FD_APPS
, 2);
2005 stream
->handle
= stream
->obj
->handle
;
2012 * Duplicate the ust data object of the ust app. channel and save it in the
2013 * buffer registry channel.
2015 * Return 0 on success or else a negative value.
2017 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2018 struct ust_app_channel
*ua_chan
)
2025 /* Need two fds for the channel. */
2026 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2028 ERR("Exhausted number of available FD upon duplicate channel");
2032 /* Duplicate object for stream once the original is in the registry. */
2033 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2035 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2036 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2039 ua_chan
->handle
= ua_chan
->obj
->handle
;
2044 lttng_fd_put(LTTNG_FD_APPS
, 1);
2050 * For a given channel buffer registry, setup all streams of the given ust
2051 * application channel.
2053 * Return 0 on success or else a negative value.
2055 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2056 struct ust_app_channel
*ua_chan
)
2059 struct ust_app_stream
*stream
, *stmp
;
2064 DBG2("UST app setup buffer registry stream");
2066 /* Send all streams to application. */
2067 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2068 struct buffer_reg_stream
*reg_stream
;
2070 ret
= buffer_reg_stream_create(®_stream
);
2076 * Keep original pointer and nullify it in the stream so the delete
2077 * stream call does not release the object.
2079 reg_stream
->obj
.ust
= stream
->obj
;
2081 buffer_reg_stream_add(reg_stream
, reg_chan
);
2083 /* We don't need the streams anymore. */
2084 cds_list_del(&stream
->list
);
2085 delete_ust_app_stream(-1, stream
);
2093 * Create a buffer registry channel for the given session registry and
2094 * application channel object. If regp pointer is valid, it's set with the
2095 * created object. Important, the created object is NOT added to the session
2096 * registry hash table.
2098 * Return 0 on success else a negative value.
2100 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2101 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2104 struct buffer_reg_channel
*reg_chan
= NULL
;
2109 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2111 /* Create buffer registry channel. */
2112 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2117 reg_chan
->consumer_key
= ua_chan
->key
;
2119 /* Create and add a channel registry to session. */
2120 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2121 ua_chan
->tracing_channel_id
);
2125 buffer_reg_channel_add(reg_sess
, reg_chan
);
2134 /* Safe because the registry channel object was not added to any HT. */
2135 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2141 * Setup buffer registry channel for the given session registry and application
2142 * channel object. If regp pointer is valid, it's set with the created object.
2144 * Return 0 on success else a negative value.
2146 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2147 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
)
2154 assert(ua_chan
->obj
);
2156 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2158 /* Setup all streams for the registry. */
2159 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
);
2164 reg_chan
->obj
.ust
= ua_chan
->obj
;
2165 ua_chan
->obj
= NULL
;
2170 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2171 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2176 * Send buffer registry channel to the application.
2178 * Return 0 on success else a negative value.
2180 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2181 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2182 struct ust_app_channel
*ua_chan
)
2185 struct buffer_reg_stream
*reg_stream
;
2192 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2194 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2199 /* Send channel to the application. */
2200 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2205 health_code_update();
2207 /* Send all streams to application. */
2208 pthread_mutex_lock(®_chan
->stream_list_lock
);
2209 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2210 struct ust_app_stream stream
;
2212 ret
= duplicate_stream_object(reg_stream
, &stream
);
2214 goto error_stream_unlock
;
2217 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2219 (void) release_ust_app_stream(-1, &stream
);
2220 goto error_stream_unlock
;
2224 * The return value is not important here. This function will output an
2227 (void) release_ust_app_stream(-1, &stream
);
2229 ua_chan
->is_sent
= 1;
2231 error_stream_unlock
:
2232 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2238 * Create and send to the application the created buffers with per UID buffers.
2240 * Return 0 on success else a negative value.
2242 static int create_channel_per_uid(struct ust_app
*app
,
2243 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2244 struct ust_app_channel
*ua_chan
)
2247 struct buffer_reg_uid
*reg_uid
;
2248 struct buffer_reg_channel
*reg_chan
;
2255 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2257 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2259 * The session creation handles the creation of this global registry
2260 * object. If none can be find, there is a code flow problem or a
2265 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2268 /* Create the buffer registry channel object. */
2269 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2276 * Create the buffers on the consumer side. This call populates the
2277 * ust app channel object with all streams and data object.
2279 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2280 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2283 * Let's remove the previously created buffer registry channel so
2284 * it's not visible anymore in the session registry.
2286 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2287 ua_chan
->tracing_channel_id
);
2288 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2289 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2294 * Setup the streams and add it to the session registry.
2296 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, reg_chan
);
2303 /* Send buffers to the application. */
2304 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2314 * Create and send to the application the created buffers with per PID buffers.
2316 * Return 0 on success else a negative value.
2318 static int create_channel_per_pid(struct ust_app
*app
,
2319 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2320 struct ust_app_channel
*ua_chan
)
2323 struct ust_registry_session
*registry
;
2330 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2334 registry
= get_session_registry(ua_sess
);
2337 /* Create and add a new channel registry to session. */
2338 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2343 /* Create and get channel on the consumer side. */
2344 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2345 app
->bits_per_long
, registry
);
2350 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2361 * From an already allocated ust app channel, create the channel buffers if
2362 * need and send it to the application. This MUST be called with a RCU read
2363 * side lock acquired.
2365 * Return 0 on success or else a negative value.
2367 static int do_create_channel(struct ust_app
*app
,
2368 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2369 struct ust_app_channel
*ua_chan
)
2378 /* Handle buffer type before sending the channel to the application. */
2379 switch (usess
->buffer_type
) {
2380 case LTTNG_BUFFER_PER_UID
:
2382 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
2388 case LTTNG_BUFFER_PER_PID
:
2390 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
2402 /* Initialize ust objd object using the received handle and add it. */
2403 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
2404 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
2406 /* If channel is not enabled, disable it on the tracer */
2407 if (!ua_chan
->enabled
) {
2408 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2419 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2420 * newly created channel if not NULL.
2422 * Called with UST app session lock and RCU read-side lock held.
2424 * Return 0 on success or else a negative value.
2426 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
2427 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
2428 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
2429 struct ust_app_channel
**ua_chanp
)
2432 struct lttng_ht_iter iter
;
2433 struct lttng_ht_node_str
*ua_chan_node
;
2434 struct ust_app_channel
*ua_chan
;
2436 /* Lookup channel in the ust app session */
2437 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2438 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2439 if (ua_chan_node
!= NULL
) {
2440 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2444 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
2445 if (ua_chan
== NULL
) {
2446 /* Only malloc can fail here */
2450 shadow_copy_channel(ua_chan
, uchan
);
2452 /* Set channel type. */
2453 ua_chan
->attr
.type
= type
;
2455 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
2460 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
2463 /* Only add the channel if successful on the tracer side. */
2464 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
2468 *ua_chanp
= ua_chan
;
2471 /* Everything went well. */
2475 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
2481 * Create UST app event and create it on the tracer side.
2483 * Called with ust app session mutex held.
2486 int create_ust_app_event(struct ust_app_session
*ua_sess
,
2487 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
2488 struct ust_app
*app
)
2491 struct ust_app_event
*ua_event
;
2493 /* Get event node */
2494 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2495 uevent
->filter
, uevent
->attr
.loglevel
);
2496 if (ua_event
!= NULL
) {
2501 /* Does not exist so create one */
2502 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
2503 if (ua_event
== NULL
) {
2504 /* Only malloc can failed so something is really wrong */
2508 shadow_copy_event(ua_event
, uevent
);
2510 /* Create it on the tracer side */
2511 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2513 /* Not found previously means that it does not exist on the tracer */
2514 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
2518 add_unique_ust_app_event(ua_chan
, ua_event
);
2520 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
2527 /* Valid. Calling here is already in a read side lock */
2528 delete_ust_app_event(-1, ua_event
);
2533 * Create UST metadata and open it on the tracer side.
2535 * Called with UST app session lock held and RCU read side lock.
2537 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
2538 struct ust_app
*app
, struct consumer_output
*consumer
,
2539 struct ustctl_consumer_channel_attr
*attr
)
2542 struct ust_app_channel
*metadata
;
2543 struct consumer_socket
*socket
;
2544 struct ust_registry_session
*registry
;
2550 registry
= get_session_registry(ua_sess
);
2553 /* Metadata already exists for this registry or it was closed previously */
2554 if (registry
->metadata_key
|| registry
->metadata_closed
) {
2559 /* Allocate UST metadata */
2560 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
2562 /* malloc() failed */
2568 /* Set default attributes for metadata. */
2569 metadata
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2570 metadata
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
2571 metadata
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
2572 metadata
->attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
2573 metadata
->attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;
2574 metadata
->attr
.output
= LTTNG_UST_MMAP
;
2575 metadata
->attr
.type
= LTTNG_UST_CHAN_METADATA
;
2577 memcpy(&metadata
->attr
, attr
, sizeof(metadata
->attr
));
2578 metadata
->attr
.output
= LTTNG_UST_MMAP
;
2579 metadata
->attr
.type
= LTTNG_UST_CHAN_METADATA
;
2582 /* Need one fd for the channel. */
2583 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2585 ERR("Exhausted number of available FD upon create metadata");
2589 /* Get the right consumer socket for the application. */
2590 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
2593 goto error_consumer
;
2597 * Keep metadata key so we can identify it on the consumer side. Assign it
2598 * to the registry *before* we ask the consumer so we avoid the race of the
2599 * consumer requesting the metadata and the ask_channel call on our side
2600 * did not returned yet.
2602 registry
->metadata_key
= metadata
->key
;
2605 * Ask the metadata channel creation to the consumer. The metadata object
2606 * will be created by the consumer and kept their. However, the stream is
2607 * never added or monitored until we do a first push metadata to the
2610 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
2614 * Safe because the metadata obj pointer is not set so the delete below
2615 * will not put a FD back again.
2617 goto error_consumer
;
2621 * The setup command will make the metadata stream be sent to the relayd,
2622 * if applicable, and the thread managing the metadatas. This is important
2623 * because after this point, if an error occurs, the only way the stream
2624 * can be deleted is to be monitored in the consumer.
2626 ret
= consumer_setup_metadata(socket
, metadata
->key
);
2629 * Safe because the metadata obj pointer is not set so the delete below
2630 * will not put a FD back again.
2632 goto error_consumer
;
2635 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
2636 metadata
->key
, app
->pid
);
2639 lttng_fd_put(LTTNG_FD_APPS
, 1);
2640 delete_ust_app_channel(-1, metadata
, app
);
2646 * Return pointer to traceable apps list.
2648 struct lttng_ht
*ust_app_get_ht(void)
2654 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
2655 * acquired before calling this function.
2657 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
2659 struct ust_app
*app
= NULL
;
2660 struct lttng_ht_node_ulong
*node
;
2661 struct lttng_ht_iter iter
;
2663 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
2664 node
= lttng_ht_iter_get_node_ulong(&iter
);
2666 DBG2("UST app no found with pid %d", pid
);
2670 DBG2("Found UST app by pid %d", pid
);
2672 app
= caa_container_of(node
, struct ust_app
, pid_n
);
2679 * Allocate and init an UST app object using the registration information and
2680 * the command socket. This is called when the command socket connects to the
2683 * The object is returned on success or else NULL.
2685 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
2687 struct ust_app
*lta
= NULL
;
2692 DBG3("UST app creating application for socket %d", sock
);
2694 if ((msg
->bits_per_long
== 64 &&
2695 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
2696 || (msg
->bits_per_long
== 32 &&
2697 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
2698 ERR("Registration failed: application \"%s\" (pid: %d) has "
2699 "%d-bit long, but no consumerd for this size is available.\n",
2700 msg
->name
, msg
->pid
, msg
->bits_per_long
);
2704 lta
= zmalloc(sizeof(struct ust_app
));
2710 lta
->ppid
= msg
->ppid
;
2711 lta
->uid
= msg
->uid
;
2712 lta
->gid
= msg
->gid
;
2714 lta
->bits_per_long
= msg
->bits_per_long
;
2715 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
2716 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
2717 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
2718 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
2719 lta
->long_alignment
= msg
->long_alignment
;
2720 lta
->byte_order
= msg
->byte_order
;
2722 lta
->v_major
= msg
->major
;
2723 lta
->v_minor
= msg
->minor
;
2724 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2725 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2726 lta
->notify_sock
= -1;
2728 /* Copy name and make sure it's NULL terminated. */
2729 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
2730 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
2733 * Before this can be called, when receiving the registration information,
2734 * the application compatibility is checked. So, at this point, the
2735 * application can work with this session daemon.
2737 lta
->compatible
= 1;
2739 lta
->pid
= msg
->pid
;
2740 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
2742 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
2744 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
2751 * For a given application object, add it to every hash table.
2753 void ust_app_add(struct ust_app
*app
)
2756 assert(app
->notify_sock
>= 0);
2761 * On a re-registration, we want to kick out the previous registration of
2764 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
2767 * The socket _should_ be unique until _we_ call close. So, a add_unique
2768 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
2769 * already in the table.
2771 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
2773 /* Add application to the notify socket hash table. */
2774 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
2775 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
2777 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
2778 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
2779 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
2786 * Set the application version into the object.
2788 * Return 0 on success else a negative value either an errno code or a
2789 * LTTng-UST error code.
2791 int ust_app_version(struct ust_app
*app
)
2797 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
2799 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
2800 ERR("UST app %d verson failed with ret %d", app
->sock
, ret
);
2802 DBG3("UST app %d verion failed. Application is dead", app
->sock
);
2810 * Unregister app by removing it from the global traceable app list and freeing
2813 * The socket is already closed at this point so no close to sock.
2815 void ust_app_unregister(int sock
)
2817 struct ust_app
*lta
;
2818 struct lttng_ht_node_ulong
*node
;
2819 struct lttng_ht_iter iter
;
2820 struct ust_app_session
*ua_sess
;
2825 /* Get the node reference for a call_rcu */
2826 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
2827 node
= lttng_ht_iter_get_node_ulong(&iter
);
2830 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
2831 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
2833 /* Remove application from PID hash table */
2834 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
2838 * Remove application from notify hash table. The thread handling the
2839 * notify socket could have deleted the node so ignore on error because
2840 * either way it's valid. The close of that socket is handled by the other
2843 iter
.iter
.node
= <a
->notify_sock_n
.node
;
2844 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
2847 * Ignore return value since the node might have been removed before by an
2848 * add replace during app registration because the PID can be reassigned by
2851 iter
.iter
.node
= <a
->pid_n
.node
;
2852 ret
= lttng_ht_del(ust_app_ht
, &iter
);
2854 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
2858 /* Remove sessions so they are not visible during deletion.*/
2859 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
2861 struct ust_registry_session
*registry
;
2863 ret
= lttng_ht_del(lta
->sessions
, &iter
);
2865 /* The session was already removed so scheduled for teardown. */
2870 * Add session to list for teardown. This is safe since at this point we
2871 * are the only one using this list.
2873 pthread_mutex_lock(&ua_sess
->lock
);
2876 * Normally, this is done in the delete session process which is
2877 * executed in the call rcu below. However, upon registration we can't
2878 * afford to wait for the grace period before pushing data or else the
2879 * data pending feature can race between the unregistration and stop
2880 * command where the data pending command is sent *before* the grace
2883 * The close metadata below nullifies the metadata pointer in the
2884 * session so the delete session will NOT push/close a second time.
2886 registry
= get_session_registry(ua_sess
);
2887 if (registry
&& !registry
->metadata_closed
) {
2888 /* Push metadata for application before freeing the application. */
2889 (void) push_metadata(registry
, ua_sess
->consumer
);
2892 * Don't ask to close metadata for global per UID buffers. Close
2893 * metadata only on destroy trace session in this case. Also, the
2894 * previous push metadata could have flag the metadata registry to
2895 * close so don't send a close command if closed.
2897 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
&&
2898 !registry
->metadata_closed
) {
2899 /* And ask to close it for this session registry. */
2900 (void) close_metadata(registry
, ua_sess
->consumer
);
2904 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
2905 pthread_mutex_unlock(&ua_sess
->lock
);
2909 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
2916 * Return traceable_app_count
2918 unsigned long ust_app_list_count(void)
2920 unsigned long count
;
2923 count
= lttng_ht_get_count(ust_app_ht
);
2930 * Fill events array with all events name of all registered apps.
2932 int ust_app_list_events(struct lttng_event
**events
)
2935 size_t nbmem
, count
= 0;
2936 struct lttng_ht_iter iter
;
2937 struct ust_app
*app
;
2938 struct lttng_event
*tmp_event
;
2940 nbmem
= UST_APP_EVENT_LIST_SIZE
;
2941 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
2942 if (tmp_event
== NULL
) {
2943 PERROR("zmalloc ust app events");
2950 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2951 struct lttng_ust_tracepoint_iter uiter
;
2953 health_code_update();
2955 if (!app
->compatible
) {
2957 * TODO: In time, we should notice the caller of this error by
2958 * telling him that this is a version error.
2962 handle
= ustctl_tracepoint_list(app
->sock
);
2964 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
2965 ERR("UST app list events getting handle failed for app pid %d",
2971 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
2972 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
2973 /* Handle ustctl error. */
2976 if (ret
!= -LTTNG_UST_ERR_EXITING
|| ret
!= -EPIPE
) {
2977 ERR("UST app tp list get failed for app %d with ret %d",
2980 DBG3("UST app tp list get failed. Application is dead");
2985 health_code_update();
2986 if (count
>= nbmem
) {
2987 /* In case the realloc fails, we free the memory */
2990 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
2993 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event
));
2995 PERROR("realloc ust app events");
3002 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3003 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3004 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3005 tmp_event
[count
].pid
= app
->pid
;
3006 tmp_event
[count
].enabled
= -1;
3012 *events
= tmp_event
;
3014 DBG2("UST app list events done (%zu events)", count
);
3019 health_code_update();
3024 * Fill events array with all events name of all registered apps.
3026 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3029 size_t nbmem
, count
= 0;
3030 struct lttng_ht_iter iter
;
3031 struct ust_app
*app
;
3032 struct lttng_event_field
*tmp_event
;
3034 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3035 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3036 if (tmp_event
== NULL
) {
3037 PERROR("zmalloc ust app event fields");
3044 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3045 struct lttng_ust_field_iter uiter
;
3047 health_code_update();
3049 if (!app
->compatible
) {
3051 * TODO: In time, we should notice the caller of this error by
3052 * telling him that this is a version error.
3056 handle
= ustctl_tracepoint_field_list(app
->sock
);
3058 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3059 ERR("UST app list field getting handle failed for app pid %d",
3065 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3066 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3067 /* Handle ustctl error. */
3070 if (ret
!= -LTTNG_UST_ERR_EXITING
|| ret
!= -EPIPE
) {
3071 ERR("UST app tp list field failed for app %d with ret %d",
3074 DBG3("UST app tp list field failed. Application is dead");
3079 health_code_update();
3080 if (count
>= nbmem
) {
3081 /* In case the realloc fails, we free the memory */
3084 DBG2("Reallocating event field list from %zu to %zu entries", nbmem
,
3087 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event_field
));
3089 PERROR("realloc ust app event fields");
3097 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3098 tmp_event
[count
].type
= uiter
.type
;
3099 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3101 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3102 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3103 tmp_event
[count
].event
.type
= LTTNG_UST_TRACEPOINT
;
3104 tmp_event
[count
].event
.pid
= app
->pid
;
3105 tmp_event
[count
].event
.enabled
= -1;
3111 *fields
= tmp_event
;
3113 DBG2("UST app list event fields done (%zu events)", count
);
3118 health_code_update();
3123 * Free and clean all traceable apps of the global list.
3125 * Should _NOT_ be called with RCU read-side lock held.
3127 void ust_app_clean_list(void)
3130 struct ust_app
*app
;
3131 struct lttng_ht_iter iter
;
3133 DBG2("UST app cleaning registered apps hash table");
3137 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3138 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3140 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3143 /* Cleanup socket hash table */
3144 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3146 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3150 /* Cleanup notify socket hash table */
3151 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3152 notify_sock_n
.node
) {
3153 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3158 /* Destroy is done only when the ht is empty */
3159 ht_cleanup_push(ust_app_ht
);
3160 ht_cleanup_push(ust_app_ht_by_sock
);
3161 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3165 * Init UST app hash table.
3167 void ust_app_ht_alloc(void)
3169 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3170 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3171 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3175 * For a specific UST session, disable the channel for all registered apps.
3177 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3178 struct ltt_ust_channel
*uchan
)
3181 struct lttng_ht_iter iter
;
3182 struct lttng_ht_node_str
*ua_chan_node
;
3183 struct ust_app
*app
;
3184 struct ust_app_session
*ua_sess
;
3185 struct ust_app_channel
*ua_chan
;
3187 if (usess
== NULL
|| uchan
== NULL
) {
3188 ERR("Disabling UST global channel with NULL values");
3193 DBG2("UST app disabling channel %s from global domain for session id %d",
3194 uchan
->name
, usess
->id
);
3198 /* For every registered applications */
3199 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3200 struct lttng_ht_iter uiter
;
3201 if (!app
->compatible
) {
3203 * TODO: In time, we should notice the caller of this error by
3204 * telling him that this is a version error.
3208 ua_sess
= lookup_session_by_app(usess
, app
);
3209 if (ua_sess
== NULL
) {
3214 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3215 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3216 /* If the session if found for the app, the channel must be there */
3217 assert(ua_chan_node
);
3219 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3220 /* The channel must not be already disabled */
3221 assert(ua_chan
->enabled
== 1);
3223 /* Disable channel onto application */
3224 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3226 /* XXX: We might want to report this error at some point... */
3238 * For a specific UST session, enable the channel for all registered apps.
3240 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3241 struct ltt_ust_channel
*uchan
)
3244 struct lttng_ht_iter iter
;
3245 struct ust_app
*app
;
3246 struct ust_app_session
*ua_sess
;
3248 if (usess
== NULL
|| uchan
== NULL
) {
3249 ERR("Adding UST global channel to NULL values");
3254 DBG2("UST app enabling channel %s to global domain for session id %d",
3255 uchan
->name
, usess
->id
);
3259 /* For every registered applications */
3260 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3261 if (!app
->compatible
) {
3263 * TODO: In time, we should notice the caller of this error by
3264 * telling him that this is a version error.
3268 ua_sess
= lookup_session_by_app(usess
, app
);
3269 if (ua_sess
== NULL
) {
3273 /* Enable channel onto application */
3274 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3276 /* XXX: We might want to report this error at some point... */
3288 * Disable an event in a channel and for a specific session.
3290 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3291 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3294 struct lttng_ht_iter iter
, uiter
;
3295 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3296 struct ust_app
*app
;
3297 struct ust_app_session
*ua_sess
;
3298 struct ust_app_channel
*ua_chan
;
3299 struct ust_app_event
*ua_event
;
3301 DBG("UST app disabling event %s for all apps in channel "
3302 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
3306 /* For all registered applications */
3307 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3308 if (!app
->compatible
) {
3310 * TODO: In time, we should notice the caller of this error by
3311 * telling him that this is a version error.
3315 ua_sess
= lookup_session_by_app(usess
, app
);
3316 if (ua_sess
== NULL
) {
3321 /* Lookup channel in the ust app session */
3322 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3323 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3324 if (ua_chan_node
== NULL
) {
3325 DBG2("Channel %s not found in session id %d for app pid %d."
3326 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
3329 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3331 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
3332 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
3333 if (ua_event_node
== NULL
) {
3334 DBG2("Event %s not found in channel %s for app pid %d."
3335 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
3338 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3340 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3342 /* XXX: Report error someday... */
3353 * For a specific UST session and UST channel, the event for all
3356 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
3357 struct ltt_ust_channel
*uchan
)
3360 struct lttng_ht_iter iter
, uiter
;
3361 struct lttng_ht_node_str
*ua_chan_node
;
3362 struct ust_app
*app
;
3363 struct ust_app_session
*ua_sess
;
3364 struct ust_app_channel
*ua_chan
;
3365 struct ust_app_event
*ua_event
;
3367 DBG("UST app disabling all event for all apps in channel "
3368 "%s for session id %d", uchan
->name
, usess
->id
);
3372 /* For all registered applications */
3373 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3374 if (!app
->compatible
) {
3376 * TODO: In time, we should notice the caller of this error by
3377 * telling him that this is a version error.
3381 ua_sess
= lookup_session_by_app(usess
, app
);
3383 /* The application has problem or is probably dead. */
3387 /* Lookup channel in the ust app session */
3388 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3389 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3390 /* If the channel is not found, there is a code flow error */
3391 assert(ua_chan_node
);
3393 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3395 /* Disable each events of channel */
3396 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
3398 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3400 /* XXX: Report error someday... */
3412 * For a specific UST session, create the channel for all registered apps.
3414 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
3415 struct ltt_ust_channel
*uchan
)
3417 int ret
= 0, created
;
3418 struct lttng_ht_iter iter
;
3419 struct ust_app
*app
;
3420 struct ust_app_session
*ua_sess
= NULL
;
3422 /* Very wrong code flow */
3426 DBG2("UST app adding channel %s to UST domain for session id %d",
3427 uchan
->name
, usess
->id
);
3431 /* For every registered applications */
3432 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3433 if (!app
->compatible
) {
3435 * TODO: In time, we should notice the caller of this error by
3436 * telling him that this is a version error.
3441 * Create session on the tracer side and add it to app session HT. Note
3442 * that if session exist, it will simply return a pointer to the ust
3445 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
3450 * The application's socket is not valid. Either a bad socket
3451 * or a timeout on it. We can't inform the caller that for a
3452 * specific app, the session failed so lets continue here.
3457 goto error_rcu_unlock
;
3462 pthread_mutex_lock(&ua_sess
->lock
);
3463 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
3464 sizeof(uchan
->name
))) {
3465 struct ustctl_consumer_channel_attr attr
;
3466 copy_channel_attr_to_ustctl(&attr
, &uchan
->attr
);
3467 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
,
3470 /* Create channel onto application. We don't need the chan ref. */
3471 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
3472 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
3474 pthread_mutex_unlock(&ua_sess
->lock
);
3476 if (ret
== -ENOMEM
) {
3477 /* No more memory is a fatal error. Stop right now. */
3478 goto error_rcu_unlock
;
3480 /* Cleanup the created session if it's the case. */
3482 destroy_app_session(app
, ua_sess
);
3493 * Enable event for a specific session and channel on the tracer.
3495 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
3496 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3499 struct lttng_ht_iter iter
, uiter
;
3500 struct lttng_ht_node_str
*ua_chan_node
;
3501 struct ust_app
*app
;
3502 struct ust_app_session
*ua_sess
;
3503 struct ust_app_channel
*ua_chan
;
3504 struct ust_app_event
*ua_event
;
3506 DBG("UST app enabling event %s for all apps for session id %d",
3507 uevent
->attr
.name
, usess
->id
);
3510 * NOTE: At this point, this function is called only if the session and
3511 * channel passed are already created for all apps. and enabled on the
3517 /* For all registered applications */
3518 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3519 if (!app
->compatible
) {
3521 * TODO: In time, we should notice the caller of this error by
3522 * telling him that this is a version error.
3526 ua_sess
= lookup_session_by_app(usess
, app
);
3528 /* The application has problem or is probably dead. */
3532 pthread_mutex_lock(&ua_sess
->lock
);
3534 /* Lookup channel in the ust app session */
3535 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3536 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3537 /* If the channel is not found, there is a code flow error */
3538 assert(ua_chan_node
);
3540 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3542 /* Get event node */
3543 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3544 uevent
->filter
, uevent
->attr
.loglevel
);
3545 if (ua_event
== NULL
) {
3546 DBG3("UST app enable event %s not found for app PID %d."
3547 "Skipping app", uevent
->attr
.name
, app
->pid
);
3551 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
3553 pthread_mutex_unlock(&ua_sess
->lock
);
3557 pthread_mutex_unlock(&ua_sess
->lock
);
3566 * For a specific existing UST session and UST channel, creates the event for
3567 * all registered apps.
3569 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
3570 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3573 struct lttng_ht_iter iter
, uiter
;
3574 struct lttng_ht_node_str
*ua_chan_node
;
3575 struct ust_app
*app
;
3576 struct ust_app_session
*ua_sess
;
3577 struct ust_app_channel
*ua_chan
;
3579 DBG("UST app creating event %s for all apps for session id %d",
3580 uevent
->attr
.name
, usess
->id
);
3584 /* For all registered applications */
3585 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3586 if (!app
->compatible
) {
3588 * TODO: In time, we should notice the caller of this error by
3589 * telling him that this is a version error.
3593 ua_sess
= lookup_session_by_app(usess
, app
);
3595 /* The application has problem or is probably dead. */
3599 pthread_mutex_lock(&ua_sess
->lock
);
3600 /* Lookup channel in the ust app session */
3601 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3602 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3603 /* If the channel is not found, there is a code flow error */
3604 assert(ua_chan_node
);
3606 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3608 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
3609 pthread_mutex_unlock(&ua_sess
->lock
);
3611 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
3612 /* Possible value at this point: -ENOMEM. If so, we stop! */
3615 DBG2("UST app event %s already exist on app PID %d",
3616 uevent
->attr
.name
, app
->pid
);
3627 * Start tracing for a specific UST session and app.
3630 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
3633 struct ust_app_session
*ua_sess
;
3635 DBG("Starting tracing for ust app pid %d", app
->pid
);
3639 if (!app
->compatible
) {
3643 ua_sess
= lookup_session_by_app(usess
, app
);
3644 if (ua_sess
== NULL
) {
3645 /* The session is in teardown process. Ignore and continue. */
3649 pthread_mutex_lock(&ua_sess
->lock
);
3651 /* Upon restart, we skip the setup, already done */
3652 if (ua_sess
->started
) {
3656 /* Create directories if consumer is LOCAL and has a path defined. */
3657 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
3658 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
3659 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
3660 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
3662 if (ret
!= -EEXIST
) {
3663 ERR("Trace directory creation error");
3670 * Create the metadata for the application. This returns gracefully if a
3671 * metadata was already set for the session.
3673 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
, NULL
);
3678 health_code_update();
3681 /* This start the UST tracing */
3682 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
3684 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3685 ERR("Error starting tracing for app pid: %d (ret: %d)",
3688 DBG("UST app start session failed. Application is dead.");
3693 /* Indicate that the session has been started once */
3694 ua_sess
->started
= 1;
3696 pthread_mutex_unlock(&ua_sess
->lock
);
3698 health_code_update();
3700 /* Quiescent wait after starting trace */
3701 ret
= ustctl_wait_quiescent(app
->sock
);
3702 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3703 ERR("UST app wait quiescent failed for app pid %d ret %d",
3709 health_code_update();
3713 pthread_mutex_unlock(&ua_sess
->lock
);
3715 health_code_update();
3720 * Stop tracing for a specific UST session and app.
3723 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
3726 struct ust_app_session
*ua_sess
;
3727 struct ust_registry_session
*registry
;
3729 DBG("Stopping tracing for ust app pid %d", app
->pid
);
3733 if (!app
->compatible
) {
3734 goto end_no_session
;
3737 ua_sess
= lookup_session_by_app(usess
, app
);
3738 if (ua_sess
== NULL
) {
3739 goto end_no_session
;
3742 pthread_mutex_lock(&ua_sess
->lock
);
3745 * If started = 0, it means that stop trace has been called for a session
3746 * that was never started. It's possible since we can have a fail start
3747 * from either the application manager thread or the command thread. Simply
3748 * indicate that this is a stop error.
3750 if (!ua_sess
->started
) {
3751 goto error_rcu_unlock
;
3754 health_code_update();
3756 /* This inhibits UST tracing */
3757 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
3759 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3760 ERR("Error stopping tracing for app pid: %d (ret: %d)",
3763 DBG("UST app stop session failed. Application is dead.");
3765 goto error_rcu_unlock
;
3768 health_code_update();
3770 /* Quiescent wait after stopping trace */
3771 ret
= ustctl_wait_quiescent(app
->sock
);
3772 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3773 ERR("UST app wait quiescent failed for app pid %d ret %d",
3777 health_code_update();
3779 registry
= get_session_registry(ua_sess
);
3782 if (!registry
->metadata_closed
) {
3783 /* Push metadata for application before freeing the application. */
3784 (void) push_metadata(registry
, ua_sess
->consumer
);
3787 pthread_mutex_unlock(&ua_sess
->lock
);
3790 health_code_update();
3794 pthread_mutex_unlock(&ua_sess
->lock
);
3796 health_code_update();
3801 * Flush buffers for a specific UST session and app.
3804 int ust_app_flush_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
3807 struct lttng_ht_iter iter
;
3808 struct ust_app_session
*ua_sess
;
3809 struct ust_app_channel
*ua_chan
;
3811 DBG("Flushing buffers for ust app pid %d", app
->pid
);
3815 if (!app
->compatible
) {
3816 goto end_no_session
;
3819 ua_sess
= lookup_session_by_app(usess
, app
);
3820 if (ua_sess
== NULL
) {
3821 goto end_no_session
;
3824 pthread_mutex_lock(&ua_sess
->lock
);
3826 health_code_update();
3828 /* Flushing buffers */
3829 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
3831 health_code_update();
3832 assert(ua_chan
->is_sent
);
3833 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_chan
->obj
);
3835 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3836 ERR("UST app PID %d channel %s flush failed with ret %d",
3837 app
->pid
, ua_chan
->name
, ret
);
3839 DBG3("UST app failed to flush %s. Application is dead.",
3841 /* No need to continue. */
3844 /* Continuing flushing all buffers */
3849 health_code_update();
3851 pthread_mutex_unlock(&ua_sess
->lock
);
3854 health_code_update();
3859 * Destroy a specific UST session in apps.
3861 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
3864 struct ust_app_session
*ua_sess
;
3865 struct lttng_ht_iter iter
;
3866 struct lttng_ht_node_ulong
*node
;
3868 DBG("Destroy tracing for ust app pid %d", app
->pid
);
3872 if (!app
->compatible
) {
3876 __lookup_session_by_app(usess
, app
, &iter
);
3877 node
= lttng_ht_iter_get_node_ulong(&iter
);
3879 /* Session is being or is deleted. */
3882 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
3884 health_code_update();
3885 destroy_app_session(app
, ua_sess
);
3887 health_code_update();
3889 /* Quiescent wait after stopping trace */
3890 ret
= ustctl_wait_quiescent(app
->sock
);
3891 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3892 ERR("UST app wait quiescent failed for app pid %d ret %d",
3897 health_code_update();
3902 * Start tracing for the UST session.
3904 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
3907 struct lttng_ht_iter iter
;
3908 struct ust_app
*app
;
3910 DBG("Starting all UST traces");
3914 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3915 ret
= ust_app_start_trace(usess
, app
);
3917 /* Continue to next apps even on error */
3928 * Start tracing for the UST session.
3930 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
3933 struct lttng_ht_iter iter
;
3934 struct ust_app
*app
;
3936 DBG("Stopping all UST traces");
3940 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3941 ret
= ust_app_stop_trace(usess
, app
);
3943 /* Continue to next apps even on error */
3949 switch (usess
->buffer_type
) {
3950 case LTTNG_BUFFER_PER_UID
:
3952 struct buffer_reg_uid
*reg
;
3954 /* Flush all per UID buffers associated to that session. */
3955 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3956 struct buffer_reg_channel
*reg_chan
;
3957 struct consumer_socket
*socket
;
3959 /* Get consumer socket to use to push the metadata.*/
3960 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
3963 /* Ignore request if no consumer is found for the session. */
3967 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
3968 reg_chan
, node
.node
) {
3970 * The following call will print error values so the return
3971 * code is of little importance because whatever happens, we
3972 * have to try them all.
3974 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
3979 case LTTNG_BUFFER_PER_PID
:
3980 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3981 ret
= ust_app_flush_trace(usess
, app
);
3983 /* Continue to next apps even on error */
3999 * Destroy app UST session.
4001 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4004 struct lttng_ht_iter iter
;
4005 struct ust_app
*app
;
4007 DBG("Destroy all UST traces");
4011 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4012 ret
= destroy_trace(usess
, app
);
4014 /* Continue to next apps even on error */
4025 * Add channels/events from UST global domain to registered apps at sock.
4027 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
4030 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
4031 struct ust_app
*app
;
4032 struct ust_app_session
*ua_sess
= NULL
;
4033 struct ust_app_channel
*ua_chan
;
4034 struct ust_app_event
*ua_event
;
4035 struct ust_app_ctx
*ua_ctx
;
4040 DBG2("UST app global update for app sock %d for session id %d", sock
,
4045 app
= find_app_by_sock(sock
);
4048 * Application can be unregistered before so this is possible hence
4049 * simply stopping the update.
4051 DBG3("UST app update failed to find app sock %d", sock
);
4055 if (!app
->compatible
) {
4059 ret
= create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
4061 /* Tracer is probably gone or ENOMEM. */
4066 pthread_mutex_lock(&ua_sess
->lock
);
4069 * We can iterate safely here over all UST app session since the create ust
4070 * app session above made a shadow copy of the UST global domain from the
4073 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4076 * For a metadata channel, handle it differently.
4078 if (!strncmp(ua_chan
->name
, DEFAULT_METADATA_NAME
,
4079 sizeof(ua_chan
->name
))) {
4080 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
,
4085 /* Remove it from the hash table and continue!. */
4086 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
4088 delete_ust_app_channel(-1, ua_chan
, app
);
4091 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4094 * Stop everything. On error, the application failed, no more
4095 * file descriptor are available or ENOMEM so stopping here is
4096 * the only thing we can do for now.
4102 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
4104 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
4111 /* For each events */
4112 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
4114 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
4121 pthread_mutex_unlock(&ua_sess
->lock
);
4123 if (usess
->start_trace
) {
4124 ret
= ust_app_start_trace(usess
, app
);
4129 DBG2("UST trace started for app pid %d", app
->pid
);
4132 /* Everything went well at this point. */
4137 pthread_mutex_unlock(&ua_sess
->lock
);
4140 destroy_app_session(app
, ua_sess
);
4147 * Add context to a specific channel for global UST domain.
4149 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
4150 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
4153 struct lttng_ht_node_str
*ua_chan_node
;
4154 struct lttng_ht_iter iter
, uiter
;
4155 struct ust_app_channel
*ua_chan
= NULL
;
4156 struct ust_app_session
*ua_sess
;
4157 struct ust_app
*app
;
4161 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4162 if (!app
->compatible
) {
4164 * TODO: In time, we should notice the caller of this error by
4165 * telling him that this is a version error.
4169 ua_sess
= lookup_session_by_app(usess
, app
);
4170 if (ua_sess
== NULL
) {
4174 pthread_mutex_lock(&ua_sess
->lock
);
4175 /* Lookup channel in the ust app session */
4176 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4177 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4178 if (ua_chan_node
== NULL
) {
4181 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4183 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4188 pthread_mutex_unlock(&ua_sess
->lock
);
4196 * Enable event for a channel from a UST session for a specific PID.
4198 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4199 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4202 struct lttng_ht_iter iter
;
4203 struct lttng_ht_node_str
*ua_chan_node
;
4204 struct ust_app
*app
;
4205 struct ust_app_session
*ua_sess
;
4206 struct ust_app_channel
*ua_chan
;
4207 struct ust_app_event
*ua_event
;
4209 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4213 app
= ust_app_find_by_pid(pid
);
4215 ERR("UST app enable event per PID %d not found", pid
);
4220 if (!app
->compatible
) {
4225 ua_sess
= lookup_session_by_app(usess
, app
);
4227 /* The application has problem or is probably dead. */
4232 pthread_mutex_lock(&ua_sess
->lock
);
4233 /* Lookup channel in the ust app session */
4234 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4235 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4236 /* If the channel is not found, there is a code flow error */
4237 assert(ua_chan_node
);
4239 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4241 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4242 uevent
->filter
, uevent
->attr
.loglevel
);
4243 if (ua_event
== NULL
) {
4244 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4249 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4256 pthread_mutex_unlock(&ua_sess
->lock
);
4263 * Disable event for a channel from a UST session for a specific PID.
4265 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
4266 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4269 struct lttng_ht_iter iter
;
4270 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
4271 struct ust_app
*app
;
4272 struct ust_app_session
*ua_sess
;
4273 struct ust_app_channel
*ua_chan
;
4274 struct ust_app_event
*ua_event
;
4276 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
4280 app
= ust_app_find_by_pid(pid
);
4282 ERR("UST app disable event per PID %d not found", pid
);
4287 if (!app
->compatible
) {
4292 ua_sess
= lookup_session_by_app(usess
, app
);
4294 /* The application has problem or is probably dead. */
4298 /* Lookup channel in the ust app session */
4299 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4300 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4301 if (ua_chan_node
== NULL
) {
4302 /* Channel does not exist, skip disabling */
4305 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4307 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
4308 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
4309 if (ua_event_node
== NULL
) {
4310 /* Event does not exist, skip disabling */
4313 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
4315 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4326 * Calibrate registered applications.
4328 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
4331 struct lttng_ht_iter iter
;
4332 struct ust_app
*app
;
4336 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4337 if (!app
->compatible
) {
4339 * TODO: In time, we should notice the caller of this error by
4340 * telling him that this is a version error.
4345 health_code_update();
4347 ret
= ustctl_calibrate(app
->sock
, calibrate
);
4351 /* Means that it's not implemented on the tracer side. */
4355 DBG2("Calibrate app PID %d returned with error %d",
4362 DBG("UST app global domain calibration finished");
4366 health_code_update();
4372 * Receive registration and populate the given msg structure.
4374 * On success return 0 else a negative value returned by the ustctl call.
4376 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
4379 uint32_t pid
, ppid
, uid
, gid
;
4383 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
4384 &pid
, &ppid
, &uid
, &gid
,
4385 &msg
->bits_per_long
,
4386 &msg
->uint8_t_alignment
,
4387 &msg
->uint16_t_alignment
,
4388 &msg
->uint32_t_alignment
,
4389 &msg
->uint64_t_alignment
,
4390 &msg
->long_alignment
,
4397 case LTTNG_UST_ERR_EXITING
:
4398 DBG3("UST app recv reg message failed. Application died");
4400 case LTTNG_UST_ERR_UNSUP_MAJOR
:
4401 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
4402 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
4403 LTTNG_UST_ABI_MINOR_VERSION
);
4406 ERR("UST app recv reg message failed with ret %d", ret
);
4411 msg
->pid
= (pid_t
) pid
;
4412 msg
->ppid
= (pid_t
) ppid
;
4413 msg
->uid
= (uid_t
) uid
;
4414 msg
->gid
= (gid_t
) gid
;
4421 * Return a ust app channel object using the application object and the channel
4422 * object descriptor has a key. If not found, NULL is returned. A RCU read side
4423 * lock MUST be acquired before calling this function.
4425 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
4428 struct lttng_ht_node_ulong
*node
;
4429 struct lttng_ht_iter iter
;
4430 struct ust_app_channel
*ua_chan
= NULL
;
4434 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
4435 node
= lttng_ht_iter_get_node_ulong(&iter
);
4437 DBG2("UST app channel find by objd %d not found", objd
);
4441 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
4448 * Reply to a register channel notification from an application on the notify
4449 * socket. The channel metadata is also created.
4451 * The session UST registry lock is acquired in this function.
4453 * On success 0 is returned else a negative value.
4455 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
4456 size_t nr_fields
, struct ustctl_field
*fields
)
4458 int ret
, ret_code
= 0;
4459 uint32_t chan_id
, reg_count
;
4460 uint64_t chan_reg_key
;
4461 enum ustctl_channel_header type
;
4462 struct ust_app
*app
;
4463 struct ust_app_channel
*ua_chan
;
4464 struct ust_app_session
*ua_sess
;
4465 struct ust_registry_session
*registry
;
4466 struct ust_registry_channel
*chan_reg
;
4470 /* Lookup application. If not found, there is a code flow error. */
4471 app
= find_app_by_notify_sock(sock
);
4473 DBG("Application socket %d is being teardown. Abort event notify",
4476 goto error_rcu_unlock
;
4479 /* Lookup channel by UST object descriptor. */
4480 ua_chan
= find_channel_by_objd(app
, cobjd
);
4482 DBG("Application channel is being teardown. Abort event notify");
4484 goto error_rcu_unlock
;
4487 assert(ua_chan
->session
);
4488 ua_sess
= ua_chan
->session
;
4490 /* Get right session registry depending on the session buffer type. */
4491 registry
= get_session_registry(ua_sess
);
4494 /* Depending on the buffer type, a different channel key is used. */
4495 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4496 chan_reg_key
= ua_chan
->tracing_channel_id
;
4498 chan_reg_key
= ua_chan
->key
;
4501 pthread_mutex_lock(®istry
->lock
);
4503 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
4506 if (!chan_reg
->register_done
) {
4507 reg_count
= ust_registry_get_event_count(chan_reg
);
4508 if (reg_count
< 31) {
4509 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
4511 type
= USTCTL_CHANNEL_HEADER_LARGE
;
4514 chan_reg
->nr_ctx_fields
= nr_fields
;
4515 chan_reg
->ctx_fields
= fields
;
4516 chan_reg
->header_type
= type
;
4518 /* Get current already assigned values. */
4519 type
= chan_reg
->header_type
;
4521 /* Channel id is set during the object creation. */
4522 chan_id
= chan_reg
->chan_id
;
4524 /* Append to metadata */
4525 if (!chan_reg
->metadata_dumped
) {
4526 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
4528 ERR("Error appending channel metadata (errno = %d)", ret_code
);
4534 DBG3("UST app replying to register channel key %" PRIu64
4535 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
4538 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
4540 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4541 ERR("UST app reply channel failed with ret %d", ret
);
4543 DBG3("UST app reply channel failed. Application died");
4548 /* This channel registry registration is completed. */
4549 chan_reg
->register_done
= 1;
4552 pthread_mutex_unlock(®istry
->lock
);
4559 * Add event to the UST channel registry. When the event is added to the
4560 * registry, the metadata is also created. Once done, this replies to the
4561 * application with the appropriate error code.
4563 * The session UST registry lock is acquired in the function.
4565 * On success 0 is returned else a negative value.
4567 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
4568 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
4569 char *model_emf_uri
)
4572 uint32_t event_id
= 0;
4573 uint64_t chan_reg_key
;
4574 struct ust_app
*app
;
4575 struct ust_app_channel
*ua_chan
;
4576 struct ust_app_session
*ua_sess
;
4577 struct ust_registry_session
*registry
;
4581 /* Lookup application. If not found, there is a code flow error. */
4582 app
= find_app_by_notify_sock(sock
);
4584 DBG("Application socket %d is being teardown. Abort event notify",
4587 goto error_rcu_unlock
;
4590 /* Lookup channel by UST object descriptor. */
4591 ua_chan
= find_channel_by_objd(app
, cobjd
);
4593 DBG("Application channel is being teardown. Abort event notify");
4595 goto error_rcu_unlock
;
4598 assert(ua_chan
->session
);
4599 ua_sess
= ua_chan
->session
;
4601 registry
= get_session_registry(ua_sess
);
4604 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4605 chan_reg_key
= ua_chan
->tracing_channel_id
;
4607 chan_reg_key
= ua_chan
->key
;
4610 pthread_mutex_lock(®istry
->lock
);
4612 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
4613 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
, loglevel
,
4614 model_emf_uri
, ua_sess
->buffer_type
, &event_id
);
4617 * The return value is returned to ustctl so in case of an error, the
4618 * application can be notified. In case of an error, it's important not to
4619 * return a negative error or else the application will get closed.
4621 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
4623 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4624 ERR("UST app reply event failed with ret %d", ret
);
4626 DBG3("UST app reply event failed. Application died");
4629 * No need to wipe the create event since the application socket will
4630 * get close on error hence cleaning up everything by itself.
4635 DBG3("UST registry event %s with id %" PRId32
" added successfully",
4639 pthread_mutex_unlock(®istry
->lock
);
4646 * Handle application notification through the given notify socket.
4648 * Return 0 on success or else a negative value.
4650 int ust_app_recv_notify(int sock
)
4653 enum ustctl_notify_cmd cmd
;
4655 DBG3("UST app receiving notify from sock %d", sock
);
4657 ret
= ustctl_recv_notify(sock
, &cmd
);
4659 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4660 ERR("UST app recv notify failed with ret %d", ret
);
4662 DBG3("UST app recv notify failed. Application died");
4668 case USTCTL_NOTIFY_CMD_EVENT
:
4670 int sobjd
, cobjd
, loglevel
;
4671 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
4673 struct ustctl_field
*fields
;
4675 DBG2("UST app ustctl register event received");
4677 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
4678 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
4680 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4681 ERR("UST app recv event failed with ret %d", ret
);
4683 DBG3("UST app recv event failed. Application died");
4688 /* Add event to the UST registry coming from the notify socket. */
4689 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
4690 fields
, loglevel
, model_emf_uri
);
4697 case USTCTL_NOTIFY_CMD_CHANNEL
:
4701 struct ustctl_field
*fields
;
4703 DBG2("UST app ustctl register channel received");
4705 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
4708 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4709 ERR("UST app recv channel failed with ret %d", ret
);
4711 DBG3("UST app recv channel failed. Application died");
4716 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
4725 /* Should NEVER happen. */
4734 * Once the notify socket hangs up, this is called. First, it tries to find the
4735 * corresponding application. On failure, the call_rcu to close the socket is
4736 * executed. If an application is found, it tries to delete it from the notify
4737 * socket hash table. Whathever the result, it proceeds to the call_rcu.
4739 * Note that an object needs to be allocated here so on ENOMEM failure, the
4740 * call RCU is not done but the rest of the cleanup is.
4742 void ust_app_notify_sock_unregister(int sock
)
4745 struct lttng_ht_iter iter
;
4746 struct ust_app
*app
;
4747 struct ust_app_notify_sock_obj
*obj
;
4753 obj
= zmalloc(sizeof(*obj
));
4756 * An ENOMEM is kind of uncool. If this strikes we continue the
4757 * procedure but the call_rcu will not be called. In this case, we
4758 * accept the fd leak rather than possibly creating an unsynchronized
4759 * state between threads.
4761 * TODO: The notify object should be created once the notify socket is
4762 * registered and stored independantely from the ust app object. The
4763 * tricky part is to synchronize the teardown of the application and
4764 * this notify object. Let's keep that in mind so we can avoid this
4765 * kind of shenanigans with ENOMEM in the teardown path.
4772 DBG("UST app notify socket unregister %d", sock
);
4775 * Lookup application by notify socket. If this fails, this means that the
4776 * hash table delete has already been done by the application
4777 * unregistration process so we can safely close the notify socket in a
4780 app
= find_app_by_notify_sock(sock
);
4785 iter
.iter
.node
= &app
->notify_sock_n
.node
;
4788 * Whatever happens here either we fail or succeed, in both cases we have
4789 * to close the socket after a grace period to continue to the call RCU
4790 * here. If the deletion is successful, the application is not visible
4791 * anymore by other threads and is it fails it means that it was already
4792 * deleted from the hash table so either way we just have to close the
4795 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4801 * Close socket after a grace period to avoid for the socket to be reused
4802 * before the application object is freed creating potential race between
4803 * threads trying to add unique in the global hash table.
4806 call_rcu(&obj
->head
, close_notify_sock_rcu
);
4811 * Destroy a ust app data structure and free its memory.
4813 void ust_app_destroy(struct ust_app
*app
)
4819 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);