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.
27 #include <sys/types.h>
29 #include <urcu/compiler.h>
30 #include <lttng/ust-error.h>
33 #include <common/common.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
36 #include "buffer-registry.h"
38 #include "health-sessiond.h"
40 #include "ust-consumer.h"
45 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
47 /* Next available channel key. Access under next_channel_key_lock. */
48 static uint64_t _next_channel_key
;
49 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
51 /* Next available session ID. Access under next_session_id_lock. */
52 static uint64_t _next_session_id
;
53 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
56 * Return the incremented value of next_channel_key.
58 static uint64_t get_next_channel_key(void)
62 pthread_mutex_lock(&next_channel_key_lock
);
63 ret
= ++_next_channel_key
;
64 pthread_mutex_unlock(&next_channel_key_lock
);
69 * Return the atomically incremented value of next_session_id.
71 static uint64_t get_next_session_id(void)
75 pthread_mutex_lock(&next_session_id_lock
);
76 ret
= ++_next_session_id
;
77 pthread_mutex_unlock(&next_session_id_lock
);
81 static void copy_channel_attr_to_ustctl(
82 struct ustctl_consumer_channel_attr
*attr
,
83 struct lttng_ust_channel_attr
*uattr
)
85 /* Copy event attributes since the layout is different. */
86 attr
->subbuf_size
= uattr
->subbuf_size
;
87 attr
->num_subbuf
= uattr
->num_subbuf
;
88 attr
->overwrite
= uattr
->overwrite
;
89 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
90 attr
->read_timer_interval
= uattr
->read_timer_interval
;
91 attr
->output
= uattr
->output
;
95 * Match function for the hash table lookup.
97 * It matches an ust app event based on three attributes which are the event
98 * name, the filter bytecode and the loglevel.
100 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
102 struct ust_app_event
*event
;
103 const struct ust_app_ht_key
*key
;
104 int ev_loglevel_value
;
109 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
111 ev_loglevel_value
= event
->attr
.loglevel
;
113 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
116 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
120 /* Event loglevel. */
121 if (ev_loglevel_value
!= key
->loglevel_type
) {
122 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
123 && key
->loglevel_type
== 0 &&
124 ev_loglevel_value
== -1) {
126 * Match is accepted. This is because on event creation, the
127 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
128 * -1 are accepted for this loglevel type since 0 is the one set by
129 * the API when receiving an enable event.
136 /* One of the filters is NULL, fail. */
137 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
141 if (key
->filter
&& event
->filter
) {
142 /* Both filters exists, check length followed by the bytecode. */
143 if (event
->filter
->len
!= key
->filter
->len
||
144 memcmp(event
->filter
->data
, key
->filter
->data
,
145 event
->filter
->len
) != 0) {
150 /* One of the exclusions is NULL, fail. */
151 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
155 if (key
->exclusion
&& event
->exclusion
) {
156 /* Both exclusions exists, check count followed by the names. */
157 if (event
->exclusion
->count
!= key
->exclusion
->count
||
158 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
159 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
173 * Unique add of an ust app event in the given ht. This uses the custom
174 * ht_match_ust_app_event match function and the event name as hash.
176 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
177 struct ust_app_event
*event
)
179 struct cds_lfht_node
*node_ptr
;
180 struct ust_app_ht_key key
;
184 assert(ua_chan
->events
);
187 ht
= ua_chan
->events
;
188 key
.name
= event
->attr
.name
;
189 key
.filter
= event
->filter
;
190 key
.loglevel_type
= event
->attr
.loglevel
;
191 key
.exclusion
= event
->exclusion
;
193 node_ptr
= cds_lfht_add_unique(ht
->ht
,
194 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
195 ht_match_ust_app_event
, &key
, &event
->node
.node
);
196 assert(node_ptr
== &event
->node
.node
);
200 * Close the notify socket from the given RCU head object. This MUST be called
201 * through a call_rcu().
203 static void close_notify_sock_rcu(struct rcu_head
*head
)
206 struct ust_app_notify_sock_obj
*obj
=
207 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
209 /* Must have a valid fd here. */
210 assert(obj
->fd
>= 0);
212 ret
= close(obj
->fd
);
214 ERR("close notify sock %d RCU", obj
->fd
);
216 lttng_fd_put(LTTNG_FD_APPS
, 1);
222 * Return the session registry according to the buffer type of the given
225 * A registry per UID object MUST exists before calling this function or else
226 * it assert() if not found. RCU read side lock must be acquired.
228 static struct ust_registry_session
*get_session_registry(
229 struct ust_app_session
*ua_sess
)
231 struct ust_registry_session
*registry
= NULL
;
235 switch (ua_sess
->buffer_type
) {
236 case LTTNG_BUFFER_PER_PID
:
238 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
242 registry
= reg_pid
->registry
->reg
.ust
;
245 case LTTNG_BUFFER_PER_UID
:
247 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
248 ua_sess
->tracing_id
, ua_sess
->bits_per_long
, ua_sess
->uid
);
252 registry
= reg_uid
->registry
->reg
.ust
;
264 * Delete ust context safely. RCU read lock must be held before calling
268 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
276 pthread_mutex_lock(&app
->sock_lock
);
277 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
278 pthread_mutex_unlock(&app
->sock_lock
);
279 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
280 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
281 sock
, ua_ctx
->obj
->handle
, ret
);
289 * Delete ust app event safely. RCU read lock must be held before calling
293 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
300 free(ua_event
->filter
);
301 if (ua_event
->exclusion
!= NULL
)
302 free(ua_event
->exclusion
);
303 if (ua_event
->obj
!= NULL
) {
304 pthread_mutex_lock(&app
->sock_lock
);
305 ret
= ustctl_release_object(sock
, ua_event
->obj
);
306 pthread_mutex_unlock(&app
->sock_lock
);
307 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
308 ERR("UST app sock %d release event obj failed with ret %d",
317 * Release ust data object of the given stream.
319 * Return 0 on success or else a negative value.
321 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
329 pthread_mutex_lock(&app
->sock_lock
);
330 ret
= ustctl_release_object(sock
, stream
->obj
);
331 pthread_mutex_unlock(&app
->sock_lock
);
332 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
333 ERR("UST app sock %d release stream obj failed with ret %d",
336 lttng_fd_put(LTTNG_FD_APPS
, 2);
344 * Delete ust app stream safely. RCU read lock must be held before calling
348 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
353 (void) release_ust_app_stream(sock
, stream
, app
);
358 * We need to execute ht_destroy outside of RCU read-side critical
359 * section and outside of call_rcu thread, so we postpone its execution
360 * using ht_cleanup_push. It is simpler than to change the semantic of
361 * the many callers of delete_ust_app_session().
364 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
366 struct ust_app_channel
*ua_chan
=
367 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
369 ht_cleanup_push(ua_chan
->ctx
);
370 ht_cleanup_push(ua_chan
->events
);
375 * Delete ust app channel safely. RCU read lock must be held before calling
379 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
383 struct lttng_ht_iter iter
;
384 struct ust_app_event
*ua_event
;
385 struct ust_app_ctx
*ua_ctx
;
386 struct ust_app_stream
*stream
, *stmp
;
387 struct ust_registry_session
*registry
;
391 DBG3("UST app deleting channel %s", ua_chan
->name
);
394 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
395 cds_list_del(&stream
->list
);
396 delete_ust_app_stream(sock
, stream
, app
);
400 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
401 cds_list_del(&ua_ctx
->list
);
402 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
404 delete_ust_app_ctx(sock
, ua_ctx
, app
);
408 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
410 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
412 delete_ust_app_event(sock
, ua_event
, app
);
415 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
416 /* Wipe and free registry from session registry. */
417 registry
= get_session_registry(ua_chan
->session
);
419 ust_registry_channel_del_free(registry
, ua_chan
->key
);
423 if (ua_chan
->obj
!= NULL
) {
424 /* Remove channel from application UST object descriptor. */
425 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
426 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
428 pthread_mutex_lock(&app
->sock_lock
);
429 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
430 pthread_mutex_unlock(&app
->sock_lock
);
431 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
432 ERR("UST app sock %d release channel obj failed with ret %d",
435 lttng_fd_put(LTTNG_FD_APPS
, 1);
438 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
441 int ust_app_register_done(struct ust_app
*app
)
445 pthread_mutex_lock(&app
->sock_lock
);
446 ret
= ustctl_register_done(app
->sock
);
447 pthread_mutex_unlock(&app
->sock_lock
);
451 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
456 pthread_mutex_lock(&app
->sock_lock
);
461 ret
= ustctl_release_object(sock
, data
);
463 pthread_mutex_unlock(&app
->sock_lock
);
469 * Push metadata to consumer socket.
471 * RCU read-side lock must be held to guarantee existance of socket.
472 * Must be called with the ust app session lock held.
473 * Must be called with the registry lock held.
475 * On success, return the len of metadata pushed or else a negative value.
476 * Returning a -EPIPE return value means we could not send the metadata,
477 * but it can be caused by recoverable errors (e.g. the application has
478 * terminated concurrently).
480 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
481 struct consumer_socket
*socket
, int send_zero_data
)
484 char *metadata_str
= NULL
;
485 size_t len
, offset
, new_metadata_len_sent
;
487 uint64_t metadata_key
;
492 metadata_key
= registry
->metadata_key
;
495 * Means that no metadata was assigned to the session. This can
496 * happens if no start has been done previously.
503 * On a push metadata error either the consumer is dead or the
504 * metadata channel has been destroyed because its endpoint
505 * might have died (e.g: relayd), or because the application has
506 * exited. If so, the metadata closed flag is set to 1 so we
507 * deny pushing metadata again which is not valid anymore on the
510 if (registry
->metadata_closed
) {
514 offset
= registry
->metadata_len_sent
;
515 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
516 new_metadata_len_sent
= registry
->metadata_len
;
518 DBG3("No metadata to push for metadata key %" PRIu64
,
519 registry
->metadata_key
);
521 if (send_zero_data
) {
522 DBG("No metadata to push");
528 /* Allocate only what we have to send. */
529 metadata_str
= zmalloc(len
);
531 PERROR("zmalloc ust app metadata string");
535 /* Copy what we haven't sent out. */
536 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
539 pthread_mutex_unlock(®istry
->lock
);
541 * We need to unlock the registry while we push metadata to
542 * break a circular dependency between the consumerd metadata
543 * lock and the sessiond registry lock. Indeed, pushing metadata
544 * to the consumerd awaits that it gets pushed all the way to
545 * relayd, but doing so requires grabbing the metadata lock. If
546 * a concurrent metadata request is being performed by
547 * consumerd, this can try to grab the registry lock on the
548 * sessiond while holding the metadata lock on the consumer
549 * daemon. Those push and pull schemes are performed on two
550 * different bidirectionnal communication sockets.
552 ret
= consumer_push_metadata(socket
, metadata_key
,
553 metadata_str
, len
, offset
);
554 pthread_mutex_lock(®istry
->lock
);
557 * There is an acceptable race here between the registry
558 * metadata key assignment and the creation on the
559 * consumer. The session daemon can concurrently push
560 * metadata for this registry while being created on the
561 * consumer since the metadata key of the registry is
562 * assigned *before* it is setup to avoid the consumer
563 * to ask for metadata that could possibly be not found
564 * in the session daemon.
566 * The metadata will get pushed either by the session
567 * being stopped or the consumer requesting metadata if
568 * that race is triggered.
570 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
573 ERR("Error pushing metadata to consumer");
579 * Metadata may have been concurrently pushed, since
580 * we're not holding the registry lock while pushing to
581 * consumer. This is handled by the fact that we send
582 * the metadata content, size, and the offset at which
583 * that metadata belongs. This may arrive out of order
584 * on the consumer side, and the consumer is able to
585 * deal with overlapping fragments. The consumer
586 * supports overlapping fragments, which must be
587 * contiguous starting from offset 0. We keep the
588 * largest metadata_len_sent value of the concurrent
591 registry
->metadata_len_sent
=
592 max_t(size_t, registry
->metadata_len_sent
,
593 new_metadata_len_sent
);
602 * On error, flag the registry that the metadata is
603 * closed. We were unable to push anything and this
604 * means that either the consumer is not responding or
605 * the metadata cache has been destroyed on the
608 registry
->metadata_closed
= 1;
616 * For a given application and session, push metadata to consumer.
617 * Either sock or consumer is required : if sock is NULL, the default
618 * socket to send the metadata is retrieved from consumer, if sock
619 * is not NULL we use it to send the metadata.
620 * RCU read-side lock must be held while calling this function,
621 * therefore ensuring existance of registry. It also ensures existance
622 * of socket throughout this function.
624 * Return 0 on success else a negative error.
625 * Returning a -EPIPE return value means we could not send the metadata,
626 * but it can be caused by recoverable errors (e.g. the application has
627 * terminated concurrently).
629 static int push_metadata(struct ust_registry_session
*registry
,
630 struct consumer_output
*consumer
)
634 struct consumer_socket
*socket
;
639 pthread_mutex_lock(®istry
->lock
);
640 if (registry
->metadata_closed
) {
645 /* Get consumer socket to use to push the metadata.*/
646 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
653 ret
= ust_app_push_metadata(registry
, socket
, 0);
658 pthread_mutex_unlock(®istry
->lock
);
662 pthread_mutex_unlock(®istry
->lock
);
667 * Send to the consumer a close metadata command for the given session. Once
668 * done, the metadata channel is deleted and the session metadata pointer is
669 * nullified. The session lock MUST be held unless the application is
670 * in the destroy path.
672 * Return 0 on success else a negative value.
674 static int close_metadata(struct ust_registry_session
*registry
,
675 struct consumer_output
*consumer
)
678 struct consumer_socket
*socket
;
685 pthread_mutex_lock(®istry
->lock
);
687 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
692 /* Get consumer socket to use to push the metadata.*/
693 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
700 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
707 * Metadata closed. Even on error this means that the consumer is not
708 * responding or not found so either way a second close should NOT be emit
711 registry
->metadata_closed
= 1;
713 pthread_mutex_unlock(®istry
->lock
);
719 * We need to execute ht_destroy outside of RCU read-side critical
720 * section and outside of call_rcu thread, so we postpone its execution
721 * using ht_cleanup_push. It is simpler than to change the semantic of
722 * the many callers of delete_ust_app_session().
725 void delete_ust_app_session_rcu(struct rcu_head
*head
)
727 struct ust_app_session
*ua_sess
=
728 caa_container_of(head
, struct ust_app_session
, rcu_head
);
730 ht_cleanup_push(ua_sess
->channels
);
735 * Delete ust app session safely. RCU read lock must be held before calling
739 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
743 struct lttng_ht_iter iter
;
744 struct ust_app_channel
*ua_chan
;
745 struct ust_registry_session
*registry
;
749 pthread_mutex_lock(&ua_sess
->lock
);
751 assert(!ua_sess
->deleted
);
752 ua_sess
->deleted
= true;
754 registry
= get_session_registry(ua_sess
);
756 /* Push metadata for application before freeing the application. */
757 (void) push_metadata(registry
, ua_sess
->consumer
);
760 * Don't ask to close metadata for global per UID buffers. Close
761 * metadata only on destroy trace session in this case. Also, the
762 * previous push metadata could have flag the metadata registry to
763 * close so don't send a close command if closed.
765 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
766 /* And ask to close it for this session registry. */
767 (void) close_metadata(registry
, ua_sess
->consumer
);
771 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
773 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
775 delete_ust_app_channel(sock
, ua_chan
, app
);
778 /* In case of per PID, the registry is kept in the session. */
779 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
780 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
782 buffer_reg_pid_remove(reg_pid
);
783 buffer_reg_pid_destroy(reg_pid
);
787 if (ua_sess
->handle
!= -1) {
788 pthread_mutex_lock(&app
->sock_lock
);
789 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
790 pthread_mutex_unlock(&app
->sock_lock
);
791 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
792 ERR("UST app sock %d release session handle failed with ret %d",
796 pthread_mutex_unlock(&ua_sess
->lock
);
798 consumer_output_put(ua_sess
->consumer
);
800 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
804 * Delete a traceable application structure from the global list. Never call
805 * this function outside of a call_rcu call.
807 * RCU read side lock should _NOT_ be held when calling this function.
810 void delete_ust_app(struct ust_app
*app
)
813 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
815 /* Delete ust app sessions info */
820 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
822 /* Free every object in the session and the session. */
824 delete_ust_app_session(sock
, ua_sess
, app
);
828 ht_cleanup_push(app
->sessions
);
829 ht_cleanup_push(app
->ust_objd
);
832 * Wait until we have deleted the application from the sock hash table
833 * before closing this socket, otherwise an application could re-use the
834 * socket ID and race with the teardown, using the same hash table entry.
836 * It's OK to leave the close in call_rcu. We want it to stay unique for
837 * all RCU readers that could run concurrently with unregister app,
838 * therefore we _need_ to only close that socket after a grace period. So
839 * it should stay in this RCU callback.
841 * This close() is a very important step of the synchronization model so
842 * every modification to this function must be carefully reviewed.
848 lttng_fd_put(LTTNG_FD_APPS
, 1);
850 DBG2("UST app pid %d deleted", app
->pid
);
855 * URCU intermediate call to delete an UST app.
858 void delete_ust_app_rcu(struct rcu_head
*head
)
860 struct lttng_ht_node_ulong
*node
=
861 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
862 struct ust_app
*app
=
863 caa_container_of(node
, struct ust_app
, pid_n
);
865 DBG3("Call RCU deleting app PID %d", app
->pid
);
870 * Delete the session from the application ht and delete the data structure by
871 * freeing every object inside and releasing them.
873 static void destroy_app_session(struct ust_app
*app
,
874 struct ust_app_session
*ua_sess
)
877 struct lttng_ht_iter iter
;
882 iter
.iter
.node
= &ua_sess
->node
.node
;
883 ret
= lttng_ht_del(app
->sessions
, &iter
);
885 /* Already scheduled for teardown. */
889 /* Once deleted, free the data structure. */
890 delete_ust_app_session(app
->sock
, ua_sess
, app
);
897 * Alloc new UST app session.
900 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
902 struct ust_app_session
*ua_sess
;
904 /* Init most of the default value by allocating and zeroing */
905 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
906 if (ua_sess
== NULL
) {
911 ua_sess
->handle
= -1;
912 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
913 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
914 pthread_mutex_init(&ua_sess
->lock
, NULL
);
923 * Alloc new UST app channel.
926 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
927 struct ust_app_session
*ua_sess
,
928 struct lttng_ust_channel_attr
*attr
)
930 struct ust_app_channel
*ua_chan
;
932 /* Init most of the default value by allocating and zeroing */
933 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
934 if (ua_chan
== NULL
) {
939 /* Setup channel name */
940 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
941 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
943 ua_chan
->enabled
= 1;
944 ua_chan
->handle
= -1;
945 ua_chan
->session
= ua_sess
;
946 ua_chan
->key
= get_next_channel_key();
947 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
948 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
949 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
951 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
952 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
954 /* Copy attributes */
956 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
957 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
958 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
959 ua_chan
->attr
.overwrite
= attr
->overwrite
;
960 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
961 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
962 ua_chan
->attr
.output
= attr
->output
;
964 /* By default, the channel is a per cpu channel. */
965 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
967 DBG3("UST app channel %s allocated", ua_chan
->name
);
976 * Allocate and initialize a UST app stream.
978 * Return newly allocated stream pointer or NULL on error.
980 struct ust_app_stream
*ust_app_alloc_stream(void)
982 struct ust_app_stream
*stream
= NULL
;
984 stream
= zmalloc(sizeof(*stream
));
985 if (stream
== NULL
) {
986 PERROR("zmalloc ust app stream");
990 /* Zero could be a valid value for a handle so flag it to -1. */
998 * Alloc new UST app event.
1001 struct ust_app_event
*alloc_ust_app_event(char *name
,
1002 struct lttng_ust_event
*attr
)
1004 struct ust_app_event
*ua_event
;
1006 /* Init most of the default value by allocating and zeroing */
1007 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1008 if (ua_event
== NULL
) {
1013 ua_event
->enabled
= 1;
1014 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1015 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1016 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1018 /* Copy attributes */
1020 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1023 DBG3("UST app event %s allocated", ua_event
->name
);
1032 * Alloc new UST app context.
1035 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
1037 struct ust_app_ctx
*ua_ctx
;
1039 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1040 if (ua_ctx
== NULL
) {
1044 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1047 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1050 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1057 * Allocate a filter and copy the given original filter.
1059 * Return allocated filter or NULL on error.
1061 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1062 struct lttng_filter_bytecode
*orig_f
)
1064 struct lttng_filter_bytecode
*filter
= NULL
;
1066 /* Copy filter bytecode */
1067 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1069 PERROR("zmalloc alloc filter bytecode");
1073 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1080 * Create a liblttng-ust filter bytecode from given bytecode.
1082 * Return allocated filter or NULL on error.
1084 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1085 struct lttng_filter_bytecode
*orig_f
)
1087 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1089 /* Copy filter bytecode */
1090 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1092 PERROR("zmalloc alloc ust filter bytecode");
1096 assert(sizeof(struct lttng_filter_bytecode
) ==
1097 sizeof(struct lttng_ust_filter_bytecode
));
1098 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1104 * Find an ust_app using the sock and return it. RCU read side lock must be
1105 * held before calling this helper function.
1107 struct ust_app
*ust_app_find_by_sock(int sock
)
1109 struct lttng_ht_node_ulong
*node
;
1110 struct lttng_ht_iter iter
;
1112 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1113 node
= lttng_ht_iter_get_node_ulong(&iter
);
1115 DBG2("UST app find by sock %d not found", sock
);
1119 return caa_container_of(node
, struct ust_app
, sock_n
);
1126 * Find an ust_app using the notify sock and return it. RCU read side lock must
1127 * be held before calling this helper function.
1129 static struct ust_app
*find_app_by_notify_sock(int sock
)
1131 struct lttng_ht_node_ulong
*node
;
1132 struct lttng_ht_iter iter
;
1134 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1136 node
= lttng_ht_iter_get_node_ulong(&iter
);
1138 DBG2("UST app find by notify sock %d not found", sock
);
1142 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1149 * Lookup for an ust app event based on event name, filter bytecode and the
1152 * Return an ust_app_event object or NULL on error.
1154 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1155 char *name
, struct lttng_filter_bytecode
*filter
,
1157 const struct lttng_event_exclusion
*exclusion
)
1159 struct lttng_ht_iter iter
;
1160 struct lttng_ht_node_str
*node
;
1161 struct ust_app_event
*event
= NULL
;
1162 struct ust_app_ht_key key
;
1167 /* Setup key for event lookup. */
1169 key
.filter
= filter
;
1170 key
.loglevel_type
= loglevel_value
;
1171 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1172 key
.exclusion
= exclusion
;
1174 /* Lookup using the event name as hash and a custom match fct. */
1175 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1176 ht_match_ust_app_event
, &key
, &iter
.iter
);
1177 node
= lttng_ht_iter_get_node_str(&iter
);
1182 event
= caa_container_of(node
, struct ust_app_event
, node
);
1189 * Create the channel context on the tracer.
1191 * Called with UST app session lock held.
1194 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1195 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1199 health_code_update();
1201 pthread_mutex_lock(&app
->sock_lock
);
1202 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1203 ua_chan
->obj
, &ua_ctx
->obj
);
1204 pthread_mutex_unlock(&app
->sock_lock
);
1206 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1207 ERR("UST app create channel context failed for app (pid: %d) "
1208 "with ret %d", app
->pid
, ret
);
1211 * This is normal behavior, an application can die during the
1212 * creation process. Don't report an error so the execution can
1213 * continue normally.
1216 DBG3("UST app disable event failed. Application is dead.");
1221 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1223 DBG2("UST app context handle %d created successfully for channel %s",
1224 ua_ctx
->handle
, ua_chan
->name
);
1227 health_code_update();
1232 * Set the filter on the tracer.
1235 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1236 struct ust_app
*app
)
1239 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1241 health_code_update();
1243 if (!ua_event
->filter
) {
1248 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1249 if (!ust_bytecode
) {
1250 ret
= -LTTNG_ERR_NOMEM
;
1253 pthread_mutex_lock(&app
->sock_lock
);
1254 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1256 pthread_mutex_unlock(&app
->sock_lock
);
1258 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1259 ERR("UST app event %s filter failed for app (pid: %d) "
1260 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1263 * This is normal behavior, an application can die during the
1264 * creation process. Don't report an error so the execution can
1265 * continue normally.
1268 DBG3("UST app filter event failed. Application is dead.");
1273 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1276 health_code_update();
1282 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1283 struct lttng_event_exclusion
*exclusion
)
1285 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1286 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1287 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1289 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1290 if (!ust_exclusion
) {
1295 assert(sizeof(struct lttng_event_exclusion
) ==
1296 sizeof(struct lttng_ust_event_exclusion
));
1297 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1299 return ust_exclusion
;
1303 * Set event exclusions on the tracer.
1306 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1307 struct ust_app
*app
)
1310 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1312 health_code_update();
1314 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1319 ust_exclusion
= create_ust_exclusion_from_exclusion(
1320 ua_event
->exclusion
);
1321 if (!ust_exclusion
) {
1322 ret
= -LTTNG_ERR_NOMEM
;
1325 pthread_mutex_lock(&app
->sock_lock
);
1326 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1327 pthread_mutex_unlock(&app
->sock_lock
);
1329 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1330 ERR("UST app event %s exclusions failed for app (pid: %d) "
1331 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1334 * This is normal behavior, an application can die during the
1335 * creation process. Don't report an error so the execution can
1336 * continue normally.
1339 DBG3("UST app event exclusion failed. Application is dead.");
1344 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1347 health_code_update();
1348 free(ust_exclusion
);
1353 * Disable the specified event on to UST tracer for the UST session.
1355 static int disable_ust_event(struct ust_app
*app
,
1356 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1360 health_code_update();
1362 pthread_mutex_lock(&app
->sock_lock
);
1363 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1364 pthread_mutex_unlock(&app
->sock_lock
);
1366 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1367 ERR("UST app event %s disable failed for app (pid: %d) "
1368 "and session handle %d with ret %d",
1369 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1372 * This is normal behavior, an application can die during the
1373 * creation process. Don't report an error so the execution can
1374 * continue normally.
1377 DBG3("UST app disable event failed. Application is dead.");
1382 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1383 ua_event
->attr
.name
, app
->pid
);
1386 health_code_update();
1391 * Disable the specified channel on to UST tracer for the UST session.
1393 static int disable_ust_channel(struct ust_app
*app
,
1394 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1398 health_code_update();
1400 pthread_mutex_lock(&app
->sock_lock
);
1401 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1402 pthread_mutex_unlock(&app
->sock_lock
);
1404 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1405 ERR("UST app channel %s disable failed for app (pid: %d) "
1406 "and session handle %d with ret %d",
1407 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1410 * This is normal behavior, an application can die during the
1411 * creation process. Don't report an error so the execution can
1412 * continue normally.
1415 DBG3("UST app disable channel failed. Application is dead.");
1420 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1421 ua_chan
->name
, app
->pid
);
1424 health_code_update();
1429 * Enable the specified channel on to UST tracer for the UST session.
1431 static int enable_ust_channel(struct ust_app
*app
,
1432 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1436 health_code_update();
1438 pthread_mutex_lock(&app
->sock_lock
);
1439 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1440 pthread_mutex_unlock(&app
->sock_lock
);
1442 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1443 ERR("UST app channel %s enable failed for app (pid: %d) "
1444 "and session handle %d with ret %d",
1445 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1448 * This is normal behavior, an application can die during the
1449 * creation process. Don't report an error so the execution can
1450 * continue normally.
1453 DBG3("UST app enable channel failed. Application is dead.");
1458 ua_chan
->enabled
= 1;
1460 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1461 ua_chan
->name
, app
->pid
);
1464 health_code_update();
1469 * Enable the specified event on to UST tracer for the UST session.
1471 static int enable_ust_event(struct ust_app
*app
,
1472 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1476 health_code_update();
1478 pthread_mutex_lock(&app
->sock_lock
);
1479 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1480 pthread_mutex_unlock(&app
->sock_lock
);
1482 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1483 ERR("UST app event %s enable failed for app (pid: %d) "
1484 "and session handle %d with ret %d",
1485 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1488 * This is normal behavior, an application can die during the
1489 * creation process. Don't report an error so the execution can
1490 * continue normally.
1493 DBG3("UST app enable event failed. Application is dead.");
1498 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1499 ua_event
->attr
.name
, app
->pid
);
1502 health_code_update();
1507 * Send channel and stream buffer to application.
1509 * Return 0 on success. On error, a negative value is returned.
1511 static int send_channel_pid_to_ust(struct ust_app
*app
,
1512 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1515 struct ust_app_stream
*stream
, *stmp
;
1521 health_code_update();
1523 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1526 /* Send channel to the application. */
1527 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1528 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1529 ret
= -ENOTCONN
; /* Caused by app exiting. */
1531 } else if (ret
< 0) {
1535 health_code_update();
1537 /* Send all streams to application. */
1538 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1539 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1540 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1541 ret
= -ENOTCONN
; /* Caused by app exiting. */
1543 } else if (ret
< 0) {
1546 /* We don't need the stream anymore once sent to the tracer. */
1547 cds_list_del(&stream
->list
);
1548 delete_ust_app_stream(-1, stream
, app
);
1550 /* Flag the channel that it is sent to the application. */
1551 ua_chan
->is_sent
= 1;
1554 health_code_update();
1559 * Create the specified event onto the UST tracer for a UST session.
1561 * Should be called with session mutex held.
1564 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1565 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1569 health_code_update();
1571 /* Create UST event on tracer */
1572 pthread_mutex_lock(&app
->sock_lock
);
1573 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1575 pthread_mutex_unlock(&app
->sock_lock
);
1577 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1578 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1579 ua_event
->attr
.name
, app
->pid
, ret
);
1582 * This is normal behavior, an application can die during the
1583 * creation process. Don't report an error so the execution can
1584 * continue normally.
1587 DBG3("UST app create event failed. Application is dead.");
1592 ua_event
->handle
= ua_event
->obj
->handle
;
1594 DBG2("UST app event %s created successfully for pid:%d",
1595 ua_event
->attr
.name
, app
->pid
);
1597 health_code_update();
1599 /* Set filter if one is present. */
1600 if (ua_event
->filter
) {
1601 ret
= set_ust_event_filter(ua_event
, app
);
1607 /* Set exclusions for the event */
1608 if (ua_event
->exclusion
) {
1609 ret
= set_ust_event_exclusion(ua_event
, app
);
1615 /* If event not enabled, disable it on the tracer */
1616 if (ua_event
->enabled
) {
1618 * We now need to explicitly enable the event, since it
1619 * is now disabled at creation.
1621 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1624 * If we hit an EPERM, something is wrong with our enable call. If
1625 * we get an EEXIST, there is a problem on the tracer side since we
1629 case -LTTNG_UST_ERR_PERM
:
1630 /* Code flow problem */
1632 case -LTTNG_UST_ERR_EXIST
:
1633 /* It's OK for our use case. */
1644 health_code_update();
1649 * Copy data between an UST app event and a LTT event.
1651 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1652 struct ltt_ust_event
*uevent
)
1654 size_t exclusion_alloc_size
;
1656 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1657 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1659 ua_event
->enabled
= uevent
->enabled
;
1661 /* Copy event attributes */
1662 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1664 /* Copy filter bytecode */
1665 if (uevent
->filter
) {
1666 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1667 /* Filter might be NULL here in case of ENONEM. */
1670 /* Copy exclusion data */
1671 if (uevent
->exclusion
) {
1672 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1673 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1674 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1675 if (ua_event
->exclusion
== NULL
) {
1678 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1679 exclusion_alloc_size
);
1685 * Copy data between an UST app channel and a LTT channel.
1687 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1688 struct ltt_ust_channel
*uchan
)
1690 struct lttng_ht_iter iter
;
1691 struct ltt_ust_event
*uevent
;
1692 struct ltt_ust_context
*uctx
;
1693 struct ust_app_event
*ua_event
;
1694 struct ust_app_ctx
*ua_ctx
;
1696 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1698 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1699 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1701 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1702 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1704 /* Copy event attributes since the layout is different. */
1705 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1706 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1707 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1708 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1709 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1710 ua_chan
->attr
.output
= uchan
->attr
.output
;
1712 * Note that the attribute channel type is not set since the channel on the
1713 * tracing registry side does not have this information.
1716 ua_chan
->enabled
= uchan
->enabled
;
1717 ua_chan
->tracing_channel_id
= uchan
->id
;
1719 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1720 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1721 if (ua_ctx
== NULL
) {
1724 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1725 (unsigned long) ua_ctx
->ctx
.ctx
);
1726 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1727 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1730 /* Copy all events from ltt ust channel to ust app channel */
1731 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1732 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1733 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1734 if (ua_event
== NULL
) {
1735 DBG2("UST event %s not found on shadow copy channel",
1737 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1738 if (ua_event
== NULL
) {
1741 shadow_copy_event(ua_event
, uevent
);
1742 add_unique_ust_app_event(ua_chan
, ua_event
);
1746 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1750 * Copy data between a UST app session and a regular LTT session.
1752 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1753 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1755 struct lttng_ht_node_str
*ua_chan_node
;
1756 struct lttng_ht_iter iter
;
1757 struct ltt_ust_channel
*uchan
;
1758 struct ust_app_channel
*ua_chan
;
1760 struct tm
*timeinfo
;
1763 char tmp_shm_path
[PATH_MAX
];
1765 /* Get date and time for unique app path */
1767 timeinfo
= localtime(&rawtime
);
1768 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1770 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1772 ua_sess
->tracing_id
= usess
->id
;
1773 ua_sess
->id
= get_next_session_id();
1774 ua_sess
->uid
= app
->uid
;
1775 ua_sess
->gid
= app
->gid
;
1776 ua_sess
->euid
= usess
->uid
;
1777 ua_sess
->egid
= usess
->gid
;
1778 ua_sess
->buffer_type
= usess
->buffer_type
;
1779 ua_sess
->bits_per_long
= app
->bits_per_long
;
1781 /* There is only one consumer object per session possible. */
1782 consumer_output_get(usess
->consumer
);
1783 ua_sess
->consumer
= usess
->consumer
;
1785 ua_sess
->output_traces
= usess
->output_traces
;
1786 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1787 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1788 &usess
->metadata_attr
);
1790 switch (ua_sess
->buffer_type
) {
1791 case LTTNG_BUFFER_PER_PID
:
1792 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1793 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1796 case LTTNG_BUFFER_PER_UID
:
1797 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1798 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1805 PERROR("asprintf UST shadow copy session");
1810 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1811 sizeof(ua_sess
->root_shm_path
));
1812 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1813 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1814 sizeof(ua_sess
->shm_path
));
1815 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1816 if (ua_sess
->shm_path
[0]) {
1817 switch (ua_sess
->buffer_type
) {
1818 case LTTNG_BUFFER_PER_PID
:
1819 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1820 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1821 app
->name
, app
->pid
, datetime
);
1823 case LTTNG_BUFFER_PER_UID
:
1824 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1825 DEFAULT_UST_TRACE_UID_PATH
,
1826 app
->uid
, app
->bits_per_long
);
1833 PERROR("sprintf UST shadow copy session");
1837 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1838 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1839 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1842 /* Iterate over all channels in global domain. */
1843 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1845 struct lttng_ht_iter uiter
;
1847 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1848 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1849 if (ua_chan_node
!= NULL
) {
1850 /* Session exist. Contiuing. */
1854 DBG2("Channel %s not found on shadow session copy, creating it",
1856 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1857 if (ua_chan
== NULL
) {
1858 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1861 shadow_copy_channel(ua_chan
, uchan
);
1863 * The concept of metadata channel does not exist on the tracing
1864 * registry side of the session daemon so this can only be a per CPU
1865 * channel and not metadata.
1867 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1869 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1874 consumer_output_put(ua_sess
->consumer
);
1878 * Lookup sesison wrapper.
1881 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1882 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1884 /* Get right UST app session from app */
1885 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1889 * Return ust app session from the app session hashtable using the UST session
1892 static struct ust_app_session
*lookup_session_by_app(
1893 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1895 struct lttng_ht_iter iter
;
1896 struct lttng_ht_node_u64
*node
;
1898 __lookup_session_by_app(usess
, app
, &iter
);
1899 node
= lttng_ht_iter_get_node_u64(&iter
);
1904 return caa_container_of(node
, struct ust_app_session
, node
);
1911 * Setup buffer registry per PID for the given session and application. If none
1912 * is found, a new one is created, added to the global registry and
1913 * initialized. If regp is valid, it's set with the newly created object.
1915 * Return 0 on success or else a negative value.
1917 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1918 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1921 struct buffer_reg_pid
*reg_pid
;
1928 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1931 * This is the create channel path meaning that if there is NO
1932 * registry available, we have to create one for this session.
1934 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
1935 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1943 /* Initialize registry. */
1944 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1945 app
->bits_per_long
, app
->uint8_t_alignment
,
1946 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1947 app
->uint64_t_alignment
, app
->long_alignment
,
1948 app
->byte_order
, app
->version
.major
,
1949 app
->version
.minor
, reg_pid
->root_shm_path
,
1951 ua_sess
->euid
, ua_sess
->egid
);
1954 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1955 * destroy the buffer registry, because it is always expected
1956 * that if the buffer registry can be found, its ust registry is
1959 buffer_reg_pid_destroy(reg_pid
);
1963 buffer_reg_pid_add(reg_pid
);
1965 DBG3("UST app buffer registry per PID created successfully");
1977 * Setup buffer registry per UID for the given session and application. If none
1978 * is found, a new one is created, added to the global registry and
1979 * initialized. If regp is valid, it's set with the newly created object.
1981 * Return 0 on success or else a negative value.
1983 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
1984 struct ust_app_session
*ua_sess
,
1985 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
1988 struct buffer_reg_uid
*reg_uid
;
1995 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
1998 * This is the create channel path meaning that if there is NO
1999 * registry available, we have to create one for this session.
2001 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2002 LTTNG_DOMAIN_UST
, ®_uid
,
2003 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2011 /* Initialize registry. */
2012 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2013 app
->bits_per_long
, app
->uint8_t_alignment
,
2014 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2015 app
->uint64_t_alignment
, app
->long_alignment
,
2016 app
->byte_order
, app
->version
.major
,
2017 app
->version
.minor
, reg_uid
->root_shm_path
,
2018 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
2021 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2022 * destroy the buffer registry, because it is always expected
2023 * that if the buffer registry can be found, its ust registry is
2026 buffer_reg_uid_destroy(reg_uid
, NULL
);
2029 /* Add node to teardown list of the session. */
2030 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2032 buffer_reg_uid_add(reg_uid
);
2034 DBG3("UST app buffer registry per UID created successfully");
2045 * Create a session on the tracer side for the given app.
2047 * On success, ua_sess_ptr is populated with the session pointer or else left
2048 * untouched. If the session was created, is_created is set to 1. On error,
2049 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2052 * Returns 0 on success or else a negative code which is either -ENOMEM or
2053 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2055 static int create_ust_app_session(struct ltt_ust_session
*usess
,
2056 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2059 int ret
, created
= 0;
2060 struct ust_app_session
*ua_sess
;
2064 assert(ua_sess_ptr
);
2066 health_code_update();
2068 ua_sess
= lookup_session_by_app(usess
, app
);
2069 if (ua_sess
== NULL
) {
2070 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2071 app
->pid
, usess
->id
);
2072 ua_sess
= alloc_ust_app_session(app
);
2073 if (ua_sess
== NULL
) {
2074 /* Only malloc can failed so something is really wrong */
2078 shadow_copy_session(ua_sess
, usess
, app
);
2082 switch (usess
->buffer_type
) {
2083 case LTTNG_BUFFER_PER_PID
:
2084 /* Init local registry. */
2085 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2087 delete_ust_app_session(-1, ua_sess
, app
);
2091 case LTTNG_BUFFER_PER_UID
:
2092 /* Look for a global registry. If none exists, create one. */
2093 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2095 delete_ust_app_session(-1, ua_sess
, app
);
2105 health_code_update();
2107 if (ua_sess
->handle
== -1) {
2108 pthread_mutex_lock(&app
->sock_lock
);
2109 ret
= ustctl_create_session(app
->sock
);
2110 pthread_mutex_unlock(&app
->sock_lock
);
2112 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2113 ERR("Creating session for app pid %d with ret %d",
2116 DBG("UST app creating session failed. Application is dead");
2118 * This is normal behavior, an application can die during the
2119 * creation process. Don't report an error so the execution can
2120 * continue normally. This will get flagged ENOTCONN and the
2121 * caller will handle it.
2125 delete_ust_app_session(-1, ua_sess
, app
);
2126 if (ret
!= -ENOMEM
) {
2128 * Tracer is probably gone or got an internal error so let's
2129 * behave like it will soon unregister or not usable.
2136 ua_sess
->handle
= ret
;
2138 /* Add ust app session to app's HT */
2139 lttng_ht_node_init_u64(&ua_sess
->node
,
2140 ua_sess
->tracing_id
);
2141 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2143 DBG2("UST app session created successfully with handle %d", ret
);
2146 *ua_sess_ptr
= ua_sess
;
2148 *is_created
= created
;
2151 /* Everything went well. */
2155 health_code_update();
2160 * Match function for a hash table lookup of ust_app_ctx.
2162 * It matches an ust app context based on the context type and, in the case
2163 * of perf counters, their name.
2165 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2167 struct ust_app_ctx
*ctx
;
2168 const struct lttng_ust_context
*key
;
2173 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2177 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2181 /* Check the name in the case of perf thread counters. */
2182 if (key
->ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
2183 if (strncmp(key
->u
.perf_counter
.name
,
2184 ctx
->ctx
.u
.perf_counter
.name
,
2185 sizeof(key
->u
.perf_counter
.name
))) {
2198 * Lookup for an ust app context from an lttng_ust_context.
2200 * Must be called while holding RCU read side lock.
2201 * Return an ust_app_ctx object or NULL on error.
2204 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2205 struct lttng_ust_context
*uctx
)
2207 struct lttng_ht_iter iter
;
2208 struct lttng_ht_node_ulong
*node
;
2209 struct ust_app_ctx
*app_ctx
= NULL
;
2214 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2215 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2216 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2217 node
= lttng_ht_iter_get_node_ulong(&iter
);
2222 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2229 * Create a context for the channel on the tracer.
2231 * Called with UST app session lock held and a RCU read side lock.
2234 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2235 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
2236 struct ust_app
*app
)
2239 struct ust_app_ctx
*ua_ctx
;
2241 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2243 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2249 ua_ctx
= alloc_ust_app_ctx(uctx
);
2250 if (ua_ctx
== NULL
) {
2256 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2257 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2258 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2260 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2270 * Enable on the tracer side a ust app event for the session and channel.
2272 * Called with UST app session lock held.
2275 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2276 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2280 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2285 ua_event
->enabled
= 1;
2292 * Disable on the tracer side a ust app event for the session and channel.
2294 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2295 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2299 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2304 ua_event
->enabled
= 0;
2311 * Lookup ust app channel for session and disable it on the tracer side.
2314 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2315 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2319 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2324 ua_chan
->enabled
= 0;
2331 * Lookup ust app channel for session and enable it on the tracer side. This
2332 * MUST be called with a RCU read side lock acquired.
2334 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2335 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2338 struct lttng_ht_iter iter
;
2339 struct lttng_ht_node_str
*ua_chan_node
;
2340 struct ust_app_channel
*ua_chan
;
2342 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2343 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2344 if (ua_chan_node
== NULL
) {
2345 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2346 uchan
->name
, ua_sess
->tracing_id
);
2350 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2352 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2362 * Ask the consumer to create a channel and get it if successful.
2364 * Return 0 on success or else a negative value.
2366 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2367 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2368 int bitness
, struct ust_registry_session
*registry
)
2371 unsigned int nb_fd
= 0;
2372 struct consumer_socket
*socket
;
2380 health_code_update();
2382 /* Get the right consumer socket for the application. */
2383 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2389 health_code_update();
2391 /* Need one fd for the channel. */
2392 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2394 ERR("Exhausted number of available FD upon create channel");
2399 * Ask consumer to create channel. The consumer will return the number of
2400 * stream we have to expect.
2402 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2409 * Compute the number of fd needed before receiving them. It must be 2 per
2410 * stream (2 being the default value here).
2412 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2414 /* Reserve the amount of file descriptor we need. */
2415 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2417 ERR("Exhausted number of available FD upon create channel");
2418 goto error_fd_get_stream
;
2421 health_code_update();
2424 * Now get the channel from the consumer. This call wil populate the stream
2425 * list of that channel and set the ust objects.
2427 if (usess
->consumer
->enabled
) {
2428 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2438 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2439 error_fd_get_stream
:
2441 * Initiate a destroy channel on the consumer since we had an error
2442 * handling it on our side. The return value is of no importance since we
2443 * already have a ret value set by the previous error that we need to
2446 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2448 lttng_fd_put(LTTNG_FD_APPS
, 1);
2450 health_code_update();
2456 * Duplicate the ust data object of the ust app stream and save it in the
2457 * buffer registry stream.
2459 * Return 0 on success or else a negative value.
2461 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2462 struct ust_app_stream
*stream
)
2469 /* Reserve the amount of file descriptor we need. */
2470 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2472 ERR("Exhausted number of available FD upon duplicate stream");
2476 /* Duplicate object for stream once the original is in the registry. */
2477 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2478 reg_stream
->obj
.ust
);
2480 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2481 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2482 lttng_fd_put(LTTNG_FD_APPS
, 2);
2485 stream
->handle
= stream
->obj
->handle
;
2492 * Duplicate the ust data object of the ust app. channel and save it in the
2493 * buffer registry channel.
2495 * Return 0 on success or else a negative value.
2497 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2498 struct ust_app_channel
*ua_chan
)
2505 /* Need two fds for the channel. */
2506 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2508 ERR("Exhausted number of available FD upon duplicate channel");
2512 /* Duplicate object for stream once the original is in the registry. */
2513 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2515 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2516 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2519 ua_chan
->handle
= ua_chan
->obj
->handle
;
2524 lttng_fd_put(LTTNG_FD_APPS
, 1);
2530 * For a given channel buffer registry, setup all streams of the given ust
2531 * application channel.
2533 * Return 0 on success or else a negative value.
2535 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2536 struct ust_app_channel
*ua_chan
,
2537 struct ust_app
*app
)
2540 struct ust_app_stream
*stream
, *stmp
;
2545 DBG2("UST app setup buffer registry stream");
2547 /* Send all streams to application. */
2548 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2549 struct buffer_reg_stream
*reg_stream
;
2551 ret
= buffer_reg_stream_create(®_stream
);
2557 * Keep original pointer and nullify it in the stream so the delete
2558 * stream call does not release the object.
2560 reg_stream
->obj
.ust
= stream
->obj
;
2562 buffer_reg_stream_add(reg_stream
, reg_chan
);
2564 /* We don't need the streams anymore. */
2565 cds_list_del(&stream
->list
);
2566 delete_ust_app_stream(-1, stream
, app
);
2574 * Create a buffer registry channel for the given session registry and
2575 * application channel object. If regp pointer is valid, it's set with the
2576 * created object. Important, the created object is NOT added to the session
2577 * registry hash table.
2579 * Return 0 on success else a negative value.
2581 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2582 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2585 struct buffer_reg_channel
*reg_chan
= NULL
;
2590 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2592 /* Create buffer registry channel. */
2593 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2598 reg_chan
->consumer_key
= ua_chan
->key
;
2599 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2600 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2602 /* Create and add a channel registry to session. */
2603 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2604 ua_chan
->tracing_channel_id
);
2608 buffer_reg_channel_add(reg_sess
, reg_chan
);
2617 /* Safe because the registry channel object was not added to any HT. */
2618 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2624 * Setup buffer registry channel for the given session registry and application
2625 * channel object. If regp pointer is valid, it's set with the created object.
2627 * Return 0 on success else a negative value.
2629 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2630 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2631 struct ust_app
*app
)
2638 assert(ua_chan
->obj
);
2640 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2642 /* Setup all streams for the registry. */
2643 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2648 reg_chan
->obj
.ust
= ua_chan
->obj
;
2649 ua_chan
->obj
= NULL
;
2654 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2655 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2660 * Send buffer registry channel to the application.
2662 * Return 0 on success else a negative value.
2664 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2665 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2666 struct ust_app_channel
*ua_chan
)
2669 struct buffer_reg_stream
*reg_stream
;
2676 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2678 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2683 /* Send channel to the application. */
2684 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2685 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2686 ret
= -ENOTCONN
; /* Caused by app exiting. */
2688 } else if (ret
< 0) {
2692 health_code_update();
2694 /* Send all streams to application. */
2695 pthread_mutex_lock(®_chan
->stream_list_lock
);
2696 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2697 struct ust_app_stream stream
;
2699 ret
= duplicate_stream_object(reg_stream
, &stream
);
2701 goto error_stream_unlock
;
2704 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2706 (void) release_ust_app_stream(-1, &stream
, app
);
2707 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2708 ret
= -ENOTCONN
; /* Caused by app exiting. */
2709 goto error_stream_unlock
;
2710 } else if (ret
< 0) {
2711 goto error_stream_unlock
;
2713 goto error_stream_unlock
;
2717 * The return value is not important here. This function will output an
2720 (void) release_ust_app_stream(-1, &stream
, app
);
2722 ua_chan
->is_sent
= 1;
2724 error_stream_unlock
:
2725 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2731 * Create and send to the application the created buffers with per UID buffers.
2733 * Return 0 on success else a negative value.
2735 static int create_channel_per_uid(struct ust_app
*app
,
2736 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2737 struct ust_app_channel
*ua_chan
)
2740 struct buffer_reg_uid
*reg_uid
;
2741 struct buffer_reg_channel
*reg_chan
;
2748 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2750 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2752 * The session creation handles the creation of this global registry
2753 * object. If none can be find, there is a code flow problem or a
2758 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2761 /* Create the buffer registry channel object. */
2762 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2764 ERR("Error creating the UST channel \"%s\" registry instance",
2771 * Create the buffers on the consumer side. This call populates the
2772 * ust app channel object with all streams and data object.
2774 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2775 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2777 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2781 * Let's remove the previously created buffer registry channel so
2782 * it's not visible anymore in the session registry.
2784 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2785 ua_chan
->tracing_channel_id
);
2786 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2787 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2792 * Setup the streams and add it to the session registry.
2794 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2795 ua_chan
, reg_chan
, app
);
2797 ERR("Error setting up UST channel \"%s\"",
2804 /* Send buffers to the application. */
2805 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2807 if (ret
!= -ENOTCONN
) {
2808 ERR("Error sending channel to application");
2818 * Create and send to the application the created buffers with per PID buffers.
2820 * Return 0 on success else a negative value.
2822 static int create_channel_per_pid(struct ust_app
*app
,
2823 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2824 struct ust_app_channel
*ua_chan
)
2827 struct ust_registry_session
*registry
;
2834 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2838 registry
= get_session_registry(ua_sess
);
2841 /* Create and add a new channel registry to session. */
2842 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2844 ERR("Error creating the UST channel \"%s\" registry instance",
2849 /* Create and get channel on the consumer side. */
2850 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2851 app
->bits_per_long
, registry
);
2853 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2858 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2860 if (ret
!= -ENOTCONN
) {
2861 ERR("Error sending channel to application");
2872 * From an already allocated ust app channel, create the channel buffers if
2873 * need and send it to the application. This MUST be called with a RCU read
2874 * side lock acquired.
2876 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2877 * the application exited concurrently.
2879 static int do_create_channel(struct ust_app
*app
,
2880 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2881 struct ust_app_channel
*ua_chan
)
2890 /* Handle buffer type before sending the channel to the application. */
2891 switch (usess
->buffer_type
) {
2892 case LTTNG_BUFFER_PER_UID
:
2894 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
2900 case LTTNG_BUFFER_PER_PID
:
2902 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
2914 /* Initialize ust objd object using the received handle and add it. */
2915 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
2916 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
2918 /* If channel is not enabled, disable it on the tracer */
2919 if (!ua_chan
->enabled
) {
2920 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2931 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2932 * newly created channel if not NULL.
2934 * Called with UST app session lock and RCU read-side lock held.
2936 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2937 * the application exited concurrently.
2939 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
2940 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
2941 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
2942 struct ust_app_channel
**ua_chanp
)
2945 struct lttng_ht_iter iter
;
2946 struct lttng_ht_node_str
*ua_chan_node
;
2947 struct ust_app_channel
*ua_chan
;
2949 /* Lookup channel in the ust app session */
2950 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2951 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2952 if (ua_chan_node
!= NULL
) {
2953 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2957 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
2958 if (ua_chan
== NULL
) {
2959 /* Only malloc can fail here */
2963 shadow_copy_channel(ua_chan
, uchan
);
2965 /* Set channel type. */
2966 ua_chan
->attr
.type
= type
;
2968 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
2973 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
2976 /* Only add the channel if successful on the tracer side. */
2977 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
2981 *ua_chanp
= ua_chan
;
2984 /* Everything went well. */
2988 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
2994 * Create UST app event and create it on the tracer side.
2996 * Called with ust app session mutex held.
2999 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3000 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3001 struct ust_app
*app
)
3004 struct ust_app_event
*ua_event
;
3006 /* Get event node */
3007 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3008 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3009 if (ua_event
!= NULL
) {
3014 /* Does not exist so create one */
3015 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3016 if (ua_event
== NULL
) {
3017 /* Only malloc can failed so something is really wrong */
3021 shadow_copy_event(ua_event
, uevent
);
3023 /* Create it on the tracer side */
3024 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3026 /* Not found previously means that it does not exist on the tracer */
3027 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
3031 add_unique_ust_app_event(ua_chan
, ua_event
);
3033 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3040 /* Valid. Calling here is already in a read side lock */
3041 delete_ust_app_event(-1, ua_event
, app
);
3046 * Create UST metadata and open it on the tracer side.
3048 * Called with UST app session lock held and RCU read side lock.
3050 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3051 struct ust_app
*app
, struct consumer_output
*consumer
)
3054 struct ust_app_channel
*metadata
;
3055 struct consumer_socket
*socket
;
3056 struct ust_registry_session
*registry
;
3062 registry
= get_session_registry(ua_sess
);
3065 pthread_mutex_lock(®istry
->lock
);
3067 /* Metadata already exists for this registry or it was closed previously */
3068 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3073 /* Allocate UST metadata */
3074 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3076 /* malloc() failed */
3081 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3083 /* Need one fd for the channel. */
3084 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3086 ERR("Exhausted number of available FD upon create metadata");
3090 /* Get the right consumer socket for the application. */
3091 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3094 goto error_consumer
;
3098 * Keep metadata key so we can identify it on the consumer side. Assign it
3099 * to the registry *before* we ask the consumer so we avoid the race of the
3100 * consumer requesting the metadata and the ask_channel call on our side
3101 * did not returned yet.
3103 registry
->metadata_key
= metadata
->key
;
3106 * Ask the metadata channel creation to the consumer. The metadata object
3107 * will be created by the consumer and kept their. However, the stream is
3108 * never added or monitored until we do a first push metadata to the
3111 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3114 /* Nullify the metadata key so we don't try to close it later on. */
3115 registry
->metadata_key
= 0;
3116 goto error_consumer
;
3120 * The setup command will make the metadata stream be sent to the relayd,
3121 * if applicable, and the thread managing the metadatas. This is important
3122 * because after this point, if an error occurs, the only way the stream
3123 * can be deleted is to be monitored in the consumer.
3125 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3127 /* Nullify the metadata key so we don't try to close it later on. */
3128 registry
->metadata_key
= 0;
3129 goto error_consumer
;
3132 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3133 metadata
->key
, app
->pid
);
3136 lttng_fd_put(LTTNG_FD_APPS
, 1);
3137 delete_ust_app_channel(-1, metadata
, app
);
3139 pthread_mutex_unlock(®istry
->lock
);
3144 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3145 * acquired before calling this function.
3147 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3149 struct ust_app
*app
= NULL
;
3150 struct lttng_ht_node_ulong
*node
;
3151 struct lttng_ht_iter iter
;
3153 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3154 node
= lttng_ht_iter_get_node_ulong(&iter
);
3156 DBG2("UST app no found with pid %d", pid
);
3160 DBG2("Found UST app by pid %d", pid
);
3162 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3169 * Allocate and init an UST app object using the registration information and
3170 * the command socket. This is called when the command socket connects to the
3173 * The object is returned on success or else NULL.
3175 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3177 struct ust_app
*lta
= NULL
;
3182 DBG3("UST app creating application for socket %d", sock
);
3184 if ((msg
->bits_per_long
== 64 &&
3185 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3186 || (msg
->bits_per_long
== 32 &&
3187 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3188 ERR("Registration failed: application \"%s\" (pid: %d) has "
3189 "%d-bit long, but no consumerd for this size is available.\n",
3190 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3194 lta
= zmalloc(sizeof(struct ust_app
));
3200 lta
->ppid
= msg
->ppid
;
3201 lta
->uid
= msg
->uid
;
3202 lta
->gid
= msg
->gid
;
3204 lta
->bits_per_long
= msg
->bits_per_long
;
3205 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3206 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3207 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3208 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3209 lta
->long_alignment
= msg
->long_alignment
;
3210 lta
->byte_order
= msg
->byte_order
;
3212 lta
->v_major
= msg
->major
;
3213 lta
->v_minor
= msg
->minor
;
3214 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3215 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3216 lta
->notify_sock
= -1;
3218 /* Copy name and make sure it's NULL terminated. */
3219 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3220 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3223 * Before this can be called, when receiving the registration information,
3224 * the application compatibility is checked. So, at this point, the
3225 * application can work with this session daemon.
3227 lta
->compatible
= 1;
3229 lta
->pid
= msg
->pid
;
3230 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3232 pthread_mutex_init(<a
->sock_lock
, NULL
);
3233 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3235 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3241 * For a given application object, add it to every hash table.
3243 void ust_app_add(struct ust_app
*app
)
3246 assert(app
->notify_sock
>= 0);
3251 * On a re-registration, we want to kick out the previous registration of
3254 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3257 * The socket _should_ be unique until _we_ call close. So, a add_unique
3258 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3259 * already in the table.
3261 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3263 /* Add application to the notify socket hash table. */
3264 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3265 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3267 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3268 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3269 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3276 * Set the application version into the object.
3278 * Return 0 on success else a negative value either an errno code or a
3279 * LTTng-UST error code.
3281 int ust_app_version(struct ust_app
*app
)
3287 pthread_mutex_lock(&app
->sock_lock
);
3288 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3289 pthread_mutex_unlock(&app
->sock_lock
);
3291 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3292 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3294 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3302 * Unregister app by removing it from the global traceable app list and freeing
3305 * The socket is already closed at this point so no close to sock.
3307 void ust_app_unregister(int sock
)
3309 struct ust_app
*lta
;
3310 struct lttng_ht_node_ulong
*node
;
3311 struct lttng_ht_iter ust_app_sock_iter
;
3312 struct lttng_ht_iter iter
;
3313 struct ust_app_session
*ua_sess
;
3318 /* Get the node reference for a call_rcu */
3319 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3320 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3323 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3324 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3327 * For per-PID buffers, perform "push metadata" and flush all
3328 * application streams before removing app from hash tables,
3329 * ensuring proper behavior of data_pending check.
3330 * Remove sessions so they are not visible during deletion.
3332 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3334 struct ust_registry_session
*registry
;
3336 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3338 /* The session was already removed so scheduled for teardown. */
3342 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3343 (void) ust_app_flush_app_session(lta
, ua_sess
);
3347 * Add session to list for teardown. This is safe since at this point we
3348 * are the only one using this list.
3350 pthread_mutex_lock(&ua_sess
->lock
);
3352 if (ua_sess
->deleted
) {
3353 pthread_mutex_unlock(&ua_sess
->lock
);
3358 * Normally, this is done in the delete session process which is
3359 * executed in the call rcu below. However, upon registration we can't
3360 * afford to wait for the grace period before pushing data or else the
3361 * data pending feature can race between the unregistration and stop
3362 * command where the data pending command is sent *before* the grace
3365 * The close metadata below nullifies the metadata pointer in the
3366 * session so the delete session will NOT push/close a second time.
3368 registry
= get_session_registry(ua_sess
);
3370 /* Push metadata for application before freeing the application. */
3371 (void) push_metadata(registry
, ua_sess
->consumer
);
3374 * Don't ask to close metadata for global per UID buffers. Close
3375 * metadata only on destroy trace session in this case. Also, the
3376 * previous push metadata could have flag the metadata registry to
3377 * close so don't send a close command if closed.
3379 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3380 /* And ask to close it for this session registry. */
3381 (void) close_metadata(registry
, ua_sess
->consumer
);
3384 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3386 pthread_mutex_unlock(&ua_sess
->lock
);
3389 /* Remove application from PID hash table */
3390 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3394 * Remove application from notify hash table. The thread handling the
3395 * notify socket could have deleted the node so ignore on error because
3396 * either way it's valid. The close of that socket is handled by the other
3399 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3400 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3403 * Ignore return value since the node might have been removed before by an
3404 * add replace during app registration because the PID can be reassigned by
3407 iter
.iter
.node
= <a
->pid_n
.node
;
3408 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3410 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3415 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3422 * Fill events array with all events name of all registered apps.
3424 int ust_app_list_events(struct lttng_event
**events
)
3427 size_t nbmem
, count
= 0;
3428 struct lttng_ht_iter iter
;
3429 struct ust_app
*app
;
3430 struct lttng_event
*tmp_event
;
3432 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3433 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3434 if (tmp_event
== NULL
) {
3435 PERROR("zmalloc ust app events");
3442 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3443 struct lttng_ust_tracepoint_iter uiter
;
3445 health_code_update();
3447 if (!app
->compatible
) {
3449 * TODO: In time, we should notice the caller of this error by
3450 * telling him that this is a version error.
3454 pthread_mutex_lock(&app
->sock_lock
);
3455 handle
= ustctl_tracepoint_list(app
->sock
);
3457 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3458 ERR("UST app list events getting handle failed for app pid %d",
3461 pthread_mutex_unlock(&app
->sock_lock
);
3465 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3466 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3467 /* Handle ustctl error. */
3471 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3472 ERR("UST app tp list get failed for app %d with ret %d",
3475 DBG3("UST app tp list get failed. Application is dead");
3477 * This is normal behavior, an application can die during the
3478 * creation process. Don't report an error so the execution can
3479 * continue normally. Continue normal execution.
3484 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3485 if (release_ret
!= -LTTNG_UST_ERR_EXITING
&& release_ret
!= -EPIPE
) {
3486 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3488 pthread_mutex_unlock(&app
->sock_lock
);
3492 health_code_update();
3493 if (count
>= nbmem
) {
3494 /* In case the realloc fails, we free the memory */
3495 struct lttng_event
*new_tmp_event
;
3498 new_nbmem
= nbmem
<< 1;
3499 DBG2("Reallocating event list from %zu to %zu entries",
3501 new_tmp_event
= realloc(tmp_event
,
3502 new_nbmem
* sizeof(struct lttng_event
));
3503 if (new_tmp_event
== NULL
) {
3506 PERROR("realloc ust app events");
3509 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3510 if (release_ret
!= -LTTNG_UST_ERR_EXITING
&& release_ret
!= -EPIPE
) {
3511 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3513 pthread_mutex_unlock(&app
->sock_lock
);
3516 /* Zero the new memory */
3517 memset(new_tmp_event
+ nbmem
, 0,
3518 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3520 tmp_event
= new_tmp_event
;
3522 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3523 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3524 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3525 tmp_event
[count
].pid
= app
->pid
;
3526 tmp_event
[count
].enabled
= -1;
3529 ret
= ustctl_release_handle(app
->sock
, handle
);
3530 pthread_mutex_unlock(&app
->sock_lock
);
3531 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3532 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3537 *events
= tmp_event
;
3539 DBG2("UST app list events done (%zu events)", count
);
3544 health_code_update();
3549 * Fill events array with all events name of all registered apps.
3551 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3554 size_t nbmem
, count
= 0;
3555 struct lttng_ht_iter iter
;
3556 struct ust_app
*app
;
3557 struct lttng_event_field
*tmp_event
;
3559 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3560 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3561 if (tmp_event
== NULL
) {
3562 PERROR("zmalloc ust app event fields");
3569 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3570 struct lttng_ust_field_iter uiter
;
3572 health_code_update();
3574 if (!app
->compatible
) {
3576 * TODO: In time, we should notice the caller of this error by
3577 * telling him that this is a version error.
3581 pthread_mutex_lock(&app
->sock_lock
);
3582 handle
= ustctl_tracepoint_field_list(app
->sock
);
3584 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3585 ERR("UST app list field getting handle failed for app pid %d",
3588 pthread_mutex_unlock(&app
->sock_lock
);
3592 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3593 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3594 /* Handle ustctl error. */
3598 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3599 ERR("UST app tp list field failed for app %d with ret %d",
3602 DBG3("UST app tp list field failed. Application is dead");
3604 * This is normal behavior, an application can die during the
3605 * creation process. Don't report an error so the execution can
3606 * continue normally. Reset list and count for next app.
3611 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3612 pthread_mutex_unlock(&app
->sock_lock
);
3613 if (release_ret
!= -LTTNG_UST_ERR_EXITING
&& release_ret
!= -EPIPE
) {
3614 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3619 health_code_update();
3620 if (count
>= nbmem
) {
3621 /* In case the realloc fails, we free the memory */
3622 struct lttng_event_field
*new_tmp_event
;
3625 new_nbmem
= nbmem
<< 1;
3626 DBG2("Reallocating event field list from %zu to %zu entries",
3628 new_tmp_event
= realloc(tmp_event
,
3629 new_nbmem
* sizeof(struct lttng_event_field
));
3630 if (new_tmp_event
== NULL
) {
3633 PERROR("realloc ust app event fields");
3636 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3637 pthread_mutex_unlock(&app
->sock_lock
);
3638 if (release_ret
!= -LTTNG_UST_ERR_EXITING
&& release_ret
!= -EPIPE
) {
3639 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3643 /* Zero the new memory */
3644 memset(new_tmp_event
+ nbmem
, 0,
3645 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3647 tmp_event
= new_tmp_event
;
3650 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3651 /* Mapping between these enums matches 1 to 1. */
3652 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3653 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3655 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3656 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3657 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3658 tmp_event
[count
].event
.pid
= app
->pid
;
3659 tmp_event
[count
].event
.enabled
= -1;
3662 ret
= ustctl_release_handle(app
->sock
, handle
);
3663 pthread_mutex_unlock(&app
->sock_lock
);
3664 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3665 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3670 *fields
= tmp_event
;
3672 DBG2("UST app list event fields done (%zu events)", count
);
3677 health_code_update();
3682 * Free and clean all traceable apps of the global list.
3684 * Should _NOT_ be called with RCU read-side lock held.
3686 void ust_app_clean_list(void)
3689 struct ust_app
*app
;
3690 struct lttng_ht_iter iter
;
3692 DBG2("UST app cleaning registered apps hash table");
3697 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3698 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3700 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3704 /* Cleanup socket hash table */
3705 if (ust_app_ht_by_sock
) {
3706 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3708 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3713 /* Cleanup notify socket hash table */
3714 if (ust_app_ht_by_notify_sock
) {
3715 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3716 notify_sock_n
.node
) {
3717 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3723 /* Destroy is done only when the ht is empty */
3725 ht_cleanup_push(ust_app_ht
);
3727 if (ust_app_ht_by_sock
) {
3728 ht_cleanup_push(ust_app_ht_by_sock
);
3730 if (ust_app_ht_by_notify_sock
) {
3731 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3736 * Init UST app hash table.
3738 int ust_app_ht_alloc(void)
3740 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3744 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3745 if (!ust_app_ht_by_sock
) {
3748 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3749 if (!ust_app_ht_by_notify_sock
) {
3756 * For a specific UST session, disable the channel for all registered apps.
3758 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3759 struct ltt_ust_channel
*uchan
)
3762 struct lttng_ht_iter iter
;
3763 struct lttng_ht_node_str
*ua_chan_node
;
3764 struct ust_app
*app
;
3765 struct ust_app_session
*ua_sess
;
3766 struct ust_app_channel
*ua_chan
;
3768 if (usess
== NULL
|| uchan
== NULL
) {
3769 ERR("Disabling UST global channel with NULL values");
3774 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3775 uchan
->name
, usess
->id
);
3779 /* For every registered applications */
3780 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3781 struct lttng_ht_iter uiter
;
3782 if (!app
->compatible
) {
3784 * TODO: In time, we should notice the caller of this error by
3785 * telling him that this is a version error.
3789 ua_sess
= lookup_session_by_app(usess
, app
);
3790 if (ua_sess
== NULL
) {
3795 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3796 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3797 /* If the session if found for the app, the channel must be there */
3798 assert(ua_chan_node
);
3800 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3801 /* The channel must not be already disabled */
3802 assert(ua_chan
->enabled
== 1);
3804 /* Disable channel onto application */
3805 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3807 /* XXX: We might want to report this error at some point... */
3819 * For a specific UST session, enable the channel for all registered apps.
3821 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3822 struct ltt_ust_channel
*uchan
)
3825 struct lttng_ht_iter iter
;
3826 struct ust_app
*app
;
3827 struct ust_app_session
*ua_sess
;
3829 if (usess
== NULL
|| uchan
== NULL
) {
3830 ERR("Adding UST global channel to NULL values");
3835 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3836 uchan
->name
, usess
->id
);
3840 /* For every registered applications */
3841 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3842 if (!app
->compatible
) {
3844 * TODO: In time, we should notice the caller of this error by
3845 * telling him that this is a version error.
3849 ua_sess
= lookup_session_by_app(usess
, app
);
3850 if (ua_sess
== NULL
) {
3854 /* Enable channel onto application */
3855 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3857 /* XXX: We might want to report this error at some point... */
3869 * Disable an event in a channel and for a specific session.
3871 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3872 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3875 struct lttng_ht_iter iter
, uiter
;
3876 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3877 struct ust_app
*app
;
3878 struct ust_app_session
*ua_sess
;
3879 struct ust_app_channel
*ua_chan
;
3880 struct ust_app_event
*ua_event
;
3882 DBG("UST app disabling event %s for all apps in channel "
3883 "%s for session id %" PRIu64
,
3884 uevent
->attr
.name
, uchan
->name
, usess
->id
);
3888 /* For all registered applications */
3889 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3890 if (!app
->compatible
) {
3892 * TODO: In time, we should notice the caller of this error by
3893 * telling him that this is a version error.
3897 ua_sess
= lookup_session_by_app(usess
, app
);
3898 if (ua_sess
== NULL
) {
3903 /* Lookup channel in the ust app session */
3904 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3905 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3906 if (ua_chan_node
== NULL
) {
3907 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
3908 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
3911 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3913 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
3914 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
3915 if (ua_event_node
== NULL
) {
3916 DBG2("Event %s not found in channel %s for app pid %d."
3917 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
3920 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3922 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3924 /* XXX: Report error someday... */
3935 * For a specific UST session, create the channel for all registered apps.
3937 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
3938 struct ltt_ust_channel
*uchan
)
3940 int ret
= 0, created
;
3941 struct lttng_ht_iter iter
;
3942 struct ust_app
*app
;
3943 struct ust_app_session
*ua_sess
= NULL
;
3945 /* Very wrong code flow */
3949 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
3950 uchan
->name
, usess
->id
);
3954 /* For every registered applications */
3955 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3956 if (!app
->compatible
) {
3958 * TODO: In time, we should notice the caller of this error by
3959 * telling him that this is a version error.
3963 if (!trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
3969 * Create session on the tracer side and add it to app session HT. Note
3970 * that if session exist, it will simply return a pointer to the ust
3973 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
3978 * The application's socket is not valid. Either a bad socket
3979 * or a timeout on it. We can't inform the caller that for a
3980 * specific app, the session failed so lets continue here.
3982 ret
= 0; /* Not an error. */
3986 goto error_rcu_unlock
;
3991 pthread_mutex_lock(&ua_sess
->lock
);
3993 if (ua_sess
->deleted
) {
3994 pthread_mutex_unlock(&ua_sess
->lock
);
3998 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
3999 sizeof(uchan
->name
))) {
4000 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
4003 /* Create channel onto application. We don't need the chan ref. */
4004 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
4005 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
4007 pthread_mutex_unlock(&ua_sess
->lock
);
4009 /* Cleanup the created session if it's the case. */
4011 destroy_app_session(app
, ua_sess
);
4016 * The application's socket is not valid. Either a bad socket
4017 * or a timeout on it. We can't inform the caller that for a
4018 * specific app, the session failed so lets continue here.
4020 ret
= 0; /* Not an error. */
4024 goto error_rcu_unlock
;
4035 * Enable event for a specific session and channel on the tracer.
4037 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4038 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4041 struct lttng_ht_iter iter
, uiter
;
4042 struct lttng_ht_node_str
*ua_chan_node
;
4043 struct ust_app
*app
;
4044 struct ust_app_session
*ua_sess
;
4045 struct ust_app_channel
*ua_chan
;
4046 struct ust_app_event
*ua_event
;
4048 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4049 uevent
->attr
.name
, usess
->id
);
4052 * NOTE: At this point, this function is called only if the session and
4053 * channel passed are already created for all apps. and enabled on the
4059 /* For all registered applications */
4060 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4061 if (!app
->compatible
) {
4063 * TODO: In time, we should notice the caller of this error by
4064 * telling him that this is a version error.
4068 ua_sess
= lookup_session_by_app(usess
, app
);
4070 /* The application has problem or is probably dead. */
4074 pthread_mutex_lock(&ua_sess
->lock
);
4076 if (ua_sess
->deleted
) {
4077 pthread_mutex_unlock(&ua_sess
->lock
);
4081 /* Lookup channel in the ust app session */
4082 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4083 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4085 * It is possible that the channel cannot be found is
4086 * the channel/event creation occurs concurrently with
4087 * an application exit.
4089 if (!ua_chan_node
) {
4090 pthread_mutex_unlock(&ua_sess
->lock
);
4094 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4096 /* Get event node */
4097 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4098 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4099 if (ua_event
== NULL
) {
4100 DBG3("UST app enable event %s not found for app PID %d."
4101 "Skipping app", uevent
->attr
.name
, app
->pid
);
4105 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4107 pthread_mutex_unlock(&ua_sess
->lock
);
4111 pthread_mutex_unlock(&ua_sess
->lock
);
4120 * For a specific existing UST session and UST channel, creates the event for
4121 * all registered apps.
4123 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4124 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4127 struct lttng_ht_iter iter
, uiter
;
4128 struct lttng_ht_node_str
*ua_chan_node
;
4129 struct ust_app
*app
;
4130 struct ust_app_session
*ua_sess
;
4131 struct ust_app_channel
*ua_chan
;
4133 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4134 uevent
->attr
.name
, usess
->id
);
4138 /* For all registered applications */
4139 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4140 if (!app
->compatible
) {
4142 * TODO: In time, we should notice the caller of this error by
4143 * telling him that this is a version error.
4147 ua_sess
= lookup_session_by_app(usess
, app
);
4149 /* The application has problem or is probably dead. */
4153 pthread_mutex_lock(&ua_sess
->lock
);
4155 if (ua_sess
->deleted
) {
4156 pthread_mutex_unlock(&ua_sess
->lock
);
4160 /* Lookup channel in the ust app session */
4161 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4162 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4163 /* If the channel is not found, there is a code flow error */
4164 assert(ua_chan_node
);
4166 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4168 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4169 pthread_mutex_unlock(&ua_sess
->lock
);
4171 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4172 /* Possible value at this point: -ENOMEM. If so, we stop! */
4175 DBG2("UST app event %s already exist on app PID %d",
4176 uevent
->attr
.name
, app
->pid
);
4187 * Start tracing for a specific UST session and app.
4190 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4193 struct ust_app_session
*ua_sess
;
4195 DBG("Starting tracing for ust app pid %d", app
->pid
);
4199 if (!app
->compatible
) {
4203 ua_sess
= lookup_session_by_app(usess
, app
);
4204 if (ua_sess
== NULL
) {
4205 /* The session is in teardown process. Ignore and continue. */
4209 pthread_mutex_lock(&ua_sess
->lock
);
4211 if (ua_sess
->deleted
) {
4212 pthread_mutex_unlock(&ua_sess
->lock
);
4216 /* Upon restart, we skip the setup, already done */
4217 if (ua_sess
->started
) {
4221 /* Create directories if consumer is LOCAL and has a path defined. */
4222 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
4223 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
4224 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
4225 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
4227 if (errno
!= EEXIST
) {
4228 ERR("Trace directory creation error");
4235 * Create the metadata for the application. This returns gracefully if a
4236 * metadata was already set for the session.
4238 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4243 health_code_update();
4246 /* This start the UST tracing */
4247 pthread_mutex_lock(&app
->sock_lock
);
4248 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4249 pthread_mutex_unlock(&app
->sock_lock
);
4251 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4252 ERR("Error starting tracing for app pid: %d (ret: %d)",
4255 DBG("UST app start session failed. Application is dead.");
4257 * This is normal behavior, an application can die during the
4258 * creation process. Don't report an error so the execution can
4259 * continue normally.
4261 pthread_mutex_unlock(&ua_sess
->lock
);
4267 /* Indicate that the session has been started once */
4268 ua_sess
->started
= 1;
4270 pthread_mutex_unlock(&ua_sess
->lock
);
4272 health_code_update();
4274 /* Quiescent wait after starting trace */
4275 pthread_mutex_lock(&app
->sock_lock
);
4276 ret
= ustctl_wait_quiescent(app
->sock
);
4277 pthread_mutex_unlock(&app
->sock_lock
);
4278 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4279 ERR("UST app wait quiescent failed for app pid %d ret %d",
4285 health_code_update();
4289 pthread_mutex_unlock(&ua_sess
->lock
);
4291 health_code_update();
4296 * Stop tracing for a specific UST session and app.
4299 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4302 struct ust_app_session
*ua_sess
;
4303 struct ust_registry_session
*registry
;
4305 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4309 if (!app
->compatible
) {
4310 goto end_no_session
;
4313 ua_sess
= lookup_session_by_app(usess
, app
);
4314 if (ua_sess
== NULL
) {
4315 goto end_no_session
;
4318 pthread_mutex_lock(&ua_sess
->lock
);
4320 if (ua_sess
->deleted
) {
4321 pthread_mutex_unlock(&ua_sess
->lock
);
4322 goto end_no_session
;
4326 * If started = 0, it means that stop trace has been called for a session
4327 * that was never started. It's possible since we can have a fail start
4328 * from either the application manager thread or the command thread. Simply
4329 * indicate that this is a stop error.
4331 if (!ua_sess
->started
) {
4332 goto error_rcu_unlock
;
4335 health_code_update();
4337 /* This inhibits UST tracing */
4338 pthread_mutex_lock(&app
->sock_lock
);
4339 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4340 pthread_mutex_unlock(&app
->sock_lock
);
4342 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4343 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4346 DBG("UST app stop session failed. Application is dead.");
4348 * This is normal behavior, an application can die during the
4349 * creation process. Don't report an error so the execution can
4350 * continue normally.
4354 goto error_rcu_unlock
;
4357 health_code_update();
4359 /* Quiescent wait after stopping trace */
4360 pthread_mutex_lock(&app
->sock_lock
);
4361 ret
= ustctl_wait_quiescent(app
->sock
);
4362 pthread_mutex_unlock(&app
->sock_lock
);
4363 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4364 ERR("UST app wait quiescent failed for app pid %d ret %d",
4368 health_code_update();
4370 registry
= get_session_registry(ua_sess
);
4373 /* Push metadata for application before freeing the application. */
4374 (void) push_metadata(registry
, ua_sess
->consumer
);
4377 pthread_mutex_unlock(&ua_sess
->lock
);
4380 health_code_update();
4384 pthread_mutex_unlock(&ua_sess
->lock
);
4386 health_code_update();
4391 int ust_app_flush_app_session(struct ust_app
*app
,
4392 struct ust_app_session
*ua_sess
)
4394 int ret
, retval
= 0;
4395 struct lttng_ht_iter iter
;
4396 struct ust_app_channel
*ua_chan
;
4397 struct consumer_socket
*socket
;
4399 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4403 if (!app
->compatible
) {
4404 goto end_not_compatible
;
4407 pthread_mutex_lock(&ua_sess
->lock
);
4409 if (ua_sess
->deleted
) {
4413 health_code_update();
4415 /* Flushing buffers */
4416 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4419 /* Flush buffers and push metadata. */
4420 switch (ua_sess
->buffer_type
) {
4421 case LTTNG_BUFFER_PER_PID
:
4422 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4424 health_code_update();
4425 assert(ua_chan
->is_sent
);
4426 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4428 ERR("Error flushing consumer channel");
4434 case LTTNG_BUFFER_PER_UID
:
4440 health_code_update();
4443 pthread_mutex_unlock(&ua_sess
->lock
);
4447 health_code_update();
4452 * Flush buffers for all applications for a specific UST session.
4453 * Called with UST session lock held.
4456 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4461 DBG("Flushing session buffers for all ust apps");
4465 /* Flush buffers and push metadata. */
4466 switch (usess
->buffer_type
) {
4467 case LTTNG_BUFFER_PER_UID
:
4469 struct buffer_reg_uid
*reg
;
4470 struct lttng_ht_iter iter
;
4472 /* Flush all per UID buffers associated to that session. */
4473 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4474 struct ust_registry_session
*ust_session_reg
;
4475 struct buffer_reg_channel
*reg_chan
;
4476 struct consumer_socket
*socket
;
4478 /* Get consumer socket to use to push the metadata.*/
4479 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4482 /* Ignore request if no consumer is found for the session. */
4486 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4487 reg_chan
, node
.node
) {
4489 * The following call will print error values so the return
4490 * code is of little importance because whatever happens, we
4491 * have to try them all.
4493 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4496 ust_session_reg
= reg
->registry
->reg
.ust
;
4497 /* Push metadata. */
4498 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4502 case LTTNG_BUFFER_PER_PID
:
4504 struct ust_app_session
*ua_sess
;
4505 struct lttng_ht_iter iter
;
4506 struct ust_app
*app
;
4508 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4509 ua_sess
= lookup_session_by_app(usess
, app
);
4510 if (ua_sess
== NULL
) {
4513 (void) ust_app_flush_app_session(app
, ua_sess
);
4524 health_code_update();
4529 * Destroy a specific UST session in apps.
4531 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4534 struct ust_app_session
*ua_sess
;
4535 struct lttng_ht_iter iter
;
4536 struct lttng_ht_node_u64
*node
;
4538 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4542 if (!app
->compatible
) {
4546 __lookup_session_by_app(usess
, app
, &iter
);
4547 node
= lttng_ht_iter_get_node_u64(&iter
);
4549 /* Session is being or is deleted. */
4552 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4554 health_code_update();
4555 destroy_app_session(app
, ua_sess
);
4557 health_code_update();
4559 /* Quiescent wait after stopping trace */
4560 pthread_mutex_lock(&app
->sock_lock
);
4561 ret
= ustctl_wait_quiescent(app
->sock
);
4562 pthread_mutex_unlock(&app
->sock_lock
);
4563 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4564 ERR("UST app wait quiescent failed for app pid %d ret %d",
4569 health_code_update();
4574 * Start tracing for the UST session.
4576 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4579 struct lttng_ht_iter iter
;
4580 struct ust_app
*app
;
4582 DBG("Starting all UST traces");
4586 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4587 ret
= ust_app_start_trace(usess
, app
);
4589 /* Continue to next apps even on error */
4600 * Start tracing for the UST session.
4601 * Called with UST session lock held.
4603 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4606 struct lttng_ht_iter iter
;
4607 struct ust_app
*app
;
4609 DBG("Stopping all UST traces");
4613 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4614 ret
= ust_app_stop_trace(usess
, app
);
4616 /* Continue to next apps even on error */
4621 (void) ust_app_flush_session(usess
);
4629 * Destroy app UST session.
4631 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4634 struct lttng_ht_iter iter
;
4635 struct ust_app
*app
;
4637 DBG("Destroy all UST traces");
4641 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4642 ret
= destroy_trace(usess
, app
);
4644 /* Continue to next apps even on error */
4655 void ust_app_global_create(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4658 struct lttng_ht_iter iter
, uiter
;
4659 struct ust_app_session
*ua_sess
= NULL
;
4660 struct ust_app_channel
*ua_chan
;
4661 struct ust_app_event
*ua_event
;
4662 struct ust_app_ctx
*ua_ctx
;
4665 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &is_created
);
4667 /* Tracer is probably gone or ENOMEM. */
4671 /* App session already created. */
4676 pthread_mutex_lock(&ua_sess
->lock
);
4678 if (ua_sess
->deleted
) {
4679 pthread_mutex_unlock(&ua_sess
->lock
);
4684 * We can iterate safely here over all UST app session since the create ust
4685 * app session above made a shadow copy of the UST global domain from the
4688 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4690 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4691 if (ret
< 0 && ret
!= -ENOTCONN
) {
4693 * Stop everything. On error, the application
4694 * failed, no more file descriptor are available
4695 * or ENOMEM so stopping here is the only thing
4696 * we can do for now. The only exception is
4697 * -ENOTCONN, which indicates that the application
4704 * Add context using the list so they are enabled in the same order the
4707 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
4708 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
4715 /* For each events */
4716 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
4718 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
4725 pthread_mutex_unlock(&ua_sess
->lock
);
4727 if (usess
->active
) {
4728 ret
= ust_app_start_trace(usess
, app
);
4733 DBG2("UST trace started for app pid %d", app
->pid
);
4736 /* Everything went well at this point. */
4740 pthread_mutex_unlock(&ua_sess
->lock
);
4743 destroy_app_session(app
, ua_sess
);
4749 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4751 struct ust_app_session
*ua_sess
;
4753 ua_sess
= lookup_session_by_app(usess
, app
);
4754 if (ua_sess
== NULL
) {
4757 destroy_app_session(app
, ua_sess
);
4761 * Add channels/events from UST global domain to registered apps at sock.
4763 * Called with session lock held.
4764 * Called with RCU read-side lock held.
4766 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4770 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
4771 app
->sock
, usess
->id
);
4773 if (!app
->compatible
) {
4777 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4778 ust_app_global_create(usess
, app
);
4780 ust_app_global_destroy(usess
, app
);
4785 * Called with session lock held.
4787 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
4789 struct lttng_ht_iter iter
;
4790 struct ust_app
*app
;
4793 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4794 ust_app_global_update(usess
, app
);
4800 * Add context to a specific channel for global UST domain.
4802 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
4803 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
4806 struct lttng_ht_node_str
*ua_chan_node
;
4807 struct lttng_ht_iter iter
, uiter
;
4808 struct ust_app_channel
*ua_chan
= NULL
;
4809 struct ust_app_session
*ua_sess
;
4810 struct ust_app
*app
;
4814 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4815 if (!app
->compatible
) {
4817 * TODO: In time, we should notice the caller of this error by
4818 * telling him that this is a version error.
4822 ua_sess
= lookup_session_by_app(usess
, app
);
4823 if (ua_sess
== NULL
) {
4827 pthread_mutex_lock(&ua_sess
->lock
);
4829 if (ua_sess
->deleted
) {
4830 pthread_mutex_unlock(&ua_sess
->lock
);
4834 /* Lookup channel in the ust app session */
4835 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4836 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4837 if (ua_chan_node
== NULL
) {
4840 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4842 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4847 pthread_mutex_unlock(&ua_sess
->lock
);
4855 * Enable event for a channel from a UST session for a specific PID.
4857 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4858 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4861 struct lttng_ht_iter iter
;
4862 struct lttng_ht_node_str
*ua_chan_node
;
4863 struct ust_app
*app
;
4864 struct ust_app_session
*ua_sess
;
4865 struct ust_app_channel
*ua_chan
;
4866 struct ust_app_event
*ua_event
;
4868 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4872 app
= ust_app_find_by_pid(pid
);
4874 ERR("UST app enable event per PID %d not found", pid
);
4879 if (!app
->compatible
) {
4884 ua_sess
= lookup_session_by_app(usess
, app
);
4886 /* The application has problem or is probably dead. */
4891 pthread_mutex_lock(&ua_sess
->lock
);
4893 if (ua_sess
->deleted
) {
4898 /* Lookup channel in the ust app session */
4899 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4900 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4901 /* If the channel is not found, there is a code flow error */
4902 assert(ua_chan_node
);
4904 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4906 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4907 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4908 if (ua_event
== NULL
) {
4909 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4914 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4921 pthread_mutex_unlock(&ua_sess
->lock
);
4928 * Calibrate registered applications.
4930 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
4933 struct lttng_ht_iter iter
;
4934 struct ust_app
*app
;
4938 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4939 if (!app
->compatible
) {
4941 * TODO: In time, we should notice the caller of this error by
4942 * telling him that this is a version error.
4947 health_code_update();
4949 pthread_mutex_lock(&app
->sock_lock
);
4950 ret
= ustctl_calibrate(app
->sock
, calibrate
);
4951 pthread_mutex_unlock(&app
->sock_lock
);
4955 /* Means that it's not implemented on the tracer side. */
4959 DBG2("Calibrate app PID %d returned with error %d",
4966 DBG("UST app global domain calibration finished");
4970 health_code_update();
4976 * Receive registration and populate the given msg structure.
4978 * On success return 0 else a negative value returned by the ustctl call.
4980 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
4983 uint32_t pid
, ppid
, uid
, gid
;
4987 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
4988 &pid
, &ppid
, &uid
, &gid
,
4989 &msg
->bits_per_long
,
4990 &msg
->uint8_t_alignment
,
4991 &msg
->uint16_t_alignment
,
4992 &msg
->uint32_t_alignment
,
4993 &msg
->uint64_t_alignment
,
4994 &msg
->long_alignment
,
5001 case LTTNG_UST_ERR_EXITING
:
5002 DBG3("UST app recv reg message failed. Application died");
5004 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5005 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5006 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5007 LTTNG_UST_ABI_MINOR_VERSION
);
5010 ERR("UST app recv reg message failed with ret %d", ret
);
5015 msg
->pid
= (pid_t
) pid
;
5016 msg
->ppid
= (pid_t
) ppid
;
5017 msg
->uid
= (uid_t
) uid
;
5018 msg
->gid
= (gid_t
) gid
;
5025 * Return a ust app channel object using the application object and the channel
5026 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5027 * lock MUST be acquired before calling this function.
5029 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5032 struct lttng_ht_node_ulong
*node
;
5033 struct lttng_ht_iter iter
;
5034 struct ust_app_channel
*ua_chan
= NULL
;
5038 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5039 node
= lttng_ht_iter_get_node_ulong(&iter
);
5041 DBG2("UST app channel find by objd %d not found", objd
);
5045 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5052 * Reply to a register channel notification from an application on the notify
5053 * socket. The channel metadata is also created.
5055 * The session UST registry lock is acquired in this function.
5057 * On success 0 is returned else a negative value.
5059 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
5060 size_t nr_fields
, struct ustctl_field
*fields
)
5062 int ret
, ret_code
= 0;
5063 uint32_t chan_id
, reg_count
;
5064 uint64_t chan_reg_key
;
5065 enum ustctl_channel_header type
;
5066 struct ust_app
*app
;
5067 struct ust_app_channel
*ua_chan
;
5068 struct ust_app_session
*ua_sess
;
5069 struct ust_registry_session
*registry
;
5070 struct ust_registry_channel
*chan_reg
;
5074 /* Lookup application. If not found, there is a code flow error. */
5075 app
= find_app_by_notify_sock(sock
);
5077 DBG("Application socket %d is being teardown. Abort event notify",
5081 goto error_rcu_unlock
;
5084 /* Lookup channel by UST object descriptor. */
5085 ua_chan
= find_channel_by_objd(app
, cobjd
);
5087 DBG("Application channel is being teardown. Abort event notify");
5090 goto error_rcu_unlock
;
5093 assert(ua_chan
->session
);
5094 ua_sess
= ua_chan
->session
;
5096 /* Get right session registry depending on the session buffer type. */
5097 registry
= get_session_registry(ua_sess
);
5100 /* Depending on the buffer type, a different channel key is used. */
5101 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5102 chan_reg_key
= ua_chan
->tracing_channel_id
;
5104 chan_reg_key
= ua_chan
->key
;
5107 pthread_mutex_lock(®istry
->lock
);
5109 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5112 if (!chan_reg
->register_done
) {
5113 reg_count
= ust_registry_get_event_count(chan_reg
);
5114 if (reg_count
< 31) {
5115 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
5117 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5120 chan_reg
->nr_ctx_fields
= nr_fields
;
5121 chan_reg
->ctx_fields
= fields
;
5122 chan_reg
->header_type
= type
;
5124 /* Get current already assigned values. */
5125 type
= chan_reg
->header_type
;
5127 /* Set to NULL so the error path does not do a double free. */
5130 /* Channel id is set during the object creation. */
5131 chan_id
= chan_reg
->chan_id
;
5133 /* Append to metadata */
5134 if (!chan_reg
->metadata_dumped
) {
5135 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5137 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5143 DBG3("UST app replying to register channel key %" PRIu64
5144 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5147 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5149 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5150 ERR("UST app reply channel failed with ret %d", ret
);
5152 DBG3("UST app reply channel failed. Application died");
5157 /* This channel registry registration is completed. */
5158 chan_reg
->register_done
= 1;
5161 pthread_mutex_unlock(®istry
->lock
);
5171 * Add event to the UST channel registry. When the event is added to the
5172 * registry, the metadata is also created. Once done, this replies to the
5173 * application with the appropriate error code.
5175 * The session UST registry lock is acquired in the function.
5177 * On success 0 is returned else a negative value.
5179 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5180 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5181 int loglevel_value
, char *model_emf_uri
)
5184 uint32_t event_id
= 0;
5185 uint64_t chan_reg_key
;
5186 struct ust_app
*app
;
5187 struct ust_app_channel
*ua_chan
;
5188 struct ust_app_session
*ua_sess
;
5189 struct ust_registry_session
*registry
;
5193 /* Lookup application. If not found, there is a code flow error. */
5194 app
= find_app_by_notify_sock(sock
);
5196 DBG("Application socket %d is being teardown. Abort event notify",
5201 free(model_emf_uri
);
5202 goto error_rcu_unlock
;
5205 /* Lookup channel by UST object descriptor. */
5206 ua_chan
= find_channel_by_objd(app
, cobjd
);
5208 DBG("Application channel is being teardown. Abort event notify");
5212 free(model_emf_uri
);
5213 goto error_rcu_unlock
;
5216 assert(ua_chan
->session
);
5217 ua_sess
= ua_chan
->session
;
5219 registry
= get_session_registry(ua_sess
);
5222 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5223 chan_reg_key
= ua_chan
->tracing_channel_id
;
5225 chan_reg_key
= ua_chan
->key
;
5228 pthread_mutex_lock(®istry
->lock
);
5231 * From this point on, this call acquires the ownership of the sig, fields
5232 * and model_emf_uri meaning any free are done inside it if needed. These
5233 * three variables MUST NOT be read/write after this.
5235 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5236 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5237 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5241 * The return value is returned to ustctl so in case of an error, the
5242 * application can be notified. In case of an error, it's important not to
5243 * return a negative error or else the application will get closed.
5245 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5247 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5248 ERR("UST app reply event failed with ret %d", ret
);
5250 DBG3("UST app reply event failed. Application died");
5253 * No need to wipe the create event since the application socket will
5254 * get close on error hence cleaning up everything by itself.
5259 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5263 pthread_mutex_unlock(®istry
->lock
);
5270 * Handle application notification through the given notify socket.
5272 * Return 0 on success or else a negative value.
5274 int ust_app_recv_notify(int sock
)
5277 enum ustctl_notify_cmd cmd
;
5279 DBG3("UST app receiving notify from sock %d", sock
);
5281 ret
= ustctl_recv_notify(sock
, &cmd
);
5283 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5284 ERR("UST app recv notify failed with ret %d", ret
);
5286 DBG3("UST app recv notify failed. Application died");
5292 case USTCTL_NOTIFY_CMD_EVENT
:
5294 int sobjd
, cobjd
, loglevel_value
;
5295 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5297 struct ustctl_field
*fields
;
5299 DBG2("UST app ustctl register event received");
5301 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5302 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5305 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5306 ERR("UST app recv event failed with ret %d", ret
);
5308 DBG3("UST app recv event failed. Application died");
5314 * Add event to the UST registry coming from the notify socket. This
5315 * call will free if needed the sig, fields and model_emf_uri. This
5316 * code path loses the ownsership of these variables and transfer them
5317 * to the this function.
5319 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5320 fields
, loglevel_value
, model_emf_uri
);
5327 case USTCTL_NOTIFY_CMD_CHANNEL
:
5331 struct ustctl_field
*fields
;
5333 DBG2("UST app ustctl register channel received");
5335 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5338 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5339 ERR("UST app recv channel failed with ret %d", ret
);
5341 DBG3("UST app recv channel failed. Application died");
5347 * The fields ownership are transfered to this function call meaning
5348 * that if needed it will be freed. After this, it's invalid to access
5349 * fields or clean it up.
5351 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
5360 /* Should NEVER happen. */
5369 * Once the notify socket hangs up, this is called. First, it tries to find the
5370 * corresponding application. On failure, the call_rcu to close the socket is
5371 * executed. If an application is found, it tries to delete it from the notify
5372 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5374 * Note that an object needs to be allocated here so on ENOMEM failure, the
5375 * call RCU is not done but the rest of the cleanup is.
5377 void ust_app_notify_sock_unregister(int sock
)
5380 struct lttng_ht_iter iter
;
5381 struct ust_app
*app
;
5382 struct ust_app_notify_sock_obj
*obj
;
5388 obj
= zmalloc(sizeof(*obj
));
5391 * An ENOMEM is kind of uncool. If this strikes we continue the
5392 * procedure but the call_rcu will not be called. In this case, we
5393 * accept the fd leak rather than possibly creating an unsynchronized
5394 * state between threads.
5396 * TODO: The notify object should be created once the notify socket is
5397 * registered and stored independantely from the ust app object. The
5398 * tricky part is to synchronize the teardown of the application and
5399 * this notify object. Let's keep that in mind so we can avoid this
5400 * kind of shenanigans with ENOMEM in the teardown path.
5407 DBG("UST app notify socket unregister %d", sock
);
5410 * Lookup application by notify socket. If this fails, this means that the
5411 * hash table delete has already been done by the application
5412 * unregistration process so we can safely close the notify socket in a
5415 app
= find_app_by_notify_sock(sock
);
5420 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5423 * Whatever happens here either we fail or succeed, in both cases we have
5424 * to close the socket after a grace period to continue to the call RCU
5425 * here. If the deletion is successful, the application is not visible
5426 * anymore by other threads and is it fails it means that it was already
5427 * deleted from the hash table so either way we just have to close the
5430 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5436 * Close socket after a grace period to avoid for the socket to be reused
5437 * before the application object is freed creating potential race between
5438 * threads trying to add unique in the global hash table.
5441 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5446 * Destroy a ust app data structure and free its memory.
5448 void ust_app_destroy(struct ust_app
*app
)
5454 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5458 * Take a snapshot for a given UST session. The snapshot is sent to the given
5461 * Return 0 on success or else a negative value.
5463 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5464 struct snapshot_output
*output
, int wait
,
5465 uint64_t nb_packets_per_stream
)
5468 unsigned int snapshot_done
= 0;
5469 struct lttng_ht_iter iter
;
5470 struct ust_app
*app
;
5471 char pathname
[PATH_MAX
];
5478 switch (usess
->buffer_type
) {
5479 case LTTNG_BUFFER_PER_UID
:
5481 struct buffer_reg_uid
*reg
;
5483 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5484 struct buffer_reg_channel
*reg_chan
;
5485 struct consumer_socket
*socket
;
5487 /* Get consumer socket to use to push the metadata.*/
5488 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5495 memset(pathname
, 0, sizeof(pathname
));
5496 ret
= snprintf(pathname
, sizeof(pathname
),
5497 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5498 reg
->uid
, reg
->bits_per_long
);
5500 PERROR("snprintf snapshot path");
5504 /* Add the UST default trace dir to path. */
5505 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5506 reg_chan
, node
.node
) {
5507 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5508 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5509 nb_packets_per_stream
);
5514 ret
= consumer_snapshot_channel(socket
,
5515 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5516 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
5524 case LTTNG_BUFFER_PER_PID
:
5526 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5527 struct consumer_socket
*socket
;
5528 struct lttng_ht_iter chan_iter
;
5529 struct ust_app_channel
*ua_chan
;
5530 struct ust_app_session
*ua_sess
;
5531 struct ust_registry_session
*registry
;
5533 ua_sess
= lookup_session_by_app(usess
, app
);
5535 /* Session not associated with this app. */
5539 /* Get the right consumer socket for the application. */
5540 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5547 /* Add the UST default trace dir to path. */
5548 memset(pathname
, 0, sizeof(pathname
));
5549 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5552 PERROR("snprintf snapshot path");
5556 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5557 ua_chan
, node
.node
) {
5558 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
5559 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
5560 nb_packets_per_stream
);
5566 registry
= get_session_registry(ua_sess
);
5568 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
5569 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
5582 if (!snapshot_done
) {
5584 * If no snapshot was made and we are not in the error path, this means
5585 * that there are no buffers thus no (prior) application to snapshot
5586 * data from so we have simply NO data.
5597 * Return the size taken by one more packet per stream.
5599 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
5600 uint64_t cur_nr_packets
)
5602 uint64_t tot_size
= 0;
5603 struct ust_app
*app
;
5604 struct lttng_ht_iter iter
;
5608 switch (usess
->buffer_type
) {
5609 case LTTNG_BUFFER_PER_UID
:
5611 struct buffer_reg_uid
*reg
;
5613 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5614 struct buffer_reg_channel
*reg_chan
;
5617 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5618 reg_chan
, node
.node
) {
5619 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
5621 * Don't take channel into account if we
5622 * already grab all its packets.
5626 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
5632 case LTTNG_BUFFER_PER_PID
:
5635 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5636 struct ust_app_channel
*ua_chan
;
5637 struct ust_app_session
*ua_sess
;
5638 struct lttng_ht_iter chan_iter
;
5640 ua_sess
= lookup_session_by_app(usess
, app
);
5642 /* Session not associated with this app. */
5646 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5647 ua_chan
, node
.node
) {
5648 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
5650 * Don't take channel into account if we
5651 * already grab all its packets.
5655 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;