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
)
271 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
272 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
273 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
274 sock
, ua_ctx
->obj
->handle
, ret
);
282 * Delete ust app event safely. RCU read lock must be held before calling
286 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
292 free(ua_event
->filter
);
293 if (ua_event
->exclusion
!= NULL
)
294 free(ua_event
->exclusion
);
295 if (ua_event
->obj
!= NULL
) {
296 ret
= ustctl_release_object(sock
, ua_event
->obj
);
297 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
298 ERR("UST app sock %d release event obj failed with ret %d",
307 * Release ust data object of the given stream.
309 * Return 0 on success or else a negative value.
311 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
318 ret
= ustctl_release_object(sock
, stream
->obj
);
319 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
320 ERR("UST app sock %d release stream obj failed with ret %d",
323 lttng_fd_put(LTTNG_FD_APPS
, 2);
331 * Delete ust app stream safely. RCU read lock must be held before calling
335 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
339 (void) release_ust_app_stream(sock
, stream
);
344 * We need to execute ht_destroy outside of RCU read-side critical
345 * section and outside of call_rcu thread, so we postpone its execution
346 * using ht_cleanup_push. It is simpler than to change the semantic of
347 * the many callers of delete_ust_app_session().
350 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
352 struct ust_app_channel
*ua_chan
=
353 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
355 ht_cleanup_push(ua_chan
->ctx
);
356 ht_cleanup_push(ua_chan
->events
);
361 * Delete ust app channel safely. RCU read lock must be held before calling
365 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
369 struct lttng_ht_iter iter
;
370 struct ust_app_event
*ua_event
;
371 struct ust_app_ctx
*ua_ctx
;
372 struct ust_app_stream
*stream
, *stmp
;
373 struct ust_registry_session
*registry
;
377 DBG3("UST app deleting channel %s", ua_chan
->name
);
380 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
381 cds_list_del(&stream
->list
);
382 delete_ust_app_stream(sock
, stream
);
386 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
387 cds_list_del(&ua_ctx
->list
);
388 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
390 delete_ust_app_ctx(sock
, ua_ctx
);
394 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
396 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
398 delete_ust_app_event(sock
, ua_event
);
401 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
402 /* Wipe and free registry from session registry. */
403 registry
= get_session_registry(ua_chan
->session
);
405 ust_registry_channel_del_free(registry
, ua_chan
->key
);
409 if (ua_chan
->obj
!= NULL
) {
410 /* Remove channel from application UST object descriptor. */
411 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
412 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
414 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
415 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
416 ERR("UST app sock %d release channel obj failed with ret %d",
419 lttng_fd_put(LTTNG_FD_APPS
, 1);
422 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
426 * Push metadata to consumer socket.
428 * RCU read-side lock must be held to guarantee existance of socket.
429 * Must be called with the ust app session lock held.
430 * Must be called with the registry lock held.
432 * On success, return the len of metadata pushed or else a negative value.
433 * Returning a -EPIPE return value means we could not send the metadata,
434 * but it can be caused by recoverable errors (e.g. the application has
435 * terminated concurrently).
437 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
438 struct consumer_socket
*socket
, int send_zero_data
)
441 char *metadata_str
= NULL
;
442 size_t len
, offset
, new_metadata_len_sent
;
444 uint64_t metadata_key
;
449 metadata_key
= registry
->metadata_key
;
452 * Means that no metadata was assigned to the session. This can
453 * happens if no start has been done previously.
460 * On a push metadata error either the consumer is dead or the
461 * metadata channel has been destroyed because its endpoint
462 * might have died (e.g: relayd), or because the application has
463 * exited. If so, the metadata closed flag is set to 1 so we
464 * deny pushing metadata again which is not valid anymore on the
467 if (registry
->metadata_closed
) {
471 offset
= registry
->metadata_len_sent
;
472 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
473 new_metadata_len_sent
= registry
->metadata_len
;
475 DBG3("No metadata to push for metadata key %" PRIu64
,
476 registry
->metadata_key
);
478 if (send_zero_data
) {
479 DBG("No metadata to push");
485 /* Allocate only what we have to send. */
486 metadata_str
= zmalloc(len
);
488 PERROR("zmalloc ust app metadata string");
492 /* Copy what we haven't sent out. */
493 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
496 pthread_mutex_unlock(®istry
->lock
);
498 * We need to unlock the registry while we push metadata to
499 * break a circular dependency between the consumerd metadata
500 * lock and the sessiond registry lock. Indeed, pushing metadata
501 * to the consumerd awaits that it gets pushed all the way to
502 * relayd, but doing so requires grabbing the metadata lock. If
503 * a concurrent metadata request is being performed by
504 * consumerd, this can try to grab the registry lock on the
505 * sessiond while holding the metadata lock on the consumer
506 * daemon. Those push and pull schemes are performed on two
507 * different bidirectionnal communication sockets.
509 ret
= consumer_push_metadata(socket
, metadata_key
,
510 metadata_str
, len
, offset
);
511 pthread_mutex_lock(®istry
->lock
);
514 * There is an acceptable race here between the registry
515 * metadata key assignment and the creation on the
516 * consumer. The session daemon can concurrently push
517 * metadata for this registry while being created on the
518 * consumer since the metadata key of the registry is
519 * assigned *before* it is setup to avoid the consumer
520 * to ask for metadata that could possibly be not found
521 * in the session daemon.
523 * The metadata will get pushed either by the session
524 * being stopped or the consumer requesting metadata if
525 * that race is triggered.
527 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
530 ERR("Error pushing metadata to consumer");
536 * Metadata may have been concurrently pushed, since
537 * we're not holding the registry lock while pushing to
538 * consumer. This is handled by the fact that we send
539 * the metadata content, size, and the offset at which
540 * that metadata belongs. This may arrive out of order
541 * on the consumer side, and the consumer is able to
542 * deal with overlapping fragments. The consumer
543 * supports overlapping fragments, which must be
544 * contiguous starting from offset 0. We keep the
545 * largest metadata_len_sent value of the concurrent
548 registry
->metadata_len_sent
=
549 max_t(size_t, registry
->metadata_len_sent
,
550 new_metadata_len_sent
);
559 * On error, flag the registry that the metadata is
560 * closed. We were unable to push anything and this
561 * means that either the consumer is not responding or
562 * the metadata cache has been destroyed on the
565 registry
->metadata_closed
= 1;
573 * For a given application and session, push metadata to consumer.
574 * Either sock or consumer is required : if sock is NULL, the default
575 * socket to send the metadata is retrieved from consumer, if sock
576 * is not NULL we use it to send the metadata.
577 * RCU read-side lock must be held while calling this function,
578 * therefore ensuring existance of registry. It also ensures existance
579 * of socket throughout this function.
581 * Return 0 on success else a negative error.
582 * Returning a -EPIPE return value means we could not send the metadata,
583 * but it can be caused by recoverable errors (e.g. the application has
584 * terminated concurrently).
586 static int push_metadata(struct ust_registry_session
*registry
,
587 struct consumer_output
*consumer
)
591 struct consumer_socket
*socket
;
596 pthread_mutex_lock(®istry
->lock
);
597 if (registry
->metadata_closed
) {
602 /* Get consumer socket to use to push the metadata.*/
603 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
610 ret
= ust_app_push_metadata(registry
, socket
, 0);
615 pthread_mutex_unlock(®istry
->lock
);
619 pthread_mutex_unlock(®istry
->lock
);
624 * Send to the consumer a close metadata command for the given session. Once
625 * done, the metadata channel is deleted and the session metadata pointer is
626 * nullified. The session lock MUST be held unless the application is
627 * in the destroy path.
629 * Return 0 on success else a negative value.
631 static int close_metadata(struct ust_registry_session
*registry
,
632 struct consumer_output
*consumer
)
635 struct consumer_socket
*socket
;
642 pthread_mutex_lock(®istry
->lock
);
644 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
649 /* Get consumer socket to use to push the metadata.*/
650 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
657 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
664 * Metadata closed. Even on error this means that the consumer is not
665 * responding or not found so either way a second close should NOT be emit
668 registry
->metadata_closed
= 1;
670 pthread_mutex_unlock(®istry
->lock
);
676 * We need to execute ht_destroy outside of RCU read-side critical
677 * section and outside of call_rcu thread, so we postpone its execution
678 * using ht_cleanup_push. It is simpler than to change the semantic of
679 * the many callers of delete_ust_app_session().
682 void delete_ust_app_session_rcu(struct rcu_head
*head
)
684 struct ust_app_session
*ua_sess
=
685 caa_container_of(head
, struct ust_app_session
, rcu_head
);
687 ht_cleanup_push(ua_sess
->channels
);
692 * Delete ust app session safely. RCU read lock must be held before calling
696 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
700 struct lttng_ht_iter iter
;
701 struct ust_app_channel
*ua_chan
;
702 struct ust_registry_session
*registry
;
706 pthread_mutex_lock(&ua_sess
->lock
);
708 assert(!ua_sess
->deleted
);
709 ua_sess
->deleted
= true;
711 registry
= get_session_registry(ua_sess
);
713 /* Push metadata for application before freeing the application. */
714 (void) push_metadata(registry
, ua_sess
->consumer
);
717 * Don't ask to close metadata for global per UID buffers. Close
718 * metadata only on destroy trace session in this case. Also, the
719 * previous push metadata could have flag the metadata registry to
720 * close so don't send a close command if closed.
722 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
723 /* And ask to close it for this session registry. */
724 (void) close_metadata(registry
, ua_sess
->consumer
);
728 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
730 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
732 delete_ust_app_channel(sock
, ua_chan
, app
);
735 /* In case of per PID, the registry is kept in the session. */
736 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
737 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
739 buffer_reg_pid_remove(reg_pid
);
740 buffer_reg_pid_destroy(reg_pid
);
744 if (ua_sess
->handle
!= -1) {
745 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
746 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
747 ERR("UST app sock %d release session handle failed with ret %d",
751 pthread_mutex_unlock(&ua_sess
->lock
);
753 consumer_output_put(ua_sess
->consumer
);
755 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
759 * Delete a traceable application structure from the global list. Never call
760 * this function outside of a call_rcu call.
762 * RCU read side lock should _NOT_ be held when calling this function.
765 void delete_ust_app(struct ust_app
*app
)
768 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
770 /* Delete ust app sessions info */
775 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
777 /* Free every object in the session and the session. */
779 delete_ust_app_session(sock
, ua_sess
, app
);
783 ht_cleanup_push(app
->sessions
);
784 ht_cleanup_push(app
->ust_objd
);
787 * Wait until we have deleted the application from the sock hash table
788 * before closing this socket, otherwise an application could re-use the
789 * socket ID and race with the teardown, using the same hash table entry.
791 * It's OK to leave the close in call_rcu. We want it to stay unique for
792 * all RCU readers that could run concurrently with unregister app,
793 * therefore we _need_ to only close that socket after a grace period. So
794 * it should stay in this RCU callback.
796 * This close() is a very important step of the synchronization model so
797 * every modification to this function must be carefully reviewed.
803 lttng_fd_put(LTTNG_FD_APPS
, 1);
805 DBG2("UST app pid %d deleted", app
->pid
);
810 * URCU intermediate call to delete an UST app.
813 void delete_ust_app_rcu(struct rcu_head
*head
)
815 struct lttng_ht_node_ulong
*node
=
816 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
817 struct ust_app
*app
=
818 caa_container_of(node
, struct ust_app
, pid_n
);
820 DBG3("Call RCU deleting app PID %d", app
->pid
);
825 * Delete the session from the application ht and delete the data structure by
826 * freeing every object inside and releasing them.
828 static void destroy_app_session(struct ust_app
*app
,
829 struct ust_app_session
*ua_sess
)
832 struct lttng_ht_iter iter
;
837 iter
.iter
.node
= &ua_sess
->node
.node
;
838 ret
= lttng_ht_del(app
->sessions
, &iter
);
840 /* Already scheduled for teardown. */
844 /* Once deleted, free the data structure. */
845 delete_ust_app_session(app
->sock
, ua_sess
, app
);
852 * Alloc new UST app session.
855 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
857 struct ust_app_session
*ua_sess
;
859 /* Init most of the default value by allocating and zeroing */
860 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
861 if (ua_sess
== NULL
) {
866 ua_sess
->handle
= -1;
867 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
868 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
869 pthread_mutex_init(&ua_sess
->lock
, NULL
);
878 * Alloc new UST app channel.
881 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
882 struct ust_app_session
*ua_sess
,
883 struct lttng_ust_channel_attr
*attr
)
885 struct ust_app_channel
*ua_chan
;
887 /* Init most of the default value by allocating and zeroing */
888 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
889 if (ua_chan
== NULL
) {
894 /* Setup channel name */
895 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
896 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
898 ua_chan
->enabled
= 1;
899 ua_chan
->handle
= -1;
900 ua_chan
->session
= ua_sess
;
901 ua_chan
->key
= get_next_channel_key();
902 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
903 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
904 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
906 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
907 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
909 /* Copy attributes */
911 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
912 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
913 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
914 ua_chan
->attr
.overwrite
= attr
->overwrite
;
915 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
916 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
917 ua_chan
->attr
.output
= attr
->output
;
919 /* By default, the channel is a per cpu channel. */
920 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
922 DBG3("UST app channel %s allocated", ua_chan
->name
);
931 * Allocate and initialize a UST app stream.
933 * Return newly allocated stream pointer or NULL on error.
935 struct ust_app_stream
*ust_app_alloc_stream(void)
937 struct ust_app_stream
*stream
= NULL
;
939 stream
= zmalloc(sizeof(*stream
));
940 if (stream
== NULL
) {
941 PERROR("zmalloc ust app stream");
945 /* Zero could be a valid value for a handle so flag it to -1. */
953 * Alloc new UST app event.
956 struct ust_app_event
*alloc_ust_app_event(char *name
,
957 struct lttng_ust_event
*attr
)
959 struct ust_app_event
*ua_event
;
961 /* Init most of the default value by allocating and zeroing */
962 ua_event
= zmalloc(sizeof(struct ust_app_event
));
963 if (ua_event
== NULL
) {
968 ua_event
->enabled
= 1;
969 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
970 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
971 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
973 /* Copy attributes */
975 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
978 DBG3("UST app event %s allocated", ua_event
->name
);
987 * Alloc new UST app context.
990 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
992 struct ust_app_ctx
*ua_ctx
;
994 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
995 if (ua_ctx
== NULL
) {
999 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1002 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1005 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1012 * Allocate a filter and copy the given original filter.
1014 * Return allocated filter or NULL on error.
1016 static struct lttng_ust_filter_bytecode
*alloc_copy_ust_app_filter(
1017 struct lttng_ust_filter_bytecode
*orig_f
)
1019 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1021 /* Copy filter bytecode */
1022 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1024 PERROR("zmalloc alloc ust app filter");
1028 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1035 * Find an ust_app using the sock and return it. RCU read side lock must be
1036 * held before calling this helper function.
1038 struct ust_app
*ust_app_find_by_sock(int sock
)
1040 struct lttng_ht_node_ulong
*node
;
1041 struct lttng_ht_iter iter
;
1043 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1044 node
= lttng_ht_iter_get_node_ulong(&iter
);
1046 DBG2("UST app find by sock %d not found", sock
);
1050 return caa_container_of(node
, struct ust_app
, sock_n
);
1057 * Find an ust_app using the notify sock and return it. RCU read side lock must
1058 * be held before calling this helper function.
1060 static struct ust_app
*find_app_by_notify_sock(int sock
)
1062 struct lttng_ht_node_ulong
*node
;
1063 struct lttng_ht_iter iter
;
1065 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1067 node
= lttng_ht_iter_get_node_ulong(&iter
);
1069 DBG2("UST app find by notify sock %d not found", sock
);
1073 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1080 * Lookup for an ust app event based on event name, filter bytecode and the
1083 * Return an ust_app_event object or NULL on error.
1085 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1086 char *name
, struct lttng_ust_filter_bytecode
*filter
, int loglevel
,
1087 const struct lttng_event_exclusion
*exclusion
)
1089 struct lttng_ht_iter iter
;
1090 struct lttng_ht_node_str
*node
;
1091 struct ust_app_event
*event
= NULL
;
1092 struct ust_app_ht_key key
;
1097 /* Setup key for event lookup. */
1099 key
.filter
= filter
;
1100 key
.loglevel
= loglevel
;
1101 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1102 key
.exclusion
= (struct lttng_ust_event_exclusion
*)exclusion
;
1104 /* Lookup using the event name as hash and a custom match fct. */
1105 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1106 ht_match_ust_app_event
, &key
, &iter
.iter
);
1107 node
= lttng_ht_iter_get_node_str(&iter
);
1112 event
= caa_container_of(node
, struct ust_app_event
, node
);
1119 * Create the channel context on the tracer.
1121 * Called with UST app session lock held.
1124 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1125 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1129 health_code_update();
1131 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1132 ua_chan
->obj
, &ua_ctx
->obj
);
1134 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1135 ERR("UST app create channel context failed for app (pid: %d) "
1136 "with ret %d", app
->pid
, ret
);
1139 * This is normal behavior, an application can die during the
1140 * creation process. Don't report an error so the execution can
1141 * continue normally.
1144 DBG3("UST app disable event failed. Application is dead.");
1149 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1151 DBG2("UST app context handle %d created successfully for channel %s",
1152 ua_ctx
->handle
, ua_chan
->name
);
1155 health_code_update();
1160 * Set the filter on the tracer.
1163 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1164 struct ust_app
*app
)
1168 health_code_update();
1170 if (!ua_event
->filter
) {
1175 ret
= ustctl_set_filter(app
->sock
, ua_event
->filter
,
1178 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1179 ERR("UST app event %s filter failed for app (pid: %d) "
1180 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1183 * This is normal behavior, an application can die during the
1184 * creation process. Don't report an error so the execution can
1185 * continue normally.
1188 DBG3("UST app filter event failed. Application is dead.");
1193 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1196 health_code_update();
1201 * Set event exclusions on the tracer.
1204 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1205 struct ust_app
*app
)
1209 health_code_update();
1211 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1216 ret
= ustctl_set_exclusion(app
->sock
, ua_event
->exclusion
,
1219 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1220 ERR("UST app event %s exclusions failed for app (pid: %d) "
1221 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1224 * This is normal behavior, an application can die during the
1225 * creation process. Don't report an error so the execution can
1226 * continue normally.
1229 DBG3("UST app event exclusion failed. Application is dead.");
1234 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1237 health_code_update();
1242 * Disable the specified event on to UST tracer for the UST session.
1244 static int disable_ust_event(struct ust_app
*app
,
1245 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1249 health_code_update();
1251 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1253 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1254 ERR("UST app event %s disable failed for app (pid: %d) "
1255 "and session handle %d with ret %d",
1256 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1259 * This is normal behavior, an application can die during the
1260 * creation process. Don't report an error so the execution can
1261 * continue normally.
1264 DBG3("UST app disable event failed. Application is dead.");
1269 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1270 ua_event
->attr
.name
, app
->pid
);
1273 health_code_update();
1278 * Disable the specified channel on to UST tracer for the UST session.
1280 static int disable_ust_channel(struct ust_app
*app
,
1281 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1285 health_code_update();
1287 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1289 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1290 ERR("UST app channel %s disable failed for app (pid: %d) "
1291 "and session handle %d with ret %d",
1292 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1295 * This is normal behavior, an application can die during the
1296 * creation process. Don't report an error so the execution can
1297 * continue normally.
1300 DBG3("UST app disable channel failed. Application is dead.");
1305 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1306 ua_chan
->name
, app
->pid
);
1309 health_code_update();
1314 * Enable the specified channel on to UST tracer for the UST session.
1316 static int enable_ust_channel(struct ust_app
*app
,
1317 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1321 health_code_update();
1323 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1325 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1326 ERR("UST app channel %s enable failed for app (pid: %d) "
1327 "and session handle %d with ret %d",
1328 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1331 * This is normal behavior, an application can die during the
1332 * creation process. Don't report an error so the execution can
1333 * continue normally.
1336 DBG3("UST app enable channel failed. Application is dead.");
1341 ua_chan
->enabled
= 1;
1343 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1344 ua_chan
->name
, app
->pid
);
1347 health_code_update();
1352 * Enable the specified event on to UST tracer for the UST session.
1354 static int enable_ust_event(struct ust_app
*app
,
1355 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1359 health_code_update();
1361 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1363 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1364 ERR("UST app event %s enable failed for app (pid: %d) "
1365 "and session handle %d with ret %d",
1366 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1369 * This is normal behavior, an application can die during the
1370 * creation process. Don't report an error so the execution can
1371 * continue normally.
1374 DBG3("UST app enable event failed. Application is dead.");
1379 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1380 ua_event
->attr
.name
, app
->pid
);
1383 health_code_update();
1388 * Send channel and stream buffer to application.
1390 * Return 0 on success. On error, a negative value is returned.
1392 static int send_channel_pid_to_ust(struct ust_app
*app
,
1393 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1396 struct ust_app_stream
*stream
, *stmp
;
1402 health_code_update();
1404 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1407 /* Send channel to the application. */
1408 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1409 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1410 ret
= -ENOTCONN
; /* Caused by app exiting. */
1412 } else if (ret
< 0) {
1416 health_code_update();
1418 /* Send all streams to application. */
1419 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1420 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1421 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1422 ret
= -ENOTCONN
; /* Caused by app exiting. */
1424 } else if (ret
< 0) {
1427 /* We don't need the stream anymore once sent to the tracer. */
1428 cds_list_del(&stream
->list
);
1429 delete_ust_app_stream(-1, stream
);
1431 /* Flag the channel that it is sent to the application. */
1432 ua_chan
->is_sent
= 1;
1435 health_code_update();
1440 * Create the specified event onto the UST tracer for a UST session.
1442 * Should be called with session mutex held.
1445 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1446 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1450 health_code_update();
1452 /* Create UST event on tracer */
1453 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1456 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1457 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1458 ua_event
->attr
.name
, app
->pid
, ret
);
1461 * This is normal behavior, an application can die during the
1462 * creation process. Don't report an error so the execution can
1463 * continue normally.
1466 DBG3("UST app create event failed. Application is dead.");
1471 ua_event
->handle
= ua_event
->obj
->handle
;
1473 DBG2("UST app event %s created successfully for pid:%d",
1474 ua_event
->attr
.name
, app
->pid
);
1476 health_code_update();
1478 /* Set filter if one is present. */
1479 if (ua_event
->filter
) {
1480 ret
= set_ust_event_filter(ua_event
, app
);
1486 /* Set exclusions for the event */
1487 if (ua_event
->exclusion
) {
1488 ret
= set_ust_event_exclusion(ua_event
, app
);
1494 /* If event not enabled, disable it on the tracer */
1495 if (ua_event
->enabled
) {
1497 * We now need to explicitly enable the event, since it
1498 * is now disabled at creation.
1500 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1503 * If we hit an EPERM, something is wrong with our enable call. If
1504 * we get an EEXIST, there is a problem on the tracer side since we
1508 case -LTTNG_UST_ERR_PERM
:
1509 /* Code flow problem */
1511 case -LTTNG_UST_ERR_EXIST
:
1512 /* It's OK for our use case. */
1521 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1524 * If we hit an EPERM, something is wrong with our disable call. If
1525 * we get an EEXIST, there is a problem on the tracer side since we
1529 case -LTTNG_UST_ERR_PERM
:
1530 /* Code flow problem */
1532 case -LTTNG_UST_ERR_EXIST
:
1533 /* It's OK for our use case. */
1544 health_code_update();
1549 * Copy data between an UST app event and a LTT event.
1551 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1552 struct ltt_ust_event
*uevent
)
1554 size_t exclusion_alloc_size
;
1556 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1557 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1559 ua_event
->enabled
= uevent
->enabled
;
1561 /* Copy event attributes */
1562 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1564 /* Copy filter bytecode */
1565 if (uevent
->filter
) {
1566 ua_event
->filter
= alloc_copy_ust_app_filter(uevent
->filter
);
1567 /* Filter might be NULL here in case of ENONEM. */
1570 /* Copy exclusion data */
1571 if (uevent
->exclusion
) {
1572 exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1573 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1574 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1575 if (ua_event
->exclusion
== NULL
) {
1578 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1579 exclusion_alloc_size
);
1585 * Copy data between an UST app channel and a LTT channel.
1587 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1588 struct ltt_ust_channel
*uchan
)
1590 struct lttng_ht_iter iter
;
1591 struct ltt_ust_event
*uevent
;
1592 struct ltt_ust_context
*uctx
;
1593 struct ust_app_event
*ua_event
;
1594 struct ust_app_ctx
*ua_ctx
;
1596 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1598 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1599 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1601 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1602 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1604 /* Copy event attributes since the layout is different. */
1605 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1606 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1607 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1608 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1609 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1610 ua_chan
->attr
.output
= uchan
->attr
.output
;
1612 * Note that the attribute channel type is not set since the channel on the
1613 * tracing registry side does not have this information.
1616 ua_chan
->enabled
= uchan
->enabled
;
1617 ua_chan
->tracing_channel_id
= uchan
->id
;
1619 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1620 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1621 if (ua_ctx
== NULL
) {
1624 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1625 (unsigned long) ua_ctx
->ctx
.ctx
);
1626 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1627 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1630 /* Copy all events from ltt ust channel to ust app channel */
1631 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1632 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1633 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1634 if (ua_event
== NULL
) {
1635 DBG2("UST event %s not found on shadow copy channel",
1637 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1638 if (ua_event
== NULL
) {
1641 shadow_copy_event(ua_event
, uevent
);
1642 add_unique_ust_app_event(ua_chan
, ua_event
);
1646 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1650 * Copy data between a UST app session and a regular LTT session.
1652 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1653 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1655 struct lttng_ht_node_str
*ua_chan_node
;
1656 struct lttng_ht_iter iter
;
1657 struct ltt_ust_channel
*uchan
;
1658 struct ust_app_channel
*ua_chan
;
1660 struct tm
*timeinfo
;
1664 /* Get date and time for unique app path */
1666 timeinfo
= localtime(&rawtime
);
1667 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1669 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1671 ua_sess
->tracing_id
= usess
->id
;
1672 ua_sess
->id
= get_next_session_id();
1673 ua_sess
->uid
= app
->uid
;
1674 ua_sess
->gid
= app
->gid
;
1675 ua_sess
->euid
= usess
->uid
;
1676 ua_sess
->egid
= usess
->gid
;
1677 ua_sess
->buffer_type
= usess
->buffer_type
;
1678 ua_sess
->bits_per_long
= app
->bits_per_long
;
1680 /* There is only one consumer object per session possible. */
1681 consumer_output_get(usess
->consumer
);
1682 ua_sess
->consumer
= usess
->consumer
;
1684 ua_sess
->output_traces
= usess
->output_traces
;
1685 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1686 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1687 &usess
->metadata_attr
);
1689 switch (ua_sess
->buffer_type
) {
1690 case LTTNG_BUFFER_PER_PID
:
1691 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1692 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1695 case LTTNG_BUFFER_PER_UID
:
1696 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1697 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1704 PERROR("asprintf UST shadow copy session");
1709 /* Iterate over all channels in global domain. */
1710 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1712 struct lttng_ht_iter uiter
;
1714 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1715 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1716 if (ua_chan_node
!= NULL
) {
1717 /* Session exist. Contiuing. */
1721 DBG2("Channel %s not found on shadow session copy, creating it",
1723 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1724 if (ua_chan
== NULL
) {
1725 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1728 shadow_copy_channel(ua_chan
, uchan
);
1730 * The concept of metadata channel does not exist on the tracing
1731 * registry side of the session daemon so this can only be a per CPU
1732 * channel and not metadata.
1734 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1736 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1741 consumer_output_put(ua_sess
->consumer
);
1745 * Lookup sesison wrapper.
1748 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1749 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1751 /* Get right UST app session from app */
1752 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1756 * Return ust app session from the app session hashtable using the UST session
1759 static struct ust_app_session
*lookup_session_by_app(
1760 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1762 struct lttng_ht_iter iter
;
1763 struct lttng_ht_node_u64
*node
;
1765 __lookup_session_by_app(usess
, app
, &iter
);
1766 node
= lttng_ht_iter_get_node_u64(&iter
);
1771 return caa_container_of(node
, struct ust_app_session
, node
);
1778 * Setup buffer registry per PID for the given session and application. If none
1779 * is found, a new one is created, added to the global registry and
1780 * initialized. If regp is valid, it's set with the newly created object.
1782 * Return 0 on success or else a negative value.
1784 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1785 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1788 struct buffer_reg_pid
*reg_pid
;
1795 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1798 * This is the create channel path meaning that if there is NO
1799 * registry available, we have to create one for this session.
1801 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
);
1805 buffer_reg_pid_add(reg_pid
);
1810 /* Initialize registry. */
1811 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1812 app
->bits_per_long
, app
->uint8_t_alignment
,
1813 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1814 app
->uint64_t_alignment
, app
->long_alignment
,
1815 app
->byte_order
, app
->version
.major
,
1816 app
->version
.minor
);
1821 DBG3("UST app buffer registry per PID created successfully");
1833 * Setup buffer registry per UID for the given session and application. If none
1834 * is found, a new one is created, added to the global registry and
1835 * initialized. If regp is valid, it's set with the newly created object.
1837 * Return 0 on success or else a negative value.
1839 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
1840 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
1843 struct buffer_reg_uid
*reg_uid
;
1850 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
1853 * This is the create channel path meaning that if there is NO
1854 * registry available, we have to create one for this session.
1856 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
1857 LTTNG_DOMAIN_UST
, ®_uid
);
1861 buffer_reg_uid_add(reg_uid
);
1866 /* Initialize registry. */
1867 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
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
);
1876 /* Add node to teardown list of the session. */
1877 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
1879 DBG3("UST app buffer registry per UID created successfully");
1891 * Create a session on the tracer side for the given app.
1893 * On success, ua_sess_ptr is populated with the session pointer or else left
1894 * untouched. If the session was created, is_created is set to 1. On error,
1895 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1898 * Returns 0 on success or else a negative code which is either -ENOMEM or
1899 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1901 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1902 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1905 int ret
, created
= 0;
1906 struct ust_app_session
*ua_sess
;
1910 assert(ua_sess_ptr
);
1912 health_code_update();
1914 ua_sess
= lookup_session_by_app(usess
, app
);
1915 if (ua_sess
== NULL
) {
1916 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
1917 app
->pid
, usess
->id
);
1918 ua_sess
= alloc_ust_app_session(app
);
1919 if (ua_sess
== NULL
) {
1920 /* Only malloc can failed so something is really wrong */
1924 shadow_copy_session(ua_sess
, usess
, app
);
1928 switch (usess
->buffer_type
) {
1929 case LTTNG_BUFFER_PER_PID
:
1930 /* Init local registry. */
1931 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
1936 case LTTNG_BUFFER_PER_UID
:
1937 /* Look for a global registry. If none exists, create one. */
1938 ret
= setup_buffer_reg_uid(usess
, app
, NULL
);
1949 health_code_update();
1951 if (ua_sess
->handle
== -1) {
1952 ret
= ustctl_create_session(app
->sock
);
1954 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1955 ERR("Creating session for app pid %d with ret %d",
1958 DBG("UST app creating session failed. Application is dead");
1960 * This is normal behavior, an application can die during the
1961 * creation process. Don't report an error so the execution can
1962 * continue normally. This will get flagged ENOTCONN and the
1963 * caller will handle it.
1967 delete_ust_app_session(-1, ua_sess
, app
);
1968 if (ret
!= -ENOMEM
) {
1970 * Tracer is probably gone or got an internal error so let's
1971 * behave like it will soon unregister or not usable.
1978 ua_sess
->handle
= ret
;
1980 /* Add ust app session to app's HT */
1981 lttng_ht_node_init_u64(&ua_sess
->node
,
1982 ua_sess
->tracing_id
);
1983 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
1985 DBG2("UST app session created successfully with handle %d", ret
);
1988 *ua_sess_ptr
= ua_sess
;
1990 *is_created
= created
;
1993 /* Everything went well. */
1997 health_code_update();
2002 * Match function for a hash table lookup of ust_app_ctx.
2004 * It matches an ust app context based on the context type and, in the case
2005 * of perf counters, their name.
2007 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2009 struct ust_app_ctx
*ctx
;
2010 const struct lttng_ust_context
*key
;
2015 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2019 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2023 /* Check the name in the case of perf thread counters. */
2024 if (key
->ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
2025 if (strncmp(key
->u
.perf_counter
.name
,
2026 ctx
->ctx
.u
.perf_counter
.name
,
2027 sizeof(key
->u
.perf_counter
.name
))) {
2040 * Lookup for an ust app context from an lttng_ust_context.
2042 * Must be called while holding RCU read side lock.
2043 * Return an ust_app_ctx object or NULL on error.
2046 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2047 struct lttng_ust_context
*uctx
)
2049 struct lttng_ht_iter iter
;
2050 struct lttng_ht_node_ulong
*node
;
2051 struct ust_app_ctx
*app_ctx
= NULL
;
2056 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2057 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2058 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2059 node
= lttng_ht_iter_get_node_ulong(&iter
);
2064 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2071 * Create a context for the channel on the tracer.
2073 * Called with UST app session lock held and a RCU read side lock.
2076 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2077 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
2078 struct ust_app
*app
)
2081 struct ust_app_ctx
*ua_ctx
;
2083 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2085 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2091 ua_ctx
= alloc_ust_app_ctx(uctx
);
2092 if (ua_ctx
== NULL
) {
2098 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2099 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2100 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2102 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2112 * Enable on the tracer side a ust app event for the session and channel.
2114 * Called with UST app session lock held.
2117 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2118 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2122 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2127 ua_event
->enabled
= 1;
2134 * Disable on the tracer side a ust app event for the session and channel.
2136 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2137 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2141 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2146 ua_event
->enabled
= 0;
2153 * Lookup ust app channel for session and disable it on the tracer side.
2156 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2157 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2161 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2166 ua_chan
->enabled
= 0;
2173 * Lookup ust app channel for session and enable it on the tracer side. This
2174 * MUST be called with a RCU read side lock acquired.
2176 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2177 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2180 struct lttng_ht_iter iter
;
2181 struct lttng_ht_node_str
*ua_chan_node
;
2182 struct ust_app_channel
*ua_chan
;
2184 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2185 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2186 if (ua_chan_node
== NULL
) {
2187 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2188 uchan
->name
, ua_sess
->tracing_id
);
2192 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2194 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2204 * Ask the consumer to create a channel and get it if successful.
2206 * Return 0 on success or else a negative value.
2208 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2209 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2210 int bitness
, struct ust_registry_session
*registry
)
2213 unsigned int nb_fd
= 0;
2214 struct consumer_socket
*socket
;
2222 health_code_update();
2224 /* Get the right consumer socket for the application. */
2225 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2231 health_code_update();
2233 /* Need one fd for the channel. */
2234 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2236 ERR("Exhausted number of available FD upon create channel");
2241 * Ask consumer to create channel. The consumer will return the number of
2242 * stream we have to expect.
2244 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2251 * Compute the number of fd needed before receiving them. It must be 2 per
2252 * stream (2 being the default value here).
2254 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2256 /* Reserve the amount of file descriptor we need. */
2257 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2259 ERR("Exhausted number of available FD upon create channel");
2260 goto error_fd_get_stream
;
2263 health_code_update();
2266 * Now get the channel from the consumer. This call wil populate the stream
2267 * list of that channel and set the ust objects.
2269 if (usess
->consumer
->enabled
) {
2270 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2280 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2281 error_fd_get_stream
:
2283 * Initiate a destroy channel on the consumer since we had an error
2284 * handling it on our side. The return value is of no importance since we
2285 * already have a ret value set by the previous error that we need to
2288 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2290 lttng_fd_put(LTTNG_FD_APPS
, 1);
2292 health_code_update();
2298 * Duplicate the ust data object of the ust app stream and save it in the
2299 * buffer registry stream.
2301 * Return 0 on success or else a negative value.
2303 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2304 struct ust_app_stream
*stream
)
2311 /* Reserve the amount of file descriptor we need. */
2312 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2314 ERR("Exhausted number of available FD upon duplicate stream");
2318 /* Duplicate object for stream once the original is in the registry. */
2319 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2320 reg_stream
->obj
.ust
);
2322 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2323 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2324 lttng_fd_put(LTTNG_FD_APPS
, 2);
2327 stream
->handle
= stream
->obj
->handle
;
2334 * Duplicate the ust data object of the ust app. channel and save it in the
2335 * buffer registry channel.
2337 * Return 0 on success or else a negative value.
2339 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2340 struct ust_app_channel
*ua_chan
)
2347 /* Need two fds for the channel. */
2348 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2350 ERR("Exhausted number of available FD upon duplicate channel");
2354 /* Duplicate object for stream once the original is in the registry. */
2355 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2357 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2358 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2361 ua_chan
->handle
= ua_chan
->obj
->handle
;
2366 lttng_fd_put(LTTNG_FD_APPS
, 1);
2372 * For a given channel buffer registry, setup all streams of the given ust
2373 * application channel.
2375 * Return 0 on success or else a negative value.
2377 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2378 struct ust_app_channel
*ua_chan
)
2381 struct ust_app_stream
*stream
, *stmp
;
2386 DBG2("UST app setup buffer registry stream");
2388 /* Send all streams to application. */
2389 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2390 struct buffer_reg_stream
*reg_stream
;
2392 ret
= buffer_reg_stream_create(®_stream
);
2398 * Keep original pointer and nullify it in the stream so the delete
2399 * stream call does not release the object.
2401 reg_stream
->obj
.ust
= stream
->obj
;
2403 buffer_reg_stream_add(reg_stream
, reg_chan
);
2405 /* We don't need the streams anymore. */
2406 cds_list_del(&stream
->list
);
2407 delete_ust_app_stream(-1, stream
);
2415 * Create a buffer registry channel for the given session registry and
2416 * application channel object. If regp pointer is valid, it's set with the
2417 * created object. Important, the created object is NOT added to the session
2418 * registry hash table.
2420 * Return 0 on success else a negative value.
2422 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2423 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2426 struct buffer_reg_channel
*reg_chan
= NULL
;
2431 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2433 /* Create buffer registry channel. */
2434 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2439 reg_chan
->consumer_key
= ua_chan
->key
;
2440 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2441 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2443 /* Create and add a channel registry to session. */
2444 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2445 ua_chan
->tracing_channel_id
);
2449 buffer_reg_channel_add(reg_sess
, reg_chan
);
2458 /* Safe because the registry channel object was not added to any HT. */
2459 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2465 * Setup buffer registry channel for the given session registry and application
2466 * channel object. If regp pointer is valid, it's set with the created object.
2468 * Return 0 on success else a negative value.
2470 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2471 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
)
2478 assert(ua_chan
->obj
);
2480 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2482 /* Setup all streams for the registry. */
2483 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
);
2488 reg_chan
->obj
.ust
= ua_chan
->obj
;
2489 ua_chan
->obj
= NULL
;
2494 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2495 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2500 * Send buffer registry channel to the application.
2502 * Return 0 on success else a negative value.
2504 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2505 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2506 struct ust_app_channel
*ua_chan
)
2509 struct buffer_reg_stream
*reg_stream
;
2516 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2518 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2523 /* Send channel to the application. */
2524 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2525 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2526 ret
= -ENOTCONN
; /* Caused by app exiting. */
2528 } else if (ret
< 0) {
2532 health_code_update();
2534 /* Send all streams to application. */
2535 pthread_mutex_lock(®_chan
->stream_list_lock
);
2536 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2537 struct ust_app_stream stream
;
2539 ret
= duplicate_stream_object(reg_stream
, &stream
);
2541 goto error_stream_unlock
;
2544 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2546 (void) release_ust_app_stream(-1, &stream
);
2547 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2548 ret
= -ENOTCONN
; /* Caused by app exiting. */
2549 goto error_stream_unlock
;
2550 } else if (ret
< 0) {
2551 goto error_stream_unlock
;
2553 goto error_stream_unlock
;
2557 * The return value is not important here. This function will output an
2560 (void) release_ust_app_stream(-1, &stream
);
2562 ua_chan
->is_sent
= 1;
2564 error_stream_unlock
:
2565 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2571 * Create and send to the application the created buffers with per UID buffers.
2573 * Return 0 on success else a negative value.
2575 static int create_channel_per_uid(struct ust_app
*app
,
2576 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2577 struct ust_app_channel
*ua_chan
)
2580 struct buffer_reg_uid
*reg_uid
;
2581 struct buffer_reg_channel
*reg_chan
;
2588 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2590 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2592 * The session creation handles the creation of this global registry
2593 * object. If none can be find, there is a code flow problem or a
2598 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2601 /* Create the buffer registry channel object. */
2602 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2604 ERR("Error creating the UST channel \"%s\" registry instance",
2611 * Create the buffers on the consumer side. This call populates the
2612 * ust app channel object with all streams and data object.
2614 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2615 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2617 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2621 * Let's remove the previously created buffer registry channel so
2622 * it's not visible anymore in the session registry.
2624 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2625 ua_chan
->tracing_channel_id
);
2626 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2627 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2632 * Setup the streams and add it to the session registry.
2634 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, reg_chan
);
2636 ERR("Error setting up UST channel \"%s\"",
2643 /* Send buffers to the application. */
2644 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2646 if (ret
!= -ENOTCONN
) {
2647 ERR("Error sending channel to application");
2657 * Create and send to the application the created buffers with per PID buffers.
2659 * Return 0 on success else a negative value.
2661 static int create_channel_per_pid(struct ust_app
*app
,
2662 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2663 struct ust_app_channel
*ua_chan
)
2666 struct ust_registry_session
*registry
;
2673 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2677 registry
= get_session_registry(ua_sess
);
2680 /* Create and add a new channel registry to session. */
2681 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2683 ERR("Error creating the UST channel \"%s\" registry instance",
2688 /* Create and get channel on the consumer side. */
2689 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2690 app
->bits_per_long
, registry
);
2692 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2697 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2699 if (ret
!= -ENOTCONN
) {
2700 ERR("Error sending channel to application");
2711 * From an already allocated ust app channel, create the channel buffers if
2712 * need and send it to the application. This MUST be called with a RCU read
2713 * side lock acquired.
2715 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2716 * the application exited concurrently.
2718 static int do_create_channel(struct ust_app
*app
,
2719 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2720 struct ust_app_channel
*ua_chan
)
2729 /* Handle buffer type before sending the channel to the application. */
2730 switch (usess
->buffer_type
) {
2731 case LTTNG_BUFFER_PER_UID
:
2733 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
2739 case LTTNG_BUFFER_PER_PID
:
2741 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
2753 /* Initialize ust objd object using the received handle and add it. */
2754 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
2755 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
2757 /* If channel is not enabled, disable it on the tracer */
2758 if (!ua_chan
->enabled
) {
2759 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2770 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2771 * newly created channel if not NULL.
2773 * Called with UST app session lock and RCU read-side lock held.
2775 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2776 * the application exited concurrently.
2778 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
2779 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
2780 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
2781 struct ust_app_channel
**ua_chanp
)
2784 struct lttng_ht_iter iter
;
2785 struct lttng_ht_node_str
*ua_chan_node
;
2786 struct ust_app_channel
*ua_chan
;
2788 /* Lookup channel in the ust app session */
2789 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2790 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2791 if (ua_chan_node
!= NULL
) {
2792 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2796 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
2797 if (ua_chan
== NULL
) {
2798 /* Only malloc can fail here */
2802 shadow_copy_channel(ua_chan
, uchan
);
2804 /* Set channel type. */
2805 ua_chan
->attr
.type
= type
;
2807 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
2812 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
2815 /* Only add the channel if successful on the tracer side. */
2816 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
2820 *ua_chanp
= ua_chan
;
2823 /* Everything went well. */
2827 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
2833 * Create UST app event and create it on the tracer side.
2835 * Called with ust app session mutex held.
2838 int create_ust_app_event(struct ust_app_session
*ua_sess
,
2839 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
2840 struct ust_app
*app
)
2843 struct ust_app_event
*ua_event
;
2845 /* Get event node */
2846 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2847 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
2848 if (ua_event
!= NULL
) {
2853 /* Does not exist so create one */
2854 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
2855 if (ua_event
== NULL
) {
2856 /* Only malloc can failed so something is really wrong */
2860 shadow_copy_event(ua_event
, uevent
);
2862 /* Create it on the tracer side */
2863 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2865 /* Not found previously means that it does not exist on the tracer */
2866 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
2870 add_unique_ust_app_event(ua_chan
, ua_event
);
2872 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
2879 /* Valid. Calling here is already in a read side lock */
2880 delete_ust_app_event(-1, ua_event
);
2885 * Create UST metadata and open it on the tracer side.
2887 * Called with UST app session lock held and RCU read side lock.
2889 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
2890 struct ust_app
*app
, struct consumer_output
*consumer
)
2893 struct ust_app_channel
*metadata
;
2894 struct consumer_socket
*socket
;
2895 struct ust_registry_session
*registry
;
2901 registry
= get_session_registry(ua_sess
);
2904 pthread_mutex_lock(®istry
->lock
);
2906 /* Metadata already exists for this registry or it was closed previously */
2907 if (registry
->metadata_key
|| registry
->metadata_closed
) {
2912 /* Allocate UST metadata */
2913 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
2915 /* malloc() failed */
2920 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
2922 /* Need one fd for the channel. */
2923 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2925 ERR("Exhausted number of available FD upon create metadata");
2929 /* Get the right consumer socket for the application. */
2930 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
2933 goto error_consumer
;
2937 * Keep metadata key so we can identify it on the consumer side. Assign it
2938 * to the registry *before* we ask the consumer so we avoid the race of the
2939 * consumer requesting the metadata and the ask_channel call on our side
2940 * did not returned yet.
2942 registry
->metadata_key
= metadata
->key
;
2945 * Ask the metadata channel creation to the consumer. The metadata object
2946 * will be created by the consumer and kept their. However, the stream is
2947 * never added or monitored until we do a first push metadata to the
2950 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
2953 /* Nullify the metadata key so we don't try to close it later on. */
2954 registry
->metadata_key
= 0;
2955 goto error_consumer
;
2959 * The setup command will make the metadata stream be sent to the relayd,
2960 * if applicable, and the thread managing the metadatas. This is important
2961 * because after this point, if an error occurs, the only way the stream
2962 * can be deleted is to be monitored in the consumer.
2964 ret
= consumer_setup_metadata(socket
, metadata
->key
);
2966 /* Nullify the metadata key so we don't try to close it later on. */
2967 registry
->metadata_key
= 0;
2968 goto error_consumer
;
2971 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
2972 metadata
->key
, app
->pid
);
2975 lttng_fd_put(LTTNG_FD_APPS
, 1);
2976 delete_ust_app_channel(-1, metadata
, app
);
2978 pthread_mutex_unlock(®istry
->lock
);
2983 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
2984 * acquired before calling this function.
2986 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
2988 struct ust_app
*app
= NULL
;
2989 struct lttng_ht_node_ulong
*node
;
2990 struct lttng_ht_iter iter
;
2992 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
2993 node
= lttng_ht_iter_get_node_ulong(&iter
);
2995 DBG2("UST app no found with pid %d", pid
);
2999 DBG2("Found UST app by pid %d", pid
);
3001 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3008 * Allocate and init an UST app object using the registration information and
3009 * the command socket. This is called when the command socket connects to the
3012 * The object is returned on success or else NULL.
3014 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3016 struct ust_app
*lta
= NULL
;
3021 DBG3("UST app creating application for socket %d", sock
);
3023 if ((msg
->bits_per_long
== 64 &&
3024 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3025 || (msg
->bits_per_long
== 32 &&
3026 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3027 ERR("Registration failed: application \"%s\" (pid: %d) has "
3028 "%d-bit long, but no consumerd for this size is available.\n",
3029 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3033 lta
= zmalloc(sizeof(struct ust_app
));
3039 lta
->ppid
= msg
->ppid
;
3040 lta
->uid
= msg
->uid
;
3041 lta
->gid
= msg
->gid
;
3043 lta
->bits_per_long
= msg
->bits_per_long
;
3044 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3045 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3046 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3047 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3048 lta
->long_alignment
= msg
->long_alignment
;
3049 lta
->byte_order
= msg
->byte_order
;
3051 lta
->v_major
= msg
->major
;
3052 lta
->v_minor
= msg
->minor
;
3053 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3054 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3055 lta
->notify_sock
= -1;
3057 /* Copy name and make sure it's NULL terminated. */
3058 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3059 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3062 * Before this can be called, when receiving the registration information,
3063 * the application compatibility is checked. So, at this point, the
3064 * application can work with this session daemon.
3066 lta
->compatible
= 1;
3068 lta
->pid
= msg
->pid
;
3069 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3071 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3073 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3080 * For a given application object, add it to every hash table.
3082 void ust_app_add(struct ust_app
*app
)
3085 assert(app
->notify_sock
>= 0);
3090 * On a re-registration, we want to kick out the previous registration of
3093 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3096 * The socket _should_ be unique until _we_ call close. So, a add_unique
3097 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3098 * already in the table.
3100 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3102 /* Add application to the notify socket hash table. */
3103 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3104 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3106 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3107 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3108 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3115 * Set the application version into the object.
3117 * Return 0 on success else a negative value either an errno code or a
3118 * LTTng-UST error code.
3120 int ust_app_version(struct ust_app
*app
)
3126 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3128 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3129 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3131 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3139 * Unregister app by removing it from the global traceable app list and freeing
3142 * The socket is already closed at this point so no close to sock.
3144 void ust_app_unregister(int sock
)
3146 struct ust_app
*lta
;
3147 struct lttng_ht_node_ulong
*node
;
3148 struct lttng_ht_iter ust_app_sock_iter
;
3149 struct lttng_ht_iter iter
;
3150 struct ust_app_session
*ua_sess
;
3155 /* Get the node reference for a call_rcu */
3156 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3157 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3160 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3161 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3164 * For per-PID buffers, perform "push metadata" and flush all
3165 * application streams before removing app from hash tables,
3166 * ensuring proper behavior of data_pending check.
3167 * Remove sessions so they are not visible during deletion.
3169 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3171 struct ust_registry_session
*registry
;
3173 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3175 /* The session was already removed so scheduled for teardown. */
3179 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3180 (void) ust_app_flush_app_session(lta
, ua_sess
);
3184 * Add session to list for teardown. This is safe since at this point we
3185 * are the only one using this list.
3187 pthread_mutex_lock(&ua_sess
->lock
);
3189 if (ua_sess
->deleted
) {
3190 pthread_mutex_unlock(&ua_sess
->lock
);
3195 * Normally, this is done in the delete session process which is
3196 * executed in the call rcu below. However, upon registration we can't
3197 * afford to wait for the grace period before pushing data or else the
3198 * data pending feature can race between the unregistration and stop
3199 * command where the data pending command is sent *before* the grace
3202 * The close metadata below nullifies the metadata pointer in the
3203 * session so the delete session will NOT push/close a second time.
3205 registry
= get_session_registry(ua_sess
);
3207 /* Push metadata for application before freeing the application. */
3208 (void) push_metadata(registry
, ua_sess
->consumer
);
3211 * Don't ask to close metadata for global per UID buffers. Close
3212 * metadata only on destroy trace session in this case. Also, the
3213 * previous push metadata could have flag the metadata registry to
3214 * close so don't send a close command if closed.
3216 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3217 /* And ask to close it for this session registry. */
3218 (void) close_metadata(registry
, ua_sess
->consumer
);
3221 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3223 pthread_mutex_unlock(&ua_sess
->lock
);
3226 /* Remove application from PID hash table */
3227 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3231 * Remove application from notify hash table. The thread handling the
3232 * notify socket could have deleted the node so ignore on error because
3233 * either way it's valid. The close of that socket is handled by the other
3236 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3237 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3240 * Ignore return value since the node might have been removed before by an
3241 * add replace during app registration because the PID can be reassigned by
3244 iter
.iter
.node
= <a
->pid_n
.node
;
3245 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3247 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3252 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3259 * Fill events array with all events name of all registered apps.
3261 int ust_app_list_events(struct lttng_event
**events
)
3264 size_t nbmem
, count
= 0;
3265 struct lttng_ht_iter iter
;
3266 struct ust_app
*app
;
3267 struct lttng_event
*tmp_event
;
3269 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3270 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3271 if (tmp_event
== NULL
) {
3272 PERROR("zmalloc ust app events");
3279 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3280 struct lttng_ust_tracepoint_iter uiter
;
3282 health_code_update();
3284 if (!app
->compatible
) {
3286 * TODO: In time, we should notice the caller of this error by
3287 * telling him that this is a version error.
3291 handle
= ustctl_tracepoint_list(app
->sock
);
3293 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3294 ERR("UST app list events getting handle failed for app pid %d",
3300 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3301 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3302 /* Handle ustctl error. */
3304 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3305 ERR("UST app tp list get failed for app %d with ret %d",
3308 DBG3("UST app tp list get failed. Application is dead");
3310 * This is normal behavior, an application can die during the
3311 * creation process. Don't report an error so the execution can
3312 * continue normally. Continue normal execution.
3320 health_code_update();
3321 if (count
>= nbmem
) {
3322 /* In case the realloc fails, we free the memory */
3323 struct lttng_event
*new_tmp_event
;
3326 new_nbmem
= nbmem
<< 1;
3327 DBG2("Reallocating event list from %zu to %zu entries",
3329 new_tmp_event
= realloc(tmp_event
,
3330 new_nbmem
* sizeof(struct lttng_event
));
3331 if (new_tmp_event
== NULL
) {
3332 PERROR("realloc ust app events");
3337 /* Zero the new memory */
3338 memset(new_tmp_event
+ nbmem
, 0,
3339 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3341 tmp_event
= new_tmp_event
;
3343 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3344 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3345 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3346 tmp_event
[count
].pid
= app
->pid
;
3347 tmp_event
[count
].enabled
= -1;
3353 *events
= tmp_event
;
3355 DBG2("UST app list events done (%zu events)", count
);
3360 health_code_update();
3365 * Fill events array with all events name of all registered apps.
3367 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3370 size_t nbmem
, count
= 0;
3371 struct lttng_ht_iter iter
;
3372 struct ust_app
*app
;
3373 struct lttng_event_field
*tmp_event
;
3375 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3376 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3377 if (tmp_event
== NULL
) {
3378 PERROR("zmalloc ust app event fields");
3385 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3386 struct lttng_ust_field_iter uiter
;
3388 health_code_update();
3390 if (!app
->compatible
) {
3392 * TODO: In time, we should notice the caller of this error by
3393 * telling him that this is a version error.
3397 handle
= ustctl_tracepoint_field_list(app
->sock
);
3399 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3400 ERR("UST app list field getting handle failed for app pid %d",
3406 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3407 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3408 /* Handle ustctl error. */
3410 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3411 ERR("UST app tp list field failed for app %d with ret %d",
3414 DBG3("UST app tp list field failed. Application is dead");
3416 * This is normal behavior, an application can die during the
3417 * creation process. Don't report an error so the execution can
3418 * continue normally. Reset list and count for next app.
3426 health_code_update();
3427 if (count
>= nbmem
) {
3428 /* In case the realloc fails, we free the memory */
3429 struct lttng_event_field
*new_tmp_event
;
3432 new_nbmem
= nbmem
<< 1;
3433 DBG2("Reallocating event field list from %zu to %zu entries",
3435 new_tmp_event
= realloc(tmp_event
,
3436 new_nbmem
* sizeof(struct lttng_event_field
));
3437 if (new_tmp_event
== NULL
) {
3438 PERROR("realloc ust app event fields");
3443 /* Zero the new memory */
3444 memset(new_tmp_event
+ nbmem
, 0,
3445 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3447 tmp_event
= new_tmp_event
;
3450 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3451 /* Mapping between these enums matches 1 to 1. */
3452 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3453 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3455 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3456 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3457 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3458 tmp_event
[count
].event
.pid
= app
->pid
;
3459 tmp_event
[count
].event
.enabled
= -1;
3465 *fields
= tmp_event
;
3467 DBG2("UST app list event fields done (%zu events)", count
);
3472 health_code_update();
3477 * Free and clean all traceable apps of the global list.
3479 * Should _NOT_ be called with RCU read-side lock held.
3481 void ust_app_clean_list(void)
3484 struct ust_app
*app
;
3485 struct lttng_ht_iter iter
;
3487 DBG2("UST app cleaning registered apps hash table");
3491 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3492 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3494 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3497 /* Cleanup socket hash table */
3498 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3500 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3504 /* Cleanup notify socket hash table */
3505 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3506 notify_sock_n
.node
) {
3507 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3512 /* Destroy is done only when the ht is empty */
3513 ht_cleanup_push(ust_app_ht
);
3514 ht_cleanup_push(ust_app_ht_by_sock
);
3515 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3519 * Init UST app hash table.
3521 void ust_app_ht_alloc(void)
3523 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3524 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3525 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3529 * For a specific UST session, disable the channel for all registered apps.
3531 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3532 struct ltt_ust_channel
*uchan
)
3535 struct lttng_ht_iter iter
;
3536 struct lttng_ht_node_str
*ua_chan_node
;
3537 struct ust_app
*app
;
3538 struct ust_app_session
*ua_sess
;
3539 struct ust_app_channel
*ua_chan
;
3541 if (usess
== NULL
|| uchan
== NULL
) {
3542 ERR("Disabling UST global channel with NULL values");
3547 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3548 uchan
->name
, usess
->id
);
3552 /* For every registered applications */
3553 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3554 struct lttng_ht_iter uiter
;
3555 if (!app
->compatible
) {
3557 * TODO: In time, we should notice the caller of this error by
3558 * telling him that this is a version error.
3562 ua_sess
= lookup_session_by_app(usess
, app
);
3563 if (ua_sess
== NULL
) {
3568 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3569 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3570 /* If the session if found for the app, the channel must be there */
3571 assert(ua_chan_node
);
3573 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3574 /* The channel must not be already disabled */
3575 assert(ua_chan
->enabled
== 1);
3577 /* Disable channel onto application */
3578 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3580 /* XXX: We might want to report this error at some point... */
3592 * For a specific UST session, enable the channel for all registered apps.
3594 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3595 struct ltt_ust_channel
*uchan
)
3598 struct lttng_ht_iter iter
;
3599 struct ust_app
*app
;
3600 struct ust_app_session
*ua_sess
;
3602 if (usess
== NULL
|| uchan
== NULL
) {
3603 ERR("Adding UST global channel to NULL values");
3608 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3609 uchan
->name
, usess
->id
);
3613 /* For every registered applications */
3614 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3615 if (!app
->compatible
) {
3617 * TODO: In time, we should notice the caller of this error by
3618 * telling him that this is a version error.
3622 ua_sess
= lookup_session_by_app(usess
, app
);
3623 if (ua_sess
== NULL
) {
3627 /* Enable channel onto application */
3628 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3630 /* XXX: We might want to report this error at some point... */
3642 * Disable an event in a channel and for a specific session.
3644 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3645 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3648 struct lttng_ht_iter iter
, uiter
;
3649 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3650 struct ust_app
*app
;
3651 struct ust_app_session
*ua_sess
;
3652 struct ust_app_channel
*ua_chan
;
3653 struct ust_app_event
*ua_event
;
3655 DBG("UST app disabling event %s for all apps in channel "
3656 "%s for session id %" PRIu64
,
3657 uevent
->attr
.name
, uchan
->name
, usess
->id
);
3661 /* For all registered applications */
3662 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3663 if (!app
->compatible
) {
3665 * TODO: In time, we should notice the caller of this error by
3666 * telling him that this is a version error.
3670 ua_sess
= lookup_session_by_app(usess
, app
);
3671 if (ua_sess
== NULL
) {
3676 /* Lookup channel in the ust app session */
3677 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3678 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3679 if (ua_chan_node
== NULL
) {
3680 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
3681 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
3684 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3686 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
3687 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
3688 if (ua_event_node
== NULL
) {
3689 DBG2("Event %s not found in channel %s for app pid %d."
3690 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
3693 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3695 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3697 /* XXX: Report error someday... */
3708 * For a specific UST session, create the channel for all registered apps.
3710 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
3711 struct ltt_ust_channel
*uchan
)
3713 int ret
= 0, created
;
3714 struct lttng_ht_iter iter
;
3715 struct ust_app
*app
;
3716 struct ust_app_session
*ua_sess
= NULL
;
3718 /* Very wrong code flow */
3722 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
3723 uchan
->name
, usess
->id
);
3727 /* For every registered applications */
3728 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3729 if (!app
->compatible
) {
3731 * TODO: In time, we should notice the caller of this error by
3732 * telling him that this is a version error.
3737 * Create session on the tracer side and add it to app session HT. Note
3738 * that if session exist, it will simply return a pointer to the ust
3741 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
3746 * The application's socket is not valid. Either a bad socket
3747 * or a timeout on it. We can't inform the caller that for a
3748 * specific app, the session failed so lets continue here.
3750 ret
= 0; /* Not an error. */
3754 goto error_rcu_unlock
;
3759 pthread_mutex_lock(&ua_sess
->lock
);
3761 if (ua_sess
->deleted
) {
3762 pthread_mutex_unlock(&ua_sess
->lock
);
3766 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
3767 sizeof(uchan
->name
))) {
3768 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
3771 /* Create channel onto application. We don't need the chan ref. */
3772 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
3773 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
3775 pthread_mutex_unlock(&ua_sess
->lock
);
3777 /* Cleanup the created session if it's the case. */
3779 destroy_app_session(app
, ua_sess
);
3784 * The application's socket is not valid. Either a bad socket
3785 * or a timeout on it. We can't inform the caller that for a
3786 * specific app, the session failed so lets continue here.
3788 ret
= 0; /* Not an error. */
3792 goto error_rcu_unlock
;
3803 * Enable event for a specific session and channel on the tracer.
3805 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
3806 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3809 struct lttng_ht_iter iter
, uiter
;
3810 struct lttng_ht_node_str
*ua_chan_node
;
3811 struct ust_app
*app
;
3812 struct ust_app_session
*ua_sess
;
3813 struct ust_app_channel
*ua_chan
;
3814 struct ust_app_event
*ua_event
;
3816 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
3817 uevent
->attr
.name
, usess
->id
);
3820 * NOTE: At this point, this function is called only if the session and
3821 * channel passed are already created for all apps. and enabled on the
3827 /* For all registered applications */
3828 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3829 if (!app
->compatible
) {
3831 * TODO: In time, we should notice the caller of this error by
3832 * telling him that this is a version error.
3836 ua_sess
= lookup_session_by_app(usess
, app
);
3838 /* The application has problem or is probably dead. */
3842 pthread_mutex_lock(&ua_sess
->lock
);
3844 if (ua_sess
->deleted
) {
3845 pthread_mutex_unlock(&ua_sess
->lock
);
3849 /* Lookup channel in the ust app session */
3850 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3851 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3853 * It is possible that the channel cannot be found is
3854 * the channel/event creation occurs concurrently with
3855 * an application exit.
3857 if (!ua_chan_node
) {
3858 pthread_mutex_unlock(&ua_sess
->lock
);
3862 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3864 /* Get event node */
3865 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3866 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3867 if (ua_event
== NULL
) {
3868 DBG3("UST app enable event %s not found for app PID %d."
3869 "Skipping app", uevent
->attr
.name
, app
->pid
);
3873 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
3875 pthread_mutex_unlock(&ua_sess
->lock
);
3879 pthread_mutex_unlock(&ua_sess
->lock
);
3888 * For a specific existing UST session and UST channel, creates the event for
3889 * all registered apps.
3891 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
3892 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3895 struct lttng_ht_iter iter
, uiter
;
3896 struct lttng_ht_node_str
*ua_chan_node
;
3897 struct ust_app
*app
;
3898 struct ust_app_session
*ua_sess
;
3899 struct ust_app_channel
*ua_chan
;
3901 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
3902 uevent
->attr
.name
, usess
->id
);
3906 /* For all registered applications */
3907 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3908 if (!app
->compatible
) {
3910 * TODO: In time, we should notice the caller of this error by
3911 * telling him that this is a version error.
3915 ua_sess
= lookup_session_by_app(usess
, app
);
3917 /* The application has problem or is probably dead. */
3921 pthread_mutex_lock(&ua_sess
->lock
);
3923 if (ua_sess
->deleted
) {
3924 pthread_mutex_unlock(&ua_sess
->lock
);
3928 /* Lookup channel in the ust app session */
3929 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3930 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3931 /* If the channel is not found, there is a code flow error */
3932 assert(ua_chan_node
);
3934 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3936 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
3937 pthread_mutex_unlock(&ua_sess
->lock
);
3939 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
3940 /* Possible value at this point: -ENOMEM. If so, we stop! */
3943 DBG2("UST app event %s already exist on app PID %d",
3944 uevent
->attr
.name
, app
->pid
);
3955 * Start tracing for a specific UST session and app.
3958 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
3961 struct ust_app_session
*ua_sess
;
3963 DBG("Starting tracing for ust app pid %d", app
->pid
);
3967 if (!app
->compatible
) {
3971 ua_sess
= lookup_session_by_app(usess
, app
);
3972 if (ua_sess
== NULL
) {
3973 /* The session is in teardown process. Ignore and continue. */
3977 pthread_mutex_lock(&ua_sess
->lock
);
3979 if (ua_sess
->deleted
) {
3980 pthread_mutex_unlock(&ua_sess
->lock
);
3984 /* Upon restart, we skip the setup, already done */
3985 if (ua_sess
->started
) {
3989 /* Create directories if consumer is LOCAL and has a path defined. */
3990 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
3991 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
3992 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
3993 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
3995 if (errno
!= EEXIST
) {
3996 ERR("Trace directory creation error");
4003 * Create the metadata for the application. This returns gracefully if a
4004 * metadata was already set for the session.
4006 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4011 health_code_update();
4014 /* This start the UST tracing */
4015 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4017 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4018 ERR("Error starting tracing for app pid: %d (ret: %d)",
4021 DBG("UST app start session failed. Application is dead.");
4023 * This is normal behavior, an application can die during the
4024 * creation process. Don't report an error so the execution can
4025 * continue normally.
4027 pthread_mutex_unlock(&ua_sess
->lock
);
4033 /* Indicate that the session has been started once */
4034 ua_sess
->started
= 1;
4036 pthread_mutex_unlock(&ua_sess
->lock
);
4038 health_code_update();
4040 /* Quiescent wait after starting trace */
4041 ret
= ustctl_wait_quiescent(app
->sock
);
4042 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4043 ERR("UST app wait quiescent failed for app pid %d ret %d",
4049 health_code_update();
4053 pthread_mutex_unlock(&ua_sess
->lock
);
4055 health_code_update();
4060 * Stop tracing for a specific UST session and app.
4063 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4066 struct ust_app_session
*ua_sess
;
4067 struct ust_registry_session
*registry
;
4069 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4073 if (!app
->compatible
) {
4074 goto end_no_session
;
4077 ua_sess
= lookup_session_by_app(usess
, app
);
4078 if (ua_sess
== NULL
) {
4079 goto end_no_session
;
4082 pthread_mutex_lock(&ua_sess
->lock
);
4084 if (ua_sess
->deleted
) {
4085 pthread_mutex_unlock(&ua_sess
->lock
);
4086 goto end_no_session
;
4090 * If started = 0, it means that stop trace has been called for a session
4091 * that was never started. It's possible since we can have a fail start
4092 * from either the application manager thread or the command thread. Simply
4093 * indicate that this is a stop error.
4095 if (!ua_sess
->started
) {
4096 goto error_rcu_unlock
;
4099 health_code_update();
4101 /* This inhibits UST tracing */
4102 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4104 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4105 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4108 DBG("UST app stop session failed. Application is dead.");
4110 * This is normal behavior, an application can die during the
4111 * creation process. Don't report an error so the execution can
4112 * continue normally.
4116 goto error_rcu_unlock
;
4119 health_code_update();
4121 /* Quiescent wait after stopping trace */
4122 ret
= ustctl_wait_quiescent(app
->sock
);
4123 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4124 ERR("UST app wait quiescent failed for app pid %d ret %d",
4128 health_code_update();
4130 registry
= get_session_registry(ua_sess
);
4133 /* Push metadata for application before freeing the application. */
4134 (void) push_metadata(registry
, ua_sess
->consumer
);
4137 pthread_mutex_unlock(&ua_sess
->lock
);
4140 health_code_update();
4144 pthread_mutex_unlock(&ua_sess
->lock
);
4146 health_code_update();
4151 int ust_app_flush_app_session(struct ust_app
*app
,
4152 struct ust_app_session
*ua_sess
)
4154 int ret
, retval
= 0;
4155 struct lttng_ht_iter iter
;
4156 struct ust_app_channel
*ua_chan
;
4157 struct consumer_socket
*socket
;
4159 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4163 if (!app
->compatible
) {
4164 goto end_not_compatible
;
4167 pthread_mutex_lock(&ua_sess
->lock
);
4169 if (ua_sess
->deleted
) {
4173 health_code_update();
4175 /* Flushing buffers */
4176 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4179 /* Flush buffers and push metadata. */
4180 switch (ua_sess
->buffer_type
) {
4181 case LTTNG_BUFFER_PER_PID
:
4182 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4184 health_code_update();
4185 assert(ua_chan
->is_sent
);
4186 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4188 ERR("Error flushing consumer channel");
4194 case LTTNG_BUFFER_PER_UID
:
4200 health_code_update();
4203 pthread_mutex_unlock(&ua_sess
->lock
);
4207 health_code_update();
4212 * Flush buffers for all applications for a specific UST session.
4213 * Called with UST session lock held.
4216 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4221 DBG("Flushing session buffers for all ust apps");
4225 /* Flush buffers and push metadata. */
4226 switch (usess
->buffer_type
) {
4227 case LTTNG_BUFFER_PER_UID
:
4229 struct buffer_reg_uid
*reg
;
4230 struct lttng_ht_iter iter
;
4232 /* Flush all per UID buffers associated to that session. */
4233 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4234 struct ust_registry_session
*ust_session_reg
;
4235 struct buffer_reg_channel
*reg_chan
;
4236 struct consumer_socket
*socket
;
4238 /* Get consumer socket to use to push the metadata.*/
4239 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4242 /* Ignore request if no consumer is found for the session. */
4246 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4247 reg_chan
, node
.node
) {
4249 * The following call will print error values so the return
4250 * code is of little importance because whatever happens, we
4251 * have to try them all.
4253 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4256 ust_session_reg
= reg
->registry
->reg
.ust
;
4257 /* Push metadata. */
4258 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4262 case LTTNG_BUFFER_PER_PID
:
4264 struct ust_app_session
*ua_sess
;
4265 struct lttng_ht_iter iter
;
4266 struct ust_app
*app
;
4268 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4269 ua_sess
= lookup_session_by_app(usess
, app
);
4270 if (ua_sess
== NULL
) {
4273 (void) ust_app_flush_app_session(app
, ua_sess
);
4284 health_code_update();
4289 * Destroy a specific UST session in apps.
4291 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4294 struct ust_app_session
*ua_sess
;
4295 struct lttng_ht_iter iter
;
4296 struct lttng_ht_node_u64
*node
;
4298 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4302 if (!app
->compatible
) {
4306 __lookup_session_by_app(usess
, app
, &iter
);
4307 node
= lttng_ht_iter_get_node_u64(&iter
);
4309 /* Session is being or is deleted. */
4312 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4314 health_code_update();
4315 destroy_app_session(app
, ua_sess
);
4317 health_code_update();
4319 /* Quiescent wait after stopping trace */
4320 ret
= ustctl_wait_quiescent(app
->sock
);
4321 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4322 ERR("UST app wait quiescent failed for app pid %d ret %d",
4327 health_code_update();
4332 * Start tracing for the UST session.
4334 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4337 struct lttng_ht_iter iter
;
4338 struct ust_app
*app
;
4340 DBG("Starting all UST traces");
4344 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4345 ret
= ust_app_start_trace(usess
, app
);
4347 /* Continue to next apps even on error */
4358 * Start tracing for the UST session.
4359 * Called with UST session lock held.
4361 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4364 struct lttng_ht_iter iter
;
4365 struct ust_app
*app
;
4367 DBG("Stopping all UST traces");
4371 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4372 ret
= ust_app_stop_trace(usess
, app
);
4374 /* Continue to next apps even on error */
4379 (void) ust_app_flush_session(usess
);
4387 * Destroy app UST session.
4389 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4392 struct lttng_ht_iter iter
;
4393 struct ust_app
*app
;
4395 DBG("Destroy all UST traces");
4399 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4400 ret
= destroy_trace(usess
, app
);
4402 /* Continue to next apps even on error */
4413 * Add channels/events from UST global domain to registered apps at sock.
4415 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
4418 struct lttng_ht_iter iter
, uiter
;
4419 struct ust_app
*app
;
4420 struct ust_app_session
*ua_sess
= NULL
;
4421 struct ust_app_channel
*ua_chan
;
4422 struct ust_app_event
*ua_event
;
4423 struct ust_app_ctx
*ua_ctx
;
4428 DBG2("UST app global update for app sock %d for session id %" PRIu64
, sock
,
4433 app
= ust_app_find_by_sock(sock
);
4436 * Application can be unregistered before so this is possible hence
4437 * simply stopping the update.
4439 DBG3("UST app update failed to find app sock %d", sock
);
4443 if (!app
->compatible
) {
4447 ret
= create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
4449 /* Tracer is probably gone or ENOMEM. */
4454 pthread_mutex_lock(&ua_sess
->lock
);
4456 if (ua_sess
->deleted
) {
4457 pthread_mutex_unlock(&ua_sess
->lock
);
4462 * We can iterate safely here over all UST app session since the create ust
4463 * app session above made a shadow copy of the UST global domain from the
4466 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4468 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4469 if (ret
< 0 && ret
!= -ENOTCONN
) {
4471 * Stop everything. On error, the application
4472 * failed, no more file descriptor are available
4473 * or ENOMEM so stopping here is the only thing
4474 * we can do for now. The only exception is
4475 * -ENOTCONN, which indicates that the application
4482 * Add context using the list so they are enabled in the same order the
4485 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
4486 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
4493 /* For each events */
4494 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
4496 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
4503 pthread_mutex_unlock(&ua_sess
->lock
);
4505 if (usess
->active
) {
4506 ret
= ust_app_start_trace(usess
, app
);
4511 DBG2("UST trace started for app pid %d", app
->pid
);
4514 /* Everything went well at this point. */
4519 pthread_mutex_unlock(&ua_sess
->lock
);
4522 destroy_app_session(app
, ua_sess
);
4529 * Add context to a specific channel for global UST domain.
4531 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
4532 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
4535 struct lttng_ht_node_str
*ua_chan_node
;
4536 struct lttng_ht_iter iter
, uiter
;
4537 struct ust_app_channel
*ua_chan
= NULL
;
4538 struct ust_app_session
*ua_sess
;
4539 struct ust_app
*app
;
4543 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4544 if (!app
->compatible
) {
4546 * TODO: In time, we should notice the caller of this error by
4547 * telling him that this is a version error.
4551 ua_sess
= lookup_session_by_app(usess
, app
);
4552 if (ua_sess
== NULL
) {
4556 pthread_mutex_lock(&ua_sess
->lock
);
4558 if (ua_sess
->deleted
) {
4559 pthread_mutex_unlock(&ua_sess
->lock
);
4563 /* Lookup channel in the ust app session */
4564 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4565 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4566 if (ua_chan_node
== NULL
) {
4569 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4571 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4576 pthread_mutex_unlock(&ua_sess
->lock
);
4584 * Enable event for a channel from a UST session for a specific PID.
4586 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4587 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4590 struct lttng_ht_iter iter
;
4591 struct lttng_ht_node_str
*ua_chan_node
;
4592 struct ust_app
*app
;
4593 struct ust_app_session
*ua_sess
;
4594 struct ust_app_channel
*ua_chan
;
4595 struct ust_app_event
*ua_event
;
4597 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4601 app
= ust_app_find_by_pid(pid
);
4603 ERR("UST app enable event per PID %d not found", pid
);
4608 if (!app
->compatible
) {
4613 ua_sess
= lookup_session_by_app(usess
, app
);
4615 /* The application has problem or is probably dead. */
4620 pthread_mutex_lock(&ua_sess
->lock
);
4622 if (ua_sess
->deleted
) {
4627 /* Lookup channel in the ust app session */
4628 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4629 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4630 /* If the channel is not found, there is a code flow error */
4631 assert(ua_chan_node
);
4633 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4635 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4636 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4637 if (ua_event
== NULL
) {
4638 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4643 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4650 pthread_mutex_unlock(&ua_sess
->lock
);
4657 * Calibrate registered applications.
4659 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
4662 struct lttng_ht_iter iter
;
4663 struct ust_app
*app
;
4667 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4668 if (!app
->compatible
) {
4670 * TODO: In time, we should notice the caller of this error by
4671 * telling him that this is a version error.
4676 health_code_update();
4678 ret
= ustctl_calibrate(app
->sock
, calibrate
);
4682 /* Means that it's not implemented on the tracer side. */
4686 DBG2("Calibrate app PID %d returned with error %d",
4693 DBG("UST app global domain calibration finished");
4697 health_code_update();
4703 * Receive registration and populate the given msg structure.
4705 * On success return 0 else a negative value returned by the ustctl call.
4707 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
4710 uint32_t pid
, ppid
, uid
, gid
;
4714 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
4715 &pid
, &ppid
, &uid
, &gid
,
4716 &msg
->bits_per_long
,
4717 &msg
->uint8_t_alignment
,
4718 &msg
->uint16_t_alignment
,
4719 &msg
->uint32_t_alignment
,
4720 &msg
->uint64_t_alignment
,
4721 &msg
->long_alignment
,
4728 case LTTNG_UST_ERR_EXITING
:
4729 DBG3("UST app recv reg message failed. Application died");
4731 case LTTNG_UST_ERR_UNSUP_MAJOR
:
4732 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
4733 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
4734 LTTNG_UST_ABI_MINOR_VERSION
);
4737 ERR("UST app recv reg message failed with ret %d", ret
);
4742 msg
->pid
= (pid_t
) pid
;
4743 msg
->ppid
= (pid_t
) ppid
;
4744 msg
->uid
= (uid_t
) uid
;
4745 msg
->gid
= (gid_t
) gid
;
4752 * Return a ust app channel object using the application object and the channel
4753 * object descriptor has a key. If not found, NULL is returned. A RCU read side
4754 * lock MUST be acquired before calling this function.
4756 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
4759 struct lttng_ht_node_ulong
*node
;
4760 struct lttng_ht_iter iter
;
4761 struct ust_app_channel
*ua_chan
= NULL
;
4765 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
4766 node
= lttng_ht_iter_get_node_ulong(&iter
);
4768 DBG2("UST app channel find by objd %d not found", objd
);
4772 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
4779 * Reply to a register channel notification from an application on the notify
4780 * socket. The channel metadata is also created.
4782 * The session UST registry lock is acquired in this function.
4784 * On success 0 is returned else a negative value.
4786 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
4787 size_t nr_fields
, struct ustctl_field
*fields
)
4789 int ret
, ret_code
= 0;
4790 uint32_t chan_id
, reg_count
;
4791 uint64_t chan_reg_key
;
4792 enum ustctl_channel_header type
;
4793 struct ust_app
*app
;
4794 struct ust_app_channel
*ua_chan
;
4795 struct ust_app_session
*ua_sess
;
4796 struct ust_registry_session
*registry
;
4797 struct ust_registry_channel
*chan_reg
;
4801 /* Lookup application. If not found, there is a code flow error. */
4802 app
= find_app_by_notify_sock(sock
);
4804 DBG("Application socket %d is being teardown. Abort event notify",
4808 goto error_rcu_unlock
;
4811 /* Lookup channel by UST object descriptor. */
4812 ua_chan
= find_channel_by_objd(app
, cobjd
);
4814 DBG("Application channel is being teardown. Abort event notify");
4817 goto error_rcu_unlock
;
4820 assert(ua_chan
->session
);
4821 ua_sess
= ua_chan
->session
;
4823 /* Get right session registry depending on the session buffer type. */
4824 registry
= get_session_registry(ua_sess
);
4827 /* Depending on the buffer type, a different channel key is used. */
4828 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4829 chan_reg_key
= ua_chan
->tracing_channel_id
;
4831 chan_reg_key
= ua_chan
->key
;
4834 pthread_mutex_lock(®istry
->lock
);
4836 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
4839 if (!chan_reg
->register_done
) {
4840 reg_count
= ust_registry_get_event_count(chan_reg
);
4841 if (reg_count
< 31) {
4842 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
4844 type
= USTCTL_CHANNEL_HEADER_LARGE
;
4847 chan_reg
->nr_ctx_fields
= nr_fields
;
4848 chan_reg
->ctx_fields
= fields
;
4849 chan_reg
->header_type
= type
;
4851 /* Get current already assigned values. */
4852 type
= chan_reg
->header_type
;
4854 /* Set to NULL so the error path does not do a double free. */
4857 /* Channel id is set during the object creation. */
4858 chan_id
= chan_reg
->chan_id
;
4860 /* Append to metadata */
4861 if (!chan_reg
->metadata_dumped
) {
4862 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
4864 ERR("Error appending channel metadata (errno = %d)", ret_code
);
4870 DBG3("UST app replying to register channel key %" PRIu64
4871 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
4874 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
4876 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4877 ERR("UST app reply channel failed with ret %d", ret
);
4879 DBG3("UST app reply channel failed. Application died");
4884 /* This channel registry registration is completed. */
4885 chan_reg
->register_done
= 1;
4888 pthread_mutex_unlock(®istry
->lock
);
4898 * Add event to the UST channel registry. When the event is added to the
4899 * registry, the metadata is also created. Once done, this replies to the
4900 * application with the appropriate error code.
4902 * The session UST registry lock is acquired in the function.
4904 * On success 0 is returned else a negative value.
4906 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
4907 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
4908 char *model_emf_uri
)
4911 uint32_t event_id
= 0;
4912 uint64_t chan_reg_key
;
4913 struct ust_app
*app
;
4914 struct ust_app_channel
*ua_chan
;
4915 struct ust_app_session
*ua_sess
;
4916 struct ust_registry_session
*registry
;
4920 /* Lookup application. If not found, there is a code flow error. */
4921 app
= find_app_by_notify_sock(sock
);
4923 DBG("Application socket %d is being teardown. Abort event notify",
4928 free(model_emf_uri
);
4929 goto error_rcu_unlock
;
4932 /* Lookup channel by UST object descriptor. */
4933 ua_chan
= find_channel_by_objd(app
, cobjd
);
4935 DBG("Application channel is being teardown. Abort event notify");
4939 free(model_emf_uri
);
4940 goto error_rcu_unlock
;
4943 assert(ua_chan
->session
);
4944 ua_sess
= ua_chan
->session
;
4946 registry
= get_session_registry(ua_sess
);
4949 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4950 chan_reg_key
= ua_chan
->tracing_channel_id
;
4952 chan_reg_key
= ua_chan
->key
;
4955 pthread_mutex_lock(®istry
->lock
);
4958 * From this point on, this call acquires the ownership of the sig, fields
4959 * and model_emf_uri meaning any free are done inside it if needed. These
4960 * three variables MUST NOT be read/write after this.
4962 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
4963 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
, loglevel
,
4964 model_emf_uri
, ua_sess
->buffer_type
, &event_id
,
4968 * The return value is returned to ustctl so in case of an error, the
4969 * application can be notified. In case of an error, it's important not to
4970 * return a negative error or else the application will get closed.
4972 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
4974 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4975 ERR("UST app reply event failed with ret %d", ret
);
4977 DBG3("UST app reply event failed. Application died");
4980 * No need to wipe the create event since the application socket will
4981 * get close on error hence cleaning up everything by itself.
4986 DBG3("UST registry event %s with id %" PRId32
" added successfully",
4990 pthread_mutex_unlock(®istry
->lock
);
4997 * Handle application notification through the given notify socket.
4999 * Return 0 on success or else a negative value.
5001 int ust_app_recv_notify(int sock
)
5004 enum ustctl_notify_cmd cmd
;
5006 DBG3("UST app receiving notify from sock %d", sock
);
5008 ret
= ustctl_recv_notify(sock
, &cmd
);
5010 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5011 ERR("UST app recv notify failed with ret %d", ret
);
5013 DBG3("UST app recv notify failed. Application died");
5019 case USTCTL_NOTIFY_CMD_EVENT
:
5021 int sobjd
, cobjd
, loglevel
;
5022 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5024 struct ustctl_field
*fields
;
5026 DBG2("UST app ustctl register event received");
5028 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
5029 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
5031 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5032 ERR("UST app recv event failed with ret %d", ret
);
5034 DBG3("UST app recv event failed. Application died");
5040 * Add event to the UST registry coming from the notify socket. This
5041 * call will free if needed the sig, fields and model_emf_uri. This
5042 * code path loses the ownsership of these variables and transfer them
5043 * to the this function.
5045 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5046 fields
, loglevel
, model_emf_uri
);
5053 case USTCTL_NOTIFY_CMD_CHANNEL
:
5057 struct ustctl_field
*fields
;
5059 DBG2("UST app ustctl register channel received");
5061 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5064 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5065 ERR("UST app recv channel failed with ret %d", ret
);
5067 DBG3("UST app recv channel failed. Application died");
5073 * The fields ownership are transfered to this function call meaning
5074 * that if needed it will be freed. After this, it's invalid to access
5075 * fields or clean it up.
5077 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
5086 /* Should NEVER happen. */
5095 * Once the notify socket hangs up, this is called. First, it tries to find the
5096 * corresponding application. On failure, the call_rcu to close the socket is
5097 * executed. If an application is found, it tries to delete it from the notify
5098 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5100 * Note that an object needs to be allocated here so on ENOMEM failure, the
5101 * call RCU is not done but the rest of the cleanup is.
5103 void ust_app_notify_sock_unregister(int sock
)
5106 struct lttng_ht_iter iter
;
5107 struct ust_app
*app
;
5108 struct ust_app_notify_sock_obj
*obj
;
5114 obj
= zmalloc(sizeof(*obj
));
5117 * An ENOMEM is kind of uncool. If this strikes we continue the
5118 * procedure but the call_rcu will not be called. In this case, we
5119 * accept the fd leak rather than possibly creating an unsynchronized
5120 * state between threads.
5122 * TODO: The notify object should be created once the notify socket is
5123 * registered and stored independantely from the ust app object. The
5124 * tricky part is to synchronize the teardown of the application and
5125 * this notify object. Let's keep that in mind so we can avoid this
5126 * kind of shenanigans with ENOMEM in the teardown path.
5133 DBG("UST app notify socket unregister %d", sock
);
5136 * Lookup application by notify socket. If this fails, this means that the
5137 * hash table delete has already been done by the application
5138 * unregistration process so we can safely close the notify socket in a
5141 app
= find_app_by_notify_sock(sock
);
5146 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5149 * Whatever happens here either we fail or succeed, in both cases we have
5150 * to close the socket after a grace period to continue to the call RCU
5151 * here. If the deletion is successful, the application is not visible
5152 * anymore by other threads and is it fails it means that it was already
5153 * deleted from the hash table so either way we just have to close the
5156 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5162 * Close socket after a grace period to avoid for the socket to be reused
5163 * before the application object is freed creating potential race between
5164 * threads trying to add unique in the global hash table.
5167 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5172 * Destroy a ust app data structure and free its memory.
5174 void ust_app_destroy(struct ust_app
*app
)
5180 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5184 * Take a snapshot for a given UST session. The snapshot is sent to the given
5187 * Return 0 on success or else a negative value.
5189 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5190 struct snapshot_output
*output
, int wait
,
5191 uint64_t nb_packets_per_stream
)
5194 unsigned int snapshot_done
= 0;
5195 struct lttng_ht_iter iter
;
5196 struct ust_app
*app
;
5197 char pathname
[PATH_MAX
];
5204 switch (usess
->buffer_type
) {
5205 case LTTNG_BUFFER_PER_UID
:
5207 struct buffer_reg_uid
*reg
;
5209 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5210 struct buffer_reg_channel
*reg_chan
;
5211 struct consumer_socket
*socket
;
5213 /* Get consumer socket to use to push the metadata.*/
5214 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5221 memset(pathname
, 0, sizeof(pathname
));
5222 ret
= snprintf(pathname
, sizeof(pathname
),
5223 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5224 reg
->uid
, reg
->bits_per_long
);
5226 PERROR("snprintf snapshot path");
5230 /* Add the UST default trace dir to path. */
5231 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5232 reg_chan
, node
.node
) {
5233 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5234 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5235 nb_packets_per_stream
);
5240 ret
= consumer_snapshot_channel(socket
,
5241 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5242 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
5250 case LTTNG_BUFFER_PER_PID
:
5252 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5253 struct consumer_socket
*socket
;
5254 struct lttng_ht_iter chan_iter
;
5255 struct ust_app_channel
*ua_chan
;
5256 struct ust_app_session
*ua_sess
;
5257 struct ust_registry_session
*registry
;
5259 ua_sess
= lookup_session_by_app(usess
, app
);
5261 /* Session not associated with this app. */
5265 /* Get the right consumer socket for the application. */
5266 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5273 /* Add the UST default trace dir to path. */
5274 memset(pathname
, 0, sizeof(pathname
));
5275 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5278 PERROR("snprintf snapshot path");
5282 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5283 ua_chan
, node
.node
) {
5284 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
5285 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
5286 nb_packets_per_stream
);
5292 registry
= get_session_registry(ua_sess
);
5294 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
5295 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
5308 if (!snapshot_done
) {
5310 * If no snapshot was made and we are not in the error path, this means
5311 * that there are no buffers thus no (prior) application to snapshot
5312 * data from so we have simply NO data.
5323 * Return the size taken by one more packet per stream.
5325 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
5326 uint64_t cur_nr_packets
)
5328 uint64_t tot_size
= 0;
5329 struct ust_app
*app
;
5330 struct lttng_ht_iter iter
;
5334 switch (usess
->buffer_type
) {
5335 case LTTNG_BUFFER_PER_UID
:
5337 struct buffer_reg_uid
*reg
;
5339 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5340 struct buffer_reg_channel
*reg_chan
;
5343 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5344 reg_chan
, node
.node
) {
5345 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
5347 * Don't take channel into account if we
5348 * already grab all its packets.
5352 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
5358 case LTTNG_BUFFER_PER_PID
:
5361 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5362 struct ust_app_channel
*ua_chan
;
5363 struct ust_app_session
*ua_sess
;
5364 struct lttng_ht_iter chan_iter
;
5366 ua_sess
= lookup_session_by_app(usess
, app
);
5368 /* Session not associated with this app. */
5372 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5373 ua_chan
, node
.node
) {
5374 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
5376 * Don't take channel into account if we
5377 * already grab all its packets.
5381 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;