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
)
272 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
273 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
274 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
275 sock
, ua_ctx
->obj
->handle
, ret
);
283 * Delete ust app event safely. RCU read lock must be held before calling
287 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
293 free(ua_event
->filter
);
294 if (ua_event
->exclusion
!= NULL
)
295 free(ua_event
->exclusion
);
296 if (ua_event
->obj
!= NULL
) {
297 ret
= ustctl_release_object(sock
, ua_event
->obj
);
298 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
299 ERR("UST app sock %d release event obj failed with ret %d",
308 * Release ust data object of the given stream.
310 * Return 0 on success or else a negative value.
312 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
319 ret
= ustctl_release_object(sock
, stream
->obj
);
320 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
321 ERR("UST app sock %d release stream obj failed with ret %d",
324 lttng_fd_put(LTTNG_FD_APPS
, 2);
332 * Delete ust app stream safely. RCU read lock must be held before calling
336 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
340 (void) release_ust_app_stream(sock
, stream
);
345 * We need to execute ht_destroy outside of RCU read-side critical
346 * section and outside of call_rcu thread, so we postpone its execution
347 * using ht_cleanup_push. It is simpler than to change the semantic of
348 * the many callers of delete_ust_app_session().
351 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
353 struct ust_app_channel
*ua_chan
=
354 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
356 ht_cleanup_push(ua_chan
->ctx
);
357 ht_cleanup_push(ua_chan
->events
);
362 * Delete ust app channel safely. RCU read lock must be held before calling
366 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
370 struct lttng_ht_iter iter
;
371 struct ust_app_event
*ua_event
;
372 struct ust_app_ctx
*ua_ctx
;
373 struct ust_app_stream
*stream
, *stmp
;
374 struct ust_registry_session
*registry
;
378 DBG3("UST app deleting channel %s", ua_chan
->name
);
381 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
382 cds_list_del(&stream
->list
);
383 delete_ust_app_stream(sock
, stream
);
387 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
388 cds_list_del(&ua_ctx
->list
);
389 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
391 delete_ust_app_ctx(sock
, ua_ctx
);
395 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
397 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
399 delete_ust_app_event(sock
, ua_event
);
402 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
403 /* Wipe and free registry from session registry. */
404 registry
= get_session_registry(ua_chan
->session
);
406 ust_registry_channel_del_free(registry
, ua_chan
->key
);
410 if (ua_chan
->obj
!= NULL
) {
411 /* Remove channel from application UST object descriptor. */
412 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
413 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
415 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
416 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
417 ERR("UST app sock %d release channel obj failed with ret %d",
420 lttng_fd_put(LTTNG_FD_APPS
, 1);
423 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
427 * Push metadata to consumer socket.
429 * RCU read-side lock must be held to guarantee existance of socket.
430 * Must be called with the ust app session lock held.
431 * Must be called with the registry lock held.
433 * On success, return the len of metadata pushed or else a negative value.
434 * Returning a -EPIPE return value means we could not send the metadata,
435 * but it can be caused by recoverable errors (e.g. the application has
436 * terminated concurrently).
438 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
439 struct consumer_socket
*socket
, int send_zero_data
)
442 char *metadata_str
= NULL
;
450 * Means that no metadata was assigned to the session. This can
451 * happens if no start has been done previously.
453 if (!registry
->metadata_key
) {
458 * On a push metadata error either the consumer is dead or the
459 * metadata channel has been destroyed because its endpoint
460 * might have died (e.g: relayd), or because the application has
461 * exited. If so, the metadata closed flag is set to 1 so we
462 * deny pushing metadata again which is not valid anymore on the
465 if (registry
->metadata_closed
) {
469 offset
= registry
->metadata_len_sent
;
470 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
472 DBG3("No metadata to push for metadata key %" PRIu64
,
473 registry
->metadata_key
);
475 if (send_zero_data
) {
476 DBG("No metadata to push");
482 /* Allocate only what we have to send. */
483 metadata_str
= zmalloc(len
);
485 PERROR("zmalloc ust app metadata string");
489 /* Copy what we haven't send out. */
490 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
491 registry
->metadata_len_sent
+= len
;
494 ret
= consumer_push_metadata(socket
, registry
->metadata_key
,
495 metadata_str
, len
, offset
);
498 * There is an acceptable race here between the registry
499 * metadata key assignment and the creation on the
500 * consumer. The session daemon can concurrently push
501 * metadata for this registry while being created on the
502 * consumer since the metadata key of the registry is
503 * assigned *before* it is setup to avoid the consumer
504 * to ask for metadata that could possibly be not found
505 * in the session daemon.
507 * The metadata will get pushed either by the session
508 * being stopped or the consumer requesting metadata if
509 * that race is triggered.
511 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
516 * Update back the actual metadata len sent since it
519 registry
->metadata_len_sent
-= len
;
531 * On error, flag the registry that the metadata is
532 * closed. We were unable to push anything and this
533 * means that either the consumer is not responding or
534 * the metadata cache has been destroyed on the
537 registry
->metadata_closed
= 1;
545 * For a given application and session, push metadata to consumer.
546 * Either sock or consumer is required : if sock is NULL, the default
547 * socket to send the metadata is retrieved from consumer, if sock
548 * is not NULL we use it to send the metadata.
549 * RCU read-side lock must be held while calling this function,
550 * therefore ensuring existance of registry. It also ensures existance
551 * of socket throughout this function.
553 * Return 0 on success else a negative error.
554 * Returning a -EPIPE return value means we could not send the metadata,
555 * but it can be caused by recoverable errors (e.g. the application has
556 * terminated concurrently).
558 static int push_metadata(struct ust_registry_session
*registry
,
559 struct consumer_output
*consumer
)
563 struct consumer_socket
*socket
;
568 pthread_mutex_lock(®istry
->lock
);
569 if (registry
->metadata_closed
) {
574 /* Get consumer socket to use to push the metadata.*/
575 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
582 ret
= ust_app_push_metadata(registry
, socket
, 0);
587 pthread_mutex_unlock(®istry
->lock
);
591 pthread_mutex_unlock(®istry
->lock
);
596 * Send to the consumer a close metadata command for the given session. Once
597 * done, the metadata channel is deleted and the session metadata pointer is
598 * nullified. The session lock MUST be held unless the application is
599 * in the destroy path.
601 * Return 0 on success else a negative value.
603 static int close_metadata(struct ust_registry_session
*registry
,
604 struct consumer_output
*consumer
)
607 struct consumer_socket
*socket
;
614 pthread_mutex_lock(®istry
->lock
);
616 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
621 /* Get consumer socket to use to push the metadata.*/
622 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
629 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
636 * Metadata closed. Even on error this means that the consumer is not
637 * responding or not found so either way a second close should NOT be emit
640 registry
->metadata_closed
= 1;
642 pthread_mutex_unlock(®istry
->lock
);
648 * We need to execute ht_destroy outside of RCU read-side critical
649 * section and outside of call_rcu thread, so we postpone its execution
650 * using ht_cleanup_push. It is simpler than to change the semantic of
651 * the many callers of delete_ust_app_session().
654 void delete_ust_app_session_rcu(struct rcu_head
*head
)
656 struct ust_app_session
*ua_sess
=
657 caa_container_of(head
, struct ust_app_session
, rcu_head
);
659 ht_cleanup_push(ua_sess
->channels
);
664 * Delete ust app session safely. RCU read lock must be held before calling
668 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
672 struct lttng_ht_iter iter
;
673 struct ust_app_channel
*ua_chan
;
674 struct ust_registry_session
*registry
;
678 pthread_mutex_lock(&ua_sess
->lock
);
680 registry
= get_session_registry(ua_sess
);
682 /* Push metadata for application before freeing the application. */
683 (void) push_metadata(registry
, ua_sess
->consumer
);
686 * Don't ask to close metadata for global per UID buffers. Close
687 * metadata only on destroy trace session in this case. Also, the
688 * previous push metadata could have flag the metadata registry to
689 * close so don't send a close command if closed.
691 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
692 /* And ask to close it for this session registry. */
693 (void) close_metadata(registry
, ua_sess
->consumer
);
697 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
699 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
701 delete_ust_app_channel(sock
, ua_chan
, app
);
704 /* In case of per PID, the registry is kept in the session. */
705 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
706 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
708 buffer_reg_pid_remove(reg_pid
);
709 buffer_reg_pid_destroy(reg_pid
);
713 if (ua_sess
->handle
!= -1) {
714 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
715 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
716 ERR("UST app sock %d release session handle failed with ret %d",
720 pthread_mutex_unlock(&ua_sess
->lock
);
722 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
726 * Delete a traceable application structure from the global list. Never call
727 * this function outside of a call_rcu call.
729 * RCU read side lock should _NOT_ be held when calling this function.
732 void delete_ust_app(struct ust_app
*app
)
735 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
737 /* Delete ust app sessions info */
742 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
744 /* Free every object in the session and the session. */
746 delete_ust_app_session(sock
, ua_sess
, app
);
750 ht_cleanup_push(app
->sessions
);
751 ht_cleanup_push(app
->ust_objd
);
754 * Wait until we have deleted the application from the sock hash table
755 * before closing this socket, otherwise an application could re-use the
756 * socket ID and race with the teardown, using the same hash table entry.
758 * It's OK to leave the close in call_rcu. We want it to stay unique for
759 * all RCU readers that could run concurrently with unregister app,
760 * therefore we _need_ to only close that socket after a grace period. So
761 * it should stay in this RCU callback.
763 * This close() is a very important step of the synchronization model so
764 * every modification to this function must be carefully reviewed.
770 lttng_fd_put(LTTNG_FD_APPS
, 1);
772 DBG2("UST app pid %d deleted", app
->pid
);
777 * URCU intermediate call to delete an UST app.
780 void delete_ust_app_rcu(struct rcu_head
*head
)
782 struct lttng_ht_node_ulong
*node
=
783 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
784 struct ust_app
*app
=
785 caa_container_of(node
, struct ust_app
, pid_n
);
787 DBG3("Call RCU deleting app PID %d", app
->pid
);
792 * Delete the session from the application ht and delete the data structure by
793 * freeing every object inside and releasing them.
795 static void destroy_app_session(struct ust_app
*app
,
796 struct ust_app_session
*ua_sess
)
799 struct lttng_ht_iter iter
;
804 iter
.iter
.node
= &ua_sess
->node
.node
;
805 ret
= lttng_ht_del(app
->sessions
, &iter
);
807 /* Already scheduled for teardown. */
811 /* Once deleted, free the data structure. */
812 delete_ust_app_session(app
->sock
, ua_sess
, app
);
819 * Alloc new UST app session.
822 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
824 struct ust_app_session
*ua_sess
;
826 /* Init most of the default value by allocating and zeroing */
827 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
828 if (ua_sess
== NULL
) {
833 ua_sess
->handle
= -1;
834 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
835 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
836 pthread_mutex_init(&ua_sess
->lock
, NULL
);
845 * Alloc new UST app channel.
848 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
849 struct ust_app_session
*ua_sess
,
850 struct lttng_ust_channel_attr
*attr
)
852 struct ust_app_channel
*ua_chan
;
854 /* Init most of the default value by allocating and zeroing */
855 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
856 if (ua_chan
== NULL
) {
861 /* Setup channel name */
862 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
863 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
865 ua_chan
->enabled
= 1;
866 ua_chan
->handle
= -1;
867 ua_chan
->session
= ua_sess
;
868 ua_chan
->key
= get_next_channel_key();
869 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
870 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
871 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
873 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
874 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
876 /* Copy attributes */
878 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
879 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
880 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
881 ua_chan
->attr
.overwrite
= attr
->overwrite
;
882 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
883 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
884 ua_chan
->attr
.output
= attr
->output
;
886 /* By default, the channel is a per cpu channel. */
887 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
889 DBG3("UST app channel %s allocated", ua_chan
->name
);
898 * Allocate and initialize a UST app stream.
900 * Return newly allocated stream pointer or NULL on error.
902 struct ust_app_stream
*ust_app_alloc_stream(void)
904 struct ust_app_stream
*stream
= NULL
;
906 stream
= zmalloc(sizeof(*stream
));
907 if (stream
== NULL
) {
908 PERROR("zmalloc ust app stream");
912 /* Zero could be a valid value for a handle so flag it to -1. */
920 * Alloc new UST app event.
923 struct ust_app_event
*alloc_ust_app_event(char *name
,
924 struct lttng_ust_event
*attr
)
926 struct ust_app_event
*ua_event
;
928 /* Init most of the default value by allocating and zeroing */
929 ua_event
= zmalloc(sizeof(struct ust_app_event
));
930 if (ua_event
== NULL
) {
935 ua_event
->enabled
= 1;
936 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
937 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
938 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
940 /* Copy attributes */
942 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
945 DBG3("UST app event %s allocated", ua_event
->name
);
954 * Alloc new UST app context.
957 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
959 struct ust_app_ctx
*ua_ctx
;
961 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
962 if (ua_ctx
== NULL
) {
966 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
969 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
972 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
979 * Allocate a filter and copy the given original filter.
981 * Return allocated filter or NULL on error.
983 static struct lttng_filter_bytecode
*copy_filter_bytecode(
984 struct lttng_filter_bytecode
*orig_f
)
986 struct lttng_filter_bytecode
*filter
= NULL
;
988 /* Copy filter bytecode */
989 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
991 PERROR("zmalloc alloc filter bytecode");
995 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1002 * Create a liblttng-ust filter bytecode from given bytecode.
1004 * Return allocated filter or NULL on error.
1006 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1007 struct lttng_filter_bytecode
*orig_f
)
1009 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1011 /* Copy filter bytecode */
1012 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1014 PERROR("zmalloc alloc ust filter bytecode");
1018 assert(sizeof(struct lttng_filter_bytecode
) ==
1019 sizeof(struct lttng_ust_filter_bytecode
));
1020 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1026 * Find an ust_app using the sock and return it. RCU read side lock must be
1027 * held before calling this helper function.
1029 struct ust_app
*ust_app_find_by_sock(int sock
)
1031 struct lttng_ht_node_ulong
*node
;
1032 struct lttng_ht_iter iter
;
1034 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1035 node
= lttng_ht_iter_get_node_ulong(&iter
);
1037 DBG2("UST app find by sock %d not found", sock
);
1041 return caa_container_of(node
, struct ust_app
, sock_n
);
1048 * Find an ust_app using the notify sock and return it. RCU read side lock must
1049 * be held before calling this helper function.
1051 static struct ust_app
*find_app_by_notify_sock(int sock
)
1053 struct lttng_ht_node_ulong
*node
;
1054 struct lttng_ht_iter iter
;
1056 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1058 node
= lttng_ht_iter_get_node_ulong(&iter
);
1060 DBG2("UST app find by notify sock %d not found", sock
);
1064 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1071 * Lookup for an ust app event based on event name, filter bytecode and the
1074 * Return an ust_app_event object or NULL on error.
1076 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1077 char *name
, struct lttng_filter_bytecode
*filter
, int loglevel
,
1078 const struct lttng_event_exclusion
*exclusion
)
1080 struct lttng_ht_iter iter
;
1081 struct lttng_ht_node_str
*node
;
1082 struct ust_app_event
*event
= NULL
;
1083 struct ust_app_ht_key key
;
1088 /* Setup key for event lookup. */
1090 key
.filter
= filter
;
1091 key
.loglevel
= loglevel
;
1092 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1093 key
.exclusion
= exclusion
;
1095 /* Lookup using the event name as hash and a custom match fct. */
1096 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1097 ht_match_ust_app_event
, &key
, &iter
.iter
);
1098 node
= lttng_ht_iter_get_node_str(&iter
);
1103 event
= caa_container_of(node
, struct ust_app_event
, node
);
1110 * Create the channel context on the tracer.
1112 * Called with UST app session lock held.
1115 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1116 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1120 health_code_update();
1122 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1123 ua_chan
->obj
, &ua_ctx
->obj
);
1125 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1126 ERR("UST app create channel context failed for app (pid: %d) "
1127 "with ret %d", app
->pid
, ret
);
1130 * This is normal behavior, an application can die during the
1131 * creation process. Don't report an error so the execution can
1132 * continue normally.
1135 DBG3("UST app disable event failed. Application is dead.");
1140 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1142 DBG2("UST app context handle %d created successfully for channel %s",
1143 ua_ctx
->handle
, ua_chan
->name
);
1146 health_code_update();
1151 * Set the filter on the tracer.
1154 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1155 struct ust_app
*app
)
1158 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1160 health_code_update();
1162 if (!ua_event
->filter
) {
1167 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1168 if (!ust_bytecode
) {
1169 ret
= -LTTNG_ERR_NOMEM
;
1172 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1175 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1176 ERR("UST app event %s filter failed for app (pid: %d) "
1177 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1180 * This is normal behavior, an application can die during the
1181 * creation process. Don't report an error so the execution can
1182 * continue normally.
1185 DBG3("UST app filter event failed. Application is dead.");
1190 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1193 health_code_update();
1199 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1200 struct lttng_event_exclusion
*exclusion
)
1202 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1203 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1204 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1206 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1207 if (!ust_exclusion
) {
1212 assert(sizeof(struct lttng_event_exclusion
) ==
1213 sizeof(struct lttng_ust_event_exclusion
));
1214 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1216 return ust_exclusion
;
1220 * Set event exclusions on the tracer.
1223 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1224 struct ust_app
*app
)
1227 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1229 health_code_update();
1231 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1236 ust_exclusion
= create_ust_exclusion_from_exclusion(
1237 ua_event
->exclusion
);
1238 if (!ust_exclusion
) {
1239 ret
= -LTTNG_ERR_NOMEM
;
1242 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1244 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1245 ERR("UST app event %s exclusions failed for app (pid: %d) "
1246 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1249 * This is normal behavior, an application can die during the
1250 * creation process. Don't report an error so the execution can
1251 * continue normally.
1254 DBG3("UST app event exclusion failed. Application is dead.");
1259 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1262 health_code_update();
1263 free(ust_exclusion
);
1268 * Disable the specified event on to UST tracer for the UST session.
1270 static int disable_ust_event(struct ust_app
*app
,
1271 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1275 health_code_update();
1277 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1279 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1280 ERR("UST app event %s disable failed for app (pid: %d) "
1281 "and session handle %d with ret %d",
1282 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1285 * This is normal behavior, an application can die during the
1286 * creation process. Don't report an error so the execution can
1287 * continue normally.
1290 DBG3("UST app disable event failed. Application is dead.");
1295 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1296 ua_event
->attr
.name
, app
->pid
);
1299 health_code_update();
1304 * Disable the specified channel on to UST tracer for the UST session.
1306 static int disable_ust_channel(struct ust_app
*app
,
1307 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1311 health_code_update();
1313 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1315 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1316 ERR("UST app channel %s disable failed for app (pid: %d) "
1317 "and session handle %d with ret %d",
1318 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1321 * This is normal behavior, an application can die during the
1322 * creation process. Don't report an error so the execution can
1323 * continue normally.
1326 DBG3("UST app disable channel failed. Application is dead.");
1331 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1332 ua_chan
->name
, app
->pid
);
1335 health_code_update();
1340 * Enable the specified channel on to UST tracer for the UST session.
1342 static int enable_ust_channel(struct ust_app
*app
,
1343 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1347 health_code_update();
1349 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1351 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1352 ERR("UST app channel %s enable failed for app (pid: %d) "
1353 "and session handle %d with ret %d",
1354 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1357 * This is normal behavior, an application can die during the
1358 * creation process. Don't report an error so the execution can
1359 * continue normally.
1362 DBG3("UST app enable channel failed. Application is dead.");
1367 ua_chan
->enabled
= 1;
1369 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1370 ua_chan
->name
, app
->pid
);
1373 health_code_update();
1378 * Enable the specified event on to UST tracer for the UST session.
1380 static int enable_ust_event(struct ust_app
*app
,
1381 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1385 health_code_update();
1387 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1389 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1390 ERR("UST app event %s enable failed for app (pid: %d) "
1391 "and session handle %d with ret %d",
1392 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1395 * This is normal behavior, an application can die during the
1396 * creation process. Don't report an error so the execution can
1397 * continue normally.
1400 DBG3("UST app enable event failed. Application is dead.");
1405 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1406 ua_event
->attr
.name
, app
->pid
);
1409 health_code_update();
1414 * Send channel and stream buffer to application.
1416 * Return 0 on success. On error, a negative value is returned.
1418 static int send_channel_pid_to_ust(struct ust_app
*app
,
1419 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1422 struct ust_app_stream
*stream
, *stmp
;
1428 health_code_update();
1430 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1433 /* Send channel to the application. */
1434 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1439 health_code_update();
1441 /* Send all streams to application. */
1442 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1443 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1447 /* We don't need the stream anymore once sent to the tracer. */
1448 cds_list_del(&stream
->list
);
1449 delete_ust_app_stream(-1, stream
);
1451 /* Flag the channel that it is sent to the application. */
1452 ua_chan
->is_sent
= 1;
1455 health_code_update();
1460 * Create the specified event onto the UST tracer for a UST session.
1462 * Should be called with session mutex held.
1465 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1466 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1470 health_code_update();
1472 /* Create UST event on tracer */
1473 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1476 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1477 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1478 ua_event
->attr
.name
, app
->pid
, ret
);
1481 * This is normal behavior, an application can die during the
1482 * creation process. Don't report an error so the execution can
1483 * continue normally.
1486 DBG3("UST app create event failed. Application is dead.");
1491 ua_event
->handle
= ua_event
->obj
->handle
;
1493 DBG2("UST app event %s created successfully for pid:%d",
1494 ua_event
->attr
.name
, app
->pid
);
1496 health_code_update();
1498 /* Set filter if one is present. */
1499 if (ua_event
->filter
) {
1500 ret
= set_ust_event_filter(ua_event
, app
);
1506 /* Set exclusions for the event */
1507 if (ua_event
->exclusion
) {
1508 ret
= set_ust_event_exclusion(ua_event
, app
);
1514 /* If event not enabled, disable it on the tracer */
1515 if (ua_event
->enabled
) {
1517 * We now need to explicitly enable the event, since it
1518 * is now disabled at creation.
1520 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1523 * If we hit an EPERM, something is wrong with our enable call. If
1524 * we get an EEXIST, there is a problem on the tracer side since we
1528 case -LTTNG_UST_ERR_PERM
:
1529 /* Code flow problem */
1531 case -LTTNG_UST_ERR_EXIST
:
1532 /* It's OK for our use case. */
1543 health_code_update();
1548 * Copy data between an UST app event and a LTT event.
1550 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1551 struct ltt_ust_event
*uevent
)
1553 size_t exclusion_alloc_size
;
1555 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1556 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1558 ua_event
->enabled
= uevent
->enabled
;
1560 /* Copy event attributes */
1561 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1563 /* Copy filter bytecode */
1564 if (uevent
->filter
) {
1565 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1566 /* Filter might be NULL here in case of ENONEM. */
1569 /* Copy exclusion data */
1570 if (uevent
->exclusion
) {
1571 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1572 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1573 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1574 if (ua_event
->exclusion
== NULL
) {
1577 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1578 exclusion_alloc_size
);
1584 * Copy data between an UST app channel and a LTT channel.
1586 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1587 struct ltt_ust_channel
*uchan
)
1589 struct lttng_ht_iter iter
;
1590 struct ltt_ust_event
*uevent
;
1591 struct ltt_ust_context
*uctx
;
1592 struct ust_app_event
*ua_event
;
1593 struct ust_app_ctx
*ua_ctx
;
1595 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1597 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1598 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1600 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1601 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1603 /* Copy event attributes since the layout is different. */
1604 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1605 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1606 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1607 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1608 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1609 ua_chan
->attr
.output
= uchan
->attr
.output
;
1611 * Note that the attribute channel type is not set since the channel on the
1612 * tracing registry side does not have this information.
1615 ua_chan
->enabled
= uchan
->enabled
;
1616 ua_chan
->tracing_channel_id
= uchan
->id
;
1618 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1619 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1620 if (ua_ctx
== NULL
) {
1623 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1624 (unsigned long) ua_ctx
->ctx
.ctx
);
1625 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1626 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1629 /* Copy all events from ltt ust channel to ust app channel */
1630 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1631 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1632 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1633 if (ua_event
== NULL
) {
1634 DBG2("UST event %s not found on shadow copy channel",
1636 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1637 if (ua_event
== NULL
) {
1640 shadow_copy_event(ua_event
, uevent
);
1641 add_unique_ust_app_event(ua_chan
, ua_event
);
1645 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1649 * Copy data between a UST app session and a regular LTT session.
1651 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1652 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1654 struct lttng_ht_node_str
*ua_chan_node
;
1655 struct lttng_ht_iter iter
;
1656 struct ltt_ust_channel
*uchan
;
1657 struct ust_app_channel
*ua_chan
;
1659 struct tm
*timeinfo
;
1662 char tmp_shm_path
[PATH_MAX
];
1664 /* Get date and time for unique app path */
1666 timeinfo
= localtime(&rawtime
);
1667 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1669 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1671 ua_sess
->tracing_id
= usess
->id
;
1672 ua_sess
->id
= get_next_session_id();
1673 ua_sess
->uid
= app
->uid
;
1674 ua_sess
->gid
= app
->gid
;
1675 ua_sess
->euid
= usess
->uid
;
1676 ua_sess
->egid
= usess
->gid
;
1677 ua_sess
->buffer_type
= usess
->buffer_type
;
1678 ua_sess
->bits_per_long
= app
->bits_per_long
;
1679 /* There is only one consumer object per session possible. */
1680 ua_sess
->consumer
= usess
->consumer
;
1681 ua_sess
->output_traces
= usess
->output_traces
;
1682 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1683 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1684 &usess
->metadata_attr
);
1686 switch (ua_sess
->buffer_type
) {
1687 case LTTNG_BUFFER_PER_PID
:
1688 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1689 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1692 case LTTNG_BUFFER_PER_UID
:
1693 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1694 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1701 PERROR("asprintf UST shadow copy session");
1706 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1707 sizeof(ua_sess
->root_shm_path
));
1708 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1709 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1710 sizeof(ua_sess
->shm_path
));
1711 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1712 if (ua_sess
->shm_path
[0]) {
1713 switch (ua_sess
->buffer_type
) {
1714 case LTTNG_BUFFER_PER_PID
:
1715 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1716 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1717 app
->name
, app
->pid
, datetime
);
1719 case LTTNG_BUFFER_PER_UID
:
1720 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1721 DEFAULT_UST_TRACE_UID_PATH
,
1722 app
->uid
, app
->bits_per_long
);
1729 PERROR("sprintf UST shadow copy session");
1733 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1734 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1735 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1738 /* Iterate over all channels in global domain. */
1739 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1741 struct lttng_ht_iter uiter
;
1743 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1744 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1745 if (ua_chan_node
!= NULL
) {
1746 /* Session exist. Contiuing. */
1750 DBG2("Channel %s not found on shadow session copy, creating it",
1752 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1753 if (ua_chan
== NULL
) {
1754 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1757 shadow_copy_channel(ua_chan
, uchan
);
1759 * The concept of metadata channel does not exist on the tracing
1760 * registry side of the session daemon so this can only be a per CPU
1761 * channel and not metadata.
1763 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1765 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1773 * Lookup sesison wrapper.
1776 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1777 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1779 /* Get right UST app session from app */
1780 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1784 * Return ust app session from the app session hashtable using the UST session
1787 static struct ust_app_session
*lookup_session_by_app(
1788 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1790 struct lttng_ht_iter iter
;
1791 struct lttng_ht_node_u64
*node
;
1793 __lookup_session_by_app(usess
, app
, &iter
);
1794 node
= lttng_ht_iter_get_node_u64(&iter
);
1799 return caa_container_of(node
, struct ust_app_session
, node
);
1806 * Setup buffer registry per PID for the given session and application. If none
1807 * is found, a new one is created, added to the global registry and
1808 * initialized. If regp is valid, it's set with the newly created object.
1810 * Return 0 on success or else a negative value.
1812 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1813 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1816 struct buffer_reg_pid
*reg_pid
;
1823 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1826 * This is the create channel path meaning that if there is NO
1827 * registry available, we have to create one for this session.
1829 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
1830 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1838 /* Initialize registry. */
1839 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1840 app
->bits_per_long
, app
->uint8_t_alignment
,
1841 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1842 app
->uint64_t_alignment
, app
->long_alignment
,
1843 app
->byte_order
, app
->version
.major
,
1844 app
->version
.minor
, reg_pid
->root_shm_path
,
1846 ua_sess
->euid
, ua_sess
->egid
);
1849 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1850 * destroy the buffer registry, because it is always expected
1851 * that if the buffer registry can be found, its ust registry is
1854 buffer_reg_pid_destroy(reg_pid
);
1858 buffer_reg_pid_add(reg_pid
);
1860 DBG3("UST app buffer registry per PID created successfully");
1872 * Setup buffer registry per UID for the given session and application. If none
1873 * is found, a new one is created, added to the global registry and
1874 * initialized. If regp is valid, it's set with the newly created object.
1876 * Return 0 on success or else a negative value.
1878 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
1879 struct ust_app_session
*ua_sess
,
1880 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
1883 struct buffer_reg_uid
*reg_uid
;
1890 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
1893 * This is the create channel path meaning that if there is NO
1894 * registry available, we have to create one for this session.
1896 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
1897 LTTNG_DOMAIN_UST
, ®_uid
,
1898 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1906 /* Initialize registry. */
1907 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
1908 app
->bits_per_long
, app
->uint8_t_alignment
,
1909 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1910 app
->uint64_t_alignment
, app
->long_alignment
,
1911 app
->byte_order
, app
->version
.major
,
1912 app
->version
.minor
, reg_uid
->root_shm_path
,
1913 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
1916 * reg_uid->registry->reg.ust is NULL upon error, so we need to
1917 * destroy the buffer registry, because it is always expected
1918 * that if the buffer registry can be found, its ust registry is
1921 buffer_reg_uid_destroy(reg_uid
, NULL
);
1924 /* Add node to teardown list of the session. */
1925 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
1927 buffer_reg_uid_add(reg_uid
);
1929 DBG3("UST app buffer registry per UID created successfully");
1940 * Create a session on the tracer side for the given app.
1942 * On success, ua_sess_ptr is populated with the session pointer or else left
1943 * untouched. If the session was created, is_created is set to 1. On error,
1944 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1947 * Returns 0 on success or else a negative code which is either -ENOMEM or
1948 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1950 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1951 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1954 int ret
, created
= 0;
1955 struct ust_app_session
*ua_sess
;
1959 assert(ua_sess_ptr
);
1961 health_code_update();
1963 ua_sess
= lookup_session_by_app(usess
, app
);
1964 if (ua_sess
== NULL
) {
1965 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
1966 app
->pid
, usess
->id
);
1967 ua_sess
= alloc_ust_app_session(app
);
1968 if (ua_sess
== NULL
) {
1969 /* Only malloc can failed so something is really wrong */
1973 shadow_copy_session(ua_sess
, usess
, app
);
1977 switch (usess
->buffer_type
) {
1978 case LTTNG_BUFFER_PER_PID
:
1979 /* Init local registry. */
1980 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
1982 delete_ust_app_session(-1, ua_sess
, app
);
1986 case LTTNG_BUFFER_PER_UID
:
1987 /* Look for a global registry. If none exists, create one. */
1988 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
1990 delete_ust_app_session(-1, ua_sess
, app
);
2000 health_code_update();
2002 if (ua_sess
->handle
== -1) {
2003 ret
= ustctl_create_session(app
->sock
);
2005 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2006 ERR("Creating session for app pid %d with ret %d",
2009 DBG("UST app creating session failed. Application is dead");
2011 * This is normal behavior, an application can die during the
2012 * creation process. Don't report an error so the execution can
2013 * continue normally. This will get flagged ENOTCONN and the
2014 * caller will handle it.
2018 delete_ust_app_session(-1, ua_sess
, app
);
2019 if (ret
!= -ENOMEM
) {
2021 * Tracer is probably gone or got an internal error so let's
2022 * behave like it will soon unregister or not usable.
2029 ua_sess
->handle
= ret
;
2031 /* Add ust app session to app's HT */
2032 lttng_ht_node_init_u64(&ua_sess
->node
,
2033 ua_sess
->tracing_id
);
2034 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2036 DBG2("UST app session created successfully with handle %d", ret
);
2039 *ua_sess_ptr
= ua_sess
;
2041 *is_created
= created
;
2044 /* Everything went well. */
2048 health_code_update();
2053 * Match function for a hash table lookup of ust_app_ctx.
2055 * It matches an ust app context based on the context type and, in the case
2056 * of perf counters, their name.
2058 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2060 struct ust_app_ctx
*ctx
;
2061 const struct lttng_ust_context
*key
;
2066 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2070 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2074 /* Check the name in the case of perf thread counters. */
2075 if (key
->ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
2076 if (strncmp(key
->u
.perf_counter
.name
,
2077 ctx
->ctx
.u
.perf_counter
.name
,
2078 sizeof(key
->u
.perf_counter
.name
))) {
2091 * Lookup for an ust app context from an lttng_ust_context.
2093 * Must be called while holding RCU read side lock.
2094 * Return an ust_app_ctx object or NULL on error.
2097 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2098 struct lttng_ust_context
*uctx
)
2100 struct lttng_ht_iter iter
;
2101 struct lttng_ht_node_ulong
*node
;
2102 struct ust_app_ctx
*app_ctx
= NULL
;
2107 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2108 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2109 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2110 node
= lttng_ht_iter_get_node_ulong(&iter
);
2115 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2122 * Create a context for the channel on the tracer.
2124 * Called with UST app session lock held and a RCU read side lock.
2127 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2128 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
2129 struct ust_app
*app
)
2132 struct ust_app_ctx
*ua_ctx
;
2134 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2136 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2142 ua_ctx
= alloc_ust_app_ctx(uctx
);
2143 if (ua_ctx
== NULL
) {
2149 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2150 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2151 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2153 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2163 * Enable on the tracer side a ust app event for the session and channel.
2165 * Called with UST app session lock held.
2168 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2169 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2173 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2178 ua_event
->enabled
= 1;
2185 * Disable on the tracer side a ust app event for the session and channel.
2187 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2188 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2192 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2197 ua_event
->enabled
= 0;
2204 * Lookup ust app channel for session and disable it on the tracer side.
2207 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2208 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2212 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2217 ua_chan
->enabled
= 0;
2224 * Lookup ust app channel for session and enable it on the tracer side. This
2225 * MUST be called with a RCU read side lock acquired.
2227 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2228 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2231 struct lttng_ht_iter iter
;
2232 struct lttng_ht_node_str
*ua_chan_node
;
2233 struct ust_app_channel
*ua_chan
;
2235 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2236 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2237 if (ua_chan_node
== NULL
) {
2238 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2239 uchan
->name
, ua_sess
->tracing_id
);
2243 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2245 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2255 * Ask the consumer to create a channel and get it if successful.
2257 * Return 0 on success or else a negative value.
2259 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2260 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2261 int bitness
, struct ust_registry_session
*registry
)
2264 unsigned int nb_fd
= 0;
2265 struct consumer_socket
*socket
;
2273 health_code_update();
2275 /* Get the right consumer socket for the application. */
2276 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2282 health_code_update();
2284 /* Need one fd for the channel. */
2285 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2287 ERR("Exhausted number of available FD upon create channel");
2292 * Ask consumer to create channel. The consumer will return the number of
2293 * stream we have to expect.
2295 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2302 * Compute the number of fd needed before receiving them. It must be 2 per
2303 * stream (2 being the default value here).
2305 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2307 /* Reserve the amount of file descriptor we need. */
2308 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2310 ERR("Exhausted number of available FD upon create channel");
2311 goto error_fd_get_stream
;
2314 health_code_update();
2317 * Now get the channel from the consumer. This call wil populate the stream
2318 * list of that channel and set the ust objects.
2320 if (usess
->consumer
->enabled
) {
2321 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2331 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2332 error_fd_get_stream
:
2334 * Initiate a destroy channel on the consumer since we had an error
2335 * handling it on our side. The return value is of no importance since we
2336 * already have a ret value set by the previous error that we need to
2339 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2341 lttng_fd_put(LTTNG_FD_APPS
, 1);
2343 health_code_update();
2349 * Duplicate the ust data object of the ust app stream and save it in the
2350 * buffer registry stream.
2352 * Return 0 on success or else a negative value.
2354 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2355 struct ust_app_stream
*stream
)
2362 /* Reserve the amount of file descriptor we need. */
2363 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2365 ERR("Exhausted number of available FD upon duplicate stream");
2369 /* Duplicate object for stream once the original is in the registry. */
2370 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2371 reg_stream
->obj
.ust
);
2373 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2374 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2375 lttng_fd_put(LTTNG_FD_APPS
, 2);
2378 stream
->handle
= stream
->obj
->handle
;
2385 * Duplicate the ust data object of the ust app. channel and save it in the
2386 * buffer registry channel.
2388 * Return 0 on success or else a negative value.
2390 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2391 struct ust_app_channel
*ua_chan
)
2398 /* Need two fds for the channel. */
2399 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2401 ERR("Exhausted number of available FD upon duplicate channel");
2405 /* Duplicate object for stream once the original is in the registry. */
2406 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2408 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2409 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2412 ua_chan
->handle
= ua_chan
->obj
->handle
;
2417 lttng_fd_put(LTTNG_FD_APPS
, 1);
2423 * For a given channel buffer registry, setup all streams of the given ust
2424 * application channel.
2426 * Return 0 on success or else a negative value.
2428 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2429 struct ust_app_channel
*ua_chan
)
2432 struct ust_app_stream
*stream
, *stmp
;
2437 DBG2("UST app setup buffer registry stream");
2439 /* Send all streams to application. */
2440 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2441 struct buffer_reg_stream
*reg_stream
;
2443 ret
= buffer_reg_stream_create(®_stream
);
2449 * Keep original pointer and nullify it in the stream so the delete
2450 * stream call does not release the object.
2452 reg_stream
->obj
.ust
= stream
->obj
;
2454 buffer_reg_stream_add(reg_stream
, reg_chan
);
2456 /* We don't need the streams anymore. */
2457 cds_list_del(&stream
->list
);
2458 delete_ust_app_stream(-1, stream
);
2466 * Create a buffer registry channel for the given session registry and
2467 * application channel object. If regp pointer is valid, it's set with the
2468 * created object. Important, the created object is NOT added to the session
2469 * registry hash table.
2471 * Return 0 on success else a negative value.
2473 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2474 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2477 struct buffer_reg_channel
*reg_chan
= NULL
;
2482 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2484 /* Create buffer registry channel. */
2485 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2490 reg_chan
->consumer_key
= ua_chan
->key
;
2491 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2492 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2494 /* Create and add a channel registry to session. */
2495 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2496 ua_chan
->tracing_channel_id
);
2500 buffer_reg_channel_add(reg_sess
, reg_chan
);
2509 /* Safe because the registry channel object was not added to any HT. */
2510 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2516 * Setup buffer registry channel for the given session registry and application
2517 * channel object. If regp pointer is valid, it's set with the created object.
2519 * Return 0 on success else a negative value.
2521 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2522 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
)
2529 assert(ua_chan
->obj
);
2531 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2533 /* Setup all streams for the registry. */
2534 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
);
2539 reg_chan
->obj
.ust
= ua_chan
->obj
;
2540 ua_chan
->obj
= NULL
;
2545 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2546 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2551 * Send buffer registry channel to the application.
2553 * Return 0 on success else a negative value.
2555 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2556 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2557 struct ust_app_channel
*ua_chan
)
2560 struct buffer_reg_stream
*reg_stream
;
2567 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2569 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2574 /* Send channel to the application. */
2575 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2580 health_code_update();
2582 /* Send all streams to application. */
2583 pthread_mutex_lock(®_chan
->stream_list_lock
);
2584 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2585 struct ust_app_stream stream
;
2587 ret
= duplicate_stream_object(reg_stream
, &stream
);
2589 goto error_stream_unlock
;
2592 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2594 (void) release_ust_app_stream(-1, &stream
);
2595 goto error_stream_unlock
;
2599 * The return value is not important here. This function will output an
2602 (void) release_ust_app_stream(-1, &stream
);
2604 ua_chan
->is_sent
= 1;
2606 error_stream_unlock
:
2607 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2613 * Create and send to the application the created buffers with per UID buffers.
2615 * Return 0 on success else a negative value.
2617 static int create_channel_per_uid(struct ust_app
*app
,
2618 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2619 struct ust_app_channel
*ua_chan
)
2622 struct buffer_reg_uid
*reg_uid
;
2623 struct buffer_reg_channel
*reg_chan
;
2630 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2632 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2634 * The session creation handles the creation of this global registry
2635 * object. If none can be find, there is a code flow problem or a
2640 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2643 /* Create the buffer registry channel object. */
2644 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2646 ERR("Error creating the UST channel \"%s\" registry instance",
2653 * Create the buffers on the consumer side. This call populates the
2654 * ust app channel object with all streams and data object.
2656 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2657 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2659 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2663 * Let's remove the previously created buffer registry channel so
2664 * it's not visible anymore in the session registry.
2666 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2667 ua_chan
->tracing_channel_id
);
2668 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2669 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2674 * Setup the streams and add it to the session registry.
2676 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, reg_chan
);
2678 ERR("Error setting up UST channel \"%s\"",
2685 /* Send buffers to the application. */
2686 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2689 * Don't report error to the console, since it may be
2690 * caused by application concurrently exiting.
2700 * Create and send to the application the created buffers with per PID buffers.
2702 * Return 0 on success else a negative value.
2704 static int create_channel_per_pid(struct ust_app
*app
,
2705 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2706 struct ust_app_channel
*ua_chan
)
2709 struct ust_registry_session
*registry
;
2716 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2720 registry
= get_session_registry(ua_sess
);
2723 /* Create and add a new channel registry to session. */
2724 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2726 ERR("Error creating the UST channel \"%s\" registry instance",
2731 /* Create and get channel on the consumer side. */
2732 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2733 app
->bits_per_long
, registry
);
2735 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2740 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2743 * Don't report error to the console, since it may be
2744 * caused by application concurrently exiting.
2755 * From an already allocated ust app channel, create the channel buffers if
2756 * need and send it to the application. This MUST be called with a RCU read
2757 * side lock acquired.
2759 * Return 0 on success or else a negative value.
2761 static int do_create_channel(struct ust_app
*app
,
2762 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2763 struct ust_app_channel
*ua_chan
)
2772 /* Handle buffer type before sending the channel to the application. */
2773 switch (usess
->buffer_type
) {
2774 case LTTNG_BUFFER_PER_UID
:
2776 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
2782 case LTTNG_BUFFER_PER_PID
:
2784 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
2796 /* Initialize ust objd object using the received handle and add it. */
2797 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
2798 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
2800 /* If channel is not enabled, disable it on the tracer */
2801 if (!ua_chan
->enabled
) {
2802 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2813 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2814 * newly created channel if not NULL.
2816 * Called with UST app session lock and RCU read-side lock held.
2818 * Return 0 on success or else a negative value.
2820 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
2821 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
2822 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
2823 struct ust_app_channel
**ua_chanp
)
2826 struct lttng_ht_iter iter
;
2827 struct lttng_ht_node_str
*ua_chan_node
;
2828 struct ust_app_channel
*ua_chan
;
2830 /* Lookup channel in the ust app session */
2831 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2832 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2833 if (ua_chan_node
!= NULL
) {
2834 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2838 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
2839 if (ua_chan
== NULL
) {
2840 /* Only malloc can fail here */
2844 shadow_copy_channel(ua_chan
, uchan
);
2846 /* Set channel type. */
2847 ua_chan
->attr
.type
= type
;
2849 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
2854 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
2857 /* Only add the channel if successful on the tracer side. */
2858 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
2862 *ua_chanp
= ua_chan
;
2865 /* Everything went well. */
2869 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
2875 * Create UST app event and create it on the tracer side.
2877 * Called with ust app session mutex held.
2880 int create_ust_app_event(struct ust_app_session
*ua_sess
,
2881 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
2882 struct ust_app
*app
)
2885 struct ust_app_event
*ua_event
;
2887 /* Get event node */
2888 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2889 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
2890 if (ua_event
!= NULL
) {
2895 /* Does not exist so create one */
2896 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
2897 if (ua_event
== NULL
) {
2898 /* Only malloc can failed so something is really wrong */
2902 shadow_copy_event(ua_event
, uevent
);
2904 /* Create it on the tracer side */
2905 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2907 /* Not found previously means that it does not exist on the tracer */
2908 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
2912 add_unique_ust_app_event(ua_chan
, ua_event
);
2914 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
2921 /* Valid. Calling here is already in a read side lock */
2922 delete_ust_app_event(-1, ua_event
);
2927 * Create UST metadata and open it on the tracer side.
2929 * Called with UST app session lock held and RCU read side lock.
2931 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
2932 struct ust_app
*app
, struct consumer_output
*consumer
)
2935 struct ust_app_channel
*metadata
;
2936 struct consumer_socket
*socket
;
2937 struct ust_registry_session
*registry
;
2943 registry
= get_session_registry(ua_sess
);
2946 pthread_mutex_lock(®istry
->lock
);
2948 /* Metadata already exists for this registry or it was closed previously */
2949 if (registry
->metadata_key
|| registry
->metadata_closed
) {
2954 /* Allocate UST metadata */
2955 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
2957 /* malloc() failed */
2962 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
2964 /* Need one fd for the channel. */
2965 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2967 ERR("Exhausted number of available FD upon create metadata");
2971 /* Get the right consumer socket for the application. */
2972 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
2975 goto error_consumer
;
2979 * Keep metadata key so we can identify it on the consumer side. Assign it
2980 * to the registry *before* we ask the consumer so we avoid the race of the
2981 * consumer requesting the metadata and the ask_channel call on our side
2982 * did not returned yet.
2984 registry
->metadata_key
= metadata
->key
;
2987 * Ask the metadata channel creation to the consumer. The metadata object
2988 * will be created by the consumer and kept their. However, the stream is
2989 * never added or monitored until we do a first push metadata to the
2992 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
2995 /* Nullify the metadata key so we don't try to close it later on. */
2996 registry
->metadata_key
= 0;
2997 goto error_consumer
;
3001 * The setup command will make the metadata stream be sent to the relayd,
3002 * if applicable, and the thread managing the metadatas. This is important
3003 * because after this point, if an error occurs, the only way the stream
3004 * can be deleted is to be monitored in the consumer.
3006 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3008 /* Nullify the metadata key so we don't try to close it later on. */
3009 registry
->metadata_key
= 0;
3010 goto error_consumer
;
3013 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3014 metadata
->key
, app
->pid
);
3017 lttng_fd_put(LTTNG_FD_APPS
, 1);
3018 delete_ust_app_channel(-1, metadata
, app
);
3020 pthread_mutex_unlock(®istry
->lock
);
3025 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3026 * acquired before calling this function.
3028 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3030 struct ust_app
*app
= NULL
;
3031 struct lttng_ht_node_ulong
*node
;
3032 struct lttng_ht_iter iter
;
3034 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3035 node
= lttng_ht_iter_get_node_ulong(&iter
);
3037 DBG2("UST app no found with pid %d", pid
);
3041 DBG2("Found UST app by pid %d", pid
);
3043 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3050 * Allocate and init an UST app object using the registration information and
3051 * the command socket. This is called when the command socket connects to the
3054 * The object is returned on success or else NULL.
3056 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3058 struct ust_app
*lta
= NULL
;
3063 DBG3("UST app creating application for socket %d", sock
);
3065 if ((msg
->bits_per_long
== 64 &&
3066 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3067 || (msg
->bits_per_long
== 32 &&
3068 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3069 ERR("Registration failed: application \"%s\" (pid: %d) has "
3070 "%d-bit long, but no consumerd for this size is available.\n",
3071 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3075 lta
= zmalloc(sizeof(struct ust_app
));
3081 lta
->ppid
= msg
->ppid
;
3082 lta
->uid
= msg
->uid
;
3083 lta
->gid
= msg
->gid
;
3085 lta
->bits_per_long
= msg
->bits_per_long
;
3086 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3087 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3088 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3089 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3090 lta
->long_alignment
= msg
->long_alignment
;
3091 lta
->byte_order
= msg
->byte_order
;
3093 lta
->v_major
= msg
->major
;
3094 lta
->v_minor
= msg
->minor
;
3095 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3096 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3097 lta
->notify_sock
= -1;
3099 /* Copy name and make sure it's NULL terminated. */
3100 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3101 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3104 * Before this can be called, when receiving the registration information,
3105 * the application compatibility is checked. So, at this point, the
3106 * application can work with this session daemon.
3108 lta
->compatible
= 1;
3110 lta
->pid
= msg
->pid
;
3111 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3113 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3115 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3122 * For a given application object, add it to every hash table.
3124 void ust_app_add(struct ust_app
*app
)
3127 assert(app
->notify_sock
>= 0);
3132 * On a re-registration, we want to kick out the previous registration of
3135 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3138 * The socket _should_ be unique until _we_ call close. So, a add_unique
3139 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3140 * already in the table.
3142 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3144 /* Add application to the notify socket hash table. */
3145 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3146 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3148 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3149 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3150 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3157 * Set the application version into the object.
3159 * Return 0 on success else a negative value either an errno code or a
3160 * LTTng-UST error code.
3162 int ust_app_version(struct ust_app
*app
)
3168 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3170 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3171 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3173 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3181 * Unregister app by removing it from the global traceable app list and freeing
3184 * The socket is already closed at this point so no close to sock.
3186 void ust_app_unregister(int sock
)
3188 struct ust_app
*lta
;
3189 struct lttng_ht_node_ulong
*node
;
3190 struct lttng_ht_iter ust_app_sock_iter
;
3191 struct lttng_ht_iter iter
;
3192 struct ust_app_session
*ua_sess
;
3197 /* Get the node reference for a call_rcu */
3198 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3199 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3202 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3203 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3206 * For per-PID buffers, perform "push metadata" and flush all
3207 * application streams before removing app from hash tables,
3208 * ensuring proper behavior of data_pending check.
3209 * Remove sessions so they are not visible during deletion.
3211 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3213 struct ust_registry_session
*registry
;
3215 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3217 /* The session was already removed so scheduled for teardown. */
3221 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3222 (void) ust_app_flush_app_session(lta
, ua_sess
);
3226 * Add session to list for teardown. This is safe since at this point we
3227 * are the only one using this list.
3229 pthread_mutex_lock(&ua_sess
->lock
);
3232 * Normally, this is done in the delete session process which is
3233 * executed in the call rcu below. However, upon registration we can't
3234 * afford to wait for the grace period before pushing data or else the
3235 * data pending feature can race between the unregistration and stop
3236 * command where the data pending command is sent *before* the grace
3239 * The close metadata below nullifies the metadata pointer in the
3240 * session so the delete session will NOT push/close a second time.
3242 registry
= get_session_registry(ua_sess
);
3244 /* Push metadata for application before freeing the application. */
3245 (void) push_metadata(registry
, ua_sess
->consumer
);
3248 * Don't ask to close metadata for global per UID buffers. Close
3249 * metadata only on destroy trace session in this case. Also, the
3250 * previous push metadata could have flag the metadata registry to
3251 * close so don't send a close command if closed.
3253 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3254 /* And ask to close it for this session registry. */
3255 (void) close_metadata(registry
, ua_sess
->consumer
);
3258 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3260 pthread_mutex_unlock(&ua_sess
->lock
);
3263 /* Remove application from PID hash table */
3264 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3268 * Remove application from notify hash table. The thread handling the
3269 * notify socket could have deleted the node so ignore on error because
3270 * either way it's valid. The close of that socket is handled by the other
3273 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3274 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3277 * Ignore return value since the node might have been removed before by an
3278 * add replace during app registration because the PID can be reassigned by
3281 iter
.iter
.node
= <a
->pid_n
.node
;
3282 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3284 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3289 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3296 * Fill events array with all events name of all registered apps.
3298 int ust_app_list_events(struct lttng_event
**events
)
3301 size_t nbmem
, count
= 0;
3302 struct lttng_ht_iter iter
;
3303 struct ust_app
*app
;
3304 struct lttng_event
*tmp_event
;
3306 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3307 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3308 if (tmp_event
== NULL
) {
3309 PERROR("zmalloc ust app events");
3316 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3317 struct lttng_ust_tracepoint_iter uiter
;
3319 health_code_update();
3321 if (!app
->compatible
) {
3323 * TODO: In time, we should notice the caller of this error by
3324 * telling him that this is a version error.
3328 handle
= ustctl_tracepoint_list(app
->sock
);
3330 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3331 ERR("UST app list events getting handle failed for app pid %d",
3337 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3338 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3339 /* Handle ustctl error. */
3341 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3342 ERR("UST app tp list get failed for app %d with ret %d",
3345 DBG3("UST app tp list get failed. Application is dead");
3347 * This is normal behavior, an application can die during the
3348 * creation process. Don't report an error so the execution can
3349 * continue normally. Continue normal execution.
3357 health_code_update();
3358 if (count
>= nbmem
) {
3359 /* In case the realloc fails, we free the memory */
3360 struct lttng_event
*new_tmp_event
;
3363 new_nbmem
= nbmem
<< 1;
3364 DBG2("Reallocating event list from %zu to %zu entries",
3366 new_tmp_event
= realloc(tmp_event
,
3367 new_nbmem
* sizeof(struct lttng_event
));
3368 if (new_tmp_event
== NULL
) {
3369 PERROR("realloc ust app events");
3374 /* Zero the new memory */
3375 memset(new_tmp_event
+ nbmem
, 0,
3376 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3378 tmp_event
= new_tmp_event
;
3380 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3381 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3382 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3383 tmp_event
[count
].pid
= app
->pid
;
3384 tmp_event
[count
].enabled
= -1;
3390 *events
= tmp_event
;
3392 DBG2("UST app list events done (%zu events)", count
);
3397 health_code_update();
3402 * Fill events array with all events name of all registered apps.
3404 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3407 size_t nbmem
, count
= 0;
3408 struct lttng_ht_iter iter
;
3409 struct ust_app
*app
;
3410 struct lttng_event_field
*tmp_event
;
3412 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3413 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3414 if (tmp_event
== NULL
) {
3415 PERROR("zmalloc ust app event fields");
3422 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3423 struct lttng_ust_field_iter uiter
;
3425 health_code_update();
3427 if (!app
->compatible
) {
3429 * TODO: In time, we should notice the caller of this error by
3430 * telling him that this is a version error.
3434 handle
= ustctl_tracepoint_field_list(app
->sock
);
3436 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3437 ERR("UST app list field getting handle failed for app pid %d",
3443 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3444 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3445 /* Handle ustctl error. */
3447 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3448 ERR("UST app tp list field failed for app %d with ret %d",
3451 DBG3("UST app tp list field failed. Application is dead");
3453 * This is normal behavior, an application can die during the
3454 * creation process. Don't report an error so the execution can
3455 * continue normally. Reset list and count for next app.
3463 health_code_update();
3464 if (count
>= nbmem
) {
3465 /* In case the realloc fails, we free the memory */
3466 struct lttng_event_field
*new_tmp_event
;
3469 new_nbmem
= nbmem
<< 1;
3470 DBG2("Reallocating event field list from %zu to %zu entries",
3472 new_tmp_event
= realloc(tmp_event
,
3473 new_nbmem
* sizeof(struct lttng_event_field
));
3474 if (new_tmp_event
== NULL
) {
3475 PERROR("realloc ust app event fields");
3480 /* Zero the new memory */
3481 memset(new_tmp_event
+ nbmem
, 0,
3482 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3484 tmp_event
= new_tmp_event
;
3487 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3488 /* Mapping between these enums matches 1 to 1. */
3489 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3490 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3492 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3493 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3494 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3495 tmp_event
[count
].event
.pid
= app
->pid
;
3496 tmp_event
[count
].event
.enabled
= -1;
3502 *fields
= tmp_event
;
3504 DBG2("UST app list event fields done (%zu events)", count
);
3509 health_code_update();
3514 * Free and clean all traceable apps of the global list.
3516 * Should _NOT_ be called with RCU read-side lock held.
3518 void ust_app_clean_list(void)
3521 struct ust_app
*app
;
3522 struct lttng_ht_iter iter
;
3524 DBG2("UST app cleaning registered apps hash table");
3529 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3530 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3532 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3536 /* Cleanup socket hash table */
3537 if (ust_app_ht_by_sock
) {
3538 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3540 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3545 /* Cleanup notify socket hash table */
3546 if (ust_app_ht_by_notify_sock
) {
3547 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3548 notify_sock_n
.node
) {
3549 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3555 /* Destroy is done only when the ht is empty */
3557 ht_cleanup_push(ust_app_ht
);
3559 if (ust_app_ht_by_sock
) {
3560 ht_cleanup_push(ust_app_ht_by_sock
);
3562 if (ust_app_ht_by_notify_sock
) {
3563 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3568 * Init UST app hash table.
3570 int ust_app_ht_alloc(void)
3572 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3576 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3577 if (!ust_app_ht_by_sock
) {
3580 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3581 if (!ust_app_ht_by_notify_sock
) {
3588 * For a specific UST session, disable the channel for all registered apps.
3590 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3591 struct ltt_ust_channel
*uchan
)
3594 struct lttng_ht_iter iter
;
3595 struct lttng_ht_node_str
*ua_chan_node
;
3596 struct ust_app
*app
;
3597 struct ust_app_session
*ua_sess
;
3598 struct ust_app_channel
*ua_chan
;
3600 if (usess
== NULL
|| uchan
== NULL
) {
3601 ERR("Disabling UST global channel with NULL values");
3606 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3607 uchan
->name
, usess
->id
);
3611 /* For every registered applications */
3612 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3613 struct lttng_ht_iter uiter
;
3614 if (!app
->compatible
) {
3616 * TODO: In time, we should notice the caller of this error by
3617 * telling him that this is a version error.
3621 ua_sess
= lookup_session_by_app(usess
, app
);
3622 if (ua_sess
== NULL
) {
3627 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3628 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3629 /* If the session if found for the app, the channel must be there */
3630 assert(ua_chan_node
);
3632 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3633 /* The channel must not be already disabled */
3634 assert(ua_chan
->enabled
== 1);
3636 /* Disable channel onto application */
3637 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3639 /* XXX: We might want to report this error at some point... */
3651 * For a specific UST session, enable the channel for all registered apps.
3653 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3654 struct ltt_ust_channel
*uchan
)
3657 struct lttng_ht_iter iter
;
3658 struct ust_app
*app
;
3659 struct ust_app_session
*ua_sess
;
3661 if (usess
== NULL
|| uchan
== NULL
) {
3662 ERR("Adding UST global channel to NULL values");
3667 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3668 uchan
->name
, usess
->id
);
3672 /* For every registered applications */
3673 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3674 if (!app
->compatible
) {
3676 * TODO: In time, we should notice the caller of this error by
3677 * telling him that this is a version error.
3681 ua_sess
= lookup_session_by_app(usess
, app
);
3682 if (ua_sess
== NULL
) {
3686 /* Enable channel onto application */
3687 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3689 /* XXX: We might want to report this error at some point... */
3701 * Disable an event in a channel and for a specific session.
3703 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3704 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3707 struct lttng_ht_iter iter
, uiter
;
3708 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3709 struct ust_app
*app
;
3710 struct ust_app_session
*ua_sess
;
3711 struct ust_app_channel
*ua_chan
;
3712 struct ust_app_event
*ua_event
;
3714 DBG("UST app disabling event %s for all apps in channel "
3715 "%s for session id %" PRIu64
,
3716 uevent
->attr
.name
, uchan
->name
, usess
->id
);
3720 /* For all registered applications */
3721 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3722 if (!app
->compatible
) {
3724 * TODO: In time, we should notice the caller of this error by
3725 * telling him that this is a version error.
3729 ua_sess
= lookup_session_by_app(usess
, app
);
3730 if (ua_sess
== NULL
) {
3735 /* Lookup channel in the ust app session */
3736 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3737 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3738 if (ua_chan_node
== NULL
) {
3739 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
3740 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
3743 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3745 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
3746 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
3747 if (ua_event_node
== NULL
) {
3748 DBG2("Event %s not found in channel %s for app pid %d."
3749 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
3752 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3754 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3756 /* XXX: Report error someday... */
3767 * For a specific UST session, create the channel for all registered apps.
3769 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
3770 struct ltt_ust_channel
*uchan
)
3772 int ret
= 0, created
;
3773 struct lttng_ht_iter iter
;
3774 struct ust_app
*app
;
3775 struct ust_app_session
*ua_sess
= NULL
;
3777 /* Very wrong code flow */
3781 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
3782 uchan
->name
, usess
->id
);
3786 /* For every registered applications */
3787 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3788 if (!app
->compatible
) {
3790 * TODO: In time, we should notice the caller of this error by
3791 * telling him that this is a version error.
3795 if (!trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
3801 * Create session on the tracer side and add it to app session HT. Note
3802 * that if session exist, it will simply return a pointer to the ust
3805 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
3810 * The application's socket is not valid. Either a bad socket
3811 * or a timeout on it. We can't inform the caller that for a
3812 * specific app, the session failed so lets continue here.
3817 goto error_rcu_unlock
;
3822 pthread_mutex_lock(&ua_sess
->lock
);
3823 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
3824 sizeof(uchan
->name
))) {
3825 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
3828 /* Create channel onto application. We don't need the chan ref. */
3829 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
3830 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
3832 pthread_mutex_unlock(&ua_sess
->lock
);
3834 if (ret
== -ENOMEM
) {
3835 /* No more memory is a fatal error. Stop right now. */
3836 goto error_rcu_unlock
;
3838 /* Cleanup the created session if it's the case. */
3840 destroy_app_session(app
, ua_sess
);
3851 * Enable event for a specific session and channel on the tracer.
3853 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
3854 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3857 struct lttng_ht_iter iter
, uiter
;
3858 struct lttng_ht_node_str
*ua_chan_node
;
3859 struct ust_app
*app
;
3860 struct ust_app_session
*ua_sess
;
3861 struct ust_app_channel
*ua_chan
;
3862 struct ust_app_event
*ua_event
;
3864 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
3865 uevent
->attr
.name
, usess
->id
);
3868 * NOTE: At this point, this function is called only if the session and
3869 * channel passed are already created for all apps. and enabled on the
3875 /* For all registered applications */
3876 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3877 if (!app
->compatible
) {
3879 * TODO: In time, we should notice the caller of this error by
3880 * telling him that this is a version error.
3884 ua_sess
= lookup_session_by_app(usess
, app
);
3886 /* The application has problem or is probably dead. */
3890 pthread_mutex_lock(&ua_sess
->lock
);
3892 /* Lookup channel in the ust app session */
3893 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3894 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3895 /* If the channel is not found, there is a code flow error */
3896 assert(ua_chan_node
);
3898 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3900 /* Get event node */
3901 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3902 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3903 if (ua_event
== NULL
) {
3904 DBG3("UST app enable event %s not found for app PID %d."
3905 "Skipping app", uevent
->attr
.name
, app
->pid
);
3909 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
3911 pthread_mutex_unlock(&ua_sess
->lock
);
3915 pthread_mutex_unlock(&ua_sess
->lock
);
3924 * For a specific existing UST session and UST channel, creates the event for
3925 * all registered apps.
3927 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
3928 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3931 struct lttng_ht_iter iter
, uiter
;
3932 struct lttng_ht_node_str
*ua_chan_node
;
3933 struct ust_app
*app
;
3934 struct ust_app_session
*ua_sess
;
3935 struct ust_app_channel
*ua_chan
;
3937 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
3938 uevent
->attr
.name
, usess
->id
);
3942 /* For all registered applications */
3943 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3944 if (!app
->compatible
) {
3946 * TODO: In time, we should notice the caller of this error by
3947 * telling him that this is a version error.
3951 ua_sess
= lookup_session_by_app(usess
, app
);
3953 /* The application has problem or is probably dead. */
3957 pthread_mutex_lock(&ua_sess
->lock
);
3958 /* Lookup channel in the ust app session */
3959 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3960 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3961 /* If the channel is not found, there is a code flow error */
3962 assert(ua_chan_node
);
3964 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3966 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
3967 pthread_mutex_unlock(&ua_sess
->lock
);
3969 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
3970 /* Possible value at this point: -ENOMEM. If so, we stop! */
3973 DBG2("UST app event %s already exist on app PID %d",
3974 uevent
->attr
.name
, app
->pid
);
3985 * Start tracing for a specific UST session and app.
3988 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
3991 struct ust_app_session
*ua_sess
;
3993 DBG("Starting tracing for ust app pid %d", app
->pid
);
3997 if (!app
->compatible
) {
4001 ua_sess
= lookup_session_by_app(usess
, app
);
4002 if (ua_sess
== NULL
) {
4003 /* The session is in teardown process. Ignore and continue. */
4007 pthread_mutex_lock(&ua_sess
->lock
);
4009 /* Upon restart, we skip the setup, already done */
4010 if (ua_sess
->started
) {
4014 /* Create directories if consumer is LOCAL and has a path defined. */
4015 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
4016 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
4017 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
4018 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
4020 if (ret
!= -EEXIST
) {
4021 ERR("Trace directory creation error");
4028 * Create the metadata for the application. This returns gracefully if a
4029 * metadata was already set for the session.
4031 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4036 health_code_update();
4039 /* This start the UST tracing */
4040 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4042 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4043 ERR("Error starting tracing for app pid: %d (ret: %d)",
4046 DBG("UST app start session failed. Application is dead.");
4048 * This is normal behavior, an application can die during the
4049 * creation process. Don't report an error so the execution can
4050 * continue normally.
4052 pthread_mutex_unlock(&ua_sess
->lock
);
4058 /* Indicate that the session has been started once */
4059 ua_sess
->started
= 1;
4061 pthread_mutex_unlock(&ua_sess
->lock
);
4063 health_code_update();
4065 /* Quiescent wait after starting trace */
4066 ret
= ustctl_wait_quiescent(app
->sock
);
4067 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4068 ERR("UST app wait quiescent failed for app pid %d ret %d",
4074 health_code_update();
4078 pthread_mutex_unlock(&ua_sess
->lock
);
4080 health_code_update();
4085 * Stop tracing for a specific UST session and app.
4088 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4091 struct ust_app_session
*ua_sess
;
4092 struct ust_registry_session
*registry
;
4094 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4098 if (!app
->compatible
) {
4099 goto end_no_session
;
4102 ua_sess
= lookup_session_by_app(usess
, app
);
4103 if (ua_sess
== NULL
) {
4104 goto end_no_session
;
4107 pthread_mutex_lock(&ua_sess
->lock
);
4110 * If started = 0, it means that stop trace has been called for a session
4111 * that was never started. It's possible since we can have a fail start
4112 * from either the application manager thread or the command thread. Simply
4113 * indicate that this is a stop error.
4115 if (!ua_sess
->started
) {
4116 goto error_rcu_unlock
;
4119 health_code_update();
4121 /* This inhibits UST tracing */
4122 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4124 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4125 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4128 DBG("UST app stop session failed. Application is dead.");
4130 * This is normal behavior, an application can die during the
4131 * creation process. Don't report an error so the execution can
4132 * continue normally.
4136 goto error_rcu_unlock
;
4139 health_code_update();
4141 /* Quiescent wait after stopping trace */
4142 ret
= ustctl_wait_quiescent(app
->sock
);
4143 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4144 ERR("UST app wait quiescent failed for app pid %d ret %d",
4148 health_code_update();
4150 registry
= get_session_registry(ua_sess
);
4153 /* Push metadata for application before freeing the application. */
4154 (void) push_metadata(registry
, ua_sess
->consumer
);
4157 pthread_mutex_unlock(&ua_sess
->lock
);
4160 health_code_update();
4164 pthread_mutex_unlock(&ua_sess
->lock
);
4166 health_code_update();
4171 int ust_app_flush_app_session(struct ust_app
*app
,
4172 struct ust_app_session
*ua_sess
)
4174 int ret
, retval
= 0;
4175 struct lttng_ht_iter iter
;
4176 struct ust_app_channel
*ua_chan
;
4177 struct consumer_socket
*socket
;
4179 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4183 if (!app
->compatible
) {
4184 goto end_not_compatible
;
4187 pthread_mutex_lock(&ua_sess
->lock
);
4189 health_code_update();
4191 /* Flushing buffers */
4192 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4195 /* Flush buffers and push metadata. */
4196 switch (ua_sess
->buffer_type
) {
4197 case LTTNG_BUFFER_PER_PID
:
4198 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4200 health_code_update();
4201 assert(ua_chan
->is_sent
);
4202 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4204 ERR("Error flushing consumer channel");
4210 case LTTNG_BUFFER_PER_UID
:
4216 health_code_update();
4218 pthread_mutex_unlock(&ua_sess
->lock
);
4222 health_code_update();
4227 * Flush buffers for all applications for a specific UST session.
4228 * Called with UST session lock held.
4231 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4236 DBG("Flushing session buffers for all ust apps");
4240 /* Flush buffers and push metadata. */
4241 switch (usess
->buffer_type
) {
4242 case LTTNG_BUFFER_PER_UID
:
4244 struct buffer_reg_uid
*reg
;
4245 struct lttng_ht_iter iter
;
4247 /* Flush all per UID buffers associated to that session. */
4248 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4249 struct ust_registry_session
*ust_session_reg
;
4250 struct buffer_reg_channel
*reg_chan
;
4251 struct consumer_socket
*socket
;
4253 /* Get consumer socket to use to push the metadata.*/
4254 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4257 /* Ignore request if no consumer is found for the session. */
4261 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4262 reg_chan
, node
.node
) {
4264 * The following call will print error values so the return
4265 * code is of little importance because whatever happens, we
4266 * have to try them all.
4268 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4271 ust_session_reg
= reg
->registry
->reg
.ust
;
4272 /* Push metadata. */
4273 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4277 case LTTNG_BUFFER_PER_PID
:
4279 struct ust_app_session
*ua_sess
;
4280 struct lttng_ht_iter iter
;
4281 struct ust_app
*app
;
4283 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4284 ua_sess
= lookup_session_by_app(usess
, app
);
4285 if (ua_sess
== NULL
) {
4288 (void) ust_app_flush_app_session(app
, ua_sess
);
4299 health_code_update();
4304 * Destroy a specific UST session in apps.
4306 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4309 struct ust_app_session
*ua_sess
;
4310 struct lttng_ht_iter iter
;
4311 struct lttng_ht_node_u64
*node
;
4313 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4317 if (!app
->compatible
) {
4321 __lookup_session_by_app(usess
, app
, &iter
);
4322 node
= lttng_ht_iter_get_node_u64(&iter
);
4324 /* Session is being or is deleted. */
4327 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4329 health_code_update();
4330 destroy_app_session(app
, ua_sess
);
4332 health_code_update();
4334 /* Quiescent wait after stopping trace */
4335 ret
= ustctl_wait_quiescent(app
->sock
);
4336 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4337 ERR("UST app wait quiescent failed for app pid %d ret %d",
4342 health_code_update();
4347 * Start tracing for the UST session.
4349 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4352 struct lttng_ht_iter iter
;
4353 struct ust_app
*app
;
4355 DBG("Starting all UST traces");
4359 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4360 ret
= ust_app_start_trace(usess
, app
);
4362 /* Continue to next apps even on error */
4373 * Start tracing for the UST session.
4374 * Called with UST session lock held.
4376 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4379 struct lttng_ht_iter iter
;
4380 struct ust_app
*app
;
4382 DBG("Stopping all UST traces");
4386 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4387 ret
= ust_app_stop_trace(usess
, app
);
4389 /* Continue to next apps even on error */
4394 (void) ust_app_flush_session(usess
);
4402 * Destroy app UST session.
4404 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4407 struct lttng_ht_iter iter
;
4408 struct ust_app
*app
;
4410 DBG("Destroy all UST traces");
4414 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4415 ret
= destroy_trace(usess
, app
);
4417 /* Continue to next apps even on error */
4428 void ust_app_global_create(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4431 struct lttng_ht_iter iter
, uiter
;
4432 struct ust_app_session
*ua_sess
= NULL
;
4433 struct ust_app_channel
*ua_chan
;
4434 struct ust_app_event
*ua_event
;
4435 struct ust_app_ctx
*ua_ctx
;
4438 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &is_created
);
4440 /* Tracer is probably gone or ENOMEM. */
4444 /* App session already created. */
4449 pthread_mutex_lock(&ua_sess
->lock
);
4452 * We can iterate safely here over all UST app session since the create ust
4453 * app session above made a shadow copy of the UST global domain from the
4456 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4458 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4461 * Stop everything. On error, the application failed, no more
4462 * file descriptor are available or ENOMEM so stopping here is
4463 * the only thing we can do for now.
4469 * Add context using the list so they are enabled in the same order the
4472 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
4473 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
4480 /* For each events */
4481 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
4483 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
4490 pthread_mutex_unlock(&ua_sess
->lock
);
4492 if (usess
->active
) {
4493 ret
= ust_app_start_trace(usess
, app
);
4498 DBG2("UST trace started for app pid %d", app
->pid
);
4501 /* Everything went well at this point. */
4505 pthread_mutex_unlock(&ua_sess
->lock
);
4508 destroy_app_session(app
, ua_sess
);
4514 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4516 struct ust_app_session
*ua_sess
;
4518 ua_sess
= lookup_session_by_app(usess
, app
);
4519 if (ua_sess
== NULL
) {
4522 destroy_app_session(app
, ua_sess
);
4526 * Add channels/events from UST global domain to registered apps at sock.
4528 * Called with session lock held.
4529 * Called with RCU read-side lock held.
4531 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4535 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
4536 app
->sock
, usess
->id
);
4538 if (!app
->compatible
) {
4542 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4543 ust_app_global_create(usess
, app
);
4545 ust_app_global_destroy(usess
, app
);
4550 * Called with session lock held.
4552 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
4554 struct lttng_ht_iter iter
;
4555 struct ust_app
*app
;
4558 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4559 ust_app_global_update(usess
, app
);
4565 * Add context to a specific channel for global UST domain.
4567 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
4568 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
4571 struct lttng_ht_node_str
*ua_chan_node
;
4572 struct lttng_ht_iter iter
, uiter
;
4573 struct ust_app_channel
*ua_chan
= NULL
;
4574 struct ust_app_session
*ua_sess
;
4575 struct ust_app
*app
;
4579 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4580 if (!app
->compatible
) {
4582 * TODO: In time, we should notice the caller of this error by
4583 * telling him that this is a version error.
4587 ua_sess
= lookup_session_by_app(usess
, app
);
4588 if (ua_sess
== NULL
) {
4592 pthread_mutex_lock(&ua_sess
->lock
);
4593 /* Lookup channel in the ust app session */
4594 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4595 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4596 if (ua_chan_node
== NULL
) {
4599 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4601 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4606 pthread_mutex_unlock(&ua_sess
->lock
);
4614 * Enable event for a channel from a UST session for a specific PID.
4616 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4617 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4620 struct lttng_ht_iter iter
;
4621 struct lttng_ht_node_str
*ua_chan_node
;
4622 struct ust_app
*app
;
4623 struct ust_app_session
*ua_sess
;
4624 struct ust_app_channel
*ua_chan
;
4625 struct ust_app_event
*ua_event
;
4627 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4631 app
= ust_app_find_by_pid(pid
);
4633 ERR("UST app enable event per PID %d not found", pid
);
4638 if (!app
->compatible
) {
4643 ua_sess
= lookup_session_by_app(usess
, app
);
4645 /* The application has problem or is probably dead. */
4650 pthread_mutex_lock(&ua_sess
->lock
);
4651 /* Lookup channel in the ust app session */
4652 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4653 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4654 /* If the channel is not found, there is a code flow error */
4655 assert(ua_chan_node
);
4657 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4659 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4660 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4661 if (ua_event
== NULL
) {
4662 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4667 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4674 pthread_mutex_unlock(&ua_sess
->lock
);
4681 * Calibrate registered applications.
4683 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
4686 struct lttng_ht_iter iter
;
4687 struct ust_app
*app
;
4691 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4692 if (!app
->compatible
) {
4694 * TODO: In time, we should notice the caller of this error by
4695 * telling him that this is a version error.
4700 health_code_update();
4702 ret
= ustctl_calibrate(app
->sock
, calibrate
);
4706 /* Means that it's not implemented on the tracer side. */
4710 DBG2("Calibrate app PID %d returned with error %d",
4717 DBG("UST app global domain calibration finished");
4721 health_code_update();
4727 * Receive registration and populate the given msg structure.
4729 * On success return 0 else a negative value returned by the ustctl call.
4731 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
4734 uint32_t pid
, ppid
, uid
, gid
;
4738 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
4739 &pid
, &ppid
, &uid
, &gid
,
4740 &msg
->bits_per_long
,
4741 &msg
->uint8_t_alignment
,
4742 &msg
->uint16_t_alignment
,
4743 &msg
->uint32_t_alignment
,
4744 &msg
->uint64_t_alignment
,
4745 &msg
->long_alignment
,
4752 case LTTNG_UST_ERR_EXITING
:
4753 DBG3("UST app recv reg message failed. Application died");
4755 case LTTNG_UST_ERR_UNSUP_MAJOR
:
4756 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
4757 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
4758 LTTNG_UST_ABI_MINOR_VERSION
);
4761 ERR("UST app recv reg message failed with ret %d", ret
);
4766 msg
->pid
= (pid_t
) pid
;
4767 msg
->ppid
= (pid_t
) ppid
;
4768 msg
->uid
= (uid_t
) uid
;
4769 msg
->gid
= (gid_t
) gid
;
4776 * Return a ust app channel object using the application object and the channel
4777 * object descriptor has a key. If not found, NULL is returned. A RCU read side
4778 * lock MUST be acquired before calling this function.
4780 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
4783 struct lttng_ht_node_ulong
*node
;
4784 struct lttng_ht_iter iter
;
4785 struct ust_app_channel
*ua_chan
= NULL
;
4789 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
4790 node
= lttng_ht_iter_get_node_ulong(&iter
);
4792 DBG2("UST app channel find by objd %d not found", objd
);
4796 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
4803 * Reply to a register channel notification from an application on the notify
4804 * socket. The channel metadata is also created.
4806 * The session UST registry lock is acquired in this function.
4808 * On success 0 is returned else a negative value.
4810 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
4811 size_t nr_fields
, struct ustctl_field
*fields
)
4813 int ret
, ret_code
= 0;
4814 uint32_t chan_id
, reg_count
;
4815 uint64_t chan_reg_key
;
4816 enum ustctl_channel_header type
;
4817 struct ust_app
*app
;
4818 struct ust_app_channel
*ua_chan
;
4819 struct ust_app_session
*ua_sess
;
4820 struct ust_registry_session
*registry
;
4821 struct ust_registry_channel
*chan_reg
;
4825 /* Lookup application. If not found, there is a code flow error. */
4826 app
= find_app_by_notify_sock(sock
);
4828 DBG("Application socket %d is being teardown. Abort event notify",
4832 goto error_rcu_unlock
;
4835 /* Lookup channel by UST object descriptor. */
4836 ua_chan
= find_channel_by_objd(app
, cobjd
);
4838 DBG("Application channel is being teardown. Abort event notify");
4841 goto error_rcu_unlock
;
4844 assert(ua_chan
->session
);
4845 ua_sess
= ua_chan
->session
;
4847 /* Get right session registry depending on the session buffer type. */
4848 registry
= get_session_registry(ua_sess
);
4851 /* Depending on the buffer type, a different channel key is used. */
4852 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4853 chan_reg_key
= ua_chan
->tracing_channel_id
;
4855 chan_reg_key
= ua_chan
->key
;
4858 pthread_mutex_lock(®istry
->lock
);
4860 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
4863 if (!chan_reg
->register_done
) {
4864 reg_count
= ust_registry_get_event_count(chan_reg
);
4865 if (reg_count
< 31) {
4866 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
4868 type
= USTCTL_CHANNEL_HEADER_LARGE
;
4871 chan_reg
->nr_ctx_fields
= nr_fields
;
4872 chan_reg
->ctx_fields
= fields
;
4873 chan_reg
->header_type
= type
;
4875 /* Get current already assigned values. */
4876 type
= chan_reg
->header_type
;
4878 /* Set to NULL so the error path does not do a double free. */
4881 /* Channel id is set during the object creation. */
4882 chan_id
= chan_reg
->chan_id
;
4884 /* Append to metadata */
4885 if (!chan_reg
->metadata_dumped
) {
4886 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
4888 ERR("Error appending channel metadata (errno = %d)", ret_code
);
4894 DBG3("UST app replying to register channel key %" PRIu64
4895 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
4898 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
4900 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4901 ERR("UST app reply channel failed with ret %d", ret
);
4903 DBG3("UST app reply channel failed. Application died");
4908 /* This channel registry registration is completed. */
4909 chan_reg
->register_done
= 1;
4912 pthread_mutex_unlock(®istry
->lock
);
4922 * Add event to the UST channel registry. When the event is added to the
4923 * registry, the metadata is also created. Once done, this replies to the
4924 * application with the appropriate error code.
4926 * The session UST registry lock is acquired in the function.
4928 * On success 0 is returned else a negative value.
4930 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
4931 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
4932 char *model_emf_uri
)
4935 uint32_t event_id
= 0;
4936 uint64_t chan_reg_key
;
4937 struct ust_app
*app
;
4938 struct ust_app_channel
*ua_chan
;
4939 struct ust_app_session
*ua_sess
;
4940 struct ust_registry_session
*registry
;
4944 /* Lookup application. If not found, there is a code flow error. */
4945 app
= find_app_by_notify_sock(sock
);
4947 DBG("Application socket %d is being teardown. Abort event notify",
4952 free(model_emf_uri
);
4953 goto error_rcu_unlock
;
4956 /* Lookup channel by UST object descriptor. */
4957 ua_chan
= find_channel_by_objd(app
, cobjd
);
4959 DBG("Application channel is being teardown. Abort event notify");
4963 free(model_emf_uri
);
4964 goto error_rcu_unlock
;
4967 assert(ua_chan
->session
);
4968 ua_sess
= ua_chan
->session
;
4970 registry
= get_session_registry(ua_sess
);
4973 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4974 chan_reg_key
= ua_chan
->tracing_channel_id
;
4976 chan_reg_key
= ua_chan
->key
;
4979 pthread_mutex_lock(®istry
->lock
);
4982 * From this point on, this call acquires the ownership of the sig, fields
4983 * and model_emf_uri meaning any free are done inside it if needed. These
4984 * three variables MUST NOT be read/write after this.
4986 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
4987 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
, loglevel
,
4988 model_emf_uri
, ua_sess
->buffer_type
, &event_id
,
4992 * The return value is returned to ustctl so in case of an error, the
4993 * application can be notified. In case of an error, it's important not to
4994 * return a negative error or else the application will get closed.
4996 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
4998 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4999 ERR("UST app reply event failed with ret %d", ret
);
5001 DBG3("UST app reply event failed. Application died");
5004 * No need to wipe the create event since the application socket will
5005 * get close on error hence cleaning up everything by itself.
5010 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5014 pthread_mutex_unlock(®istry
->lock
);
5021 * Handle application notification through the given notify socket.
5023 * Return 0 on success or else a negative value.
5025 int ust_app_recv_notify(int sock
)
5028 enum ustctl_notify_cmd cmd
;
5030 DBG3("UST app receiving notify from sock %d", sock
);
5032 ret
= ustctl_recv_notify(sock
, &cmd
);
5034 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5035 ERR("UST app recv notify failed with ret %d", ret
);
5037 DBG3("UST app recv notify failed. Application died");
5043 case USTCTL_NOTIFY_CMD_EVENT
:
5045 int sobjd
, cobjd
, loglevel
;
5046 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5048 struct ustctl_field
*fields
;
5050 DBG2("UST app ustctl register event received");
5052 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
5053 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
5055 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5056 ERR("UST app recv event failed with ret %d", ret
);
5058 DBG3("UST app recv event failed. Application died");
5064 * Add event to the UST registry coming from the notify socket. This
5065 * call will free if needed the sig, fields and model_emf_uri. This
5066 * code path loses the ownsership of these variables and transfer them
5067 * to the this function.
5069 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5070 fields
, loglevel
, model_emf_uri
);
5077 case USTCTL_NOTIFY_CMD_CHANNEL
:
5081 struct ustctl_field
*fields
;
5083 DBG2("UST app ustctl register channel received");
5085 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5088 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5089 ERR("UST app recv channel failed with ret %d", ret
);
5091 DBG3("UST app recv channel failed. Application died");
5097 * The fields ownership are transfered to this function call meaning
5098 * that if needed it will be freed. After this, it's invalid to access
5099 * fields or clean it up.
5101 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
5110 /* Should NEVER happen. */
5119 * Once the notify socket hangs up, this is called. First, it tries to find the
5120 * corresponding application. On failure, the call_rcu to close the socket is
5121 * executed. If an application is found, it tries to delete it from the notify
5122 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5124 * Note that an object needs to be allocated here so on ENOMEM failure, the
5125 * call RCU is not done but the rest of the cleanup is.
5127 void ust_app_notify_sock_unregister(int sock
)
5130 struct lttng_ht_iter iter
;
5131 struct ust_app
*app
;
5132 struct ust_app_notify_sock_obj
*obj
;
5138 obj
= zmalloc(sizeof(*obj
));
5141 * An ENOMEM is kind of uncool. If this strikes we continue the
5142 * procedure but the call_rcu will not be called. In this case, we
5143 * accept the fd leak rather than possibly creating an unsynchronized
5144 * state between threads.
5146 * TODO: The notify object should be created once the notify socket is
5147 * registered and stored independantely from the ust app object. The
5148 * tricky part is to synchronize the teardown of the application and
5149 * this notify object. Let's keep that in mind so we can avoid this
5150 * kind of shenanigans with ENOMEM in the teardown path.
5157 DBG("UST app notify socket unregister %d", sock
);
5160 * Lookup application by notify socket. If this fails, this means that the
5161 * hash table delete has already been done by the application
5162 * unregistration process so we can safely close the notify socket in a
5165 app
= find_app_by_notify_sock(sock
);
5170 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5173 * Whatever happens here either we fail or succeed, in both cases we have
5174 * to close the socket after a grace period to continue to the call RCU
5175 * here. If the deletion is successful, the application is not visible
5176 * anymore by other threads and is it fails it means that it was already
5177 * deleted from the hash table so either way we just have to close the
5180 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5186 * Close socket after a grace period to avoid for the socket to be reused
5187 * before the application object is freed creating potential race between
5188 * threads trying to add unique in the global hash table.
5191 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5196 * Destroy a ust app data structure and free its memory.
5198 void ust_app_destroy(struct ust_app
*app
)
5204 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5208 * Take a snapshot for a given UST session. The snapshot is sent to the given
5211 * Return 0 on success or else a negative value.
5213 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5214 struct snapshot_output
*output
, int wait
,
5215 uint64_t nb_packets_per_stream
)
5218 unsigned int snapshot_done
= 0;
5219 struct lttng_ht_iter iter
;
5220 struct ust_app
*app
;
5221 char pathname
[PATH_MAX
];
5228 switch (usess
->buffer_type
) {
5229 case LTTNG_BUFFER_PER_UID
:
5231 struct buffer_reg_uid
*reg
;
5233 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5234 struct buffer_reg_channel
*reg_chan
;
5235 struct consumer_socket
*socket
;
5237 /* Get consumer socket to use to push the metadata.*/
5238 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5245 memset(pathname
, 0, sizeof(pathname
));
5246 ret
= snprintf(pathname
, sizeof(pathname
),
5247 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5248 reg
->uid
, reg
->bits_per_long
);
5250 PERROR("snprintf snapshot path");
5254 /* Add the UST default trace dir to path. */
5255 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5256 reg_chan
, node
.node
) {
5257 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5258 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5259 nb_packets_per_stream
);
5264 ret
= consumer_snapshot_channel(socket
,
5265 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5266 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
5274 case LTTNG_BUFFER_PER_PID
:
5276 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5277 struct consumer_socket
*socket
;
5278 struct lttng_ht_iter chan_iter
;
5279 struct ust_app_channel
*ua_chan
;
5280 struct ust_app_session
*ua_sess
;
5281 struct ust_registry_session
*registry
;
5283 ua_sess
= lookup_session_by_app(usess
, app
);
5285 /* Session not associated with this app. */
5289 /* Get the right consumer socket for the application. */
5290 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5297 /* Add the UST default trace dir to path. */
5298 memset(pathname
, 0, sizeof(pathname
));
5299 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5302 PERROR("snprintf snapshot path");
5306 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5307 ua_chan
, node
.node
) {
5308 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
5309 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
5310 nb_packets_per_stream
);
5316 registry
= get_session_registry(ua_sess
);
5318 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
5319 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
5332 if (!snapshot_done
) {
5334 * If no snapshot was made and we are not in the error path, this means
5335 * that there are no buffers thus no (prior) application to snapshot
5336 * data from so we have simply NO data.
5347 * Return the size taken by one more packet per stream.
5349 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
5350 uint64_t cur_nr_packets
)
5352 uint64_t tot_size
= 0;
5353 struct ust_app
*app
;
5354 struct lttng_ht_iter iter
;
5358 switch (usess
->buffer_type
) {
5359 case LTTNG_BUFFER_PER_UID
:
5361 struct buffer_reg_uid
*reg
;
5363 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5364 struct buffer_reg_channel
*reg_chan
;
5367 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5368 reg_chan
, node
.node
) {
5369 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
5371 * Don't take channel into account if we
5372 * already grab all its packets.
5376 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
5382 case LTTNG_BUFFER_PER_PID
:
5385 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5386 struct ust_app_channel
*ua_chan
;
5387 struct ust_app_session
*ua_sess
;
5388 struct lttng_ht_iter chan_iter
;
5390 ua_sess
= lookup_session_by_app(usess
, app
);
5392 /* Session not associated with this app. */
5396 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5397 ua_chan
, node
.node
) {
5398 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
5400 * Don't take channel into account if we
5401 * already grab all its packets.
5405 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;