2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
29 #include <urcu/compiler.h>
32 #include <common/common.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
35 #include "buffer-registry.h"
37 #include "health-sessiond.h"
39 #include "ust-consumer.h"
40 #include "lttng-ust-ctl.h"
41 #include "lttng-ust-error.h"
44 #include "lttng-sessiond.h"
45 #include "notification-thread-commands.h"
49 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
51 /* Next available channel key. Access under next_channel_key_lock. */
52 static uint64_t _next_channel_key
;
53 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
55 /* Next available session ID. Access under next_session_id_lock. */
56 static uint64_t _next_session_id
;
57 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
60 * Return the incremented value of next_channel_key.
62 static uint64_t get_next_channel_key(void)
66 pthread_mutex_lock(&next_channel_key_lock
);
67 ret
= ++_next_channel_key
;
68 pthread_mutex_unlock(&next_channel_key_lock
);
73 * Return the atomically incremented value of next_session_id.
75 static uint64_t get_next_session_id(void)
79 pthread_mutex_lock(&next_session_id_lock
);
80 ret
= ++_next_session_id
;
81 pthread_mutex_unlock(&next_session_id_lock
);
85 static void copy_channel_attr_to_ustctl(
86 struct ustctl_consumer_channel_attr
*attr
,
87 struct lttng_ust_channel_attr
*uattr
)
89 /* Copy event attributes since the layout is different. */
90 attr
->subbuf_size
= uattr
->subbuf_size
;
91 attr
->num_subbuf
= uattr
->num_subbuf
;
92 attr
->overwrite
= uattr
->overwrite
;
93 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
94 attr
->read_timer_interval
= uattr
->read_timer_interval
;
95 attr
->output
= uattr
->output
;
96 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
100 * Match function for the hash table lookup.
102 * It matches an ust app event based on three attributes which are the event
103 * name, the filter bytecode and the loglevel.
105 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
107 struct ust_app_event
*event
;
108 const struct ust_app_ht_key
*key
;
109 int ev_loglevel_value
;
114 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
116 ev_loglevel_value
= event
->attr
.loglevel
;
118 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
121 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
125 /* Event loglevel. */
126 if (ev_loglevel_value
!= key
->loglevel_type
) {
127 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
128 && key
->loglevel_type
== 0 &&
129 ev_loglevel_value
== -1) {
131 * Match is accepted. This is because on event creation, the
132 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
133 * -1 are accepted for this loglevel type since 0 is the one set by
134 * the API when receiving an enable event.
141 /* One of the filters is NULL, fail. */
142 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
146 if (key
->filter
&& event
->filter
) {
147 /* Both filters exists, check length followed by the bytecode. */
148 if (event
->filter
->len
!= key
->filter
->len
||
149 memcmp(event
->filter
->data
, key
->filter
->data
,
150 event
->filter
->len
) != 0) {
155 /* One of the exclusions is NULL, fail. */
156 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
160 if (key
->exclusion
&& event
->exclusion
) {
161 /* Both exclusions exists, check count followed by the names. */
162 if (event
->exclusion
->count
!= key
->exclusion
->count
||
163 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
164 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
178 * Unique add of an ust app event in the given ht. This uses the custom
179 * ht_match_ust_app_event match function and the event name as hash.
181 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
182 struct ust_app_event
*event
)
184 struct cds_lfht_node
*node_ptr
;
185 struct ust_app_ht_key key
;
189 assert(ua_chan
->events
);
192 ht
= ua_chan
->events
;
193 key
.name
= event
->attr
.name
;
194 key
.filter
= event
->filter
;
195 key
.loglevel_type
= event
->attr
.loglevel
;
196 key
.exclusion
= event
->exclusion
;
198 node_ptr
= cds_lfht_add_unique(ht
->ht
,
199 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
200 ht_match_ust_app_event
, &key
, &event
->node
.node
);
201 assert(node_ptr
== &event
->node
.node
);
205 * Close the notify socket from the given RCU head object. This MUST be called
206 * through a call_rcu().
208 static void close_notify_sock_rcu(struct rcu_head
*head
)
211 struct ust_app_notify_sock_obj
*obj
=
212 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
214 /* Must have a valid fd here. */
215 assert(obj
->fd
>= 0);
217 ret
= close(obj
->fd
);
219 ERR("close notify sock %d RCU", obj
->fd
);
221 lttng_fd_put(LTTNG_FD_APPS
, 1);
227 * Return the session registry according to the buffer type of the given
230 * A registry per UID object MUST exists before calling this function or else
231 * it assert() if not found. RCU read side lock must be acquired.
233 static struct ust_registry_session
*get_session_registry(
234 struct ust_app_session
*ua_sess
)
236 struct ust_registry_session
*registry
= NULL
;
240 switch (ua_sess
->buffer_type
) {
241 case LTTNG_BUFFER_PER_PID
:
243 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
247 registry
= reg_pid
->registry
->reg
.ust
;
250 case LTTNG_BUFFER_PER_UID
:
252 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
253 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
254 ua_sess
->real_credentials
.uid
);
258 registry
= reg_uid
->registry
->reg
.ust
;
270 * Delete ust context safely. RCU read lock must be held before calling
274 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
282 pthread_mutex_lock(&app
->sock_lock
);
283 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
284 pthread_mutex_unlock(&app
->sock_lock
);
285 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
286 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
287 sock
, ua_ctx
->obj
->handle
, ret
);
295 * Delete ust app event safely. RCU read lock must be held before calling
299 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
306 free(ua_event
->filter
);
307 if (ua_event
->exclusion
!= NULL
)
308 free(ua_event
->exclusion
);
309 if (ua_event
->obj
!= NULL
) {
310 pthread_mutex_lock(&app
->sock_lock
);
311 ret
= ustctl_release_object(sock
, ua_event
->obj
);
312 pthread_mutex_unlock(&app
->sock_lock
);
313 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
314 ERR("UST app sock %d release event obj failed with ret %d",
323 * Release ust data object of the given stream.
325 * Return 0 on success or else a negative value.
327 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
335 pthread_mutex_lock(&app
->sock_lock
);
336 ret
= ustctl_release_object(sock
, stream
->obj
);
337 pthread_mutex_unlock(&app
->sock_lock
);
338 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
339 ERR("UST app sock %d release stream obj failed with ret %d",
342 lttng_fd_put(LTTNG_FD_APPS
, 2);
350 * Delete ust app stream safely. RCU read lock must be held before calling
354 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
359 (void) release_ust_app_stream(sock
, stream
, app
);
364 * We need to execute ht_destroy outside of RCU read-side critical
365 * section and outside of call_rcu thread, so we postpone its execution
366 * using ht_cleanup_push. It is simpler than to change the semantic of
367 * the many callers of delete_ust_app_session().
370 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
372 struct ust_app_channel
*ua_chan
=
373 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
375 ht_cleanup_push(ua_chan
->ctx
);
376 ht_cleanup_push(ua_chan
->events
);
381 * Extract the lost packet or discarded events counter when the channel is
382 * being deleted and store the value in the parent channel so we can
383 * access it from lttng list and at stop/destroy.
385 * The session list lock must be held by the caller.
388 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
390 uint64_t discarded
= 0, lost
= 0;
391 struct ltt_session
*session
;
392 struct ltt_ust_channel
*uchan
;
394 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
399 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
400 if (!session
|| !session
->ust_session
) {
402 * Not finding the session is not an error because there are
403 * multiple ways the channels can be torn down.
405 * 1) The session daemon can initiate the destruction of the
406 * ust app session after receiving a destroy command or
407 * during its shutdown/teardown.
408 * 2) The application, since we are in per-pid tracing, is
409 * unregistering and tearing down its ust app session.
411 * Both paths are protected by the session list lock which
412 * ensures that the accounting of lost packets and discarded
413 * events is done exactly once. The session is then unpublished
414 * from the session list, resulting in this condition.
419 if (ua_chan
->attr
.overwrite
) {
420 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
421 ua_chan
->key
, session
->ust_session
->consumer
,
424 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
425 ua_chan
->key
, session
->ust_session
->consumer
,
428 uchan
= trace_ust_find_channel_by_name(
429 session
->ust_session
->domain_global
.channels
,
432 ERR("Missing UST channel to store discarded counters");
436 uchan
->per_pid_closed_app_discarded
+= discarded
;
437 uchan
->per_pid_closed_app_lost
+= lost
;
442 session_put(session
);
447 * Delete ust app channel safely. RCU read lock must be held before calling
450 * The session list lock must be held by the caller.
453 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
457 struct lttng_ht_iter iter
;
458 struct ust_app_event
*ua_event
;
459 struct ust_app_ctx
*ua_ctx
;
460 struct ust_app_stream
*stream
, *stmp
;
461 struct ust_registry_session
*registry
;
465 DBG3("UST app deleting channel %s", ua_chan
->name
);
468 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
469 cds_list_del(&stream
->list
);
470 delete_ust_app_stream(sock
, stream
, app
);
474 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
475 cds_list_del(&ua_ctx
->list
);
476 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
478 delete_ust_app_ctx(sock
, ua_ctx
, app
);
482 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
484 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
486 delete_ust_app_event(sock
, ua_event
, app
);
489 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
490 /* Wipe and free registry from session registry. */
491 registry
= get_session_registry(ua_chan
->session
);
493 ust_registry_channel_del_free(registry
, ua_chan
->key
,
497 * A negative socket can be used by the caller when
498 * cleaning-up a ua_chan in an error path. Skip the
499 * accounting in this case.
502 save_per_pid_lost_discarded_counters(ua_chan
);
506 if (ua_chan
->obj
!= NULL
) {
507 /* Remove channel from application UST object descriptor. */
508 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
509 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
511 pthread_mutex_lock(&app
->sock_lock
);
512 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
513 pthread_mutex_unlock(&app
->sock_lock
);
514 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
515 ERR("UST app sock %d release channel obj failed with ret %d",
518 lttng_fd_put(LTTNG_FD_APPS
, 1);
521 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
524 int ust_app_register_done(struct ust_app
*app
)
528 pthread_mutex_lock(&app
->sock_lock
);
529 ret
= ustctl_register_done(app
->sock
);
530 pthread_mutex_unlock(&app
->sock_lock
);
534 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
539 pthread_mutex_lock(&app
->sock_lock
);
544 ret
= ustctl_release_object(sock
, data
);
546 pthread_mutex_unlock(&app
->sock_lock
);
552 * Push metadata to consumer socket.
554 * RCU read-side lock must be held to guarantee existance of socket.
555 * Must be called with the ust app session lock held.
556 * Must be called with the registry lock held.
558 * On success, return the len of metadata pushed or else a negative value.
559 * Returning a -EPIPE return value means we could not send the metadata,
560 * but it can be caused by recoverable errors (e.g. the application has
561 * terminated concurrently).
563 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
564 struct consumer_socket
*socket
, int send_zero_data
)
567 char *metadata_str
= NULL
;
568 size_t len
, offset
, new_metadata_len_sent
;
570 uint64_t metadata_key
, metadata_version
;
575 metadata_key
= registry
->metadata_key
;
578 * Means that no metadata was assigned to the session. This can
579 * happens if no start has been done previously.
585 offset
= registry
->metadata_len_sent
;
586 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
587 new_metadata_len_sent
= registry
->metadata_len
;
588 metadata_version
= registry
->metadata_version
;
590 DBG3("No metadata to push for metadata key %" PRIu64
,
591 registry
->metadata_key
);
593 if (send_zero_data
) {
594 DBG("No metadata to push");
600 /* Allocate only what we have to send. */
601 metadata_str
= zmalloc(len
);
603 PERROR("zmalloc ust app metadata string");
607 /* Copy what we haven't sent out. */
608 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
611 pthread_mutex_unlock(®istry
->lock
);
613 * We need to unlock the registry while we push metadata to
614 * break a circular dependency between the consumerd metadata
615 * lock and the sessiond registry lock. Indeed, pushing metadata
616 * to the consumerd awaits that it gets pushed all the way to
617 * relayd, but doing so requires grabbing the metadata lock. If
618 * a concurrent metadata request is being performed by
619 * consumerd, this can try to grab the registry lock on the
620 * sessiond while holding the metadata lock on the consumer
621 * daemon. Those push and pull schemes are performed on two
622 * different bidirectionnal communication sockets.
624 ret
= consumer_push_metadata(socket
, metadata_key
,
625 metadata_str
, len
, offset
, metadata_version
);
626 pthread_mutex_lock(®istry
->lock
);
629 * There is an acceptable race here between the registry
630 * metadata key assignment and the creation on the
631 * consumer. The session daemon can concurrently push
632 * metadata for this registry while being created on the
633 * consumer since the metadata key of the registry is
634 * assigned *before* it is setup to avoid the consumer
635 * to ask for metadata that could possibly be not found
636 * in the session daemon.
638 * The metadata will get pushed either by the session
639 * being stopped or the consumer requesting metadata if
640 * that race is triggered.
642 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
645 ERR("Error pushing metadata to consumer");
651 * Metadata may have been concurrently pushed, since
652 * we're not holding the registry lock while pushing to
653 * consumer. This is handled by the fact that we send
654 * the metadata content, size, and the offset at which
655 * that metadata belongs. This may arrive out of order
656 * on the consumer side, and the consumer is able to
657 * deal with overlapping fragments. The consumer
658 * supports overlapping fragments, which must be
659 * contiguous starting from offset 0. We keep the
660 * largest metadata_len_sent value of the concurrent
663 registry
->metadata_len_sent
=
664 max_t(size_t, registry
->metadata_len_sent
,
665 new_metadata_len_sent
);
674 * On error, flag the registry that the metadata is
675 * closed. We were unable to push anything and this
676 * means that either the consumer is not responding or
677 * the metadata cache has been destroyed on the
680 registry
->metadata_closed
= 1;
688 * For a given application and session, push metadata to consumer.
689 * Either sock or consumer is required : if sock is NULL, the default
690 * socket to send the metadata is retrieved from consumer, if sock
691 * is not NULL we use it to send the metadata.
692 * RCU read-side lock must be held while calling this function,
693 * therefore ensuring existance of registry. It also ensures existance
694 * of socket throughout this function.
696 * Return 0 on success else a negative error.
697 * Returning a -EPIPE return value means we could not send the metadata,
698 * but it can be caused by recoverable errors (e.g. the application has
699 * terminated concurrently).
701 static int push_metadata(struct ust_registry_session
*registry
,
702 struct consumer_output
*consumer
)
706 struct consumer_socket
*socket
;
711 pthread_mutex_lock(®istry
->lock
);
712 if (registry
->metadata_closed
) {
717 /* Get consumer socket to use to push the metadata.*/
718 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
725 ret
= ust_app_push_metadata(registry
, socket
, 0);
730 pthread_mutex_unlock(®istry
->lock
);
734 pthread_mutex_unlock(®istry
->lock
);
739 * Send to the consumer a close metadata command for the given session. Once
740 * done, the metadata channel is deleted and the session metadata pointer is
741 * nullified. The session lock MUST be held unless the application is
742 * in the destroy path.
744 * Do not hold the registry lock while communicating with the consumerd, because
745 * doing so causes inter-process deadlocks between consumerd and sessiond with
746 * the metadata request notification.
748 * Return 0 on success else a negative value.
750 static int close_metadata(struct ust_registry_session
*registry
,
751 struct consumer_output
*consumer
)
754 struct consumer_socket
*socket
;
755 uint64_t metadata_key
;
756 bool registry_was_already_closed
;
763 pthread_mutex_lock(®istry
->lock
);
764 metadata_key
= registry
->metadata_key
;
765 registry_was_already_closed
= registry
->metadata_closed
;
766 if (metadata_key
!= 0) {
768 * Metadata closed. Even on error this means that the consumer
769 * is not responding or not found so either way a second close
770 * should NOT be emit for this registry.
772 registry
->metadata_closed
= 1;
774 pthread_mutex_unlock(®istry
->lock
);
776 if (metadata_key
== 0 || registry_was_already_closed
) {
781 /* Get consumer socket to use to push the metadata.*/
782 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
789 ret
= consumer_close_metadata(socket
, metadata_key
);
800 * We need to execute ht_destroy outside of RCU read-side critical
801 * section and outside of call_rcu thread, so we postpone its execution
802 * using ht_cleanup_push. It is simpler than to change the semantic of
803 * the many callers of delete_ust_app_session().
806 void delete_ust_app_session_rcu(struct rcu_head
*head
)
808 struct ust_app_session
*ua_sess
=
809 caa_container_of(head
, struct ust_app_session
, rcu_head
);
811 ht_cleanup_push(ua_sess
->channels
);
816 * Delete ust app session safely. RCU read lock must be held before calling
819 * The session list lock must be held by the caller.
822 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
826 struct lttng_ht_iter iter
;
827 struct ust_app_channel
*ua_chan
;
828 struct ust_registry_session
*registry
;
832 pthread_mutex_lock(&ua_sess
->lock
);
834 assert(!ua_sess
->deleted
);
835 ua_sess
->deleted
= true;
837 registry
= get_session_registry(ua_sess
);
838 /* Registry can be null on error path during initialization. */
840 /* Push metadata for application before freeing the application. */
841 (void) push_metadata(registry
, ua_sess
->consumer
);
844 * Don't ask to close metadata for global per UID buffers. Close
845 * metadata only on destroy trace session in this case. Also, the
846 * previous push metadata could have flag the metadata registry to
847 * close so don't send a close command if closed.
849 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
850 /* And ask to close it for this session registry. */
851 (void) close_metadata(registry
, ua_sess
->consumer
);
855 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
857 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
859 delete_ust_app_channel(sock
, ua_chan
, app
);
862 /* In case of per PID, the registry is kept in the session. */
863 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
864 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
867 * Registry can be null on error path during
870 buffer_reg_pid_remove(reg_pid
);
871 buffer_reg_pid_destroy(reg_pid
);
875 if (ua_sess
->handle
!= -1) {
876 pthread_mutex_lock(&app
->sock_lock
);
877 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
878 pthread_mutex_unlock(&app
->sock_lock
);
879 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
880 ERR("UST app sock %d release session handle failed with ret %d",
883 /* Remove session from application UST object descriptor. */
884 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
885 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
889 pthread_mutex_unlock(&ua_sess
->lock
);
891 consumer_output_put(ua_sess
->consumer
);
893 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
897 * Delete a traceable application structure from the global list. Never call
898 * this function outside of a call_rcu call.
900 * RCU read side lock should _NOT_ be held when calling this function.
903 void delete_ust_app(struct ust_app
*app
)
906 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
909 * The session list lock must be held during this function to guarantee
910 * the existence of ua_sess.
913 /* Delete ust app sessions info */
918 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
920 /* Free every object in the session and the session. */
922 delete_ust_app_session(sock
, ua_sess
, app
);
926 ht_cleanup_push(app
->sessions
);
927 ht_cleanup_push(app
->ust_sessions_objd
);
928 ht_cleanup_push(app
->ust_objd
);
931 * Wait until we have deleted the application from the sock hash table
932 * before closing this socket, otherwise an application could re-use the
933 * socket ID and race with the teardown, using the same hash table entry.
935 * It's OK to leave the close in call_rcu. We want it to stay unique for
936 * all RCU readers that could run concurrently with unregister app,
937 * therefore we _need_ to only close that socket after a grace period. So
938 * it should stay in this RCU callback.
940 * This close() is a very important step of the synchronization model so
941 * every modification to this function must be carefully reviewed.
947 lttng_fd_put(LTTNG_FD_APPS
, 1);
949 DBG2("UST app pid %d deleted", app
->pid
);
951 session_unlock_list();
955 * URCU intermediate call to delete an UST app.
958 void delete_ust_app_rcu(struct rcu_head
*head
)
960 struct lttng_ht_node_ulong
*node
=
961 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
962 struct ust_app
*app
=
963 caa_container_of(node
, struct ust_app
, pid_n
);
965 DBG3("Call RCU deleting app PID %d", app
->pid
);
970 * Delete the session from the application ht and delete the data structure by
971 * freeing every object inside and releasing them.
973 * The session list lock must be held by the caller.
975 static void destroy_app_session(struct ust_app
*app
,
976 struct ust_app_session
*ua_sess
)
979 struct lttng_ht_iter iter
;
984 iter
.iter
.node
= &ua_sess
->node
.node
;
985 ret
= lttng_ht_del(app
->sessions
, &iter
);
987 /* Already scheduled for teardown. */
991 /* Once deleted, free the data structure. */
992 delete_ust_app_session(app
->sock
, ua_sess
, app
);
999 * Alloc new UST app session.
1002 struct ust_app_session
*alloc_ust_app_session(void)
1004 struct ust_app_session
*ua_sess
;
1006 /* Init most of the default value by allocating and zeroing */
1007 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1008 if (ua_sess
== NULL
) {
1013 ua_sess
->handle
= -1;
1014 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1015 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1016 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1025 * Alloc new UST app channel.
1028 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
1029 struct ust_app_session
*ua_sess
,
1030 struct lttng_ust_channel_attr
*attr
)
1032 struct ust_app_channel
*ua_chan
;
1034 /* Init most of the default value by allocating and zeroing */
1035 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1036 if (ua_chan
== NULL
) {
1041 /* Setup channel name */
1042 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1043 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1045 ua_chan
->enabled
= 1;
1046 ua_chan
->handle
= -1;
1047 ua_chan
->session
= ua_sess
;
1048 ua_chan
->key
= get_next_channel_key();
1049 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1050 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1051 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1053 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1054 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1056 /* Copy attributes */
1058 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1059 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1060 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1061 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1062 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1063 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1064 ua_chan
->attr
.output
= attr
->output
;
1065 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1067 /* By default, the channel is a per cpu channel. */
1068 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1070 DBG3("UST app channel %s allocated", ua_chan
->name
);
1079 * Allocate and initialize a UST app stream.
1081 * Return newly allocated stream pointer or NULL on error.
1083 struct ust_app_stream
*ust_app_alloc_stream(void)
1085 struct ust_app_stream
*stream
= NULL
;
1087 stream
= zmalloc(sizeof(*stream
));
1088 if (stream
== NULL
) {
1089 PERROR("zmalloc ust app stream");
1093 /* Zero could be a valid value for a handle so flag it to -1. */
1094 stream
->handle
= -1;
1101 * Alloc new UST app event.
1104 struct ust_app_event
*alloc_ust_app_event(char *name
,
1105 struct lttng_ust_event
*attr
)
1107 struct ust_app_event
*ua_event
;
1109 /* Init most of the default value by allocating and zeroing */
1110 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1111 if (ua_event
== NULL
) {
1112 PERROR("Failed to allocate ust_app_event structure");
1116 ua_event
->enabled
= 1;
1117 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1118 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1119 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1121 /* Copy attributes */
1123 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1126 DBG3("UST app event %s allocated", ua_event
->name
);
1135 * Alloc new UST app context.
1138 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1140 struct ust_app_ctx
*ua_ctx
;
1142 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1143 if (ua_ctx
== NULL
) {
1147 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1150 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1151 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1152 char *provider_name
= NULL
, *ctx_name
= NULL
;
1154 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1155 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1156 if (!provider_name
|| !ctx_name
) {
1157 free(provider_name
);
1162 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1163 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1167 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1175 * Allocate a filter and copy the given original filter.
1177 * Return allocated filter or NULL on error.
1179 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1180 struct lttng_filter_bytecode
*orig_f
)
1182 struct lttng_filter_bytecode
*filter
= NULL
;
1184 /* Copy filter bytecode */
1185 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1187 PERROR("zmalloc alloc filter bytecode");
1191 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1198 * Create a liblttng-ust filter bytecode from given bytecode.
1200 * Return allocated filter or NULL on error.
1202 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1203 struct lttng_filter_bytecode
*orig_f
)
1205 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1207 /* Copy filter bytecode */
1208 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1210 PERROR("zmalloc alloc ust filter bytecode");
1214 assert(sizeof(struct lttng_filter_bytecode
) ==
1215 sizeof(struct lttng_ust_filter_bytecode
));
1216 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1222 * Find an ust_app using the sock and return it. RCU read side lock must be
1223 * held before calling this helper function.
1225 struct ust_app
*ust_app_find_by_sock(int sock
)
1227 struct lttng_ht_node_ulong
*node
;
1228 struct lttng_ht_iter iter
;
1230 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1231 node
= lttng_ht_iter_get_node_ulong(&iter
);
1233 DBG2("UST app find by sock %d not found", sock
);
1237 return caa_container_of(node
, struct ust_app
, sock_n
);
1244 * Find an ust_app using the notify sock and return it. RCU read side lock must
1245 * be held before calling this helper function.
1247 static struct ust_app
*find_app_by_notify_sock(int sock
)
1249 struct lttng_ht_node_ulong
*node
;
1250 struct lttng_ht_iter iter
;
1252 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1254 node
= lttng_ht_iter_get_node_ulong(&iter
);
1256 DBG2("UST app find by notify sock %d not found", sock
);
1260 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1267 * Lookup for an ust app event based on event name, filter bytecode and the
1270 * Return an ust_app_event object or NULL on error.
1272 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1273 const char *name
, const struct lttng_filter_bytecode
*filter
,
1275 const struct lttng_event_exclusion
*exclusion
)
1277 struct lttng_ht_iter iter
;
1278 struct lttng_ht_node_str
*node
;
1279 struct ust_app_event
*event
= NULL
;
1280 struct ust_app_ht_key key
;
1285 /* Setup key for event lookup. */
1287 key
.filter
= filter
;
1288 key
.loglevel_type
= loglevel_value
;
1289 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1290 key
.exclusion
= exclusion
;
1292 /* Lookup using the event name as hash and a custom match fct. */
1293 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1294 ht_match_ust_app_event
, &key
, &iter
.iter
);
1295 node
= lttng_ht_iter_get_node_str(&iter
);
1300 event
= caa_container_of(node
, struct ust_app_event
, node
);
1307 * Create the channel context on the tracer.
1309 * Called with UST app session lock held.
1312 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1313 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1317 health_code_update();
1319 pthread_mutex_lock(&app
->sock_lock
);
1320 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1321 ua_chan
->obj
, &ua_ctx
->obj
);
1322 pthread_mutex_unlock(&app
->sock_lock
);
1324 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1325 ERR("UST app create channel context failed for app (pid: %d) "
1326 "with ret %d", app
->pid
, ret
);
1329 * This is normal behavior, an application can die during the
1330 * creation process. Don't report an error so the execution can
1331 * continue normally.
1334 DBG3("UST app add context failed. Application is dead.");
1339 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1341 DBG2("UST app context handle %d created successfully for channel %s",
1342 ua_ctx
->handle
, ua_chan
->name
);
1345 health_code_update();
1350 * Set the filter on the tracer.
1353 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1354 struct ust_app
*app
)
1357 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1359 health_code_update();
1361 if (!ua_event
->filter
) {
1366 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1367 if (!ust_bytecode
) {
1368 ret
= -LTTNG_ERR_NOMEM
;
1371 pthread_mutex_lock(&app
->sock_lock
);
1372 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1374 pthread_mutex_unlock(&app
->sock_lock
);
1376 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1377 ERR("UST app event %s filter failed for app (pid: %d) "
1378 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1381 * This is normal behavior, an application can die during the
1382 * creation process. Don't report an error so the execution can
1383 * continue normally.
1386 DBG3("UST app filter event failed. Application is dead.");
1391 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1394 health_code_update();
1400 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1401 struct lttng_event_exclusion
*exclusion
)
1403 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1404 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1405 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1407 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1408 if (!ust_exclusion
) {
1413 assert(sizeof(struct lttng_event_exclusion
) ==
1414 sizeof(struct lttng_ust_event_exclusion
));
1415 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1417 return ust_exclusion
;
1421 * Set event exclusions on the tracer.
1424 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1425 struct ust_app
*app
)
1428 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1430 health_code_update();
1432 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1437 ust_exclusion
= create_ust_exclusion_from_exclusion(
1438 ua_event
->exclusion
);
1439 if (!ust_exclusion
) {
1440 ret
= -LTTNG_ERR_NOMEM
;
1443 pthread_mutex_lock(&app
->sock_lock
);
1444 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1445 pthread_mutex_unlock(&app
->sock_lock
);
1447 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1448 ERR("UST app event %s exclusions failed for app (pid: %d) "
1449 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1452 * This is normal behavior, an application can die during the
1453 * creation process. Don't report an error so the execution can
1454 * continue normally.
1457 DBG3("UST app event exclusion failed. Application is dead.");
1462 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1465 health_code_update();
1466 free(ust_exclusion
);
1471 * Disable the specified event on to UST tracer for the UST session.
1473 static int disable_ust_event(struct ust_app
*app
,
1474 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1478 health_code_update();
1480 pthread_mutex_lock(&app
->sock_lock
);
1481 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1482 pthread_mutex_unlock(&app
->sock_lock
);
1484 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1485 ERR("UST app event %s disable failed for app (pid: %d) "
1486 "and session handle %d with ret %d",
1487 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1490 * This is normal behavior, an application can die during the
1491 * creation process. Don't report an error so the execution can
1492 * continue normally.
1495 DBG3("UST app disable event failed. Application is dead.");
1500 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1501 ua_event
->attr
.name
, app
->pid
);
1504 health_code_update();
1509 * Disable the specified channel on to UST tracer for the UST session.
1511 static int disable_ust_channel(struct ust_app
*app
,
1512 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1516 health_code_update();
1518 pthread_mutex_lock(&app
->sock_lock
);
1519 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1520 pthread_mutex_unlock(&app
->sock_lock
);
1522 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1523 ERR("UST app channel %s disable failed for app (pid: %d) "
1524 "and session handle %d with ret %d",
1525 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1528 * This is normal behavior, an application can die during the
1529 * creation process. Don't report an error so the execution can
1530 * continue normally.
1533 DBG3("UST app disable channel failed. Application is dead.");
1538 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1539 ua_chan
->name
, app
->pid
);
1542 health_code_update();
1547 * Enable the specified channel on to UST tracer for the UST session.
1549 static int enable_ust_channel(struct ust_app
*app
,
1550 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1554 health_code_update();
1556 pthread_mutex_lock(&app
->sock_lock
);
1557 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1558 pthread_mutex_unlock(&app
->sock_lock
);
1560 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1561 ERR("UST app channel %s enable failed for app (pid: %d) "
1562 "and session handle %d with ret %d",
1563 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1566 * This is normal behavior, an application can die during the
1567 * creation process. Don't report an error so the execution can
1568 * continue normally.
1571 DBG3("UST app enable channel failed. Application is dead.");
1576 ua_chan
->enabled
= 1;
1578 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1579 ua_chan
->name
, app
->pid
);
1582 health_code_update();
1587 * Enable the specified event on to UST tracer for the UST session.
1589 static int enable_ust_event(struct ust_app
*app
,
1590 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1594 health_code_update();
1596 pthread_mutex_lock(&app
->sock_lock
);
1597 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1598 pthread_mutex_unlock(&app
->sock_lock
);
1600 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1601 ERR("UST app event %s enable failed for app (pid: %d) "
1602 "and session handle %d with ret %d",
1603 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1606 * This is normal behavior, an application can die during the
1607 * creation process. Don't report an error so the execution can
1608 * continue normally.
1611 DBG3("UST app enable event failed. Application is dead.");
1616 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1617 ua_event
->attr
.name
, app
->pid
);
1620 health_code_update();
1625 * Send channel and stream buffer to application.
1627 * Return 0 on success. On error, a negative value is returned.
1629 static int send_channel_pid_to_ust(struct ust_app
*app
,
1630 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1633 struct ust_app_stream
*stream
, *stmp
;
1639 health_code_update();
1641 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1644 /* Send channel to the application. */
1645 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1646 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1647 ret
= -ENOTCONN
; /* Caused by app exiting. */
1649 } else if (ret
< 0) {
1653 health_code_update();
1655 /* Send all streams to application. */
1656 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1657 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1658 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1659 ret
= -ENOTCONN
; /* Caused by app exiting. */
1661 } else if (ret
< 0) {
1664 /* We don't need the stream anymore once sent to the tracer. */
1665 cds_list_del(&stream
->list
);
1666 delete_ust_app_stream(-1, stream
, app
);
1668 /* Flag the channel that it is sent to the application. */
1669 ua_chan
->is_sent
= 1;
1672 health_code_update();
1677 * Create the specified event onto the UST tracer for a UST session.
1679 * Should be called with session mutex held.
1682 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1683 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1687 health_code_update();
1689 /* Create UST event on tracer */
1690 pthread_mutex_lock(&app
->sock_lock
);
1691 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1693 pthread_mutex_unlock(&app
->sock_lock
);
1695 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1697 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1698 ua_event
->attr
.name
, app
->pid
, ret
);
1701 * This is normal behavior, an application can die during the
1702 * creation process. Don't report an error so the execution can
1703 * continue normally.
1706 DBG3("UST app create event failed. Application is dead.");
1711 ua_event
->handle
= ua_event
->obj
->handle
;
1713 DBG2("UST app event %s created successfully for pid:%d",
1714 ua_event
->attr
.name
, app
->pid
);
1716 health_code_update();
1718 /* Set filter if one is present. */
1719 if (ua_event
->filter
) {
1720 ret
= set_ust_event_filter(ua_event
, app
);
1726 /* Set exclusions for the event */
1727 if (ua_event
->exclusion
) {
1728 ret
= set_ust_event_exclusion(ua_event
, app
);
1734 /* If event not enabled, disable it on the tracer */
1735 if (ua_event
->enabled
) {
1737 * We now need to explicitly enable the event, since it
1738 * is now disabled at creation.
1740 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1743 * If we hit an EPERM, something is wrong with our enable call. If
1744 * we get an EEXIST, there is a problem on the tracer side since we
1748 case -LTTNG_UST_ERR_PERM
:
1749 /* Code flow problem */
1751 case -LTTNG_UST_ERR_EXIST
:
1752 /* It's OK for our use case. */
1763 health_code_update();
1768 * Copy data between an UST app event and a LTT event.
1770 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1771 struct ltt_ust_event
*uevent
)
1773 size_t exclusion_alloc_size
;
1775 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1776 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1778 ua_event
->enabled
= uevent
->enabled
;
1780 /* Copy event attributes */
1781 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1783 /* Copy filter bytecode */
1784 if (uevent
->filter
) {
1785 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1786 /* Filter might be NULL here in case of ENONEM. */
1789 /* Copy exclusion data */
1790 if (uevent
->exclusion
) {
1791 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1792 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1793 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1794 if (ua_event
->exclusion
== NULL
) {
1797 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1798 exclusion_alloc_size
);
1804 * Copy data between an UST app channel and a LTT channel.
1806 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1807 struct ltt_ust_channel
*uchan
)
1809 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1811 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1812 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1814 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1815 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1817 /* Copy event attributes since the layout is different. */
1818 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1819 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1820 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1821 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1822 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1823 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
1824 ua_chan
->attr
.output
= uchan
->attr
.output
;
1825 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
1828 * Note that the attribute channel type is not set since the channel on the
1829 * tracing registry side does not have this information.
1832 ua_chan
->enabled
= uchan
->enabled
;
1833 ua_chan
->tracing_channel_id
= uchan
->id
;
1835 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1839 * Copy data between a UST app session and a regular LTT session.
1841 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1842 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1844 struct tm
*timeinfo
;
1847 char tmp_shm_path
[PATH_MAX
];
1849 timeinfo
= localtime(&app
->registration_time
);
1850 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1852 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1854 ua_sess
->tracing_id
= usess
->id
;
1855 ua_sess
->id
= get_next_session_id();
1856 ua_sess
->real_credentials
.uid
= app
->uid
;
1857 ua_sess
->real_credentials
.gid
= app
->gid
;
1858 ua_sess
->effective_credentials
.uid
= usess
->uid
;
1859 ua_sess
->effective_credentials
.gid
= usess
->gid
;
1860 ua_sess
->buffer_type
= usess
->buffer_type
;
1861 ua_sess
->bits_per_long
= app
->bits_per_long
;
1863 /* There is only one consumer object per session possible. */
1864 consumer_output_get(usess
->consumer
);
1865 ua_sess
->consumer
= usess
->consumer
;
1867 ua_sess
->output_traces
= usess
->output_traces
;
1868 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1869 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1870 &usess
->metadata_attr
);
1872 switch (ua_sess
->buffer_type
) {
1873 case LTTNG_BUFFER_PER_PID
:
1874 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1875 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1878 case LTTNG_BUFFER_PER_UID
:
1879 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1880 DEFAULT_UST_TRACE_UID_PATH
,
1881 ua_sess
->real_credentials
.uid
,
1882 app
->bits_per_long
);
1889 PERROR("asprintf UST shadow copy session");
1894 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1895 sizeof(ua_sess
->root_shm_path
));
1896 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1897 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1898 sizeof(ua_sess
->shm_path
));
1899 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1900 if (ua_sess
->shm_path
[0]) {
1901 switch (ua_sess
->buffer_type
) {
1902 case LTTNG_BUFFER_PER_PID
:
1903 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1904 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1905 app
->name
, app
->pid
, datetime
);
1907 case LTTNG_BUFFER_PER_UID
:
1908 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1909 "/" DEFAULT_UST_TRACE_UID_PATH
,
1910 app
->uid
, app
->bits_per_long
);
1917 PERROR("sprintf UST shadow copy session");
1921 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1922 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1923 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1928 consumer_output_put(ua_sess
->consumer
);
1932 * Lookup sesison wrapper.
1935 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
1936 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1938 /* Get right UST app session from app */
1939 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1943 * Return ust app session from the app session hashtable using the UST session
1946 static struct ust_app_session
*lookup_session_by_app(
1947 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
1949 struct lttng_ht_iter iter
;
1950 struct lttng_ht_node_u64
*node
;
1952 __lookup_session_by_app(usess
, app
, &iter
);
1953 node
= lttng_ht_iter_get_node_u64(&iter
);
1958 return caa_container_of(node
, struct ust_app_session
, node
);
1965 * Setup buffer registry per PID for the given session and application. If none
1966 * is found, a new one is created, added to the global registry and
1967 * initialized. If regp is valid, it's set with the newly created object.
1969 * Return 0 on success or else a negative value.
1971 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1972 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1975 struct buffer_reg_pid
*reg_pid
;
1982 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1985 * This is the create channel path meaning that if there is NO
1986 * registry available, we have to create one for this session.
1988 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
1989 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1997 /* Initialize registry. */
1998 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1999 app
->bits_per_long
, app
->uint8_t_alignment
,
2000 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2001 app
->uint64_t_alignment
, app
->long_alignment
,
2002 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2003 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2004 ua_sess
->effective_credentials
.uid
,
2005 ua_sess
->effective_credentials
.gid
, ua_sess
->tracing_id
,
2009 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2010 * destroy the buffer registry, because it is always expected
2011 * that if the buffer registry can be found, its ust registry is
2014 buffer_reg_pid_destroy(reg_pid
);
2018 buffer_reg_pid_add(reg_pid
);
2020 DBG3("UST app buffer registry per PID created successfully");
2032 * Setup buffer registry per UID for the given session and application. If none
2033 * is found, a new one is created, added to the global registry and
2034 * initialized. If regp is valid, it's set with the newly created object.
2036 * Return 0 on success or else a negative value.
2038 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2039 struct ust_app_session
*ua_sess
,
2040 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2043 struct buffer_reg_uid
*reg_uid
;
2050 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2053 * This is the create channel path meaning that if there is NO
2054 * registry available, we have to create one for this session.
2056 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2057 LTTNG_DOMAIN_UST
, ®_uid
,
2058 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2066 /* Initialize registry. */
2067 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2068 app
->bits_per_long
, app
->uint8_t_alignment
,
2069 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2070 app
->uint64_t_alignment
, app
->long_alignment
,
2071 app
->byte_order
, app
->version
.major
,
2072 app
->version
.minor
, reg_uid
->root_shm_path
,
2073 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2074 ua_sess
->tracing_id
, app
->uid
);
2077 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2078 * destroy the buffer registry, because it is always expected
2079 * that if the buffer registry can be found, its ust registry is
2082 buffer_reg_uid_destroy(reg_uid
, NULL
);
2085 /* Add node to teardown list of the session. */
2086 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2088 buffer_reg_uid_add(reg_uid
);
2090 DBG3("UST app buffer registry per UID created successfully");
2101 * Create a session on the tracer side for the given app.
2103 * On success, ua_sess_ptr is populated with the session pointer or else left
2104 * untouched. If the session was created, is_created is set to 1. On error,
2105 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2108 * Returns 0 on success or else a negative code which is either -ENOMEM or
2109 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2111 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2112 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2115 int ret
, created
= 0;
2116 struct ust_app_session
*ua_sess
;
2120 assert(ua_sess_ptr
);
2122 health_code_update();
2124 ua_sess
= lookup_session_by_app(usess
, app
);
2125 if (ua_sess
== NULL
) {
2126 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2127 app
->pid
, usess
->id
);
2128 ua_sess
= alloc_ust_app_session();
2129 if (ua_sess
== NULL
) {
2130 /* Only malloc can failed so something is really wrong */
2134 shadow_copy_session(ua_sess
, usess
, app
);
2138 switch (usess
->buffer_type
) {
2139 case LTTNG_BUFFER_PER_PID
:
2140 /* Init local registry. */
2141 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2143 delete_ust_app_session(-1, ua_sess
, app
);
2147 case LTTNG_BUFFER_PER_UID
:
2148 /* Look for a global registry. If none exists, create one. */
2149 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2151 delete_ust_app_session(-1, ua_sess
, app
);
2161 health_code_update();
2163 if (ua_sess
->handle
== -1) {
2164 pthread_mutex_lock(&app
->sock_lock
);
2165 ret
= ustctl_create_session(app
->sock
);
2166 pthread_mutex_unlock(&app
->sock_lock
);
2168 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2169 ERR("Creating session for app pid %d with ret %d",
2172 DBG("UST app creating session failed. Application is dead");
2174 * This is normal behavior, an application can die during the
2175 * creation process. Don't report an error so the execution can
2176 * continue normally. This will get flagged ENOTCONN and the
2177 * caller will handle it.
2181 delete_ust_app_session(-1, ua_sess
, app
);
2182 if (ret
!= -ENOMEM
) {
2184 * Tracer is probably gone or got an internal error so let's
2185 * behave like it will soon unregister or not usable.
2192 ua_sess
->handle
= ret
;
2194 /* Add ust app session to app's HT */
2195 lttng_ht_node_init_u64(&ua_sess
->node
,
2196 ua_sess
->tracing_id
);
2197 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2198 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2199 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2200 &ua_sess
->ust_objd_node
);
2202 DBG2("UST app session created successfully with handle %d", ret
);
2205 *ua_sess_ptr
= ua_sess
;
2207 *is_created
= created
;
2210 /* Everything went well. */
2214 health_code_update();
2219 * Match function for a hash table lookup of ust_app_ctx.
2221 * It matches an ust app context based on the context type and, in the case
2222 * of perf counters, their name.
2224 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2226 struct ust_app_ctx
*ctx
;
2227 const struct lttng_ust_context_attr
*key
;
2232 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2236 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2241 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2242 if (strncmp(key
->u
.perf_counter
.name
,
2243 ctx
->ctx
.u
.perf_counter
.name
,
2244 sizeof(key
->u
.perf_counter
.name
))) {
2248 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2249 if (strcmp(key
->u
.app_ctx
.provider_name
,
2250 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2251 strcmp(key
->u
.app_ctx
.ctx_name
,
2252 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2268 * Lookup for an ust app context from an lttng_ust_context.
2270 * Must be called while holding RCU read side lock.
2271 * Return an ust_app_ctx object or NULL on error.
2274 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2275 struct lttng_ust_context_attr
*uctx
)
2277 struct lttng_ht_iter iter
;
2278 struct lttng_ht_node_ulong
*node
;
2279 struct ust_app_ctx
*app_ctx
= NULL
;
2284 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2285 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2286 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2287 node
= lttng_ht_iter_get_node_ulong(&iter
);
2292 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2299 * Create a context for the channel on the tracer.
2301 * Called with UST app session lock held and a RCU read side lock.
2304 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2305 struct lttng_ust_context_attr
*uctx
,
2306 struct ust_app
*app
)
2309 struct ust_app_ctx
*ua_ctx
;
2311 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2313 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2319 ua_ctx
= alloc_ust_app_ctx(uctx
);
2320 if (ua_ctx
== NULL
) {
2326 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2327 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2328 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2330 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2340 * Enable on the tracer side a ust app event for the session and channel.
2342 * Called with UST app session lock held.
2345 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2346 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2350 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2355 ua_event
->enabled
= 1;
2362 * Disable on the tracer side a ust app event for the session and channel.
2364 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2365 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2369 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2374 ua_event
->enabled
= 0;
2381 * Lookup ust app channel for session and disable it on the tracer side.
2384 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2385 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2389 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2394 ua_chan
->enabled
= 0;
2401 * Lookup ust app channel for session and enable it on the tracer side. This
2402 * MUST be called with a RCU read side lock acquired.
2404 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2405 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2408 struct lttng_ht_iter iter
;
2409 struct lttng_ht_node_str
*ua_chan_node
;
2410 struct ust_app_channel
*ua_chan
;
2412 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2413 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2414 if (ua_chan_node
== NULL
) {
2415 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2416 uchan
->name
, ua_sess
->tracing_id
);
2420 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2422 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2432 * Ask the consumer to create a channel and get it if successful.
2434 * Called with UST app session lock held.
2436 * Return 0 on success or else a negative value.
2438 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2439 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2440 int bitness
, struct ust_registry_session
*registry
,
2441 uint64_t trace_archive_id
)
2444 unsigned int nb_fd
= 0;
2445 struct consumer_socket
*socket
;
2453 health_code_update();
2455 /* Get the right consumer socket for the application. */
2456 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2462 health_code_update();
2464 /* Need one fd for the channel. */
2465 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2467 ERR("Exhausted number of available FD upon create channel");
2472 * Ask consumer to create channel. The consumer will return the number of
2473 * stream we have to expect.
2475 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2476 registry
, usess
->current_trace_chunk
);
2482 * Compute the number of fd needed before receiving them. It must be 2 per
2483 * stream (2 being the default value here).
2485 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2487 /* Reserve the amount of file descriptor we need. */
2488 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2490 ERR("Exhausted number of available FD upon create channel");
2491 goto error_fd_get_stream
;
2494 health_code_update();
2497 * Now get the channel from the consumer. This call wil populate the stream
2498 * list of that channel and set the ust objects.
2500 if (usess
->consumer
->enabled
) {
2501 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2511 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2512 error_fd_get_stream
:
2514 * Initiate a destroy channel on the consumer since we had an error
2515 * handling it on our side. The return value is of no importance since we
2516 * already have a ret value set by the previous error that we need to
2519 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2521 lttng_fd_put(LTTNG_FD_APPS
, 1);
2523 health_code_update();
2529 * Duplicate the ust data object of the ust app stream and save it in the
2530 * buffer registry stream.
2532 * Return 0 on success or else a negative value.
2534 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2535 struct ust_app_stream
*stream
)
2542 /* Reserve the amount of file descriptor we need. */
2543 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2545 ERR("Exhausted number of available FD upon duplicate stream");
2549 /* Duplicate object for stream once the original is in the registry. */
2550 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2551 reg_stream
->obj
.ust
);
2553 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2554 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2555 lttng_fd_put(LTTNG_FD_APPS
, 2);
2558 stream
->handle
= stream
->obj
->handle
;
2565 * Duplicate the ust data object of the ust app. channel and save it in the
2566 * buffer registry channel.
2568 * Return 0 on success or else a negative value.
2570 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2571 struct ust_app_channel
*ua_chan
)
2578 /* Need two fds for the channel. */
2579 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2581 ERR("Exhausted number of available FD upon duplicate channel");
2585 /* Duplicate object for stream once the original is in the registry. */
2586 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2588 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2589 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2592 ua_chan
->handle
= ua_chan
->obj
->handle
;
2597 lttng_fd_put(LTTNG_FD_APPS
, 1);
2603 * For a given channel buffer registry, setup all streams of the given ust
2604 * application channel.
2606 * Return 0 on success or else a negative value.
2608 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2609 struct ust_app_channel
*ua_chan
,
2610 struct ust_app
*app
)
2613 struct ust_app_stream
*stream
, *stmp
;
2618 DBG2("UST app setup buffer registry stream");
2620 /* Send all streams to application. */
2621 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2622 struct buffer_reg_stream
*reg_stream
;
2624 ret
= buffer_reg_stream_create(®_stream
);
2630 * Keep original pointer and nullify it in the stream so the delete
2631 * stream call does not release the object.
2633 reg_stream
->obj
.ust
= stream
->obj
;
2635 buffer_reg_stream_add(reg_stream
, reg_chan
);
2637 /* We don't need the streams anymore. */
2638 cds_list_del(&stream
->list
);
2639 delete_ust_app_stream(-1, stream
, app
);
2647 * Create a buffer registry channel for the given session registry and
2648 * application channel object. If regp pointer is valid, it's set with the
2649 * created object. Important, the created object is NOT added to the session
2650 * registry hash table.
2652 * Return 0 on success else a negative value.
2654 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2655 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2658 struct buffer_reg_channel
*reg_chan
= NULL
;
2663 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2665 /* Create buffer registry channel. */
2666 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2671 reg_chan
->consumer_key
= ua_chan
->key
;
2672 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2673 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2675 /* Create and add a channel registry to session. */
2676 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2677 ua_chan
->tracing_channel_id
);
2681 buffer_reg_channel_add(reg_sess
, reg_chan
);
2690 /* Safe because the registry channel object was not added to any HT. */
2691 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2697 * Setup buffer registry channel for the given session registry and application
2698 * channel object. If regp pointer is valid, it's set with the created object.
2700 * Return 0 on success else a negative value.
2702 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2703 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2704 struct ust_app
*app
)
2711 assert(ua_chan
->obj
);
2713 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2715 /* Setup all streams for the registry. */
2716 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2721 reg_chan
->obj
.ust
= ua_chan
->obj
;
2722 ua_chan
->obj
= NULL
;
2727 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2728 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2733 * Send buffer registry channel to the application.
2735 * Return 0 on success else a negative value.
2737 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2738 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2739 struct ust_app_channel
*ua_chan
)
2742 struct buffer_reg_stream
*reg_stream
;
2749 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2751 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2756 /* Send channel to the application. */
2757 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2758 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2759 ret
= -ENOTCONN
; /* Caused by app exiting. */
2761 } else if (ret
< 0) {
2765 health_code_update();
2767 /* Send all streams to application. */
2768 pthread_mutex_lock(®_chan
->stream_list_lock
);
2769 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2770 struct ust_app_stream stream
;
2772 ret
= duplicate_stream_object(reg_stream
, &stream
);
2774 goto error_stream_unlock
;
2777 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2779 (void) release_ust_app_stream(-1, &stream
, app
);
2780 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2781 ret
= -ENOTCONN
; /* Caused by app exiting. */
2783 goto error_stream_unlock
;
2787 * The return value is not important here. This function will output an
2790 (void) release_ust_app_stream(-1, &stream
, app
);
2792 ua_chan
->is_sent
= 1;
2794 error_stream_unlock
:
2795 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2801 * Create and send to the application the created buffers with per UID buffers.
2803 * This MUST be called with a RCU read side lock acquired.
2804 * The session list lock and the session's lock must be acquired.
2806 * Return 0 on success else a negative value.
2808 static int create_channel_per_uid(struct ust_app
*app
,
2809 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2810 struct ust_app_channel
*ua_chan
)
2813 struct buffer_reg_uid
*reg_uid
;
2814 struct buffer_reg_channel
*reg_chan
;
2815 struct ltt_session
*session
= NULL
;
2816 enum lttng_error_code notification_ret
;
2817 struct ust_registry_channel
*chan_reg
;
2824 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2826 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2828 * The session creation handles the creation of this global registry
2829 * object. If none can be find, there is a code flow problem or a
2834 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2840 /* Create the buffer registry channel object. */
2841 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2843 ERR("Error creating the UST channel \"%s\" registry instance",
2848 session
= session_find_by_id(ua_sess
->tracing_id
);
2850 assert(pthread_mutex_trylock(&session
->lock
));
2851 assert(session_trylock_list());
2854 * Create the buffers on the consumer side. This call populates the
2855 * ust app channel object with all streams and data object.
2857 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2858 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
2859 session
->most_recent_chunk_id
.value
);
2861 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2865 * Let's remove the previously created buffer registry channel so
2866 * it's not visible anymore in the session registry.
2868 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2869 ua_chan
->tracing_channel_id
, false);
2870 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2871 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2876 * Setup the streams and add it to the session registry.
2878 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2879 ua_chan
, reg_chan
, app
);
2881 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
2885 /* Notify the notification subsystem of the channel's creation. */
2886 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
2887 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
2888 ua_chan
->tracing_channel_id
);
2890 chan_reg
->consumer_key
= ua_chan
->key
;
2892 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
2894 notification_ret
= notification_thread_command_add_channel(
2895 notification_thread_handle
, session
->name
,
2896 ua_sess
->effective_credentials
.uid
,
2897 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
2898 ua_chan
->key
, LTTNG_DOMAIN_UST
,
2899 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
2900 if (notification_ret
!= LTTNG_OK
) {
2901 ret
= - (int) notification_ret
;
2902 ERR("Failed to add channel to notification thread");
2907 /* Send buffers to the application. */
2908 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2910 if (ret
!= -ENOTCONN
) {
2911 ERR("Error sending channel to application");
2918 session_put(session
);
2924 * Create and send to the application the created buffers with per PID buffers.
2926 * Called with UST app session lock held.
2927 * The session list lock and the session's lock must be acquired.
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
;
2937 enum lttng_error_code cmd_ret
;
2938 struct ltt_session
*session
= NULL
;
2939 uint64_t chan_reg_key
;
2940 struct ust_registry_channel
*chan_reg
;
2947 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2951 registry
= get_session_registry(ua_sess
);
2952 /* The UST app session lock is held, registry shall not be null. */
2955 /* Create and add a new channel registry to session. */
2956 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2958 ERR("Error creating the UST channel \"%s\" registry instance",
2963 session
= session_find_by_id(ua_sess
->tracing_id
);
2966 assert(pthread_mutex_trylock(&session
->lock
));
2967 assert(session_trylock_list());
2969 /* Create and get channel on the consumer side. */
2970 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2971 app
->bits_per_long
, registry
,
2972 session
->most_recent_chunk_id
.value
);
2974 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2976 goto error_remove_from_registry
;
2979 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2981 if (ret
!= -ENOTCONN
) {
2982 ERR("Error sending channel to application");
2984 goto error_remove_from_registry
;
2987 chan_reg_key
= ua_chan
->key
;
2988 pthread_mutex_lock(®istry
->lock
);
2989 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
2991 chan_reg
->consumer_key
= ua_chan
->key
;
2992 pthread_mutex_unlock(®istry
->lock
);
2994 cmd_ret
= notification_thread_command_add_channel(
2995 notification_thread_handle
, session
->name
,
2996 ua_sess
->effective_credentials
.uid
,
2997 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
2998 ua_chan
->key
, LTTNG_DOMAIN_UST
,
2999 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3000 if (cmd_ret
!= LTTNG_OK
) {
3001 ret
= - (int) cmd_ret
;
3002 ERR("Failed to add channel to notification thread");
3003 goto error_remove_from_registry
;
3006 error_remove_from_registry
:
3008 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3013 session_put(session
);
3019 * From an already allocated ust app channel, create the channel buffers if
3020 * needed and send them to the application. This MUST be called with a RCU read
3021 * side lock acquired.
3023 * Called with UST app session lock held.
3025 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3026 * the application exited concurrently.
3028 static int ust_app_channel_send(struct ust_app
*app
,
3029 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3030 struct ust_app_channel
*ua_chan
)
3036 assert(usess
->active
);
3040 /* Handle buffer type before sending the channel to the application. */
3041 switch (usess
->buffer_type
) {
3042 case LTTNG_BUFFER_PER_UID
:
3044 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3050 case LTTNG_BUFFER_PER_PID
:
3052 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3064 /* Initialize ust objd object using the received handle and add it. */
3065 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3066 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3068 /* If channel is not enabled, disable it on the tracer */
3069 if (!ua_chan
->enabled
) {
3070 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3081 * Create UST app channel and return it through ua_chanp if not NULL.
3083 * Called with UST app session lock and RCU read-side lock held.
3085 * Return 0 on success or else a negative value.
3087 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3088 struct ltt_ust_channel
*uchan
,
3089 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3090 struct ust_app_channel
**ua_chanp
)
3093 struct lttng_ht_iter iter
;
3094 struct lttng_ht_node_str
*ua_chan_node
;
3095 struct ust_app_channel
*ua_chan
;
3097 /* Lookup channel in the ust app session */
3098 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3099 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3100 if (ua_chan_node
!= NULL
) {
3101 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3105 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3106 if (ua_chan
== NULL
) {
3107 /* Only malloc can fail here */
3111 shadow_copy_channel(ua_chan
, uchan
);
3113 /* Set channel type. */
3114 ua_chan
->attr
.type
= type
;
3116 /* Only add the channel if successful on the tracer side. */
3117 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3120 *ua_chanp
= ua_chan
;
3123 /* Everything went well. */
3131 * Create UST app event and create it on the tracer side.
3133 * Called with ust app session mutex held.
3136 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3137 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3138 struct ust_app
*app
)
3141 struct ust_app_event
*ua_event
;
3143 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3144 if (ua_event
== NULL
) {
3145 /* Only failure mode of alloc_ust_app_event(). */
3149 shadow_copy_event(ua_event
, uevent
);
3151 /* Create it on the tracer side */
3152 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3155 * Not found previously means that it does not exist on the
3156 * tracer. If the application reports that the event existed,
3157 * it means there is a bug in the sessiond or lttng-ust
3158 * (or corruption, etc.)
3160 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3161 ERR("Tracer for application reported that an event being created already existed: "
3162 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3164 app
->pid
, app
->ppid
, app
->uid
,
3170 add_unique_ust_app_event(ua_chan
, ua_event
);
3172 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3179 /* Valid. Calling here is already in a read side lock */
3180 delete_ust_app_event(-1, ua_event
, app
);
3185 * Create UST metadata and open it on the tracer side.
3187 * Called with UST app session lock held and RCU read side lock.
3189 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3190 struct ust_app
*app
, struct consumer_output
*consumer
)
3193 struct ust_app_channel
*metadata
;
3194 struct consumer_socket
*socket
;
3195 struct ust_registry_session
*registry
;
3196 struct ltt_session
*session
= NULL
;
3202 registry
= get_session_registry(ua_sess
);
3203 /* The UST app session is held registry shall not be null. */
3206 pthread_mutex_lock(®istry
->lock
);
3208 /* Metadata already exists for this registry or it was closed previously */
3209 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3214 /* Allocate UST metadata */
3215 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3217 /* malloc() failed */
3222 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3224 /* Need one fd for the channel. */
3225 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3227 ERR("Exhausted number of available FD upon create metadata");
3231 /* Get the right consumer socket for the application. */
3232 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3235 goto error_consumer
;
3239 * Keep metadata key so we can identify it on the consumer side. Assign it
3240 * to the registry *before* we ask the consumer so we avoid the race of the
3241 * consumer requesting the metadata and the ask_channel call on our side
3242 * did not returned yet.
3244 registry
->metadata_key
= metadata
->key
;
3246 session
= session_find_by_id(ua_sess
->tracing_id
);
3249 assert(pthread_mutex_trylock(&session
->lock
));
3250 assert(session_trylock_list());
3253 * Ask the metadata channel creation to the consumer. The metadata object
3254 * will be created by the consumer and kept their. However, the stream is
3255 * never added or monitored until we do a first push metadata to the
3258 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3259 registry
, session
->current_trace_chunk
);
3261 /* Nullify the metadata key so we don't try to close it later on. */
3262 registry
->metadata_key
= 0;
3263 goto error_consumer
;
3267 * The setup command will make the metadata stream be sent to the relayd,
3268 * if applicable, and the thread managing the metadatas. This is important
3269 * because after this point, if an error occurs, the only way the stream
3270 * can be deleted is to be monitored in the consumer.
3272 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3274 /* Nullify the metadata key so we don't try to close it later on. */
3275 registry
->metadata_key
= 0;
3276 goto error_consumer
;
3279 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3280 metadata
->key
, app
->pid
);
3283 lttng_fd_put(LTTNG_FD_APPS
, 1);
3284 delete_ust_app_channel(-1, metadata
, app
);
3286 pthread_mutex_unlock(®istry
->lock
);
3288 session_put(session
);
3294 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3295 * acquired before calling this function.
3297 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3299 struct ust_app
*app
= NULL
;
3300 struct lttng_ht_node_ulong
*node
;
3301 struct lttng_ht_iter iter
;
3303 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3304 node
= lttng_ht_iter_get_node_ulong(&iter
);
3306 DBG2("UST app no found with pid %d", pid
);
3310 DBG2("Found UST app by pid %d", pid
);
3312 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3319 * Allocate and init an UST app object using the registration information and
3320 * the command socket. This is called when the command socket connects to the
3323 * The object is returned on success or else NULL.
3325 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3327 struct ust_app
*lta
= NULL
;
3332 DBG3("UST app creating application for socket %d", sock
);
3334 if ((msg
->bits_per_long
== 64 &&
3335 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3336 || (msg
->bits_per_long
== 32 &&
3337 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3338 ERR("Registration failed: application \"%s\" (pid: %d) has "
3339 "%d-bit long, but no consumerd for this size is available.\n",
3340 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3344 lta
= zmalloc(sizeof(struct ust_app
));
3350 lta
->ppid
= msg
->ppid
;
3351 lta
->uid
= msg
->uid
;
3352 lta
->gid
= msg
->gid
;
3354 lta
->bits_per_long
= msg
->bits_per_long
;
3355 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3356 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3357 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3358 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3359 lta
->long_alignment
= msg
->long_alignment
;
3360 lta
->byte_order
= msg
->byte_order
;
3362 lta
->v_major
= msg
->major
;
3363 lta
->v_minor
= msg
->minor
;
3364 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3365 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3366 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3367 lta
->notify_sock
= -1;
3369 /* Copy name and make sure it's NULL terminated. */
3370 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3371 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3374 * Before this can be called, when receiving the registration information,
3375 * the application compatibility is checked. So, at this point, the
3376 * application can work with this session daemon.
3378 lta
->compatible
= 1;
3380 lta
->pid
= msg
->pid
;
3381 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3383 pthread_mutex_init(<a
->sock_lock
, NULL
);
3384 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3386 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3392 * For a given application object, add it to every hash table.
3394 void ust_app_add(struct ust_app
*app
)
3397 assert(app
->notify_sock
>= 0);
3399 app
->registration_time
= time(NULL
);
3404 * On a re-registration, we want to kick out the previous registration of
3407 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3410 * The socket _should_ be unique until _we_ call close. So, a add_unique
3411 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3412 * already in the table.
3414 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3416 /* Add application to the notify socket hash table. */
3417 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3418 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3420 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3421 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3422 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3429 * Set the application version into the object.
3431 * Return 0 on success else a negative value either an errno code or a
3432 * LTTng-UST error code.
3434 int ust_app_version(struct ust_app
*app
)
3440 pthread_mutex_lock(&app
->sock_lock
);
3441 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3442 pthread_mutex_unlock(&app
->sock_lock
);
3444 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3445 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3447 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3455 * Unregister app by removing it from the global traceable app list and freeing
3458 * The socket is already closed at this point so no close to sock.
3460 void ust_app_unregister(int sock
)
3462 struct ust_app
*lta
;
3463 struct lttng_ht_node_ulong
*node
;
3464 struct lttng_ht_iter ust_app_sock_iter
;
3465 struct lttng_ht_iter iter
;
3466 struct ust_app_session
*ua_sess
;
3471 /* Get the node reference for a call_rcu */
3472 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3473 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3476 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3477 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3480 * For per-PID buffers, perform "push metadata" and flush all
3481 * application streams before removing app from hash tables,
3482 * ensuring proper behavior of data_pending check.
3483 * Remove sessions so they are not visible during deletion.
3485 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3487 struct ust_registry_session
*registry
;
3489 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3491 /* The session was already removed so scheduled for teardown. */
3495 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3496 (void) ust_app_flush_app_session(lta
, ua_sess
);
3500 * Add session to list for teardown. This is safe since at this point we
3501 * are the only one using this list.
3503 pthread_mutex_lock(&ua_sess
->lock
);
3505 if (ua_sess
->deleted
) {
3506 pthread_mutex_unlock(&ua_sess
->lock
);
3511 * Normally, this is done in the delete session process which is
3512 * executed in the call rcu below. However, upon registration we can't
3513 * afford to wait for the grace period before pushing data or else the
3514 * data pending feature can race between the unregistration and stop
3515 * command where the data pending command is sent *before* the grace
3518 * The close metadata below nullifies the metadata pointer in the
3519 * session so the delete session will NOT push/close a second time.
3521 registry
= get_session_registry(ua_sess
);
3523 /* Push metadata for application before freeing the application. */
3524 (void) push_metadata(registry
, ua_sess
->consumer
);
3527 * Don't ask to close metadata for global per UID buffers. Close
3528 * metadata only on destroy trace session in this case. Also, the
3529 * previous push metadata could have flag the metadata registry to
3530 * close so don't send a close command if closed.
3532 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3533 /* And ask to close it for this session registry. */
3534 (void) close_metadata(registry
, ua_sess
->consumer
);
3537 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3539 pthread_mutex_unlock(&ua_sess
->lock
);
3542 /* Remove application from PID hash table */
3543 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3547 * Remove application from notify hash table. The thread handling the
3548 * notify socket could have deleted the node so ignore on error because
3549 * either way it's valid. The close of that socket is handled by the
3550 * apps_notify_thread.
3552 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3553 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3556 * Ignore return value since the node might have been removed before by an
3557 * add replace during app registration because the PID can be reassigned by
3560 iter
.iter
.node
= <a
->pid_n
.node
;
3561 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3563 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3568 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3575 * Fill events array with all events name of all registered apps.
3577 int ust_app_list_events(struct lttng_event
**events
)
3580 size_t nbmem
, count
= 0;
3581 struct lttng_ht_iter iter
;
3582 struct ust_app
*app
;
3583 struct lttng_event
*tmp_event
;
3585 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3586 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3587 if (tmp_event
== NULL
) {
3588 PERROR("zmalloc ust app events");
3595 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3596 struct lttng_ust_tracepoint_iter uiter
;
3598 health_code_update();
3600 if (!app
->compatible
) {
3602 * TODO: In time, we should notice the caller of this error by
3603 * telling him that this is a version error.
3607 pthread_mutex_lock(&app
->sock_lock
);
3608 handle
= ustctl_tracepoint_list(app
->sock
);
3610 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3611 ERR("UST app list events getting handle failed for app pid %d",
3614 pthread_mutex_unlock(&app
->sock_lock
);
3618 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3619 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3620 /* Handle ustctl error. */
3624 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3625 ERR("UST app tp list get failed for app %d with ret %d",
3628 DBG3("UST app tp list get failed. Application is dead");
3630 * This is normal behavior, an application can die during the
3631 * creation process. Don't report an error so the execution can
3632 * continue normally. Continue normal execution.
3637 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3638 if (release_ret
< 0 &&
3639 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3640 release_ret
!= -EPIPE
) {
3641 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3643 pthread_mutex_unlock(&app
->sock_lock
);
3647 health_code_update();
3648 if (count
>= nbmem
) {
3649 /* In case the realloc fails, we free the memory */
3650 struct lttng_event
*new_tmp_event
;
3653 new_nbmem
= nbmem
<< 1;
3654 DBG2("Reallocating event list from %zu to %zu entries",
3656 new_tmp_event
= realloc(tmp_event
,
3657 new_nbmem
* sizeof(struct lttng_event
));
3658 if (new_tmp_event
== NULL
) {
3661 PERROR("realloc ust app events");
3664 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3665 if (release_ret
< 0 &&
3666 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3667 release_ret
!= -EPIPE
) {
3668 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3670 pthread_mutex_unlock(&app
->sock_lock
);
3673 /* Zero the new memory */
3674 memset(new_tmp_event
+ nbmem
, 0,
3675 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3677 tmp_event
= new_tmp_event
;
3679 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3680 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3681 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3682 tmp_event
[count
].pid
= app
->pid
;
3683 tmp_event
[count
].enabled
= -1;
3686 ret
= ustctl_release_handle(app
->sock
, handle
);
3687 pthread_mutex_unlock(&app
->sock_lock
);
3688 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3689 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3694 *events
= tmp_event
;
3696 DBG2("UST app list events done (%zu events)", count
);
3701 health_code_update();
3706 * Fill events array with all events name of all registered apps.
3708 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3711 size_t nbmem
, count
= 0;
3712 struct lttng_ht_iter iter
;
3713 struct ust_app
*app
;
3714 struct lttng_event_field
*tmp_event
;
3716 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3717 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3718 if (tmp_event
== NULL
) {
3719 PERROR("zmalloc ust app event fields");
3726 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3727 struct lttng_ust_field_iter uiter
;
3729 health_code_update();
3731 if (!app
->compatible
) {
3733 * TODO: In time, we should notice the caller of this error by
3734 * telling him that this is a version error.
3738 pthread_mutex_lock(&app
->sock_lock
);
3739 handle
= ustctl_tracepoint_field_list(app
->sock
);
3741 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3742 ERR("UST app list field getting handle failed for app pid %d",
3745 pthread_mutex_unlock(&app
->sock_lock
);
3749 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3750 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3751 /* Handle ustctl error. */
3755 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3756 ERR("UST app tp list field failed for app %d with ret %d",
3759 DBG3("UST app tp list field failed. Application is dead");
3761 * This is normal behavior, an application can die during the
3762 * creation process. Don't report an error so the execution can
3763 * continue normally. Reset list and count for next app.
3768 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3769 pthread_mutex_unlock(&app
->sock_lock
);
3770 if (release_ret
< 0 &&
3771 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3772 release_ret
!= -EPIPE
) {
3773 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3778 health_code_update();
3779 if (count
>= nbmem
) {
3780 /* In case the realloc fails, we free the memory */
3781 struct lttng_event_field
*new_tmp_event
;
3784 new_nbmem
= nbmem
<< 1;
3785 DBG2("Reallocating event field list from %zu to %zu entries",
3787 new_tmp_event
= realloc(tmp_event
,
3788 new_nbmem
* sizeof(struct lttng_event_field
));
3789 if (new_tmp_event
== NULL
) {
3792 PERROR("realloc ust app event fields");
3795 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3796 pthread_mutex_unlock(&app
->sock_lock
);
3798 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3799 release_ret
!= -EPIPE
) {
3800 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3804 /* Zero the new memory */
3805 memset(new_tmp_event
+ nbmem
, 0,
3806 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3808 tmp_event
= new_tmp_event
;
3811 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3812 /* Mapping between these enums matches 1 to 1. */
3813 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3814 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3816 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3817 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3818 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3819 tmp_event
[count
].event
.pid
= app
->pid
;
3820 tmp_event
[count
].event
.enabled
= -1;
3823 ret
= ustctl_release_handle(app
->sock
, handle
);
3824 pthread_mutex_unlock(&app
->sock_lock
);
3826 ret
!= -LTTNG_UST_ERR_EXITING
&&
3828 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3833 *fields
= tmp_event
;
3835 DBG2("UST app list event fields done (%zu events)", count
);
3840 health_code_update();
3845 * Free and clean all traceable apps of the global list.
3847 * Should _NOT_ be called with RCU read-side lock held.
3849 void ust_app_clean_list(void)
3852 struct ust_app
*app
;
3853 struct lttng_ht_iter iter
;
3855 DBG2("UST app cleaning registered apps hash table");
3859 /* Cleanup notify socket hash table */
3860 if (ust_app_ht_by_notify_sock
) {
3861 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3862 notify_sock_n
.node
) {
3863 struct cds_lfht_node
*node
;
3864 struct ust_app
*app
;
3866 node
= cds_lfht_iter_get_node(&iter
.iter
);
3871 app
= container_of(node
, struct ust_app
,
3872 notify_sock_n
.node
);
3873 ust_app_notify_sock_unregister(app
->notify_sock
);
3878 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3879 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3881 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3885 /* Cleanup socket hash table */
3886 if (ust_app_ht_by_sock
) {
3887 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3889 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3896 /* Destroy is done only when the ht is empty */
3898 ht_cleanup_push(ust_app_ht
);
3900 if (ust_app_ht_by_sock
) {
3901 ht_cleanup_push(ust_app_ht_by_sock
);
3903 if (ust_app_ht_by_notify_sock
) {
3904 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3909 * Init UST app hash table.
3911 int ust_app_ht_alloc(void)
3913 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3917 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3918 if (!ust_app_ht_by_sock
) {
3921 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3922 if (!ust_app_ht_by_notify_sock
) {
3929 * For a specific UST session, disable the channel for all registered apps.
3931 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3932 struct ltt_ust_channel
*uchan
)
3935 struct lttng_ht_iter iter
;
3936 struct lttng_ht_node_str
*ua_chan_node
;
3937 struct ust_app
*app
;
3938 struct ust_app_session
*ua_sess
;
3939 struct ust_app_channel
*ua_chan
;
3941 assert(usess
->active
);
3942 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3943 uchan
->name
, usess
->id
);
3947 /* For every registered applications */
3948 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3949 struct lttng_ht_iter uiter
;
3950 if (!app
->compatible
) {
3952 * TODO: In time, we should notice the caller of this error by
3953 * telling him that this is a version error.
3957 ua_sess
= lookup_session_by_app(usess
, app
);
3958 if (ua_sess
== NULL
) {
3963 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3964 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3965 /* If the session if found for the app, the channel must be there */
3966 assert(ua_chan_node
);
3968 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3969 /* The channel must not be already disabled */
3970 assert(ua_chan
->enabled
== 1);
3972 /* Disable channel onto application */
3973 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3975 /* XXX: We might want to report this error at some point... */
3985 * For a specific UST session, enable the channel for all registered apps.
3987 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3988 struct ltt_ust_channel
*uchan
)
3991 struct lttng_ht_iter iter
;
3992 struct ust_app
*app
;
3993 struct ust_app_session
*ua_sess
;
3995 assert(usess
->active
);
3996 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3997 uchan
->name
, usess
->id
);
4001 /* For every registered applications */
4002 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4003 if (!app
->compatible
) {
4005 * TODO: In time, we should notice the caller of this error by
4006 * telling him that this is a version error.
4010 ua_sess
= lookup_session_by_app(usess
, app
);
4011 if (ua_sess
== NULL
) {
4015 /* Enable channel onto application */
4016 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4018 /* XXX: We might want to report this error at some point... */
4028 * Disable an event in a channel and for a specific session.
4030 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4031 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4034 struct lttng_ht_iter iter
, uiter
;
4035 struct lttng_ht_node_str
*ua_chan_node
;
4036 struct ust_app
*app
;
4037 struct ust_app_session
*ua_sess
;
4038 struct ust_app_channel
*ua_chan
;
4039 struct ust_app_event
*ua_event
;
4041 assert(usess
->active
);
4042 DBG("UST app disabling event %s for all apps in channel "
4043 "%s for session id %" PRIu64
,
4044 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4048 /* For all registered applications */
4049 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4050 if (!app
->compatible
) {
4052 * TODO: In time, we should notice the caller of this error by
4053 * telling him that this is a version error.
4057 ua_sess
= lookup_session_by_app(usess
, app
);
4058 if (ua_sess
== NULL
) {
4063 /* Lookup channel in the ust app session */
4064 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4065 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4066 if (ua_chan_node
== NULL
) {
4067 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4068 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4071 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4073 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4074 uevent
->filter
, uevent
->attr
.loglevel
,
4076 if (ua_event
== NULL
) {
4077 DBG2("Event %s not found in channel %s for app pid %d."
4078 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4082 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4084 /* XXX: Report error someday... */
4093 /* The ua_sess lock must be held by the caller. */
4095 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4096 struct ust_app_session
*ua_sess
,
4097 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4098 struct ust_app_channel
**_ua_chan
)
4101 struct ust_app_channel
*ua_chan
= NULL
;
4104 ASSERT_LOCKED(ua_sess
->lock
);
4106 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4107 sizeof(uchan
->name
))) {
4108 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4112 struct ltt_ust_context
*uctx
= NULL
;
4115 * Create channel onto application and synchronize its
4118 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4119 LTTNG_UST_CHAN_PER_CPU
, usess
,
4122 ret
= ust_app_channel_send(app
, usess
,
4129 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4130 ret
= create_ust_app_channel_context(ua_chan
,
4141 * The application's socket is not valid. Either a bad socket
4142 * or a timeout on it. We can't inform the caller that for a
4143 * specific app, the session failed so lets continue here.
4145 ret
= 0; /* Not an error. */
4153 if (ret
== 0 && _ua_chan
) {
4155 * Only return the application's channel on success. Note
4156 * that the channel can still be part of the application's
4157 * channel hashtable on error.
4159 *_ua_chan
= ua_chan
;
4165 * For a specific UST session, create the channel for all registered apps.
4167 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
4168 struct ltt_ust_channel
*uchan
)
4171 struct cds_lfht_iter iter
;
4172 struct ust_app
*app
;
4175 assert(usess
->active
);
4178 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
4179 uchan
->name
, usess
->id
);
4182 /* For every registered applications */
4183 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
, app
, pid_n
.node
) {
4184 struct ust_app_session
*ua_sess
;
4185 int session_was_created
= 0;
4187 if (!app
->compatible
||
4188 !trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4189 goto error_rcu_unlock
;
4193 * Create session on the tracer side and add it to app session HT. Note
4194 * that if session exist, it will simply return a pointer to the ust
4197 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
,
4198 &session_was_created
);
4203 * The application's socket is not valid. Either a bad
4204 * socket or a timeout on it. We can't inform the caller
4205 * that for a specific app, the session failed so lets
4206 * continue here; it is not an error.
4209 goto error_rcu_unlock
;
4212 goto error_rcu_unlock
;
4216 if (ua_sess
->deleted
) {
4219 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, NULL
);
4221 if (session_was_created
) {
4222 destroy_app_session(app
, ua_sess
);
4224 /* Continue to the next application. */
4234 * Enable event for a specific session and channel on the tracer.
4236 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4237 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4240 struct lttng_ht_iter iter
, uiter
;
4241 struct lttng_ht_node_str
*ua_chan_node
;
4242 struct ust_app
*app
;
4243 struct ust_app_session
*ua_sess
;
4244 struct ust_app_channel
*ua_chan
;
4245 struct ust_app_event
*ua_event
;
4247 assert(usess
->active
);
4248 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4249 uevent
->attr
.name
, usess
->id
);
4252 * NOTE: At this point, this function is called only if the session and
4253 * channel passed are already created for all apps. and enabled on the
4259 /* For all registered applications */
4260 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4261 if (!app
->compatible
) {
4263 * TODO: In time, we should notice the caller of this error by
4264 * telling him that this is a version error.
4268 ua_sess
= lookup_session_by_app(usess
, app
);
4270 /* The application has problem or is probably dead. */
4274 pthread_mutex_lock(&ua_sess
->lock
);
4276 if (ua_sess
->deleted
) {
4277 pthread_mutex_unlock(&ua_sess
->lock
);
4281 /* Lookup channel in the ust app session */
4282 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4283 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4285 * It is possible that the channel cannot be found is
4286 * the channel/event creation occurs concurrently with
4287 * an application exit.
4289 if (!ua_chan_node
) {
4290 pthread_mutex_unlock(&ua_sess
->lock
);
4294 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4296 /* Get event node */
4297 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4298 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4299 if (ua_event
== NULL
) {
4300 DBG3("UST app enable event %s not found for app PID %d."
4301 "Skipping app", uevent
->attr
.name
, app
->pid
);
4305 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4307 pthread_mutex_unlock(&ua_sess
->lock
);
4311 pthread_mutex_unlock(&ua_sess
->lock
);
4320 * For a specific existing UST session and UST channel, creates the event for
4321 * all registered apps.
4323 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4324 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4327 struct lttng_ht_iter iter
, uiter
;
4328 struct lttng_ht_node_str
*ua_chan_node
;
4329 struct ust_app
*app
;
4330 struct ust_app_session
*ua_sess
;
4331 struct ust_app_channel
*ua_chan
;
4333 assert(usess
->active
);
4334 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4335 uevent
->attr
.name
, usess
->id
);
4339 /* For all registered applications */
4340 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4341 if (!app
->compatible
) {
4343 * TODO: In time, we should notice the caller of this error by
4344 * telling him that this is a version error.
4348 ua_sess
= lookup_session_by_app(usess
, app
);
4350 /* The application has problem or is probably dead. */
4354 pthread_mutex_lock(&ua_sess
->lock
);
4356 if (ua_sess
->deleted
) {
4357 pthread_mutex_unlock(&ua_sess
->lock
);
4361 /* Lookup channel in the ust app session */
4362 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4363 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4364 /* If the channel is not found, there is a code flow error */
4365 assert(ua_chan_node
);
4367 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4369 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4370 pthread_mutex_unlock(&ua_sess
->lock
);
4372 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4373 /* Possible value at this point: -ENOMEM. If so, we stop! */
4376 DBG2("UST app event %s already exist on app PID %d",
4377 uevent
->attr
.name
, app
->pid
);
4387 * Start tracing for a specific UST session and app.
4389 * Called with UST app session lock held.
4393 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4396 struct ust_app_session
*ua_sess
;
4398 DBG("Starting tracing for ust app pid %d", app
->pid
);
4402 if (!app
->compatible
) {
4406 ua_sess
= lookup_session_by_app(usess
, app
);
4407 if (ua_sess
== NULL
) {
4408 /* The session is in teardown process. Ignore and continue. */
4412 pthread_mutex_lock(&ua_sess
->lock
);
4414 if (ua_sess
->deleted
) {
4415 pthread_mutex_unlock(&ua_sess
->lock
);
4419 /* Upon restart, we skip the setup, already done */
4420 if (ua_sess
->started
) {
4425 * Create the metadata for the application. This returns gracefully if a
4426 * metadata was already set for the session.
4428 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4433 health_code_update();
4436 /* This starts the UST tracing */
4437 pthread_mutex_lock(&app
->sock_lock
);
4438 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4439 pthread_mutex_unlock(&app
->sock_lock
);
4441 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4442 ERR("Error starting tracing for app pid: %d (ret: %d)",
4445 DBG("UST app start session failed. Application is dead.");
4447 * This is normal behavior, an application can die during the
4448 * creation process. Don't report an error so the execution can
4449 * continue normally.
4451 pthread_mutex_unlock(&ua_sess
->lock
);
4457 /* Indicate that the session has been started once */
4458 ua_sess
->started
= 1;
4460 pthread_mutex_unlock(&ua_sess
->lock
);
4462 health_code_update();
4464 /* Quiescent wait after starting trace */
4465 pthread_mutex_lock(&app
->sock_lock
);
4466 ret
= ustctl_wait_quiescent(app
->sock
);
4467 pthread_mutex_unlock(&app
->sock_lock
);
4468 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4469 ERR("UST app wait quiescent failed for app pid %d ret %d",
4475 health_code_update();
4479 pthread_mutex_unlock(&ua_sess
->lock
);
4481 health_code_update();
4486 * Stop tracing for a specific UST session and app.
4489 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4492 struct ust_app_session
*ua_sess
;
4493 struct ust_registry_session
*registry
;
4495 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4499 if (!app
->compatible
) {
4500 goto end_no_session
;
4503 ua_sess
= lookup_session_by_app(usess
, app
);
4504 if (ua_sess
== NULL
) {
4505 goto end_no_session
;
4508 pthread_mutex_lock(&ua_sess
->lock
);
4510 if (ua_sess
->deleted
) {
4511 pthread_mutex_unlock(&ua_sess
->lock
);
4512 goto end_no_session
;
4516 * If started = 0, it means that stop trace has been called for a session
4517 * that was never started. It's possible since we can have a fail start
4518 * from either the application manager thread or the command thread. Simply
4519 * indicate that this is a stop error.
4521 if (!ua_sess
->started
) {
4522 goto error_rcu_unlock
;
4525 health_code_update();
4527 /* This inhibits UST tracing */
4528 pthread_mutex_lock(&app
->sock_lock
);
4529 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4530 pthread_mutex_unlock(&app
->sock_lock
);
4532 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4533 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4536 DBG("UST app stop session failed. Application is dead.");
4538 * This is normal behavior, an application can die during the
4539 * creation process. Don't report an error so the execution can
4540 * continue normally.
4544 goto error_rcu_unlock
;
4547 health_code_update();
4549 /* Quiescent wait after stopping trace */
4550 pthread_mutex_lock(&app
->sock_lock
);
4551 ret
= ustctl_wait_quiescent(app
->sock
);
4552 pthread_mutex_unlock(&app
->sock_lock
);
4553 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4554 ERR("UST app wait quiescent failed for app pid %d ret %d",
4558 health_code_update();
4560 registry
= get_session_registry(ua_sess
);
4562 /* The UST app session is held registry shall not be null. */
4565 /* Push metadata for application before freeing the application. */
4566 (void) push_metadata(registry
, ua_sess
->consumer
);
4569 pthread_mutex_unlock(&ua_sess
->lock
);
4572 health_code_update();
4576 pthread_mutex_unlock(&ua_sess
->lock
);
4578 health_code_update();
4583 int ust_app_flush_app_session(struct ust_app
*app
,
4584 struct ust_app_session
*ua_sess
)
4586 int ret
, retval
= 0;
4587 struct lttng_ht_iter iter
;
4588 struct ust_app_channel
*ua_chan
;
4589 struct consumer_socket
*socket
;
4591 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4595 if (!app
->compatible
) {
4596 goto end_not_compatible
;
4599 pthread_mutex_lock(&ua_sess
->lock
);
4601 if (ua_sess
->deleted
) {
4605 health_code_update();
4607 /* Flushing buffers */
4608 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4611 /* Flush buffers and push metadata. */
4612 switch (ua_sess
->buffer_type
) {
4613 case LTTNG_BUFFER_PER_PID
:
4614 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4616 health_code_update();
4617 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4619 ERR("Error flushing consumer channel");
4625 case LTTNG_BUFFER_PER_UID
:
4631 health_code_update();
4634 pthread_mutex_unlock(&ua_sess
->lock
);
4638 health_code_update();
4643 * Flush buffers for all applications for a specific UST session.
4644 * Called with UST session lock held.
4647 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4652 DBG("Flushing session buffers for all ust apps");
4656 /* Flush buffers and push metadata. */
4657 switch (usess
->buffer_type
) {
4658 case LTTNG_BUFFER_PER_UID
:
4660 struct buffer_reg_uid
*reg
;
4661 struct lttng_ht_iter iter
;
4663 /* Flush all per UID buffers associated to that session. */
4664 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4665 struct ust_registry_session
*ust_session_reg
;
4666 struct buffer_reg_channel
*reg_chan
;
4667 struct consumer_socket
*socket
;
4669 /* Get consumer socket to use to push the metadata.*/
4670 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4673 /* Ignore request if no consumer is found for the session. */
4677 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4678 reg_chan
, node
.node
) {
4680 * The following call will print error values so the return
4681 * code is of little importance because whatever happens, we
4682 * have to try them all.
4684 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4687 ust_session_reg
= reg
->registry
->reg
.ust
;
4688 /* Push metadata. */
4689 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4693 case LTTNG_BUFFER_PER_PID
:
4695 struct ust_app_session
*ua_sess
;
4696 struct lttng_ht_iter iter
;
4697 struct ust_app
*app
;
4699 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4700 ua_sess
= lookup_session_by_app(usess
, app
);
4701 if (ua_sess
== NULL
) {
4704 (void) ust_app_flush_app_session(app
, ua_sess
);
4715 health_code_update();
4720 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
4721 struct ust_app_session
*ua_sess
)
4724 struct lttng_ht_iter iter
;
4725 struct ust_app_channel
*ua_chan
;
4726 struct consumer_socket
*socket
;
4728 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
4732 if (!app
->compatible
) {
4733 goto end_not_compatible
;
4736 pthread_mutex_lock(&ua_sess
->lock
);
4738 if (ua_sess
->deleted
) {
4742 health_code_update();
4744 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4747 ERR("Failed to find consumer (%" PRIu32
") socket",
4748 app
->bits_per_long
);
4753 /* Clear quiescent state. */
4754 switch (ua_sess
->buffer_type
) {
4755 case LTTNG_BUFFER_PER_PID
:
4756 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
4757 ua_chan
, node
.node
) {
4758 health_code_update();
4759 ret
= consumer_clear_quiescent_channel(socket
,
4762 ERR("Error clearing quiescent state for consumer channel");
4768 case LTTNG_BUFFER_PER_UID
:
4775 health_code_update();
4778 pthread_mutex_unlock(&ua_sess
->lock
);
4782 health_code_update();
4787 * Clear quiescent state in each stream for all applications for a
4788 * specific UST session.
4789 * Called with UST session lock held.
4792 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
4797 DBG("Clearing stream quiescent state for all ust apps");
4801 switch (usess
->buffer_type
) {
4802 case LTTNG_BUFFER_PER_UID
:
4804 struct lttng_ht_iter iter
;
4805 struct buffer_reg_uid
*reg
;
4808 * Clear quiescent for all per UID buffers associated to
4811 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4812 struct consumer_socket
*socket
;
4813 struct buffer_reg_channel
*reg_chan
;
4815 /* Get associated consumer socket.*/
4816 socket
= consumer_find_socket_by_bitness(
4817 reg
->bits_per_long
, usess
->consumer
);
4820 * Ignore request if no consumer is found for
4826 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
4827 &iter
.iter
, reg_chan
, node
.node
) {
4829 * The following call will print error values so
4830 * the return code is of little importance
4831 * because whatever happens, we have to try them
4834 (void) consumer_clear_quiescent_channel(socket
,
4835 reg_chan
->consumer_key
);
4840 case LTTNG_BUFFER_PER_PID
:
4842 struct ust_app_session
*ua_sess
;
4843 struct lttng_ht_iter iter
;
4844 struct ust_app
*app
;
4846 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
4848 ua_sess
= lookup_session_by_app(usess
, app
);
4849 if (ua_sess
== NULL
) {
4852 (void) ust_app_clear_quiescent_app_session(app
,
4864 health_code_update();
4869 * Destroy a specific UST session in apps.
4871 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4874 struct ust_app_session
*ua_sess
;
4875 struct lttng_ht_iter iter
;
4876 struct lttng_ht_node_u64
*node
;
4878 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4882 if (!app
->compatible
) {
4886 __lookup_session_by_app(usess
, app
, &iter
);
4887 node
= lttng_ht_iter_get_node_u64(&iter
);
4889 /* Session is being or is deleted. */
4892 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4894 health_code_update();
4895 destroy_app_session(app
, ua_sess
);
4897 health_code_update();
4899 /* Quiescent wait after stopping trace */
4900 pthread_mutex_lock(&app
->sock_lock
);
4901 ret
= ustctl_wait_quiescent(app
->sock
);
4902 pthread_mutex_unlock(&app
->sock_lock
);
4903 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4904 ERR("UST app wait quiescent failed for app pid %d ret %d",
4909 health_code_update();
4914 * Start tracing for the UST session.
4916 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4918 struct lttng_ht_iter iter
;
4919 struct ust_app
*app
;
4921 DBG("Starting all UST traces");
4924 * Even though the start trace might fail, flag this session active so
4925 * other application coming in are started by default.
4932 * In a start-stop-start use-case, we need to clear the quiescent state
4933 * of each channel set by the prior stop command, thus ensuring that a
4934 * following stop or destroy is sure to grab a timestamp_end near those
4935 * operations, even if the packet is empty.
4937 (void) ust_app_clear_quiescent_session(usess
);
4939 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4940 ust_app_global_update(usess
, app
);
4949 * Start tracing for the UST session.
4950 * Called with UST session lock held.
4952 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4955 struct lttng_ht_iter iter
;
4956 struct ust_app
*app
;
4958 DBG("Stopping all UST traces");
4961 * Even though the stop trace might fail, flag this session inactive so
4962 * other application coming in are not started by default.
4968 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4969 ret
= ust_app_stop_trace(usess
, app
);
4971 /* Continue to next apps even on error */
4976 (void) ust_app_flush_session(usess
);
4984 * Destroy app UST session.
4986 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4989 struct lttng_ht_iter iter
;
4990 struct ust_app
*app
;
4992 DBG("Destroy all UST traces");
4996 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4997 ret
= destroy_trace(usess
, app
);
4999 /* Continue to next apps even on error */
5009 /* The ua_sess lock must be held by the caller. */
5011 int find_or_create_ust_app_channel(
5012 struct ltt_ust_session
*usess
,
5013 struct ust_app_session
*ua_sess
,
5014 struct ust_app
*app
,
5015 struct ltt_ust_channel
*uchan
,
5016 struct ust_app_channel
**ua_chan
)
5019 struct lttng_ht_iter iter
;
5020 struct lttng_ht_node_str
*ua_chan_node
;
5022 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5023 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5025 *ua_chan
= caa_container_of(ua_chan_node
,
5026 struct ust_app_channel
, node
);
5030 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5039 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5040 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5041 struct ust_app
*app
)
5044 struct ust_app_event
*ua_event
= NULL
;
5046 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5047 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5049 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5054 if (ua_event
->enabled
!= uevent
->enabled
) {
5055 ret
= uevent
->enabled
?
5056 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5057 disable_ust_app_event(ua_sess
, ua_event
, app
);
5066 * The caller must ensure that the application is compatible and is tracked
5067 * by the PID tracker.
5070 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5071 struct ust_app
*app
)
5074 struct cds_lfht_iter uchan_iter
;
5075 struct ltt_ust_channel
*uchan
;
5076 struct ust_app_session
*ua_sess
= NULL
;
5079 * The application's configuration should only be synchronized for
5082 assert(usess
->active
);
5084 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5086 /* Tracer is probably gone or ENOMEM. */
5091 pthread_mutex_lock(&ua_sess
->lock
);
5092 if (ua_sess
->deleted
) {
5093 pthread_mutex_unlock(&ua_sess
->lock
);
5098 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5100 struct ust_app_channel
*ua_chan
;
5101 struct cds_lfht_iter uevent_iter
;
5102 struct ltt_ust_event
*uevent
;
5105 * Search for a matching ust_app_channel. If none is found,
5106 * create it. Creating the channel will cause the ua_chan
5107 * structure to be allocated, the channel buffers to be
5108 * allocated (if necessary) and sent to the application, and
5109 * all enabled contexts will be added to the channel.
5111 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5112 app
, uchan
, &ua_chan
);
5114 /* Tracer is probably gone or ENOMEM. */
5119 /* ua_chan will be NULL for the metadata channel */
5123 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5125 ret
= ust_app_channel_synchronize_event(ua_chan
,
5126 uevent
, ua_sess
, app
);
5132 if (ua_chan
->enabled
!= uchan
->enabled
) {
5133 ret
= uchan
->enabled
?
5134 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5135 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5144 pthread_mutex_unlock(&ua_sess
->lock
);
5145 /* Everything went well at this point. */
5150 pthread_mutex_unlock(&ua_sess
->lock
);
5153 destroy_app_session(app
, ua_sess
);
5159 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5161 struct ust_app_session
*ua_sess
;
5163 ua_sess
= lookup_session_by_app(usess
, app
);
5164 if (ua_sess
== NULL
) {
5167 destroy_app_session(app
, ua_sess
);
5171 * Add channels/events from UST global domain to registered apps at sock.
5173 * Called with session lock held.
5174 * Called with RCU read-side lock held.
5176 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5179 assert(usess
->active
);
5181 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5182 app
->sock
, usess
->id
);
5184 if (!app
->compatible
) {
5187 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
5189 * Synchronize the application's internal tracing configuration
5190 * and start tracing.
5192 ust_app_synchronize(usess
, app
);
5193 ust_app_start_trace(usess
, app
);
5195 ust_app_global_destroy(usess
, app
);
5200 * Called with session lock held.
5202 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5204 struct lttng_ht_iter iter
;
5205 struct ust_app
*app
;
5208 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5209 ust_app_global_update(usess
, app
);
5215 * Add context to a specific channel for global UST domain.
5217 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5218 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5221 struct lttng_ht_node_str
*ua_chan_node
;
5222 struct lttng_ht_iter iter
, uiter
;
5223 struct ust_app_channel
*ua_chan
= NULL
;
5224 struct ust_app_session
*ua_sess
;
5225 struct ust_app
*app
;
5227 assert(usess
->active
);
5230 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5231 if (!app
->compatible
) {
5233 * TODO: In time, we should notice the caller of this error by
5234 * telling him that this is a version error.
5238 ua_sess
= lookup_session_by_app(usess
, app
);
5239 if (ua_sess
== NULL
) {
5243 pthread_mutex_lock(&ua_sess
->lock
);
5245 if (ua_sess
->deleted
) {
5246 pthread_mutex_unlock(&ua_sess
->lock
);
5250 /* Lookup channel in the ust app session */
5251 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5252 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5253 if (ua_chan_node
== NULL
) {
5256 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5258 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5263 pthread_mutex_unlock(&ua_sess
->lock
);
5271 * Receive registration and populate the given msg structure.
5273 * On success return 0 else a negative value returned by the ustctl call.
5275 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5278 uint32_t pid
, ppid
, uid
, gid
;
5282 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5283 &pid
, &ppid
, &uid
, &gid
,
5284 &msg
->bits_per_long
,
5285 &msg
->uint8_t_alignment
,
5286 &msg
->uint16_t_alignment
,
5287 &msg
->uint32_t_alignment
,
5288 &msg
->uint64_t_alignment
,
5289 &msg
->long_alignment
,
5296 case LTTNG_UST_ERR_EXITING
:
5297 DBG3("UST app recv reg message failed. Application died");
5299 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5300 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5301 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5302 LTTNG_UST_ABI_MINOR_VERSION
);
5305 ERR("UST app recv reg message failed with ret %d", ret
);
5310 msg
->pid
= (pid_t
) pid
;
5311 msg
->ppid
= (pid_t
) ppid
;
5312 msg
->uid
= (uid_t
) uid
;
5313 msg
->gid
= (gid_t
) gid
;
5320 * Return a ust app session object using the application object and the
5321 * session object descriptor has a key. If not found, NULL is returned.
5322 * A RCU read side lock MUST be acquired when calling this function.
5324 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5327 struct lttng_ht_node_ulong
*node
;
5328 struct lttng_ht_iter iter
;
5329 struct ust_app_session
*ua_sess
= NULL
;
5333 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5334 node
= lttng_ht_iter_get_node_ulong(&iter
);
5336 DBG2("UST app session find by objd %d not found", objd
);
5340 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5347 * Return a ust app channel object using the application object and the channel
5348 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5349 * lock MUST be acquired before calling this function.
5351 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5354 struct lttng_ht_node_ulong
*node
;
5355 struct lttng_ht_iter iter
;
5356 struct ust_app_channel
*ua_chan
= NULL
;
5360 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5361 node
= lttng_ht_iter_get_node_ulong(&iter
);
5363 DBG2("UST app channel find by objd %d not found", objd
);
5367 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5374 * Reply to a register channel notification from an application on the notify
5375 * socket. The channel metadata is also created.
5377 * The session UST registry lock is acquired in this function.
5379 * On success 0 is returned else a negative value.
5381 static int reply_ust_register_channel(int sock
, int cobjd
,
5382 size_t nr_fields
, struct ustctl_field
*fields
)
5384 int ret
, ret_code
= 0;
5386 uint64_t chan_reg_key
;
5387 enum ustctl_channel_header type
;
5388 struct ust_app
*app
;
5389 struct ust_app_channel
*ua_chan
;
5390 struct ust_app_session
*ua_sess
;
5391 struct ust_registry_session
*registry
;
5392 struct ust_registry_channel
*chan_reg
;
5396 /* Lookup application. If not found, there is a code flow error. */
5397 app
= find_app_by_notify_sock(sock
);
5399 DBG("Application socket %d is being torn down. Abort event notify",
5402 goto error_rcu_unlock
;
5405 /* Lookup channel by UST object descriptor. */
5406 ua_chan
= find_channel_by_objd(app
, cobjd
);
5408 DBG("Application channel is being torn down. Abort event notify");
5410 goto error_rcu_unlock
;
5413 assert(ua_chan
->session
);
5414 ua_sess
= ua_chan
->session
;
5416 /* Get right session registry depending on the session buffer type. */
5417 registry
= get_session_registry(ua_sess
);
5419 DBG("Application session is being torn down. Abort event notify");
5421 goto error_rcu_unlock
;
5424 /* Depending on the buffer type, a different channel key is used. */
5425 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5426 chan_reg_key
= ua_chan
->tracing_channel_id
;
5428 chan_reg_key
= ua_chan
->key
;
5431 pthread_mutex_lock(®istry
->lock
);
5433 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5436 if (!chan_reg
->register_done
) {
5438 * TODO: eventually use the registry event count for
5439 * this channel to better guess header type for per-pid
5442 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5443 chan_reg
->nr_ctx_fields
= nr_fields
;
5444 chan_reg
->ctx_fields
= fields
;
5446 chan_reg
->header_type
= type
;
5448 /* Get current already assigned values. */
5449 type
= chan_reg
->header_type
;
5451 /* Channel id is set during the object creation. */
5452 chan_id
= chan_reg
->chan_id
;
5454 /* Append to metadata */
5455 if (!chan_reg
->metadata_dumped
) {
5456 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5458 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5464 DBG3("UST app replying to register channel key %" PRIu64
5465 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5468 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5470 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5471 ERR("UST app reply channel failed with ret %d", ret
);
5473 DBG3("UST app reply channel failed. Application died");
5478 /* This channel registry registration is completed. */
5479 chan_reg
->register_done
= 1;
5482 pthread_mutex_unlock(®istry
->lock
);
5490 * Add event to the UST channel registry. When the event is added to the
5491 * registry, the metadata is also created. Once done, this replies to the
5492 * application with the appropriate error code.
5494 * The session UST registry lock is acquired in the function.
5496 * On success 0 is returned else a negative value.
5498 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5499 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5500 int loglevel_value
, char *model_emf_uri
)
5503 uint32_t event_id
= 0;
5504 uint64_t chan_reg_key
;
5505 struct ust_app
*app
;
5506 struct ust_app_channel
*ua_chan
;
5507 struct ust_app_session
*ua_sess
;
5508 struct ust_registry_session
*registry
;
5512 /* Lookup application. If not found, there is a code flow error. */
5513 app
= find_app_by_notify_sock(sock
);
5515 DBG("Application socket %d is being torn down. Abort event notify",
5518 goto error_rcu_unlock
;
5521 /* Lookup channel by UST object descriptor. */
5522 ua_chan
= find_channel_by_objd(app
, cobjd
);
5524 DBG("Application channel is being torn down. Abort event notify");
5526 goto error_rcu_unlock
;
5529 assert(ua_chan
->session
);
5530 ua_sess
= ua_chan
->session
;
5532 registry
= get_session_registry(ua_sess
);
5534 DBG("Application session is being torn down. Abort event notify");
5536 goto error_rcu_unlock
;
5539 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5540 chan_reg_key
= ua_chan
->tracing_channel_id
;
5542 chan_reg_key
= ua_chan
->key
;
5545 pthread_mutex_lock(®istry
->lock
);
5548 * From this point on, this call acquires the ownership of the sig, fields
5549 * and model_emf_uri meaning any free are done inside it if needed. These
5550 * three variables MUST NOT be read/write after this.
5552 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5553 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5554 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5558 model_emf_uri
= NULL
;
5561 * The return value is returned to ustctl so in case of an error, the
5562 * application can be notified. In case of an error, it's important not to
5563 * return a negative error or else the application will get closed.
5565 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5567 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5568 ERR("UST app reply event failed with ret %d", ret
);
5570 DBG3("UST app reply event failed. Application died");
5573 * No need to wipe the create event since the application socket will
5574 * get close on error hence cleaning up everything by itself.
5579 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5583 pthread_mutex_unlock(®istry
->lock
);
5588 free(model_emf_uri
);
5593 * Add enum to the UST session registry. Once done, this replies to the
5594 * application with the appropriate error code.
5596 * The session UST registry lock is acquired within this function.
5598 * On success 0 is returned else a negative value.
5600 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
5601 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
5603 int ret
= 0, ret_code
;
5604 struct ust_app
*app
;
5605 struct ust_app_session
*ua_sess
;
5606 struct ust_registry_session
*registry
;
5607 uint64_t enum_id
= -1ULL;
5611 /* Lookup application. If not found, there is a code flow error. */
5612 app
= find_app_by_notify_sock(sock
);
5614 /* Return an error since this is not an error */
5615 DBG("Application socket %d is being torn down. Aborting enum registration",
5618 goto error_rcu_unlock
;
5621 /* Lookup session by UST object descriptor. */
5622 ua_sess
= find_session_by_objd(app
, sobjd
);
5624 /* Return an error since this is not an error */
5625 DBG("Application session is being torn down (session not found). Aborting enum registration.");
5627 goto error_rcu_unlock
;
5630 registry
= get_session_registry(ua_sess
);
5632 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
5634 goto error_rcu_unlock
;
5637 pthread_mutex_lock(®istry
->lock
);
5640 * From this point on, the callee acquires the ownership of
5641 * entries. The variable entries MUST NOT be read/written after
5644 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
5645 entries
, nr_entries
, &enum_id
);
5649 * The return value is returned to ustctl so in case of an error, the
5650 * application can be notified. In case of an error, it's important not to
5651 * return a negative error or else the application will get closed.
5653 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
5655 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5656 ERR("UST app reply enum failed with ret %d", ret
);
5658 DBG3("UST app reply enum failed. Application died");
5661 * No need to wipe the create enum since the application socket will
5662 * get close on error hence cleaning up everything by itself.
5667 DBG3("UST registry enum %s added successfully or already found", name
);
5670 pthread_mutex_unlock(®istry
->lock
);
5677 * Handle application notification through the given notify socket.
5679 * Return 0 on success or else a negative value.
5681 int ust_app_recv_notify(int sock
)
5684 enum ustctl_notify_cmd cmd
;
5686 DBG3("UST app receiving notify from sock %d", sock
);
5688 ret
= ustctl_recv_notify(sock
, &cmd
);
5690 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5691 ERR("UST app recv notify failed with ret %d", ret
);
5693 DBG3("UST app recv notify failed. Application died");
5699 case USTCTL_NOTIFY_CMD_EVENT
:
5701 int sobjd
, cobjd
, loglevel_value
;
5702 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5704 struct ustctl_field
*fields
;
5706 DBG2("UST app ustctl register event received");
5708 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5709 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5712 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5713 ERR("UST app recv event failed with ret %d", ret
);
5715 DBG3("UST app recv event failed. Application died");
5721 * Add event to the UST registry coming from the notify socket. This
5722 * call will free if needed the sig, fields and model_emf_uri. This
5723 * code path loses the ownsership of these variables and transfer them
5724 * to the this function.
5726 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5727 fields
, loglevel_value
, model_emf_uri
);
5734 case USTCTL_NOTIFY_CMD_CHANNEL
:
5738 struct ustctl_field
*fields
;
5740 DBG2("UST app ustctl register channel received");
5742 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5745 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5746 ERR("UST app recv channel failed with ret %d", ret
);
5748 DBG3("UST app recv channel failed. Application died");
5754 * The fields ownership are transfered to this function call meaning
5755 * that if needed it will be freed. After this, it's invalid to access
5756 * fields or clean it up.
5758 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
5766 case USTCTL_NOTIFY_CMD_ENUM
:
5769 char name
[LTTNG_UST_SYM_NAME_LEN
];
5771 struct ustctl_enum_entry
*entries
;
5773 DBG2("UST app ustctl register enum received");
5775 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
5776 &entries
, &nr_entries
);
5778 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5779 ERR("UST app recv enum failed with ret %d", ret
);
5781 DBG3("UST app recv enum failed. Application died");
5786 /* Callee assumes ownership of entries */
5787 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
5788 entries
, nr_entries
);
5796 /* Should NEVER happen. */
5805 * Once the notify socket hangs up, this is called. First, it tries to find the
5806 * corresponding application. On failure, the call_rcu to close the socket is
5807 * executed. If an application is found, it tries to delete it from the notify
5808 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5810 * Note that an object needs to be allocated here so on ENOMEM failure, the
5811 * call RCU is not done but the rest of the cleanup is.
5813 void ust_app_notify_sock_unregister(int sock
)
5816 struct lttng_ht_iter iter
;
5817 struct ust_app
*app
;
5818 struct ust_app_notify_sock_obj
*obj
;
5824 obj
= zmalloc(sizeof(*obj
));
5827 * An ENOMEM is kind of uncool. If this strikes we continue the
5828 * procedure but the call_rcu will not be called. In this case, we
5829 * accept the fd leak rather than possibly creating an unsynchronized
5830 * state between threads.
5832 * TODO: The notify object should be created once the notify socket is
5833 * registered and stored independantely from the ust app object. The
5834 * tricky part is to synchronize the teardown of the application and
5835 * this notify object. Let's keep that in mind so we can avoid this
5836 * kind of shenanigans with ENOMEM in the teardown path.
5843 DBG("UST app notify socket unregister %d", sock
);
5846 * Lookup application by notify socket. If this fails, this means that the
5847 * hash table delete has already been done by the application
5848 * unregistration process so we can safely close the notify socket in a
5851 app
= find_app_by_notify_sock(sock
);
5856 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5859 * Whatever happens here either we fail or succeed, in both cases we have
5860 * to close the socket after a grace period to continue to the call RCU
5861 * here. If the deletion is successful, the application is not visible
5862 * anymore by other threads and is it fails it means that it was already
5863 * deleted from the hash table so either way we just have to close the
5866 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5872 * Close socket after a grace period to avoid for the socket to be reused
5873 * before the application object is freed creating potential race between
5874 * threads trying to add unique in the global hash table.
5877 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5882 * Destroy a ust app data structure and free its memory.
5884 void ust_app_destroy(struct ust_app
*app
)
5890 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5894 * Take a snapshot for a given UST session. The snapshot is sent to the given
5897 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5899 enum lttng_error_code
ust_app_snapshot_record(
5900 const struct ltt_ust_session
*usess
,
5901 const struct consumer_output
*output
, int wait
,
5902 uint64_t nb_packets_per_stream
)
5905 enum lttng_error_code status
= LTTNG_OK
;
5906 struct lttng_ht_iter iter
;
5907 struct ust_app
*app
;
5908 char *trace_path
= NULL
;
5915 switch (usess
->buffer_type
) {
5916 case LTTNG_BUFFER_PER_UID
:
5918 struct buffer_reg_uid
*reg
;
5920 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5921 struct buffer_reg_channel
*reg_chan
;
5922 struct consumer_socket
*socket
;
5923 char pathname
[PATH_MAX
];
5924 size_t consumer_path_offset
= 0;
5926 if (!reg
->registry
->reg
.ust
->metadata_key
) {
5927 /* Skip since no metadata is present */
5931 /* Get consumer socket to use to push the metadata.*/
5932 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5935 status
= LTTNG_ERR_INVALID
;
5939 memset(pathname
, 0, sizeof(pathname
));
5940 ret
= snprintf(pathname
, sizeof(pathname
),
5941 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5942 reg
->uid
, reg
->bits_per_long
);
5944 PERROR("snprintf snapshot path");
5945 status
= LTTNG_ERR_INVALID
;
5948 /* Free path allowed on previous iteration. */
5950 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
5951 &consumer_path_offset
);
5953 status
= LTTNG_ERR_INVALID
;
5956 /* Add the UST default trace dir to path. */
5957 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5958 reg_chan
, node
.node
) {
5959 status
= consumer_snapshot_channel(socket
,
5960 reg_chan
->consumer_key
,
5961 output
, 0, usess
->uid
,
5962 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
5963 nb_packets_per_stream
);
5964 if (status
!= LTTNG_OK
) {
5968 status
= consumer_snapshot_channel(socket
,
5969 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5970 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
5972 if (status
!= LTTNG_OK
) {
5978 case LTTNG_BUFFER_PER_PID
:
5980 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5981 struct consumer_socket
*socket
;
5982 struct lttng_ht_iter chan_iter
;
5983 struct ust_app_channel
*ua_chan
;
5984 struct ust_app_session
*ua_sess
;
5985 struct ust_registry_session
*registry
;
5986 char pathname
[PATH_MAX
];
5987 size_t consumer_path_offset
= 0;
5989 ua_sess
= lookup_session_by_app(usess
, app
);
5991 /* Session not associated with this app. */
5995 /* Get the right consumer socket for the application. */
5996 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5999 status
= LTTNG_ERR_INVALID
;
6003 /* Add the UST default trace dir to path. */
6004 memset(pathname
, 0, sizeof(pathname
));
6005 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
6008 status
= LTTNG_ERR_INVALID
;
6009 PERROR("snprintf snapshot path");
6012 /* Free path allowed on previous iteration. */
6014 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6015 &consumer_path_offset
);
6017 status
= LTTNG_ERR_INVALID
;
6020 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6021 ua_chan
, node
.node
) {
6022 status
= consumer_snapshot_channel(socket
,
6023 ua_chan
->key
, output
, 0,
6024 ua_sess
->effective_credentials
6026 ua_sess
->effective_credentials
6028 &trace_path
[consumer_path_offset
], wait
,
6029 nb_packets_per_stream
);
6033 case LTTNG_ERR_CHAN_NOT_FOUND
:
6040 registry
= get_session_registry(ua_sess
);
6042 DBG("Application session is being torn down. Skip application.");
6045 status
= consumer_snapshot_channel(socket
,
6046 registry
->metadata_key
, output
, 1,
6047 ua_sess
->effective_credentials
.uid
,
6048 ua_sess
->effective_credentials
.gid
,
6049 &trace_path
[consumer_path_offset
], wait
, 0);
6053 case LTTNG_ERR_CHAN_NOT_FOUND
:
6073 * Return the size taken by one more packet per stream.
6075 uint64_t ust_app_get_size_one_more_packet_per_stream(
6076 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6078 uint64_t tot_size
= 0;
6079 struct ust_app
*app
;
6080 struct lttng_ht_iter iter
;
6084 switch (usess
->buffer_type
) {
6085 case LTTNG_BUFFER_PER_UID
:
6087 struct buffer_reg_uid
*reg
;
6089 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6090 struct buffer_reg_channel
*reg_chan
;
6093 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6094 reg_chan
, node
.node
) {
6095 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6097 * Don't take channel into account if we
6098 * already grab all its packets.
6102 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6108 case LTTNG_BUFFER_PER_PID
:
6111 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6112 struct ust_app_channel
*ua_chan
;
6113 struct ust_app_session
*ua_sess
;
6114 struct lttng_ht_iter chan_iter
;
6116 ua_sess
= lookup_session_by_app(usess
, app
);
6118 /* Session not associated with this app. */
6122 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6123 ua_chan
, node
.node
) {
6124 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6126 * Don't take channel into account if we
6127 * already grab all its packets.
6131 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6145 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6146 struct cds_list_head
*buffer_reg_uid_list
,
6147 struct consumer_output
*consumer
, uint64_t uchan_id
,
6148 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6151 uint64_t consumer_chan_key
;
6156 ret
= buffer_reg_uid_consumer_channel_key(
6157 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6165 ret
= consumer_get_lost_packets(ust_session_id
,
6166 consumer_chan_key
, consumer
, lost
);
6168 ret
= consumer_get_discarded_events(ust_session_id
,
6169 consumer_chan_key
, consumer
, discarded
);
6176 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6177 struct ltt_ust_channel
*uchan
,
6178 struct consumer_output
*consumer
, int overwrite
,
6179 uint64_t *discarded
, uint64_t *lost
)
6182 struct lttng_ht_iter iter
;
6183 struct lttng_ht_node_str
*ua_chan_node
;
6184 struct ust_app
*app
;
6185 struct ust_app_session
*ua_sess
;
6186 struct ust_app_channel
*ua_chan
;
6193 * Iterate over every registered applications. Sum counters for
6194 * all applications containing requested session and channel.
6196 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6197 struct lttng_ht_iter uiter
;
6199 ua_sess
= lookup_session_by_app(usess
, app
);
6200 if (ua_sess
== NULL
) {
6205 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6206 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6207 /* If the session is found for the app, the channel must be there */
6208 assert(ua_chan_node
);
6210 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6215 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6222 uint64_t _discarded
;
6224 ret
= consumer_get_discarded_events(usess
->id
,
6225 ua_chan
->key
, consumer
, &_discarded
);
6229 (*discarded
) += _discarded
;
6238 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6239 struct ust_app
*app
)
6242 struct ust_app_session
*ua_sess
;
6244 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6248 ua_sess
= lookup_session_by_app(usess
, app
);
6249 if (ua_sess
== NULL
) {
6250 /* The session is in teardown process. Ignore and continue. */
6254 pthread_mutex_lock(&ua_sess
->lock
);
6256 if (ua_sess
->deleted
) {
6260 pthread_mutex_lock(&app
->sock_lock
);
6261 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6262 pthread_mutex_unlock(&app
->sock_lock
);
6265 pthread_mutex_unlock(&ua_sess
->lock
);
6269 health_code_update();
6274 * Regenerate the statedump for each app in the session.
6276 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6279 struct lttng_ht_iter iter
;
6280 struct ust_app
*app
;
6282 DBG("Regenerating the metadata for all UST apps");
6286 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6287 if (!app
->compatible
) {
6291 ret
= ust_app_regenerate_statedump(usess
, app
);
6293 /* Continue to the next app even on error */
6304 * Rotate all the channels of a session.
6306 * Return LTTNG_OK on success or else an LTTng error code.
6308 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
6311 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6312 struct lttng_ht_iter iter
;
6313 struct ust_app
*app
;
6314 struct ltt_ust_session
*usess
= session
->ust_session
;
6320 switch (usess
->buffer_type
) {
6321 case LTTNG_BUFFER_PER_UID
:
6323 struct buffer_reg_uid
*reg
;
6325 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6326 struct buffer_reg_channel
*reg_chan
;
6327 struct consumer_socket
*socket
;
6329 /* Get consumer socket to use to push the metadata.*/
6330 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6333 cmd_ret
= LTTNG_ERR_INVALID
;
6337 /* Rotate the data channels. */
6338 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6339 reg_chan
, node
.node
) {
6340 ret
= consumer_rotate_channel(socket
,
6341 reg_chan
->consumer_key
,
6342 usess
->uid
, usess
->gid
,
6344 /* is_metadata_channel */ false);
6346 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6351 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6353 ret
= consumer_rotate_channel(socket
,
6354 reg
->registry
->reg
.ust
->metadata_key
,
6355 usess
->uid
, usess
->gid
,
6357 /* is_metadata_channel */ true);
6359 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6365 case LTTNG_BUFFER_PER_PID
:
6367 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6368 struct consumer_socket
*socket
;
6369 struct lttng_ht_iter chan_iter
;
6370 struct ust_app_channel
*ua_chan
;
6371 struct ust_app_session
*ua_sess
;
6372 struct ust_registry_session
*registry
;
6374 ua_sess
= lookup_session_by_app(usess
, app
);
6376 /* Session not associated with this app. */
6380 /* Get the right consumer socket for the application. */
6381 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6384 cmd_ret
= LTTNG_ERR_INVALID
;
6388 registry
= get_session_registry(ua_sess
);
6390 DBG("Application session is being torn down. Skip application.");
6394 /* Rotate the data channels. */
6395 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6396 ua_chan
, node
.node
) {
6397 ret
= consumer_rotate_channel(socket
,
6399 ua_sess
->effective_credentials
6401 ua_sess
->effective_credentials
6404 /* is_metadata_channel */ false);
6406 /* Per-PID buffer and application going away. */
6407 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6409 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6414 /* Rotate the metadata channel. */
6415 (void) push_metadata(registry
, usess
->consumer
);
6416 ret
= consumer_rotate_channel(socket
,
6417 registry
->metadata_key
,
6418 ua_sess
->effective_credentials
.uid
,
6419 ua_sess
->effective_credentials
.gid
,
6421 /* is_metadata_channel */ true);
6423 /* Per-PID buffer and application going away. */
6424 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6426 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6444 enum lttng_error_code
ust_app_create_channel_subdirectories(
6445 const struct ltt_ust_session
*usess
)
6447 enum lttng_error_code ret
= LTTNG_OK
;
6448 struct lttng_ht_iter iter
;
6449 enum lttng_trace_chunk_status chunk_status
;
6450 char *pathname_index
;
6453 assert(usess
->current_trace_chunk
);
6456 switch (usess
->buffer_type
) {
6457 case LTTNG_BUFFER_PER_UID
:
6459 struct buffer_reg_uid
*reg
;
6461 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6462 fmt_ret
= asprintf(&pathname_index
,
6463 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
6464 reg
->uid
, reg
->bits_per_long
);
6466 ERR("Failed to format channel index directory");
6467 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6472 * Create the index subdirectory which will take care
6473 * of implicitly creating the channel's path.
6475 chunk_status
= lttng_trace_chunk_create_subdirectory(
6476 usess
->current_trace_chunk
,
6478 free(pathname_index
);
6479 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6480 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6486 case LTTNG_BUFFER_PER_PID
:
6488 struct ust_app
*app
;
6491 * Create the toplevel ust/ directory in case no apps are running.
6493 chunk_status
= lttng_trace_chunk_create_subdirectory(
6494 usess
->current_trace_chunk
,
6495 DEFAULT_UST_TRACE_DIR
);
6496 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6497 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6501 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
6503 struct ust_app_session
*ua_sess
;
6504 struct ust_registry_session
*registry
;
6506 ua_sess
= lookup_session_by_app(usess
, app
);
6508 /* Session not associated with this app. */
6512 registry
= get_session_registry(ua_sess
);
6514 DBG("Application session is being torn down. Skip application.");
6518 fmt_ret
= asprintf(&pathname_index
,
6519 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
6522 ERR("Failed to format channel index directory");
6523 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6527 * Create the index subdirectory which will take care
6528 * of implicitly creating the channel's path.
6530 chunk_status
= lttng_trace_chunk_create_subdirectory(
6531 usess
->current_trace_chunk
,
6533 free(pathname_index
);
6534 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6535 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6552 * Clear all the channels of a session.
6554 * Return LTTNG_OK on success or else an LTTng error code.
6556 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
6559 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6560 struct lttng_ht_iter iter
;
6561 struct ust_app
*app
;
6562 struct ltt_ust_session
*usess
= session
->ust_session
;
6568 if (usess
->active
) {
6569 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
6570 cmd_ret
= LTTNG_ERR_FATAL
;
6574 switch (usess
->buffer_type
) {
6575 case LTTNG_BUFFER_PER_UID
:
6577 struct buffer_reg_uid
*reg
;
6579 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6580 struct buffer_reg_channel
*reg_chan
;
6581 struct consumer_socket
*socket
;
6583 /* Get consumer socket to use to push the metadata.*/
6584 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6587 cmd_ret
= LTTNG_ERR_INVALID
;
6591 /* Clear the data channels. */
6592 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6593 reg_chan
, node
.node
) {
6594 ret
= consumer_clear_channel(socket
,
6595 reg_chan
->consumer_key
);
6601 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6604 * Clear the metadata channel.
6605 * Metadata channel is not cleared per se but we still need to
6606 * perform a rotation operation on it behind the scene.
6608 ret
= consumer_clear_channel(socket
,
6609 reg
->registry
->reg
.ust
->metadata_key
);
6616 case LTTNG_BUFFER_PER_PID
:
6618 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6619 struct consumer_socket
*socket
;
6620 struct lttng_ht_iter chan_iter
;
6621 struct ust_app_channel
*ua_chan
;
6622 struct ust_app_session
*ua_sess
;
6623 struct ust_registry_session
*registry
;
6625 ua_sess
= lookup_session_by_app(usess
, app
);
6627 /* Session not associated with this app. */
6631 /* Get the right consumer socket for the application. */
6632 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6635 cmd_ret
= LTTNG_ERR_INVALID
;
6639 registry
= get_session_registry(ua_sess
);
6641 DBG("Application session is being torn down. Skip application.");
6645 /* Clear the data channels. */
6646 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6647 ua_chan
, node
.node
) {
6648 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
6650 /* Per-PID buffer and application going away. */
6651 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6658 (void) push_metadata(registry
, usess
->consumer
);
6661 * Clear the metadata channel.
6662 * Metadata channel is not cleared per se but we still need to
6663 * perform rotation operation on it behind the scene.
6665 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
6667 /* Per-PID buffer and application going away. */
6668 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
6686 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
6687 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
6690 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;