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
;
108 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
111 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
114 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
118 /* Event loglevel. */
119 if (event
->attr
.loglevel
!= key
->loglevel
) {
120 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
121 && key
->loglevel
== 0 && event
->attr
.loglevel
== -1) {
123 * Match is accepted. This is because on event creation, the
124 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
125 * -1 are accepted for this loglevel type since 0 is the one set by
126 * the API when receiving an enable event.
133 /* One of the filters is NULL, fail. */
134 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
138 if (key
->filter
&& event
->filter
) {
139 /* Both filters exists, check length followed by the bytecode. */
140 if (event
->filter
->len
!= key
->filter
->len
||
141 memcmp(event
->filter
->data
, key
->filter
->data
,
142 event
->filter
->len
) != 0) {
147 /* One of the exclusions is NULL, fail. */
148 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
152 if (key
->exclusion
&& event
->exclusion
) {
153 /* Both exclusions exists, check count followed by the names. */
154 if (event
->exclusion
->count
!= key
->exclusion
->count
||
155 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
156 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
170 * Unique add of an ust app event in the given ht. This uses the custom
171 * ht_match_ust_app_event match function and the event name as hash.
173 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
174 struct ust_app_event
*event
)
176 struct cds_lfht_node
*node_ptr
;
177 struct ust_app_ht_key key
;
181 assert(ua_chan
->events
);
184 ht
= ua_chan
->events
;
185 key
.name
= event
->attr
.name
;
186 key
.filter
= event
->filter
;
187 key
.loglevel
= event
->attr
.loglevel
;
188 key
.exclusion
= event
->exclusion
;
190 node_ptr
= cds_lfht_add_unique(ht
->ht
,
191 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
192 ht_match_ust_app_event
, &key
, &event
->node
.node
);
193 assert(node_ptr
== &event
->node
.node
);
197 * Close the notify socket from the given RCU head object. This MUST be called
198 * through a call_rcu().
200 static void close_notify_sock_rcu(struct rcu_head
*head
)
203 struct ust_app_notify_sock_obj
*obj
=
204 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
206 /* Must have a valid fd here. */
207 assert(obj
->fd
>= 0);
209 ret
= close(obj
->fd
);
211 ERR("close notify sock %d RCU", obj
->fd
);
213 lttng_fd_put(LTTNG_FD_APPS
, 1);
219 * Return the session registry according to the buffer type of the given
222 * A registry per UID object MUST exists before calling this function or else
223 * it assert() if not found. RCU read side lock must be acquired.
225 static struct ust_registry_session
*get_session_registry(
226 struct ust_app_session
*ua_sess
)
228 struct ust_registry_session
*registry
= NULL
;
232 switch (ua_sess
->buffer_type
) {
233 case LTTNG_BUFFER_PER_PID
:
235 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
239 registry
= reg_pid
->registry
->reg
.ust
;
242 case LTTNG_BUFFER_PER_UID
:
244 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
245 ua_sess
->tracing_id
, ua_sess
->bits_per_long
, ua_sess
->uid
);
249 registry
= reg_uid
->registry
->reg
.ust
;
261 * Delete ust context safely. RCU read lock must be held before calling
265 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
273 pthread_mutex_lock(&app
->sock_lock
);
274 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
275 pthread_mutex_unlock(&app
->sock_lock
);
276 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
277 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
278 sock
, ua_ctx
->obj
->handle
, ret
);
286 * Delete ust app event safely. RCU read lock must be held before calling
290 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
297 free(ua_event
->filter
);
298 if (ua_event
->exclusion
!= NULL
)
299 free(ua_event
->exclusion
);
300 if (ua_event
->obj
!= NULL
) {
301 pthread_mutex_lock(&app
->sock_lock
);
302 ret
= ustctl_release_object(sock
, ua_event
->obj
);
303 pthread_mutex_unlock(&app
->sock_lock
);
304 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
305 ERR("UST app sock %d release event obj failed with ret %d",
314 * Release ust data object of the given stream.
316 * Return 0 on success or else a negative value.
318 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
326 pthread_mutex_lock(&app
->sock_lock
);
327 ret
= ustctl_release_object(sock
, stream
->obj
);
328 pthread_mutex_unlock(&app
->sock_lock
);
329 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
330 ERR("UST app sock %d release stream obj failed with ret %d",
333 lttng_fd_put(LTTNG_FD_APPS
, 2);
341 * Delete ust app stream safely. RCU read lock must be held before calling
345 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
350 (void) release_ust_app_stream(sock
, stream
, app
);
355 * We need to execute ht_destroy outside of RCU read-side critical
356 * section and outside of call_rcu thread, so we postpone its execution
357 * using ht_cleanup_push. It is simpler than to change the semantic of
358 * the many callers of delete_ust_app_session().
361 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
363 struct ust_app_channel
*ua_chan
=
364 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
366 ht_cleanup_push(ua_chan
->ctx
);
367 ht_cleanup_push(ua_chan
->events
);
372 * Delete ust app channel safely. RCU read lock must be held before calling
376 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
380 struct lttng_ht_iter iter
;
381 struct ust_app_event
*ua_event
;
382 struct ust_app_ctx
*ua_ctx
;
383 struct ust_app_stream
*stream
, *stmp
;
384 struct ust_registry_session
*registry
;
388 DBG3("UST app deleting channel %s", ua_chan
->name
);
391 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
392 cds_list_del(&stream
->list
);
393 delete_ust_app_stream(sock
, stream
, app
);
397 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
398 cds_list_del(&ua_ctx
->list
);
399 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
401 delete_ust_app_ctx(sock
, ua_ctx
, app
);
405 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
407 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
409 delete_ust_app_event(sock
, ua_event
, app
);
412 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
413 /* Wipe and free registry from session registry. */
414 registry
= get_session_registry(ua_chan
->session
);
416 ust_registry_channel_del_free(registry
, ua_chan
->key
);
420 if (ua_chan
->obj
!= NULL
) {
421 /* Remove channel from application UST object descriptor. */
422 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
423 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
425 pthread_mutex_lock(&app
->sock_lock
);
426 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
427 pthread_mutex_unlock(&app
->sock_lock
);
428 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
429 ERR("UST app sock %d release channel obj failed with ret %d",
432 lttng_fd_put(LTTNG_FD_APPS
, 1);
435 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
438 int ust_app_register_done(struct ust_app
*app
)
442 pthread_mutex_lock(&app
->sock_lock
);
443 ret
= ustctl_register_done(app
->sock
);
444 pthread_mutex_unlock(&app
->sock_lock
);
448 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
453 pthread_mutex_lock(&app
->sock_lock
);
458 ret
= ustctl_release_object(sock
, data
);
460 pthread_mutex_unlock(&app
->sock_lock
);
466 * Push metadata to consumer socket.
468 * RCU read-side lock must be held to guarantee existance of socket.
469 * Must be called with the ust app session lock held.
470 * Must be called with the registry lock held.
472 * On success, return the len of metadata pushed or else a negative value.
473 * Returning a -EPIPE return value means we could not send the metadata,
474 * but it can be caused by recoverable errors (e.g. the application has
475 * terminated concurrently).
477 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
478 struct consumer_socket
*socket
, int send_zero_data
)
481 char *metadata_str
= NULL
;
482 size_t len
, offset
, new_metadata_len_sent
;
484 uint64_t metadata_key
;
489 metadata_key
= registry
->metadata_key
;
492 * Means that no metadata was assigned to the session. This can
493 * happens if no start has been done previously.
500 * On a push metadata error either the consumer is dead or the
501 * metadata channel has been destroyed because its endpoint
502 * might have died (e.g: relayd), or because the application has
503 * exited. If so, the metadata closed flag is set to 1 so we
504 * deny pushing metadata again which is not valid anymore on the
507 if (registry
->metadata_closed
) {
511 offset
= registry
->metadata_len_sent
;
512 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
513 new_metadata_len_sent
= registry
->metadata_len
;
515 DBG3("No metadata to push for metadata key %" PRIu64
,
516 registry
->metadata_key
);
518 if (send_zero_data
) {
519 DBG("No metadata to push");
525 /* Allocate only what we have to send. */
526 metadata_str
= zmalloc(len
);
528 PERROR("zmalloc ust app metadata string");
532 /* Copy what we haven't sent out. */
533 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
536 pthread_mutex_unlock(®istry
->lock
);
538 * We need to unlock the registry while we push metadata to
539 * break a circular dependency between the consumerd metadata
540 * lock and the sessiond registry lock. Indeed, pushing metadata
541 * to the consumerd awaits that it gets pushed all the way to
542 * relayd, but doing so requires grabbing the metadata lock. If
543 * a concurrent metadata request is being performed by
544 * consumerd, this can try to grab the registry lock on the
545 * sessiond while holding the metadata lock on the consumer
546 * daemon. Those push and pull schemes are performed on two
547 * different bidirectionnal communication sockets.
549 ret
= consumer_push_metadata(socket
, metadata_key
,
550 metadata_str
, len
, offset
);
551 pthread_mutex_lock(®istry
->lock
);
554 * There is an acceptable race here between the registry
555 * metadata key assignment and the creation on the
556 * consumer. The session daemon can concurrently push
557 * metadata for this registry while being created on the
558 * consumer since the metadata key of the registry is
559 * assigned *before* it is setup to avoid the consumer
560 * to ask for metadata that could possibly be not found
561 * in the session daemon.
563 * The metadata will get pushed either by the session
564 * being stopped or the consumer requesting metadata if
565 * that race is triggered.
567 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
570 ERR("Error pushing metadata to consumer");
576 * Metadata may have been concurrently pushed, since
577 * we're not holding the registry lock while pushing to
578 * consumer. This is handled by the fact that we send
579 * the metadata content, size, and the offset at which
580 * that metadata belongs. This may arrive out of order
581 * on the consumer side, and the consumer is able to
582 * deal with overlapping fragments. The consumer
583 * supports overlapping fragments, which must be
584 * contiguous starting from offset 0. We keep the
585 * largest metadata_len_sent value of the concurrent
588 registry
->metadata_len_sent
=
589 max_t(size_t, registry
->metadata_len_sent
,
590 new_metadata_len_sent
);
599 * On error, flag the registry that the metadata is
600 * closed. We were unable to push anything and this
601 * means that either the consumer is not responding or
602 * the metadata cache has been destroyed on the
605 registry
->metadata_closed
= 1;
613 * For a given application and session, push metadata to consumer.
614 * Either sock or consumer is required : if sock is NULL, the default
615 * socket to send the metadata is retrieved from consumer, if sock
616 * is not NULL we use it to send the metadata.
617 * RCU read-side lock must be held while calling this function,
618 * therefore ensuring existance of registry. It also ensures existance
619 * of socket throughout this function.
621 * Return 0 on success else a negative error.
622 * Returning a -EPIPE return value means we could not send the metadata,
623 * but it can be caused by recoverable errors (e.g. the application has
624 * terminated concurrently).
626 static int push_metadata(struct ust_registry_session
*registry
,
627 struct consumer_output
*consumer
)
631 struct consumer_socket
*socket
;
636 pthread_mutex_lock(®istry
->lock
);
637 if (registry
->metadata_closed
) {
642 /* Get consumer socket to use to push the metadata.*/
643 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
650 ret
= ust_app_push_metadata(registry
, socket
, 0);
655 pthread_mutex_unlock(®istry
->lock
);
659 pthread_mutex_unlock(®istry
->lock
);
664 * Send to the consumer a close metadata command for the given session. Once
665 * done, the metadata channel is deleted and the session metadata pointer is
666 * nullified. The session lock MUST be held unless the application is
667 * in the destroy path.
669 * Return 0 on success else a negative value.
671 static int close_metadata(struct ust_registry_session
*registry
,
672 struct consumer_output
*consumer
)
675 struct consumer_socket
*socket
;
682 pthread_mutex_lock(®istry
->lock
);
684 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
689 /* Get consumer socket to use to push the metadata.*/
690 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
697 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
704 * Metadata closed. Even on error this means that the consumer is not
705 * responding or not found so either way a second close should NOT be emit
708 registry
->metadata_closed
= 1;
710 pthread_mutex_unlock(®istry
->lock
);
716 * We need to execute ht_destroy outside of RCU read-side critical
717 * section and outside of call_rcu thread, so we postpone its execution
718 * using ht_cleanup_push. It is simpler than to change the semantic of
719 * the many callers of delete_ust_app_session().
722 void delete_ust_app_session_rcu(struct rcu_head
*head
)
724 struct ust_app_session
*ua_sess
=
725 caa_container_of(head
, struct ust_app_session
, rcu_head
);
727 ht_cleanup_push(ua_sess
->channels
);
732 * Delete ust app session safely. RCU read lock must be held before calling
736 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
740 struct lttng_ht_iter iter
;
741 struct ust_app_channel
*ua_chan
;
742 struct ust_registry_session
*registry
;
746 pthread_mutex_lock(&ua_sess
->lock
);
748 assert(!ua_sess
->deleted
);
749 ua_sess
->deleted
= true;
751 registry
= get_session_registry(ua_sess
);
753 /* Push metadata for application before freeing the application. */
754 (void) push_metadata(registry
, ua_sess
->consumer
);
757 * Don't ask to close metadata for global per UID buffers. Close
758 * metadata only on destroy trace session in this case. Also, the
759 * previous push metadata could have flag the metadata registry to
760 * close so don't send a close command if closed.
762 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
763 /* And ask to close it for this session registry. */
764 (void) close_metadata(registry
, ua_sess
->consumer
);
768 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
770 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
772 delete_ust_app_channel(sock
, ua_chan
, app
);
775 /* In case of per PID, the registry is kept in the session. */
776 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
777 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
779 buffer_reg_pid_remove(reg_pid
);
780 buffer_reg_pid_destroy(reg_pid
);
784 if (ua_sess
->handle
!= -1) {
785 pthread_mutex_lock(&app
->sock_lock
);
786 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
787 pthread_mutex_unlock(&app
->sock_lock
);
788 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
789 ERR("UST app sock %d release session handle failed with ret %d",
793 pthread_mutex_unlock(&ua_sess
->lock
);
795 consumer_output_put(ua_sess
->consumer
);
797 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
801 * Delete a traceable application structure from the global list. Never call
802 * this function outside of a call_rcu call.
804 * RCU read side lock should _NOT_ be held when calling this function.
807 void delete_ust_app(struct ust_app
*app
)
810 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
812 /* Delete ust app sessions info */
817 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
819 /* Free every object in the session and the session. */
821 delete_ust_app_session(sock
, ua_sess
, app
);
825 ht_cleanup_push(app
->sessions
);
826 ht_cleanup_push(app
->ust_objd
);
829 * Wait until we have deleted the application from the sock hash table
830 * before closing this socket, otherwise an application could re-use the
831 * socket ID and race with the teardown, using the same hash table entry.
833 * It's OK to leave the close in call_rcu. We want it to stay unique for
834 * all RCU readers that could run concurrently with unregister app,
835 * therefore we _need_ to only close that socket after a grace period. So
836 * it should stay in this RCU callback.
838 * This close() is a very important step of the synchronization model so
839 * every modification to this function must be carefully reviewed.
845 lttng_fd_put(LTTNG_FD_APPS
, 1);
847 DBG2("UST app pid %d deleted", app
->pid
);
852 * URCU intermediate call to delete an UST app.
855 void delete_ust_app_rcu(struct rcu_head
*head
)
857 struct lttng_ht_node_ulong
*node
=
858 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
859 struct ust_app
*app
=
860 caa_container_of(node
, struct ust_app
, pid_n
);
862 DBG3("Call RCU deleting app PID %d", app
->pid
);
867 * Delete the session from the application ht and delete the data structure by
868 * freeing every object inside and releasing them.
870 static void destroy_app_session(struct ust_app
*app
,
871 struct ust_app_session
*ua_sess
)
874 struct lttng_ht_iter iter
;
879 iter
.iter
.node
= &ua_sess
->node
.node
;
880 ret
= lttng_ht_del(app
->sessions
, &iter
);
882 /* Already scheduled for teardown. */
886 /* Once deleted, free the data structure. */
887 delete_ust_app_session(app
->sock
, ua_sess
, app
);
894 * Alloc new UST app session.
897 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
899 struct ust_app_session
*ua_sess
;
901 /* Init most of the default value by allocating and zeroing */
902 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
903 if (ua_sess
== NULL
) {
908 ua_sess
->handle
= -1;
909 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
910 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
911 pthread_mutex_init(&ua_sess
->lock
, NULL
);
920 * Alloc new UST app channel.
923 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
924 struct ust_app_session
*ua_sess
,
925 struct lttng_ust_channel_attr
*attr
)
927 struct ust_app_channel
*ua_chan
;
929 /* Init most of the default value by allocating and zeroing */
930 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
931 if (ua_chan
== NULL
) {
936 /* Setup channel name */
937 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
938 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
940 ua_chan
->enabled
= 1;
941 ua_chan
->handle
= -1;
942 ua_chan
->session
= ua_sess
;
943 ua_chan
->key
= get_next_channel_key();
944 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
945 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
946 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
948 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
949 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
951 /* Copy attributes */
953 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
954 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
955 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
956 ua_chan
->attr
.overwrite
= attr
->overwrite
;
957 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
958 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
959 ua_chan
->attr
.output
= attr
->output
;
961 /* By default, the channel is a per cpu channel. */
962 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
964 DBG3("UST app channel %s allocated", ua_chan
->name
);
973 * Allocate and initialize a UST app stream.
975 * Return newly allocated stream pointer or NULL on error.
977 struct ust_app_stream
*ust_app_alloc_stream(void)
979 struct ust_app_stream
*stream
= NULL
;
981 stream
= zmalloc(sizeof(*stream
));
982 if (stream
== NULL
) {
983 PERROR("zmalloc ust app stream");
987 /* Zero could be a valid value for a handle so flag it to -1. */
995 * Alloc new UST app event.
998 struct ust_app_event
*alloc_ust_app_event(char *name
,
999 struct lttng_ust_event
*attr
)
1001 struct ust_app_event
*ua_event
;
1003 /* Init most of the default value by allocating and zeroing */
1004 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1005 if (ua_event
== NULL
) {
1010 ua_event
->enabled
= 1;
1011 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1012 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1013 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1015 /* Copy attributes */
1017 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1020 DBG3("UST app event %s allocated", ua_event
->name
);
1029 * Alloc new UST app context.
1032 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
1034 struct ust_app_ctx
*ua_ctx
;
1036 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1037 if (ua_ctx
== NULL
) {
1041 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1044 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1047 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1054 * Allocate a filter and copy the given original filter.
1056 * Return allocated filter or NULL on error.
1058 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1059 struct lttng_filter_bytecode
*orig_f
)
1061 struct lttng_filter_bytecode
*filter
= NULL
;
1063 /* Copy filter bytecode */
1064 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1066 PERROR("zmalloc alloc filter bytecode");
1070 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1077 * Create a liblttng-ust filter bytecode from given bytecode.
1079 * Return allocated filter or NULL on error.
1081 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1082 struct lttng_filter_bytecode
*orig_f
)
1084 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1086 /* Copy filter bytecode */
1087 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1089 PERROR("zmalloc alloc ust filter bytecode");
1093 assert(sizeof(struct lttng_filter_bytecode
) ==
1094 sizeof(struct lttng_ust_filter_bytecode
));
1095 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1101 * Find an ust_app using the sock and return it. RCU read side lock must be
1102 * held before calling this helper function.
1104 struct ust_app
*ust_app_find_by_sock(int sock
)
1106 struct lttng_ht_node_ulong
*node
;
1107 struct lttng_ht_iter iter
;
1109 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1110 node
= lttng_ht_iter_get_node_ulong(&iter
);
1112 DBG2("UST app find by sock %d not found", sock
);
1116 return caa_container_of(node
, struct ust_app
, sock_n
);
1123 * Find an ust_app using the notify sock and return it. RCU read side lock must
1124 * be held before calling this helper function.
1126 static struct ust_app
*find_app_by_notify_sock(int sock
)
1128 struct lttng_ht_node_ulong
*node
;
1129 struct lttng_ht_iter iter
;
1131 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1133 node
= lttng_ht_iter_get_node_ulong(&iter
);
1135 DBG2("UST app find by notify sock %d not found", sock
);
1139 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1146 * Lookup for an ust app event based on event name, filter bytecode and the
1149 * Return an ust_app_event object or NULL on error.
1151 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1152 char *name
, struct lttng_filter_bytecode
*filter
, int loglevel
,
1153 const struct lttng_event_exclusion
*exclusion
)
1155 struct lttng_ht_iter iter
;
1156 struct lttng_ht_node_str
*node
;
1157 struct ust_app_event
*event
= NULL
;
1158 struct ust_app_ht_key key
;
1163 /* Setup key for event lookup. */
1165 key
.filter
= filter
;
1166 key
.loglevel
= loglevel
;
1167 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1168 key
.exclusion
= exclusion
;
1170 /* Lookup using the event name as hash and a custom match fct. */
1171 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1172 ht_match_ust_app_event
, &key
, &iter
.iter
);
1173 node
= lttng_ht_iter_get_node_str(&iter
);
1178 event
= caa_container_of(node
, struct ust_app_event
, node
);
1185 * Create the channel context on the tracer.
1187 * Called with UST app session lock held.
1190 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1191 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1195 health_code_update();
1197 pthread_mutex_lock(&app
->sock_lock
);
1198 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1199 ua_chan
->obj
, &ua_ctx
->obj
);
1200 pthread_mutex_unlock(&app
->sock_lock
);
1202 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1203 ERR("UST app create channel context failed for app (pid: %d) "
1204 "with ret %d", app
->pid
, ret
);
1207 * This is normal behavior, an application can die during the
1208 * creation process. Don't report an error so the execution can
1209 * continue normally.
1212 DBG3("UST app disable event failed. Application is dead.");
1217 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1219 DBG2("UST app context handle %d created successfully for channel %s",
1220 ua_ctx
->handle
, ua_chan
->name
);
1223 health_code_update();
1228 * Set the filter on the tracer.
1231 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1232 struct ust_app
*app
)
1235 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1237 health_code_update();
1239 if (!ua_event
->filter
) {
1244 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1245 if (!ust_bytecode
) {
1246 ret
= -LTTNG_ERR_NOMEM
;
1249 pthread_mutex_lock(&app
->sock_lock
);
1250 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1252 pthread_mutex_unlock(&app
->sock_lock
);
1254 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1255 ERR("UST app event %s filter failed for app (pid: %d) "
1256 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1259 * This is normal behavior, an application can die during the
1260 * creation process. Don't report an error so the execution can
1261 * continue normally.
1264 DBG3("UST app filter event failed. Application is dead.");
1269 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1272 health_code_update();
1278 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1279 struct lttng_event_exclusion
*exclusion
)
1281 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1282 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1283 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1285 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1286 if (!ust_exclusion
) {
1291 assert(sizeof(struct lttng_event_exclusion
) ==
1292 sizeof(struct lttng_ust_event_exclusion
));
1293 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1295 return ust_exclusion
;
1299 * Set event exclusions on the tracer.
1302 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1303 struct ust_app
*app
)
1306 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1308 health_code_update();
1310 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1315 ust_exclusion
= create_ust_exclusion_from_exclusion(
1316 ua_event
->exclusion
);
1317 if (!ust_exclusion
) {
1318 ret
= -LTTNG_ERR_NOMEM
;
1321 pthread_mutex_lock(&app
->sock_lock
);
1322 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1323 pthread_mutex_unlock(&app
->sock_lock
);
1325 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1326 ERR("UST app event %s exclusions failed for app (pid: %d) "
1327 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1330 * This is normal behavior, an application can die during the
1331 * creation process. Don't report an error so the execution can
1332 * continue normally.
1335 DBG3("UST app event exclusion failed. Application is dead.");
1340 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1343 health_code_update();
1344 free(ust_exclusion
);
1349 * Disable the specified event on to UST tracer for the UST session.
1351 static int disable_ust_event(struct ust_app
*app
,
1352 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1356 health_code_update();
1358 pthread_mutex_lock(&app
->sock_lock
);
1359 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1360 pthread_mutex_unlock(&app
->sock_lock
);
1362 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1363 ERR("UST app event %s disable failed for app (pid: %d) "
1364 "and session handle %d with ret %d",
1365 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1368 * This is normal behavior, an application can die during the
1369 * creation process. Don't report an error so the execution can
1370 * continue normally.
1373 DBG3("UST app disable event failed. Application is dead.");
1378 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1379 ua_event
->attr
.name
, app
->pid
);
1382 health_code_update();
1387 * Disable the specified channel on to UST tracer for the UST session.
1389 static int disable_ust_channel(struct ust_app
*app
,
1390 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1394 health_code_update();
1396 pthread_mutex_lock(&app
->sock_lock
);
1397 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1398 pthread_mutex_unlock(&app
->sock_lock
);
1400 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1401 ERR("UST app channel %s disable failed for app (pid: %d) "
1402 "and session handle %d with ret %d",
1403 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1406 * This is normal behavior, an application can die during the
1407 * creation process. Don't report an error so the execution can
1408 * continue normally.
1411 DBG3("UST app disable channel failed. Application is dead.");
1416 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1417 ua_chan
->name
, app
->pid
);
1420 health_code_update();
1425 * Enable the specified channel on to UST tracer for the UST session.
1427 static int enable_ust_channel(struct ust_app
*app
,
1428 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1432 health_code_update();
1434 pthread_mutex_lock(&app
->sock_lock
);
1435 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1436 pthread_mutex_unlock(&app
->sock_lock
);
1438 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1439 ERR("UST app channel %s enable failed for app (pid: %d) "
1440 "and session handle %d with ret %d",
1441 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1444 * This is normal behavior, an application can die during the
1445 * creation process. Don't report an error so the execution can
1446 * continue normally.
1449 DBG3("UST app enable channel failed. Application is dead.");
1454 ua_chan
->enabled
= 1;
1456 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1457 ua_chan
->name
, app
->pid
);
1460 health_code_update();
1465 * Enable the specified event on to UST tracer for the UST session.
1467 static int enable_ust_event(struct ust_app
*app
,
1468 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1472 health_code_update();
1474 pthread_mutex_lock(&app
->sock_lock
);
1475 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1476 pthread_mutex_unlock(&app
->sock_lock
);
1478 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1479 ERR("UST app event %s enable failed for app (pid: %d) "
1480 "and session handle %d with ret %d",
1481 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1484 * This is normal behavior, an application can die during the
1485 * creation process. Don't report an error so the execution can
1486 * continue normally.
1489 DBG3("UST app enable event failed. Application is dead.");
1494 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1495 ua_event
->attr
.name
, app
->pid
);
1498 health_code_update();
1503 * Send channel and stream buffer to application.
1505 * Return 0 on success. On error, a negative value is returned.
1507 static int send_channel_pid_to_ust(struct ust_app
*app
,
1508 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1511 struct ust_app_stream
*stream
, *stmp
;
1517 health_code_update();
1519 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1522 /* Send channel to the application. */
1523 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1524 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1525 ret
= -ENOTCONN
; /* Caused by app exiting. */
1527 } else if (ret
< 0) {
1531 health_code_update();
1533 /* Send all streams to application. */
1534 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1535 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1536 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1537 ret
= -ENOTCONN
; /* Caused by app exiting. */
1539 } else if (ret
< 0) {
1542 /* We don't need the stream anymore once sent to the tracer. */
1543 cds_list_del(&stream
->list
);
1544 delete_ust_app_stream(-1, stream
, app
);
1546 /* Flag the channel that it is sent to the application. */
1547 ua_chan
->is_sent
= 1;
1550 health_code_update();
1555 * Create the specified event onto the UST tracer for a UST session.
1557 * Should be called with session mutex held.
1560 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1561 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1565 health_code_update();
1567 /* Create UST event on tracer */
1568 pthread_mutex_lock(&app
->sock_lock
);
1569 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1571 pthread_mutex_unlock(&app
->sock_lock
);
1573 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1574 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1575 ua_event
->attr
.name
, app
->pid
, ret
);
1578 * This is normal behavior, an application can die during the
1579 * creation process. Don't report an error so the execution can
1580 * continue normally.
1583 DBG3("UST app create event failed. Application is dead.");
1588 ua_event
->handle
= ua_event
->obj
->handle
;
1590 DBG2("UST app event %s created successfully for pid:%d",
1591 ua_event
->attr
.name
, app
->pid
);
1593 health_code_update();
1595 /* Set filter if one is present. */
1596 if (ua_event
->filter
) {
1597 ret
= set_ust_event_filter(ua_event
, app
);
1603 /* Set exclusions for the event */
1604 if (ua_event
->exclusion
) {
1605 ret
= set_ust_event_exclusion(ua_event
, app
);
1611 /* If event not enabled, disable it on the tracer */
1612 if (ua_event
->enabled
) {
1614 * We now need to explicitly enable the event, since it
1615 * is now disabled at creation.
1617 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1620 * If we hit an EPERM, something is wrong with our enable call. If
1621 * we get an EEXIST, there is a problem on the tracer side since we
1625 case -LTTNG_UST_ERR_PERM
:
1626 /* Code flow problem */
1628 case -LTTNG_UST_ERR_EXIST
:
1629 /* It's OK for our use case. */
1640 health_code_update();
1645 * Copy data between an UST app event and a LTT event.
1647 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1648 struct ltt_ust_event
*uevent
)
1650 size_t exclusion_alloc_size
;
1652 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1653 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1655 ua_event
->enabled
= uevent
->enabled
;
1657 /* Copy event attributes */
1658 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1660 /* Copy filter bytecode */
1661 if (uevent
->filter
) {
1662 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1663 /* Filter might be NULL here in case of ENONEM. */
1666 /* Copy exclusion data */
1667 if (uevent
->exclusion
) {
1668 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1669 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1670 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1671 if (ua_event
->exclusion
== NULL
) {
1674 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1675 exclusion_alloc_size
);
1681 * Copy data between an UST app channel and a LTT channel.
1683 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1684 struct ltt_ust_channel
*uchan
)
1686 struct lttng_ht_iter iter
;
1687 struct ltt_ust_event
*uevent
;
1688 struct ltt_ust_context
*uctx
;
1689 struct ust_app_event
*ua_event
;
1690 struct ust_app_ctx
*ua_ctx
;
1692 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1694 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1695 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1697 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1698 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1700 /* Copy event attributes since the layout is different. */
1701 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1702 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1703 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1704 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1705 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1706 ua_chan
->attr
.output
= uchan
->attr
.output
;
1708 * Note that the attribute channel type is not set since the channel on the
1709 * tracing registry side does not have this information.
1712 ua_chan
->enabled
= uchan
->enabled
;
1713 ua_chan
->tracing_channel_id
= uchan
->id
;
1715 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1716 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1717 if (ua_ctx
== NULL
) {
1720 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1721 (unsigned long) ua_ctx
->ctx
.ctx
);
1722 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1723 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1726 /* Copy all events from ltt ust channel to ust app channel */
1727 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1728 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1729 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1730 if (ua_event
== NULL
) {
1731 DBG2("UST event %s not found on shadow copy channel",
1733 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1734 if (ua_event
== NULL
) {
1737 shadow_copy_event(ua_event
, uevent
);
1738 add_unique_ust_app_event(ua_chan
, ua_event
);
1742 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1746 * Copy data between a UST app session and a regular LTT session.
1748 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1749 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1751 struct lttng_ht_node_str
*ua_chan_node
;
1752 struct lttng_ht_iter iter
;
1753 struct ltt_ust_channel
*uchan
;
1754 struct ust_app_channel
*ua_chan
;
1756 struct tm
*timeinfo
;
1759 char tmp_shm_path
[PATH_MAX
];
1761 /* Get date and time for unique app path */
1763 timeinfo
= localtime(&rawtime
);
1764 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1766 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1768 ua_sess
->tracing_id
= usess
->id
;
1769 ua_sess
->id
= get_next_session_id();
1770 ua_sess
->uid
= app
->uid
;
1771 ua_sess
->gid
= app
->gid
;
1772 ua_sess
->euid
= usess
->uid
;
1773 ua_sess
->egid
= usess
->gid
;
1774 ua_sess
->buffer_type
= usess
->buffer_type
;
1775 ua_sess
->bits_per_long
= app
->bits_per_long
;
1777 /* There is only one consumer object per session possible. */
1778 consumer_output_get(usess
->consumer
);
1779 ua_sess
->consumer
= usess
->consumer
;
1781 ua_sess
->output_traces
= usess
->output_traces
;
1782 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1783 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1784 &usess
->metadata_attr
);
1786 switch (ua_sess
->buffer_type
) {
1787 case LTTNG_BUFFER_PER_PID
:
1788 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1789 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1792 case LTTNG_BUFFER_PER_UID
:
1793 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1794 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1801 PERROR("asprintf UST shadow copy session");
1806 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1807 sizeof(ua_sess
->root_shm_path
));
1808 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1809 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1810 sizeof(ua_sess
->shm_path
));
1811 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1812 if (ua_sess
->shm_path
[0]) {
1813 switch (ua_sess
->buffer_type
) {
1814 case LTTNG_BUFFER_PER_PID
:
1815 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1816 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1817 app
->name
, app
->pid
, datetime
);
1819 case LTTNG_BUFFER_PER_UID
:
1820 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1821 DEFAULT_UST_TRACE_UID_PATH
,
1822 app
->uid
, app
->bits_per_long
);
1829 PERROR("sprintf UST shadow copy session");
1833 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1834 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1835 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1838 /* Iterate over all channels in global domain. */
1839 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1841 struct lttng_ht_iter uiter
;
1843 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1844 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1845 if (ua_chan_node
!= NULL
) {
1846 /* Session exist. Contiuing. */
1850 DBG2("Channel %s not found on shadow session copy, creating it",
1852 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1853 if (ua_chan
== NULL
) {
1854 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1857 shadow_copy_channel(ua_chan
, uchan
);
1859 * The concept of metadata channel does not exist on the tracing
1860 * registry side of the session daemon so this can only be a per CPU
1861 * channel and not metadata.
1863 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1865 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1870 consumer_output_put(ua_sess
->consumer
);
1874 * Lookup sesison wrapper.
1877 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1878 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1880 /* Get right UST app session from app */
1881 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1885 * Return ust app session from the app session hashtable using the UST session
1888 static struct ust_app_session
*lookup_session_by_app(
1889 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1891 struct lttng_ht_iter iter
;
1892 struct lttng_ht_node_u64
*node
;
1894 __lookup_session_by_app(usess
, app
, &iter
);
1895 node
= lttng_ht_iter_get_node_u64(&iter
);
1900 return caa_container_of(node
, struct ust_app_session
, node
);
1907 * Setup buffer registry per PID for the given session and application. If none
1908 * is found, a new one is created, added to the global registry and
1909 * initialized. If regp is valid, it's set with the newly created object.
1911 * Return 0 on success or else a negative value.
1913 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1914 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1917 struct buffer_reg_pid
*reg_pid
;
1924 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1927 * This is the create channel path meaning that if there is NO
1928 * registry available, we have to create one for this session.
1930 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
1931 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1939 /* Initialize registry. */
1940 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1941 app
->bits_per_long
, app
->uint8_t_alignment
,
1942 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1943 app
->uint64_t_alignment
, app
->long_alignment
,
1944 app
->byte_order
, app
->version
.major
,
1945 app
->version
.minor
, reg_pid
->root_shm_path
,
1947 ua_sess
->euid
, ua_sess
->egid
);
1950 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1951 * destroy the buffer registry, because it is always expected
1952 * that if the buffer registry can be found, its ust registry is
1955 buffer_reg_pid_destroy(reg_pid
);
1959 buffer_reg_pid_add(reg_pid
);
1961 DBG3("UST app buffer registry per PID created successfully");
1973 * Setup buffer registry per UID for the given session and application. If none
1974 * is found, a new one is created, added to the global registry and
1975 * initialized. If regp is valid, it's set with the newly created object.
1977 * Return 0 on success or else a negative value.
1979 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
1980 struct ust_app_session
*ua_sess
,
1981 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
1984 struct buffer_reg_uid
*reg_uid
;
1991 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
1994 * This is the create channel path meaning that if there is NO
1995 * registry available, we have to create one for this session.
1997 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
1998 LTTNG_DOMAIN_UST
, ®_uid
,
1999 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2007 /* Initialize registry. */
2008 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2009 app
->bits_per_long
, app
->uint8_t_alignment
,
2010 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2011 app
->uint64_t_alignment
, app
->long_alignment
,
2012 app
->byte_order
, app
->version
.major
,
2013 app
->version
.minor
, reg_uid
->root_shm_path
,
2014 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
2017 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2018 * destroy the buffer registry, because it is always expected
2019 * that if the buffer registry can be found, its ust registry is
2022 buffer_reg_uid_destroy(reg_uid
, NULL
);
2025 /* Add node to teardown list of the session. */
2026 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2028 buffer_reg_uid_add(reg_uid
);
2030 DBG3("UST app buffer registry per UID created successfully");
2041 * Create a session on the tracer side for the given app.
2043 * On success, ua_sess_ptr is populated with the session pointer or else left
2044 * untouched. If the session was created, is_created is set to 1. On error,
2045 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2048 * Returns 0 on success or else a negative code which is either -ENOMEM or
2049 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2051 static int create_ust_app_session(struct ltt_ust_session
*usess
,
2052 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2055 int ret
, created
= 0;
2056 struct ust_app_session
*ua_sess
;
2060 assert(ua_sess_ptr
);
2062 health_code_update();
2064 ua_sess
= lookup_session_by_app(usess
, app
);
2065 if (ua_sess
== NULL
) {
2066 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2067 app
->pid
, usess
->id
);
2068 ua_sess
= alloc_ust_app_session(app
);
2069 if (ua_sess
== NULL
) {
2070 /* Only malloc can failed so something is really wrong */
2074 shadow_copy_session(ua_sess
, usess
, app
);
2078 switch (usess
->buffer_type
) {
2079 case LTTNG_BUFFER_PER_PID
:
2080 /* Init local registry. */
2081 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2083 delete_ust_app_session(-1, ua_sess
, app
);
2087 case LTTNG_BUFFER_PER_UID
:
2088 /* Look for a global registry. If none exists, create one. */
2089 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2091 delete_ust_app_session(-1, ua_sess
, app
);
2101 health_code_update();
2103 if (ua_sess
->handle
== -1) {
2104 pthread_mutex_lock(&app
->sock_lock
);
2105 ret
= ustctl_create_session(app
->sock
);
2106 pthread_mutex_unlock(&app
->sock_lock
);
2108 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2109 ERR("Creating session for app pid %d with ret %d",
2112 DBG("UST app creating session failed. Application is dead");
2114 * This is normal behavior, an application can die during the
2115 * creation process. Don't report an error so the execution can
2116 * continue normally. This will get flagged ENOTCONN and the
2117 * caller will handle it.
2121 delete_ust_app_session(-1, ua_sess
, app
);
2122 if (ret
!= -ENOMEM
) {
2124 * Tracer is probably gone or got an internal error so let's
2125 * behave like it will soon unregister or not usable.
2132 ua_sess
->handle
= ret
;
2134 /* Add ust app session to app's HT */
2135 lttng_ht_node_init_u64(&ua_sess
->node
,
2136 ua_sess
->tracing_id
);
2137 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2139 DBG2("UST app session created successfully with handle %d", ret
);
2142 *ua_sess_ptr
= ua_sess
;
2144 *is_created
= created
;
2147 /* Everything went well. */
2151 health_code_update();
2156 * Match function for a hash table lookup of ust_app_ctx.
2158 * It matches an ust app context based on the context type and, in the case
2159 * of perf counters, their name.
2161 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2163 struct ust_app_ctx
*ctx
;
2164 const struct lttng_ust_context
*key
;
2169 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2173 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2177 /* Check the name in the case of perf thread counters. */
2178 if (key
->ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
2179 if (strncmp(key
->u
.perf_counter
.name
,
2180 ctx
->ctx
.u
.perf_counter
.name
,
2181 sizeof(key
->u
.perf_counter
.name
))) {
2194 * Lookup for an ust app context from an lttng_ust_context.
2196 * Must be called while holding RCU read side lock.
2197 * Return an ust_app_ctx object or NULL on error.
2200 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2201 struct lttng_ust_context
*uctx
)
2203 struct lttng_ht_iter iter
;
2204 struct lttng_ht_node_ulong
*node
;
2205 struct ust_app_ctx
*app_ctx
= NULL
;
2210 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2211 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2212 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2213 node
= lttng_ht_iter_get_node_ulong(&iter
);
2218 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2225 * Create a context for the channel on the tracer.
2227 * Called with UST app session lock held and a RCU read side lock.
2230 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2231 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
2232 struct ust_app
*app
)
2235 struct ust_app_ctx
*ua_ctx
;
2237 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2239 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2245 ua_ctx
= alloc_ust_app_ctx(uctx
);
2246 if (ua_ctx
== NULL
) {
2252 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2253 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2254 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2256 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2266 * Enable on the tracer side a ust app event for the session and channel.
2268 * Called with UST app session lock held.
2271 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2272 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2276 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2281 ua_event
->enabled
= 1;
2288 * Disable on the tracer side a ust app event for the session and channel.
2290 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2291 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2295 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2300 ua_event
->enabled
= 0;
2307 * Lookup ust app channel for session and disable it on the tracer side.
2310 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2311 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2315 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2320 ua_chan
->enabled
= 0;
2327 * Lookup ust app channel for session and enable it on the tracer side. This
2328 * MUST be called with a RCU read side lock acquired.
2330 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2331 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2334 struct lttng_ht_iter iter
;
2335 struct lttng_ht_node_str
*ua_chan_node
;
2336 struct ust_app_channel
*ua_chan
;
2338 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2339 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2340 if (ua_chan_node
== NULL
) {
2341 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2342 uchan
->name
, ua_sess
->tracing_id
);
2346 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2348 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2358 * Ask the consumer to create a channel and get it if successful.
2360 * Return 0 on success or else a negative value.
2362 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2363 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2364 int bitness
, struct ust_registry_session
*registry
)
2367 unsigned int nb_fd
= 0;
2368 struct consumer_socket
*socket
;
2376 health_code_update();
2378 /* Get the right consumer socket for the application. */
2379 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2385 health_code_update();
2387 /* Need one fd for the channel. */
2388 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2390 ERR("Exhausted number of available FD upon create channel");
2395 * Ask consumer to create channel. The consumer will return the number of
2396 * stream we have to expect.
2398 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2405 * Compute the number of fd needed before receiving them. It must be 2 per
2406 * stream (2 being the default value here).
2408 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2410 /* Reserve the amount of file descriptor we need. */
2411 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2413 ERR("Exhausted number of available FD upon create channel");
2414 goto error_fd_get_stream
;
2417 health_code_update();
2420 * Now get the channel from the consumer. This call wil populate the stream
2421 * list of that channel and set the ust objects.
2423 if (usess
->consumer
->enabled
) {
2424 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2434 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2435 error_fd_get_stream
:
2437 * Initiate a destroy channel on the consumer since we had an error
2438 * handling it on our side. The return value is of no importance since we
2439 * already have a ret value set by the previous error that we need to
2442 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2444 lttng_fd_put(LTTNG_FD_APPS
, 1);
2446 health_code_update();
2452 * Duplicate the ust data object of the ust app stream and save it in the
2453 * buffer registry stream.
2455 * Return 0 on success or else a negative value.
2457 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2458 struct ust_app_stream
*stream
)
2465 /* Reserve the amount of file descriptor we need. */
2466 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2468 ERR("Exhausted number of available FD upon duplicate stream");
2472 /* Duplicate object for stream once the original is in the registry. */
2473 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2474 reg_stream
->obj
.ust
);
2476 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2477 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2478 lttng_fd_put(LTTNG_FD_APPS
, 2);
2481 stream
->handle
= stream
->obj
->handle
;
2488 * Duplicate the ust data object of the ust app. channel and save it in the
2489 * buffer registry channel.
2491 * Return 0 on success or else a negative value.
2493 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2494 struct ust_app_channel
*ua_chan
)
2501 /* Need two fds for the channel. */
2502 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2504 ERR("Exhausted number of available FD upon duplicate channel");
2508 /* Duplicate object for stream once the original is in the registry. */
2509 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2511 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2512 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2515 ua_chan
->handle
= ua_chan
->obj
->handle
;
2520 lttng_fd_put(LTTNG_FD_APPS
, 1);
2526 * For a given channel buffer registry, setup all streams of the given ust
2527 * application channel.
2529 * Return 0 on success or else a negative value.
2531 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2532 struct ust_app_channel
*ua_chan
,
2533 struct ust_app
*app
)
2536 struct ust_app_stream
*stream
, *stmp
;
2541 DBG2("UST app setup buffer registry stream");
2543 /* Send all streams to application. */
2544 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2545 struct buffer_reg_stream
*reg_stream
;
2547 ret
= buffer_reg_stream_create(®_stream
);
2553 * Keep original pointer and nullify it in the stream so the delete
2554 * stream call does not release the object.
2556 reg_stream
->obj
.ust
= stream
->obj
;
2558 buffer_reg_stream_add(reg_stream
, reg_chan
);
2560 /* We don't need the streams anymore. */
2561 cds_list_del(&stream
->list
);
2562 delete_ust_app_stream(-1, stream
, app
);
2570 * Create a buffer registry channel for the given session registry and
2571 * application channel object. If regp pointer is valid, it's set with the
2572 * created object. Important, the created object is NOT added to the session
2573 * registry hash table.
2575 * Return 0 on success else a negative value.
2577 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2578 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2581 struct buffer_reg_channel
*reg_chan
= NULL
;
2586 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2588 /* Create buffer registry channel. */
2589 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2594 reg_chan
->consumer_key
= ua_chan
->key
;
2595 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2596 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2598 /* Create and add a channel registry to session. */
2599 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2600 ua_chan
->tracing_channel_id
);
2604 buffer_reg_channel_add(reg_sess
, reg_chan
);
2613 /* Safe because the registry channel object was not added to any HT. */
2614 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2620 * Setup buffer registry channel for the given session registry and application
2621 * channel object. If regp pointer is valid, it's set with the created object.
2623 * Return 0 on success else a negative value.
2625 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2626 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2627 struct ust_app
*app
)
2634 assert(ua_chan
->obj
);
2636 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2638 /* Setup all streams for the registry. */
2639 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2644 reg_chan
->obj
.ust
= ua_chan
->obj
;
2645 ua_chan
->obj
= NULL
;
2650 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2651 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2656 * Send buffer registry channel to the application.
2658 * Return 0 on success else a negative value.
2660 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2661 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2662 struct ust_app_channel
*ua_chan
)
2665 struct buffer_reg_stream
*reg_stream
;
2672 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2674 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2679 /* Send channel to the application. */
2680 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2681 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2682 ret
= -ENOTCONN
; /* Caused by app exiting. */
2684 } else if (ret
< 0) {
2688 health_code_update();
2690 /* Send all streams to application. */
2691 pthread_mutex_lock(®_chan
->stream_list_lock
);
2692 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2693 struct ust_app_stream stream
;
2695 ret
= duplicate_stream_object(reg_stream
, &stream
);
2697 goto error_stream_unlock
;
2700 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2702 (void) release_ust_app_stream(-1, &stream
, app
);
2703 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2704 ret
= -ENOTCONN
; /* Caused by app exiting. */
2705 goto error_stream_unlock
;
2706 } else if (ret
< 0) {
2707 goto error_stream_unlock
;
2709 goto error_stream_unlock
;
2713 * The return value is not important here. This function will output an
2716 (void) release_ust_app_stream(-1, &stream
, app
);
2718 ua_chan
->is_sent
= 1;
2720 error_stream_unlock
:
2721 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2727 * Create and send to the application the created buffers with per UID buffers.
2729 * Return 0 on success else a negative value.
2731 static int create_channel_per_uid(struct ust_app
*app
,
2732 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2733 struct ust_app_channel
*ua_chan
)
2736 struct buffer_reg_uid
*reg_uid
;
2737 struct buffer_reg_channel
*reg_chan
;
2744 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2746 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2748 * The session creation handles the creation of this global registry
2749 * object. If none can be find, there is a code flow problem or a
2754 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2757 /* Create the buffer registry channel object. */
2758 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2760 ERR("Error creating the UST channel \"%s\" registry instance",
2767 * Create the buffers on the consumer side. This call populates the
2768 * ust app channel object with all streams and data object.
2770 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2771 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2773 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2777 * Let's remove the previously created buffer registry channel so
2778 * it's not visible anymore in the session registry.
2780 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2781 ua_chan
->tracing_channel_id
);
2782 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2783 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2788 * Setup the streams and add it to the session registry.
2790 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2791 ua_chan
, reg_chan
, app
);
2793 ERR("Error setting up UST channel \"%s\"",
2800 /* Send buffers to the application. */
2801 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2803 if (ret
!= -ENOTCONN
) {
2804 ERR("Error sending channel to application");
2814 * Create and send to the application the created buffers with per PID buffers.
2816 * Return 0 on success else a negative value.
2818 static int create_channel_per_pid(struct ust_app
*app
,
2819 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2820 struct ust_app_channel
*ua_chan
)
2823 struct ust_registry_session
*registry
;
2830 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2834 registry
= get_session_registry(ua_sess
);
2837 /* Create and add a new channel registry to session. */
2838 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2840 ERR("Error creating the UST channel \"%s\" registry instance",
2845 /* Create and get channel on the consumer side. */
2846 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2847 app
->bits_per_long
, registry
);
2849 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2854 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2856 if (ret
!= -ENOTCONN
) {
2857 ERR("Error sending channel to application");
2868 * From an already allocated ust app channel, create the channel buffers if
2869 * need and send it to the application. This MUST be called with a RCU read
2870 * side lock acquired.
2872 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2873 * the application exited concurrently.
2875 static int do_create_channel(struct ust_app
*app
,
2876 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2877 struct ust_app_channel
*ua_chan
)
2886 /* Handle buffer type before sending the channel to the application. */
2887 switch (usess
->buffer_type
) {
2888 case LTTNG_BUFFER_PER_UID
:
2890 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
2896 case LTTNG_BUFFER_PER_PID
:
2898 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
2910 /* Initialize ust objd object using the received handle and add it. */
2911 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
2912 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
2914 /* If channel is not enabled, disable it on the tracer */
2915 if (!ua_chan
->enabled
) {
2916 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2927 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2928 * newly created channel if not NULL.
2930 * Called with UST app session lock and RCU read-side lock held.
2932 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2933 * the application exited concurrently.
2935 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
2936 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
2937 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
2938 struct ust_app_channel
**ua_chanp
)
2941 struct lttng_ht_iter iter
;
2942 struct lttng_ht_node_str
*ua_chan_node
;
2943 struct ust_app_channel
*ua_chan
;
2945 /* Lookup channel in the ust app session */
2946 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2947 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2948 if (ua_chan_node
!= NULL
) {
2949 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2953 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
2954 if (ua_chan
== NULL
) {
2955 /* Only malloc can fail here */
2959 shadow_copy_channel(ua_chan
, uchan
);
2961 /* Set channel type. */
2962 ua_chan
->attr
.type
= type
;
2964 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
2969 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
2972 /* Only add the channel if successful on the tracer side. */
2973 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
2977 *ua_chanp
= ua_chan
;
2980 /* Everything went well. */
2984 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
2990 * Create UST app event and create it on the tracer side.
2992 * Called with ust app session mutex held.
2995 int create_ust_app_event(struct ust_app_session
*ua_sess
,
2996 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
2997 struct ust_app
*app
)
3000 struct ust_app_event
*ua_event
;
3002 /* Get event node */
3003 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3004 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3005 if (ua_event
!= NULL
) {
3010 /* Does not exist so create one */
3011 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3012 if (ua_event
== NULL
) {
3013 /* Only malloc can failed so something is really wrong */
3017 shadow_copy_event(ua_event
, uevent
);
3019 /* Create it on the tracer side */
3020 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3022 /* Not found previously means that it does not exist on the tracer */
3023 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
3027 add_unique_ust_app_event(ua_chan
, ua_event
);
3029 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3036 /* Valid. Calling here is already in a read side lock */
3037 delete_ust_app_event(-1, ua_event
, app
);
3042 * Create UST metadata and open it on the tracer side.
3044 * Called with UST app session lock held and RCU read side lock.
3046 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3047 struct ust_app
*app
, struct consumer_output
*consumer
)
3050 struct ust_app_channel
*metadata
;
3051 struct consumer_socket
*socket
;
3052 struct ust_registry_session
*registry
;
3058 registry
= get_session_registry(ua_sess
);
3061 pthread_mutex_lock(®istry
->lock
);
3063 /* Metadata already exists for this registry or it was closed previously */
3064 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3069 /* Allocate UST metadata */
3070 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3072 /* malloc() failed */
3077 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3079 /* Need one fd for the channel. */
3080 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3082 ERR("Exhausted number of available FD upon create metadata");
3086 /* Get the right consumer socket for the application. */
3087 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3090 goto error_consumer
;
3094 * Keep metadata key so we can identify it on the consumer side. Assign it
3095 * to the registry *before* we ask the consumer so we avoid the race of the
3096 * consumer requesting the metadata and the ask_channel call on our side
3097 * did not returned yet.
3099 registry
->metadata_key
= metadata
->key
;
3102 * Ask the metadata channel creation to the consumer. The metadata object
3103 * will be created by the consumer and kept their. However, the stream is
3104 * never added or monitored until we do a first push metadata to the
3107 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3110 /* Nullify the metadata key so we don't try to close it later on. */
3111 registry
->metadata_key
= 0;
3112 goto error_consumer
;
3116 * The setup command will make the metadata stream be sent to the relayd,
3117 * if applicable, and the thread managing the metadatas. This is important
3118 * because after this point, if an error occurs, the only way the stream
3119 * can be deleted is to be monitored in the consumer.
3121 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3123 /* Nullify the metadata key so we don't try to close it later on. */
3124 registry
->metadata_key
= 0;
3125 goto error_consumer
;
3128 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3129 metadata
->key
, app
->pid
);
3132 lttng_fd_put(LTTNG_FD_APPS
, 1);
3133 delete_ust_app_channel(-1, metadata
, app
);
3135 pthread_mutex_unlock(®istry
->lock
);
3140 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3141 * acquired before calling this function.
3143 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3145 struct ust_app
*app
= NULL
;
3146 struct lttng_ht_node_ulong
*node
;
3147 struct lttng_ht_iter iter
;
3149 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3150 node
= lttng_ht_iter_get_node_ulong(&iter
);
3152 DBG2("UST app no found with pid %d", pid
);
3156 DBG2("Found UST app by pid %d", pid
);
3158 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3165 * Allocate and init an UST app object using the registration information and
3166 * the command socket. This is called when the command socket connects to the
3169 * The object is returned on success or else NULL.
3171 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3173 struct ust_app
*lta
= NULL
;
3178 DBG3("UST app creating application for socket %d", sock
);
3180 if ((msg
->bits_per_long
== 64 &&
3181 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3182 || (msg
->bits_per_long
== 32 &&
3183 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3184 ERR("Registration failed: application \"%s\" (pid: %d) has "
3185 "%d-bit long, but no consumerd for this size is available.\n",
3186 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3190 lta
= zmalloc(sizeof(struct ust_app
));
3196 lta
->ppid
= msg
->ppid
;
3197 lta
->uid
= msg
->uid
;
3198 lta
->gid
= msg
->gid
;
3200 lta
->bits_per_long
= msg
->bits_per_long
;
3201 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3202 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3203 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3204 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3205 lta
->long_alignment
= msg
->long_alignment
;
3206 lta
->byte_order
= msg
->byte_order
;
3208 lta
->v_major
= msg
->major
;
3209 lta
->v_minor
= msg
->minor
;
3210 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3211 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3212 lta
->notify_sock
= -1;
3214 /* Copy name and make sure it's NULL terminated. */
3215 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3216 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3219 * Before this can be called, when receiving the registration information,
3220 * the application compatibility is checked. So, at this point, the
3221 * application can work with this session daemon.
3223 lta
->compatible
= 1;
3225 lta
->pid
= msg
->pid
;
3226 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3228 pthread_mutex_init(<a
->sock_lock
, NULL
);
3229 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3231 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3237 * For a given application object, add it to every hash table.
3239 void ust_app_add(struct ust_app
*app
)
3242 assert(app
->notify_sock
>= 0);
3247 * On a re-registration, we want to kick out the previous registration of
3250 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3253 * The socket _should_ be unique until _we_ call close. So, a add_unique
3254 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3255 * already in the table.
3257 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3259 /* Add application to the notify socket hash table. */
3260 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3261 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3263 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3264 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3265 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3272 * Set the application version into the object.
3274 * Return 0 on success else a negative value either an errno code or a
3275 * LTTng-UST error code.
3277 int ust_app_version(struct ust_app
*app
)
3283 pthread_mutex_lock(&app
->sock_lock
);
3284 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3285 pthread_mutex_unlock(&app
->sock_lock
);
3287 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3288 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3290 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3298 * Unregister app by removing it from the global traceable app list and freeing
3301 * The socket is already closed at this point so no close to sock.
3303 void ust_app_unregister(int sock
)
3305 struct ust_app
*lta
;
3306 struct lttng_ht_node_ulong
*node
;
3307 struct lttng_ht_iter ust_app_sock_iter
;
3308 struct lttng_ht_iter iter
;
3309 struct ust_app_session
*ua_sess
;
3314 /* Get the node reference for a call_rcu */
3315 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3316 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3319 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3320 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3323 * For per-PID buffers, perform "push metadata" and flush all
3324 * application streams before removing app from hash tables,
3325 * ensuring proper behavior of data_pending check.
3326 * Remove sessions so they are not visible during deletion.
3328 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3330 struct ust_registry_session
*registry
;
3332 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3334 /* The session was already removed so scheduled for teardown. */
3338 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3339 (void) ust_app_flush_app_session(lta
, ua_sess
);
3343 * Add session to list for teardown. This is safe since at this point we
3344 * are the only one using this list.
3346 pthread_mutex_lock(&ua_sess
->lock
);
3348 if (ua_sess
->deleted
) {
3349 pthread_mutex_unlock(&ua_sess
->lock
);
3354 * Normally, this is done in the delete session process which is
3355 * executed in the call rcu below. However, upon registration we can't
3356 * afford to wait for the grace period before pushing data or else the
3357 * data pending feature can race between the unregistration and stop
3358 * command where the data pending command is sent *before* the grace
3361 * The close metadata below nullifies the metadata pointer in the
3362 * session so the delete session will NOT push/close a second time.
3364 registry
= get_session_registry(ua_sess
);
3366 /* Push metadata for application before freeing the application. */
3367 (void) push_metadata(registry
, ua_sess
->consumer
);
3370 * Don't ask to close metadata for global per UID buffers. Close
3371 * metadata only on destroy trace session in this case. Also, the
3372 * previous push metadata could have flag the metadata registry to
3373 * close so don't send a close command if closed.
3375 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3376 /* And ask to close it for this session registry. */
3377 (void) close_metadata(registry
, ua_sess
->consumer
);
3380 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3382 pthread_mutex_unlock(&ua_sess
->lock
);
3385 /* Remove application from PID hash table */
3386 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3390 * Remove application from notify hash table. The thread handling the
3391 * notify socket could have deleted the node so ignore on error because
3392 * either way it's valid. The close of that socket is handled by the other
3395 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3396 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3399 * Ignore return value since the node might have been removed before by an
3400 * add replace during app registration because the PID can be reassigned by
3403 iter
.iter
.node
= <a
->pid_n
.node
;
3404 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3406 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3411 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3418 * Fill events array with all events name of all registered apps.
3420 int ust_app_list_events(struct lttng_event
**events
)
3423 size_t nbmem
, count
= 0;
3424 struct lttng_ht_iter iter
;
3425 struct ust_app
*app
;
3426 struct lttng_event
*tmp_event
;
3428 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3429 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3430 if (tmp_event
== NULL
) {
3431 PERROR("zmalloc ust app events");
3438 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3439 struct lttng_ust_tracepoint_iter uiter
;
3441 health_code_update();
3443 if (!app
->compatible
) {
3445 * TODO: In time, we should notice the caller of this error by
3446 * telling him that this is a version error.
3450 pthread_mutex_lock(&app
->sock_lock
);
3451 handle
= ustctl_tracepoint_list(app
->sock
);
3453 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3454 ERR("UST app list events getting handle failed for app pid %d",
3457 pthread_mutex_unlock(&app
->sock_lock
);
3461 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3462 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3463 /* Handle ustctl error. */
3467 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3468 ERR("UST app tp list get failed for app %d with ret %d",
3471 DBG3("UST app tp list get failed. Application is dead");
3473 * This is normal behavior, an application can die during the
3474 * creation process. Don't report an error so the execution can
3475 * continue normally. Continue normal execution.
3480 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3481 if (release_ret
!= -LTTNG_UST_ERR_EXITING
&& release_ret
!= -EPIPE
) {
3482 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3484 pthread_mutex_unlock(&app
->sock_lock
);
3488 health_code_update();
3489 if (count
>= nbmem
) {
3490 /* In case the realloc fails, we free the memory */
3491 struct lttng_event
*new_tmp_event
;
3494 new_nbmem
= nbmem
<< 1;
3495 DBG2("Reallocating event list from %zu to %zu entries",
3497 new_tmp_event
= realloc(tmp_event
,
3498 new_nbmem
* sizeof(struct lttng_event
));
3499 if (new_tmp_event
== NULL
) {
3502 PERROR("realloc ust app events");
3505 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3506 if (release_ret
!= -LTTNG_UST_ERR_EXITING
&& release_ret
!= -EPIPE
) {
3507 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3509 pthread_mutex_unlock(&app
->sock_lock
);
3512 /* Zero the new memory */
3513 memset(new_tmp_event
+ nbmem
, 0,
3514 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3516 tmp_event
= new_tmp_event
;
3518 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3519 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3520 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3521 tmp_event
[count
].pid
= app
->pid
;
3522 tmp_event
[count
].enabled
= -1;
3525 ret
= ustctl_release_handle(app
->sock
, handle
);
3526 pthread_mutex_unlock(&app
->sock_lock
);
3527 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3528 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3533 *events
= tmp_event
;
3535 DBG2("UST app list events done (%zu events)", count
);
3540 health_code_update();
3545 * Fill events array with all events name of all registered apps.
3547 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3550 size_t nbmem
, count
= 0;
3551 struct lttng_ht_iter iter
;
3552 struct ust_app
*app
;
3553 struct lttng_event_field
*tmp_event
;
3555 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3556 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3557 if (tmp_event
== NULL
) {
3558 PERROR("zmalloc ust app event fields");
3565 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3566 struct lttng_ust_field_iter uiter
;
3568 health_code_update();
3570 if (!app
->compatible
) {
3572 * TODO: In time, we should notice the caller of this error by
3573 * telling him that this is a version error.
3577 pthread_mutex_lock(&app
->sock_lock
);
3578 handle
= ustctl_tracepoint_field_list(app
->sock
);
3580 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3581 ERR("UST app list field getting handle failed for app pid %d",
3584 pthread_mutex_unlock(&app
->sock_lock
);
3588 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3589 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3590 /* Handle ustctl error. */
3594 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3595 ERR("UST app tp list field failed for app %d with ret %d",
3598 DBG3("UST app tp list field failed. Application is dead");
3600 * This is normal behavior, an application can die during the
3601 * creation process. Don't report an error so the execution can
3602 * continue normally. Reset list and count for next app.
3607 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3608 pthread_mutex_unlock(&app
->sock_lock
);
3609 if (release_ret
!= -LTTNG_UST_ERR_EXITING
&& release_ret
!= -EPIPE
) {
3610 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3615 health_code_update();
3616 if (count
>= nbmem
) {
3617 /* In case the realloc fails, we free the memory */
3618 struct lttng_event_field
*new_tmp_event
;
3621 new_nbmem
= nbmem
<< 1;
3622 DBG2("Reallocating event field list from %zu to %zu entries",
3624 new_tmp_event
= realloc(tmp_event
,
3625 new_nbmem
* sizeof(struct lttng_event_field
));
3626 if (new_tmp_event
== NULL
) {
3629 PERROR("realloc ust app event fields");
3632 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3633 pthread_mutex_unlock(&app
->sock_lock
);
3634 if (release_ret
!= -LTTNG_UST_ERR_EXITING
&& release_ret
!= -EPIPE
) {
3635 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3639 /* Zero the new memory */
3640 memset(new_tmp_event
+ nbmem
, 0,
3641 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3643 tmp_event
= new_tmp_event
;
3646 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3647 /* Mapping between these enums matches 1 to 1. */
3648 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3649 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3651 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3652 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3653 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3654 tmp_event
[count
].event
.pid
= app
->pid
;
3655 tmp_event
[count
].event
.enabled
= -1;
3658 ret
= ustctl_release_handle(app
->sock
, handle
);
3659 pthread_mutex_unlock(&app
->sock_lock
);
3660 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3661 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3666 *fields
= tmp_event
;
3668 DBG2("UST app list event fields done (%zu events)", count
);
3673 health_code_update();
3678 * Free and clean all traceable apps of the global list.
3680 * Should _NOT_ be called with RCU read-side lock held.
3682 void ust_app_clean_list(void)
3685 struct ust_app
*app
;
3686 struct lttng_ht_iter iter
;
3688 DBG2("UST app cleaning registered apps hash table");
3693 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3694 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3696 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3700 /* Cleanup socket hash table */
3701 if (ust_app_ht_by_sock
) {
3702 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3704 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3709 /* Cleanup notify socket hash table */
3710 if (ust_app_ht_by_notify_sock
) {
3711 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3712 notify_sock_n
.node
) {
3713 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3719 /* Destroy is done only when the ht is empty */
3721 ht_cleanup_push(ust_app_ht
);
3723 if (ust_app_ht_by_sock
) {
3724 ht_cleanup_push(ust_app_ht_by_sock
);
3726 if (ust_app_ht_by_notify_sock
) {
3727 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3732 * Init UST app hash table.
3734 int ust_app_ht_alloc(void)
3736 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3740 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3741 if (!ust_app_ht_by_sock
) {
3744 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3745 if (!ust_app_ht_by_notify_sock
) {
3752 * For a specific UST session, disable the channel for all registered apps.
3754 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3755 struct ltt_ust_channel
*uchan
)
3758 struct lttng_ht_iter iter
;
3759 struct lttng_ht_node_str
*ua_chan_node
;
3760 struct ust_app
*app
;
3761 struct ust_app_session
*ua_sess
;
3762 struct ust_app_channel
*ua_chan
;
3764 if (usess
== NULL
|| uchan
== NULL
) {
3765 ERR("Disabling UST global channel with NULL values");
3770 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3771 uchan
->name
, usess
->id
);
3775 /* For every registered applications */
3776 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3777 struct lttng_ht_iter uiter
;
3778 if (!app
->compatible
) {
3780 * TODO: In time, we should notice the caller of this error by
3781 * telling him that this is a version error.
3785 ua_sess
= lookup_session_by_app(usess
, app
);
3786 if (ua_sess
== NULL
) {
3791 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3792 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3793 /* If the session if found for the app, the channel must be there */
3794 assert(ua_chan_node
);
3796 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3797 /* The channel must not be already disabled */
3798 assert(ua_chan
->enabled
== 1);
3800 /* Disable channel onto application */
3801 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3803 /* XXX: We might want to report this error at some point... */
3815 * For a specific UST session, enable the channel for all registered apps.
3817 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3818 struct ltt_ust_channel
*uchan
)
3821 struct lttng_ht_iter iter
;
3822 struct ust_app
*app
;
3823 struct ust_app_session
*ua_sess
;
3825 if (usess
== NULL
|| uchan
== NULL
) {
3826 ERR("Adding UST global channel to NULL values");
3831 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3832 uchan
->name
, usess
->id
);
3836 /* For every registered applications */
3837 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3838 if (!app
->compatible
) {
3840 * TODO: In time, we should notice the caller of this error by
3841 * telling him that this is a version error.
3845 ua_sess
= lookup_session_by_app(usess
, app
);
3846 if (ua_sess
== NULL
) {
3850 /* Enable channel onto application */
3851 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3853 /* XXX: We might want to report this error at some point... */
3865 * Disable an event in a channel and for a specific session.
3867 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3868 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3871 struct lttng_ht_iter iter
, uiter
;
3872 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3873 struct ust_app
*app
;
3874 struct ust_app_session
*ua_sess
;
3875 struct ust_app_channel
*ua_chan
;
3876 struct ust_app_event
*ua_event
;
3878 DBG("UST app disabling event %s for all apps in channel "
3879 "%s for session id %" PRIu64
,
3880 uevent
->attr
.name
, uchan
->name
, usess
->id
);
3884 /* For all registered applications */
3885 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3886 if (!app
->compatible
) {
3888 * TODO: In time, we should notice the caller of this error by
3889 * telling him that this is a version error.
3893 ua_sess
= lookup_session_by_app(usess
, app
);
3894 if (ua_sess
== NULL
) {
3899 /* Lookup channel in the ust app session */
3900 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3901 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3902 if (ua_chan_node
== NULL
) {
3903 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
3904 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
3907 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3909 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
3910 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
3911 if (ua_event_node
== NULL
) {
3912 DBG2("Event %s not found in channel %s for app pid %d."
3913 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
3916 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3918 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3920 /* XXX: Report error someday... */
3931 * For a specific UST session, create the channel for all registered apps.
3933 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
3934 struct ltt_ust_channel
*uchan
)
3936 int ret
= 0, created
;
3937 struct lttng_ht_iter iter
;
3938 struct ust_app
*app
;
3939 struct ust_app_session
*ua_sess
= NULL
;
3941 /* Very wrong code flow */
3945 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
3946 uchan
->name
, usess
->id
);
3950 /* For every registered applications */
3951 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3952 if (!app
->compatible
) {
3954 * TODO: In time, we should notice the caller of this error by
3955 * telling him that this is a version error.
3959 if (!trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
3965 * Create session on the tracer side and add it to app session HT. Note
3966 * that if session exist, it will simply return a pointer to the ust
3969 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
3974 * The application's socket is not valid. Either a bad socket
3975 * or a timeout on it. We can't inform the caller that for a
3976 * specific app, the session failed so lets continue here.
3978 ret
= 0; /* Not an error. */
3982 goto error_rcu_unlock
;
3987 pthread_mutex_lock(&ua_sess
->lock
);
3989 if (ua_sess
->deleted
) {
3990 pthread_mutex_unlock(&ua_sess
->lock
);
3994 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
3995 sizeof(uchan
->name
))) {
3996 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
3999 /* Create channel onto application. We don't need the chan ref. */
4000 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
4001 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
4003 pthread_mutex_unlock(&ua_sess
->lock
);
4005 /* Cleanup the created session if it's the case. */
4007 destroy_app_session(app
, ua_sess
);
4012 * The application's socket is not valid. Either a bad socket
4013 * or a timeout on it. We can't inform the caller that for a
4014 * specific app, the session failed so lets continue here.
4016 ret
= 0; /* Not an error. */
4020 goto error_rcu_unlock
;
4031 * Enable event for a specific session and channel on the tracer.
4033 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4034 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4037 struct lttng_ht_iter iter
, uiter
;
4038 struct lttng_ht_node_str
*ua_chan_node
;
4039 struct ust_app
*app
;
4040 struct ust_app_session
*ua_sess
;
4041 struct ust_app_channel
*ua_chan
;
4042 struct ust_app_event
*ua_event
;
4044 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4045 uevent
->attr
.name
, usess
->id
);
4048 * NOTE: At this point, this function is called only if the session and
4049 * channel passed are already created for all apps. and enabled on the
4055 /* For all registered applications */
4056 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4057 if (!app
->compatible
) {
4059 * TODO: In time, we should notice the caller of this error by
4060 * telling him that this is a version error.
4064 ua_sess
= lookup_session_by_app(usess
, app
);
4066 /* The application has problem or is probably dead. */
4070 pthread_mutex_lock(&ua_sess
->lock
);
4072 if (ua_sess
->deleted
) {
4073 pthread_mutex_unlock(&ua_sess
->lock
);
4077 /* Lookup channel in the ust app session */
4078 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4079 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4081 * It is possible that the channel cannot be found is
4082 * the channel/event creation occurs concurrently with
4083 * an application exit.
4085 if (!ua_chan_node
) {
4086 pthread_mutex_unlock(&ua_sess
->lock
);
4090 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4092 /* Get event node */
4093 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4094 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4095 if (ua_event
== NULL
) {
4096 DBG3("UST app enable event %s not found for app PID %d."
4097 "Skipping app", uevent
->attr
.name
, app
->pid
);
4101 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4103 pthread_mutex_unlock(&ua_sess
->lock
);
4107 pthread_mutex_unlock(&ua_sess
->lock
);
4116 * For a specific existing UST session and UST channel, creates the event for
4117 * all registered apps.
4119 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4120 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4123 struct lttng_ht_iter iter
, uiter
;
4124 struct lttng_ht_node_str
*ua_chan_node
;
4125 struct ust_app
*app
;
4126 struct ust_app_session
*ua_sess
;
4127 struct ust_app_channel
*ua_chan
;
4129 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4130 uevent
->attr
.name
, usess
->id
);
4134 /* For all registered applications */
4135 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4136 if (!app
->compatible
) {
4138 * TODO: In time, we should notice the caller of this error by
4139 * telling him that this is a version error.
4143 ua_sess
= lookup_session_by_app(usess
, app
);
4145 /* The application has problem or is probably dead. */
4149 pthread_mutex_lock(&ua_sess
->lock
);
4151 if (ua_sess
->deleted
) {
4152 pthread_mutex_unlock(&ua_sess
->lock
);
4156 /* Lookup channel in the ust app session */
4157 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4158 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4159 /* If the channel is not found, there is a code flow error */
4160 assert(ua_chan_node
);
4162 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4164 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4165 pthread_mutex_unlock(&ua_sess
->lock
);
4167 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4168 /* Possible value at this point: -ENOMEM. If so, we stop! */
4171 DBG2("UST app event %s already exist on app PID %d",
4172 uevent
->attr
.name
, app
->pid
);
4183 * Start tracing for a specific UST session and app.
4186 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4189 struct ust_app_session
*ua_sess
;
4191 DBG("Starting tracing for ust app pid %d", app
->pid
);
4195 if (!app
->compatible
) {
4199 ua_sess
= lookup_session_by_app(usess
, app
);
4200 if (ua_sess
== NULL
) {
4201 /* The session is in teardown process. Ignore and continue. */
4205 pthread_mutex_lock(&ua_sess
->lock
);
4207 if (ua_sess
->deleted
) {
4208 pthread_mutex_unlock(&ua_sess
->lock
);
4212 /* Upon restart, we skip the setup, already done */
4213 if (ua_sess
->started
) {
4217 /* Create directories if consumer is LOCAL and has a path defined. */
4218 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
4219 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
4220 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
4221 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
4223 if (errno
!= EEXIST
) {
4224 ERR("Trace directory creation error");
4231 * Create the metadata for the application. This returns gracefully if a
4232 * metadata was already set for the session.
4234 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4239 health_code_update();
4242 /* This start the UST tracing */
4243 pthread_mutex_lock(&app
->sock_lock
);
4244 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4245 pthread_mutex_unlock(&app
->sock_lock
);
4247 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4248 ERR("Error starting tracing for app pid: %d (ret: %d)",
4251 DBG("UST app start session failed. Application is dead.");
4253 * This is normal behavior, an application can die during the
4254 * creation process. Don't report an error so the execution can
4255 * continue normally.
4257 pthread_mutex_unlock(&ua_sess
->lock
);
4263 /* Indicate that the session has been started once */
4264 ua_sess
->started
= 1;
4266 pthread_mutex_unlock(&ua_sess
->lock
);
4268 health_code_update();
4270 /* Quiescent wait after starting trace */
4271 pthread_mutex_lock(&app
->sock_lock
);
4272 ret
= ustctl_wait_quiescent(app
->sock
);
4273 pthread_mutex_unlock(&app
->sock_lock
);
4274 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4275 ERR("UST app wait quiescent failed for app pid %d ret %d",
4281 health_code_update();
4285 pthread_mutex_unlock(&ua_sess
->lock
);
4287 health_code_update();
4292 * Stop tracing for a specific UST session and app.
4295 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4298 struct ust_app_session
*ua_sess
;
4299 struct ust_registry_session
*registry
;
4301 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4305 if (!app
->compatible
) {
4306 goto end_no_session
;
4309 ua_sess
= lookup_session_by_app(usess
, app
);
4310 if (ua_sess
== NULL
) {
4311 goto end_no_session
;
4314 pthread_mutex_lock(&ua_sess
->lock
);
4316 if (ua_sess
->deleted
) {
4317 pthread_mutex_unlock(&ua_sess
->lock
);
4318 goto end_no_session
;
4322 * If started = 0, it means that stop trace has been called for a session
4323 * that was never started. It's possible since we can have a fail start
4324 * from either the application manager thread or the command thread. Simply
4325 * indicate that this is a stop error.
4327 if (!ua_sess
->started
) {
4328 goto error_rcu_unlock
;
4331 health_code_update();
4333 /* This inhibits UST tracing */
4334 pthread_mutex_lock(&app
->sock_lock
);
4335 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4336 pthread_mutex_unlock(&app
->sock_lock
);
4338 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4339 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4342 DBG("UST app stop session failed. Application is dead.");
4344 * This is normal behavior, an application can die during the
4345 * creation process. Don't report an error so the execution can
4346 * continue normally.
4350 goto error_rcu_unlock
;
4353 health_code_update();
4355 /* Quiescent wait after stopping trace */
4356 pthread_mutex_lock(&app
->sock_lock
);
4357 ret
= ustctl_wait_quiescent(app
->sock
);
4358 pthread_mutex_unlock(&app
->sock_lock
);
4359 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4360 ERR("UST app wait quiescent failed for app pid %d ret %d",
4364 health_code_update();
4366 registry
= get_session_registry(ua_sess
);
4369 /* Push metadata for application before freeing the application. */
4370 (void) push_metadata(registry
, ua_sess
->consumer
);
4373 pthread_mutex_unlock(&ua_sess
->lock
);
4376 health_code_update();
4380 pthread_mutex_unlock(&ua_sess
->lock
);
4382 health_code_update();
4387 int ust_app_flush_app_session(struct ust_app
*app
,
4388 struct ust_app_session
*ua_sess
)
4390 int ret
, retval
= 0;
4391 struct lttng_ht_iter iter
;
4392 struct ust_app_channel
*ua_chan
;
4393 struct consumer_socket
*socket
;
4395 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4399 if (!app
->compatible
) {
4400 goto end_not_compatible
;
4403 pthread_mutex_lock(&ua_sess
->lock
);
4405 if (ua_sess
->deleted
) {
4409 health_code_update();
4411 /* Flushing buffers */
4412 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4415 /* Flush buffers and push metadata. */
4416 switch (ua_sess
->buffer_type
) {
4417 case LTTNG_BUFFER_PER_PID
:
4418 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4420 health_code_update();
4421 assert(ua_chan
->is_sent
);
4422 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4424 ERR("Error flushing consumer channel");
4430 case LTTNG_BUFFER_PER_UID
:
4436 health_code_update();
4439 pthread_mutex_unlock(&ua_sess
->lock
);
4443 health_code_update();
4448 * Flush buffers for all applications for a specific UST session.
4449 * Called with UST session lock held.
4452 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4457 DBG("Flushing session buffers for all ust apps");
4461 /* Flush buffers and push metadata. */
4462 switch (usess
->buffer_type
) {
4463 case LTTNG_BUFFER_PER_UID
:
4465 struct buffer_reg_uid
*reg
;
4466 struct lttng_ht_iter iter
;
4468 /* Flush all per UID buffers associated to that session. */
4469 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4470 struct ust_registry_session
*ust_session_reg
;
4471 struct buffer_reg_channel
*reg_chan
;
4472 struct consumer_socket
*socket
;
4474 /* Get consumer socket to use to push the metadata.*/
4475 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4478 /* Ignore request if no consumer is found for the session. */
4482 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4483 reg_chan
, node
.node
) {
4485 * The following call will print error values so the return
4486 * code is of little importance because whatever happens, we
4487 * have to try them all.
4489 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4492 ust_session_reg
= reg
->registry
->reg
.ust
;
4493 /* Push metadata. */
4494 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4498 case LTTNG_BUFFER_PER_PID
:
4500 struct ust_app_session
*ua_sess
;
4501 struct lttng_ht_iter iter
;
4502 struct ust_app
*app
;
4504 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4505 ua_sess
= lookup_session_by_app(usess
, app
);
4506 if (ua_sess
== NULL
) {
4509 (void) ust_app_flush_app_session(app
, ua_sess
);
4520 health_code_update();
4525 * Destroy a specific UST session in apps.
4527 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4530 struct ust_app_session
*ua_sess
;
4531 struct lttng_ht_iter iter
;
4532 struct lttng_ht_node_u64
*node
;
4534 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4538 if (!app
->compatible
) {
4542 __lookup_session_by_app(usess
, app
, &iter
);
4543 node
= lttng_ht_iter_get_node_u64(&iter
);
4545 /* Session is being or is deleted. */
4548 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4550 health_code_update();
4551 destroy_app_session(app
, ua_sess
);
4553 health_code_update();
4555 /* Quiescent wait after stopping trace */
4556 pthread_mutex_lock(&app
->sock_lock
);
4557 ret
= ustctl_wait_quiescent(app
->sock
);
4558 pthread_mutex_unlock(&app
->sock_lock
);
4559 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4560 ERR("UST app wait quiescent failed for app pid %d ret %d",
4565 health_code_update();
4570 * Start tracing for the UST session.
4572 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4575 struct lttng_ht_iter iter
;
4576 struct ust_app
*app
;
4578 DBG("Starting all UST traces");
4582 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4583 ret
= ust_app_start_trace(usess
, app
);
4585 /* Continue to next apps even on error */
4596 * Start tracing for the UST session.
4597 * Called with UST session lock held.
4599 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4602 struct lttng_ht_iter iter
;
4603 struct ust_app
*app
;
4605 DBG("Stopping all UST traces");
4609 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4610 ret
= ust_app_stop_trace(usess
, app
);
4612 /* Continue to next apps even on error */
4617 (void) ust_app_flush_session(usess
);
4625 * Destroy app UST session.
4627 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4630 struct lttng_ht_iter iter
;
4631 struct ust_app
*app
;
4633 DBG("Destroy all UST traces");
4637 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4638 ret
= destroy_trace(usess
, app
);
4640 /* Continue to next apps even on error */
4651 void ust_app_global_create(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4654 struct lttng_ht_iter iter
, uiter
;
4655 struct ust_app_session
*ua_sess
= NULL
;
4656 struct ust_app_channel
*ua_chan
;
4657 struct ust_app_event
*ua_event
;
4658 struct ust_app_ctx
*ua_ctx
;
4661 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &is_created
);
4663 /* Tracer is probably gone or ENOMEM. */
4667 /* App session already created. */
4672 pthread_mutex_lock(&ua_sess
->lock
);
4674 if (ua_sess
->deleted
) {
4675 pthread_mutex_unlock(&ua_sess
->lock
);
4680 * We can iterate safely here over all UST app session since the create ust
4681 * app session above made a shadow copy of the UST global domain from the
4684 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4686 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4687 if (ret
< 0 && ret
!= -ENOTCONN
) {
4689 * Stop everything. On error, the application
4690 * failed, no more file descriptor are available
4691 * or ENOMEM so stopping here is the only thing
4692 * we can do for now. The only exception is
4693 * -ENOTCONN, which indicates that the application
4700 * Add context using the list so they are enabled in the same order the
4703 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
4704 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
4711 /* For each events */
4712 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
4714 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
4721 pthread_mutex_unlock(&ua_sess
->lock
);
4723 if (usess
->active
) {
4724 ret
= ust_app_start_trace(usess
, app
);
4729 DBG2("UST trace started for app pid %d", app
->pid
);
4732 /* Everything went well at this point. */
4736 pthread_mutex_unlock(&ua_sess
->lock
);
4739 destroy_app_session(app
, ua_sess
);
4745 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4747 struct ust_app_session
*ua_sess
;
4749 ua_sess
= lookup_session_by_app(usess
, app
);
4750 if (ua_sess
== NULL
) {
4753 destroy_app_session(app
, ua_sess
);
4757 * Add channels/events from UST global domain to registered apps at sock.
4759 * Called with session lock held.
4760 * Called with RCU read-side lock held.
4762 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4766 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
4767 app
->sock
, usess
->id
);
4769 if (!app
->compatible
) {
4773 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4774 ust_app_global_create(usess
, app
);
4776 ust_app_global_destroy(usess
, app
);
4781 * Called with session lock held.
4783 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
4785 struct lttng_ht_iter iter
;
4786 struct ust_app
*app
;
4789 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4790 ust_app_global_update(usess
, app
);
4796 * Add context to a specific channel for global UST domain.
4798 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
4799 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
4802 struct lttng_ht_node_str
*ua_chan_node
;
4803 struct lttng_ht_iter iter
, uiter
;
4804 struct ust_app_channel
*ua_chan
= NULL
;
4805 struct ust_app_session
*ua_sess
;
4806 struct ust_app
*app
;
4810 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4811 if (!app
->compatible
) {
4813 * TODO: In time, we should notice the caller of this error by
4814 * telling him that this is a version error.
4818 ua_sess
= lookup_session_by_app(usess
, app
);
4819 if (ua_sess
== NULL
) {
4823 pthread_mutex_lock(&ua_sess
->lock
);
4825 if (ua_sess
->deleted
) {
4826 pthread_mutex_unlock(&ua_sess
->lock
);
4830 /* Lookup channel in the ust app session */
4831 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4832 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4833 if (ua_chan_node
== NULL
) {
4836 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4838 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4843 pthread_mutex_unlock(&ua_sess
->lock
);
4851 * Enable event for a channel from a UST session for a specific PID.
4853 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4854 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4857 struct lttng_ht_iter iter
;
4858 struct lttng_ht_node_str
*ua_chan_node
;
4859 struct ust_app
*app
;
4860 struct ust_app_session
*ua_sess
;
4861 struct ust_app_channel
*ua_chan
;
4862 struct ust_app_event
*ua_event
;
4864 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4868 app
= ust_app_find_by_pid(pid
);
4870 ERR("UST app enable event per PID %d not found", pid
);
4875 if (!app
->compatible
) {
4880 ua_sess
= lookup_session_by_app(usess
, app
);
4882 /* The application has problem or is probably dead. */
4887 pthread_mutex_lock(&ua_sess
->lock
);
4889 if (ua_sess
->deleted
) {
4894 /* Lookup channel in the ust app session */
4895 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4896 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4897 /* If the channel is not found, there is a code flow error */
4898 assert(ua_chan_node
);
4900 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4902 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4903 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4904 if (ua_event
== NULL
) {
4905 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4910 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4917 pthread_mutex_unlock(&ua_sess
->lock
);
4924 * Calibrate registered applications.
4926 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
4929 struct lttng_ht_iter iter
;
4930 struct ust_app
*app
;
4934 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4935 if (!app
->compatible
) {
4937 * TODO: In time, we should notice the caller of this error by
4938 * telling him that this is a version error.
4943 health_code_update();
4945 pthread_mutex_lock(&app
->sock_lock
);
4946 ret
= ustctl_calibrate(app
->sock
, calibrate
);
4947 pthread_mutex_unlock(&app
->sock_lock
);
4951 /* Means that it's not implemented on the tracer side. */
4955 DBG2("Calibrate app PID %d returned with error %d",
4962 DBG("UST app global domain calibration finished");
4966 health_code_update();
4972 * Receive registration and populate the given msg structure.
4974 * On success return 0 else a negative value returned by the ustctl call.
4976 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
4979 uint32_t pid
, ppid
, uid
, gid
;
4983 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
4984 &pid
, &ppid
, &uid
, &gid
,
4985 &msg
->bits_per_long
,
4986 &msg
->uint8_t_alignment
,
4987 &msg
->uint16_t_alignment
,
4988 &msg
->uint32_t_alignment
,
4989 &msg
->uint64_t_alignment
,
4990 &msg
->long_alignment
,
4997 case LTTNG_UST_ERR_EXITING
:
4998 DBG3("UST app recv reg message failed. Application died");
5000 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5001 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5002 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5003 LTTNG_UST_ABI_MINOR_VERSION
);
5006 ERR("UST app recv reg message failed with ret %d", ret
);
5011 msg
->pid
= (pid_t
) pid
;
5012 msg
->ppid
= (pid_t
) ppid
;
5013 msg
->uid
= (uid_t
) uid
;
5014 msg
->gid
= (gid_t
) gid
;
5021 * Return a ust app channel object using the application object and the channel
5022 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5023 * lock MUST be acquired before calling this function.
5025 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5028 struct lttng_ht_node_ulong
*node
;
5029 struct lttng_ht_iter iter
;
5030 struct ust_app_channel
*ua_chan
= NULL
;
5034 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5035 node
= lttng_ht_iter_get_node_ulong(&iter
);
5037 DBG2("UST app channel find by objd %d not found", objd
);
5041 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5048 * Reply to a register channel notification from an application on the notify
5049 * socket. The channel metadata is also created.
5051 * The session UST registry lock is acquired in this function.
5053 * On success 0 is returned else a negative value.
5055 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
5056 size_t nr_fields
, struct ustctl_field
*fields
)
5058 int ret
, ret_code
= 0;
5059 uint32_t chan_id
, reg_count
;
5060 uint64_t chan_reg_key
;
5061 enum ustctl_channel_header type
;
5062 struct ust_app
*app
;
5063 struct ust_app_channel
*ua_chan
;
5064 struct ust_app_session
*ua_sess
;
5065 struct ust_registry_session
*registry
;
5066 struct ust_registry_channel
*chan_reg
;
5070 /* Lookup application. If not found, there is a code flow error. */
5071 app
= find_app_by_notify_sock(sock
);
5073 DBG("Application socket %d is being teardown. Abort event notify",
5077 goto error_rcu_unlock
;
5080 /* Lookup channel by UST object descriptor. */
5081 ua_chan
= find_channel_by_objd(app
, cobjd
);
5083 DBG("Application channel is being teardown. Abort event notify");
5086 goto error_rcu_unlock
;
5089 assert(ua_chan
->session
);
5090 ua_sess
= ua_chan
->session
;
5092 /* Get right session registry depending on the session buffer type. */
5093 registry
= get_session_registry(ua_sess
);
5096 /* Depending on the buffer type, a different channel key is used. */
5097 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5098 chan_reg_key
= ua_chan
->tracing_channel_id
;
5100 chan_reg_key
= ua_chan
->key
;
5103 pthread_mutex_lock(®istry
->lock
);
5105 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5108 if (!chan_reg
->register_done
) {
5109 reg_count
= ust_registry_get_event_count(chan_reg
);
5110 if (reg_count
< 31) {
5111 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
5113 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5116 chan_reg
->nr_ctx_fields
= nr_fields
;
5117 chan_reg
->ctx_fields
= fields
;
5118 chan_reg
->header_type
= type
;
5120 /* Get current already assigned values. */
5121 type
= chan_reg
->header_type
;
5123 /* Set to NULL so the error path does not do a double free. */
5126 /* Channel id is set during the object creation. */
5127 chan_id
= chan_reg
->chan_id
;
5129 /* Append to metadata */
5130 if (!chan_reg
->metadata_dumped
) {
5131 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5133 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5139 DBG3("UST app replying to register channel key %" PRIu64
5140 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5143 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5145 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5146 ERR("UST app reply channel failed with ret %d", ret
);
5148 DBG3("UST app reply channel failed. Application died");
5153 /* This channel registry registration is completed. */
5154 chan_reg
->register_done
= 1;
5157 pthread_mutex_unlock(®istry
->lock
);
5167 * Add event to the UST channel registry. When the event is added to the
5168 * registry, the metadata is also created. Once done, this replies to the
5169 * application with the appropriate error code.
5171 * The session UST registry lock is acquired in the function.
5173 * On success 0 is returned else a negative value.
5175 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5176 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
5177 char *model_emf_uri
)
5180 uint32_t event_id
= 0;
5181 uint64_t chan_reg_key
;
5182 struct ust_app
*app
;
5183 struct ust_app_channel
*ua_chan
;
5184 struct ust_app_session
*ua_sess
;
5185 struct ust_registry_session
*registry
;
5189 /* Lookup application. If not found, there is a code flow error. */
5190 app
= find_app_by_notify_sock(sock
);
5192 DBG("Application socket %d is being teardown. Abort event notify",
5197 free(model_emf_uri
);
5198 goto error_rcu_unlock
;
5201 /* Lookup channel by UST object descriptor. */
5202 ua_chan
= find_channel_by_objd(app
, cobjd
);
5204 DBG("Application channel is being teardown. Abort event notify");
5208 free(model_emf_uri
);
5209 goto error_rcu_unlock
;
5212 assert(ua_chan
->session
);
5213 ua_sess
= ua_chan
->session
;
5215 registry
= get_session_registry(ua_sess
);
5218 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5219 chan_reg_key
= ua_chan
->tracing_channel_id
;
5221 chan_reg_key
= ua_chan
->key
;
5224 pthread_mutex_lock(®istry
->lock
);
5227 * From this point on, this call acquires the ownership of the sig, fields
5228 * and model_emf_uri meaning any free are done inside it if needed. These
5229 * three variables MUST NOT be read/write after this.
5231 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5232 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
, loglevel
,
5233 model_emf_uri
, ua_sess
->buffer_type
, &event_id
,
5237 * The return value is returned to ustctl so in case of an error, the
5238 * application can be notified. In case of an error, it's important not to
5239 * return a negative error or else the application will get closed.
5241 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5243 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5244 ERR("UST app reply event failed with ret %d", ret
);
5246 DBG3("UST app reply event failed. Application died");
5249 * No need to wipe the create event since the application socket will
5250 * get close on error hence cleaning up everything by itself.
5255 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5259 pthread_mutex_unlock(®istry
->lock
);
5266 * Handle application notification through the given notify socket.
5268 * Return 0 on success or else a negative value.
5270 int ust_app_recv_notify(int sock
)
5273 enum ustctl_notify_cmd cmd
;
5275 DBG3("UST app receiving notify from sock %d", sock
);
5277 ret
= ustctl_recv_notify(sock
, &cmd
);
5279 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5280 ERR("UST app recv notify failed with ret %d", ret
);
5282 DBG3("UST app recv notify failed. Application died");
5288 case USTCTL_NOTIFY_CMD_EVENT
:
5290 int sobjd
, cobjd
, loglevel
;
5291 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5293 struct ustctl_field
*fields
;
5295 DBG2("UST app ustctl register event received");
5297 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
5298 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
5300 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5301 ERR("UST app recv event failed with ret %d", ret
);
5303 DBG3("UST app recv event failed. Application died");
5309 * Add event to the UST registry coming from the notify socket. This
5310 * call will free if needed the sig, fields and model_emf_uri. This
5311 * code path loses the ownsership of these variables and transfer them
5312 * to the this function.
5314 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5315 fields
, loglevel
, model_emf_uri
);
5322 case USTCTL_NOTIFY_CMD_CHANNEL
:
5326 struct ustctl_field
*fields
;
5328 DBG2("UST app ustctl register channel received");
5330 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5333 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5334 ERR("UST app recv channel failed with ret %d", ret
);
5336 DBG3("UST app recv channel failed. Application died");
5342 * The fields ownership are transfered to this function call meaning
5343 * that if needed it will be freed. After this, it's invalid to access
5344 * fields or clean it up.
5346 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
5355 /* Should NEVER happen. */
5364 * Once the notify socket hangs up, this is called. First, it tries to find the
5365 * corresponding application. On failure, the call_rcu to close the socket is
5366 * executed. If an application is found, it tries to delete it from the notify
5367 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5369 * Note that an object needs to be allocated here so on ENOMEM failure, the
5370 * call RCU is not done but the rest of the cleanup is.
5372 void ust_app_notify_sock_unregister(int sock
)
5375 struct lttng_ht_iter iter
;
5376 struct ust_app
*app
;
5377 struct ust_app_notify_sock_obj
*obj
;
5383 obj
= zmalloc(sizeof(*obj
));
5386 * An ENOMEM is kind of uncool. If this strikes we continue the
5387 * procedure but the call_rcu will not be called. In this case, we
5388 * accept the fd leak rather than possibly creating an unsynchronized
5389 * state between threads.
5391 * TODO: The notify object should be created once the notify socket is
5392 * registered and stored independantely from the ust app object. The
5393 * tricky part is to synchronize the teardown of the application and
5394 * this notify object. Let's keep that in mind so we can avoid this
5395 * kind of shenanigans with ENOMEM in the teardown path.
5402 DBG("UST app notify socket unregister %d", sock
);
5405 * Lookup application by notify socket. If this fails, this means that the
5406 * hash table delete has already been done by the application
5407 * unregistration process so we can safely close the notify socket in a
5410 app
= find_app_by_notify_sock(sock
);
5415 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5418 * Whatever happens here either we fail or succeed, in both cases we have
5419 * to close the socket after a grace period to continue to the call RCU
5420 * here. If the deletion is successful, the application is not visible
5421 * anymore by other threads and is it fails it means that it was already
5422 * deleted from the hash table so either way we just have to close the
5425 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5431 * Close socket after a grace period to avoid for the socket to be reused
5432 * before the application object is freed creating potential race between
5433 * threads trying to add unique in the global hash table.
5436 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5441 * Destroy a ust app data structure and free its memory.
5443 void ust_app_destroy(struct ust_app
*app
)
5449 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5453 * Take a snapshot for a given UST session. The snapshot is sent to the given
5456 * Return 0 on success or else a negative value.
5458 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5459 struct snapshot_output
*output
, int wait
,
5460 uint64_t nb_packets_per_stream
)
5463 unsigned int snapshot_done
= 0;
5464 struct lttng_ht_iter iter
;
5465 struct ust_app
*app
;
5466 char pathname
[PATH_MAX
];
5473 switch (usess
->buffer_type
) {
5474 case LTTNG_BUFFER_PER_UID
:
5476 struct buffer_reg_uid
*reg
;
5478 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5479 struct buffer_reg_channel
*reg_chan
;
5480 struct consumer_socket
*socket
;
5482 /* Get consumer socket to use to push the metadata.*/
5483 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5490 memset(pathname
, 0, sizeof(pathname
));
5491 ret
= snprintf(pathname
, sizeof(pathname
),
5492 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5493 reg
->uid
, reg
->bits_per_long
);
5495 PERROR("snprintf snapshot path");
5499 /* Add the UST default trace dir to path. */
5500 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5501 reg_chan
, node
.node
) {
5502 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5503 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5504 nb_packets_per_stream
);
5509 ret
= consumer_snapshot_channel(socket
,
5510 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5511 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
5519 case LTTNG_BUFFER_PER_PID
:
5521 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5522 struct consumer_socket
*socket
;
5523 struct lttng_ht_iter chan_iter
;
5524 struct ust_app_channel
*ua_chan
;
5525 struct ust_app_session
*ua_sess
;
5526 struct ust_registry_session
*registry
;
5528 ua_sess
= lookup_session_by_app(usess
, app
);
5530 /* Session not associated with this app. */
5534 /* Get the right consumer socket for the application. */
5535 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5542 /* Add the UST default trace dir to path. */
5543 memset(pathname
, 0, sizeof(pathname
));
5544 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5547 PERROR("snprintf snapshot path");
5551 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5552 ua_chan
, node
.node
) {
5553 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
5554 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
5555 nb_packets_per_stream
);
5561 registry
= get_session_registry(ua_sess
);
5563 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
5564 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
5577 if (!snapshot_done
) {
5579 * If no snapshot was made and we are not in the error path, this means
5580 * that there are no buffers thus no (prior) application to snapshot
5581 * data from so we have simply NO data.
5592 * Return the size taken by one more packet per stream.
5594 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
5595 uint64_t cur_nr_packets
)
5597 uint64_t tot_size
= 0;
5598 struct ust_app
*app
;
5599 struct lttng_ht_iter iter
;
5603 switch (usess
->buffer_type
) {
5604 case LTTNG_BUFFER_PER_UID
:
5606 struct buffer_reg_uid
*reg
;
5608 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5609 struct buffer_reg_channel
*reg_chan
;
5612 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5613 reg_chan
, node
.node
) {
5614 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
5616 * Don't take channel into account if we
5617 * already grab all its packets.
5621 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
5627 case LTTNG_BUFFER_PER_PID
:
5630 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5631 struct ust_app_channel
*ua_chan
;
5632 struct ust_app_session
*ua_sess
;
5633 struct lttng_ht_iter chan_iter
;
5635 ua_sess
= lookup_session_by_app(usess
, app
);
5637 /* Session not associated with this app. */
5641 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5642 ua_chan
, node
.node
) {
5643 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
5645 * Don't take channel into account if we
5646 * already grab all its packets.
5650 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;