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 assert(!ua_sess
->deleted
);
681 ua_sess
->deleted
= true;
683 registry
= get_session_registry(ua_sess
);
685 /* Push metadata for application before freeing the application. */
686 (void) push_metadata(registry
, ua_sess
->consumer
);
689 * Don't ask to close metadata for global per UID buffers. Close
690 * metadata only on destroy trace session in this case. Also, the
691 * previous push metadata could have flag the metadata registry to
692 * close so don't send a close command if closed.
694 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
695 /* And ask to close it for this session registry. */
696 (void) close_metadata(registry
, ua_sess
->consumer
);
700 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
702 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
704 delete_ust_app_channel(sock
, ua_chan
, app
);
707 /* In case of per PID, the registry is kept in the session. */
708 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
709 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
711 buffer_reg_pid_remove(reg_pid
);
712 buffer_reg_pid_destroy(reg_pid
);
716 if (ua_sess
->handle
!= -1) {
717 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
718 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
719 ERR("UST app sock %d release session handle failed with ret %d",
723 pthread_mutex_unlock(&ua_sess
->lock
);
725 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
729 * Delete a traceable application structure from the global list. Never call
730 * this function outside of a call_rcu call.
732 * RCU read side lock should _NOT_ be held when calling this function.
735 void delete_ust_app(struct ust_app
*app
)
738 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
740 /* Delete ust app sessions info */
745 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
747 /* Free every object in the session and the session. */
749 delete_ust_app_session(sock
, ua_sess
, app
);
753 ht_cleanup_push(app
->sessions
);
754 ht_cleanup_push(app
->ust_objd
);
757 * Wait until we have deleted the application from the sock hash table
758 * before closing this socket, otherwise an application could re-use the
759 * socket ID and race with the teardown, using the same hash table entry.
761 * It's OK to leave the close in call_rcu. We want it to stay unique for
762 * all RCU readers that could run concurrently with unregister app,
763 * therefore we _need_ to only close that socket after a grace period. So
764 * it should stay in this RCU callback.
766 * This close() is a very important step of the synchronization model so
767 * every modification to this function must be carefully reviewed.
773 lttng_fd_put(LTTNG_FD_APPS
, 1);
775 DBG2("UST app pid %d deleted", app
->pid
);
780 * URCU intermediate call to delete an UST app.
783 void delete_ust_app_rcu(struct rcu_head
*head
)
785 struct lttng_ht_node_ulong
*node
=
786 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
787 struct ust_app
*app
=
788 caa_container_of(node
, struct ust_app
, pid_n
);
790 DBG3("Call RCU deleting app PID %d", app
->pid
);
795 * Delete the session from the application ht and delete the data structure by
796 * freeing every object inside and releasing them.
798 static void destroy_app_session(struct ust_app
*app
,
799 struct ust_app_session
*ua_sess
)
802 struct lttng_ht_iter iter
;
807 iter
.iter
.node
= &ua_sess
->node
.node
;
808 ret
= lttng_ht_del(app
->sessions
, &iter
);
810 /* Already scheduled for teardown. */
814 /* Once deleted, free the data structure. */
815 delete_ust_app_session(app
->sock
, ua_sess
, app
);
822 * Alloc new UST app session.
825 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
827 struct ust_app_session
*ua_sess
;
829 /* Init most of the default value by allocating and zeroing */
830 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
831 if (ua_sess
== NULL
) {
836 ua_sess
->handle
= -1;
837 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
838 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
839 pthread_mutex_init(&ua_sess
->lock
, NULL
);
848 * Alloc new UST app channel.
851 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
852 struct ust_app_session
*ua_sess
,
853 struct lttng_ust_channel_attr
*attr
)
855 struct ust_app_channel
*ua_chan
;
857 /* Init most of the default value by allocating and zeroing */
858 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
859 if (ua_chan
== NULL
) {
864 /* Setup channel name */
865 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
866 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
868 ua_chan
->enabled
= 1;
869 ua_chan
->handle
= -1;
870 ua_chan
->session
= ua_sess
;
871 ua_chan
->key
= get_next_channel_key();
872 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
873 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
874 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
876 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
877 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
879 /* Copy attributes */
881 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
882 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
883 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
884 ua_chan
->attr
.overwrite
= attr
->overwrite
;
885 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
886 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
887 ua_chan
->attr
.output
= attr
->output
;
889 /* By default, the channel is a per cpu channel. */
890 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
892 DBG3("UST app channel %s allocated", ua_chan
->name
);
901 * Allocate and initialize a UST app stream.
903 * Return newly allocated stream pointer or NULL on error.
905 struct ust_app_stream
*ust_app_alloc_stream(void)
907 struct ust_app_stream
*stream
= NULL
;
909 stream
= zmalloc(sizeof(*stream
));
910 if (stream
== NULL
) {
911 PERROR("zmalloc ust app stream");
915 /* Zero could be a valid value for a handle so flag it to -1. */
923 * Alloc new UST app event.
926 struct ust_app_event
*alloc_ust_app_event(char *name
,
927 struct lttng_ust_event
*attr
)
929 struct ust_app_event
*ua_event
;
931 /* Init most of the default value by allocating and zeroing */
932 ua_event
= zmalloc(sizeof(struct ust_app_event
));
933 if (ua_event
== NULL
) {
938 ua_event
->enabled
= 1;
939 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
940 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
941 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
943 /* Copy attributes */
945 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
948 DBG3("UST app event %s allocated", ua_event
->name
);
957 * Alloc new UST app context.
960 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
962 struct ust_app_ctx
*ua_ctx
;
964 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
965 if (ua_ctx
== NULL
) {
969 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
972 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
975 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
982 * Allocate a filter and copy the given original filter.
984 * Return allocated filter or NULL on error.
986 static struct lttng_filter_bytecode
*copy_filter_bytecode(
987 struct lttng_filter_bytecode
*orig_f
)
989 struct lttng_filter_bytecode
*filter
= NULL
;
991 /* Copy filter bytecode */
992 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
994 PERROR("zmalloc alloc filter bytecode");
998 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1005 * Create a liblttng-ust filter bytecode from given bytecode.
1007 * Return allocated filter or NULL on error.
1009 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1010 struct lttng_filter_bytecode
*orig_f
)
1012 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1014 /* Copy filter bytecode */
1015 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1017 PERROR("zmalloc alloc ust filter bytecode");
1021 assert(sizeof(struct lttng_filter_bytecode
) ==
1022 sizeof(struct lttng_ust_filter_bytecode
));
1023 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1029 * Find an ust_app using the sock and return it. RCU read side lock must be
1030 * held before calling this helper function.
1032 struct ust_app
*ust_app_find_by_sock(int sock
)
1034 struct lttng_ht_node_ulong
*node
;
1035 struct lttng_ht_iter iter
;
1037 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1038 node
= lttng_ht_iter_get_node_ulong(&iter
);
1040 DBG2("UST app find by sock %d not found", sock
);
1044 return caa_container_of(node
, struct ust_app
, sock_n
);
1051 * Find an ust_app using the notify sock and return it. RCU read side lock must
1052 * be held before calling this helper function.
1054 static struct ust_app
*find_app_by_notify_sock(int sock
)
1056 struct lttng_ht_node_ulong
*node
;
1057 struct lttng_ht_iter iter
;
1059 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1061 node
= lttng_ht_iter_get_node_ulong(&iter
);
1063 DBG2("UST app find by notify sock %d not found", sock
);
1067 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1074 * Lookup for an ust app event based on event name, filter bytecode and the
1077 * Return an ust_app_event object or NULL on error.
1079 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1080 char *name
, struct lttng_filter_bytecode
*filter
, int loglevel
,
1081 const struct lttng_event_exclusion
*exclusion
)
1083 struct lttng_ht_iter iter
;
1084 struct lttng_ht_node_str
*node
;
1085 struct ust_app_event
*event
= NULL
;
1086 struct ust_app_ht_key key
;
1091 /* Setup key for event lookup. */
1093 key
.filter
= filter
;
1094 key
.loglevel
= loglevel
;
1095 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1096 key
.exclusion
= exclusion
;
1098 /* Lookup using the event name as hash and a custom match fct. */
1099 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1100 ht_match_ust_app_event
, &key
, &iter
.iter
);
1101 node
= lttng_ht_iter_get_node_str(&iter
);
1106 event
= caa_container_of(node
, struct ust_app_event
, node
);
1113 * Create the channel context on the tracer.
1115 * Called with UST app session lock held.
1118 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1119 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1123 health_code_update();
1125 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1126 ua_chan
->obj
, &ua_ctx
->obj
);
1128 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1129 ERR("UST app create channel context failed for app (pid: %d) "
1130 "with ret %d", app
->pid
, ret
);
1133 * This is normal behavior, an application can die during the
1134 * creation process. Don't report an error so the execution can
1135 * continue normally.
1138 DBG3("UST app disable event failed. Application is dead.");
1143 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1145 DBG2("UST app context handle %d created successfully for channel %s",
1146 ua_ctx
->handle
, ua_chan
->name
);
1149 health_code_update();
1154 * Set the filter on the tracer.
1157 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1158 struct ust_app
*app
)
1161 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1163 health_code_update();
1165 if (!ua_event
->filter
) {
1170 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1171 if (!ust_bytecode
) {
1172 ret
= -LTTNG_ERR_NOMEM
;
1175 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1178 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1179 ERR("UST app event %s filter failed for app (pid: %d) "
1180 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1183 * This is normal behavior, an application can die during the
1184 * creation process. Don't report an error so the execution can
1185 * continue normally.
1188 DBG3("UST app filter event failed. Application is dead.");
1193 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1196 health_code_update();
1202 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1203 struct lttng_event_exclusion
*exclusion
)
1205 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1206 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1207 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1209 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1210 if (!ust_exclusion
) {
1215 assert(sizeof(struct lttng_event_exclusion
) ==
1216 sizeof(struct lttng_ust_event_exclusion
));
1217 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1219 return ust_exclusion
;
1223 * Set event exclusions on the tracer.
1226 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1227 struct ust_app
*app
)
1230 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1232 health_code_update();
1234 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1239 ust_exclusion
= create_ust_exclusion_from_exclusion(
1240 ua_event
->exclusion
);
1241 if (!ust_exclusion
) {
1242 ret
= -LTTNG_ERR_NOMEM
;
1245 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1247 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1248 ERR("UST app event %s exclusions failed for app (pid: %d) "
1249 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1252 * This is normal behavior, an application can die during the
1253 * creation process. Don't report an error so the execution can
1254 * continue normally.
1257 DBG3("UST app event exclusion failed. Application is dead.");
1262 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1265 health_code_update();
1266 free(ust_exclusion
);
1271 * Disable the specified event on to UST tracer for the UST session.
1273 static int disable_ust_event(struct ust_app
*app
,
1274 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1278 health_code_update();
1280 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1282 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1283 ERR("UST app event %s disable failed for app (pid: %d) "
1284 "and session handle %d with ret %d",
1285 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1288 * This is normal behavior, an application can die during the
1289 * creation process. Don't report an error so the execution can
1290 * continue normally.
1293 DBG3("UST app disable event failed. Application is dead.");
1298 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1299 ua_event
->attr
.name
, app
->pid
);
1302 health_code_update();
1307 * Disable the specified channel on to UST tracer for the UST session.
1309 static int disable_ust_channel(struct ust_app
*app
,
1310 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1314 health_code_update();
1316 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1318 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1319 ERR("UST app channel %s disable failed for app (pid: %d) "
1320 "and session handle %d with ret %d",
1321 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1324 * This is normal behavior, an application can die during the
1325 * creation process. Don't report an error so the execution can
1326 * continue normally.
1329 DBG3("UST app disable channel failed. Application is dead.");
1334 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1335 ua_chan
->name
, app
->pid
);
1338 health_code_update();
1343 * Enable the specified channel on to UST tracer for the UST session.
1345 static int enable_ust_channel(struct ust_app
*app
,
1346 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1350 health_code_update();
1352 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1354 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1355 ERR("UST app channel %s enable failed for app (pid: %d) "
1356 "and session handle %d with ret %d",
1357 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1360 * This is normal behavior, an application can die during the
1361 * creation process. Don't report an error so the execution can
1362 * continue normally.
1365 DBG3("UST app enable channel failed. Application is dead.");
1370 ua_chan
->enabled
= 1;
1372 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1373 ua_chan
->name
, app
->pid
);
1376 health_code_update();
1381 * Enable the specified event on to UST tracer for the UST session.
1383 static int enable_ust_event(struct ust_app
*app
,
1384 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1388 health_code_update();
1390 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1392 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1393 ERR("UST app event %s enable failed for app (pid: %d) "
1394 "and session handle %d with ret %d",
1395 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1398 * This is normal behavior, an application can die during the
1399 * creation process. Don't report an error so the execution can
1400 * continue normally.
1403 DBG3("UST app enable event failed. Application is dead.");
1408 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1409 ua_event
->attr
.name
, app
->pid
);
1412 health_code_update();
1417 * Send channel and stream buffer to application.
1419 * Return 0 on success. On error, a negative value is returned.
1421 static int send_channel_pid_to_ust(struct ust_app
*app
,
1422 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1425 struct ust_app_stream
*stream
, *stmp
;
1431 health_code_update();
1433 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1436 /* Send channel to the application. */
1437 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1442 health_code_update();
1444 /* Send all streams to application. */
1445 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1446 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1450 /* We don't need the stream anymore once sent to the tracer. */
1451 cds_list_del(&stream
->list
);
1452 delete_ust_app_stream(-1, stream
);
1454 /* Flag the channel that it is sent to the application. */
1455 ua_chan
->is_sent
= 1;
1458 health_code_update();
1463 * Create the specified event onto the UST tracer for a UST session.
1465 * Should be called with session mutex held.
1468 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1469 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1473 health_code_update();
1475 /* Create UST event on tracer */
1476 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1479 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1480 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1481 ua_event
->attr
.name
, app
->pid
, ret
);
1484 * This is normal behavior, an application can die during the
1485 * creation process. Don't report an error so the execution can
1486 * continue normally.
1489 DBG3("UST app create event failed. Application is dead.");
1494 ua_event
->handle
= ua_event
->obj
->handle
;
1496 DBG2("UST app event %s created successfully for pid:%d",
1497 ua_event
->attr
.name
, app
->pid
);
1499 health_code_update();
1501 /* Set filter if one is present. */
1502 if (ua_event
->filter
) {
1503 ret
= set_ust_event_filter(ua_event
, app
);
1509 /* Set exclusions for the event */
1510 if (ua_event
->exclusion
) {
1511 ret
= set_ust_event_exclusion(ua_event
, app
);
1517 /* If event not enabled, disable it on the tracer */
1518 if (ua_event
->enabled
) {
1520 * We now need to explicitly enable the event, since it
1521 * is now disabled at creation.
1523 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1526 * If we hit an EPERM, something is wrong with our enable call. If
1527 * we get an EEXIST, there is a problem on the tracer side since we
1531 case -LTTNG_UST_ERR_PERM
:
1532 /* Code flow problem */
1534 case -LTTNG_UST_ERR_EXIST
:
1535 /* It's OK for our use case. */
1546 health_code_update();
1551 * Copy data between an UST app event and a LTT event.
1553 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1554 struct ltt_ust_event
*uevent
)
1556 size_t exclusion_alloc_size
;
1558 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1559 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1561 ua_event
->enabled
= uevent
->enabled
;
1563 /* Copy event attributes */
1564 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1566 /* Copy filter bytecode */
1567 if (uevent
->filter
) {
1568 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1569 /* Filter might be NULL here in case of ENONEM. */
1572 /* Copy exclusion data */
1573 if (uevent
->exclusion
) {
1574 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1575 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1576 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1577 if (ua_event
->exclusion
== NULL
) {
1580 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1581 exclusion_alloc_size
);
1587 * Copy data between an UST app channel and a LTT channel.
1589 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1590 struct ltt_ust_channel
*uchan
)
1592 struct lttng_ht_iter iter
;
1593 struct ltt_ust_event
*uevent
;
1594 struct ltt_ust_context
*uctx
;
1595 struct ust_app_event
*ua_event
;
1596 struct ust_app_ctx
*ua_ctx
;
1598 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1600 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1601 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1603 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1604 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1606 /* Copy event attributes since the layout is different. */
1607 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1608 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1609 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1610 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1611 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1612 ua_chan
->attr
.output
= uchan
->attr
.output
;
1614 * Note that the attribute channel type is not set since the channel on the
1615 * tracing registry side does not have this information.
1618 ua_chan
->enabled
= uchan
->enabled
;
1619 ua_chan
->tracing_channel_id
= uchan
->id
;
1621 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1622 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1623 if (ua_ctx
== NULL
) {
1626 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1627 (unsigned long) ua_ctx
->ctx
.ctx
);
1628 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1629 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1632 /* Copy all events from ltt ust channel to ust app channel */
1633 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1634 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1635 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1636 if (ua_event
== NULL
) {
1637 DBG2("UST event %s not found on shadow copy channel",
1639 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1640 if (ua_event
== NULL
) {
1643 shadow_copy_event(ua_event
, uevent
);
1644 add_unique_ust_app_event(ua_chan
, ua_event
);
1648 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1652 * Copy data between a UST app session and a regular LTT session.
1654 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1655 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1657 struct lttng_ht_node_str
*ua_chan_node
;
1658 struct lttng_ht_iter iter
;
1659 struct ltt_ust_channel
*uchan
;
1660 struct ust_app_channel
*ua_chan
;
1662 struct tm
*timeinfo
;
1665 char tmp_shm_path
[PATH_MAX
];
1667 /* Get date and time for unique app path */
1669 timeinfo
= localtime(&rawtime
);
1670 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1672 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1674 ua_sess
->tracing_id
= usess
->id
;
1675 ua_sess
->id
= get_next_session_id();
1676 ua_sess
->uid
= app
->uid
;
1677 ua_sess
->gid
= app
->gid
;
1678 ua_sess
->euid
= usess
->uid
;
1679 ua_sess
->egid
= usess
->gid
;
1680 ua_sess
->buffer_type
= usess
->buffer_type
;
1681 ua_sess
->bits_per_long
= app
->bits_per_long
;
1682 /* There is only one consumer object per session possible. */
1683 ua_sess
->consumer
= usess
->consumer
;
1684 ua_sess
->output_traces
= usess
->output_traces
;
1685 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1686 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1687 &usess
->metadata_attr
);
1689 switch (ua_sess
->buffer_type
) {
1690 case LTTNG_BUFFER_PER_PID
:
1691 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1692 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1695 case LTTNG_BUFFER_PER_UID
:
1696 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1697 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1704 PERROR("asprintf UST shadow copy session");
1709 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1710 sizeof(ua_sess
->root_shm_path
));
1711 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1712 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1713 sizeof(ua_sess
->shm_path
));
1714 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1715 if (ua_sess
->shm_path
[0]) {
1716 switch (ua_sess
->buffer_type
) {
1717 case LTTNG_BUFFER_PER_PID
:
1718 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1719 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1720 app
->name
, app
->pid
, datetime
);
1722 case LTTNG_BUFFER_PER_UID
:
1723 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1724 DEFAULT_UST_TRACE_UID_PATH
,
1725 app
->uid
, app
->bits_per_long
);
1732 PERROR("sprintf UST shadow copy session");
1736 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1737 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1738 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1741 /* Iterate over all channels in global domain. */
1742 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1744 struct lttng_ht_iter uiter
;
1746 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1747 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1748 if (ua_chan_node
!= NULL
) {
1749 /* Session exist. Contiuing. */
1753 DBG2("Channel %s not found on shadow session copy, creating it",
1755 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1756 if (ua_chan
== NULL
) {
1757 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1760 shadow_copy_channel(ua_chan
, uchan
);
1762 * The concept of metadata channel does not exist on the tracing
1763 * registry side of the session daemon so this can only be a per CPU
1764 * channel and not metadata.
1766 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1768 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1776 * Lookup sesison wrapper.
1779 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1780 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1782 /* Get right UST app session from app */
1783 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1787 * Return ust app session from the app session hashtable using the UST session
1790 static struct ust_app_session
*lookup_session_by_app(
1791 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1793 struct lttng_ht_iter iter
;
1794 struct lttng_ht_node_u64
*node
;
1796 __lookup_session_by_app(usess
, app
, &iter
);
1797 node
= lttng_ht_iter_get_node_u64(&iter
);
1802 return caa_container_of(node
, struct ust_app_session
, node
);
1809 * Setup buffer registry per PID for the given session and application. If none
1810 * is found, a new one is created, added to the global registry and
1811 * initialized. If regp is valid, it's set with the newly created object.
1813 * Return 0 on success or else a negative value.
1815 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1816 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1819 struct buffer_reg_pid
*reg_pid
;
1826 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1829 * This is the create channel path meaning that if there is NO
1830 * registry available, we have to create one for this session.
1832 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
1833 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1841 /* Initialize registry. */
1842 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1843 app
->bits_per_long
, app
->uint8_t_alignment
,
1844 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1845 app
->uint64_t_alignment
, app
->long_alignment
,
1846 app
->byte_order
, app
->version
.major
,
1847 app
->version
.minor
, reg_pid
->root_shm_path
,
1849 ua_sess
->euid
, ua_sess
->egid
);
1852 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1853 * destroy the buffer registry, because it is always expected
1854 * that if the buffer registry can be found, its ust registry is
1857 buffer_reg_pid_destroy(reg_pid
);
1861 buffer_reg_pid_add(reg_pid
);
1863 DBG3("UST app buffer registry per PID created successfully");
1875 * Setup buffer registry per UID for the given session and application. If none
1876 * is found, a new one is created, added to the global registry and
1877 * initialized. If regp is valid, it's set with the newly created object.
1879 * Return 0 on success or else a negative value.
1881 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
1882 struct ust_app_session
*ua_sess
,
1883 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
1886 struct buffer_reg_uid
*reg_uid
;
1893 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
1896 * This is the create channel path meaning that if there is NO
1897 * registry available, we have to create one for this session.
1899 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
1900 LTTNG_DOMAIN_UST
, ®_uid
,
1901 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1909 /* Initialize registry. */
1910 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
1911 app
->bits_per_long
, app
->uint8_t_alignment
,
1912 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1913 app
->uint64_t_alignment
, app
->long_alignment
,
1914 app
->byte_order
, app
->version
.major
,
1915 app
->version
.minor
, reg_uid
->root_shm_path
,
1916 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
1919 * reg_uid->registry->reg.ust is NULL upon error, so we need to
1920 * destroy the buffer registry, because it is always expected
1921 * that if the buffer registry can be found, its ust registry is
1924 buffer_reg_uid_destroy(reg_uid
, NULL
);
1927 /* Add node to teardown list of the session. */
1928 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
1930 buffer_reg_uid_add(reg_uid
);
1932 DBG3("UST app buffer registry per UID created successfully");
1943 * Create a session on the tracer side for the given app.
1945 * On success, ua_sess_ptr is populated with the session pointer or else left
1946 * untouched. If the session was created, is_created is set to 1. On error,
1947 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1950 * Returns 0 on success or else a negative code which is either -ENOMEM or
1951 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1953 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1954 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1957 int ret
, created
= 0;
1958 struct ust_app_session
*ua_sess
;
1962 assert(ua_sess_ptr
);
1964 health_code_update();
1966 ua_sess
= lookup_session_by_app(usess
, app
);
1967 if (ua_sess
== NULL
) {
1968 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
1969 app
->pid
, usess
->id
);
1970 ua_sess
= alloc_ust_app_session(app
);
1971 if (ua_sess
== NULL
) {
1972 /* Only malloc can failed so something is really wrong */
1976 shadow_copy_session(ua_sess
, usess
, app
);
1980 switch (usess
->buffer_type
) {
1981 case LTTNG_BUFFER_PER_PID
:
1982 /* Init local registry. */
1983 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
1985 delete_ust_app_session(-1, ua_sess
, app
);
1989 case LTTNG_BUFFER_PER_UID
:
1990 /* Look for a global registry. If none exists, create one. */
1991 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
1993 delete_ust_app_session(-1, ua_sess
, app
);
2003 health_code_update();
2005 if (ua_sess
->handle
== -1) {
2006 ret
= ustctl_create_session(app
->sock
);
2008 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2009 ERR("Creating session for app pid %d with ret %d",
2012 DBG("UST app creating session failed. Application is dead");
2014 * This is normal behavior, an application can die during the
2015 * creation process. Don't report an error so the execution can
2016 * continue normally. This will get flagged ENOTCONN and the
2017 * caller will handle it.
2021 delete_ust_app_session(-1, ua_sess
, app
);
2022 if (ret
!= -ENOMEM
) {
2024 * Tracer is probably gone or got an internal error so let's
2025 * behave like it will soon unregister or not usable.
2032 ua_sess
->handle
= ret
;
2034 /* Add ust app session to app's HT */
2035 lttng_ht_node_init_u64(&ua_sess
->node
,
2036 ua_sess
->tracing_id
);
2037 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2039 DBG2("UST app session created successfully with handle %d", ret
);
2042 *ua_sess_ptr
= ua_sess
;
2044 *is_created
= created
;
2047 /* Everything went well. */
2051 health_code_update();
2056 * Match function for a hash table lookup of ust_app_ctx.
2058 * It matches an ust app context based on the context type and, in the case
2059 * of perf counters, their name.
2061 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2063 struct ust_app_ctx
*ctx
;
2064 const struct lttng_ust_context
*key
;
2069 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2073 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2077 /* Check the name in the case of perf thread counters. */
2078 if (key
->ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
2079 if (strncmp(key
->u
.perf_counter
.name
,
2080 ctx
->ctx
.u
.perf_counter
.name
,
2081 sizeof(key
->u
.perf_counter
.name
))) {
2094 * Lookup for an ust app context from an lttng_ust_context.
2096 * Must be called while holding RCU read side lock.
2097 * Return an ust_app_ctx object or NULL on error.
2100 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2101 struct lttng_ust_context
*uctx
)
2103 struct lttng_ht_iter iter
;
2104 struct lttng_ht_node_ulong
*node
;
2105 struct ust_app_ctx
*app_ctx
= NULL
;
2110 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2111 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2112 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2113 node
= lttng_ht_iter_get_node_ulong(&iter
);
2118 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2125 * Create a context for the channel on the tracer.
2127 * Called with UST app session lock held and a RCU read side lock.
2130 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2131 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
2132 struct ust_app
*app
)
2135 struct ust_app_ctx
*ua_ctx
;
2137 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2139 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2145 ua_ctx
= alloc_ust_app_ctx(uctx
);
2146 if (ua_ctx
== NULL
) {
2152 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2153 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2154 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2156 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2166 * Enable on the tracer side a ust app event for the session and channel.
2168 * Called with UST app session lock held.
2171 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2172 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2176 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2181 ua_event
->enabled
= 1;
2188 * Disable on the tracer side a ust app event for the session and channel.
2190 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2191 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2195 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2200 ua_event
->enabled
= 0;
2207 * Lookup ust app channel for session and disable it on the tracer side.
2210 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2211 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2215 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2220 ua_chan
->enabled
= 0;
2227 * Lookup ust app channel for session and enable it on the tracer side. This
2228 * MUST be called with a RCU read side lock acquired.
2230 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2231 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2234 struct lttng_ht_iter iter
;
2235 struct lttng_ht_node_str
*ua_chan_node
;
2236 struct ust_app_channel
*ua_chan
;
2238 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2239 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2240 if (ua_chan_node
== NULL
) {
2241 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2242 uchan
->name
, ua_sess
->tracing_id
);
2246 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2248 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2258 * Ask the consumer to create a channel and get it if successful.
2260 * Return 0 on success or else a negative value.
2262 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2263 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2264 int bitness
, struct ust_registry_session
*registry
)
2267 unsigned int nb_fd
= 0;
2268 struct consumer_socket
*socket
;
2276 health_code_update();
2278 /* Get the right consumer socket for the application. */
2279 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2285 health_code_update();
2287 /* Need one fd for the channel. */
2288 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2290 ERR("Exhausted number of available FD upon create channel");
2295 * Ask consumer to create channel. The consumer will return the number of
2296 * stream we have to expect.
2298 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2305 * Compute the number of fd needed before receiving them. It must be 2 per
2306 * stream (2 being the default value here).
2308 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2310 /* Reserve the amount of file descriptor we need. */
2311 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2313 ERR("Exhausted number of available FD upon create channel");
2314 goto error_fd_get_stream
;
2317 health_code_update();
2320 * Now get the channel from the consumer. This call wil populate the stream
2321 * list of that channel and set the ust objects.
2323 if (usess
->consumer
->enabled
) {
2324 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2334 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2335 error_fd_get_stream
:
2337 * Initiate a destroy channel on the consumer since we had an error
2338 * handling it on our side. The return value is of no importance since we
2339 * already have a ret value set by the previous error that we need to
2342 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2344 lttng_fd_put(LTTNG_FD_APPS
, 1);
2346 health_code_update();
2352 * Duplicate the ust data object of the ust app stream and save it in the
2353 * buffer registry stream.
2355 * Return 0 on success or else a negative value.
2357 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2358 struct ust_app_stream
*stream
)
2365 /* Reserve the amount of file descriptor we need. */
2366 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2368 ERR("Exhausted number of available FD upon duplicate stream");
2372 /* Duplicate object for stream once the original is in the registry. */
2373 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2374 reg_stream
->obj
.ust
);
2376 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2377 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2378 lttng_fd_put(LTTNG_FD_APPS
, 2);
2381 stream
->handle
= stream
->obj
->handle
;
2388 * Duplicate the ust data object of the ust app. channel and save it in the
2389 * buffer registry channel.
2391 * Return 0 on success or else a negative value.
2393 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2394 struct ust_app_channel
*ua_chan
)
2401 /* Need two fds for the channel. */
2402 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2404 ERR("Exhausted number of available FD upon duplicate channel");
2408 /* Duplicate object for stream once the original is in the registry. */
2409 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2411 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2412 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2415 ua_chan
->handle
= ua_chan
->obj
->handle
;
2420 lttng_fd_put(LTTNG_FD_APPS
, 1);
2426 * For a given channel buffer registry, setup all streams of the given ust
2427 * application channel.
2429 * Return 0 on success or else a negative value.
2431 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2432 struct ust_app_channel
*ua_chan
)
2435 struct ust_app_stream
*stream
, *stmp
;
2440 DBG2("UST app setup buffer registry stream");
2442 /* Send all streams to application. */
2443 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2444 struct buffer_reg_stream
*reg_stream
;
2446 ret
= buffer_reg_stream_create(®_stream
);
2452 * Keep original pointer and nullify it in the stream so the delete
2453 * stream call does not release the object.
2455 reg_stream
->obj
.ust
= stream
->obj
;
2457 buffer_reg_stream_add(reg_stream
, reg_chan
);
2459 /* We don't need the streams anymore. */
2460 cds_list_del(&stream
->list
);
2461 delete_ust_app_stream(-1, stream
);
2469 * Create a buffer registry channel for the given session registry and
2470 * application channel object. If regp pointer is valid, it's set with the
2471 * created object. Important, the created object is NOT added to the session
2472 * registry hash table.
2474 * Return 0 on success else a negative value.
2476 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2477 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2480 struct buffer_reg_channel
*reg_chan
= NULL
;
2485 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2487 /* Create buffer registry channel. */
2488 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2493 reg_chan
->consumer_key
= ua_chan
->key
;
2494 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2495 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2497 /* Create and add a channel registry to session. */
2498 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2499 ua_chan
->tracing_channel_id
);
2503 buffer_reg_channel_add(reg_sess
, reg_chan
);
2512 /* Safe because the registry channel object was not added to any HT. */
2513 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2519 * Setup buffer registry channel for the given session registry and application
2520 * channel object. If regp pointer is valid, it's set with the created object.
2522 * Return 0 on success else a negative value.
2524 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2525 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
)
2532 assert(ua_chan
->obj
);
2534 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2536 /* Setup all streams for the registry. */
2537 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
);
2542 reg_chan
->obj
.ust
= ua_chan
->obj
;
2543 ua_chan
->obj
= NULL
;
2548 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2549 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2554 * Send buffer registry channel to the application.
2556 * Return 0 on success else a negative value.
2558 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2559 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2560 struct ust_app_channel
*ua_chan
)
2563 struct buffer_reg_stream
*reg_stream
;
2570 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2572 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2577 /* Send channel to the application. */
2578 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2583 health_code_update();
2585 /* Send all streams to application. */
2586 pthread_mutex_lock(®_chan
->stream_list_lock
);
2587 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2588 struct ust_app_stream stream
;
2590 ret
= duplicate_stream_object(reg_stream
, &stream
);
2592 goto error_stream_unlock
;
2595 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2597 (void) release_ust_app_stream(-1, &stream
);
2598 goto error_stream_unlock
;
2602 * The return value is not important here. This function will output an
2605 (void) release_ust_app_stream(-1, &stream
);
2607 ua_chan
->is_sent
= 1;
2609 error_stream_unlock
:
2610 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2616 * Create and send to the application the created buffers with per UID buffers.
2618 * Return 0 on success else a negative value.
2620 static int create_channel_per_uid(struct ust_app
*app
,
2621 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2622 struct ust_app_channel
*ua_chan
)
2625 struct buffer_reg_uid
*reg_uid
;
2626 struct buffer_reg_channel
*reg_chan
;
2633 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2635 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2637 * The session creation handles the creation of this global registry
2638 * object. If none can be find, there is a code flow problem or a
2643 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2646 /* Create the buffer registry channel object. */
2647 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2649 ERR("Error creating the UST channel \"%s\" registry instance",
2656 * Create the buffers on the consumer side. This call populates the
2657 * ust app channel object with all streams and data object.
2659 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2660 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2662 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2666 * Let's remove the previously created buffer registry channel so
2667 * it's not visible anymore in the session registry.
2669 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2670 ua_chan
->tracing_channel_id
);
2671 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2672 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2677 * Setup the streams and add it to the session registry.
2679 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, reg_chan
);
2681 ERR("Error setting up UST channel \"%s\"",
2688 /* Send buffers to the application. */
2689 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2692 * Don't report error to the console, since it may be
2693 * caused by application concurrently exiting.
2703 * Create and send to the application the created buffers with per PID buffers.
2705 * Return 0 on success else a negative value.
2707 static int create_channel_per_pid(struct ust_app
*app
,
2708 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2709 struct ust_app_channel
*ua_chan
)
2712 struct ust_registry_session
*registry
;
2719 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2723 registry
= get_session_registry(ua_sess
);
2726 /* Create and add a new channel registry to session. */
2727 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2729 ERR("Error creating the UST channel \"%s\" registry instance",
2734 /* Create and get channel on the consumer side. */
2735 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2736 app
->bits_per_long
, registry
);
2738 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2743 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2746 * Don't report error to the console, since it may be
2747 * caused by application concurrently exiting.
2758 * From an already allocated ust app channel, create the channel buffers if
2759 * need and send it to the application. This MUST be called with a RCU read
2760 * side lock acquired.
2762 * Return 0 on success or else a negative value.
2764 static int do_create_channel(struct ust_app
*app
,
2765 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2766 struct ust_app_channel
*ua_chan
)
2775 /* Handle buffer type before sending the channel to the application. */
2776 switch (usess
->buffer_type
) {
2777 case LTTNG_BUFFER_PER_UID
:
2779 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
2785 case LTTNG_BUFFER_PER_PID
:
2787 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
2799 /* Initialize ust objd object using the received handle and add it. */
2800 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
2801 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
2803 /* If channel is not enabled, disable it on the tracer */
2804 if (!ua_chan
->enabled
) {
2805 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2816 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2817 * newly created channel if not NULL.
2819 * Called with UST app session lock and RCU read-side lock held.
2821 * Return 0 on success or else a negative value.
2823 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
2824 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
2825 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
2826 struct ust_app_channel
**ua_chanp
)
2829 struct lttng_ht_iter iter
;
2830 struct lttng_ht_node_str
*ua_chan_node
;
2831 struct ust_app_channel
*ua_chan
;
2833 /* Lookup channel in the ust app session */
2834 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2835 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2836 if (ua_chan_node
!= NULL
) {
2837 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2841 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
2842 if (ua_chan
== NULL
) {
2843 /* Only malloc can fail here */
2847 shadow_copy_channel(ua_chan
, uchan
);
2849 /* Set channel type. */
2850 ua_chan
->attr
.type
= type
;
2852 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
2857 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
2860 /* Only add the channel if successful on the tracer side. */
2861 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
2865 *ua_chanp
= ua_chan
;
2868 /* Everything went well. */
2872 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
2878 * Create UST app event and create it on the tracer side.
2880 * Called with ust app session mutex held.
2883 int create_ust_app_event(struct ust_app_session
*ua_sess
,
2884 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
2885 struct ust_app
*app
)
2888 struct ust_app_event
*ua_event
;
2890 /* Get event node */
2891 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2892 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
2893 if (ua_event
!= NULL
) {
2898 /* Does not exist so create one */
2899 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
2900 if (ua_event
== NULL
) {
2901 /* Only malloc can failed so something is really wrong */
2905 shadow_copy_event(ua_event
, uevent
);
2907 /* Create it on the tracer side */
2908 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2910 /* Not found previously means that it does not exist on the tracer */
2911 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
2915 add_unique_ust_app_event(ua_chan
, ua_event
);
2917 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
2924 /* Valid. Calling here is already in a read side lock */
2925 delete_ust_app_event(-1, ua_event
);
2930 * Create UST metadata and open it on the tracer side.
2932 * Called with UST app session lock held and RCU read side lock.
2934 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
2935 struct ust_app
*app
, struct consumer_output
*consumer
)
2938 struct ust_app_channel
*metadata
;
2939 struct consumer_socket
*socket
;
2940 struct ust_registry_session
*registry
;
2946 registry
= get_session_registry(ua_sess
);
2949 pthread_mutex_lock(®istry
->lock
);
2951 /* Metadata already exists for this registry or it was closed previously */
2952 if (registry
->metadata_key
|| registry
->metadata_closed
) {
2957 /* Allocate UST metadata */
2958 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
2960 /* malloc() failed */
2965 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
2967 /* Need one fd for the channel. */
2968 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2970 ERR("Exhausted number of available FD upon create metadata");
2974 /* Get the right consumer socket for the application. */
2975 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
2978 goto error_consumer
;
2982 * Keep metadata key so we can identify it on the consumer side. Assign it
2983 * to the registry *before* we ask the consumer so we avoid the race of the
2984 * consumer requesting the metadata and the ask_channel call on our side
2985 * did not returned yet.
2987 registry
->metadata_key
= metadata
->key
;
2990 * Ask the metadata channel creation to the consumer. The metadata object
2991 * will be created by the consumer and kept their. However, the stream is
2992 * never added or monitored until we do a first push metadata to the
2995 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
2998 /* Nullify the metadata key so we don't try to close it later on. */
2999 registry
->metadata_key
= 0;
3000 goto error_consumer
;
3004 * The setup command will make the metadata stream be sent to the relayd,
3005 * if applicable, and the thread managing the metadatas. This is important
3006 * because after this point, if an error occurs, the only way the stream
3007 * can be deleted is to be monitored in the consumer.
3009 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3011 /* Nullify the metadata key so we don't try to close it later on. */
3012 registry
->metadata_key
= 0;
3013 goto error_consumer
;
3016 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3017 metadata
->key
, app
->pid
);
3020 lttng_fd_put(LTTNG_FD_APPS
, 1);
3021 delete_ust_app_channel(-1, metadata
, app
);
3023 pthread_mutex_unlock(®istry
->lock
);
3028 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3029 * acquired before calling this function.
3031 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3033 struct ust_app
*app
= NULL
;
3034 struct lttng_ht_node_ulong
*node
;
3035 struct lttng_ht_iter iter
;
3037 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3038 node
= lttng_ht_iter_get_node_ulong(&iter
);
3040 DBG2("UST app no found with pid %d", pid
);
3044 DBG2("Found UST app by pid %d", pid
);
3046 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3053 * Allocate and init an UST app object using the registration information and
3054 * the command socket. This is called when the command socket connects to the
3057 * The object is returned on success or else NULL.
3059 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3061 struct ust_app
*lta
= NULL
;
3066 DBG3("UST app creating application for socket %d", sock
);
3068 if ((msg
->bits_per_long
== 64 &&
3069 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3070 || (msg
->bits_per_long
== 32 &&
3071 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3072 ERR("Registration failed: application \"%s\" (pid: %d) has "
3073 "%d-bit long, but no consumerd for this size is available.\n",
3074 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3078 lta
= zmalloc(sizeof(struct ust_app
));
3084 lta
->ppid
= msg
->ppid
;
3085 lta
->uid
= msg
->uid
;
3086 lta
->gid
= msg
->gid
;
3088 lta
->bits_per_long
= msg
->bits_per_long
;
3089 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3090 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3091 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3092 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3093 lta
->long_alignment
= msg
->long_alignment
;
3094 lta
->byte_order
= msg
->byte_order
;
3096 lta
->v_major
= msg
->major
;
3097 lta
->v_minor
= msg
->minor
;
3098 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3099 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3100 lta
->notify_sock
= -1;
3102 /* Copy name and make sure it's NULL terminated. */
3103 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3104 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3107 * Before this can be called, when receiving the registration information,
3108 * the application compatibility is checked. So, at this point, the
3109 * application can work with this session daemon.
3111 lta
->compatible
= 1;
3113 lta
->pid
= msg
->pid
;
3114 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3116 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3118 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3125 * For a given application object, add it to every hash table.
3127 void ust_app_add(struct ust_app
*app
)
3130 assert(app
->notify_sock
>= 0);
3135 * On a re-registration, we want to kick out the previous registration of
3138 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3141 * The socket _should_ be unique until _we_ call close. So, a add_unique
3142 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3143 * already in the table.
3145 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3147 /* Add application to the notify socket hash table. */
3148 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3149 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3151 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3152 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3153 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3160 * Set the application version into the object.
3162 * Return 0 on success else a negative value either an errno code or a
3163 * LTTng-UST error code.
3165 int ust_app_version(struct ust_app
*app
)
3171 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3173 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3174 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3176 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3184 * Unregister app by removing it from the global traceable app list and freeing
3187 * The socket is already closed at this point so no close to sock.
3189 void ust_app_unregister(int sock
)
3191 struct ust_app
*lta
;
3192 struct lttng_ht_node_ulong
*node
;
3193 struct lttng_ht_iter ust_app_sock_iter
;
3194 struct lttng_ht_iter iter
;
3195 struct ust_app_session
*ua_sess
;
3200 /* Get the node reference for a call_rcu */
3201 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3202 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3205 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3206 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3209 * For per-PID buffers, perform "push metadata" and flush all
3210 * application streams before removing app from hash tables,
3211 * ensuring proper behavior of data_pending check.
3212 * Remove sessions so they are not visible during deletion.
3214 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3216 struct ust_registry_session
*registry
;
3218 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3220 /* The session was already removed so scheduled for teardown. */
3224 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3225 (void) ust_app_flush_app_session(lta
, ua_sess
);
3229 * Add session to list for teardown. This is safe since at this point we
3230 * are the only one using this list.
3232 pthread_mutex_lock(&ua_sess
->lock
);
3234 if (ua_sess
->deleted
) {
3235 pthread_mutex_unlock(&ua_sess
->lock
);
3240 * Normally, this is done in the delete session process which is
3241 * executed in the call rcu below. However, upon registration we can't
3242 * afford to wait for the grace period before pushing data or else the
3243 * data pending feature can race between the unregistration and stop
3244 * command where the data pending command is sent *before* the grace
3247 * The close metadata below nullifies the metadata pointer in the
3248 * session so the delete session will NOT push/close a second time.
3250 registry
= get_session_registry(ua_sess
);
3252 /* Push metadata for application before freeing the application. */
3253 (void) push_metadata(registry
, ua_sess
->consumer
);
3256 * Don't ask to close metadata for global per UID buffers. Close
3257 * metadata only on destroy trace session in this case. Also, the
3258 * previous push metadata could have flag the metadata registry to
3259 * close so don't send a close command if closed.
3261 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3262 /* And ask to close it for this session registry. */
3263 (void) close_metadata(registry
, ua_sess
->consumer
);
3266 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3268 pthread_mutex_unlock(&ua_sess
->lock
);
3271 /* Remove application from PID hash table */
3272 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3276 * Remove application from notify hash table. The thread handling the
3277 * notify socket could have deleted the node so ignore on error because
3278 * either way it's valid. The close of that socket is handled by the other
3281 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3282 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3285 * Ignore return value since the node might have been removed before by an
3286 * add replace during app registration because the PID can be reassigned by
3289 iter
.iter
.node
= <a
->pid_n
.node
;
3290 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3292 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3297 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3304 * Fill events array with all events name of all registered apps.
3306 int ust_app_list_events(struct lttng_event
**events
)
3309 size_t nbmem
, count
= 0;
3310 struct lttng_ht_iter iter
;
3311 struct ust_app
*app
;
3312 struct lttng_event
*tmp_event
;
3314 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3315 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3316 if (tmp_event
== NULL
) {
3317 PERROR("zmalloc ust app events");
3324 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3325 struct lttng_ust_tracepoint_iter uiter
;
3327 health_code_update();
3329 if (!app
->compatible
) {
3331 * TODO: In time, we should notice the caller of this error by
3332 * telling him that this is a version error.
3336 handle
= ustctl_tracepoint_list(app
->sock
);
3338 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3339 ERR("UST app list events getting handle failed for app pid %d",
3345 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3346 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3347 /* Handle ustctl error. */
3349 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3350 ERR("UST app tp list get failed for app %d with ret %d",
3353 DBG3("UST app tp list get failed. Application is dead");
3355 * This is normal behavior, an application can die during the
3356 * creation process. Don't report an error so the execution can
3357 * continue normally. Continue normal execution.
3365 health_code_update();
3366 if (count
>= nbmem
) {
3367 /* In case the realloc fails, we free the memory */
3368 struct lttng_event
*new_tmp_event
;
3371 new_nbmem
= nbmem
<< 1;
3372 DBG2("Reallocating event list from %zu to %zu entries",
3374 new_tmp_event
= realloc(tmp_event
,
3375 new_nbmem
* sizeof(struct lttng_event
));
3376 if (new_tmp_event
== NULL
) {
3377 PERROR("realloc ust app events");
3382 /* Zero the new memory */
3383 memset(new_tmp_event
+ nbmem
, 0,
3384 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3386 tmp_event
= new_tmp_event
;
3388 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3389 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3390 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3391 tmp_event
[count
].pid
= app
->pid
;
3392 tmp_event
[count
].enabled
= -1;
3398 *events
= tmp_event
;
3400 DBG2("UST app list events done (%zu events)", count
);
3405 health_code_update();
3410 * Fill events array with all events name of all registered apps.
3412 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3415 size_t nbmem
, count
= 0;
3416 struct lttng_ht_iter iter
;
3417 struct ust_app
*app
;
3418 struct lttng_event_field
*tmp_event
;
3420 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3421 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3422 if (tmp_event
== NULL
) {
3423 PERROR("zmalloc ust app event fields");
3430 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3431 struct lttng_ust_field_iter uiter
;
3433 health_code_update();
3435 if (!app
->compatible
) {
3437 * TODO: In time, we should notice the caller of this error by
3438 * telling him that this is a version error.
3442 handle
= ustctl_tracepoint_field_list(app
->sock
);
3444 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3445 ERR("UST app list field getting handle failed for app pid %d",
3451 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3452 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3453 /* Handle ustctl error. */
3455 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3456 ERR("UST app tp list field failed for app %d with ret %d",
3459 DBG3("UST app tp list field failed. Application is dead");
3461 * This is normal behavior, an application can die during the
3462 * creation process. Don't report an error so the execution can
3463 * continue normally. Reset list and count for next app.
3471 health_code_update();
3472 if (count
>= nbmem
) {
3473 /* In case the realloc fails, we free the memory */
3474 struct lttng_event_field
*new_tmp_event
;
3477 new_nbmem
= nbmem
<< 1;
3478 DBG2("Reallocating event field list from %zu to %zu entries",
3480 new_tmp_event
= realloc(tmp_event
,
3481 new_nbmem
* sizeof(struct lttng_event_field
));
3482 if (new_tmp_event
== NULL
) {
3483 PERROR("realloc ust app event fields");
3488 /* Zero the new memory */
3489 memset(new_tmp_event
+ nbmem
, 0,
3490 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3492 tmp_event
= new_tmp_event
;
3495 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3496 /* Mapping between these enums matches 1 to 1. */
3497 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3498 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3500 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3501 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3502 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3503 tmp_event
[count
].event
.pid
= app
->pid
;
3504 tmp_event
[count
].event
.enabled
= -1;
3510 *fields
= tmp_event
;
3512 DBG2("UST app list event fields done (%zu events)", count
);
3517 health_code_update();
3522 * Free and clean all traceable apps of the global list.
3524 * Should _NOT_ be called with RCU read-side lock held.
3526 void ust_app_clean_list(void)
3529 struct ust_app
*app
;
3530 struct lttng_ht_iter iter
;
3532 DBG2("UST app cleaning registered apps hash table");
3537 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3538 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3540 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3544 /* Cleanup socket hash table */
3545 if (ust_app_ht_by_sock
) {
3546 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3548 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3553 /* Cleanup notify socket hash table */
3554 if (ust_app_ht_by_notify_sock
) {
3555 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3556 notify_sock_n
.node
) {
3557 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3563 /* Destroy is done only when the ht is empty */
3565 ht_cleanup_push(ust_app_ht
);
3567 if (ust_app_ht_by_sock
) {
3568 ht_cleanup_push(ust_app_ht_by_sock
);
3570 if (ust_app_ht_by_notify_sock
) {
3571 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3576 * Init UST app hash table.
3578 int ust_app_ht_alloc(void)
3580 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3584 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3585 if (!ust_app_ht_by_sock
) {
3588 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3589 if (!ust_app_ht_by_notify_sock
) {
3596 * For a specific UST session, disable the channel for all registered apps.
3598 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3599 struct ltt_ust_channel
*uchan
)
3602 struct lttng_ht_iter iter
;
3603 struct lttng_ht_node_str
*ua_chan_node
;
3604 struct ust_app
*app
;
3605 struct ust_app_session
*ua_sess
;
3606 struct ust_app_channel
*ua_chan
;
3608 if (usess
== NULL
|| uchan
== NULL
) {
3609 ERR("Disabling UST global channel with NULL values");
3614 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3615 uchan
->name
, usess
->id
);
3619 /* For every registered applications */
3620 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3621 struct lttng_ht_iter uiter
;
3622 if (!app
->compatible
) {
3624 * TODO: In time, we should notice the caller of this error by
3625 * telling him that this is a version error.
3629 ua_sess
= lookup_session_by_app(usess
, app
);
3630 if (ua_sess
== NULL
) {
3635 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3636 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3637 /* If the session if found for the app, the channel must be there */
3638 assert(ua_chan_node
);
3640 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3641 /* The channel must not be already disabled */
3642 assert(ua_chan
->enabled
== 1);
3644 /* Disable channel onto application */
3645 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3647 /* XXX: We might want to report this error at some point... */
3659 * For a specific UST session, enable the channel for all registered apps.
3661 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3662 struct ltt_ust_channel
*uchan
)
3665 struct lttng_ht_iter iter
;
3666 struct ust_app
*app
;
3667 struct ust_app_session
*ua_sess
;
3669 if (usess
== NULL
|| uchan
== NULL
) {
3670 ERR("Adding UST global channel to NULL values");
3675 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3676 uchan
->name
, usess
->id
);
3680 /* For every registered applications */
3681 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3682 if (!app
->compatible
) {
3684 * TODO: In time, we should notice the caller of this error by
3685 * telling him that this is a version error.
3689 ua_sess
= lookup_session_by_app(usess
, app
);
3690 if (ua_sess
== NULL
) {
3694 /* Enable channel onto application */
3695 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3697 /* XXX: We might want to report this error at some point... */
3709 * Disable an event in a channel and for a specific session.
3711 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3712 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3715 struct lttng_ht_iter iter
, uiter
;
3716 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3717 struct ust_app
*app
;
3718 struct ust_app_session
*ua_sess
;
3719 struct ust_app_channel
*ua_chan
;
3720 struct ust_app_event
*ua_event
;
3722 DBG("UST app disabling event %s for all apps in channel "
3723 "%s for session id %" PRIu64
,
3724 uevent
->attr
.name
, uchan
->name
, usess
->id
);
3728 /* For all registered applications */
3729 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3730 if (!app
->compatible
) {
3732 * TODO: In time, we should notice the caller of this error by
3733 * telling him that this is a version error.
3737 ua_sess
= lookup_session_by_app(usess
, app
);
3738 if (ua_sess
== NULL
) {
3743 /* Lookup channel in the ust app session */
3744 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3745 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3746 if (ua_chan_node
== NULL
) {
3747 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
3748 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
3751 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3753 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
3754 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
3755 if (ua_event_node
== NULL
) {
3756 DBG2("Event %s not found in channel %s for app pid %d."
3757 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
3760 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3762 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3764 /* XXX: Report error someday... */
3775 * For a specific UST session, create the channel for all registered apps.
3777 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
3778 struct ltt_ust_channel
*uchan
)
3780 int ret
= 0, created
;
3781 struct lttng_ht_iter iter
;
3782 struct ust_app
*app
;
3783 struct ust_app_session
*ua_sess
= NULL
;
3785 /* Very wrong code flow */
3789 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
3790 uchan
->name
, usess
->id
);
3794 /* For every registered applications */
3795 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3796 if (!app
->compatible
) {
3798 * TODO: In time, we should notice the caller of this error by
3799 * telling him that this is a version error.
3803 if (!trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
3809 * Create session on the tracer side and add it to app session HT. Note
3810 * that if session exist, it will simply return a pointer to the ust
3813 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
3818 * The application's socket is not valid. Either a bad socket
3819 * or a timeout on it. We can't inform the caller that for a
3820 * specific app, the session failed so lets continue here.
3825 goto error_rcu_unlock
;
3830 pthread_mutex_lock(&ua_sess
->lock
);
3832 if (ua_sess
->deleted
) {
3833 pthread_mutex_unlock(&ua_sess
->lock
);
3837 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
3838 sizeof(uchan
->name
))) {
3839 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
3842 /* Create channel onto application. We don't need the chan ref. */
3843 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
3844 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
3846 pthread_mutex_unlock(&ua_sess
->lock
);
3848 if (ret
== -ENOMEM
) {
3849 /* No more memory is a fatal error. Stop right now. */
3850 goto error_rcu_unlock
;
3852 /* Cleanup the created session if it's the case. */
3854 destroy_app_session(app
, ua_sess
);
3865 * Enable event for a specific session and channel on the tracer.
3867 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
3868 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3871 struct lttng_ht_iter iter
, uiter
;
3872 struct lttng_ht_node_str
*ua_chan_node
;
3873 struct ust_app
*app
;
3874 struct ust_app_session
*ua_sess
;
3875 struct ust_app_channel
*ua_chan
;
3876 struct ust_app_event
*ua_event
;
3878 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
3879 uevent
->attr
.name
, usess
->id
);
3882 * NOTE: At this point, this function is called only if the session and
3883 * channel passed are already created for all apps. and enabled on the
3889 /* For all registered applications */
3890 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3891 if (!app
->compatible
) {
3893 * TODO: In time, we should notice the caller of this error by
3894 * telling him that this is a version error.
3898 ua_sess
= lookup_session_by_app(usess
, app
);
3900 /* The application has problem or is probably dead. */
3904 pthread_mutex_lock(&ua_sess
->lock
);
3906 if (ua_sess
->deleted
) {
3907 pthread_mutex_unlock(&ua_sess
->lock
);
3911 /* Lookup channel in the ust app session */
3912 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3913 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3914 /* If the channel is not found, there is a code flow error */
3915 assert(ua_chan_node
);
3917 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3919 /* Get event node */
3920 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3921 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3922 if (ua_event
== NULL
) {
3923 DBG3("UST app enable event %s not found for app PID %d."
3924 "Skipping app", uevent
->attr
.name
, app
->pid
);
3928 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
3930 pthread_mutex_unlock(&ua_sess
->lock
);
3934 pthread_mutex_unlock(&ua_sess
->lock
);
3943 * For a specific existing UST session and UST channel, creates the event for
3944 * all registered apps.
3946 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
3947 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3950 struct lttng_ht_iter iter
, uiter
;
3951 struct lttng_ht_node_str
*ua_chan_node
;
3952 struct ust_app
*app
;
3953 struct ust_app_session
*ua_sess
;
3954 struct ust_app_channel
*ua_chan
;
3956 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
3957 uevent
->attr
.name
, usess
->id
);
3961 /* For all registered applications */
3962 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3963 if (!app
->compatible
) {
3965 * TODO: In time, we should notice the caller of this error by
3966 * telling him that this is a version error.
3970 ua_sess
= lookup_session_by_app(usess
, app
);
3972 /* The application has problem or is probably dead. */
3976 pthread_mutex_lock(&ua_sess
->lock
);
3978 if (ua_sess
->deleted
) {
3979 pthread_mutex_unlock(&ua_sess
->lock
);
3983 /* Lookup channel in the ust app session */
3984 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3985 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3986 /* If the channel is not found, there is a code flow error */
3987 assert(ua_chan_node
);
3989 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3991 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
3992 pthread_mutex_unlock(&ua_sess
->lock
);
3994 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
3995 /* Possible value at this point: -ENOMEM. If so, we stop! */
3998 DBG2("UST app event %s already exist on app PID %d",
3999 uevent
->attr
.name
, app
->pid
);
4010 * Start tracing for a specific UST session and app.
4013 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4016 struct ust_app_session
*ua_sess
;
4018 DBG("Starting tracing for ust app pid %d", app
->pid
);
4022 if (!app
->compatible
) {
4026 ua_sess
= lookup_session_by_app(usess
, app
);
4027 if (ua_sess
== NULL
) {
4028 /* The session is in teardown process. Ignore and continue. */
4032 pthread_mutex_lock(&ua_sess
->lock
);
4034 if (ua_sess
->deleted
) {
4035 pthread_mutex_unlock(&ua_sess
->lock
);
4039 /* Upon restart, we skip the setup, already done */
4040 if (ua_sess
->started
) {
4044 /* Create directories if consumer is LOCAL and has a path defined. */
4045 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
4046 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
4047 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
4048 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
4050 if (ret
!= -EEXIST
) {
4051 ERR("Trace directory creation error");
4058 * Create the metadata for the application. This returns gracefully if a
4059 * metadata was already set for the session.
4061 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4066 health_code_update();
4069 /* This start the UST tracing */
4070 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4072 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4073 ERR("Error starting tracing for app pid: %d (ret: %d)",
4076 DBG("UST app start session failed. Application is dead.");
4078 * This is normal behavior, an application can die during the
4079 * creation process. Don't report an error so the execution can
4080 * continue normally.
4082 pthread_mutex_unlock(&ua_sess
->lock
);
4088 /* Indicate that the session has been started once */
4089 ua_sess
->started
= 1;
4091 pthread_mutex_unlock(&ua_sess
->lock
);
4093 health_code_update();
4095 /* Quiescent wait after starting trace */
4096 ret
= ustctl_wait_quiescent(app
->sock
);
4097 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4098 ERR("UST app wait quiescent failed for app pid %d ret %d",
4104 health_code_update();
4108 pthread_mutex_unlock(&ua_sess
->lock
);
4110 health_code_update();
4115 * Stop tracing for a specific UST session and app.
4118 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4121 struct ust_app_session
*ua_sess
;
4122 struct ust_registry_session
*registry
;
4124 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4128 if (!app
->compatible
) {
4129 goto end_no_session
;
4132 ua_sess
= lookup_session_by_app(usess
, app
);
4133 if (ua_sess
== NULL
) {
4134 goto end_no_session
;
4137 pthread_mutex_lock(&ua_sess
->lock
);
4139 if (ua_sess
->deleted
) {
4140 pthread_mutex_unlock(&ua_sess
->lock
);
4141 goto end_no_session
;
4145 * If started = 0, it means that stop trace has been called for a session
4146 * that was never started. It's possible since we can have a fail start
4147 * from either the application manager thread or the command thread. Simply
4148 * indicate that this is a stop error.
4150 if (!ua_sess
->started
) {
4151 goto error_rcu_unlock
;
4154 health_code_update();
4156 /* This inhibits UST tracing */
4157 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4159 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4160 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4163 DBG("UST app stop session failed. Application is dead.");
4165 * This is normal behavior, an application can die during the
4166 * creation process. Don't report an error so the execution can
4167 * continue normally.
4171 goto error_rcu_unlock
;
4174 health_code_update();
4176 /* Quiescent wait after stopping trace */
4177 ret
= ustctl_wait_quiescent(app
->sock
);
4178 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4179 ERR("UST app wait quiescent failed for app pid %d ret %d",
4183 health_code_update();
4185 registry
= get_session_registry(ua_sess
);
4188 /* Push metadata for application before freeing the application. */
4189 (void) push_metadata(registry
, ua_sess
->consumer
);
4192 pthread_mutex_unlock(&ua_sess
->lock
);
4195 health_code_update();
4199 pthread_mutex_unlock(&ua_sess
->lock
);
4201 health_code_update();
4206 int ust_app_flush_app_session(struct ust_app
*app
,
4207 struct ust_app_session
*ua_sess
)
4209 int ret
, retval
= 0;
4210 struct lttng_ht_iter iter
;
4211 struct ust_app_channel
*ua_chan
;
4212 struct consumer_socket
*socket
;
4214 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4218 if (!app
->compatible
) {
4219 goto end_not_compatible
;
4222 pthread_mutex_lock(&ua_sess
->lock
);
4224 if (ua_sess
->deleted
) {
4228 health_code_update();
4230 /* Flushing buffers */
4231 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4234 /* Flush buffers and push metadata. */
4235 switch (ua_sess
->buffer_type
) {
4236 case LTTNG_BUFFER_PER_PID
:
4237 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4239 health_code_update();
4240 assert(ua_chan
->is_sent
);
4241 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4243 ERR("Error flushing consumer channel");
4249 case LTTNG_BUFFER_PER_UID
:
4255 health_code_update();
4258 pthread_mutex_unlock(&ua_sess
->lock
);
4262 health_code_update();
4267 * Flush buffers for all applications for a specific UST session.
4268 * Called with UST session lock held.
4271 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4276 DBG("Flushing session buffers for all ust apps");
4280 /* Flush buffers and push metadata. */
4281 switch (usess
->buffer_type
) {
4282 case LTTNG_BUFFER_PER_UID
:
4284 struct buffer_reg_uid
*reg
;
4285 struct lttng_ht_iter iter
;
4287 /* Flush all per UID buffers associated to that session. */
4288 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4289 struct ust_registry_session
*ust_session_reg
;
4290 struct buffer_reg_channel
*reg_chan
;
4291 struct consumer_socket
*socket
;
4293 /* Get consumer socket to use to push the metadata.*/
4294 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4297 /* Ignore request if no consumer is found for the session. */
4301 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4302 reg_chan
, node
.node
) {
4304 * The following call will print error values so the return
4305 * code is of little importance because whatever happens, we
4306 * have to try them all.
4308 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4311 ust_session_reg
= reg
->registry
->reg
.ust
;
4312 /* Push metadata. */
4313 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4317 case LTTNG_BUFFER_PER_PID
:
4319 struct ust_app_session
*ua_sess
;
4320 struct lttng_ht_iter iter
;
4321 struct ust_app
*app
;
4323 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4324 ua_sess
= lookup_session_by_app(usess
, app
);
4325 if (ua_sess
== NULL
) {
4328 (void) ust_app_flush_app_session(app
, ua_sess
);
4339 health_code_update();
4344 * Destroy a specific UST session in apps.
4346 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4349 struct ust_app_session
*ua_sess
;
4350 struct lttng_ht_iter iter
;
4351 struct lttng_ht_node_u64
*node
;
4353 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4357 if (!app
->compatible
) {
4361 __lookup_session_by_app(usess
, app
, &iter
);
4362 node
= lttng_ht_iter_get_node_u64(&iter
);
4364 /* Session is being or is deleted. */
4367 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4369 health_code_update();
4370 destroy_app_session(app
, ua_sess
);
4372 health_code_update();
4374 /* Quiescent wait after stopping trace */
4375 ret
= ustctl_wait_quiescent(app
->sock
);
4376 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4377 ERR("UST app wait quiescent failed for app pid %d ret %d",
4382 health_code_update();
4387 * Start tracing for the UST session.
4389 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4392 struct lttng_ht_iter iter
;
4393 struct ust_app
*app
;
4395 DBG("Starting all UST traces");
4399 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4400 ret
= ust_app_start_trace(usess
, app
);
4402 /* Continue to next apps even on error */
4413 * Start tracing for the UST session.
4414 * Called with UST session lock held.
4416 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4419 struct lttng_ht_iter iter
;
4420 struct ust_app
*app
;
4422 DBG("Stopping all UST traces");
4426 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4427 ret
= ust_app_stop_trace(usess
, app
);
4429 /* Continue to next apps even on error */
4434 (void) ust_app_flush_session(usess
);
4442 * Destroy app UST session.
4444 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4447 struct lttng_ht_iter iter
;
4448 struct ust_app
*app
;
4450 DBG("Destroy all UST traces");
4454 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4455 ret
= destroy_trace(usess
, app
);
4457 /* Continue to next apps even on error */
4468 void ust_app_global_create(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4471 struct lttng_ht_iter iter
, uiter
;
4472 struct ust_app_session
*ua_sess
= NULL
;
4473 struct ust_app_channel
*ua_chan
;
4474 struct ust_app_event
*ua_event
;
4475 struct ust_app_ctx
*ua_ctx
;
4478 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &is_created
);
4480 /* Tracer is probably gone or ENOMEM. */
4484 /* App session already created. */
4489 pthread_mutex_lock(&ua_sess
->lock
);
4491 if (ua_sess
->deleted
) {
4492 pthread_mutex_unlock(&ua_sess
->lock
);
4497 * We can iterate safely here over all UST app session since the create ust
4498 * app session above made a shadow copy of the UST global domain from the
4501 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4503 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4506 * Stop everything. On error, the application failed, no more
4507 * file descriptor are available or ENOMEM so stopping here is
4508 * the only thing we can do for now.
4514 * Add context using the list so they are enabled in the same order the
4517 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
4518 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
4525 /* For each events */
4526 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
4528 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
4535 pthread_mutex_unlock(&ua_sess
->lock
);
4537 if (usess
->active
) {
4538 ret
= ust_app_start_trace(usess
, app
);
4543 DBG2("UST trace started for app pid %d", app
->pid
);
4546 /* Everything went well at this point. */
4550 pthread_mutex_unlock(&ua_sess
->lock
);
4553 destroy_app_session(app
, ua_sess
);
4559 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4561 struct ust_app_session
*ua_sess
;
4563 ua_sess
= lookup_session_by_app(usess
, app
);
4564 if (ua_sess
== NULL
) {
4567 destroy_app_session(app
, ua_sess
);
4571 * Add channels/events from UST global domain to registered apps at sock.
4573 * Called with session lock held.
4574 * Called with RCU read-side lock held.
4576 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4580 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
4581 app
->sock
, usess
->id
);
4583 if (!app
->compatible
) {
4587 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4588 ust_app_global_create(usess
, app
);
4590 ust_app_global_destroy(usess
, app
);
4595 * Called with session lock held.
4597 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
4599 struct lttng_ht_iter iter
;
4600 struct ust_app
*app
;
4603 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4604 ust_app_global_update(usess
, app
);
4610 * Add context to a specific channel for global UST domain.
4612 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
4613 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
4616 struct lttng_ht_node_str
*ua_chan_node
;
4617 struct lttng_ht_iter iter
, uiter
;
4618 struct ust_app_channel
*ua_chan
= NULL
;
4619 struct ust_app_session
*ua_sess
;
4620 struct ust_app
*app
;
4624 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4625 if (!app
->compatible
) {
4627 * TODO: In time, we should notice the caller of this error by
4628 * telling him that this is a version error.
4632 ua_sess
= lookup_session_by_app(usess
, app
);
4633 if (ua_sess
== NULL
) {
4637 pthread_mutex_lock(&ua_sess
->lock
);
4639 if (ua_sess
->deleted
) {
4640 pthread_mutex_unlock(&ua_sess
->lock
);
4644 /* Lookup channel in the ust app session */
4645 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4646 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4647 if (ua_chan_node
== NULL
) {
4650 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4652 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4657 pthread_mutex_unlock(&ua_sess
->lock
);
4665 * Enable event for a channel from a UST session for a specific PID.
4667 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4668 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4671 struct lttng_ht_iter iter
;
4672 struct lttng_ht_node_str
*ua_chan_node
;
4673 struct ust_app
*app
;
4674 struct ust_app_session
*ua_sess
;
4675 struct ust_app_channel
*ua_chan
;
4676 struct ust_app_event
*ua_event
;
4678 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4682 app
= ust_app_find_by_pid(pid
);
4684 ERR("UST app enable event per PID %d not found", pid
);
4689 if (!app
->compatible
) {
4694 ua_sess
= lookup_session_by_app(usess
, app
);
4696 /* The application has problem or is probably dead. */
4701 pthread_mutex_lock(&ua_sess
->lock
);
4703 if (ua_sess
->deleted
) {
4708 /* Lookup channel in the ust app session */
4709 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4710 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4711 /* If the channel is not found, there is a code flow error */
4712 assert(ua_chan_node
);
4714 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4716 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4717 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4718 if (ua_event
== NULL
) {
4719 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4724 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4731 pthread_mutex_unlock(&ua_sess
->lock
);
4738 * Calibrate registered applications.
4740 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
4743 struct lttng_ht_iter iter
;
4744 struct ust_app
*app
;
4748 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4749 if (!app
->compatible
) {
4751 * TODO: In time, we should notice the caller of this error by
4752 * telling him that this is a version error.
4757 health_code_update();
4759 ret
= ustctl_calibrate(app
->sock
, calibrate
);
4763 /* Means that it's not implemented on the tracer side. */
4767 DBG2("Calibrate app PID %d returned with error %d",
4774 DBG("UST app global domain calibration finished");
4778 health_code_update();
4784 * Receive registration and populate the given msg structure.
4786 * On success return 0 else a negative value returned by the ustctl call.
4788 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
4791 uint32_t pid
, ppid
, uid
, gid
;
4795 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
4796 &pid
, &ppid
, &uid
, &gid
,
4797 &msg
->bits_per_long
,
4798 &msg
->uint8_t_alignment
,
4799 &msg
->uint16_t_alignment
,
4800 &msg
->uint32_t_alignment
,
4801 &msg
->uint64_t_alignment
,
4802 &msg
->long_alignment
,
4809 case LTTNG_UST_ERR_EXITING
:
4810 DBG3("UST app recv reg message failed. Application died");
4812 case LTTNG_UST_ERR_UNSUP_MAJOR
:
4813 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
4814 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
4815 LTTNG_UST_ABI_MINOR_VERSION
);
4818 ERR("UST app recv reg message failed with ret %d", ret
);
4823 msg
->pid
= (pid_t
) pid
;
4824 msg
->ppid
= (pid_t
) ppid
;
4825 msg
->uid
= (uid_t
) uid
;
4826 msg
->gid
= (gid_t
) gid
;
4833 * Return a ust app channel object using the application object and the channel
4834 * object descriptor has a key. If not found, NULL is returned. A RCU read side
4835 * lock MUST be acquired before calling this function.
4837 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
4840 struct lttng_ht_node_ulong
*node
;
4841 struct lttng_ht_iter iter
;
4842 struct ust_app_channel
*ua_chan
= NULL
;
4846 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
4847 node
= lttng_ht_iter_get_node_ulong(&iter
);
4849 DBG2("UST app channel find by objd %d not found", objd
);
4853 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
4860 * Reply to a register channel notification from an application on the notify
4861 * socket. The channel metadata is also created.
4863 * The session UST registry lock is acquired in this function.
4865 * On success 0 is returned else a negative value.
4867 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
4868 size_t nr_fields
, struct ustctl_field
*fields
)
4870 int ret
, ret_code
= 0;
4871 uint32_t chan_id
, reg_count
;
4872 uint64_t chan_reg_key
;
4873 enum ustctl_channel_header type
;
4874 struct ust_app
*app
;
4875 struct ust_app_channel
*ua_chan
;
4876 struct ust_app_session
*ua_sess
;
4877 struct ust_registry_session
*registry
;
4878 struct ust_registry_channel
*chan_reg
;
4882 /* Lookup application. If not found, there is a code flow error. */
4883 app
= find_app_by_notify_sock(sock
);
4885 DBG("Application socket %d is being teardown. Abort event notify",
4889 goto error_rcu_unlock
;
4892 /* Lookup channel by UST object descriptor. */
4893 ua_chan
= find_channel_by_objd(app
, cobjd
);
4895 DBG("Application channel is being teardown. Abort event notify");
4898 goto error_rcu_unlock
;
4901 assert(ua_chan
->session
);
4902 ua_sess
= ua_chan
->session
;
4904 /* Get right session registry depending on the session buffer type. */
4905 registry
= get_session_registry(ua_sess
);
4908 /* Depending on the buffer type, a different channel key is used. */
4909 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4910 chan_reg_key
= ua_chan
->tracing_channel_id
;
4912 chan_reg_key
= ua_chan
->key
;
4915 pthread_mutex_lock(®istry
->lock
);
4917 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
4920 if (!chan_reg
->register_done
) {
4921 reg_count
= ust_registry_get_event_count(chan_reg
);
4922 if (reg_count
< 31) {
4923 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
4925 type
= USTCTL_CHANNEL_HEADER_LARGE
;
4928 chan_reg
->nr_ctx_fields
= nr_fields
;
4929 chan_reg
->ctx_fields
= fields
;
4930 chan_reg
->header_type
= type
;
4932 /* Get current already assigned values. */
4933 type
= chan_reg
->header_type
;
4935 /* Set to NULL so the error path does not do a double free. */
4938 /* Channel id is set during the object creation. */
4939 chan_id
= chan_reg
->chan_id
;
4941 /* Append to metadata */
4942 if (!chan_reg
->metadata_dumped
) {
4943 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
4945 ERR("Error appending channel metadata (errno = %d)", ret_code
);
4951 DBG3("UST app replying to register channel key %" PRIu64
4952 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
4955 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
4957 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4958 ERR("UST app reply channel failed with ret %d", ret
);
4960 DBG3("UST app reply channel failed. Application died");
4965 /* This channel registry registration is completed. */
4966 chan_reg
->register_done
= 1;
4969 pthread_mutex_unlock(®istry
->lock
);
4979 * Add event to the UST channel registry. When the event is added to the
4980 * registry, the metadata is also created. Once done, this replies to the
4981 * application with the appropriate error code.
4983 * The session UST registry lock is acquired in the function.
4985 * On success 0 is returned else a negative value.
4987 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
4988 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
4989 char *model_emf_uri
)
4992 uint32_t event_id
= 0;
4993 uint64_t chan_reg_key
;
4994 struct ust_app
*app
;
4995 struct ust_app_channel
*ua_chan
;
4996 struct ust_app_session
*ua_sess
;
4997 struct ust_registry_session
*registry
;
5001 /* Lookup application. If not found, there is a code flow error. */
5002 app
= find_app_by_notify_sock(sock
);
5004 DBG("Application socket %d is being teardown. Abort event notify",
5009 free(model_emf_uri
);
5010 goto error_rcu_unlock
;
5013 /* Lookup channel by UST object descriptor. */
5014 ua_chan
= find_channel_by_objd(app
, cobjd
);
5016 DBG("Application channel is being teardown. Abort event notify");
5020 free(model_emf_uri
);
5021 goto error_rcu_unlock
;
5024 assert(ua_chan
->session
);
5025 ua_sess
= ua_chan
->session
;
5027 registry
= get_session_registry(ua_sess
);
5030 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5031 chan_reg_key
= ua_chan
->tracing_channel_id
;
5033 chan_reg_key
= ua_chan
->key
;
5036 pthread_mutex_lock(®istry
->lock
);
5039 * From this point on, this call acquires the ownership of the sig, fields
5040 * and model_emf_uri meaning any free are done inside it if needed. These
5041 * three variables MUST NOT be read/write after this.
5043 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5044 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
, loglevel
,
5045 model_emf_uri
, ua_sess
->buffer_type
, &event_id
,
5049 * The return value is returned to ustctl so in case of an error, the
5050 * application can be notified. In case of an error, it's important not to
5051 * return a negative error or else the application will get closed.
5053 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5055 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5056 ERR("UST app reply event failed with ret %d", ret
);
5058 DBG3("UST app reply event failed. Application died");
5061 * No need to wipe the create event since the application socket will
5062 * get close on error hence cleaning up everything by itself.
5067 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5071 pthread_mutex_unlock(®istry
->lock
);
5078 * Handle application notification through the given notify socket.
5080 * Return 0 on success or else a negative value.
5082 int ust_app_recv_notify(int sock
)
5085 enum ustctl_notify_cmd cmd
;
5087 DBG3("UST app receiving notify from sock %d", sock
);
5089 ret
= ustctl_recv_notify(sock
, &cmd
);
5091 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5092 ERR("UST app recv notify failed with ret %d", ret
);
5094 DBG3("UST app recv notify failed. Application died");
5100 case USTCTL_NOTIFY_CMD_EVENT
:
5102 int sobjd
, cobjd
, loglevel
;
5103 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5105 struct ustctl_field
*fields
;
5107 DBG2("UST app ustctl register event received");
5109 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
5110 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
5112 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5113 ERR("UST app recv event failed with ret %d", ret
);
5115 DBG3("UST app recv event failed. Application died");
5121 * Add event to the UST registry coming from the notify socket. This
5122 * call will free if needed the sig, fields and model_emf_uri. This
5123 * code path loses the ownsership of these variables and transfer them
5124 * to the this function.
5126 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5127 fields
, loglevel
, model_emf_uri
);
5134 case USTCTL_NOTIFY_CMD_CHANNEL
:
5138 struct ustctl_field
*fields
;
5140 DBG2("UST app ustctl register channel received");
5142 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5145 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5146 ERR("UST app recv channel failed with ret %d", ret
);
5148 DBG3("UST app recv channel failed. Application died");
5154 * The fields ownership are transfered to this function call meaning
5155 * that if needed it will be freed. After this, it's invalid to access
5156 * fields or clean it up.
5158 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
5167 /* Should NEVER happen. */
5176 * Once the notify socket hangs up, this is called. First, it tries to find the
5177 * corresponding application. On failure, the call_rcu to close the socket is
5178 * executed. If an application is found, it tries to delete it from the notify
5179 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5181 * Note that an object needs to be allocated here so on ENOMEM failure, the
5182 * call RCU is not done but the rest of the cleanup is.
5184 void ust_app_notify_sock_unregister(int sock
)
5187 struct lttng_ht_iter iter
;
5188 struct ust_app
*app
;
5189 struct ust_app_notify_sock_obj
*obj
;
5195 obj
= zmalloc(sizeof(*obj
));
5198 * An ENOMEM is kind of uncool. If this strikes we continue the
5199 * procedure but the call_rcu will not be called. In this case, we
5200 * accept the fd leak rather than possibly creating an unsynchronized
5201 * state between threads.
5203 * TODO: The notify object should be created once the notify socket is
5204 * registered and stored independantely from the ust app object. The
5205 * tricky part is to synchronize the teardown of the application and
5206 * this notify object. Let's keep that in mind so we can avoid this
5207 * kind of shenanigans with ENOMEM in the teardown path.
5214 DBG("UST app notify socket unregister %d", sock
);
5217 * Lookup application by notify socket. If this fails, this means that the
5218 * hash table delete has already been done by the application
5219 * unregistration process so we can safely close the notify socket in a
5222 app
= find_app_by_notify_sock(sock
);
5227 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5230 * Whatever happens here either we fail or succeed, in both cases we have
5231 * to close the socket after a grace period to continue to the call RCU
5232 * here. If the deletion is successful, the application is not visible
5233 * anymore by other threads and is it fails it means that it was already
5234 * deleted from the hash table so either way we just have to close the
5237 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5243 * Close socket after a grace period to avoid for the socket to be reused
5244 * before the application object is freed creating potential race between
5245 * threads trying to add unique in the global hash table.
5248 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5253 * Destroy a ust app data structure and free its memory.
5255 void ust_app_destroy(struct ust_app
*app
)
5261 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5265 * Take a snapshot for a given UST session. The snapshot is sent to the given
5268 * Return 0 on success or else a negative value.
5270 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5271 struct snapshot_output
*output
, int wait
,
5272 uint64_t nb_packets_per_stream
)
5275 unsigned int snapshot_done
= 0;
5276 struct lttng_ht_iter iter
;
5277 struct ust_app
*app
;
5278 char pathname
[PATH_MAX
];
5285 switch (usess
->buffer_type
) {
5286 case LTTNG_BUFFER_PER_UID
:
5288 struct buffer_reg_uid
*reg
;
5290 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5291 struct buffer_reg_channel
*reg_chan
;
5292 struct consumer_socket
*socket
;
5294 /* Get consumer socket to use to push the metadata.*/
5295 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5302 memset(pathname
, 0, sizeof(pathname
));
5303 ret
= snprintf(pathname
, sizeof(pathname
),
5304 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5305 reg
->uid
, reg
->bits_per_long
);
5307 PERROR("snprintf snapshot path");
5311 /* Add the UST default trace dir to path. */
5312 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5313 reg_chan
, node
.node
) {
5314 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5315 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5316 nb_packets_per_stream
);
5321 ret
= consumer_snapshot_channel(socket
,
5322 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5323 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
5331 case LTTNG_BUFFER_PER_PID
:
5333 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5334 struct consumer_socket
*socket
;
5335 struct lttng_ht_iter chan_iter
;
5336 struct ust_app_channel
*ua_chan
;
5337 struct ust_app_session
*ua_sess
;
5338 struct ust_registry_session
*registry
;
5340 ua_sess
= lookup_session_by_app(usess
, app
);
5342 /* Session not associated with this app. */
5346 /* Get the right consumer socket for the application. */
5347 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5354 /* Add the UST default trace dir to path. */
5355 memset(pathname
, 0, sizeof(pathname
));
5356 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5359 PERROR("snprintf snapshot path");
5363 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5364 ua_chan
, node
.node
) {
5365 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
5366 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
5367 nb_packets_per_stream
);
5373 registry
= get_session_registry(ua_sess
);
5375 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
5376 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
5389 if (!snapshot_done
) {
5391 * If no snapshot was made and we are not in the error path, this means
5392 * that there are no buffers thus no (prior) application to snapshot
5393 * data from so we have simply NO data.
5404 * Return the size taken by one more packet per stream.
5406 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
5407 uint64_t cur_nr_packets
)
5409 uint64_t tot_size
= 0;
5410 struct ust_app
*app
;
5411 struct lttng_ht_iter iter
;
5415 switch (usess
->buffer_type
) {
5416 case LTTNG_BUFFER_PER_UID
:
5418 struct buffer_reg_uid
*reg
;
5420 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5421 struct buffer_reg_channel
*reg_chan
;
5424 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5425 reg_chan
, node
.node
) {
5426 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
5428 * Don't take channel into account if we
5429 * already grab all its packets.
5433 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
5439 case LTTNG_BUFFER_PER_PID
:
5442 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5443 struct ust_app_channel
*ua_chan
;
5444 struct ust_app_session
*ua_sess
;
5445 struct lttng_ht_iter chan_iter
;
5447 ua_sess
= lookup_session_by_app(usess
, app
);
5449 /* Session not associated with this app. */
5453 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5454 ua_chan
, node
.node
) {
5455 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
5457 * Don't take channel into account if we
5458 * already grab all its packets.
5462 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;