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"
37 #include "health-sessiond.h"
39 #include "ust-consumer.h"
44 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
46 /* Next available channel key. Access under next_channel_key_lock. */
47 static uint64_t _next_channel_key
;
48 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
50 /* Next available session ID. Access under next_session_id_lock. */
51 static uint64_t _next_session_id
;
52 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
55 * Return the incremented value of next_channel_key.
57 static uint64_t get_next_channel_key(void)
61 pthread_mutex_lock(&next_channel_key_lock
);
62 ret
= ++_next_channel_key
;
63 pthread_mutex_unlock(&next_channel_key_lock
);
68 * Return the atomically incremented value of next_session_id.
70 static uint64_t get_next_session_id(void)
74 pthread_mutex_lock(&next_session_id_lock
);
75 ret
= ++_next_session_id
;
76 pthread_mutex_unlock(&next_session_id_lock
);
80 static void copy_channel_attr_to_ustctl(
81 struct ustctl_consumer_channel_attr
*attr
,
82 struct lttng_ust_channel_attr
*uattr
)
84 /* Copy event attributes since the layout is different. */
85 attr
->subbuf_size
= uattr
->subbuf_size
;
86 attr
->num_subbuf
= uattr
->num_subbuf
;
87 attr
->overwrite
= uattr
->overwrite
;
88 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
89 attr
->read_timer_interval
= uattr
->read_timer_interval
;
90 attr
->output
= uattr
->output
;
94 * Match function for the hash table lookup.
96 * It matches an ust app event based on three attributes which are the event
97 * name, the filter bytecode and the loglevel.
99 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
101 struct ust_app_event
*event
;
102 const struct ust_app_ht_key
*key
;
107 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
110 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
113 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
117 /* Event loglevel. */
118 if (event
->attr
.loglevel
!= key
->loglevel
) {
119 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
120 && key
->loglevel
== 0 && event
->attr
.loglevel
== -1) {
122 * Match is accepted. This is because on event creation, the
123 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
124 * -1 are accepted for this loglevel type since 0 is the one set by
125 * the API when receiving an enable event.
132 /* One of the filters is NULL, fail. */
133 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
137 if (key
->filter
&& event
->filter
) {
138 /* Both filters exists, check length followed by the bytecode. */
139 if (event
->filter
->len
!= key
->filter
->len
||
140 memcmp(event
->filter
->data
, key
->filter
->data
,
141 event
->filter
->len
) != 0) {
146 /* One of the exclusions is NULL, fail. */
147 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
151 if (key
->exclusion
&& event
->exclusion
) {
152 /* Both exclusions exists, check count followed by the names. */
153 if (event
->exclusion
->count
!= key
->exclusion
->count
||
154 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
155 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
169 * Unique add of an ust app event in the given ht. This uses the custom
170 * ht_match_ust_app_event match function and the event name as hash.
172 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
173 struct ust_app_event
*event
)
175 struct cds_lfht_node
*node_ptr
;
176 struct ust_app_ht_key key
;
180 assert(ua_chan
->events
);
183 ht
= ua_chan
->events
;
184 key
.name
= event
->attr
.name
;
185 key
.filter
= event
->filter
;
186 key
.loglevel
= event
->attr
.loglevel
;
187 key
.exclusion
= event
->exclusion
;
189 node_ptr
= cds_lfht_add_unique(ht
->ht
,
190 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
191 ht_match_ust_app_event
, &key
, &event
->node
.node
);
192 assert(node_ptr
== &event
->node
.node
);
196 * Close the notify socket from the given RCU head object. This MUST be called
197 * through a call_rcu().
199 static void close_notify_sock_rcu(struct rcu_head
*head
)
202 struct ust_app_notify_sock_obj
*obj
=
203 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
205 /* Must have a valid fd here. */
206 assert(obj
->fd
>= 0);
208 ret
= close(obj
->fd
);
210 ERR("close notify sock %d RCU", obj
->fd
);
212 lttng_fd_put(LTTNG_FD_APPS
, 1);
218 * Return the session registry according to the buffer type of the given
221 * A registry per UID object MUST exists before calling this function or else
222 * it assert() if not found. RCU read side lock must be acquired.
224 static struct ust_registry_session
*get_session_registry(
225 struct ust_app_session
*ua_sess
)
227 struct ust_registry_session
*registry
= NULL
;
231 switch (ua_sess
->buffer_type
) {
232 case LTTNG_BUFFER_PER_PID
:
234 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
238 registry
= reg_pid
->registry
->reg
.ust
;
241 case LTTNG_BUFFER_PER_UID
:
243 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
244 ua_sess
->tracing_id
, ua_sess
->bits_per_long
, ua_sess
->uid
);
248 registry
= reg_uid
->registry
->reg
.ust
;
260 * Delete ust context safely. RCU read lock must be held before calling
264 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
271 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
272 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
273 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
274 sock
, ua_ctx
->obj
->handle
, ret
);
282 * Delete ust app event safely. RCU read lock must be held before calling
286 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
292 free(ua_event
->filter
);
293 if (ua_event
->exclusion
!= NULL
)
294 free(ua_event
->exclusion
);
295 if (ua_event
->obj
!= NULL
) {
296 ret
= ustctl_release_object(sock
, ua_event
->obj
);
297 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
298 ERR("UST app sock %d release event obj failed with ret %d",
307 * Release ust data object of the given stream.
309 * Return 0 on success or else a negative value.
311 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
318 ret
= ustctl_release_object(sock
, stream
->obj
);
319 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
320 ERR("UST app sock %d release stream obj failed with ret %d",
323 lttng_fd_put(LTTNG_FD_APPS
, 2);
331 * Delete ust app stream safely. RCU read lock must be held before calling
335 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
339 (void) release_ust_app_stream(sock
, stream
);
344 * We need to execute ht_destroy outside of RCU read-side critical
345 * section and outside of call_rcu thread, so we postpone its execution
346 * using ht_cleanup_push. It is simpler than to change the semantic of
347 * the many callers of delete_ust_app_session().
350 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
352 struct ust_app_channel
*ua_chan
=
353 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
355 ht_cleanup_push(ua_chan
->ctx
);
356 ht_cleanup_push(ua_chan
->events
);
361 * Delete ust app channel safely. RCU read lock must be held before calling
365 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
369 struct lttng_ht_iter iter
;
370 struct ust_app_event
*ua_event
;
371 struct ust_app_ctx
*ua_ctx
;
372 struct ust_app_stream
*stream
, *stmp
;
373 struct ust_registry_session
*registry
;
377 DBG3("UST app deleting channel %s", ua_chan
->name
);
380 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
381 cds_list_del(&stream
->list
);
382 delete_ust_app_stream(sock
, stream
);
386 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
387 cds_list_del(&ua_ctx
->list
);
388 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
390 delete_ust_app_ctx(sock
, ua_ctx
);
394 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
396 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
398 delete_ust_app_event(sock
, ua_event
);
401 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
402 /* Wipe and free registry from session registry. */
403 registry
= get_session_registry(ua_chan
->session
);
405 ust_registry_channel_del_free(registry
, ua_chan
->key
);
409 if (ua_chan
->obj
!= NULL
) {
410 /* Remove channel from application UST object descriptor. */
411 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
412 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
414 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
415 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
416 ERR("UST app sock %d release channel obj failed with ret %d",
419 lttng_fd_put(LTTNG_FD_APPS
, 1);
422 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
426 * Push metadata to consumer socket.
428 * RCU read-side lock must be held to guarantee existance of socket.
429 * Must be called with the ust app session lock held.
430 * Must be called with the registry lock held.
432 * On success, return the len of metadata pushed or else a negative value.
433 * Returning a -EPIPE return value means we could not send the metadata,
434 * but it can be caused by recoverable errors (e.g. the application has
435 * terminated concurrently).
437 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
438 struct consumer_socket
*socket
, int send_zero_data
)
441 char *metadata_str
= NULL
;
449 * Means that no metadata was assigned to the session. This can
450 * happens if no start has been done previously.
452 if (!registry
->metadata_key
) {
457 * On a push metadata error either the consumer is dead or the
458 * metadata channel has been destroyed because its endpoint
459 * might have died (e.g: relayd), or because the application has
460 * exited. If so, the metadata closed flag is set to 1 so we
461 * deny pushing metadata again which is not valid anymore on the
464 if (registry
->metadata_closed
) {
468 offset
= registry
->metadata_len_sent
;
469 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
471 DBG3("No metadata to push for metadata key %" PRIu64
,
472 registry
->metadata_key
);
474 if (send_zero_data
) {
475 DBG("No metadata to push");
481 /* Allocate only what we have to send. */
482 metadata_str
= zmalloc(len
);
484 PERROR("zmalloc ust app metadata string");
488 /* Copy what we haven't send out. */
489 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
490 registry
->metadata_len_sent
+= len
;
493 ret
= consumer_push_metadata(socket
, registry
->metadata_key
,
494 metadata_str
, len
, offset
);
497 * There is an acceptable race here between the registry
498 * metadata key assignment and the creation on the
499 * consumer. The session daemon can concurrently push
500 * metadata for this registry while being created on the
501 * consumer since the metadata key of the registry is
502 * assigned *before* it is setup to avoid the consumer
503 * to ask for metadata that could possibly be not found
504 * in the session daemon.
506 * The metadata will get pushed either by the session
507 * being stopped or the consumer requesting metadata if
508 * that race is triggered.
510 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
515 * Update back the actual metadata len sent since it
518 registry
->metadata_len_sent
-= len
;
530 * On error, flag the registry that the metadata is
531 * closed. We were unable to push anything and this
532 * means that either the consumer is not responding or
533 * the metadata cache has been destroyed on the
536 registry
->metadata_closed
= 1;
544 * For a given application and session, push metadata to consumer.
545 * Either sock or consumer is required : if sock is NULL, the default
546 * socket to send the metadata is retrieved from consumer, if sock
547 * is not NULL we use it to send the metadata.
548 * RCU read-side lock must be held while calling this function,
549 * therefore ensuring existance of registry. It also ensures existance
550 * of socket throughout this function.
552 * Return 0 on success else a negative error.
553 * Returning a -EPIPE return value means we could not send the metadata,
554 * but it can be caused by recoverable errors (e.g. the application has
555 * terminated concurrently).
557 static int push_metadata(struct ust_registry_session
*registry
,
558 struct consumer_output
*consumer
)
562 struct consumer_socket
*socket
;
567 pthread_mutex_lock(®istry
->lock
);
568 if (registry
->metadata_closed
) {
573 /* Get consumer socket to use to push the metadata.*/
574 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
581 ret
= ust_app_push_metadata(registry
, socket
, 0);
586 pthread_mutex_unlock(®istry
->lock
);
590 pthread_mutex_unlock(®istry
->lock
);
595 * Send to the consumer a close metadata command for the given session. Once
596 * done, the metadata channel is deleted and the session metadata pointer is
597 * nullified. The session lock MUST be held unless the application is
598 * in the destroy path.
600 * Return 0 on success else a negative value.
602 static int close_metadata(struct ust_registry_session
*registry
,
603 struct consumer_output
*consumer
)
606 struct consumer_socket
*socket
;
613 pthread_mutex_lock(®istry
->lock
);
615 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
620 /* Get consumer socket to use to push the metadata.*/
621 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
628 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
635 * Metadata closed. Even on error this means that the consumer is not
636 * responding or not found so either way a second close should NOT be emit
639 registry
->metadata_closed
= 1;
641 pthread_mutex_unlock(®istry
->lock
);
647 * We need to execute ht_destroy outside of RCU read-side critical
648 * section and outside of call_rcu thread, so we postpone its execution
649 * using ht_cleanup_push. It is simpler than to change the semantic of
650 * the many callers of delete_ust_app_session().
653 void delete_ust_app_session_rcu(struct rcu_head
*head
)
655 struct ust_app_session
*ua_sess
=
656 caa_container_of(head
, struct ust_app_session
, rcu_head
);
658 ht_cleanup_push(ua_sess
->channels
);
663 * Delete ust app session safely. RCU read lock must be held before calling
667 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
671 struct lttng_ht_iter iter
;
672 struct ust_app_channel
*ua_chan
;
673 struct ust_registry_session
*registry
;
677 pthread_mutex_lock(&ua_sess
->lock
);
679 assert(!ua_sess
->deleted
);
680 ua_sess
->deleted
= true;
682 registry
= get_session_registry(ua_sess
);
684 /* Push metadata for application before freeing the application. */
685 (void) push_metadata(registry
, ua_sess
->consumer
);
688 * Don't ask to close metadata for global per UID buffers. Close
689 * metadata only on destroy trace session in this case. Also, the
690 * previous push metadata could have flag the metadata registry to
691 * close so don't send a close command if closed.
693 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
694 /* And ask to close it for this session registry. */
695 (void) close_metadata(registry
, ua_sess
->consumer
);
699 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
701 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
703 delete_ust_app_channel(sock
, ua_chan
, app
);
706 /* In case of per PID, the registry is kept in the session. */
707 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
708 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
710 buffer_reg_pid_remove(reg_pid
);
711 buffer_reg_pid_destroy(reg_pid
);
715 if (ua_sess
->handle
!= -1) {
716 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
717 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
718 ERR("UST app sock %d release session handle failed with ret %d",
722 pthread_mutex_unlock(&ua_sess
->lock
);
724 consumer_output_put(ua_sess
->consumer
);
726 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
730 * Delete a traceable application structure from the global list. Never call
731 * this function outside of a call_rcu call.
733 * RCU read side lock should _NOT_ be held when calling this function.
736 void delete_ust_app(struct ust_app
*app
)
739 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
741 /* Delete ust app sessions info */
746 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
748 /* Free every object in the session and the session. */
750 delete_ust_app_session(sock
, ua_sess
, app
);
754 ht_cleanup_push(app
->sessions
);
755 ht_cleanup_push(app
->ust_objd
);
758 * Wait until we have deleted the application from the sock hash table
759 * before closing this socket, otherwise an application could re-use the
760 * socket ID and race with the teardown, using the same hash table entry.
762 * It's OK to leave the close in call_rcu. We want it to stay unique for
763 * all RCU readers that could run concurrently with unregister app,
764 * therefore we _need_ to only close that socket after a grace period. So
765 * it should stay in this RCU callback.
767 * This close() is a very important step of the synchronization model so
768 * every modification to this function must be carefully reviewed.
774 lttng_fd_put(LTTNG_FD_APPS
, 1);
776 DBG2("UST app pid %d deleted", app
->pid
);
781 * URCU intermediate call to delete an UST app.
784 void delete_ust_app_rcu(struct rcu_head
*head
)
786 struct lttng_ht_node_ulong
*node
=
787 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
788 struct ust_app
*app
=
789 caa_container_of(node
, struct ust_app
, pid_n
);
791 DBG3("Call RCU deleting app PID %d", app
->pid
);
796 * Delete the session from the application ht and delete the data structure by
797 * freeing every object inside and releasing them.
799 static void destroy_app_session(struct ust_app
*app
,
800 struct ust_app_session
*ua_sess
)
803 struct lttng_ht_iter iter
;
808 iter
.iter
.node
= &ua_sess
->node
.node
;
809 ret
= lttng_ht_del(app
->sessions
, &iter
);
811 /* Already scheduled for teardown. */
815 /* Once deleted, free the data structure. */
816 delete_ust_app_session(app
->sock
, ua_sess
, app
);
823 * Alloc new UST app session.
826 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
828 struct ust_app_session
*ua_sess
;
830 /* Init most of the default value by allocating and zeroing */
831 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
832 if (ua_sess
== NULL
) {
837 ua_sess
->handle
= -1;
838 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
839 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
840 pthread_mutex_init(&ua_sess
->lock
, NULL
);
849 * Alloc new UST app channel.
852 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
853 struct ust_app_session
*ua_sess
,
854 struct lttng_ust_channel_attr
*attr
)
856 struct ust_app_channel
*ua_chan
;
858 /* Init most of the default value by allocating and zeroing */
859 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
860 if (ua_chan
== NULL
) {
865 /* Setup channel name */
866 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
867 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
869 ua_chan
->enabled
= 1;
870 ua_chan
->handle
= -1;
871 ua_chan
->session
= ua_sess
;
872 ua_chan
->key
= get_next_channel_key();
873 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
874 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
875 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
877 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
878 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
880 /* Copy attributes */
882 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
883 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
884 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
885 ua_chan
->attr
.overwrite
= attr
->overwrite
;
886 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
887 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
888 ua_chan
->attr
.output
= attr
->output
;
890 /* By default, the channel is a per cpu channel. */
891 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
893 DBG3("UST app channel %s allocated", ua_chan
->name
);
902 * Allocate and initialize a UST app stream.
904 * Return newly allocated stream pointer or NULL on error.
906 struct ust_app_stream
*ust_app_alloc_stream(void)
908 struct ust_app_stream
*stream
= NULL
;
910 stream
= zmalloc(sizeof(*stream
));
911 if (stream
== NULL
) {
912 PERROR("zmalloc ust app stream");
916 /* Zero could be a valid value for a handle so flag it to -1. */
924 * Alloc new UST app event.
927 struct ust_app_event
*alloc_ust_app_event(char *name
,
928 struct lttng_ust_event
*attr
)
930 struct ust_app_event
*ua_event
;
932 /* Init most of the default value by allocating and zeroing */
933 ua_event
= zmalloc(sizeof(struct ust_app_event
));
934 if (ua_event
== NULL
) {
939 ua_event
->enabled
= 1;
940 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
941 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
942 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
944 /* Copy attributes */
946 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
949 DBG3("UST app event %s allocated", ua_event
->name
);
958 * Alloc new UST app context.
961 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
963 struct ust_app_ctx
*ua_ctx
;
965 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
966 if (ua_ctx
== NULL
) {
970 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
973 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
976 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
983 * Allocate a filter and copy the given original filter.
985 * Return allocated filter or NULL on error.
987 static struct lttng_ust_filter_bytecode
*alloc_copy_ust_app_filter(
988 struct lttng_ust_filter_bytecode
*orig_f
)
990 struct lttng_ust_filter_bytecode
*filter
= NULL
;
992 /* Copy filter bytecode */
993 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
995 PERROR("zmalloc alloc ust app filter");
999 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1006 * Find an ust_app using the sock and return it. RCU read side lock must be
1007 * held before calling this helper function.
1009 struct ust_app
*ust_app_find_by_sock(int sock
)
1011 struct lttng_ht_node_ulong
*node
;
1012 struct lttng_ht_iter iter
;
1014 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1015 node
= lttng_ht_iter_get_node_ulong(&iter
);
1017 DBG2("UST app find by sock %d not found", sock
);
1021 return caa_container_of(node
, struct ust_app
, sock_n
);
1028 * Find an ust_app using the notify sock and return it. RCU read side lock must
1029 * be held before calling this helper function.
1031 static struct ust_app
*find_app_by_notify_sock(int sock
)
1033 struct lttng_ht_node_ulong
*node
;
1034 struct lttng_ht_iter iter
;
1036 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1038 node
= lttng_ht_iter_get_node_ulong(&iter
);
1040 DBG2("UST app find by notify sock %d not found", sock
);
1044 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1051 * Lookup for an ust app event based on event name, filter bytecode and the
1054 * Return an ust_app_event object or NULL on error.
1056 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1057 char *name
, struct lttng_ust_filter_bytecode
*filter
, int loglevel
,
1058 const struct lttng_event_exclusion
*exclusion
)
1060 struct lttng_ht_iter iter
;
1061 struct lttng_ht_node_str
*node
;
1062 struct ust_app_event
*event
= NULL
;
1063 struct ust_app_ht_key key
;
1068 /* Setup key for event lookup. */
1070 key
.filter
= filter
;
1071 key
.loglevel
= loglevel
;
1072 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1073 key
.exclusion
= (struct lttng_ust_event_exclusion
*)exclusion
;
1075 /* Lookup using the event name as hash and a custom match fct. */
1076 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1077 ht_match_ust_app_event
, &key
, &iter
.iter
);
1078 node
= lttng_ht_iter_get_node_str(&iter
);
1083 event
= caa_container_of(node
, struct ust_app_event
, node
);
1090 * Create the channel context on the tracer.
1092 * Called with UST app session lock held.
1095 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1096 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1100 health_code_update();
1102 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1103 ua_chan
->obj
, &ua_ctx
->obj
);
1105 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1106 ERR("UST app create channel context failed for app (pid: %d) "
1107 "with ret %d", app
->pid
, ret
);
1110 * This is normal behavior, an application can die during the
1111 * creation process. Don't report an error so the execution can
1112 * continue normally.
1115 DBG3("UST app disable event failed. Application is dead.");
1120 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1122 DBG2("UST app context handle %d created successfully for channel %s",
1123 ua_ctx
->handle
, ua_chan
->name
);
1126 health_code_update();
1131 * Set the filter on the tracer.
1134 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1135 struct ust_app
*app
)
1139 health_code_update();
1141 if (!ua_event
->filter
) {
1146 ret
= ustctl_set_filter(app
->sock
, ua_event
->filter
,
1149 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1150 ERR("UST app event %s filter failed for app (pid: %d) "
1151 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1154 * This is normal behavior, an application can die during the
1155 * creation process. Don't report an error so the execution can
1156 * continue normally.
1159 DBG3("UST app filter event failed. Application is dead.");
1164 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1167 health_code_update();
1172 * Set event exclusions on the tracer.
1175 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1176 struct ust_app
*app
)
1180 health_code_update();
1182 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1187 ret
= ustctl_set_exclusion(app
->sock
, ua_event
->exclusion
,
1190 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1191 ERR("UST app event %s exclusions failed for app (pid: %d) "
1192 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1195 * This is normal behavior, an application can die during the
1196 * creation process. Don't report an error so the execution can
1197 * continue normally.
1200 DBG3("UST app event exclusion failed. Application is dead.");
1205 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1208 health_code_update();
1213 * Disable the specified event on to UST tracer for the UST session.
1215 static int disable_ust_event(struct ust_app
*app
,
1216 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1220 health_code_update();
1222 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1224 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1225 ERR("UST app event %s disable failed for app (pid: %d) "
1226 "and session handle %d with ret %d",
1227 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1230 * This is normal behavior, an application can die during the
1231 * creation process. Don't report an error so the execution can
1232 * continue normally.
1235 DBG3("UST app disable event failed. Application is dead.");
1240 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1241 ua_event
->attr
.name
, app
->pid
);
1244 health_code_update();
1249 * Disable the specified channel on to UST tracer for the UST session.
1251 static int disable_ust_channel(struct ust_app
*app
,
1252 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1256 health_code_update();
1258 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1260 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1261 ERR("UST app channel %s disable failed for app (pid: %d) "
1262 "and session handle %d with ret %d",
1263 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1266 * This is normal behavior, an application can die during the
1267 * creation process. Don't report an error so the execution can
1268 * continue normally.
1271 DBG3("UST app disable channel failed. Application is dead.");
1276 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1277 ua_chan
->name
, app
->pid
);
1280 health_code_update();
1285 * Enable the specified channel on to UST tracer for the UST session.
1287 static int enable_ust_channel(struct ust_app
*app
,
1288 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1292 health_code_update();
1294 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1296 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1297 ERR("UST app channel %s enable failed for app (pid: %d) "
1298 "and session handle %d with ret %d",
1299 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1302 * This is normal behavior, an application can die during the
1303 * creation process. Don't report an error so the execution can
1304 * continue normally.
1307 DBG3("UST app enable channel failed. Application is dead.");
1312 ua_chan
->enabled
= 1;
1314 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1315 ua_chan
->name
, app
->pid
);
1318 health_code_update();
1323 * Enable the specified event on to UST tracer for the UST session.
1325 static int enable_ust_event(struct ust_app
*app
,
1326 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1330 health_code_update();
1332 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1334 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1335 ERR("UST app event %s enable failed for app (pid: %d) "
1336 "and session handle %d with ret %d",
1337 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1340 * This is normal behavior, an application can die during the
1341 * creation process. Don't report an error so the execution can
1342 * continue normally.
1345 DBG3("UST app enable event failed. Application is dead.");
1350 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1351 ua_event
->attr
.name
, app
->pid
);
1354 health_code_update();
1359 * Send channel and stream buffer to application.
1361 * Return 0 on success. On error, a negative value is returned.
1363 static int send_channel_pid_to_ust(struct ust_app
*app
,
1364 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1367 struct ust_app_stream
*stream
, *stmp
;
1373 health_code_update();
1375 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1378 /* Send channel to the application. */
1379 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1380 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1381 ret
= -ENOTCONN
; /* Caused by app exiting. */
1383 } else if (ret
< 0) {
1387 health_code_update();
1389 /* Send all streams to application. */
1390 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1391 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1392 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1393 ret
= -ENOTCONN
; /* Caused by app exiting. */
1395 } else if (ret
< 0) {
1398 /* We don't need the stream anymore once sent to the tracer. */
1399 cds_list_del(&stream
->list
);
1400 delete_ust_app_stream(-1, stream
);
1402 /* Flag the channel that it is sent to the application. */
1403 ua_chan
->is_sent
= 1;
1406 health_code_update();
1411 * Create the specified event onto the UST tracer for a UST session.
1413 * Should be called with session mutex held.
1416 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1417 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1421 health_code_update();
1423 /* Create UST event on tracer */
1424 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1427 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1428 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1429 ua_event
->attr
.name
, app
->pid
, ret
);
1432 * This is normal behavior, an application can die during the
1433 * creation process. Don't report an error so the execution can
1434 * continue normally.
1437 DBG3("UST app create event failed. Application is dead.");
1442 ua_event
->handle
= ua_event
->obj
->handle
;
1444 DBG2("UST app event %s created successfully for pid:%d",
1445 ua_event
->attr
.name
, app
->pid
);
1447 health_code_update();
1449 /* Set filter if one is present. */
1450 if (ua_event
->filter
) {
1451 ret
= set_ust_event_filter(ua_event
, app
);
1457 /* Set exclusions for the event */
1458 if (ua_event
->exclusion
) {
1459 ret
= set_ust_event_exclusion(ua_event
, app
);
1465 /* If event not enabled, disable it on the tracer */
1466 if (ua_event
->enabled
) {
1468 * We now need to explicitly enable the event, since it
1469 * is now disabled at creation.
1471 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1474 * If we hit an EPERM, something is wrong with our enable call. If
1475 * we get an EEXIST, there is a problem on the tracer side since we
1479 case -LTTNG_UST_ERR_PERM
:
1480 /* Code flow problem */
1482 case -LTTNG_UST_ERR_EXIST
:
1483 /* It's OK for our use case. */
1492 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1495 * If we hit an EPERM, something is wrong with our disable call. If
1496 * we get an EEXIST, there is a problem on the tracer side since we
1500 case -LTTNG_UST_ERR_PERM
:
1501 /* Code flow problem */
1503 case -LTTNG_UST_ERR_EXIST
:
1504 /* It's OK for our use case. */
1515 health_code_update();
1520 * Copy data between an UST app event and a LTT event.
1522 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1523 struct ltt_ust_event
*uevent
)
1525 size_t exclusion_alloc_size
;
1527 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1528 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1530 ua_event
->enabled
= uevent
->enabled
;
1532 /* Copy event attributes */
1533 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1535 /* Copy filter bytecode */
1536 if (uevent
->filter
) {
1537 ua_event
->filter
= alloc_copy_ust_app_filter(uevent
->filter
);
1538 /* Filter might be NULL here in case of ENONEM. */
1541 /* Copy exclusion data */
1542 if (uevent
->exclusion
) {
1543 exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1544 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1545 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1546 if (ua_event
->exclusion
== NULL
) {
1549 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1550 exclusion_alloc_size
);
1556 * Copy data between an UST app channel and a LTT channel.
1558 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1559 struct ltt_ust_channel
*uchan
)
1561 struct lttng_ht_iter iter
;
1562 struct ltt_ust_event
*uevent
;
1563 struct ltt_ust_context
*uctx
;
1564 struct ust_app_event
*ua_event
;
1565 struct ust_app_ctx
*ua_ctx
;
1567 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1569 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1570 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1572 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1573 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1575 /* Copy event attributes since the layout is different. */
1576 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1577 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1578 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1579 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1580 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1581 ua_chan
->attr
.output
= uchan
->attr
.output
;
1583 * Note that the attribute channel type is not set since the channel on the
1584 * tracing registry side does not have this information.
1587 ua_chan
->enabled
= uchan
->enabled
;
1588 ua_chan
->tracing_channel_id
= uchan
->id
;
1590 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1591 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1592 if (ua_ctx
== NULL
) {
1595 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1596 (unsigned long) ua_ctx
->ctx
.ctx
);
1597 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1598 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1601 /* Copy all events from ltt ust channel to ust app channel */
1602 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1603 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1604 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1605 if (ua_event
== NULL
) {
1606 DBG2("UST event %s not found on shadow copy channel",
1608 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1609 if (ua_event
== NULL
) {
1612 shadow_copy_event(ua_event
, uevent
);
1613 add_unique_ust_app_event(ua_chan
, ua_event
);
1617 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1621 * Copy data between a UST app session and a regular LTT session.
1623 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1624 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1626 struct lttng_ht_node_str
*ua_chan_node
;
1627 struct lttng_ht_iter iter
;
1628 struct ltt_ust_channel
*uchan
;
1629 struct ust_app_channel
*ua_chan
;
1631 struct tm
*timeinfo
;
1635 /* Get date and time for unique app path */
1637 timeinfo
= localtime(&rawtime
);
1638 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1640 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1642 ua_sess
->tracing_id
= usess
->id
;
1643 ua_sess
->id
= get_next_session_id();
1644 ua_sess
->uid
= app
->uid
;
1645 ua_sess
->gid
= app
->gid
;
1646 ua_sess
->euid
= usess
->uid
;
1647 ua_sess
->egid
= usess
->gid
;
1648 ua_sess
->buffer_type
= usess
->buffer_type
;
1649 ua_sess
->bits_per_long
= app
->bits_per_long
;
1651 /* There is only one consumer object per session possible. */
1652 consumer_output_get(usess
->consumer
);
1653 ua_sess
->consumer
= usess
->consumer
;
1655 ua_sess
->output_traces
= usess
->output_traces
;
1656 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1657 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1658 &usess
->metadata_attr
);
1660 switch (ua_sess
->buffer_type
) {
1661 case LTTNG_BUFFER_PER_PID
:
1662 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1663 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1666 case LTTNG_BUFFER_PER_UID
:
1667 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1668 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1675 PERROR("asprintf UST shadow copy session");
1680 /* Iterate over all channels in global domain. */
1681 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1683 struct lttng_ht_iter uiter
;
1685 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1686 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1687 if (ua_chan_node
!= NULL
) {
1688 /* Session exist. Contiuing. */
1692 DBG2("Channel %s not found on shadow session copy, creating it",
1694 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1695 if (ua_chan
== NULL
) {
1696 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1699 shadow_copy_channel(ua_chan
, uchan
);
1701 * The concept of metadata channel does not exist on the tracing
1702 * registry side of the session daemon so this can only be a per CPU
1703 * channel and not metadata.
1705 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1707 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1712 consumer_output_put(ua_sess
->consumer
);
1716 * Lookup sesison wrapper.
1719 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1720 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1722 /* Get right UST app session from app */
1723 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1727 * Return ust app session from the app session hashtable using the UST session
1730 static struct ust_app_session
*lookup_session_by_app(
1731 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1733 struct lttng_ht_iter iter
;
1734 struct lttng_ht_node_u64
*node
;
1736 __lookup_session_by_app(usess
, app
, &iter
);
1737 node
= lttng_ht_iter_get_node_u64(&iter
);
1742 return caa_container_of(node
, struct ust_app_session
, node
);
1749 * Setup buffer registry per PID for the given session and application. If none
1750 * is found, a new one is created, added to the global registry and
1751 * initialized. If regp is valid, it's set with the newly created object.
1753 * Return 0 on success or else a negative value.
1755 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1756 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1759 struct buffer_reg_pid
*reg_pid
;
1766 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1769 * This is the create channel path meaning that if there is NO
1770 * registry available, we have to create one for this session.
1772 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
);
1776 buffer_reg_pid_add(reg_pid
);
1781 /* Initialize registry. */
1782 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1783 app
->bits_per_long
, app
->uint8_t_alignment
,
1784 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1785 app
->uint64_t_alignment
, app
->long_alignment
,
1786 app
->byte_order
, app
->version
.major
,
1787 app
->version
.minor
);
1792 DBG3("UST app buffer registry per PID created successfully");
1804 * Setup buffer registry per UID for the given session and application. If none
1805 * is found, a new one is created, added to the global registry and
1806 * initialized. If regp is valid, it's set with the newly created object.
1808 * Return 0 on success or else a negative value.
1810 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
1811 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
1814 struct buffer_reg_uid
*reg_uid
;
1821 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
1824 * This is the create channel path meaning that if there is NO
1825 * registry available, we have to create one for this session.
1827 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
1828 LTTNG_DOMAIN_UST
, ®_uid
);
1832 buffer_reg_uid_add(reg_uid
);
1837 /* Initialize registry. */
1838 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
1839 app
->bits_per_long
, app
->uint8_t_alignment
,
1840 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1841 app
->uint64_t_alignment
, app
->long_alignment
,
1842 app
->byte_order
, app
->version
.major
,
1843 app
->version
.minor
);
1847 /* Add node to teardown list of the session. */
1848 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
1850 DBG3("UST app buffer registry per UID created successfully");
1862 * Create a session on the tracer side for the given app.
1864 * On success, ua_sess_ptr is populated with the session pointer or else left
1865 * untouched. If the session was created, is_created is set to 1. On error,
1866 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1869 * Returns 0 on success or else a negative code which is either -ENOMEM or
1870 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1872 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1873 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1876 int ret
, created
= 0;
1877 struct ust_app_session
*ua_sess
;
1881 assert(ua_sess_ptr
);
1883 health_code_update();
1885 ua_sess
= lookup_session_by_app(usess
, app
);
1886 if (ua_sess
== NULL
) {
1887 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
1888 app
->pid
, usess
->id
);
1889 ua_sess
= alloc_ust_app_session(app
);
1890 if (ua_sess
== NULL
) {
1891 /* Only malloc can failed so something is really wrong */
1895 shadow_copy_session(ua_sess
, usess
, app
);
1899 switch (usess
->buffer_type
) {
1900 case LTTNG_BUFFER_PER_PID
:
1901 /* Init local registry. */
1902 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
1907 case LTTNG_BUFFER_PER_UID
:
1908 /* Look for a global registry. If none exists, create one. */
1909 ret
= setup_buffer_reg_uid(usess
, app
, NULL
);
1920 health_code_update();
1922 if (ua_sess
->handle
== -1) {
1923 ret
= ustctl_create_session(app
->sock
);
1925 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1926 ERR("Creating session for app pid %d with ret %d",
1929 DBG("UST app creating session failed. Application is dead");
1931 * This is normal behavior, an application can die during the
1932 * creation process. Don't report an error so the execution can
1933 * continue normally. This will get flagged ENOTCONN and the
1934 * caller will handle it.
1938 delete_ust_app_session(-1, ua_sess
, app
);
1939 if (ret
!= -ENOMEM
) {
1941 * Tracer is probably gone or got an internal error so let's
1942 * behave like it will soon unregister or not usable.
1949 ua_sess
->handle
= ret
;
1951 /* Add ust app session to app's HT */
1952 lttng_ht_node_init_u64(&ua_sess
->node
,
1953 ua_sess
->tracing_id
);
1954 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
1956 DBG2("UST app session created successfully with handle %d", ret
);
1959 *ua_sess_ptr
= ua_sess
;
1961 *is_created
= created
;
1964 /* Everything went well. */
1968 health_code_update();
1973 * Match function for a hash table lookup of ust_app_ctx.
1975 * It matches an ust app context based on the context type and, in the case
1976 * of perf counters, their name.
1978 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
1980 struct ust_app_ctx
*ctx
;
1981 const struct lttng_ust_context
*key
;
1986 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
1990 if (ctx
->ctx
.ctx
!= key
->ctx
) {
1994 /* Check the name in the case of perf thread counters. */
1995 if (key
->ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
1996 if (strncmp(key
->u
.perf_counter
.name
,
1997 ctx
->ctx
.u
.perf_counter
.name
,
1998 sizeof(key
->u
.perf_counter
.name
))) {
2011 * Lookup for an ust app context from an lttng_ust_context.
2013 * Must be called while holding RCU read side lock.
2014 * Return an ust_app_ctx object or NULL on error.
2017 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2018 struct lttng_ust_context
*uctx
)
2020 struct lttng_ht_iter iter
;
2021 struct lttng_ht_node_ulong
*node
;
2022 struct ust_app_ctx
*app_ctx
= NULL
;
2027 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2028 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2029 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2030 node
= lttng_ht_iter_get_node_ulong(&iter
);
2035 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2042 * Create a context for the channel on the tracer.
2044 * Called with UST app session lock held and a RCU read side lock.
2047 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2048 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
2049 struct ust_app
*app
)
2052 struct ust_app_ctx
*ua_ctx
;
2054 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2056 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2062 ua_ctx
= alloc_ust_app_ctx(uctx
);
2063 if (ua_ctx
== NULL
) {
2069 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2070 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2071 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2073 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2083 * Enable on the tracer side a ust app event for the session and channel.
2085 * Called with UST app session lock held.
2088 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2089 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2093 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2098 ua_event
->enabled
= 1;
2105 * Disable on the tracer side a ust app event for the session and channel.
2107 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2108 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2112 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2117 ua_event
->enabled
= 0;
2124 * Lookup ust app channel for session and disable it on the tracer side.
2127 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2128 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2132 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2137 ua_chan
->enabled
= 0;
2144 * Lookup ust app channel for session and enable it on the tracer side. This
2145 * MUST be called with a RCU read side lock acquired.
2147 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2148 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2151 struct lttng_ht_iter iter
;
2152 struct lttng_ht_node_str
*ua_chan_node
;
2153 struct ust_app_channel
*ua_chan
;
2155 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2156 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2157 if (ua_chan_node
== NULL
) {
2158 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2159 uchan
->name
, ua_sess
->tracing_id
);
2163 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2165 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2175 * Ask the consumer to create a channel and get it if successful.
2177 * Return 0 on success or else a negative value.
2179 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2180 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2181 int bitness
, struct ust_registry_session
*registry
)
2184 unsigned int nb_fd
= 0;
2185 struct consumer_socket
*socket
;
2193 health_code_update();
2195 /* Get the right consumer socket for the application. */
2196 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2202 health_code_update();
2204 /* Need one fd for the channel. */
2205 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2207 ERR("Exhausted number of available FD upon create channel");
2212 * Ask consumer to create channel. The consumer will return the number of
2213 * stream we have to expect.
2215 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2222 * Compute the number of fd needed before receiving them. It must be 2 per
2223 * stream (2 being the default value here).
2225 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2227 /* Reserve the amount of file descriptor we need. */
2228 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2230 ERR("Exhausted number of available FD upon create channel");
2231 goto error_fd_get_stream
;
2234 health_code_update();
2237 * Now get the channel from the consumer. This call wil populate the stream
2238 * list of that channel and set the ust objects.
2240 if (usess
->consumer
->enabled
) {
2241 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2251 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2252 error_fd_get_stream
:
2254 * Initiate a destroy channel on the consumer since we had an error
2255 * handling it on our side. The return value is of no importance since we
2256 * already have a ret value set by the previous error that we need to
2259 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2261 lttng_fd_put(LTTNG_FD_APPS
, 1);
2263 health_code_update();
2269 * Duplicate the ust data object of the ust app stream and save it in the
2270 * buffer registry stream.
2272 * Return 0 on success or else a negative value.
2274 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2275 struct ust_app_stream
*stream
)
2282 /* Reserve the amount of file descriptor we need. */
2283 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2285 ERR("Exhausted number of available FD upon duplicate stream");
2289 /* Duplicate object for stream once the original is in the registry. */
2290 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2291 reg_stream
->obj
.ust
);
2293 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2294 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2295 lttng_fd_put(LTTNG_FD_APPS
, 2);
2298 stream
->handle
= stream
->obj
->handle
;
2305 * Duplicate the ust data object of the ust app. channel and save it in the
2306 * buffer registry channel.
2308 * Return 0 on success or else a negative value.
2310 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2311 struct ust_app_channel
*ua_chan
)
2318 /* Need two fds for the channel. */
2319 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2321 ERR("Exhausted number of available FD upon duplicate channel");
2325 /* Duplicate object for stream once the original is in the registry. */
2326 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2328 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2329 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2332 ua_chan
->handle
= ua_chan
->obj
->handle
;
2337 lttng_fd_put(LTTNG_FD_APPS
, 1);
2343 * For a given channel buffer registry, setup all streams of the given ust
2344 * application channel.
2346 * Return 0 on success or else a negative value.
2348 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2349 struct ust_app_channel
*ua_chan
)
2352 struct ust_app_stream
*stream
, *stmp
;
2357 DBG2("UST app setup buffer registry stream");
2359 /* Send all streams to application. */
2360 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2361 struct buffer_reg_stream
*reg_stream
;
2363 ret
= buffer_reg_stream_create(®_stream
);
2369 * Keep original pointer and nullify it in the stream so the delete
2370 * stream call does not release the object.
2372 reg_stream
->obj
.ust
= stream
->obj
;
2374 buffer_reg_stream_add(reg_stream
, reg_chan
);
2376 /* We don't need the streams anymore. */
2377 cds_list_del(&stream
->list
);
2378 delete_ust_app_stream(-1, stream
);
2386 * Create a buffer registry channel for the given session registry and
2387 * application channel object. If regp pointer is valid, it's set with the
2388 * created object. Important, the created object is NOT added to the session
2389 * registry hash table.
2391 * Return 0 on success else a negative value.
2393 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2394 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2397 struct buffer_reg_channel
*reg_chan
= NULL
;
2402 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2404 /* Create buffer registry channel. */
2405 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2410 reg_chan
->consumer_key
= ua_chan
->key
;
2411 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2412 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2414 /* Create and add a channel registry to session. */
2415 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2416 ua_chan
->tracing_channel_id
);
2420 buffer_reg_channel_add(reg_sess
, reg_chan
);
2429 /* Safe because the registry channel object was not added to any HT. */
2430 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2436 * Setup buffer registry channel for the given session registry and application
2437 * channel object. If regp pointer is valid, it's set with the created object.
2439 * Return 0 on success else a negative value.
2441 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2442 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
)
2449 assert(ua_chan
->obj
);
2451 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2453 /* Setup all streams for the registry. */
2454 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
);
2459 reg_chan
->obj
.ust
= ua_chan
->obj
;
2460 ua_chan
->obj
= NULL
;
2465 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2466 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2471 * Send buffer registry channel to the application.
2473 * Return 0 on success else a negative value.
2475 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2476 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2477 struct ust_app_channel
*ua_chan
)
2480 struct buffer_reg_stream
*reg_stream
;
2487 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2489 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2494 /* Send channel to the application. */
2495 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2496 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2497 ret
= -ENOTCONN
; /* Caused by app exiting. */
2499 } else if (ret
< 0) {
2503 health_code_update();
2505 /* Send all streams to application. */
2506 pthread_mutex_lock(®_chan
->stream_list_lock
);
2507 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2508 struct ust_app_stream stream
;
2510 ret
= duplicate_stream_object(reg_stream
, &stream
);
2512 goto error_stream_unlock
;
2515 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2517 (void) release_ust_app_stream(-1, &stream
);
2518 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2519 ret
= -ENOTCONN
; /* Caused by app exiting. */
2520 goto error_stream_unlock
;
2521 } else if (ret
< 0) {
2522 goto error_stream_unlock
;
2524 goto error_stream_unlock
;
2528 * The return value is not important here. This function will output an
2531 (void) release_ust_app_stream(-1, &stream
);
2533 ua_chan
->is_sent
= 1;
2535 error_stream_unlock
:
2536 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2542 * Create and send to the application the created buffers with per UID buffers.
2544 * Return 0 on success else a negative value.
2546 static int create_channel_per_uid(struct ust_app
*app
,
2547 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2548 struct ust_app_channel
*ua_chan
)
2551 struct buffer_reg_uid
*reg_uid
;
2552 struct buffer_reg_channel
*reg_chan
;
2559 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2561 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2563 * The session creation handles the creation of this global registry
2564 * object. If none can be find, there is a code flow problem or a
2569 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2572 /* Create the buffer registry channel object. */
2573 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2575 ERR("Error creating the UST channel \"%s\" registry instance",
2582 * Create the buffers on the consumer side. This call populates the
2583 * ust app channel object with all streams and data object.
2585 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2586 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2588 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2592 * Let's remove the previously created buffer registry channel so
2593 * it's not visible anymore in the session registry.
2595 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2596 ua_chan
->tracing_channel_id
);
2597 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2598 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2603 * Setup the streams and add it to the session registry.
2605 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, reg_chan
);
2607 ERR("Error setting up UST channel \"%s\"",
2614 /* Send buffers to the application. */
2615 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2617 if (ret
!= -ENOTCONN
) {
2618 ERR("Error sending channel to application");
2628 * Create and send to the application the created buffers with per PID buffers.
2630 * Return 0 on success else a negative value.
2632 static int create_channel_per_pid(struct ust_app
*app
,
2633 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2634 struct ust_app_channel
*ua_chan
)
2637 struct ust_registry_session
*registry
;
2644 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2648 registry
= get_session_registry(ua_sess
);
2651 /* Create and add a new channel registry to session. */
2652 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2654 ERR("Error creating the UST channel \"%s\" registry instance",
2659 /* Create and get channel on the consumer side. */
2660 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2661 app
->bits_per_long
, registry
);
2663 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2668 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2670 if (ret
!= -ENOTCONN
) {
2671 ERR("Error sending channel to application");
2682 * From an already allocated ust app channel, create the channel buffers if
2683 * need and send it to the application. This MUST be called with a RCU read
2684 * side lock acquired.
2686 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2687 * the application exited concurrently.
2689 static int do_create_channel(struct ust_app
*app
,
2690 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2691 struct ust_app_channel
*ua_chan
)
2700 /* Handle buffer type before sending the channel to the application. */
2701 switch (usess
->buffer_type
) {
2702 case LTTNG_BUFFER_PER_UID
:
2704 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
2710 case LTTNG_BUFFER_PER_PID
:
2712 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
2724 /* Initialize ust objd object using the received handle and add it. */
2725 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
2726 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
2728 /* If channel is not enabled, disable it on the tracer */
2729 if (!ua_chan
->enabled
) {
2730 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2741 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2742 * newly created channel if not NULL.
2744 * Called with UST app session lock and RCU read-side lock held.
2746 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2747 * the application exited concurrently.
2749 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
2750 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
2751 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
2752 struct ust_app_channel
**ua_chanp
)
2755 struct lttng_ht_iter iter
;
2756 struct lttng_ht_node_str
*ua_chan_node
;
2757 struct ust_app_channel
*ua_chan
;
2759 /* Lookup channel in the ust app session */
2760 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2761 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2762 if (ua_chan_node
!= NULL
) {
2763 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2767 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
2768 if (ua_chan
== NULL
) {
2769 /* Only malloc can fail here */
2773 shadow_copy_channel(ua_chan
, uchan
);
2775 /* Set channel type. */
2776 ua_chan
->attr
.type
= type
;
2778 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
2783 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
2786 /* Only add the channel if successful on the tracer side. */
2787 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
2791 *ua_chanp
= ua_chan
;
2794 /* Everything went well. */
2798 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
2804 * Create UST app event and create it on the tracer side.
2806 * Called with ust app session mutex held.
2809 int create_ust_app_event(struct ust_app_session
*ua_sess
,
2810 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
2811 struct ust_app
*app
)
2814 struct ust_app_event
*ua_event
;
2816 /* Get event node */
2817 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2818 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
2819 if (ua_event
!= NULL
) {
2824 /* Does not exist so create one */
2825 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
2826 if (ua_event
== NULL
) {
2827 /* Only malloc can failed so something is really wrong */
2831 shadow_copy_event(ua_event
, uevent
);
2833 /* Create it on the tracer side */
2834 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2836 /* Not found previously means that it does not exist on the tracer */
2837 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
2841 add_unique_ust_app_event(ua_chan
, ua_event
);
2843 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
2850 /* Valid. Calling here is already in a read side lock */
2851 delete_ust_app_event(-1, ua_event
);
2856 * Create UST metadata and open it on the tracer side.
2858 * Called with UST app session lock held and RCU read side lock.
2860 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
2861 struct ust_app
*app
, struct consumer_output
*consumer
)
2864 struct ust_app_channel
*metadata
;
2865 struct consumer_socket
*socket
;
2866 struct ust_registry_session
*registry
;
2872 registry
= get_session_registry(ua_sess
);
2875 pthread_mutex_lock(®istry
->lock
);
2877 /* Metadata already exists for this registry or it was closed previously */
2878 if (registry
->metadata_key
|| registry
->metadata_closed
) {
2883 /* Allocate UST metadata */
2884 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
2886 /* malloc() failed */
2891 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
2893 /* Need one fd for the channel. */
2894 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2896 ERR("Exhausted number of available FD upon create metadata");
2900 /* Get the right consumer socket for the application. */
2901 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
2904 goto error_consumer
;
2908 * Keep metadata key so we can identify it on the consumer side. Assign it
2909 * to the registry *before* we ask the consumer so we avoid the race of the
2910 * consumer requesting the metadata and the ask_channel call on our side
2911 * did not returned yet.
2913 registry
->metadata_key
= metadata
->key
;
2916 * Ask the metadata channel creation to the consumer. The metadata object
2917 * will be created by the consumer and kept their. However, the stream is
2918 * never added or monitored until we do a first push metadata to the
2921 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
2924 /* Nullify the metadata key so we don't try to close it later on. */
2925 registry
->metadata_key
= 0;
2926 goto error_consumer
;
2930 * The setup command will make the metadata stream be sent to the relayd,
2931 * if applicable, and the thread managing the metadatas. This is important
2932 * because after this point, if an error occurs, the only way the stream
2933 * can be deleted is to be monitored in the consumer.
2935 ret
= consumer_setup_metadata(socket
, metadata
->key
);
2937 /* Nullify the metadata key so we don't try to close it later on. */
2938 registry
->metadata_key
= 0;
2939 goto error_consumer
;
2942 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
2943 metadata
->key
, app
->pid
);
2946 lttng_fd_put(LTTNG_FD_APPS
, 1);
2947 delete_ust_app_channel(-1, metadata
, app
);
2949 pthread_mutex_unlock(®istry
->lock
);
2954 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
2955 * acquired before calling this function.
2957 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
2959 struct ust_app
*app
= NULL
;
2960 struct lttng_ht_node_ulong
*node
;
2961 struct lttng_ht_iter iter
;
2963 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
2964 node
= lttng_ht_iter_get_node_ulong(&iter
);
2966 DBG2("UST app no found with pid %d", pid
);
2970 DBG2("Found UST app by pid %d", pid
);
2972 app
= caa_container_of(node
, struct ust_app
, pid_n
);
2979 * Allocate and init an UST app object using the registration information and
2980 * the command socket. This is called when the command socket connects to the
2983 * The object is returned on success or else NULL.
2985 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
2987 struct ust_app
*lta
= NULL
;
2992 DBG3("UST app creating application for socket %d", sock
);
2994 if ((msg
->bits_per_long
== 64 &&
2995 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
2996 || (msg
->bits_per_long
== 32 &&
2997 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
2998 ERR("Registration failed: application \"%s\" (pid: %d) has "
2999 "%d-bit long, but no consumerd for this size is available.\n",
3000 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3004 lta
= zmalloc(sizeof(struct ust_app
));
3010 lta
->ppid
= msg
->ppid
;
3011 lta
->uid
= msg
->uid
;
3012 lta
->gid
= msg
->gid
;
3014 lta
->bits_per_long
= msg
->bits_per_long
;
3015 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3016 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3017 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3018 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3019 lta
->long_alignment
= msg
->long_alignment
;
3020 lta
->byte_order
= msg
->byte_order
;
3022 lta
->v_major
= msg
->major
;
3023 lta
->v_minor
= msg
->minor
;
3024 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3025 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3026 lta
->notify_sock
= -1;
3028 /* Copy name and make sure it's NULL terminated. */
3029 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3030 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3033 * Before this can be called, when receiving the registration information,
3034 * the application compatibility is checked. So, at this point, the
3035 * application can work with this session daemon.
3037 lta
->compatible
= 1;
3039 lta
->pid
= msg
->pid
;
3040 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3042 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3044 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3051 * For a given application object, add it to every hash table.
3053 void ust_app_add(struct ust_app
*app
)
3056 assert(app
->notify_sock
>= 0);
3061 * On a re-registration, we want to kick out the previous registration of
3064 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3067 * The socket _should_ be unique until _we_ call close. So, a add_unique
3068 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3069 * already in the table.
3071 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3073 /* Add application to the notify socket hash table. */
3074 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3075 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3077 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3078 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3079 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3086 * Set the application version into the object.
3088 * Return 0 on success else a negative value either an errno code or a
3089 * LTTng-UST error code.
3091 int ust_app_version(struct ust_app
*app
)
3097 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3099 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3100 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3102 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3110 * Unregister app by removing it from the global traceable app list and freeing
3113 * The socket is already closed at this point so no close to sock.
3115 void ust_app_unregister(int sock
)
3117 struct ust_app
*lta
;
3118 struct lttng_ht_node_ulong
*node
;
3119 struct lttng_ht_iter ust_app_sock_iter
;
3120 struct lttng_ht_iter iter
;
3121 struct ust_app_session
*ua_sess
;
3126 /* Get the node reference for a call_rcu */
3127 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3128 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3131 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3132 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3135 * For per-PID buffers, perform "push metadata" and flush all
3136 * application streams before removing app from hash tables,
3137 * ensuring proper behavior of data_pending check.
3138 * Remove sessions so they are not visible during deletion.
3140 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3142 struct ust_registry_session
*registry
;
3144 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3146 /* The session was already removed so scheduled for teardown. */
3150 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3151 (void) ust_app_flush_app_session(lta
, ua_sess
);
3155 * Add session to list for teardown. This is safe since at this point we
3156 * are the only one using this list.
3158 pthread_mutex_lock(&ua_sess
->lock
);
3160 if (ua_sess
->deleted
) {
3161 pthread_mutex_unlock(&ua_sess
->lock
);
3166 * Normally, this is done in the delete session process which is
3167 * executed in the call rcu below. However, upon registration we can't
3168 * afford to wait for the grace period before pushing data or else the
3169 * data pending feature can race between the unregistration and stop
3170 * command where the data pending command is sent *before* the grace
3173 * The close metadata below nullifies the metadata pointer in the
3174 * session so the delete session will NOT push/close a second time.
3176 registry
= get_session_registry(ua_sess
);
3178 /* Push metadata for application before freeing the application. */
3179 (void) push_metadata(registry
, ua_sess
->consumer
);
3182 * Don't ask to close metadata for global per UID buffers. Close
3183 * metadata only on destroy trace session in this case. Also, the
3184 * previous push metadata could have flag the metadata registry to
3185 * close so don't send a close command if closed.
3187 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3188 /* And ask to close it for this session registry. */
3189 (void) close_metadata(registry
, ua_sess
->consumer
);
3192 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3194 pthread_mutex_unlock(&ua_sess
->lock
);
3197 /* Remove application from PID hash table */
3198 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3202 * Remove application from notify hash table. The thread handling the
3203 * notify socket could have deleted the node so ignore on error because
3204 * either way it's valid. The close of that socket is handled by the other
3207 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3208 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3211 * Ignore return value since the node might have been removed before by an
3212 * add replace during app registration because the PID can be reassigned by
3215 iter
.iter
.node
= <a
->pid_n
.node
;
3216 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3218 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3223 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3230 * Fill events array with all events name of all registered apps.
3232 int ust_app_list_events(struct lttng_event
**events
)
3235 size_t nbmem
, count
= 0;
3236 struct lttng_ht_iter iter
;
3237 struct ust_app
*app
;
3238 struct lttng_event
*tmp_event
;
3240 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3241 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3242 if (tmp_event
== NULL
) {
3243 PERROR("zmalloc ust app events");
3250 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3251 struct lttng_ust_tracepoint_iter uiter
;
3253 health_code_update();
3255 if (!app
->compatible
) {
3257 * TODO: In time, we should notice the caller of this error by
3258 * telling him that this is a version error.
3262 handle
= ustctl_tracepoint_list(app
->sock
);
3264 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3265 ERR("UST app list events getting handle failed for app pid %d",
3271 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3272 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3273 /* Handle ustctl error. */
3275 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3276 ERR("UST app tp list get failed for app %d with ret %d",
3279 DBG3("UST app tp list get failed. Application is dead");
3281 * This is normal behavior, an application can die during the
3282 * creation process. Don't report an error so the execution can
3283 * continue normally. Continue normal execution.
3291 health_code_update();
3292 if (count
>= nbmem
) {
3293 /* In case the realloc fails, we free the memory */
3294 struct lttng_event
*new_tmp_event
;
3297 new_nbmem
= nbmem
<< 1;
3298 DBG2("Reallocating event list from %zu to %zu entries",
3300 new_tmp_event
= realloc(tmp_event
,
3301 new_nbmem
* sizeof(struct lttng_event
));
3302 if (new_tmp_event
== NULL
) {
3303 PERROR("realloc ust app events");
3308 /* Zero the new memory */
3309 memset(new_tmp_event
+ nbmem
, 0,
3310 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3312 tmp_event
= new_tmp_event
;
3314 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3315 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3316 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3317 tmp_event
[count
].pid
= app
->pid
;
3318 tmp_event
[count
].enabled
= -1;
3324 *events
= tmp_event
;
3326 DBG2("UST app list events done (%zu events)", count
);
3331 health_code_update();
3336 * Fill events array with all events name of all registered apps.
3338 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3341 size_t nbmem
, count
= 0;
3342 struct lttng_ht_iter iter
;
3343 struct ust_app
*app
;
3344 struct lttng_event_field
*tmp_event
;
3346 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3347 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3348 if (tmp_event
== NULL
) {
3349 PERROR("zmalloc ust app event fields");
3356 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3357 struct lttng_ust_field_iter uiter
;
3359 health_code_update();
3361 if (!app
->compatible
) {
3363 * TODO: In time, we should notice the caller of this error by
3364 * telling him that this is a version error.
3368 handle
= ustctl_tracepoint_field_list(app
->sock
);
3370 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3371 ERR("UST app list field getting handle failed for app pid %d",
3377 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3378 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3379 /* Handle ustctl error. */
3381 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3382 ERR("UST app tp list field failed for app %d with ret %d",
3385 DBG3("UST app tp list field failed. Application is dead");
3387 * This is normal behavior, an application can die during the
3388 * creation process. Don't report an error so the execution can
3389 * continue normally. Reset list and count for next app.
3397 health_code_update();
3398 if (count
>= nbmem
) {
3399 /* In case the realloc fails, we free the memory */
3400 struct lttng_event_field
*new_tmp_event
;
3403 new_nbmem
= nbmem
<< 1;
3404 DBG2("Reallocating event field list from %zu to %zu entries",
3406 new_tmp_event
= realloc(tmp_event
,
3407 new_nbmem
* sizeof(struct lttng_event_field
));
3408 if (new_tmp_event
== NULL
) {
3409 PERROR("realloc ust app event fields");
3414 /* Zero the new memory */
3415 memset(new_tmp_event
+ nbmem
, 0,
3416 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3418 tmp_event
= new_tmp_event
;
3421 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3422 /* Mapping between these enums matches 1 to 1. */
3423 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3424 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3426 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3427 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3428 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3429 tmp_event
[count
].event
.pid
= app
->pid
;
3430 tmp_event
[count
].event
.enabled
= -1;
3436 *fields
= tmp_event
;
3438 DBG2("UST app list event fields done (%zu events)", count
);
3443 health_code_update();
3448 * Free and clean all traceable apps of the global list.
3450 * Should _NOT_ be called with RCU read-side lock held.
3452 void ust_app_clean_list(void)
3455 struct ust_app
*app
;
3456 struct lttng_ht_iter iter
;
3458 DBG2("UST app cleaning registered apps hash table");
3462 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3463 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3465 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3468 /* Cleanup socket hash table */
3469 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3471 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3475 /* Cleanup notify socket hash table */
3476 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3477 notify_sock_n
.node
) {
3478 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3483 /* Destroy is done only when the ht is empty */
3484 ht_cleanup_push(ust_app_ht
);
3485 ht_cleanup_push(ust_app_ht_by_sock
);
3486 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3490 * Init UST app hash table.
3492 void ust_app_ht_alloc(void)
3494 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3495 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3496 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3500 * For a specific UST session, disable the channel for all registered apps.
3502 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3503 struct ltt_ust_channel
*uchan
)
3506 struct lttng_ht_iter iter
;
3507 struct lttng_ht_node_str
*ua_chan_node
;
3508 struct ust_app
*app
;
3509 struct ust_app_session
*ua_sess
;
3510 struct ust_app_channel
*ua_chan
;
3512 if (usess
== NULL
|| uchan
== NULL
) {
3513 ERR("Disabling UST global channel with NULL values");
3518 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3519 uchan
->name
, usess
->id
);
3523 /* For every registered applications */
3524 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3525 struct lttng_ht_iter uiter
;
3526 if (!app
->compatible
) {
3528 * TODO: In time, we should notice the caller of this error by
3529 * telling him that this is a version error.
3533 ua_sess
= lookup_session_by_app(usess
, app
);
3534 if (ua_sess
== NULL
) {
3539 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3540 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3541 /* If the session if found for the app, the channel must be there */
3542 assert(ua_chan_node
);
3544 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3545 /* The channel must not be already disabled */
3546 assert(ua_chan
->enabled
== 1);
3548 /* Disable channel onto application */
3549 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3551 /* XXX: We might want to report this error at some point... */
3563 * For a specific UST session, enable the channel for all registered apps.
3565 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3566 struct ltt_ust_channel
*uchan
)
3569 struct lttng_ht_iter iter
;
3570 struct ust_app
*app
;
3571 struct ust_app_session
*ua_sess
;
3573 if (usess
== NULL
|| uchan
== NULL
) {
3574 ERR("Adding UST global channel to NULL values");
3579 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3580 uchan
->name
, usess
->id
);
3584 /* For every 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
);
3594 if (ua_sess
== NULL
) {
3598 /* Enable channel onto application */
3599 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3601 /* XXX: We might want to report this error at some point... */
3613 * Disable an event in a channel and for a specific session.
3615 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3616 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3619 struct lttng_ht_iter iter
, uiter
;
3620 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3621 struct ust_app
*app
;
3622 struct ust_app_session
*ua_sess
;
3623 struct ust_app_channel
*ua_chan
;
3624 struct ust_app_event
*ua_event
;
3626 DBG("UST app disabling event %s for all apps in channel "
3627 "%s for session id %" PRIu64
,
3628 uevent
->attr
.name
, uchan
->name
, usess
->id
);
3632 /* For all registered applications */
3633 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3634 if (!app
->compatible
) {
3636 * TODO: In time, we should notice the caller of this error by
3637 * telling him that this is a version error.
3641 ua_sess
= lookup_session_by_app(usess
, app
);
3642 if (ua_sess
== NULL
) {
3647 /* Lookup channel in the ust app session */
3648 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3649 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3650 if (ua_chan_node
== NULL
) {
3651 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
3652 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
3655 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3657 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
3658 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
3659 if (ua_event_node
== NULL
) {
3660 DBG2("Event %s not found in channel %s for app pid %d."
3661 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
3664 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3666 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3668 /* XXX: Report error someday... */
3679 * For a specific UST session, create the channel for all registered apps.
3681 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
3682 struct ltt_ust_channel
*uchan
)
3684 int ret
= 0, created
;
3685 struct lttng_ht_iter iter
;
3686 struct ust_app
*app
;
3687 struct ust_app_session
*ua_sess
= NULL
;
3689 /* Very wrong code flow */
3693 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
3694 uchan
->name
, usess
->id
);
3698 /* For every registered applications */
3699 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3700 if (!app
->compatible
) {
3702 * TODO: In time, we should notice the caller of this error by
3703 * telling him that this is a version error.
3708 * Create session on the tracer side and add it to app session HT. Note
3709 * that if session exist, it will simply return a pointer to the ust
3712 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
3717 * The application's socket is not valid. Either a bad socket
3718 * or a timeout on it. We can't inform the caller that for a
3719 * specific app, the session failed so lets continue here.
3721 ret
= 0; /* Not an error. */
3725 goto error_rcu_unlock
;
3730 pthread_mutex_lock(&ua_sess
->lock
);
3732 if (ua_sess
->deleted
) {
3733 pthread_mutex_unlock(&ua_sess
->lock
);
3737 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
3738 sizeof(uchan
->name
))) {
3739 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
3742 /* Create channel onto application. We don't need the chan ref. */
3743 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
3744 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
3746 pthread_mutex_unlock(&ua_sess
->lock
);
3748 /* Cleanup the created session if it's the case. */
3750 destroy_app_session(app
, ua_sess
);
3755 * The application's socket is not valid. Either a bad socket
3756 * or a timeout on it. We can't inform the caller that for a
3757 * specific app, the session failed so lets continue here.
3759 ret
= 0; /* Not an error. */
3763 goto error_rcu_unlock
;
3774 * Enable event for a specific session and channel on the tracer.
3776 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
3777 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3780 struct lttng_ht_iter iter
, uiter
;
3781 struct lttng_ht_node_str
*ua_chan_node
;
3782 struct ust_app
*app
;
3783 struct ust_app_session
*ua_sess
;
3784 struct ust_app_channel
*ua_chan
;
3785 struct ust_app_event
*ua_event
;
3787 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
3788 uevent
->attr
.name
, usess
->id
);
3791 * NOTE: At this point, this function is called only if the session and
3792 * channel passed are already created for all apps. and enabled on the
3798 /* For all registered applications */
3799 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3800 if (!app
->compatible
) {
3802 * TODO: In time, we should notice the caller of this error by
3803 * telling him that this is a version error.
3807 ua_sess
= lookup_session_by_app(usess
, app
);
3809 /* The application has problem or is probably dead. */
3813 pthread_mutex_lock(&ua_sess
->lock
);
3815 if (ua_sess
->deleted
) {
3816 pthread_mutex_unlock(&ua_sess
->lock
);
3820 /* Lookup channel in the ust app session */
3821 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3822 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3824 * It is possible that the channel cannot be found is
3825 * the channel/event creation occurs concurrently with
3826 * an application exit.
3828 if (!ua_chan_node
) {
3829 pthread_mutex_unlock(&ua_sess
->lock
);
3833 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3835 /* Get event node */
3836 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3837 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3838 if (ua_event
== NULL
) {
3839 DBG3("UST app enable event %s not found for app PID %d."
3840 "Skipping app", uevent
->attr
.name
, app
->pid
);
3844 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
3846 pthread_mutex_unlock(&ua_sess
->lock
);
3850 pthread_mutex_unlock(&ua_sess
->lock
);
3859 * For a specific existing UST session and UST channel, creates the event for
3860 * all registered apps.
3862 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
3863 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3866 struct lttng_ht_iter iter
, uiter
;
3867 struct lttng_ht_node_str
*ua_chan_node
;
3868 struct ust_app
*app
;
3869 struct ust_app_session
*ua_sess
;
3870 struct ust_app_channel
*ua_chan
;
3872 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
3873 uevent
->attr
.name
, usess
->id
);
3877 /* For all registered applications */
3878 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3879 if (!app
->compatible
) {
3881 * TODO: In time, we should notice the caller of this error by
3882 * telling him that this is a version error.
3886 ua_sess
= lookup_session_by_app(usess
, app
);
3888 /* The application has problem or is probably dead. */
3892 pthread_mutex_lock(&ua_sess
->lock
);
3894 if (ua_sess
->deleted
) {
3895 pthread_mutex_unlock(&ua_sess
->lock
);
3899 /* Lookup channel in the ust app session */
3900 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3901 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3902 /* If the channel is not found, there is a code flow error */
3903 assert(ua_chan_node
);
3905 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3907 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
3908 pthread_mutex_unlock(&ua_sess
->lock
);
3910 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
3911 /* Possible value at this point: -ENOMEM. If so, we stop! */
3914 DBG2("UST app event %s already exist on app PID %d",
3915 uevent
->attr
.name
, app
->pid
);
3926 * Start tracing for a specific UST session and app.
3929 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
3932 struct ust_app_session
*ua_sess
;
3934 DBG("Starting tracing for ust app pid %d", app
->pid
);
3938 if (!app
->compatible
) {
3942 ua_sess
= lookup_session_by_app(usess
, app
);
3943 if (ua_sess
== NULL
) {
3944 /* The session is in teardown process. Ignore and continue. */
3948 pthread_mutex_lock(&ua_sess
->lock
);
3950 if (ua_sess
->deleted
) {
3951 pthread_mutex_unlock(&ua_sess
->lock
);
3955 /* Upon restart, we skip the setup, already done */
3956 if (ua_sess
->started
) {
3960 /* Create directories if consumer is LOCAL and has a path defined. */
3961 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
3962 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
3963 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
3964 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
3966 if (errno
!= EEXIST
) {
3967 ERR("Trace directory creation error");
3974 * Create the metadata for the application. This returns gracefully if a
3975 * metadata was already set for the session.
3977 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
3982 health_code_update();
3985 /* This start the UST tracing */
3986 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
3988 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3989 ERR("Error starting tracing for app pid: %d (ret: %d)",
3992 DBG("UST app start session failed. Application is dead.");
3994 * This is normal behavior, an application can die during the
3995 * creation process. Don't report an error so the execution can
3996 * continue normally.
3998 pthread_mutex_unlock(&ua_sess
->lock
);
4004 /* Indicate that the session has been started once */
4005 ua_sess
->started
= 1;
4007 pthread_mutex_unlock(&ua_sess
->lock
);
4009 health_code_update();
4011 /* Quiescent wait after starting trace */
4012 ret
= ustctl_wait_quiescent(app
->sock
);
4013 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4014 ERR("UST app wait quiescent failed for app pid %d ret %d",
4020 health_code_update();
4024 pthread_mutex_unlock(&ua_sess
->lock
);
4026 health_code_update();
4031 * Stop tracing for a specific UST session and app.
4034 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4037 struct ust_app_session
*ua_sess
;
4038 struct ust_registry_session
*registry
;
4040 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4044 if (!app
->compatible
) {
4045 goto end_no_session
;
4048 ua_sess
= lookup_session_by_app(usess
, app
);
4049 if (ua_sess
== NULL
) {
4050 goto end_no_session
;
4053 pthread_mutex_lock(&ua_sess
->lock
);
4055 if (ua_sess
->deleted
) {
4056 pthread_mutex_unlock(&ua_sess
->lock
);
4057 goto end_no_session
;
4061 * If started = 0, it means that stop trace has been called for a session
4062 * that was never started. It's possible since we can have a fail start
4063 * from either the application manager thread or the command thread. Simply
4064 * indicate that this is a stop error.
4066 if (!ua_sess
->started
) {
4067 goto error_rcu_unlock
;
4070 health_code_update();
4072 /* This inhibits UST tracing */
4073 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4075 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4076 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4079 DBG("UST app stop session failed. Application is dead.");
4081 * This is normal behavior, an application can die during the
4082 * creation process. Don't report an error so the execution can
4083 * continue normally.
4087 goto error_rcu_unlock
;
4090 health_code_update();
4092 /* Quiescent wait after stopping trace */
4093 ret
= ustctl_wait_quiescent(app
->sock
);
4094 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4095 ERR("UST app wait quiescent failed for app pid %d ret %d",
4099 health_code_update();
4101 registry
= get_session_registry(ua_sess
);
4104 /* Push metadata for application before freeing the application. */
4105 (void) push_metadata(registry
, ua_sess
->consumer
);
4108 pthread_mutex_unlock(&ua_sess
->lock
);
4111 health_code_update();
4115 pthread_mutex_unlock(&ua_sess
->lock
);
4117 health_code_update();
4122 int ust_app_flush_app_session(struct ust_app
*app
,
4123 struct ust_app_session
*ua_sess
)
4125 int ret
, retval
= 0;
4126 struct lttng_ht_iter iter
;
4127 struct ust_app_channel
*ua_chan
;
4128 struct consumer_socket
*socket
;
4130 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4134 if (!app
->compatible
) {
4135 goto end_not_compatible
;
4138 pthread_mutex_lock(&ua_sess
->lock
);
4140 if (ua_sess
->deleted
) {
4144 health_code_update();
4146 /* Flushing buffers */
4147 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4150 /* Flush buffers and push metadata. */
4151 switch (ua_sess
->buffer_type
) {
4152 case LTTNG_BUFFER_PER_PID
:
4153 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4155 health_code_update();
4156 assert(ua_chan
->is_sent
);
4157 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4159 ERR("Error flushing consumer channel");
4165 case LTTNG_BUFFER_PER_UID
:
4171 health_code_update();
4174 pthread_mutex_unlock(&ua_sess
->lock
);
4178 health_code_update();
4183 * Flush buffers for all applications for a specific UST session.
4184 * Called with UST session lock held.
4187 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4192 DBG("Flushing session buffers for all ust apps");
4196 /* Flush buffers and push metadata. */
4197 switch (usess
->buffer_type
) {
4198 case LTTNG_BUFFER_PER_UID
:
4200 struct buffer_reg_uid
*reg
;
4201 struct lttng_ht_iter iter
;
4203 /* Flush all per UID buffers associated to that session. */
4204 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4205 struct ust_registry_session
*ust_session_reg
;
4206 struct buffer_reg_channel
*reg_chan
;
4207 struct consumer_socket
*socket
;
4209 /* Get consumer socket to use to push the metadata.*/
4210 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4213 /* Ignore request if no consumer is found for the session. */
4217 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4218 reg_chan
, node
.node
) {
4220 * The following call will print error values so the return
4221 * code is of little importance because whatever happens, we
4222 * have to try them all.
4224 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4227 ust_session_reg
= reg
->registry
->reg
.ust
;
4228 /* Push metadata. */
4229 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4233 case LTTNG_BUFFER_PER_PID
:
4235 struct ust_app_session
*ua_sess
;
4236 struct lttng_ht_iter iter
;
4237 struct ust_app
*app
;
4239 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4240 ua_sess
= lookup_session_by_app(usess
, app
);
4241 if (ua_sess
== NULL
) {
4244 (void) ust_app_flush_app_session(app
, ua_sess
);
4255 health_code_update();
4260 * Destroy a specific UST session in apps.
4262 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4265 struct ust_app_session
*ua_sess
;
4266 struct lttng_ht_iter iter
;
4267 struct lttng_ht_node_u64
*node
;
4269 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4273 if (!app
->compatible
) {
4277 __lookup_session_by_app(usess
, app
, &iter
);
4278 node
= lttng_ht_iter_get_node_u64(&iter
);
4280 /* Session is being or is deleted. */
4283 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4285 health_code_update();
4286 destroy_app_session(app
, ua_sess
);
4288 health_code_update();
4290 /* Quiescent wait after stopping trace */
4291 ret
= ustctl_wait_quiescent(app
->sock
);
4292 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4293 ERR("UST app wait quiescent failed for app pid %d ret %d",
4298 health_code_update();
4303 * Start tracing for the UST session.
4305 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4308 struct lttng_ht_iter iter
;
4309 struct ust_app
*app
;
4311 DBG("Starting all UST traces");
4315 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4316 ret
= ust_app_start_trace(usess
, app
);
4318 /* Continue to next apps even on error */
4329 * Start tracing for the UST session.
4330 * Called with UST session lock held.
4332 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4335 struct lttng_ht_iter iter
;
4336 struct ust_app
*app
;
4338 DBG("Stopping all UST traces");
4342 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4343 ret
= ust_app_stop_trace(usess
, app
);
4345 /* Continue to next apps even on error */
4350 (void) ust_app_flush_session(usess
);
4358 * Destroy app UST session.
4360 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4363 struct lttng_ht_iter iter
;
4364 struct ust_app
*app
;
4366 DBG("Destroy all UST traces");
4370 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4371 ret
= destroy_trace(usess
, app
);
4373 /* Continue to next apps even on error */
4384 * Add channels/events from UST global domain to registered apps at sock.
4386 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
4389 struct lttng_ht_iter iter
, uiter
;
4390 struct ust_app
*app
;
4391 struct ust_app_session
*ua_sess
= NULL
;
4392 struct ust_app_channel
*ua_chan
;
4393 struct ust_app_event
*ua_event
;
4394 struct ust_app_ctx
*ua_ctx
;
4399 DBG2("UST app global update for app sock %d for session id %" PRIu64
, sock
,
4404 app
= ust_app_find_by_sock(sock
);
4407 * Application can be unregistered before so this is possible hence
4408 * simply stopping the update.
4410 DBG3("UST app update failed to find app sock %d", sock
);
4414 if (!app
->compatible
) {
4418 ret
= create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
4420 /* Tracer is probably gone or ENOMEM. */
4425 pthread_mutex_lock(&ua_sess
->lock
);
4427 if (ua_sess
->deleted
) {
4428 pthread_mutex_unlock(&ua_sess
->lock
);
4433 * We can iterate safely here over all UST app session since the create ust
4434 * app session above made a shadow copy of the UST global domain from the
4437 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4439 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4440 if (ret
< 0 && ret
!= -ENOTCONN
) {
4442 * Stop everything. On error, the application
4443 * failed, no more file descriptor are available
4444 * or ENOMEM so stopping here is the only thing
4445 * we can do for now. The only exception is
4446 * -ENOTCONN, which indicates that the application
4453 * Add context using the list so they are enabled in the same order the
4456 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
4457 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
4464 /* For each events */
4465 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
4467 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
4474 pthread_mutex_unlock(&ua_sess
->lock
);
4476 if (usess
->active
) {
4477 ret
= ust_app_start_trace(usess
, app
);
4482 DBG2("UST trace started for app pid %d", app
->pid
);
4485 /* Everything went well at this point. */
4490 pthread_mutex_unlock(&ua_sess
->lock
);
4493 destroy_app_session(app
, ua_sess
);
4500 * Add context to a specific channel for global UST domain.
4502 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
4503 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
4506 struct lttng_ht_node_str
*ua_chan_node
;
4507 struct lttng_ht_iter iter
, uiter
;
4508 struct ust_app_channel
*ua_chan
= NULL
;
4509 struct ust_app_session
*ua_sess
;
4510 struct ust_app
*app
;
4514 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4515 if (!app
->compatible
) {
4517 * TODO: In time, we should notice the caller of this error by
4518 * telling him that this is a version error.
4522 ua_sess
= lookup_session_by_app(usess
, app
);
4523 if (ua_sess
== NULL
) {
4527 pthread_mutex_lock(&ua_sess
->lock
);
4529 if (ua_sess
->deleted
) {
4530 pthread_mutex_unlock(&ua_sess
->lock
);
4534 /* Lookup channel in the ust app session */
4535 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4536 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4537 if (ua_chan_node
== NULL
) {
4540 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4542 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4547 pthread_mutex_unlock(&ua_sess
->lock
);
4555 * Enable event for a channel from a UST session for a specific PID.
4557 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4558 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4561 struct lttng_ht_iter iter
;
4562 struct lttng_ht_node_str
*ua_chan_node
;
4563 struct ust_app
*app
;
4564 struct ust_app_session
*ua_sess
;
4565 struct ust_app_channel
*ua_chan
;
4566 struct ust_app_event
*ua_event
;
4568 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4572 app
= ust_app_find_by_pid(pid
);
4574 ERR("UST app enable event per PID %d not found", pid
);
4579 if (!app
->compatible
) {
4584 ua_sess
= lookup_session_by_app(usess
, app
);
4586 /* The application has problem or is probably dead. */
4591 pthread_mutex_lock(&ua_sess
->lock
);
4593 if (ua_sess
->deleted
) {
4598 /* Lookup channel in the ust app session */
4599 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4600 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4601 /* If the channel is not found, there is a code flow error */
4602 assert(ua_chan_node
);
4604 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4606 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4607 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4608 if (ua_event
== NULL
) {
4609 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4614 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4621 pthread_mutex_unlock(&ua_sess
->lock
);
4628 * Calibrate registered applications.
4630 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
4633 struct lttng_ht_iter iter
;
4634 struct ust_app
*app
;
4638 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4639 if (!app
->compatible
) {
4641 * TODO: In time, we should notice the caller of this error by
4642 * telling him that this is a version error.
4647 health_code_update();
4649 ret
= ustctl_calibrate(app
->sock
, calibrate
);
4653 /* Means that it's not implemented on the tracer side. */
4657 DBG2("Calibrate app PID %d returned with error %d",
4664 DBG("UST app global domain calibration finished");
4668 health_code_update();
4674 * Receive registration and populate the given msg structure.
4676 * On success return 0 else a negative value returned by the ustctl call.
4678 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
4681 uint32_t pid
, ppid
, uid
, gid
;
4685 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
4686 &pid
, &ppid
, &uid
, &gid
,
4687 &msg
->bits_per_long
,
4688 &msg
->uint8_t_alignment
,
4689 &msg
->uint16_t_alignment
,
4690 &msg
->uint32_t_alignment
,
4691 &msg
->uint64_t_alignment
,
4692 &msg
->long_alignment
,
4699 case LTTNG_UST_ERR_EXITING
:
4700 DBG3("UST app recv reg message failed. Application died");
4702 case LTTNG_UST_ERR_UNSUP_MAJOR
:
4703 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
4704 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
4705 LTTNG_UST_ABI_MINOR_VERSION
);
4708 ERR("UST app recv reg message failed with ret %d", ret
);
4713 msg
->pid
= (pid_t
) pid
;
4714 msg
->ppid
= (pid_t
) ppid
;
4715 msg
->uid
= (uid_t
) uid
;
4716 msg
->gid
= (gid_t
) gid
;
4723 * Return a ust app channel object using the application object and the channel
4724 * object descriptor has a key. If not found, NULL is returned. A RCU read side
4725 * lock MUST be acquired before calling this function.
4727 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
4730 struct lttng_ht_node_ulong
*node
;
4731 struct lttng_ht_iter iter
;
4732 struct ust_app_channel
*ua_chan
= NULL
;
4736 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
4737 node
= lttng_ht_iter_get_node_ulong(&iter
);
4739 DBG2("UST app channel find by objd %d not found", objd
);
4743 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
4750 * Reply to a register channel notification from an application on the notify
4751 * socket. The channel metadata is also created.
4753 * The session UST registry lock is acquired in this function.
4755 * On success 0 is returned else a negative value.
4757 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
4758 size_t nr_fields
, struct ustctl_field
*fields
)
4760 int ret
, ret_code
= 0;
4761 uint32_t chan_id
, reg_count
;
4762 uint64_t chan_reg_key
;
4763 enum ustctl_channel_header type
;
4764 struct ust_app
*app
;
4765 struct ust_app_channel
*ua_chan
;
4766 struct ust_app_session
*ua_sess
;
4767 struct ust_registry_session
*registry
;
4768 struct ust_registry_channel
*chan_reg
;
4772 /* Lookup application. If not found, there is a code flow error. */
4773 app
= find_app_by_notify_sock(sock
);
4775 DBG("Application socket %d is being teardown. Abort event notify",
4779 goto error_rcu_unlock
;
4782 /* Lookup channel by UST object descriptor. */
4783 ua_chan
= find_channel_by_objd(app
, cobjd
);
4785 DBG("Application channel is being teardown. Abort event notify");
4788 goto error_rcu_unlock
;
4791 assert(ua_chan
->session
);
4792 ua_sess
= ua_chan
->session
;
4794 /* Get right session registry depending on the session buffer type. */
4795 registry
= get_session_registry(ua_sess
);
4798 /* Depending on the buffer type, a different channel key is used. */
4799 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4800 chan_reg_key
= ua_chan
->tracing_channel_id
;
4802 chan_reg_key
= ua_chan
->key
;
4805 pthread_mutex_lock(®istry
->lock
);
4807 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
4810 if (!chan_reg
->register_done
) {
4811 reg_count
= ust_registry_get_event_count(chan_reg
);
4812 if (reg_count
< 31) {
4813 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
4815 type
= USTCTL_CHANNEL_HEADER_LARGE
;
4818 chan_reg
->nr_ctx_fields
= nr_fields
;
4819 chan_reg
->ctx_fields
= fields
;
4820 chan_reg
->header_type
= type
;
4822 /* Get current already assigned values. */
4823 type
= chan_reg
->header_type
;
4825 /* Set to NULL so the error path does not do a double free. */
4828 /* Channel id is set during the object creation. */
4829 chan_id
= chan_reg
->chan_id
;
4831 /* Append to metadata */
4832 if (!chan_reg
->metadata_dumped
) {
4833 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
4835 ERR("Error appending channel metadata (errno = %d)", ret_code
);
4841 DBG3("UST app replying to register channel key %" PRIu64
4842 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
4845 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
4847 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4848 ERR("UST app reply channel failed with ret %d", ret
);
4850 DBG3("UST app reply channel failed. Application died");
4855 /* This channel registry registration is completed. */
4856 chan_reg
->register_done
= 1;
4859 pthread_mutex_unlock(®istry
->lock
);
4869 * Add event to the UST channel registry. When the event is added to the
4870 * registry, the metadata is also created. Once done, this replies to the
4871 * application with the appropriate error code.
4873 * The session UST registry lock is acquired in the function.
4875 * On success 0 is returned else a negative value.
4877 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
4878 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
4879 char *model_emf_uri
)
4882 uint32_t event_id
= 0;
4883 uint64_t chan_reg_key
;
4884 struct ust_app
*app
;
4885 struct ust_app_channel
*ua_chan
;
4886 struct ust_app_session
*ua_sess
;
4887 struct ust_registry_session
*registry
;
4891 /* Lookup application. If not found, there is a code flow error. */
4892 app
= find_app_by_notify_sock(sock
);
4894 DBG("Application socket %d is being teardown. Abort event notify",
4899 free(model_emf_uri
);
4900 goto error_rcu_unlock
;
4903 /* Lookup channel by UST object descriptor. */
4904 ua_chan
= find_channel_by_objd(app
, cobjd
);
4906 DBG("Application channel is being teardown. Abort event notify");
4910 free(model_emf_uri
);
4911 goto error_rcu_unlock
;
4914 assert(ua_chan
->session
);
4915 ua_sess
= ua_chan
->session
;
4917 registry
= get_session_registry(ua_sess
);
4920 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4921 chan_reg_key
= ua_chan
->tracing_channel_id
;
4923 chan_reg_key
= ua_chan
->key
;
4926 pthread_mutex_lock(®istry
->lock
);
4929 * From this point on, this call acquires the ownership of the sig, fields
4930 * and model_emf_uri meaning any free are done inside it if needed. These
4931 * three variables MUST NOT be read/write after this.
4933 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
4934 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
, loglevel
,
4935 model_emf_uri
, ua_sess
->buffer_type
, &event_id
,
4939 * The return value is returned to ustctl so in case of an error, the
4940 * application can be notified. In case of an error, it's important not to
4941 * return a negative error or else the application will get closed.
4943 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
4945 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4946 ERR("UST app reply event failed with ret %d", ret
);
4948 DBG3("UST app reply event failed. Application died");
4951 * No need to wipe the create event since the application socket will
4952 * get close on error hence cleaning up everything by itself.
4957 DBG3("UST registry event %s with id %" PRId32
" added successfully",
4961 pthread_mutex_unlock(®istry
->lock
);
4968 * Handle application notification through the given notify socket.
4970 * Return 0 on success or else a negative value.
4972 int ust_app_recv_notify(int sock
)
4975 enum ustctl_notify_cmd cmd
;
4977 DBG3("UST app receiving notify from sock %d", sock
);
4979 ret
= ustctl_recv_notify(sock
, &cmd
);
4981 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4982 ERR("UST app recv notify failed with ret %d", ret
);
4984 DBG3("UST app recv notify failed. Application died");
4990 case USTCTL_NOTIFY_CMD_EVENT
:
4992 int sobjd
, cobjd
, loglevel
;
4993 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
4995 struct ustctl_field
*fields
;
4997 DBG2("UST app ustctl register event received");
4999 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
5000 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
5002 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5003 ERR("UST app recv event failed with ret %d", ret
);
5005 DBG3("UST app recv event failed. Application died");
5011 * Add event to the UST registry coming from the notify socket. This
5012 * call will free if needed the sig, fields and model_emf_uri. This
5013 * code path loses the ownsership of these variables and transfer them
5014 * to the this function.
5016 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5017 fields
, loglevel
, model_emf_uri
);
5024 case USTCTL_NOTIFY_CMD_CHANNEL
:
5028 struct ustctl_field
*fields
;
5030 DBG2("UST app ustctl register channel received");
5032 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5035 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5036 ERR("UST app recv channel failed with ret %d", ret
);
5038 DBG3("UST app recv channel failed. Application died");
5044 * The fields ownership are transfered to this function call meaning
5045 * that if needed it will be freed. After this, it's invalid to access
5046 * fields or clean it up.
5048 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
5057 /* Should NEVER happen. */
5066 * Once the notify socket hangs up, this is called. First, it tries to find the
5067 * corresponding application. On failure, the call_rcu to close the socket is
5068 * executed. If an application is found, it tries to delete it from the notify
5069 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5071 * Note that an object needs to be allocated here so on ENOMEM failure, the
5072 * call RCU is not done but the rest of the cleanup is.
5074 void ust_app_notify_sock_unregister(int sock
)
5077 struct lttng_ht_iter iter
;
5078 struct ust_app
*app
;
5079 struct ust_app_notify_sock_obj
*obj
;
5085 obj
= zmalloc(sizeof(*obj
));
5088 * An ENOMEM is kind of uncool. If this strikes we continue the
5089 * procedure but the call_rcu will not be called. In this case, we
5090 * accept the fd leak rather than possibly creating an unsynchronized
5091 * state between threads.
5093 * TODO: The notify object should be created once the notify socket is
5094 * registered and stored independantely from the ust app object. The
5095 * tricky part is to synchronize the teardown of the application and
5096 * this notify object. Let's keep that in mind so we can avoid this
5097 * kind of shenanigans with ENOMEM in the teardown path.
5104 DBG("UST app notify socket unregister %d", sock
);
5107 * Lookup application by notify socket. If this fails, this means that the
5108 * hash table delete has already been done by the application
5109 * unregistration process so we can safely close the notify socket in a
5112 app
= find_app_by_notify_sock(sock
);
5117 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5120 * Whatever happens here either we fail or succeed, in both cases we have
5121 * to close the socket after a grace period to continue to the call RCU
5122 * here. If the deletion is successful, the application is not visible
5123 * anymore by other threads and is it fails it means that it was already
5124 * deleted from the hash table so either way we just have to close the
5127 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5133 * Close socket after a grace period to avoid for the socket to be reused
5134 * before the application object is freed creating potential race between
5135 * threads trying to add unique in the global hash table.
5138 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5143 * Destroy a ust app data structure and free its memory.
5145 void ust_app_destroy(struct ust_app
*app
)
5151 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5155 * Take a snapshot for a given UST session. The snapshot is sent to the given
5158 * Return 0 on success or else a negative value.
5160 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5161 struct snapshot_output
*output
, int wait
,
5162 uint64_t nb_packets_per_stream
)
5165 unsigned int snapshot_done
= 0;
5166 struct lttng_ht_iter iter
;
5167 struct ust_app
*app
;
5168 char pathname
[PATH_MAX
];
5175 switch (usess
->buffer_type
) {
5176 case LTTNG_BUFFER_PER_UID
:
5178 struct buffer_reg_uid
*reg
;
5180 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5181 struct buffer_reg_channel
*reg_chan
;
5182 struct consumer_socket
*socket
;
5184 /* Get consumer socket to use to push the metadata.*/
5185 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5192 memset(pathname
, 0, sizeof(pathname
));
5193 ret
= snprintf(pathname
, sizeof(pathname
),
5194 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5195 reg
->uid
, reg
->bits_per_long
);
5197 PERROR("snprintf snapshot path");
5201 /* Add the UST default trace dir to path. */
5202 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5203 reg_chan
, node
.node
) {
5204 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5205 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5206 nb_packets_per_stream
);
5211 ret
= consumer_snapshot_channel(socket
,
5212 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5213 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
5221 case LTTNG_BUFFER_PER_PID
:
5223 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5224 struct consumer_socket
*socket
;
5225 struct lttng_ht_iter chan_iter
;
5226 struct ust_app_channel
*ua_chan
;
5227 struct ust_app_session
*ua_sess
;
5228 struct ust_registry_session
*registry
;
5230 ua_sess
= lookup_session_by_app(usess
, app
);
5232 /* Session not associated with this app. */
5236 /* Get the right consumer socket for the application. */
5237 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5244 /* Add the UST default trace dir to path. */
5245 memset(pathname
, 0, sizeof(pathname
));
5246 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5249 PERROR("snprintf snapshot path");
5253 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5254 ua_chan
, node
.node
) {
5255 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
5256 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
5257 nb_packets_per_stream
);
5263 registry
= get_session_registry(ua_sess
);
5265 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
5266 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
5279 if (!snapshot_done
) {
5281 * If no snapshot was made and we are not in the error path, this means
5282 * that there are no buffers thus no (prior) application to snapshot
5283 * data from so we have simply NO data.
5294 * Return the size taken by one more packet per stream.
5296 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
5297 uint64_t cur_nr_packets
)
5299 uint64_t tot_size
= 0;
5300 struct ust_app
*app
;
5301 struct lttng_ht_iter iter
;
5305 switch (usess
->buffer_type
) {
5306 case LTTNG_BUFFER_PER_UID
:
5308 struct buffer_reg_uid
*reg
;
5310 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5311 struct buffer_reg_channel
*reg_chan
;
5314 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5315 reg_chan
, node
.node
) {
5316 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
5318 * Don't take channel into account if we
5319 * already grab all its packets.
5323 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
5329 case LTTNG_BUFFER_PER_PID
:
5332 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5333 struct ust_app_channel
*ua_chan
;
5334 struct ust_app_session
*ua_sess
;
5335 struct lttng_ht_iter chan_iter
;
5337 ua_sess
= lookup_session_by_app(usess
, app
);
5339 /* Session not associated with this app. */
5343 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5344 ua_chan
, node
.node
) {
5345 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
5347 * Don't take channel into account if we
5348 * already grab all its packets.
5352 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;