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 consumer_output_put(ua_sess
->consumer
);
727 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
731 * Delete a traceable application structure from the global list. Never call
732 * this function outside of a call_rcu call.
734 * RCU read side lock should _NOT_ be held when calling this function.
737 void delete_ust_app(struct ust_app
*app
)
740 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
742 /* Delete ust app sessions info */
747 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
749 /* Free every object in the session and the session. */
751 delete_ust_app_session(sock
, ua_sess
, app
);
755 ht_cleanup_push(app
->sessions
);
756 ht_cleanup_push(app
->ust_objd
);
759 * Wait until we have deleted the application from the sock hash table
760 * before closing this socket, otherwise an application could re-use the
761 * socket ID and race with the teardown, using the same hash table entry.
763 * It's OK to leave the close in call_rcu. We want it to stay unique for
764 * all RCU readers that could run concurrently with unregister app,
765 * therefore we _need_ to only close that socket after a grace period. So
766 * it should stay in this RCU callback.
768 * This close() is a very important step of the synchronization model so
769 * every modification to this function must be carefully reviewed.
775 lttng_fd_put(LTTNG_FD_APPS
, 1);
777 DBG2("UST app pid %d deleted", app
->pid
);
782 * URCU intermediate call to delete an UST app.
785 void delete_ust_app_rcu(struct rcu_head
*head
)
787 struct lttng_ht_node_ulong
*node
=
788 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
789 struct ust_app
*app
=
790 caa_container_of(node
, struct ust_app
, pid_n
);
792 DBG3("Call RCU deleting app PID %d", app
->pid
);
797 * Delete the session from the application ht and delete the data structure by
798 * freeing every object inside and releasing them.
800 static void destroy_app_session(struct ust_app
*app
,
801 struct ust_app_session
*ua_sess
)
804 struct lttng_ht_iter iter
;
809 iter
.iter
.node
= &ua_sess
->node
.node
;
810 ret
= lttng_ht_del(app
->sessions
, &iter
);
812 /* Already scheduled for teardown. */
816 /* Once deleted, free the data structure. */
817 delete_ust_app_session(app
->sock
, ua_sess
, app
);
824 * Alloc new UST app session.
827 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
829 struct ust_app_session
*ua_sess
;
831 /* Init most of the default value by allocating and zeroing */
832 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
833 if (ua_sess
== NULL
) {
838 ua_sess
->handle
= -1;
839 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
840 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
841 pthread_mutex_init(&ua_sess
->lock
, NULL
);
850 * Alloc new UST app channel.
853 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
854 struct ust_app_session
*ua_sess
,
855 struct lttng_ust_channel_attr
*attr
)
857 struct ust_app_channel
*ua_chan
;
859 /* Init most of the default value by allocating and zeroing */
860 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
861 if (ua_chan
== NULL
) {
866 /* Setup channel name */
867 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
868 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
870 ua_chan
->enabled
= 1;
871 ua_chan
->handle
= -1;
872 ua_chan
->session
= ua_sess
;
873 ua_chan
->key
= get_next_channel_key();
874 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
875 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
876 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
878 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
879 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
881 /* Copy attributes */
883 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
884 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
885 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
886 ua_chan
->attr
.overwrite
= attr
->overwrite
;
887 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
888 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
889 ua_chan
->attr
.output
= attr
->output
;
891 /* By default, the channel is a per cpu channel. */
892 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
894 DBG3("UST app channel %s allocated", ua_chan
->name
);
903 * Allocate and initialize a UST app stream.
905 * Return newly allocated stream pointer or NULL on error.
907 struct ust_app_stream
*ust_app_alloc_stream(void)
909 struct ust_app_stream
*stream
= NULL
;
911 stream
= zmalloc(sizeof(*stream
));
912 if (stream
== NULL
) {
913 PERROR("zmalloc ust app stream");
917 /* Zero could be a valid value for a handle so flag it to -1. */
925 * Alloc new UST app event.
928 struct ust_app_event
*alloc_ust_app_event(char *name
,
929 struct lttng_ust_event
*attr
)
931 struct ust_app_event
*ua_event
;
933 /* Init most of the default value by allocating and zeroing */
934 ua_event
= zmalloc(sizeof(struct ust_app_event
));
935 if (ua_event
== NULL
) {
940 ua_event
->enabled
= 1;
941 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
942 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
943 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
945 /* Copy attributes */
947 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
950 DBG3("UST app event %s allocated", ua_event
->name
);
959 * Alloc new UST app context.
962 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
964 struct ust_app_ctx
*ua_ctx
;
966 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
967 if (ua_ctx
== NULL
) {
971 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
974 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
977 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
984 * Allocate a filter and copy the given original filter.
986 * Return allocated filter or NULL on error.
988 static struct lttng_filter_bytecode
*copy_filter_bytecode(
989 struct lttng_filter_bytecode
*orig_f
)
991 struct lttng_filter_bytecode
*filter
= NULL
;
993 /* Copy filter bytecode */
994 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
996 PERROR("zmalloc alloc filter bytecode");
1000 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1007 * Create a liblttng-ust filter bytecode from given bytecode.
1009 * Return allocated filter or NULL on error.
1011 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1012 struct lttng_filter_bytecode
*orig_f
)
1014 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1016 /* Copy filter bytecode */
1017 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1019 PERROR("zmalloc alloc ust filter bytecode");
1023 assert(sizeof(struct lttng_filter_bytecode
) ==
1024 sizeof(struct lttng_ust_filter_bytecode
));
1025 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1031 * Find an ust_app using the sock and return it. RCU read side lock must be
1032 * held before calling this helper function.
1034 struct ust_app
*ust_app_find_by_sock(int sock
)
1036 struct lttng_ht_node_ulong
*node
;
1037 struct lttng_ht_iter iter
;
1039 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1040 node
= lttng_ht_iter_get_node_ulong(&iter
);
1042 DBG2("UST app find by sock %d not found", sock
);
1046 return caa_container_of(node
, struct ust_app
, sock_n
);
1053 * Find an ust_app using the notify sock and return it. RCU read side lock must
1054 * be held before calling this helper function.
1056 static struct ust_app
*find_app_by_notify_sock(int sock
)
1058 struct lttng_ht_node_ulong
*node
;
1059 struct lttng_ht_iter iter
;
1061 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1063 node
= lttng_ht_iter_get_node_ulong(&iter
);
1065 DBG2("UST app find by notify sock %d not found", sock
);
1069 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1076 * Lookup for an ust app event based on event name, filter bytecode and the
1079 * Return an ust_app_event object or NULL on error.
1081 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1082 char *name
, struct lttng_filter_bytecode
*filter
, int loglevel
,
1083 const struct lttng_event_exclusion
*exclusion
)
1085 struct lttng_ht_iter iter
;
1086 struct lttng_ht_node_str
*node
;
1087 struct ust_app_event
*event
= NULL
;
1088 struct ust_app_ht_key key
;
1093 /* Setup key for event lookup. */
1095 key
.filter
= filter
;
1096 key
.loglevel
= loglevel
;
1097 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1098 key
.exclusion
= exclusion
;
1100 /* Lookup using the event name as hash and a custom match fct. */
1101 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1102 ht_match_ust_app_event
, &key
, &iter
.iter
);
1103 node
= lttng_ht_iter_get_node_str(&iter
);
1108 event
= caa_container_of(node
, struct ust_app_event
, node
);
1115 * Create the channel context on the tracer.
1117 * Called with UST app session lock held.
1120 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1121 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1125 health_code_update();
1127 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1128 ua_chan
->obj
, &ua_ctx
->obj
);
1130 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1131 ERR("UST app create channel context failed for app (pid: %d) "
1132 "with ret %d", app
->pid
, ret
);
1135 * This is normal behavior, an application can die during the
1136 * creation process. Don't report an error so the execution can
1137 * continue normally.
1140 DBG3("UST app disable event failed. Application is dead.");
1145 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1147 DBG2("UST app context handle %d created successfully for channel %s",
1148 ua_ctx
->handle
, ua_chan
->name
);
1151 health_code_update();
1156 * Set the filter on the tracer.
1159 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1160 struct ust_app
*app
)
1163 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1165 health_code_update();
1167 if (!ua_event
->filter
) {
1172 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1173 if (!ust_bytecode
) {
1174 ret
= -LTTNG_ERR_NOMEM
;
1177 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1180 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1181 ERR("UST app event %s filter failed for app (pid: %d) "
1182 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1185 * This is normal behavior, an application can die during the
1186 * creation process. Don't report an error so the execution can
1187 * continue normally.
1190 DBG3("UST app filter event failed. Application is dead.");
1195 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1198 health_code_update();
1204 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1205 struct lttng_event_exclusion
*exclusion
)
1207 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1208 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1209 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1211 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1212 if (!ust_exclusion
) {
1217 assert(sizeof(struct lttng_event_exclusion
) ==
1218 sizeof(struct lttng_ust_event_exclusion
));
1219 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1221 return ust_exclusion
;
1225 * Set event exclusions on the tracer.
1228 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1229 struct ust_app
*app
)
1232 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1234 health_code_update();
1236 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1241 ust_exclusion
= create_ust_exclusion_from_exclusion(
1242 ua_event
->exclusion
);
1243 if (!ust_exclusion
) {
1244 ret
= -LTTNG_ERR_NOMEM
;
1247 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1249 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1250 ERR("UST app event %s exclusions failed for app (pid: %d) "
1251 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1254 * This is normal behavior, an application can die during the
1255 * creation process. Don't report an error so the execution can
1256 * continue normally.
1259 DBG3("UST app event exclusion failed. Application is dead.");
1264 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1267 health_code_update();
1268 free(ust_exclusion
);
1273 * Disable the specified event on to UST tracer for the UST session.
1275 static int disable_ust_event(struct ust_app
*app
,
1276 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1280 health_code_update();
1282 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1284 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1285 ERR("UST app event %s disable failed for app (pid: %d) "
1286 "and session handle %d with ret %d",
1287 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1290 * This is normal behavior, an application can die during the
1291 * creation process. Don't report an error so the execution can
1292 * continue normally.
1295 DBG3("UST app disable event failed. Application is dead.");
1300 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1301 ua_event
->attr
.name
, app
->pid
);
1304 health_code_update();
1309 * Disable the specified channel on to UST tracer for the UST session.
1311 static int disable_ust_channel(struct ust_app
*app
,
1312 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1316 health_code_update();
1318 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1320 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1321 ERR("UST app channel %s disable failed for app (pid: %d) "
1322 "and session handle %d with ret %d",
1323 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1326 * This is normal behavior, an application can die during the
1327 * creation process. Don't report an error so the execution can
1328 * continue normally.
1331 DBG3("UST app disable channel failed. Application is dead.");
1336 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1337 ua_chan
->name
, app
->pid
);
1340 health_code_update();
1345 * Enable the specified channel on to UST tracer for the UST session.
1347 static int enable_ust_channel(struct ust_app
*app
,
1348 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1352 health_code_update();
1354 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1356 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1357 ERR("UST app channel %s enable failed for app (pid: %d) "
1358 "and session handle %d with ret %d",
1359 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1362 * This is normal behavior, an application can die during the
1363 * creation process. Don't report an error so the execution can
1364 * continue normally.
1367 DBG3("UST app enable channel failed. Application is dead.");
1372 ua_chan
->enabled
= 1;
1374 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1375 ua_chan
->name
, app
->pid
);
1378 health_code_update();
1383 * Enable the specified event on to UST tracer for the UST session.
1385 static int enable_ust_event(struct ust_app
*app
,
1386 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1390 health_code_update();
1392 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1394 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1395 ERR("UST app event %s enable failed for app (pid: %d) "
1396 "and session handle %d with ret %d",
1397 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1400 * This is normal behavior, an application can die during the
1401 * creation process. Don't report an error so the execution can
1402 * continue normally.
1405 DBG3("UST app enable event failed. Application is dead.");
1410 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1411 ua_event
->attr
.name
, app
->pid
);
1414 health_code_update();
1419 * Send channel and stream buffer to application.
1421 * Return 0 on success. On error, a negative value is returned.
1423 static int send_channel_pid_to_ust(struct ust_app
*app
,
1424 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1427 struct ust_app_stream
*stream
, *stmp
;
1433 health_code_update();
1435 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1438 /* Send channel to the application. */
1439 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1440 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1441 ret
= -ENOTCONN
; /* Caused by app exiting. */
1443 } else if (ret
< 0) {
1447 health_code_update();
1449 /* Send all streams to application. */
1450 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1451 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1452 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1453 ret
= -ENOTCONN
; /* Caused by app exiting. */
1455 } else if (ret
< 0) {
1458 /* We don't need the stream anymore once sent to the tracer. */
1459 cds_list_del(&stream
->list
);
1460 delete_ust_app_stream(-1, stream
);
1462 /* Flag the channel that it is sent to the application. */
1463 ua_chan
->is_sent
= 1;
1466 health_code_update();
1471 * Create the specified event onto the UST tracer for a UST session.
1473 * Should be called with session mutex held.
1476 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1477 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1481 health_code_update();
1483 /* Create UST event on tracer */
1484 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1487 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1488 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1489 ua_event
->attr
.name
, app
->pid
, ret
);
1492 * This is normal behavior, an application can die during the
1493 * creation process. Don't report an error so the execution can
1494 * continue normally.
1497 DBG3("UST app create event failed. Application is dead.");
1502 ua_event
->handle
= ua_event
->obj
->handle
;
1504 DBG2("UST app event %s created successfully for pid:%d",
1505 ua_event
->attr
.name
, app
->pid
);
1507 health_code_update();
1509 /* Set filter if one is present. */
1510 if (ua_event
->filter
) {
1511 ret
= set_ust_event_filter(ua_event
, app
);
1517 /* Set exclusions for the event */
1518 if (ua_event
->exclusion
) {
1519 ret
= set_ust_event_exclusion(ua_event
, app
);
1525 /* If event not enabled, disable it on the tracer */
1526 if (ua_event
->enabled
) {
1528 * We now need to explicitly enable the event, since it
1529 * is now disabled at creation.
1531 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1534 * If we hit an EPERM, something is wrong with our enable call. If
1535 * we get an EEXIST, there is a problem on the tracer side since we
1539 case -LTTNG_UST_ERR_PERM
:
1540 /* Code flow problem */
1542 case -LTTNG_UST_ERR_EXIST
:
1543 /* It's OK for our use case. */
1554 health_code_update();
1559 * Copy data between an UST app event and a LTT event.
1561 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1562 struct ltt_ust_event
*uevent
)
1564 size_t exclusion_alloc_size
;
1566 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1567 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1569 ua_event
->enabled
= uevent
->enabled
;
1571 /* Copy event attributes */
1572 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1574 /* Copy filter bytecode */
1575 if (uevent
->filter
) {
1576 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1577 /* Filter might be NULL here in case of ENONEM. */
1580 /* Copy exclusion data */
1581 if (uevent
->exclusion
) {
1582 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1583 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1584 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1585 if (ua_event
->exclusion
== NULL
) {
1588 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1589 exclusion_alloc_size
);
1595 * Copy data between an UST app channel and a LTT channel.
1597 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1598 struct ltt_ust_channel
*uchan
)
1600 struct lttng_ht_iter iter
;
1601 struct ltt_ust_event
*uevent
;
1602 struct ltt_ust_context
*uctx
;
1603 struct ust_app_event
*ua_event
;
1604 struct ust_app_ctx
*ua_ctx
;
1606 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1608 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1609 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1611 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1612 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1614 /* Copy event attributes since the layout is different. */
1615 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1616 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1617 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1618 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1619 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1620 ua_chan
->attr
.output
= uchan
->attr
.output
;
1622 * Note that the attribute channel type is not set since the channel on the
1623 * tracing registry side does not have this information.
1626 ua_chan
->enabled
= uchan
->enabled
;
1627 ua_chan
->tracing_channel_id
= uchan
->id
;
1629 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1630 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1631 if (ua_ctx
== NULL
) {
1634 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1635 (unsigned long) ua_ctx
->ctx
.ctx
);
1636 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1637 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1640 /* Copy all events from ltt ust channel to ust app channel */
1641 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1642 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1643 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1644 if (ua_event
== NULL
) {
1645 DBG2("UST event %s not found on shadow copy channel",
1647 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1648 if (ua_event
== NULL
) {
1651 shadow_copy_event(ua_event
, uevent
);
1652 add_unique_ust_app_event(ua_chan
, ua_event
);
1656 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1660 * Copy data between a UST app session and a regular LTT session.
1662 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1663 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1665 struct lttng_ht_node_str
*ua_chan_node
;
1666 struct lttng_ht_iter iter
;
1667 struct ltt_ust_channel
*uchan
;
1668 struct ust_app_channel
*ua_chan
;
1670 struct tm
*timeinfo
;
1673 char tmp_shm_path
[PATH_MAX
];
1675 /* Get date and time for unique app path */
1677 timeinfo
= localtime(&rawtime
);
1678 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1680 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1682 ua_sess
->tracing_id
= usess
->id
;
1683 ua_sess
->id
= get_next_session_id();
1684 ua_sess
->uid
= app
->uid
;
1685 ua_sess
->gid
= app
->gid
;
1686 ua_sess
->euid
= usess
->uid
;
1687 ua_sess
->egid
= usess
->gid
;
1688 ua_sess
->buffer_type
= usess
->buffer_type
;
1689 ua_sess
->bits_per_long
= app
->bits_per_long
;
1691 /* There is only one consumer object per session possible. */
1692 consumer_output_get(usess
->consumer
);
1693 ua_sess
->consumer
= usess
->consumer
;
1695 ua_sess
->output_traces
= usess
->output_traces
;
1696 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1697 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1698 &usess
->metadata_attr
);
1700 switch (ua_sess
->buffer_type
) {
1701 case LTTNG_BUFFER_PER_PID
:
1702 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1703 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1706 case LTTNG_BUFFER_PER_UID
:
1707 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1708 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1715 PERROR("asprintf UST shadow copy session");
1720 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1721 sizeof(ua_sess
->root_shm_path
));
1722 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1723 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1724 sizeof(ua_sess
->shm_path
));
1725 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1726 if (ua_sess
->shm_path
[0]) {
1727 switch (ua_sess
->buffer_type
) {
1728 case LTTNG_BUFFER_PER_PID
:
1729 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1730 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1731 app
->name
, app
->pid
, datetime
);
1733 case LTTNG_BUFFER_PER_UID
:
1734 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1735 DEFAULT_UST_TRACE_UID_PATH
,
1736 app
->uid
, app
->bits_per_long
);
1743 PERROR("sprintf UST shadow copy session");
1747 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1748 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1749 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1752 /* Iterate over all channels in global domain. */
1753 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1755 struct lttng_ht_iter uiter
;
1757 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1758 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1759 if (ua_chan_node
!= NULL
) {
1760 /* Session exist. Contiuing. */
1764 DBG2("Channel %s not found on shadow session copy, creating it",
1766 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1767 if (ua_chan
== NULL
) {
1768 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1771 shadow_copy_channel(ua_chan
, uchan
);
1773 * The concept of metadata channel does not exist on the tracing
1774 * registry side of the session daemon so this can only be a per CPU
1775 * channel and not metadata.
1777 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1779 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1784 consumer_output_put(ua_sess
->consumer
);
1788 * Lookup sesison wrapper.
1791 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1792 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1794 /* Get right UST app session from app */
1795 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1799 * Return ust app session from the app session hashtable using the UST session
1802 static struct ust_app_session
*lookup_session_by_app(
1803 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1805 struct lttng_ht_iter iter
;
1806 struct lttng_ht_node_u64
*node
;
1808 __lookup_session_by_app(usess
, app
, &iter
);
1809 node
= lttng_ht_iter_get_node_u64(&iter
);
1814 return caa_container_of(node
, struct ust_app_session
, node
);
1821 * Setup buffer registry per PID for the given session and application. If none
1822 * is found, a new one is created, added to the global registry and
1823 * initialized. If regp is valid, it's set with the newly created object.
1825 * Return 0 on success or else a negative value.
1827 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1828 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1831 struct buffer_reg_pid
*reg_pid
;
1838 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1841 * This is the create channel path meaning that if there is NO
1842 * registry available, we have to create one for this session.
1844 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
1845 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1853 /* Initialize registry. */
1854 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1855 app
->bits_per_long
, app
->uint8_t_alignment
,
1856 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1857 app
->uint64_t_alignment
, app
->long_alignment
,
1858 app
->byte_order
, app
->version
.major
,
1859 app
->version
.minor
, reg_pid
->root_shm_path
,
1861 ua_sess
->euid
, ua_sess
->egid
);
1864 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1865 * destroy the buffer registry, because it is always expected
1866 * that if the buffer registry can be found, its ust registry is
1869 buffer_reg_pid_destroy(reg_pid
);
1873 buffer_reg_pid_add(reg_pid
);
1875 DBG3("UST app buffer registry per PID created successfully");
1887 * Setup buffer registry per UID for the given session and application. If none
1888 * is found, a new one is created, added to the global registry and
1889 * initialized. If regp is valid, it's set with the newly created object.
1891 * Return 0 on success or else a negative value.
1893 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
1894 struct ust_app_session
*ua_sess
,
1895 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
1898 struct buffer_reg_uid
*reg_uid
;
1905 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
1908 * This is the create channel path meaning that if there is NO
1909 * registry available, we have to create one for this session.
1911 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
1912 LTTNG_DOMAIN_UST
, ®_uid
,
1913 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1921 /* Initialize registry. */
1922 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
1923 app
->bits_per_long
, app
->uint8_t_alignment
,
1924 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1925 app
->uint64_t_alignment
, app
->long_alignment
,
1926 app
->byte_order
, app
->version
.major
,
1927 app
->version
.minor
, reg_uid
->root_shm_path
,
1928 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
1931 * reg_uid->registry->reg.ust is NULL upon error, so we need to
1932 * destroy the buffer registry, because it is always expected
1933 * that if the buffer registry can be found, its ust registry is
1936 buffer_reg_uid_destroy(reg_uid
, NULL
);
1939 /* Add node to teardown list of the session. */
1940 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
1942 buffer_reg_uid_add(reg_uid
);
1944 DBG3("UST app buffer registry per UID created successfully");
1955 * Create a session on the tracer side for the given app.
1957 * On success, ua_sess_ptr is populated with the session pointer or else left
1958 * untouched. If the session was created, is_created is set to 1. On error,
1959 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1962 * Returns 0 on success or else a negative code which is either -ENOMEM or
1963 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1965 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1966 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1969 int ret
, created
= 0;
1970 struct ust_app_session
*ua_sess
;
1974 assert(ua_sess_ptr
);
1976 health_code_update();
1978 ua_sess
= lookup_session_by_app(usess
, app
);
1979 if (ua_sess
== NULL
) {
1980 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
1981 app
->pid
, usess
->id
);
1982 ua_sess
= alloc_ust_app_session(app
);
1983 if (ua_sess
== NULL
) {
1984 /* Only malloc can failed so something is really wrong */
1988 shadow_copy_session(ua_sess
, usess
, app
);
1992 switch (usess
->buffer_type
) {
1993 case LTTNG_BUFFER_PER_PID
:
1994 /* Init local registry. */
1995 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
1997 delete_ust_app_session(-1, ua_sess
, app
);
2001 case LTTNG_BUFFER_PER_UID
:
2002 /* Look for a global registry. If none exists, create one. */
2003 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2005 delete_ust_app_session(-1, ua_sess
, app
);
2015 health_code_update();
2017 if (ua_sess
->handle
== -1) {
2018 ret
= ustctl_create_session(app
->sock
);
2020 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2021 ERR("Creating session for app pid %d with ret %d",
2024 DBG("UST app creating session failed. Application is dead");
2026 * This is normal behavior, an application can die during the
2027 * creation process. Don't report an error so the execution can
2028 * continue normally. This will get flagged ENOTCONN and the
2029 * caller will handle it.
2033 delete_ust_app_session(-1, ua_sess
, app
);
2034 if (ret
!= -ENOMEM
) {
2036 * Tracer is probably gone or got an internal error so let's
2037 * behave like it will soon unregister or not usable.
2044 ua_sess
->handle
= ret
;
2046 /* Add ust app session to app's HT */
2047 lttng_ht_node_init_u64(&ua_sess
->node
,
2048 ua_sess
->tracing_id
);
2049 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2051 DBG2("UST app session created successfully with handle %d", ret
);
2054 *ua_sess_ptr
= ua_sess
;
2056 *is_created
= created
;
2059 /* Everything went well. */
2063 health_code_update();
2068 * Match function for a hash table lookup of ust_app_ctx.
2070 * It matches an ust app context based on the context type and, in the case
2071 * of perf counters, their name.
2073 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2075 struct ust_app_ctx
*ctx
;
2076 const struct lttng_ust_context
*key
;
2081 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2085 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2089 /* Check the name in the case of perf thread counters. */
2090 if (key
->ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
2091 if (strncmp(key
->u
.perf_counter
.name
,
2092 ctx
->ctx
.u
.perf_counter
.name
,
2093 sizeof(key
->u
.perf_counter
.name
))) {
2106 * Lookup for an ust app context from an lttng_ust_context.
2108 * Must be called while holding RCU read side lock.
2109 * Return an ust_app_ctx object or NULL on error.
2112 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2113 struct lttng_ust_context
*uctx
)
2115 struct lttng_ht_iter iter
;
2116 struct lttng_ht_node_ulong
*node
;
2117 struct ust_app_ctx
*app_ctx
= NULL
;
2122 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2123 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2124 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2125 node
= lttng_ht_iter_get_node_ulong(&iter
);
2130 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2137 * Create a context for the channel on the tracer.
2139 * Called with UST app session lock held and a RCU read side lock.
2142 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2143 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
2144 struct ust_app
*app
)
2147 struct ust_app_ctx
*ua_ctx
;
2149 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2151 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2157 ua_ctx
= alloc_ust_app_ctx(uctx
);
2158 if (ua_ctx
== NULL
) {
2164 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2165 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2166 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2168 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2178 * Enable on the tracer side a ust app event for the session and channel.
2180 * Called with UST app session lock held.
2183 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2184 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2188 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2193 ua_event
->enabled
= 1;
2200 * Disable on the tracer side a ust app event for the session and channel.
2202 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2203 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2207 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2212 ua_event
->enabled
= 0;
2219 * Lookup ust app channel for session and disable it on the tracer side.
2222 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2223 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2227 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2232 ua_chan
->enabled
= 0;
2239 * Lookup ust app channel for session and enable it on the tracer side. This
2240 * MUST be called with a RCU read side lock acquired.
2242 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2243 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2246 struct lttng_ht_iter iter
;
2247 struct lttng_ht_node_str
*ua_chan_node
;
2248 struct ust_app_channel
*ua_chan
;
2250 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2251 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2252 if (ua_chan_node
== NULL
) {
2253 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2254 uchan
->name
, ua_sess
->tracing_id
);
2258 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2260 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2270 * Ask the consumer to create a channel and get it if successful.
2272 * Return 0 on success or else a negative value.
2274 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2275 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2276 int bitness
, struct ust_registry_session
*registry
)
2279 unsigned int nb_fd
= 0;
2280 struct consumer_socket
*socket
;
2288 health_code_update();
2290 /* Get the right consumer socket for the application. */
2291 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2297 health_code_update();
2299 /* Need one fd for the channel. */
2300 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2302 ERR("Exhausted number of available FD upon create channel");
2307 * Ask consumer to create channel. The consumer will return the number of
2308 * stream we have to expect.
2310 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2317 * Compute the number of fd needed before receiving them. It must be 2 per
2318 * stream (2 being the default value here).
2320 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2322 /* Reserve the amount of file descriptor we need. */
2323 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2325 ERR("Exhausted number of available FD upon create channel");
2326 goto error_fd_get_stream
;
2329 health_code_update();
2332 * Now get the channel from the consumer. This call wil populate the stream
2333 * list of that channel and set the ust objects.
2335 if (usess
->consumer
->enabled
) {
2336 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2346 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2347 error_fd_get_stream
:
2349 * Initiate a destroy channel on the consumer since we had an error
2350 * handling it on our side. The return value is of no importance since we
2351 * already have a ret value set by the previous error that we need to
2354 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2356 lttng_fd_put(LTTNG_FD_APPS
, 1);
2358 health_code_update();
2364 * Duplicate the ust data object of the ust app stream and save it in the
2365 * buffer registry stream.
2367 * Return 0 on success or else a negative value.
2369 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2370 struct ust_app_stream
*stream
)
2377 /* Reserve the amount of file descriptor we need. */
2378 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2380 ERR("Exhausted number of available FD upon duplicate stream");
2384 /* Duplicate object for stream once the original is in the registry. */
2385 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2386 reg_stream
->obj
.ust
);
2388 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2389 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2390 lttng_fd_put(LTTNG_FD_APPS
, 2);
2393 stream
->handle
= stream
->obj
->handle
;
2400 * Duplicate the ust data object of the ust app. channel and save it in the
2401 * buffer registry channel.
2403 * Return 0 on success or else a negative value.
2405 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2406 struct ust_app_channel
*ua_chan
)
2413 /* Need two fds for the channel. */
2414 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2416 ERR("Exhausted number of available FD upon duplicate channel");
2420 /* Duplicate object for stream once the original is in the registry. */
2421 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2423 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2424 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2427 ua_chan
->handle
= ua_chan
->obj
->handle
;
2432 lttng_fd_put(LTTNG_FD_APPS
, 1);
2438 * For a given channel buffer registry, setup all streams of the given ust
2439 * application channel.
2441 * Return 0 on success or else a negative value.
2443 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2444 struct ust_app_channel
*ua_chan
)
2447 struct ust_app_stream
*stream
, *stmp
;
2452 DBG2("UST app setup buffer registry stream");
2454 /* Send all streams to application. */
2455 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2456 struct buffer_reg_stream
*reg_stream
;
2458 ret
= buffer_reg_stream_create(®_stream
);
2464 * Keep original pointer and nullify it in the stream so the delete
2465 * stream call does not release the object.
2467 reg_stream
->obj
.ust
= stream
->obj
;
2469 buffer_reg_stream_add(reg_stream
, reg_chan
);
2471 /* We don't need the streams anymore. */
2472 cds_list_del(&stream
->list
);
2473 delete_ust_app_stream(-1, stream
);
2481 * Create a buffer registry channel for the given session registry and
2482 * application channel object. If regp pointer is valid, it's set with the
2483 * created object. Important, the created object is NOT added to the session
2484 * registry hash table.
2486 * Return 0 on success else a negative value.
2488 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2489 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2492 struct buffer_reg_channel
*reg_chan
= NULL
;
2497 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2499 /* Create buffer registry channel. */
2500 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2505 reg_chan
->consumer_key
= ua_chan
->key
;
2506 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2507 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2509 /* Create and add a channel registry to session. */
2510 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2511 ua_chan
->tracing_channel_id
);
2515 buffer_reg_channel_add(reg_sess
, reg_chan
);
2524 /* Safe because the registry channel object was not added to any HT. */
2525 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2531 * Setup buffer registry channel for the given session registry and application
2532 * channel object. If regp pointer is valid, it's set with the created object.
2534 * Return 0 on success else a negative value.
2536 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2537 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
)
2544 assert(ua_chan
->obj
);
2546 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2548 /* Setup all streams for the registry. */
2549 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
);
2554 reg_chan
->obj
.ust
= ua_chan
->obj
;
2555 ua_chan
->obj
= NULL
;
2560 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2561 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2566 * Send buffer registry channel to the application.
2568 * Return 0 on success else a negative value.
2570 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2571 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2572 struct ust_app_channel
*ua_chan
)
2575 struct buffer_reg_stream
*reg_stream
;
2582 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2584 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2589 /* Send channel to the application. */
2590 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2591 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2592 ret
= -ENOTCONN
; /* Caused by app exiting. */
2594 } else if (ret
< 0) {
2598 health_code_update();
2600 /* Send all streams to application. */
2601 pthread_mutex_lock(®_chan
->stream_list_lock
);
2602 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2603 struct ust_app_stream stream
;
2605 ret
= duplicate_stream_object(reg_stream
, &stream
);
2607 goto error_stream_unlock
;
2610 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2612 (void) release_ust_app_stream(-1, &stream
);
2613 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2614 ret
= -ENOTCONN
; /* Caused by app exiting. */
2615 goto error_stream_unlock
;
2616 } else if (ret
< 0) {
2617 goto error_stream_unlock
;
2619 goto error_stream_unlock
;
2623 * The return value is not important here. This function will output an
2626 (void) release_ust_app_stream(-1, &stream
);
2628 ua_chan
->is_sent
= 1;
2630 error_stream_unlock
:
2631 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2637 * Create and send to the application the created buffers with per UID buffers.
2639 * Return 0 on success else a negative value.
2641 static int create_channel_per_uid(struct ust_app
*app
,
2642 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2643 struct ust_app_channel
*ua_chan
)
2646 struct buffer_reg_uid
*reg_uid
;
2647 struct buffer_reg_channel
*reg_chan
;
2654 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2656 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2658 * The session creation handles the creation of this global registry
2659 * object. If none can be find, there is a code flow problem or a
2664 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2667 /* Create the buffer registry channel object. */
2668 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2670 ERR("Error creating the UST channel \"%s\" registry instance",
2677 * Create the buffers on the consumer side. This call populates the
2678 * ust app channel object with all streams and data object.
2680 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2681 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2683 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2687 * Let's remove the previously created buffer registry channel so
2688 * it's not visible anymore in the session registry.
2690 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2691 ua_chan
->tracing_channel_id
);
2692 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2693 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2698 * Setup the streams and add it to the session registry.
2700 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, reg_chan
);
2702 ERR("Error setting up UST channel \"%s\"",
2709 /* Send buffers to the application. */
2710 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2712 if (ret
!= -ENOTCONN
) {
2713 ERR("Error sending channel to application");
2723 * Create and send to the application the created buffers with per PID buffers.
2725 * Return 0 on success else a negative value.
2727 static int create_channel_per_pid(struct ust_app
*app
,
2728 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2729 struct ust_app_channel
*ua_chan
)
2732 struct ust_registry_session
*registry
;
2739 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2743 registry
= get_session_registry(ua_sess
);
2746 /* Create and add a new channel registry to session. */
2747 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2749 ERR("Error creating the UST channel \"%s\" registry instance",
2754 /* Create and get channel on the consumer side. */
2755 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2756 app
->bits_per_long
, registry
);
2758 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2763 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2765 if (ret
!= -ENOTCONN
) {
2766 ERR("Error sending channel to application");
2777 * From an already allocated ust app channel, create the channel buffers if
2778 * need and send it to the application. This MUST be called with a RCU read
2779 * side lock acquired.
2781 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2782 * the application exited concurrently.
2784 static int do_create_channel(struct ust_app
*app
,
2785 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2786 struct ust_app_channel
*ua_chan
)
2795 /* Handle buffer type before sending the channel to the application. */
2796 switch (usess
->buffer_type
) {
2797 case LTTNG_BUFFER_PER_UID
:
2799 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
2805 case LTTNG_BUFFER_PER_PID
:
2807 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
2819 /* Initialize ust objd object using the received handle and add it. */
2820 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
2821 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
2823 /* If channel is not enabled, disable it on the tracer */
2824 if (!ua_chan
->enabled
) {
2825 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2836 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2837 * newly created channel if not NULL.
2839 * Called with UST app session lock and RCU read-side lock held.
2841 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2842 * the application exited concurrently.
2844 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
2845 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
2846 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
2847 struct ust_app_channel
**ua_chanp
)
2850 struct lttng_ht_iter iter
;
2851 struct lttng_ht_node_str
*ua_chan_node
;
2852 struct ust_app_channel
*ua_chan
;
2854 /* Lookup channel in the ust app session */
2855 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2856 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2857 if (ua_chan_node
!= NULL
) {
2858 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2862 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
2863 if (ua_chan
== NULL
) {
2864 /* Only malloc can fail here */
2868 shadow_copy_channel(ua_chan
, uchan
);
2870 /* Set channel type. */
2871 ua_chan
->attr
.type
= type
;
2873 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
2878 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
2881 /* Only add the channel if successful on the tracer side. */
2882 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
2886 *ua_chanp
= ua_chan
;
2889 /* Everything went well. */
2893 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
2899 * Create UST app event and create it on the tracer side.
2901 * Called with ust app session mutex held.
2904 int create_ust_app_event(struct ust_app_session
*ua_sess
,
2905 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
2906 struct ust_app
*app
)
2909 struct ust_app_event
*ua_event
;
2911 /* Get event node */
2912 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2913 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
2914 if (ua_event
!= NULL
) {
2919 /* Does not exist so create one */
2920 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
2921 if (ua_event
== NULL
) {
2922 /* Only malloc can failed so something is really wrong */
2926 shadow_copy_event(ua_event
, uevent
);
2928 /* Create it on the tracer side */
2929 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2931 /* Not found previously means that it does not exist on the tracer */
2932 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
2936 add_unique_ust_app_event(ua_chan
, ua_event
);
2938 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
2945 /* Valid. Calling here is already in a read side lock */
2946 delete_ust_app_event(-1, ua_event
);
2951 * Create UST metadata and open it on the tracer side.
2953 * Called with UST app session lock held and RCU read side lock.
2955 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
2956 struct ust_app
*app
, struct consumer_output
*consumer
)
2959 struct ust_app_channel
*metadata
;
2960 struct consumer_socket
*socket
;
2961 struct ust_registry_session
*registry
;
2967 registry
= get_session_registry(ua_sess
);
2970 pthread_mutex_lock(®istry
->lock
);
2972 /* Metadata already exists for this registry or it was closed previously */
2973 if (registry
->metadata_key
|| registry
->metadata_closed
) {
2978 /* Allocate UST metadata */
2979 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
2981 /* malloc() failed */
2986 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
2988 /* Need one fd for the channel. */
2989 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2991 ERR("Exhausted number of available FD upon create metadata");
2995 /* Get the right consumer socket for the application. */
2996 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
2999 goto error_consumer
;
3003 * Keep metadata key so we can identify it on the consumer side. Assign it
3004 * to the registry *before* we ask the consumer so we avoid the race of the
3005 * consumer requesting the metadata and the ask_channel call on our side
3006 * did not returned yet.
3008 registry
->metadata_key
= metadata
->key
;
3011 * Ask the metadata channel creation to the consumer. The metadata object
3012 * will be created by the consumer and kept their. However, the stream is
3013 * never added or monitored until we do a first push metadata to the
3016 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3019 /* Nullify the metadata key so we don't try to close it later on. */
3020 registry
->metadata_key
= 0;
3021 goto error_consumer
;
3025 * The setup command will make the metadata stream be sent to the relayd,
3026 * if applicable, and the thread managing the metadatas. This is important
3027 * because after this point, if an error occurs, the only way the stream
3028 * can be deleted is to be monitored in the consumer.
3030 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3032 /* Nullify the metadata key so we don't try to close it later on. */
3033 registry
->metadata_key
= 0;
3034 goto error_consumer
;
3037 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3038 metadata
->key
, app
->pid
);
3041 lttng_fd_put(LTTNG_FD_APPS
, 1);
3042 delete_ust_app_channel(-1, metadata
, app
);
3044 pthread_mutex_unlock(®istry
->lock
);
3049 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3050 * acquired before calling this function.
3052 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3054 struct ust_app
*app
= NULL
;
3055 struct lttng_ht_node_ulong
*node
;
3056 struct lttng_ht_iter iter
;
3058 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3059 node
= lttng_ht_iter_get_node_ulong(&iter
);
3061 DBG2("UST app no found with pid %d", pid
);
3065 DBG2("Found UST app by pid %d", pid
);
3067 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3074 * Allocate and init an UST app object using the registration information and
3075 * the command socket. This is called when the command socket connects to the
3078 * The object is returned on success or else NULL.
3080 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3082 struct ust_app
*lta
= NULL
;
3087 DBG3("UST app creating application for socket %d", sock
);
3089 if ((msg
->bits_per_long
== 64 &&
3090 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3091 || (msg
->bits_per_long
== 32 &&
3092 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3093 ERR("Registration failed: application \"%s\" (pid: %d) has "
3094 "%d-bit long, but no consumerd for this size is available.\n",
3095 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3099 lta
= zmalloc(sizeof(struct ust_app
));
3105 lta
->ppid
= msg
->ppid
;
3106 lta
->uid
= msg
->uid
;
3107 lta
->gid
= msg
->gid
;
3109 lta
->bits_per_long
= msg
->bits_per_long
;
3110 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3111 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3112 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3113 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3114 lta
->long_alignment
= msg
->long_alignment
;
3115 lta
->byte_order
= msg
->byte_order
;
3117 lta
->v_major
= msg
->major
;
3118 lta
->v_minor
= msg
->minor
;
3119 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3120 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3121 lta
->notify_sock
= -1;
3123 /* Copy name and make sure it's NULL terminated. */
3124 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3125 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3128 * Before this can be called, when receiving the registration information,
3129 * the application compatibility is checked. So, at this point, the
3130 * application can work with this session daemon.
3132 lta
->compatible
= 1;
3134 lta
->pid
= msg
->pid
;
3135 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3137 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3139 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3146 * For a given application object, add it to every hash table.
3148 void ust_app_add(struct ust_app
*app
)
3151 assert(app
->notify_sock
>= 0);
3156 * On a re-registration, we want to kick out the previous registration of
3159 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3162 * The socket _should_ be unique until _we_ call close. So, a add_unique
3163 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3164 * already in the table.
3166 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3168 /* Add application to the notify socket hash table. */
3169 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3170 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3172 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3173 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3174 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3181 * Set the application version into the object.
3183 * Return 0 on success else a negative value either an errno code or a
3184 * LTTng-UST error code.
3186 int ust_app_version(struct ust_app
*app
)
3192 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3194 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3195 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3197 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3205 * Unregister app by removing it from the global traceable app list and freeing
3208 * The socket is already closed at this point so no close to sock.
3210 void ust_app_unregister(int sock
)
3212 struct ust_app
*lta
;
3213 struct lttng_ht_node_ulong
*node
;
3214 struct lttng_ht_iter ust_app_sock_iter
;
3215 struct lttng_ht_iter iter
;
3216 struct ust_app_session
*ua_sess
;
3221 /* Get the node reference for a call_rcu */
3222 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3223 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3226 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3227 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3230 * For per-PID buffers, perform "push metadata" and flush all
3231 * application streams before removing app from hash tables,
3232 * ensuring proper behavior of data_pending check.
3233 * Remove sessions so they are not visible during deletion.
3235 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3237 struct ust_registry_session
*registry
;
3239 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3241 /* The session was already removed so scheduled for teardown. */
3245 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3246 (void) ust_app_flush_app_session(lta
, ua_sess
);
3250 * Add session to list for teardown. This is safe since at this point we
3251 * are the only one using this list.
3253 pthread_mutex_lock(&ua_sess
->lock
);
3255 if (ua_sess
->deleted
) {
3256 pthread_mutex_unlock(&ua_sess
->lock
);
3261 * Normally, this is done in the delete session process which is
3262 * executed in the call rcu below. However, upon registration we can't
3263 * afford to wait for the grace period before pushing data or else the
3264 * data pending feature can race between the unregistration and stop
3265 * command where the data pending command is sent *before* the grace
3268 * The close metadata below nullifies the metadata pointer in the
3269 * session so the delete session will NOT push/close a second time.
3271 registry
= get_session_registry(ua_sess
);
3273 /* Push metadata for application before freeing the application. */
3274 (void) push_metadata(registry
, ua_sess
->consumer
);
3277 * Don't ask to close metadata for global per UID buffers. Close
3278 * metadata only on destroy trace session in this case. Also, the
3279 * previous push metadata could have flag the metadata registry to
3280 * close so don't send a close command if closed.
3282 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3283 /* And ask to close it for this session registry. */
3284 (void) close_metadata(registry
, ua_sess
->consumer
);
3287 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3289 pthread_mutex_unlock(&ua_sess
->lock
);
3292 /* Remove application from PID hash table */
3293 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3297 * Remove application from notify hash table. The thread handling the
3298 * notify socket could have deleted the node so ignore on error because
3299 * either way it's valid. The close of that socket is handled by the other
3302 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3303 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3306 * Ignore return value since the node might have been removed before by an
3307 * add replace during app registration because the PID can be reassigned by
3310 iter
.iter
.node
= <a
->pid_n
.node
;
3311 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3313 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3318 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3325 * Fill events array with all events name of all registered apps.
3327 int ust_app_list_events(struct lttng_event
**events
)
3330 size_t nbmem
, count
= 0;
3331 struct lttng_ht_iter iter
;
3332 struct ust_app
*app
;
3333 struct lttng_event
*tmp_event
;
3335 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3336 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3337 if (tmp_event
== NULL
) {
3338 PERROR("zmalloc ust app events");
3345 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3346 struct lttng_ust_tracepoint_iter uiter
;
3348 health_code_update();
3350 if (!app
->compatible
) {
3352 * TODO: In time, we should notice the caller of this error by
3353 * telling him that this is a version error.
3357 handle
= ustctl_tracepoint_list(app
->sock
);
3359 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3360 ERR("UST app list events getting handle failed for app pid %d",
3366 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3367 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3368 /* Handle ustctl error. */
3370 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3371 ERR("UST app tp list get failed for app %d with ret %d",
3374 DBG3("UST app tp list get failed. Application is dead");
3376 * This is normal behavior, an application can die during the
3377 * creation process. Don't report an error so the execution can
3378 * continue normally. Continue normal execution.
3386 health_code_update();
3387 if (count
>= nbmem
) {
3388 /* In case the realloc fails, we free the memory */
3389 struct lttng_event
*new_tmp_event
;
3392 new_nbmem
= nbmem
<< 1;
3393 DBG2("Reallocating event list from %zu to %zu entries",
3395 new_tmp_event
= realloc(tmp_event
,
3396 new_nbmem
* sizeof(struct lttng_event
));
3397 if (new_tmp_event
== NULL
) {
3398 PERROR("realloc ust app events");
3403 /* Zero the new memory */
3404 memset(new_tmp_event
+ nbmem
, 0,
3405 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3407 tmp_event
= new_tmp_event
;
3409 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3410 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3411 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3412 tmp_event
[count
].pid
= app
->pid
;
3413 tmp_event
[count
].enabled
= -1;
3419 *events
= tmp_event
;
3421 DBG2("UST app list events done (%zu events)", count
);
3426 health_code_update();
3431 * Fill events array with all events name of all registered apps.
3433 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3436 size_t nbmem
, count
= 0;
3437 struct lttng_ht_iter iter
;
3438 struct ust_app
*app
;
3439 struct lttng_event_field
*tmp_event
;
3441 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3442 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3443 if (tmp_event
== NULL
) {
3444 PERROR("zmalloc ust app event fields");
3451 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3452 struct lttng_ust_field_iter uiter
;
3454 health_code_update();
3456 if (!app
->compatible
) {
3458 * TODO: In time, we should notice the caller of this error by
3459 * telling him that this is a version error.
3463 handle
= ustctl_tracepoint_field_list(app
->sock
);
3465 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3466 ERR("UST app list field getting handle failed for app pid %d",
3472 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3473 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3474 /* Handle ustctl error. */
3476 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3477 ERR("UST app tp list field failed for app %d with ret %d",
3480 DBG3("UST app tp list field failed. Application is dead");
3482 * This is normal behavior, an application can die during the
3483 * creation process. Don't report an error so the execution can
3484 * continue normally. Reset list and count for next app.
3492 health_code_update();
3493 if (count
>= nbmem
) {
3494 /* In case the realloc fails, we free the memory */
3495 struct lttng_event_field
*new_tmp_event
;
3498 new_nbmem
= nbmem
<< 1;
3499 DBG2("Reallocating event field list from %zu to %zu entries",
3501 new_tmp_event
= realloc(tmp_event
,
3502 new_nbmem
* sizeof(struct lttng_event_field
));
3503 if (new_tmp_event
== NULL
) {
3504 PERROR("realloc ust app event fields");
3509 /* Zero the new memory */
3510 memset(new_tmp_event
+ nbmem
, 0,
3511 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3513 tmp_event
= new_tmp_event
;
3516 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3517 /* Mapping between these enums matches 1 to 1. */
3518 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3519 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3521 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3522 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3523 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3524 tmp_event
[count
].event
.pid
= app
->pid
;
3525 tmp_event
[count
].event
.enabled
= -1;
3531 *fields
= tmp_event
;
3533 DBG2("UST app list event fields done (%zu events)", count
);
3538 health_code_update();
3543 * Free and clean all traceable apps of the global list.
3545 * Should _NOT_ be called with RCU read-side lock held.
3547 void ust_app_clean_list(void)
3550 struct ust_app
*app
;
3551 struct lttng_ht_iter iter
;
3553 DBG2("UST app cleaning registered apps hash table");
3558 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3559 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3561 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3565 /* Cleanup socket hash table */
3566 if (ust_app_ht_by_sock
) {
3567 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3569 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3574 /* Cleanup notify socket hash table */
3575 if (ust_app_ht_by_notify_sock
) {
3576 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3577 notify_sock_n
.node
) {
3578 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3584 /* Destroy is done only when the ht is empty */
3586 ht_cleanup_push(ust_app_ht
);
3588 if (ust_app_ht_by_sock
) {
3589 ht_cleanup_push(ust_app_ht_by_sock
);
3591 if (ust_app_ht_by_notify_sock
) {
3592 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3597 * Init UST app hash table.
3599 int ust_app_ht_alloc(void)
3601 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3605 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3606 if (!ust_app_ht_by_sock
) {
3609 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3610 if (!ust_app_ht_by_notify_sock
) {
3617 * For a specific UST session, disable the channel for all registered apps.
3619 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3620 struct ltt_ust_channel
*uchan
)
3623 struct lttng_ht_iter iter
;
3624 struct lttng_ht_node_str
*ua_chan_node
;
3625 struct ust_app
*app
;
3626 struct ust_app_session
*ua_sess
;
3627 struct ust_app_channel
*ua_chan
;
3629 if (usess
== NULL
|| uchan
== NULL
) {
3630 ERR("Disabling UST global channel with NULL values");
3635 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3636 uchan
->name
, usess
->id
);
3640 /* For every registered applications */
3641 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3642 struct lttng_ht_iter uiter
;
3643 if (!app
->compatible
) {
3645 * TODO: In time, we should notice the caller of this error by
3646 * telling him that this is a version error.
3650 ua_sess
= lookup_session_by_app(usess
, app
);
3651 if (ua_sess
== NULL
) {
3656 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3657 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3658 /* If the session if found for the app, the channel must be there */
3659 assert(ua_chan_node
);
3661 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3662 /* The channel must not be already disabled */
3663 assert(ua_chan
->enabled
== 1);
3665 /* Disable channel onto application */
3666 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3668 /* XXX: We might want to report this error at some point... */
3680 * For a specific UST session, enable the channel for all registered apps.
3682 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3683 struct ltt_ust_channel
*uchan
)
3686 struct lttng_ht_iter iter
;
3687 struct ust_app
*app
;
3688 struct ust_app_session
*ua_sess
;
3690 if (usess
== NULL
|| uchan
== NULL
) {
3691 ERR("Adding UST global channel to NULL values");
3696 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3697 uchan
->name
, usess
->id
);
3701 /* For every registered applications */
3702 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3703 if (!app
->compatible
) {
3705 * TODO: In time, we should notice the caller of this error by
3706 * telling him that this is a version error.
3710 ua_sess
= lookup_session_by_app(usess
, app
);
3711 if (ua_sess
== NULL
) {
3715 /* Enable channel onto application */
3716 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3718 /* XXX: We might want to report this error at some point... */
3730 * Disable an event in a channel and for a specific session.
3732 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3733 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3736 struct lttng_ht_iter iter
, uiter
;
3737 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3738 struct ust_app
*app
;
3739 struct ust_app_session
*ua_sess
;
3740 struct ust_app_channel
*ua_chan
;
3741 struct ust_app_event
*ua_event
;
3743 DBG("UST app disabling event %s for all apps in channel "
3744 "%s for session id %" PRIu64
,
3745 uevent
->attr
.name
, uchan
->name
, usess
->id
);
3749 /* For all registered applications */
3750 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3751 if (!app
->compatible
) {
3753 * TODO: In time, we should notice the caller of this error by
3754 * telling him that this is a version error.
3758 ua_sess
= lookup_session_by_app(usess
, app
);
3759 if (ua_sess
== NULL
) {
3764 /* Lookup channel in the ust app session */
3765 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3766 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3767 if (ua_chan_node
== NULL
) {
3768 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
3769 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
3772 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3774 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
3775 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
3776 if (ua_event_node
== NULL
) {
3777 DBG2("Event %s not found in channel %s for app pid %d."
3778 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
3781 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3783 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3785 /* XXX: Report error someday... */
3796 * For a specific UST session, create the channel for all registered apps.
3798 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
3799 struct ltt_ust_channel
*uchan
)
3801 int ret
= 0, created
;
3802 struct lttng_ht_iter iter
;
3803 struct ust_app
*app
;
3804 struct ust_app_session
*ua_sess
= NULL
;
3806 /* Very wrong code flow */
3810 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
3811 uchan
->name
, usess
->id
);
3815 /* For every registered applications */
3816 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3817 if (!app
->compatible
) {
3819 * TODO: In time, we should notice the caller of this error by
3820 * telling him that this is a version error.
3824 if (!trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
3830 * Create session on the tracer side and add it to app session HT. Note
3831 * that if session exist, it will simply return a pointer to the ust
3834 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
3839 * The application's socket is not valid. Either a bad socket
3840 * or a timeout on it. We can't inform the caller that for a
3841 * specific app, the session failed so lets continue here.
3843 ret
= 0; /* Not an error. */
3847 goto error_rcu_unlock
;
3852 pthread_mutex_lock(&ua_sess
->lock
);
3854 if (ua_sess
->deleted
) {
3855 pthread_mutex_unlock(&ua_sess
->lock
);
3859 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
3860 sizeof(uchan
->name
))) {
3861 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
3864 /* Create channel onto application. We don't need the chan ref. */
3865 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
3866 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
3868 pthread_mutex_unlock(&ua_sess
->lock
);
3870 /* Cleanup the created session if it's the case. */
3872 destroy_app_session(app
, ua_sess
);
3877 * The application's socket is not valid. Either a bad socket
3878 * or a timeout on it. We can't inform the caller that for a
3879 * specific app, the session failed so lets continue here.
3881 ret
= 0; /* Not an error. */
3885 goto error_rcu_unlock
;
3896 * Enable event for a specific session and channel on the tracer.
3898 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
3899 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3902 struct lttng_ht_iter iter
, uiter
;
3903 struct lttng_ht_node_str
*ua_chan_node
;
3904 struct ust_app
*app
;
3905 struct ust_app_session
*ua_sess
;
3906 struct ust_app_channel
*ua_chan
;
3907 struct ust_app_event
*ua_event
;
3909 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
3910 uevent
->attr
.name
, usess
->id
);
3913 * NOTE: At this point, this function is called only if the session and
3914 * channel passed are already created for all apps. and enabled on the
3920 /* For all registered applications */
3921 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3922 if (!app
->compatible
) {
3924 * TODO: In time, we should notice the caller of this error by
3925 * telling him that this is a version error.
3929 ua_sess
= lookup_session_by_app(usess
, app
);
3931 /* The application has problem or is probably dead. */
3935 pthread_mutex_lock(&ua_sess
->lock
);
3937 if (ua_sess
->deleted
) {
3938 pthread_mutex_unlock(&ua_sess
->lock
);
3942 /* Lookup channel in the ust app session */
3943 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3944 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3946 * It is possible that the channel cannot be found is
3947 * the channel/event creation occurs concurrently with
3948 * an application exit.
3950 if (!ua_chan_node
) {
3951 pthread_mutex_unlock(&ua_sess
->lock
);
3955 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3957 /* Get event node */
3958 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3959 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3960 if (ua_event
== NULL
) {
3961 DBG3("UST app enable event %s not found for app PID %d."
3962 "Skipping app", uevent
->attr
.name
, app
->pid
);
3966 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
3968 pthread_mutex_unlock(&ua_sess
->lock
);
3972 pthread_mutex_unlock(&ua_sess
->lock
);
3981 * For a specific existing UST session and UST channel, creates the event for
3982 * all registered apps.
3984 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
3985 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3988 struct lttng_ht_iter iter
, uiter
;
3989 struct lttng_ht_node_str
*ua_chan_node
;
3990 struct ust_app
*app
;
3991 struct ust_app_session
*ua_sess
;
3992 struct ust_app_channel
*ua_chan
;
3994 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
3995 uevent
->attr
.name
, usess
->id
);
3999 /* For all registered applications */
4000 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4001 if (!app
->compatible
) {
4003 * TODO: In time, we should notice the caller of this error by
4004 * telling him that this is a version error.
4008 ua_sess
= lookup_session_by_app(usess
, app
);
4010 /* The application has problem or is probably dead. */
4014 pthread_mutex_lock(&ua_sess
->lock
);
4016 if (ua_sess
->deleted
) {
4017 pthread_mutex_unlock(&ua_sess
->lock
);
4021 /* Lookup channel in the ust app session */
4022 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4023 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4024 /* If the channel is not found, there is a code flow error */
4025 assert(ua_chan_node
);
4027 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4029 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4030 pthread_mutex_unlock(&ua_sess
->lock
);
4032 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4033 /* Possible value at this point: -ENOMEM. If so, we stop! */
4036 DBG2("UST app event %s already exist on app PID %d",
4037 uevent
->attr
.name
, app
->pid
);
4048 * Start tracing for a specific UST session and app.
4051 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4054 struct ust_app_session
*ua_sess
;
4056 DBG("Starting tracing for ust app pid %d", app
->pid
);
4060 if (!app
->compatible
) {
4064 ua_sess
= lookup_session_by_app(usess
, app
);
4065 if (ua_sess
== NULL
) {
4066 /* The session is in teardown process. Ignore and continue. */
4070 pthread_mutex_lock(&ua_sess
->lock
);
4072 if (ua_sess
->deleted
) {
4073 pthread_mutex_unlock(&ua_sess
->lock
);
4077 /* Upon restart, we skip the setup, already done */
4078 if (ua_sess
->started
) {
4082 /* Create directories if consumer is LOCAL and has a path defined. */
4083 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
4084 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
4085 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
4086 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
4088 if (errno
!= EEXIST
) {
4089 ERR("Trace directory creation error");
4096 * Create the metadata for the application. This returns gracefully if a
4097 * metadata was already set for the session.
4099 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4104 health_code_update();
4107 /* This start the UST tracing */
4108 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4110 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4111 ERR("Error starting tracing for app pid: %d (ret: %d)",
4114 DBG("UST app start session failed. Application is dead.");
4116 * This is normal behavior, an application can die during the
4117 * creation process. Don't report an error so the execution can
4118 * continue normally.
4120 pthread_mutex_unlock(&ua_sess
->lock
);
4126 /* Indicate that the session has been started once */
4127 ua_sess
->started
= 1;
4129 pthread_mutex_unlock(&ua_sess
->lock
);
4131 health_code_update();
4133 /* Quiescent wait after starting trace */
4134 ret
= ustctl_wait_quiescent(app
->sock
);
4135 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4136 ERR("UST app wait quiescent failed for app pid %d ret %d",
4142 health_code_update();
4146 pthread_mutex_unlock(&ua_sess
->lock
);
4148 health_code_update();
4153 * Stop tracing for a specific UST session and app.
4156 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4159 struct ust_app_session
*ua_sess
;
4160 struct ust_registry_session
*registry
;
4162 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4166 if (!app
->compatible
) {
4167 goto end_no_session
;
4170 ua_sess
= lookup_session_by_app(usess
, app
);
4171 if (ua_sess
== NULL
) {
4172 goto end_no_session
;
4175 pthread_mutex_lock(&ua_sess
->lock
);
4177 if (ua_sess
->deleted
) {
4178 pthread_mutex_unlock(&ua_sess
->lock
);
4179 goto end_no_session
;
4183 * If started = 0, it means that stop trace has been called for a session
4184 * that was never started. It's possible since we can have a fail start
4185 * from either the application manager thread or the command thread. Simply
4186 * indicate that this is a stop error.
4188 if (!ua_sess
->started
) {
4189 goto error_rcu_unlock
;
4192 health_code_update();
4194 /* This inhibits UST tracing */
4195 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4197 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4198 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4201 DBG("UST app stop session failed. Application is dead.");
4203 * This is normal behavior, an application can die during the
4204 * creation process. Don't report an error so the execution can
4205 * continue normally.
4209 goto error_rcu_unlock
;
4212 health_code_update();
4214 /* Quiescent wait after stopping trace */
4215 ret
= ustctl_wait_quiescent(app
->sock
);
4216 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4217 ERR("UST app wait quiescent failed for app pid %d ret %d",
4221 health_code_update();
4223 registry
= get_session_registry(ua_sess
);
4226 /* Push metadata for application before freeing the application. */
4227 (void) push_metadata(registry
, ua_sess
->consumer
);
4230 pthread_mutex_unlock(&ua_sess
->lock
);
4233 health_code_update();
4237 pthread_mutex_unlock(&ua_sess
->lock
);
4239 health_code_update();
4244 int ust_app_flush_app_session(struct ust_app
*app
,
4245 struct ust_app_session
*ua_sess
)
4247 int ret
, retval
= 0;
4248 struct lttng_ht_iter iter
;
4249 struct ust_app_channel
*ua_chan
;
4250 struct consumer_socket
*socket
;
4252 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4256 if (!app
->compatible
) {
4257 goto end_not_compatible
;
4260 pthread_mutex_lock(&ua_sess
->lock
);
4262 if (ua_sess
->deleted
) {
4266 health_code_update();
4268 /* Flushing buffers */
4269 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4272 /* Flush buffers and push metadata. */
4273 switch (ua_sess
->buffer_type
) {
4274 case LTTNG_BUFFER_PER_PID
:
4275 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4277 health_code_update();
4278 assert(ua_chan
->is_sent
);
4279 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4281 ERR("Error flushing consumer channel");
4287 case LTTNG_BUFFER_PER_UID
:
4293 health_code_update();
4296 pthread_mutex_unlock(&ua_sess
->lock
);
4300 health_code_update();
4305 * Flush buffers for all applications for a specific UST session.
4306 * Called with UST session lock held.
4309 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4314 DBG("Flushing session buffers for all ust apps");
4318 /* Flush buffers and push metadata. */
4319 switch (usess
->buffer_type
) {
4320 case LTTNG_BUFFER_PER_UID
:
4322 struct buffer_reg_uid
*reg
;
4323 struct lttng_ht_iter iter
;
4325 /* Flush all per UID buffers associated to that session. */
4326 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4327 struct ust_registry_session
*ust_session_reg
;
4328 struct buffer_reg_channel
*reg_chan
;
4329 struct consumer_socket
*socket
;
4331 /* Get consumer socket to use to push the metadata.*/
4332 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4335 /* Ignore request if no consumer is found for the session. */
4339 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4340 reg_chan
, node
.node
) {
4342 * The following call will print error values so the return
4343 * code is of little importance because whatever happens, we
4344 * have to try them all.
4346 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4349 ust_session_reg
= reg
->registry
->reg
.ust
;
4350 /* Push metadata. */
4351 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4355 case LTTNG_BUFFER_PER_PID
:
4357 struct ust_app_session
*ua_sess
;
4358 struct lttng_ht_iter iter
;
4359 struct ust_app
*app
;
4361 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4362 ua_sess
= lookup_session_by_app(usess
, app
);
4363 if (ua_sess
== NULL
) {
4366 (void) ust_app_flush_app_session(app
, ua_sess
);
4377 health_code_update();
4382 * Destroy a specific UST session in apps.
4384 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4387 struct ust_app_session
*ua_sess
;
4388 struct lttng_ht_iter iter
;
4389 struct lttng_ht_node_u64
*node
;
4391 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4395 if (!app
->compatible
) {
4399 __lookup_session_by_app(usess
, app
, &iter
);
4400 node
= lttng_ht_iter_get_node_u64(&iter
);
4402 /* Session is being or is deleted. */
4405 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4407 health_code_update();
4408 destroy_app_session(app
, ua_sess
);
4410 health_code_update();
4412 /* Quiescent wait after stopping trace */
4413 ret
= ustctl_wait_quiescent(app
->sock
);
4414 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4415 ERR("UST app wait quiescent failed for app pid %d ret %d",
4420 health_code_update();
4425 * Start tracing for the UST session.
4427 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4430 struct lttng_ht_iter iter
;
4431 struct ust_app
*app
;
4433 DBG("Starting all UST traces");
4437 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4438 ret
= ust_app_start_trace(usess
, app
);
4440 /* Continue to next apps even on error */
4451 * Start tracing for the UST session.
4452 * Called with UST session lock held.
4454 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4457 struct lttng_ht_iter iter
;
4458 struct ust_app
*app
;
4460 DBG("Stopping all UST traces");
4464 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4465 ret
= ust_app_stop_trace(usess
, app
);
4467 /* Continue to next apps even on error */
4472 (void) ust_app_flush_session(usess
);
4480 * Destroy app UST session.
4482 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4485 struct lttng_ht_iter iter
;
4486 struct ust_app
*app
;
4488 DBG("Destroy all UST traces");
4492 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4493 ret
= destroy_trace(usess
, app
);
4495 /* Continue to next apps even on error */
4506 void ust_app_global_create(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4509 struct lttng_ht_iter iter
, uiter
;
4510 struct ust_app_session
*ua_sess
= NULL
;
4511 struct ust_app_channel
*ua_chan
;
4512 struct ust_app_event
*ua_event
;
4513 struct ust_app_ctx
*ua_ctx
;
4516 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &is_created
);
4518 /* Tracer is probably gone or ENOMEM. */
4522 /* App session already created. */
4527 pthread_mutex_lock(&ua_sess
->lock
);
4529 if (ua_sess
->deleted
) {
4530 pthread_mutex_unlock(&ua_sess
->lock
);
4535 * We can iterate safely here over all UST app session since the create ust
4536 * app session above made a shadow copy of the UST global domain from the
4539 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4541 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4542 if (ret
< 0 && ret
!= -ENOTCONN
) {
4544 * Stop everything. On error, the application
4545 * failed, no more file descriptor are available
4546 * or ENOMEM so stopping here is the only thing
4547 * we can do for now. The only exception is
4548 * -ENOTCONN, which indicates that the application
4555 * Add context using the list so they are enabled in the same order the
4558 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
4559 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
4566 /* For each events */
4567 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
4569 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
4576 pthread_mutex_unlock(&ua_sess
->lock
);
4578 if (usess
->active
) {
4579 ret
= ust_app_start_trace(usess
, app
);
4584 DBG2("UST trace started for app pid %d", app
->pid
);
4587 /* Everything went well at this point. */
4591 pthread_mutex_unlock(&ua_sess
->lock
);
4594 destroy_app_session(app
, ua_sess
);
4600 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4602 struct ust_app_session
*ua_sess
;
4604 ua_sess
= lookup_session_by_app(usess
, app
);
4605 if (ua_sess
== NULL
) {
4608 destroy_app_session(app
, ua_sess
);
4612 * Add channels/events from UST global domain to registered apps at sock.
4614 * Called with session lock held.
4615 * Called with RCU read-side lock held.
4617 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4621 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
4622 app
->sock
, usess
->id
);
4624 if (!app
->compatible
) {
4628 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4629 ust_app_global_create(usess
, app
);
4631 ust_app_global_destroy(usess
, app
);
4636 * Called with session lock held.
4638 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
4640 struct lttng_ht_iter iter
;
4641 struct ust_app
*app
;
4644 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4645 ust_app_global_update(usess
, app
);
4651 * Add context to a specific channel for global UST domain.
4653 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
4654 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
4657 struct lttng_ht_node_str
*ua_chan_node
;
4658 struct lttng_ht_iter iter
, uiter
;
4659 struct ust_app_channel
*ua_chan
= NULL
;
4660 struct ust_app_session
*ua_sess
;
4661 struct ust_app
*app
;
4665 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4666 if (!app
->compatible
) {
4668 * TODO: In time, we should notice the caller of this error by
4669 * telling him that this is a version error.
4673 ua_sess
= lookup_session_by_app(usess
, app
);
4674 if (ua_sess
== NULL
) {
4678 pthread_mutex_lock(&ua_sess
->lock
);
4680 if (ua_sess
->deleted
) {
4681 pthread_mutex_unlock(&ua_sess
->lock
);
4685 /* Lookup channel in the ust app session */
4686 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4687 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4688 if (ua_chan_node
== NULL
) {
4691 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4693 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4698 pthread_mutex_unlock(&ua_sess
->lock
);
4706 * Enable event for a channel from a UST session for a specific PID.
4708 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4709 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4712 struct lttng_ht_iter iter
;
4713 struct lttng_ht_node_str
*ua_chan_node
;
4714 struct ust_app
*app
;
4715 struct ust_app_session
*ua_sess
;
4716 struct ust_app_channel
*ua_chan
;
4717 struct ust_app_event
*ua_event
;
4719 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4723 app
= ust_app_find_by_pid(pid
);
4725 ERR("UST app enable event per PID %d not found", pid
);
4730 if (!app
->compatible
) {
4735 ua_sess
= lookup_session_by_app(usess
, app
);
4737 /* The application has problem or is probably dead. */
4742 pthread_mutex_lock(&ua_sess
->lock
);
4744 if (ua_sess
->deleted
) {
4749 /* Lookup channel in the ust app session */
4750 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4751 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4752 /* If the channel is not found, there is a code flow error */
4753 assert(ua_chan_node
);
4755 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4757 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4758 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4759 if (ua_event
== NULL
) {
4760 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4765 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4772 pthread_mutex_unlock(&ua_sess
->lock
);
4779 * Calibrate registered applications.
4781 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
4784 struct lttng_ht_iter iter
;
4785 struct ust_app
*app
;
4789 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4790 if (!app
->compatible
) {
4792 * TODO: In time, we should notice the caller of this error by
4793 * telling him that this is a version error.
4798 health_code_update();
4800 ret
= ustctl_calibrate(app
->sock
, calibrate
);
4804 /* Means that it's not implemented on the tracer side. */
4808 DBG2("Calibrate app PID %d returned with error %d",
4815 DBG("UST app global domain calibration finished");
4819 health_code_update();
4825 * Receive registration and populate the given msg structure.
4827 * On success return 0 else a negative value returned by the ustctl call.
4829 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
4832 uint32_t pid
, ppid
, uid
, gid
;
4836 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
4837 &pid
, &ppid
, &uid
, &gid
,
4838 &msg
->bits_per_long
,
4839 &msg
->uint8_t_alignment
,
4840 &msg
->uint16_t_alignment
,
4841 &msg
->uint32_t_alignment
,
4842 &msg
->uint64_t_alignment
,
4843 &msg
->long_alignment
,
4850 case LTTNG_UST_ERR_EXITING
:
4851 DBG3("UST app recv reg message failed. Application died");
4853 case LTTNG_UST_ERR_UNSUP_MAJOR
:
4854 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
4855 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
4856 LTTNG_UST_ABI_MINOR_VERSION
);
4859 ERR("UST app recv reg message failed with ret %d", ret
);
4864 msg
->pid
= (pid_t
) pid
;
4865 msg
->ppid
= (pid_t
) ppid
;
4866 msg
->uid
= (uid_t
) uid
;
4867 msg
->gid
= (gid_t
) gid
;
4874 * Return a ust app channel object using the application object and the channel
4875 * object descriptor has a key. If not found, NULL is returned. A RCU read side
4876 * lock MUST be acquired before calling this function.
4878 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
4881 struct lttng_ht_node_ulong
*node
;
4882 struct lttng_ht_iter iter
;
4883 struct ust_app_channel
*ua_chan
= NULL
;
4887 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
4888 node
= lttng_ht_iter_get_node_ulong(&iter
);
4890 DBG2("UST app channel find by objd %d not found", objd
);
4894 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
4901 * Reply to a register channel notification from an application on the notify
4902 * socket. The channel metadata is also created.
4904 * The session UST registry lock is acquired in this function.
4906 * On success 0 is returned else a negative value.
4908 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
4909 size_t nr_fields
, struct ustctl_field
*fields
)
4911 int ret
, ret_code
= 0;
4912 uint32_t chan_id
, reg_count
;
4913 uint64_t chan_reg_key
;
4914 enum ustctl_channel_header type
;
4915 struct ust_app
*app
;
4916 struct ust_app_channel
*ua_chan
;
4917 struct ust_app_session
*ua_sess
;
4918 struct ust_registry_session
*registry
;
4919 struct ust_registry_channel
*chan_reg
;
4923 /* Lookup application. If not found, there is a code flow error. */
4924 app
= find_app_by_notify_sock(sock
);
4926 DBG("Application socket %d is being teardown. Abort event notify",
4930 goto error_rcu_unlock
;
4933 /* Lookup channel by UST object descriptor. */
4934 ua_chan
= find_channel_by_objd(app
, cobjd
);
4936 DBG("Application channel is being teardown. Abort event notify");
4939 goto error_rcu_unlock
;
4942 assert(ua_chan
->session
);
4943 ua_sess
= ua_chan
->session
;
4945 /* Get right session registry depending on the session buffer type. */
4946 registry
= get_session_registry(ua_sess
);
4949 /* Depending on the buffer type, a different channel key is used. */
4950 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4951 chan_reg_key
= ua_chan
->tracing_channel_id
;
4953 chan_reg_key
= ua_chan
->key
;
4956 pthread_mutex_lock(®istry
->lock
);
4958 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
4961 if (!chan_reg
->register_done
) {
4962 reg_count
= ust_registry_get_event_count(chan_reg
);
4963 if (reg_count
< 31) {
4964 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
4966 type
= USTCTL_CHANNEL_HEADER_LARGE
;
4969 chan_reg
->nr_ctx_fields
= nr_fields
;
4970 chan_reg
->ctx_fields
= fields
;
4971 chan_reg
->header_type
= type
;
4973 /* Get current already assigned values. */
4974 type
= chan_reg
->header_type
;
4976 /* Set to NULL so the error path does not do a double free. */
4979 /* Channel id is set during the object creation. */
4980 chan_id
= chan_reg
->chan_id
;
4982 /* Append to metadata */
4983 if (!chan_reg
->metadata_dumped
) {
4984 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
4986 ERR("Error appending channel metadata (errno = %d)", ret_code
);
4992 DBG3("UST app replying to register channel key %" PRIu64
4993 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
4996 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
4998 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4999 ERR("UST app reply channel failed with ret %d", ret
);
5001 DBG3("UST app reply channel failed. Application died");
5006 /* This channel registry registration is completed. */
5007 chan_reg
->register_done
= 1;
5010 pthread_mutex_unlock(®istry
->lock
);
5020 * Add event to the UST channel registry. When the event is added to the
5021 * registry, the metadata is also created. Once done, this replies to the
5022 * application with the appropriate error code.
5024 * The session UST registry lock is acquired in the function.
5026 * On success 0 is returned else a negative value.
5028 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5029 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
5030 char *model_emf_uri
)
5033 uint32_t event_id
= 0;
5034 uint64_t chan_reg_key
;
5035 struct ust_app
*app
;
5036 struct ust_app_channel
*ua_chan
;
5037 struct ust_app_session
*ua_sess
;
5038 struct ust_registry_session
*registry
;
5042 /* Lookup application. If not found, there is a code flow error. */
5043 app
= find_app_by_notify_sock(sock
);
5045 DBG("Application socket %d is being teardown. Abort event notify",
5050 free(model_emf_uri
);
5051 goto error_rcu_unlock
;
5054 /* Lookup channel by UST object descriptor. */
5055 ua_chan
= find_channel_by_objd(app
, cobjd
);
5057 DBG("Application channel is being teardown. Abort event notify");
5061 free(model_emf_uri
);
5062 goto error_rcu_unlock
;
5065 assert(ua_chan
->session
);
5066 ua_sess
= ua_chan
->session
;
5068 registry
= get_session_registry(ua_sess
);
5071 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5072 chan_reg_key
= ua_chan
->tracing_channel_id
;
5074 chan_reg_key
= ua_chan
->key
;
5077 pthread_mutex_lock(®istry
->lock
);
5080 * From this point on, this call acquires the ownership of the sig, fields
5081 * and model_emf_uri meaning any free are done inside it if needed. These
5082 * three variables MUST NOT be read/write after this.
5084 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5085 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
, loglevel
,
5086 model_emf_uri
, ua_sess
->buffer_type
, &event_id
,
5090 * The return value is returned to ustctl so in case of an error, the
5091 * application can be notified. In case of an error, it's important not to
5092 * return a negative error or else the application will get closed.
5094 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5096 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5097 ERR("UST app reply event failed with ret %d", ret
);
5099 DBG3("UST app reply event failed. Application died");
5102 * No need to wipe the create event since the application socket will
5103 * get close on error hence cleaning up everything by itself.
5108 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5112 pthread_mutex_unlock(®istry
->lock
);
5119 * Handle application notification through the given notify socket.
5121 * Return 0 on success or else a negative value.
5123 int ust_app_recv_notify(int sock
)
5126 enum ustctl_notify_cmd cmd
;
5128 DBG3("UST app receiving notify from sock %d", sock
);
5130 ret
= ustctl_recv_notify(sock
, &cmd
);
5132 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5133 ERR("UST app recv notify failed with ret %d", ret
);
5135 DBG3("UST app recv notify failed. Application died");
5141 case USTCTL_NOTIFY_CMD_EVENT
:
5143 int sobjd
, cobjd
, loglevel
;
5144 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5146 struct ustctl_field
*fields
;
5148 DBG2("UST app ustctl register event received");
5150 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
5151 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
5153 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5154 ERR("UST app recv event failed with ret %d", ret
);
5156 DBG3("UST app recv event failed. Application died");
5162 * Add event to the UST registry coming from the notify socket. This
5163 * call will free if needed the sig, fields and model_emf_uri. This
5164 * code path loses the ownsership of these variables and transfer them
5165 * to the this function.
5167 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5168 fields
, loglevel
, model_emf_uri
);
5175 case USTCTL_NOTIFY_CMD_CHANNEL
:
5179 struct ustctl_field
*fields
;
5181 DBG2("UST app ustctl register channel received");
5183 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5186 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5187 ERR("UST app recv channel failed with ret %d", ret
);
5189 DBG3("UST app recv channel failed. Application died");
5195 * The fields ownership are transfered to this function call meaning
5196 * that if needed it will be freed. After this, it's invalid to access
5197 * fields or clean it up.
5199 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
5208 /* Should NEVER happen. */
5217 * Once the notify socket hangs up, this is called. First, it tries to find the
5218 * corresponding application. On failure, the call_rcu to close the socket is
5219 * executed. If an application is found, it tries to delete it from the notify
5220 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5222 * Note that an object needs to be allocated here so on ENOMEM failure, the
5223 * call RCU is not done but the rest of the cleanup is.
5225 void ust_app_notify_sock_unregister(int sock
)
5228 struct lttng_ht_iter iter
;
5229 struct ust_app
*app
;
5230 struct ust_app_notify_sock_obj
*obj
;
5236 obj
= zmalloc(sizeof(*obj
));
5239 * An ENOMEM is kind of uncool. If this strikes we continue the
5240 * procedure but the call_rcu will not be called. In this case, we
5241 * accept the fd leak rather than possibly creating an unsynchronized
5242 * state between threads.
5244 * TODO: The notify object should be created once the notify socket is
5245 * registered and stored independantely from the ust app object. The
5246 * tricky part is to synchronize the teardown of the application and
5247 * this notify object. Let's keep that in mind so we can avoid this
5248 * kind of shenanigans with ENOMEM in the teardown path.
5255 DBG("UST app notify socket unregister %d", sock
);
5258 * Lookup application by notify socket. If this fails, this means that the
5259 * hash table delete has already been done by the application
5260 * unregistration process so we can safely close the notify socket in a
5263 app
= find_app_by_notify_sock(sock
);
5268 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5271 * Whatever happens here either we fail or succeed, in both cases we have
5272 * to close the socket after a grace period to continue to the call RCU
5273 * here. If the deletion is successful, the application is not visible
5274 * anymore by other threads and is it fails it means that it was already
5275 * deleted from the hash table so either way we just have to close the
5278 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5284 * Close socket after a grace period to avoid for the socket to be reused
5285 * before the application object is freed creating potential race between
5286 * threads trying to add unique in the global hash table.
5289 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5294 * Destroy a ust app data structure and free its memory.
5296 void ust_app_destroy(struct ust_app
*app
)
5302 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5306 * Take a snapshot for a given UST session. The snapshot is sent to the given
5309 * Return 0 on success or else a negative value.
5311 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5312 struct snapshot_output
*output
, int wait
,
5313 uint64_t nb_packets_per_stream
)
5316 unsigned int snapshot_done
= 0;
5317 struct lttng_ht_iter iter
;
5318 struct ust_app
*app
;
5319 char pathname
[PATH_MAX
];
5326 switch (usess
->buffer_type
) {
5327 case LTTNG_BUFFER_PER_UID
:
5329 struct buffer_reg_uid
*reg
;
5331 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5332 struct buffer_reg_channel
*reg_chan
;
5333 struct consumer_socket
*socket
;
5335 /* Get consumer socket to use to push the metadata.*/
5336 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5343 memset(pathname
, 0, sizeof(pathname
));
5344 ret
= snprintf(pathname
, sizeof(pathname
),
5345 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5346 reg
->uid
, reg
->bits_per_long
);
5348 PERROR("snprintf snapshot path");
5352 /* Add the UST default trace dir to path. */
5353 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5354 reg_chan
, node
.node
) {
5355 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5356 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5357 nb_packets_per_stream
);
5362 ret
= consumer_snapshot_channel(socket
,
5363 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5364 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
5372 case LTTNG_BUFFER_PER_PID
:
5374 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5375 struct consumer_socket
*socket
;
5376 struct lttng_ht_iter chan_iter
;
5377 struct ust_app_channel
*ua_chan
;
5378 struct ust_app_session
*ua_sess
;
5379 struct ust_registry_session
*registry
;
5381 ua_sess
= lookup_session_by_app(usess
, app
);
5383 /* Session not associated with this app. */
5387 /* Get the right consumer socket for the application. */
5388 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5395 /* Add the UST default trace dir to path. */
5396 memset(pathname
, 0, sizeof(pathname
));
5397 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5400 PERROR("snprintf snapshot path");
5404 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5405 ua_chan
, node
.node
) {
5406 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
5407 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
5408 nb_packets_per_stream
);
5414 registry
= get_session_registry(ua_sess
);
5416 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
5417 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
5430 if (!snapshot_done
) {
5432 * If no snapshot was made and we are not in the error path, this means
5433 * that there are no buffers thus no (prior) application to snapshot
5434 * data from so we have simply NO data.
5445 * Return the size taken by one more packet per stream.
5447 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
5448 uint64_t cur_nr_packets
)
5450 uint64_t tot_size
= 0;
5451 struct ust_app
*app
;
5452 struct lttng_ht_iter iter
;
5456 switch (usess
->buffer_type
) {
5457 case LTTNG_BUFFER_PER_UID
:
5459 struct buffer_reg_uid
*reg
;
5461 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5462 struct buffer_reg_channel
*reg_chan
;
5465 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5466 reg_chan
, node
.node
) {
5467 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
5469 * Don't take channel into account if we
5470 * already grab all its packets.
5474 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
5480 case LTTNG_BUFFER_PER_PID
:
5483 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5484 struct ust_app_channel
*ua_chan
;
5485 struct ust_app_session
*ua_sess
;
5486 struct lttng_ht_iter chan_iter
;
5488 ua_sess
= lookup_session_by_app(usess
, app
);
5490 /* Session not associated with this app. */
5494 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5495 ua_chan
, node
.node
) {
5496 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
5498 * Don't take channel into account if we
5499 * already grab all its packets.
5503 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;