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.
26 #include <sys/types.h>
28 #include <urcu/compiler.h>
29 #include <lttng/ust-error.h>
32 #include <common/common.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
35 #include "buffer-registry.h"
37 #include "health-sessiond.h"
39 #include "ust-consumer.h"
44 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
46 /* Next available channel key. Access under next_channel_key_lock. */
47 static uint64_t _next_channel_key
;
48 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
50 /* Next available session ID. Access under next_session_id_lock. */
51 static uint64_t _next_session_id
;
52 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
55 * Return the incremented value of next_channel_key.
57 static uint64_t get_next_channel_key(void)
61 pthread_mutex_lock(&next_channel_key_lock
);
62 ret
= ++_next_channel_key
;
63 pthread_mutex_unlock(&next_channel_key_lock
);
68 * Return the atomically incremented value of next_session_id.
70 static uint64_t get_next_session_id(void)
74 pthread_mutex_lock(&next_session_id_lock
);
75 ret
= ++_next_session_id
;
76 pthread_mutex_unlock(&next_session_id_lock
);
80 static void copy_channel_attr_to_ustctl(
81 struct ustctl_consumer_channel_attr
*attr
,
82 struct lttng_ust_channel_attr
*uattr
)
84 /* Copy event attributes since the layout is different. */
85 attr
->subbuf_size
= uattr
->subbuf_size
;
86 attr
->num_subbuf
= uattr
->num_subbuf
;
87 attr
->overwrite
= uattr
->overwrite
;
88 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
89 attr
->read_timer_interval
= uattr
->read_timer_interval
;
90 attr
->output
= uattr
->output
;
94 * Match function for the hash table lookup.
96 * It matches an ust app event based on three attributes which are the event
97 * name, the filter bytecode and the loglevel.
99 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
101 struct ust_app_event
*event
;
102 const struct ust_app_ht_key
*key
;
107 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
110 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
113 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
117 /* Event loglevel. */
118 if (event
->attr
.loglevel
!= key
->loglevel
) {
119 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
120 && key
->loglevel
== 0 && event
->attr
.loglevel
== -1) {
122 * Match is accepted. This is because on event creation, the
123 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
124 * -1 are accepted for this loglevel type since 0 is the one set by
125 * the API when receiving an enable event.
132 /* One of the filters is NULL, fail. */
133 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
137 if (key
->filter
&& event
->filter
) {
138 /* Both filters exists, check length followed by the bytecode. */
139 if (event
->filter
->len
!= key
->filter
->len
||
140 memcmp(event
->filter
->data
, key
->filter
->data
,
141 event
->filter
->len
) != 0) {
146 /* One of the exclusions is NULL, fail. */
147 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
151 if (key
->exclusion
&& event
->exclusion
) {
152 /* Both exclusions exists, check count followed by the names. */
153 if (event
->exclusion
->count
!= key
->exclusion
->count
||
154 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
155 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
169 * Unique add of an ust app event in the given ht. This uses the custom
170 * ht_match_ust_app_event match function and the event name as hash.
172 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
173 struct ust_app_event
*event
)
175 struct cds_lfht_node
*node_ptr
;
176 struct ust_app_ht_key key
;
180 assert(ua_chan
->events
);
183 ht
= ua_chan
->events
;
184 key
.name
= event
->attr
.name
;
185 key
.filter
= event
->filter
;
186 key
.loglevel
= event
->attr
.loglevel
;
187 key
.exclusion
= event
->exclusion
;
189 node_ptr
= cds_lfht_add_unique(ht
->ht
,
190 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
191 ht_match_ust_app_event
, &key
, &event
->node
.node
);
192 assert(node_ptr
== &event
->node
.node
);
196 * Close the notify socket from the given RCU head object. This MUST be called
197 * through a call_rcu().
199 static void close_notify_sock_rcu(struct rcu_head
*head
)
202 struct ust_app_notify_sock_obj
*obj
=
203 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
205 /* Must have a valid fd here. */
206 assert(obj
->fd
>= 0);
208 ret
= close(obj
->fd
);
210 ERR("close notify sock %d RCU", obj
->fd
);
212 lttng_fd_put(LTTNG_FD_APPS
, 1);
218 * Return the session registry according to the buffer type of the given
221 * A registry per UID object MUST exists before calling this function or else
222 * it assert() if not found. RCU read side lock must be acquired.
224 static struct ust_registry_session
*get_session_registry(
225 struct ust_app_session
*ua_sess
)
227 struct ust_registry_session
*registry
= NULL
;
231 switch (ua_sess
->buffer_type
) {
232 case LTTNG_BUFFER_PER_PID
:
234 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
238 registry
= reg_pid
->registry
->reg
.ust
;
241 case LTTNG_BUFFER_PER_UID
:
243 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
244 ua_sess
->tracing_id
, ua_sess
->bits_per_long
, ua_sess
->uid
);
248 registry
= reg_uid
->registry
->reg
.ust
;
260 * Delete ust context safely. RCU read lock must be held before calling
264 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
272 pthread_mutex_lock(&app
->sock_lock
);
273 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
274 pthread_mutex_unlock(&app
->sock_lock
);
275 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
276 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
277 sock
, ua_ctx
->obj
->handle
, ret
);
285 * Delete ust app event safely. RCU read lock must be held before calling
289 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
296 free(ua_event
->filter
);
297 if (ua_event
->exclusion
!= NULL
)
298 free(ua_event
->exclusion
);
299 if (ua_event
->obj
!= NULL
) {
300 pthread_mutex_lock(&app
->sock_lock
);
301 ret
= ustctl_release_object(sock
, ua_event
->obj
);
302 pthread_mutex_unlock(&app
->sock_lock
);
303 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
304 ERR("UST app sock %d release event obj failed with ret %d",
313 * Release ust data object of the given stream.
315 * Return 0 on success or else a negative value.
317 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
325 pthread_mutex_lock(&app
->sock_lock
);
326 ret
= ustctl_release_object(sock
, stream
->obj
);
327 pthread_mutex_unlock(&app
->sock_lock
);
328 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
329 ERR("UST app sock %d release stream obj failed with ret %d",
332 lttng_fd_put(LTTNG_FD_APPS
, 2);
340 * Delete ust app stream safely. RCU read lock must be held before calling
344 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
349 (void) release_ust_app_stream(sock
, stream
, app
);
354 * We need to execute ht_destroy outside of RCU read-side critical
355 * section and outside of call_rcu thread, so we postpone its execution
356 * using ht_cleanup_push. It is simpler than to change the semantic of
357 * the many callers of delete_ust_app_session().
360 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
362 struct ust_app_channel
*ua_chan
=
363 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
365 ht_cleanup_push(ua_chan
->ctx
);
366 ht_cleanup_push(ua_chan
->events
);
371 * Delete ust app channel safely. RCU read lock must be held before calling
375 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
379 struct lttng_ht_iter iter
;
380 struct ust_app_event
*ua_event
;
381 struct ust_app_ctx
*ua_ctx
;
382 struct ust_app_stream
*stream
, *stmp
;
383 struct ust_registry_session
*registry
;
387 DBG3("UST app deleting channel %s", ua_chan
->name
);
390 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
391 cds_list_del(&stream
->list
);
392 delete_ust_app_stream(sock
, stream
, app
);
396 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
397 cds_list_del(&ua_ctx
->list
);
398 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
400 delete_ust_app_ctx(sock
, ua_ctx
, app
);
404 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
406 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
408 delete_ust_app_event(sock
, ua_event
, app
);
411 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
412 /* Wipe and free registry from session registry. */
413 registry
= get_session_registry(ua_chan
->session
);
415 ust_registry_channel_del_free(registry
, ua_chan
->key
);
419 if (ua_chan
->obj
!= NULL
) {
420 /* Remove channel from application UST object descriptor. */
421 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
422 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
424 pthread_mutex_lock(&app
->sock_lock
);
425 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
426 pthread_mutex_unlock(&app
->sock_lock
);
427 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
428 ERR("UST app sock %d release channel obj failed with ret %d",
431 lttng_fd_put(LTTNG_FD_APPS
, 1);
434 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
437 int ust_app_register_done(struct ust_app
*app
)
441 pthread_mutex_lock(&app
->sock_lock
);
442 ret
= ustctl_register_done(app
->sock
);
443 pthread_mutex_unlock(&app
->sock_lock
);
447 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
452 pthread_mutex_lock(&app
->sock_lock
);
457 ret
= ustctl_release_object(sock
, data
);
459 pthread_mutex_unlock(&app
->sock_lock
);
465 * Push metadata to consumer socket.
467 * RCU read-side lock must be held to guarantee existance of socket.
468 * Must be called with the ust app session lock held.
469 * Must be called with the registry lock held.
471 * On success, return the len of metadata pushed or else a negative value.
472 * Returning a -EPIPE return value means we could not send the metadata,
473 * but it can be caused by recoverable errors (e.g. the application has
474 * terminated concurrently).
476 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
477 struct consumer_socket
*socket
, int send_zero_data
)
480 char *metadata_str
= NULL
;
481 size_t len
, offset
, new_metadata_len_sent
;
483 uint64_t metadata_key
;
488 metadata_key
= registry
->metadata_key
;
491 * Means that no metadata was assigned to the session. This can
492 * happens if no start has been done previously.
499 * On a push metadata error either the consumer is dead or the
500 * metadata channel has been destroyed because its endpoint
501 * might have died (e.g: relayd), or because the application has
502 * exited. If so, the metadata closed flag is set to 1 so we
503 * deny pushing metadata again which is not valid anymore on the
506 if (registry
->metadata_closed
) {
510 offset
= registry
->metadata_len_sent
;
511 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
512 new_metadata_len_sent
= registry
->metadata_len
;
514 DBG3("No metadata to push for metadata key %" PRIu64
,
515 registry
->metadata_key
);
517 if (send_zero_data
) {
518 DBG("No metadata to push");
524 /* Allocate only what we have to send. */
525 metadata_str
= zmalloc(len
);
527 PERROR("zmalloc ust app metadata string");
531 /* Copy what we haven't sent out. */
532 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
535 pthread_mutex_unlock(®istry
->lock
);
537 * We need to unlock the registry while we push metadata to
538 * break a circular dependency between the consumerd metadata
539 * lock and the sessiond registry lock. Indeed, pushing metadata
540 * to the consumerd awaits that it gets pushed all the way to
541 * relayd, but doing so requires grabbing the metadata lock. If
542 * a concurrent metadata request is being performed by
543 * consumerd, this can try to grab the registry lock on the
544 * sessiond while holding the metadata lock on the consumer
545 * daemon. Those push and pull schemes are performed on two
546 * different bidirectionnal communication sockets.
548 ret
= consumer_push_metadata(socket
, metadata_key
,
549 metadata_str
, len
, offset
);
550 pthread_mutex_lock(®istry
->lock
);
553 * There is an acceptable race here between the registry
554 * metadata key assignment and the creation on the
555 * consumer. The session daemon can concurrently push
556 * metadata for this registry while being created on the
557 * consumer since the metadata key of the registry is
558 * assigned *before* it is setup to avoid the consumer
559 * to ask for metadata that could possibly be not found
560 * in the session daemon.
562 * The metadata will get pushed either by the session
563 * being stopped or the consumer requesting metadata if
564 * that race is triggered.
566 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
569 ERR("Error pushing metadata to consumer");
575 * Metadata may have been concurrently pushed, since
576 * we're not holding the registry lock while pushing to
577 * consumer. This is handled by the fact that we send
578 * the metadata content, size, and the offset at which
579 * that metadata belongs. This may arrive out of order
580 * on the consumer side, and the consumer is able to
581 * deal with overlapping fragments. The consumer
582 * supports overlapping fragments, which must be
583 * contiguous starting from offset 0. We keep the
584 * largest metadata_len_sent value of the concurrent
587 registry
->metadata_len_sent
=
588 max_t(size_t, registry
->metadata_len_sent
,
589 new_metadata_len_sent
);
598 * On error, flag the registry that the metadata is
599 * closed. We were unable to push anything and this
600 * means that either the consumer is not responding or
601 * the metadata cache has been destroyed on the
604 registry
->metadata_closed
= 1;
612 * For a given application and session, push metadata to consumer.
613 * Either sock or consumer is required : if sock is NULL, the default
614 * socket to send the metadata is retrieved from consumer, if sock
615 * is not NULL we use it to send the metadata.
616 * RCU read-side lock must be held while calling this function,
617 * therefore ensuring existance of registry. It also ensures existance
618 * of socket throughout this function.
620 * Return 0 on success else a negative error.
621 * Returning a -EPIPE return value means we could not send the metadata,
622 * but it can be caused by recoverable errors (e.g. the application has
623 * terminated concurrently).
625 static int push_metadata(struct ust_registry_session
*registry
,
626 struct consumer_output
*consumer
)
630 struct consumer_socket
*socket
;
635 pthread_mutex_lock(®istry
->lock
);
636 if (registry
->metadata_closed
) {
641 /* Get consumer socket to use to push the metadata.*/
642 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
649 ret
= ust_app_push_metadata(registry
, socket
, 0);
654 pthread_mutex_unlock(®istry
->lock
);
658 pthread_mutex_unlock(®istry
->lock
);
663 * Send to the consumer a close metadata command for the given session. Once
664 * done, the metadata channel is deleted and the session metadata pointer is
665 * nullified. The session lock MUST be held unless the application is
666 * in the destroy path.
668 * Return 0 on success else a negative value.
670 static int close_metadata(struct ust_registry_session
*registry
,
671 struct consumer_output
*consumer
)
674 struct consumer_socket
*socket
;
681 pthread_mutex_lock(®istry
->lock
);
683 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
688 /* Get consumer socket to use to push the metadata.*/
689 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
696 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
703 * Metadata closed. Even on error this means that the consumer is not
704 * responding or not found so either way a second close should NOT be emit
707 registry
->metadata_closed
= 1;
709 pthread_mutex_unlock(®istry
->lock
);
715 * We need to execute ht_destroy outside of RCU read-side critical
716 * section and outside of call_rcu thread, so we postpone its execution
717 * using ht_cleanup_push. It is simpler than to change the semantic of
718 * the many callers of delete_ust_app_session().
721 void delete_ust_app_session_rcu(struct rcu_head
*head
)
723 struct ust_app_session
*ua_sess
=
724 caa_container_of(head
, struct ust_app_session
, rcu_head
);
726 ht_cleanup_push(ua_sess
->channels
);
731 * Delete ust app session safely. RCU read lock must be held before calling
735 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
739 struct lttng_ht_iter iter
;
740 struct ust_app_channel
*ua_chan
;
741 struct ust_registry_session
*registry
;
745 pthread_mutex_lock(&ua_sess
->lock
);
747 assert(!ua_sess
->deleted
);
748 ua_sess
->deleted
= true;
750 registry
= get_session_registry(ua_sess
);
752 /* Push metadata for application before freeing the application. */
753 (void) push_metadata(registry
, ua_sess
->consumer
);
756 * Don't ask to close metadata for global per UID buffers. Close
757 * metadata only on destroy trace session in this case. Also, the
758 * previous push metadata could have flag the metadata registry to
759 * close so don't send a close command if closed.
761 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
762 /* And ask to close it for this session registry. */
763 (void) close_metadata(registry
, ua_sess
->consumer
);
767 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
769 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
771 delete_ust_app_channel(sock
, ua_chan
, app
);
774 /* In case of per PID, the registry is kept in the session. */
775 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
776 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
778 buffer_reg_pid_remove(reg_pid
);
779 buffer_reg_pid_destroy(reg_pid
);
783 if (ua_sess
->handle
!= -1) {
784 pthread_mutex_lock(&app
->sock_lock
);
785 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
786 pthread_mutex_unlock(&app
->sock_lock
);
787 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
788 ERR("UST app sock %d release session handle failed with ret %d",
792 pthread_mutex_unlock(&ua_sess
->lock
);
794 consumer_output_put(ua_sess
->consumer
);
796 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
800 * Delete a traceable application structure from the global list. Never call
801 * this function outside of a call_rcu call.
803 * RCU read side lock should _NOT_ be held when calling this function.
806 void delete_ust_app(struct ust_app
*app
)
809 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
811 /* Delete ust app sessions info */
816 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
818 /* Free every object in the session and the session. */
820 delete_ust_app_session(sock
, ua_sess
, app
);
824 ht_cleanup_push(app
->sessions
);
825 ht_cleanup_push(app
->ust_objd
);
828 * Wait until we have deleted the application from the sock hash table
829 * before closing this socket, otherwise an application could re-use the
830 * socket ID and race with the teardown, using the same hash table entry.
832 * It's OK to leave the close in call_rcu. We want it to stay unique for
833 * all RCU readers that could run concurrently with unregister app,
834 * therefore we _need_ to only close that socket after a grace period. So
835 * it should stay in this RCU callback.
837 * This close() is a very important step of the synchronization model so
838 * every modification to this function must be carefully reviewed.
844 lttng_fd_put(LTTNG_FD_APPS
, 1);
846 DBG2("UST app pid %d deleted", app
->pid
);
851 * URCU intermediate call to delete an UST app.
854 void delete_ust_app_rcu(struct rcu_head
*head
)
856 struct lttng_ht_node_ulong
*node
=
857 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
858 struct ust_app
*app
=
859 caa_container_of(node
, struct ust_app
, pid_n
);
861 DBG3("Call RCU deleting app PID %d", app
->pid
);
866 * Delete the session from the application ht and delete the data structure by
867 * freeing every object inside and releasing them.
869 static void destroy_app_session(struct ust_app
*app
,
870 struct ust_app_session
*ua_sess
)
873 struct lttng_ht_iter iter
;
878 iter
.iter
.node
= &ua_sess
->node
.node
;
879 ret
= lttng_ht_del(app
->sessions
, &iter
);
881 /* Already scheduled for teardown. */
885 /* Once deleted, free the data structure. */
886 delete_ust_app_session(app
->sock
, ua_sess
, app
);
893 * Alloc new UST app session.
896 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
898 struct ust_app_session
*ua_sess
;
900 /* Init most of the default value by allocating and zeroing */
901 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
902 if (ua_sess
== NULL
) {
907 ua_sess
->handle
= -1;
908 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
909 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
910 pthread_mutex_init(&ua_sess
->lock
, NULL
);
919 * Alloc new UST app channel.
922 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
923 struct ust_app_session
*ua_sess
,
924 struct lttng_ust_channel_attr
*attr
)
926 struct ust_app_channel
*ua_chan
;
928 /* Init most of the default value by allocating and zeroing */
929 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
930 if (ua_chan
== NULL
) {
935 /* Setup channel name */
936 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
937 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
939 ua_chan
->enabled
= 1;
940 ua_chan
->handle
= -1;
941 ua_chan
->session
= ua_sess
;
942 ua_chan
->key
= get_next_channel_key();
943 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
944 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
945 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
947 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
948 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
950 /* Copy attributes */
952 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
953 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
954 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
955 ua_chan
->attr
.overwrite
= attr
->overwrite
;
956 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
957 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
958 ua_chan
->attr
.output
= attr
->output
;
960 /* By default, the channel is a per cpu channel. */
961 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
963 DBG3("UST app channel %s allocated", ua_chan
->name
);
972 * Allocate and initialize a UST app stream.
974 * Return newly allocated stream pointer or NULL on error.
976 struct ust_app_stream
*ust_app_alloc_stream(void)
978 struct ust_app_stream
*stream
= NULL
;
980 stream
= zmalloc(sizeof(*stream
));
981 if (stream
== NULL
) {
982 PERROR("zmalloc ust app stream");
986 /* Zero could be a valid value for a handle so flag it to -1. */
994 * Alloc new UST app event.
997 struct ust_app_event
*alloc_ust_app_event(char *name
,
998 struct lttng_ust_event
*attr
)
1000 struct ust_app_event
*ua_event
;
1002 /* Init most of the default value by allocating and zeroing */
1003 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1004 if (ua_event
== NULL
) {
1009 ua_event
->enabled
= 1;
1010 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1011 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1012 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1014 /* Copy attributes */
1016 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1019 DBG3("UST app event %s allocated", ua_event
->name
);
1028 * Alloc new UST app context.
1031 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
1033 struct ust_app_ctx
*ua_ctx
;
1035 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1036 if (ua_ctx
== NULL
) {
1040 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1043 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1046 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1053 * Allocate a filter and copy the given original filter.
1055 * Return allocated filter or NULL on error.
1057 static struct lttng_ust_filter_bytecode
*alloc_copy_ust_app_filter(
1058 struct lttng_ust_filter_bytecode
*orig_f
)
1060 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1062 /* Copy filter bytecode */
1063 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1065 PERROR("zmalloc alloc ust app filter");
1069 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1076 * Find an ust_app using the sock and return it. RCU read side lock must be
1077 * held before calling this helper function.
1079 struct ust_app
*ust_app_find_by_sock(int sock
)
1081 struct lttng_ht_node_ulong
*node
;
1082 struct lttng_ht_iter iter
;
1084 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1085 node
= lttng_ht_iter_get_node_ulong(&iter
);
1087 DBG2("UST app find by sock %d not found", sock
);
1091 return caa_container_of(node
, struct ust_app
, sock_n
);
1098 * Find an ust_app using the notify sock and return it. RCU read side lock must
1099 * be held before calling this helper function.
1101 static struct ust_app
*find_app_by_notify_sock(int sock
)
1103 struct lttng_ht_node_ulong
*node
;
1104 struct lttng_ht_iter iter
;
1106 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1108 node
= lttng_ht_iter_get_node_ulong(&iter
);
1110 DBG2("UST app find by notify sock %d not found", sock
);
1114 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1121 * Lookup for an ust app event based on event name, filter bytecode and the
1124 * Return an ust_app_event object or NULL on error.
1126 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1127 char *name
, struct lttng_ust_filter_bytecode
*filter
, int loglevel
,
1128 const struct lttng_event_exclusion
*exclusion
)
1130 struct lttng_ht_iter iter
;
1131 struct lttng_ht_node_str
*node
;
1132 struct ust_app_event
*event
= NULL
;
1133 struct ust_app_ht_key key
;
1138 /* Setup key for event lookup. */
1140 key
.filter
= filter
;
1141 key
.loglevel
= loglevel
;
1142 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1143 key
.exclusion
= (struct lttng_ust_event_exclusion
*)exclusion
;
1145 /* Lookup using the event name as hash and a custom match fct. */
1146 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1147 ht_match_ust_app_event
, &key
, &iter
.iter
);
1148 node
= lttng_ht_iter_get_node_str(&iter
);
1153 event
= caa_container_of(node
, struct ust_app_event
, node
);
1160 * Create the channel context on the tracer.
1162 * Called with UST app session lock held.
1165 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1166 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1170 health_code_update();
1172 pthread_mutex_lock(&app
->sock_lock
);
1173 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1174 ua_chan
->obj
, &ua_ctx
->obj
);
1175 pthread_mutex_unlock(&app
->sock_lock
);
1177 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1178 ERR("UST app create channel context failed for app (pid: %d) "
1179 "with ret %d", app
->pid
, ret
);
1182 * This is normal behavior, an application can die during the
1183 * creation process. Don't report an error so the execution can
1184 * continue normally.
1187 DBG3("UST app disable event failed. Application is dead.");
1192 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1194 DBG2("UST app context handle %d created successfully for channel %s",
1195 ua_ctx
->handle
, ua_chan
->name
);
1198 health_code_update();
1203 * Set the filter on the tracer.
1206 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1207 struct ust_app
*app
)
1211 health_code_update();
1213 if (!ua_event
->filter
) {
1218 pthread_mutex_lock(&app
->sock_lock
);
1219 ret
= ustctl_set_filter(app
->sock
, ua_event
->filter
,
1221 pthread_mutex_unlock(&app
->sock_lock
);
1223 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1224 ERR("UST app event %s filter failed for app (pid: %d) "
1225 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1228 * This is normal behavior, an application can die during the
1229 * creation process. Don't report an error so the execution can
1230 * continue normally.
1233 DBG3("UST app filter event failed. Application is dead.");
1238 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1241 health_code_update();
1246 * Set event exclusions on the tracer.
1249 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1250 struct ust_app
*app
)
1254 health_code_update();
1256 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1261 pthread_mutex_lock(&app
->sock_lock
);
1262 ret
= ustctl_set_exclusion(app
->sock
, ua_event
->exclusion
, ua_event
->obj
);
1263 pthread_mutex_unlock(&app
->sock_lock
);
1265 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1266 ERR("UST app event %s exclusions failed for app (pid: %d) "
1267 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1270 * This is normal behavior, an application can die during the
1271 * creation process. Don't report an error so the execution can
1272 * continue normally.
1275 DBG3("UST app event exclusion failed. Application is dead.");
1280 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1283 health_code_update();
1288 * Disable the specified event on to UST tracer for the UST session.
1290 static int disable_ust_event(struct ust_app
*app
,
1291 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1295 health_code_update();
1297 pthread_mutex_lock(&app
->sock_lock
);
1298 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1299 pthread_mutex_unlock(&app
->sock_lock
);
1301 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1302 ERR("UST app event %s disable failed for app (pid: %d) "
1303 "and session handle %d with ret %d",
1304 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1307 * This is normal behavior, an application can die during the
1308 * creation process. Don't report an error so the execution can
1309 * continue normally.
1312 DBG3("UST app disable event failed. Application is dead.");
1317 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1318 ua_event
->attr
.name
, app
->pid
);
1321 health_code_update();
1326 * Disable the specified channel on to UST tracer for the UST session.
1328 static int disable_ust_channel(struct ust_app
*app
,
1329 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1333 health_code_update();
1335 pthread_mutex_lock(&app
->sock_lock
);
1336 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1337 pthread_mutex_unlock(&app
->sock_lock
);
1339 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1340 ERR("UST app channel %s disable failed for app (pid: %d) "
1341 "and session handle %d with ret %d",
1342 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1345 * This is normal behavior, an application can die during the
1346 * creation process. Don't report an error so the execution can
1347 * continue normally.
1350 DBG3("UST app disable channel failed. Application is dead.");
1355 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1356 ua_chan
->name
, app
->pid
);
1359 health_code_update();
1364 * Enable the specified channel on to UST tracer for the UST session.
1366 static int enable_ust_channel(struct ust_app
*app
,
1367 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1371 health_code_update();
1373 pthread_mutex_lock(&app
->sock_lock
);
1374 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1375 pthread_mutex_unlock(&app
->sock_lock
);
1377 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1378 ERR("UST app channel %s enable failed for app (pid: %d) "
1379 "and session handle %d with ret %d",
1380 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1383 * This is normal behavior, an application can die during the
1384 * creation process. Don't report an error so the execution can
1385 * continue normally.
1388 DBG3("UST app enable channel failed. Application is dead.");
1393 ua_chan
->enabled
= 1;
1395 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1396 ua_chan
->name
, app
->pid
);
1399 health_code_update();
1404 * Enable the specified event on to UST tracer for the UST session.
1406 static int enable_ust_event(struct ust_app
*app
,
1407 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1411 health_code_update();
1413 pthread_mutex_lock(&app
->sock_lock
);
1414 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1415 pthread_mutex_unlock(&app
->sock_lock
);
1417 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1418 ERR("UST app event %s enable failed for app (pid: %d) "
1419 "and session handle %d with ret %d",
1420 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1423 * This is normal behavior, an application can die during the
1424 * creation process. Don't report an error so the execution can
1425 * continue normally.
1428 DBG3("UST app enable event failed. Application is dead.");
1433 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1434 ua_event
->attr
.name
, app
->pid
);
1437 health_code_update();
1442 * Send channel and stream buffer to application.
1444 * Return 0 on success. On error, a negative value is returned.
1446 static int send_channel_pid_to_ust(struct ust_app
*app
,
1447 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1450 struct ust_app_stream
*stream
, *stmp
;
1456 health_code_update();
1458 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1461 /* Send channel to the application. */
1462 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1463 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1464 ret
= -ENOTCONN
; /* Caused by app exiting. */
1466 } else if (ret
< 0) {
1470 health_code_update();
1472 /* Send all streams to application. */
1473 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1474 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1475 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1476 ret
= -ENOTCONN
; /* Caused by app exiting. */
1478 } else if (ret
< 0) {
1481 /* We don't need the stream anymore once sent to the tracer. */
1482 cds_list_del(&stream
->list
);
1483 delete_ust_app_stream(-1, stream
, app
);
1485 /* Flag the channel that it is sent to the application. */
1486 ua_chan
->is_sent
= 1;
1489 health_code_update();
1494 * Create the specified event onto the UST tracer for a UST session.
1496 * Should be called with session mutex held.
1499 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1500 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1504 health_code_update();
1506 /* Create UST event on tracer */
1507 pthread_mutex_lock(&app
->sock_lock
);
1508 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1510 pthread_mutex_unlock(&app
->sock_lock
);
1512 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1513 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1514 ua_event
->attr
.name
, app
->pid
, ret
);
1517 * This is normal behavior, an application can die during the
1518 * creation process. Don't report an error so the execution can
1519 * continue normally.
1522 DBG3("UST app create event failed. Application is dead.");
1527 ua_event
->handle
= ua_event
->obj
->handle
;
1529 DBG2("UST app event %s created successfully for pid:%d",
1530 ua_event
->attr
.name
, app
->pid
);
1532 health_code_update();
1534 /* Set filter if one is present. */
1535 if (ua_event
->filter
) {
1536 ret
= set_ust_event_filter(ua_event
, app
);
1542 /* Set exclusions for the event */
1543 if (ua_event
->exclusion
) {
1544 ret
= set_ust_event_exclusion(ua_event
, app
);
1550 /* If event not enabled, disable it on the tracer */
1551 if (ua_event
->enabled
) {
1553 * We now need to explicitly enable the event, since it
1554 * is now disabled at creation.
1556 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1559 * If we hit an EPERM, something is wrong with our enable call. If
1560 * we get an EEXIST, there is a problem on the tracer side since we
1564 case -LTTNG_UST_ERR_PERM
:
1565 /* Code flow problem */
1567 case -LTTNG_UST_ERR_EXIST
:
1568 /* It's OK for our use case. */
1577 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1580 * If we hit an EPERM, something is wrong with our disable call. If
1581 * we get an EEXIST, there is a problem on the tracer side since we
1585 case -LTTNG_UST_ERR_PERM
:
1586 /* Code flow problem */
1588 case -LTTNG_UST_ERR_EXIST
:
1589 /* It's OK for our use case. */
1600 health_code_update();
1605 * Copy data between an UST app event and a LTT event.
1607 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1608 struct ltt_ust_event
*uevent
)
1610 size_t exclusion_alloc_size
;
1612 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1613 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1615 ua_event
->enabled
= uevent
->enabled
;
1617 /* Copy event attributes */
1618 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1620 /* Copy filter bytecode */
1621 if (uevent
->filter
) {
1622 ua_event
->filter
= alloc_copy_ust_app_filter(uevent
->filter
);
1623 /* Filter might be NULL here in case of ENONEM. */
1626 /* Copy exclusion data */
1627 if (uevent
->exclusion
) {
1628 exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1629 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1630 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1631 if (ua_event
->exclusion
== NULL
) {
1634 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1635 exclusion_alloc_size
);
1641 * Copy data between an UST app channel and a LTT channel.
1643 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1644 struct ltt_ust_channel
*uchan
)
1646 struct lttng_ht_iter iter
;
1647 struct ltt_ust_event
*uevent
;
1648 struct ltt_ust_context
*uctx
;
1649 struct ust_app_event
*ua_event
;
1650 struct ust_app_ctx
*ua_ctx
;
1652 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1654 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1655 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1657 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1658 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1660 /* Copy event attributes since the layout is different. */
1661 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1662 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1663 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1664 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1665 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1666 ua_chan
->attr
.output
= uchan
->attr
.output
;
1668 * Note that the attribute channel type is not set since the channel on the
1669 * tracing registry side does not have this information.
1672 ua_chan
->enabled
= uchan
->enabled
;
1673 ua_chan
->tracing_channel_id
= uchan
->id
;
1675 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1676 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1677 if (ua_ctx
== NULL
) {
1680 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1681 (unsigned long) ua_ctx
->ctx
.ctx
);
1682 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1683 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1686 /* Copy all events from ltt ust channel to ust app channel */
1687 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1688 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1689 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1690 if (ua_event
== NULL
) {
1691 DBG2("UST event %s not found on shadow copy channel",
1693 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1694 if (ua_event
== NULL
) {
1697 shadow_copy_event(ua_event
, uevent
);
1698 add_unique_ust_app_event(ua_chan
, ua_event
);
1702 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1706 * Copy data between a UST app session and a regular LTT session.
1708 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1709 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1711 struct lttng_ht_node_str
*ua_chan_node
;
1712 struct lttng_ht_iter iter
;
1713 struct ltt_ust_channel
*uchan
;
1714 struct ust_app_channel
*ua_chan
;
1716 struct tm
*timeinfo
;
1720 /* Get date and time for unique app path */
1722 timeinfo
= localtime(&rawtime
);
1723 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1725 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1727 ua_sess
->tracing_id
= usess
->id
;
1728 ua_sess
->id
= get_next_session_id();
1729 ua_sess
->uid
= app
->uid
;
1730 ua_sess
->gid
= app
->gid
;
1731 ua_sess
->euid
= usess
->uid
;
1732 ua_sess
->egid
= usess
->gid
;
1733 ua_sess
->buffer_type
= usess
->buffer_type
;
1734 ua_sess
->bits_per_long
= app
->bits_per_long
;
1736 /* There is only one consumer object per session possible. */
1737 consumer_output_get(usess
->consumer
);
1738 ua_sess
->consumer
= usess
->consumer
;
1740 ua_sess
->output_traces
= usess
->output_traces
;
1741 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1742 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1743 &usess
->metadata_attr
);
1745 switch (ua_sess
->buffer_type
) {
1746 case LTTNG_BUFFER_PER_PID
:
1747 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1748 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1751 case LTTNG_BUFFER_PER_UID
:
1752 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1753 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1760 PERROR("asprintf UST shadow copy session");
1765 /* Iterate over all channels in global domain. */
1766 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1768 struct lttng_ht_iter uiter
;
1770 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1771 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1772 if (ua_chan_node
!= NULL
) {
1773 /* Session exist. Contiuing. */
1777 DBG2("Channel %s not found on shadow session copy, creating it",
1779 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1780 if (ua_chan
== NULL
) {
1781 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1784 shadow_copy_channel(ua_chan
, uchan
);
1786 * The concept of metadata channel does not exist on the tracing
1787 * registry side of the session daemon so this can only be a per CPU
1788 * channel and not metadata.
1790 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1792 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1797 consumer_output_put(ua_sess
->consumer
);
1801 * Lookup sesison wrapper.
1804 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1805 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1807 /* Get right UST app session from app */
1808 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1812 * Return ust app session from the app session hashtable using the UST session
1815 static struct ust_app_session
*lookup_session_by_app(
1816 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1818 struct lttng_ht_iter iter
;
1819 struct lttng_ht_node_u64
*node
;
1821 __lookup_session_by_app(usess
, app
, &iter
);
1822 node
= lttng_ht_iter_get_node_u64(&iter
);
1827 return caa_container_of(node
, struct ust_app_session
, node
);
1834 * Setup buffer registry per PID for the given session and application. If none
1835 * is found, a new one is created, added to the global registry and
1836 * initialized. If regp is valid, it's set with the newly created object.
1838 * Return 0 on success or else a negative value.
1840 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1841 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1844 struct buffer_reg_pid
*reg_pid
;
1851 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1854 * This is the create channel path meaning that if there is NO
1855 * registry available, we have to create one for this session.
1857 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
);
1861 buffer_reg_pid_add(reg_pid
);
1866 /* Initialize registry. */
1867 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1868 app
->bits_per_long
, app
->uint8_t_alignment
,
1869 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1870 app
->uint64_t_alignment
, app
->long_alignment
,
1871 app
->byte_order
, app
->version
.major
,
1872 app
->version
.minor
);
1877 DBG3("UST app buffer registry per PID created successfully");
1889 * Setup buffer registry per UID for the given session and application. If none
1890 * is found, a new one is created, added to the global registry and
1891 * initialized. If regp is valid, it's set with the newly created object.
1893 * Return 0 on success or else a negative value.
1895 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
1896 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
1899 struct buffer_reg_uid
*reg_uid
;
1906 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
1909 * This is the create channel path meaning that if there is NO
1910 * registry available, we have to create one for this session.
1912 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
1913 LTTNG_DOMAIN_UST
, ®_uid
);
1917 buffer_reg_uid_add(reg_uid
);
1922 /* Initialize registry. */
1923 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
1924 app
->bits_per_long
, app
->uint8_t_alignment
,
1925 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1926 app
->uint64_t_alignment
, app
->long_alignment
,
1927 app
->byte_order
, app
->version
.major
,
1928 app
->version
.minor
);
1932 /* Add node to teardown list of the session. */
1933 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
1935 DBG3("UST app buffer registry per UID created successfully");
1947 * Create a session on the tracer side for the given app.
1949 * On success, ua_sess_ptr is populated with the session pointer or else left
1950 * untouched. If the session was created, is_created is set to 1. On error,
1951 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1954 * Returns 0 on success or else a negative code which is either -ENOMEM or
1955 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1957 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1958 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1961 int ret
, created
= 0;
1962 struct ust_app_session
*ua_sess
;
1966 assert(ua_sess_ptr
);
1968 health_code_update();
1970 ua_sess
= lookup_session_by_app(usess
, app
);
1971 if (ua_sess
== NULL
) {
1972 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
1973 app
->pid
, usess
->id
);
1974 ua_sess
= alloc_ust_app_session(app
);
1975 if (ua_sess
== NULL
) {
1976 /* Only malloc can failed so something is really wrong */
1980 shadow_copy_session(ua_sess
, usess
, app
);
1984 switch (usess
->buffer_type
) {
1985 case LTTNG_BUFFER_PER_PID
:
1986 /* Init local registry. */
1987 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
1992 case LTTNG_BUFFER_PER_UID
:
1993 /* Look for a global registry. If none exists, create one. */
1994 ret
= setup_buffer_reg_uid(usess
, app
, NULL
);
2005 health_code_update();
2007 if (ua_sess
->handle
== -1) {
2008 pthread_mutex_lock(&app
->sock_lock
);
2009 ret
= ustctl_create_session(app
->sock
);
2010 pthread_mutex_unlock(&app
->sock_lock
);
2012 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2013 ERR("Creating session for app pid %d with ret %d",
2016 DBG("UST app creating session failed. Application is dead");
2018 * This is normal behavior, an application can die during the
2019 * creation process. Don't report an error so the execution can
2020 * continue normally. This will get flagged ENOTCONN and the
2021 * caller will handle it.
2025 delete_ust_app_session(-1, ua_sess
, app
);
2026 if (ret
!= -ENOMEM
) {
2028 * Tracer is probably gone or got an internal error so let's
2029 * behave like it will soon unregister or not usable.
2036 ua_sess
->handle
= ret
;
2038 /* Add ust app session to app's HT */
2039 lttng_ht_node_init_u64(&ua_sess
->node
,
2040 ua_sess
->tracing_id
);
2041 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2043 DBG2("UST app session created successfully with handle %d", ret
);
2046 *ua_sess_ptr
= ua_sess
;
2048 *is_created
= created
;
2051 /* Everything went well. */
2055 health_code_update();
2060 * Match function for a hash table lookup of ust_app_ctx.
2062 * It matches an ust app context based on the context type and, in the case
2063 * of perf counters, their name.
2065 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2067 struct ust_app_ctx
*ctx
;
2068 const struct lttng_ust_context
*key
;
2073 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2077 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2081 /* Check the name in the case of perf thread counters. */
2082 if (key
->ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
2083 if (strncmp(key
->u
.perf_counter
.name
,
2084 ctx
->ctx
.u
.perf_counter
.name
,
2085 sizeof(key
->u
.perf_counter
.name
))) {
2098 * Lookup for an ust app context from an lttng_ust_context.
2100 * Must be called while holding RCU read side lock.
2101 * Return an ust_app_ctx object or NULL on error.
2104 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2105 struct lttng_ust_context
*uctx
)
2107 struct lttng_ht_iter iter
;
2108 struct lttng_ht_node_ulong
*node
;
2109 struct ust_app_ctx
*app_ctx
= NULL
;
2114 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2115 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2116 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2117 node
= lttng_ht_iter_get_node_ulong(&iter
);
2122 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2129 * Create a context for the channel on the tracer.
2131 * Called with UST app session lock held and a RCU read side lock.
2134 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2135 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
2136 struct ust_app
*app
)
2139 struct ust_app_ctx
*ua_ctx
;
2141 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2143 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2149 ua_ctx
= alloc_ust_app_ctx(uctx
);
2150 if (ua_ctx
== NULL
) {
2156 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2157 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2158 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2160 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2170 * Enable on the tracer side a ust app event for the session and channel.
2172 * Called with UST app session lock held.
2175 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2176 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2180 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2185 ua_event
->enabled
= 1;
2192 * Disable on the tracer side a ust app event for the session and channel.
2194 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2195 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2199 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2204 ua_event
->enabled
= 0;
2211 * Lookup ust app channel for session and disable it on the tracer side.
2214 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2215 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2219 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2224 ua_chan
->enabled
= 0;
2231 * Lookup ust app channel for session and enable it on the tracer side. This
2232 * MUST be called with a RCU read side lock acquired.
2234 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2235 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2238 struct lttng_ht_iter iter
;
2239 struct lttng_ht_node_str
*ua_chan_node
;
2240 struct ust_app_channel
*ua_chan
;
2242 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2243 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2244 if (ua_chan_node
== NULL
) {
2245 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2246 uchan
->name
, ua_sess
->tracing_id
);
2250 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2252 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2262 * Ask the consumer to create a channel and get it if successful.
2264 * Return 0 on success or else a negative value.
2266 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2267 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2268 int bitness
, struct ust_registry_session
*registry
)
2271 unsigned int nb_fd
= 0;
2272 struct consumer_socket
*socket
;
2280 health_code_update();
2282 /* Get the right consumer socket for the application. */
2283 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2289 health_code_update();
2291 /* Need one fd for the channel. */
2292 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2294 ERR("Exhausted number of available FD upon create channel");
2299 * Ask consumer to create channel. The consumer will return the number of
2300 * stream we have to expect.
2302 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2309 * Compute the number of fd needed before receiving them. It must be 2 per
2310 * stream (2 being the default value here).
2312 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2314 /* Reserve the amount of file descriptor we need. */
2315 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2317 ERR("Exhausted number of available FD upon create channel");
2318 goto error_fd_get_stream
;
2321 health_code_update();
2324 * Now get the channel from the consumer. This call wil populate the stream
2325 * list of that channel and set the ust objects.
2327 if (usess
->consumer
->enabled
) {
2328 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2338 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2339 error_fd_get_stream
:
2341 * Initiate a destroy channel on the consumer since we had an error
2342 * handling it on our side. The return value is of no importance since we
2343 * already have a ret value set by the previous error that we need to
2346 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2348 lttng_fd_put(LTTNG_FD_APPS
, 1);
2350 health_code_update();
2356 * Duplicate the ust data object of the ust app stream and save it in the
2357 * buffer registry stream.
2359 * Return 0 on success or else a negative value.
2361 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2362 struct ust_app_stream
*stream
)
2369 /* Reserve the amount of file descriptor we need. */
2370 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2372 ERR("Exhausted number of available FD upon duplicate stream");
2376 /* Duplicate object for stream once the original is in the registry. */
2377 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2378 reg_stream
->obj
.ust
);
2380 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2381 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2382 lttng_fd_put(LTTNG_FD_APPS
, 2);
2385 stream
->handle
= stream
->obj
->handle
;
2392 * Duplicate the ust data object of the ust app. channel and save it in the
2393 * buffer registry channel.
2395 * Return 0 on success or else a negative value.
2397 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2398 struct ust_app_channel
*ua_chan
)
2405 /* Need two fds for the channel. */
2406 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2408 ERR("Exhausted number of available FD upon duplicate channel");
2412 /* Duplicate object for stream once the original is in the registry. */
2413 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2415 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2416 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2419 ua_chan
->handle
= ua_chan
->obj
->handle
;
2424 lttng_fd_put(LTTNG_FD_APPS
, 1);
2430 * For a given channel buffer registry, setup all streams of the given ust
2431 * application channel.
2433 * Return 0 on success or else a negative value.
2435 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2436 struct ust_app_channel
*ua_chan
,
2437 struct ust_app
*app
)
2440 struct ust_app_stream
*stream
, *stmp
;
2445 DBG2("UST app setup buffer registry stream");
2447 /* Send all streams to application. */
2448 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2449 struct buffer_reg_stream
*reg_stream
;
2451 ret
= buffer_reg_stream_create(®_stream
);
2457 * Keep original pointer and nullify it in the stream so the delete
2458 * stream call does not release the object.
2460 reg_stream
->obj
.ust
= stream
->obj
;
2462 buffer_reg_stream_add(reg_stream
, reg_chan
);
2464 /* We don't need the streams anymore. */
2465 cds_list_del(&stream
->list
);
2466 delete_ust_app_stream(-1, stream
, app
);
2474 * Create a buffer registry channel for the given session registry and
2475 * application channel object. If regp pointer is valid, it's set with the
2476 * created object. Important, the created object is NOT added to the session
2477 * registry hash table.
2479 * Return 0 on success else a negative value.
2481 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2482 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2485 struct buffer_reg_channel
*reg_chan
= NULL
;
2490 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2492 /* Create buffer registry channel. */
2493 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2498 reg_chan
->consumer_key
= ua_chan
->key
;
2499 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2500 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2502 /* Create and add a channel registry to session. */
2503 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2504 ua_chan
->tracing_channel_id
);
2508 buffer_reg_channel_add(reg_sess
, reg_chan
);
2517 /* Safe because the registry channel object was not added to any HT. */
2518 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2524 * Setup buffer registry channel for the given session registry and application
2525 * channel object. If regp pointer is valid, it's set with the created object.
2527 * Return 0 on success else a negative value.
2529 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2530 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2531 struct ust_app
*app
)
2538 assert(ua_chan
->obj
);
2540 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2542 /* Setup all streams for the registry. */
2543 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2548 reg_chan
->obj
.ust
= ua_chan
->obj
;
2549 ua_chan
->obj
= NULL
;
2554 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2555 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2560 * Send buffer registry channel to the application.
2562 * Return 0 on success else a negative value.
2564 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2565 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2566 struct ust_app_channel
*ua_chan
)
2569 struct buffer_reg_stream
*reg_stream
;
2576 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2578 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2583 /* Send channel to the application. */
2584 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2585 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2586 ret
= -ENOTCONN
; /* Caused by app exiting. */
2588 } else if (ret
< 0) {
2592 health_code_update();
2594 /* Send all streams to application. */
2595 pthread_mutex_lock(®_chan
->stream_list_lock
);
2596 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2597 struct ust_app_stream stream
;
2599 ret
= duplicate_stream_object(reg_stream
, &stream
);
2601 goto error_stream_unlock
;
2604 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2606 (void) release_ust_app_stream(-1, &stream
, app
);
2607 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2608 ret
= -ENOTCONN
; /* Caused by app exiting. */
2609 goto error_stream_unlock
;
2610 } else if (ret
< 0) {
2611 goto error_stream_unlock
;
2613 goto error_stream_unlock
;
2617 * The return value is not important here. This function will output an
2620 (void) release_ust_app_stream(-1, &stream
, app
);
2622 ua_chan
->is_sent
= 1;
2624 error_stream_unlock
:
2625 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2631 * Create and send to the application the created buffers with per UID buffers.
2633 * Return 0 on success else a negative value.
2635 static int create_channel_per_uid(struct ust_app
*app
,
2636 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2637 struct ust_app_channel
*ua_chan
)
2640 struct buffer_reg_uid
*reg_uid
;
2641 struct buffer_reg_channel
*reg_chan
;
2648 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2650 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2652 * The session creation handles the creation of this global registry
2653 * object. If none can be find, there is a code flow problem or a
2658 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2661 /* Create the buffer registry channel object. */
2662 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2664 ERR("Error creating the UST channel \"%s\" registry instance",
2671 * Create the buffers on the consumer side. This call populates the
2672 * ust app channel object with all streams and data object.
2674 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2675 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2677 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2681 * Let's remove the previously created buffer registry channel so
2682 * it's not visible anymore in the session registry.
2684 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2685 ua_chan
->tracing_channel_id
);
2686 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2687 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2692 * Setup the streams and add it to the session registry.
2694 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2695 ua_chan
, reg_chan
, app
);
2697 ERR("Error setting up UST channel \"%s\"",
2704 /* Send buffers to the application. */
2705 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2707 if (ret
!= -ENOTCONN
) {
2708 ERR("Error sending channel to application");
2718 * Create and send to the application the created buffers with per PID buffers.
2720 * Return 0 on success else a negative value.
2722 static int create_channel_per_pid(struct ust_app
*app
,
2723 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2724 struct ust_app_channel
*ua_chan
)
2727 struct ust_registry_session
*registry
;
2734 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2738 registry
= get_session_registry(ua_sess
);
2741 /* Create and add a new channel registry to session. */
2742 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2744 ERR("Error creating the UST channel \"%s\" registry instance",
2749 /* Create and get channel on the consumer side. */
2750 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2751 app
->bits_per_long
, registry
);
2753 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2758 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2760 if (ret
!= -ENOTCONN
) {
2761 ERR("Error sending channel to application");
2772 * From an already allocated ust app channel, create the channel buffers if
2773 * need and send it to the application. This MUST be called with a RCU read
2774 * side lock acquired.
2776 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2777 * the application exited concurrently.
2779 static int do_create_channel(struct ust_app
*app
,
2780 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2781 struct ust_app_channel
*ua_chan
)
2790 /* Handle buffer type before sending the channel to the application. */
2791 switch (usess
->buffer_type
) {
2792 case LTTNG_BUFFER_PER_UID
:
2794 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
2800 case LTTNG_BUFFER_PER_PID
:
2802 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
2814 /* Initialize ust objd object using the received handle and add it. */
2815 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
2816 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
2818 /* If channel is not enabled, disable it on the tracer */
2819 if (!ua_chan
->enabled
) {
2820 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2831 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2832 * newly created channel if not NULL.
2834 * Called with UST app session lock and RCU read-side lock held.
2836 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2837 * the application exited concurrently.
2839 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
2840 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
2841 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
2842 struct ust_app_channel
**ua_chanp
)
2845 struct lttng_ht_iter iter
;
2846 struct lttng_ht_node_str
*ua_chan_node
;
2847 struct ust_app_channel
*ua_chan
;
2849 /* Lookup channel in the ust app session */
2850 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2851 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2852 if (ua_chan_node
!= NULL
) {
2853 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2857 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
2858 if (ua_chan
== NULL
) {
2859 /* Only malloc can fail here */
2863 shadow_copy_channel(ua_chan
, uchan
);
2865 /* Set channel type. */
2866 ua_chan
->attr
.type
= type
;
2868 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
2873 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
2876 /* Only add the channel if successful on the tracer side. */
2877 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
2881 *ua_chanp
= ua_chan
;
2884 /* Everything went well. */
2888 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
2894 * Create UST app event and create it on the tracer side.
2896 * Called with ust app session mutex held.
2899 int create_ust_app_event(struct ust_app_session
*ua_sess
,
2900 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
2901 struct ust_app
*app
)
2904 struct ust_app_event
*ua_event
;
2906 /* Get event node */
2907 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2908 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
2909 if (ua_event
!= NULL
) {
2914 /* Does not exist so create one */
2915 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
2916 if (ua_event
== NULL
) {
2917 /* Only malloc can failed so something is really wrong */
2921 shadow_copy_event(ua_event
, uevent
);
2923 /* Create it on the tracer side */
2924 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2926 /* Not found previously means that it does not exist on the tracer */
2927 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
2931 add_unique_ust_app_event(ua_chan
, ua_event
);
2933 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
2940 /* Valid. Calling here is already in a read side lock */
2941 delete_ust_app_event(-1, ua_event
, app
);
2946 * Create UST metadata and open it on the tracer side.
2948 * Called with UST app session lock held and RCU read side lock.
2950 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
2951 struct ust_app
*app
, struct consumer_output
*consumer
)
2954 struct ust_app_channel
*metadata
;
2955 struct consumer_socket
*socket
;
2956 struct ust_registry_session
*registry
;
2962 registry
= get_session_registry(ua_sess
);
2965 pthread_mutex_lock(®istry
->lock
);
2967 /* Metadata already exists for this registry or it was closed previously */
2968 if (registry
->metadata_key
|| registry
->metadata_closed
) {
2973 /* Allocate UST metadata */
2974 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
2976 /* malloc() failed */
2981 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
2983 /* Need one fd for the channel. */
2984 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2986 ERR("Exhausted number of available FD upon create metadata");
2990 /* Get the right consumer socket for the application. */
2991 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
2994 goto error_consumer
;
2998 * Keep metadata key so we can identify it on the consumer side. Assign it
2999 * to the registry *before* we ask the consumer so we avoid the race of the
3000 * consumer requesting the metadata and the ask_channel call on our side
3001 * did not returned yet.
3003 registry
->metadata_key
= metadata
->key
;
3006 * Ask the metadata channel creation to the consumer. The metadata object
3007 * will be created by the consumer and kept their. However, the stream is
3008 * never added or monitored until we do a first push metadata to the
3011 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3014 /* Nullify the metadata key so we don't try to close it later on. */
3015 registry
->metadata_key
= 0;
3016 goto error_consumer
;
3020 * The setup command will make the metadata stream be sent to the relayd,
3021 * if applicable, and the thread managing the metadatas. This is important
3022 * because after this point, if an error occurs, the only way the stream
3023 * can be deleted is to be monitored in the consumer.
3025 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3027 /* Nullify the metadata key so we don't try to close it later on. */
3028 registry
->metadata_key
= 0;
3029 goto error_consumer
;
3032 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3033 metadata
->key
, app
->pid
);
3036 lttng_fd_put(LTTNG_FD_APPS
, 1);
3037 delete_ust_app_channel(-1, metadata
, app
);
3039 pthread_mutex_unlock(®istry
->lock
);
3044 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3045 * acquired before calling this function.
3047 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3049 struct ust_app
*app
= NULL
;
3050 struct lttng_ht_node_ulong
*node
;
3051 struct lttng_ht_iter iter
;
3053 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3054 node
= lttng_ht_iter_get_node_ulong(&iter
);
3056 DBG2("UST app no found with pid %d", pid
);
3060 DBG2("Found UST app by pid %d", pid
);
3062 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3069 * Allocate and init an UST app object using the registration information and
3070 * the command socket. This is called when the command socket connects to the
3073 * The object is returned on success or else NULL.
3075 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3077 struct ust_app
*lta
= NULL
;
3082 DBG3("UST app creating application for socket %d", sock
);
3084 if ((msg
->bits_per_long
== 64 &&
3085 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3086 || (msg
->bits_per_long
== 32 &&
3087 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3088 ERR("Registration failed: application \"%s\" (pid: %d) has "
3089 "%d-bit long, but no consumerd for this size is available.\n",
3090 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3094 lta
= zmalloc(sizeof(struct ust_app
));
3100 lta
->ppid
= msg
->ppid
;
3101 lta
->uid
= msg
->uid
;
3102 lta
->gid
= msg
->gid
;
3104 lta
->bits_per_long
= msg
->bits_per_long
;
3105 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3106 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3107 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3108 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3109 lta
->long_alignment
= msg
->long_alignment
;
3110 lta
->byte_order
= msg
->byte_order
;
3112 lta
->v_major
= msg
->major
;
3113 lta
->v_minor
= msg
->minor
;
3114 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3115 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3116 lta
->notify_sock
= -1;
3118 /* Copy name and make sure it's NULL terminated. */
3119 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3120 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3123 * Before this can be called, when receiving the registration information,
3124 * the application compatibility is checked. So, at this point, the
3125 * application can work with this session daemon.
3127 lta
->compatible
= 1;
3129 lta
->pid
= msg
->pid
;
3130 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3132 pthread_mutex_init(<a
->sock_lock
, NULL
);
3133 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3135 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3141 * For a given application object, add it to every hash table.
3143 void ust_app_add(struct ust_app
*app
)
3146 assert(app
->notify_sock
>= 0);
3151 * On a re-registration, we want to kick out the previous registration of
3154 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3157 * The socket _should_ be unique until _we_ call close. So, a add_unique
3158 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3159 * already in the table.
3161 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3163 /* Add application to the notify socket hash table. */
3164 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3165 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3167 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3168 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3169 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3176 * Set the application version into the object.
3178 * Return 0 on success else a negative value either an errno code or a
3179 * LTTng-UST error code.
3181 int ust_app_version(struct ust_app
*app
)
3187 pthread_mutex_lock(&app
->sock_lock
);
3188 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3189 pthread_mutex_unlock(&app
->sock_lock
);
3191 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3192 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3194 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3202 * Unregister app by removing it from the global traceable app list and freeing
3205 * The socket is already closed at this point so no close to sock.
3207 void ust_app_unregister(int sock
)
3209 struct ust_app
*lta
;
3210 struct lttng_ht_node_ulong
*node
;
3211 struct lttng_ht_iter ust_app_sock_iter
;
3212 struct lttng_ht_iter iter
;
3213 struct ust_app_session
*ua_sess
;
3218 /* Get the node reference for a call_rcu */
3219 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3220 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3223 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3224 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3227 * For per-PID buffers, perform "push metadata" and flush all
3228 * application streams before removing app from hash tables,
3229 * ensuring proper behavior of data_pending check.
3230 * Remove sessions so they are not visible during deletion.
3232 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3234 struct ust_registry_session
*registry
;
3236 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3238 /* The session was already removed so scheduled for teardown. */
3242 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3243 (void) ust_app_flush_app_session(lta
, ua_sess
);
3247 * Add session to list for teardown. This is safe since at this point we
3248 * are the only one using this list.
3250 pthread_mutex_lock(&ua_sess
->lock
);
3252 if (ua_sess
->deleted
) {
3253 pthread_mutex_unlock(&ua_sess
->lock
);
3258 * Normally, this is done in the delete session process which is
3259 * executed in the call rcu below. However, upon registration we can't
3260 * afford to wait for the grace period before pushing data or else the
3261 * data pending feature can race between the unregistration and stop
3262 * command where the data pending command is sent *before* the grace
3265 * The close metadata below nullifies the metadata pointer in the
3266 * session so the delete session will NOT push/close a second time.
3268 registry
= get_session_registry(ua_sess
);
3270 /* Push metadata for application before freeing the application. */
3271 (void) push_metadata(registry
, ua_sess
->consumer
);
3274 * Don't ask to close metadata for global per UID buffers. Close
3275 * metadata only on destroy trace session in this case. Also, the
3276 * previous push metadata could have flag the metadata registry to
3277 * close so don't send a close command if closed.
3279 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3280 /* And ask to close it for this session registry. */
3281 (void) close_metadata(registry
, ua_sess
->consumer
);
3284 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3286 pthread_mutex_unlock(&ua_sess
->lock
);
3289 /* Remove application from PID hash table */
3290 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3294 * Remove application from notify hash table. The thread handling the
3295 * notify socket could have deleted the node so ignore on error because
3296 * either way it's valid. The close of that socket is handled by the other
3299 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3300 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3303 * Ignore return value since the node might have been removed before by an
3304 * add replace during app registration because the PID can be reassigned by
3307 iter
.iter
.node
= <a
->pid_n
.node
;
3308 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3310 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3315 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3322 * Fill events array with all events name of all registered apps.
3324 int ust_app_list_events(struct lttng_event
**events
)
3327 size_t nbmem
, count
= 0;
3328 struct lttng_ht_iter iter
;
3329 struct ust_app
*app
;
3330 struct lttng_event
*tmp_event
;
3332 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3333 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3334 if (tmp_event
== NULL
) {
3335 PERROR("zmalloc ust app events");
3342 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3343 struct lttng_ust_tracepoint_iter uiter
;
3345 health_code_update();
3347 if (!app
->compatible
) {
3349 * TODO: In time, we should notice the caller of this error by
3350 * telling him that this is a version error.
3354 pthread_mutex_lock(&app
->sock_lock
);
3355 handle
= ustctl_tracepoint_list(app
->sock
);
3357 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3358 ERR("UST app list events getting handle failed for app pid %d",
3361 pthread_mutex_unlock(&app
->sock_lock
);
3365 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3366 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3367 /* Handle ustctl error. */
3371 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3372 ERR("UST app tp list get failed for app %d with ret %d",
3375 DBG3("UST app tp list get failed. Application is dead");
3377 * This is normal behavior, an application can die during the
3378 * creation process. Don't report an error so the execution can
3379 * continue normally. Continue normal execution.
3384 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3385 if (release_ret
< 0 &&
3386 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3387 release_ret
!= -EPIPE
) {
3388 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3390 pthread_mutex_unlock(&app
->sock_lock
);
3394 health_code_update();
3395 if (count
>= nbmem
) {
3396 /* In case the realloc fails, we free the memory */
3397 struct lttng_event
*new_tmp_event
;
3400 new_nbmem
= nbmem
<< 1;
3401 DBG2("Reallocating event list from %zu to %zu entries",
3403 new_tmp_event
= realloc(tmp_event
,
3404 new_nbmem
* sizeof(struct lttng_event
));
3405 if (new_tmp_event
== NULL
) {
3408 PERROR("realloc ust app events");
3411 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3412 if (release_ret
< 0 &&
3413 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3414 release_ret
!= -EPIPE
) {
3415 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3417 pthread_mutex_unlock(&app
->sock_lock
);
3420 /* Zero the new memory */
3421 memset(new_tmp_event
+ nbmem
, 0,
3422 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3424 tmp_event
= new_tmp_event
;
3426 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3427 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3428 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3429 tmp_event
[count
].pid
= app
->pid
;
3430 tmp_event
[count
].enabled
= -1;
3433 ret
= ustctl_release_handle(app
->sock
, handle
);
3434 pthread_mutex_unlock(&app
->sock_lock
);
3435 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3436 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3441 *events
= tmp_event
;
3443 DBG2("UST app list events done (%zu events)", count
);
3448 health_code_update();
3453 * Fill events array with all events name of all registered apps.
3455 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3458 size_t nbmem
, count
= 0;
3459 struct lttng_ht_iter iter
;
3460 struct ust_app
*app
;
3461 struct lttng_event_field
*tmp_event
;
3463 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3464 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3465 if (tmp_event
== NULL
) {
3466 PERROR("zmalloc ust app event fields");
3473 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3474 struct lttng_ust_field_iter uiter
;
3476 health_code_update();
3478 if (!app
->compatible
) {
3480 * TODO: In time, we should notice the caller of this error by
3481 * telling him that this is a version error.
3485 pthread_mutex_lock(&app
->sock_lock
);
3486 handle
= ustctl_tracepoint_field_list(app
->sock
);
3488 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3489 ERR("UST app list field getting handle failed for app pid %d",
3492 pthread_mutex_unlock(&app
->sock_lock
);
3496 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3497 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3498 /* Handle ustctl error. */
3502 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3503 ERR("UST app tp list field failed for app %d with ret %d",
3506 DBG3("UST app tp list field failed. Application is dead");
3508 * This is normal behavior, an application can die during the
3509 * creation process. Don't report an error so the execution can
3510 * continue normally. Reset list and count for next app.
3515 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3516 pthread_mutex_unlock(&app
->sock_lock
);
3517 if (release_ret
< 0 &&
3518 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3519 release_ret
!= -EPIPE
) {
3520 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3525 health_code_update();
3526 if (count
>= nbmem
) {
3527 /* In case the realloc fails, we free the memory */
3528 struct lttng_event_field
*new_tmp_event
;
3531 new_nbmem
= nbmem
<< 1;
3532 DBG2("Reallocating event field list from %zu to %zu entries",
3534 new_tmp_event
= realloc(tmp_event
,
3535 new_nbmem
* sizeof(struct lttng_event_field
));
3536 if (new_tmp_event
== NULL
) {
3539 PERROR("realloc ust app event fields");
3542 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3543 pthread_mutex_unlock(&app
->sock_lock
);
3545 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3546 release_ret
!= -EPIPE
) {
3547 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3551 /* Zero the new memory */
3552 memset(new_tmp_event
+ nbmem
, 0,
3553 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3555 tmp_event
= new_tmp_event
;
3558 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3559 /* Mapping between these enums matches 1 to 1. */
3560 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3561 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3563 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3564 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3565 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3566 tmp_event
[count
].event
.pid
= app
->pid
;
3567 tmp_event
[count
].event
.enabled
= -1;
3570 ret
= ustctl_release_handle(app
->sock
, handle
);
3571 pthread_mutex_unlock(&app
->sock_lock
);
3573 ret
!= -LTTNG_UST_ERR_EXITING
&&
3575 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3580 *fields
= tmp_event
;
3582 DBG2("UST app list event fields done (%zu events)", count
);
3587 health_code_update();
3592 * Free and clean all traceable apps of the global list.
3594 * Should _NOT_ be called with RCU read-side lock held.
3596 void ust_app_clean_list(void)
3599 struct ust_app
*app
;
3600 struct lttng_ht_iter iter
;
3602 DBG2("UST app cleaning registered apps hash table");
3606 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3607 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3609 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3612 /* Cleanup socket hash table */
3613 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3615 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3619 /* Cleanup notify socket hash table */
3620 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3621 notify_sock_n
.node
) {
3622 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3627 /* Destroy is done only when the ht is empty */
3628 ht_cleanup_push(ust_app_ht
);
3629 ht_cleanup_push(ust_app_ht_by_sock
);
3630 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3634 * Init UST app hash table.
3636 void ust_app_ht_alloc(void)
3638 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3639 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3640 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3644 * For a specific UST session, disable the channel for all registered apps.
3646 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3647 struct ltt_ust_channel
*uchan
)
3650 struct lttng_ht_iter iter
;
3651 struct lttng_ht_node_str
*ua_chan_node
;
3652 struct ust_app
*app
;
3653 struct ust_app_session
*ua_sess
;
3654 struct ust_app_channel
*ua_chan
;
3656 if (usess
== NULL
|| uchan
== NULL
) {
3657 ERR("Disabling UST global channel with NULL values");
3662 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3663 uchan
->name
, usess
->id
);
3667 /* For every registered applications */
3668 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3669 struct lttng_ht_iter uiter
;
3670 if (!app
->compatible
) {
3672 * TODO: In time, we should notice the caller of this error by
3673 * telling him that this is a version error.
3677 ua_sess
= lookup_session_by_app(usess
, app
);
3678 if (ua_sess
== NULL
) {
3683 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3684 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3685 /* If the session if found for the app, the channel must be there */
3686 assert(ua_chan_node
);
3688 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3689 /* The channel must not be already disabled */
3690 assert(ua_chan
->enabled
== 1);
3692 /* Disable channel onto application */
3693 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3695 /* XXX: We might want to report this error at some point... */
3707 * For a specific UST session, enable the channel for all registered apps.
3709 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3710 struct ltt_ust_channel
*uchan
)
3713 struct lttng_ht_iter iter
;
3714 struct ust_app
*app
;
3715 struct ust_app_session
*ua_sess
;
3717 if (usess
== NULL
|| uchan
== NULL
) {
3718 ERR("Adding UST global channel to NULL values");
3723 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3724 uchan
->name
, usess
->id
);
3728 /* For every registered applications */
3729 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3730 if (!app
->compatible
) {
3732 * TODO: In time, we should notice the caller of this error by
3733 * telling him that this is a version error.
3737 ua_sess
= lookup_session_by_app(usess
, app
);
3738 if (ua_sess
== NULL
) {
3742 /* Enable channel onto application */
3743 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3745 /* XXX: We might want to report this error at some point... */
3757 * Disable an event in a channel and for a specific session.
3759 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3760 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3763 struct lttng_ht_iter iter
, uiter
;
3764 struct lttng_ht_node_str
*ua_chan_node
;
3765 struct ust_app
*app
;
3766 struct ust_app_session
*ua_sess
;
3767 struct ust_app_channel
*ua_chan
;
3768 struct ust_app_event
*ua_event
;
3770 DBG("UST app disabling event %s for all apps in channel "
3771 "%s for session id %" PRIu64
,
3772 uevent
->attr
.name
, uchan
->name
, usess
->id
);
3776 /* For all registered applications */
3777 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3778 if (!app
->compatible
) {
3780 * TODO: In time, we should notice the caller of this error by
3781 * telling him that this is a version error.
3785 ua_sess
= lookup_session_by_app(usess
, app
);
3786 if (ua_sess
== NULL
) {
3791 /* Lookup channel in the ust app session */
3792 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3793 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3794 if (ua_chan_node
== NULL
) {
3795 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
3796 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
3799 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3801 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3802 uevent
->filter
, uevent
->attr
.loglevel
,
3804 if (ua_event
== NULL
) {
3805 DBG2("Event %s not found in channel %s for app pid %d."
3806 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
3810 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3812 /* XXX: Report error someday... */
3823 * For a specific UST session, create the channel for all registered apps.
3825 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
3826 struct ltt_ust_channel
*uchan
)
3828 int ret
= 0, created
;
3829 struct lttng_ht_iter iter
;
3830 struct ust_app
*app
;
3831 struct ust_app_session
*ua_sess
= NULL
;
3833 /* Very wrong code flow */
3837 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
3838 uchan
->name
, usess
->id
);
3842 /* For every registered applications */
3843 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3844 if (!app
->compatible
) {
3846 * TODO: In time, we should notice the caller of this error by
3847 * telling him that this is a version error.
3852 * Create session on the tracer side and add it to app session HT. Note
3853 * that if session exist, it will simply return a pointer to the ust
3856 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
3861 * The application's socket is not valid. Either a bad socket
3862 * or a timeout on it. We can't inform the caller that for a
3863 * specific app, the session failed so lets continue here.
3865 ret
= 0; /* Not an error. */
3869 goto error_rcu_unlock
;
3874 pthread_mutex_lock(&ua_sess
->lock
);
3876 if (ua_sess
->deleted
) {
3877 pthread_mutex_unlock(&ua_sess
->lock
);
3881 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
3882 sizeof(uchan
->name
))) {
3883 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
3886 /* Create channel onto application. We don't need the chan ref. */
3887 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
3888 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
3890 pthread_mutex_unlock(&ua_sess
->lock
);
3892 /* Cleanup the created session if it's the case. */
3894 destroy_app_session(app
, ua_sess
);
3899 * The application's socket is not valid. Either a bad socket
3900 * or a timeout on it. We can't inform the caller that for a
3901 * specific app, the session failed so lets continue here.
3903 ret
= 0; /* Not an error. */
3907 goto error_rcu_unlock
;
3918 * Enable event for a specific session and channel on the tracer.
3920 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
3921 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3924 struct lttng_ht_iter iter
, uiter
;
3925 struct lttng_ht_node_str
*ua_chan_node
;
3926 struct ust_app
*app
;
3927 struct ust_app_session
*ua_sess
;
3928 struct ust_app_channel
*ua_chan
;
3929 struct ust_app_event
*ua_event
;
3931 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
3932 uevent
->attr
.name
, usess
->id
);
3935 * NOTE: At this point, this function is called only if the session and
3936 * channel passed are already created for all apps. and enabled on the
3942 /* For all registered applications */
3943 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3944 if (!app
->compatible
) {
3946 * TODO: In time, we should notice the caller of this error by
3947 * telling him that this is a version error.
3951 ua_sess
= lookup_session_by_app(usess
, app
);
3953 /* The application has problem or is probably dead. */
3957 pthread_mutex_lock(&ua_sess
->lock
);
3959 if (ua_sess
->deleted
) {
3960 pthread_mutex_unlock(&ua_sess
->lock
);
3964 /* Lookup channel in the ust app session */
3965 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3966 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3968 * It is possible that the channel cannot be found is
3969 * the channel/event creation occurs concurrently with
3970 * an application exit.
3972 if (!ua_chan_node
) {
3973 pthread_mutex_unlock(&ua_sess
->lock
);
3977 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3979 /* Get event node */
3980 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3981 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3982 if (ua_event
== NULL
) {
3983 DBG3("UST app enable event %s not found for app PID %d."
3984 "Skipping app", uevent
->attr
.name
, app
->pid
);
3988 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
3990 pthread_mutex_unlock(&ua_sess
->lock
);
3994 pthread_mutex_unlock(&ua_sess
->lock
);
4003 * For a specific existing UST session and UST channel, creates the event for
4004 * all registered apps.
4006 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4007 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4010 struct lttng_ht_iter iter
, uiter
;
4011 struct lttng_ht_node_str
*ua_chan_node
;
4012 struct ust_app
*app
;
4013 struct ust_app_session
*ua_sess
;
4014 struct ust_app_channel
*ua_chan
;
4016 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4017 uevent
->attr
.name
, usess
->id
);
4021 /* For all registered applications */
4022 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4023 if (!app
->compatible
) {
4025 * TODO: In time, we should notice the caller of this error by
4026 * telling him that this is a version error.
4030 ua_sess
= lookup_session_by_app(usess
, app
);
4032 /* The application has problem or is probably dead. */
4036 pthread_mutex_lock(&ua_sess
->lock
);
4038 if (ua_sess
->deleted
) {
4039 pthread_mutex_unlock(&ua_sess
->lock
);
4043 /* Lookup channel in the ust app session */
4044 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4045 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4046 /* If the channel is not found, there is a code flow error */
4047 assert(ua_chan_node
);
4049 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4051 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4052 pthread_mutex_unlock(&ua_sess
->lock
);
4054 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4055 /* Possible value at this point: -ENOMEM. If so, we stop! */
4058 DBG2("UST app event %s already exist on app PID %d",
4059 uevent
->attr
.name
, app
->pid
);
4070 * Start tracing for a specific UST session and app.
4073 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4076 struct ust_app_session
*ua_sess
;
4078 DBG("Starting tracing for ust app pid %d", app
->pid
);
4082 if (!app
->compatible
) {
4086 ua_sess
= lookup_session_by_app(usess
, app
);
4087 if (ua_sess
== NULL
) {
4088 /* The session is in teardown process. Ignore and continue. */
4092 pthread_mutex_lock(&ua_sess
->lock
);
4094 if (ua_sess
->deleted
) {
4095 pthread_mutex_unlock(&ua_sess
->lock
);
4099 /* Upon restart, we skip the setup, already done */
4100 if (ua_sess
->started
) {
4104 /* Create directories if consumer is LOCAL and has a path defined. */
4105 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
4106 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
4107 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
4108 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
4110 if (errno
!= EEXIST
) {
4111 ERR("Trace directory creation error");
4118 * Create the metadata for the application. This returns gracefully if a
4119 * metadata was already set for the session.
4121 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4126 health_code_update();
4129 /* This start the UST tracing */
4130 pthread_mutex_lock(&app
->sock_lock
);
4131 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4132 pthread_mutex_unlock(&app
->sock_lock
);
4134 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4135 ERR("Error starting tracing for app pid: %d (ret: %d)",
4138 DBG("UST app start session failed. Application is dead.");
4140 * This is normal behavior, an application can die during the
4141 * creation process. Don't report an error so the execution can
4142 * continue normally.
4144 pthread_mutex_unlock(&ua_sess
->lock
);
4150 /* Indicate that the session has been started once */
4151 ua_sess
->started
= 1;
4153 pthread_mutex_unlock(&ua_sess
->lock
);
4155 health_code_update();
4157 /* Quiescent wait after starting trace */
4158 pthread_mutex_lock(&app
->sock_lock
);
4159 ret
= ustctl_wait_quiescent(app
->sock
);
4160 pthread_mutex_unlock(&app
->sock_lock
);
4161 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4162 ERR("UST app wait quiescent failed for app pid %d ret %d",
4168 health_code_update();
4172 pthread_mutex_unlock(&ua_sess
->lock
);
4174 health_code_update();
4179 * Stop tracing for a specific UST session and app.
4182 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4185 struct ust_app_session
*ua_sess
;
4186 struct ust_registry_session
*registry
;
4188 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4192 if (!app
->compatible
) {
4193 goto end_no_session
;
4196 ua_sess
= lookup_session_by_app(usess
, app
);
4197 if (ua_sess
== NULL
) {
4198 goto end_no_session
;
4201 pthread_mutex_lock(&ua_sess
->lock
);
4203 if (ua_sess
->deleted
) {
4204 pthread_mutex_unlock(&ua_sess
->lock
);
4205 goto end_no_session
;
4209 * If started = 0, it means that stop trace has been called for a session
4210 * that was never started. It's possible since we can have a fail start
4211 * from either the application manager thread or the command thread. Simply
4212 * indicate that this is a stop error.
4214 if (!ua_sess
->started
) {
4215 goto error_rcu_unlock
;
4218 health_code_update();
4220 /* This inhibits UST tracing */
4221 pthread_mutex_lock(&app
->sock_lock
);
4222 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4223 pthread_mutex_unlock(&app
->sock_lock
);
4225 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4226 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4229 DBG("UST app stop session failed. Application is dead.");
4231 * This is normal behavior, an application can die during the
4232 * creation process. Don't report an error so the execution can
4233 * continue normally.
4237 goto error_rcu_unlock
;
4240 health_code_update();
4242 /* Quiescent wait after stopping trace */
4243 pthread_mutex_lock(&app
->sock_lock
);
4244 ret
= ustctl_wait_quiescent(app
->sock
);
4245 pthread_mutex_unlock(&app
->sock_lock
);
4246 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4247 ERR("UST app wait quiescent failed for app pid %d ret %d",
4251 health_code_update();
4253 registry
= get_session_registry(ua_sess
);
4256 /* Push metadata for application before freeing the application. */
4257 (void) push_metadata(registry
, ua_sess
->consumer
);
4260 pthread_mutex_unlock(&ua_sess
->lock
);
4263 health_code_update();
4267 pthread_mutex_unlock(&ua_sess
->lock
);
4269 health_code_update();
4274 int ust_app_flush_app_session(struct ust_app
*app
,
4275 struct ust_app_session
*ua_sess
)
4277 int ret
, retval
= 0;
4278 struct lttng_ht_iter iter
;
4279 struct ust_app_channel
*ua_chan
;
4280 struct consumer_socket
*socket
;
4282 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4286 if (!app
->compatible
) {
4287 goto end_not_compatible
;
4290 pthread_mutex_lock(&ua_sess
->lock
);
4292 if (ua_sess
->deleted
) {
4296 health_code_update();
4298 /* Flushing buffers */
4299 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4302 /* Flush buffers and push metadata. */
4303 switch (ua_sess
->buffer_type
) {
4304 case LTTNG_BUFFER_PER_PID
:
4305 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4307 health_code_update();
4308 assert(ua_chan
->is_sent
);
4309 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4311 ERR("Error flushing consumer channel");
4317 case LTTNG_BUFFER_PER_UID
:
4323 health_code_update();
4326 pthread_mutex_unlock(&ua_sess
->lock
);
4330 health_code_update();
4335 * Flush buffers for all applications for a specific UST session.
4336 * Called with UST session lock held.
4339 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4344 DBG("Flushing session buffers for all ust apps");
4348 /* Flush buffers and push metadata. */
4349 switch (usess
->buffer_type
) {
4350 case LTTNG_BUFFER_PER_UID
:
4352 struct buffer_reg_uid
*reg
;
4353 struct lttng_ht_iter iter
;
4355 /* Flush all per UID buffers associated to that session. */
4356 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4357 struct ust_registry_session
*ust_session_reg
;
4358 struct buffer_reg_channel
*reg_chan
;
4359 struct consumer_socket
*socket
;
4361 /* Get consumer socket to use to push the metadata.*/
4362 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4365 /* Ignore request if no consumer is found for the session. */
4369 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4370 reg_chan
, node
.node
) {
4372 * The following call will print error values so the return
4373 * code is of little importance because whatever happens, we
4374 * have to try them all.
4376 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4379 ust_session_reg
= reg
->registry
->reg
.ust
;
4380 /* Push metadata. */
4381 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4385 case LTTNG_BUFFER_PER_PID
:
4387 struct ust_app_session
*ua_sess
;
4388 struct lttng_ht_iter iter
;
4389 struct ust_app
*app
;
4391 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4392 ua_sess
= lookup_session_by_app(usess
, app
);
4393 if (ua_sess
== NULL
) {
4396 (void) ust_app_flush_app_session(app
, ua_sess
);
4407 health_code_update();
4412 * Destroy a specific UST session in apps.
4414 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4417 struct ust_app_session
*ua_sess
;
4418 struct lttng_ht_iter iter
;
4419 struct lttng_ht_node_u64
*node
;
4421 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4425 if (!app
->compatible
) {
4429 __lookup_session_by_app(usess
, app
, &iter
);
4430 node
= lttng_ht_iter_get_node_u64(&iter
);
4432 /* Session is being or is deleted. */
4435 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4437 health_code_update();
4438 destroy_app_session(app
, ua_sess
);
4440 health_code_update();
4442 /* Quiescent wait after stopping trace */
4443 pthread_mutex_lock(&app
->sock_lock
);
4444 ret
= ustctl_wait_quiescent(app
->sock
);
4445 pthread_mutex_unlock(&app
->sock_lock
);
4446 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4447 ERR("UST app wait quiescent failed for app pid %d ret %d",
4452 health_code_update();
4457 * Start tracing for the UST session.
4459 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4462 struct lttng_ht_iter iter
;
4463 struct ust_app
*app
;
4465 DBG("Starting all UST traces");
4469 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4470 ret
= ust_app_start_trace(usess
, app
);
4472 /* Continue to next apps even on error */
4483 * Start tracing for the UST session.
4484 * Called with UST session lock held.
4486 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4489 struct lttng_ht_iter iter
;
4490 struct ust_app
*app
;
4492 DBG("Stopping all UST traces");
4496 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4497 ret
= ust_app_stop_trace(usess
, app
);
4499 /* Continue to next apps even on error */
4504 (void) ust_app_flush_session(usess
);
4512 * Destroy app UST session.
4514 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4517 struct lttng_ht_iter iter
;
4518 struct ust_app
*app
;
4520 DBG("Destroy all UST traces");
4524 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4525 ret
= destroy_trace(usess
, app
);
4527 /* Continue to next apps even on error */
4538 * Add channels/events from UST global domain to registered apps at sock.
4540 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
4543 struct lttng_ht_iter iter
, uiter
;
4544 struct ust_app
*app
;
4545 struct ust_app_session
*ua_sess
= NULL
;
4546 struct ust_app_channel
*ua_chan
;
4547 struct ust_app_event
*ua_event
;
4548 struct ust_app_ctx
*ua_ctx
;
4553 DBG2("UST app global update for app sock %d for session id %" PRIu64
, sock
,
4558 app
= ust_app_find_by_sock(sock
);
4561 * Application can be unregistered before so this is possible hence
4562 * simply stopping the update.
4564 DBG3("UST app update failed to find app sock %d", sock
);
4568 if (!app
->compatible
) {
4572 ret
= create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
4574 /* Tracer is probably gone or ENOMEM. */
4579 pthread_mutex_lock(&ua_sess
->lock
);
4581 if (ua_sess
->deleted
) {
4582 pthread_mutex_unlock(&ua_sess
->lock
);
4587 * We can iterate safely here over all UST app session since the create ust
4588 * app session above made a shadow copy of the UST global domain from the
4591 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4593 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4594 if (ret
< 0 && ret
!= -ENOTCONN
) {
4596 * Stop everything. On error, the application
4597 * failed, no more file descriptor are available
4598 * or ENOMEM so stopping here is the only thing
4599 * we can do for now. The only exception is
4600 * -ENOTCONN, which indicates that the application
4607 * Add context using the list so they are enabled in the same order the
4610 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
4611 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
4618 /* For each events */
4619 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
4621 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
4628 pthread_mutex_unlock(&ua_sess
->lock
);
4630 if (usess
->active
) {
4631 ret
= ust_app_start_trace(usess
, app
);
4636 DBG2("UST trace started for app pid %d", app
->pid
);
4639 /* Everything went well at this point. */
4644 pthread_mutex_unlock(&ua_sess
->lock
);
4647 destroy_app_session(app
, ua_sess
);
4654 * Add context to a specific channel for global UST domain.
4656 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
4657 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
4660 struct lttng_ht_node_str
*ua_chan_node
;
4661 struct lttng_ht_iter iter
, uiter
;
4662 struct ust_app_channel
*ua_chan
= NULL
;
4663 struct ust_app_session
*ua_sess
;
4664 struct ust_app
*app
;
4668 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4669 if (!app
->compatible
) {
4671 * TODO: In time, we should notice the caller of this error by
4672 * telling him that this is a version error.
4676 ua_sess
= lookup_session_by_app(usess
, app
);
4677 if (ua_sess
== NULL
) {
4681 pthread_mutex_lock(&ua_sess
->lock
);
4683 if (ua_sess
->deleted
) {
4684 pthread_mutex_unlock(&ua_sess
->lock
);
4688 /* Lookup channel in the ust app session */
4689 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4690 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4691 if (ua_chan_node
== NULL
) {
4694 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4696 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4701 pthread_mutex_unlock(&ua_sess
->lock
);
4709 * Enable event for a channel from a UST session for a specific PID.
4711 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4712 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4715 struct lttng_ht_iter iter
;
4716 struct lttng_ht_node_str
*ua_chan_node
;
4717 struct ust_app
*app
;
4718 struct ust_app_session
*ua_sess
;
4719 struct ust_app_channel
*ua_chan
;
4720 struct ust_app_event
*ua_event
;
4722 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4726 app
= ust_app_find_by_pid(pid
);
4728 ERR("UST app enable event per PID %d not found", pid
);
4733 if (!app
->compatible
) {
4738 ua_sess
= lookup_session_by_app(usess
, app
);
4740 /* The application has problem or is probably dead. */
4745 pthread_mutex_lock(&ua_sess
->lock
);
4747 if (ua_sess
->deleted
) {
4752 /* Lookup channel in the ust app session */
4753 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4754 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4755 /* If the channel is not found, there is a code flow error */
4756 assert(ua_chan_node
);
4758 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4760 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4761 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4762 if (ua_event
== NULL
) {
4763 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4768 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4775 pthread_mutex_unlock(&ua_sess
->lock
);
4782 * Calibrate registered applications.
4784 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
4787 struct lttng_ht_iter iter
;
4788 struct ust_app
*app
;
4792 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4793 if (!app
->compatible
) {
4795 * TODO: In time, we should notice the caller of this error by
4796 * telling him that this is a version error.
4801 health_code_update();
4803 pthread_mutex_lock(&app
->sock_lock
);
4804 ret
= ustctl_calibrate(app
->sock
, calibrate
);
4805 pthread_mutex_unlock(&app
->sock_lock
);
4809 /* Means that it's not implemented on the tracer side. */
4813 DBG2("Calibrate app PID %d returned with error %d",
4820 DBG("UST app global domain calibration finished");
4824 health_code_update();
4830 * Receive registration and populate the given msg structure.
4832 * On success return 0 else a negative value returned by the ustctl call.
4834 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
4837 uint32_t pid
, ppid
, uid
, gid
;
4841 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
4842 &pid
, &ppid
, &uid
, &gid
,
4843 &msg
->bits_per_long
,
4844 &msg
->uint8_t_alignment
,
4845 &msg
->uint16_t_alignment
,
4846 &msg
->uint32_t_alignment
,
4847 &msg
->uint64_t_alignment
,
4848 &msg
->long_alignment
,
4855 case LTTNG_UST_ERR_EXITING
:
4856 DBG3("UST app recv reg message failed. Application died");
4858 case LTTNG_UST_ERR_UNSUP_MAJOR
:
4859 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
4860 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
4861 LTTNG_UST_ABI_MINOR_VERSION
);
4864 ERR("UST app recv reg message failed with ret %d", ret
);
4869 msg
->pid
= (pid_t
) pid
;
4870 msg
->ppid
= (pid_t
) ppid
;
4871 msg
->uid
= (uid_t
) uid
;
4872 msg
->gid
= (gid_t
) gid
;
4879 * Return a ust app channel object using the application object and the channel
4880 * object descriptor has a key. If not found, NULL is returned. A RCU read side
4881 * lock MUST be acquired before calling this function.
4883 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
4886 struct lttng_ht_node_ulong
*node
;
4887 struct lttng_ht_iter iter
;
4888 struct ust_app_channel
*ua_chan
= NULL
;
4892 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
4893 node
= lttng_ht_iter_get_node_ulong(&iter
);
4895 DBG2("UST app channel find by objd %d not found", objd
);
4899 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
4906 * Reply to a register channel notification from an application on the notify
4907 * socket. The channel metadata is also created.
4909 * The session UST registry lock is acquired in this function.
4911 * On success 0 is returned else a negative value.
4913 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
4914 size_t nr_fields
, struct ustctl_field
*fields
)
4916 int ret
, ret_code
= 0;
4917 uint32_t chan_id
, reg_count
;
4918 uint64_t chan_reg_key
;
4919 enum ustctl_channel_header type
;
4920 struct ust_app
*app
;
4921 struct ust_app_channel
*ua_chan
;
4922 struct ust_app_session
*ua_sess
;
4923 struct ust_registry_session
*registry
;
4924 struct ust_registry_channel
*chan_reg
;
4928 /* Lookup application. If not found, there is a code flow error. */
4929 app
= find_app_by_notify_sock(sock
);
4931 DBG("Application socket %d is being teardown. Abort event notify",
4935 goto error_rcu_unlock
;
4938 /* Lookup channel by UST object descriptor. */
4939 ua_chan
= find_channel_by_objd(app
, cobjd
);
4941 DBG("Application channel is being teardown. Abort event notify");
4944 goto error_rcu_unlock
;
4947 assert(ua_chan
->session
);
4948 ua_sess
= ua_chan
->session
;
4950 /* Get right session registry depending on the session buffer type. */
4951 registry
= get_session_registry(ua_sess
);
4954 /* Depending on the buffer type, a different channel key is used. */
4955 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4956 chan_reg_key
= ua_chan
->tracing_channel_id
;
4958 chan_reg_key
= ua_chan
->key
;
4961 pthread_mutex_lock(®istry
->lock
);
4963 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
4966 if (!chan_reg
->register_done
) {
4967 reg_count
= ust_registry_get_event_count(chan_reg
);
4968 if (reg_count
< 31) {
4969 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
4971 type
= USTCTL_CHANNEL_HEADER_LARGE
;
4974 chan_reg
->nr_ctx_fields
= nr_fields
;
4975 chan_reg
->ctx_fields
= fields
;
4976 chan_reg
->header_type
= type
;
4978 /* Get current already assigned values. */
4979 type
= chan_reg
->header_type
;
4981 /* Set to NULL so the error path does not do a double free. */
4984 /* Channel id is set during the object creation. */
4985 chan_id
= chan_reg
->chan_id
;
4987 /* Append to metadata */
4988 if (!chan_reg
->metadata_dumped
) {
4989 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
4991 ERR("Error appending channel metadata (errno = %d)", ret_code
);
4997 DBG3("UST app replying to register channel key %" PRIu64
4998 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5001 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5003 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5004 ERR("UST app reply channel failed with ret %d", ret
);
5006 DBG3("UST app reply channel failed. Application died");
5011 /* This channel registry registration is completed. */
5012 chan_reg
->register_done
= 1;
5015 pthread_mutex_unlock(®istry
->lock
);
5025 * Add event to the UST channel registry. When the event is added to the
5026 * registry, the metadata is also created. Once done, this replies to the
5027 * application with the appropriate error code.
5029 * The session UST registry lock is acquired in the function.
5031 * On success 0 is returned else a negative value.
5033 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5034 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
5035 char *model_emf_uri
)
5038 uint32_t event_id
= 0;
5039 uint64_t chan_reg_key
;
5040 struct ust_app
*app
;
5041 struct ust_app_channel
*ua_chan
;
5042 struct ust_app_session
*ua_sess
;
5043 struct ust_registry_session
*registry
;
5047 /* Lookup application. If not found, there is a code flow error. */
5048 app
= find_app_by_notify_sock(sock
);
5050 DBG("Application socket %d is being teardown. Abort event notify",
5055 free(model_emf_uri
);
5056 goto error_rcu_unlock
;
5059 /* Lookup channel by UST object descriptor. */
5060 ua_chan
= find_channel_by_objd(app
, cobjd
);
5062 DBG("Application channel is being teardown. Abort event notify");
5066 free(model_emf_uri
);
5067 goto error_rcu_unlock
;
5070 assert(ua_chan
->session
);
5071 ua_sess
= ua_chan
->session
;
5073 registry
= get_session_registry(ua_sess
);
5076 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5077 chan_reg_key
= ua_chan
->tracing_channel_id
;
5079 chan_reg_key
= ua_chan
->key
;
5082 pthread_mutex_lock(®istry
->lock
);
5085 * From this point on, this call acquires the ownership of the sig, fields
5086 * and model_emf_uri meaning any free are done inside it if needed. These
5087 * three variables MUST NOT be read/write after this.
5089 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5090 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
, loglevel
,
5091 model_emf_uri
, ua_sess
->buffer_type
, &event_id
,
5095 * The return value is returned to ustctl so in case of an error, the
5096 * application can be notified. In case of an error, it's important not to
5097 * return a negative error or else the application will get closed.
5099 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5101 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5102 ERR("UST app reply event failed with ret %d", ret
);
5104 DBG3("UST app reply event failed. Application died");
5107 * No need to wipe the create event since the application socket will
5108 * get close on error hence cleaning up everything by itself.
5113 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5117 pthread_mutex_unlock(®istry
->lock
);
5124 * Handle application notification through the given notify socket.
5126 * Return 0 on success or else a negative value.
5128 int ust_app_recv_notify(int sock
)
5131 enum ustctl_notify_cmd cmd
;
5133 DBG3("UST app receiving notify from sock %d", sock
);
5135 ret
= ustctl_recv_notify(sock
, &cmd
);
5137 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5138 ERR("UST app recv notify failed with ret %d", ret
);
5140 DBG3("UST app recv notify failed. Application died");
5146 case USTCTL_NOTIFY_CMD_EVENT
:
5148 int sobjd
, cobjd
, loglevel
;
5149 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5151 struct ustctl_field
*fields
;
5153 DBG2("UST app ustctl register event received");
5155 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
5156 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
5158 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5159 ERR("UST app recv event failed with ret %d", ret
);
5161 DBG3("UST app recv event failed. Application died");
5167 * Add event to the UST registry coming from the notify socket. This
5168 * call will free if needed the sig, fields and model_emf_uri. This
5169 * code path loses the ownsership of these variables and transfer them
5170 * to the this function.
5172 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5173 fields
, loglevel
, model_emf_uri
);
5180 case USTCTL_NOTIFY_CMD_CHANNEL
:
5184 struct ustctl_field
*fields
;
5186 DBG2("UST app ustctl register channel received");
5188 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5191 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5192 ERR("UST app recv channel failed with ret %d", ret
);
5194 DBG3("UST app recv channel failed. Application died");
5200 * The fields ownership are transfered to this function call meaning
5201 * that if needed it will be freed. After this, it's invalid to access
5202 * fields or clean it up.
5204 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
5213 /* Should NEVER happen. */
5222 * Once the notify socket hangs up, this is called. First, it tries to find the
5223 * corresponding application. On failure, the call_rcu to close the socket is
5224 * executed. If an application is found, it tries to delete it from the notify
5225 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5227 * Note that an object needs to be allocated here so on ENOMEM failure, the
5228 * call RCU is not done but the rest of the cleanup is.
5230 void ust_app_notify_sock_unregister(int sock
)
5233 struct lttng_ht_iter iter
;
5234 struct ust_app
*app
;
5235 struct ust_app_notify_sock_obj
*obj
;
5241 obj
= zmalloc(sizeof(*obj
));
5244 * An ENOMEM is kind of uncool. If this strikes we continue the
5245 * procedure but the call_rcu will not be called. In this case, we
5246 * accept the fd leak rather than possibly creating an unsynchronized
5247 * state between threads.
5249 * TODO: The notify object should be created once the notify socket is
5250 * registered and stored independantely from the ust app object. The
5251 * tricky part is to synchronize the teardown of the application and
5252 * this notify object. Let's keep that in mind so we can avoid this
5253 * kind of shenanigans with ENOMEM in the teardown path.
5260 DBG("UST app notify socket unregister %d", sock
);
5263 * Lookup application by notify socket. If this fails, this means that the
5264 * hash table delete has already been done by the application
5265 * unregistration process so we can safely close the notify socket in a
5268 app
= find_app_by_notify_sock(sock
);
5273 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5276 * Whatever happens here either we fail or succeed, in both cases we have
5277 * to close the socket after a grace period to continue to the call RCU
5278 * here. If the deletion is successful, the application is not visible
5279 * anymore by other threads and is it fails it means that it was already
5280 * deleted from the hash table so either way we just have to close the
5283 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5289 * Close socket after a grace period to avoid for the socket to be reused
5290 * before the application object is freed creating potential race between
5291 * threads trying to add unique in the global hash table.
5294 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5299 * Destroy a ust app data structure and free its memory.
5301 void ust_app_destroy(struct ust_app
*app
)
5307 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5311 * Take a snapshot for a given UST session. The snapshot is sent to the given
5314 * Return 0 on success or else a negative value.
5316 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5317 struct snapshot_output
*output
, int wait
,
5318 uint64_t nb_packets_per_stream
)
5321 unsigned int snapshot_done
= 0;
5322 struct lttng_ht_iter iter
;
5323 struct ust_app
*app
;
5324 char pathname
[PATH_MAX
];
5331 switch (usess
->buffer_type
) {
5332 case LTTNG_BUFFER_PER_UID
:
5334 struct buffer_reg_uid
*reg
;
5336 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5337 struct buffer_reg_channel
*reg_chan
;
5338 struct consumer_socket
*socket
;
5340 /* Get consumer socket to use to push the metadata.*/
5341 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5348 memset(pathname
, 0, sizeof(pathname
));
5349 ret
= snprintf(pathname
, sizeof(pathname
),
5350 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5351 reg
->uid
, reg
->bits_per_long
);
5353 PERROR("snprintf snapshot path");
5357 /* Add the UST default trace dir to path. */
5358 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5359 reg_chan
, node
.node
) {
5360 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5361 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5362 nb_packets_per_stream
);
5367 ret
= consumer_snapshot_channel(socket
,
5368 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5369 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
5377 case LTTNG_BUFFER_PER_PID
:
5379 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5380 struct consumer_socket
*socket
;
5381 struct lttng_ht_iter chan_iter
;
5382 struct ust_app_channel
*ua_chan
;
5383 struct ust_app_session
*ua_sess
;
5384 struct ust_registry_session
*registry
;
5386 ua_sess
= lookup_session_by_app(usess
, app
);
5388 /* Session not associated with this app. */
5392 /* Get the right consumer socket for the application. */
5393 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5400 /* Add the UST default trace dir to path. */
5401 memset(pathname
, 0, sizeof(pathname
));
5402 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5405 PERROR("snprintf snapshot path");
5409 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5410 ua_chan
, node
.node
) {
5411 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
5412 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
5413 nb_packets_per_stream
);
5419 registry
= get_session_registry(ua_sess
);
5421 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
5422 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
5435 if (!snapshot_done
) {
5437 * If no snapshot was made and we are not in the error path, this means
5438 * that there are no buffers thus no (prior) application to snapshot
5439 * data from so we have simply NO data.
5450 * Return the size taken by one more packet per stream.
5452 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
5453 uint64_t cur_nr_packets
)
5455 uint64_t tot_size
= 0;
5456 struct ust_app
*app
;
5457 struct lttng_ht_iter iter
;
5461 switch (usess
->buffer_type
) {
5462 case LTTNG_BUFFER_PER_UID
:
5464 struct buffer_reg_uid
*reg
;
5466 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5467 struct buffer_reg_channel
*reg_chan
;
5470 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5471 reg_chan
, node
.node
) {
5472 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
5474 * Don't take channel into account if we
5475 * already grab all its packets.
5479 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
5485 case LTTNG_BUFFER_PER_PID
:
5488 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5489 struct ust_app_channel
*ua_chan
;
5490 struct ust_app_session
*ua_sess
;
5491 struct lttng_ht_iter chan_iter
;
5493 ua_sess
= lookup_session_by_app(usess
, app
);
5495 /* Session not associated with this app. */
5499 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5500 ua_chan
, node
.node
) {
5501 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
5503 * Don't take channel into account if we
5504 * already grab all its packets.
5508 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;