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/common.h>
23 #include <common/hashtable/utils.h>
24 #include <lttng/event-rule/event-rule.h>
25 #include <lttng/event-rule/event-rule-internal.h>
26 #include <lttng/event-rule/tracepoint.h>
27 #include <lttng/condition/condition.h>
28 #include <lttng/condition/event-rule-internal.h>
29 #include <lttng/condition/event-rule.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
32 #include "buffer-registry.h"
34 #include "health-sessiond.h"
36 #include "ust-consumer.h"
37 #include "lttng-ust-ctl.h"
38 #include "lttng-ust-error.h"
41 #include "lttng-sessiond.h"
42 #include "notification-thread-commands.h"
46 struct lttng_ht
*ust_app_ht
;
47 struct lttng_ht
*ust_app_ht_by_sock
;
48 struct lttng_ht
*ust_app_ht_by_notify_sock
;
51 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
53 /* Next available channel key. Access under next_channel_key_lock. */
54 static uint64_t _next_channel_key
;
55 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
57 /* Next available session ID. Access under next_session_id_lock. */
58 static uint64_t _next_session_id
;
59 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
62 * Return the incremented value of next_channel_key.
64 static uint64_t get_next_channel_key(void)
68 pthread_mutex_lock(&next_channel_key_lock
);
69 ret
= ++_next_channel_key
;
70 pthread_mutex_unlock(&next_channel_key_lock
);
75 * Return the atomically incremented value of next_session_id.
77 static uint64_t get_next_session_id(void)
81 pthread_mutex_lock(&next_session_id_lock
);
82 ret
= ++_next_session_id
;
83 pthread_mutex_unlock(&next_session_id_lock
);
87 static void copy_channel_attr_to_ustctl(
88 struct ustctl_consumer_channel_attr
*attr
,
89 struct lttng_ust_channel_attr
*uattr
)
91 /* Copy event attributes since the layout is different. */
92 attr
->subbuf_size
= uattr
->subbuf_size
;
93 attr
->num_subbuf
= uattr
->num_subbuf
;
94 attr
->overwrite
= uattr
->overwrite
;
95 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
96 attr
->read_timer_interval
= uattr
->read_timer_interval
;
97 attr
->output
= uattr
->output
;
98 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
102 * Match function for the hash table lookup.
104 * It matches an ust app event based on three attributes which are the event
105 * name, the filter bytecode and the loglevel.
107 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
109 struct ust_app_event
*event
;
110 const struct ust_app_ht_key
*key
;
111 int ev_loglevel_value
;
116 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
118 ev_loglevel_value
= event
->attr
.loglevel
;
120 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
123 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
127 /* Event loglevel. */
128 if (ev_loglevel_value
!= key
->loglevel_type
) {
129 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
130 && key
->loglevel_type
== 0 &&
131 ev_loglevel_value
== -1) {
133 * Match is accepted. This is because on event creation, the
134 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
135 * -1 are accepted for this loglevel type since 0 is the one set by
136 * the API when receiving an enable event.
143 /* One of the filters is NULL, fail. */
144 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
148 if (key
->filter
&& event
->filter
) {
149 /* Both filters exists, check length followed by the bytecode. */
150 if (event
->filter
->len
!= key
->filter
->len
||
151 memcmp(event
->filter
->data
, key
->filter
->data
,
152 event
->filter
->len
) != 0) {
157 /* One of the exclusions is NULL, fail. */
158 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
162 if (key
->exclusion
&& event
->exclusion
) {
163 /* Both exclusions exists, check count followed by the names. */
164 if (event
->exclusion
->count
!= key
->exclusion
->count
||
165 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
166 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
180 * Unique add of an ust app event in the given ht. This uses the custom
181 * ht_match_ust_app_event match function and the event name as hash.
183 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
184 struct ust_app_event
*event
)
186 struct cds_lfht_node
*node_ptr
;
187 struct ust_app_ht_key key
;
191 assert(ua_chan
->events
);
194 ht
= ua_chan
->events
;
195 key
.name
= event
->attr
.name
;
196 key
.filter
= event
->filter
;
197 key
.loglevel_type
= event
->attr
.loglevel
;
198 key
.exclusion
= event
->exclusion
;
200 node_ptr
= cds_lfht_add_unique(ht
->ht
,
201 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
202 ht_match_ust_app_event
, &key
, &event
->node
.node
);
203 assert(node_ptr
== &event
->node
.node
);
207 * Close the notify socket from the given RCU head object. This MUST be called
208 * through a call_rcu().
210 static void close_notify_sock_rcu(struct rcu_head
*head
)
213 struct ust_app_notify_sock_obj
*obj
=
214 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
216 /* Must have a valid fd here. */
217 assert(obj
->fd
>= 0);
219 ret
= close(obj
->fd
);
221 ERR("close notify sock %d RCU", obj
->fd
);
223 lttng_fd_put(LTTNG_FD_APPS
, 1);
229 * Return the session registry according to the buffer type of the given
232 * A registry per UID object MUST exists before calling this function or else
233 * it assert() if not found. RCU read side lock must be acquired.
235 static struct ust_registry_session
*get_session_registry(
236 struct ust_app_session
*ua_sess
)
238 struct ust_registry_session
*registry
= NULL
;
242 switch (ua_sess
->buffer_type
) {
243 case LTTNG_BUFFER_PER_PID
:
245 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
249 registry
= reg_pid
->registry
->reg
.ust
;
252 case LTTNG_BUFFER_PER_UID
:
254 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
255 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
256 ua_sess
->real_credentials
.uid
);
260 registry
= reg_uid
->registry
->reg
.ust
;
272 * Delete ust context safely. RCU read lock must be held before calling
276 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
284 pthread_mutex_lock(&app
->sock_lock
);
285 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
286 pthread_mutex_unlock(&app
->sock_lock
);
287 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
288 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
289 sock
, ua_ctx
->obj
->handle
, ret
);
297 * Delete ust app event safely. RCU read lock must be held before calling
301 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
308 free(ua_event
->filter
);
309 if (ua_event
->exclusion
!= NULL
)
310 free(ua_event
->exclusion
);
311 if (ua_event
->obj
!= NULL
) {
312 pthread_mutex_lock(&app
->sock_lock
);
313 ret
= ustctl_release_object(sock
, ua_event
->obj
);
314 pthread_mutex_unlock(&app
->sock_lock
);
315 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
316 ERR("UST app sock %d release event obj failed with ret %d",
325 * Delete ust app token event_rule safely. RCU read lock must be held before calling
326 * this function. TODO: or does it????
329 void delete_ust_app_token_event_rule(int sock
, struct ust_app_token_event_rule
*ua_token
,
336 if (ua_token
->exclusion
!= NULL
)
337 free(ua_token
->exclusion
);
338 if (ua_token
->obj
!= NULL
) {
339 pthread_mutex_lock(&app
->sock_lock
);
340 ret
= ustctl_release_object(sock
, ua_token
->obj
);
341 pthread_mutex_unlock(&app
->sock_lock
);
342 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
343 ERR("UST app sock %d release event obj failed with ret %d",
348 lttng_event_rule_put(ua_token
->event_rule
);
353 * Release ust data object of the given stream.
355 * Return 0 on success or else a negative value.
357 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
365 pthread_mutex_lock(&app
->sock_lock
);
366 ret
= ustctl_release_object(sock
, stream
->obj
);
367 pthread_mutex_unlock(&app
->sock_lock
);
368 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
369 ERR("UST app sock %d release stream obj failed with ret %d",
372 lttng_fd_put(LTTNG_FD_APPS
, 2);
380 * Delete ust app stream safely. RCU read lock must be held before calling
384 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
389 (void) release_ust_app_stream(sock
, stream
, app
);
394 * We need to execute ht_destroy outside of RCU read-side critical
395 * section and outside of call_rcu thread, so we postpone its execution
396 * using ht_cleanup_push. It is simpler than to change the semantic of
397 * the many callers of delete_ust_app_session().
400 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
402 struct ust_app_channel
*ua_chan
=
403 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
405 ht_cleanup_push(ua_chan
->ctx
);
406 ht_cleanup_push(ua_chan
->events
);
411 * Extract the lost packet or discarded events counter when the channel is
412 * being deleted and store the value in the parent channel so we can
413 * access it from lttng list and at stop/destroy.
415 * The session list lock must be held by the caller.
418 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
420 uint64_t discarded
= 0, lost
= 0;
421 struct ltt_session
*session
;
422 struct ltt_ust_channel
*uchan
;
424 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
429 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
430 if (!session
|| !session
->ust_session
) {
432 * Not finding the session is not an error because there are
433 * multiple ways the channels can be torn down.
435 * 1) The session daemon can initiate the destruction of the
436 * ust app session after receiving a destroy command or
437 * during its shutdown/teardown.
438 * 2) The application, since we are in per-pid tracing, is
439 * unregistering and tearing down its ust app session.
441 * Both paths are protected by the session list lock which
442 * ensures that the accounting of lost packets and discarded
443 * events is done exactly once. The session is then unpublished
444 * from the session list, resulting in this condition.
449 if (ua_chan
->attr
.overwrite
) {
450 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
451 ua_chan
->key
, session
->ust_session
->consumer
,
454 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
455 ua_chan
->key
, session
->ust_session
->consumer
,
458 uchan
= trace_ust_find_channel_by_name(
459 session
->ust_session
->domain_global
.channels
,
462 ERR("Missing UST channel to store discarded counters");
466 uchan
->per_pid_closed_app_discarded
+= discarded
;
467 uchan
->per_pid_closed_app_lost
+= lost
;
472 session_put(session
);
477 * Delete ust app channel safely. RCU read lock must be held before calling
480 * The session list lock must be held by the caller.
483 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
487 struct lttng_ht_iter iter
;
488 struct ust_app_event
*ua_event
;
489 struct ust_app_ctx
*ua_ctx
;
490 struct ust_app_stream
*stream
, *stmp
;
491 struct ust_registry_session
*registry
;
495 DBG3("UST app deleting channel %s", ua_chan
->name
);
498 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
499 cds_list_del(&stream
->list
);
500 delete_ust_app_stream(sock
, stream
, app
);
504 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
505 cds_list_del(&ua_ctx
->list
);
506 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
508 delete_ust_app_ctx(sock
, ua_ctx
, app
);
512 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
514 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
516 delete_ust_app_event(sock
, ua_event
, app
);
519 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
520 /* Wipe and free registry from session registry. */
521 registry
= get_session_registry(ua_chan
->session
);
523 ust_registry_channel_del_free(registry
, ua_chan
->key
,
527 * A negative socket can be used by the caller when
528 * cleaning-up a ua_chan in an error path. Skip the
529 * accounting in this case.
532 save_per_pid_lost_discarded_counters(ua_chan
);
536 if (ua_chan
->obj
!= NULL
) {
537 /* Remove channel from application UST object descriptor. */
538 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
539 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
541 pthread_mutex_lock(&app
->sock_lock
);
542 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
543 pthread_mutex_unlock(&app
->sock_lock
);
544 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
545 ERR("UST app sock %d release channel obj failed with ret %d",
548 lttng_fd_put(LTTNG_FD_APPS
, 1);
551 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
554 int ust_app_register_done(struct ust_app
*app
)
558 pthread_mutex_lock(&app
->sock_lock
);
559 ret
= ustctl_register_done(app
->sock
);
560 pthread_mutex_unlock(&app
->sock_lock
);
564 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
569 pthread_mutex_lock(&app
->sock_lock
);
574 ret
= ustctl_release_object(sock
, data
);
576 pthread_mutex_unlock(&app
->sock_lock
);
582 * Push metadata to consumer socket.
584 * RCU read-side lock must be held to guarantee existance of socket.
585 * Must be called with the ust app session lock held.
586 * Must be called with the registry lock held.
588 * On success, return the len of metadata pushed or else a negative value.
589 * Returning a -EPIPE return value means we could not send the metadata,
590 * but it can be caused by recoverable errors (e.g. the application has
591 * terminated concurrently).
593 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
594 struct consumer_socket
*socket
, int send_zero_data
)
597 char *metadata_str
= NULL
;
598 size_t len
, offset
, new_metadata_len_sent
;
600 uint64_t metadata_key
, metadata_version
;
605 metadata_key
= registry
->metadata_key
;
608 * Means that no metadata was assigned to the session. This can
609 * happens if no start has been done previously.
615 offset
= registry
->metadata_len_sent
;
616 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
617 new_metadata_len_sent
= registry
->metadata_len
;
618 metadata_version
= registry
->metadata_version
;
620 DBG3("No metadata to push for metadata key %" PRIu64
,
621 registry
->metadata_key
);
623 if (send_zero_data
) {
624 DBG("No metadata to push");
630 /* Allocate only what we have to send. */
631 metadata_str
= zmalloc(len
);
633 PERROR("zmalloc ust app metadata string");
637 /* Copy what we haven't sent out. */
638 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
641 pthread_mutex_unlock(®istry
->lock
);
643 * We need to unlock the registry while we push metadata to
644 * break a circular dependency between the consumerd metadata
645 * lock and the sessiond registry lock. Indeed, pushing metadata
646 * to the consumerd awaits that it gets pushed all the way to
647 * relayd, but doing so requires grabbing the metadata lock. If
648 * a concurrent metadata request is being performed by
649 * consumerd, this can try to grab the registry lock on the
650 * sessiond while holding the metadata lock on the consumer
651 * daemon. Those push and pull schemes are performed on two
652 * different bidirectionnal communication sockets.
654 ret
= consumer_push_metadata(socket
, metadata_key
,
655 metadata_str
, len
, offset
, metadata_version
);
656 pthread_mutex_lock(®istry
->lock
);
659 * There is an acceptable race here between the registry
660 * metadata key assignment and the creation on the
661 * consumer. The session daemon can concurrently push
662 * metadata for this registry while being created on the
663 * consumer since the metadata key of the registry is
664 * assigned *before* it is setup to avoid the consumer
665 * to ask for metadata that could possibly be not found
666 * in the session daemon.
668 * The metadata will get pushed either by the session
669 * being stopped or the consumer requesting metadata if
670 * that race is triggered.
672 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
675 ERR("Error pushing metadata to consumer");
681 * Metadata may have been concurrently pushed, since
682 * we're not holding the registry lock while pushing to
683 * consumer. This is handled by the fact that we send
684 * the metadata content, size, and the offset at which
685 * that metadata belongs. This may arrive out of order
686 * on the consumer side, and the consumer is able to
687 * deal with overlapping fragments. The consumer
688 * supports overlapping fragments, which must be
689 * contiguous starting from offset 0. We keep the
690 * largest metadata_len_sent value of the concurrent
693 registry
->metadata_len_sent
=
694 max_t(size_t, registry
->metadata_len_sent
,
695 new_metadata_len_sent
);
704 * On error, flag the registry that the metadata is
705 * closed. We were unable to push anything and this
706 * means that either the consumer is not responding or
707 * the metadata cache has been destroyed on the
710 registry
->metadata_closed
= 1;
718 * For a given application and session, push metadata to consumer.
719 * Either sock or consumer is required : if sock is NULL, the default
720 * socket to send the metadata is retrieved from consumer, if sock
721 * is not NULL we use it to send the metadata.
722 * RCU read-side lock must be held while calling this function,
723 * therefore ensuring existance of registry. It also ensures existance
724 * of socket throughout this function.
726 * Return 0 on success else a negative error.
727 * Returning a -EPIPE return value means we could not send the metadata,
728 * but it can be caused by recoverable errors (e.g. the application has
729 * terminated concurrently).
731 static int push_metadata(struct ust_registry_session
*registry
,
732 struct consumer_output
*consumer
)
736 struct consumer_socket
*socket
;
741 pthread_mutex_lock(®istry
->lock
);
742 if (registry
->metadata_closed
) {
747 /* Get consumer socket to use to push the metadata.*/
748 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
755 ret
= ust_app_push_metadata(registry
, socket
, 0);
760 pthread_mutex_unlock(®istry
->lock
);
764 pthread_mutex_unlock(®istry
->lock
);
769 * Send to the consumer a close metadata command for the given session. Once
770 * done, the metadata channel is deleted and the session metadata pointer is
771 * nullified. The session lock MUST be held unless the application is
772 * in the destroy path.
774 * Do not hold the registry lock while communicating with the consumerd, because
775 * doing so causes inter-process deadlocks between consumerd and sessiond with
776 * the metadata request notification.
778 * Return 0 on success else a negative value.
780 static int close_metadata(struct ust_registry_session
*registry
,
781 struct consumer_output
*consumer
)
784 struct consumer_socket
*socket
;
785 uint64_t metadata_key
;
786 bool registry_was_already_closed
;
793 pthread_mutex_lock(®istry
->lock
);
794 metadata_key
= registry
->metadata_key
;
795 registry_was_already_closed
= registry
->metadata_closed
;
796 if (metadata_key
!= 0) {
798 * Metadata closed. Even on error this means that the consumer
799 * is not responding or not found so either way a second close
800 * should NOT be emit for this registry.
802 registry
->metadata_closed
= 1;
804 pthread_mutex_unlock(®istry
->lock
);
806 if (metadata_key
== 0 || registry_was_already_closed
) {
811 /* Get consumer socket to use to push the metadata.*/
812 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
819 ret
= consumer_close_metadata(socket
, metadata_key
);
830 * We need to execute ht_destroy outside of RCU read-side critical
831 * section and outside of call_rcu thread, so we postpone its execution
832 * using ht_cleanup_push. It is simpler than to change the semantic of
833 * the many callers of delete_ust_app_session().
836 void delete_ust_app_session_rcu(struct rcu_head
*head
)
838 struct ust_app_session
*ua_sess
=
839 caa_container_of(head
, struct ust_app_session
, rcu_head
);
841 ht_cleanup_push(ua_sess
->channels
);
846 * Delete ust app session safely. RCU read lock must be held before calling
849 * The session list lock must be held by the caller.
852 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
856 struct lttng_ht_iter iter
;
857 struct ust_app_channel
*ua_chan
;
858 struct ust_registry_session
*registry
;
862 pthread_mutex_lock(&ua_sess
->lock
);
864 assert(!ua_sess
->deleted
);
865 ua_sess
->deleted
= true;
867 registry
= get_session_registry(ua_sess
);
868 /* Registry can be null on error path during initialization. */
870 /* Push metadata for application before freeing the application. */
871 (void) push_metadata(registry
, ua_sess
->consumer
);
874 * Don't ask to close metadata for global per UID buffers. Close
875 * metadata only on destroy trace session in this case. Also, the
876 * previous push metadata could have flag the metadata registry to
877 * close so don't send a close command if closed.
879 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
880 /* And ask to close it for this session registry. */
881 (void) close_metadata(registry
, ua_sess
->consumer
);
885 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
887 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
889 delete_ust_app_channel(sock
, ua_chan
, app
);
892 /* In case of per PID, the registry is kept in the session. */
893 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
894 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
897 * Registry can be null on error path during
900 buffer_reg_pid_remove(reg_pid
);
901 buffer_reg_pid_destroy(reg_pid
);
905 if (ua_sess
->handle
!= -1) {
906 pthread_mutex_lock(&app
->sock_lock
);
907 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
908 pthread_mutex_unlock(&app
->sock_lock
);
909 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
910 ERR("UST app sock %d release session handle failed with ret %d",
913 /* Remove session from application UST object descriptor. */
914 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
915 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
919 pthread_mutex_unlock(&ua_sess
->lock
);
921 consumer_output_put(ua_sess
->consumer
);
923 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
927 * Delete a traceable application structure from the global list. Never call
928 * this function outside of a call_rcu call.
930 * RCU read side lock should _NOT_ be held when calling this function.
933 void delete_ust_app(struct ust_app
*app
)
936 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
939 * The session list lock must be held during this function to guarantee
940 * the existence of ua_sess.
943 /* Delete ust app sessions info */
948 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
950 /* Free every object in the session and the session. */
952 delete_ust_app_session(sock
, ua_sess
, app
);
956 ht_cleanup_push(app
->sessions
);
957 ht_cleanup_push(app
->ust_sessions_objd
);
958 ht_cleanup_push(app
->ust_objd
);
960 ustctl_release_object(sock
, app
->token_communication
.handle
);
962 lttng_pipe_close(app
->token_communication
.trigger_event_pipe
);
965 * Wait until we have deleted the application from the sock hash table
966 * before closing this socket, otherwise an application could re-use the
967 * socket ID and race with the teardown, using the same hash table entry.
969 * It's OK to leave the close in call_rcu. We want it to stay unique for
970 * all RCU readers that could run concurrently with unregister app,
971 * therefore we _need_ to only close that socket after a grace period. So
972 * it should stay in this RCU callback.
974 * This close() is a very important step of the synchronization model so
975 * every modification to this function must be carefully reviewed.
981 lttng_fd_put(LTTNG_FD_APPS
, 1);
983 DBG2("UST app pid %d deleted", app
->pid
);
985 session_unlock_list();
989 * URCU intermediate call to delete an UST app.
992 void delete_ust_app_rcu(struct rcu_head
*head
)
994 struct lttng_ht_node_ulong
*node
=
995 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
996 struct ust_app
*app
=
997 caa_container_of(node
, struct ust_app
, pid_n
);
999 DBG3("Call RCU deleting app PID %d", app
->pid
);
1000 delete_ust_app(app
);
1004 * Delete the session from the application ht and delete the data structure by
1005 * freeing every object inside and releasing them.
1007 * The session list lock must be held by the caller.
1009 static void destroy_app_session(struct ust_app
*app
,
1010 struct ust_app_session
*ua_sess
)
1013 struct lttng_ht_iter iter
;
1018 iter
.iter
.node
= &ua_sess
->node
.node
;
1019 ret
= lttng_ht_del(app
->sessions
, &iter
);
1021 /* Already scheduled for teardown. */
1025 /* Once deleted, free the data structure. */
1026 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1033 * Alloc new UST app session.
1036 struct ust_app_session
*alloc_ust_app_session(void)
1038 struct ust_app_session
*ua_sess
;
1040 /* Init most of the default value by allocating and zeroing */
1041 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1042 if (ua_sess
== NULL
) {
1047 ua_sess
->handle
= -1;
1048 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1049 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1050 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1059 * Alloc new UST app channel.
1062 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1063 struct ust_app_session
*ua_sess
,
1064 struct lttng_ust_channel_attr
*attr
)
1066 struct ust_app_channel
*ua_chan
;
1068 /* Init most of the default value by allocating and zeroing */
1069 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1070 if (ua_chan
== NULL
) {
1075 /* Setup channel name */
1076 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1077 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1079 ua_chan
->enabled
= 1;
1080 ua_chan
->handle
= -1;
1081 ua_chan
->session
= ua_sess
;
1082 ua_chan
->key
= get_next_channel_key();
1083 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1084 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1085 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1087 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1088 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1090 /* Copy attributes */
1092 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1093 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1094 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1095 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1096 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1097 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1098 ua_chan
->attr
.output
= attr
->output
;
1099 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1101 /* By default, the channel is a per cpu channel. */
1102 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1104 DBG3("UST app channel %s allocated", ua_chan
->name
);
1113 * Allocate and initialize a UST app stream.
1115 * Return newly allocated stream pointer or NULL on error.
1117 struct ust_app_stream
*ust_app_alloc_stream(void)
1119 struct ust_app_stream
*stream
= NULL
;
1121 stream
= zmalloc(sizeof(*stream
));
1122 if (stream
== NULL
) {
1123 PERROR("zmalloc ust app stream");
1127 /* Zero could be a valid value for a handle so flag it to -1. */
1128 stream
->handle
= -1;
1135 * Alloc new UST app event.
1138 struct ust_app_event
*alloc_ust_app_event(char *name
,
1139 struct lttng_ust_event
*attr
)
1141 struct ust_app_event
*ua_event
;
1143 /* Init most of the default value by allocating and zeroing */
1144 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1145 if (ua_event
== NULL
) {
1146 PERROR("Failed to allocate ust_app_event structure");
1150 ua_event
->enabled
= 1;
1151 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1152 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1153 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1155 /* Copy attributes */
1157 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1160 DBG3("UST app event %s allocated", ua_event
->name
);
1169 * Alloc new UST app token event rule.
1171 static struct ust_app_token_event_rule
*alloc_ust_app_token_event_rule(
1172 struct lttng_event_rule
*event_rule
, uint64_t token
)
1174 struct ust_app_token_event_rule
*ua_token
;
1176 ua_token
= zmalloc(sizeof(struct ust_app_token_event_rule
));
1177 if (ua_token
== NULL
) {
1178 PERROR("Failed to allocate ust_app_token_event_rule structure");
1182 ua_token
->enabled
= 1;
1183 ua_token
->token
= token
;
1184 lttng_ht_node_init_u64(&ua_token
->node
, token
);
1186 /* Get reference of the event_rule */
1187 if (!lttng_event_rule_get(event_rule
)) {
1191 ua_token
->event_rule
= event_rule
;
1192 ua_token
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1193 ua_token
->exclusion
= lttng_event_rule_generate_exclusions(event_rule
);
1195 DBG3("UST app token event rule %" PRIu64
" allocated", ua_token
->token
);
1204 * Alloc new UST app context.
1207 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1209 struct ust_app_ctx
*ua_ctx
;
1211 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1212 if (ua_ctx
== NULL
) {
1216 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1219 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1220 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1221 char *provider_name
= NULL
, *ctx_name
= NULL
;
1223 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1224 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1225 if (!provider_name
|| !ctx_name
) {
1226 free(provider_name
);
1231 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1232 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1236 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1244 * Create a liblttng-ust filter bytecode from given bytecode.
1246 * Return allocated filter or NULL on error.
1248 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1249 const struct lttng_filter_bytecode
*orig_f
)
1251 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1253 /* Copy filter bytecode */
1254 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1256 PERROR("zmalloc alloc ust filter bytecode");
1260 assert(sizeof(struct lttng_filter_bytecode
) ==
1261 sizeof(struct lttng_ust_filter_bytecode
));
1262 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1268 * Find an ust_app using the sock and return it. RCU read side lock must be
1269 * held before calling this helper function.
1271 struct ust_app
*ust_app_find_by_sock(int sock
)
1273 struct lttng_ht_node_ulong
*node
;
1274 struct lttng_ht_iter iter
;
1276 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1277 node
= lttng_ht_iter_get_node_ulong(&iter
);
1279 DBG2("UST app find by sock %d not found", sock
);
1283 return caa_container_of(node
, struct ust_app
, sock_n
);
1290 * Find an ust_app using the notify sock and return it. RCU read side lock must
1291 * be held before calling this helper function.
1293 static struct ust_app
*find_app_by_notify_sock(int sock
)
1295 struct lttng_ht_node_ulong
*node
;
1296 struct lttng_ht_iter iter
;
1298 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1300 node
= lttng_ht_iter_get_node_ulong(&iter
);
1302 DBG2("UST app find by notify sock %d not found", sock
);
1306 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1313 * Lookup for an ust app event based on event name, filter bytecode and the
1316 * Return an ust_app_event object or NULL on error.
1318 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1319 const char *name
, const struct lttng_filter_bytecode
*filter
,
1321 const struct lttng_event_exclusion
*exclusion
)
1323 struct lttng_ht_iter iter
;
1324 struct lttng_ht_node_str
*node
;
1325 struct ust_app_event
*event
= NULL
;
1326 struct ust_app_ht_key key
;
1331 /* Setup key for event lookup. */
1333 key
.filter
= filter
;
1334 key
.loglevel_type
= loglevel_value
;
1335 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1336 key
.exclusion
= exclusion
;
1338 /* Lookup using the event name as hash and a custom match fct. */
1339 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1340 ht_match_ust_app_event
, &key
, &iter
.iter
);
1341 node
= lttng_ht_iter_get_node_str(&iter
);
1346 event
= caa_container_of(node
, struct ust_app_event
, node
);
1353 * Lookup for an ust app tokens based on a token id.
1355 * Return an ust_app_token_event_rule object or NULL on error.
1357 static struct ust_app_token_event_rule
*find_ust_app_token_event_rule(struct lttng_ht
*ht
,
1360 struct lttng_ht_iter iter
;
1361 struct lttng_ht_node_u64
*node
;
1362 struct ust_app_token_event_rule
*token_event_rule
= NULL
;
1366 lttng_ht_lookup(ht
, &token
, &iter
);
1367 node
= lttng_ht_iter_get_node_u64(&iter
);
1369 DBG2("UST app token %" PRIu64
" not found", token
);
1373 token_event_rule
= caa_container_of(node
, struct ust_app_token_event_rule
, node
);
1375 return token_event_rule
;
1379 * Create the channel context on the tracer.
1381 * Called with UST app session lock held.
1384 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1385 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1389 health_code_update();
1391 pthread_mutex_lock(&app
->sock_lock
);
1392 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1393 ua_chan
->obj
, &ua_ctx
->obj
);
1394 pthread_mutex_unlock(&app
->sock_lock
);
1396 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1397 ERR("UST app create channel context failed for app (pid: %d) "
1398 "with ret %d", app
->pid
, ret
);
1401 * This is normal behavior, an application can die during the
1402 * creation process. Don't report an error so the execution can
1403 * continue normally.
1406 DBG3("UST app add context failed. Application is dead.");
1411 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1413 DBG2("UST app context handle %d created successfully for channel %s",
1414 ua_ctx
->handle
, ua_chan
->name
);
1417 health_code_update();
1422 * Set the filter on the tracer.
1424 static int set_ust_filter(struct ust_app
*app
,
1425 const struct lttng_filter_bytecode
*bytecode
,
1426 struct lttng_ust_object_data
*ust_object
)
1429 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1431 health_code_update();
1433 ust_bytecode
= create_ust_bytecode_from_bytecode(bytecode
);
1434 if (!ust_bytecode
) {
1435 ret
= -LTTNG_ERR_NOMEM
;
1438 pthread_mutex_lock(&app
->sock_lock
);
1439 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1441 pthread_mutex_unlock(&app
->sock_lock
);
1443 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1444 ERR("UST app set filter failed for object %p of app (pid: %d) "
1445 "with ret %d", ust_object
, app
->pid
, ret
);
1448 * This is normal behavior, an application can die during the
1449 * creation process. Don't report an error so the execution can
1450 * continue normally.
1453 DBG3("UST app set filter. Application is dead.");
1458 DBG2("UST filter set for object %p successfully", ust_object
);
1461 health_code_update();
1467 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1468 struct lttng_event_exclusion
*exclusion
)
1470 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1471 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1472 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1474 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1475 if (!ust_exclusion
) {
1480 assert(sizeof(struct lttng_event_exclusion
) ==
1481 sizeof(struct lttng_ust_event_exclusion
));
1482 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1484 return ust_exclusion
;
1488 * Set event exclusions on the tracer.
1490 static int set_ust_exclusions(struct ust_app
*app
,
1491 struct lttng_event_exclusion
*exclusions
,
1492 struct lttng_ust_object_data
*ust_object
)
1495 struct lttng_ust_event_exclusion
*ust_exclusions
= NULL
;
1497 assert(exclusions
&& exclusions
->count
> 0);
1499 health_code_update();
1501 ust_exclusions
= create_ust_exclusion_from_exclusion(
1503 if (!ust_exclusions
) {
1504 ret
= -LTTNG_ERR_NOMEM
;
1507 pthread_mutex_lock(&app
->sock_lock
);
1508 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1509 pthread_mutex_unlock(&app
->sock_lock
);
1511 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1512 ERR("UST app exclusions failed for object %p of app (pid: %d) "
1513 "with ret %d", ust_object
, app
->pid
, ret
);
1516 * This is normal behavior, an application can die during the
1517 * creation process. Don't report an error so the execution can
1518 * continue normally.
1521 DBG3("UST app set exclusions failed. Application is dead.");
1526 DBG2("UST exclusions set successfully for object %p", ust_object
);
1529 health_code_update();
1530 free(ust_exclusions
);
1535 * Disable the specified event on to UST tracer for the UST session.
1537 static int disable_ust_object(struct ust_app
*app
,
1538 struct lttng_ust_object_data
*object
)
1542 health_code_update();
1544 pthread_mutex_lock(&app
->sock_lock
);
1545 ret
= ustctl_disable(app
->sock
, object
);
1546 pthread_mutex_unlock(&app
->sock_lock
);
1548 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1549 ERR("UST app disable failed for object %p app (pid: %d) with ret %d",
1550 object
, app
->pid
, ret
);
1553 * This is normal behavior, an application can die during the
1554 * creation process. Don't report an error so the execution can
1555 * continue normally.
1558 DBG3("UST app disable event failed. Application is dead.");
1563 DBG2("UST app object %p disabled successfully for app (pid: %d)",
1567 health_code_update();
1572 * Disable the specified channel on to UST tracer for the UST session.
1574 static int disable_ust_channel(struct ust_app
*app
,
1575 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1579 health_code_update();
1581 pthread_mutex_lock(&app
->sock_lock
);
1582 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1583 pthread_mutex_unlock(&app
->sock_lock
);
1585 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1586 ERR("UST app channel %s disable failed for app (pid: %d) "
1587 "and session handle %d with ret %d",
1588 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1591 * This is normal behavior, an application can die during the
1592 * creation process. Don't report an error so the execution can
1593 * continue normally.
1596 DBG3("UST app disable channel failed. Application is dead.");
1601 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1602 ua_chan
->name
, app
->pid
);
1605 health_code_update();
1610 * Enable the specified channel on to UST tracer for the UST session.
1612 static int enable_ust_channel(struct ust_app
*app
,
1613 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1617 health_code_update();
1619 pthread_mutex_lock(&app
->sock_lock
);
1620 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1621 pthread_mutex_unlock(&app
->sock_lock
);
1623 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1624 ERR("UST app channel %s enable failed for app (pid: %d) "
1625 "and session handle %d with ret %d",
1626 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1629 * This is normal behavior, an application can die during the
1630 * creation process. Don't report an error so the execution can
1631 * continue normally.
1634 DBG3("UST app enable channel failed. Application is dead.");
1639 ua_chan
->enabled
= 1;
1641 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1642 ua_chan
->name
, app
->pid
);
1645 health_code_update();
1650 * Enable the specified event on to UST tracer for the UST session.
1652 static int enable_ust_object(struct ust_app
*app
, struct lttng_ust_object_data
*ust_object
)
1656 health_code_update();
1658 pthread_mutex_lock(&app
->sock_lock
);
1659 ret
= ustctl_enable(app
->sock
, ust_object
);
1660 pthread_mutex_unlock(&app
->sock_lock
);
1662 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1663 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
1664 ust_object
, app
->pid
, ret
);
1667 * This is normal behavior, an application can die during the
1668 * creation process. Don't report an error so the execution can
1669 * continue normally.
1672 DBG3("UST app enable failed. Application is dead.");
1677 DBG2("UST app object %p enabled successfully for app (pid: %d)",
1678 ust_object
, app
->pid
);
1681 health_code_update();
1686 * Send channel and stream buffer to application.
1688 * Return 0 on success. On error, a negative value is returned.
1690 static int send_channel_pid_to_ust(struct ust_app
*app
,
1691 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1694 struct ust_app_stream
*stream
, *stmp
;
1700 health_code_update();
1702 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1705 /* Send channel to the application. */
1706 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1707 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1708 ret
= -ENOTCONN
; /* Caused by app exiting. */
1710 } else if (ret
< 0) {
1714 health_code_update();
1716 /* Send all streams to application. */
1717 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1718 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1719 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1720 ret
= -ENOTCONN
; /* Caused by app exiting. */
1722 } else if (ret
< 0) {
1725 /* We don't need the stream anymore once sent to the tracer. */
1726 cds_list_del(&stream
->list
);
1727 delete_ust_app_stream(-1, stream
, app
);
1729 /* Flag the channel that it is sent to the application. */
1730 ua_chan
->is_sent
= 1;
1733 health_code_update();
1738 * Create the specified event onto the UST tracer for a UST session.
1740 * Should be called with session mutex held.
1743 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1744 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1748 health_code_update();
1750 /* Create UST event on tracer */
1751 pthread_mutex_lock(&app
->sock_lock
);
1752 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1754 pthread_mutex_unlock(&app
->sock_lock
);
1756 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1758 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1759 ua_event
->attr
.name
, app
->pid
, ret
);
1762 * This is normal behavior, an application can die during the
1763 * creation process. Don't report an error so the execution can
1764 * continue normally.
1767 DBG3("UST app create event failed. Application is dead.");
1772 ua_event
->handle
= ua_event
->obj
->handle
;
1774 DBG2("UST app event %s created successfully for pid:%d object: %p",
1775 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
1777 health_code_update();
1779 /* Set filter if one is present. */
1780 if (ua_event
->filter
) {
1781 ret
= set_ust_filter(app
, ua_event
->filter
, ua_event
->obj
);
1787 /* Set exclusions for the event */
1788 if (ua_event
->exclusion
) {
1789 ret
= set_ust_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
1795 /* If event not enabled, disable it on the tracer */
1796 if (ua_event
->enabled
) {
1798 * We now need to explicitly enable the event, since it
1799 * is now disabled at creation.
1801 ret
= enable_ust_object(app
, ua_event
->obj
);
1804 * If we hit an EPERM, something is wrong with our enable call. If
1805 * we get an EEXIST, there is a problem on the tracer side since we
1809 case -LTTNG_UST_ERR_PERM
:
1810 /* Code flow problem */
1812 case -LTTNG_UST_ERR_EXIST
:
1813 /* It's OK for our use case. */
1824 health_code_update();
1829 void init_ust_trigger_from_event_rule(const struct lttng_event_rule
*rule
, struct lttng_ust_trigger
*trigger
)
1831 enum lttng_event_rule_status status
;
1832 enum lttng_loglevel_type loglevel_type
;
1833 enum lttng_ust_loglevel_type ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1835 const char *pattern
;
1837 /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported */
1838 assert(lttng_event_rule_get_type(rule
) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
1840 memset(trigger
, 0, sizeof(*trigger
));
1842 if (lttng_event_rule_is_agent(rule
)) {
1844 * Special event for agents
1845 * The actual meat of the event is in the filter that will be
1846 * attached later on.
1847 * Set the default values for the agent event.
1849 pattern
= event_get_default_agent_ust_name(lttng_event_rule_get_domain_type(rule
));
1851 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1853 status
= lttng_event_rule_tracepoint_get_pattern(rule
, &pattern
);
1854 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1855 /* At this point this is a fatal error */
1859 status
= lttng_event_rule_tracepoint_get_loglevel_type(
1860 rule
, &loglevel_type
);
1861 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1862 /* At this point this is a fatal error */
1866 switch (loglevel_type
) {
1867 case LTTNG_EVENT_LOGLEVEL_ALL
:
1868 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1870 case LTTNG_EVENT_LOGLEVEL_RANGE
:
1871 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
1873 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
1874 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
1878 if (loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1879 status
= lttng_event_rule_tracepoint_get_loglevel(
1881 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
1885 trigger
->instrumentation
= LTTNG_UST_TRACEPOINT
;
1886 strncpy(trigger
->name
, pattern
, LTTNG_UST_SYM_NAME_LEN
- 1);
1887 trigger
->loglevel_type
= ust_loglevel_type
;
1888 trigger
->loglevel
= loglevel
;
1892 * Create the specified event rule token onto the UST tracer for a UST app.
1896 int create_ust_token_event_rule(struct ust_app
*app
, struct ust_app_token_event_rule
*ua_token
)
1899 struct lttng_ust_trigger trigger
;
1901 health_code_update();
1903 init_ust_trigger_from_event_rule(ua_token
->event_rule
, &trigger
);
1904 trigger
.id
= ua_token
->token
;
1906 /* Create UST trigger on tracer */
1907 pthread_mutex_lock(&app
->sock_lock
);
1908 ret
= ustctl_create_trigger(app
->sock
, &trigger
, app
->token_communication
.handle
, &ua_token
->obj
);
1909 pthread_mutex_unlock(&app
->sock_lock
);
1911 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1913 ERR("Error ustctl create trigger %s for app pid: %d with ret %d",
1914 trigger
.name
, app
->pid
, ret
);
1917 * This is normal behavior, an application can die during the
1918 * creation process. Don't report an error so the execution can
1919 * continue normally.
1922 DBG3("UST app create event failed. Application is dead.");
1927 ua_token
->handle
= ua_token
->obj
->handle
;
1929 DBG2("UST app event %s created successfully for pid:%d object: %p",
1930 trigger
.name
, app
->pid
, ua_token
->obj
);
1932 health_code_update();
1934 /* Set filter if one is present. */
1935 if (ua_token
->filter
) {
1936 ret
= set_ust_filter(app
, ua_token
->filter
, ua_token
->obj
);
1942 /* Set exclusions for the event */
1943 if (ua_token
->exclusion
) {
1944 ret
= set_ust_exclusions(app
, ua_token
->exclusion
, ua_token
->obj
);
1951 * We now need to explicitly enable the event, since it
1952 * is disabled at creation.
1954 ret
= enable_ust_object(app
, ua_token
->obj
);
1957 * If we hit an EPERM, something is wrong with our enable call. If
1958 * we get an EEXIST, there is a problem on the tracer side since we
1962 case -LTTNG_UST_ERR_PERM
:
1963 /* Code flow problem */
1965 case -LTTNG_UST_ERR_EXIST
:
1966 /* It's OK for our use case. */
1974 ua_token
->enabled
= true;
1977 health_code_update();
1982 * Copy data between an UST app event and a LTT event.
1984 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1985 struct ltt_ust_event
*uevent
)
1987 size_t exclusion_alloc_size
;
1989 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1990 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1992 ua_event
->enabled
= uevent
->enabled
;
1994 /* Copy event attributes */
1995 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1997 /* Copy filter bytecode */
1998 if (uevent
->filter
) {
1999 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
2000 /* Filter might be NULL here in case of ENONEM. */
2003 /* Copy exclusion data */
2004 if (uevent
->exclusion
) {
2005 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2006 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2007 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
2008 if (ua_event
->exclusion
== NULL
) {
2011 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2012 exclusion_alloc_size
);
2018 * Copy data between an UST app channel and a LTT channel.
2020 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2021 struct ltt_ust_channel
*uchan
)
2023 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2025 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2026 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2028 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2029 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2031 /* Copy event attributes since the layout is different. */
2032 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2033 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2034 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2035 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2036 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2037 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2038 ua_chan
->attr
.output
= uchan
->attr
.output
;
2039 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2042 * Note that the attribute channel type is not set since the channel on the
2043 * tracing registry side does not have this information.
2046 ua_chan
->enabled
= uchan
->enabled
;
2047 ua_chan
->tracing_channel_id
= uchan
->id
;
2049 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2053 * Copy data between a UST app session and a regular LTT session.
2055 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2056 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2058 struct tm
*timeinfo
;
2061 char tmp_shm_path
[PATH_MAX
];
2063 timeinfo
= localtime(&app
->registration_time
);
2064 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2066 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2068 ua_sess
->tracing_id
= usess
->id
;
2069 ua_sess
->id
= get_next_session_id();
2070 ua_sess
->real_credentials
.uid
= app
->uid
;
2071 ua_sess
->real_credentials
.gid
= app
->gid
;
2072 ua_sess
->effective_credentials
.uid
= usess
->uid
;
2073 ua_sess
->effective_credentials
.gid
= usess
->gid
;
2074 ua_sess
->buffer_type
= usess
->buffer_type
;
2075 ua_sess
->bits_per_long
= app
->bits_per_long
;
2077 /* There is only one consumer object per session possible. */
2078 consumer_output_get(usess
->consumer
);
2079 ua_sess
->consumer
= usess
->consumer
;
2081 ua_sess
->output_traces
= usess
->output_traces
;
2082 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2083 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2084 &usess
->metadata_attr
);
2086 switch (ua_sess
->buffer_type
) {
2087 case LTTNG_BUFFER_PER_PID
:
2088 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2089 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2092 case LTTNG_BUFFER_PER_UID
:
2093 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2094 DEFAULT_UST_TRACE_UID_PATH
,
2095 ua_sess
->real_credentials
.uid
,
2096 app
->bits_per_long
);
2103 PERROR("asprintf UST shadow copy session");
2108 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2109 sizeof(ua_sess
->root_shm_path
));
2110 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2111 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2112 sizeof(ua_sess
->shm_path
));
2113 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2114 if (ua_sess
->shm_path
[0]) {
2115 switch (ua_sess
->buffer_type
) {
2116 case LTTNG_BUFFER_PER_PID
:
2117 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2118 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2119 app
->name
, app
->pid
, datetime
);
2121 case LTTNG_BUFFER_PER_UID
:
2122 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2123 "/" DEFAULT_UST_TRACE_UID_PATH
,
2124 app
->uid
, app
->bits_per_long
);
2131 PERROR("sprintf UST shadow copy session");
2135 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2136 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2137 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2142 consumer_output_put(ua_sess
->consumer
);
2146 * Lookup sesison wrapper.
2149 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2150 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2152 /* Get right UST app session from app */
2153 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2157 * Return ust app session from the app session hashtable using the UST session
2160 static struct ust_app_session
*lookup_session_by_app(
2161 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2163 struct lttng_ht_iter iter
;
2164 struct lttng_ht_node_u64
*node
;
2166 __lookup_session_by_app(usess
, app
, &iter
);
2167 node
= lttng_ht_iter_get_node_u64(&iter
);
2172 return caa_container_of(node
, struct ust_app_session
, node
);
2179 * Setup buffer registry per PID for the given session and application. If none
2180 * is found, a new one is created, added to the global registry and
2181 * initialized. If regp is valid, it's set with the newly created object.
2183 * Return 0 on success or else a negative value.
2185 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2186 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2189 struct buffer_reg_pid
*reg_pid
;
2196 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2199 * This is the create channel path meaning that if there is NO
2200 * registry available, we have to create one for this session.
2202 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2203 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2211 /* Initialize registry. */
2212 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2213 app
->bits_per_long
, app
->uint8_t_alignment
,
2214 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2215 app
->uint64_t_alignment
, app
->long_alignment
,
2216 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2217 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2218 ua_sess
->effective_credentials
.uid
,
2219 ua_sess
->effective_credentials
.gid
, ua_sess
->tracing_id
,
2223 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2224 * destroy the buffer registry, because it is always expected
2225 * that if the buffer registry can be found, its ust registry is
2228 buffer_reg_pid_destroy(reg_pid
);
2232 buffer_reg_pid_add(reg_pid
);
2234 DBG3("UST app buffer registry per PID created successfully");
2246 * Setup buffer registry per UID for the given session and application. If none
2247 * is found, a new one is created, added to the global registry and
2248 * initialized. If regp is valid, it's set with the newly created object.
2250 * Return 0 on success or else a negative value.
2252 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2253 struct ust_app_session
*ua_sess
,
2254 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2257 struct buffer_reg_uid
*reg_uid
;
2264 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2267 * This is the create channel path meaning that if there is NO
2268 * registry available, we have to create one for this session.
2270 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2271 LTTNG_DOMAIN_UST
, ®_uid
,
2272 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2280 /* Initialize registry. */
2281 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2282 app
->bits_per_long
, app
->uint8_t_alignment
,
2283 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2284 app
->uint64_t_alignment
, app
->long_alignment
,
2285 app
->byte_order
, app
->version
.major
,
2286 app
->version
.minor
, reg_uid
->root_shm_path
,
2287 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2288 ua_sess
->tracing_id
, app
->uid
);
2291 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2292 * destroy the buffer registry, because it is always expected
2293 * that if the buffer registry can be found, its ust registry is
2296 buffer_reg_uid_destroy(reg_uid
, NULL
);
2299 /* Add node to teardown list of the session. */
2300 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2302 buffer_reg_uid_add(reg_uid
);
2304 DBG3("UST app buffer registry per UID created successfully");
2315 * Create a session on the tracer side for the given app.
2317 * On success, ua_sess_ptr is populated with the session pointer or else left
2318 * untouched. If the session was created, is_created is set to 1. On error,
2319 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2322 * Returns 0 on success or else a negative code which is either -ENOMEM or
2323 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2325 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2326 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2329 int ret
, created
= 0;
2330 struct ust_app_session
*ua_sess
;
2334 assert(ua_sess_ptr
);
2336 health_code_update();
2338 ua_sess
= lookup_session_by_app(usess
, app
);
2339 if (ua_sess
== NULL
) {
2340 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2341 app
->pid
, usess
->id
);
2342 ua_sess
= alloc_ust_app_session();
2343 if (ua_sess
== NULL
) {
2344 /* Only malloc can failed so something is really wrong */
2348 shadow_copy_session(ua_sess
, usess
, app
);
2352 switch (usess
->buffer_type
) {
2353 case LTTNG_BUFFER_PER_PID
:
2354 /* Init local registry. */
2355 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2357 delete_ust_app_session(-1, ua_sess
, app
);
2361 case LTTNG_BUFFER_PER_UID
:
2362 /* Look for a global registry. If none exists, create one. */
2363 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2365 delete_ust_app_session(-1, ua_sess
, app
);
2375 health_code_update();
2377 if (ua_sess
->handle
== -1) {
2378 pthread_mutex_lock(&app
->sock_lock
);
2379 ret
= ustctl_create_session(app
->sock
);
2380 pthread_mutex_unlock(&app
->sock_lock
);
2382 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2383 ERR("Creating session for app pid %d with ret %d",
2386 DBG("UST app creating session failed. Application is dead");
2388 * This is normal behavior, an application can die during the
2389 * creation process. Don't report an error so the execution can
2390 * continue normally. This will get flagged ENOTCONN and the
2391 * caller will handle it.
2395 delete_ust_app_session(-1, ua_sess
, app
);
2396 if (ret
!= -ENOMEM
) {
2398 * Tracer is probably gone or got an internal error so let's
2399 * behave like it will soon unregister or not usable.
2406 ua_sess
->handle
= ret
;
2408 /* Add ust app session to app's HT */
2409 lttng_ht_node_init_u64(&ua_sess
->node
,
2410 ua_sess
->tracing_id
);
2411 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2412 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2413 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2414 &ua_sess
->ust_objd_node
);
2416 DBG2("UST app session created successfully with handle %d", ret
);
2419 *ua_sess_ptr
= ua_sess
;
2421 *is_created
= created
;
2424 /* Everything went well. */
2428 health_code_update();
2433 * Match function for a hash table lookup of ust_app_ctx.
2435 * It matches an ust app context based on the context type and, in the case
2436 * of perf counters, their name.
2438 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2440 struct ust_app_ctx
*ctx
;
2441 const struct lttng_ust_context_attr
*key
;
2446 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2450 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2455 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2456 if (strncmp(key
->u
.perf_counter
.name
,
2457 ctx
->ctx
.u
.perf_counter
.name
,
2458 sizeof(key
->u
.perf_counter
.name
))) {
2462 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2463 if (strcmp(key
->u
.app_ctx
.provider_name
,
2464 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2465 strcmp(key
->u
.app_ctx
.ctx_name
,
2466 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2482 * Lookup for an ust app context from an lttng_ust_context.
2484 * Must be called while holding RCU read side lock.
2485 * Return an ust_app_ctx object or NULL on error.
2488 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2489 struct lttng_ust_context_attr
*uctx
)
2491 struct lttng_ht_iter iter
;
2492 struct lttng_ht_node_ulong
*node
;
2493 struct ust_app_ctx
*app_ctx
= NULL
;
2498 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2499 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2500 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2501 node
= lttng_ht_iter_get_node_ulong(&iter
);
2506 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2513 * Create a context for the channel on the tracer.
2515 * Called with UST app session lock held and a RCU read side lock.
2518 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2519 struct lttng_ust_context_attr
*uctx
,
2520 struct ust_app
*app
)
2523 struct ust_app_ctx
*ua_ctx
;
2525 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2527 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2533 ua_ctx
= alloc_ust_app_ctx(uctx
);
2534 if (ua_ctx
== NULL
) {
2540 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2541 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2542 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2544 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2554 * Enable on the tracer side a ust app event for the session and channel.
2556 * Called with UST app session lock held.
2559 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2560 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2564 ret
= enable_ust_object(app
, ua_event
->obj
);
2569 ua_event
->enabled
= 1;
2576 * Disable on the tracer side a ust app event for the session and channel.
2578 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2579 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2583 ret
= disable_ust_object(app
, ua_event
->obj
);
2588 ua_event
->enabled
= 0;
2595 * Lookup ust app channel for session and disable it on the tracer side.
2598 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2599 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2603 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2608 ua_chan
->enabled
= 0;
2615 * Lookup ust app channel for session and enable it on the tracer side. This
2616 * MUST be called with a RCU read side lock acquired.
2618 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2619 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2622 struct lttng_ht_iter iter
;
2623 struct lttng_ht_node_str
*ua_chan_node
;
2624 struct ust_app_channel
*ua_chan
;
2626 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2627 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2628 if (ua_chan_node
== NULL
) {
2629 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2630 uchan
->name
, ua_sess
->tracing_id
);
2634 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2636 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2646 * Ask the consumer to create a channel and get it if successful.
2648 * Called with UST app session lock held.
2650 * Return 0 on success or else a negative value.
2652 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2653 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2654 int bitness
, struct ust_registry_session
*registry
,
2655 uint64_t trace_archive_id
)
2658 unsigned int nb_fd
= 0;
2659 struct consumer_socket
*socket
;
2667 health_code_update();
2669 /* Get the right consumer socket for the application. */
2670 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2676 health_code_update();
2678 /* Need one fd for the channel. */
2679 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2681 ERR("Exhausted number of available FD upon create channel");
2686 * Ask consumer to create channel. The consumer will return the number of
2687 * stream we have to expect.
2689 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2690 registry
, usess
->current_trace_chunk
);
2696 * Compute the number of fd needed before receiving them. It must be 2 per
2697 * stream (2 being the default value here).
2699 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2701 /* Reserve the amount of file descriptor we need. */
2702 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2704 ERR("Exhausted number of available FD upon create channel");
2705 goto error_fd_get_stream
;
2708 health_code_update();
2711 * Now get the channel from the consumer. This call wil populate the stream
2712 * list of that channel and set the ust objects.
2714 if (usess
->consumer
->enabled
) {
2715 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2725 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2726 error_fd_get_stream
:
2728 * Initiate a destroy channel on the consumer since we had an error
2729 * handling it on our side. The return value is of no importance since we
2730 * already have a ret value set by the previous error that we need to
2733 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2735 lttng_fd_put(LTTNG_FD_APPS
, 1);
2737 health_code_update();
2743 * Duplicate the ust data object of the ust app stream and save it in the
2744 * buffer registry stream.
2746 * Return 0 on success or else a negative value.
2748 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2749 struct ust_app_stream
*stream
)
2756 /* Reserve the amount of file descriptor we need. */
2757 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2759 ERR("Exhausted number of available FD upon duplicate stream");
2763 /* Duplicate object for stream once the original is in the registry. */
2764 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2765 reg_stream
->obj
.ust
);
2767 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2768 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2769 lttng_fd_put(LTTNG_FD_APPS
, 2);
2772 stream
->handle
= stream
->obj
->handle
;
2779 * Duplicate the ust data object of the ust app. channel and save it in the
2780 * buffer registry channel.
2782 * Return 0 on success or else a negative value.
2784 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2785 struct ust_app_channel
*ua_chan
)
2792 /* Need two fds for the channel. */
2793 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2795 ERR("Exhausted number of available FD upon duplicate channel");
2799 /* Duplicate object for stream once the original is in the registry. */
2800 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2802 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2803 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2806 ua_chan
->handle
= ua_chan
->obj
->handle
;
2811 lttng_fd_put(LTTNG_FD_APPS
, 1);
2817 * For a given channel buffer registry, setup all streams of the given ust
2818 * application channel.
2820 * Return 0 on success or else a negative value.
2822 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2823 struct ust_app_channel
*ua_chan
,
2824 struct ust_app
*app
)
2827 struct ust_app_stream
*stream
, *stmp
;
2832 DBG2("UST app setup buffer registry stream");
2834 /* Send all streams to application. */
2835 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2836 struct buffer_reg_stream
*reg_stream
;
2838 ret
= buffer_reg_stream_create(®_stream
);
2844 * Keep original pointer and nullify it in the stream so the delete
2845 * stream call does not release the object.
2847 reg_stream
->obj
.ust
= stream
->obj
;
2849 buffer_reg_stream_add(reg_stream
, reg_chan
);
2851 /* We don't need the streams anymore. */
2852 cds_list_del(&stream
->list
);
2853 delete_ust_app_stream(-1, stream
, app
);
2861 * Create a buffer registry channel for the given session registry and
2862 * application channel object. If regp pointer is valid, it's set with the
2863 * created object. Important, the created object is NOT added to the session
2864 * registry hash table.
2866 * Return 0 on success else a negative value.
2868 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2869 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2872 struct buffer_reg_channel
*reg_chan
= NULL
;
2877 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2879 /* Create buffer registry channel. */
2880 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2885 reg_chan
->consumer_key
= ua_chan
->key
;
2886 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2887 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2889 /* Create and add a channel registry to session. */
2890 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2891 ua_chan
->tracing_channel_id
);
2895 buffer_reg_channel_add(reg_sess
, reg_chan
);
2904 /* Safe because the registry channel object was not added to any HT. */
2905 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2911 * Setup buffer registry channel for the given session registry and application
2912 * channel object. If regp pointer is valid, it's set with the created object.
2914 * Return 0 on success else a negative value.
2916 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2917 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2918 struct ust_app
*app
)
2925 assert(ua_chan
->obj
);
2927 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2929 /* Setup all streams for the registry. */
2930 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2935 reg_chan
->obj
.ust
= ua_chan
->obj
;
2936 ua_chan
->obj
= NULL
;
2941 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2942 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2947 * Send buffer registry channel to the application.
2949 * Return 0 on success else a negative value.
2951 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2952 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2953 struct ust_app_channel
*ua_chan
)
2956 struct buffer_reg_stream
*reg_stream
;
2963 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2965 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2970 /* Send channel to the application. */
2971 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2972 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2973 ret
= -ENOTCONN
; /* Caused by app exiting. */
2975 } else if (ret
< 0) {
2979 health_code_update();
2981 /* Send all streams to application. */
2982 pthread_mutex_lock(®_chan
->stream_list_lock
);
2983 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2984 struct ust_app_stream stream
;
2986 ret
= duplicate_stream_object(reg_stream
, &stream
);
2988 goto error_stream_unlock
;
2991 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2993 (void) release_ust_app_stream(-1, &stream
, app
);
2994 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2995 ret
= -ENOTCONN
; /* Caused by app exiting. */
2997 goto error_stream_unlock
;
3001 * The return value is not important here. This function will output an
3004 (void) release_ust_app_stream(-1, &stream
, app
);
3006 ua_chan
->is_sent
= 1;
3008 error_stream_unlock
:
3009 pthread_mutex_unlock(®_chan
->stream_list_lock
);
3015 * Create and send to the application the created buffers with per UID buffers.
3017 * This MUST be called with a RCU read side lock acquired.
3018 * The session list lock and the session's lock must be acquired.
3020 * Return 0 on success else a negative value.
3022 static int create_channel_per_uid(struct ust_app
*app
,
3023 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3024 struct ust_app_channel
*ua_chan
)
3027 struct buffer_reg_uid
*reg_uid
;
3028 struct buffer_reg_channel
*reg_chan
;
3029 struct ltt_session
*session
= NULL
;
3030 enum lttng_error_code notification_ret
;
3031 struct ust_registry_channel
*chan_reg
;
3038 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3040 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3042 * The session creation handles the creation of this global registry
3043 * object. If none can be find, there is a code flow problem or a
3048 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3054 /* Create the buffer registry channel object. */
3055 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
3057 ERR("Error creating the UST channel \"%s\" registry instance",
3062 session
= session_find_by_id(ua_sess
->tracing_id
);
3064 assert(pthread_mutex_trylock(&session
->lock
));
3065 assert(session_trylock_list());
3068 * Create the buffers on the consumer side. This call populates the
3069 * ust app channel object with all streams and data object.
3071 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3072 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
3073 session
->most_recent_chunk_id
.value
);
3075 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3079 * Let's remove the previously created buffer registry channel so
3080 * it's not visible anymore in the session registry.
3082 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3083 ua_chan
->tracing_channel_id
, false);
3084 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
3085 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3090 * Setup the streams and add it to the session registry.
3092 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3093 ua_chan
, reg_chan
, app
);
3095 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3099 /* Notify the notification subsystem of the channel's creation. */
3100 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
3101 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3102 ua_chan
->tracing_channel_id
);
3104 chan_reg
->consumer_key
= ua_chan
->key
;
3106 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
3108 notification_ret
= notification_thread_command_add_channel(
3109 notification_thread_handle
, session
->name
,
3110 ua_sess
->effective_credentials
.uid
,
3111 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
3112 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3113 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3114 if (notification_ret
!= LTTNG_OK
) {
3115 ret
= - (int) notification_ret
;
3116 ERR("Failed to add channel to notification thread");
3121 /* Send buffers to the application. */
3122 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
3124 if (ret
!= -ENOTCONN
) {
3125 ERR("Error sending channel to application");
3132 session_put(session
);
3138 * Create and send to the application the created buffers with per PID buffers.
3140 * Called with UST app session lock held.
3141 * The session list lock and the session's lock must be acquired.
3143 * Return 0 on success else a negative value.
3145 static int create_channel_per_pid(struct ust_app
*app
,
3146 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3147 struct ust_app_channel
*ua_chan
)
3150 struct ust_registry_session
*registry
;
3151 enum lttng_error_code cmd_ret
;
3152 struct ltt_session
*session
= NULL
;
3153 uint64_t chan_reg_key
;
3154 struct ust_registry_channel
*chan_reg
;
3161 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3165 registry
= get_session_registry(ua_sess
);
3166 /* The UST app session lock is held, registry shall not be null. */
3169 /* Create and add a new channel registry to session. */
3170 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3172 ERR("Error creating the UST channel \"%s\" registry instance",
3177 session
= session_find_by_id(ua_sess
->tracing_id
);
3180 assert(pthread_mutex_trylock(&session
->lock
));
3181 assert(session_trylock_list());
3183 /* Create and get channel on the consumer side. */
3184 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3185 app
->bits_per_long
, registry
,
3186 session
->most_recent_chunk_id
.value
);
3188 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3190 goto error_remove_from_registry
;
3193 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3195 if (ret
!= -ENOTCONN
) {
3196 ERR("Error sending channel to application");
3198 goto error_remove_from_registry
;
3201 chan_reg_key
= ua_chan
->key
;
3202 pthread_mutex_lock(®istry
->lock
);
3203 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3205 chan_reg
->consumer_key
= ua_chan
->key
;
3206 pthread_mutex_unlock(®istry
->lock
);
3208 cmd_ret
= notification_thread_command_add_channel(
3209 notification_thread_handle
, session
->name
,
3210 ua_sess
->effective_credentials
.uid
,
3211 ua_sess
->effective_credentials
.gid
, ua_chan
->name
,
3212 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3213 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3214 if (cmd_ret
!= LTTNG_OK
) {
3215 ret
= - (int) cmd_ret
;
3216 ERR("Failed to add channel to notification thread");
3217 goto error_remove_from_registry
;
3220 error_remove_from_registry
:
3222 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3227 session_put(session
);
3233 * From an already allocated ust app channel, create the channel buffers if
3234 * needed and send them to the application. This MUST be called with a RCU read
3235 * side lock acquired.
3237 * Called with UST app session lock held.
3239 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3240 * the application exited concurrently.
3242 static int ust_app_channel_send(struct ust_app
*app
,
3243 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3244 struct ust_app_channel
*ua_chan
)
3250 assert(usess
->active
);
3254 /* Handle buffer type before sending the channel to the application. */
3255 switch (usess
->buffer_type
) {
3256 case LTTNG_BUFFER_PER_UID
:
3258 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3264 case LTTNG_BUFFER_PER_PID
:
3266 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3278 /* Initialize ust objd object using the received handle and add it. */
3279 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3280 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3282 /* If channel is not enabled, disable it on the tracer */
3283 if (!ua_chan
->enabled
) {
3284 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3295 * Create UST app channel and return it through ua_chanp if not NULL.
3297 * Called with UST app session lock and RCU read-side lock held.
3299 * Return 0 on success or else a negative value.
3301 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3302 struct ltt_ust_channel
*uchan
,
3303 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3304 struct ust_app_channel
**ua_chanp
)
3307 struct lttng_ht_iter iter
;
3308 struct lttng_ht_node_str
*ua_chan_node
;
3309 struct ust_app_channel
*ua_chan
;
3311 /* Lookup channel in the ust app session */
3312 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3313 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3314 if (ua_chan_node
!= NULL
) {
3315 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3319 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3320 if (ua_chan
== NULL
) {
3321 /* Only malloc can fail here */
3325 shadow_copy_channel(ua_chan
, uchan
);
3327 /* Set channel type. */
3328 ua_chan
->attr
.type
= type
;
3330 /* Only add the channel if successful on the tracer side. */
3331 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3334 *ua_chanp
= ua_chan
;
3337 /* Everything went well. */
3345 * Create UST app event and create it on the tracer side.
3347 * Called with ust app session mutex held.
3350 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3351 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3352 struct ust_app
*app
)
3355 struct ust_app_event
*ua_event
;
3357 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3358 if (ua_event
== NULL
) {
3359 /* Only failure mode of alloc_ust_app_event(). */
3363 shadow_copy_event(ua_event
, uevent
);
3365 /* Create it on the tracer side */
3366 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3369 * Not found previously means that it does not exist on the
3370 * tracer. If the application reports that the event existed,
3371 * it means there is a bug in the sessiond or lttng-ust
3372 * (or corruption, etc.)
3374 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3375 ERR("Tracer for application reported that an event being created already existed: "
3376 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3378 app
->pid
, app
->ppid
, app
->uid
,
3384 add_unique_ust_app_event(ua_chan
, ua_event
);
3386 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3393 /* Valid. Calling here is already in a read side lock */
3394 delete_ust_app_event(-1, ua_event
, app
);
3399 * Create UST app event and create it on the tracer side.
3401 * Called with ust app session mutex held.
3404 int create_ust_app_token_event_rule(struct lttng_event_rule
*rule
,
3405 struct ust_app
*app
, uint64_t token
)
3408 struct ust_app_token_event_rule
*ua_token
;
3410 ua_token
= alloc_ust_app_token_event_rule(rule
, token
);
3411 if (ua_token
== NULL
) {
3416 /* Create it on the tracer side */
3417 ret
= create_ust_token_event_rule(app
, ua_token
);
3420 * Not found previously means that it does not exist on the
3421 * tracer. If the application reports that the event existed,
3422 * it means there is a bug in the sessiond or lttng-ust
3423 * (or corruption, etc.)
3425 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3426 ERR("Tracer for application reported that a token event rule being created already existed: "
3427 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3429 app
->pid
, app
->ppid
, app
->uid
,
3435 lttng_ht_add_unique_u64(app
->tokens_ht
, &ua_token
->node
);
3437 DBG2("UST app create token event rule %" PRIu64
" for PID %d completed", token
,
3444 /* Valid. Calling here is already in a read side lock */
3445 delete_ust_app_token_event_rule(-1, ua_token
, app
);
3450 * Create UST metadata and open it on the tracer side.
3452 * Called with UST app session lock held and RCU read side lock.
3454 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3455 struct ust_app
*app
, struct consumer_output
*consumer
)
3458 struct ust_app_channel
*metadata
;
3459 struct consumer_socket
*socket
;
3460 struct ust_registry_session
*registry
;
3461 struct ltt_session
*session
= NULL
;
3467 registry
= get_session_registry(ua_sess
);
3468 /* The UST app session is held registry shall not be null. */
3471 pthread_mutex_lock(®istry
->lock
);
3473 /* Metadata already exists for this registry or it was closed previously */
3474 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3479 /* Allocate UST metadata */
3480 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3482 /* malloc() failed */
3487 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3489 /* Need one fd for the channel. */
3490 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3492 ERR("Exhausted number of available FD upon create metadata");
3496 /* Get the right consumer socket for the application. */
3497 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3500 goto error_consumer
;
3504 * Keep metadata key so we can identify it on the consumer side. Assign it
3505 * to the registry *before* we ask the consumer so we avoid the race of the
3506 * consumer requesting the metadata and the ask_channel call on our side
3507 * did not returned yet.
3509 registry
->metadata_key
= metadata
->key
;
3511 session
= session_find_by_id(ua_sess
->tracing_id
);
3514 assert(pthread_mutex_trylock(&session
->lock
));
3515 assert(session_trylock_list());
3518 * Ask the metadata channel creation to the consumer. The metadata object
3519 * will be created by the consumer and kept their. However, the stream is
3520 * never added or monitored until we do a first push metadata to the
3523 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3524 registry
, session
->current_trace_chunk
);
3526 /* Nullify the metadata key so we don't try to close it later on. */
3527 registry
->metadata_key
= 0;
3528 goto error_consumer
;
3532 * The setup command will make the metadata stream be sent to the relayd,
3533 * if applicable, and the thread managing the metadatas. This is important
3534 * because after this point, if an error occurs, the only way the stream
3535 * can be deleted is to be monitored in the consumer.
3537 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3539 /* Nullify the metadata key so we don't try to close it later on. */
3540 registry
->metadata_key
= 0;
3541 goto error_consumer
;
3544 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3545 metadata
->key
, app
->pid
);
3548 lttng_fd_put(LTTNG_FD_APPS
, 1);
3549 delete_ust_app_channel(-1, metadata
, app
);
3551 pthread_mutex_unlock(®istry
->lock
);
3553 session_put(session
);
3559 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3560 * acquired before calling this function.
3562 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3564 struct ust_app
*app
= NULL
;
3565 struct lttng_ht_node_ulong
*node
;
3566 struct lttng_ht_iter iter
;
3568 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3569 node
= lttng_ht_iter_get_node_ulong(&iter
);
3571 DBG2("UST app no found with pid %d", pid
);
3575 DBG2("Found UST app by pid %d", pid
);
3577 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3584 * Allocate and init an UST app object using the registration information and
3585 * the command socket. This is called when the command socket connects to the
3588 * The object is returned on success or else NULL.
3590 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3592 struct ust_app
*lta
= NULL
;
3593 struct lttng_pipe
*trigger_event_source_pipe
= NULL
;
3598 DBG3("UST app creating application for socket %d", sock
);
3600 if ((msg
->bits_per_long
== 64 &&
3601 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3602 || (msg
->bits_per_long
== 32 &&
3603 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3604 ERR("Registration failed: application \"%s\" (pid: %d) has "
3605 "%d-bit long, but no consumerd for this size is available.\n",
3606 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3610 trigger_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3611 if (!trigger_event_source_pipe
) {
3612 PERROR("Open trigger pipe");
3616 lta
= zmalloc(sizeof(struct ust_app
));
3622 lta
->token_communication
.trigger_event_pipe
= trigger_event_source_pipe
;
3624 lta
->ppid
= msg
->ppid
;
3625 lta
->uid
= msg
->uid
;
3626 lta
->gid
= msg
->gid
;
3628 lta
->bits_per_long
= msg
->bits_per_long
;
3629 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3630 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3631 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3632 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3633 lta
->long_alignment
= msg
->long_alignment
;
3634 lta
->byte_order
= msg
->byte_order
;
3636 lta
->v_major
= msg
->major
;
3637 lta
->v_minor
= msg
->minor
;
3638 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3639 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3640 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3641 lta
->notify_sock
= -1;
3642 lta
->tokens_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3644 /* Copy name and make sure it's NULL terminated. */
3645 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3646 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3649 * Before this can be called, when receiving the registration information,
3650 * the application compatibility is checked. So, at this point, the
3651 * application can work with this session daemon.
3653 lta
->compatible
= 1;
3655 lta
->pid
= msg
->pid
;
3656 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3658 pthread_mutex_init(<a
->sock_lock
, NULL
);
3659 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3661 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3667 * For a given application object, add it to every hash table.
3669 void ust_app_add(struct ust_app
*app
)
3672 assert(app
->notify_sock
>= 0);
3674 app
->registration_time
= time(NULL
);
3679 * On a re-registration, we want to kick out the previous registration of
3682 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3685 * The socket _should_ be unique until _we_ call close. So, a add_unique
3686 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3687 * already in the table.
3689 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3691 /* Add application to the notify socket hash table. */
3692 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3693 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3695 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3696 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3697 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3704 * Set the application version into the object.
3706 * Return 0 on success else a negative value either an errno code or a
3707 * LTTng-UST error code.
3709 int ust_app_version(struct ust_app
*app
)
3715 pthread_mutex_lock(&app
->sock_lock
);
3716 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3717 pthread_mutex_unlock(&app
->sock_lock
);
3719 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3720 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3722 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3730 * Setup the base trigger group.
3732 * Return 0 on success else a negative value either an errno code or a
3733 * LTTng-UST error code.
3735 int ust_app_setup_trigger_group(struct ust_app
*app
)
3739 struct lttng_ust_object_data
*group
= NULL
;
3740 enum lttng_error_code lttng_ret
;
3744 /* Get the write side of the pipe */
3745 writefd
= lttng_pipe_get_writefd(app
->token_communication
.trigger_event_pipe
);
3747 pthread_mutex_lock(&app
->sock_lock
);
3748 ret
= ustctl_create_trigger_group(app
->sock
, writefd
, &group
);
3749 pthread_mutex_unlock(&app
->sock_lock
);
3751 ERR("UST app %d create_trigger_group failed with ret %d", app
->sock
, ret
);
3755 app
->token_communication
.handle
= group
;
3757 lttng_ret
= notification_thread_command_add_application(
3758 notification_thread_handle
, app
->token_communication
.trigger_event_pipe
);
3759 if (lttng_ret
!= LTTNG_OK
) {
3762 ERR("Failed to add channel to notification thread");
3771 * Unregister app by removing it from the global traceable app list and freeing
3774 * The socket is already closed at this point so no close to sock.
3776 void ust_app_unregister(int sock
)
3778 enum lttng_error_code ret_code
;
3779 struct ust_app
*lta
;
3780 struct lttng_ht_node_ulong
*node
;
3781 struct lttng_ht_iter ust_app_sock_iter
;
3782 struct lttng_ht_iter iter
;
3783 struct ust_app_session
*ua_sess
;
3788 /* Get the node reference for a call_rcu */
3789 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3790 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3793 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3794 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3797 * For per-PID buffers, perform "push metadata" and flush all
3798 * application streams before removing app from hash tables,
3799 * ensuring proper behavior of data_pending check.
3800 * Remove sessions so they are not visible during deletion.
3802 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3804 struct ust_registry_session
*registry
;
3806 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3808 /* The session was already removed so scheduled for teardown. */
3812 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3813 (void) ust_app_flush_app_session(lta
, ua_sess
);
3817 * Add session to list for teardown. This is safe since at this point we
3818 * are the only one using this list.
3820 pthread_mutex_lock(&ua_sess
->lock
);
3822 if (ua_sess
->deleted
) {
3823 pthread_mutex_unlock(&ua_sess
->lock
);
3828 * Normally, this is done in the delete session process which is
3829 * executed in the call rcu below. However, upon registration we can't
3830 * afford to wait for the grace period before pushing data or else the
3831 * data pending feature can race between the unregistration and stop
3832 * command where the data pending command is sent *before* the grace
3835 * The close metadata below nullifies the metadata pointer in the
3836 * session so the delete session will NOT push/close a second time.
3838 registry
= get_session_registry(ua_sess
);
3840 /* Push metadata for application before freeing the application. */
3841 (void) push_metadata(registry
, ua_sess
->consumer
);
3844 * Don't ask to close metadata for global per UID buffers. Close
3845 * metadata only on destroy trace session in this case. Also, the
3846 * previous push metadata could have flag the metadata registry to
3847 * close so don't send a close command if closed.
3849 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3850 /* And ask to close it for this session registry. */
3851 (void) close_metadata(registry
, ua_sess
->consumer
);
3854 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3856 pthread_mutex_unlock(&ua_sess
->lock
);
3859 /* Remove application from PID hash table */
3860 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3864 * Remove application from notify hash table. The thread handling the
3865 * notify socket could have deleted the node so ignore on error because
3866 * either way it's valid. The close of that socket is handled by the
3867 * apps_notify_thread.
3869 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3870 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3873 * Ignore return value since the node might have been removed before by an
3874 * add replace during app registration because the PID can be reassigned by
3877 iter
.iter
.node
= <a
->pid_n
.node
;
3878 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3880 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3884 ret_code
= notification_thread_command_remove_application(
3885 notification_thread_handle
,
3886 lta
->token_communication
.trigger_event_pipe
);
3887 if (ret_code
!= LTTNG_OK
) {
3888 ERR("Failed to remove application from notification thread");
3892 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3899 * Fill events array with all events name of all registered apps.
3901 int ust_app_list_events(struct lttng_event
**events
)
3904 size_t nbmem
, count
= 0;
3905 struct lttng_ht_iter iter
;
3906 struct ust_app
*app
;
3907 struct lttng_event
*tmp_event
;
3909 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3910 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3911 if (tmp_event
== NULL
) {
3912 PERROR("zmalloc ust app events");
3919 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3920 struct lttng_ust_tracepoint_iter uiter
;
3922 health_code_update();
3924 if (!app
->compatible
) {
3926 * TODO: In time, we should notice the caller of this error by
3927 * telling him that this is a version error.
3931 pthread_mutex_lock(&app
->sock_lock
);
3932 handle
= ustctl_tracepoint_list(app
->sock
);
3934 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3935 ERR("UST app list events getting handle failed for app pid %d",
3938 pthread_mutex_unlock(&app
->sock_lock
);
3942 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3943 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3944 /* Handle ustctl error. */
3948 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3949 ERR("UST app tp list get failed for app %d with ret %d",
3952 DBG3("UST app tp list get failed. Application is dead");
3954 * This is normal behavior, an application can die during the
3955 * creation process. Don't report an error so the execution can
3956 * continue normally. Continue normal execution.
3961 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3962 if (release_ret
< 0 &&
3963 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3964 release_ret
!= -EPIPE
) {
3965 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3967 pthread_mutex_unlock(&app
->sock_lock
);
3971 health_code_update();
3972 if (count
>= nbmem
) {
3973 /* In case the realloc fails, we free the memory */
3974 struct lttng_event
*new_tmp_event
;
3977 new_nbmem
= nbmem
<< 1;
3978 DBG2("Reallocating event list from %zu to %zu entries",
3980 new_tmp_event
= realloc(tmp_event
,
3981 new_nbmem
* sizeof(struct lttng_event
));
3982 if (new_tmp_event
== NULL
) {
3985 PERROR("realloc ust app events");
3988 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3989 if (release_ret
< 0 &&
3990 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3991 release_ret
!= -EPIPE
) {
3992 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3994 pthread_mutex_unlock(&app
->sock_lock
);
3997 /* Zero the new memory */
3998 memset(new_tmp_event
+ nbmem
, 0,
3999 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4001 tmp_event
= new_tmp_event
;
4003 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
4004 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4005 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
4006 tmp_event
[count
].pid
= app
->pid
;
4007 tmp_event
[count
].enabled
= -1;
4010 ret
= ustctl_release_handle(app
->sock
, handle
);
4011 pthread_mutex_unlock(&app
->sock_lock
);
4012 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4013 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4018 *events
= tmp_event
;
4020 DBG2("UST app list events done (%zu events)", count
);
4025 health_code_update();
4030 * Fill events array with all events name of all registered apps.
4032 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4035 size_t nbmem
, count
= 0;
4036 struct lttng_ht_iter iter
;
4037 struct ust_app
*app
;
4038 struct lttng_event_field
*tmp_event
;
4040 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4041 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
4042 if (tmp_event
== NULL
) {
4043 PERROR("zmalloc ust app event fields");
4050 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4051 struct lttng_ust_field_iter uiter
;
4053 health_code_update();
4055 if (!app
->compatible
) {
4057 * TODO: In time, we should notice the caller of this error by
4058 * telling him that this is a version error.
4062 pthread_mutex_lock(&app
->sock_lock
);
4063 handle
= ustctl_tracepoint_field_list(app
->sock
);
4065 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4066 ERR("UST app list field getting handle failed for app pid %d",
4069 pthread_mutex_unlock(&app
->sock_lock
);
4073 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
4074 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4075 /* Handle ustctl error. */
4079 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4080 ERR("UST app tp list field failed for app %d with ret %d",
4083 DBG3("UST app tp list field failed. Application is dead");
4085 * This is normal behavior, an application can die during the
4086 * creation process. Don't report an error so the execution can
4087 * continue normally. Reset list and count for next app.
4092 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4093 pthread_mutex_unlock(&app
->sock_lock
);
4094 if (release_ret
< 0 &&
4095 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4096 release_ret
!= -EPIPE
) {
4097 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4102 health_code_update();
4103 if (count
>= nbmem
) {
4104 /* In case the realloc fails, we free the memory */
4105 struct lttng_event_field
*new_tmp_event
;
4108 new_nbmem
= nbmem
<< 1;
4109 DBG2("Reallocating event field list from %zu to %zu entries",
4111 new_tmp_event
= realloc(tmp_event
,
4112 new_nbmem
* sizeof(struct lttng_event_field
));
4113 if (new_tmp_event
== NULL
) {
4116 PERROR("realloc ust app event fields");
4119 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4120 pthread_mutex_unlock(&app
->sock_lock
);
4122 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4123 release_ret
!= -EPIPE
) {
4124 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4128 /* Zero the new memory */
4129 memset(new_tmp_event
+ nbmem
, 0,
4130 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4132 tmp_event
= new_tmp_event
;
4135 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
4136 /* Mapping between these enums matches 1 to 1. */
4137 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4138 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4140 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
4141 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4142 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4143 tmp_event
[count
].event
.pid
= app
->pid
;
4144 tmp_event
[count
].event
.enabled
= -1;
4147 ret
= ustctl_release_handle(app
->sock
, handle
);
4148 pthread_mutex_unlock(&app
->sock_lock
);
4150 ret
!= -LTTNG_UST_ERR_EXITING
&&
4152 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4157 *fields
= tmp_event
;
4159 DBG2("UST app list event fields done (%zu events)", count
);
4164 health_code_update();
4169 * Free and clean all traceable apps of the global list.
4171 * Should _NOT_ be called with RCU read-side lock held.
4173 void ust_app_clean_list(void)
4176 struct ust_app
*app
;
4177 struct lttng_ht_iter iter
;
4179 DBG2("UST app cleaning registered apps hash table");
4183 /* Cleanup notify socket hash table */
4184 if (ust_app_ht_by_notify_sock
) {
4185 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4186 notify_sock_n
.node
) {
4187 struct cds_lfht_node
*node
;
4188 struct ust_app
*app
;
4190 node
= cds_lfht_iter_get_node(&iter
.iter
);
4195 app
= container_of(node
, struct ust_app
,
4196 notify_sock_n
.node
);
4197 ust_app_notify_sock_unregister(app
->notify_sock
);
4202 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4203 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4205 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4209 /* Cleanup socket hash table */
4210 if (ust_app_ht_by_sock
) {
4211 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4213 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4220 /* Destroy is done only when the ht is empty */
4222 ht_cleanup_push(ust_app_ht
);
4224 if (ust_app_ht_by_sock
) {
4225 ht_cleanup_push(ust_app_ht_by_sock
);
4227 if (ust_app_ht_by_notify_sock
) {
4228 ht_cleanup_push(ust_app_ht_by_notify_sock
);
4233 * Init UST app hash table.
4235 int ust_app_ht_alloc(void)
4237 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4241 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4242 if (!ust_app_ht_by_sock
) {
4245 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4246 if (!ust_app_ht_by_notify_sock
) {
4253 * For a specific UST session, disable the channel for all registered apps.
4255 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4256 struct ltt_ust_channel
*uchan
)
4259 struct lttng_ht_iter iter
;
4260 struct lttng_ht_node_str
*ua_chan_node
;
4261 struct ust_app
*app
;
4262 struct ust_app_session
*ua_sess
;
4263 struct ust_app_channel
*ua_chan
;
4265 assert(usess
->active
);
4266 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4267 uchan
->name
, usess
->id
);
4271 /* For every registered applications */
4272 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4273 struct lttng_ht_iter uiter
;
4274 if (!app
->compatible
) {
4276 * TODO: In time, we should notice the caller of this error by
4277 * telling him that this is a version error.
4281 ua_sess
= lookup_session_by_app(usess
, app
);
4282 if (ua_sess
== NULL
) {
4287 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4288 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4289 /* If the session if found for the app, the channel must be there */
4290 assert(ua_chan_node
);
4292 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4293 /* The channel must not be already disabled */
4294 assert(ua_chan
->enabled
== 1);
4296 /* Disable channel onto application */
4297 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4299 /* XXX: We might want to report this error at some point... */
4309 * For a specific UST session, enable the channel for all registered apps.
4311 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4312 struct ltt_ust_channel
*uchan
)
4315 struct lttng_ht_iter iter
;
4316 struct ust_app
*app
;
4317 struct ust_app_session
*ua_sess
;
4319 assert(usess
->active
);
4320 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4321 uchan
->name
, usess
->id
);
4325 /* For every registered applications */
4326 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4327 if (!app
->compatible
) {
4329 * TODO: In time, we should notice the caller of this error by
4330 * telling him that this is a version error.
4334 ua_sess
= lookup_session_by_app(usess
, app
);
4335 if (ua_sess
== NULL
) {
4339 /* Enable channel onto application */
4340 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4342 /* XXX: We might want to report this error at some point... */
4352 * Disable an event in a channel and for a specific session.
4354 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4355 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4358 struct lttng_ht_iter iter
, uiter
;
4359 struct lttng_ht_node_str
*ua_chan_node
;
4360 struct ust_app
*app
;
4361 struct ust_app_session
*ua_sess
;
4362 struct ust_app_channel
*ua_chan
;
4363 struct ust_app_event
*ua_event
;
4365 assert(usess
->active
);
4366 DBG("UST app disabling event %s for all apps in channel "
4367 "%s for session id %" PRIu64
,
4368 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4372 /* For all registered applications */
4373 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4374 if (!app
->compatible
) {
4376 * TODO: In time, we should notice the caller of this error by
4377 * telling him that this is a version error.
4381 ua_sess
= lookup_session_by_app(usess
, app
);
4382 if (ua_sess
== NULL
) {
4387 /* Lookup channel in the ust app session */
4388 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4389 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4390 if (ua_chan_node
== NULL
) {
4391 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4392 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4395 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4397 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4398 uevent
->filter
, uevent
->attr
.loglevel
,
4400 if (ua_event
== NULL
) {
4401 DBG2("Event %s not found in channel %s for app pid %d."
4402 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4406 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4408 /* XXX: Report error someday... */
4417 /* The ua_sess lock must be held by the caller. */
4419 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4420 struct ust_app_session
*ua_sess
,
4421 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4422 struct ust_app_channel
**_ua_chan
)
4425 struct ust_app_channel
*ua_chan
= NULL
;
4428 ASSERT_LOCKED(ua_sess
->lock
);
4430 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4431 sizeof(uchan
->name
))) {
4432 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4436 struct ltt_ust_context
*uctx
= NULL
;
4439 * Create channel onto application and synchronize its
4442 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4443 LTTNG_UST_CHAN_PER_CPU
, usess
,
4446 ret
= ust_app_channel_send(app
, usess
,
4453 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4454 ret
= create_ust_app_channel_context(ua_chan
,
4465 * The application's socket is not valid. Either a bad socket
4466 * or a timeout on it. We can't inform the caller that for a
4467 * specific app, the session failed so lets continue here.
4469 ret
= 0; /* Not an error. */
4477 if (ret
== 0 && _ua_chan
) {
4479 * Only return the application's channel on success. Note
4480 * that the channel can still be part of the application's
4481 * channel hashtable on error.
4483 *_ua_chan
= ua_chan
;
4489 * Enable event for a specific session and channel on the tracer.
4491 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4492 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4495 struct lttng_ht_iter iter
, uiter
;
4496 struct lttng_ht_node_str
*ua_chan_node
;
4497 struct ust_app
*app
;
4498 struct ust_app_session
*ua_sess
;
4499 struct ust_app_channel
*ua_chan
;
4500 struct ust_app_event
*ua_event
;
4502 assert(usess
->active
);
4503 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4504 uevent
->attr
.name
, usess
->id
);
4507 * NOTE: At this point, this function is called only if the session and
4508 * channel passed are already created for all apps. and enabled on the
4514 /* For all registered applications */
4515 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4516 if (!app
->compatible
) {
4518 * TODO: In time, we should notice the caller of this error by
4519 * telling him that this is a version error.
4523 ua_sess
= lookup_session_by_app(usess
, app
);
4525 /* The application has problem or is probably dead. */
4529 pthread_mutex_lock(&ua_sess
->lock
);
4531 if (ua_sess
->deleted
) {
4532 pthread_mutex_unlock(&ua_sess
->lock
);
4536 /* Lookup channel in the ust app session */
4537 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4538 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4540 * It is possible that the channel cannot be found is
4541 * the channel/event creation occurs concurrently with
4542 * an application exit.
4544 if (!ua_chan_node
) {
4545 pthread_mutex_unlock(&ua_sess
->lock
);
4549 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4551 /* Get event node */
4552 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4553 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4554 if (ua_event
== NULL
) {
4555 DBG3("UST app enable event %s not found for app PID %d."
4556 "Skipping app", uevent
->attr
.name
, app
->pid
);
4560 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4562 pthread_mutex_unlock(&ua_sess
->lock
);
4566 pthread_mutex_unlock(&ua_sess
->lock
);
4575 * For a specific existing UST session and UST channel, creates the event for
4576 * all registered apps.
4578 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4579 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4582 struct lttng_ht_iter iter
, uiter
;
4583 struct lttng_ht_node_str
*ua_chan_node
;
4584 struct ust_app
*app
;
4585 struct ust_app_session
*ua_sess
;
4586 struct ust_app_channel
*ua_chan
;
4588 assert(usess
->active
);
4589 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4590 uevent
->attr
.name
, usess
->id
);
4594 /* For all registered applications */
4595 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4596 if (!app
->compatible
) {
4598 * TODO: In time, we should notice the caller of this error by
4599 * telling him that this is a version error.
4603 ua_sess
= lookup_session_by_app(usess
, app
);
4605 /* The application has problem or is probably dead. */
4609 pthread_mutex_lock(&ua_sess
->lock
);
4611 if (ua_sess
->deleted
) {
4612 pthread_mutex_unlock(&ua_sess
->lock
);
4616 /* Lookup channel in the ust app session */
4617 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4618 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4619 /* If the channel is not found, there is a code flow error */
4620 assert(ua_chan_node
);
4622 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4624 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4625 pthread_mutex_unlock(&ua_sess
->lock
);
4627 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4628 /* Possible value at this point: -ENOMEM. If so, we stop! */
4631 DBG2("UST app event %s already exist on app PID %d",
4632 uevent
->attr
.name
, app
->pid
);
4642 * Start tracing for a specific UST session and app.
4644 * Called with UST app session lock held.
4648 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4651 struct ust_app_session
*ua_sess
;
4653 DBG("Starting tracing for ust app pid %d", app
->pid
);
4657 if (!app
->compatible
) {
4661 ua_sess
= lookup_session_by_app(usess
, app
);
4662 if (ua_sess
== NULL
) {
4663 /* The session is in teardown process. Ignore and continue. */
4667 pthread_mutex_lock(&ua_sess
->lock
);
4669 if (ua_sess
->deleted
) {
4670 pthread_mutex_unlock(&ua_sess
->lock
);
4674 if (ua_sess
->enabled
) {
4675 pthread_mutex_unlock(&ua_sess
->lock
);
4679 /* Upon restart, we skip the setup, already done */
4680 if (ua_sess
->started
) {
4685 * Create the metadata for the application. This returns gracefully if a
4686 * metadata was already set for the session.
4688 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4693 health_code_update();
4696 /* This starts the UST tracing */
4697 pthread_mutex_lock(&app
->sock_lock
);
4698 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4699 pthread_mutex_unlock(&app
->sock_lock
);
4701 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4702 ERR("Error starting tracing for app pid: %d (ret: %d)",
4705 DBG("UST app start session failed. Application is dead.");
4707 * This is normal behavior, an application can die during the
4708 * creation process. Don't report an error so the execution can
4709 * continue normally.
4711 pthread_mutex_unlock(&ua_sess
->lock
);
4717 /* Indicate that the session has been started once */
4718 ua_sess
->started
= 1;
4719 ua_sess
->enabled
= 1;
4721 pthread_mutex_unlock(&ua_sess
->lock
);
4723 health_code_update();
4725 /* Quiescent wait after starting trace */
4726 pthread_mutex_lock(&app
->sock_lock
);
4727 ret
= ustctl_wait_quiescent(app
->sock
);
4728 pthread_mutex_unlock(&app
->sock_lock
);
4729 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4730 ERR("UST app wait quiescent failed for app pid %d ret %d",
4736 health_code_update();
4740 pthread_mutex_unlock(&ua_sess
->lock
);
4742 health_code_update();
4747 * Stop tracing for a specific UST session and app.
4750 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4753 struct ust_app_session
*ua_sess
;
4754 struct ust_registry_session
*registry
;
4756 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4760 if (!app
->compatible
) {
4761 goto end_no_session
;
4764 ua_sess
= lookup_session_by_app(usess
, app
);
4765 if (ua_sess
== NULL
) {
4766 goto end_no_session
;
4769 pthread_mutex_lock(&ua_sess
->lock
);
4771 if (ua_sess
->deleted
) {
4772 pthread_mutex_unlock(&ua_sess
->lock
);
4773 goto end_no_session
;
4777 * If started = 0, it means that stop trace has been called for a session
4778 * that was never started. It's possible since we can have a fail start
4779 * from either the application manager thread or the command thread. Simply
4780 * indicate that this is a stop error.
4782 if (!ua_sess
->started
) {
4783 goto error_rcu_unlock
;
4786 health_code_update();
4788 /* This inhibits UST tracing */
4789 pthread_mutex_lock(&app
->sock_lock
);
4790 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4791 pthread_mutex_unlock(&app
->sock_lock
);
4793 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4794 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4797 DBG("UST app stop session failed. Application is dead.");
4799 * This is normal behavior, an application can die during the
4800 * creation process. Don't report an error so the execution can
4801 * continue normally.
4805 goto error_rcu_unlock
;
4808 health_code_update();
4809 ua_sess
->enabled
= 0;
4811 /* Quiescent wait after stopping trace */
4812 pthread_mutex_lock(&app
->sock_lock
);
4813 ret
= ustctl_wait_quiescent(app
->sock
);
4814 pthread_mutex_unlock(&app
->sock_lock
);
4815 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4816 ERR("UST app wait quiescent failed for app pid %d ret %d",
4820 health_code_update();
4822 registry
= get_session_registry(ua_sess
);
4824 /* The UST app session is held registry shall not be null. */
4827 /* Push metadata for application before freeing the application. */
4828 (void) push_metadata(registry
, ua_sess
->consumer
);
4831 pthread_mutex_unlock(&ua_sess
->lock
);
4834 health_code_update();
4838 pthread_mutex_unlock(&ua_sess
->lock
);
4840 health_code_update();
4845 int ust_app_flush_app_session(struct ust_app
*app
,
4846 struct ust_app_session
*ua_sess
)
4848 int ret
, retval
= 0;
4849 struct lttng_ht_iter iter
;
4850 struct ust_app_channel
*ua_chan
;
4851 struct consumer_socket
*socket
;
4853 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4857 if (!app
->compatible
) {
4858 goto end_not_compatible
;
4861 pthread_mutex_lock(&ua_sess
->lock
);
4863 if (ua_sess
->deleted
) {
4867 health_code_update();
4869 /* Flushing buffers */
4870 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4873 /* Flush buffers and push metadata. */
4874 switch (ua_sess
->buffer_type
) {
4875 case LTTNG_BUFFER_PER_PID
:
4876 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4878 health_code_update();
4879 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4881 ERR("Error flushing consumer channel");
4887 case LTTNG_BUFFER_PER_UID
:
4893 health_code_update();
4896 pthread_mutex_unlock(&ua_sess
->lock
);
4900 health_code_update();
4905 * Flush buffers for all applications for a specific UST session.
4906 * Called with UST session lock held.
4909 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4914 DBG("Flushing session buffers for all ust apps");
4918 /* Flush buffers and push metadata. */
4919 switch (usess
->buffer_type
) {
4920 case LTTNG_BUFFER_PER_UID
:
4922 struct buffer_reg_uid
*reg
;
4923 struct lttng_ht_iter iter
;
4925 /* Flush all per UID buffers associated to that session. */
4926 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4927 struct ust_registry_session
*ust_session_reg
;
4928 struct buffer_reg_channel
*reg_chan
;
4929 struct consumer_socket
*socket
;
4931 /* Get consumer socket to use to push the metadata.*/
4932 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4935 /* Ignore request if no consumer is found for the session. */
4939 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4940 reg_chan
, node
.node
) {
4942 * The following call will print error values so the return
4943 * code is of little importance because whatever happens, we
4944 * have to try them all.
4946 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4949 ust_session_reg
= reg
->registry
->reg
.ust
;
4950 /* Push metadata. */
4951 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4955 case LTTNG_BUFFER_PER_PID
:
4957 struct ust_app_session
*ua_sess
;
4958 struct lttng_ht_iter iter
;
4959 struct ust_app
*app
;
4961 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4962 ua_sess
= lookup_session_by_app(usess
, app
);
4963 if (ua_sess
== NULL
) {
4966 (void) ust_app_flush_app_session(app
, ua_sess
);
4977 health_code_update();
4982 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
4983 struct ust_app_session
*ua_sess
)
4986 struct lttng_ht_iter iter
;
4987 struct ust_app_channel
*ua_chan
;
4988 struct consumer_socket
*socket
;
4990 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
4994 if (!app
->compatible
) {
4995 goto end_not_compatible
;
4998 pthread_mutex_lock(&ua_sess
->lock
);
5000 if (ua_sess
->deleted
) {
5004 health_code_update();
5006 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5009 ERR("Failed to find consumer (%" PRIu32
") socket",
5010 app
->bits_per_long
);
5015 /* Clear quiescent state. */
5016 switch (ua_sess
->buffer_type
) {
5017 case LTTNG_BUFFER_PER_PID
:
5018 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5019 ua_chan
, node
.node
) {
5020 health_code_update();
5021 ret
= consumer_clear_quiescent_channel(socket
,
5024 ERR("Error clearing quiescent state for consumer channel");
5030 case LTTNG_BUFFER_PER_UID
:
5037 health_code_update();
5040 pthread_mutex_unlock(&ua_sess
->lock
);
5044 health_code_update();
5049 * Clear quiescent state in each stream for all applications for a
5050 * specific UST session.
5051 * Called with UST session lock held.
5054 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5059 DBG("Clearing stream quiescent state for all ust apps");
5063 switch (usess
->buffer_type
) {
5064 case LTTNG_BUFFER_PER_UID
:
5066 struct lttng_ht_iter iter
;
5067 struct buffer_reg_uid
*reg
;
5070 * Clear quiescent for all per UID buffers associated to
5073 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5074 struct consumer_socket
*socket
;
5075 struct buffer_reg_channel
*reg_chan
;
5077 /* Get associated consumer socket.*/
5078 socket
= consumer_find_socket_by_bitness(
5079 reg
->bits_per_long
, usess
->consumer
);
5082 * Ignore request if no consumer is found for
5088 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5089 &iter
.iter
, reg_chan
, node
.node
) {
5091 * The following call will print error values so
5092 * the return code is of little importance
5093 * because whatever happens, we have to try them
5096 (void) consumer_clear_quiescent_channel(socket
,
5097 reg_chan
->consumer_key
);
5102 case LTTNG_BUFFER_PER_PID
:
5104 struct ust_app_session
*ua_sess
;
5105 struct lttng_ht_iter iter
;
5106 struct ust_app
*app
;
5108 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5110 ua_sess
= lookup_session_by_app(usess
, app
);
5111 if (ua_sess
== NULL
) {
5114 (void) ust_app_clear_quiescent_app_session(app
,
5126 health_code_update();
5131 * Destroy a specific UST session in apps.
5133 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5136 struct ust_app_session
*ua_sess
;
5137 struct lttng_ht_iter iter
;
5138 struct lttng_ht_node_u64
*node
;
5140 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5144 if (!app
->compatible
) {
5148 __lookup_session_by_app(usess
, app
, &iter
);
5149 node
= lttng_ht_iter_get_node_u64(&iter
);
5151 /* Session is being or is deleted. */
5154 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
5156 health_code_update();
5157 destroy_app_session(app
, ua_sess
);
5159 health_code_update();
5161 /* Quiescent wait after stopping trace */
5162 pthread_mutex_lock(&app
->sock_lock
);
5163 ret
= ustctl_wait_quiescent(app
->sock
);
5164 pthread_mutex_unlock(&app
->sock_lock
);
5165 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5166 ERR("UST app wait quiescent failed for app pid %d ret %d",
5171 health_code_update();
5176 * Start tracing for the UST session.
5178 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5180 struct lttng_ht_iter iter
;
5181 struct ust_app
*app
;
5183 DBG("Starting all UST traces");
5186 * Even though the start trace might fail, flag this session active so
5187 * other application coming in are started by default.
5194 * In a start-stop-start use-case, we need to clear the quiescent state
5195 * of each channel set by the prior stop command, thus ensuring that a
5196 * following stop or destroy is sure to grab a timestamp_end near those
5197 * operations, even if the packet is empty.
5199 (void) ust_app_clear_quiescent_session(usess
);
5201 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5202 ust_app_global_update(usess
, app
);
5211 * Start tracing for the UST session.
5212 * Called with UST session lock held.
5214 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5217 struct lttng_ht_iter iter
;
5218 struct ust_app
*app
;
5220 DBG("Stopping all UST traces");
5223 * Even though the stop trace might fail, flag this session inactive so
5224 * other application coming in are not started by default.
5230 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5231 ret
= ust_app_stop_trace(usess
, app
);
5233 /* Continue to next apps even on error */
5238 (void) ust_app_flush_session(usess
);
5246 * Destroy app UST session.
5248 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5251 struct lttng_ht_iter iter
;
5252 struct ust_app
*app
;
5254 DBG("Destroy all UST traces");
5258 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5259 ret
= destroy_trace(usess
, app
);
5261 /* Continue to next apps even on error */
5271 /* The ua_sess lock must be held by the caller. */
5273 int find_or_create_ust_app_channel(
5274 struct ltt_ust_session
*usess
,
5275 struct ust_app_session
*ua_sess
,
5276 struct ust_app
*app
,
5277 struct ltt_ust_channel
*uchan
,
5278 struct ust_app_channel
**ua_chan
)
5281 struct lttng_ht_iter iter
;
5282 struct lttng_ht_node_str
*ua_chan_node
;
5284 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5285 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5287 *ua_chan
= caa_container_of(ua_chan_node
,
5288 struct ust_app_channel
, node
);
5292 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5301 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5302 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5303 struct ust_app
*app
)
5306 struct ust_app_event
*ua_event
= NULL
;
5308 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5309 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5311 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5316 if (ua_event
->enabled
!= uevent
->enabled
) {
5317 ret
= uevent
->enabled
?
5318 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5319 disable_ust_app_event(ua_sess
, ua_event
, app
);
5328 void ust_app_synchronize_tokens(struct ust_app
*app
)
5331 enum lttng_error_code ret_code
;
5332 enum lttng_trigger_status t_status
;
5333 struct lttng_ht_iter app_trigger_iter
;
5334 struct lttng_triggers
*triggers
;
5335 struct ust_app_token_event_rule
*token_event_rule_element
;
5339 /* TODO: is this necessary to protect against new trigger being added ?
5340 * notification_trigger_tokens_ht is still the backing data structure
5341 * for this listing. Leave it there for now.
5343 pthread_mutex_lock(¬ification_trigger_tokens_ht_lock
);
5344 ret_code
= notification_thread_command_get_tokens(
5345 notification_thread_handle
, &triggers
);
5346 if (ret_code
!= LTTNG_OK
) {
5353 t_status
= lttng_triggers_get_count(triggers
, &count
);
5354 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5359 for (unsigned int i
= 0; i
< count
; i
++) {
5360 struct lttng_condition
*condition
;
5361 struct lttng_event_rule
*event_rule
;
5362 struct lttng_trigger
*trigger
;
5363 struct ust_app_token_event_rule
*ua_token
;
5366 trigger
= lttng_triggers_get_pointer_of_index(triggers
, i
);
5369 /* TODO: error checking and type checking */
5370 token
= lttng_trigger_get_key(trigger
);
5371 condition
= lttng_trigger_get_condition(trigger
);
5372 (void) lttng_condition_event_rule_get_rule_no_const(condition
, &event_rule
);
5374 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5375 /* Skip kernel related trigger */
5379 /* Iterate over all known token trigger */
5380 ua_token
= find_ust_app_token_event_rule(app
->tokens_ht
, token
);
5382 ret
= create_ust_app_token_event_rule(event_rule
, app
, token
);
5389 /* Remove all unknown trigger from the app
5390 * TODO find a way better way then this, do it on the unregister command
5391 * and be specific on the token to remove instead of going over all
5392 * trigger known to the app. This is sub optimal.
5394 cds_lfht_for_each_entry (app
->tokens_ht
->ht
, &app_trigger_iter
.iter
,
5395 token_event_rule_element
, node
.node
) {
5399 token
= token_event_rule_element
->token
;
5402 * Check if the app event trigger still exists on the
5403 * notification side.
5404 * TODO: might want to change the backing data struct of the
5405 * lttng_triggers object to allow quick lookup?
5406 * For kernel mostly all of this can be removed once we delete
5407 * on a per trigger basis.
5410 for (unsigned int i
= 0; i
< count
; i
++) {
5411 struct lttng_trigger
*trigger
;
5412 uint64_t inner_token
;
5414 trigger
= lttng_triggers_get_pointer_of_index(
5418 inner_token
= lttng_trigger_get_key(trigger
);
5420 if (inner_token
== token
) {
5431 /* TODO: This is fucking ugly API for fuck sake */
5432 assert(!lttng_ht_del(app
->tokens_ht
, &app_trigger_iter
));
5434 (void) disable_ust_object(app
, token_event_rule_element
->obj
);
5436 delete_ust_app_token_event_rule(app
->sock
, token_event_rule_element
, app
);
5440 pthread_mutex_unlock(¬ification_trigger_tokens_ht_lock
);
5445 * The caller must ensure that the application is compatible and is tracked
5446 * by the PID tracker.
5449 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5450 struct ust_app
*app
)
5453 struct cds_lfht_iter uchan_iter
;
5454 struct ltt_ust_channel
*uchan
;
5455 struct ust_app_session
*ua_sess
= NULL
;
5458 * The application's configuration should only be synchronized for
5461 assert(usess
->active
);
5463 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5465 /* Tracer is probably gone or ENOMEM. */
5470 pthread_mutex_lock(&ua_sess
->lock
);
5471 if (ua_sess
->deleted
) {
5472 pthread_mutex_unlock(&ua_sess
->lock
);
5477 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5479 struct ust_app_channel
*ua_chan
;
5480 struct cds_lfht_iter uevent_iter
;
5481 struct ltt_ust_event
*uevent
;
5484 * Search for a matching ust_app_channel. If none is found,
5485 * create it. Creating the channel will cause the ua_chan
5486 * structure to be allocated, the channel buffers to be
5487 * allocated (if necessary) and sent to the application, and
5488 * all enabled contexts will be added to the channel.
5490 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5491 app
, uchan
, &ua_chan
);
5493 /* Tracer is probably gone or ENOMEM. */
5498 /* ua_chan will be NULL for the metadata channel */
5502 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5504 ret
= ust_app_channel_synchronize_event(ua_chan
,
5505 uevent
, ua_sess
, app
);
5511 if (ua_chan
->enabled
!= uchan
->enabled
) {
5512 ret
= uchan
->enabled
?
5513 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5514 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5523 pthread_mutex_unlock(&ua_sess
->lock
);
5524 /* Everything went well at this point. */
5529 pthread_mutex_unlock(&ua_sess
->lock
);
5532 destroy_app_session(app
, ua_sess
);
5538 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5540 struct ust_app_session
*ua_sess
;
5542 ua_sess
= lookup_session_by_app(usess
, app
);
5543 if (ua_sess
== NULL
) {
5546 destroy_app_session(app
, ua_sess
);
5550 * Add channels/events from UST global domain to registered apps at sock.
5552 * Called with session lock held.
5553 * Called with RCU read-side lock held.
5555 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5558 assert(usess
->active
);
5560 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5561 app
->sock
, usess
->id
);
5563 if (!app
->compatible
) {
5566 if (trace_ust_id_tracker_lookup(LTTNG_TRACKER_VPID
, usess
, app
->pid
) &&
5567 trace_ust_id_tracker_lookup(
5568 LTTNG_TRACKER_VUID
, usess
, app
->uid
) &&
5569 trace_ust_id_tracker_lookup(
5570 LTTNG_TRACKER_VGID
, usess
, app
->gid
)) {
5572 * Synchronize the application's internal tracing configuration
5573 * and start tracing.
5575 ust_app_synchronize(usess
, app
);
5576 ust_app_start_trace(usess
, app
);
5578 ust_app_global_destroy(usess
, app
);
5582 void ust_app_global_update_tokens(struct ust_app
*app
)
5584 DBG2("UST app global update token for app sock %d", app
->sock
);
5586 if (!app
->compatible
) {
5589 ust_app_synchronize_tokens(app
);
5593 * Called with session lock held.
5595 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5597 struct lttng_ht_iter iter
;
5598 struct ust_app
*app
;
5601 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5602 ust_app_global_update(usess
, app
);
5607 void ust_app_global_update_all_tokens(void)
5609 struct lttng_ht_iter iter
;
5610 struct ust_app
*app
;
5613 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5614 ust_app_global_update_tokens(app
);
5620 * Add context to a specific channel for global UST domain.
5622 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5623 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5626 struct lttng_ht_node_str
*ua_chan_node
;
5627 struct lttng_ht_iter iter
, uiter
;
5628 struct ust_app_channel
*ua_chan
= NULL
;
5629 struct ust_app_session
*ua_sess
;
5630 struct ust_app
*app
;
5632 assert(usess
->active
);
5635 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5636 if (!app
->compatible
) {
5638 * TODO: In time, we should notice the caller of this error by
5639 * telling him that this is a version error.
5643 ua_sess
= lookup_session_by_app(usess
, app
);
5644 if (ua_sess
== NULL
) {
5648 pthread_mutex_lock(&ua_sess
->lock
);
5650 if (ua_sess
->deleted
) {
5651 pthread_mutex_unlock(&ua_sess
->lock
);
5655 /* Lookup channel in the ust app session */
5656 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5657 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5658 if (ua_chan_node
== NULL
) {
5661 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5663 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5668 pthread_mutex_unlock(&ua_sess
->lock
);
5676 * Receive registration and populate the given msg structure.
5678 * On success return 0 else a negative value returned by the ustctl call.
5680 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5683 uint32_t pid
, ppid
, uid
, gid
;
5687 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5688 &pid
, &ppid
, &uid
, &gid
,
5689 &msg
->bits_per_long
,
5690 &msg
->uint8_t_alignment
,
5691 &msg
->uint16_t_alignment
,
5692 &msg
->uint32_t_alignment
,
5693 &msg
->uint64_t_alignment
,
5694 &msg
->long_alignment
,
5701 case LTTNG_UST_ERR_EXITING
:
5702 DBG3("UST app recv reg message failed. Application died");
5704 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5705 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5706 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5707 LTTNG_UST_ABI_MINOR_VERSION
);
5710 ERR("UST app recv reg message failed with ret %d", ret
);
5715 msg
->pid
= (pid_t
) pid
;
5716 msg
->ppid
= (pid_t
) ppid
;
5717 msg
->uid
= (uid_t
) uid
;
5718 msg
->gid
= (gid_t
) gid
;
5725 * Return a ust app session object using the application object and the
5726 * session object descriptor has a key. If not found, NULL is returned.
5727 * A RCU read side lock MUST be acquired when calling this function.
5729 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5732 struct lttng_ht_node_ulong
*node
;
5733 struct lttng_ht_iter iter
;
5734 struct ust_app_session
*ua_sess
= NULL
;
5738 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5739 node
= lttng_ht_iter_get_node_ulong(&iter
);
5741 DBG2("UST app session find by objd %d not found", objd
);
5745 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5752 * Return a ust app channel object using the application object and the channel
5753 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5754 * lock MUST be acquired before calling this function.
5756 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5759 struct lttng_ht_node_ulong
*node
;
5760 struct lttng_ht_iter iter
;
5761 struct ust_app_channel
*ua_chan
= NULL
;
5765 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5766 node
= lttng_ht_iter_get_node_ulong(&iter
);
5768 DBG2("UST app channel find by objd %d not found", objd
);
5772 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5779 * Reply to a register channel notification from an application on the notify
5780 * socket. The channel metadata is also created.
5782 * The session UST registry lock is acquired in this function.
5784 * On success 0 is returned else a negative value.
5786 static int reply_ust_register_channel(int sock
, int cobjd
,
5787 size_t nr_fields
, struct ustctl_field
*fields
)
5789 int ret
, ret_code
= 0;
5791 uint64_t chan_reg_key
;
5792 enum ustctl_channel_header type
;
5793 struct ust_app
*app
;
5794 struct ust_app_channel
*ua_chan
;
5795 struct ust_app_session
*ua_sess
;
5796 struct ust_registry_session
*registry
;
5797 struct ust_registry_channel
*chan_reg
;
5801 /* Lookup application. If not found, there is a code flow error. */
5802 app
= find_app_by_notify_sock(sock
);
5804 DBG("Application socket %d is being torn down. Abort event notify",
5807 goto error_rcu_unlock
;
5810 /* Lookup channel by UST object descriptor. */
5811 ua_chan
= find_channel_by_objd(app
, cobjd
);
5813 DBG("Application channel is being torn down. Abort event notify");
5815 goto error_rcu_unlock
;
5818 assert(ua_chan
->session
);
5819 ua_sess
= ua_chan
->session
;
5821 /* Get right session registry depending on the session buffer type. */
5822 registry
= get_session_registry(ua_sess
);
5824 DBG("Application session is being torn down. Abort event notify");
5826 goto error_rcu_unlock
;
5829 /* Depending on the buffer type, a different channel key is used. */
5830 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5831 chan_reg_key
= ua_chan
->tracing_channel_id
;
5833 chan_reg_key
= ua_chan
->key
;
5836 pthread_mutex_lock(®istry
->lock
);
5838 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5841 if (!chan_reg
->register_done
) {
5843 * TODO: eventually use the registry event count for
5844 * this channel to better guess header type for per-pid
5847 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5848 chan_reg
->nr_ctx_fields
= nr_fields
;
5849 chan_reg
->ctx_fields
= fields
;
5851 chan_reg
->header_type
= type
;
5853 /* Get current already assigned values. */
5854 type
= chan_reg
->header_type
;
5856 /* Channel id is set during the object creation. */
5857 chan_id
= chan_reg
->chan_id
;
5859 /* Append to metadata */
5860 if (!chan_reg
->metadata_dumped
) {
5861 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5863 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5869 DBG3("UST app replying to register channel key %" PRIu64
5870 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5873 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5875 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5876 ERR("UST app reply channel failed with ret %d", ret
);
5878 DBG3("UST app reply channel failed. Application died");
5883 /* This channel registry registration is completed. */
5884 chan_reg
->register_done
= 1;
5887 pthread_mutex_unlock(®istry
->lock
);
5895 * Add event to the UST channel registry. When the event is added to the
5896 * registry, the metadata is also created. Once done, this replies to the
5897 * application with the appropriate error code.
5899 * The session UST registry lock is acquired in the function.
5901 * On success 0 is returned else a negative value.
5903 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5904 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5905 int loglevel_value
, char *model_emf_uri
)
5908 uint32_t event_id
= 0;
5909 uint64_t chan_reg_key
;
5910 struct ust_app
*app
;
5911 struct ust_app_channel
*ua_chan
;
5912 struct ust_app_session
*ua_sess
;
5913 struct ust_registry_session
*registry
;
5917 /* Lookup application. If not found, there is a code flow error. */
5918 app
= find_app_by_notify_sock(sock
);
5920 DBG("Application socket %d is being torn down. Abort event notify",
5923 goto error_rcu_unlock
;
5926 /* Lookup channel by UST object descriptor. */
5927 ua_chan
= find_channel_by_objd(app
, cobjd
);
5929 DBG("Application channel is being torn down. Abort event notify");
5931 goto error_rcu_unlock
;
5934 assert(ua_chan
->session
);
5935 ua_sess
= ua_chan
->session
;
5937 registry
= get_session_registry(ua_sess
);
5939 DBG("Application session is being torn down. Abort event notify");
5941 goto error_rcu_unlock
;
5944 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5945 chan_reg_key
= ua_chan
->tracing_channel_id
;
5947 chan_reg_key
= ua_chan
->key
;
5950 pthread_mutex_lock(®istry
->lock
);
5953 * From this point on, this call acquires the ownership of the sig, fields
5954 * and model_emf_uri meaning any free are done inside it if needed. These
5955 * three variables MUST NOT be read/write after this.
5957 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5958 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5959 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5963 model_emf_uri
= NULL
;
5966 * The return value is returned to ustctl so in case of an error, the
5967 * application can be notified. In case of an error, it's important not to
5968 * return a negative error or else the application will get closed.
5970 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5972 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5973 ERR("UST app reply event failed with ret %d", ret
);
5975 DBG3("UST app reply event failed. Application died");
5978 * No need to wipe the create event since the application socket will
5979 * get close on error hence cleaning up everything by itself.
5984 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5988 pthread_mutex_unlock(®istry
->lock
);
5993 free(model_emf_uri
);
5998 * Add enum to the UST session registry. Once done, this replies to the
5999 * application with the appropriate error code.
6001 * The session UST registry lock is acquired within this function.
6003 * On success 0 is returned else a negative value.
6005 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
6006 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
6008 int ret
= 0, ret_code
;
6009 struct ust_app
*app
;
6010 struct ust_app_session
*ua_sess
;
6011 struct ust_registry_session
*registry
;
6012 uint64_t enum_id
= -1ULL;
6016 /* Lookup application. If not found, there is a code flow error. */
6017 app
= find_app_by_notify_sock(sock
);
6019 /* Return an error since this is not an error */
6020 DBG("Application socket %d is being torn down. Aborting enum registration",
6023 goto error_rcu_unlock
;
6026 /* Lookup session by UST object descriptor. */
6027 ua_sess
= find_session_by_objd(app
, sobjd
);
6029 /* Return an error since this is not an error */
6030 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6032 goto error_rcu_unlock
;
6035 registry
= get_session_registry(ua_sess
);
6037 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6039 goto error_rcu_unlock
;
6042 pthread_mutex_lock(®istry
->lock
);
6045 * From this point on, the callee acquires the ownership of
6046 * entries. The variable entries MUST NOT be read/written after
6049 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
6050 entries
, nr_entries
, &enum_id
);
6054 * The return value is returned to ustctl so in case of an error, the
6055 * application can be notified. In case of an error, it's important not to
6056 * return a negative error or else the application will get closed.
6058 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
6060 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6061 ERR("UST app reply enum failed with ret %d", ret
);
6063 DBG3("UST app reply enum failed. Application died");
6066 * No need to wipe the create enum since the application socket will
6067 * get close on error hence cleaning up everything by itself.
6072 DBG3("UST registry enum %s added successfully or already found", name
);
6075 pthread_mutex_unlock(®istry
->lock
);
6082 * Handle application notification through the given notify socket.
6084 * Return 0 on success or else a negative value.
6086 int ust_app_recv_notify(int sock
)
6089 enum ustctl_notify_cmd cmd
;
6091 DBG3("UST app receiving notify from sock %d", sock
);
6093 ret
= ustctl_recv_notify(sock
, &cmd
);
6095 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6096 ERR("UST app recv notify failed with ret %d", ret
);
6098 DBG3("UST app recv notify failed. Application died");
6104 case USTCTL_NOTIFY_CMD_EVENT
:
6106 int sobjd
, cobjd
, loglevel_value
;
6107 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6109 struct ustctl_field
*fields
;
6111 DBG2("UST app ustctl register event received");
6113 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
6114 &loglevel_value
, &sig
, &nr_fields
, &fields
,
6117 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6118 ERR("UST app recv event failed with ret %d", ret
);
6120 DBG3("UST app recv event failed. Application died");
6126 * Add event to the UST registry coming from the notify socket. This
6127 * call will free if needed the sig, fields and model_emf_uri. This
6128 * code path loses the ownsership of these variables and transfer them
6129 * to the this function.
6131 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6132 fields
, loglevel_value
, model_emf_uri
);
6139 case USTCTL_NOTIFY_CMD_CHANNEL
:
6143 struct ustctl_field
*fields
;
6145 DBG2("UST app ustctl register channel received");
6147 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
6150 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6151 ERR("UST app recv channel failed with ret %d", ret
);
6153 DBG3("UST app recv channel failed. Application died");
6159 * The fields ownership are transfered to this function call meaning
6160 * that if needed it will be freed. After this, it's invalid to access
6161 * fields or clean it up.
6163 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
6171 case USTCTL_NOTIFY_CMD_ENUM
:
6174 char name
[LTTNG_UST_SYM_NAME_LEN
];
6176 struct ustctl_enum_entry
*entries
;
6178 DBG2("UST app ustctl register enum received");
6180 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
6181 &entries
, &nr_entries
);
6183 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6184 ERR("UST app recv enum failed with ret %d", ret
);
6186 DBG3("UST app recv enum failed. Application died");
6191 /* Callee assumes ownership of entries */
6192 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6193 entries
, nr_entries
);
6201 /* Should NEVER happen. */
6210 * Once the notify socket hangs up, this is called. First, it tries to find the
6211 * corresponding application. On failure, the call_rcu to close the socket is
6212 * executed. If an application is found, it tries to delete it from the notify
6213 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6215 * Note that an object needs to be allocated here so on ENOMEM failure, the
6216 * call RCU is not done but the rest of the cleanup is.
6218 void ust_app_notify_sock_unregister(int sock
)
6221 struct lttng_ht_iter iter
;
6222 struct ust_app
*app
;
6223 struct ust_app_notify_sock_obj
*obj
;
6229 obj
= zmalloc(sizeof(*obj
));
6232 * An ENOMEM is kind of uncool. If this strikes we continue the
6233 * procedure but the call_rcu will not be called. In this case, we
6234 * accept the fd leak rather than possibly creating an unsynchronized
6235 * state between threads.
6237 * TODO: The notify object should be created once the notify socket is
6238 * registered and stored independantely from the ust app object. The
6239 * tricky part is to synchronize the teardown of the application and
6240 * this notify object. Let's keep that in mind so we can avoid this
6241 * kind of shenanigans with ENOMEM in the teardown path.
6248 DBG("UST app notify socket unregister %d", sock
);
6251 * Lookup application by notify socket. If this fails, this means that the
6252 * hash table delete has already been done by the application
6253 * unregistration process so we can safely close the notify socket in a
6256 app
= find_app_by_notify_sock(sock
);
6261 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6264 * Whatever happens here either we fail or succeed, in both cases we have
6265 * to close the socket after a grace period to continue to the call RCU
6266 * here. If the deletion is successful, the application is not visible
6267 * anymore by other threads and is it fails it means that it was already
6268 * deleted from the hash table so either way we just have to close the
6271 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6277 * Close socket after a grace period to avoid for the socket to be reused
6278 * before the application object is freed creating potential race between
6279 * threads trying to add unique in the global hash table.
6282 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6287 * Destroy a ust app data structure and free its memory.
6289 void ust_app_destroy(struct ust_app
*app
)
6295 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6299 * Take a snapshot for a given UST session. The snapshot is sent to the given
6302 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6304 enum lttng_error_code
ust_app_snapshot_record(
6305 const struct ltt_ust_session
*usess
,
6306 const struct consumer_output
*output
, int wait
,
6307 uint64_t nb_packets_per_stream
)
6310 enum lttng_error_code status
= LTTNG_OK
;
6311 struct lttng_ht_iter iter
;
6312 struct ust_app
*app
;
6313 char *trace_path
= NULL
;
6320 switch (usess
->buffer_type
) {
6321 case LTTNG_BUFFER_PER_UID
:
6323 struct buffer_reg_uid
*reg
;
6325 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6326 struct buffer_reg_channel
*reg_chan
;
6327 struct consumer_socket
*socket
;
6328 char pathname
[PATH_MAX
];
6329 size_t consumer_path_offset
= 0;
6331 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6332 /* Skip since no metadata is present */
6336 /* Get consumer socket to use to push the metadata.*/
6337 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6340 status
= LTTNG_ERR_INVALID
;
6344 memset(pathname
, 0, sizeof(pathname
));
6345 ret
= snprintf(pathname
, sizeof(pathname
),
6346 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
6347 reg
->uid
, reg
->bits_per_long
);
6349 PERROR("snprintf snapshot path");
6350 status
= LTTNG_ERR_INVALID
;
6353 /* Free path allowed on previous iteration. */
6355 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6356 &consumer_path_offset
);
6358 status
= LTTNG_ERR_INVALID
;
6361 /* Add the UST default trace dir to path. */
6362 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6363 reg_chan
, node
.node
) {
6364 status
= consumer_snapshot_channel(socket
,
6365 reg_chan
->consumer_key
,
6366 output
, 0, usess
->uid
,
6367 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
6368 nb_packets_per_stream
);
6369 if (status
!= LTTNG_OK
) {
6373 status
= consumer_snapshot_channel(socket
,
6374 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6375 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6377 if (status
!= LTTNG_OK
) {
6383 case LTTNG_BUFFER_PER_PID
:
6385 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6386 struct consumer_socket
*socket
;
6387 struct lttng_ht_iter chan_iter
;
6388 struct ust_app_channel
*ua_chan
;
6389 struct ust_app_session
*ua_sess
;
6390 struct ust_registry_session
*registry
;
6391 char pathname
[PATH_MAX
];
6392 size_t consumer_path_offset
= 0;
6394 ua_sess
= lookup_session_by_app(usess
, app
);
6396 /* Session not associated with this app. */
6400 /* Get the right consumer socket for the application. */
6401 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6404 status
= LTTNG_ERR_INVALID
;
6408 /* Add the UST default trace dir to path. */
6409 memset(pathname
, 0, sizeof(pathname
));
6410 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
6413 status
= LTTNG_ERR_INVALID
;
6414 PERROR("snprintf snapshot path");
6417 /* Free path allowed on previous iteration. */
6419 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6420 &consumer_path_offset
);
6422 status
= LTTNG_ERR_INVALID
;
6425 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6426 ua_chan
, node
.node
) {
6427 status
= consumer_snapshot_channel(socket
,
6428 ua_chan
->key
, output
, 0,
6429 ua_sess
->effective_credentials
6431 ua_sess
->effective_credentials
6433 &trace_path
[consumer_path_offset
], wait
,
6434 nb_packets_per_stream
);
6438 case LTTNG_ERR_CHAN_NOT_FOUND
:
6445 registry
= get_session_registry(ua_sess
);
6447 DBG("Application session is being torn down. Skip application.");
6450 status
= consumer_snapshot_channel(socket
,
6451 registry
->metadata_key
, output
, 1,
6452 ua_sess
->effective_credentials
.uid
,
6453 ua_sess
->effective_credentials
.gid
,
6454 &trace_path
[consumer_path_offset
], wait
, 0);
6458 case LTTNG_ERR_CHAN_NOT_FOUND
:
6478 * Return the size taken by one more packet per stream.
6480 uint64_t ust_app_get_size_one_more_packet_per_stream(
6481 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6483 uint64_t tot_size
= 0;
6484 struct ust_app
*app
;
6485 struct lttng_ht_iter iter
;
6489 switch (usess
->buffer_type
) {
6490 case LTTNG_BUFFER_PER_UID
:
6492 struct buffer_reg_uid
*reg
;
6494 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6495 struct buffer_reg_channel
*reg_chan
;
6498 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6499 reg_chan
, node
.node
) {
6500 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6502 * Don't take channel into account if we
6503 * already grab all its packets.
6507 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6513 case LTTNG_BUFFER_PER_PID
:
6516 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6517 struct ust_app_channel
*ua_chan
;
6518 struct ust_app_session
*ua_sess
;
6519 struct lttng_ht_iter chan_iter
;
6521 ua_sess
= lookup_session_by_app(usess
, app
);
6523 /* Session not associated with this app. */
6527 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6528 ua_chan
, node
.node
) {
6529 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6531 * Don't take channel into account if we
6532 * already grab all its packets.
6536 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6550 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6551 struct cds_list_head
*buffer_reg_uid_list
,
6552 struct consumer_output
*consumer
, uint64_t uchan_id
,
6553 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6556 uint64_t consumer_chan_key
;
6561 ret
= buffer_reg_uid_consumer_channel_key(
6562 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6570 ret
= consumer_get_lost_packets(ust_session_id
,
6571 consumer_chan_key
, consumer
, lost
);
6573 ret
= consumer_get_discarded_events(ust_session_id
,
6574 consumer_chan_key
, consumer
, discarded
);
6581 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6582 struct ltt_ust_channel
*uchan
,
6583 struct consumer_output
*consumer
, int overwrite
,
6584 uint64_t *discarded
, uint64_t *lost
)
6587 struct lttng_ht_iter iter
;
6588 struct lttng_ht_node_str
*ua_chan_node
;
6589 struct ust_app
*app
;
6590 struct ust_app_session
*ua_sess
;
6591 struct ust_app_channel
*ua_chan
;
6598 * Iterate over every registered applications. Sum counters for
6599 * all applications containing requested session and channel.
6601 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6602 struct lttng_ht_iter uiter
;
6604 ua_sess
= lookup_session_by_app(usess
, app
);
6605 if (ua_sess
== NULL
) {
6610 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6611 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6612 /* If the session is found for the app, the channel must be there */
6613 assert(ua_chan_node
);
6615 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6620 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6627 uint64_t _discarded
;
6629 ret
= consumer_get_discarded_events(usess
->id
,
6630 ua_chan
->key
, consumer
, &_discarded
);
6634 (*discarded
) += _discarded
;
6643 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6644 struct ust_app
*app
)
6647 struct ust_app_session
*ua_sess
;
6649 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6653 ua_sess
= lookup_session_by_app(usess
, app
);
6654 if (ua_sess
== NULL
) {
6655 /* The session is in teardown process. Ignore and continue. */
6659 pthread_mutex_lock(&ua_sess
->lock
);
6661 if (ua_sess
->deleted
) {
6665 pthread_mutex_lock(&app
->sock_lock
);
6666 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6667 pthread_mutex_unlock(&app
->sock_lock
);
6670 pthread_mutex_unlock(&ua_sess
->lock
);
6674 health_code_update();
6679 * Regenerate the statedump for each app in the session.
6681 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6684 struct lttng_ht_iter iter
;
6685 struct ust_app
*app
;
6687 DBG("Regenerating the metadata for all UST apps");
6691 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6692 if (!app
->compatible
) {
6696 ret
= ust_app_regenerate_statedump(usess
, app
);
6698 /* Continue to the next app even on error */
6709 * Rotate all the channels of a session.
6711 * Return LTTNG_OK on success or else an LTTng error code.
6713 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
6716 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6717 struct lttng_ht_iter iter
;
6718 struct ust_app
*app
;
6719 struct ltt_ust_session
*usess
= session
->ust_session
;
6725 switch (usess
->buffer_type
) {
6726 case LTTNG_BUFFER_PER_UID
:
6728 struct buffer_reg_uid
*reg
;
6730 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6731 struct buffer_reg_channel
*reg_chan
;
6732 struct consumer_socket
*socket
;
6734 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6735 /* Skip since no metadata is present */
6739 /* Get consumer socket to use to push the metadata.*/
6740 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6743 cmd_ret
= LTTNG_ERR_INVALID
;
6747 /* Rotate the data channels. */
6748 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6749 reg_chan
, node
.node
) {
6750 ret
= consumer_rotate_channel(socket
,
6751 reg_chan
->consumer_key
,
6752 usess
->uid
, usess
->gid
,
6754 /* is_metadata_channel */ false);
6756 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6761 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6763 ret
= consumer_rotate_channel(socket
,
6764 reg
->registry
->reg
.ust
->metadata_key
,
6765 usess
->uid
, usess
->gid
,
6767 /* is_metadata_channel */ true);
6769 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6775 case LTTNG_BUFFER_PER_PID
:
6777 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6778 struct consumer_socket
*socket
;
6779 struct lttng_ht_iter chan_iter
;
6780 struct ust_app_channel
*ua_chan
;
6781 struct ust_app_session
*ua_sess
;
6782 struct ust_registry_session
*registry
;
6784 ua_sess
= lookup_session_by_app(usess
, app
);
6786 /* Session not associated with this app. */
6790 /* Get the right consumer socket for the application. */
6791 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6794 cmd_ret
= LTTNG_ERR_INVALID
;
6798 registry
= get_session_registry(ua_sess
);
6800 DBG("Application session is being torn down. Skip application.");
6804 /* Rotate the data channels. */
6805 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6806 ua_chan
, node
.node
) {
6807 ret
= consumer_rotate_channel(socket
,
6809 ua_sess
->effective_credentials
6811 ua_sess
->effective_credentials
6814 /* is_metadata_channel */ false);
6816 /* Per-PID buffer and application going away. */
6817 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6819 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6824 /* Rotate the metadata channel. */
6825 (void) push_metadata(registry
, usess
->consumer
);
6826 ret
= consumer_rotate_channel(socket
,
6827 registry
->metadata_key
,
6828 ua_sess
->effective_credentials
.uid
,
6829 ua_sess
->effective_credentials
.gid
,
6831 /* is_metadata_channel */ true);
6833 /* Per-PID buffer and application going away. */
6834 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
6836 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6854 enum lttng_error_code
ust_app_create_channel_subdirectories(
6855 const struct ltt_ust_session
*usess
)
6857 enum lttng_error_code ret
= LTTNG_OK
;
6858 struct lttng_ht_iter iter
;
6859 enum lttng_trace_chunk_status chunk_status
;
6860 char *pathname_index
;
6863 assert(usess
->current_trace_chunk
);
6866 switch (usess
->buffer_type
) {
6867 case LTTNG_BUFFER_PER_UID
:
6869 struct buffer_reg_uid
*reg
;
6871 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6872 fmt_ret
= asprintf(&pathname_index
,
6873 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
6874 reg
->uid
, reg
->bits_per_long
);
6876 ERR("Failed to format channel index directory");
6877 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6882 * Create the index subdirectory which will take care
6883 * of implicitly creating the channel's path.
6885 chunk_status
= lttng_trace_chunk_create_subdirectory(
6886 usess
->current_trace_chunk
,
6888 free(pathname_index
);
6889 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6890 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6896 case LTTNG_BUFFER_PER_PID
:
6898 struct ust_app
*app
;
6901 * Create the toplevel ust/ directory in case no apps are running.
6903 chunk_status
= lttng_trace_chunk_create_subdirectory(
6904 usess
->current_trace_chunk
,
6905 DEFAULT_UST_TRACE_DIR
);
6906 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6907 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6911 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
6913 struct ust_app_session
*ua_sess
;
6914 struct ust_registry_session
*registry
;
6916 ua_sess
= lookup_session_by_app(usess
, app
);
6918 /* Session not associated with this app. */
6922 registry
= get_session_registry(ua_sess
);
6924 DBG("Application session is being torn down. Skip application.");
6928 fmt_ret
= asprintf(&pathname_index
,
6929 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
6932 ERR("Failed to format channel index directory");
6933 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6937 * Create the index subdirectory which will take care
6938 * of implicitly creating the channel's path.
6940 chunk_status
= lttng_trace_chunk_create_subdirectory(
6941 usess
->current_trace_chunk
,
6943 free(pathname_index
);
6944 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
6945 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
6962 * Clear all the channels of a session.
6964 * Return LTTNG_OK on success or else an LTTng error code.
6966 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
6969 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6970 struct lttng_ht_iter iter
;
6971 struct ust_app
*app
;
6972 struct ltt_ust_session
*usess
= session
->ust_session
;
6978 if (usess
->active
) {
6979 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
6980 cmd_ret
= LTTNG_ERR_FATAL
;
6984 switch (usess
->buffer_type
) {
6985 case LTTNG_BUFFER_PER_UID
:
6987 struct buffer_reg_uid
*reg
;
6989 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6990 struct buffer_reg_channel
*reg_chan
;
6991 struct consumer_socket
*socket
;
6993 /* Get consumer socket to use to push the metadata.*/
6994 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6997 cmd_ret
= LTTNG_ERR_INVALID
;
7001 /* Clear the data channels. */
7002 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7003 reg_chan
, node
.node
) {
7004 ret
= consumer_clear_channel(socket
,
7005 reg_chan
->consumer_key
);
7011 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7014 * Clear the metadata channel.
7015 * Metadata channel is not cleared per se but we still need to
7016 * perform a rotation operation on it behind the scene.
7018 ret
= consumer_clear_channel(socket
,
7019 reg
->registry
->reg
.ust
->metadata_key
);
7026 case LTTNG_BUFFER_PER_PID
:
7028 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7029 struct consumer_socket
*socket
;
7030 struct lttng_ht_iter chan_iter
;
7031 struct ust_app_channel
*ua_chan
;
7032 struct ust_app_session
*ua_sess
;
7033 struct ust_registry_session
*registry
;
7035 ua_sess
= lookup_session_by_app(usess
, app
);
7037 /* Session not associated with this app. */
7041 /* Get the right consumer socket for the application. */
7042 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7045 cmd_ret
= LTTNG_ERR_INVALID
;
7049 registry
= get_session_registry(ua_sess
);
7051 DBG("Application session is being torn down. Skip application.");
7055 /* Clear the data channels. */
7056 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7057 ua_chan
, node
.node
) {
7058 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7060 /* Per-PID buffer and application going away. */
7061 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7068 (void) push_metadata(registry
, usess
->consumer
);
7071 * Clear the metadata channel.
7072 * Metadata channel is not cleared per se but we still need to
7073 * perform rotation operation on it behind the scene.
7075 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
7077 /* Per-PID buffer and application going away. */
7078 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7096 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7097 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7100 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;