2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
29 #include <urcu/compiler.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"
40 #include "lttng-ust-ctl.h"
41 #include "lttng-ust-error.h"
44 #include "lttng-sessiond.h"
45 #include "notification-thread-commands.h"
48 struct lttng_ht
*ust_app_ht
;
49 struct lttng_ht
*ust_app_ht_by_sock
;
50 struct lttng_ht
*ust_app_ht_by_notify_sock
;
53 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
55 /* Next available channel key. Access under next_channel_key_lock. */
56 static uint64_t _next_channel_key
;
57 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
59 /* Next available session ID. Access under next_session_id_lock. */
60 static uint64_t _next_session_id
;
61 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
64 * Return the incremented value of next_channel_key.
66 static uint64_t get_next_channel_key(void)
70 pthread_mutex_lock(&next_channel_key_lock
);
71 ret
= ++_next_channel_key
;
72 pthread_mutex_unlock(&next_channel_key_lock
);
77 * Return the atomically incremented value of next_session_id.
79 static uint64_t get_next_session_id(void)
83 pthread_mutex_lock(&next_session_id_lock
);
84 ret
= ++_next_session_id
;
85 pthread_mutex_unlock(&next_session_id_lock
);
89 static void copy_channel_attr_to_ustctl(
90 struct ustctl_consumer_channel_attr
*attr
,
91 struct lttng_ust_channel_attr
*uattr
)
93 /* Copy event attributes since the layout is different. */
94 attr
->subbuf_size
= uattr
->subbuf_size
;
95 attr
->num_subbuf
= uattr
->num_subbuf
;
96 attr
->overwrite
= uattr
->overwrite
;
97 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
98 attr
->read_timer_interval
= uattr
->read_timer_interval
;
99 attr
->output
= uattr
->output
;
100 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
104 * Match function for the hash table lookup.
106 * It matches an ust app event based on three attributes which are the event
107 * name, the filter bytecode and the loglevel.
109 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
111 struct ust_app_event
*event
;
112 const struct ust_app_ht_key
*key
;
113 int ev_loglevel_value
;
118 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
120 ev_loglevel_value
= event
->attr
.loglevel
;
122 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
125 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
129 /* Event loglevel. */
130 if (ev_loglevel_value
!= key
->loglevel_type
) {
131 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
132 && key
->loglevel_type
== 0 &&
133 ev_loglevel_value
== -1) {
135 * Match is accepted. This is because on event creation, the
136 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
137 * -1 are accepted for this loglevel type since 0 is the one set by
138 * the API when receiving an enable event.
145 /* One of the filters is NULL, fail. */
146 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
150 if (key
->filter
&& event
->filter
) {
151 /* Both filters exists, check length followed by the bytecode. */
152 if (event
->filter
->len
!= key
->filter
->len
||
153 memcmp(event
->filter
->data
, key
->filter
->data
,
154 event
->filter
->len
) != 0) {
159 /* One of the exclusions is NULL, fail. */
160 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
164 if (key
->exclusion
&& event
->exclusion
) {
165 /* Both exclusions exists, check count followed by the names. */
166 if (event
->exclusion
->count
!= key
->exclusion
->count
||
167 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
168 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
182 * Unique add of an ust app event in the given ht. This uses the custom
183 * ht_match_ust_app_event match function and the event name as hash.
185 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
186 struct ust_app_event
*event
)
188 struct cds_lfht_node
*node_ptr
;
189 struct ust_app_ht_key key
;
193 assert(ua_chan
->events
);
196 ht
= ua_chan
->events
;
197 key
.name
= event
->attr
.name
;
198 key
.filter
= event
->filter
;
199 key
.loglevel_type
= event
->attr
.loglevel
;
200 key
.exclusion
= event
->exclusion
;
202 node_ptr
= cds_lfht_add_unique(ht
->ht
,
203 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
204 ht_match_ust_app_event
, &key
, &event
->node
.node
);
205 assert(node_ptr
== &event
->node
.node
);
209 * Close the notify socket from the given RCU head object. This MUST be called
210 * through a call_rcu().
212 static void close_notify_sock_rcu(struct rcu_head
*head
)
215 struct ust_app_notify_sock_obj
*obj
=
216 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
218 /* Must have a valid fd here. */
219 assert(obj
->fd
>= 0);
221 ret
= close(obj
->fd
);
223 ERR("close notify sock %d RCU", obj
->fd
);
225 lttng_fd_put(LTTNG_FD_APPS
, 1);
231 * Return the session registry according to the buffer type of the given
234 * A registry per UID object MUST exists before calling this function or else
235 * it assert() if not found. RCU read side lock must be acquired.
237 static struct ust_registry_session
*get_session_registry(
238 struct ust_app_session
*ua_sess
)
240 struct ust_registry_session
*registry
= NULL
;
244 switch (ua_sess
->buffer_type
) {
245 case LTTNG_BUFFER_PER_PID
:
247 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
251 registry
= reg_pid
->registry
->reg
.ust
;
254 case LTTNG_BUFFER_PER_UID
:
256 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
257 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
258 ua_sess
->real_credentials
.uid
);
262 registry
= reg_uid
->registry
->reg
.ust
;
274 * Delete ust context safely. RCU read lock must be held before calling
278 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
286 pthread_mutex_lock(&app
->sock_lock
);
287 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
288 pthread_mutex_unlock(&app
->sock_lock
);
289 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
290 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
291 sock
, ua_ctx
->obj
->handle
, ret
);
299 * Delete ust app event safely. RCU read lock must be held before calling
303 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
310 free(ua_event
->filter
);
311 if (ua_event
->exclusion
!= NULL
)
312 free(ua_event
->exclusion
);
313 if (ua_event
->obj
!= NULL
) {
314 pthread_mutex_lock(&app
->sock_lock
);
315 ret
= ustctl_release_object(sock
, ua_event
->obj
);
316 pthread_mutex_unlock(&app
->sock_lock
);
317 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
318 ERR("UST app sock %d release event obj failed with ret %d",
327 * Release ust data object of the given stream.
329 * Return 0 on success or else a negative value.
331 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
339 pthread_mutex_lock(&app
->sock_lock
);
340 ret
= ustctl_release_object(sock
, stream
->obj
);
341 pthread_mutex_unlock(&app
->sock_lock
);
342 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
343 ERR("UST app sock %d release stream obj failed with ret %d",
346 lttng_fd_put(LTTNG_FD_APPS
, 2);
354 * Delete ust app stream safely. RCU read lock must be held before calling
358 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
363 (void) release_ust_app_stream(sock
, stream
, app
);
368 * We need to execute ht_destroy outside of RCU read-side critical
369 * section and outside of call_rcu thread, so we postpone its execution
370 * using ht_cleanup_push. It is simpler than to change the semantic of
371 * the many callers of delete_ust_app_session().
374 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
376 struct ust_app_channel
*ua_chan
=
377 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
379 ht_cleanup_push(ua_chan
->ctx
);
380 ht_cleanup_push(ua_chan
->events
);
385 * Extract the lost packet or discarded events counter when the channel is
386 * being deleted and store the value in the parent channel so we can
387 * access it from lttng list and at stop/destroy.
389 * The session list lock must be held by the caller.
392 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
394 uint64_t discarded
= 0, lost
= 0;
395 struct ltt_session
*session
;
396 struct ltt_ust_channel
*uchan
;
398 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
403 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
404 if (!session
|| !session
->ust_session
) {
406 * Not finding the session is not an error because there are
407 * multiple ways the channels can be torn down.
409 * 1) The session daemon can initiate the destruction of the
410 * ust app session after receiving a destroy command or
411 * during its shutdown/teardown.
412 * 2) The application, since we are in per-pid tracing, is
413 * unregistering and tearing down its ust app session.
415 * Both paths are protected by the session list lock which
416 * ensures that the accounting of lost packets and discarded
417 * events is done exactly once. The session is then unpublished
418 * from the session list, resulting in this condition.
423 if (ua_chan
->attr
.overwrite
) {
424 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
425 ua_chan
->key
, session
->ust_session
->consumer
,
428 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
429 ua_chan
->key
, session
->ust_session
->consumer
,
432 uchan
= trace_ust_find_channel_by_name(
433 session
->ust_session
->domain_global
.channels
,
436 ERR("Missing UST channel to store discarded counters");
440 uchan
->per_pid_closed_app_discarded
+= discarded
;
441 uchan
->per_pid_closed_app_lost
+= lost
;
446 session_put(session
);
451 * Delete ust app channel safely. RCU read lock must be held before calling
454 * The session list lock must be held by the caller.
457 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
461 struct lttng_ht_iter iter
;
462 struct ust_app_event
*ua_event
;
463 struct ust_app_ctx
*ua_ctx
;
464 struct ust_app_stream
*stream
, *stmp
;
465 struct ust_registry_session
*registry
;
469 DBG3("UST app deleting channel %s", ua_chan
->name
);
472 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
473 cds_list_del(&stream
->list
);
474 delete_ust_app_stream(sock
, stream
, app
);
478 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
479 cds_list_del(&ua_ctx
->list
);
480 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
482 delete_ust_app_ctx(sock
, ua_ctx
, app
);
486 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
488 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
490 delete_ust_app_event(sock
, ua_event
, app
);
493 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
494 /* Wipe and free registry from session registry. */
495 registry
= get_session_registry(ua_chan
->session
);
497 ust_registry_channel_del_free(registry
, ua_chan
->key
,
501 * A negative socket can be used by the caller when
502 * cleaning-up a ua_chan in an error path. Skip the
503 * accounting in this case.
506 save_per_pid_lost_discarded_counters(ua_chan
);
510 if (ua_chan
->obj
!= NULL
) {
511 /* Remove channel from application UST object descriptor. */
512 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
513 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
515 pthread_mutex_lock(&app
->sock_lock
);
516 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
517 pthread_mutex_unlock(&app
->sock_lock
);
518 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
519 ERR("UST app sock %d release channel obj failed with ret %d",
522 lttng_fd_put(LTTNG_FD_APPS
, 1);
525 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
528 int ust_app_register_done(struct ust_app
*app
)
532 pthread_mutex_lock(&app
->sock_lock
);
533 ret
= ustctl_register_done(app
->sock
);
534 pthread_mutex_unlock(&app
->sock_lock
);
538 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
543 pthread_mutex_lock(&app
->sock_lock
);
548 ret
= ustctl_release_object(sock
, data
);
550 pthread_mutex_unlock(&app
->sock_lock
);
556 * Push metadata to consumer socket.
558 * RCU read-side lock must be held to guarantee existance of socket.
559 * Must be called with the ust app session lock held.
560 * Must be called with the registry lock held.
562 * On success, return the len of metadata pushed or else a negative value.
563 * Returning a -EPIPE return value means we could not send the metadata,
564 * but it can be caused by recoverable errors (e.g. the application has
565 * terminated concurrently).
567 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
568 struct consumer_socket
*socket
, int send_zero_data
)
571 char *metadata_str
= NULL
;
572 size_t len
, offset
, new_metadata_len_sent
;
574 uint64_t metadata_key
, metadata_version
;
579 metadata_key
= registry
->metadata_key
;
582 * Means that no metadata was assigned to the session. This can
583 * happens if no start has been done previously.
589 offset
= registry
->metadata_len_sent
;
590 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
591 new_metadata_len_sent
= registry
->metadata_len
;
592 metadata_version
= registry
->metadata_version
;
594 DBG3("No metadata to push for metadata key %" PRIu64
,
595 registry
->metadata_key
);
597 if (send_zero_data
) {
598 DBG("No metadata to push");
604 /* Allocate only what we have to send. */
605 metadata_str
= zmalloc(len
);
607 PERROR("zmalloc ust app metadata string");
611 /* Copy what we haven't sent out. */
612 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
615 pthread_mutex_unlock(®istry
->lock
);
617 * We need to unlock the registry while we push metadata to
618 * break a circular dependency between the consumerd metadata
619 * lock and the sessiond registry lock. Indeed, pushing metadata
620 * to the consumerd awaits that it gets pushed all the way to
621 * relayd, but doing so requires grabbing the metadata lock. If
622 * a concurrent metadata request is being performed by
623 * consumerd, this can try to grab the registry lock on the
624 * sessiond while holding the metadata lock on the consumer
625 * daemon. Those push and pull schemes are performed on two
626 * different bidirectionnal communication sockets.
628 ret
= consumer_push_metadata(socket
, metadata_key
,
629 metadata_str
, len
, offset
, metadata_version
);
630 pthread_mutex_lock(®istry
->lock
);
633 * There is an acceptable race here between the registry
634 * metadata key assignment and the creation on the
635 * consumer. The session daemon can concurrently push
636 * metadata for this registry while being created on the
637 * consumer since the metadata key of the registry is
638 * assigned *before* it is setup to avoid the consumer
639 * to ask for metadata that could possibly be not found
640 * in the session daemon.
642 * The metadata will get pushed either by the session
643 * being stopped or the consumer requesting metadata if
644 * that race is triggered.
646 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
649 ERR("Error pushing metadata to consumer");
655 * Metadata may have been concurrently pushed, since
656 * we're not holding the registry lock while pushing to
657 * consumer. This is handled by the fact that we send
658 * the metadata content, size, and the offset at which
659 * that metadata belongs. This may arrive out of order
660 * on the consumer side, and the consumer is able to
661 * deal with overlapping fragments. The consumer
662 * supports overlapping fragments, which must be
663 * contiguous starting from offset 0. We keep the
664 * largest metadata_len_sent value of the concurrent
667 registry
->metadata_len_sent
=
668 max_t(size_t, registry
->metadata_len_sent
,
669 new_metadata_len_sent
);
678 * On error, flag the registry that the metadata is
679 * closed. We were unable to push anything and this
680 * means that either the consumer is not responding or
681 * the metadata cache has been destroyed on the
684 registry
->metadata_closed
= 1;
692 * For a given application and session, push metadata to consumer.
693 * Either sock or consumer is required : if sock is NULL, the default
694 * socket to send the metadata is retrieved from consumer, if sock
695 * is not NULL we use it to send the metadata.
696 * RCU read-side lock must be held while calling this function,
697 * therefore ensuring existance of registry. It also ensures existance
698 * of socket throughout this function.
700 * Return 0 on success else a negative error.
701 * Returning a -EPIPE return value means we could not send the metadata,
702 * but it can be caused by recoverable errors (e.g. the application has
703 * terminated concurrently).
705 static int push_metadata(struct ust_registry_session
*registry
,
706 struct consumer_output
*consumer
)
710 struct consumer_socket
*socket
;
715 pthread_mutex_lock(®istry
->lock
);
716 if (registry
->metadata_closed
) {
721 /* Get consumer socket to use to push the metadata.*/
722 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
729 ret
= ust_app_push_metadata(registry
, socket
, 0);
734 pthread_mutex_unlock(®istry
->lock
);
738 pthread_mutex_unlock(®istry
->lock
);
743 * Send to the consumer a close metadata command for the given session. Once
744 * done, the metadata channel is deleted and the session metadata pointer is
745 * nullified. The session lock MUST be held unless the application is
746 * in the destroy path.
748 * Do not hold the registry lock while communicating with the consumerd, because
749 * doing so causes inter-process deadlocks between consumerd and sessiond with
750 * the metadata request notification.
752 * Return 0 on success else a negative value.
754 static int close_metadata(struct ust_registry_session
*registry
,
755 struct consumer_output
*consumer
)
758 struct consumer_socket
*socket
;
759 uint64_t metadata_key
;
760 bool registry_was_already_closed
;
767 pthread_mutex_lock(®istry
->lock
);
768 metadata_key
= registry
->metadata_key
;
769 registry_was_already_closed
= registry
->metadata_closed
;
770 if (metadata_key
!= 0) {
772 * Metadata closed. Even on error this means that the consumer
773 * is not responding or not found so either way a second close
774 * should NOT be emit for this registry.
776 registry
->metadata_closed
= 1;
778 pthread_mutex_unlock(®istry
->lock
);
780 if (metadata_key
== 0 || registry_was_already_closed
) {
785 /* Get consumer socket to use to push the metadata.*/
786 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
793 ret
= consumer_close_metadata(socket
, metadata_key
);
804 * We need to execute ht_destroy outside of RCU read-side critical
805 * section and outside of call_rcu thread, so we postpone its execution
806 * using ht_cleanup_push. It is simpler than to change the semantic of
807 * the many callers of delete_ust_app_session().
810 void delete_ust_app_session_rcu(struct rcu_head
*head
)
812 struct ust_app_session
*ua_sess
=
813 caa_container_of(head
, struct ust_app_session
, rcu_head
);
815 ht_cleanup_push(ua_sess
->channels
);
820 * Delete ust app session safely. RCU read lock must be held before calling
823 * The session list lock must be held by the caller.
826 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
830 struct lttng_ht_iter iter
;
831 struct ust_app_channel
*ua_chan
;
832 struct ust_registry_session
*registry
;
836 pthread_mutex_lock(&ua_sess
->lock
);
838 assert(!ua_sess
->deleted
);
839 ua_sess
->deleted
= true;
841 registry
= get_session_registry(ua_sess
);
842 /* Registry can be null on error path during initialization. */
844 /* Push metadata for application before freeing the application. */
845 (void) push_metadata(registry
, ua_sess
->consumer
);
848 * Don't ask to close metadata for global per UID buffers. Close
849 * metadata only on destroy trace session in this case. Also, the
850 * previous push metadata could have flag the metadata registry to
851 * close so don't send a close command if closed.
853 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
854 /* And ask to close it for this session registry. */
855 (void) close_metadata(registry
, ua_sess
->consumer
);
859 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
861 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
863 delete_ust_app_channel(sock
, ua_chan
, app
);
866 /* In case of per PID, the registry is kept in the session. */
867 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
868 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
871 * Registry can be null on error path during
874 buffer_reg_pid_remove(reg_pid
);
875 buffer_reg_pid_destroy(reg_pid
);
879 if (ua_sess
->handle
!= -1) {
880 pthread_mutex_lock(&app
->sock_lock
);
881 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
882 pthread_mutex_unlock(&app
->sock_lock
);
883 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
884 ERR("UST app sock %d release session handle failed with ret %d",
887 /* Remove session from application UST object descriptor. */
888 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
889 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
893 pthread_mutex_unlock(&ua_sess
->lock
);
895 consumer_output_put(ua_sess
->consumer
);
897 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
901 * Delete a traceable application structure from the global list. Never call
902 * this function outside of a call_rcu call.
904 * RCU read side lock should _NOT_ be held when calling this function.
907 void delete_ust_app(struct ust_app
*app
)
910 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
913 * The session list lock must be held during this function to guarantee
914 * the existence of ua_sess.
917 /* Delete ust app sessions info */
922 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
924 /* Free every object in the session and the session. */
926 delete_ust_app_session(sock
, ua_sess
, app
);
930 ht_cleanup_push(app
->sessions
);
931 ht_cleanup_push(app
->ust_sessions_objd
);
932 ht_cleanup_push(app
->ust_objd
);
935 * Wait until we have deleted the application from the sock hash table
936 * before closing this socket, otherwise an application could re-use the
937 * socket ID and race with the teardown, using the same hash table entry.
939 * It's OK to leave the close in call_rcu. We want it to stay unique for
940 * all RCU readers that could run concurrently with unregister app,
941 * therefore we _need_ to only close that socket after a grace period. So
942 * it should stay in this RCU callback.
944 * This close() is a very important step of the synchronization model so
945 * every modification to this function must be carefully reviewed.
951 lttng_fd_put(LTTNG_FD_APPS
, 1);
953 DBG2("UST app pid %d deleted", app
->pid
);
955 session_unlock_list();
959 * URCU intermediate call to delete an UST app.
962 void delete_ust_app_rcu(struct rcu_head
*head
)
964 struct lttng_ht_node_ulong
*node
=
965 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
966 struct ust_app
*app
=
967 caa_container_of(node
, struct ust_app
, pid_n
);
969 DBG3("Call RCU deleting app PID %d", app
->pid
);
974 * Delete the session from the application ht and delete the data structure by
975 * freeing every object inside and releasing them.
977 * The session list lock must be held by the caller.
979 static void destroy_app_session(struct ust_app
*app
,
980 struct ust_app_session
*ua_sess
)
983 struct lttng_ht_iter iter
;
988 iter
.iter
.node
= &ua_sess
->node
.node
;
989 ret
= lttng_ht_del(app
->sessions
, &iter
);
991 /* Already scheduled for teardown. */
995 /* Once deleted, free the data structure. */
996 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1003 * Alloc new UST app session.
1006 struct ust_app_session
*alloc_ust_app_session(void)
1008 struct ust_app_session
*ua_sess
;
1010 /* Init most of the default value by allocating and zeroing */
1011 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1012 if (ua_sess
== NULL
) {
1017 ua_sess
->handle
= -1;
1018 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1019 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1020 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1029 * Alloc new UST app channel.
1032 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
1033 struct ust_app_session
*ua_sess
,
1034 struct lttng_ust_channel_attr
*attr
)
1036 struct ust_app_channel
*ua_chan
;
1038 /* Init most of the default value by allocating and zeroing */
1039 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1040 if (ua_chan
== NULL
) {
1045 /* Setup channel name */
1046 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1047 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1049 ua_chan
->enabled
= 1;
1050 ua_chan
->handle
= -1;
1051 ua_chan
->session
= ua_sess
;
1052 ua_chan
->key
= get_next_channel_key();
1053 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1054 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1055 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1057 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1058 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1060 /* Copy attributes */
1062 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1063 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1064 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1065 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1066 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1067 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1068 ua_chan
->attr
.output
= attr
->output
;
1069 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1071 /* By default, the channel is a per cpu channel. */
1072 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1074 DBG3("UST app channel %s allocated", ua_chan
->name
);
1083 * Allocate and initialize a UST app stream.
1085 * Return newly allocated stream pointer or NULL on error.
1087 struct ust_app_stream
*ust_app_alloc_stream(void)
1089 struct ust_app_stream
*stream
= NULL
;
1091 stream
= zmalloc(sizeof(*stream
));
1092 if (stream
== NULL
) {
1093 PERROR("zmalloc ust app stream");
1097 /* Zero could be a valid value for a handle so flag it to -1. */
1098 stream
->handle
= -1;
1105 * Alloc new UST app event.
1108 struct ust_app_event
*alloc_ust_app_event(char *name
,
1109 struct lttng_ust_event
*attr
)
1111 struct ust_app_event
*ua_event
;
1113 /* Init most of the default value by allocating and zeroing */
1114 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1115 if (ua_event
== NULL
) {
1116 PERROR("Failed to allocate ust_app_event structure");
1120 ua_event
->enabled
= 1;
1121 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1122 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1123 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1125 /* Copy attributes */
1127 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1130 DBG3("UST app event %s allocated", ua_event
->name
);
1139 * Alloc new UST app context.
1142 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1144 struct ust_app_ctx
*ua_ctx
;
1146 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1147 if (ua_ctx
== NULL
) {
1151 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1154 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1155 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1156 char *provider_name
= NULL
, *ctx_name
= NULL
;
1158 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1159 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1160 if (!provider_name
|| !ctx_name
) {
1161 free(provider_name
);
1166 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1167 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1171 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1179 * Allocate a filter and copy the given original filter.
1181 * Return allocated filter or NULL on error.
1183 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1184 struct lttng_filter_bytecode
*orig_f
)
1186 struct lttng_filter_bytecode
*filter
= NULL
;
1188 /* Copy filter bytecode */
1189 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1191 PERROR("zmalloc alloc filter bytecode");
1195 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1202 * Create a liblttng-ust filter bytecode from given bytecode.
1204 * Return allocated filter or NULL on error.
1206 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1207 struct lttng_filter_bytecode
*orig_f
)
1209 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1211 /* Copy filter bytecode */
1212 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1214 PERROR("zmalloc alloc ust filter bytecode");
1218 assert(sizeof(struct lttng_filter_bytecode
) ==
1219 sizeof(struct lttng_ust_filter_bytecode
));
1220 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1226 * Find an ust_app using the sock and return it. RCU read side lock must be
1227 * held before calling this helper function.
1229 struct ust_app
*ust_app_find_by_sock(int sock
)
1231 struct lttng_ht_node_ulong
*node
;
1232 struct lttng_ht_iter iter
;
1234 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1235 node
= lttng_ht_iter_get_node_ulong(&iter
);
1237 DBG2("UST app find by sock %d not found", sock
);
1241 return caa_container_of(node
, struct ust_app
, sock_n
);
1248 * Find an ust_app using the notify sock and return it. RCU read side lock must
1249 * be held before calling this helper function.
1251 static struct ust_app
*find_app_by_notify_sock(int sock
)
1253 struct lttng_ht_node_ulong
*node
;
1254 struct lttng_ht_iter iter
;
1256 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1258 node
= lttng_ht_iter_get_node_ulong(&iter
);
1260 DBG2("UST app find by notify sock %d not found", sock
);
1264 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1271 * Lookup for an ust app event based on event name, filter bytecode and the
1274 * Return an ust_app_event object or NULL on error.
1276 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1277 const char *name
, const struct lttng_filter_bytecode
*filter
,
1279 const struct lttng_event_exclusion
*exclusion
)
1281 struct lttng_ht_iter iter
;
1282 struct lttng_ht_node_str
*node
;
1283 struct ust_app_event
*event
= NULL
;
1284 struct ust_app_ht_key key
;
1289 /* Setup key for event lookup. */
1291 key
.filter
= filter
;
1292 key
.loglevel_type
= loglevel_value
;
1293 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1294 key
.exclusion
= exclusion
;
1296 /* Lookup using the event name as hash and a custom match fct. */
1297 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1298 ht_match_ust_app_event
, &key
, &iter
.iter
);
1299 node
= lttng_ht_iter_get_node_str(&iter
);
1304 event
= caa_container_of(node
, struct ust_app_event
, node
);
1311 * Create the channel context on the tracer.
1313 * Called with UST app session lock held.
1316 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1317 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1321 health_code_update();
1323 pthread_mutex_lock(&app
->sock_lock
);
1324 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1325 ua_chan
->obj
, &ua_ctx
->obj
);
1326 pthread_mutex_unlock(&app
->sock_lock
);
1328 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1329 ERR("UST app create channel context failed for app (pid: %d) "
1330 "with ret %d", app
->pid
, ret
);
1333 * This is normal behavior, an application can die during the
1334 * creation process. Don't report an error so the execution can
1335 * continue normally.
1338 DBG3("UST app add context failed. Application is dead.");
1343 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1345 DBG2("UST app context handle %d created successfully for channel %s",
1346 ua_ctx
->handle
, ua_chan
->name
);
1349 health_code_update();
1354 * Set the filter on the tracer.
1357 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1358 struct ust_app
*app
)
1361 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1363 health_code_update();
1365 if (!ua_event
->filter
) {
1370 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1371 if (!ust_bytecode
) {
1372 ret
= -LTTNG_ERR_NOMEM
;
1375 pthread_mutex_lock(&app
->sock_lock
);
1376 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1378 pthread_mutex_unlock(&app
->sock_lock
);
1380 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1381 ERR("UST app event %s filter failed for app (pid: %d) "
1382 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1385 * This is normal behavior, an application can die during the
1386 * creation process. Don't report an error so the execution can
1387 * continue normally.
1390 DBG3("UST app filter event failed. Application is dead.");
1395 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1398 health_code_update();
1404 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1405 struct lttng_event_exclusion
*exclusion
)
1407 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1408 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1409 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1411 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1412 if (!ust_exclusion
) {
1417 assert(sizeof(struct lttng_event_exclusion
) ==
1418 sizeof(struct lttng_ust_event_exclusion
));
1419 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1421 return ust_exclusion
;
1425 * Set event exclusions on the tracer.
1428 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1429 struct ust_app
*app
)
1432 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1434 health_code_update();
1436 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1441 ust_exclusion
= create_ust_exclusion_from_exclusion(
1442 ua_event
->exclusion
);
1443 if (!ust_exclusion
) {
1444 ret
= -LTTNG_ERR_NOMEM
;
1447 pthread_mutex_lock(&app
->sock_lock
);
1448 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1449 pthread_mutex_unlock(&app
->sock_lock
);
1451 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1452 ERR("UST app event %s exclusions failed for app (pid: %d) "
1453 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1456 * This is normal behavior, an application can die during the
1457 * creation process. Don't report an error so the execution can
1458 * continue normally.
1461 DBG3("UST app event exclusion failed. Application is dead.");
1466 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1469 health_code_update();
1470 free(ust_exclusion
);
1475 * Disable the specified event on to UST tracer for the UST session.
1477 static int disable_ust_event(struct ust_app
*app
,
1478 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1482 health_code_update();
1484 pthread_mutex_lock(&app
->sock_lock
);
1485 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1486 pthread_mutex_unlock(&app
->sock_lock
);
1488 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1489 ERR("UST app event %s disable failed for app (pid: %d) "
1490 "and session handle %d with ret %d",
1491 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1494 * This is normal behavior, an application can die during the
1495 * creation process. Don't report an error so the execution can
1496 * continue normally.
1499 DBG3("UST app disable event failed. Application is dead.");
1504 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1505 ua_event
->attr
.name
, app
->pid
);
1508 health_code_update();
1513 * Disable the specified channel on to UST tracer for the UST session.
1515 static int disable_ust_channel(struct ust_app
*app
,
1516 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1520 health_code_update();
1522 pthread_mutex_lock(&app
->sock_lock
);
1523 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1524 pthread_mutex_unlock(&app
->sock_lock
);
1526 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1527 ERR("UST app channel %s disable failed for app (pid: %d) "
1528 "and session handle %d with ret %d",
1529 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1532 * This is normal behavior, an application can die during the
1533 * creation process. Don't report an error so the execution can
1534 * continue normally.
1537 DBG3("UST app disable channel failed. Application is dead.");
1542 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1543 ua_chan
->name
, app
->pid
);
1546 health_code_update();
1551 * Enable the specified channel on to UST tracer for the UST session.
1553 static int enable_ust_channel(struct ust_app
*app
,
1554 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1558 health_code_update();
1560 pthread_mutex_lock(&app
->sock_lock
);
1561 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1562 pthread_mutex_unlock(&app
->sock_lock
);
1564 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1565 ERR("UST app channel %s enable failed for app (pid: %d) "
1566 "and session handle %d with ret %d",
1567 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1570 * This is normal behavior, an application can die during the
1571 * creation process. Don't report an error so the execution can
1572 * continue normally.
1575 DBG3("UST app enable channel failed. Application is dead.");
1580 ua_chan
->enabled
= 1;
1582 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1583 ua_chan
->name
, app
->pid
);
1586 health_code_update();
1591 * Enable the specified event on to UST tracer for the UST session.
1593 static int enable_ust_event(struct ust_app
*app
,
1594 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1598 health_code_update();
1600 pthread_mutex_lock(&app
->sock_lock
);
1601 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1602 pthread_mutex_unlock(&app
->sock_lock
);
1604 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1605 ERR("UST app event %s enable failed for app (pid: %d) "
1606 "and session handle %d with ret %d",
1607 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1610 * This is normal behavior, an application can die during the
1611 * creation process. Don't report an error so the execution can
1612 * continue normally.
1615 DBG3("UST app enable event failed. Application is dead.");
1620 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1621 ua_event
->attr
.name
, app
->pid
);
1624 health_code_update();
1629 * Send channel and stream buffer to application.
1631 * Return 0 on success. On error, a negative value is returned.
1633 static int send_channel_pid_to_ust(struct ust_app
*app
,
1634 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1637 struct ust_app_stream
*stream
, *stmp
;
1643 health_code_update();
1645 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1648 /* Send channel to the application. */
1649 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1650 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1651 ret
= -ENOTCONN
; /* Caused by app exiting. */
1653 } else if (ret
< 0) {
1657 health_code_update();
1659 /* Send all streams to application. */
1660 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1661 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1662 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1663 ret
= -ENOTCONN
; /* Caused by app exiting. */
1665 } else if (ret
< 0) {
1668 /* We don't need the stream anymore once sent to the tracer. */
1669 cds_list_del(&stream
->list
);
1670 delete_ust_app_stream(-1, stream
, app
);
1672 /* Flag the channel that it is sent to the application. */
1673 ua_chan
->is_sent
= 1;
1676 health_code_update();
1681 * Create the specified event onto the UST tracer for a UST session.
1683 * Should be called with session mutex held.
1686 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1687 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1691 health_code_update();
1693 /* Create UST event on tracer */
1694 pthread_mutex_lock(&app
->sock_lock
);
1695 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1697 pthread_mutex_unlock(&app
->sock_lock
);
1699 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1701 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1702 ua_event
->attr
.name
, app
->pid
, ret
);
1705 * This is normal behavior, an application can die during the
1706 * creation process. Don't report an error so the execution can
1707 * continue normally.
1710 DBG3("UST app create event failed. Application is dead.");
1715 ua_event
->handle
= ua_event
->obj
->handle
;
1717 DBG2("UST app event %s created successfully for pid:%d",
1718 ua_event
->attr
.name
, app
->pid
);
1720 health_code_update();
1722 /* Set filter if one is present. */
1723 if (ua_event
->filter
) {
1724 ret
= set_ust_event_filter(ua_event
, app
);
1730 /* Set exclusions for the event */
1731 if (ua_event
->exclusion
) {
1732 ret
= set_ust_event_exclusion(ua_event
, app
);
1738 /* If event not enabled, disable it on the tracer */
1739 if (ua_event
->enabled
) {
1741 * We now need to explicitly enable the event, since it
1742 * is now disabled at creation.
1744 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1747 * If we hit an EPERM, something is wrong with our enable call. If
1748 * we get an EEXIST, there is a problem on the tracer side since we
1752 case -LTTNG_UST_ERR_PERM
:
1753 /* Code flow problem */
1755 case -LTTNG_UST_ERR_EXIST
:
1756 /* It's OK for our use case. */
1767 health_code_update();
1772 * Copy data between an UST app event and a LTT event.
1774 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1775 struct ltt_ust_event
*uevent
)
1777 size_t exclusion_alloc_size
;
1779 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1780 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1782 ua_event
->enabled
= uevent
->enabled
;
1784 /* Copy event attributes */
1785 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1787 /* Copy filter bytecode */
1788 if (uevent
->filter
) {
1789 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1790 /* Filter might be NULL here in case of ENONEM. */
1793 /* Copy exclusion data */
1794 if (uevent
->exclusion
) {
1795 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1796 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1797 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1798 if (ua_event
->exclusion
== NULL
) {
1801 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1802 exclusion_alloc_size
);
1808 * Copy data between an UST app channel and a LTT channel.
1810 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1811 struct ltt_ust_channel
*uchan
)
1813 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1815 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1816 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1818 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1819 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1821 /* Copy event attributes since the layout is different. */
1822 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1823 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1824 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1825 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1826 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1827 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1828 ua_chan
->attr
.output
= uchan
->attr
.output
;
1829 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
1832 * Note that the attribute channel type is not set since the channel on the
1833 * tracing registry side does not have this information.
1836 ua_chan
->enabled
= uchan
->enabled
;
1837 ua_chan
->tracing_channel_id
= uchan
->id
;
1839 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1843 * Copy data between a UST app session and a regular LTT session.
1845 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1846 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1848 struct tm
*timeinfo
;
1851 char tmp_shm_path
[PATH_MAX
];
1853 timeinfo
= localtime(&app
->registration_time
);
1854 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1856 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1858 ua_sess
->tracing_id
= usess
->id
;
1859 ua_sess
->id
= get_next_session_id();
1860 ua_sess
->real_credentials
.uid
= app
->uid
;
1861 ua_sess
->real_credentials
.gid
= app
->gid
;
1862 ua_sess
->effective_credentials
.uid
= usess
->uid
;
1863 ua_sess
->effective_credentials
.gid
= usess
->gid
;
1864 ua_sess
->buffer_type
= usess
->buffer_type
;
1865 ua_sess
->bits_per_long
= app
->bits_per_long
;
1867 /* There is only one consumer object per session possible. */
1868 consumer_output_get(usess
->consumer
);
1869 ua_sess
->consumer
= usess
->consumer
;
1871 ua_sess
->output_traces
= usess
->output_traces
;
1872 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1873 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1874 &usess
->metadata_attr
);
1876 switch (ua_sess
->buffer_type
) {
1877 case LTTNG_BUFFER_PER_PID
:
1878 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1879 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1882 case LTTNG_BUFFER_PER_UID
:
1883 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1884 DEFAULT_UST_TRACE_UID_PATH
,
1885 ua_sess
->real_credentials
.uid
,
1886 app
->bits_per_long
);
1893 PERROR("asprintf UST shadow copy session");
1898 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1899 sizeof(ua_sess
->root_shm_path
));
1900 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1901 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1902 sizeof(ua_sess
->shm_path
));
1903 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1904 if (ua_sess
->shm_path
[0]) {
1905 switch (ua_sess
->buffer_type
) {
1906 case LTTNG_BUFFER_PER_PID
:
1907 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1908 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1909 app
->name
, app
->pid
, datetime
);
1911 case LTTNG_BUFFER_PER_UID
:
1912 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1913 DEFAULT_UST_TRACE_UID_PATH
,
1914 app
->uid
, app
->bits_per_long
);
1921 PERROR("sprintf UST shadow copy session");
1925 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1926 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1927 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1932 consumer_output_put(ua_sess
->consumer
);
1936 * Lookup sesison wrapper.
1939 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
1940 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1942 /* Get right UST app session from app */
1943 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1947 * Return ust app session from the app session hashtable using the UST session
1950 static struct ust_app_session
*lookup_session_by_app(
1951 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
1953 struct lttng_ht_iter iter
;
1954 struct lttng_ht_node_u64
*node
;
1956 __lookup_session_by_app(usess
, app
, &iter
);
1957 node
= lttng_ht_iter_get_node_u64(&iter
);
1962 return caa_container_of(node
, struct ust_app_session
, node
);
1969 * Setup buffer registry per PID for the given session and application. If none
1970 * is found, a new one is created, added to the global registry and
1971 * initialized. If regp is valid, it's set with the newly created object.
1973 * Return 0 on success or else a negative value.
1975 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1976 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1979 struct buffer_reg_pid
*reg_pid
;
1986 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1989 * This is the create channel path meaning that if there is NO
1990 * registry available, we have to create one for this session.
1992 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
1993 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2001 /* Initialize registry. */
2002 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2003 app
->bits_per_long
, app
->uint8_t_alignment
,
2004 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2005 app
->uint64_t_alignment
, app
->long_alignment
,
2006 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2007 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2008 ua_sess
->effective_credentials
.uid
,
2009 ua_sess
->effective_credentials
.gid
, ua_sess
->tracing_id
,
2013 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2014 * destroy the buffer registry, because it is always expected
2015 * that if the buffer registry can be found, its ust registry is
2018 buffer_reg_pid_destroy(reg_pid
);
2022 buffer_reg_pid_add(reg_pid
);
2024 DBG3("UST app buffer registry per PID created successfully");
2036 * Setup buffer registry per UID for the given session and application. If none
2037 * is found, a new one is created, added to the global registry and
2038 * initialized. If regp is valid, it's set with the newly created object.
2040 * Return 0 on success or else a negative value.
2042 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2043 struct ust_app_session
*ua_sess
,
2044 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2047 struct buffer_reg_uid
*reg_uid
;
2054 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2057 * This is the create channel path meaning that if there is NO
2058 * registry available, we have to create one for this session.
2060 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2061 LTTNG_DOMAIN_UST
, ®_uid
,
2062 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2070 /* Initialize registry. */
2071 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2072 app
->bits_per_long
, app
->uint8_t_alignment
,
2073 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2074 app
->uint64_t_alignment
, app
->long_alignment
,
2075 app
->byte_order
, app
->version
.major
,
2076 app
->version
.minor
, reg_uid
->root_shm_path
,
2077 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2078 ua_sess
->tracing_id
, app
->uid
);
2081 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2082 * destroy the buffer registry, because it is always expected
2083 * that if the buffer registry can be found, its ust registry is
2086 buffer_reg_uid_destroy(reg_uid
, NULL
);
2089 /* Add node to teardown list of the session. */
2090 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2092 buffer_reg_uid_add(reg_uid
);
2094 DBG3("UST app buffer registry per UID created successfully");
2105 * Create a session on the tracer side for the given app.
2107 * On success, ua_sess_ptr is populated with the session pointer or else left
2108 * untouched. If the session was created, is_created is set to 1. On error,
2109 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2112 * Returns 0 on success or else a negative code which is either -ENOMEM or
2113 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2115 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2116 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2119 int ret
, created
= 0;
2120 struct ust_app_session
*ua_sess
;
2124 assert(ua_sess_ptr
);
2126 health_code_update();
2128 ua_sess
= lookup_session_by_app(usess
, app
);
2129 if (ua_sess
== NULL
) {
2130 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2131 app
->pid
, usess
->id
);
2132 ua_sess
= alloc_ust_app_session();
2133 if (ua_sess
== NULL
) {
2134 /* Only malloc can failed so something is really wrong */
2138 shadow_copy_session(ua_sess
, usess
, app
);
2142 switch (usess
->buffer_type
) {
2143 case LTTNG_BUFFER_PER_PID
:
2144 /* Init local registry. */
2145 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2147 delete_ust_app_session(-1, ua_sess
, app
);
2151 case LTTNG_BUFFER_PER_UID
:
2152 /* Look for a global registry. If none exists, create one. */
2153 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2155 delete_ust_app_session(-1, ua_sess
, app
);
2165 health_code_update();
2167 if (ua_sess
->handle
== -1) {
2168 pthread_mutex_lock(&app
->sock_lock
);
2169 ret
= ustctl_create_session(app
->sock
);
2170 pthread_mutex_unlock(&app
->sock_lock
);
2172 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2173 ERR("Creating session for app pid %d with ret %d",
2176 DBG("UST app creating session failed. Application is dead");
2178 * This is normal behavior, an application can die during the
2179 * creation process. Don't report an error so the execution can
2180 * continue normally. This will get flagged ENOTCONN and the
2181 * caller will handle it.
2185 delete_ust_app_session(-1, ua_sess
, app
);
2186 if (ret
!= -ENOMEM
) {
2188 * Tracer is probably gone or got an internal error so let's
2189 * behave like it will soon unregister or not usable.
2196 ua_sess
->handle
= ret
;
2198 /* Add ust app session to app's HT */
2199 lttng_ht_node_init_u64(&ua_sess
->node
,
2200 ua_sess
->tracing_id
);
2201 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2202 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2203 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2204 &ua_sess
->ust_objd_node
);
2206 DBG2("UST app session created successfully with handle %d", ret
);
2209 *ua_sess_ptr
= ua_sess
;
2211 *is_created
= created
;
2214 /* Everything went well. */
2218 health_code_update();
2223 * Match function for a hash table lookup of ust_app_ctx.
2225 * It matches an ust app context based on the context type and, in the case
2226 * of perf counters, their name.
2228 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2230 struct ust_app_ctx
*ctx
;
2231 const struct lttng_ust_context_attr
*key
;
2236 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2240 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2245 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2246 if (strncmp(key
->u
.perf_counter
.name
,
2247 ctx
->ctx
.u
.perf_counter
.name
,
2248 sizeof(key
->u
.perf_counter
.name
))) {
2252 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2253 if (strcmp(key
->u
.app_ctx
.provider_name
,
2254 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2255 strcmp(key
->u
.app_ctx
.ctx_name
,
2256 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2272 * Lookup for an ust app context from an lttng_ust_context.
2274 * Must be called while holding RCU read side lock.
2275 * Return an ust_app_ctx object or NULL on error.
2278 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2279 struct lttng_ust_context_attr
*uctx
)
2281 struct lttng_ht_iter iter
;
2282 struct lttng_ht_node_ulong
*node
;
2283 struct ust_app_ctx
*app_ctx
= NULL
;
2288 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2289 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2290 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2291 node
= lttng_ht_iter_get_node_ulong(&iter
);
2296 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2303 * Create a context for the channel on the tracer.
2305 * Called with UST app session lock held and a RCU read side lock.
2308 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2309 struct lttng_ust_context_attr
*uctx
,
2310 struct ust_app
*app
)
2313 struct ust_app_ctx
*ua_ctx
;
2315 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2317 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2323 ua_ctx
= alloc_ust_app_ctx(uctx
);
2324 if (ua_ctx
== NULL
) {
2330 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2331 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2332 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2334 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2344 * Enable on the tracer side a ust app event for the session and channel.
2346 * Called with UST app session lock held.
2349 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2350 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2354 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2359 ua_event
->enabled
= 1;
2366 * Disable on the tracer side a ust app event for the session and channel.
2368 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2369 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2373 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2378 ua_event
->enabled
= 0;
2385 * Lookup ust app channel for session and disable it on the tracer side.
2388 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2389 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2393 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2398 ua_chan
->enabled
= 0;
2405 * Lookup ust app channel for session and enable it on the tracer side. This
2406 * MUST be called with a RCU read side lock acquired.
2408 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2409 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2412 struct lttng_ht_iter iter
;
2413 struct lttng_ht_node_str
*ua_chan_node
;
2414 struct ust_app_channel
*ua_chan
;
2416 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2417 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2418 if (ua_chan_node
== NULL
) {
2419 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2420 uchan
->name
, ua_sess
->tracing_id
);
2424 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2426 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2436 * Ask the consumer to create a channel and get it if successful.
2438 * Called with UST app session lock held.
2440 * Return 0 on success or else a negative value.
2442 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2443 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2444 int bitness
, struct ust_registry_session
*registry
,
2445 uint64_t trace_archive_id
)
2448 unsigned int nb_fd
= 0;
2449 struct consumer_socket
*socket
;
2457 health_code_update();
2459 /* Get the right consumer socket for the application. */
2460 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2466 health_code_update();
2468 /* Need one fd for the channel. */
2469 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2471 ERR("Exhausted number of available FD upon create channel");
2476 * Ask consumer to create channel. The consumer will return the number of
2477 * stream we have to expect.
2479 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2480 registry
, usess
->current_trace_chunk
);
2486 * Compute the number of fd needed before receiving them. It must be 2 per
2487 * stream (2 being the default value here).
2489 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2491 /* Reserve the amount of file descriptor we need. */
2492 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2494 ERR("Exhausted number of available FD upon create channel");
2495 goto error_fd_get_stream
;
2498 health_code_update();
2501 * Now get the channel from the consumer. This call wil populate the stream
2502 * list of that channel and set the ust objects.
2504 if (usess
->consumer
->enabled
) {
2505 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2515 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2516 error_fd_get_stream
:
2518 * Initiate a destroy channel on the consumer since we had an error
2519 * handling it on our side. The return value is of no importance since we
2520 * already have a ret value set by the previous error that we need to
2523 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2525 lttng_fd_put(LTTNG_FD_APPS
, 1);
2527 health_code_update();
2533 * Duplicate the ust data object of the ust app stream and save it in the
2534 * buffer registry stream.
2536 * Return 0 on success or else a negative value.
2538 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2539 struct ust_app_stream
*stream
)
2546 /* Reserve the amount of file descriptor we need. */
2547 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2549 ERR("Exhausted number of available FD upon duplicate stream");
2553 /* Duplicate object for stream once the original is in the registry. */
2554 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2555 reg_stream
->obj
.ust
);
2557 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2558 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2559 lttng_fd_put(LTTNG_FD_APPS
, 2);
2562 stream
->handle
= stream
->obj
->handle
;
2569 * Duplicate the ust data object of the ust app. channel and save it in the
2570 * buffer registry channel.
2572 * Return 0 on success or else a negative value.
2574 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2575 struct ust_app_channel
*ua_chan
)
2582 /* Need two fds for the channel. */
2583 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2585 ERR("Exhausted number of available FD upon duplicate channel");
2589 /* Duplicate object for stream once the original is in the registry. */
2590 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2592 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2593 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2596 ua_chan
->handle
= ua_chan
->obj
->handle
;
2601 lttng_fd_put(LTTNG_FD_APPS
, 1);
2607 * For a given channel buffer registry, setup all streams of the given ust
2608 * application channel.
2610 * Return 0 on success or else a negative value.
2612 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2613 struct ust_app_channel
*ua_chan
,
2614 struct ust_app
*app
)
2617 struct ust_app_stream
*stream
, *stmp
;
2622 DBG2("UST app setup buffer registry stream");
2624 /* Send all streams to application. */
2625 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2626 struct buffer_reg_stream
*reg_stream
;
2628 ret
= buffer_reg_stream_create(®_stream
);
2634 * Keep original pointer and nullify it in the stream so the delete
2635 * stream call does not release the object.
2637 reg_stream
->obj
.ust
= stream
->obj
;
2639 buffer_reg_stream_add(reg_stream
, reg_chan
);
2641 /* We don't need the streams anymore. */
2642 cds_list_del(&stream
->list
);
2643 delete_ust_app_stream(-1, stream
, app
);
2651 * Create a buffer registry channel for the given session registry and
2652 * application channel object. If regp pointer is valid, it's set with the
2653 * created object. Important, the created object is NOT added to the session
2654 * registry hash table.
2656 * Return 0 on success else a negative value.
2658 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2659 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2662 struct buffer_reg_channel
*reg_chan
= NULL
;
2667 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2669 /* Create buffer registry channel. */
2670 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2675 reg_chan
->consumer_key
= ua_chan
->key
;
2676 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2677 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2679 /* Create and add a channel registry to session. */
2680 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2681 ua_chan
->tracing_channel_id
);
2685 buffer_reg_channel_add(reg_sess
, reg_chan
);
2694 /* Safe because the registry channel object was not added to any HT. */
2695 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2701 * Setup buffer registry channel for the given session registry and application
2702 * channel object. If regp pointer is valid, it's set with the created object.
2704 * Return 0 on success else a negative value.
2706 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2707 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2708 struct ust_app
*app
)
2715 assert(ua_chan
->obj
);
2717 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2719 /* Setup all streams for the registry. */
2720 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2725 reg_chan
->obj
.ust
= ua_chan
->obj
;
2726 ua_chan
->obj
= NULL
;
2731 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2732 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2737 * Send buffer registry channel to the application.
2739 * Return 0 on success else a negative value.
2741 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2742 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2743 struct ust_app_channel
*ua_chan
)
2746 struct buffer_reg_stream
*reg_stream
;
2753 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2755 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2760 /* Send channel to the application. */
2761 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2762 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2763 ret
= -ENOTCONN
; /* Caused by app exiting. */
2765 } else if (ret
< 0) {
2769 health_code_update();
2771 /* Send all streams to application. */
2772 pthread_mutex_lock(®_chan
->stream_list_lock
);
2773 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2774 struct ust_app_stream stream
;
2776 ret
= duplicate_stream_object(reg_stream
, &stream
);
2778 goto error_stream_unlock
;
2781 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2783 (void) release_ust_app_stream(-1, &stream
, app
);
2784 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2785 ret
= -ENOTCONN
; /* Caused by app exiting. */
2787 goto error_stream_unlock
;
2791 * The return value is not important here. This function will output an
2794 (void) release_ust_app_stream(-1, &stream
, app
);
2796 ua_chan
->is_sent
= 1;
2798 error_stream_unlock
:
2799 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2805 * Create and send to the application the created buffers with per UID buffers.
2807 * This MUST be called with a RCU read side lock acquired.
2808 * The session list lock and the session's lock must be acquired.
2810 * Return 0 on success else a negative value.
2812 static int create_channel_per_uid(struct ust_app
*app
,
2813 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2814 struct ust_app_channel
*ua_chan
)
2817 struct buffer_reg_uid
*reg_uid
;
2818 struct buffer_reg_channel
*reg_chan
;
2819 struct ltt_session
*session
= NULL
;
2820 enum lttng_error_code notification_ret
;
2821 struct ust_registry_channel
*chan_reg
;
2828 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2830 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2832 * The session creation handles the creation of this global registry
2833 * object. If none can be find, there is a code flow problem or a
2838 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2844 /* Create the buffer registry channel object. */
2845 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2847 ERR("Error creating the UST channel \"%s\" registry instance",
2852 session
= session_find_by_id(ua_sess
->tracing_id
);
2854 assert(pthread_mutex_trylock(&session
->lock
));
2855 assert(session_trylock_list());
2858 * Create the buffers on the consumer side. This call populates the
2859 * ust app channel object with all streams and data object.
2861 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2862 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
2863 session
->most_recent_chunk_id
.value
);
2865 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2869 * Let's remove the previously created buffer registry channel so
2870 * it's not visible anymore in the session registry.
2872 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2873 ua_chan
->tracing_channel_id
, false);
2874 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2875 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2880 * Setup the streams and add it to the session registry.
2882 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2883 ua_chan
, reg_chan
, app
);
2885 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
2889 /* Notify the notification subsystem of the channel's creation. */
2890 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2891 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2892 ua_chan
->tracing_channel_id
);
2894 chan_reg
->consumer_key
= ua_chan
->key
;
2896 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2898 notification_ret
= notification_thread_command_add_channel(
2899 notification_thread_handle
, session
->name
,
2900 ua_sess
->effective_credentials
.uid
,
2901 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
2902 ua_chan
->key
, LTTNG_DOMAIN_UST
,
2903 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2904 if (notification_ret
!= LTTNG_OK
) {
2905 ret
= - (int) notification_ret
;
2906 ERR("Failed to add channel to notification thread");
2911 /* Send buffers to the application. */
2912 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2914 if (ret
!= -ENOTCONN
) {
2915 ERR("Error sending channel to application");
2922 session_put(session
);
2928 * Create and send to the application the created buffers with per PID buffers.
2930 * Called with UST app session lock held.
2931 * The session list lock and the session's lock must be acquired.
2933 * Return 0 on success else a negative value.
2935 static int create_channel_per_pid(struct ust_app
*app
,
2936 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2937 struct ust_app_channel
*ua_chan
)
2940 struct ust_registry_session
*registry
;
2941 enum lttng_error_code cmd_ret
;
2942 struct ltt_session
*session
= NULL
;
2943 uint64_t chan_reg_key
;
2944 struct ust_registry_channel
*chan_reg
;
2951 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2955 registry
= get_session_registry(ua_sess
);
2956 /* The UST app session lock is held, registry shall not be null. */
2959 /* Create and add a new channel registry to session. */
2960 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2962 ERR("Error creating the UST channel \"%s\" registry instance",
2967 session
= session_find_by_id(ua_sess
->tracing_id
);
2970 assert(pthread_mutex_trylock(&session
->lock
));
2971 assert(session_trylock_list());
2973 /* Create and get channel on the consumer side. */
2974 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2975 app
->bits_per_long
, registry
,
2976 session
->most_recent_chunk_id
.value
);
2978 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2980 goto error_remove_from_registry
;
2983 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2985 if (ret
!= -ENOTCONN
) {
2986 ERR("Error sending channel to application");
2988 goto error_remove_from_registry
;
2991 chan_reg_key
= ua_chan
->key
;
2992 pthread_mutex_lock(®istry
->lock
);
2993 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
2995 chan_reg
->consumer_key
= ua_chan
->key
;
2996 pthread_mutex_unlock(®istry
->lock
);
2998 cmd_ret
= notification_thread_command_add_channel(
2999 notification_thread_handle
, session
->name
,
3000 ua_sess
->effective_credentials
.uid
,
3001 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
3002 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3003 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3004 if (cmd_ret
!= LTTNG_OK
) {
3005 ret
= - (int) cmd_ret
;
3006 ERR("Failed to add channel to notification thread");
3007 goto error_remove_from_registry
;
3010 error_remove_from_registry
:
3012 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3017 session_put(session
);
3023 * From an already allocated ust app channel, create the channel buffers if
3024 * needed and send them to the application. This MUST be called with a RCU read
3025 * side lock acquired.
3027 * Called with UST app session lock held.
3029 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3030 * the application exited concurrently.
3032 static int ust_app_channel_send(struct ust_app
*app
,
3033 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3034 struct ust_app_channel
*ua_chan
)
3040 assert(usess
->active
);
3044 /* Handle buffer type before sending the channel to the application. */
3045 switch (usess
->buffer_type
) {
3046 case LTTNG_BUFFER_PER_UID
:
3048 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3054 case LTTNG_BUFFER_PER_PID
:
3056 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3068 /* Initialize ust objd object using the received handle and add it. */
3069 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3070 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3072 /* If channel is not enabled, disable it on the tracer */
3073 if (!ua_chan
->enabled
) {
3074 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3085 * Create UST app channel and return it through ua_chanp if not NULL.
3087 * Called with UST app session lock and RCU read-side lock held.
3089 * Return 0 on success or else a negative value.
3091 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3092 struct ltt_ust_channel
*uchan
,
3093 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3094 struct ust_app_channel
**ua_chanp
)
3097 struct lttng_ht_iter iter
;
3098 struct lttng_ht_node_str
*ua_chan_node
;
3099 struct ust_app_channel
*ua_chan
;
3101 /* Lookup channel in the ust app session */
3102 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3103 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3104 if (ua_chan_node
!= NULL
) {
3105 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3109 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3110 if (ua_chan
== NULL
) {
3111 /* Only malloc can fail here */
3115 shadow_copy_channel(ua_chan
, uchan
);
3117 /* Set channel type. */
3118 ua_chan
->attr
.type
= type
;
3120 /* Only add the channel if successful on the tracer side. */
3121 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3124 *ua_chanp
= ua_chan
;
3127 /* Everything went well. */
3135 * Create UST app event and create it on the tracer side.
3137 * Called with ust app session mutex held.
3140 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3141 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3142 struct ust_app
*app
)
3145 struct ust_app_event
*ua_event
;
3147 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3148 if (ua_event
== NULL
) {
3149 /* Only failure mode of alloc_ust_app_event(). */
3153 shadow_copy_event(ua_event
, uevent
);
3155 /* Create it on the tracer side */
3156 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3159 * Not found previously means that it does not exist on the
3160 * tracer. If the application reports that the event existed,
3161 * it means there is a bug in the sessiond or lttng-ust
3162 * (or corruption, etc.)
3164 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3165 ERR("Tracer for application reported that an event being created already existed: "
3166 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3168 app
->pid
, app
->ppid
, app
->uid
,
3174 add_unique_ust_app_event(ua_chan
, ua_event
);
3176 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3183 /* Valid. Calling here is already in a read side lock */
3184 delete_ust_app_event(-1, ua_event
, app
);
3189 * Create UST metadata and open it on the tracer side.
3191 * Called with UST app session lock held and RCU read side lock.
3193 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3194 struct ust_app
*app
, struct consumer_output
*consumer
)
3197 struct ust_app_channel
*metadata
;
3198 struct consumer_socket
*socket
;
3199 struct ust_registry_session
*registry
;
3200 struct ltt_session
*session
= NULL
;
3206 registry
= get_session_registry(ua_sess
);
3207 /* The UST app session is held registry shall not be null. */
3210 pthread_mutex_lock(®istry
->lock
);
3212 /* Metadata already exists for this registry or it was closed previously */
3213 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3218 /* Allocate UST metadata */
3219 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3221 /* malloc() failed */
3226 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3228 /* Need one fd for the channel. */
3229 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3231 ERR("Exhausted number of available FD upon create metadata");
3235 /* Get the right consumer socket for the application. */
3236 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3239 goto error_consumer
;
3243 * Keep metadata key so we can identify it on the consumer side. Assign it
3244 * to the registry *before* we ask the consumer so we avoid the race of the
3245 * consumer requesting the metadata and the ask_channel call on our side
3246 * did not returned yet.
3248 registry
->metadata_key
= metadata
->key
;
3250 session
= session_find_by_id(ua_sess
->tracing_id
);
3253 assert(pthread_mutex_trylock(&session
->lock
));
3254 assert(session_trylock_list());
3257 * Ask the metadata channel creation to the consumer. The metadata object
3258 * will be created by the consumer and kept their. However, the stream is
3259 * never added or monitored until we do a first push metadata to the
3262 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3263 registry
, session
->current_trace_chunk
);
3265 /* Nullify the metadata key so we don't try to close it later on. */
3266 registry
->metadata_key
= 0;
3267 goto error_consumer
;
3271 * The setup command will make the metadata stream be sent to the relayd,
3272 * if applicable, and the thread managing the metadatas. This is important
3273 * because after this point, if an error occurs, the only way the stream
3274 * can be deleted is to be monitored in the consumer.
3276 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3278 /* Nullify the metadata key so we don't try to close it later on. */
3279 registry
->metadata_key
= 0;
3280 goto error_consumer
;
3283 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3284 metadata
->key
, app
->pid
);
3287 lttng_fd_put(LTTNG_FD_APPS
, 1);
3288 delete_ust_app_channel(-1, metadata
, app
);
3290 pthread_mutex_unlock(®istry
->lock
);
3292 session_put(session
);
3298 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3299 * acquired before calling this function.
3301 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3303 struct ust_app
*app
= NULL
;
3304 struct lttng_ht_node_ulong
*node
;
3305 struct lttng_ht_iter iter
;
3307 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3308 node
= lttng_ht_iter_get_node_ulong(&iter
);
3310 DBG2("UST app no found with pid %d", pid
);
3314 DBG2("Found UST app by pid %d", pid
);
3316 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3323 * Allocate and init an UST app object using the registration information and
3324 * the command socket. This is called when the command socket connects to the
3327 * The object is returned on success or else NULL.
3329 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3331 struct ust_app
*lta
= NULL
;
3336 DBG3("UST app creating application for socket %d", sock
);
3338 if ((msg
->bits_per_long
== 64 &&
3339 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3340 || (msg
->bits_per_long
== 32 &&
3341 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3342 ERR("Registration failed: application \"%s\" (pid: %d) has "
3343 "%d-bit long, but no consumerd for this size is available.\n",
3344 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3348 lta
= zmalloc(sizeof(struct ust_app
));
3354 lta
->ppid
= msg
->ppid
;
3355 lta
->uid
= msg
->uid
;
3356 lta
->gid
= msg
->gid
;
3358 lta
->bits_per_long
= msg
->bits_per_long
;
3359 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3360 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3361 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3362 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3363 lta
->long_alignment
= msg
->long_alignment
;
3364 lta
->byte_order
= msg
->byte_order
;
3366 lta
->v_major
= msg
->major
;
3367 lta
->v_minor
= msg
->minor
;
3368 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3369 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3370 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3371 lta
->notify_sock
= -1;
3373 /* Copy name and make sure it's NULL terminated. */
3374 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3375 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3378 * Before this can be called, when receiving the registration information,
3379 * the application compatibility is checked. So, at this point, the
3380 * application can work with this session daemon.
3382 lta
->compatible
= 1;
3384 lta
->pid
= msg
->pid
;
3385 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3387 pthread_mutex_init(<a
->sock_lock
, NULL
);
3388 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3390 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3396 * For a given application object, add it to every hash table.
3398 void ust_app_add(struct ust_app
*app
)
3401 assert(app
->notify_sock
>= 0);
3403 app
->registration_time
= time(NULL
);
3408 * On a re-registration, we want to kick out the previous registration of
3411 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3414 * The socket _should_ be unique until _we_ call close. So, a add_unique
3415 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3416 * already in the table.
3418 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3420 /* Add application to the notify socket hash table. */
3421 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3422 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3424 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3425 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3426 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3433 * Set the application version into the object.
3435 * Return 0 on success else a negative value either an errno code or a
3436 * LTTng-UST error code.
3438 int ust_app_version(struct ust_app
*app
)
3444 pthread_mutex_lock(&app
->sock_lock
);
3445 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3446 pthread_mutex_unlock(&app
->sock_lock
);
3448 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3449 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3451 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3459 * Unregister app by removing it from the global traceable app list and freeing
3462 * The socket is already closed at this point so no close to sock.
3464 void ust_app_unregister(int sock
)
3466 struct ust_app
*lta
;
3467 struct lttng_ht_node_ulong
*node
;
3468 struct lttng_ht_iter ust_app_sock_iter
;
3469 struct lttng_ht_iter iter
;
3470 struct ust_app_session
*ua_sess
;
3475 /* Get the node reference for a call_rcu */
3476 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3477 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3480 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3481 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3484 * For per-PID buffers, perform "push metadata" and flush all
3485 * application streams before removing app from hash tables,
3486 * ensuring proper behavior of data_pending check.
3487 * Remove sessions so they are not visible during deletion.
3489 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3491 struct ust_registry_session
*registry
;
3493 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3495 /* The session was already removed so scheduled for teardown. */
3499 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3500 (void) ust_app_flush_app_session(lta
, ua_sess
);
3504 * Add session to list for teardown. This is safe since at this point we
3505 * are the only one using this list.
3507 pthread_mutex_lock(&ua_sess
->lock
);
3509 if (ua_sess
->deleted
) {
3510 pthread_mutex_unlock(&ua_sess
->lock
);
3515 * Normally, this is done in the delete session process which is
3516 * executed in the call rcu below. However, upon registration we can't
3517 * afford to wait for the grace period before pushing data or else the
3518 * data pending feature can race between the unregistration and stop
3519 * command where the data pending command is sent *before* the grace
3522 * The close metadata below nullifies the metadata pointer in the
3523 * session so the delete session will NOT push/close a second time.
3525 registry
= get_session_registry(ua_sess
);
3527 /* Push metadata for application before freeing the application. */
3528 (void) push_metadata(registry
, ua_sess
->consumer
);
3531 * Don't ask to close metadata for global per UID buffers. Close
3532 * metadata only on destroy trace session in this case. Also, the
3533 * previous push metadata could have flag the metadata registry to
3534 * close so don't send a close command if closed.
3536 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3537 /* And ask to close it for this session registry. */
3538 (void) close_metadata(registry
, ua_sess
->consumer
);
3541 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3543 pthread_mutex_unlock(&ua_sess
->lock
);
3546 /* Remove application from PID hash table */
3547 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3551 * Remove application from notify hash table. The thread handling the
3552 * notify socket could have deleted the node so ignore on error because
3553 * either way it's valid. The close of that socket is handled by the
3554 * apps_notify_thread.
3556 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3557 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3560 * Ignore return value since the node might have been removed before by an
3561 * add replace during app registration because the PID can be reassigned by
3564 iter
.iter
.node
= <a
->pid_n
.node
;
3565 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3567 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3572 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3579 * Fill events array with all events name of all registered apps.
3581 int ust_app_list_events(struct lttng_event
**events
)
3584 size_t nbmem
, count
= 0;
3585 struct lttng_ht_iter iter
;
3586 struct ust_app
*app
;
3587 struct lttng_event
*tmp_event
;
3589 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3590 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3591 if (tmp_event
== NULL
) {
3592 PERROR("zmalloc ust app events");
3599 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3600 struct lttng_ust_tracepoint_iter uiter
;
3602 health_code_update();
3604 if (!app
->compatible
) {
3606 * TODO: In time, we should notice the caller of this error by
3607 * telling him that this is a version error.
3611 pthread_mutex_lock(&app
->sock_lock
);
3612 handle
= ustctl_tracepoint_list(app
->sock
);
3614 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3615 ERR("UST app list events getting handle failed for app pid %d",
3618 pthread_mutex_unlock(&app
->sock_lock
);
3622 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3623 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3624 /* Handle ustctl error. */
3628 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3629 ERR("UST app tp list get failed for app %d with ret %d",
3632 DBG3("UST app tp list get failed. Application is dead");
3634 * This is normal behavior, an application can die during the
3635 * creation process. Don't report an error so the execution can
3636 * continue normally. Continue normal execution.
3641 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3642 if (release_ret
< 0 &&
3643 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3644 release_ret
!= -EPIPE
) {
3645 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3647 pthread_mutex_unlock(&app
->sock_lock
);
3651 health_code_update();
3652 if (count
>= nbmem
) {
3653 /* In case the realloc fails, we free the memory */
3654 struct lttng_event
*new_tmp_event
;
3657 new_nbmem
= nbmem
<< 1;
3658 DBG2("Reallocating event list from %zu to %zu entries",
3660 new_tmp_event
= realloc(tmp_event
,
3661 new_nbmem
* sizeof(struct lttng_event
));
3662 if (new_tmp_event
== NULL
) {
3665 PERROR("realloc ust app events");
3668 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3669 if (release_ret
< 0 &&
3670 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3671 release_ret
!= -EPIPE
) {
3672 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3674 pthread_mutex_unlock(&app
->sock_lock
);
3677 /* Zero the new memory */
3678 memset(new_tmp_event
+ nbmem
, 0,
3679 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3681 tmp_event
= new_tmp_event
;
3683 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3684 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3685 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3686 tmp_event
[count
].pid
= app
->pid
;
3687 tmp_event
[count
].enabled
= -1;
3690 ret
= ustctl_release_handle(app
->sock
, handle
);
3691 pthread_mutex_unlock(&app
->sock_lock
);
3692 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3693 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3698 *events
= tmp_event
;
3700 DBG2("UST app list events done (%zu events)", count
);
3705 health_code_update();
3710 * Fill events array with all events name of all registered apps.
3712 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3715 size_t nbmem
, count
= 0;
3716 struct lttng_ht_iter iter
;
3717 struct ust_app
*app
;
3718 struct lttng_event_field
*tmp_event
;
3720 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3721 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3722 if (tmp_event
== NULL
) {
3723 PERROR("zmalloc ust app event fields");
3730 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3731 struct lttng_ust_field_iter uiter
;
3733 health_code_update();
3735 if (!app
->compatible
) {
3737 * TODO: In time, we should notice the caller of this error by
3738 * telling him that this is a version error.
3742 pthread_mutex_lock(&app
->sock_lock
);
3743 handle
= ustctl_tracepoint_field_list(app
->sock
);
3745 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3746 ERR("UST app list field getting handle failed for app pid %d",
3749 pthread_mutex_unlock(&app
->sock_lock
);
3753 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3754 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3755 /* Handle ustctl error. */
3759 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3760 ERR("UST app tp list field failed for app %d with ret %d",
3763 DBG3("UST app tp list field failed. Application is dead");
3765 * This is normal behavior, an application can die during the
3766 * creation process. Don't report an error so the execution can
3767 * continue normally. Reset list and count for next app.
3772 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3773 pthread_mutex_unlock(&app
->sock_lock
);
3774 if (release_ret
< 0 &&
3775 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3776 release_ret
!= -EPIPE
) {
3777 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3782 health_code_update();
3783 if (count
>= nbmem
) {
3784 /* In case the realloc fails, we free the memory */
3785 struct lttng_event_field
*new_tmp_event
;
3788 new_nbmem
= nbmem
<< 1;
3789 DBG2("Reallocating event field list from %zu to %zu entries",
3791 new_tmp_event
= realloc(tmp_event
,
3792 new_nbmem
* sizeof(struct lttng_event_field
));
3793 if (new_tmp_event
== NULL
) {
3796 PERROR("realloc ust app event fields");
3799 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3800 pthread_mutex_unlock(&app
->sock_lock
);
3802 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3803 release_ret
!= -EPIPE
) {
3804 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3808 /* Zero the new memory */
3809 memset(new_tmp_event
+ nbmem
, 0,
3810 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3812 tmp_event
= new_tmp_event
;
3815 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3816 /* Mapping between these enums matches 1 to 1. */
3817 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3818 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3820 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3821 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3822 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3823 tmp_event
[count
].event
.pid
= app
->pid
;
3824 tmp_event
[count
].event
.enabled
= -1;
3827 ret
= ustctl_release_handle(app
->sock
, handle
);
3828 pthread_mutex_unlock(&app
->sock_lock
);
3830 ret
!= -LTTNG_UST_ERR_EXITING
&&
3832 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3837 *fields
= tmp_event
;
3839 DBG2("UST app list event fields done (%zu events)", count
);
3844 health_code_update();
3849 * Free and clean all traceable apps of the global list.
3851 * Should _NOT_ be called with RCU read-side lock held.
3853 void ust_app_clean_list(void)
3856 struct ust_app
*app
;
3857 struct lttng_ht_iter iter
;
3859 DBG2("UST app cleaning registered apps hash table");
3863 /* Cleanup notify socket hash table */
3864 if (ust_app_ht_by_notify_sock
) {
3865 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3866 notify_sock_n
.node
) {
3867 struct cds_lfht_node
*node
;
3868 struct ust_app
*app
;
3870 node
= cds_lfht_iter_get_node(&iter
.iter
);
3875 app
= container_of(node
, struct ust_app
,
3876 notify_sock_n
.node
);
3877 ust_app_notify_sock_unregister(app
->notify_sock
);
3882 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3883 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3885 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3889 /* Cleanup socket hash table */
3890 if (ust_app_ht_by_sock
) {
3891 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3893 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3900 /* Destroy is done only when the ht is empty */
3902 ht_cleanup_push(ust_app_ht
);
3904 if (ust_app_ht_by_sock
) {
3905 ht_cleanup_push(ust_app_ht_by_sock
);
3907 if (ust_app_ht_by_notify_sock
) {
3908 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3913 * Init UST app hash table.
3915 int ust_app_ht_alloc(void)
3917 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3921 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3922 if (!ust_app_ht_by_sock
) {
3925 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3926 if (!ust_app_ht_by_notify_sock
) {
3933 * For a specific UST session, disable the channel for all registered apps.
3935 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3936 struct ltt_ust_channel
*uchan
)
3939 struct lttng_ht_iter iter
;
3940 struct lttng_ht_node_str
*ua_chan_node
;
3941 struct ust_app
*app
;
3942 struct ust_app_session
*ua_sess
;
3943 struct ust_app_channel
*ua_chan
;
3945 assert(usess
->active
);
3946 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3947 uchan
->name
, usess
->id
);
3951 /* For every registered applications */
3952 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3953 struct lttng_ht_iter uiter
;
3954 if (!app
->compatible
) {
3956 * TODO: In time, we should notice the caller of this error by
3957 * telling him that this is a version error.
3961 ua_sess
= lookup_session_by_app(usess
, app
);
3962 if (ua_sess
== NULL
) {
3967 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3968 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3969 /* If the session if found for the app, the channel must be there */
3970 assert(ua_chan_node
);
3972 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3973 /* The channel must not be already disabled */
3974 assert(ua_chan
->enabled
== 1);
3976 /* Disable channel onto application */
3977 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3979 /* XXX: We might want to report this error at some point... */
3989 * For a specific UST session, enable the channel for all registered apps.
3991 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3992 struct ltt_ust_channel
*uchan
)
3995 struct lttng_ht_iter iter
;
3996 struct ust_app
*app
;
3997 struct ust_app_session
*ua_sess
;
3999 assert(usess
->active
);
4000 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4001 uchan
->name
, usess
->id
);
4005 /* For every registered applications */
4006 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4007 if (!app
->compatible
) {
4009 * TODO: In time, we should notice the caller of this error by
4010 * telling him that this is a version error.
4014 ua_sess
= lookup_session_by_app(usess
, app
);
4015 if (ua_sess
== NULL
) {
4019 /* Enable channel onto application */
4020 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4022 /* XXX: We might want to report this error at some point... */
4032 * Disable an event in a channel and for a specific session.
4034 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4035 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4038 struct lttng_ht_iter iter
, uiter
;
4039 struct lttng_ht_node_str
*ua_chan_node
;
4040 struct ust_app
*app
;
4041 struct ust_app_session
*ua_sess
;
4042 struct ust_app_channel
*ua_chan
;
4043 struct ust_app_event
*ua_event
;
4045 assert(usess
->active
);
4046 DBG("UST app disabling event %s for all apps in channel "
4047 "%s for session id %" PRIu64
,
4048 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4052 /* For all registered applications */
4053 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4054 if (!app
->compatible
) {
4056 * TODO: In time, we should notice the caller of this error by
4057 * telling him that this is a version error.
4061 ua_sess
= lookup_session_by_app(usess
, app
);
4062 if (ua_sess
== NULL
) {
4067 /* Lookup channel in the ust app session */
4068 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4069 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4070 if (ua_chan_node
== NULL
) {
4071 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4072 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4075 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4077 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4078 uevent
->filter
, uevent
->attr
.loglevel
,
4080 if (ua_event
== NULL
) {
4081 DBG2("Event %s not found in channel %s for app pid %d."
4082 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4086 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4088 /* XXX: Report error someday... */
4097 /* The ua_sess lock must be held by the caller. */
4099 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4100 struct ust_app_session
*ua_sess
,
4101 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4102 struct ust_app_channel
**_ua_chan
)
4105 struct ust_app_channel
*ua_chan
= NULL
;
4108 ASSERT_LOCKED(ua_sess
->lock
);
4110 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4111 sizeof(uchan
->name
))) {
4112 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4116 struct ltt_ust_context
*uctx
= NULL
;
4119 * Create channel onto application and synchronize its
4122 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4123 LTTNG_UST_CHAN_PER_CPU
, usess
,
4126 ret
= ust_app_channel_send(app
, usess
,
4133 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4134 ret
= create_ust_app_channel_context(ua_chan
,
4145 * The application's socket is not valid. Either a bad socket
4146 * or a timeout on it. We can't inform the caller that for a
4147 * specific app, the session failed so lets continue here.
4149 ret
= 0; /* Not an error. */
4157 if (ret
== 0 && _ua_chan
) {
4159 * Only return the application's channel on success. Note
4160 * that the channel can still be part of the application's
4161 * channel hashtable on error.
4163 *_ua_chan
= ua_chan
;
4169 * For a specific UST session, create the channel for all registered apps.
4171 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
4172 struct ltt_ust_channel
*uchan
)
4175 struct cds_lfht_iter iter
;
4176 struct ust_app
*app
;
4179 assert(usess
->active
);
4182 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
4183 uchan
->name
, usess
->id
);
4186 /* For every registered applications */
4187 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
, app
, pid_n
.node
) {
4188 struct ust_app_session
*ua_sess
;
4189 int session_was_created
= 0;
4191 if (!app
->compatible
||
4192 !trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4193 goto error_rcu_unlock
;
4197 * Create session on the tracer side and add it to app session HT. Note
4198 * that if session exist, it will simply return a pointer to the ust
4201 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
,
4202 &session_was_created
);
4207 * The application's socket is not valid. Either a bad
4208 * socket or a timeout on it. We can't inform the caller
4209 * that for a specific app, the session failed so lets
4210 * continue here; it is not an error.
4213 goto error_rcu_unlock
;
4216 goto error_rcu_unlock
;
4220 if (ua_sess
->deleted
) {
4223 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, NULL
);
4225 if (session_was_created
) {
4226 destroy_app_session(app
, ua_sess
);
4228 /* Continue to the next application. */
4238 * Enable event for a specific session and channel on the tracer.
4240 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4241 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4244 struct lttng_ht_iter iter
, uiter
;
4245 struct lttng_ht_node_str
*ua_chan_node
;
4246 struct ust_app
*app
;
4247 struct ust_app_session
*ua_sess
;
4248 struct ust_app_channel
*ua_chan
;
4249 struct ust_app_event
*ua_event
;
4251 assert(usess
->active
);
4252 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4253 uevent
->attr
.name
, usess
->id
);
4256 * NOTE: At this point, this function is called only if the session and
4257 * channel passed are already created for all apps. and enabled on the
4263 /* For all registered applications */
4264 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4265 if (!app
->compatible
) {
4267 * TODO: In time, we should notice the caller of this error by
4268 * telling him that this is a version error.
4272 ua_sess
= lookup_session_by_app(usess
, app
);
4274 /* The application has problem or is probably dead. */
4278 pthread_mutex_lock(&ua_sess
->lock
);
4280 if (ua_sess
->deleted
) {
4281 pthread_mutex_unlock(&ua_sess
->lock
);
4285 /* Lookup channel in the ust app session */
4286 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4287 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4289 * It is possible that the channel cannot be found is
4290 * the channel/event creation occurs concurrently with
4291 * an application exit.
4293 if (!ua_chan_node
) {
4294 pthread_mutex_unlock(&ua_sess
->lock
);
4298 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4300 /* Get event node */
4301 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4302 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4303 if (ua_event
== NULL
) {
4304 DBG3("UST app enable event %s not found for app PID %d."
4305 "Skipping app", uevent
->attr
.name
, app
->pid
);
4309 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4311 pthread_mutex_unlock(&ua_sess
->lock
);
4315 pthread_mutex_unlock(&ua_sess
->lock
);
4324 * For a specific existing UST session and UST channel, creates the event for
4325 * all registered apps.
4327 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4328 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4331 struct lttng_ht_iter iter
, uiter
;
4332 struct lttng_ht_node_str
*ua_chan_node
;
4333 struct ust_app
*app
;
4334 struct ust_app_session
*ua_sess
;
4335 struct ust_app_channel
*ua_chan
;
4337 assert(usess
->active
);
4338 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4339 uevent
->attr
.name
, usess
->id
);
4343 /* For all registered applications */
4344 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4345 if (!app
->compatible
) {
4347 * TODO: In time, we should notice the caller of this error by
4348 * telling him that this is a version error.
4352 ua_sess
= lookup_session_by_app(usess
, app
);
4354 /* The application has problem or is probably dead. */
4358 pthread_mutex_lock(&ua_sess
->lock
);
4360 if (ua_sess
->deleted
) {
4361 pthread_mutex_unlock(&ua_sess
->lock
);
4365 /* Lookup channel in the ust app session */
4366 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4367 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4368 /* If the channel is not found, there is a code flow error */
4369 assert(ua_chan_node
);
4371 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4373 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4374 pthread_mutex_unlock(&ua_sess
->lock
);
4376 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4377 /* Possible value at this point: -ENOMEM. If so, we stop! */
4380 DBG2("UST app event %s already exist on app PID %d",
4381 uevent
->attr
.name
, app
->pid
);
4391 * Start tracing for a specific UST session and app.
4393 * Called with UST app session lock held.
4397 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4400 struct ust_app_session
*ua_sess
;
4402 DBG("Starting tracing for ust app pid %d", app
->pid
);
4406 if (!app
->compatible
) {
4410 ua_sess
= lookup_session_by_app(usess
, app
);
4411 if (ua_sess
== NULL
) {
4412 /* The session is in teardown process. Ignore and continue. */
4416 pthread_mutex_lock(&ua_sess
->lock
);
4418 if (ua_sess
->deleted
) {
4419 pthread_mutex_unlock(&ua_sess
->lock
);
4423 /* Upon restart, we skip the setup, already done */
4424 if (ua_sess
->started
) {
4429 * Create the metadata for the application. This returns gracefully if a
4430 * metadata was already set for the session.
4432 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4437 health_code_update();
4440 /* This start the UST tracing */
4441 pthread_mutex_lock(&app
->sock_lock
);
4442 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4443 pthread_mutex_unlock(&app
->sock_lock
);
4445 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4446 ERR("Error starting tracing for app pid: %d (ret: %d)",
4449 DBG("UST app start session failed. Application is dead.");
4451 * This is normal behavior, an application can die during the
4452 * creation process. Don't report an error so the execution can
4453 * continue normally.
4455 pthread_mutex_unlock(&ua_sess
->lock
);
4461 /* Indicate that the session has been started once */
4462 ua_sess
->started
= 1;
4464 pthread_mutex_unlock(&ua_sess
->lock
);
4466 health_code_update();
4468 /* Quiescent wait after starting trace */
4469 pthread_mutex_lock(&app
->sock_lock
);
4470 ret
= ustctl_wait_quiescent(app
->sock
);
4471 pthread_mutex_unlock(&app
->sock_lock
);
4472 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4473 ERR("UST app wait quiescent failed for app pid %d ret %d",
4479 health_code_update();
4483 pthread_mutex_unlock(&ua_sess
->lock
);
4485 health_code_update();
4490 * Stop tracing for a specific UST session and app.
4493 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4496 struct ust_app_session
*ua_sess
;
4497 struct ust_registry_session
*registry
;
4499 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4503 if (!app
->compatible
) {
4504 goto end_no_session
;
4507 ua_sess
= lookup_session_by_app(usess
, app
);
4508 if (ua_sess
== NULL
) {
4509 goto end_no_session
;
4512 pthread_mutex_lock(&ua_sess
->lock
);
4514 if (ua_sess
->deleted
) {
4515 pthread_mutex_unlock(&ua_sess
->lock
);
4516 goto end_no_session
;
4520 * If started = 0, it means that stop trace has been called for a session
4521 * that was never started. It's possible since we can have a fail start
4522 * from either the application manager thread or the command thread. Simply
4523 * indicate that this is a stop error.
4525 if (!ua_sess
->started
) {
4526 goto error_rcu_unlock
;
4529 health_code_update();
4531 /* This inhibits UST tracing */
4532 pthread_mutex_lock(&app
->sock_lock
);
4533 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4534 pthread_mutex_unlock(&app
->sock_lock
);
4536 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4537 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4540 DBG("UST app stop session failed. Application is dead.");
4542 * This is normal behavior, an application can die during the
4543 * creation process. Don't report an error so the execution can
4544 * continue normally.
4548 goto error_rcu_unlock
;
4551 health_code_update();
4553 /* Quiescent wait after stopping trace */
4554 pthread_mutex_lock(&app
->sock_lock
);
4555 ret
= ustctl_wait_quiescent(app
->sock
);
4556 pthread_mutex_unlock(&app
->sock_lock
);
4557 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4558 ERR("UST app wait quiescent failed for app pid %d ret %d",
4562 health_code_update();
4564 registry
= get_session_registry(ua_sess
);
4566 /* The UST app session is held registry shall not be null. */
4569 /* Push metadata for application before freeing the application. */
4570 (void) push_metadata(registry
, ua_sess
->consumer
);
4573 pthread_mutex_unlock(&ua_sess
->lock
);
4576 health_code_update();
4580 pthread_mutex_unlock(&ua_sess
->lock
);
4582 health_code_update();
4587 int ust_app_flush_app_session(struct ust_app
*app
,
4588 struct ust_app_session
*ua_sess
)
4590 int ret
, retval
= 0;
4591 struct lttng_ht_iter iter
;
4592 struct ust_app_channel
*ua_chan
;
4593 struct consumer_socket
*socket
;
4595 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4599 if (!app
->compatible
) {
4600 goto end_not_compatible
;
4603 pthread_mutex_lock(&ua_sess
->lock
);
4605 if (ua_sess
->deleted
) {
4609 health_code_update();
4611 /* Flushing buffers */
4612 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4615 /* Flush buffers and push metadata. */
4616 switch (ua_sess
->buffer_type
) {
4617 case LTTNG_BUFFER_PER_PID
:
4618 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4620 health_code_update();
4621 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4623 ERR("Error flushing consumer channel");
4629 case LTTNG_BUFFER_PER_UID
:
4635 health_code_update();
4638 pthread_mutex_unlock(&ua_sess
->lock
);
4642 health_code_update();
4647 * Flush buffers for all applications for a specific UST session.
4648 * Called with UST session lock held.
4651 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4656 DBG("Flushing session buffers for all ust apps");
4660 /* Flush buffers and push metadata. */
4661 switch (usess
->buffer_type
) {
4662 case LTTNG_BUFFER_PER_UID
:
4664 struct buffer_reg_uid
*reg
;
4665 struct lttng_ht_iter iter
;
4667 /* Flush all per UID buffers associated to that session. */
4668 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4669 struct ust_registry_session
*ust_session_reg
;
4670 struct buffer_reg_channel
*reg_chan
;
4671 struct consumer_socket
*socket
;
4673 /* Get consumer socket to use to push the metadata.*/
4674 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4677 /* Ignore request if no consumer is found for the session. */
4681 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4682 reg_chan
, node
.node
) {
4684 * The following call will print error values so the return
4685 * code is of little importance because whatever happens, we
4686 * have to try them all.
4688 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4691 ust_session_reg
= reg
->registry
->reg
.ust
;
4692 /* Push metadata. */
4693 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4697 case LTTNG_BUFFER_PER_PID
:
4699 struct ust_app_session
*ua_sess
;
4700 struct lttng_ht_iter iter
;
4701 struct ust_app
*app
;
4703 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4704 ua_sess
= lookup_session_by_app(usess
, app
);
4705 if (ua_sess
== NULL
) {
4708 (void) ust_app_flush_app_session(app
, ua_sess
);
4719 health_code_update();
4724 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
4725 struct ust_app_session
*ua_sess
)
4728 struct lttng_ht_iter iter
;
4729 struct ust_app_channel
*ua_chan
;
4730 struct consumer_socket
*socket
;
4732 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
4736 if (!app
->compatible
) {
4737 goto end_not_compatible
;
4740 pthread_mutex_lock(&ua_sess
->lock
);
4742 if (ua_sess
->deleted
) {
4746 health_code_update();
4748 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4751 ERR("Failed to find consumer (%" PRIu32
") socket",
4752 app
->bits_per_long
);
4757 /* Clear quiescent state. */
4758 switch (ua_sess
->buffer_type
) {
4759 case LTTNG_BUFFER_PER_PID
:
4760 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
4761 ua_chan
, node
.node
) {
4762 health_code_update();
4763 ret
= consumer_clear_quiescent_channel(socket
,
4766 ERR("Error clearing quiescent state for consumer channel");
4772 case LTTNG_BUFFER_PER_UID
:
4779 health_code_update();
4782 pthread_mutex_unlock(&ua_sess
->lock
);
4786 health_code_update();
4791 * Clear quiescent state in each stream for all applications for a
4792 * specific UST session.
4793 * Called with UST session lock held.
4796 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
4801 DBG("Clearing stream quiescent state for all ust apps");
4805 switch (usess
->buffer_type
) {
4806 case LTTNG_BUFFER_PER_UID
:
4808 struct lttng_ht_iter iter
;
4809 struct buffer_reg_uid
*reg
;
4812 * Clear quiescent for all per UID buffers associated to
4815 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4816 struct consumer_socket
*socket
;
4817 struct buffer_reg_channel
*reg_chan
;
4819 /* Get associated consumer socket.*/
4820 socket
= consumer_find_socket_by_bitness(
4821 reg
->bits_per_long
, usess
->consumer
);
4824 * Ignore request if no consumer is found for
4830 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
4831 &iter
.iter
, reg_chan
, node
.node
) {
4833 * The following call will print error values so
4834 * the return code is of little importance
4835 * because whatever happens, we have to try them
4838 (void) consumer_clear_quiescent_channel(socket
,
4839 reg_chan
->consumer_key
);
4844 case LTTNG_BUFFER_PER_PID
:
4846 struct ust_app_session
*ua_sess
;
4847 struct lttng_ht_iter iter
;
4848 struct ust_app
*app
;
4850 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
4852 ua_sess
= lookup_session_by_app(usess
, app
);
4853 if (ua_sess
== NULL
) {
4856 (void) ust_app_clear_quiescent_app_session(app
,
4868 health_code_update();
4873 * Destroy a specific UST session in apps.
4875 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4878 struct ust_app_session
*ua_sess
;
4879 struct lttng_ht_iter iter
;
4880 struct lttng_ht_node_u64
*node
;
4882 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4886 if (!app
->compatible
) {
4890 __lookup_session_by_app(usess
, app
, &iter
);
4891 node
= lttng_ht_iter_get_node_u64(&iter
);
4893 /* Session is being or is deleted. */
4896 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4898 health_code_update();
4899 destroy_app_session(app
, ua_sess
);
4901 health_code_update();
4903 /* Quiescent wait after stopping trace */
4904 pthread_mutex_lock(&app
->sock_lock
);
4905 ret
= ustctl_wait_quiescent(app
->sock
);
4906 pthread_mutex_unlock(&app
->sock_lock
);
4907 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4908 ERR("UST app wait quiescent failed for app pid %d ret %d",
4913 health_code_update();
4918 * Start tracing for the UST session.
4920 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4922 struct lttng_ht_iter iter
;
4923 struct ust_app
*app
;
4925 DBG("Starting all UST traces");
4928 * Even though the start trace might fail, flag this session active so
4929 * other application coming in are started by default.
4936 * In a start-stop-start use-case, we need to clear the quiescent state
4937 * of each channel set by the prior stop command, thus ensuring that a
4938 * following stop or destroy is sure to grab a timestamp_end near those
4939 * operations, even if the packet is empty.
4941 (void) ust_app_clear_quiescent_session(usess
);
4943 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4944 ust_app_global_update(usess
, app
);
4953 * Start tracing for the UST session.
4954 * Called with UST session lock held.
4956 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4959 struct lttng_ht_iter iter
;
4960 struct ust_app
*app
;
4962 DBG("Stopping all UST traces");
4965 * Even though the stop trace might fail, flag this session inactive so
4966 * other application coming in are not started by default.
4972 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4973 ret
= ust_app_stop_trace(usess
, app
);
4975 /* Continue to next apps even on error */
4980 (void) ust_app_flush_session(usess
);
4988 * Destroy app UST session.
4990 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4993 struct lttng_ht_iter iter
;
4994 struct ust_app
*app
;
4996 DBG("Destroy all UST traces");
5000 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5001 ret
= destroy_trace(usess
, app
);
5003 /* Continue to next apps even on error */
5013 /* The ua_sess lock must be held by the caller. */
5015 int find_or_create_ust_app_channel(
5016 struct ltt_ust_session
*usess
,
5017 struct ust_app_session
*ua_sess
,
5018 struct ust_app
*app
,
5019 struct ltt_ust_channel
*uchan
,
5020 struct ust_app_channel
**ua_chan
)
5023 struct lttng_ht_iter iter
;
5024 struct lttng_ht_node_str
*ua_chan_node
;
5026 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5027 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5029 *ua_chan
= caa_container_of(ua_chan_node
,
5030 struct ust_app_channel
, node
);
5034 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5043 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5044 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5045 struct ust_app
*app
)
5048 struct ust_app_event
*ua_event
= NULL
;
5050 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5051 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5053 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5058 if (ua_event
->enabled
!= uevent
->enabled
) {
5059 ret
= uevent
->enabled
?
5060 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5061 disable_ust_app_event(ua_sess
, ua_event
, app
);
5070 * The caller must ensure that the application is compatible and is tracked
5071 * by the PID tracker.
5074 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5075 struct ust_app
*app
)
5078 struct cds_lfht_iter uchan_iter
;
5079 struct ltt_ust_channel
*uchan
;
5080 struct ust_app_session
*ua_sess
= NULL
;
5083 * The application's configuration should only be synchronized for
5086 assert(usess
->active
);
5088 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5090 /* Tracer is probably gone or ENOMEM. */
5095 pthread_mutex_lock(&ua_sess
->lock
);
5096 if (ua_sess
->deleted
) {
5097 pthread_mutex_unlock(&ua_sess
->lock
);
5102 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5104 struct ust_app_channel
*ua_chan
;
5105 struct cds_lfht_iter uevent_iter
;
5106 struct ltt_ust_event
*uevent
;
5109 * Search for a matching ust_app_channel. If none is found,
5110 * create it. Creating the channel will cause the ua_chan
5111 * structure to be allocated, the channel buffers to be
5112 * allocated (if necessary) and sent to the application, and
5113 * all enabled contexts will be added to the channel.
5115 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5116 app
, uchan
, &ua_chan
);
5118 /* Tracer is probably gone or ENOMEM. */
5123 /* ua_chan will be NULL for the metadata channel */
5127 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5129 ret
= ust_app_channel_synchronize_event(ua_chan
,
5130 uevent
, ua_sess
, app
);
5136 if (ua_chan
->enabled
!= uchan
->enabled
) {
5137 ret
= uchan
->enabled
?
5138 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5139 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5148 pthread_mutex_unlock(&ua_sess
->lock
);
5149 /* Everything went well at this point. */
5154 pthread_mutex_unlock(&ua_sess
->lock
);
5157 destroy_app_session(app
, ua_sess
);
5163 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5165 struct ust_app_session
*ua_sess
;
5167 ua_sess
= lookup_session_by_app(usess
, app
);
5168 if (ua_sess
== NULL
) {
5171 destroy_app_session(app
, ua_sess
);
5175 * Add channels/events from UST global domain to registered apps at sock.
5177 * Called with session lock held.
5178 * Called with RCU read-side lock held.
5180 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5183 assert(usess
->active
);
5185 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5186 app
->sock
, usess
->id
);
5188 if (!app
->compatible
) {
5191 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
5193 * Synchronize the application's internal tracing configuration
5194 * and start tracing.
5196 ust_app_synchronize(usess
, app
);
5197 ust_app_start_trace(usess
, app
);
5199 ust_app_global_destroy(usess
, app
);
5204 * Called with session lock held.
5206 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5208 struct lttng_ht_iter iter
;
5209 struct ust_app
*app
;
5212 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5213 ust_app_global_update(usess
, app
);
5219 * Add context to a specific channel for global UST domain.
5221 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5222 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5225 struct lttng_ht_node_str
*ua_chan_node
;
5226 struct lttng_ht_iter iter
, uiter
;
5227 struct ust_app_channel
*ua_chan
= NULL
;
5228 struct ust_app_session
*ua_sess
;
5229 struct ust_app
*app
;
5231 assert(usess
->active
);
5234 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5235 if (!app
->compatible
) {
5237 * TODO: In time, we should notice the caller of this error by
5238 * telling him that this is a version error.
5242 ua_sess
= lookup_session_by_app(usess
, app
);
5243 if (ua_sess
== NULL
) {
5247 pthread_mutex_lock(&ua_sess
->lock
);
5249 if (ua_sess
->deleted
) {
5250 pthread_mutex_unlock(&ua_sess
->lock
);
5254 /* Lookup channel in the ust app session */
5255 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5256 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5257 if (ua_chan_node
== NULL
) {
5260 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5262 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5267 pthread_mutex_unlock(&ua_sess
->lock
);
5275 * Receive registration and populate the given msg structure.
5277 * On success return 0 else a negative value returned by the ustctl call.
5279 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5282 uint32_t pid
, ppid
, uid
, gid
;
5286 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5287 &pid
, &ppid
, &uid
, &gid
,
5288 &msg
->bits_per_long
,
5289 &msg
->uint8_t_alignment
,
5290 &msg
->uint16_t_alignment
,
5291 &msg
->uint32_t_alignment
,
5292 &msg
->uint64_t_alignment
,
5293 &msg
->long_alignment
,
5300 case LTTNG_UST_ERR_EXITING
:
5301 DBG3("UST app recv reg message failed. Application died");
5303 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5304 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5305 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5306 LTTNG_UST_ABI_MINOR_VERSION
);
5309 ERR("UST app recv reg message failed with ret %d", ret
);
5314 msg
->pid
= (pid_t
) pid
;
5315 msg
->ppid
= (pid_t
) ppid
;
5316 msg
->uid
= (uid_t
) uid
;
5317 msg
->gid
= (gid_t
) gid
;
5324 * Return a ust app session object using the application object and the
5325 * session object descriptor has a key. If not found, NULL is returned.
5326 * A RCU read side lock MUST be acquired when calling this function.
5328 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5331 struct lttng_ht_node_ulong
*node
;
5332 struct lttng_ht_iter iter
;
5333 struct ust_app_session
*ua_sess
= NULL
;
5337 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5338 node
= lttng_ht_iter_get_node_ulong(&iter
);
5340 DBG2("UST app session find by objd %d not found", objd
);
5344 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5351 * Return a ust app channel object using the application object and the channel
5352 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5353 * lock MUST be acquired before calling this function.
5355 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5358 struct lttng_ht_node_ulong
*node
;
5359 struct lttng_ht_iter iter
;
5360 struct ust_app_channel
*ua_chan
= NULL
;
5364 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5365 node
= lttng_ht_iter_get_node_ulong(&iter
);
5367 DBG2("UST app channel find by objd %d not found", objd
);
5371 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5378 * Reply to a register channel notification from an application on the notify
5379 * socket. The channel metadata is also created.
5381 * The session UST registry lock is acquired in this function.
5383 * On success 0 is returned else a negative value.
5385 static int reply_ust_register_channel(int sock
, int cobjd
,
5386 size_t nr_fields
, struct ustctl_field
*fields
)
5388 int ret
, ret_code
= 0;
5390 uint64_t chan_reg_key
;
5391 enum ustctl_channel_header type
;
5392 struct ust_app
*app
;
5393 struct ust_app_channel
*ua_chan
;
5394 struct ust_app_session
*ua_sess
;
5395 struct ust_registry_session
*registry
;
5396 struct ust_registry_channel
*chan_reg
;
5400 /* Lookup application. If not found, there is a code flow error. */
5401 app
= find_app_by_notify_sock(sock
);
5403 DBG("Application socket %d is being torn down. Abort event notify",
5406 goto error_rcu_unlock
;
5409 /* Lookup channel by UST object descriptor. */
5410 ua_chan
= find_channel_by_objd(app
, cobjd
);
5412 DBG("Application channel is being torn down. Abort event notify");
5414 goto error_rcu_unlock
;
5417 assert(ua_chan
->session
);
5418 ua_sess
= ua_chan
->session
;
5420 /* Get right session registry depending on the session buffer type. */
5421 registry
= get_session_registry(ua_sess
);
5423 DBG("Application session is being torn down. Abort event notify");
5425 goto error_rcu_unlock
;
5428 /* Depending on the buffer type, a different channel key is used. */
5429 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5430 chan_reg_key
= ua_chan
->tracing_channel_id
;
5432 chan_reg_key
= ua_chan
->key
;
5435 pthread_mutex_lock(®istry
->lock
);
5437 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5440 if (!chan_reg
->register_done
) {
5442 * TODO: eventually use the registry event count for
5443 * this channel to better guess header type for per-pid
5446 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5447 chan_reg
->nr_ctx_fields
= nr_fields
;
5448 chan_reg
->ctx_fields
= fields
;
5450 chan_reg
->header_type
= type
;
5452 /* Get current already assigned values. */
5453 type
= chan_reg
->header_type
;
5455 /* Channel id is set during the object creation. */
5456 chan_id
= chan_reg
->chan_id
;
5458 /* Append to metadata */
5459 if (!chan_reg
->metadata_dumped
) {
5460 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5462 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5468 DBG3("UST app replying to register channel key %" PRIu64
5469 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5472 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5474 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5475 ERR("UST app reply channel failed with ret %d", ret
);
5477 DBG3("UST app reply channel failed. Application died");
5482 /* This channel registry registration is completed. */
5483 chan_reg
->register_done
= 1;
5486 pthread_mutex_unlock(®istry
->lock
);
5494 * Add event to the UST channel registry. When the event is added to the
5495 * registry, the metadata is also created. Once done, this replies to the
5496 * application with the appropriate error code.
5498 * The session UST registry lock is acquired in the function.
5500 * On success 0 is returned else a negative value.
5502 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5503 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5504 int loglevel_value
, char *model_emf_uri
)
5507 uint32_t event_id
= 0;
5508 uint64_t chan_reg_key
;
5509 struct ust_app
*app
;
5510 struct ust_app_channel
*ua_chan
;
5511 struct ust_app_session
*ua_sess
;
5512 struct ust_registry_session
*registry
;
5516 /* Lookup application. If not found, there is a code flow error. */
5517 app
= find_app_by_notify_sock(sock
);
5519 DBG("Application socket %d is being torn down. Abort event notify",
5522 goto error_rcu_unlock
;
5525 /* Lookup channel by UST object descriptor. */
5526 ua_chan
= find_channel_by_objd(app
, cobjd
);
5528 DBG("Application channel is being torn down. Abort event notify");
5530 goto error_rcu_unlock
;
5533 assert(ua_chan
->session
);
5534 ua_sess
= ua_chan
->session
;
5536 registry
= get_session_registry(ua_sess
);
5538 DBG("Application session is being torn down. Abort event notify");
5540 goto error_rcu_unlock
;
5543 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5544 chan_reg_key
= ua_chan
->tracing_channel_id
;
5546 chan_reg_key
= ua_chan
->key
;
5549 pthread_mutex_lock(®istry
->lock
);
5552 * From this point on, this call acquires the ownership of the sig, fields
5553 * and model_emf_uri meaning any free are done inside it if needed. These
5554 * three variables MUST NOT be read/write after this.
5556 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5557 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5558 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5562 model_emf_uri
= NULL
;
5565 * The return value is returned to ustctl so in case of an error, the
5566 * application can be notified. In case of an error, it's important not to
5567 * return a negative error or else the application will get closed.
5569 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5571 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5572 ERR("UST app reply event failed with ret %d", ret
);
5574 DBG3("UST app reply event failed. Application died");
5577 * No need to wipe the create event since the application socket will
5578 * get close on error hence cleaning up everything by itself.
5583 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5587 pthread_mutex_unlock(®istry
->lock
);
5592 free(model_emf_uri
);
5597 * Add enum to the UST session registry. Once done, this replies to the
5598 * application with the appropriate error code.
5600 * The session UST registry lock is acquired within this function.
5602 * On success 0 is returned else a negative value.
5604 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
5605 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
5607 int ret
= 0, ret_code
;
5608 struct ust_app
*app
;
5609 struct ust_app_session
*ua_sess
;
5610 struct ust_registry_session
*registry
;
5611 uint64_t enum_id
= -1ULL;
5615 /* Lookup application. If not found, there is a code flow error. */
5616 app
= find_app_by_notify_sock(sock
);
5618 /* Return an error since this is not an error */
5619 DBG("Application socket %d is being torn down. Aborting enum registration",
5622 goto error_rcu_unlock
;
5625 /* Lookup session by UST object descriptor. */
5626 ua_sess
= find_session_by_objd(app
, sobjd
);
5628 /* Return an error since this is not an error */
5629 DBG("Application session is being torn down (session not found). Aborting enum registration.");
5631 goto error_rcu_unlock
;
5634 registry
= get_session_registry(ua_sess
);
5636 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
5638 goto error_rcu_unlock
;
5641 pthread_mutex_lock(®istry
->lock
);
5644 * From this point on, the callee acquires the ownership of
5645 * entries. The variable entries MUST NOT be read/written after
5648 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
5649 entries
, nr_entries
, &enum_id
);
5653 * The return value is returned to ustctl so in case of an error, the
5654 * application can be notified. In case of an error, it's important not to
5655 * return a negative error or else the application will get closed.
5657 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
5659 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5660 ERR("UST app reply enum failed with ret %d", ret
);
5662 DBG3("UST app reply enum failed. Application died");
5665 * No need to wipe the create enum since the application socket will
5666 * get close on error hence cleaning up everything by itself.
5671 DBG3("UST registry enum %s added successfully or already found", name
);
5674 pthread_mutex_unlock(®istry
->lock
);
5681 * Handle application notification through the given notify socket.
5683 * Return 0 on success or else a negative value.
5685 int ust_app_recv_notify(int sock
)
5688 enum ustctl_notify_cmd cmd
;
5690 DBG3("UST app receiving notify from sock %d", sock
);
5692 ret
= ustctl_recv_notify(sock
, &cmd
);
5694 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5695 ERR("UST app recv notify failed with ret %d", ret
);
5697 DBG3("UST app recv notify failed. Application died");
5703 case USTCTL_NOTIFY_CMD_EVENT
:
5705 int sobjd
, cobjd
, loglevel_value
;
5706 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5708 struct ustctl_field
*fields
;
5710 DBG2("UST app ustctl register event received");
5712 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5713 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5716 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5717 ERR("UST app recv event failed with ret %d", ret
);
5719 DBG3("UST app recv event failed. Application died");
5725 * Add event to the UST registry coming from the notify socket. This
5726 * call will free if needed the sig, fields and model_emf_uri. This
5727 * code path loses the ownsership of these variables and transfer them
5728 * to the this function.
5730 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5731 fields
, loglevel_value
, model_emf_uri
);
5738 case USTCTL_NOTIFY_CMD_CHANNEL
:
5742 struct ustctl_field
*fields
;
5744 DBG2("UST app ustctl register channel received");
5746 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5749 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5750 ERR("UST app recv channel failed with ret %d", ret
);
5752 DBG3("UST app recv channel failed. Application died");
5758 * The fields ownership are transfered to this function call meaning
5759 * that if needed it will be freed. After this, it's invalid to access
5760 * fields or clean it up.
5762 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
5770 case USTCTL_NOTIFY_CMD_ENUM
:
5773 char name
[LTTNG_UST_SYM_NAME_LEN
];
5775 struct ustctl_enum_entry
*entries
;
5777 DBG2("UST app ustctl register enum received");
5779 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
5780 &entries
, &nr_entries
);
5782 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5783 ERR("UST app recv enum failed with ret %d", ret
);
5785 DBG3("UST app recv enum failed. Application died");
5790 /* Callee assumes ownership of entries */
5791 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
5792 entries
, nr_entries
);
5800 /* Should NEVER happen. */
5809 * Once the notify socket hangs up, this is called. First, it tries to find the
5810 * corresponding application. On failure, the call_rcu to close the socket is
5811 * executed. If an application is found, it tries to delete it from the notify
5812 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5814 * Note that an object needs to be allocated here so on ENOMEM failure, the
5815 * call RCU is not done but the rest of the cleanup is.
5817 void ust_app_notify_sock_unregister(int sock
)
5820 struct lttng_ht_iter iter
;
5821 struct ust_app
*app
;
5822 struct ust_app_notify_sock_obj
*obj
;
5828 obj
= zmalloc(sizeof(*obj
));
5831 * An ENOMEM is kind of uncool. If this strikes we continue the
5832 * procedure but the call_rcu will not be called. In this case, we
5833 * accept the fd leak rather than possibly creating an unsynchronized
5834 * state between threads.
5836 * TODO: The notify object should be created once the notify socket is
5837 * registered and stored independantely from the ust app object. The
5838 * tricky part is to synchronize the teardown of the application and
5839 * this notify object. Let's keep that in mind so we can avoid this
5840 * kind of shenanigans with ENOMEM in the teardown path.
5847 DBG("UST app notify socket unregister %d", sock
);
5850 * Lookup application by notify socket. If this fails, this means that the
5851 * hash table delete has already been done by the application
5852 * unregistration process so we can safely close the notify socket in a
5855 app
= find_app_by_notify_sock(sock
);
5860 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5863 * Whatever happens here either we fail or succeed, in both cases we have
5864 * to close the socket after a grace period to continue to the call RCU
5865 * here. If the deletion is successful, the application is not visible
5866 * anymore by other threads and is it fails it means that it was already
5867 * deleted from the hash table so either way we just have to close the
5870 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5876 * Close socket after a grace period to avoid for the socket to be reused
5877 * before the application object is freed creating potential race between
5878 * threads trying to add unique in the global hash table.
5881 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5886 * Destroy a ust app data structure and free its memory.
5888 void ust_app_destroy(struct ust_app
*app
)
5894 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5898 * Take a snapshot for a given UST session. The snapshot is sent to the given
5901 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5903 enum lttng_error_code
ust_app_snapshot_record(
5904 const struct ltt_ust_session
*usess
,
5905 const struct consumer_output
*output
, int wait
,
5906 uint64_t nb_packets_per_stream
)
5909 enum lttng_error_code status
= LTTNG_OK
;
5910 struct lttng_ht_iter iter
;
5911 struct ust_app
*app
;
5912 char *trace_path
= NULL
;
5919 switch (usess
->buffer_type
) {
5920 case LTTNG_BUFFER_PER_UID
:
5922 struct buffer_reg_uid
*reg
;
5924 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5925 struct buffer_reg_channel
*reg_chan
;
5926 struct consumer_socket
*socket
;
5927 char pathname
[PATH_MAX
];
5929 if (!reg
->registry
->reg
.ust
->metadata_key
) {
5930 /* Skip since no metadata is present */
5934 /* Get consumer socket to use to push the metadata.*/
5935 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5938 status
= LTTNG_ERR_INVALID
;
5942 memset(pathname
, 0, sizeof(pathname
));
5944 * DEFAULT_UST_TRACE_UID_PATH already contains a path
5947 ret
= snprintf(pathname
, sizeof(pathname
),
5948 DEFAULT_UST_TRACE_DIR DEFAULT_UST_TRACE_UID_PATH
,
5949 reg
->uid
, reg
->bits_per_long
);
5951 PERROR("snprintf snapshot path");
5952 status
= LTTNG_ERR_INVALID
;
5955 /* Free path allowed on previous iteration. */
5957 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
);
5959 status
= LTTNG_ERR_INVALID
;
5962 /* Add the UST default trace dir to path. */
5963 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5964 reg_chan
, node
.node
) {
5965 status
= consumer_snapshot_channel(socket
,
5966 reg_chan
->consumer_key
,
5967 output
, 0, usess
->uid
,
5968 usess
->gid
, trace_path
, wait
,
5969 nb_packets_per_stream
);
5970 if (status
!= LTTNG_OK
) {
5974 status
= consumer_snapshot_channel(socket
,
5975 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5976 usess
->uid
, usess
->gid
, trace_path
, wait
, 0);
5977 if (status
!= LTTNG_OK
) {
5983 case LTTNG_BUFFER_PER_PID
:
5985 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5986 struct consumer_socket
*socket
;
5987 struct lttng_ht_iter chan_iter
;
5988 struct ust_app_channel
*ua_chan
;
5989 struct ust_app_session
*ua_sess
;
5990 struct ust_registry_session
*registry
;
5991 char pathname
[PATH_MAX
];
5993 ua_sess
= lookup_session_by_app(usess
, app
);
5995 /* Session not associated with this app. */
5999 /* Get the right consumer socket for the application. */
6000 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6003 status
= LTTNG_ERR_INVALID
;
6007 /* Add the UST default trace dir to path. */
6008 memset(pathname
, 0, sizeof(pathname
));
6009 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"%s",
6012 status
= LTTNG_ERR_INVALID
;
6013 PERROR("snprintf snapshot path");
6016 /* Free path allowed on previous iteration. */
6018 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
);
6020 status
= LTTNG_ERR_INVALID
;
6023 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6024 ua_chan
, node
.node
) {
6025 status
= consumer_snapshot_channel(socket
,
6026 ua_chan
->key
, output
, 0,
6027 ua_sess
->effective_credentials
6029 ua_sess
->effective_credentials
6032 nb_packets_per_stream
);
6036 case LTTNG_ERR_CHAN_NOT_FOUND
:
6043 registry
= get_session_registry(ua_sess
);
6045 DBG("Application session is being torn down. Skip application.");
6048 status
= consumer_snapshot_channel(socket
,
6049 registry
->metadata_key
, output
, 1,
6050 ua_sess
->effective_credentials
.uid
,
6051 ua_sess
->effective_credentials
.gid
,
6052 trace_path
, wait
, 0);
6056 case LTTNG_ERR_CHAN_NOT_FOUND
:
6076 * Return the size taken by one more packet per stream.
6078 uint64_t ust_app_get_size_one_more_packet_per_stream(
6079 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6081 uint64_t tot_size
= 0;
6082 struct ust_app
*app
;
6083 struct lttng_ht_iter iter
;
6087 switch (usess
->buffer_type
) {
6088 case LTTNG_BUFFER_PER_UID
:
6090 struct buffer_reg_uid
*reg
;
6092 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6093 struct buffer_reg_channel
*reg_chan
;
6096 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6097 reg_chan
, node
.node
) {
6098 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6100 * Don't take channel into account if we
6101 * already grab all its packets.
6105 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6111 case LTTNG_BUFFER_PER_PID
:
6114 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6115 struct ust_app_channel
*ua_chan
;
6116 struct ust_app_session
*ua_sess
;
6117 struct lttng_ht_iter chan_iter
;
6119 ua_sess
= lookup_session_by_app(usess
, app
);
6121 /* Session not associated with this app. */
6125 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6126 ua_chan
, node
.node
) {
6127 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6129 * Don't take channel into account if we
6130 * already grab all its packets.
6134 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6148 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6149 struct cds_list_head
*buffer_reg_uid_list
,
6150 struct consumer_output
*consumer
, uint64_t uchan_id
,
6151 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6154 uint64_t consumer_chan_key
;
6159 ret
= buffer_reg_uid_consumer_channel_key(
6160 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6168 ret
= consumer_get_lost_packets(ust_session_id
,
6169 consumer_chan_key
, consumer
, lost
);
6171 ret
= consumer_get_discarded_events(ust_session_id
,
6172 consumer_chan_key
, consumer
, discarded
);
6179 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6180 struct ltt_ust_channel
*uchan
,
6181 struct consumer_output
*consumer
, int overwrite
,
6182 uint64_t *discarded
, uint64_t *lost
)
6185 struct lttng_ht_iter iter
;
6186 struct lttng_ht_node_str
*ua_chan_node
;
6187 struct ust_app
*app
;
6188 struct ust_app_session
*ua_sess
;
6189 struct ust_app_channel
*ua_chan
;
6196 * Iterate over every registered applications. Sum counters for
6197 * all applications containing requested session and channel.
6199 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6200 struct lttng_ht_iter uiter
;
6202 ua_sess
= lookup_session_by_app(usess
, app
);
6203 if (ua_sess
== NULL
) {
6208 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6209 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6210 /* If the session is found for the app, the channel must be there */
6211 assert(ua_chan_node
);
6213 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6218 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6225 uint64_t _discarded
;
6227 ret
= consumer_get_discarded_events(usess
->id
,
6228 ua_chan
->key
, consumer
, &_discarded
);
6232 (*discarded
) += _discarded
;
6241 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6242 struct ust_app
*app
)
6245 struct ust_app_session
*ua_sess
;
6247 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6251 ua_sess
= lookup_session_by_app(usess
, app
);
6252 if (ua_sess
== NULL
) {
6253 /* The session is in teardown process. Ignore and continue. */
6257 pthread_mutex_lock(&ua_sess
->lock
);
6259 if (ua_sess
->deleted
) {
6263 pthread_mutex_lock(&app
->sock_lock
);
6264 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6265 pthread_mutex_unlock(&app
->sock_lock
);
6268 pthread_mutex_unlock(&ua_sess
->lock
);
6272 health_code_update();
6277 * Regenerate the statedump for each app in the session.
6279 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6282 struct lttng_ht_iter iter
;
6283 struct ust_app
*app
;
6285 DBG("Regenerating the metadata for all UST apps");
6289 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6290 if (!app
->compatible
) {
6294 ret
= ust_app_regenerate_statedump(usess
, app
);
6296 /* Continue to the next app even on error */
6307 * Rotate all the channels of a session.
6309 * Return LTTNG_OK on success or else an LTTng error code.
6311 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
6314 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6315 struct lttng_ht_iter iter
;
6316 struct ust_app
*app
;
6317 struct ltt_ust_session
*usess
= session
->ust_session
;
6323 switch (usess
->buffer_type
) {
6324 case LTTNG_BUFFER_PER_UID
:
6326 struct buffer_reg_uid
*reg
;
6328 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6329 struct buffer_reg_channel
*reg_chan
;
6330 struct consumer_socket
*socket
;
6332 /* Get consumer socket to use to push the metadata.*/
6333 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6336 cmd_ret
= LTTNG_ERR_INVALID
;
6340 /* Rotate the data channels. */
6341 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6342 reg_chan
, node
.node
) {
6343 ret
= consumer_rotate_channel(socket
,
6344 reg_chan
->consumer_key
,
6345 usess
->uid
, usess
->gid
,
6347 /* is_metadata_channel */ false);
6349 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6354 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6356 ret
= consumer_rotate_channel(socket
,
6357 reg
->registry
->reg
.ust
->metadata_key
,
6358 usess
->uid
, usess
->gid
,
6360 /* is_metadata_channel */ true);
6362 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6368 case LTTNG_BUFFER_PER_PID
:
6370 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6371 struct consumer_socket
*socket
;
6372 struct lttng_ht_iter chan_iter
;
6373 struct ust_app_channel
*ua_chan
;
6374 struct ust_app_session
*ua_sess
;
6375 struct ust_registry_session
*registry
;
6377 ua_sess
= lookup_session_by_app(usess
, app
);
6379 /* Session not associated with this app. */
6383 /* Get the right consumer socket for the application. */
6384 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6387 cmd_ret
= LTTNG_ERR_INVALID
;
6391 registry
= get_session_registry(ua_sess
);
6393 DBG("Application session is being torn down. Skip application.");
6397 /* Rotate the data channels. */
6398 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6399 ua_chan
, node
.node
) {
6400 ret
= consumer_rotate_channel(socket
,
6402 ua_sess
->effective_credentials
6404 ua_sess
->effective_credentials
6407 /* is_metadata_channel */ false);
6409 /* Per-PID buffer and application going away. */
6410 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6412 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6417 /* Rotate the metadata channel. */
6418 (void) push_metadata(registry
, usess
->consumer
);
6419 ret
= consumer_rotate_channel(socket
,
6420 registry
->metadata_key
,
6421 ua_sess
->effective_credentials
.uid
,
6422 ua_sess
->effective_credentials
.gid
,
6424 /* is_metadata_channel */ true);
6426 /* Per-PID buffer and application going away. */
6427 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6429 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6447 enum lttng_error_code
ust_app_create_channel_subdirectories(
6448 const struct ltt_ust_session
*usess
)
6450 enum lttng_error_code ret
= LTTNG_OK
;
6451 struct lttng_ht_iter iter
;
6452 enum lttng_trace_chunk_status chunk_status
;
6453 char *pathname_index
;
6456 assert(usess
->current_trace_chunk
);
6459 switch (usess
->buffer_type
) {
6460 case LTTNG_BUFFER_PER_UID
:
6462 struct buffer_reg_uid
*reg
;
6464 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6465 fmt_ret
= asprintf(&pathname_index
,
6466 DEFAULT_UST_TRACE_DIR DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
6467 reg
->uid
, reg
->bits_per_long
);
6469 ERR("Failed to format channel index directory");
6470 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6475 * Create the index subdirectory which will take care
6476 * of implicitly creating the channel's path.
6478 chunk_status
= lttng_trace_chunk_create_subdirectory(
6479 usess
->current_trace_chunk
,
6481 free(pathname_index
);
6482 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6483 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6489 case LTTNG_BUFFER_PER_PID
:
6491 struct ust_app
*app
;
6494 * Create the toplevel ust/ directory in case no apps are running.
6496 chunk_status
= lttng_trace_chunk_create_subdirectory(
6497 usess
->current_trace_chunk
,
6498 DEFAULT_UST_TRACE_DIR
);
6499 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6500 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6504 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
6506 struct ust_app_session
*ua_sess
;
6507 struct ust_registry_session
*registry
;
6509 ua_sess
= lookup_session_by_app(usess
, app
);
6511 /* Session not associated with this app. */
6515 registry
= get_session_registry(ua_sess
);
6517 DBG("Application session is being torn down. Skip application.");
6521 fmt_ret
= asprintf(&pathname_index
,
6522 DEFAULT_UST_TRACE_DIR
"%s/" DEFAULT_INDEX_DIR
,
6525 ERR("Failed to format channel index directory");
6526 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6530 * Create the index subdirectory which will take care
6531 * of implicitly creating the channel's path.
6533 chunk_status
= lttng_trace_chunk_create_subdirectory(
6534 usess
->current_trace_chunk
,
6536 free(pathname_index
);
6537 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6538 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;