2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
17 #include <sys/types.h>
19 #include <urcu/compiler.h>
22 #include <common/bytecode/bytecode.h>
23 #include <common/common.h>
24 #include <common/hashtable/utils.h>
25 #include <lttng/event-rule/event-rule.h>
26 #include <lttng/event-rule/event-rule-internal.h>
27 #include <lttng/event-rule/tracepoint.h>
28 #include <lttng/condition/condition.h>
29 #include <lttng/condition/event-rule-internal.h>
30 #include <lttng/condition/event-rule.h>
31 #include <lttng/trigger/trigger-internal.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
34 #include "buffer-registry.h"
36 #include "health-sessiond.h"
38 #include "ust-consumer.h"
39 #include "lttng-ust-ctl.h"
40 #include "lttng-ust-error.h"
43 #include "lttng-sessiond.h"
44 #include "notification-thread-commands.h"
48 struct lttng_ht
*ust_app_ht
;
49 struct lttng_ht
*ust_app_ht_by_sock
;
50 struct lttng_ht
*ust_app_ht_by_notify_sock
;
53 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
55 /* Next available channel key. Access under next_channel_key_lock. */
56 static uint64_t _next_channel_key
;
57 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
59 /* Next available session ID. Access under next_session_id_lock. */
60 static uint64_t _next_session_id
;
61 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
64 * Return the incremented value of next_channel_key.
66 static uint64_t get_next_channel_key(void)
70 pthread_mutex_lock(&next_channel_key_lock
);
71 ret
= ++_next_channel_key
;
72 pthread_mutex_unlock(&next_channel_key_lock
);
77 * Return the atomically incremented value of next_session_id.
79 static uint64_t get_next_session_id(void)
83 pthread_mutex_lock(&next_session_id_lock
);
84 ret
= ++_next_session_id
;
85 pthread_mutex_unlock(&next_session_id_lock
);
89 static void copy_channel_attr_to_ustctl(
90 struct ustctl_consumer_channel_attr
*attr
,
91 struct lttng_ust_channel_attr
*uattr
)
93 /* Copy event attributes since the layout is different. */
94 attr
->subbuf_size
= uattr
->subbuf_size
;
95 attr
->num_subbuf
= uattr
->num_subbuf
;
96 attr
->overwrite
= uattr
->overwrite
;
97 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
98 attr
->read_timer_interval
= uattr
->read_timer_interval
;
99 attr
->output
= uattr
->output
;
100 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
104 * Match function for the hash table lookup.
106 * It matches an ust app event based on three attributes which are the event
107 * name, the filter bytecode and the loglevel.
109 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
111 struct ust_app_event
*event
;
112 const struct ust_app_ht_key
*key
;
113 int ev_loglevel_value
;
118 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
120 ev_loglevel_value
= event
->attr
.loglevel
;
122 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
125 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
129 /* Event loglevel. */
130 if (ev_loglevel_value
!= key
->loglevel_type
) {
131 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
132 && key
->loglevel_type
== 0 &&
133 ev_loglevel_value
== -1) {
135 * Match is accepted. This is because on event creation, the
136 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
137 * -1 are accepted for this loglevel type since 0 is the one set by
138 * the API when receiving an enable event.
145 /* One of the filters is NULL, fail. */
146 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
150 if (key
->filter
&& event
->filter
) {
151 /* Both filters exists, check length followed by the bytecode. */
152 if (event
->filter
->len
!= key
->filter
->len
||
153 memcmp(event
->filter
->data
, key
->filter
->data
,
154 event
->filter
->len
) != 0) {
159 /* One of the exclusions is NULL, fail. */
160 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
164 if (key
->exclusion
&& event
->exclusion
) {
165 /* Both exclusions exists, check count followed by the names. */
166 if (event
->exclusion
->count
!= key
->exclusion
->count
||
167 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
168 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
182 * Unique add of an ust app event in the given ht. This uses the custom
183 * ht_match_ust_app_event match function and the event name as hash.
185 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
186 struct ust_app_event
*event
)
188 struct cds_lfht_node
*node_ptr
;
189 struct ust_app_ht_key key
;
193 assert(ua_chan
->events
);
196 ht
= ua_chan
->events
;
197 key
.name
= event
->attr
.name
;
198 key
.filter
= event
->filter
;
199 key
.loglevel_type
= event
->attr
.loglevel
;
200 key
.exclusion
= event
->exclusion
;
202 node_ptr
= cds_lfht_add_unique(ht
->ht
,
203 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
204 ht_match_ust_app_event
, &key
, &event
->node
.node
);
205 assert(node_ptr
== &event
->node
.node
);
209 * Close the notify socket from the given RCU head object. This MUST be called
210 * through a call_rcu().
212 static void close_notify_sock_rcu(struct rcu_head
*head
)
215 struct ust_app_notify_sock_obj
*obj
=
216 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
218 /* Must have a valid fd here. */
219 assert(obj
->fd
>= 0);
221 ret
= close(obj
->fd
);
223 ERR("close notify sock %d RCU", obj
->fd
);
225 lttng_fd_put(LTTNG_FD_APPS
, 1);
231 * Return the session registry according to the buffer type of the given
234 * A registry per UID object MUST exists before calling this function or else
235 * it assert() if not found. RCU read side lock must be acquired.
237 static struct ust_registry_session
*get_session_registry(
238 struct ust_app_session
*ua_sess
)
240 struct ust_registry_session
*registry
= NULL
;
244 switch (ua_sess
->buffer_type
) {
245 case LTTNG_BUFFER_PER_PID
:
247 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
251 registry
= reg_pid
->registry
->reg
.ust
;
254 case LTTNG_BUFFER_PER_UID
:
256 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
257 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
258 ua_sess
->real_credentials
.uid
);
262 registry
= reg_uid
->registry
->reg
.ust
;
274 * Delete ust context safely. RCU read lock must be held before calling
278 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
286 pthread_mutex_lock(&app
->sock_lock
);
287 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
288 pthread_mutex_unlock(&app
->sock_lock
);
289 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
290 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
291 sock
, ua_ctx
->obj
->handle
, ret
);
299 * Delete ust app event safely. RCU read lock must be held before calling
303 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
310 free(ua_event
->filter
);
311 if (ua_event
->exclusion
!= NULL
)
312 free(ua_event
->exclusion
);
313 if (ua_event
->obj
!= NULL
) {
314 pthread_mutex_lock(&app
->sock_lock
);
315 ret
= ustctl_release_object(sock
, ua_event
->obj
);
316 pthread_mutex_unlock(&app
->sock_lock
);
317 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
318 ERR("UST app sock %d release event obj failed with ret %d",
327 * Delete ust app token event_rule safely. RCU read lock must be held before calling
328 * this function. TODO: or does it????
331 void delete_ust_app_token_event_rule(int sock
, struct ust_app_token_event_rule
*ua_token
,
338 if (ua_token
->exclusion
!= NULL
)
339 free(ua_token
->exclusion
);
340 if (ua_token
->obj
!= NULL
) {
341 pthread_mutex_lock(&app
->sock_lock
);
342 ret
= ustctl_release_object(sock
, ua_token
->obj
);
343 pthread_mutex_unlock(&app
->sock_lock
);
344 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
345 ERR("UST app sock %d release event obj failed with ret %d",
350 lttng_trigger_put(ua_token
->trigger
);
355 * Release ust data object of the given stream.
357 * Return 0 on success or else a negative value.
359 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
367 pthread_mutex_lock(&app
->sock_lock
);
368 ret
= ustctl_release_object(sock
, stream
->obj
);
369 pthread_mutex_unlock(&app
->sock_lock
);
370 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
371 ERR("UST app sock %d release stream obj failed with ret %d",
374 lttng_fd_put(LTTNG_FD_APPS
, 2);
382 * Delete ust app stream safely. RCU read lock must be held before calling
386 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
391 (void) release_ust_app_stream(sock
, stream
, app
);
396 * We need to execute ht_destroy outside of RCU read-side critical
397 * section and outside of call_rcu thread, so we postpone its execution
398 * using ht_cleanup_push. It is simpler than to change the semantic of
399 * the many callers of delete_ust_app_session().
402 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
404 struct ust_app_channel
*ua_chan
=
405 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
407 ht_cleanup_push(ua_chan
->ctx
);
408 ht_cleanup_push(ua_chan
->events
);
413 * Extract the lost packet or discarded events counter when the channel is
414 * being deleted and store the value in the parent channel so we can
415 * access it from lttng list and at stop/destroy.
417 * The session list lock must be held by the caller.
420 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
422 uint64_t discarded
= 0, lost
= 0;
423 struct ltt_session
*session
;
424 struct ltt_ust_channel
*uchan
;
426 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
431 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
432 if (!session
|| !session
->ust_session
) {
434 * Not finding the session is not an error because there are
435 * multiple ways the channels can be torn down.
437 * 1) The session daemon can initiate the destruction of the
438 * ust app session after receiving a destroy command or
439 * during its shutdown/teardown.
440 * 2) The application, since we are in per-pid tracing, is
441 * unregistering and tearing down its ust app session.
443 * Both paths are protected by the session list lock which
444 * ensures that the accounting of lost packets and discarded
445 * events is done exactly once. The session is then unpublished
446 * from the session list, resulting in this condition.
451 if (ua_chan
->attr
.overwrite
) {
452 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
453 ua_chan
->key
, session
->ust_session
->consumer
,
456 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
457 ua_chan
->key
, session
->ust_session
->consumer
,
460 uchan
= trace_ust_find_channel_by_name(
461 session
->ust_session
->domain_global
.channels
,
464 ERR("Missing UST channel to store discarded counters");
468 uchan
->per_pid_closed_app_discarded
+= discarded
;
469 uchan
->per_pid_closed_app_lost
+= lost
;
474 session_put(session
);
479 * Delete ust app channel safely. RCU read lock must be held before calling
482 * The session list lock must be held by the caller.
485 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
489 struct lttng_ht_iter iter
;
490 struct ust_app_event
*ua_event
;
491 struct ust_app_ctx
*ua_ctx
;
492 struct ust_app_stream
*stream
, *stmp
;
493 struct ust_registry_session
*registry
;
497 DBG3("UST app deleting channel %s", ua_chan
->name
);
500 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
501 cds_list_del(&stream
->list
);
502 delete_ust_app_stream(sock
, stream
, app
);
506 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
507 cds_list_del(&ua_ctx
->list
);
508 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
510 delete_ust_app_ctx(sock
, ua_ctx
, app
);
514 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
516 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
518 delete_ust_app_event(sock
, ua_event
, app
);
521 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
522 /* Wipe and free registry from session registry. */
523 registry
= get_session_registry(ua_chan
->session
);
525 ust_registry_channel_del_free(registry
, ua_chan
->key
,
529 * A negative socket can be used by the caller when
530 * cleaning-up a ua_chan in an error path. Skip the
531 * accounting in this case.
534 save_per_pid_lost_discarded_counters(ua_chan
);
538 if (ua_chan
->obj
!= NULL
) {
539 /* Remove channel from application UST object descriptor. */
540 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
541 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
543 pthread_mutex_lock(&app
->sock_lock
);
544 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
545 pthread_mutex_unlock(&app
->sock_lock
);
546 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
547 ERR("UST app sock %d release channel obj failed with ret %d",
550 lttng_fd_put(LTTNG_FD_APPS
, 1);
553 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
556 int ust_app_register_done(struct ust_app
*app
)
560 pthread_mutex_lock(&app
->sock_lock
);
561 ret
= ustctl_register_done(app
->sock
);
562 pthread_mutex_unlock(&app
->sock_lock
);
566 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
571 pthread_mutex_lock(&app
->sock_lock
);
576 ret
= ustctl_release_object(sock
, data
);
578 pthread_mutex_unlock(&app
->sock_lock
);
584 * Push metadata to consumer socket.
586 * RCU read-side lock must be held to guarantee existance of socket.
587 * Must be called with the ust app session lock held.
588 * Must be called with the registry lock held.
590 * On success, return the len of metadata pushed or else a negative value.
591 * Returning a -EPIPE return value means we could not send the metadata,
592 * but it can be caused by recoverable errors (e.g. the application has
593 * terminated concurrently).
595 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
596 struct consumer_socket
*socket
, int send_zero_data
)
599 char *metadata_str
= NULL
;
600 size_t len
, offset
, new_metadata_len_sent
;
602 uint64_t metadata_key
, metadata_version
;
607 metadata_key
= registry
->metadata_key
;
610 * Means that no metadata was assigned to the session. This can
611 * happens if no start has been done previously.
617 offset
= registry
->metadata_len_sent
;
618 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
619 new_metadata_len_sent
= registry
->metadata_len
;
620 metadata_version
= registry
->metadata_version
;
622 DBG3("No metadata to push for metadata key %" PRIu64
,
623 registry
->metadata_key
);
625 if (send_zero_data
) {
626 DBG("No metadata to push");
632 /* Allocate only what we have to send. */
633 metadata_str
= zmalloc(len
);
635 PERROR("zmalloc ust app metadata string");
639 /* Copy what we haven't sent out. */
640 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
643 pthread_mutex_unlock(®istry
->lock
);
645 * We need to unlock the registry while we push metadata to
646 * break a circular dependency between the consumerd metadata
647 * lock and the sessiond registry lock. Indeed, pushing metadata
648 * to the consumerd awaits that it gets pushed all the way to
649 * relayd, but doing so requires grabbing the metadata lock. If
650 * a concurrent metadata request is being performed by
651 * consumerd, this can try to grab the registry lock on the
652 * sessiond while holding the metadata lock on the consumer
653 * daemon. Those push and pull schemes are performed on two
654 * different bidirectionnal communication sockets.
656 ret
= consumer_push_metadata(socket
, metadata_key
,
657 metadata_str
, len
, offset
, metadata_version
);
658 pthread_mutex_lock(®istry
->lock
);
661 * There is an acceptable race here between the registry
662 * metadata key assignment and the creation on the
663 * consumer. The session daemon can concurrently push
664 * metadata for this registry while being created on the
665 * consumer since the metadata key of the registry is
666 * assigned *before* it is setup to avoid the consumer
667 * to ask for metadata that could possibly be not found
668 * in the session daemon.
670 * The metadata will get pushed either by the session
671 * being stopped or the consumer requesting metadata if
672 * that race is triggered.
674 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
677 ERR("Error pushing metadata to consumer");
683 * Metadata may have been concurrently pushed, since
684 * we're not holding the registry lock while pushing to
685 * consumer. This is handled by the fact that we send
686 * the metadata content, size, and the offset at which
687 * that metadata belongs. This may arrive out of order
688 * on the consumer side, and the consumer is able to
689 * deal with overlapping fragments. The consumer
690 * supports overlapping fragments, which must be
691 * contiguous starting from offset 0. We keep the
692 * largest metadata_len_sent value of the concurrent
695 registry
->metadata_len_sent
=
696 max_t(size_t, registry
->metadata_len_sent
,
697 new_metadata_len_sent
);
706 * On error, flag the registry that the metadata is
707 * closed. We were unable to push anything and this
708 * means that either the consumer is not responding or
709 * the metadata cache has been destroyed on the
712 registry
->metadata_closed
= 1;
720 * For a given application and session, push metadata to consumer.
721 * Either sock or consumer is required : if sock is NULL, the default
722 * socket to send the metadata is retrieved from consumer, if sock
723 * is not NULL we use it to send the metadata.
724 * RCU read-side lock must be held while calling this function,
725 * therefore ensuring existance of registry. It also ensures existance
726 * of socket throughout this function.
728 * Return 0 on success else a negative error.
729 * Returning a -EPIPE return value means we could not send the metadata,
730 * but it can be caused by recoverable errors (e.g. the application has
731 * terminated concurrently).
733 static int push_metadata(struct ust_registry_session
*registry
,
734 struct consumer_output
*consumer
)
738 struct consumer_socket
*socket
;
743 pthread_mutex_lock(®istry
->lock
);
744 if (registry
->metadata_closed
) {
749 /* Get consumer socket to use to push the metadata.*/
750 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
757 ret
= ust_app_push_metadata(registry
, socket
, 0);
762 pthread_mutex_unlock(®istry
->lock
);
766 pthread_mutex_unlock(®istry
->lock
);
771 * Send to the consumer a close metadata command for the given session. Once
772 * done, the metadata channel is deleted and the session metadata pointer is
773 * nullified. The session lock MUST be held unless the application is
774 * in the destroy path.
776 * Do not hold the registry lock while communicating with the consumerd, because
777 * doing so causes inter-process deadlocks between consumerd and sessiond with
778 * the metadata request notification.
780 * Return 0 on success else a negative value.
782 static int close_metadata(struct ust_registry_session
*registry
,
783 struct consumer_output
*consumer
)
786 struct consumer_socket
*socket
;
787 uint64_t metadata_key
;
788 bool registry_was_already_closed
;
795 pthread_mutex_lock(®istry
->lock
);
796 metadata_key
= registry
->metadata_key
;
797 registry_was_already_closed
= registry
->metadata_closed
;
798 if (metadata_key
!= 0) {
800 * Metadata closed. Even on error this means that the consumer
801 * is not responding or not found so either way a second close
802 * should NOT be emit for this registry.
804 registry
->metadata_closed
= 1;
806 pthread_mutex_unlock(®istry
->lock
);
808 if (metadata_key
== 0 || registry_was_already_closed
) {
813 /* Get consumer socket to use to push the metadata.*/
814 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
821 ret
= consumer_close_metadata(socket
, metadata_key
);
832 * We need to execute ht_destroy outside of RCU read-side critical
833 * section and outside of call_rcu thread, so we postpone its execution
834 * using ht_cleanup_push. It is simpler than to change the semantic of
835 * the many callers of delete_ust_app_session().
838 void delete_ust_app_session_rcu(struct rcu_head
*head
)
840 struct ust_app_session
*ua_sess
=
841 caa_container_of(head
, struct ust_app_session
, rcu_head
);
843 ht_cleanup_push(ua_sess
->channels
);
848 * Delete ust app session safely. RCU read lock must be held before calling
851 * The session list lock must be held by the caller.
854 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
858 struct lttng_ht_iter iter
;
859 struct ust_app_channel
*ua_chan
;
860 struct ust_registry_session
*registry
;
864 pthread_mutex_lock(&ua_sess
->lock
);
866 assert(!ua_sess
->deleted
);
867 ua_sess
->deleted
= true;
869 registry
= get_session_registry(ua_sess
);
870 /* Registry can be null on error path during initialization. */
872 /* Push metadata for application before freeing the application. */
873 (void) push_metadata(registry
, ua_sess
->consumer
);
876 * Don't ask to close metadata for global per UID buffers. Close
877 * metadata only on destroy trace session in this case. Also, the
878 * previous push metadata could have flag the metadata registry to
879 * close so don't send a close command if closed.
881 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
882 /* And ask to close it for this session registry. */
883 (void) close_metadata(registry
, ua_sess
->consumer
);
887 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
889 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
891 delete_ust_app_channel(sock
, ua_chan
, app
);
894 /* In case of per PID, the registry is kept in the session. */
895 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
896 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
899 * Registry can be null on error path during
902 buffer_reg_pid_remove(reg_pid
);
903 buffer_reg_pid_destroy(reg_pid
);
907 if (ua_sess
->handle
!= -1) {
908 pthread_mutex_lock(&app
->sock_lock
);
909 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
910 pthread_mutex_unlock(&app
->sock_lock
);
911 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
912 ERR("UST app sock %d release session handle failed with ret %d",
915 /* Remove session from application UST object descriptor. */
916 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
917 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
921 pthread_mutex_unlock(&ua_sess
->lock
);
923 consumer_output_put(ua_sess
->consumer
);
925 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
929 * Delete a traceable application structure from the global list. Never call
930 * this function outside of a call_rcu call.
932 * RCU read side lock should _NOT_ be held when calling this function.
935 void delete_ust_app(struct ust_app
*app
)
938 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
939 struct lttng_ht_iter iter
;
940 struct ust_app_token_event_rule
*token
;
943 * The session list lock must be held during this function to guarantee
944 * the existence of ua_sess.
947 /* Delete ust app sessions info */
952 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
954 /* Free every object in the session and the session. */
956 delete_ust_app_session(sock
, ua_sess
, app
);
960 /* Wipe token associated with the app */
961 cds_lfht_for_each_entry(app
->tokens_ht
->ht
, &iter
.iter
, token
,
963 ret
= lttng_ht_del(app
->tokens_ht
, &iter
);
965 delete_ust_app_token_event_rule(app
->sock
, token
, app
);
968 ht_cleanup_push(app
->sessions
);
969 ht_cleanup_push(app
->ust_sessions_objd
);
970 ht_cleanup_push(app
->ust_objd
);
971 ht_cleanup_push(app
->tokens_ht
);
973 ustctl_release_object(sock
, app
->token_communication
.handle
);
974 free(app
->token_communication
.handle
);
976 lttng_pipe_destroy(app
->token_communication
.trigger_event_pipe
);
979 * Wait until we have deleted the application from the sock hash table
980 * before closing this socket, otherwise an application could re-use the
981 * socket ID and race with the teardown, using the same hash table entry.
983 * It's OK to leave the close in call_rcu. We want it to stay unique for
984 * all RCU readers that could run concurrently with unregister app,
985 * therefore we _need_ to only close that socket after a grace period. So
986 * it should stay in this RCU callback.
988 * This close() is a very important step of the synchronization model so
989 * every modification to this function must be carefully reviewed.
995 lttng_fd_put(LTTNG_FD_APPS
, 1);
997 DBG2("UST app pid %d deleted", app
->pid
);
999 session_unlock_list();
1003 * URCU intermediate call to delete an UST app.
1006 void delete_ust_app_rcu(struct rcu_head
*head
)
1008 struct lttng_ht_node_ulong
*node
=
1009 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1010 struct ust_app
*app
=
1011 caa_container_of(node
, struct ust_app
, pid_n
);
1013 DBG3("Call RCU deleting app PID %d", app
->pid
);
1014 delete_ust_app(app
);
1018 * Delete the session from the application ht and delete the data structure by
1019 * freeing every object inside and releasing them.
1021 * The session list lock must be held by the caller.
1023 static void destroy_app_session(struct ust_app
*app
,
1024 struct ust_app_session
*ua_sess
)
1027 struct lttng_ht_iter iter
;
1032 iter
.iter
.node
= &ua_sess
->node
.node
;
1033 ret
= lttng_ht_del(app
->sessions
, &iter
);
1035 /* Already scheduled for teardown. */
1039 /* Once deleted, free the data structure. */
1040 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1047 * Alloc new UST app session.
1050 struct ust_app_session
*alloc_ust_app_session(void)
1052 struct ust_app_session
*ua_sess
;
1054 /* Init most of the default value by allocating and zeroing */
1055 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1056 if (ua_sess
== NULL
) {
1061 ua_sess
->handle
= -1;
1062 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1063 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1064 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1073 * Alloc new UST app channel.
1076 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1077 struct ust_app_session
*ua_sess
,
1078 struct lttng_ust_channel_attr
*attr
)
1080 struct ust_app_channel
*ua_chan
;
1082 /* Init most of the default value by allocating and zeroing */
1083 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1084 if (ua_chan
== NULL
) {
1089 /* Setup channel name */
1090 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1091 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1093 ua_chan
->enabled
= 1;
1094 ua_chan
->handle
= -1;
1095 ua_chan
->session
= ua_sess
;
1096 ua_chan
->key
= get_next_channel_key();
1097 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1098 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1099 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1101 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1102 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1104 /* Copy attributes */
1106 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1107 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1108 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1109 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1110 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1111 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1112 ua_chan
->attr
.output
= attr
->output
;
1113 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1115 /* By default, the channel is a per cpu channel. */
1116 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1118 DBG3("UST app channel %s allocated", ua_chan
->name
);
1127 * Allocate and initialize a UST app stream.
1129 * Return newly allocated stream pointer or NULL on error.
1131 struct ust_app_stream
*ust_app_alloc_stream(void)
1133 struct ust_app_stream
*stream
= NULL
;
1135 stream
= zmalloc(sizeof(*stream
));
1136 if (stream
== NULL
) {
1137 PERROR("zmalloc ust app stream");
1141 /* Zero could be a valid value for a handle so flag it to -1. */
1142 stream
->handle
= -1;
1149 * Alloc new UST app event.
1152 struct ust_app_event
*alloc_ust_app_event(char *name
,
1153 struct lttng_ust_event
*attr
)
1155 struct ust_app_event
*ua_event
;
1157 /* Init most of the default value by allocating and zeroing */
1158 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1159 if (ua_event
== NULL
) {
1160 PERROR("Failed to allocate ust_app_event structure");
1164 ua_event
->enabled
= 1;
1165 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1166 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1167 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1169 /* Copy attributes */
1171 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1174 DBG3("UST app event %s allocated", ua_event
->name
);
1183 * Alloc new UST app token event rule.
1185 static struct ust_app_token_event_rule
*alloc_ust_app_token_event_rule(
1186 struct lttng_trigger
*trigger
)
1188 struct ust_app_token_event_rule
*ua_token
;
1189 struct lttng_condition
*condition
= NULL
;
1190 struct lttng_event_rule
*event_rule
= NULL
;
1192 ua_token
= zmalloc(sizeof(struct ust_app_token_event_rule
));
1193 if (ua_token
== NULL
) {
1194 PERROR("Failed to allocate ust_app_token_event_rule structure");
1198 /* Get reference of the trigger */
1199 /* TODO should this be like lttng_event_rule_get with a returned bool? */
1200 lttng_trigger_get(trigger
);
1202 ua_token
->enabled
= 1;
1203 ua_token
->token
= lttng_trigger_get_key(trigger
);
1204 lttng_ht_node_init_u64(&ua_token
->node
, ua_token
->token
);
1206 condition
= lttng_trigger_get_condition(trigger
);
1208 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
);
1210 assert(LTTNG_CONDITION_STATUS_OK
== lttng_condition_event_rule_get_rule_no_const(condition
, &event_rule
));
1213 ua_token
->trigger
= trigger
;
1214 ua_token
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1215 ua_token
->exclusion
= lttng_event_rule_generate_exclusions(event_rule
);
1217 /* TODO put capture here? or later*/
1219 DBG3("UST app token event rule %" PRIu64
" allocated", ua_token
->token
);
1228 * Alloc new UST app context.
1231 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1233 struct ust_app_ctx
*ua_ctx
;
1235 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1236 if (ua_ctx
== NULL
) {
1240 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1243 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1244 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1245 char *provider_name
= NULL
, *ctx_name
= NULL
;
1247 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1248 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1249 if (!provider_name
|| !ctx_name
) {
1250 free(provider_name
);
1255 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1256 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1260 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1268 * Create a liblttng-ust filter bytecode from given bytecode.
1270 * Return allocated filter or NULL on error.
1272 static struct lttng_ust_filter_bytecode
*
1273 create_ust_filter_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1275 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1277 /* Copy filter bytecode */
1278 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1280 PERROR("zmalloc alloc ust filter bytecode");
1284 assert(sizeof(struct lttng_bytecode
) ==
1285 sizeof(struct lttng_ust_filter_bytecode
));
1286 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1292 * Create a liblttng-ust capture bytecode from given bytecode.
1294 * Return allocated filter or NULL on error.
1296 static struct lttng_ust_capture_bytecode
*
1297 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1299 struct lttng_ust_capture_bytecode
*capture
= NULL
;
1301 /* Copy capture bytecode */
1302 capture
= zmalloc(sizeof(*capture
) + orig_f
->len
);
1304 PERROR("zmalloc alloc ust capture bytecode");
1308 assert(sizeof(struct lttng_bytecode
) ==
1309 sizeof(struct lttng_ust_capture_bytecode
));
1310 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1316 * Find an ust_app using the sock and return it. RCU read side lock must be
1317 * held before calling this helper function.
1319 struct ust_app
*ust_app_find_by_sock(int sock
)
1321 struct lttng_ht_node_ulong
*node
;
1322 struct lttng_ht_iter iter
;
1324 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1325 node
= lttng_ht_iter_get_node_ulong(&iter
);
1327 DBG2("UST app find by sock %d not found", sock
);
1331 return caa_container_of(node
, struct ust_app
, sock_n
);
1338 * Find an ust_app using the notify sock and return it. RCU read side lock must
1339 * be held before calling this helper function.
1341 static struct ust_app
*find_app_by_notify_sock(int sock
)
1343 struct lttng_ht_node_ulong
*node
;
1344 struct lttng_ht_iter iter
;
1346 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1348 node
= lttng_ht_iter_get_node_ulong(&iter
);
1350 DBG2("UST app find by notify sock %d not found", sock
);
1354 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1361 * Lookup for an ust app event based on event name, filter bytecode and the
1364 * Return an ust_app_event object or NULL on error.
1366 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1367 const char *name
, const struct lttng_bytecode
*filter
,
1369 const struct lttng_event_exclusion
*exclusion
)
1371 struct lttng_ht_iter iter
;
1372 struct lttng_ht_node_str
*node
;
1373 struct ust_app_event
*event
= NULL
;
1374 struct ust_app_ht_key key
;
1379 /* Setup key for event lookup. */
1381 key
.filter
= filter
;
1382 key
.loglevel_type
= loglevel_value
;
1383 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1384 key
.exclusion
= exclusion
;
1386 /* Lookup using the event name as hash and a custom match fct. */
1387 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1388 ht_match_ust_app_event
, &key
, &iter
.iter
);
1389 node
= lttng_ht_iter_get_node_str(&iter
);
1394 event
= caa_container_of(node
, struct ust_app_event
, node
);
1401 * Lookup for an ust app tokens based on a token id.
1403 * Return an ust_app_token_event_rule object or NULL on error.
1405 static struct ust_app_token_event_rule
*find_ust_app_token_event_rule(struct lttng_ht
*ht
,
1408 struct lttng_ht_iter iter
;
1409 struct lttng_ht_node_u64
*node
;
1410 struct ust_app_token_event_rule
*token_event_rule
= NULL
;
1414 lttng_ht_lookup(ht
, &token
, &iter
);
1415 node
= lttng_ht_iter_get_node_u64(&iter
);
1417 DBG2("UST app token %" PRIu64
" not found", token
);
1421 token_event_rule
= caa_container_of(node
, struct ust_app_token_event_rule
, node
);
1423 return token_event_rule
;
1427 * Create the channel context on the tracer.
1429 * Called with UST app session lock held.
1432 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1433 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1437 health_code_update();
1439 pthread_mutex_lock(&app
->sock_lock
);
1440 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1441 ua_chan
->obj
, &ua_ctx
->obj
);
1442 pthread_mutex_unlock(&app
->sock_lock
);
1444 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1445 ERR("UST app create channel context failed for app (pid: %d) "
1446 "with ret %d", app
->pid
, ret
);
1449 * This is normal behavior, an application can die during the
1450 * creation process. Don't report an error so the execution can
1451 * continue normally.
1454 DBG3("UST app add context failed. Application is dead.");
1459 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1461 DBG2("UST app context handle %d created successfully for channel %s",
1462 ua_ctx
->handle
, ua_chan
->name
);
1465 health_code_update();
1470 * Set the filter on the tracer.
1472 static int set_ust_filter(struct ust_app
*app
,
1473 const struct lttng_bytecode
*bytecode
,
1474 struct lttng_ust_object_data
*ust_object
)
1477 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1479 health_code_update();
1481 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1482 if (!ust_bytecode
) {
1483 ret
= -LTTNG_ERR_NOMEM
;
1486 pthread_mutex_lock(&app
->sock_lock
);
1487 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1489 pthread_mutex_unlock(&app
->sock_lock
);
1491 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1492 ERR("UST app set filter failed for object %p of app (pid: %d) "
1493 "with ret %d", ust_object
, app
->pid
, ret
);
1496 * This is normal behavior, an application can die during the
1497 * creation process. Don't report an error so the execution can
1498 * continue normally.
1501 DBG3("UST app set filter. Application is dead.");
1506 DBG2("UST filter set for object %p successfully", ust_object
);
1509 health_code_update();
1515 * Set a capture bytecode for the passed object.
1516 * The seqnum enforce the ordering at runtime and on reception.
1518 static int set_ust_capture(struct ust_app
*app
,
1519 const struct lttng_bytecode
*bytecode
,
1520 unsigned int seqnum
,
1521 struct lttng_ust_object_data
*ust_object
)
1524 struct lttng_ust_capture_bytecode
*ust_bytecode
= NULL
;
1526 health_code_update();
1528 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1529 if (!ust_bytecode
) {
1530 ret
= -LTTNG_ERR_NOMEM
;
1534 /* Set the seqnum */
1535 ust_bytecode
->seqnum
= seqnum
;
1537 pthread_mutex_lock(&app
->sock_lock
);
1538 ret
= ustctl_set_capture(app
->sock
, ust_bytecode
,
1540 pthread_mutex_unlock(&app
->sock_lock
);
1542 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1543 ERR("UST app set capture failed for object %p of app (pid: %d) "
1544 "with ret %d", ust_object
, app
->pid
, ret
);
1547 * This is normal behavior, an application can die during the
1548 * creation process. Don't report an error so the execution can
1549 * continue normally.
1552 DBG3("UST app set capture. Application is dead.");
1557 DBG2("UST capture set for object %p successfully", ust_object
);
1560 health_code_update();
1566 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1567 struct lttng_event_exclusion
*exclusion
)
1569 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1570 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1571 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1573 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1574 if (!ust_exclusion
) {
1579 assert(sizeof(struct lttng_event_exclusion
) ==
1580 sizeof(struct lttng_ust_event_exclusion
));
1581 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1583 return ust_exclusion
;
1587 * Set event exclusions on the tracer.
1589 static int set_ust_exclusions(struct ust_app
*app
,
1590 struct lttng_event_exclusion
*exclusions
,
1591 struct lttng_ust_object_data
*ust_object
)
1594 struct lttng_ust_event_exclusion
*ust_exclusions
= NULL
;
1596 assert(exclusions
&& exclusions
->count
> 0);
1598 health_code_update();
1600 ust_exclusions
= create_ust_exclusion_from_exclusion(
1602 if (!ust_exclusions
) {
1603 ret
= -LTTNG_ERR_NOMEM
;
1606 pthread_mutex_lock(&app
->sock_lock
);
1607 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1608 pthread_mutex_unlock(&app
->sock_lock
);
1610 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1611 ERR("UST app exclusions failed for object %p of app (pid: %d) "
1612 "with ret %d", ust_object
, app
->pid
, ret
);
1615 * This is normal behavior, an application can die during the
1616 * creation process. Don't report an error so the execution can
1617 * continue normally.
1620 DBG3("UST app set exclusions failed. Application is dead.");
1625 DBG2("UST exclusions set successfully for object %p", ust_object
);
1628 health_code_update();
1629 free(ust_exclusions
);
1634 * Disable the specified event on to UST tracer for the UST session.
1636 static int disable_ust_object(struct ust_app
*app
,
1637 struct lttng_ust_object_data
*object
)
1641 health_code_update();
1643 pthread_mutex_lock(&app
->sock_lock
);
1644 ret
= ustctl_disable(app
->sock
, object
);
1645 pthread_mutex_unlock(&app
->sock_lock
);
1647 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1648 ERR("UST app disable failed for object %p app (pid: %d) with ret %d",
1649 object
, app
->pid
, ret
);
1652 * This is normal behavior, an application can die during the
1653 * creation process. Don't report an error so the execution can
1654 * continue normally.
1657 DBG3("UST app disable event failed. Application is dead.");
1662 DBG2("UST app object %p disabled successfully for app (pid: %d)",
1666 health_code_update();
1671 * Disable the specified channel on to UST tracer for the UST session.
1673 static int disable_ust_channel(struct ust_app
*app
,
1674 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1678 health_code_update();
1680 pthread_mutex_lock(&app
->sock_lock
);
1681 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1682 pthread_mutex_unlock(&app
->sock_lock
);
1684 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1685 ERR("UST app channel %s disable failed for app (pid: %d) "
1686 "and session handle %d with ret %d",
1687 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1690 * This is normal behavior, an application can die during the
1691 * creation process. Don't report an error so the execution can
1692 * continue normally.
1695 DBG3("UST app disable channel failed. Application is dead.");
1700 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1701 ua_chan
->name
, app
->pid
);
1704 health_code_update();
1709 * Enable the specified channel on to UST tracer for the UST session.
1711 static int enable_ust_channel(struct ust_app
*app
,
1712 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1716 health_code_update();
1718 pthread_mutex_lock(&app
->sock_lock
);
1719 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1720 pthread_mutex_unlock(&app
->sock_lock
);
1722 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1723 ERR("UST app channel %s enable failed for app (pid: %d) "
1724 "and session handle %d with ret %d",
1725 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1728 * This is normal behavior, an application can die during the
1729 * creation process. Don't report an error so the execution can
1730 * continue normally.
1733 DBG3("UST app enable channel failed. Application is dead.");
1738 ua_chan
->enabled
= 1;
1740 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1741 ua_chan
->name
, app
->pid
);
1744 health_code_update();
1749 * Enable the specified event on to UST tracer for the UST session.
1751 static int enable_ust_object(struct ust_app
*app
, struct lttng_ust_object_data
*ust_object
)
1755 health_code_update();
1757 pthread_mutex_lock(&app
->sock_lock
);
1758 ret
= ustctl_enable(app
->sock
, ust_object
);
1759 pthread_mutex_unlock(&app
->sock_lock
);
1761 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1762 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
1763 ust_object
, app
->pid
, ret
);
1766 * This is normal behavior, an application can die during the
1767 * creation process. Don't report an error so the execution can
1768 * continue normally.
1771 DBG3("UST app enable failed. Application is dead.");
1776 DBG2("UST app object %p enabled successfully for app (pid: %d)",
1777 ust_object
, app
->pid
);
1780 health_code_update();
1785 * Send channel and stream buffer to application.
1787 * Return 0 on success. On error, a negative value is returned.
1789 static int send_channel_pid_to_ust(struct ust_app
*app
,
1790 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1793 struct ust_app_stream
*stream
, *stmp
;
1799 health_code_update();
1801 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1804 /* Send channel to the application. */
1805 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1806 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1807 ret
= -ENOTCONN
; /* Caused by app exiting. */
1809 } else if (ret
< 0) {
1813 health_code_update();
1815 /* Send all streams to application. */
1816 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1817 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1818 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1819 ret
= -ENOTCONN
; /* Caused by app exiting. */
1821 } else if (ret
< 0) {
1824 /* We don't need the stream anymore once sent to the tracer. */
1825 cds_list_del(&stream
->list
);
1826 delete_ust_app_stream(-1, stream
, app
);
1828 /* Flag the channel that it is sent to the application. */
1829 ua_chan
->is_sent
= 1;
1832 health_code_update();
1837 * Create the specified event onto the UST tracer for a UST session.
1839 * Should be called with session mutex held.
1842 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1843 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1847 health_code_update();
1849 /* Create UST event on tracer */
1850 pthread_mutex_lock(&app
->sock_lock
);
1851 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1853 pthread_mutex_unlock(&app
->sock_lock
);
1855 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1857 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1858 ua_event
->attr
.name
, app
->pid
, ret
);
1861 * This is normal behavior, an application can die during the
1862 * creation process. Don't report an error so the execution can
1863 * continue normally.
1866 DBG3("UST app create event failed. Application is dead.");
1871 ua_event
->handle
= ua_event
->obj
->handle
;
1873 DBG2("UST app event %s created successfully for pid:%d object: %p",
1874 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
1876 health_code_update();
1878 /* Set filter if one is present. */
1879 if (ua_event
->filter
) {
1880 ret
= set_ust_filter(app
, ua_event
->filter
, ua_event
->obj
);
1886 /* Set exclusions for the event */
1887 if (ua_event
->exclusion
) {
1888 ret
= set_ust_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
1894 /* If event not enabled, disable it on the tracer */
1895 if (ua_event
->enabled
) {
1897 * We now need to explicitly enable the event, since it
1898 * is now disabled at creation.
1900 ret
= enable_ust_object(app
, ua_event
->obj
);
1903 * If we hit an EPERM, something is wrong with our enable call. If
1904 * we get an EEXIST, there is a problem on the tracer side since we
1908 case -LTTNG_UST_ERR_PERM
:
1909 /* Code flow problem */
1911 case -LTTNG_UST_ERR_EXIST
:
1912 /* It's OK for our use case. */
1923 health_code_update();
1928 void init_ust_trigger_from_event_rule(const struct lttng_event_rule
*rule
, struct lttng_ust_trigger
*trigger
)
1930 enum lttng_event_rule_status status
;
1931 enum lttng_loglevel_type loglevel_type
;
1932 enum lttng_ust_loglevel_type ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1934 const char *pattern
;
1936 /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported */
1937 assert(lttng_event_rule_get_type(rule
) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
1939 memset(trigger
, 0, sizeof(*trigger
));
1941 if (lttng_event_rule_is_agent(rule
)) {
1943 * Special event for agents
1944 * The actual meat of the event is in the filter that will be
1945 * attached later on.
1946 * Set the default values for the agent event.
1948 pattern
= event_get_default_agent_ust_name(lttng_event_rule_get_domain_type(rule
));
1950 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1952 status
= lttng_event_rule_tracepoint_get_pattern(rule
, &pattern
);
1953 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1954 /* At this point this is a fatal error */
1958 status
= lttng_event_rule_tracepoint_get_loglevel_type(
1959 rule
, &loglevel_type
);
1960 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1961 /* At this point this is a fatal error */
1965 switch (loglevel_type
) {
1966 case LTTNG_EVENT_LOGLEVEL_ALL
:
1967 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1969 case LTTNG_EVENT_LOGLEVEL_RANGE
:
1970 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
1972 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
1973 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
1977 if (loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1978 status
= lttng_event_rule_tracepoint_get_loglevel(
1980 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
1984 trigger
->instrumentation
= LTTNG_UST_TRACEPOINT
;
1985 strncpy(trigger
->name
, pattern
, LTTNG_UST_SYM_NAME_LEN
- 1);
1986 trigger
->loglevel_type
= ust_loglevel_type
;
1987 trigger
->loglevel
= loglevel
;
1991 * Create the specified event rule token onto the UST tracer for a UST app.
1994 int create_ust_token_event_rule(struct ust_app
*app
, struct ust_app_token_event_rule
*ua_token
)
1997 struct lttng_ust_trigger trigger
;
1998 struct lttng_condition
*condition
= NULL
;
1999 struct lttng_event_rule
*event_rule
= NULL
;
2000 unsigned int capture_bytecode_count
= 0;
2002 health_code_update();
2003 assert(app
->token_communication
.handle
);
2005 condition
= lttng_trigger_get_condition(ua_token
->trigger
);
2007 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
);
2009 lttng_condition_event_rule_get_rule_no_const(condition
, &event_rule
);
2011 assert(lttng_event_rule_get_type(event_rule
) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
2012 /* Should we also test for UST at this point, or do we trust all the
2015 init_ust_trigger_from_event_rule(event_rule
, &trigger
);
2016 trigger
.id
= ua_token
->token
;
2018 /* Create UST trigger on tracer */
2019 pthread_mutex_lock(&app
->sock_lock
);
2020 ret
= ustctl_create_trigger(app
->sock
, &trigger
, app
->token_communication
.handle
, &ua_token
->obj
);
2021 pthread_mutex_unlock(&app
->sock_lock
);
2023 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2025 ERR("Error ustctl create trigger %s for app pid: %d with ret %d",
2026 trigger
.name
, app
->pid
, ret
);
2029 * This is normal behavior, an application can die during the
2030 * creation process. Don't report an error so the execution can
2031 * continue normally.
2034 DBG3("UST app create event failed. Application is dead.");
2039 ua_token
->handle
= ua_token
->obj
->handle
;
2041 DBG2("UST app event %s created successfully for pid:%d object: %p",
2042 trigger
.name
, app
->pid
, ua_token
->obj
);
2044 health_code_update();
2046 /* Set filter if one is present. */
2047 if (ua_token
->filter
) {
2048 ret
= set_ust_filter(app
, ua_token
->filter
, ua_token
->obj
);
2054 /* Set exclusions for the event */
2055 if (ua_token
->exclusion
) {
2056 ret
= set_ust_exclusions(app
, ua_token
->exclusion
, ua_token
->obj
);
2062 /* Set the capture bytecode
2063 * TODO: do we want to emulate what is done with exclusion and provide
2064 * and object with a count of capture bytecode? instead of multiple
2067 capture_bytecode_count
= lttng_trigger_get_capture_bytecode_count(ua_token
->trigger
);
2068 for (unsigned int i
= 0; i
< capture_bytecode_count
; i
++) {
2069 const struct lttng_bytecode
*capture_bytecode
= lttng_trigger_get_capture_bytecode_at_index(ua_token
->trigger
, i
);
2070 ret
= set_ust_capture(app
, capture_bytecode
, i
, ua_token
->obj
);
2077 * We now need to explicitly enable the event, since it
2078 * is disabled at creation.
2080 ret
= enable_ust_object(app
, ua_token
->obj
);
2083 * If we hit an EPERM, something is wrong with our enable call. If
2084 * we get an EEXIST, there is a problem on the tracer side since we
2088 case -LTTNG_UST_ERR_PERM
:
2089 /* Code flow problem */
2091 case -LTTNG_UST_ERR_EXIST
:
2092 /* It's OK for our use case. */
2100 ua_token
->enabled
= true;
2103 health_code_update();
2108 * Copy data between an UST app event and a LTT event.
2110 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2111 struct ltt_ust_event
*uevent
)
2113 size_t exclusion_alloc_size
;
2115 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2116 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2118 ua_event
->enabled
= uevent
->enabled
;
2120 /* Copy event attributes */
2121 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2123 /* Copy filter bytecode */
2124 if (uevent
->filter
) {
2125 ua_event
->filter
= bytecode_copy(uevent
->filter
);
2126 /* Filter might be NULL here in case of ENONEM. */
2129 /* Copy exclusion data */
2130 if (uevent
->exclusion
) {
2131 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2132 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2133 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
2134 if (ua_event
->exclusion
== NULL
) {
2137 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2138 exclusion_alloc_size
);
2144 * Copy data between an UST app channel and a LTT channel.
2146 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2147 struct ltt_ust_channel
*uchan
)
2149 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2151 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2152 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2154 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2155 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2157 /* Copy event attributes since the layout is different. */
2158 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2159 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2160 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2161 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2162 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2163 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2164 ua_chan
->attr
.output
= uchan
->attr
.output
;
2165 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2168 * Note that the attribute channel type is not set since the channel on the
2169 * tracing registry side does not have this information.
2172 ua_chan
->enabled
= uchan
->enabled
;
2173 ua_chan
->tracing_channel_id
= uchan
->id
;
2175 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2179 * Copy data between a UST app session and a regular LTT session.
2181 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2182 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2184 struct tm
*timeinfo
;
2187 char tmp_shm_path
[PATH_MAX
];
2189 timeinfo
= localtime(&app
->registration_time
);
2190 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2192 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2194 ua_sess
->tracing_id
= usess
->id
;
2195 ua_sess
->id
= get_next_session_id();
2196 ua_sess
->real_credentials
.uid
= app
->uid
;
2197 ua_sess
->real_credentials
.gid
= app
->gid
;
2198 ua_sess
->effective_credentials
.uid
= usess
->uid
;
2199 ua_sess
->effective_credentials
.gid
= usess
->gid
;
2200 ua_sess
->buffer_type
= usess
->buffer_type
;
2201 ua_sess
->bits_per_long
= app
->bits_per_long
;
2203 /* There is only one consumer object per session possible. */
2204 consumer_output_get(usess
->consumer
);
2205 ua_sess
->consumer
= usess
->consumer
;
2207 ua_sess
->output_traces
= usess
->output_traces
;
2208 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2209 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2210 &usess
->metadata_attr
);
2212 switch (ua_sess
->buffer_type
) {
2213 case LTTNG_BUFFER_PER_PID
:
2214 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2215 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2218 case LTTNG_BUFFER_PER_UID
:
2219 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2220 DEFAULT_UST_TRACE_UID_PATH
,
2221 ua_sess
->real_credentials
.uid
,
2222 app
->bits_per_long
);
2229 PERROR("asprintf UST shadow copy session");
2234 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2235 sizeof(ua_sess
->root_shm_path
));
2236 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2237 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2238 sizeof(ua_sess
->shm_path
));
2239 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2240 if (ua_sess
->shm_path
[0]) {
2241 switch (ua_sess
->buffer_type
) {
2242 case LTTNG_BUFFER_PER_PID
:
2243 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2244 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2245 app
->name
, app
->pid
, datetime
);
2247 case LTTNG_BUFFER_PER_UID
:
2248 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2249 "/" DEFAULT_UST_TRACE_UID_PATH
,
2250 app
->uid
, app
->bits_per_long
);
2257 PERROR("sprintf UST shadow copy session");
2261 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2262 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2263 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2268 consumer_output_put(ua_sess
->consumer
);
2272 * Lookup sesison wrapper.
2275 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2276 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2278 /* Get right UST app session from app */
2279 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2283 * Return ust app session from the app session hashtable using the UST session
2286 static struct ust_app_session
*lookup_session_by_app(
2287 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2289 struct lttng_ht_iter iter
;
2290 struct lttng_ht_node_u64
*node
;
2292 __lookup_session_by_app(usess
, app
, &iter
);
2293 node
= lttng_ht_iter_get_node_u64(&iter
);
2298 return caa_container_of(node
, struct ust_app_session
, node
);
2305 * Setup buffer registry per PID for the given session and application. If none
2306 * is found, a new one is created, added to the global registry and
2307 * initialized. If regp is valid, it's set with the newly created object.
2309 * Return 0 on success or else a negative value.
2311 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2312 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2315 struct buffer_reg_pid
*reg_pid
;
2322 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2325 * This is the create channel path meaning that if there is NO
2326 * registry available, we have to create one for this session.
2328 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2329 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2337 /* Initialize registry. */
2338 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2339 app
->bits_per_long
, app
->uint8_t_alignment
,
2340 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2341 app
->uint64_t_alignment
, app
->long_alignment
,
2342 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2343 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2344 ua_sess
->effective_credentials
.uid
,
2345 ua_sess
->effective_credentials
.gid
, ua_sess
->tracing_id
,
2349 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2350 * destroy the buffer registry, because it is always expected
2351 * that if the buffer registry can be found, its ust registry is
2354 buffer_reg_pid_destroy(reg_pid
);
2358 buffer_reg_pid_add(reg_pid
);
2360 DBG3("UST app buffer registry per PID created successfully");
2372 * Setup buffer registry per UID for the given session and application. If none
2373 * is found, a new one is created, added to the global registry and
2374 * initialized. If regp is valid, it's set with the newly created object.
2376 * Return 0 on success or else a negative value.
2378 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2379 struct ust_app_session
*ua_sess
,
2380 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2383 struct buffer_reg_uid
*reg_uid
;
2390 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2393 * This is the create channel path meaning that if there is NO
2394 * registry available, we have to create one for this session.
2396 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2397 LTTNG_DOMAIN_UST
, ®_uid
,
2398 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2406 /* Initialize registry. */
2407 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2408 app
->bits_per_long
, app
->uint8_t_alignment
,
2409 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2410 app
->uint64_t_alignment
, app
->long_alignment
,
2411 app
->byte_order
, app
->version
.major
,
2412 app
->version
.minor
, reg_uid
->root_shm_path
,
2413 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2414 ua_sess
->tracing_id
, app
->uid
);
2417 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2418 * destroy the buffer registry, because it is always expected
2419 * that if the buffer registry can be found, its ust registry is
2422 buffer_reg_uid_destroy(reg_uid
, NULL
);
2425 /* Add node to teardown list of the session. */
2426 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2428 buffer_reg_uid_add(reg_uid
);
2430 DBG3("UST app buffer registry per UID created successfully");
2441 * Create a session on the tracer side for the given app.
2443 * On success, ua_sess_ptr is populated with the session pointer or else left
2444 * untouched. If the session was created, is_created is set to 1. On error,
2445 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2448 * Returns 0 on success or else a negative code which is either -ENOMEM or
2449 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2451 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2452 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2455 int ret
, created
= 0;
2456 struct ust_app_session
*ua_sess
;
2460 assert(ua_sess_ptr
);
2462 health_code_update();
2464 ua_sess
= lookup_session_by_app(usess
, app
);
2465 if (ua_sess
== NULL
) {
2466 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2467 app
->pid
, usess
->id
);
2468 ua_sess
= alloc_ust_app_session();
2469 if (ua_sess
== NULL
) {
2470 /* Only malloc can failed so something is really wrong */
2474 shadow_copy_session(ua_sess
, usess
, app
);
2478 switch (usess
->buffer_type
) {
2479 case LTTNG_BUFFER_PER_PID
:
2480 /* Init local registry. */
2481 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2483 delete_ust_app_session(-1, ua_sess
, app
);
2487 case LTTNG_BUFFER_PER_UID
:
2488 /* Look for a global registry. If none exists, create one. */
2489 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2491 delete_ust_app_session(-1, ua_sess
, app
);
2501 health_code_update();
2503 if (ua_sess
->handle
== -1) {
2504 pthread_mutex_lock(&app
->sock_lock
);
2505 ret
= ustctl_create_session(app
->sock
);
2506 pthread_mutex_unlock(&app
->sock_lock
);
2508 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2509 ERR("Creating session for app pid %d with ret %d",
2512 DBG("UST app creating session failed. Application is dead");
2514 * This is normal behavior, an application can die during the
2515 * creation process. Don't report an error so the execution can
2516 * continue normally. This will get flagged ENOTCONN and the
2517 * caller will handle it.
2521 delete_ust_app_session(-1, ua_sess
, app
);
2522 if (ret
!= -ENOMEM
) {
2524 * Tracer is probably gone or got an internal error so let's
2525 * behave like it will soon unregister or not usable.
2532 ua_sess
->handle
= ret
;
2534 /* Add ust app session to app's HT */
2535 lttng_ht_node_init_u64(&ua_sess
->node
,
2536 ua_sess
->tracing_id
);
2537 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2538 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2539 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2540 &ua_sess
->ust_objd_node
);
2542 DBG2("UST app session created successfully with handle %d", ret
);
2545 *ua_sess_ptr
= ua_sess
;
2547 *is_created
= created
;
2550 /* Everything went well. */
2554 health_code_update();
2559 * Match function for a hash table lookup of ust_app_ctx.
2561 * It matches an ust app context based on the context type and, in the case
2562 * of perf counters, their name.
2564 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2566 struct ust_app_ctx
*ctx
;
2567 const struct lttng_ust_context_attr
*key
;
2572 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2576 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2581 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2582 if (strncmp(key
->u
.perf_counter
.name
,
2583 ctx
->ctx
.u
.perf_counter
.name
,
2584 sizeof(key
->u
.perf_counter
.name
))) {
2588 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2589 if (strcmp(key
->u
.app_ctx
.provider_name
,
2590 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2591 strcmp(key
->u
.app_ctx
.ctx_name
,
2592 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2608 * Lookup for an ust app context from an lttng_ust_context.
2610 * Must be called while holding RCU read side lock.
2611 * Return an ust_app_ctx object or NULL on error.
2614 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2615 struct lttng_ust_context_attr
*uctx
)
2617 struct lttng_ht_iter iter
;
2618 struct lttng_ht_node_ulong
*node
;
2619 struct ust_app_ctx
*app_ctx
= NULL
;
2624 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2625 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2626 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2627 node
= lttng_ht_iter_get_node_ulong(&iter
);
2632 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2639 * Create a context for the channel on the tracer.
2641 * Called with UST app session lock held and a RCU read side lock.
2644 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2645 struct lttng_ust_context_attr
*uctx
,
2646 struct ust_app
*app
)
2649 struct ust_app_ctx
*ua_ctx
;
2651 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2653 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2659 ua_ctx
= alloc_ust_app_ctx(uctx
);
2660 if (ua_ctx
== NULL
) {
2666 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2667 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2668 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2670 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2680 * Enable on the tracer side a ust app event for the session and channel.
2682 * Called with UST app session lock held.
2685 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2686 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2690 ret
= enable_ust_object(app
, ua_event
->obj
);
2695 ua_event
->enabled
= 1;
2702 * Disable on the tracer side a ust app event for the session and channel.
2704 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2705 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2709 ret
= disable_ust_object(app
, ua_event
->obj
);
2714 ua_event
->enabled
= 0;
2721 * Lookup ust app channel for session and disable it on the tracer side.
2724 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2725 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2729 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2734 ua_chan
->enabled
= 0;
2741 * Lookup ust app channel for session and enable it on the tracer side. This
2742 * MUST be called with a RCU read side lock acquired.
2744 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2745 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2748 struct lttng_ht_iter iter
;
2749 struct lttng_ht_node_str
*ua_chan_node
;
2750 struct ust_app_channel
*ua_chan
;
2752 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2753 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2754 if (ua_chan_node
== NULL
) {
2755 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2756 uchan
->name
, ua_sess
->tracing_id
);
2760 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2762 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2772 * Ask the consumer to create a channel and get it if successful.
2774 * Called with UST app session lock held.
2776 * Return 0 on success or else a negative value.
2778 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2779 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2780 int bitness
, struct ust_registry_session
*registry
,
2781 uint64_t trace_archive_id
)
2784 unsigned int nb_fd
= 0;
2785 struct consumer_socket
*socket
;
2793 health_code_update();
2795 /* Get the right consumer socket for the application. */
2796 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2802 health_code_update();
2804 /* Need one fd for the channel. */
2805 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2807 ERR("Exhausted number of available FD upon create channel");
2812 * Ask consumer to create channel. The consumer will return the number of
2813 * stream we have to expect.
2815 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2816 registry
, usess
->current_trace_chunk
);
2822 * Compute the number of fd needed before receiving them. It must be 2 per
2823 * stream (2 being the default value here).
2825 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2827 /* Reserve the amount of file descriptor we need. */
2828 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2830 ERR("Exhausted number of available FD upon create channel");
2831 goto error_fd_get_stream
;
2834 health_code_update();
2837 * Now get the channel from the consumer. This call wil populate the stream
2838 * list of that channel and set the ust objects.
2840 if (usess
->consumer
->enabled
) {
2841 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2851 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2852 error_fd_get_stream
:
2854 * Initiate a destroy channel on the consumer since we had an error
2855 * handling it on our side. The return value is of no importance since we
2856 * already have a ret value set by the previous error that we need to
2859 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2861 lttng_fd_put(LTTNG_FD_APPS
, 1);
2863 health_code_update();
2869 * Duplicate the ust data object of the ust app stream and save it in the
2870 * buffer registry stream.
2872 * Return 0 on success or else a negative value.
2874 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2875 struct ust_app_stream
*stream
)
2882 /* Reserve the amount of file descriptor we need. */
2883 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2885 ERR("Exhausted number of available FD upon duplicate stream");
2889 /* Duplicate object for stream once the original is in the registry. */
2890 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2891 reg_stream
->obj
.ust
);
2893 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2894 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2895 lttng_fd_put(LTTNG_FD_APPS
, 2);
2898 stream
->handle
= stream
->obj
->handle
;
2905 * Duplicate the ust data object of the ust app. channel and save it in the
2906 * buffer registry channel.
2908 * Return 0 on success or else a negative value.
2910 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2911 struct ust_app_channel
*ua_chan
)
2918 /* Need two fds for the channel. */
2919 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2921 ERR("Exhausted number of available FD upon duplicate channel");
2925 /* Duplicate object for stream once the original is in the registry. */
2926 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2928 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2929 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2932 ua_chan
->handle
= ua_chan
->obj
->handle
;
2937 lttng_fd_put(LTTNG_FD_APPS
, 1);
2943 * For a given channel buffer registry, setup all streams of the given ust
2944 * application channel.
2946 * Return 0 on success or else a negative value.
2948 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2949 struct ust_app_channel
*ua_chan
,
2950 struct ust_app
*app
)
2953 struct ust_app_stream
*stream
, *stmp
;
2958 DBG2("UST app setup buffer registry stream");
2960 /* Send all streams to application. */
2961 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2962 struct buffer_reg_stream
*reg_stream
;
2964 ret
= buffer_reg_stream_create(®_stream
);
2970 * Keep original pointer and nullify it in the stream so the delete
2971 * stream call does not release the object.
2973 reg_stream
->obj
.ust
= stream
->obj
;
2975 buffer_reg_stream_add(reg_stream
, reg_chan
);
2977 /* We don't need the streams anymore. */
2978 cds_list_del(&stream
->list
);
2979 delete_ust_app_stream(-1, stream
, app
);
2987 * Create a buffer registry channel for the given session registry and
2988 * application channel object. If regp pointer is valid, it's set with the
2989 * created object. Important, the created object is NOT added to the session
2990 * registry hash table.
2992 * Return 0 on success else a negative value.
2994 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2995 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2998 struct buffer_reg_channel
*reg_chan
= NULL
;
3003 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3005 /* Create buffer registry channel. */
3006 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
3011 reg_chan
->consumer_key
= ua_chan
->key
;
3012 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3013 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3015 /* Create and add a channel registry to session. */
3016 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
3017 ua_chan
->tracing_channel_id
);
3021 buffer_reg_channel_add(reg_sess
, reg_chan
);
3030 /* Safe because the registry channel object was not added to any HT. */
3031 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3037 * Setup buffer registry channel for the given session registry and application
3038 * channel object. If regp pointer is valid, it's set with the created object.
3040 * Return 0 on success else a negative value.
3042 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3043 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
3044 struct ust_app
*app
)
3051 assert(ua_chan
->obj
);
3053 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3055 /* Setup all streams for the registry. */
3056 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
3061 reg_chan
->obj
.ust
= ua_chan
->obj
;
3062 ua_chan
->obj
= NULL
;
3067 buffer_reg_channel_remove(reg_sess
, reg_chan
);
3068 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3073 * Send buffer registry channel to the application.
3075 * Return 0 on success else a negative value.
3077 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
3078 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
3079 struct ust_app_channel
*ua_chan
)
3082 struct buffer_reg_stream
*reg_stream
;
3089 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3091 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
3096 /* Send channel to the application. */
3097 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3098 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3099 ret
= -ENOTCONN
; /* Caused by app exiting. */
3101 } else if (ret
< 0) {
3105 health_code_update();
3107 /* Send all streams to application. */
3108 pthread_mutex_lock(®_chan
->stream_list_lock
);
3109 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
3110 struct ust_app_stream stream
;
3112 ret
= duplicate_stream_object(reg_stream
, &stream
);
3114 goto error_stream_unlock
;
3117 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3119 (void) release_ust_app_stream(-1, &stream
, app
);
3120 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3121 ret
= -ENOTCONN
; /* Caused by app exiting. */
3123 goto error_stream_unlock
;
3127 * The return value is not important here. This function will output an
3130 (void) release_ust_app_stream(-1, &stream
, app
);
3132 ua_chan
->is_sent
= 1;
3134 error_stream_unlock
:
3135 pthread_mutex_unlock(®_chan
->stream_list_lock
);
3141 * Create and send to the application the created buffers with per UID buffers.
3143 * This MUST be called with a RCU read side lock acquired.
3144 * The session list lock and the session's lock must be acquired.
3146 * Return 0 on success else a negative value.
3148 static int create_channel_per_uid(struct ust_app
*app
,
3149 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3150 struct ust_app_channel
*ua_chan
)
3153 struct buffer_reg_uid
*reg_uid
;
3154 struct buffer_reg_channel
*reg_chan
;
3155 struct ltt_session
*session
= NULL
;
3156 enum lttng_error_code notification_ret
;
3157 struct ust_registry_channel
*chan_reg
;
3164 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3166 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3168 * The session creation handles the creation of this global registry
3169 * object. If none can be find, there is a code flow problem or a
3174 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3180 /* Create the buffer registry channel object. */
3181 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
3183 ERR("Error creating the UST channel \"%s\" registry instance",
3188 session
= session_find_by_id(ua_sess
->tracing_id
);
3190 assert(pthread_mutex_trylock(&session
->lock
));
3191 assert(session_trylock_list());
3194 * Create the buffers on the consumer side. This call populates the
3195 * ust app channel object with all streams and data object.
3197 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3198 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
3199 session
->most_recent_chunk_id
.value
);
3201 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3205 * Let's remove the previously created buffer registry channel so
3206 * it's not visible anymore in the session registry.
3208 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3209 ua_chan
->tracing_channel_id
, false);
3210 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
3211 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3216 * Setup the streams and add it to the session registry.
3218 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3219 ua_chan
, reg_chan
, app
);
3221 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3225 /* Notify the notification subsystem of the channel's creation. */
3226 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
3227 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3228 ua_chan
->tracing_channel_id
);
3230 chan_reg
->consumer_key
= ua_chan
->key
;
3232 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
3234 notification_ret
= notification_thread_command_add_channel(
3235 notification_thread_handle
, session
->name
,
3236 ua_sess
->effective_credentials
.uid
,
3237 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
3238 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3239 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3240 if (notification_ret
!= LTTNG_OK
) {
3241 ret
= - (int) notification_ret
;
3242 ERR("Failed to add channel to notification thread");
3247 /* Send buffers to the application. */
3248 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
3250 if (ret
!= -ENOTCONN
) {
3251 ERR("Error sending channel to application");
3258 session_put(session
);
3264 * Create and send to the application the created buffers with per PID buffers.
3266 * Called with UST app session lock held.
3267 * The session list lock and the session's lock must be acquired.
3269 * Return 0 on success else a negative value.
3271 static int create_channel_per_pid(struct ust_app
*app
,
3272 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3273 struct ust_app_channel
*ua_chan
)
3276 struct ust_registry_session
*registry
;
3277 enum lttng_error_code cmd_ret
;
3278 struct ltt_session
*session
= NULL
;
3279 uint64_t chan_reg_key
;
3280 struct ust_registry_channel
*chan_reg
;
3287 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3291 registry
= get_session_registry(ua_sess
);
3292 /* The UST app session lock is held, registry shall not be null. */
3295 /* Create and add a new channel registry to session. */
3296 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3298 ERR("Error creating the UST channel \"%s\" registry instance",
3303 session
= session_find_by_id(ua_sess
->tracing_id
);
3306 assert(pthread_mutex_trylock(&session
->lock
));
3307 assert(session_trylock_list());
3309 /* Create and get channel on the consumer side. */
3310 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3311 app
->bits_per_long
, registry
,
3312 session
->most_recent_chunk_id
.value
);
3314 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3316 goto error_remove_from_registry
;
3319 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3321 if (ret
!= -ENOTCONN
) {
3322 ERR("Error sending channel to application");
3324 goto error_remove_from_registry
;
3327 chan_reg_key
= ua_chan
->key
;
3328 pthread_mutex_lock(®istry
->lock
);
3329 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3331 chan_reg
->consumer_key
= ua_chan
->key
;
3332 pthread_mutex_unlock(®istry
->lock
);
3334 cmd_ret
= notification_thread_command_add_channel(
3335 notification_thread_handle
, session
->name
,
3336 ua_sess
->effective_credentials
.uid
,
3337 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
3338 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3339 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3340 if (cmd_ret
!= LTTNG_OK
) {
3341 ret
= - (int) cmd_ret
;
3342 ERR("Failed to add channel to notification thread");
3343 goto error_remove_from_registry
;
3346 error_remove_from_registry
:
3348 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3353 session_put(session
);
3359 * From an already allocated ust app channel, create the channel buffers if
3360 * needed and send them to the application. This MUST be called with a RCU read
3361 * side lock acquired.
3363 * Called with UST app session lock held.
3365 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3366 * the application exited concurrently.
3368 static int ust_app_channel_send(struct ust_app
*app
,
3369 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3370 struct ust_app_channel
*ua_chan
)
3376 assert(usess
->active
);
3380 /* Handle buffer type before sending the channel to the application. */
3381 switch (usess
->buffer_type
) {
3382 case LTTNG_BUFFER_PER_UID
:
3384 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3390 case LTTNG_BUFFER_PER_PID
:
3392 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3404 /* Initialize ust objd object using the received handle and add it. */
3405 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3406 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3408 /* If channel is not enabled, disable it on the tracer */
3409 if (!ua_chan
->enabled
) {
3410 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3421 * Create UST app channel and return it through ua_chanp if not NULL.
3423 * Called with UST app session lock and RCU read-side lock held.
3425 * Return 0 on success or else a negative value.
3427 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3428 struct ltt_ust_channel
*uchan
,
3429 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3430 struct ust_app_channel
**ua_chanp
)
3433 struct lttng_ht_iter iter
;
3434 struct lttng_ht_node_str
*ua_chan_node
;
3435 struct ust_app_channel
*ua_chan
;
3437 /* Lookup channel in the ust app session */
3438 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3439 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3440 if (ua_chan_node
!= NULL
) {
3441 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3445 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3446 if (ua_chan
== NULL
) {
3447 /* Only malloc can fail here */
3451 shadow_copy_channel(ua_chan
, uchan
);
3453 /* Set channel type. */
3454 ua_chan
->attr
.type
= type
;
3456 /* Only add the channel if successful on the tracer side. */
3457 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3460 *ua_chanp
= ua_chan
;
3463 /* Everything went well. */
3471 * Create UST app event and create it on the tracer side.
3473 * Called with ust app session mutex held.
3476 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3477 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3478 struct ust_app
*app
)
3481 struct ust_app_event
*ua_event
;
3483 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3484 if (ua_event
== NULL
) {
3485 /* Only failure mode of alloc_ust_app_event(). */
3489 shadow_copy_event(ua_event
, uevent
);
3491 /* Create it on the tracer side */
3492 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3495 * Not found previously means that it does not exist on the
3496 * tracer. If the application reports that the event existed,
3497 * it means there is a bug in the sessiond or lttng-ust
3498 * (or corruption, etc.)
3500 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3501 ERR("Tracer for application reported that an event being created already existed: "
3502 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3504 app
->pid
, app
->ppid
, app
->uid
,
3510 add_unique_ust_app_event(ua_chan
, ua_event
);
3512 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3519 /* Valid. Calling here is already in a read side lock */
3520 delete_ust_app_event(-1, ua_event
, app
);
3525 * Create UST app event and create it on the tracer side.
3527 * Called with ust app session mutex held.
3530 int create_ust_app_token_event_rule(struct lttng_trigger
*trigger
,
3531 struct ust_app
*app
)
3534 struct ust_app_token_event_rule
*ua_token
;
3536 ua_token
= alloc_ust_app_token_event_rule(trigger
);
3537 if (ua_token
== NULL
) {
3542 /* Create it on the tracer side */
3543 ret
= create_ust_token_event_rule(app
, ua_token
);
3546 * Not found previously means that it does not exist on the
3547 * tracer. If the application reports that the event existed,
3548 * it means there is a bug in the sessiond or lttng-ust
3549 * (or corruption, etc.)
3551 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3552 ERR("Tracer for application reported that a token event rule being created already existed: "
3553 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3554 lttng_trigger_get_key(trigger
),
3555 app
->pid
, app
->ppid
, app
->uid
,
3561 lttng_ht_add_unique_u64(app
->tokens_ht
, &ua_token
->node
);
3563 DBG2("UST app create token event rule %" PRIu64
" for PID %d completed", lttng_trigger_get_key(trigger
),
3570 /* Valid. Calling here is already in a read side lock */
3571 delete_ust_app_token_event_rule(-1, ua_token
, app
);
3576 * Create UST metadata and open it on the tracer side.
3578 * Called with UST app session lock held and RCU read side lock.
3580 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3581 struct ust_app
*app
, struct consumer_output
*consumer
)
3584 struct ust_app_channel
*metadata
;
3585 struct consumer_socket
*socket
;
3586 struct ust_registry_session
*registry
;
3587 struct ltt_session
*session
= NULL
;
3593 registry
= get_session_registry(ua_sess
);
3594 /* The UST app session is held registry shall not be null. */
3597 pthread_mutex_lock(®istry
->lock
);
3599 /* Metadata already exists for this registry or it was closed previously */
3600 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3605 /* Allocate UST metadata */
3606 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3608 /* malloc() failed */
3613 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3615 /* Need one fd for the channel. */
3616 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3618 ERR("Exhausted number of available FD upon create metadata");
3622 /* Get the right consumer socket for the application. */
3623 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3626 goto error_consumer
;
3630 * Keep metadata key so we can identify it on the consumer side. Assign it
3631 * to the registry *before* we ask the consumer so we avoid the race of the
3632 * consumer requesting the metadata and the ask_channel call on our side
3633 * did not returned yet.
3635 registry
->metadata_key
= metadata
->key
;
3637 session
= session_find_by_id(ua_sess
->tracing_id
);
3640 assert(pthread_mutex_trylock(&session
->lock
));
3641 assert(session_trylock_list());
3644 * Ask the metadata channel creation to the consumer. The metadata object
3645 * will be created by the consumer and kept their. However, the stream is
3646 * never added or monitored until we do a first push metadata to the
3649 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3650 registry
, session
->current_trace_chunk
);
3652 /* Nullify the metadata key so we don't try to close it later on. */
3653 registry
->metadata_key
= 0;
3654 goto error_consumer
;
3658 * The setup command will make the metadata stream be sent to the relayd,
3659 * if applicable, and the thread managing the metadatas. This is important
3660 * because after this point, if an error occurs, the only way the stream
3661 * can be deleted is to be monitored in the consumer.
3663 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3665 /* Nullify the metadata key so we don't try to close it later on. */
3666 registry
->metadata_key
= 0;
3667 goto error_consumer
;
3670 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3671 metadata
->key
, app
->pid
);
3674 lttng_fd_put(LTTNG_FD_APPS
, 1);
3675 delete_ust_app_channel(-1, metadata
, app
);
3677 pthread_mutex_unlock(®istry
->lock
);
3679 session_put(session
);
3685 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3686 * acquired before calling this function.
3688 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3690 struct ust_app
*app
= NULL
;
3691 struct lttng_ht_node_ulong
*node
;
3692 struct lttng_ht_iter iter
;
3694 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3695 node
= lttng_ht_iter_get_node_ulong(&iter
);
3697 DBG2("UST app no found with pid %d", pid
);
3701 DBG2("Found UST app by pid %d", pid
);
3703 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3710 * Allocate and init an UST app object using the registration information and
3711 * the command socket. This is called when the command socket connects to the
3714 * The object is returned on success or else NULL.
3716 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3718 struct ust_app
*lta
= NULL
;
3719 struct lttng_pipe
*trigger_event_source_pipe
= NULL
;
3724 DBG3("UST app creating application for socket %d", sock
);
3726 if ((msg
->bits_per_long
== 64 &&
3727 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3728 || (msg
->bits_per_long
== 32 &&
3729 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3730 ERR("Registration failed: application \"%s\" (pid: %d) has "
3731 "%d-bit long, but no consumerd for this size is available.\n",
3732 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3736 trigger_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3737 if (!trigger_event_source_pipe
) {
3738 PERROR("Open trigger pipe");
3742 lta
= zmalloc(sizeof(struct ust_app
));
3748 lta
->token_communication
.trigger_event_pipe
= trigger_event_source_pipe
;
3750 lta
->ppid
= msg
->ppid
;
3751 lta
->uid
= msg
->uid
;
3752 lta
->gid
= msg
->gid
;
3754 lta
->bits_per_long
= msg
->bits_per_long
;
3755 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3756 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3757 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3758 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3759 lta
->long_alignment
= msg
->long_alignment
;
3760 lta
->byte_order
= msg
->byte_order
;
3762 lta
->v_major
= msg
->major
;
3763 lta
->v_minor
= msg
->minor
;
3764 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3765 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3766 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3767 lta
->notify_sock
= -1;
3768 lta
->tokens_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3770 /* Copy name and make sure it's NULL terminated. */
3771 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3772 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3775 * Before this can be called, when receiving the registration information,
3776 * the application compatibility is checked. So, at this point, the
3777 * application can work with this session daemon.
3779 lta
->compatible
= 1;
3781 lta
->pid
= msg
->pid
;
3782 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3784 pthread_mutex_init(<a
->sock_lock
, NULL
);
3785 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3787 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3793 * For a given application object, add it to every hash table.
3795 void ust_app_add(struct ust_app
*app
)
3798 assert(app
->notify_sock
>= 0);
3800 app
->registration_time
= time(NULL
);
3805 * On a re-registration, we want to kick out the previous registration of
3808 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3811 * The socket _should_ be unique until _we_ call close. So, a add_unique
3812 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3813 * already in the table.
3815 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3817 /* Add application to the notify socket hash table. */
3818 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3819 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3821 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3822 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3823 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3830 * Set the application version into the object.
3832 * Return 0 on success else a negative value either an errno code or a
3833 * LTTng-UST error code.
3835 int ust_app_version(struct ust_app
*app
)
3841 pthread_mutex_lock(&app
->sock_lock
);
3842 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3843 pthread_mutex_unlock(&app
->sock_lock
);
3845 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3846 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3848 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3856 * Setup the base trigger group.
3858 * Return 0 on success else a negative value either an errno code or a
3859 * LTTng-UST error code.
3861 int ust_app_setup_trigger_group(struct ust_app
*app
)
3865 struct lttng_ust_object_data
*group
= NULL
;
3866 enum lttng_error_code lttng_ret
;
3870 /* Get the write side of the pipe */
3871 writefd
= lttng_pipe_get_writefd(app
->token_communication
.trigger_event_pipe
);
3873 pthread_mutex_lock(&app
->sock_lock
);
3874 ret
= ustctl_create_trigger_group(app
->sock
, writefd
, &group
);
3875 pthread_mutex_unlock(&app
->sock_lock
);
3877 ERR("UST app %d create_trigger_group failed with ret %d", app
->sock
, ret
);
3881 app
->token_communication
.handle
= group
;
3883 lttng_ret
= notification_thread_command_add_application(
3884 notification_thread_handle
, app
->token_communication
.trigger_event_pipe
);
3885 if (lttng_ret
!= LTTNG_OK
) {
3888 ERR("Failed to add channel to notification thread");
3897 * Unregister app by removing it from the global traceable app list and freeing
3900 * The socket is already closed at this point so no close to sock.
3902 void ust_app_unregister(int sock
)
3904 enum lttng_error_code ret_code
;
3905 struct ust_app
*lta
;
3906 struct lttng_ht_node_ulong
*node
;
3907 struct lttng_ht_iter ust_app_sock_iter
;
3908 struct lttng_ht_iter iter
;
3909 struct ust_app_session
*ua_sess
;
3914 /* Get the node reference for a call_rcu */
3915 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3916 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3919 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3920 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3923 * For per-PID buffers, perform "push metadata" and flush all
3924 * application streams before removing app from hash tables,
3925 * ensuring proper behavior of data_pending check.
3926 * Remove sessions so they are not visible during deletion.
3928 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3930 struct ust_registry_session
*registry
;
3932 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3934 /* The session was already removed so scheduled for teardown. */
3938 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3939 (void) ust_app_flush_app_session(lta
, ua_sess
);
3943 * Add session to list for teardown. This is safe since at this point we
3944 * are the only one using this list.
3946 pthread_mutex_lock(&ua_sess
->lock
);
3948 if (ua_sess
->deleted
) {
3949 pthread_mutex_unlock(&ua_sess
->lock
);
3954 * Normally, this is done in the delete session process which is
3955 * executed in the call rcu below. However, upon registration we can't
3956 * afford to wait for the grace period before pushing data or else the
3957 * data pending feature can race between the unregistration and stop
3958 * command where the data pending command is sent *before* the grace
3961 * The close metadata below nullifies the metadata pointer in the
3962 * session so the delete session will NOT push/close a second time.
3964 registry
= get_session_registry(ua_sess
);
3966 /* Push metadata for application before freeing the application. */
3967 (void) push_metadata(registry
, ua_sess
->consumer
);
3970 * Don't ask to close metadata for global per UID buffers. Close
3971 * metadata only on destroy trace session in this case. Also, the
3972 * previous push metadata could have flag the metadata registry to
3973 * close so don't send a close command if closed.
3975 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3976 /* And ask to close it for this session registry. */
3977 (void) close_metadata(registry
, ua_sess
->consumer
);
3980 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3982 pthread_mutex_unlock(&ua_sess
->lock
);
3985 /* Remove application from PID hash table */
3986 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3990 * Remove application from notify hash table. The thread handling the
3991 * notify socket could have deleted the node so ignore on error because
3992 * either way it's valid. The close of that socket is handled by the
3993 * apps_notify_thread.
3995 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3996 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3999 * Ignore return value since the node might have been removed before by an
4000 * add replace during app registration because the PID can be reassigned by
4003 iter
.iter
.node
= <a
->pid_n
.node
;
4004 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4006 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4010 ret_code
= notification_thread_command_remove_application(
4011 notification_thread_handle
,
4012 lta
->token_communication
.trigger_event_pipe
);
4013 if (ret_code
!= LTTNG_OK
) {
4014 ERR("Failed to remove application from notification thread");
4018 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4025 * Fill events array with all events name of all registered apps.
4027 int ust_app_list_events(struct lttng_event
**events
)
4030 size_t nbmem
, count
= 0;
4031 struct lttng_ht_iter iter
;
4032 struct ust_app
*app
;
4033 struct lttng_event
*tmp_event
;
4035 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4036 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
4037 if (tmp_event
== NULL
) {
4038 PERROR("zmalloc ust app events");
4045 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4046 struct lttng_ust_tracepoint_iter uiter
;
4048 health_code_update();
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 pthread_mutex_lock(&app
->sock_lock
);
4058 handle
= ustctl_tracepoint_list(app
->sock
);
4060 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4061 ERR("UST app list events getting handle failed for app pid %d",
4064 pthread_mutex_unlock(&app
->sock_lock
);
4068 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
4069 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4070 /* Handle ustctl error. */
4074 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4075 ERR("UST app tp list get failed for app %d with ret %d",
4078 DBG3("UST app tp list get failed. Application is dead");
4080 * This is normal behavior, an application can die during the
4081 * creation process. Don't report an error so the execution can
4082 * continue normally. Continue normal execution.
4087 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4088 if (release_ret
< 0 &&
4089 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4090 release_ret
!= -EPIPE
) {
4091 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4093 pthread_mutex_unlock(&app
->sock_lock
);
4097 health_code_update();
4098 if (count
>= nbmem
) {
4099 /* In case the realloc fails, we free the memory */
4100 struct lttng_event
*new_tmp_event
;
4103 new_nbmem
= nbmem
<< 1;
4104 DBG2("Reallocating event list from %zu to %zu entries",
4106 new_tmp_event
= realloc(tmp_event
,
4107 new_nbmem
* sizeof(struct lttng_event
));
4108 if (new_tmp_event
== NULL
) {
4111 PERROR("realloc ust app events");
4114 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4115 if (release_ret
< 0 &&
4116 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4117 release_ret
!= -EPIPE
) {
4118 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4120 pthread_mutex_unlock(&app
->sock_lock
);
4123 /* Zero the new memory */
4124 memset(new_tmp_event
+ nbmem
, 0,
4125 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4127 tmp_event
= new_tmp_event
;
4129 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
4130 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4131 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
4132 tmp_event
[count
].pid
= app
->pid
;
4133 tmp_event
[count
].enabled
= -1;
4136 ret
= ustctl_release_handle(app
->sock
, handle
);
4137 pthread_mutex_unlock(&app
->sock_lock
);
4138 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4139 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4144 *events
= tmp_event
;
4146 DBG2("UST app list events done (%zu events)", count
);
4151 health_code_update();
4156 * Fill events array with all events name of all registered apps.
4158 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4161 size_t nbmem
, count
= 0;
4162 struct lttng_ht_iter iter
;
4163 struct ust_app
*app
;
4164 struct lttng_event_field
*tmp_event
;
4166 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4167 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
4168 if (tmp_event
== NULL
) {
4169 PERROR("zmalloc ust app event fields");
4176 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4177 struct lttng_ust_field_iter uiter
;
4179 health_code_update();
4181 if (!app
->compatible
) {
4183 * TODO: In time, we should notice the caller of this error by
4184 * telling him that this is a version error.
4188 pthread_mutex_lock(&app
->sock_lock
);
4189 handle
= ustctl_tracepoint_field_list(app
->sock
);
4191 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4192 ERR("UST app list field getting handle failed for app pid %d",
4195 pthread_mutex_unlock(&app
->sock_lock
);
4199 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
4200 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4201 /* Handle ustctl error. */
4205 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4206 ERR("UST app tp list field failed for app %d with ret %d",
4209 DBG3("UST app tp list field failed. Application is dead");
4211 * This is normal behavior, an application can die during the
4212 * creation process. Don't report an error so the execution can
4213 * continue normally. Reset list and count for next app.
4218 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4219 pthread_mutex_unlock(&app
->sock_lock
);
4220 if (release_ret
< 0 &&
4221 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4222 release_ret
!= -EPIPE
) {
4223 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4228 health_code_update();
4229 if (count
>= nbmem
) {
4230 /* In case the realloc fails, we free the memory */
4231 struct lttng_event_field
*new_tmp_event
;
4234 new_nbmem
= nbmem
<< 1;
4235 DBG2("Reallocating event field list from %zu to %zu entries",
4237 new_tmp_event
= realloc(tmp_event
,
4238 new_nbmem
* sizeof(struct lttng_event_field
));
4239 if (new_tmp_event
== NULL
) {
4242 PERROR("realloc ust app event fields");
4245 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4246 pthread_mutex_unlock(&app
->sock_lock
);
4248 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4249 release_ret
!= -EPIPE
) {
4250 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4254 /* Zero the new memory */
4255 memset(new_tmp_event
+ nbmem
, 0,
4256 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4258 tmp_event
= new_tmp_event
;
4261 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
4262 /* Mapping between these enums matches 1 to 1. */
4263 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4264 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4266 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
4267 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4268 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4269 tmp_event
[count
].event
.pid
= app
->pid
;
4270 tmp_event
[count
].event
.enabled
= -1;
4273 ret
= ustctl_release_handle(app
->sock
, handle
);
4274 pthread_mutex_unlock(&app
->sock_lock
);
4276 ret
!= -LTTNG_UST_ERR_EXITING
&&
4278 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4283 *fields
= tmp_event
;
4285 DBG2("UST app list event fields done (%zu events)", count
);
4290 health_code_update();
4295 * Free and clean all traceable apps of the global list.
4297 * Should _NOT_ be called with RCU read-side lock held.
4299 void ust_app_clean_list(void)
4302 struct ust_app
*app
;
4303 struct lttng_ht_iter iter
;
4305 DBG2("UST app cleaning registered apps hash table");
4309 /* Cleanup notify socket hash table */
4310 if (ust_app_ht_by_notify_sock
) {
4311 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4312 notify_sock_n
.node
) {
4313 struct cds_lfht_node
*node
;
4314 struct ust_app
*app
;
4316 node
= cds_lfht_iter_get_node(&iter
.iter
);
4321 app
= container_of(node
, struct ust_app
,
4322 notify_sock_n
.node
);
4323 ust_app_notify_sock_unregister(app
->notify_sock
);
4328 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4329 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4331 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4335 /* Cleanup socket hash table */
4336 if (ust_app_ht_by_sock
) {
4337 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4339 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4346 /* Destroy is done only when the ht is empty */
4348 ht_cleanup_push(ust_app_ht
);
4350 if (ust_app_ht_by_sock
) {
4351 ht_cleanup_push(ust_app_ht_by_sock
);
4353 if (ust_app_ht_by_notify_sock
) {
4354 ht_cleanup_push(ust_app_ht_by_notify_sock
);
4359 * Init UST app hash table.
4361 int ust_app_ht_alloc(void)
4363 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4367 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4368 if (!ust_app_ht_by_sock
) {
4371 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4372 if (!ust_app_ht_by_notify_sock
) {
4379 * For a specific UST session, disable the channel for all registered apps.
4381 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4382 struct ltt_ust_channel
*uchan
)
4385 struct lttng_ht_iter iter
;
4386 struct lttng_ht_node_str
*ua_chan_node
;
4387 struct ust_app
*app
;
4388 struct ust_app_session
*ua_sess
;
4389 struct ust_app_channel
*ua_chan
;
4391 assert(usess
->active
);
4392 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4393 uchan
->name
, usess
->id
);
4397 /* For every registered applications */
4398 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4399 struct lttng_ht_iter uiter
;
4400 if (!app
->compatible
) {
4402 * TODO: In time, we should notice the caller of this error by
4403 * telling him that this is a version error.
4407 ua_sess
= lookup_session_by_app(usess
, app
);
4408 if (ua_sess
== NULL
) {
4413 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4414 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4415 /* If the session if found for the app, the channel must be there */
4416 assert(ua_chan_node
);
4418 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4419 /* The channel must not be already disabled */
4420 assert(ua_chan
->enabled
== 1);
4422 /* Disable channel onto application */
4423 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4425 /* XXX: We might want to report this error at some point... */
4435 * For a specific UST session, enable the channel for all registered apps.
4437 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4438 struct ltt_ust_channel
*uchan
)
4441 struct lttng_ht_iter iter
;
4442 struct ust_app
*app
;
4443 struct ust_app_session
*ua_sess
;
4445 assert(usess
->active
);
4446 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4447 uchan
->name
, usess
->id
);
4451 /* For every registered applications */
4452 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4453 if (!app
->compatible
) {
4455 * TODO: In time, we should notice the caller of this error by
4456 * telling him that this is a version error.
4460 ua_sess
= lookup_session_by_app(usess
, app
);
4461 if (ua_sess
== NULL
) {
4465 /* Enable channel onto application */
4466 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4468 /* XXX: We might want to report this error at some point... */
4478 * Disable an event in a channel and for a specific session.
4480 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4481 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4484 struct lttng_ht_iter iter
, uiter
;
4485 struct lttng_ht_node_str
*ua_chan_node
;
4486 struct ust_app
*app
;
4487 struct ust_app_session
*ua_sess
;
4488 struct ust_app_channel
*ua_chan
;
4489 struct ust_app_event
*ua_event
;
4491 assert(usess
->active
);
4492 DBG("UST app disabling event %s for all apps in channel "
4493 "%s for session id %" PRIu64
,
4494 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4498 /* For all registered applications */
4499 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4500 if (!app
->compatible
) {
4502 * TODO: In time, we should notice the caller of this error by
4503 * telling him that this is a version error.
4507 ua_sess
= lookup_session_by_app(usess
, app
);
4508 if (ua_sess
== NULL
) {
4513 /* Lookup channel in the ust app session */
4514 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4515 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4516 if (ua_chan_node
== NULL
) {
4517 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4518 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4521 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4523 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4524 uevent
->filter
, uevent
->attr
.loglevel
,
4526 if (ua_event
== NULL
) {
4527 DBG2("Event %s not found in channel %s for app pid %d."
4528 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4532 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4534 /* XXX: Report error someday... */
4543 /* The ua_sess lock must be held by the caller. */
4545 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4546 struct ust_app_session
*ua_sess
,
4547 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4548 struct ust_app_channel
**_ua_chan
)
4551 struct ust_app_channel
*ua_chan
= NULL
;
4554 ASSERT_LOCKED(ua_sess
->lock
);
4556 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4557 sizeof(uchan
->name
))) {
4558 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4562 struct ltt_ust_context
*uctx
= NULL
;
4565 * Create channel onto application and synchronize its
4568 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4569 LTTNG_UST_CHAN_PER_CPU
, usess
,
4572 ret
= ust_app_channel_send(app
, usess
,
4579 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4580 ret
= create_ust_app_channel_context(ua_chan
,
4591 * The application's socket is not valid. Either a bad socket
4592 * or a timeout on it. We can't inform the caller that for a
4593 * specific app, the session failed so lets continue here.
4595 ret
= 0; /* Not an error. */
4603 if (ret
== 0 && _ua_chan
) {
4605 * Only return the application's channel on success. Note
4606 * that the channel can still be part of the application's
4607 * channel hashtable on error.
4609 *_ua_chan
= ua_chan
;
4615 * Enable event for a specific session and channel on the tracer.
4617 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4618 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4621 struct lttng_ht_iter iter
, uiter
;
4622 struct lttng_ht_node_str
*ua_chan_node
;
4623 struct ust_app
*app
;
4624 struct ust_app_session
*ua_sess
;
4625 struct ust_app_channel
*ua_chan
;
4626 struct ust_app_event
*ua_event
;
4628 assert(usess
->active
);
4629 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4630 uevent
->attr
.name
, usess
->id
);
4633 * NOTE: At this point, this function is called only if the session and
4634 * channel passed are already created for all apps. and enabled on the
4640 /* For all registered applications */
4641 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4642 if (!app
->compatible
) {
4644 * TODO: In time, we should notice the caller of this error by
4645 * telling him that this is a version error.
4649 ua_sess
= lookup_session_by_app(usess
, app
);
4651 /* The application has problem or is probably dead. */
4655 pthread_mutex_lock(&ua_sess
->lock
);
4657 if (ua_sess
->deleted
) {
4658 pthread_mutex_unlock(&ua_sess
->lock
);
4662 /* Lookup channel in the ust app session */
4663 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4664 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4666 * It is possible that the channel cannot be found is
4667 * the channel/event creation occurs concurrently with
4668 * an application exit.
4670 if (!ua_chan_node
) {
4671 pthread_mutex_unlock(&ua_sess
->lock
);
4675 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4677 /* Get event node */
4678 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4679 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4680 if (ua_event
== NULL
) {
4681 DBG3("UST app enable event %s not found for app PID %d."
4682 "Skipping app", uevent
->attr
.name
, app
->pid
);
4686 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4688 pthread_mutex_unlock(&ua_sess
->lock
);
4692 pthread_mutex_unlock(&ua_sess
->lock
);
4701 * For a specific existing UST session and UST channel, creates the event for
4702 * all registered apps.
4704 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4705 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4708 struct lttng_ht_iter iter
, uiter
;
4709 struct lttng_ht_node_str
*ua_chan_node
;
4710 struct ust_app
*app
;
4711 struct ust_app_session
*ua_sess
;
4712 struct ust_app_channel
*ua_chan
;
4714 assert(usess
->active
);
4715 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4716 uevent
->attr
.name
, usess
->id
);
4720 /* For all registered applications */
4721 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4722 if (!app
->compatible
) {
4724 * TODO: In time, we should notice the caller of this error by
4725 * telling him that this is a version error.
4729 ua_sess
= lookup_session_by_app(usess
, app
);
4731 /* The application has problem or is probably dead. */
4735 pthread_mutex_lock(&ua_sess
->lock
);
4737 if (ua_sess
->deleted
) {
4738 pthread_mutex_unlock(&ua_sess
->lock
);
4742 /* Lookup channel in the ust app session */
4743 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4744 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4745 /* If the channel is not found, there is a code flow error */
4746 assert(ua_chan_node
);
4748 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4750 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4751 pthread_mutex_unlock(&ua_sess
->lock
);
4753 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4754 /* Possible value at this point: -ENOMEM. If so, we stop! */
4757 DBG2("UST app event %s already exist on app PID %d",
4758 uevent
->attr
.name
, app
->pid
);
4768 * Start tracing for a specific UST session and app.
4770 * Called with UST app session lock held.
4774 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4777 struct ust_app_session
*ua_sess
;
4779 DBG("Starting tracing for ust app pid %d", app
->pid
);
4783 if (!app
->compatible
) {
4787 ua_sess
= lookup_session_by_app(usess
, app
);
4788 if (ua_sess
== NULL
) {
4789 /* The session is in teardown process. Ignore and continue. */
4793 pthread_mutex_lock(&ua_sess
->lock
);
4795 if (ua_sess
->deleted
) {
4796 pthread_mutex_unlock(&ua_sess
->lock
);
4800 if (ua_sess
->enabled
) {
4801 pthread_mutex_unlock(&ua_sess
->lock
);
4805 /* Upon restart, we skip the setup, already done */
4806 if (ua_sess
->started
) {
4811 * Create the metadata for the application. This returns gracefully if a
4812 * metadata was already set for the session.
4814 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4819 health_code_update();
4822 /* This starts the UST tracing */
4823 pthread_mutex_lock(&app
->sock_lock
);
4824 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4825 pthread_mutex_unlock(&app
->sock_lock
);
4827 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4828 ERR("Error starting tracing for app pid: %d (ret: %d)",
4831 DBG("UST app start session failed. Application is dead.");
4833 * This is normal behavior, an application can die during the
4834 * creation process. Don't report an error so the execution can
4835 * continue normally.
4837 pthread_mutex_unlock(&ua_sess
->lock
);
4843 /* Indicate that the session has been started once */
4844 ua_sess
->started
= 1;
4845 ua_sess
->enabled
= 1;
4847 pthread_mutex_unlock(&ua_sess
->lock
);
4849 health_code_update();
4851 /* Quiescent wait after starting trace */
4852 pthread_mutex_lock(&app
->sock_lock
);
4853 ret
= ustctl_wait_quiescent(app
->sock
);
4854 pthread_mutex_unlock(&app
->sock_lock
);
4855 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4856 ERR("UST app wait quiescent failed for app pid %d ret %d",
4862 health_code_update();
4866 pthread_mutex_unlock(&ua_sess
->lock
);
4868 health_code_update();
4873 * Stop tracing for a specific UST session and app.
4876 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4879 struct ust_app_session
*ua_sess
;
4880 struct ust_registry_session
*registry
;
4882 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4886 if (!app
->compatible
) {
4887 goto end_no_session
;
4890 ua_sess
= lookup_session_by_app(usess
, app
);
4891 if (ua_sess
== NULL
) {
4892 goto end_no_session
;
4895 pthread_mutex_lock(&ua_sess
->lock
);
4897 if (ua_sess
->deleted
) {
4898 pthread_mutex_unlock(&ua_sess
->lock
);
4899 goto end_no_session
;
4903 * If started = 0, it means that stop trace has been called for a session
4904 * that was never started. It's possible since we can have a fail start
4905 * from either the application manager thread or the command thread. Simply
4906 * indicate that this is a stop error.
4908 if (!ua_sess
->started
) {
4909 goto error_rcu_unlock
;
4912 health_code_update();
4914 /* This inhibits UST tracing */
4915 pthread_mutex_lock(&app
->sock_lock
);
4916 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4917 pthread_mutex_unlock(&app
->sock_lock
);
4919 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4920 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4923 DBG("UST app stop session failed. Application is dead.");
4925 * This is normal behavior, an application can die during the
4926 * creation process. Don't report an error so the execution can
4927 * continue normally.
4931 goto error_rcu_unlock
;
4934 health_code_update();
4935 ua_sess
->enabled
= 0;
4937 /* Quiescent wait after stopping trace */
4938 pthread_mutex_lock(&app
->sock_lock
);
4939 ret
= ustctl_wait_quiescent(app
->sock
);
4940 pthread_mutex_unlock(&app
->sock_lock
);
4941 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4942 ERR("UST app wait quiescent failed for app pid %d ret %d",
4946 health_code_update();
4948 registry
= get_session_registry(ua_sess
);
4950 /* The UST app session is held registry shall not be null. */
4953 /* Push metadata for application before freeing the application. */
4954 (void) push_metadata(registry
, ua_sess
->consumer
);
4957 pthread_mutex_unlock(&ua_sess
->lock
);
4960 health_code_update();
4964 pthread_mutex_unlock(&ua_sess
->lock
);
4966 health_code_update();
4971 int ust_app_flush_app_session(struct ust_app
*app
,
4972 struct ust_app_session
*ua_sess
)
4974 int ret
, retval
= 0;
4975 struct lttng_ht_iter iter
;
4976 struct ust_app_channel
*ua_chan
;
4977 struct consumer_socket
*socket
;
4979 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4983 if (!app
->compatible
) {
4984 goto end_not_compatible
;
4987 pthread_mutex_lock(&ua_sess
->lock
);
4989 if (ua_sess
->deleted
) {
4993 health_code_update();
4995 /* Flushing buffers */
4996 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4999 /* Flush buffers and push metadata. */
5000 switch (ua_sess
->buffer_type
) {
5001 case LTTNG_BUFFER_PER_PID
:
5002 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5004 health_code_update();
5005 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5007 ERR("Error flushing consumer channel");
5013 case LTTNG_BUFFER_PER_UID
:
5019 health_code_update();
5022 pthread_mutex_unlock(&ua_sess
->lock
);
5026 health_code_update();
5031 * Flush buffers for all applications for a specific UST session.
5032 * Called with UST session lock held.
5035 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5040 DBG("Flushing session buffers for all ust apps");
5044 /* Flush buffers and push metadata. */
5045 switch (usess
->buffer_type
) {
5046 case LTTNG_BUFFER_PER_UID
:
5048 struct buffer_reg_uid
*reg
;
5049 struct lttng_ht_iter iter
;
5051 /* Flush all per UID buffers associated to that session. */
5052 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5053 struct ust_registry_session
*ust_session_reg
;
5054 struct buffer_reg_channel
*reg_chan
;
5055 struct consumer_socket
*socket
;
5057 /* Get consumer socket to use to push the metadata.*/
5058 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5061 /* Ignore request if no consumer is found for the session. */
5065 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5066 reg_chan
, node
.node
) {
5068 * The following call will print error values so the return
5069 * code is of little importance because whatever happens, we
5070 * have to try them all.
5072 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
5075 ust_session_reg
= reg
->registry
->reg
.ust
;
5076 /* Push metadata. */
5077 (void) push_metadata(ust_session_reg
, usess
->consumer
);
5081 case LTTNG_BUFFER_PER_PID
:
5083 struct ust_app_session
*ua_sess
;
5084 struct lttng_ht_iter iter
;
5085 struct ust_app
*app
;
5087 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5088 ua_sess
= lookup_session_by_app(usess
, app
);
5089 if (ua_sess
== NULL
) {
5092 (void) ust_app_flush_app_session(app
, ua_sess
);
5103 health_code_update();
5108 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5109 struct ust_app_session
*ua_sess
)
5112 struct lttng_ht_iter iter
;
5113 struct ust_app_channel
*ua_chan
;
5114 struct consumer_socket
*socket
;
5116 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5120 if (!app
->compatible
) {
5121 goto end_not_compatible
;
5124 pthread_mutex_lock(&ua_sess
->lock
);
5126 if (ua_sess
->deleted
) {
5130 health_code_update();
5132 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5135 ERR("Failed to find consumer (%" PRIu32
") socket",
5136 app
->bits_per_long
);
5141 /* Clear quiescent state. */
5142 switch (ua_sess
->buffer_type
) {
5143 case LTTNG_BUFFER_PER_PID
:
5144 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5145 ua_chan
, node
.node
) {
5146 health_code_update();
5147 ret
= consumer_clear_quiescent_channel(socket
,
5150 ERR("Error clearing quiescent state for consumer channel");
5156 case LTTNG_BUFFER_PER_UID
:
5163 health_code_update();
5166 pthread_mutex_unlock(&ua_sess
->lock
);
5170 health_code_update();
5175 * Clear quiescent state in each stream for all applications for a
5176 * specific UST session.
5177 * Called with UST session lock held.
5180 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5185 DBG("Clearing stream quiescent state for all ust apps");
5189 switch (usess
->buffer_type
) {
5190 case LTTNG_BUFFER_PER_UID
:
5192 struct lttng_ht_iter iter
;
5193 struct buffer_reg_uid
*reg
;
5196 * Clear quiescent for all per UID buffers associated to
5199 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5200 struct consumer_socket
*socket
;
5201 struct buffer_reg_channel
*reg_chan
;
5203 /* Get associated consumer socket.*/
5204 socket
= consumer_find_socket_by_bitness(
5205 reg
->bits_per_long
, usess
->consumer
);
5208 * Ignore request if no consumer is found for
5214 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5215 &iter
.iter
, reg_chan
, node
.node
) {
5217 * The following call will print error values so
5218 * the return code is of little importance
5219 * because whatever happens, we have to try them
5222 (void) consumer_clear_quiescent_channel(socket
,
5223 reg_chan
->consumer_key
);
5228 case LTTNG_BUFFER_PER_PID
:
5230 struct ust_app_session
*ua_sess
;
5231 struct lttng_ht_iter iter
;
5232 struct ust_app
*app
;
5234 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5236 ua_sess
= lookup_session_by_app(usess
, app
);
5237 if (ua_sess
== NULL
) {
5240 (void) ust_app_clear_quiescent_app_session(app
,
5252 health_code_update();
5257 * Destroy a specific UST session in apps.
5259 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5262 struct ust_app_session
*ua_sess
;
5263 struct lttng_ht_iter iter
;
5264 struct lttng_ht_node_u64
*node
;
5266 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5270 if (!app
->compatible
) {
5274 __lookup_session_by_app(usess
, app
, &iter
);
5275 node
= lttng_ht_iter_get_node_u64(&iter
);
5277 /* Session is being or is deleted. */
5280 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
5282 health_code_update();
5283 destroy_app_session(app
, ua_sess
);
5285 health_code_update();
5287 /* Quiescent wait after stopping trace */
5288 pthread_mutex_lock(&app
->sock_lock
);
5289 ret
= ustctl_wait_quiescent(app
->sock
);
5290 pthread_mutex_unlock(&app
->sock_lock
);
5291 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5292 ERR("UST app wait quiescent failed for app pid %d ret %d",
5297 health_code_update();
5302 * Start tracing for the UST session.
5304 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5306 struct lttng_ht_iter iter
;
5307 struct ust_app
*app
;
5309 DBG("Starting all UST traces");
5312 * Even though the start trace might fail, flag this session active so
5313 * other application coming in are started by default.
5320 * In a start-stop-start use-case, we need to clear the quiescent state
5321 * of each channel set by the prior stop command, thus ensuring that a
5322 * following stop or destroy is sure to grab a timestamp_end near those
5323 * operations, even if the packet is empty.
5325 (void) ust_app_clear_quiescent_session(usess
);
5327 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5328 ust_app_global_update(usess
, app
);
5337 * Start tracing for the UST session.
5338 * Called with UST session lock held.
5340 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5343 struct lttng_ht_iter iter
;
5344 struct ust_app
*app
;
5346 DBG("Stopping all UST traces");
5349 * Even though the stop trace might fail, flag this session inactive so
5350 * other application coming in are not started by default.
5356 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5357 ret
= ust_app_stop_trace(usess
, app
);
5359 /* Continue to next apps even on error */
5364 (void) ust_app_flush_session(usess
);
5372 * Destroy app UST session.
5374 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5377 struct lttng_ht_iter iter
;
5378 struct ust_app
*app
;
5380 DBG("Destroy all UST traces");
5384 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5385 ret
= destroy_trace(usess
, app
);
5387 /* Continue to next apps even on error */
5397 /* The ua_sess lock must be held by the caller. */
5399 int find_or_create_ust_app_channel(
5400 struct ltt_ust_session
*usess
,
5401 struct ust_app_session
*ua_sess
,
5402 struct ust_app
*app
,
5403 struct ltt_ust_channel
*uchan
,
5404 struct ust_app_channel
**ua_chan
)
5407 struct lttng_ht_iter iter
;
5408 struct lttng_ht_node_str
*ua_chan_node
;
5410 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5411 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5413 *ua_chan
= caa_container_of(ua_chan_node
,
5414 struct ust_app_channel
, node
);
5418 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5427 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5428 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5429 struct ust_app
*app
)
5432 struct ust_app_event
*ua_event
= NULL
;
5434 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5435 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5437 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5442 if (ua_event
->enabled
!= uevent
->enabled
) {
5443 ret
= uevent
->enabled
?
5444 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5445 disable_ust_app_event(ua_sess
, ua_event
, app
);
5454 void ust_app_synchronize_tokens(struct ust_app
*app
)
5457 enum lttng_error_code ret_code
;
5458 enum lttng_trigger_status t_status
;
5459 struct lttng_ht_iter app_trigger_iter
;
5460 struct lttng_triggers
*triggers
;
5461 struct ust_app_token_event_rule
*token_event_rule_element
;
5465 /* TODO: is this necessary to protect against new trigger being added ?
5466 * notification_trigger_tokens_ht is still the backing data structure
5467 * for this listing. Leave it there for now.
5469 pthread_mutex_lock(¬ification_trigger_tokens_ht_lock
);
5470 ret_code
= notification_thread_command_get_tokens(
5471 notification_thread_handle
, &triggers
);
5472 if (ret_code
!= LTTNG_OK
) {
5479 t_status
= lttng_triggers_get_count(triggers
, &count
);
5480 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5485 for (unsigned int i
= 0; i
< count
; i
++) {
5486 struct lttng_condition
*condition
;
5487 struct lttng_event_rule
*event_rule
;
5488 struct lttng_trigger
*trigger
;
5489 struct ust_app_token_event_rule
*ua_token
;
5492 trigger
= lttng_triggers_get_pointer_of_index(triggers
, i
);
5495 /* TODO: error checking and type checking */
5496 token
= lttng_trigger_get_key(trigger
);
5497 condition
= lttng_trigger_get_condition(trigger
);
5498 (void) lttng_condition_event_rule_get_rule_no_const(condition
, &event_rule
);
5500 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5501 /* Skip kernel related trigger */
5505 /* Iterate over all known token trigger */
5506 ua_token
= find_ust_app_token_event_rule(app
->tokens_ht
, token
);
5508 ret
= create_ust_app_token_event_rule(trigger
, app
);
5515 /* Remove all unknown trigger from the app
5516 * TODO find a way better way then this, do it on the unregister command
5517 * and be specific on the token to remove instead of going over all
5518 * trigger known to the app. This is sub optimal.
5520 cds_lfht_for_each_entry (app
->tokens_ht
->ht
, &app_trigger_iter
.iter
,
5521 token_event_rule_element
, node
.node
) {
5525 token
= token_event_rule_element
->token
;
5528 * Check if the app event trigger still exists on the
5529 * notification side.
5530 * TODO: might want to change the backing data struct of the
5531 * lttng_triggers object to allow quick lookup?
5532 * For kernel mostly all of this can be removed once we delete
5533 * on a per trigger basis.
5536 for (unsigned int i
= 0; i
< count
; i
++) {
5537 struct lttng_trigger
*trigger
;
5538 uint64_t inner_token
;
5540 trigger
= lttng_triggers_get_pointer_of_index(
5544 inner_token
= lttng_trigger_get_key(trigger
);
5546 if (inner_token
== token
) {
5557 /* TODO: This is fucking ugly API for fuck sake */
5558 assert(!lttng_ht_del(app
->tokens_ht
, &app_trigger_iter
));
5560 (void) disable_ust_object(app
, token_event_rule_element
->obj
);
5562 delete_ust_app_token_event_rule(app
->sock
, token_event_rule_element
, app
);
5565 lttng_triggers_destroy(triggers
);
5567 pthread_mutex_unlock(¬ification_trigger_tokens_ht_lock
);
5572 * The caller must ensure that the application is compatible and is tracked
5573 * by the process attribute trackers.
5576 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5577 struct ust_app
*app
)
5580 struct cds_lfht_iter uchan_iter
;
5581 struct ltt_ust_channel
*uchan
;
5582 struct ust_app_session
*ua_sess
= NULL
;
5585 * The application's configuration should only be synchronized for
5588 assert(usess
->active
);
5590 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5592 /* Tracer is probably gone or ENOMEM. */
5597 pthread_mutex_lock(&ua_sess
->lock
);
5598 if (ua_sess
->deleted
) {
5599 pthread_mutex_unlock(&ua_sess
->lock
);
5604 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5606 struct ust_app_channel
*ua_chan
;
5607 struct cds_lfht_iter uevent_iter
;
5608 struct ltt_ust_event
*uevent
;
5611 * Search for a matching ust_app_channel. If none is found,
5612 * create it. Creating the channel will cause the ua_chan
5613 * structure to be allocated, the channel buffers to be
5614 * allocated (if necessary) and sent to the application, and
5615 * all enabled contexts will be added to the channel.
5617 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5618 app
, uchan
, &ua_chan
);
5620 /* Tracer is probably gone or ENOMEM. */
5625 /* ua_chan will be NULL for the metadata channel */
5629 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5631 ret
= ust_app_channel_synchronize_event(ua_chan
,
5632 uevent
, ua_sess
, app
);
5638 if (ua_chan
->enabled
!= uchan
->enabled
) {
5639 ret
= uchan
->enabled
?
5640 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5641 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5650 pthread_mutex_unlock(&ua_sess
->lock
);
5651 /* Everything went well at this point. */
5656 pthread_mutex_unlock(&ua_sess
->lock
);
5659 destroy_app_session(app
, ua_sess
);
5665 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5667 struct ust_app_session
*ua_sess
;
5669 ua_sess
= lookup_session_by_app(usess
, app
);
5670 if (ua_sess
== NULL
) {
5673 destroy_app_session(app
, ua_sess
);
5677 * Add channels/events from UST global domain to registered apps at sock.
5679 * Called with session lock held.
5680 * Called with RCU read-side lock held.
5682 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5685 assert(usess
->active
);
5687 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5688 app
->sock
, usess
->id
);
5690 if (!app
->compatible
) {
5693 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5695 trace_ust_id_tracker_lookup(
5696 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5698 trace_ust_id_tracker_lookup(
5699 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5702 * Synchronize the application's internal tracing configuration
5703 * and start tracing.
5705 ust_app_synchronize(usess
, app
);
5706 ust_app_start_trace(usess
, app
);
5708 ust_app_global_destroy(usess
, app
);
5712 void ust_app_global_update_tokens(struct ust_app
*app
)
5714 DBG2("UST app global update token for app sock %d", app
->sock
);
5716 if (!app
->compatible
) {
5719 if (app
->token_communication
.handle
== NULL
) {
5720 WARN("UST app global update token for app sock %d skipped since communcation handle is null", app
->sock
);
5724 ust_app_synchronize_tokens(app
);
5728 * Called with session lock held.
5730 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5732 struct lttng_ht_iter iter
;
5733 struct ust_app
*app
;
5736 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5737 ust_app_global_update(usess
, app
);
5742 void ust_app_global_update_all_tokens(void)
5744 struct lttng_ht_iter iter
;
5745 struct ust_app
*app
;
5748 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5749 ust_app_global_update_tokens(app
);
5755 * Add context to a specific channel for global UST domain.
5757 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5758 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5761 struct lttng_ht_node_str
*ua_chan_node
;
5762 struct lttng_ht_iter iter
, uiter
;
5763 struct ust_app_channel
*ua_chan
= NULL
;
5764 struct ust_app_session
*ua_sess
;
5765 struct ust_app
*app
;
5767 assert(usess
->active
);
5770 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5771 if (!app
->compatible
) {
5773 * TODO: In time, we should notice the caller of this error by
5774 * telling him that this is a version error.
5778 ua_sess
= lookup_session_by_app(usess
, app
);
5779 if (ua_sess
== NULL
) {
5783 pthread_mutex_lock(&ua_sess
->lock
);
5785 if (ua_sess
->deleted
) {
5786 pthread_mutex_unlock(&ua_sess
->lock
);
5790 /* Lookup channel in the ust app session */
5791 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5792 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5793 if (ua_chan_node
== NULL
) {
5796 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5798 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5803 pthread_mutex_unlock(&ua_sess
->lock
);
5811 * Receive registration and populate the given msg structure.
5813 * On success return 0 else a negative value returned by the ustctl call.
5815 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5818 uint32_t pid
, ppid
, uid
, gid
;
5822 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5823 &pid
, &ppid
, &uid
, &gid
,
5824 &msg
->bits_per_long
,
5825 &msg
->uint8_t_alignment
,
5826 &msg
->uint16_t_alignment
,
5827 &msg
->uint32_t_alignment
,
5828 &msg
->uint64_t_alignment
,
5829 &msg
->long_alignment
,
5836 case LTTNG_UST_ERR_EXITING
:
5837 DBG3("UST app recv reg message failed. Application died");
5839 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5840 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5841 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5842 LTTNG_UST_ABI_MINOR_VERSION
);
5845 ERR("UST app recv reg message failed with ret %d", ret
);
5850 msg
->pid
= (pid_t
) pid
;
5851 msg
->ppid
= (pid_t
) ppid
;
5852 msg
->uid
= (uid_t
) uid
;
5853 msg
->gid
= (gid_t
) gid
;
5860 * Return a ust app session object using the application object and the
5861 * session object descriptor has a key. If not found, NULL is returned.
5862 * A RCU read side lock MUST be acquired when calling this function.
5864 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5867 struct lttng_ht_node_ulong
*node
;
5868 struct lttng_ht_iter iter
;
5869 struct ust_app_session
*ua_sess
= NULL
;
5873 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5874 node
= lttng_ht_iter_get_node_ulong(&iter
);
5876 DBG2("UST app session find by objd %d not found", objd
);
5880 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5887 * Return a ust app channel object using the application object and the channel
5888 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5889 * lock MUST be acquired before calling this function.
5891 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5894 struct lttng_ht_node_ulong
*node
;
5895 struct lttng_ht_iter iter
;
5896 struct ust_app_channel
*ua_chan
= NULL
;
5900 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5901 node
= lttng_ht_iter_get_node_ulong(&iter
);
5903 DBG2("UST app channel find by objd %d not found", objd
);
5907 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5914 * Reply to a register channel notification from an application on the notify
5915 * socket. The channel metadata is also created.
5917 * The session UST registry lock is acquired in this function.
5919 * On success 0 is returned else a negative value.
5921 static int reply_ust_register_channel(int sock
, int cobjd
,
5922 size_t nr_fields
, struct ustctl_field
*fields
)
5924 int ret
, ret_code
= 0;
5926 uint64_t chan_reg_key
;
5927 enum ustctl_channel_header type
;
5928 struct ust_app
*app
;
5929 struct ust_app_channel
*ua_chan
;
5930 struct ust_app_session
*ua_sess
;
5931 struct ust_registry_session
*registry
;
5932 struct ust_registry_channel
*chan_reg
;
5936 /* Lookup application. If not found, there is a code flow error. */
5937 app
= find_app_by_notify_sock(sock
);
5939 DBG("Application socket %d is being torn down. Abort event notify",
5942 goto error_rcu_unlock
;
5945 /* Lookup channel by UST object descriptor. */
5946 ua_chan
= find_channel_by_objd(app
, cobjd
);
5948 DBG("Application channel is being torn down. Abort event notify");
5950 goto error_rcu_unlock
;
5953 assert(ua_chan
->session
);
5954 ua_sess
= ua_chan
->session
;
5956 /* Get right session registry depending on the session buffer type. */
5957 registry
= get_session_registry(ua_sess
);
5959 DBG("Application session is being torn down. Abort event notify");
5961 goto error_rcu_unlock
;
5964 /* Depending on the buffer type, a different channel key is used. */
5965 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5966 chan_reg_key
= ua_chan
->tracing_channel_id
;
5968 chan_reg_key
= ua_chan
->key
;
5971 pthread_mutex_lock(®istry
->lock
);
5973 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5976 if (!chan_reg
->register_done
) {
5978 * TODO: eventually use the registry event count for
5979 * this channel to better guess header type for per-pid
5982 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5983 chan_reg
->nr_ctx_fields
= nr_fields
;
5984 chan_reg
->ctx_fields
= fields
;
5986 chan_reg
->header_type
= type
;
5988 /* Get current already assigned values. */
5989 type
= chan_reg
->header_type
;
5991 /* Channel id is set during the object creation. */
5992 chan_id
= chan_reg
->chan_id
;
5994 /* Append to metadata */
5995 if (!chan_reg
->metadata_dumped
) {
5996 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5998 ERR("Error appending channel metadata (errno = %d)", ret_code
);
6004 DBG3("UST app replying to register channel key %" PRIu64
6005 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
6008 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
6010 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6011 ERR("UST app reply channel failed with ret %d", ret
);
6013 DBG3("UST app reply channel failed. Application died");
6018 /* This channel registry registration is completed. */
6019 chan_reg
->register_done
= 1;
6022 pthread_mutex_unlock(®istry
->lock
);
6030 * Add event to the UST channel registry. When the event is added to the
6031 * registry, the metadata is also created. Once done, this replies to the
6032 * application with the appropriate error code.
6034 * The session UST registry lock is acquired in the function.
6036 * On success 0 is returned else a negative value.
6038 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
6039 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
6040 int loglevel_value
, char *model_emf_uri
)
6043 uint32_t event_id
= 0;
6044 uint64_t chan_reg_key
;
6045 struct ust_app
*app
;
6046 struct ust_app_channel
*ua_chan
;
6047 struct ust_app_session
*ua_sess
;
6048 struct ust_registry_session
*registry
;
6052 /* Lookup application. If not found, there is a code flow error. */
6053 app
= find_app_by_notify_sock(sock
);
6055 DBG("Application socket %d is being torn down. Abort event notify",
6058 goto error_rcu_unlock
;
6061 /* Lookup channel by UST object descriptor. */
6062 ua_chan
= find_channel_by_objd(app
, cobjd
);
6064 DBG("Application channel is being torn down. Abort event notify");
6066 goto error_rcu_unlock
;
6069 assert(ua_chan
->session
);
6070 ua_sess
= ua_chan
->session
;
6072 registry
= get_session_registry(ua_sess
);
6074 DBG("Application session is being torn down. Abort event notify");
6076 goto error_rcu_unlock
;
6079 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6080 chan_reg_key
= ua_chan
->tracing_channel_id
;
6082 chan_reg_key
= ua_chan
->key
;
6085 pthread_mutex_lock(®istry
->lock
);
6088 * From this point on, this call acquires the ownership of the sig, fields
6089 * and model_emf_uri meaning any free are done inside it if needed. These
6090 * three variables MUST NOT be read/write after this.
6092 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
6093 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
6094 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
6098 model_emf_uri
= NULL
;
6101 * The return value is returned to ustctl so in case of an error, the
6102 * application can be notified. In case of an error, it's important not to
6103 * return a negative error or else the application will get closed.
6105 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
6107 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6108 ERR("UST app reply event failed with ret %d", ret
);
6110 DBG3("UST app reply event failed. Application died");
6113 * No need to wipe the create event since the application socket will
6114 * get close on error hence cleaning up everything by itself.
6119 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6123 pthread_mutex_unlock(®istry
->lock
);
6128 free(model_emf_uri
);
6133 * Add enum to the UST session registry. Once done, this replies to the
6134 * application with the appropriate error code.
6136 * The session UST registry lock is acquired within this function.
6138 * On success 0 is returned else a negative value.
6140 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
6141 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
6143 int ret
= 0, ret_code
;
6144 struct ust_app
*app
;
6145 struct ust_app_session
*ua_sess
;
6146 struct ust_registry_session
*registry
;
6147 uint64_t enum_id
= -1ULL;
6151 /* Lookup application. If not found, there is a code flow error. */
6152 app
= find_app_by_notify_sock(sock
);
6154 /* Return an error since this is not an error */
6155 DBG("Application socket %d is being torn down. Aborting enum registration",
6158 goto error_rcu_unlock
;
6161 /* Lookup session by UST object descriptor. */
6162 ua_sess
= find_session_by_objd(app
, sobjd
);
6164 /* Return an error since this is not an error */
6165 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6167 goto error_rcu_unlock
;
6170 registry
= get_session_registry(ua_sess
);
6172 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6174 goto error_rcu_unlock
;
6177 pthread_mutex_lock(®istry
->lock
);
6180 * From this point on, the callee acquires the ownership of
6181 * entries. The variable entries MUST NOT be read/written after
6184 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
6185 entries
, nr_entries
, &enum_id
);
6189 * The return value is returned to ustctl so in case of an error, the
6190 * application can be notified. In case of an error, it's important not to
6191 * return a negative error or else the application will get closed.
6193 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
6195 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6196 ERR("UST app reply enum failed with ret %d", ret
);
6198 DBG3("UST app reply enum failed. Application died");
6201 * No need to wipe the create enum since the application socket will
6202 * get close on error hence cleaning up everything by itself.
6207 DBG3("UST registry enum %s added successfully or already found", name
);
6210 pthread_mutex_unlock(®istry
->lock
);
6217 * Handle application notification through the given notify socket.
6219 * Return 0 on success or else a negative value.
6221 int ust_app_recv_notify(int sock
)
6224 enum ustctl_notify_cmd cmd
;
6226 DBG3("UST app receiving notify from sock %d", sock
);
6228 ret
= ustctl_recv_notify(sock
, &cmd
);
6230 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6231 ERR("UST app recv notify failed with ret %d", ret
);
6233 DBG3("UST app recv notify failed. Application died");
6239 case USTCTL_NOTIFY_CMD_EVENT
:
6241 int sobjd
, cobjd
, loglevel_value
;
6242 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6244 struct ustctl_field
*fields
;
6246 DBG2("UST app ustctl register event received");
6248 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
6249 &loglevel_value
, &sig
, &nr_fields
, &fields
,
6252 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6253 ERR("UST app recv event failed with ret %d", ret
);
6255 DBG3("UST app recv event failed. Application died");
6261 * Add event to the UST registry coming from the notify socket. This
6262 * call will free if needed the sig, fields and model_emf_uri. This
6263 * code path loses the ownsership of these variables and transfer them
6264 * to the this function.
6266 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6267 fields
, loglevel_value
, model_emf_uri
);
6274 case USTCTL_NOTIFY_CMD_CHANNEL
:
6278 struct ustctl_field
*fields
;
6280 DBG2("UST app ustctl register channel received");
6282 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
6285 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6286 ERR("UST app recv channel failed with ret %d", ret
);
6288 DBG3("UST app recv channel failed. Application died");
6294 * The fields ownership are transfered to this function call meaning
6295 * that if needed it will be freed. After this, it's invalid to access
6296 * fields or clean it up.
6298 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
6306 case USTCTL_NOTIFY_CMD_ENUM
:
6309 char name
[LTTNG_UST_SYM_NAME_LEN
];
6311 struct ustctl_enum_entry
*entries
;
6313 DBG2("UST app ustctl register enum received");
6315 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
6316 &entries
, &nr_entries
);
6318 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6319 ERR("UST app recv enum failed with ret %d", ret
);
6321 DBG3("UST app recv enum failed. Application died");
6326 /* Callee assumes ownership of entries */
6327 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6328 entries
, nr_entries
);
6336 /* Should NEVER happen. */
6345 * Once the notify socket hangs up, this is called. First, it tries to find the
6346 * corresponding application. On failure, the call_rcu to close the socket is
6347 * executed. If an application is found, it tries to delete it from the notify
6348 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6350 * Note that an object needs to be allocated here so on ENOMEM failure, the
6351 * call RCU is not done but the rest of the cleanup is.
6353 void ust_app_notify_sock_unregister(int sock
)
6356 struct lttng_ht_iter iter
;
6357 struct ust_app
*app
;
6358 struct ust_app_notify_sock_obj
*obj
;
6364 obj
= zmalloc(sizeof(*obj
));
6367 * An ENOMEM is kind of uncool. If this strikes we continue the
6368 * procedure but the call_rcu will not be called. In this case, we
6369 * accept the fd leak rather than possibly creating an unsynchronized
6370 * state between threads.
6372 * TODO: The notify object should be created once the notify socket is
6373 * registered and stored independantely from the ust app object. The
6374 * tricky part is to synchronize the teardown of the application and
6375 * this notify object. Let's keep that in mind so we can avoid this
6376 * kind of shenanigans with ENOMEM in the teardown path.
6383 DBG("UST app notify socket unregister %d", sock
);
6386 * Lookup application by notify socket. If this fails, this means that the
6387 * hash table delete has already been done by the application
6388 * unregistration process so we can safely close the notify socket in a
6391 app
= find_app_by_notify_sock(sock
);
6396 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6399 * Whatever happens here either we fail or succeed, in both cases we have
6400 * to close the socket after a grace period to continue to the call RCU
6401 * here. If the deletion is successful, the application is not visible
6402 * anymore by other threads and is it fails it means that it was already
6403 * deleted from the hash table so either way we just have to close the
6406 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6412 * Close socket after a grace period to avoid for the socket to be reused
6413 * before the application object is freed creating potential race between
6414 * threads trying to add unique in the global hash table.
6417 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6422 * Destroy a ust app data structure and free its memory.
6424 void ust_app_destroy(struct ust_app
*app
)
6430 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6434 * Take a snapshot for a given UST session. The snapshot is sent to the given
6437 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6439 enum lttng_error_code
ust_app_snapshot_record(
6440 const struct ltt_ust_session
*usess
,
6441 const struct consumer_output
*output
, int wait
,
6442 uint64_t nb_packets_per_stream
)
6445 enum lttng_error_code status
= LTTNG_OK
;
6446 struct lttng_ht_iter iter
;
6447 struct ust_app
*app
;
6448 char *trace_path
= NULL
;
6455 switch (usess
->buffer_type
) {
6456 case LTTNG_BUFFER_PER_UID
:
6458 struct buffer_reg_uid
*reg
;
6460 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6461 struct buffer_reg_channel
*reg_chan
;
6462 struct consumer_socket
*socket
;
6463 char pathname
[PATH_MAX
];
6464 size_t consumer_path_offset
= 0;
6466 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6467 /* Skip since no metadata is present */
6471 /* Get consumer socket to use to push the metadata.*/
6472 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6475 status
= LTTNG_ERR_INVALID
;
6479 memset(pathname
, 0, sizeof(pathname
));
6480 ret
= snprintf(pathname
, sizeof(pathname
),
6481 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
6482 reg
->uid
, reg
->bits_per_long
);
6484 PERROR("snprintf snapshot path");
6485 status
= LTTNG_ERR_INVALID
;
6488 /* Free path allowed on previous iteration. */
6490 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6491 &consumer_path_offset
);
6493 status
= LTTNG_ERR_INVALID
;
6496 /* Add the UST default trace dir to path. */
6497 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6498 reg_chan
, node
.node
) {
6499 status
= consumer_snapshot_channel(socket
,
6500 reg_chan
->consumer_key
,
6501 output
, 0, usess
->uid
,
6502 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
6503 nb_packets_per_stream
);
6504 if (status
!= LTTNG_OK
) {
6508 status
= consumer_snapshot_channel(socket
,
6509 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6510 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6512 if (status
!= LTTNG_OK
) {
6518 case LTTNG_BUFFER_PER_PID
:
6520 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6521 struct consumer_socket
*socket
;
6522 struct lttng_ht_iter chan_iter
;
6523 struct ust_app_channel
*ua_chan
;
6524 struct ust_app_session
*ua_sess
;
6525 struct ust_registry_session
*registry
;
6526 char pathname
[PATH_MAX
];
6527 size_t consumer_path_offset
= 0;
6529 ua_sess
= lookup_session_by_app(usess
, app
);
6531 /* Session not associated with this app. */
6535 /* Get the right consumer socket for the application. */
6536 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6539 status
= LTTNG_ERR_INVALID
;
6543 /* Add the UST default trace dir to path. */
6544 memset(pathname
, 0, sizeof(pathname
));
6545 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
6548 status
= LTTNG_ERR_INVALID
;
6549 PERROR("snprintf snapshot path");
6552 /* Free path allowed on previous iteration. */
6554 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6555 &consumer_path_offset
);
6557 status
= LTTNG_ERR_INVALID
;
6560 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6561 ua_chan
, node
.node
) {
6562 status
= consumer_snapshot_channel(socket
,
6563 ua_chan
->key
, output
, 0,
6564 ua_sess
->effective_credentials
6566 ua_sess
->effective_credentials
6568 &trace_path
[consumer_path_offset
], wait
,
6569 nb_packets_per_stream
);
6573 case LTTNG_ERR_CHAN_NOT_FOUND
:
6580 registry
= get_session_registry(ua_sess
);
6582 DBG("Application session is being torn down. Skip application.");
6585 status
= consumer_snapshot_channel(socket
,
6586 registry
->metadata_key
, output
, 1,
6587 ua_sess
->effective_credentials
.uid
,
6588 ua_sess
->effective_credentials
.gid
,
6589 &trace_path
[consumer_path_offset
], wait
, 0);
6593 case LTTNG_ERR_CHAN_NOT_FOUND
:
6613 * Return the size taken by one more packet per stream.
6615 uint64_t ust_app_get_size_one_more_packet_per_stream(
6616 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6618 uint64_t tot_size
= 0;
6619 struct ust_app
*app
;
6620 struct lttng_ht_iter iter
;
6624 switch (usess
->buffer_type
) {
6625 case LTTNG_BUFFER_PER_UID
:
6627 struct buffer_reg_uid
*reg
;
6629 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6630 struct buffer_reg_channel
*reg_chan
;
6633 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6634 reg_chan
, node
.node
) {
6635 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6637 * Don't take channel into account if we
6638 * already grab all its packets.
6642 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6648 case LTTNG_BUFFER_PER_PID
:
6651 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6652 struct ust_app_channel
*ua_chan
;
6653 struct ust_app_session
*ua_sess
;
6654 struct lttng_ht_iter chan_iter
;
6656 ua_sess
= lookup_session_by_app(usess
, app
);
6658 /* Session not associated with this app. */
6662 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6663 ua_chan
, node
.node
) {
6664 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6666 * Don't take channel into account if we
6667 * already grab all its packets.
6671 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6685 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6686 struct cds_list_head
*buffer_reg_uid_list
,
6687 struct consumer_output
*consumer
, uint64_t uchan_id
,
6688 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6691 uint64_t consumer_chan_key
;
6696 ret
= buffer_reg_uid_consumer_channel_key(
6697 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6705 ret
= consumer_get_lost_packets(ust_session_id
,
6706 consumer_chan_key
, consumer
, lost
);
6708 ret
= consumer_get_discarded_events(ust_session_id
,
6709 consumer_chan_key
, consumer
, discarded
);
6716 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6717 struct ltt_ust_channel
*uchan
,
6718 struct consumer_output
*consumer
, int overwrite
,
6719 uint64_t *discarded
, uint64_t *lost
)
6722 struct lttng_ht_iter iter
;
6723 struct lttng_ht_node_str
*ua_chan_node
;
6724 struct ust_app
*app
;
6725 struct ust_app_session
*ua_sess
;
6726 struct ust_app_channel
*ua_chan
;
6733 * Iterate over every registered applications. Sum counters for
6734 * all applications containing requested session and channel.
6736 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6737 struct lttng_ht_iter uiter
;
6739 ua_sess
= lookup_session_by_app(usess
, app
);
6740 if (ua_sess
== NULL
) {
6745 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6746 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6747 /* If the session is found for the app, the channel must be there */
6748 assert(ua_chan_node
);
6750 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6755 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6762 uint64_t _discarded
;
6764 ret
= consumer_get_discarded_events(usess
->id
,
6765 ua_chan
->key
, consumer
, &_discarded
);
6769 (*discarded
) += _discarded
;
6778 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6779 struct ust_app
*app
)
6782 struct ust_app_session
*ua_sess
;
6784 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6788 ua_sess
= lookup_session_by_app(usess
, app
);
6789 if (ua_sess
== NULL
) {
6790 /* The session is in teardown process. Ignore and continue. */
6794 pthread_mutex_lock(&ua_sess
->lock
);
6796 if (ua_sess
->deleted
) {
6800 pthread_mutex_lock(&app
->sock_lock
);
6801 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6802 pthread_mutex_unlock(&app
->sock_lock
);
6805 pthread_mutex_unlock(&ua_sess
->lock
);
6809 health_code_update();
6814 * Regenerate the statedump for each app in the session.
6816 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6819 struct lttng_ht_iter iter
;
6820 struct ust_app
*app
;
6822 DBG("Regenerating the metadata for all UST apps");
6826 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6827 if (!app
->compatible
) {
6831 ret
= ust_app_regenerate_statedump(usess
, app
);
6833 /* Continue to the next app even on error */
6844 * Rotate all the channels of a session.
6846 * Return LTTNG_OK on success or else an LTTng error code.
6848 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
6851 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6852 struct lttng_ht_iter iter
;
6853 struct ust_app
*app
;
6854 struct ltt_ust_session
*usess
= session
->ust_session
;
6860 switch (usess
->buffer_type
) {
6861 case LTTNG_BUFFER_PER_UID
:
6863 struct buffer_reg_uid
*reg
;
6865 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6866 struct buffer_reg_channel
*reg_chan
;
6867 struct consumer_socket
*socket
;
6869 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6870 /* Skip since no metadata is present */
6874 /* Get consumer socket to use to push the metadata.*/
6875 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6878 cmd_ret
= LTTNG_ERR_INVALID
;
6882 /* Rotate the data channels. */
6883 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6884 reg_chan
, node
.node
) {
6885 ret
= consumer_rotate_channel(socket
,
6886 reg_chan
->consumer_key
,
6887 usess
->uid
, usess
->gid
,
6889 /* is_metadata_channel */ false);
6891 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6896 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6898 ret
= consumer_rotate_channel(socket
,
6899 reg
->registry
->reg
.ust
->metadata_key
,
6900 usess
->uid
, usess
->gid
,
6902 /* is_metadata_channel */ true);
6904 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6910 case LTTNG_BUFFER_PER_PID
:
6912 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6913 struct consumer_socket
*socket
;
6914 struct lttng_ht_iter chan_iter
;
6915 struct ust_app_channel
*ua_chan
;
6916 struct ust_app_session
*ua_sess
;
6917 struct ust_registry_session
*registry
;
6919 ua_sess
= lookup_session_by_app(usess
, app
);
6921 /* Session not associated with this app. */
6925 /* Get the right consumer socket for the application. */
6926 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6929 cmd_ret
= LTTNG_ERR_INVALID
;
6933 registry
= get_session_registry(ua_sess
);
6935 DBG("Application session is being torn down. Skip application.");
6939 /* Rotate the data channels. */
6940 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6941 ua_chan
, node
.node
) {
6942 ret
= consumer_rotate_channel(socket
,
6944 ua_sess
->effective_credentials
6946 ua_sess
->effective_credentials
6949 /* is_metadata_channel */ false);
6951 /* Per-PID buffer and application going away. */
6952 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6954 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6959 /* Rotate the metadata channel. */
6960 (void) push_metadata(registry
, usess
->consumer
);
6961 ret
= consumer_rotate_channel(socket
,
6962 registry
->metadata_key
,
6963 ua_sess
->effective_credentials
.uid
,
6964 ua_sess
->effective_credentials
.gid
,
6966 /* is_metadata_channel */ true);
6968 /* Per-PID buffer and application going away. */
6969 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6971 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6989 enum lttng_error_code
ust_app_create_channel_subdirectories(
6990 const struct ltt_ust_session
*usess
)
6992 enum lttng_error_code ret
= LTTNG_OK
;
6993 struct lttng_ht_iter iter
;
6994 enum lttng_trace_chunk_status chunk_status
;
6995 char *pathname_index
;
6998 assert(usess
->current_trace_chunk
);
7001 switch (usess
->buffer_type
) {
7002 case LTTNG_BUFFER_PER_UID
:
7004 struct buffer_reg_uid
*reg
;
7006 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7007 fmt_ret
= asprintf(&pathname_index
,
7008 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7009 reg
->uid
, reg
->bits_per_long
);
7011 ERR("Failed to format channel index directory");
7012 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7017 * Create the index subdirectory which will take care
7018 * of implicitly creating the channel's path.
7020 chunk_status
= lttng_trace_chunk_create_subdirectory(
7021 usess
->current_trace_chunk
,
7023 free(pathname_index
);
7024 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7025 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7031 case LTTNG_BUFFER_PER_PID
:
7033 struct ust_app
*app
;
7036 * Create the toplevel ust/ directory in case no apps are running.
7038 chunk_status
= lttng_trace_chunk_create_subdirectory(
7039 usess
->current_trace_chunk
,
7040 DEFAULT_UST_TRACE_DIR
);
7041 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7042 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7046 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7048 struct ust_app_session
*ua_sess
;
7049 struct ust_registry_session
*registry
;
7051 ua_sess
= lookup_session_by_app(usess
, app
);
7053 /* Session not associated with this app. */
7057 registry
= get_session_registry(ua_sess
);
7059 DBG("Application session is being torn down. Skip application.");
7063 fmt_ret
= asprintf(&pathname_index
,
7064 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7067 ERR("Failed to format channel index directory");
7068 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7072 * Create the index subdirectory which will take care
7073 * of implicitly creating the channel's path.
7075 chunk_status
= lttng_trace_chunk_create_subdirectory(
7076 usess
->current_trace_chunk
,
7078 free(pathname_index
);
7079 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7080 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7097 * Clear all the channels of a session.
7099 * Return LTTNG_OK on success or else an LTTng error code.
7101 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7104 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7105 struct lttng_ht_iter iter
;
7106 struct ust_app
*app
;
7107 struct ltt_ust_session
*usess
= session
->ust_session
;
7113 if (usess
->active
) {
7114 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7115 cmd_ret
= LTTNG_ERR_FATAL
;
7119 switch (usess
->buffer_type
) {
7120 case LTTNG_BUFFER_PER_UID
:
7122 struct buffer_reg_uid
*reg
;
7124 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7125 struct buffer_reg_channel
*reg_chan
;
7126 struct consumer_socket
*socket
;
7128 /* Get consumer socket to use to push the metadata.*/
7129 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7132 cmd_ret
= LTTNG_ERR_INVALID
;
7136 /* Clear the data channels. */
7137 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7138 reg_chan
, node
.node
) {
7139 ret
= consumer_clear_channel(socket
,
7140 reg_chan
->consumer_key
);
7146 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7149 * Clear the metadata channel.
7150 * Metadata channel is not cleared per se but we still need to
7151 * perform a rotation operation on it behind the scene.
7153 ret
= consumer_clear_channel(socket
,
7154 reg
->registry
->reg
.ust
->metadata_key
);
7161 case LTTNG_BUFFER_PER_PID
:
7163 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7164 struct consumer_socket
*socket
;
7165 struct lttng_ht_iter chan_iter
;
7166 struct ust_app_channel
*ua_chan
;
7167 struct ust_app_session
*ua_sess
;
7168 struct ust_registry_session
*registry
;
7170 ua_sess
= lookup_session_by_app(usess
, app
);
7172 /* Session not associated with this app. */
7176 /* Get the right consumer socket for the application. */
7177 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7180 cmd_ret
= LTTNG_ERR_INVALID
;
7184 registry
= get_session_registry(ua_sess
);
7186 DBG("Application session is being torn down. Skip application.");
7190 /* Clear the data channels. */
7191 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7192 ua_chan
, node
.node
) {
7193 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7195 /* Per-PID buffer and application going away. */
7196 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7203 (void) push_metadata(registry
, usess
->consumer
);
7206 * Clear the metadata channel.
7207 * Metadata channel is not cleared per se but we still need to
7208 * perform rotation operation on it behind the scene.
7210 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
7212 /* Per-PID buffer and application going away. */
7213 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7231 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7232 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7235 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;