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
);
1438 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1439 ret
= -ENOTCONN
; /* Caused by app exiting. */
1441 } else if (ret
< 0) {
1445 health_code_update();
1447 /* Send all streams to application. */
1448 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1449 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1450 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1451 ret
= -ENOTCONN
; /* Caused by app exiting. */
1453 } else if (ret
< 0) {
1456 /* We don't need the stream anymore once sent to the tracer. */
1457 cds_list_del(&stream
->list
);
1458 delete_ust_app_stream(-1, stream
);
1460 /* Flag the channel that it is sent to the application. */
1461 ua_chan
->is_sent
= 1;
1464 health_code_update();
1469 * Create the specified event onto the UST tracer for a UST session.
1471 * Should be called with session mutex held.
1474 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1475 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1479 health_code_update();
1481 /* Create UST event on tracer */
1482 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1485 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1486 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1487 ua_event
->attr
.name
, app
->pid
, ret
);
1490 * This is normal behavior, an application can die during the
1491 * creation process. Don't report an error so the execution can
1492 * continue normally.
1495 DBG3("UST app create event failed. Application is dead.");
1500 ua_event
->handle
= ua_event
->obj
->handle
;
1502 DBG2("UST app event %s created successfully for pid:%d",
1503 ua_event
->attr
.name
, app
->pid
);
1505 health_code_update();
1507 /* Set filter if one is present. */
1508 if (ua_event
->filter
) {
1509 ret
= set_ust_event_filter(ua_event
, app
);
1515 /* Set exclusions for the event */
1516 if (ua_event
->exclusion
) {
1517 ret
= set_ust_event_exclusion(ua_event
, app
);
1523 /* If event not enabled, disable it on the tracer */
1524 if (ua_event
->enabled
) {
1526 * We now need to explicitly enable the event, since it
1527 * is now disabled at creation.
1529 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1532 * If we hit an EPERM, something is wrong with our enable call. If
1533 * we get an EEXIST, there is a problem on the tracer side since we
1537 case -LTTNG_UST_ERR_PERM
:
1538 /* Code flow problem */
1540 case -LTTNG_UST_ERR_EXIST
:
1541 /* It's OK for our use case. */
1552 health_code_update();
1557 * Copy data between an UST app event and a LTT event.
1559 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1560 struct ltt_ust_event
*uevent
)
1562 size_t exclusion_alloc_size
;
1564 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1565 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1567 ua_event
->enabled
= uevent
->enabled
;
1569 /* Copy event attributes */
1570 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1572 /* Copy filter bytecode */
1573 if (uevent
->filter
) {
1574 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1575 /* Filter might be NULL here in case of ENONEM. */
1578 /* Copy exclusion data */
1579 if (uevent
->exclusion
) {
1580 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1581 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1582 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1583 if (ua_event
->exclusion
== NULL
) {
1586 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1587 exclusion_alloc_size
);
1593 * Copy data between an UST app channel and a LTT channel.
1595 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1596 struct ltt_ust_channel
*uchan
)
1598 struct lttng_ht_iter iter
;
1599 struct ltt_ust_event
*uevent
;
1600 struct ltt_ust_context
*uctx
;
1601 struct ust_app_event
*ua_event
;
1602 struct ust_app_ctx
*ua_ctx
;
1604 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1606 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1607 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1609 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1610 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1612 /* Copy event attributes since the layout is different. */
1613 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1614 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1615 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1616 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1617 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1618 ua_chan
->attr
.output
= uchan
->attr
.output
;
1620 * Note that the attribute channel type is not set since the channel on the
1621 * tracing registry side does not have this information.
1624 ua_chan
->enabled
= uchan
->enabled
;
1625 ua_chan
->tracing_channel_id
= uchan
->id
;
1627 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1628 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1629 if (ua_ctx
== NULL
) {
1632 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1633 (unsigned long) ua_ctx
->ctx
.ctx
);
1634 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1635 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1638 /* Copy all events from ltt ust channel to ust app channel */
1639 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1640 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1641 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1642 if (ua_event
== NULL
) {
1643 DBG2("UST event %s not found on shadow copy channel",
1645 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1646 if (ua_event
== NULL
) {
1649 shadow_copy_event(ua_event
, uevent
);
1650 add_unique_ust_app_event(ua_chan
, ua_event
);
1654 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1658 * Copy data between a UST app session and a regular LTT session.
1660 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1661 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1663 struct lttng_ht_node_str
*ua_chan_node
;
1664 struct lttng_ht_iter iter
;
1665 struct ltt_ust_channel
*uchan
;
1666 struct ust_app_channel
*ua_chan
;
1668 struct tm
*timeinfo
;
1671 char tmp_shm_path
[PATH_MAX
];
1673 /* Get date and time for unique app path */
1675 timeinfo
= localtime(&rawtime
);
1676 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1678 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1680 ua_sess
->tracing_id
= usess
->id
;
1681 ua_sess
->id
= get_next_session_id();
1682 ua_sess
->uid
= app
->uid
;
1683 ua_sess
->gid
= app
->gid
;
1684 ua_sess
->euid
= usess
->uid
;
1685 ua_sess
->egid
= usess
->gid
;
1686 ua_sess
->buffer_type
= usess
->buffer_type
;
1687 ua_sess
->bits_per_long
= app
->bits_per_long
;
1688 /* There is only one consumer object per session possible. */
1689 ua_sess
->consumer
= usess
->consumer
;
1690 ua_sess
->output_traces
= usess
->output_traces
;
1691 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1692 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1693 &usess
->metadata_attr
);
1695 switch (ua_sess
->buffer_type
) {
1696 case LTTNG_BUFFER_PER_PID
:
1697 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1698 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1701 case LTTNG_BUFFER_PER_UID
:
1702 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1703 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1710 PERROR("asprintf UST shadow copy session");
1715 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1716 sizeof(ua_sess
->root_shm_path
));
1717 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1718 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1719 sizeof(ua_sess
->shm_path
));
1720 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1721 if (ua_sess
->shm_path
[0]) {
1722 switch (ua_sess
->buffer_type
) {
1723 case LTTNG_BUFFER_PER_PID
:
1724 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1725 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1726 app
->name
, app
->pid
, datetime
);
1728 case LTTNG_BUFFER_PER_UID
:
1729 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1730 DEFAULT_UST_TRACE_UID_PATH
,
1731 app
->uid
, app
->bits_per_long
);
1738 PERROR("sprintf UST shadow copy session");
1742 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1743 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1744 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1747 /* Iterate over all channels in global domain. */
1748 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1750 struct lttng_ht_iter uiter
;
1752 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1753 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1754 if (ua_chan_node
!= NULL
) {
1755 /* Session exist. Contiuing. */
1759 DBG2("Channel %s not found on shadow session copy, creating it",
1761 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1762 if (ua_chan
== NULL
) {
1763 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1766 shadow_copy_channel(ua_chan
, uchan
);
1768 * The concept of metadata channel does not exist on the tracing
1769 * registry side of the session daemon so this can only be a per CPU
1770 * channel and not metadata.
1772 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1774 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1782 * Lookup sesison wrapper.
1785 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1786 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1788 /* Get right UST app session from app */
1789 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1793 * Return ust app session from the app session hashtable using the UST session
1796 static struct ust_app_session
*lookup_session_by_app(
1797 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1799 struct lttng_ht_iter iter
;
1800 struct lttng_ht_node_u64
*node
;
1802 __lookup_session_by_app(usess
, app
, &iter
);
1803 node
= lttng_ht_iter_get_node_u64(&iter
);
1808 return caa_container_of(node
, struct ust_app_session
, node
);
1815 * Setup buffer registry per PID for the given session and application. If none
1816 * is found, a new one is created, added to the global registry and
1817 * initialized. If regp is valid, it's set with the newly created object.
1819 * Return 0 on success or else a negative value.
1821 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1822 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1825 struct buffer_reg_pid
*reg_pid
;
1832 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1835 * This is the create channel path meaning that if there is NO
1836 * registry available, we have to create one for this session.
1838 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
1839 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1847 /* Initialize registry. */
1848 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1849 app
->bits_per_long
, app
->uint8_t_alignment
,
1850 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1851 app
->uint64_t_alignment
, app
->long_alignment
,
1852 app
->byte_order
, app
->version
.major
,
1853 app
->version
.minor
, reg_pid
->root_shm_path
,
1855 ua_sess
->euid
, ua_sess
->egid
);
1858 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1859 * destroy the buffer registry, because it is always expected
1860 * that if the buffer registry can be found, its ust registry is
1863 buffer_reg_pid_destroy(reg_pid
);
1867 buffer_reg_pid_add(reg_pid
);
1869 DBG3("UST app buffer registry per PID created successfully");
1881 * Setup buffer registry per UID for the given session and application. If none
1882 * is found, a new one is created, added to the global registry and
1883 * initialized. If regp is valid, it's set with the newly created object.
1885 * Return 0 on success or else a negative value.
1887 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
1888 struct ust_app_session
*ua_sess
,
1889 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
1892 struct buffer_reg_uid
*reg_uid
;
1899 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
1902 * This is the create channel path meaning that if there is NO
1903 * registry available, we have to create one for this session.
1905 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
1906 LTTNG_DOMAIN_UST
, ®_uid
,
1907 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1915 /* Initialize registry. */
1916 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
1917 app
->bits_per_long
, app
->uint8_t_alignment
,
1918 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1919 app
->uint64_t_alignment
, app
->long_alignment
,
1920 app
->byte_order
, app
->version
.major
,
1921 app
->version
.minor
, reg_uid
->root_shm_path
,
1922 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
1925 * reg_uid->registry->reg.ust is NULL upon error, so we need to
1926 * destroy the buffer registry, because it is always expected
1927 * that if the buffer registry can be found, its ust registry is
1930 buffer_reg_uid_destroy(reg_uid
, NULL
);
1933 /* Add node to teardown list of the session. */
1934 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
1936 buffer_reg_uid_add(reg_uid
);
1938 DBG3("UST app buffer registry per UID created successfully");
1949 * Create a session on the tracer side for the given app.
1951 * On success, ua_sess_ptr is populated with the session pointer or else left
1952 * untouched. If the session was created, is_created is set to 1. On error,
1953 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1956 * Returns 0 on success or else a negative code which is either -ENOMEM or
1957 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1959 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1960 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1963 int ret
, created
= 0;
1964 struct ust_app_session
*ua_sess
;
1968 assert(ua_sess_ptr
);
1970 health_code_update();
1972 ua_sess
= lookup_session_by_app(usess
, app
);
1973 if (ua_sess
== NULL
) {
1974 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
1975 app
->pid
, usess
->id
);
1976 ua_sess
= alloc_ust_app_session(app
);
1977 if (ua_sess
== NULL
) {
1978 /* Only malloc can failed so something is really wrong */
1982 shadow_copy_session(ua_sess
, usess
, app
);
1986 switch (usess
->buffer_type
) {
1987 case LTTNG_BUFFER_PER_PID
:
1988 /* Init local registry. */
1989 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
1991 delete_ust_app_session(-1, ua_sess
, app
);
1995 case LTTNG_BUFFER_PER_UID
:
1996 /* Look for a global registry. If none exists, create one. */
1997 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
1999 delete_ust_app_session(-1, ua_sess
, app
);
2009 health_code_update();
2011 if (ua_sess
->handle
== -1) {
2012 ret
= ustctl_create_session(app
->sock
);
2014 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2015 ERR("Creating session for app pid %d with ret %d",
2018 DBG("UST app creating session failed. Application is dead");
2020 * This is normal behavior, an application can die during the
2021 * creation process. Don't report an error so the execution can
2022 * continue normally. This will get flagged ENOTCONN and the
2023 * caller will handle it.
2027 delete_ust_app_session(-1, ua_sess
, app
);
2028 if (ret
!= -ENOMEM
) {
2030 * Tracer is probably gone or got an internal error so let's
2031 * behave like it will soon unregister or not usable.
2038 ua_sess
->handle
= ret
;
2040 /* Add ust app session to app's HT */
2041 lttng_ht_node_init_u64(&ua_sess
->node
,
2042 ua_sess
->tracing_id
);
2043 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2045 DBG2("UST app session created successfully with handle %d", ret
);
2048 *ua_sess_ptr
= ua_sess
;
2050 *is_created
= created
;
2053 /* Everything went well. */
2057 health_code_update();
2062 * Match function for a hash table lookup of ust_app_ctx.
2064 * It matches an ust app context based on the context type and, in the case
2065 * of perf counters, their name.
2067 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2069 struct ust_app_ctx
*ctx
;
2070 const struct lttng_ust_context
*key
;
2075 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2079 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2083 /* Check the name in the case of perf thread counters. */
2084 if (key
->ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
2085 if (strncmp(key
->u
.perf_counter
.name
,
2086 ctx
->ctx
.u
.perf_counter
.name
,
2087 sizeof(key
->u
.perf_counter
.name
))) {
2100 * Lookup for an ust app context from an lttng_ust_context.
2102 * Must be called while holding RCU read side lock.
2103 * Return an ust_app_ctx object or NULL on error.
2106 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2107 struct lttng_ust_context
*uctx
)
2109 struct lttng_ht_iter iter
;
2110 struct lttng_ht_node_ulong
*node
;
2111 struct ust_app_ctx
*app_ctx
= NULL
;
2116 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2117 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2118 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2119 node
= lttng_ht_iter_get_node_ulong(&iter
);
2124 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2131 * Create a context for the channel on the tracer.
2133 * Called with UST app session lock held and a RCU read side lock.
2136 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2137 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
2138 struct ust_app
*app
)
2141 struct ust_app_ctx
*ua_ctx
;
2143 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2145 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2151 ua_ctx
= alloc_ust_app_ctx(uctx
);
2152 if (ua_ctx
== NULL
) {
2158 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2159 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2160 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2162 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2172 * Enable on the tracer side a ust app event for the session and channel.
2174 * Called with UST app session lock held.
2177 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2178 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2182 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2187 ua_event
->enabled
= 1;
2194 * Disable on the tracer side a ust app event for the session and channel.
2196 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2197 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2201 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2206 ua_event
->enabled
= 0;
2213 * Lookup ust app channel for session and disable it on the tracer side.
2216 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2217 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2221 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2226 ua_chan
->enabled
= 0;
2233 * Lookup ust app channel for session and enable it on the tracer side. This
2234 * MUST be called with a RCU read side lock acquired.
2236 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2237 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2240 struct lttng_ht_iter iter
;
2241 struct lttng_ht_node_str
*ua_chan_node
;
2242 struct ust_app_channel
*ua_chan
;
2244 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2245 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2246 if (ua_chan_node
== NULL
) {
2247 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2248 uchan
->name
, ua_sess
->tracing_id
);
2252 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2254 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2264 * Ask the consumer to create a channel and get it if successful.
2266 * Return 0 on success or else a negative value.
2268 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2269 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2270 int bitness
, struct ust_registry_session
*registry
)
2273 unsigned int nb_fd
= 0;
2274 struct consumer_socket
*socket
;
2282 health_code_update();
2284 /* Get the right consumer socket for the application. */
2285 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2291 health_code_update();
2293 /* Need one fd for the channel. */
2294 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2296 ERR("Exhausted number of available FD upon create channel");
2301 * Ask consumer to create channel. The consumer will return the number of
2302 * stream we have to expect.
2304 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2311 * Compute the number of fd needed before receiving them. It must be 2 per
2312 * stream (2 being the default value here).
2314 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2316 /* Reserve the amount of file descriptor we need. */
2317 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2319 ERR("Exhausted number of available FD upon create channel");
2320 goto error_fd_get_stream
;
2323 health_code_update();
2326 * Now get the channel from the consumer. This call wil populate the stream
2327 * list of that channel and set the ust objects.
2329 if (usess
->consumer
->enabled
) {
2330 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2340 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2341 error_fd_get_stream
:
2343 * Initiate a destroy channel on the consumer since we had an error
2344 * handling it on our side. The return value is of no importance since we
2345 * already have a ret value set by the previous error that we need to
2348 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2350 lttng_fd_put(LTTNG_FD_APPS
, 1);
2352 health_code_update();
2358 * Duplicate the ust data object of the ust app stream and save it in the
2359 * buffer registry stream.
2361 * Return 0 on success or else a negative value.
2363 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2364 struct ust_app_stream
*stream
)
2371 /* Reserve the amount of file descriptor we need. */
2372 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2374 ERR("Exhausted number of available FD upon duplicate stream");
2378 /* Duplicate object for stream once the original is in the registry. */
2379 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2380 reg_stream
->obj
.ust
);
2382 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2383 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2384 lttng_fd_put(LTTNG_FD_APPS
, 2);
2387 stream
->handle
= stream
->obj
->handle
;
2394 * Duplicate the ust data object of the ust app. channel and save it in the
2395 * buffer registry channel.
2397 * Return 0 on success or else a negative value.
2399 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2400 struct ust_app_channel
*ua_chan
)
2407 /* Need two fds for the channel. */
2408 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2410 ERR("Exhausted number of available FD upon duplicate channel");
2414 /* Duplicate object for stream once the original is in the registry. */
2415 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2417 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2418 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2421 ua_chan
->handle
= ua_chan
->obj
->handle
;
2426 lttng_fd_put(LTTNG_FD_APPS
, 1);
2432 * For a given channel buffer registry, setup all streams of the given ust
2433 * application channel.
2435 * Return 0 on success or else a negative value.
2437 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2438 struct ust_app_channel
*ua_chan
)
2441 struct ust_app_stream
*stream
, *stmp
;
2446 DBG2("UST app setup buffer registry stream");
2448 /* Send all streams to application. */
2449 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2450 struct buffer_reg_stream
*reg_stream
;
2452 ret
= buffer_reg_stream_create(®_stream
);
2458 * Keep original pointer and nullify it in the stream so the delete
2459 * stream call does not release the object.
2461 reg_stream
->obj
.ust
= stream
->obj
;
2463 buffer_reg_stream_add(reg_stream
, reg_chan
);
2465 /* We don't need the streams anymore. */
2466 cds_list_del(&stream
->list
);
2467 delete_ust_app_stream(-1, stream
);
2475 * Create a buffer registry channel for the given session registry and
2476 * application channel object. If regp pointer is valid, it's set with the
2477 * created object. Important, the created object is NOT added to the session
2478 * registry hash table.
2480 * Return 0 on success else a negative value.
2482 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2483 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2486 struct buffer_reg_channel
*reg_chan
= NULL
;
2491 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2493 /* Create buffer registry channel. */
2494 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2499 reg_chan
->consumer_key
= ua_chan
->key
;
2500 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2501 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2503 /* Create and add a channel registry to session. */
2504 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2505 ua_chan
->tracing_channel_id
);
2509 buffer_reg_channel_add(reg_sess
, reg_chan
);
2518 /* Safe because the registry channel object was not added to any HT. */
2519 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2525 * Setup buffer registry channel for the given session registry and application
2526 * channel object. If regp pointer is valid, it's set with the created object.
2528 * Return 0 on success else a negative value.
2530 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2531 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
)
2538 assert(ua_chan
->obj
);
2540 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2542 /* Setup all streams for the registry. */
2543 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
);
2548 reg_chan
->obj
.ust
= ua_chan
->obj
;
2549 ua_chan
->obj
= NULL
;
2554 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2555 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2560 * Send buffer registry channel to the application.
2562 * Return 0 on success else a negative value.
2564 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2565 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2566 struct ust_app_channel
*ua_chan
)
2569 struct buffer_reg_stream
*reg_stream
;
2576 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2578 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2583 /* Send channel to the application. */
2584 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2585 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2586 ret
= -ENOTCONN
; /* Caused by app exiting. */
2588 } else if (ret
< 0) {
2592 health_code_update();
2594 /* Send all streams to application. */
2595 pthread_mutex_lock(®_chan
->stream_list_lock
);
2596 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2597 struct ust_app_stream stream
;
2599 ret
= duplicate_stream_object(reg_stream
, &stream
);
2601 goto error_stream_unlock
;
2604 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2606 (void) release_ust_app_stream(-1, &stream
);
2607 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2608 ret
= -ENOTCONN
; /* Caused by app exiting. */
2609 goto error_stream_unlock
;
2610 } else if (ret
< 0) {
2611 goto error_stream_unlock
;
2613 goto error_stream_unlock
;
2617 * The return value is not important here. This function will output an
2620 (void) release_ust_app_stream(-1, &stream
);
2622 ua_chan
->is_sent
= 1;
2624 error_stream_unlock
:
2625 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2631 * Create and send to the application the created buffers with per UID buffers.
2633 * Return 0 on success else a negative value.
2635 static int create_channel_per_uid(struct ust_app
*app
,
2636 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2637 struct ust_app_channel
*ua_chan
)
2640 struct buffer_reg_uid
*reg_uid
;
2641 struct buffer_reg_channel
*reg_chan
;
2648 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2650 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2652 * The session creation handles the creation of this global registry
2653 * object. If none can be find, there is a code flow problem or a
2658 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2661 /* Create the buffer registry channel object. */
2662 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2664 ERR("Error creating the UST channel \"%s\" registry instance",
2671 * Create the buffers on the consumer side. This call populates the
2672 * ust app channel object with all streams and data object.
2674 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2675 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2677 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2681 * Let's remove the previously created buffer registry channel so
2682 * it's not visible anymore in the session registry.
2684 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2685 ua_chan
->tracing_channel_id
);
2686 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2687 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2692 * Setup the streams and add it to the session registry.
2694 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, reg_chan
);
2696 ERR("Error setting up UST channel \"%s\"",
2703 /* Send buffers to the application. */
2704 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2706 if (ret
!= -ENOTCONN
) {
2707 ERR("Error sending channel to application");
2717 * Create and send to the application the created buffers with per PID buffers.
2719 * Return 0 on success else a negative value.
2721 static int create_channel_per_pid(struct ust_app
*app
,
2722 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2723 struct ust_app_channel
*ua_chan
)
2726 struct ust_registry_session
*registry
;
2733 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2737 registry
= get_session_registry(ua_sess
);
2740 /* Create and add a new channel registry to session. */
2741 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2743 ERR("Error creating the UST channel \"%s\" registry instance",
2748 /* Create and get channel on the consumer side. */
2749 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2750 app
->bits_per_long
, registry
);
2752 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2757 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2759 if (ret
!= -ENOTCONN
) {
2760 ERR("Error sending channel to application");
2771 * From an already allocated ust app channel, create the channel buffers if
2772 * need and send it to the application. This MUST be called with a RCU read
2773 * side lock acquired.
2775 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2776 * the application exited concurrently.
2778 static int do_create_channel(struct ust_app
*app
,
2779 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2780 struct ust_app_channel
*ua_chan
)
2789 /* Handle buffer type before sending the channel to the application. */
2790 switch (usess
->buffer_type
) {
2791 case LTTNG_BUFFER_PER_UID
:
2793 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
2799 case LTTNG_BUFFER_PER_PID
:
2801 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
2813 /* Initialize ust objd object using the received handle and add it. */
2814 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
2815 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
2817 /* If channel is not enabled, disable it on the tracer */
2818 if (!ua_chan
->enabled
) {
2819 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2830 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2831 * newly created channel if not NULL.
2833 * Called with UST app session lock and RCU read-side lock held.
2835 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2836 * the application exited concurrently.
2838 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
2839 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
2840 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
2841 struct ust_app_channel
**ua_chanp
)
2844 struct lttng_ht_iter iter
;
2845 struct lttng_ht_node_str
*ua_chan_node
;
2846 struct ust_app_channel
*ua_chan
;
2848 /* Lookup channel in the ust app session */
2849 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2850 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2851 if (ua_chan_node
!= NULL
) {
2852 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2856 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
2857 if (ua_chan
== NULL
) {
2858 /* Only malloc can fail here */
2862 shadow_copy_channel(ua_chan
, uchan
);
2864 /* Set channel type. */
2865 ua_chan
->attr
.type
= type
;
2867 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
2872 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
2875 /* Only add the channel if successful on the tracer side. */
2876 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
2880 *ua_chanp
= ua_chan
;
2883 /* Everything went well. */
2887 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
2893 * Create UST app event and create it on the tracer side.
2895 * Called with ust app session mutex held.
2898 int create_ust_app_event(struct ust_app_session
*ua_sess
,
2899 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
2900 struct ust_app
*app
)
2903 struct ust_app_event
*ua_event
;
2905 /* Get event node */
2906 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2907 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
2908 if (ua_event
!= NULL
) {
2913 /* Does not exist so create one */
2914 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
2915 if (ua_event
== NULL
) {
2916 /* Only malloc can failed so something is really wrong */
2920 shadow_copy_event(ua_event
, uevent
);
2922 /* Create it on the tracer side */
2923 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2925 /* Not found previously means that it does not exist on the tracer */
2926 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
2930 add_unique_ust_app_event(ua_chan
, ua_event
);
2932 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
2939 /* Valid. Calling here is already in a read side lock */
2940 delete_ust_app_event(-1, ua_event
);
2945 * Create UST metadata and open it on the tracer side.
2947 * Called with UST app session lock held and RCU read side lock.
2949 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
2950 struct ust_app
*app
, struct consumer_output
*consumer
)
2953 struct ust_app_channel
*metadata
;
2954 struct consumer_socket
*socket
;
2955 struct ust_registry_session
*registry
;
2961 registry
= get_session_registry(ua_sess
);
2964 pthread_mutex_lock(®istry
->lock
);
2966 /* Metadata already exists for this registry or it was closed previously */
2967 if (registry
->metadata_key
|| registry
->metadata_closed
) {
2972 /* Allocate UST metadata */
2973 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
2975 /* malloc() failed */
2980 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
2982 /* Need one fd for the channel. */
2983 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2985 ERR("Exhausted number of available FD upon create metadata");
2989 /* Get the right consumer socket for the application. */
2990 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
2993 goto error_consumer
;
2997 * Keep metadata key so we can identify it on the consumer side. Assign it
2998 * to the registry *before* we ask the consumer so we avoid the race of the
2999 * consumer requesting the metadata and the ask_channel call on our side
3000 * did not returned yet.
3002 registry
->metadata_key
= metadata
->key
;
3005 * Ask the metadata channel creation to the consumer. The metadata object
3006 * will be created by the consumer and kept their. However, the stream is
3007 * never added or monitored until we do a first push metadata to the
3010 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3013 /* Nullify the metadata key so we don't try to close it later on. */
3014 registry
->metadata_key
= 0;
3015 goto error_consumer
;
3019 * The setup command will make the metadata stream be sent to the relayd,
3020 * if applicable, and the thread managing the metadatas. This is important
3021 * because after this point, if an error occurs, the only way the stream
3022 * can be deleted is to be monitored in the consumer.
3024 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3026 /* Nullify the metadata key so we don't try to close it later on. */
3027 registry
->metadata_key
= 0;
3028 goto error_consumer
;
3031 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3032 metadata
->key
, app
->pid
);
3035 lttng_fd_put(LTTNG_FD_APPS
, 1);
3036 delete_ust_app_channel(-1, metadata
, app
);
3038 pthread_mutex_unlock(®istry
->lock
);
3043 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3044 * acquired before calling this function.
3046 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3048 struct ust_app
*app
= NULL
;
3049 struct lttng_ht_node_ulong
*node
;
3050 struct lttng_ht_iter iter
;
3052 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3053 node
= lttng_ht_iter_get_node_ulong(&iter
);
3055 DBG2("UST app no found with pid %d", pid
);
3059 DBG2("Found UST app by pid %d", pid
);
3061 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3068 * Allocate and init an UST app object using the registration information and
3069 * the command socket. This is called when the command socket connects to the
3072 * The object is returned on success or else NULL.
3074 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3076 struct ust_app
*lta
= NULL
;
3081 DBG3("UST app creating application for socket %d", sock
);
3083 if ((msg
->bits_per_long
== 64 &&
3084 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3085 || (msg
->bits_per_long
== 32 &&
3086 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3087 ERR("Registration failed: application \"%s\" (pid: %d) has "
3088 "%d-bit long, but no consumerd for this size is available.\n",
3089 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3093 lta
= zmalloc(sizeof(struct ust_app
));
3099 lta
->ppid
= msg
->ppid
;
3100 lta
->uid
= msg
->uid
;
3101 lta
->gid
= msg
->gid
;
3103 lta
->bits_per_long
= msg
->bits_per_long
;
3104 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3105 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3106 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3107 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3108 lta
->long_alignment
= msg
->long_alignment
;
3109 lta
->byte_order
= msg
->byte_order
;
3111 lta
->v_major
= msg
->major
;
3112 lta
->v_minor
= msg
->minor
;
3113 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3114 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3115 lta
->notify_sock
= -1;
3117 /* Copy name and make sure it's NULL terminated. */
3118 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3119 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3122 * Before this can be called, when receiving the registration information,
3123 * the application compatibility is checked. So, at this point, the
3124 * application can work with this session daemon.
3126 lta
->compatible
= 1;
3128 lta
->pid
= msg
->pid
;
3129 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3131 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3133 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3140 * For a given application object, add it to every hash table.
3142 void ust_app_add(struct ust_app
*app
)
3145 assert(app
->notify_sock
>= 0);
3150 * On a re-registration, we want to kick out the previous registration of
3153 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3156 * The socket _should_ be unique until _we_ call close. So, a add_unique
3157 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3158 * already in the table.
3160 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3162 /* Add application to the notify socket hash table. */
3163 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3164 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3166 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3167 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3168 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3175 * Set the application version into the object.
3177 * Return 0 on success else a negative value either an errno code or a
3178 * LTTng-UST error code.
3180 int ust_app_version(struct ust_app
*app
)
3186 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3188 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3189 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3191 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3199 * Unregister app by removing it from the global traceable app list and freeing
3202 * The socket is already closed at this point so no close to sock.
3204 void ust_app_unregister(int sock
)
3206 struct ust_app
*lta
;
3207 struct lttng_ht_node_ulong
*node
;
3208 struct lttng_ht_iter ust_app_sock_iter
;
3209 struct lttng_ht_iter iter
;
3210 struct ust_app_session
*ua_sess
;
3215 /* Get the node reference for a call_rcu */
3216 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3217 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3220 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3221 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3224 * For per-PID buffers, perform "push metadata" and flush all
3225 * application streams before removing app from hash tables,
3226 * ensuring proper behavior of data_pending check.
3227 * Remove sessions so they are not visible during deletion.
3229 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3231 struct ust_registry_session
*registry
;
3233 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3235 /* The session was already removed so scheduled for teardown. */
3239 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3240 (void) ust_app_flush_app_session(lta
, ua_sess
);
3244 * Add session to list for teardown. This is safe since at this point we
3245 * are the only one using this list.
3247 pthread_mutex_lock(&ua_sess
->lock
);
3249 if (ua_sess
->deleted
) {
3250 pthread_mutex_unlock(&ua_sess
->lock
);
3255 * Normally, this is done in the delete session process which is
3256 * executed in the call rcu below. However, upon registration we can't
3257 * afford to wait for the grace period before pushing data or else the
3258 * data pending feature can race between the unregistration and stop
3259 * command where the data pending command is sent *before* the grace
3262 * The close metadata below nullifies the metadata pointer in the
3263 * session so the delete session will NOT push/close a second time.
3265 registry
= get_session_registry(ua_sess
);
3267 /* Push metadata for application before freeing the application. */
3268 (void) push_metadata(registry
, ua_sess
->consumer
);
3271 * Don't ask to close metadata for global per UID buffers. Close
3272 * metadata only on destroy trace session in this case. Also, the
3273 * previous push metadata could have flag the metadata registry to
3274 * close so don't send a close command if closed.
3276 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3277 /* And ask to close it for this session registry. */
3278 (void) close_metadata(registry
, ua_sess
->consumer
);
3281 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3283 pthread_mutex_unlock(&ua_sess
->lock
);
3286 /* Remove application from PID hash table */
3287 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3291 * Remove application from notify hash table. The thread handling the
3292 * notify socket could have deleted the node so ignore on error because
3293 * either way it's valid. The close of that socket is handled by the other
3296 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3297 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3300 * Ignore return value since the node might have been removed before by an
3301 * add replace during app registration because the PID can be reassigned by
3304 iter
.iter
.node
= <a
->pid_n
.node
;
3305 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3307 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3312 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3319 * Fill events array with all events name of all registered apps.
3321 int ust_app_list_events(struct lttng_event
**events
)
3324 size_t nbmem
, count
= 0;
3325 struct lttng_ht_iter iter
;
3326 struct ust_app
*app
;
3327 struct lttng_event
*tmp_event
;
3329 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3330 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3331 if (tmp_event
== NULL
) {
3332 PERROR("zmalloc ust app events");
3339 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3340 struct lttng_ust_tracepoint_iter uiter
;
3342 health_code_update();
3344 if (!app
->compatible
) {
3346 * TODO: In time, we should notice the caller of this error by
3347 * telling him that this is a version error.
3351 handle
= ustctl_tracepoint_list(app
->sock
);
3353 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3354 ERR("UST app list events getting handle failed for app pid %d",
3360 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3361 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3362 /* Handle ustctl error. */
3364 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3365 ERR("UST app tp list get failed for app %d with ret %d",
3368 DBG3("UST app tp list get failed. Application is dead");
3370 * This is normal behavior, an application can die during the
3371 * creation process. Don't report an error so the execution can
3372 * continue normally. Continue normal execution.
3380 health_code_update();
3381 if (count
>= nbmem
) {
3382 /* In case the realloc fails, we free the memory */
3383 struct lttng_event
*new_tmp_event
;
3386 new_nbmem
= nbmem
<< 1;
3387 DBG2("Reallocating event list from %zu to %zu entries",
3389 new_tmp_event
= realloc(tmp_event
,
3390 new_nbmem
* sizeof(struct lttng_event
));
3391 if (new_tmp_event
== NULL
) {
3392 PERROR("realloc ust app events");
3397 /* Zero the new memory */
3398 memset(new_tmp_event
+ nbmem
, 0,
3399 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3401 tmp_event
= new_tmp_event
;
3403 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3404 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3405 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3406 tmp_event
[count
].pid
= app
->pid
;
3407 tmp_event
[count
].enabled
= -1;
3413 *events
= tmp_event
;
3415 DBG2("UST app list events done (%zu events)", count
);
3420 health_code_update();
3425 * Fill events array with all events name of all registered apps.
3427 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3430 size_t nbmem
, count
= 0;
3431 struct lttng_ht_iter iter
;
3432 struct ust_app
*app
;
3433 struct lttng_event_field
*tmp_event
;
3435 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3436 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3437 if (tmp_event
== NULL
) {
3438 PERROR("zmalloc ust app event fields");
3445 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3446 struct lttng_ust_field_iter uiter
;
3448 health_code_update();
3450 if (!app
->compatible
) {
3452 * TODO: In time, we should notice the caller of this error by
3453 * telling him that this is a version error.
3457 handle
= ustctl_tracepoint_field_list(app
->sock
);
3459 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3460 ERR("UST app list field getting handle failed for app pid %d",
3466 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3467 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3468 /* Handle ustctl error. */
3470 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3471 ERR("UST app tp list field failed for app %d with ret %d",
3474 DBG3("UST app tp list field failed. Application is dead");
3476 * This is normal behavior, an application can die during the
3477 * creation process. Don't report an error so the execution can
3478 * continue normally. Reset list and count for next app.
3486 health_code_update();
3487 if (count
>= nbmem
) {
3488 /* In case the realloc fails, we free the memory */
3489 struct lttng_event_field
*new_tmp_event
;
3492 new_nbmem
= nbmem
<< 1;
3493 DBG2("Reallocating event field list from %zu to %zu entries",
3495 new_tmp_event
= realloc(tmp_event
,
3496 new_nbmem
* sizeof(struct lttng_event_field
));
3497 if (new_tmp_event
== NULL
) {
3498 PERROR("realloc ust app event fields");
3503 /* Zero the new memory */
3504 memset(new_tmp_event
+ nbmem
, 0,
3505 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3507 tmp_event
= new_tmp_event
;
3510 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3511 /* Mapping between these enums matches 1 to 1. */
3512 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3513 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3515 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3516 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3517 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3518 tmp_event
[count
].event
.pid
= app
->pid
;
3519 tmp_event
[count
].event
.enabled
= -1;
3525 *fields
= tmp_event
;
3527 DBG2("UST app list event fields done (%zu events)", count
);
3532 health_code_update();
3537 * Free and clean all traceable apps of the global list.
3539 * Should _NOT_ be called with RCU read-side lock held.
3541 void ust_app_clean_list(void)
3544 struct ust_app
*app
;
3545 struct lttng_ht_iter iter
;
3547 DBG2("UST app cleaning registered apps hash table");
3552 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3553 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3555 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3559 /* Cleanup socket hash table */
3560 if (ust_app_ht_by_sock
) {
3561 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3563 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3568 /* Cleanup notify socket hash table */
3569 if (ust_app_ht_by_notify_sock
) {
3570 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3571 notify_sock_n
.node
) {
3572 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3578 /* Destroy is done only when the ht is empty */
3580 ht_cleanup_push(ust_app_ht
);
3582 if (ust_app_ht_by_sock
) {
3583 ht_cleanup_push(ust_app_ht_by_sock
);
3585 if (ust_app_ht_by_notify_sock
) {
3586 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3591 * Init UST app hash table.
3593 int ust_app_ht_alloc(void)
3595 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3599 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3600 if (!ust_app_ht_by_sock
) {
3603 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3604 if (!ust_app_ht_by_notify_sock
) {
3611 * For a specific UST session, disable the channel for all registered apps.
3613 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3614 struct ltt_ust_channel
*uchan
)
3617 struct lttng_ht_iter iter
;
3618 struct lttng_ht_node_str
*ua_chan_node
;
3619 struct ust_app
*app
;
3620 struct ust_app_session
*ua_sess
;
3621 struct ust_app_channel
*ua_chan
;
3623 if (usess
== NULL
|| uchan
== NULL
) {
3624 ERR("Disabling UST global channel with NULL values");
3629 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3630 uchan
->name
, usess
->id
);
3634 /* For every registered applications */
3635 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3636 struct lttng_ht_iter uiter
;
3637 if (!app
->compatible
) {
3639 * TODO: In time, we should notice the caller of this error by
3640 * telling him that this is a version error.
3644 ua_sess
= lookup_session_by_app(usess
, app
);
3645 if (ua_sess
== NULL
) {
3650 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3651 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3652 /* If the session if found for the app, the channel must be there */
3653 assert(ua_chan_node
);
3655 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3656 /* The channel must not be already disabled */
3657 assert(ua_chan
->enabled
== 1);
3659 /* Disable channel onto application */
3660 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3662 /* XXX: We might want to report this error at some point... */
3674 * For a specific UST session, enable the channel for all registered apps.
3676 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3677 struct ltt_ust_channel
*uchan
)
3680 struct lttng_ht_iter iter
;
3681 struct ust_app
*app
;
3682 struct ust_app_session
*ua_sess
;
3684 if (usess
== NULL
|| uchan
== NULL
) {
3685 ERR("Adding UST global channel to NULL values");
3690 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3691 uchan
->name
, usess
->id
);
3695 /* For every registered applications */
3696 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3697 if (!app
->compatible
) {
3699 * TODO: In time, we should notice the caller of this error by
3700 * telling him that this is a version error.
3704 ua_sess
= lookup_session_by_app(usess
, app
);
3705 if (ua_sess
== NULL
) {
3709 /* Enable channel onto application */
3710 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3712 /* XXX: We might want to report this error at some point... */
3724 * Disable an event in a channel and for a specific session.
3726 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3727 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3730 struct lttng_ht_iter iter
, uiter
;
3731 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3732 struct ust_app
*app
;
3733 struct ust_app_session
*ua_sess
;
3734 struct ust_app_channel
*ua_chan
;
3735 struct ust_app_event
*ua_event
;
3737 DBG("UST app disabling event %s for all apps in channel "
3738 "%s for session id %" PRIu64
,
3739 uevent
->attr
.name
, uchan
->name
, usess
->id
);
3743 /* For all registered applications */
3744 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3745 if (!app
->compatible
) {
3747 * TODO: In time, we should notice the caller of this error by
3748 * telling him that this is a version error.
3752 ua_sess
= lookup_session_by_app(usess
, app
);
3753 if (ua_sess
== NULL
) {
3758 /* Lookup channel in the ust app session */
3759 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3760 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3761 if (ua_chan_node
== NULL
) {
3762 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
3763 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
3766 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3768 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
3769 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
3770 if (ua_event_node
== NULL
) {
3771 DBG2("Event %s not found in channel %s for app pid %d."
3772 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
3775 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3777 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3779 /* XXX: Report error someday... */
3790 * For a specific UST session, create the channel for all registered apps.
3792 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
3793 struct ltt_ust_channel
*uchan
)
3795 int ret
= 0, created
;
3796 struct lttng_ht_iter iter
;
3797 struct ust_app
*app
;
3798 struct ust_app_session
*ua_sess
= NULL
;
3800 /* Very wrong code flow */
3804 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
3805 uchan
->name
, usess
->id
);
3809 /* For every registered applications */
3810 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3811 if (!app
->compatible
) {
3813 * TODO: In time, we should notice the caller of this error by
3814 * telling him that this is a version error.
3818 if (!trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
3824 * Create session on the tracer side and add it to app session HT. Note
3825 * that if session exist, it will simply return a pointer to the ust
3828 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
3833 * The application's socket is not valid. Either a bad socket
3834 * or a timeout on it. We can't inform the caller that for a
3835 * specific app, the session failed so lets continue here.
3837 ret
= 0; /* Not an error. */
3841 goto error_rcu_unlock
;
3846 pthread_mutex_lock(&ua_sess
->lock
);
3848 if (ua_sess
->deleted
) {
3849 pthread_mutex_unlock(&ua_sess
->lock
);
3853 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
3854 sizeof(uchan
->name
))) {
3855 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
3858 /* Create channel onto application. We don't need the chan ref. */
3859 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
3860 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
3862 pthread_mutex_unlock(&ua_sess
->lock
);
3864 /* Cleanup the created session if it's the case. */
3866 destroy_app_session(app
, ua_sess
);
3871 * The application's socket is not valid. Either a bad socket
3872 * or a timeout on it. We can't inform the caller that for a
3873 * specific app, the session failed so lets continue here.
3875 ret
= 0; /* Not an error. */
3879 goto error_rcu_unlock
;
3890 * Enable event for a specific session and channel on the tracer.
3892 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
3893 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3896 struct lttng_ht_iter iter
, uiter
;
3897 struct lttng_ht_node_str
*ua_chan_node
;
3898 struct ust_app
*app
;
3899 struct ust_app_session
*ua_sess
;
3900 struct ust_app_channel
*ua_chan
;
3901 struct ust_app_event
*ua_event
;
3903 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
3904 uevent
->attr
.name
, usess
->id
);
3907 * NOTE: At this point, this function is called only if the session and
3908 * channel passed are already created for all apps. and enabled on the
3914 /* For all registered applications */
3915 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3916 if (!app
->compatible
) {
3918 * TODO: In time, we should notice the caller of this error by
3919 * telling him that this is a version error.
3923 ua_sess
= lookup_session_by_app(usess
, app
);
3925 /* The application has problem or is probably dead. */
3929 pthread_mutex_lock(&ua_sess
->lock
);
3931 if (ua_sess
->deleted
) {
3932 pthread_mutex_unlock(&ua_sess
->lock
);
3936 /* Lookup channel in the ust app session */
3937 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3938 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3940 * It is possible that the channel cannot be found is
3941 * the channel/event creation occurs concurrently with
3942 * an application exit.
3944 if (!ua_chan_node
) {
3945 pthread_mutex_unlock(&ua_sess
->lock
);
3949 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3951 /* Get event node */
3952 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3953 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3954 if (ua_event
== NULL
) {
3955 DBG3("UST app enable event %s not found for app PID %d."
3956 "Skipping app", uevent
->attr
.name
, app
->pid
);
3960 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
3962 pthread_mutex_unlock(&ua_sess
->lock
);
3966 pthread_mutex_unlock(&ua_sess
->lock
);
3975 * For a specific existing UST session and UST channel, creates the event for
3976 * all registered apps.
3978 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
3979 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3982 struct lttng_ht_iter iter
, uiter
;
3983 struct lttng_ht_node_str
*ua_chan_node
;
3984 struct ust_app
*app
;
3985 struct ust_app_session
*ua_sess
;
3986 struct ust_app_channel
*ua_chan
;
3988 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
3989 uevent
->attr
.name
, usess
->id
);
3993 /* For all registered applications */
3994 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3995 if (!app
->compatible
) {
3997 * TODO: In time, we should notice the caller of this error by
3998 * telling him that this is a version error.
4002 ua_sess
= lookup_session_by_app(usess
, app
);
4004 /* The application has problem or is probably dead. */
4008 pthread_mutex_lock(&ua_sess
->lock
);
4010 if (ua_sess
->deleted
) {
4011 pthread_mutex_unlock(&ua_sess
->lock
);
4015 /* Lookup channel in the ust app session */
4016 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4017 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4018 /* If the channel is not found, there is a code flow error */
4019 assert(ua_chan_node
);
4021 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4023 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4024 pthread_mutex_unlock(&ua_sess
->lock
);
4026 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4027 /* Possible value at this point: -ENOMEM. If so, we stop! */
4030 DBG2("UST app event %s already exist on app PID %d",
4031 uevent
->attr
.name
, app
->pid
);
4042 * Start tracing for a specific UST session and app.
4045 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4048 struct ust_app_session
*ua_sess
;
4050 DBG("Starting tracing for ust app pid %d", app
->pid
);
4054 if (!app
->compatible
) {
4058 ua_sess
= lookup_session_by_app(usess
, app
);
4059 if (ua_sess
== NULL
) {
4060 /* The session is in teardown process. Ignore and continue. */
4064 pthread_mutex_lock(&ua_sess
->lock
);
4066 if (ua_sess
->deleted
) {
4067 pthread_mutex_unlock(&ua_sess
->lock
);
4071 /* Upon restart, we skip the setup, already done */
4072 if (ua_sess
->started
) {
4076 /* Create directories if consumer is LOCAL and has a path defined. */
4077 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
4078 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
4079 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
4080 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
4082 if (ret
!= -EEXIST
) {
4083 ERR("Trace directory creation error");
4090 * Create the metadata for the application. This returns gracefully if a
4091 * metadata was already set for the session.
4093 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4098 health_code_update();
4101 /* This start the UST tracing */
4102 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4104 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4105 ERR("Error starting tracing for app pid: %d (ret: %d)",
4108 DBG("UST app start session failed. Application is dead.");
4110 * This is normal behavior, an application can die during the
4111 * creation process. Don't report an error so the execution can
4112 * continue normally.
4114 pthread_mutex_unlock(&ua_sess
->lock
);
4120 /* Indicate that the session has been started once */
4121 ua_sess
->started
= 1;
4123 pthread_mutex_unlock(&ua_sess
->lock
);
4125 health_code_update();
4127 /* Quiescent wait after starting trace */
4128 ret
= ustctl_wait_quiescent(app
->sock
);
4129 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4130 ERR("UST app wait quiescent failed for app pid %d ret %d",
4136 health_code_update();
4140 pthread_mutex_unlock(&ua_sess
->lock
);
4142 health_code_update();
4147 * Stop tracing for a specific UST session and app.
4150 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4153 struct ust_app_session
*ua_sess
;
4154 struct ust_registry_session
*registry
;
4156 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4160 if (!app
->compatible
) {
4161 goto end_no_session
;
4164 ua_sess
= lookup_session_by_app(usess
, app
);
4165 if (ua_sess
== NULL
) {
4166 goto end_no_session
;
4169 pthread_mutex_lock(&ua_sess
->lock
);
4171 if (ua_sess
->deleted
) {
4172 pthread_mutex_unlock(&ua_sess
->lock
);
4173 goto end_no_session
;
4177 * If started = 0, it means that stop trace has been called for a session
4178 * that was never started. It's possible since we can have a fail start
4179 * from either the application manager thread or the command thread. Simply
4180 * indicate that this is a stop error.
4182 if (!ua_sess
->started
) {
4183 goto error_rcu_unlock
;
4186 health_code_update();
4188 /* This inhibits UST tracing */
4189 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4191 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4192 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4195 DBG("UST app stop session failed. Application is dead.");
4197 * This is normal behavior, an application can die during the
4198 * creation process. Don't report an error so the execution can
4199 * continue normally.
4203 goto error_rcu_unlock
;
4206 health_code_update();
4208 /* Quiescent wait after stopping trace */
4209 ret
= ustctl_wait_quiescent(app
->sock
);
4210 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4211 ERR("UST app wait quiescent failed for app pid %d ret %d",
4215 health_code_update();
4217 registry
= get_session_registry(ua_sess
);
4220 /* Push metadata for application before freeing the application. */
4221 (void) push_metadata(registry
, ua_sess
->consumer
);
4224 pthread_mutex_unlock(&ua_sess
->lock
);
4227 health_code_update();
4231 pthread_mutex_unlock(&ua_sess
->lock
);
4233 health_code_update();
4238 int ust_app_flush_app_session(struct ust_app
*app
,
4239 struct ust_app_session
*ua_sess
)
4241 int ret
, retval
= 0;
4242 struct lttng_ht_iter iter
;
4243 struct ust_app_channel
*ua_chan
;
4244 struct consumer_socket
*socket
;
4246 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4250 if (!app
->compatible
) {
4251 goto end_not_compatible
;
4254 pthread_mutex_lock(&ua_sess
->lock
);
4256 if (ua_sess
->deleted
) {
4260 health_code_update();
4262 /* Flushing buffers */
4263 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4266 /* Flush buffers and push metadata. */
4267 switch (ua_sess
->buffer_type
) {
4268 case LTTNG_BUFFER_PER_PID
:
4269 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4271 health_code_update();
4272 assert(ua_chan
->is_sent
);
4273 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4275 ERR("Error flushing consumer channel");
4281 case LTTNG_BUFFER_PER_UID
:
4287 health_code_update();
4290 pthread_mutex_unlock(&ua_sess
->lock
);
4294 health_code_update();
4299 * Flush buffers for all applications for a specific UST session.
4300 * Called with UST session lock held.
4303 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4308 DBG("Flushing session buffers for all ust apps");
4312 /* Flush buffers and push metadata. */
4313 switch (usess
->buffer_type
) {
4314 case LTTNG_BUFFER_PER_UID
:
4316 struct buffer_reg_uid
*reg
;
4317 struct lttng_ht_iter iter
;
4319 /* Flush all per UID buffers associated to that session. */
4320 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4321 struct ust_registry_session
*ust_session_reg
;
4322 struct buffer_reg_channel
*reg_chan
;
4323 struct consumer_socket
*socket
;
4325 /* Get consumer socket to use to push the metadata.*/
4326 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4329 /* Ignore request if no consumer is found for the session. */
4333 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4334 reg_chan
, node
.node
) {
4336 * The following call will print error values so the return
4337 * code is of little importance because whatever happens, we
4338 * have to try them all.
4340 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4343 ust_session_reg
= reg
->registry
->reg
.ust
;
4344 /* Push metadata. */
4345 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4349 case LTTNG_BUFFER_PER_PID
:
4351 struct ust_app_session
*ua_sess
;
4352 struct lttng_ht_iter iter
;
4353 struct ust_app
*app
;
4355 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4356 ua_sess
= lookup_session_by_app(usess
, app
);
4357 if (ua_sess
== NULL
) {
4360 (void) ust_app_flush_app_session(app
, ua_sess
);
4371 health_code_update();
4376 * Destroy a specific UST session in apps.
4378 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4381 struct ust_app_session
*ua_sess
;
4382 struct lttng_ht_iter iter
;
4383 struct lttng_ht_node_u64
*node
;
4385 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4389 if (!app
->compatible
) {
4393 __lookup_session_by_app(usess
, app
, &iter
);
4394 node
= lttng_ht_iter_get_node_u64(&iter
);
4396 /* Session is being or is deleted. */
4399 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4401 health_code_update();
4402 destroy_app_session(app
, ua_sess
);
4404 health_code_update();
4406 /* Quiescent wait after stopping trace */
4407 ret
= ustctl_wait_quiescent(app
->sock
);
4408 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4409 ERR("UST app wait quiescent failed for app pid %d ret %d",
4414 health_code_update();
4419 * Start tracing for the UST session.
4421 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4424 struct lttng_ht_iter iter
;
4425 struct ust_app
*app
;
4427 DBG("Starting all UST traces");
4431 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4432 ret
= ust_app_start_trace(usess
, app
);
4434 /* Continue to next apps even on error */
4445 * Start tracing for the UST session.
4446 * Called with UST session lock held.
4448 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4451 struct lttng_ht_iter iter
;
4452 struct ust_app
*app
;
4454 DBG("Stopping all UST traces");
4458 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4459 ret
= ust_app_stop_trace(usess
, app
);
4461 /* Continue to next apps even on error */
4466 (void) ust_app_flush_session(usess
);
4474 * Destroy app UST session.
4476 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4479 struct lttng_ht_iter iter
;
4480 struct ust_app
*app
;
4482 DBG("Destroy all UST traces");
4486 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4487 ret
= destroy_trace(usess
, app
);
4489 /* Continue to next apps even on error */
4500 void ust_app_global_create(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4503 struct lttng_ht_iter iter
, uiter
;
4504 struct ust_app_session
*ua_sess
= NULL
;
4505 struct ust_app_channel
*ua_chan
;
4506 struct ust_app_event
*ua_event
;
4507 struct ust_app_ctx
*ua_ctx
;
4510 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &is_created
);
4512 /* Tracer is probably gone or ENOMEM. */
4516 /* App session already created. */
4521 pthread_mutex_lock(&ua_sess
->lock
);
4523 if (ua_sess
->deleted
) {
4524 pthread_mutex_unlock(&ua_sess
->lock
);
4529 * We can iterate safely here over all UST app session since the create ust
4530 * app session above made a shadow copy of the UST global domain from the
4533 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4535 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4536 if (ret
< 0 && ret
!= -ENOTCONN
) {
4538 * Stop everything. On error, the application
4539 * failed, no more file descriptor are available
4540 * or ENOMEM so stopping here is the only thing
4541 * we can do for now. The only exception is
4542 * -ENOTCONN, which indicates that the application
4549 * Add context using the list so they are enabled in the same order the
4552 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
4553 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
4560 /* For each events */
4561 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
4563 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
4570 pthread_mutex_unlock(&ua_sess
->lock
);
4572 if (usess
->active
) {
4573 ret
= ust_app_start_trace(usess
, app
);
4578 DBG2("UST trace started for app pid %d", app
->pid
);
4581 /* Everything went well at this point. */
4585 pthread_mutex_unlock(&ua_sess
->lock
);
4588 destroy_app_session(app
, ua_sess
);
4594 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4596 struct ust_app_session
*ua_sess
;
4598 ua_sess
= lookup_session_by_app(usess
, app
);
4599 if (ua_sess
== NULL
) {
4602 destroy_app_session(app
, ua_sess
);
4606 * Add channels/events from UST global domain to registered apps at sock.
4608 * Called with session lock held.
4609 * Called with RCU read-side lock held.
4611 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4615 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
4616 app
->sock
, usess
->id
);
4618 if (!app
->compatible
) {
4622 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4623 ust_app_global_create(usess
, app
);
4625 ust_app_global_destroy(usess
, app
);
4630 * Called with session lock held.
4632 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
4634 struct lttng_ht_iter iter
;
4635 struct ust_app
*app
;
4638 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4639 ust_app_global_update(usess
, app
);
4645 * Add context to a specific channel for global UST domain.
4647 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
4648 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
4651 struct lttng_ht_node_str
*ua_chan_node
;
4652 struct lttng_ht_iter iter
, uiter
;
4653 struct ust_app_channel
*ua_chan
= NULL
;
4654 struct ust_app_session
*ua_sess
;
4655 struct ust_app
*app
;
4659 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4660 if (!app
->compatible
) {
4662 * TODO: In time, we should notice the caller of this error by
4663 * telling him that this is a version error.
4667 ua_sess
= lookup_session_by_app(usess
, app
);
4668 if (ua_sess
== NULL
) {
4672 pthread_mutex_lock(&ua_sess
->lock
);
4674 if (ua_sess
->deleted
) {
4675 pthread_mutex_unlock(&ua_sess
->lock
);
4679 /* Lookup channel in the ust app session */
4680 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4681 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4682 if (ua_chan_node
== NULL
) {
4685 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4687 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4692 pthread_mutex_unlock(&ua_sess
->lock
);
4700 * Enable event for a channel from a UST session for a specific PID.
4702 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4703 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4706 struct lttng_ht_iter iter
;
4707 struct lttng_ht_node_str
*ua_chan_node
;
4708 struct ust_app
*app
;
4709 struct ust_app_session
*ua_sess
;
4710 struct ust_app_channel
*ua_chan
;
4711 struct ust_app_event
*ua_event
;
4713 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4717 app
= ust_app_find_by_pid(pid
);
4719 ERR("UST app enable event per PID %d not found", pid
);
4724 if (!app
->compatible
) {
4729 ua_sess
= lookup_session_by_app(usess
, app
);
4731 /* The application has problem or is probably dead. */
4736 pthread_mutex_lock(&ua_sess
->lock
);
4738 if (ua_sess
->deleted
) {
4743 /* Lookup channel in the ust app session */
4744 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4745 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4746 /* If the channel is not found, there is a code flow error */
4747 assert(ua_chan_node
);
4749 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4751 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4752 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4753 if (ua_event
== NULL
) {
4754 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4759 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4766 pthread_mutex_unlock(&ua_sess
->lock
);
4773 * Calibrate registered applications.
4775 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
4778 struct lttng_ht_iter iter
;
4779 struct ust_app
*app
;
4783 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4784 if (!app
->compatible
) {
4786 * TODO: In time, we should notice the caller of this error by
4787 * telling him that this is a version error.
4792 health_code_update();
4794 ret
= ustctl_calibrate(app
->sock
, calibrate
);
4798 /* Means that it's not implemented on the tracer side. */
4802 DBG2("Calibrate app PID %d returned with error %d",
4809 DBG("UST app global domain calibration finished");
4813 health_code_update();
4819 * Receive registration and populate the given msg structure.
4821 * On success return 0 else a negative value returned by the ustctl call.
4823 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
4826 uint32_t pid
, ppid
, uid
, gid
;
4830 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
4831 &pid
, &ppid
, &uid
, &gid
,
4832 &msg
->bits_per_long
,
4833 &msg
->uint8_t_alignment
,
4834 &msg
->uint16_t_alignment
,
4835 &msg
->uint32_t_alignment
,
4836 &msg
->uint64_t_alignment
,
4837 &msg
->long_alignment
,
4844 case LTTNG_UST_ERR_EXITING
:
4845 DBG3("UST app recv reg message failed. Application died");
4847 case LTTNG_UST_ERR_UNSUP_MAJOR
:
4848 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
4849 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
4850 LTTNG_UST_ABI_MINOR_VERSION
);
4853 ERR("UST app recv reg message failed with ret %d", ret
);
4858 msg
->pid
= (pid_t
) pid
;
4859 msg
->ppid
= (pid_t
) ppid
;
4860 msg
->uid
= (uid_t
) uid
;
4861 msg
->gid
= (gid_t
) gid
;
4868 * Return a ust app channel object using the application object and the channel
4869 * object descriptor has a key. If not found, NULL is returned. A RCU read side
4870 * lock MUST be acquired before calling this function.
4872 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
4875 struct lttng_ht_node_ulong
*node
;
4876 struct lttng_ht_iter iter
;
4877 struct ust_app_channel
*ua_chan
= NULL
;
4881 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
4882 node
= lttng_ht_iter_get_node_ulong(&iter
);
4884 DBG2("UST app channel find by objd %d not found", objd
);
4888 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
4895 * Reply to a register channel notification from an application on the notify
4896 * socket. The channel metadata is also created.
4898 * The session UST registry lock is acquired in this function.
4900 * On success 0 is returned else a negative value.
4902 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
4903 size_t nr_fields
, struct ustctl_field
*fields
)
4905 int ret
, ret_code
= 0;
4906 uint32_t chan_id
, reg_count
;
4907 uint64_t chan_reg_key
;
4908 enum ustctl_channel_header type
;
4909 struct ust_app
*app
;
4910 struct ust_app_channel
*ua_chan
;
4911 struct ust_app_session
*ua_sess
;
4912 struct ust_registry_session
*registry
;
4913 struct ust_registry_channel
*chan_reg
;
4917 /* Lookup application. If not found, there is a code flow error. */
4918 app
= find_app_by_notify_sock(sock
);
4920 DBG("Application socket %d is being teardown. Abort event notify",
4924 goto error_rcu_unlock
;
4927 /* Lookup channel by UST object descriptor. */
4928 ua_chan
= find_channel_by_objd(app
, cobjd
);
4930 DBG("Application channel is being teardown. Abort event notify");
4933 goto error_rcu_unlock
;
4936 assert(ua_chan
->session
);
4937 ua_sess
= ua_chan
->session
;
4939 /* Get right session registry depending on the session buffer type. */
4940 registry
= get_session_registry(ua_sess
);
4943 /* Depending on the buffer type, a different channel key is used. */
4944 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4945 chan_reg_key
= ua_chan
->tracing_channel_id
;
4947 chan_reg_key
= ua_chan
->key
;
4950 pthread_mutex_lock(®istry
->lock
);
4952 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
4955 if (!chan_reg
->register_done
) {
4956 reg_count
= ust_registry_get_event_count(chan_reg
);
4957 if (reg_count
< 31) {
4958 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
4960 type
= USTCTL_CHANNEL_HEADER_LARGE
;
4963 chan_reg
->nr_ctx_fields
= nr_fields
;
4964 chan_reg
->ctx_fields
= fields
;
4965 chan_reg
->header_type
= type
;
4967 /* Get current already assigned values. */
4968 type
= chan_reg
->header_type
;
4970 /* Set to NULL so the error path does not do a double free. */
4973 /* Channel id is set during the object creation. */
4974 chan_id
= chan_reg
->chan_id
;
4976 /* Append to metadata */
4977 if (!chan_reg
->metadata_dumped
) {
4978 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
4980 ERR("Error appending channel metadata (errno = %d)", ret_code
);
4986 DBG3("UST app replying to register channel key %" PRIu64
4987 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
4990 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
4992 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4993 ERR("UST app reply channel failed with ret %d", ret
);
4995 DBG3("UST app reply channel failed. Application died");
5000 /* This channel registry registration is completed. */
5001 chan_reg
->register_done
= 1;
5004 pthread_mutex_unlock(®istry
->lock
);
5014 * Add event to the UST channel registry. When the event is added to the
5015 * registry, the metadata is also created. Once done, this replies to the
5016 * application with the appropriate error code.
5018 * The session UST registry lock is acquired in the function.
5020 * On success 0 is returned else a negative value.
5022 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5023 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
5024 char *model_emf_uri
)
5027 uint32_t event_id
= 0;
5028 uint64_t chan_reg_key
;
5029 struct ust_app
*app
;
5030 struct ust_app_channel
*ua_chan
;
5031 struct ust_app_session
*ua_sess
;
5032 struct ust_registry_session
*registry
;
5036 /* Lookup application. If not found, there is a code flow error. */
5037 app
= find_app_by_notify_sock(sock
);
5039 DBG("Application socket %d is being teardown. Abort event notify",
5044 free(model_emf_uri
);
5045 goto error_rcu_unlock
;
5048 /* Lookup channel by UST object descriptor. */
5049 ua_chan
= find_channel_by_objd(app
, cobjd
);
5051 DBG("Application channel is being teardown. Abort event notify");
5055 free(model_emf_uri
);
5056 goto error_rcu_unlock
;
5059 assert(ua_chan
->session
);
5060 ua_sess
= ua_chan
->session
;
5062 registry
= get_session_registry(ua_sess
);
5065 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5066 chan_reg_key
= ua_chan
->tracing_channel_id
;
5068 chan_reg_key
= ua_chan
->key
;
5071 pthread_mutex_lock(®istry
->lock
);
5074 * From this point on, this call acquires the ownership of the sig, fields
5075 * and model_emf_uri meaning any free are done inside it if needed. These
5076 * three variables MUST NOT be read/write after this.
5078 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5079 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
, loglevel
,
5080 model_emf_uri
, ua_sess
->buffer_type
, &event_id
,
5084 * The return value is returned to ustctl so in case of an error, the
5085 * application can be notified. In case of an error, it's important not to
5086 * return a negative error or else the application will get closed.
5088 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5090 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5091 ERR("UST app reply event failed with ret %d", ret
);
5093 DBG3("UST app reply event failed. Application died");
5096 * No need to wipe the create event since the application socket will
5097 * get close on error hence cleaning up everything by itself.
5102 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5106 pthread_mutex_unlock(®istry
->lock
);
5113 * Handle application notification through the given notify socket.
5115 * Return 0 on success or else a negative value.
5117 int ust_app_recv_notify(int sock
)
5120 enum ustctl_notify_cmd cmd
;
5122 DBG3("UST app receiving notify from sock %d", sock
);
5124 ret
= ustctl_recv_notify(sock
, &cmd
);
5126 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5127 ERR("UST app recv notify failed with ret %d", ret
);
5129 DBG3("UST app recv notify failed. Application died");
5135 case USTCTL_NOTIFY_CMD_EVENT
:
5137 int sobjd
, cobjd
, loglevel
;
5138 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5140 struct ustctl_field
*fields
;
5142 DBG2("UST app ustctl register event received");
5144 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
5145 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
5147 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5148 ERR("UST app recv event failed with ret %d", ret
);
5150 DBG3("UST app recv event failed. Application died");
5156 * Add event to the UST registry coming from the notify socket. This
5157 * call will free if needed the sig, fields and model_emf_uri. This
5158 * code path loses the ownsership of these variables and transfer them
5159 * to the this function.
5161 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5162 fields
, loglevel
, model_emf_uri
);
5169 case USTCTL_NOTIFY_CMD_CHANNEL
:
5173 struct ustctl_field
*fields
;
5175 DBG2("UST app ustctl register channel received");
5177 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5180 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5181 ERR("UST app recv channel failed with ret %d", ret
);
5183 DBG3("UST app recv channel failed. Application died");
5189 * The fields ownership are transfered to this function call meaning
5190 * that if needed it will be freed. After this, it's invalid to access
5191 * fields or clean it up.
5193 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
5202 /* Should NEVER happen. */
5211 * Once the notify socket hangs up, this is called. First, it tries to find the
5212 * corresponding application. On failure, the call_rcu to close the socket is
5213 * executed. If an application is found, it tries to delete it from the notify
5214 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5216 * Note that an object needs to be allocated here so on ENOMEM failure, the
5217 * call RCU is not done but the rest of the cleanup is.
5219 void ust_app_notify_sock_unregister(int sock
)
5222 struct lttng_ht_iter iter
;
5223 struct ust_app
*app
;
5224 struct ust_app_notify_sock_obj
*obj
;
5230 obj
= zmalloc(sizeof(*obj
));
5233 * An ENOMEM is kind of uncool. If this strikes we continue the
5234 * procedure but the call_rcu will not be called. In this case, we
5235 * accept the fd leak rather than possibly creating an unsynchronized
5236 * state between threads.
5238 * TODO: The notify object should be created once the notify socket is
5239 * registered and stored independantely from the ust app object. The
5240 * tricky part is to synchronize the teardown of the application and
5241 * this notify object. Let's keep that in mind so we can avoid this
5242 * kind of shenanigans with ENOMEM in the teardown path.
5249 DBG("UST app notify socket unregister %d", sock
);
5252 * Lookup application by notify socket. If this fails, this means that the
5253 * hash table delete has already been done by the application
5254 * unregistration process so we can safely close the notify socket in a
5257 app
= find_app_by_notify_sock(sock
);
5262 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5265 * Whatever happens here either we fail or succeed, in both cases we have
5266 * to close the socket after a grace period to continue to the call RCU
5267 * here. If the deletion is successful, the application is not visible
5268 * anymore by other threads and is it fails it means that it was already
5269 * deleted from the hash table so either way we just have to close the
5272 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5278 * Close socket after a grace period to avoid for the socket to be reused
5279 * before the application object is freed creating potential race between
5280 * threads trying to add unique in the global hash table.
5283 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5288 * Destroy a ust app data structure and free its memory.
5290 void ust_app_destroy(struct ust_app
*app
)
5296 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5300 * Take a snapshot for a given UST session. The snapshot is sent to the given
5303 * Return 0 on success or else a negative value.
5305 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5306 struct snapshot_output
*output
, int wait
,
5307 uint64_t nb_packets_per_stream
)
5310 unsigned int snapshot_done
= 0;
5311 struct lttng_ht_iter iter
;
5312 struct ust_app
*app
;
5313 char pathname
[PATH_MAX
];
5320 switch (usess
->buffer_type
) {
5321 case LTTNG_BUFFER_PER_UID
:
5323 struct buffer_reg_uid
*reg
;
5325 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5326 struct buffer_reg_channel
*reg_chan
;
5327 struct consumer_socket
*socket
;
5329 /* Get consumer socket to use to push the metadata.*/
5330 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5337 memset(pathname
, 0, sizeof(pathname
));
5338 ret
= snprintf(pathname
, sizeof(pathname
),
5339 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5340 reg
->uid
, reg
->bits_per_long
);
5342 PERROR("snprintf snapshot path");
5346 /* Add the UST default trace dir to path. */
5347 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5348 reg_chan
, node
.node
) {
5349 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5350 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5351 nb_packets_per_stream
);
5356 ret
= consumer_snapshot_channel(socket
,
5357 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5358 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
5366 case LTTNG_BUFFER_PER_PID
:
5368 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5369 struct consumer_socket
*socket
;
5370 struct lttng_ht_iter chan_iter
;
5371 struct ust_app_channel
*ua_chan
;
5372 struct ust_app_session
*ua_sess
;
5373 struct ust_registry_session
*registry
;
5375 ua_sess
= lookup_session_by_app(usess
, app
);
5377 /* Session not associated with this app. */
5381 /* Get the right consumer socket for the application. */
5382 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5389 /* Add the UST default trace dir to path. */
5390 memset(pathname
, 0, sizeof(pathname
));
5391 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5394 PERROR("snprintf snapshot path");
5398 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5399 ua_chan
, node
.node
) {
5400 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
5401 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
5402 nb_packets_per_stream
);
5408 registry
= get_session_registry(ua_sess
);
5410 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
5411 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
5424 if (!snapshot_done
) {
5426 * If no snapshot was made and we are not in the error path, this means
5427 * that there are no buffers thus no (prior) application to snapshot
5428 * data from so we have simply NO data.
5439 * Return the size taken by one more packet per stream.
5441 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
5442 uint64_t cur_nr_packets
)
5444 uint64_t tot_size
= 0;
5445 struct ust_app
*app
;
5446 struct lttng_ht_iter iter
;
5450 switch (usess
->buffer_type
) {
5451 case LTTNG_BUFFER_PER_UID
:
5453 struct buffer_reg_uid
*reg
;
5455 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5456 struct buffer_reg_channel
*reg_chan
;
5459 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5460 reg_chan
, node
.node
) {
5461 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
5463 * Don't take channel into account if we
5464 * already grab all its packets.
5468 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
5474 case LTTNG_BUFFER_PER_PID
:
5477 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5478 struct ust_app_channel
*ua_chan
;
5479 struct ust_app_session
*ua_sess
;
5480 struct lttng_ht_iter chan_iter
;
5482 ua_sess
= lookup_session_by_app(usess
, app
);
5484 /* Session not associated with this app. */
5488 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5489 ua_chan
, node
.node
) {
5490 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
5492 * Don't take channel into account if we
5493 * already grab all its packets.
5497 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;