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>
30 #include <lttng/ust-error.h>
33 #include <common/common.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
36 #include "buffer-registry.h"
38 #include "health-sessiond.h"
40 #include "ust-consumer.h"
46 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
48 /* Next available channel key. Access under next_channel_key_lock. */
49 static uint64_t _next_channel_key
;
50 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
52 /* Next available session ID. Access under next_session_id_lock. */
53 static uint64_t _next_session_id
;
54 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
57 * Return the incremented value of next_channel_key.
59 static uint64_t get_next_channel_key(void)
63 pthread_mutex_lock(&next_channel_key_lock
);
64 ret
= ++_next_channel_key
;
65 pthread_mutex_unlock(&next_channel_key_lock
);
70 * Return the atomically incremented value of next_session_id.
72 static uint64_t get_next_session_id(void)
76 pthread_mutex_lock(&next_session_id_lock
);
77 ret
= ++_next_session_id
;
78 pthread_mutex_unlock(&next_session_id_lock
);
82 static void copy_channel_attr_to_ustctl(
83 struct ustctl_consumer_channel_attr
*attr
,
84 struct lttng_ust_channel_attr
*uattr
)
86 /* Copy event attributes since the layout is different. */
87 attr
->subbuf_size
= uattr
->subbuf_size
;
88 attr
->num_subbuf
= uattr
->num_subbuf
;
89 attr
->overwrite
= uattr
->overwrite
;
90 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
91 attr
->read_timer_interval
= uattr
->read_timer_interval
;
92 attr
->output
= uattr
->output
;
96 * Match function for the hash table lookup.
98 * It matches an ust app event based on three attributes which are the event
99 * name, the filter bytecode and the loglevel.
101 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
103 struct ust_app_event
*event
;
104 const struct ust_app_ht_key
*key
;
105 int ev_loglevel_value
;
110 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
112 ev_loglevel_value
= event
->attr
.loglevel
;
114 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
117 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
121 /* Event loglevel. */
122 if (ev_loglevel_value
!= key
->loglevel_type
) {
123 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
124 && key
->loglevel_type
== 0 &&
125 ev_loglevel_value
== -1) {
127 * Match is accepted. This is because on event creation, the
128 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
129 * -1 are accepted for this loglevel type since 0 is the one set by
130 * the API when receiving an enable event.
137 /* One of the filters is NULL, fail. */
138 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
142 if (key
->filter
&& event
->filter
) {
143 /* Both filters exists, check length followed by the bytecode. */
144 if (event
->filter
->len
!= key
->filter
->len
||
145 memcmp(event
->filter
->data
, key
->filter
->data
,
146 event
->filter
->len
) != 0) {
151 /* One of the exclusions is NULL, fail. */
152 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
156 if (key
->exclusion
&& event
->exclusion
) {
157 /* Both exclusions exists, check count followed by the names. */
158 if (event
->exclusion
->count
!= key
->exclusion
->count
||
159 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
160 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
174 * Unique add of an ust app event in the given ht. This uses the custom
175 * ht_match_ust_app_event match function and the event name as hash.
177 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
178 struct ust_app_event
*event
)
180 struct cds_lfht_node
*node_ptr
;
181 struct ust_app_ht_key key
;
185 assert(ua_chan
->events
);
188 ht
= ua_chan
->events
;
189 key
.name
= event
->attr
.name
;
190 key
.filter
= event
->filter
;
191 key
.loglevel_type
= event
->attr
.loglevel
;
192 key
.exclusion
= event
->exclusion
;
194 node_ptr
= cds_lfht_add_unique(ht
->ht
,
195 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
196 ht_match_ust_app_event
, &key
, &event
->node
.node
);
197 assert(node_ptr
== &event
->node
.node
);
201 * Close the notify socket from the given RCU head object. This MUST be called
202 * through a call_rcu().
204 static void close_notify_sock_rcu(struct rcu_head
*head
)
207 struct ust_app_notify_sock_obj
*obj
=
208 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
210 /* Must have a valid fd here. */
211 assert(obj
->fd
>= 0);
213 ret
= close(obj
->fd
);
215 ERR("close notify sock %d RCU", obj
->fd
);
217 lttng_fd_put(LTTNG_FD_APPS
, 1);
223 * Return the session registry according to the buffer type of the given
226 * A registry per UID object MUST exists before calling this function or else
227 * it assert() if not found. RCU read side lock must be acquired.
229 static struct ust_registry_session
*get_session_registry(
230 struct ust_app_session
*ua_sess
)
232 struct ust_registry_session
*registry
= NULL
;
236 switch (ua_sess
->buffer_type
) {
237 case LTTNG_BUFFER_PER_PID
:
239 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
243 registry
= reg_pid
->registry
->reg
.ust
;
246 case LTTNG_BUFFER_PER_UID
:
248 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
249 ua_sess
->tracing_id
, ua_sess
->bits_per_long
, ua_sess
->uid
);
253 registry
= reg_uid
->registry
->reg
.ust
;
265 * Delete ust context safely. RCU read lock must be held before calling
269 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
277 pthread_mutex_lock(&app
->sock_lock
);
278 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
279 pthread_mutex_unlock(&app
->sock_lock
);
280 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
281 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
282 sock
, ua_ctx
->obj
->handle
, ret
);
290 * Delete ust app event safely. RCU read lock must be held before calling
294 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
301 free(ua_event
->filter
);
302 if (ua_event
->exclusion
!= NULL
)
303 free(ua_event
->exclusion
);
304 if (ua_event
->obj
!= NULL
) {
305 pthread_mutex_lock(&app
->sock_lock
);
306 ret
= ustctl_release_object(sock
, ua_event
->obj
);
307 pthread_mutex_unlock(&app
->sock_lock
);
308 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
309 ERR("UST app sock %d release event obj failed with ret %d",
318 * Release ust data object of the given stream.
320 * Return 0 on success or else a negative value.
322 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
330 pthread_mutex_lock(&app
->sock_lock
);
331 ret
= ustctl_release_object(sock
, stream
->obj
);
332 pthread_mutex_unlock(&app
->sock_lock
);
333 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
334 ERR("UST app sock %d release stream obj failed with ret %d",
337 lttng_fd_put(LTTNG_FD_APPS
, 2);
345 * Delete ust app stream safely. RCU read lock must be held before calling
349 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
354 (void) release_ust_app_stream(sock
, stream
, app
);
359 * We need to execute ht_destroy outside of RCU read-side critical
360 * section and outside of call_rcu thread, so we postpone its execution
361 * using ht_cleanup_push. It is simpler than to change the semantic of
362 * the many callers of delete_ust_app_session().
365 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
367 struct ust_app_channel
*ua_chan
=
368 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
370 ht_cleanup_push(ua_chan
->ctx
);
371 ht_cleanup_push(ua_chan
->events
);
376 * Extract the lost packet or discarded events counter when the channel is
377 * being deleted and store the value in the parent channel so we can
378 * access it from lttng list and at stop/destroy.
380 * The session list lock must be held by the caller.
383 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
385 uint64_t discarded
= 0, lost
= 0;
386 struct ltt_session
*session
;
387 struct ltt_ust_channel
*uchan
;
389 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
394 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
395 if (!session
|| !session
->ust_session
) {
397 * Not finding the session is not an error because there are
398 * multiple ways the channels can be torn down.
400 * 1) The session daemon can initiate the destruction of the
401 * ust app session after receiving a destroy command or
402 * during its shutdown/teardown.
403 * 2) The application, since we are in per-pid tracing, is
404 * unregistering and tearing down its ust app session.
406 * Both paths are protected by the session list lock which
407 * ensures that the accounting of lost packets and discarded
408 * events is done exactly once. The session is then unpublished
409 * from the session list, resulting in this condition.
414 if (ua_chan
->attr
.overwrite
) {
415 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
416 ua_chan
->key
, session
->ust_session
->consumer
,
419 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
420 ua_chan
->key
, session
->ust_session
->consumer
,
423 uchan
= trace_ust_find_channel_by_name(
424 session
->ust_session
->domain_global
.channels
,
427 ERR("Missing UST channel to store discarded counters");
431 uchan
->per_pid_closed_app_discarded
+= discarded
;
432 uchan
->per_pid_closed_app_lost
+= lost
;
439 * Delete ust app channel safely. RCU read lock must be held before calling
442 * The session list lock must be held by the caller.
445 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
449 struct lttng_ht_iter iter
;
450 struct ust_app_event
*ua_event
;
451 struct ust_app_ctx
*ua_ctx
;
452 struct ust_app_stream
*stream
, *stmp
;
453 struct ust_registry_session
*registry
;
457 DBG3("UST app deleting channel %s", ua_chan
->name
);
460 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
461 cds_list_del(&stream
->list
);
462 delete_ust_app_stream(sock
, stream
, app
);
466 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
467 cds_list_del(&ua_ctx
->list
);
468 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
470 delete_ust_app_ctx(sock
, ua_ctx
, app
);
474 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
476 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
478 delete_ust_app_event(sock
, ua_event
, app
);
481 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
482 /* Wipe and free registry from session registry. */
483 registry
= get_session_registry(ua_chan
->session
);
485 ust_registry_channel_del_free(registry
, ua_chan
->key
);
487 save_per_pid_lost_discarded_counters(ua_chan
);
490 if (ua_chan
->obj
!= NULL
) {
491 /* Remove channel from application UST object descriptor. */
492 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
493 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
495 pthread_mutex_lock(&app
->sock_lock
);
496 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
497 pthread_mutex_unlock(&app
->sock_lock
);
498 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
499 ERR("UST app sock %d release channel obj failed with ret %d",
502 lttng_fd_put(LTTNG_FD_APPS
, 1);
505 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
508 int ust_app_register_done(struct ust_app
*app
)
512 pthread_mutex_lock(&app
->sock_lock
);
513 ret
= ustctl_register_done(app
->sock
);
514 pthread_mutex_unlock(&app
->sock_lock
);
518 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
523 pthread_mutex_lock(&app
->sock_lock
);
528 ret
= ustctl_release_object(sock
, data
);
530 pthread_mutex_unlock(&app
->sock_lock
);
536 * Push metadata to consumer socket.
538 * RCU read-side lock must be held to guarantee existance of socket.
539 * Must be called with the ust app session lock held.
540 * Must be called with the registry lock held.
542 * On success, return the len of metadata pushed or else a negative value.
543 * Returning a -EPIPE return value means we could not send the metadata,
544 * but it can be caused by recoverable errors (e.g. the application has
545 * terminated concurrently).
547 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
548 struct consumer_socket
*socket
, int send_zero_data
)
551 char *metadata_str
= NULL
;
552 size_t len
, offset
, new_metadata_len_sent
;
554 uint64_t metadata_key
, metadata_version
;
559 metadata_key
= registry
->metadata_key
;
562 * Means that no metadata was assigned to the session. This can
563 * happens if no start has been done previously.
569 offset
= registry
->metadata_len_sent
;
570 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
571 new_metadata_len_sent
= registry
->metadata_len
;
572 metadata_version
= registry
->metadata_version
;
574 DBG3("No metadata to push for metadata key %" PRIu64
,
575 registry
->metadata_key
);
577 if (send_zero_data
) {
578 DBG("No metadata to push");
584 /* Allocate only what we have to send. */
585 metadata_str
= zmalloc(len
);
587 PERROR("zmalloc ust app metadata string");
591 /* Copy what we haven't sent out. */
592 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
595 pthread_mutex_unlock(®istry
->lock
);
597 * We need to unlock the registry while we push metadata to
598 * break a circular dependency between the consumerd metadata
599 * lock and the sessiond registry lock. Indeed, pushing metadata
600 * to the consumerd awaits that it gets pushed all the way to
601 * relayd, but doing so requires grabbing the metadata lock. If
602 * a concurrent metadata request is being performed by
603 * consumerd, this can try to grab the registry lock on the
604 * sessiond while holding the metadata lock on the consumer
605 * daemon. Those push and pull schemes are performed on two
606 * different bidirectionnal communication sockets.
608 ret
= consumer_push_metadata(socket
, metadata_key
,
609 metadata_str
, len
, offset
, metadata_version
);
610 pthread_mutex_lock(®istry
->lock
);
613 * There is an acceptable race here between the registry
614 * metadata key assignment and the creation on the
615 * consumer. The session daemon can concurrently push
616 * metadata for this registry while being created on the
617 * consumer since the metadata key of the registry is
618 * assigned *before* it is setup to avoid the consumer
619 * to ask for metadata that could possibly be not found
620 * in the session daemon.
622 * The metadata will get pushed either by the session
623 * being stopped or the consumer requesting metadata if
624 * that race is triggered.
626 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
629 ERR("Error pushing metadata to consumer");
635 * Metadata may have been concurrently pushed, since
636 * we're not holding the registry lock while pushing to
637 * consumer. This is handled by the fact that we send
638 * the metadata content, size, and the offset at which
639 * that metadata belongs. This may arrive out of order
640 * on the consumer side, and the consumer is able to
641 * deal with overlapping fragments. The consumer
642 * supports overlapping fragments, which must be
643 * contiguous starting from offset 0. We keep the
644 * largest metadata_len_sent value of the concurrent
647 registry
->metadata_len_sent
=
648 max_t(size_t, registry
->metadata_len_sent
,
649 new_metadata_len_sent
);
658 * On error, flag the registry that the metadata is
659 * closed. We were unable to push anything and this
660 * means that either the consumer is not responding or
661 * the metadata cache has been destroyed on the
664 registry
->metadata_closed
= 1;
672 * For a given application and session, push metadata to consumer.
673 * Either sock or consumer is required : if sock is NULL, the default
674 * socket to send the metadata is retrieved from consumer, if sock
675 * is not NULL we use it to send the metadata.
676 * RCU read-side lock must be held while calling this function,
677 * therefore ensuring existance of registry. It also ensures existance
678 * of socket throughout this function.
680 * Return 0 on success else a negative error.
681 * Returning a -EPIPE return value means we could not send the metadata,
682 * but it can be caused by recoverable errors (e.g. the application has
683 * terminated concurrently).
685 static int push_metadata(struct ust_registry_session
*registry
,
686 struct consumer_output
*consumer
)
690 struct consumer_socket
*socket
;
695 pthread_mutex_lock(®istry
->lock
);
696 if (registry
->metadata_closed
) {
701 /* Get consumer socket to use to push the metadata.*/
702 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
709 ret
= ust_app_push_metadata(registry
, socket
, 0);
714 pthread_mutex_unlock(®istry
->lock
);
718 pthread_mutex_unlock(®istry
->lock
);
723 * Send to the consumer a close metadata command for the given session. Once
724 * done, the metadata channel is deleted and the session metadata pointer is
725 * nullified. The session lock MUST be held unless the application is
726 * in the destroy path.
728 * Return 0 on success else a negative value.
730 static int close_metadata(struct ust_registry_session
*registry
,
731 struct consumer_output
*consumer
)
734 struct consumer_socket
*socket
;
741 pthread_mutex_lock(®istry
->lock
);
743 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
748 /* Get consumer socket to use to push the metadata.*/
749 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
756 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
763 * Metadata closed. Even on error this means that the consumer is not
764 * responding or not found so either way a second close should NOT be emit
767 registry
->metadata_closed
= 1;
769 pthread_mutex_unlock(®istry
->lock
);
775 * We need to execute ht_destroy outside of RCU read-side critical
776 * section and outside of call_rcu thread, so we postpone its execution
777 * using ht_cleanup_push. It is simpler than to change the semantic of
778 * the many callers of delete_ust_app_session().
781 void delete_ust_app_session_rcu(struct rcu_head
*head
)
783 struct ust_app_session
*ua_sess
=
784 caa_container_of(head
, struct ust_app_session
, rcu_head
);
786 ht_cleanup_push(ua_sess
->channels
);
791 * Delete ust app session safely. RCU read lock must be held before calling
794 * The session list lock must be held by the caller.
797 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
801 struct lttng_ht_iter iter
;
802 struct ust_app_channel
*ua_chan
;
803 struct ust_registry_session
*registry
;
807 pthread_mutex_lock(&ua_sess
->lock
);
809 assert(!ua_sess
->deleted
);
810 ua_sess
->deleted
= true;
812 registry
= get_session_registry(ua_sess
);
813 /* Registry can be null on error path during initialization. */
815 /* Push metadata for application before freeing the application. */
816 (void) push_metadata(registry
, ua_sess
->consumer
);
819 * Don't ask to close metadata for global per UID buffers. Close
820 * metadata only on destroy trace session in this case. Also, the
821 * previous push metadata could have flag the metadata registry to
822 * close so don't send a close command if closed.
824 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
825 /* And ask to close it for this session registry. */
826 (void) close_metadata(registry
, ua_sess
->consumer
);
830 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
832 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
834 delete_ust_app_channel(sock
, ua_chan
, app
);
837 /* In case of per PID, the registry is kept in the session. */
838 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
839 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
842 * Registry can be null on error path during
845 buffer_reg_pid_remove(reg_pid
);
846 buffer_reg_pid_destroy(reg_pid
);
850 if (ua_sess
->handle
!= -1) {
851 pthread_mutex_lock(&app
->sock_lock
);
852 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
853 pthread_mutex_unlock(&app
->sock_lock
);
854 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
855 ERR("UST app sock %d release session handle failed with ret %d",
858 /* Remove session from application UST object descriptor. */
859 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
860 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
864 pthread_mutex_unlock(&ua_sess
->lock
);
866 consumer_output_put(ua_sess
->consumer
);
868 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
872 * Delete a traceable application structure from the global list. Never call
873 * this function outside of a call_rcu call.
875 * RCU read side lock should _NOT_ be held when calling this function.
878 void delete_ust_app(struct ust_app
*app
)
881 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
884 * The session list lock must be held during this function to guarantee
885 * the existence of ua_sess.
888 /* Delete ust app sessions info */
893 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
895 /* Free every object in the session and the session. */
897 delete_ust_app_session(sock
, ua_sess
, app
);
901 ht_cleanup_push(app
->sessions
);
902 ht_cleanup_push(app
->ust_sessions_objd
);
903 ht_cleanup_push(app
->ust_objd
);
906 * Wait until we have deleted the application from the sock hash table
907 * before closing this socket, otherwise an application could re-use the
908 * socket ID and race with the teardown, using the same hash table entry.
910 * It's OK to leave the close in call_rcu. We want it to stay unique for
911 * all RCU readers that could run concurrently with unregister app,
912 * therefore we _need_ to only close that socket after a grace period. So
913 * it should stay in this RCU callback.
915 * This close() is a very important step of the synchronization model so
916 * every modification to this function must be carefully reviewed.
922 lttng_fd_put(LTTNG_FD_APPS
, 1);
924 DBG2("UST app pid %d deleted", app
->pid
);
926 session_unlock_list();
930 * URCU intermediate call to delete an UST app.
933 void delete_ust_app_rcu(struct rcu_head
*head
)
935 struct lttng_ht_node_ulong
*node
=
936 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
937 struct ust_app
*app
=
938 caa_container_of(node
, struct ust_app
, pid_n
);
940 DBG3("Call RCU deleting app PID %d", app
->pid
);
945 * Delete the session from the application ht and delete the data structure by
946 * freeing every object inside and releasing them.
948 * The session list lock must be held by the caller.
950 static void destroy_app_session(struct ust_app
*app
,
951 struct ust_app_session
*ua_sess
)
954 struct lttng_ht_iter iter
;
959 iter
.iter
.node
= &ua_sess
->node
.node
;
960 ret
= lttng_ht_del(app
->sessions
, &iter
);
962 /* Already scheduled for teardown. */
966 /* Once deleted, free the data structure. */
967 delete_ust_app_session(app
->sock
, ua_sess
, app
);
974 * Alloc new UST app session.
977 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
979 struct ust_app_session
*ua_sess
;
981 /* Init most of the default value by allocating and zeroing */
982 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
983 if (ua_sess
== NULL
) {
988 ua_sess
->handle
= -1;
989 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
990 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
991 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1000 * Alloc new UST app channel.
1003 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
1004 struct ust_app_session
*ua_sess
,
1005 struct lttng_ust_channel_attr
*attr
)
1007 struct ust_app_channel
*ua_chan
;
1009 /* Init most of the default value by allocating and zeroing */
1010 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1011 if (ua_chan
== NULL
) {
1016 /* Setup channel name */
1017 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1018 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1020 ua_chan
->enabled
= 1;
1021 ua_chan
->handle
= -1;
1022 ua_chan
->session
= ua_sess
;
1023 ua_chan
->key
= get_next_channel_key();
1024 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1025 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1026 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1028 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1029 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1031 /* Copy attributes */
1033 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1034 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1035 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1036 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1037 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1038 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1039 ua_chan
->attr
.output
= attr
->output
;
1041 /* By default, the channel is a per cpu channel. */
1042 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1044 DBG3("UST app channel %s allocated", ua_chan
->name
);
1053 * Allocate and initialize a UST app stream.
1055 * Return newly allocated stream pointer or NULL on error.
1057 struct ust_app_stream
*ust_app_alloc_stream(void)
1059 struct ust_app_stream
*stream
= NULL
;
1061 stream
= zmalloc(sizeof(*stream
));
1062 if (stream
== NULL
) {
1063 PERROR("zmalloc ust app stream");
1067 /* Zero could be a valid value for a handle so flag it to -1. */
1068 stream
->handle
= -1;
1075 * Alloc new UST app event.
1078 struct ust_app_event
*alloc_ust_app_event(char *name
,
1079 struct lttng_ust_event
*attr
)
1081 struct ust_app_event
*ua_event
;
1083 /* Init most of the default value by allocating and zeroing */
1084 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1085 if (ua_event
== NULL
) {
1090 ua_event
->enabled
= 1;
1091 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1092 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1093 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1095 /* Copy attributes */
1097 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1100 DBG3("UST app event %s allocated", ua_event
->name
);
1109 * Alloc new UST app context.
1112 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1114 struct ust_app_ctx
*ua_ctx
;
1116 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1117 if (ua_ctx
== NULL
) {
1121 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1124 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1125 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1126 char *provider_name
= NULL
, *ctx_name
= NULL
;
1128 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1129 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1130 if (!provider_name
|| !ctx_name
) {
1131 free(provider_name
);
1136 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1137 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1141 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1149 * Allocate a filter and copy the given original filter.
1151 * Return allocated filter or NULL on error.
1153 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1154 struct lttng_filter_bytecode
*orig_f
)
1156 struct lttng_filter_bytecode
*filter
= NULL
;
1158 /* Copy filter bytecode */
1159 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1161 PERROR("zmalloc alloc filter bytecode");
1165 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1172 * Create a liblttng-ust filter bytecode from given bytecode.
1174 * Return allocated filter or NULL on error.
1176 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1177 struct lttng_filter_bytecode
*orig_f
)
1179 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1181 /* Copy filter bytecode */
1182 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1184 PERROR("zmalloc alloc ust filter bytecode");
1188 assert(sizeof(struct lttng_filter_bytecode
) ==
1189 sizeof(struct lttng_ust_filter_bytecode
));
1190 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1196 * Find an ust_app using the sock and return it. RCU read side lock must be
1197 * held before calling this helper function.
1199 struct ust_app
*ust_app_find_by_sock(int sock
)
1201 struct lttng_ht_node_ulong
*node
;
1202 struct lttng_ht_iter iter
;
1204 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1205 node
= lttng_ht_iter_get_node_ulong(&iter
);
1207 DBG2("UST app find by sock %d not found", sock
);
1211 return caa_container_of(node
, struct ust_app
, sock_n
);
1218 * Find an ust_app using the notify sock and return it. RCU read side lock must
1219 * be held before calling this helper function.
1221 static struct ust_app
*find_app_by_notify_sock(int sock
)
1223 struct lttng_ht_node_ulong
*node
;
1224 struct lttng_ht_iter iter
;
1226 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1228 node
= lttng_ht_iter_get_node_ulong(&iter
);
1230 DBG2("UST app find by notify sock %d not found", sock
);
1234 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1241 * Lookup for an ust app event based on event name, filter bytecode and the
1244 * Return an ust_app_event object or NULL on error.
1246 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1247 char *name
, struct lttng_filter_bytecode
*filter
,
1249 const struct lttng_event_exclusion
*exclusion
)
1251 struct lttng_ht_iter iter
;
1252 struct lttng_ht_node_str
*node
;
1253 struct ust_app_event
*event
= NULL
;
1254 struct ust_app_ht_key key
;
1259 /* Setup key for event lookup. */
1261 key
.filter
= filter
;
1262 key
.loglevel_type
= loglevel_value
;
1263 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1264 key
.exclusion
= exclusion
;
1266 /* Lookup using the event name as hash and a custom match fct. */
1267 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1268 ht_match_ust_app_event
, &key
, &iter
.iter
);
1269 node
= lttng_ht_iter_get_node_str(&iter
);
1274 event
= caa_container_of(node
, struct ust_app_event
, node
);
1281 * Create the channel context on the tracer.
1283 * Called with UST app session lock held.
1286 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1287 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1291 health_code_update();
1293 pthread_mutex_lock(&app
->sock_lock
);
1294 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1295 ua_chan
->obj
, &ua_ctx
->obj
);
1296 pthread_mutex_unlock(&app
->sock_lock
);
1298 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1299 ERR("UST app create channel context failed for app (pid: %d) "
1300 "with ret %d", app
->pid
, ret
);
1303 * This is normal behavior, an application can die during the
1304 * creation process. Don't report an error so the execution can
1305 * continue normally.
1308 DBG3("UST app disable event failed. Application is dead.");
1313 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1315 DBG2("UST app context handle %d created successfully for channel %s",
1316 ua_ctx
->handle
, ua_chan
->name
);
1319 health_code_update();
1324 * Set the filter on the tracer.
1327 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1328 struct ust_app
*app
)
1331 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1333 health_code_update();
1335 if (!ua_event
->filter
) {
1340 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1341 if (!ust_bytecode
) {
1342 ret
= -LTTNG_ERR_NOMEM
;
1345 pthread_mutex_lock(&app
->sock_lock
);
1346 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1348 pthread_mutex_unlock(&app
->sock_lock
);
1350 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1351 ERR("UST app event %s filter failed for app (pid: %d) "
1352 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1355 * This is normal behavior, an application can die during the
1356 * creation process. Don't report an error so the execution can
1357 * continue normally.
1360 DBG3("UST app filter event failed. Application is dead.");
1365 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1368 health_code_update();
1374 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1375 struct lttng_event_exclusion
*exclusion
)
1377 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1378 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1379 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1381 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1382 if (!ust_exclusion
) {
1387 assert(sizeof(struct lttng_event_exclusion
) ==
1388 sizeof(struct lttng_ust_event_exclusion
));
1389 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1391 return ust_exclusion
;
1395 * Set event exclusions on the tracer.
1398 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1399 struct ust_app
*app
)
1402 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1404 health_code_update();
1406 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1411 ust_exclusion
= create_ust_exclusion_from_exclusion(
1412 ua_event
->exclusion
);
1413 if (!ust_exclusion
) {
1414 ret
= -LTTNG_ERR_NOMEM
;
1417 pthread_mutex_lock(&app
->sock_lock
);
1418 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1419 pthread_mutex_unlock(&app
->sock_lock
);
1421 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1422 ERR("UST app event %s exclusions failed for app (pid: %d) "
1423 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1426 * This is normal behavior, an application can die during the
1427 * creation process. Don't report an error so the execution can
1428 * continue normally.
1431 DBG3("UST app event exclusion failed. Application is dead.");
1436 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1439 health_code_update();
1440 free(ust_exclusion
);
1445 * Disable the specified event on to UST tracer for the UST session.
1447 static int disable_ust_event(struct ust_app
*app
,
1448 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1452 health_code_update();
1454 pthread_mutex_lock(&app
->sock_lock
);
1455 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1456 pthread_mutex_unlock(&app
->sock_lock
);
1458 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1459 ERR("UST app event %s disable failed for app (pid: %d) "
1460 "and session handle %d with ret %d",
1461 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1464 * This is normal behavior, an application can die during the
1465 * creation process. Don't report an error so the execution can
1466 * continue normally.
1469 DBG3("UST app disable event failed. Application is dead.");
1474 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1475 ua_event
->attr
.name
, app
->pid
);
1478 health_code_update();
1483 * Disable the specified channel on to UST tracer for the UST session.
1485 static int disable_ust_channel(struct ust_app
*app
,
1486 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1490 health_code_update();
1492 pthread_mutex_lock(&app
->sock_lock
);
1493 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1494 pthread_mutex_unlock(&app
->sock_lock
);
1496 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1497 ERR("UST app channel %s disable failed for app (pid: %d) "
1498 "and session handle %d with ret %d",
1499 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1502 * This is normal behavior, an application can die during the
1503 * creation process. Don't report an error so the execution can
1504 * continue normally.
1507 DBG3("UST app disable channel failed. Application is dead.");
1512 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1513 ua_chan
->name
, app
->pid
);
1516 health_code_update();
1521 * Enable the specified channel on to UST tracer for the UST session.
1523 static int enable_ust_channel(struct ust_app
*app
,
1524 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1528 health_code_update();
1530 pthread_mutex_lock(&app
->sock_lock
);
1531 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1532 pthread_mutex_unlock(&app
->sock_lock
);
1534 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1535 ERR("UST app channel %s enable failed for app (pid: %d) "
1536 "and session handle %d with ret %d",
1537 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1540 * This is normal behavior, an application can die during the
1541 * creation process. Don't report an error so the execution can
1542 * continue normally.
1545 DBG3("UST app enable channel failed. Application is dead.");
1550 ua_chan
->enabled
= 1;
1552 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1553 ua_chan
->name
, app
->pid
);
1556 health_code_update();
1561 * Enable the specified event on to UST tracer for the UST session.
1563 static int enable_ust_event(struct ust_app
*app
,
1564 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1568 health_code_update();
1570 pthread_mutex_lock(&app
->sock_lock
);
1571 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1572 pthread_mutex_unlock(&app
->sock_lock
);
1574 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1575 ERR("UST app event %s enable failed for app (pid: %d) "
1576 "and session handle %d with ret %d",
1577 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1580 * This is normal behavior, an application can die during the
1581 * creation process. Don't report an error so the execution can
1582 * continue normally.
1585 DBG3("UST app enable event failed. Application is dead.");
1590 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1591 ua_event
->attr
.name
, app
->pid
);
1594 health_code_update();
1599 * Send channel and stream buffer to application.
1601 * Return 0 on success. On error, a negative value is returned.
1603 static int send_channel_pid_to_ust(struct ust_app
*app
,
1604 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1607 struct ust_app_stream
*stream
, *stmp
;
1613 health_code_update();
1615 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1618 /* Send channel to the application. */
1619 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1620 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1621 ret
= -ENOTCONN
; /* Caused by app exiting. */
1623 } else if (ret
< 0) {
1627 health_code_update();
1629 /* Send all streams to application. */
1630 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1631 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1632 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1633 ret
= -ENOTCONN
; /* Caused by app exiting. */
1635 } else if (ret
< 0) {
1638 /* We don't need the stream anymore once sent to the tracer. */
1639 cds_list_del(&stream
->list
);
1640 delete_ust_app_stream(-1, stream
, app
);
1642 /* Flag the channel that it is sent to the application. */
1643 ua_chan
->is_sent
= 1;
1646 health_code_update();
1651 * Create the specified event onto the UST tracer for a UST session.
1653 * Should be called with session mutex held.
1656 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1657 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1661 health_code_update();
1663 /* Create UST event on tracer */
1664 pthread_mutex_lock(&app
->sock_lock
);
1665 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1667 pthread_mutex_unlock(&app
->sock_lock
);
1669 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1670 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1671 ua_event
->attr
.name
, app
->pid
, ret
);
1674 * This is normal behavior, an application can die during the
1675 * creation process. Don't report an error so the execution can
1676 * continue normally.
1679 DBG3("UST app create event failed. Application is dead.");
1684 ua_event
->handle
= ua_event
->obj
->handle
;
1686 DBG2("UST app event %s created successfully for pid:%d",
1687 ua_event
->attr
.name
, app
->pid
);
1689 health_code_update();
1691 /* Set filter if one is present. */
1692 if (ua_event
->filter
) {
1693 ret
= set_ust_event_filter(ua_event
, app
);
1699 /* Set exclusions for the event */
1700 if (ua_event
->exclusion
) {
1701 ret
= set_ust_event_exclusion(ua_event
, app
);
1707 /* If event not enabled, disable it on the tracer */
1708 if (ua_event
->enabled
) {
1710 * We now need to explicitly enable the event, since it
1711 * is now disabled at creation.
1713 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1716 * If we hit an EPERM, something is wrong with our enable call. If
1717 * we get an EEXIST, there is a problem on the tracer side since we
1721 case -LTTNG_UST_ERR_PERM
:
1722 /* Code flow problem */
1724 case -LTTNG_UST_ERR_EXIST
:
1725 /* It's OK for our use case. */
1736 health_code_update();
1741 * Copy data between an UST app event and a LTT event.
1743 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1744 struct ltt_ust_event
*uevent
)
1746 size_t exclusion_alloc_size
;
1748 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1749 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1751 ua_event
->enabled
= uevent
->enabled
;
1753 /* Copy event attributes */
1754 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1756 /* Copy filter bytecode */
1757 if (uevent
->filter
) {
1758 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1759 /* Filter might be NULL here in case of ENONEM. */
1762 /* Copy exclusion data */
1763 if (uevent
->exclusion
) {
1764 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1765 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1766 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1767 if (ua_event
->exclusion
== NULL
) {
1770 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1771 exclusion_alloc_size
);
1777 * Copy data between an UST app channel and a LTT channel.
1779 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1780 struct ltt_ust_channel
*uchan
)
1782 struct lttng_ht_iter iter
;
1783 struct ltt_ust_event
*uevent
;
1784 struct ltt_ust_context
*uctx
;
1785 struct ust_app_event
*ua_event
;
1787 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1789 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1790 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1792 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1793 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1795 /* Copy event attributes since the layout is different. */
1796 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1797 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1798 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1799 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1800 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1801 ua_chan
->attr
.output
= uchan
->attr
.output
;
1803 * Note that the attribute channel type is not set since the channel on the
1804 * tracing registry side does not have this information.
1807 ua_chan
->enabled
= uchan
->enabled
;
1808 ua_chan
->tracing_channel_id
= uchan
->id
;
1810 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1811 struct ust_app_ctx
*ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1813 if (ua_ctx
== NULL
) {
1816 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1817 (unsigned long) ua_ctx
->ctx
.ctx
);
1818 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1819 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1822 /* Copy all events from ltt ust channel to ust app channel */
1823 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1824 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1825 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1826 if (ua_event
== NULL
) {
1827 DBG2("UST event %s not found on shadow copy channel",
1829 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1830 if (ua_event
== NULL
) {
1833 shadow_copy_event(ua_event
, uevent
);
1834 add_unique_ust_app_event(ua_chan
, ua_event
);
1838 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1842 * Copy data between a UST app session and a regular LTT session.
1844 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1845 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1847 struct lttng_ht_node_str
*ua_chan_node
;
1848 struct lttng_ht_iter iter
;
1849 struct ltt_ust_channel
*uchan
;
1850 struct ust_app_channel
*ua_chan
;
1852 struct tm
*timeinfo
;
1855 char tmp_shm_path
[PATH_MAX
];
1857 /* Get date and time for unique app path */
1859 timeinfo
= localtime(&rawtime
);
1860 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1862 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1864 ua_sess
->tracing_id
= usess
->id
;
1865 ua_sess
->id
= get_next_session_id();
1866 ua_sess
->uid
= app
->uid
;
1867 ua_sess
->gid
= app
->gid
;
1868 ua_sess
->euid
= usess
->uid
;
1869 ua_sess
->egid
= usess
->gid
;
1870 ua_sess
->buffer_type
= usess
->buffer_type
;
1871 ua_sess
->bits_per_long
= app
->bits_per_long
;
1873 /* There is only one consumer object per session possible. */
1874 consumer_output_get(usess
->consumer
);
1875 ua_sess
->consumer
= usess
->consumer
;
1877 ua_sess
->output_traces
= usess
->output_traces
;
1878 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1879 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1880 &usess
->metadata_attr
);
1882 switch (ua_sess
->buffer_type
) {
1883 case LTTNG_BUFFER_PER_PID
:
1884 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1885 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1888 case LTTNG_BUFFER_PER_UID
:
1889 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1890 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1897 PERROR("asprintf UST shadow copy session");
1902 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1903 sizeof(ua_sess
->root_shm_path
));
1904 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1905 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1906 sizeof(ua_sess
->shm_path
));
1907 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1908 if (ua_sess
->shm_path
[0]) {
1909 switch (ua_sess
->buffer_type
) {
1910 case LTTNG_BUFFER_PER_PID
:
1911 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1912 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1913 app
->name
, app
->pid
, datetime
);
1915 case LTTNG_BUFFER_PER_UID
:
1916 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1917 DEFAULT_UST_TRACE_UID_PATH
,
1918 app
->uid
, app
->bits_per_long
);
1925 PERROR("sprintf UST shadow copy session");
1929 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1930 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1931 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1934 /* Iterate over all channels in global domain. */
1935 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1937 struct lttng_ht_iter uiter
;
1939 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1940 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1941 if (ua_chan_node
!= NULL
) {
1942 /* Session exist. Contiuing. */
1946 DBG2("Channel %s not found on shadow session copy, creating it",
1948 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
,
1950 if (ua_chan
== NULL
) {
1951 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1954 shadow_copy_channel(ua_chan
, uchan
);
1956 * The concept of metadata channel does not exist on the tracing
1957 * registry side of the session daemon so this can only be a per CPU
1958 * channel and not metadata.
1960 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1962 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1967 consumer_output_put(ua_sess
->consumer
);
1971 * Lookup sesison wrapper.
1974 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1975 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1977 /* Get right UST app session from app */
1978 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1982 * Return ust app session from the app session hashtable using the UST session
1985 static struct ust_app_session
*lookup_session_by_app(
1986 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1988 struct lttng_ht_iter iter
;
1989 struct lttng_ht_node_u64
*node
;
1991 __lookup_session_by_app(usess
, app
, &iter
);
1992 node
= lttng_ht_iter_get_node_u64(&iter
);
1997 return caa_container_of(node
, struct ust_app_session
, node
);
2004 * Setup buffer registry per PID for the given session and application. If none
2005 * is found, a new one is created, added to the global registry and
2006 * initialized. If regp is valid, it's set with the newly created object.
2008 * Return 0 on success or else a negative value.
2010 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2011 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2014 struct buffer_reg_pid
*reg_pid
;
2021 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2024 * This is the create channel path meaning that if there is NO
2025 * registry available, we have to create one for this session.
2027 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2028 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2036 /* Initialize registry. */
2037 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2038 app
->bits_per_long
, app
->uint8_t_alignment
,
2039 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2040 app
->uint64_t_alignment
, app
->long_alignment
,
2041 app
->byte_order
, app
->version
.major
,
2042 app
->version
.minor
, reg_pid
->root_shm_path
,
2044 ua_sess
->euid
, ua_sess
->egid
);
2047 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2048 * destroy the buffer registry, because it is always expected
2049 * that if the buffer registry can be found, its ust registry is
2052 buffer_reg_pid_destroy(reg_pid
);
2056 buffer_reg_pid_add(reg_pid
);
2058 DBG3("UST app buffer registry per PID created successfully");
2070 * Setup buffer registry per UID for the given session and application. If none
2071 * is found, a new one is created, added to the global registry and
2072 * initialized. If regp is valid, it's set with the newly created object.
2074 * Return 0 on success or else a negative value.
2076 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2077 struct ust_app_session
*ua_sess
,
2078 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2081 struct buffer_reg_uid
*reg_uid
;
2088 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2091 * This is the create channel path meaning that if there is NO
2092 * registry available, we have to create one for this session.
2094 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2095 LTTNG_DOMAIN_UST
, ®_uid
,
2096 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2104 /* Initialize registry. */
2105 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2106 app
->bits_per_long
, app
->uint8_t_alignment
,
2107 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2108 app
->uint64_t_alignment
, app
->long_alignment
,
2109 app
->byte_order
, app
->version
.major
,
2110 app
->version
.minor
, reg_uid
->root_shm_path
,
2111 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
2114 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2115 * destroy the buffer registry, because it is always expected
2116 * that if the buffer registry can be found, its ust registry is
2119 buffer_reg_uid_destroy(reg_uid
, NULL
);
2122 /* Add node to teardown list of the session. */
2123 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2125 buffer_reg_uid_add(reg_uid
);
2127 DBG3("UST app buffer registry per UID created successfully");
2138 * Create a session on the tracer side for the given app.
2140 * On success, ua_sess_ptr is populated with the session pointer or else left
2141 * untouched. If the session was created, is_created is set to 1. On error,
2142 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2145 * Returns 0 on success or else a negative code which is either -ENOMEM or
2146 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2148 static int create_ust_app_session(struct ltt_ust_session
*usess
,
2149 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2152 int ret
, created
= 0;
2153 struct ust_app_session
*ua_sess
;
2157 assert(ua_sess_ptr
);
2159 health_code_update();
2161 ua_sess
= lookup_session_by_app(usess
, app
);
2162 if (ua_sess
== NULL
) {
2163 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2164 app
->pid
, usess
->id
);
2165 ua_sess
= alloc_ust_app_session(app
);
2166 if (ua_sess
== NULL
) {
2167 /* Only malloc can failed so something is really wrong */
2171 shadow_copy_session(ua_sess
, usess
, app
);
2175 switch (usess
->buffer_type
) {
2176 case LTTNG_BUFFER_PER_PID
:
2177 /* Init local registry. */
2178 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2180 delete_ust_app_session(-1, ua_sess
, app
);
2184 case LTTNG_BUFFER_PER_UID
:
2185 /* Look for a global registry. If none exists, create one. */
2186 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2188 delete_ust_app_session(-1, ua_sess
, app
);
2198 health_code_update();
2200 if (ua_sess
->handle
== -1) {
2201 pthread_mutex_lock(&app
->sock_lock
);
2202 ret
= ustctl_create_session(app
->sock
);
2203 pthread_mutex_unlock(&app
->sock_lock
);
2205 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2206 ERR("Creating session for app pid %d with ret %d",
2209 DBG("UST app creating session failed. Application is dead");
2211 * This is normal behavior, an application can die during the
2212 * creation process. Don't report an error so the execution can
2213 * continue normally. This will get flagged ENOTCONN and the
2214 * caller will handle it.
2218 delete_ust_app_session(-1, ua_sess
, app
);
2219 if (ret
!= -ENOMEM
) {
2221 * Tracer is probably gone or got an internal error so let's
2222 * behave like it will soon unregister or not usable.
2229 ua_sess
->handle
= ret
;
2231 /* Add ust app session to app's HT */
2232 lttng_ht_node_init_u64(&ua_sess
->node
,
2233 ua_sess
->tracing_id
);
2234 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2235 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2236 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2237 &ua_sess
->ust_objd_node
);
2239 DBG2("UST app session created successfully with handle %d", ret
);
2242 *ua_sess_ptr
= ua_sess
;
2244 *is_created
= created
;
2247 /* Everything went well. */
2251 health_code_update();
2256 * Match function for a hash table lookup of ust_app_ctx.
2258 * It matches an ust app context based on the context type and, in the case
2259 * of perf counters, their name.
2261 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2263 struct ust_app_ctx
*ctx
;
2264 const struct lttng_ust_context_attr
*key
;
2269 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2273 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2278 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2279 if (strncmp(key
->u
.perf_counter
.name
,
2280 ctx
->ctx
.u
.perf_counter
.name
,
2281 sizeof(key
->u
.perf_counter
.name
))) {
2285 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2286 if (strcmp(key
->u
.app_ctx
.provider_name
,
2287 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2288 strcmp(key
->u
.app_ctx
.ctx_name
,
2289 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2305 * Lookup for an ust app context from an lttng_ust_context.
2307 * Must be called while holding RCU read side lock.
2308 * Return an ust_app_ctx object or NULL on error.
2311 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2312 struct lttng_ust_context_attr
*uctx
)
2314 struct lttng_ht_iter iter
;
2315 struct lttng_ht_node_ulong
*node
;
2316 struct ust_app_ctx
*app_ctx
= NULL
;
2321 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2322 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2323 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2324 node
= lttng_ht_iter_get_node_ulong(&iter
);
2329 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2336 * Create a context for the channel on the tracer.
2338 * Called with UST app session lock held and a RCU read side lock.
2341 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2342 struct ust_app_channel
*ua_chan
,
2343 struct lttng_ust_context_attr
*uctx
,
2344 struct ust_app
*app
)
2347 struct ust_app_ctx
*ua_ctx
;
2349 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2351 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2357 ua_ctx
= alloc_ust_app_ctx(uctx
);
2358 if (ua_ctx
== NULL
) {
2364 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2365 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2366 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2368 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2378 * Enable on the tracer side a ust app event for the session and channel.
2380 * Called with UST app session lock held.
2383 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2384 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2388 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2393 ua_event
->enabled
= 1;
2400 * Disable on the tracer side a ust app event for the session and channel.
2402 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2403 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2407 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2412 ua_event
->enabled
= 0;
2419 * Lookup ust app channel for session and disable it on the tracer side.
2422 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2423 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2427 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2432 ua_chan
->enabled
= 0;
2439 * Lookup ust app channel for session and enable it on the tracer side. This
2440 * MUST be called with a RCU read side lock acquired.
2442 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2443 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2446 struct lttng_ht_iter iter
;
2447 struct lttng_ht_node_str
*ua_chan_node
;
2448 struct ust_app_channel
*ua_chan
;
2450 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2451 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2452 if (ua_chan_node
== NULL
) {
2453 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2454 uchan
->name
, ua_sess
->tracing_id
);
2458 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2460 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2470 * Ask the consumer to create a channel and get it if successful.
2472 * Called with UST app session lock held.
2474 * Return 0 on success or else a negative value.
2476 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2477 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2478 int bitness
, struct ust_registry_session
*registry
)
2481 unsigned int nb_fd
= 0;
2482 struct consumer_socket
*socket
;
2490 health_code_update();
2492 /* Get the right consumer socket for the application. */
2493 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2499 health_code_update();
2501 /* Need one fd for the channel. */
2502 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2504 ERR("Exhausted number of available FD upon create channel");
2509 * Ask consumer to create channel. The consumer will return the number of
2510 * stream we have to expect.
2512 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2519 * Compute the number of fd needed before receiving them. It must be 2 per
2520 * stream (2 being the default value here).
2522 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2524 /* Reserve the amount of file descriptor we need. */
2525 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2527 ERR("Exhausted number of available FD upon create channel");
2528 goto error_fd_get_stream
;
2531 health_code_update();
2534 * Now get the channel from the consumer. This call wil populate the stream
2535 * list of that channel and set the ust objects.
2537 if (usess
->consumer
->enabled
) {
2538 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2548 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2549 error_fd_get_stream
:
2551 * Initiate a destroy channel on the consumer since we had an error
2552 * handling it on our side. The return value is of no importance since we
2553 * already have a ret value set by the previous error that we need to
2556 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2558 lttng_fd_put(LTTNG_FD_APPS
, 1);
2560 health_code_update();
2566 * Duplicate the ust data object of the ust app stream and save it in the
2567 * buffer registry stream.
2569 * Return 0 on success or else a negative value.
2571 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2572 struct ust_app_stream
*stream
)
2579 /* Reserve the amount of file descriptor we need. */
2580 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2582 ERR("Exhausted number of available FD upon duplicate stream");
2586 /* Duplicate object for stream once the original is in the registry. */
2587 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2588 reg_stream
->obj
.ust
);
2590 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2591 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2592 lttng_fd_put(LTTNG_FD_APPS
, 2);
2595 stream
->handle
= stream
->obj
->handle
;
2602 * Duplicate the ust data object of the ust app. channel and save it in the
2603 * buffer registry channel.
2605 * Return 0 on success or else a negative value.
2607 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2608 struct ust_app_channel
*ua_chan
)
2615 /* Need two fds for the channel. */
2616 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2618 ERR("Exhausted number of available FD upon duplicate channel");
2622 /* Duplicate object for stream once the original is in the registry. */
2623 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2625 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2626 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2629 ua_chan
->handle
= ua_chan
->obj
->handle
;
2634 lttng_fd_put(LTTNG_FD_APPS
, 1);
2640 * For a given channel buffer registry, setup all streams of the given ust
2641 * application channel.
2643 * Return 0 on success or else a negative value.
2645 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2646 struct ust_app_channel
*ua_chan
,
2647 struct ust_app
*app
)
2650 struct ust_app_stream
*stream
, *stmp
;
2655 DBG2("UST app setup buffer registry stream");
2657 /* Send all streams to application. */
2658 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2659 struct buffer_reg_stream
*reg_stream
;
2661 ret
= buffer_reg_stream_create(®_stream
);
2667 * Keep original pointer and nullify it in the stream so the delete
2668 * stream call does not release the object.
2670 reg_stream
->obj
.ust
= stream
->obj
;
2672 buffer_reg_stream_add(reg_stream
, reg_chan
);
2674 /* We don't need the streams anymore. */
2675 cds_list_del(&stream
->list
);
2676 delete_ust_app_stream(-1, stream
, app
);
2684 * Create a buffer registry channel for the given session registry and
2685 * application channel object. If regp pointer is valid, it's set with the
2686 * created object. Important, the created object is NOT added to the session
2687 * registry hash table.
2689 * Return 0 on success else a negative value.
2691 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2692 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2695 struct buffer_reg_channel
*reg_chan
= NULL
;
2700 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2702 /* Create buffer registry channel. */
2703 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2708 reg_chan
->consumer_key
= ua_chan
->key
;
2709 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2710 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2712 /* Create and add a channel registry to session. */
2713 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2714 ua_chan
->tracing_channel_id
);
2718 buffer_reg_channel_add(reg_sess
, reg_chan
);
2727 /* Safe because the registry channel object was not added to any HT. */
2728 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2734 * Setup buffer registry channel for the given session registry and application
2735 * channel object. If regp pointer is valid, it's set with the created object.
2737 * Return 0 on success else a negative value.
2739 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2740 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2741 struct ust_app
*app
)
2748 assert(ua_chan
->obj
);
2750 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2752 /* Setup all streams for the registry. */
2753 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2758 reg_chan
->obj
.ust
= ua_chan
->obj
;
2759 ua_chan
->obj
= NULL
;
2764 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2765 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2770 * Send buffer registry channel to the application.
2772 * Return 0 on success else a negative value.
2774 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2775 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2776 struct ust_app_channel
*ua_chan
)
2779 struct buffer_reg_stream
*reg_stream
;
2786 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2788 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2793 /* Send channel to the application. */
2794 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2795 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2796 ret
= -ENOTCONN
; /* Caused by app exiting. */
2798 } else if (ret
< 0) {
2802 health_code_update();
2804 /* Send all streams to application. */
2805 pthread_mutex_lock(®_chan
->stream_list_lock
);
2806 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2807 struct ust_app_stream stream
;
2809 ret
= duplicate_stream_object(reg_stream
, &stream
);
2811 goto error_stream_unlock
;
2814 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2816 (void) release_ust_app_stream(-1, &stream
, app
);
2817 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2818 ret
= -ENOTCONN
; /* Caused by app exiting. */
2820 goto error_stream_unlock
;
2824 * The return value is not important here. This function will output an
2827 (void) release_ust_app_stream(-1, &stream
, app
);
2829 ua_chan
->is_sent
= 1;
2831 error_stream_unlock
:
2832 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2838 * Create and send to the application the created buffers with per UID buffers.
2840 * Return 0 on success else a negative value.
2842 static int create_channel_per_uid(struct ust_app
*app
,
2843 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2844 struct ust_app_channel
*ua_chan
)
2847 struct buffer_reg_uid
*reg_uid
;
2848 struct buffer_reg_channel
*reg_chan
;
2855 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2857 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2859 * The session creation handles the creation of this global registry
2860 * object. If none can be find, there is a code flow problem or a
2865 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2868 /* Create the buffer registry channel object. */
2869 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2871 ERR("Error creating the UST channel \"%s\" registry instance",
2878 * Create the buffers on the consumer side. This call populates the
2879 * ust app channel object with all streams and data object.
2881 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2882 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2884 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2888 * Let's remove the previously created buffer registry channel so
2889 * it's not visible anymore in the session registry.
2891 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2892 ua_chan
->tracing_channel_id
);
2893 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2894 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2899 * Setup the streams and add it to the session registry.
2901 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2902 ua_chan
, reg_chan
, app
);
2904 ERR("Error setting up UST channel \"%s\"",
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");
2925 * Create and send to the application the created buffers with per PID buffers.
2927 * Called with UST app session lock held.
2929 * Return 0 on success else a negative value.
2931 static int create_channel_per_pid(struct ust_app
*app
,
2932 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2933 struct ust_app_channel
*ua_chan
)
2936 struct ust_registry_session
*registry
;
2943 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2947 registry
= get_session_registry(ua_sess
);
2948 /* The UST app session lock is held, registry shall not be null. */
2951 /* Create and add a new channel registry to session. */
2952 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2954 ERR("Error creating the UST channel \"%s\" registry instance",
2959 /* Create and get channel on the consumer side. */
2960 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2961 app
->bits_per_long
, registry
);
2963 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2968 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2970 if (ret
!= -ENOTCONN
) {
2971 ERR("Error sending channel to application");
2982 * From an already allocated ust app channel, create the channel buffers if
2983 * need and send it to the application. This MUST be called with a RCU read
2984 * side lock acquired.
2986 * Called with UST app session lock held.
2988 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2989 * the application exited concurrently.
2991 static int do_create_channel(struct ust_app
*app
,
2992 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2993 struct ust_app_channel
*ua_chan
)
3002 /* Handle buffer type before sending the channel to the application. */
3003 switch (usess
->buffer_type
) {
3004 case LTTNG_BUFFER_PER_UID
:
3006 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3012 case LTTNG_BUFFER_PER_PID
:
3014 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3026 /* Initialize ust objd object using the received handle and add it. */
3027 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3028 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3030 /* If channel is not enabled, disable it on the tracer */
3031 if (!ua_chan
->enabled
) {
3032 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3043 * Create UST app channel and create it on the tracer. Set ua_chanp of the
3044 * newly created channel if not NULL.
3046 * Called with UST app session lock and RCU read-side lock held.
3048 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3049 * the application exited concurrently.
3051 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
3052 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
3053 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3054 struct ust_app_channel
**ua_chanp
)
3057 struct lttng_ht_iter iter
;
3058 struct lttng_ht_node_str
*ua_chan_node
;
3059 struct ust_app_channel
*ua_chan
;
3061 /* Lookup channel in the ust app session */
3062 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3063 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3064 if (ua_chan_node
!= NULL
) {
3065 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3069 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3070 if (ua_chan
== NULL
) {
3071 /* Only malloc can fail here */
3075 shadow_copy_channel(ua_chan
, uchan
);
3077 /* Set channel type. */
3078 ua_chan
->attr
.type
= type
;
3080 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
3085 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
3088 /* Only add the channel if successful on the tracer side. */
3089 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3093 *ua_chanp
= ua_chan
;
3096 /* Everything went well. */
3100 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
3106 * Create UST app event and create it on the tracer side.
3108 * Called with ust app session mutex held.
3111 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3112 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3113 struct ust_app
*app
)
3116 struct ust_app_event
*ua_event
;
3118 /* Get event node */
3119 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3120 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3121 if (ua_event
!= NULL
) {
3126 /* Does not exist so create one */
3127 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3128 if (ua_event
== NULL
) {
3129 /* Only malloc can failed so something is really wrong */
3133 shadow_copy_event(ua_event
, uevent
);
3135 /* Create it on the tracer side */
3136 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3138 /* Not found previously means that it does not exist on the tracer */
3139 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
3143 add_unique_ust_app_event(ua_chan
, ua_event
);
3145 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3152 /* Valid. Calling here is already in a read side lock */
3153 delete_ust_app_event(-1, ua_event
, app
);
3158 * Create UST metadata and open it on the tracer side.
3160 * Called with UST app session lock held and RCU read side lock.
3162 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3163 struct ust_app
*app
, struct consumer_output
*consumer
)
3166 struct ust_app_channel
*metadata
;
3167 struct consumer_socket
*socket
;
3168 struct ust_registry_session
*registry
;
3174 registry
= get_session_registry(ua_sess
);
3175 /* The UST app session is held registry shall not be null. */
3178 pthread_mutex_lock(®istry
->lock
);
3180 /* Metadata already exists for this registry or it was closed previously */
3181 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3186 /* Allocate UST metadata */
3187 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3189 /* malloc() failed */
3194 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3196 /* Need one fd for the channel. */
3197 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3199 ERR("Exhausted number of available FD upon create metadata");
3203 /* Get the right consumer socket for the application. */
3204 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3207 goto error_consumer
;
3211 * Keep metadata key so we can identify it on the consumer side. Assign it
3212 * to the registry *before* we ask the consumer so we avoid the race of the
3213 * consumer requesting the metadata and the ask_channel call on our side
3214 * did not returned yet.
3216 registry
->metadata_key
= metadata
->key
;
3219 * Ask the metadata channel creation to the consumer. The metadata object
3220 * will be created by the consumer and kept their. However, the stream is
3221 * never added or monitored until we do a first push metadata to the
3224 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3227 /* Nullify the metadata key so we don't try to close it later on. */
3228 registry
->metadata_key
= 0;
3229 goto error_consumer
;
3233 * The setup command will make the metadata stream be sent to the relayd,
3234 * if applicable, and the thread managing the metadatas. This is important
3235 * because after this point, if an error occurs, the only way the stream
3236 * can be deleted is to be monitored in the consumer.
3238 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3240 /* Nullify the metadata key so we don't try to close it later on. */
3241 registry
->metadata_key
= 0;
3242 goto error_consumer
;
3245 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3246 metadata
->key
, app
->pid
);
3249 lttng_fd_put(LTTNG_FD_APPS
, 1);
3250 delete_ust_app_channel(-1, metadata
, app
);
3252 pthread_mutex_unlock(®istry
->lock
);
3257 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3258 * acquired before calling this function.
3260 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3262 struct ust_app
*app
= NULL
;
3263 struct lttng_ht_node_ulong
*node
;
3264 struct lttng_ht_iter iter
;
3266 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3267 node
= lttng_ht_iter_get_node_ulong(&iter
);
3269 DBG2("UST app no found with pid %d", pid
);
3273 DBG2("Found UST app by pid %d", pid
);
3275 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3282 * Allocate and init an UST app object using the registration information and
3283 * the command socket. This is called when the command socket connects to the
3286 * The object is returned on success or else NULL.
3288 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3290 struct ust_app
*lta
= NULL
;
3295 DBG3("UST app creating application for socket %d", sock
);
3297 if ((msg
->bits_per_long
== 64 &&
3298 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3299 || (msg
->bits_per_long
== 32 &&
3300 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3301 ERR("Registration failed: application \"%s\" (pid: %d) has "
3302 "%d-bit long, but no consumerd for this size is available.\n",
3303 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3307 lta
= zmalloc(sizeof(struct ust_app
));
3313 lta
->ppid
= msg
->ppid
;
3314 lta
->uid
= msg
->uid
;
3315 lta
->gid
= msg
->gid
;
3317 lta
->bits_per_long
= msg
->bits_per_long
;
3318 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3319 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3320 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3321 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3322 lta
->long_alignment
= msg
->long_alignment
;
3323 lta
->byte_order
= msg
->byte_order
;
3325 lta
->v_major
= msg
->major
;
3326 lta
->v_minor
= msg
->minor
;
3327 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3328 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3329 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3330 lta
->notify_sock
= -1;
3332 /* Copy name and make sure it's NULL terminated. */
3333 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3334 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3337 * Before this can be called, when receiving the registration information,
3338 * the application compatibility is checked. So, at this point, the
3339 * application can work with this session daemon.
3341 lta
->compatible
= 1;
3343 lta
->pid
= msg
->pid
;
3344 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3346 pthread_mutex_init(<a
->sock_lock
, NULL
);
3347 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3349 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3355 * For a given application object, add it to every hash table.
3357 void ust_app_add(struct ust_app
*app
)
3360 assert(app
->notify_sock
>= 0);
3365 * On a re-registration, we want to kick out the previous registration of
3368 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3371 * The socket _should_ be unique until _we_ call close. So, a add_unique
3372 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3373 * already in the table.
3375 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3377 /* Add application to the notify socket hash table. */
3378 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3379 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3381 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3382 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3383 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3390 * Set the application version into the object.
3392 * Return 0 on success else a negative value either an errno code or a
3393 * LTTng-UST error code.
3395 int ust_app_version(struct ust_app
*app
)
3401 pthread_mutex_lock(&app
->sock_lock
);
3402 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3403 pthread_mutex_unlock(&app
->sock_lock
);
3405 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3406 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3408 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3416 * Unregister app by removing it from the global traceable app list and freeing
3419 * The socket is already closed at this point so no close to sock.
3421 void ust_app_unregister(int sock
)
3423 struct ust_app
*lta
;
3424 struct lttng_ht_node_ulong
*node
;
3425 struct lttng_ht_iter ust_app_sock_iter
;
3426 struct lttng_ht_iter iter
;
3427 struct ust_app_session
*ua_sess
;
3432 /* Get the node reference for a call_rcu */
3433 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3434 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3437 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3438 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3441 * For per-PID buffers, perform "push metadata" and flush all
3442 * application streams before removing app from hash tables,
3443 * ensuring proper behavior of data_pending check.
3444 * Remove sessions so they are not visible during deletion.
3446 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3448 struct ust_registry_session
*registry
;
3450 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3452 /* The session was already removed so scheduled for teardown. */
3456 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3457 (void) ust_app_flush_app_session(lta
, ua_sess
);
3461 * Add session to list for teardown. This is safe since at this point we
3462 * are the only one using this list.
3464 pthread_mutex_lock(&ua_sess
->lock
);
3466 if (ua_sess
->deleted
) {
3467 pthread_mutex_unlock(&ua_sess
->lock
);
3472 * Normally, this is done in the delete session process which is
3473 * executed in the call rcu below. However, upon registration we can't
3474 * afford to wait for the grace period before pushing data or else the
3475 * data pending feature can race between the unregistration and stop
3476 * command where the data pending command is sent *before* the grace
3479 * The close metadata below nullifies the metadata pointer in the
3480 * session so the delete session will NOT push/close a second time.
3482 registry
= get_session_registry(ua_sess
);
3484 /* Push metadata for application before freeing the application. */
3485 (void) push_metadata(registry
, ua_sess
->consumer
);
3488 * Don't ask to close metadata for global per UID buffers. Close
3489 * metadata only on destroy trace session in this case. Also, the
3490 * previous push metadata could have flag the metadata registry to
3491 * close so don't send a close command if closed.
3493 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3494 /* And ask to close it for this session registry. */
3495 (void) close_metadata(registry
, ua_sess
->consumer
);
3498 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3500 pthread_mutex_unlock(&ua_sess
->lock
);
3503 /* Remove application from PID hash table */
3504 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3508 * Remove application from notify hash table. The thread handling the
3509 * notify socket could have deleted the node so ignore on error because
3510 * either way it's valid. The close of that socket is handled by the other
3513 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3514 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3517 * Ignore return value since the node might have been removed before by an
3518 * add replace during app registration because the PID can be reassigned by
3521 iter
.iter
.node
= <a
->pid_n
.node
;
3522 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3524 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3529 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3536 * Fill events array with all events name of all registered apps.
3538 int ust_app_list_events(struct lttng_event
**events
)
3541 size_t nbmem
, count
= 0;
3542 struct lttng_ht_iter iter
;
3543 struct ust_app
*app
;
3544 struct lttng_event
*tmp_event
;
3546 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3547 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3548 if (tmp_event
== NULL
) {
3549 PERROR("zmalloc ust app events");
3556 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3557 struct lttng_ust_tracepoint_iter uiter
;
3559 health_code_update();
3561 if (!app
->compatible
) {
3563 * TODO: In time, we should notice the caller of this error by
3564 * telling him that this is a version error.
3568 pthread_mutex_lock(&app
->sock_lock
);
3569 handle
= ustctl_tracepoint_list(app
->sock
);
3571 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3572 ERR("UST app list events getting handle failed for app pid %d",
3575 pthread_mutex_unlock(&app
->sock_lock
);
3579 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3580 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3581 /* Handle ustctl error. */
3585 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3586 ERR("UST app tp list get failed for app %d with ret %d",
3589 DBG3("UST app tp list get failed. Application is dead");
3591 * This is normal behavior, an application can die during the
3592 * creation process. Don't report an error so the execution can
3593 * continue normally. Continue normal execution.
3598 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3599 if (release_ret
< 0 &&
3600 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3601 release_ret
!= -EPIPE
) {
3602 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3604 pthread_mutex_unlock(&app
->sock_lock
);
3608 health_code_update();
3609 if (count
>= nbmem
) {
3610 /* In case the realloc fails, we free the memory */
3611 struct lttng_event
*new_tmp_event
;
3614 new_nbmem
= nbmem
<< 1;
3615 DBG2("Reallocating event list from %zu to %zu entries",
3617 new_tmp_event
= realloc(tmp_event
,
3618 new_nbmem
* sizeof(struct lttng_event
));
3619 if (new_tmp_event
== NULL
) {
3622 PERROR("realloc ust app events");
3625 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3626 if (release_ret
< 0 &&
3627 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3628 release_ret
!= -EPIPE
) {
3629 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3631 pthread_mutex_unlock(&app
->sock_lock
);
3634 /* Zero the new memory */
3635 memset(new_tmp_event
+ nbmem
, 0,
3636 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3638 tmp_event
= new_tmp_event
;
3640 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3641 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3642 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3643 tmp_event
[count
].pid
= app
->pid
;
3644 tmp_event
[count
].enabled
= -1;
3647 ret
= ustctl_release_handle(app
->sock
, handle
);
3648 pthread_mutex_unlock(&app
->sock_lock
);
3649 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3650 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3655 *events
= tmp_event
;
3657 DBG2("UST app list events done (%zu events)", count
);
3662 health_code_update();
3667 * Fill events array with all events name of all registered apps.
3669 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3672 size_t nbmem
, count
= 0;
3673 struct lttng_ht_iter iter
;
3674 struct ust_app
*app
;
3675 struct lttng_event_field
*tmp_event
;
3677 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3678 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3679 if (tmp_event
== NULL
) {
3680 PERROR("zmalloc ust app event fields");
3687 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3688 struct lttng_ust_field_iter uiter
;
3690 health_code_update();
3692 if (!app
->compatible
) {
3694 * TODO: In time, we should notice the caller of this error by
3695 * telling him that this is a version error.
3699 pthread_mutex_lock(&app
->sock_lock
);
3700 handle
= ustctl_tracepoint_field_list(app
->sock
);
3702 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3703 ERR("UST app list field getting handle failed for app pid %d",
3706 pthread_mutex_unlock(&app
->sock_lock
);
3710 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3711 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3712 /* Handle ustctl error. */
3716 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3717 ERR("UST app tp list field failed for app %d with ret %d",
3720 DBG3("UST app tp list field failed. Application is dead");
3722 * This is normal behavior, an application can die during the
3723 * creation process. Don't report an error so the execution can
3724 * continue normally. Reset list and count for next app.
3729 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3730 pthread_mutex_unlock(&app
->sock_lock
);
3731 if (release_ret
< 0 &&
3732 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3733 release_ret
!= -EPIPE
) {
3734 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3739 health_code_update();
3740 if (count
>= nbmem
) {
3741 /* In case the realloc fails, we free the memory */
3742 struct lttng_event_field
*new_tmp_event
;
3745 new_nbmem
= nbmem
<< 1;
3746 DBG2("Reallocating event field list from %zu to %zu entries",
3748 new_tmp_event
= realloc(tmp_event
,
3749 new_nbmem
* sizeof(struct lttng_event_field
));
3750 if (new_tmp_event
== NULL
) {
3753 PERROR("realloc ust app event fields");
3756 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3757 pthread_mutex_unlock(&app
->sock_lock
);
3759 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3760 release_ret
!= -EPIPE
) {
3761 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3765 /* Zero the new memory */
3766 memset(new_tmp_event
+ nbmem
, 0,
3767 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3769 tmp_event
= new_tmp_event
;
3772 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3773 /* Mapping between these enums matches 1 to 1. */
3774 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3775 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3777 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3778 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3779 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3780 tmp_event
[count
].event
.pid
= app
->pid
;
3781 tmp_event
[count
].event
.enabled
= -1;
3784 ret
= ustctl_release_handle(app
->sock
, handle
);
3785 pthread_mutex_unlock(&app
->sock_lock
);
3787 ret
!= -LTTNG_UST_ERR_EXITING
&&
3789 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3794 *fields
= tmp_event
;
3796 DBG2("UST app list event fields done (%zu events)", count
);
3801 health_code_update();
3806 * Free and clean all traceable apps of the global list.
3808 * Should _NOT_ be called with RCU read-side lock held.
3810 void ust_app_clean_list(void)
3813 struct ust_app
*app
;
3814 struct lttng_ht_iter iter
;
3816 DBG2("UST app cleaning registered apps hash table");
3821 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3822 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3824 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3828 /* Cleanup socket hash table */
3829 if (ust_app_ht_by_sock
) {
3830 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3832 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3837 /* Cleanup notify socket hash table */
3838 if (ust_app_ht_by_notify_sock
) {
3839 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3840 notify_sock_n
.node
) {
3841 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3847 /* Destroy is done only when the ht is empty */
3849 ht_cleanup_push(ust_app_ht
);
3851 if (ust_app_ht_by_sock
) {
3852 ht_cleanup_push(ust_app_ht_by_sock
);
3854 if (ust_app_ht_by_notify_sock
) {
3855 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3860 * Init UST app hash table.
3862 int ust_app_ht_alloc(void)
3864 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3868 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3869 if (!ust_app_ht_by_sock
) {
3872 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3873 if (!ust_app_ht_by_notify_sock
) {
3880 * For a specific UST session, disable the channel for all registered apps.
3882 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3883 struct ltt_ust_channel
*uchan
)
3886 struct lttng_ht_iter iter
;
3887 struct lttng_ht_node_str
*ua_chan_node
;
3888 struct ust_app
*app
;
3889 struct ust_app_session
*ua_sess
;
3890 struct ust_app_channel
*ua_chan
;
3892 if (usess
== NULL
|| uchan
== NULL
) {
3893 ERR("Disabling UST global channel with NULL values");
3898 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3899 uchan
->name
, usess
->id
);
3903 /* For every registered applications */
3904 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3905 struct lttng_ht_iter uiter
;
3906 if (!app
->compatible
) {
3908 * TODO: In time, we should notice the caller of this error by
3909 * telling him that this is a version error.
3913 ua_sess
= lookup_session_by_app(usess
, app
);
3914 if (ua_sess
== NULL
) {
3919 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3920 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3921 /* If the session if found for the app, the channel must be there */
3922 assert(ua_chan_node
);
3924 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3925 /* The channel must not be already disabled */
3926 assert(ua_chan
->enabled
== 1);
3928 /* Disable channel onto application */
3929 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3931 /* XXX: We might want to report this error at some point... */
3943 * For a specific UST session, enable the channel for all registered apps.
3945 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3946 struct ltt_ust_channel
*uchan
)
3949 struct lttng_ht_iter iter
;
3950 struct ust_app
*app
;
3951 struct ust_app_session
*ua_sess
;
3953 if (usess
== NULL
|| uchan
== NULL
) {
3954 ERR("Adding UST global channel to NULL values");
3959 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3960 uchan
->name
, usess
->id
);
3964 /* For every registered applications */
3965 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3966 if (!app
->compatible
) {
3968 * TODO: In time, we should notice the caller of this error by
3969 * telling him that this is a version error.
3973 ua_sess
= lookup_session_by_app(usess
, app
);
3974 if (ua_sess
== NULL
) {
3978 /* Enable channel onto application */
3979 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3981 /* XXX: We might want to report this error at some point... */
3993 * Disable an event in a channel and for a specific session.
3995 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3996 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3999 struct lttng_ht_iter iter
, uiter
;
4000 struct lttng_ht_node_str
*ua_chan_node
;
4001 struct ust_app
*app
;
4002 struct ust_app_session
*ua_sess
;
4003 struct ust_app_channel
*ua_chan
;
4004 struct ust_app_event
*ua_event
;
4006 DBG("UST app disabling event %s for all apps in channel "
4007 "%s for session id %" PRIu64
,
4008 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4012 /* For all registered applications */
4013 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4014 if (!app
->compatible
) {
4016 * TODO: In time, we should notice the caller of this error by
4017 * telling him that this is a version error.
4021 ua_sess
= lookup_session_by_app(usess
, app
);
4022 if (ua_sess
== NULL
) {
4027 /* Lookup channel in the ust app session */
4028 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4029 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4030 if (ua_chan_node
== NULL
) {
4031 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4032 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4035 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4037 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4038 uevent
->filter
, uevent
->attr
.loglevel
,
4040 if (ua_event
== NULL
) {
4041 DBG2("Event %s not found in channel %s for app pid %d."
4042 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4046 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4048 /* XXX: Report error someday... */
4059 * For a specific UST session, create the channel for all registered apps.
4061 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
4062 struct ltt_ust_channel
*uchan
)
4064 int ret
= 0, created
;
4065 struct lttng_ht_iter iter
;
4066 struct ust_app
*app
;
4067 struct ust_app_session
*ua_sess
= NULL
;
4069 /* Very wrong code flow */
4073 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
4074 uchan
->name
, usess
->id
);
4078 /* For every registered applications */
4079 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4080 if (!app
->compatible
) {
4082 * TODO: In time, we should notice the caller of this error by
4083 * telling him that this is a version error.
4087 if (!trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4093 * Create session on the tracer side and add it to app session HT. Note
4094 * that if session exist, it will simply return a pointer to the ust
4097 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
4102 * The application's socket is not valid. Either a bad socket
4103 * or a timeout on it. We can't inform the caller that for a
4104 * specific app, the session failed so lets continue here.
4106 ret
= 0; /* Not an error. */
4110 goto error_rcu_unlock
;
4115 pthread_mutex_lock(&ua_sess
->lock
);
4117 if (ua_sess
->deleted
) {
4118 pthread_mutex_unlock(&ua_sess
->lock
);
4122 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4123 sizeof(uchan
->name
))) {
4124 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
4127 /* Create channel onto application. We don't need the chan ref. */
4128 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
4129 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
4131 pthread_mutex_unlock(&ua_sess
->lock
);
4133 /* Cleanup the created session if it's the case. */
4135 destroy_app_session(app
, ua_sess
);
4140 * The application's socket is not valid. Either a bad socket
4141 * or a timeout on it. We can't inform the caller that for a
4142 * specific app, the session failed so lets continue here.
4144 ret
= 0; /* Not an error. */
4148 goto error_rcu_unlock
;
4159 * Enable event for a specific session and channel on the tracer.
4161 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4162 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4165 struct lttng_ht_iter iter
, uiter
;
4166 struct lttng_ht_node_str
*ua_chan_node
;
4167 struct ust_app
*app
;
4168 struct ust_app_session
*ua_sess
;
4169 struct ust_app_channel
*ua_chan
;
4170 struct ust_app_event
*ua_event
;
4172 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4173 uevent
->attr
.name
, usess
->id
);
4176 * NOTE: At this point, this function is called only if the session and
4177 * channel passed are already created for all apps. and enabled on the
4183 /* For all registered applications */
4184 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4185 if (!app
->compatible
) {
4187 * TODO: In time, we should notice the caller of this error by
4188 * telling him that this is a version error.
4192 ua_sess
= lookup_session_by_app(usess
, app
);
4194 /* The application has problem or is probably dead. */
4198 pthread_mutex_lock(&ua_sess
->lock
);
4200 if (ua_sess
->deleted
) {
4201 pthread_mutex_unlock(&ua_sess
->lock
);
4205 /* Lookup channel in the ust app session */
4206 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4207 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4209 * It is possible that the channel cannot be found is
4210 * the channel/event creation occurs concurrently with
4211 * an application exit.
4213 if (!ua_chan_node
) {
4214 pthread_mutex_unlock(&ua_sess
->lock
);
4218 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4220 /* Get event node */
4221 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4222 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4223 if (ua_event
== NULL
) {
4224 DBG3("UST app enable event %s not found for app PID %d."
4225 "Skipping app", uevent
->attr
.name
, app
->pid
);
4229 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4231 pthread_mutex_unlock(&ua_sess
->lock
);
4235 pthread_mutex_unlock(&ua_sess
->lock
);
4244 * For a specific existing UST session and UST channel, creates the event for
4245 * all registered apps.
4247 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4248 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4251 struct lttng_ht_iter iter
, uiter
;
4252 struct lttng_ht_node_str
*ua_chan_node
;
4253 struct ust_app
*app
;
4254 struct ust_app_session
*ua_sess
;
4255 struct ust_app_channel
*ua_chan
;
4257 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4258 uevent
->attr
.name
, usess
->id
);
4262 /* For all registered applications */
4263 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4264 if (!app
->compatible
) {
4266 * TODO: In time, we should notice the caller of this error by
4267 * telling him that this is a version error.
4271 ua_sess
= lookup_session_by_app(usess
, app
);
4273 /* The application has problem or is probably dead. */
4277 pthread_mutex_lock(&ua_sess
->lock
);
4279 if (ua_sess
->deleted
) {
4280 pthread_mutex_unlock(&ua_sess
->lock
);
4284 /* Lookup channel in the ust app session */
4285 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4286 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4287 /* If the channel is not found, there is a code flow error */
4288 assert(ua_chan_node
);
4290 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4292 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4293 pthread_mutex_unlock(&ua_sess
->lock
);
4295 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4296 /* Possible value at this point: -ENOMEM. If so, we stop! */
4299 DBG2("UST app event %s already exist on app PID %d",
4300 uevent
->attr
.name
, app
->pid
);
4311 * Start tracing for a specific UST session and app.
4313 * Called with UST app session lock held.
4317 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4320 struct ust_app_session
*ua_sess
;
4322 DBG("Starting tracing for ust app pid %d", app
->pid
);
4326 if (!app
->compatible
) {
4330 ua_sess
= lookup_session_by_app(usess
, app
);
4331 if (ua_sess
== NULL
) {
4332 /* The session is in teardown process. Ignore and continue. */
4336 pthread_mutex_lock(&ua_sess
->lock
);
4338 if (ua_sess
->deleted
) {
4339 pthread_mutex_unlock(&ua_sess
->lock
);
4343 /* Upon restart, we skip the setup, already done */
4344 if (ua_sess
->started
) {
4348 /* Create directories if consumer is LOCAL and has a path defined. */
4349 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
4350 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
4351 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
4352 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
4354 if (errno
!= EEXIST
) {
4355 ERR("Trace directory creation error");
4362 * Create the metadata for the application. This returns gracefully if a
4363 * metadata was already set for the session.
4365 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4370 health_code_update();
4373 /* This start the UST tracing */
4374 pthread_mutex_lock(&app
->sock_lock
);
4375 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4376 pthread_mutex_unlock(&app
->sock_lock
);
4378 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4379 ERR("Error starting tracing for app pid: %d (ret: %d)",
4382 DBG("UST app start session failed. Application is dead.");
4384 * This is normal behavior, an application can die during the
4385 * creation process. Don't report an error so the execution can
4386 * continue normally.
4388 pthread_mutex_unlock(&ua_sess
->lock
);
4394 /* Indicate that the session has been started once */
4395 ua_sess
->started
= 1;
4397 pthread_mutex_unlock(&ua_sess
->lock
);
4399 health_code_update();
4401 /* Quiescent wait after starting trace */
4402 pthread_mutex_lock(&app
->sock_lock
);
4403 ret
= ustctl_wait_quiescent(app
->sock
);
4404 pthread_mutex_unlock(&app
->sock_lock
);
4405 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4406 ERR("UST app wait quiescent failed for app pid %d ret %d",
4412 health_code_update();
4416 pthread_mutex_unlock(&ua_sess
->lock
);
4418 health_code_update();
4423 * Stop tracing for a specific UST session and app.
4426 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4429 struct ust_app_session
*ua_sess
;
4430 struct ust_registry_session
*registry
;
4432 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4436 if (!app
->compatible
) {
4437 goto end_no_session
;
4440 ua_sess
= lookup_session_by_app(usess
, app
);
4441 if (ua_sess
== NULL
) {
4442 goto end_no_session
;
4445 pthread_mutex_lock(&ua_sess
->lock
);
4447 if (ua_sess
->deleted
) {
4448 pthread_mutex_unlock(&ua_sess
->lock
);
4449 goto end_no_session
;
4453 * If started = 0, it means that stop trace has been called for a session
4454 * that was never started. It's possible since we can have a fail start
4455 * from either the application manager thread or the command thread. Simply
4456 * indicate that this is a stop error.
4458 if (!ua_sess
->started
) {
4459 goto error_rcu_unlock
;
4462 health_code_update();
4464 /* This inhibits UST tracing */
4465 pthread_mutex_lock(&app
->sock_lock
);
4466 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4467 pthread_mutex_unlock(&app
->sock_lock
);
4469 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4470 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4473 DBG("UST app stop session failed. Application is dead.");
4475 * This is normal behavior, an application can die during the
4476 * creation process. Don't report an error so the execution can
4477 * continue normally.
4481 goto error_rcu_unlock
;
4484 health_code_update();
4486 /* Quiescent wait after stopping trace */
4487 pthread_mutex_lock(&app
->sock_lock
);
4488 ret
= ustctl_wait_quiescent(app
->sock
);
4489 pthread_mutex_unlock(&app
->sock_lock
);
4490 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4491 ERR("UST app wait quiescent failed for app pid %d ret %d",
4495 health_code_update();
4497 registry
= get_session_registry(ua_sess
);
4499 /* The UST app session is held registry shall not be null. */
4502 /* Push metadata for application before freeing the application. */
4503 (void) push_metadata(registry
, ua_sess
->consumer
);
4506 pthread_mutex_unlock(&ua_sess
->lock
);
4509 health_code_update();
4513 pthread_mutex_unlock(&ua_sess
->lock
);
4515 health_code_update();
4520 int ust_app_flush_app_session(struct ust_app
*app
,
4521 struct ust_app_session
*ua_sess
)
4523 int ret
, retval
= 0;
4524 struct lttng_ht_iter iter
;
4525 struct ust_app_channel
*ua_chan
;
4526 struct consumer_socket
*socket
;
4528 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4532 if (!app
->compatible
) {
4533 goto end_not_compatible
;
4536 pthread_mutex_lock(&ua_sess
->lock
);
4538 if (ua_sess
->deleted
) {
4542 health_code_update();
4544 /* Flushing buffers */
4545 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4548 /* Flush buffers and push metadata. */
4549 switch (ua_sess
->buffer_type
) {
4550 case LTTNG_BUFFER_PER_PID
:
4551 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4553 health_code_update();
4554 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4556 ERR("Error flushing consumer channel");
4562 case LTTNG_BUFFER_PER_UID
:
4568 health_code_update();
4571 pthread_mutex_unlock(&ua_sess
->lock
);
4575 health_code_update();
4580 * Flush buffers for all applications for a specific UST session.
4581 * Called with UST session lock held.
4584 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4589 DBG("Flushing session buffers for all ust apps");
4593 /* Flush buffers and push metadata. */
4594 switch (usess
->buffer_type
) {
4595 case LTTNG_BUFFER_PER_UID
:
4597 struct buffer_reg_uid
*reg
;
4598 struct lttng_ht_iter iter
;
4600 /* Flush all per UID buffers associated to that session. */
4601 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4602 struct ust_registry_session
*ust_session_reg
;
4603 struct buffer_reg_channel
*reg_chan
;
4604 struct consumer_socket
*socket
;
4606 /* Get consumer socket to use to push the metadata.*/
4607 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4610 /* Ignore request if no consumer is found for the session. */
4614 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4615 reg_chan
, node
.node
) {
4617 * The following call will print error values so the return
4618 * code is of little importance because whatever happens, we
4619 * have to try them all.
4621 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4624 ust_session_reg
= reg
->registry
->reg
.ust
;
4625 /* Push metadata. */
4626 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4630 case LTTNG_BUFFER_PER_PID
:
4632 struct ust_app_session
*ua_sess
;
4633 struct lttng_ht_iter iter
;
4634 struct ust_app
*app
;
4636 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4637 ua_sess
= lookup_session_by_app(usess
, app
);
4638 if (ua_sess
== NULL
) {
4641 (void) ust_app_flush_app_session(app
, ua_sess
);
4652 health_code_update();
4657 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
4658 struct ust_app_session
*ua_sess
)
4661 struct lttng_ht_iter iter
;
4662 struct ust_app_channel
*ua_chan
;
4663 struct consumer_socket
*socket
;
4665 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
4669 if (!app
->compatible
) {
4670 goto end_not_compatible
;
4673 pthread_mutex_lock(&ua_sess
->lock
);
4675 if (ua_sess
->deleted
) {
4679 health_code_update();
4681 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4684 ERR("Failed to find consumer (%" PRIu32
") socket",
4685 app
->bits_per_long
);
4690 /* Clear quiescent state. */
4691 switch (ua_sess
->buffer_type
) {
4692 case LTTNG_BUFFER_PER_PID
:
4693 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
4694 ua_chan
, node
.node
) {
4695 health_code_update();
4696 ret
= consumer_clear_quiescent_channel(socket
,
4699 ERR("Error clearing quiescent state for consumer channel");
4705 case LTTNG_BUFFER_PER_UID
:
4712 health_code_update();
4715 pthread_mutex_unlock(&ua_sess
->lock
);
4719 health_code_update();
4724 * Clear quiescent state in each stream for all applications for a
4725 * specific UST session.
4726 * Called with UST session lock held.
4729 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
4734 DBG("Clearing stream quiescent state for all ust apps");
4738 switch (usess
->buffer_type
) {
4739 case LTTNG_BUFFER_PER_UID
:
4741 struct lttng_ht_iter iter
;
4742 struct buffer_reg_uid
*reg
;
4745 * Clear quiescent for all per UID buffers associated to
4748 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4749 struct consumer_socket
*socket
;
4750 struct buffer_reg_channel
*reg_chan
;
4752 /* Get associated consumer socket.*/
4753 socket
= consumer_find_socket_by_bitness(
4754 reg
->bits_per_long
, usess
->consumer
);
4757 * Ignore request if no consumer is found for
4763 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
4764 &iter
.iter
, reg_chan
, node
.node
) {
4766 * The following call will print error values so
4767 * the return code is of little importance
4768 * because whatever happens, we have to try them
4771 (void) consumer_clear_quiescent_channel(socket
,
4772 reg_chan
->consumer_key
);
4777 case LTTNG_BUFFER_PER_PID
:
4779 struct ust_app_session
*ua_sess
;
4780 struct lttng_ht_iter iter
;
4781 struct ust_app
*app
;
4783 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
4785 ua_sess
= lookup_session_by_app(usess
, app
);
4786 if (ua_sess
== NULL
) {
4789 (void) ust_app_clear_quiescent_app_session(app
,
4801 health_code_update();
4806 * Destroy a specific UST session in apps.
4808 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4811 struct ust_app_session
*ua_sess
;
4812 struct lttng_ht_iter iter
;
4813 struct lttng_ht_node_u64
*node
;
4815 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4819 if (!app
->compatible
) {
4823 __lookup_session_by_app(usess
, app
, &iter
);
4824 node
= lttng_ht_iter_get_node_u64(&iter
);
4826 /* Session is being or is deleted. */
4829 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4831 health_code_update();
4832 destroy_app_session(app
, ua_sess
);
4834 health_code_update();
4836 /* Quiescent wait after stopping trace */
4837 pthread_mutex_lock(&app
->sock_lock
);
4838 ret
= ustctl_wait_quiescent(app
->sock
);
4839 pthread_mutex_unlock(&app
->sock_lock
);
4840 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4841 ERR("UST app wait quiescent failed for app pid %d ret %d",
4846 health_code_update();
4851 * Start tracing for the UST session.
4853 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4856 struct lttng_ht_iter iter
;
4857 struct ust_app
*app
;
4859 DBG("Starting all UST traces");
4864 * In a start-stop-start use-case, we need to clear the quiescent state
4865 * of each channel set by the prior stop command, thus ensuring that a
4866 * following stop or destroy is sure to grab a timestamp_end near those
4867 * operations, even if the packet is empty.
4869 (void) ust_app_clear_quiescent_session(usess
);
4871 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4872 ret
= ust_app_start_trace(usess
, app
);
4874 /* Continue to next apps even on error */
4885 * Start tracing for the UST session.
4886 * Called with UST session lock held.
4888 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4891 struct lttng_ht_iter iter
;
4892 struct ust_app
*app
;
4894 DBG("Stopping all UST traces");
4898 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4899 ret
= ust_app_stop_trace(usess
, app
);
4901 /* Continue to next apps even on error */
4906 (void) ust_app_flush_session(usess
);
4914 * Destroy app UST session.
4916 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4919 struct lttng_ht_iter iter
;
4920 struct ust_app
*app
;
4922 DBG("Destroy all UST traces");
4926 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4927 ret
= destroy_trace(usess
, app
);
4929 /* Continue to next apps even on error */
4940 void ust_app_global_create(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4943 struct lttng_ht_iter iter
, uiter
;
4944 struct ust_app_session
*ua_sess
= NULL
;
4945 struct ust_app_channel
*ua_chan
;
4946 struct ust_app_event
*ua_event
;
4947 struct ust_app_ctx
*ua_ctx
;
4950 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &is_created
);
4952 /* Tracer is probably gone or ENOMEM. */
4956 /* App session already created. */
4961 pthread_mutex_lock(&ua_sess
->lock
);
4963 if (ua_sess
->deleted
) {
4964 pthread_mutex_unlock(&ua_sess
->lock
);
4969 * We can iterate safely here over all UST app session since the create ust
4970 * app session above made a shadow copy of the UST global domain from the
4973 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4975 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4976 if (ret
< 0 && ret
!= -ENOTCONN
) {
4978 * Stop everything. On error, the application
4979 * failed, no more file descriptor are available
4980 * or ENOMEM so stopping here is the only thing
4981 * we can do for now. The only exception is
4982 * -ENOTCONN, which indicates that the application
4989 * Add context using the list so they are enabled in the same order the
4992 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
4993 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
5000 /* For each events */
5001 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
5003 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
5010 pthread_mutex_unlock(&ua_sess
->lock
);
5012 if (usess
->active
) {
5013 ret
= ust_app_start_trace(usess
, app
);
5018 DBG2("UST trace started for app pid %d", app
->pid
);
5021 /* Everything went well at this point. */
5025 pthread_mutex_unlock(&ua_sess
->lock
);
5028 destroy_app_session(app
, ua_sess
);
5034 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5036 struct ust_app_session
*ua_sess
;
5038 ua_sess
= lookup_session_by_app(usess
, app
);
5039 if (ua_sess
== NULL
) {
5042 destroy_app_session(app
, ua_sess
);
5046 * Add channels/events from UST global domain to registered apps at sock.
5048 * Called with session lock held.
5049 * Called with RCU read-side lock held.
5051 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5055 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5056 app
->sock
, usess
->id
);
5058 if (!app
->compatible
) {
5062 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
5063 ust_app_global_create(usess
, app
);
5065 ust_app_global_destroy(usess
, app
);
5070 * Called with session lock held.
5072 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5074 struct lttng_ht_iter iter
;
5075 struct ust_app
*app
;
5078 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5079 ust_app_global_update(usess
, app
);
5085 * Add context to a specific channel for global UST domain.
5087 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5088 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5091 struct lttng_ht_node_str
*ua_chan_node
;
5092 struct lttng_ht_iter iter
, uiter
;
5093 struct ust_app_channel
*ua_chan
= NULL
;
5094 struct ust_app_session
*ua_sess
;
5095 struct ust_app
*app
;
5099 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5100 if (!app
->compatible
) {
5102 * TODO: In time, we should notice the caller of this error by
5103 * telling him that this is a version error.
5107 ua_sess
= lookup_session_by_app(usess
, app
);
5108 if (ua_sess
== NULL
) {
5112 pthread_mutex_lock(&ua_sess
->lock
);
5114 if (ua_sess
->deleted
) {
5115 pthread_mutex_unlock(&ua_sess
->lock
);
5119 /* Lookup channel in the ust app session */
5120 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5121 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5122 if (ua_chan_node
== NULL
) {
5125 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5127 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
5132 pthread_mutex_unlock(&ua_sess
->lock
);
5140 * Enable event for a channel from a UST session for a specific PID.
5142 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
5143 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
5146 struct lttng_ht_iter iter
;
5147 struct lttng_ht_node_str
*ua_chan_node
;
5148 struct ust_app
*app
;
5149 struct ust_app_session
*ua_sess
;
5150 struct ust_app_channel
*ua_chan
;
5151 struct ust_app_event
*ua_event
;
5153 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
5157 app
= ust_app_find_by_pid(pid
);
5159 ERR("UST app enable event per PID %d not found", pid
);
5164 if (!app
->compatible
) {
5169 ua_sess
= lookup_session_by_app(usess
, app
);
5171 /* The application has problem or is probably dead. */
5176 pthread_mutex_lock(&ua_sess
->lock
);
5178 if (ua_sess
->deleted
) {
5183 /* Lookup channel in the ust app session */
5184 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
5185 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5186 /* If the channel is not found, there is a code flow error */
5187 assert(ua_chan_node
);
5189 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5191 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5192 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5193 if (ua_event
== NULL
) {
5194 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5199 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
5206 pthread_mutex_unlock(&ua_sess
->lock
);
5213 * Receive registration and populate the given msg structure.
5215 * On success return 0 else a negative value returned by the ustctl call.
5217 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5220 uint32_t pid
, ppid
, uid
, gid
;
5224 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5225 &pid
, &ppid
, &uid
, &gid
,
5226 &msg
->bits_per_long
,
5227 &msg
->uint8_t_alignment
,
5228 &msg
->uint16_t_alignment
,
5229 &msg
->uint32_t_alignment
,
5230 &msg
->uint64_t_alignment
,
5231 &msg
->long_alignment
,
5238 case LTTNG_UST_ERR_EXITING
:
5239 DBG3("UST app recv reg message failed. Application died");
5241 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5242 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5243 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5244 LTTNG_UST_ABI_MINOR_VERSION
);
5247 ERR("UST app recv reg message failed with ret %d", ret
);
5252 msg
->pid
= (pid_t
) pid
;
5253 msg
->ppid
= (pid_t
) ppid
;
5254 msg
->uid
= (uid_t
) uid
;
5255 msg
->gid
= (gid_t
) gid
;
5262 * Return a ust app session object using the application object and the
5263 * session object descriptor has a key. If not found, NULL is returned.
5264 * A RCU read side lock MUST be acquired when calling this function.
5266 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5269 struct lttng_ht_node_ulong
*node
;
5270 struct lttng_ht_iter iter
;
5271 struct ust_app_session
*ua_sess
= NULL
;
5275 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5276 node
= lttng_ht_iter_get_node_ulong(&iter
);
5278 DBG2("UST app session find by objd %d not found", objd
);
5282 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5289 * Return a ust app channel object using the application object and the channel
5290 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5291 * lock MUST be acquired before calling this function.
5293 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5296 struct lttng_ht_node_ulong
*node
;
5297 struct lttng_ht_iter iter
;
5298 struct ust_app_channel
*ua_chan
= NULL
;
5302 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5303 node
= lttng_ht_iter_get_node_ulong(&iter
);
5305 DBG2("UST app channel find by objd %d not found", objd
);
5309 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5316 * Reply to a register channel notification from an application on the notify
5317 * socket. The channel metadata is also created.
5319 * The session UST registry lock is acquired in this function.
5321 * On success 0 is returned else a negative value.
5323 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
5324 size_t nr_fields
, struct ustctl_field
*fields
)
5326 int ret
, ret_code
= 0;
5328 uint64_t chan_reg_key
;
5329 enum ustctl_channel_header type
;
5330 struct ust_app
*app
;
5331 struct ust_app_channel
*ua_chan
;
5332 struct ust_app_session
*ua_sess
;
5333 struct ust_registry_session
*registry
;
5334 struct ust_registry_channel
*chan_reg
;
5338 /* Lookup application. If not found, there is a code flow error. */
5339 app
= find_app_by_notify_sock(sock
);
5341 DBG("Application socket %d is being torn down. Abort event notify",
5344 goto error_rcu_unlock
;
5347 /* Lookup channel by UST object descriptor. */
5348 ua_chan
= find_channel_by_objd(app
, cobjd
);
5350 DBG("Application channel is being torn down. Abort event notify");
5352 goto error_rcu_unlock
;
5355 assert(ua_chan
->session
);
5356 ua_sess
= ua_chan
->session
;
5358 /* Get right session registry depending on the session buffer type. */
5359 registry
= get_session_registry(ua_sess
);
5361 DBG("Application session is being torn down. Abort event notify");
5363 goto error_rcu_unlock
;
5366 /* Depending on the buffer type, a different channel key is used. */
5367 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5368 chan_reg_key
= ua_chan
->tracing_channel_id
;
5370 chan_reg_key
= ua_chan
->key
;
5373 pthread_mutex_lock(®istry
->lock
);
5375 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5378 if (!chan_reg
->register_done
) {
5380 * TODO: eventually use the registry event count for
5381 * this channel to better guess header type for per-pid
5384 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5385 chan_reg
->nr_ctx_fields
= nr_fields
;
5386 chan_reg
->ctx_fields
= fields
;
5388 chan_reg
->header_type
= type
;
5390 /* Get current already assigned values. */
5391 type
= chan_reg
->header_type
;
5393 /* Channel id is set during the object creation. */
5394 chan_id
= chan_reg
->chan_id
;
5396 /* Append to metadata */
5397 if (!chan_reg
->metadata_dumped
) {
5398 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5400 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5406 DBG3("UST app replying to register channel key %" PRIu64
5407 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5410 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5412 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5413 ERR("UST app reply channel failed with ret %d", ret
);
5415 DBG3("UST app reply channel failed. Application died");
5420 /* This channel registry registration is completed. */
5421 chan_reg
->register_done
= 1;
5424 pthread_mutex_unlock(®istry
->lock
);
5432 * Add event to the UST channel registry. When the event is added to the
5433 * registry, the metadata is also created. Once done, this replies to the
5434 * application with the appropriate error code.
5436 * The session UST registry lock is acquired in the function.
5438 * On success 0 is returned else a negative value.
5440 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5441 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5442 int loglevel_value
, char *model_emf_uri
)
5445 uint32_t event_id
= 0;
5446 uint64_t chan_reg_key
;
5447 struct ust_app
*app
;
5448 struct ust_app_channel
*ua_chan
;
5449 struct ust_app_session
*ua_sess
;
5450 struct ust_registry_session
*registry
;
5454 /* Lookup application. If not found, there is a code flow error. */
5455 app
= find_app_by_notify_sock(sock
);
5457 DBG("Application socket %d is being torn down. Abort event notify",
5460 goto error_rcu_unlock
;
5463 /* Lookup channel by UST object descriptor. */
5464 ua_chan
= find_channel_by_objd(app
, cobjd
);
5466 DBG("Application channel is being torn down. Abort event notify");
5468 goto error_rcu_unlock
;
5471 assert(ua_chan
->session
);
5472 ua_sess
= ua_chan
->session
;
5474 registry
= get_session_registry(ua_sess
);
5476 DBG("Application session is being torn down. Abort event notify");
5478 goto error_rcu_unlock
;
5481 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5482 chan_reg_key
= ua_chan
->tracing_channel_id
;
5484 chan_reg_key
= ua_chan
->key
;
5487 pthread_mutex_lock(®istry
->lock
);
5490 * From this point on, this call acquires the ownership of the sig, fields
5491 * and model_emf_uri meaning any free are done inside it if needed. These
5492 * three variables MUST NOT be read/write after this.
5494 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5495 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5496 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5500 model_emf_uri
= NULL
;
5503 * The return value is returned to ustctl so in case of an error, the
5504 * application can be notified. In case of an error, it's important not to
5505 * return a negative error or else the application will get closed.
5507 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5509 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5510 ERR("UST app reply event failed with ret %d", ret
);
5512 DBG3("UST app reply event failed. Application died");
5515 * No need to wipe the create event since the application socket will
5516 * get close on error hence cleaning up everything by itself.
5521 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5525 pthread_mutex_unlock(®istry
->lock
);
5530 free(model_emf_uri
);
5535 * Add enum to the UST session registry. Once done, this replies to the
5536 * application with the appropriate error code.
5538 * The session UST registry lock is acquired within this function.
5540 * On success 0 is returned else a negative value.
5542 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
5543 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
5545 int ret
= 0, ret_code
;
5546 struct ust_app
*app
;
5547 struct ust_app_session
*ua_sess
;
5548 struct ust_registry_session
*registry
;
5549 uint64_t enum_id
= -1ULL;
5553 /* Lookup application. If not found, there is a code flow error. */
5554 app
= find_app_by_notify_sock(sock
);
5556 /* Return an error since this is not an error */
5557 DBG("Application socket %d is being torn down. Aborting enum registration",
5560 goto error_rcu_unlock
;
5563 /* Lookup session by UST object descriptor. */
5564 ua_sess
= find_session_by_objd(app
, sobjd
);
5566 /* Return an error since this is not an error */
5567 DBG("Application session is being torn down (session not found). Aborting enum registration.");
5569 goto error_rcu_unlock
;
5572 registry
= get_session_registry(ua_sess
);
5574 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
5576 goto error_rcu_unlock
;
5579 pthread_mutex_lock(®istry
->lock
);
5582 * From this point on, the callee acquires the ownership of
5583 * entries. The variable entries MUST NOT be read/written after
5586 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
5587 entries
, nr_entries
, &enum_id
);
5591 * The return value is returned to ustctl so in case of an error, the
5592 * application can be notified. In case of an error, it's important not to
5593 * return a negative error or else the application will get closed.
5595 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
5597 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5598 ERR("UST app reply enum failed with ret %d", ret
);
5600 DBG3("UST app reply enum failed. Application died");
5603 * No need to wipe the create enum since the application socket will
5604 * get close on error hence cleaning up everything by itself.
5609 DBG3("UST registry enum %s added successfully or already found", name
);
5612 pthread_mutex_unlock(®istry
->lock
);
5619 * Handle application notification through the given notify socket.
5621 * Return 0 on success or else a negative value.
5623 int ust_app_recv_notify(int sock
)
5626 enum ustctl_notify_cmd cmd
;
5628 DBG3("UST app receiving notify from sock %d", sock
);
5630 ret
= ustctl_recv_notify(sock
, &cmd
);
5632 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5633 ERR("UST app recv notify failed with ret %d", ret
);
5635 DBG3("UST app recv notify failed. Application died");
5641 case USTCTL_NOTIFY_CMD_EVENT
:
5643 int sobjd
, cobjd
, loglevel_value
;
5644 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5646 struct ustctl_field
*fields
;
5648 DBG2("UST app ustctl register event received");
5650 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5651 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5654 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5655 ERR("UST app recv event failed with ret %d", ret
);
5657 DBG3("UST app recv event failed. Application died");
5663 * Add event to the UST registry coming from the notify socket. This
5664 * call will free if needed the sig, fields and model_emf_uri. This
5665 * code path loses the ownsership of these variables and transfer them
5666 * to the this function.
5668 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5669 fields
, loglevel_value
, model_emf_uri
);
5676 case USTCTL_NOTIFY_CMD_CHANNEL
:
5680 struct ustctl_field
*fields
;
5682 DBG2("UST app ustctl register channel received");
5684 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5687 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5688 ERR("UST app recv channel failed with ret %d", ret
);
5690 DBG3("UST app recv channel failed. Application died");
5696 * The fields ownership are transfered to this function call meaning
5697 * that if needed it will be freed. After this, it's invalid to access
5698 * fields or clean it up.
5700 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
5708 case USTCTL_NOTIFY_CMD_ENUM
:
5711 char name
[LTTNG_UST_SYM_NAME_LEN
];
5713 struct ustctl_enum_entry
*entries
;
5715 DBG2("UST app ustctl register enum received");
5717 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
5718 &entries
, &nr_entries
);
5720 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5721 ERR("UST app recv enum failed with ret %d", ret
);
5723 DBG3("UST app recv enum failed. Application died");
5728 /* Callee assumes ownership of entries */
5729 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
5730 entries
, nr_entries
);
5738 /* Should NEVER happen. */
5747 * Once the notify socket hangs up, this is called. First, it tries to find the
5748 * corresponding application. On failure, the call_rcu to close the socket is
5749 * executed. If an application is found, it tries to delete it from the notify
5750 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5752 * Note that an object needs to be allocated here so on ENOMEM failure, the
5753 * call RCU is not done but the rest of the cleanup is.
5755 void ust_app_notify_sock_unregister(int sock
)
5758 struct lttng_ht_iter iter
;
5759 struct ust_app
*app
;
5760 struct ust_app_notify_sock_obj
*obj
;
5766 obj
= zmalloc(sizeof(*obj
));
5769 * An ENOMEM is kind of uncool. If this strikes we continue the
5770 * procedure but the call_rcu will not be called. In this case, we
5771 * accept the fd leak rather than possibly creating an unsynchronized
5772 * state between threads.
5774 * TODO: The notify object should be created once the notify socket is
5775 * registered and stored independantely from the ust app object. The
5776 * tricky part is to synchronize the teardown of the application and
5777 * this notify object. Let's keep that in mind so we can avoid this
5778 * kind of shenanigans with ENOMEM in the teardown path.
5785 DBG("UST app notify socket unregister %d", sock
);
5788 * Lookup application by notify socket. If this fails, this means that the
5789 * hash table delete has already been done by the application
5790 * unregistration process so we can safely close the notify socket in a
5793 app
= find_app_by_notify_sock(sock
);
5798 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5801 * Whatever happens here either we fail or succeed, in both cases we have
5802 * to close the socket after a grace period to continue to the call RCU
5803 * here. If the deletion is successful, the application is not visible
5804 * anymore by other threads and is it fails it means that it was already
5805 * deleted from the hash table so either way we just have to close the
5808 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5814 * Close socket after a grace period to avoid for the socket to be reused
5815 * before the application object is freed creating potential race between
5816 * threads trying to add unique in the global hash table.
5819 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5824 * Destroy a ust app data structure and free its memory.
5826 void ust_app_destroy(struct ust_app
*app
)
5832 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5836 * Take a snapshot for a given UST session. The snapshot is sent to the given
5839 * Return 0 on success or else a negative value.
5841 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5842 struct snapshot_output
*output
, int wait
,
5843 uint64_t nb_packets_per_stream
)
5846 struct lttng_ht_iter iter
;
5847 struct ust_app
*app
;
5848 char pathname
[PATH_MAX
];
5855 switch (usess
->buffer_type
) {
5856 case LTTNG_BUFFER_PER_UID
:
5858 struct buffer_reg_uid
*reg
;
5860 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5861 struct buffer_reg_channel
*reg_chan
;
5862 struct consumer_socket
*socket
;
5864 if (!reg
->registry
->reg
.ust
->metadata_key
) {
5865 /* Skip since no metadata is present */
5869 /* Get consumer socket to use to push the metadata.*/
5870 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5877 memset(pathname
, 0, sizeof(pathname
));
5878 ret
= snprintf(pathname
, sizeof(pathname
),
5879 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5880 reg
->uid
, reg
->bits_per_long
);
5882 PERROR("snprintf snapshot path");
5886 /* Add the UST default trace dir to path. */
5887 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5888 reg_chan
, node
.node
) {
5889 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5890 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5891 nb_packets_per_stream
);
5896 ret
= consumer_snapshot_channel(socket
,
5897 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5898 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
5905 case LTTNG_BUFFER_PER_PID
:
5907 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5908 struct consumer_socket
*socket
;
5909 struct lttng_ht_iter chan_iter
;
5910 struct ust_app_channel
*ua_chan
;
5911 struct ust_app_session
*ua_sess
;
5912 struct ust_registry_session
*registry
;
5914 ua_sess
= lookup_session_by_app(usess
, app
);
5916 /* Session not associated with this app. */
5920 /* Get the right consumer socket for the application. */
5921 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5928 /* Add the UST default trace dir to path. */
5929 memset(pathname
, 0, sizeof(pathname
));
5930 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5933 PERROR("snprintf snapshot path");
5937 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5938 ua_chan
, node
.node
) {
5939 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
5940 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
5941 nb_packets_per_stream
);
5947 registry
= get_session_registry(ua_sess
);
5949 DBG("Application session is being torn down. Abort snapshot record.");
5953 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
5954 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
5972 * Return the size taken by one more packet per stream.
5974 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
5975 uint64_t cur_nr_packets
)
5977 uint64_t tot_size
= 0;
5978 struct ust_app
*app
;
5979 struct lttng_ht_iter iter
;
5983 switch (usess
->buffer_type
) {
5984 case LTTNG_BUFFER_PER_UID
:
5986 struct buffer_reg_uid
*reg
;
5988 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5989 struct buffer_reg_channel
*reg_chan
;
5992 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5993 reg_chan
, node
.node
) {
5994 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
5996 * Don't take channel into account if we
5997 * already grab all its packets.
6001 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6007 case LTTNG_BUFFER_PER_PID
:
6010 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6011 struct ust_app_channel
*ua_chan
;
6012 struct ust_app_session
*ua_sess
;
6013 struct lttng_ht_iter chan_iter
;
6015 ua_sess
= lookup_session_by_app(usess
, app
);
6017 /* Session not associated with this app. */
6021 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6022 ua_chan
, node
.node
) {
6023 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6025 * Don't take channel into account if we
6026 * already grab all its packets.
6030 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6044 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6045 struct cds_list_head
*buffer_reg_uid_list
,
6046 struct consumer_output
*consumer
, uint64_t uchan_id
,
6047 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6050 uint64_t consumer_chan_key
;
6055 ret
= buffer_reg_uid_consumer_channel_key(
6056 buffer_reg_uid_list
, ust_session_id
,
6057 uchan_id
, &consumer_chan_key
);
6065 ret
= consumer_get_lost_packets(ust_session_id
,
6066 consumer_chan_key
, consumer
, lost
);
6068 ret
= consumer_get_discarded_events(ust_session_id
,
6069 consumer_chan_key
, consumer
, discarded
);
6076 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6077 struct ltt_ust_channel
*uchan
,
6078 struct consumer_output
*consumer
, int overwrite
,
6079 uint64_t *discarded
, uint64_t *lost
)
6082 struct lttng_ht_iter iter
;
6083 struct lttng_ht_node_str
*ua_chan_node
;
6084 struct ust_app
*app
;
6085 struct ust_app_session
*ua_sess
;
6086 struct ust_app_channel
*ua_chan
;
6093 * Iterate over every registered applications. Sum counters for
6094 * all applications containing requested session and channel.
6096 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6097 struct lttng_ht_iter uiter
;
6099 ua_sess
= lookup_session_by_app(usess
, app
);
6100 if (ua_sess
== NULL
) {
6105 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6106 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6107 /* If the session is found for the app, the channel must be there */
6108 assert(ua_chan_node
);
6110 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6115 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6122 uint64_t _discarded
;
6124 ret
= consumer_get_discarded_events(usess
->id
,
6125 ua_chan
->key
, consumer
, &_discarded
);
6129 (*discarded
) += _discarded
;
6138 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6139 struct ust_app
*app
)
6142 struct ust_app_session
*ua_sess
;
6144 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6148 ua_sess
= lookup_session_by_app(usess
, app
);
6149 if (ua_sess
== NULL
) {
6150 /* The session is in teardown process. Ignore and continue. */
6154 pthread_mutex_lock(&ua_sess
->lock
);
6156 if (ua_sess
->deleted
) {
6160 pthread_mutex_lock(&app
->sock_lock
);
6161 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6162 pthread_mutex_unlock(&app
->sock_lock
);
6165 pthread_mutex_unlock(&ua_sess
->lock
);
6169 health_code_update();
6174 * Regenerate the statedump for each app in the session.
6176 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6179 struct lttng_ht_iter iter
;
6180 struct ust_app
*app
;
6182 DBG("Regenerating the metadata for all UST apps");
6186 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6187 if (!app
->compatible
) {
6191 ret
= ust_app_regenerate_statedump(usess
, app
);
6193 /* Continue to the next app even on error */