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
16 #include <sys/types.h>
18 #include <urcu/compiler.h>
21 #include <common/compat/errno.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 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
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 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
326 * through a call_rcu().
329 void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
331 struct ust_app_event_notifier_rule
*obj
= caa_container_of(
332 head
, struct ust_app_event_notifier_rule
, rcu_head
);
338 * Delete ust app event notifier rule safely.
340 static void delete_ust_app_event_notifier_rule(int sock
,
341 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
,
346 assert(ua_event_notifier_rule
);
348 if (ua_event_notifier_rule
->exclusion
!= NULL
) {
349 free(ua_event_notifier_rule
->exclusion
);
352 if (ua_event_notifier_rule
->obj
!= NULL
) {
353 pthread_mutex_lock(&app
->sock_lock
);
354 ret
= ustctl_release_object(sock
, ua_event_notifier_rule
->obj
);
355 pthread_mutex_unlock(&app
->sock_lock
);
356 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
357 ERR("Failed to release event notifier object: app = '%s' (ppid %d), ret = %d",
358 app
->name
, (int) app
->ppid
, ret
);
361 free(ua_event_notifier_rule
->obj
);
364 lttng_event_rule_put(ua_event_notifier_rule
->event_rule
);
365 call_rcu(&ua_event_notifier_rule
->rcu_head
,
366 free_ust_app_event_notifier_rule_rcu
);
370 * Release ust data object of the given stream.
372 * Return 0 on success or else a negative value.
374 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
382 pthread_mutex_lock(&app
->sock_lock
);
383 ret
= ustctl_release_object(sock
, stream
->obj
);
384 pthread_mutex_unlock(&app
->sock_lock
);
385 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
386 ERR("UST app sock %d release stream obj failed with ret %d",
389 lttng_fd_put(LTTNG_FD_APPS
, 2);
397 * Delete ust app stream safely. RCU read lock must be held before calling
401 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
406 (void) release_ust_app_stream(sock
, stream
, app
);
411 * We need to execute ht_destroy outside of RCU read-side critical
412 * section and outside of call_rcu thread, so we postpone its execution
413 * using ht_cleanup_push. It is simpler than to change the semantic of
414 * the many callers of delete_ust_app_session().
417 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
419 struct ust_app_channel
*ua_chan
=
420 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
422 ht_cleanup_push(ua_chan
->ctx
);
423 ht_cleanup_push(ua_chan
->events
);
428 * Extract the lost packet or discarded events counter when the channel is
429 * being deleted and store the value in the parent channel so we can
430 * access it from lttng list and at stop/destroy.
432 * The session list lock must be held by the caller.
435 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
437 uint64_t discarded
= 0, lost
= 0;
438 struct ltt_session
*session
;
439 struct ltt_ust_channel
*uchan
;
441 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
446 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
447 if (!session
|| !session
->ust_session
) {
449 * Not finding the session is not an error because there are
450 * multiple ways the channels can be torn down.
452 * 1) The session daemon can initiate the destruction of the
453 * ust app session after receiving a destroy command or
454 * during its shutdown/teardown.
455 * 2) The application, since we are in per-pid tracing, is
456 * unregistering and tearing down its ust app session.
458 * Both paths are protected by the session list lock which
459 * ensures that the accounting of lost packets and discarded
460 * events is done exactly once. The session is then unpublished
461 * from the session list, resulting in this condition.
466 if (ua_chan
->attr
.overwrite
) {
467 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
468 ua_chan
->key
, session
->ust_session
->consumer
,
471 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
472 ua_chan
->key
, session
->ust_session
->consumer
,
475 uchan
= trace_ust_find_channel_by_name(
476 session
->ust_session
->domain_global
.channels
,
479 ERR("Missing UST channel to store discarded counters");
483 uchan
->per_pid_closed_app_discarded
+= discarded
;
484 uchan
->per_pid_closed_app_lost
+= lost
;
489 session_put(session
);
494 * Delete ust app channel safely. RCU read lock must be held before calling
497 * The session list lock must be held by the caller.
500 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
504 struct lttng_ht_iter iter
;
505 struct ust_app_event
*ua_event
;
506 struct ust_app_ctx
*ua_ctx
;
507 struct ust_app_stream
*stream
, *stmp
;
508 struct ust_registry_session
*registry
;
512 DBG3("UST app deleting channel %s", ua_chan
->name
);
515 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
516 cds_list_del(&stream
->list
);
517 delete_ust_app_stream(sock
, stream
, app
);
521 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
522 cds_list_del(&ua_ctx
->list
);
523 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
525 delete_ust_app_ctx(sock
, ua_ctx
, app
);
529 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
531 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
533 delete_ust_app_event(sock
, ua_event
, app
);
536 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
537 /* Wipe and free registry from session registry. */
538 registry
= get_session_registry(ua_chan
->session
);
540 ust_registry_channel_del_free(registry
, ua_chan
->key
,
544 * A negative socket can be used by the caller when
545 * cleaning-up a ua_chan in an error path. Skip the
546 * accounting in this case.
549 save_per_pid_lost_discarded_counters(ua_chan
);
553 if (ua_chan
->obj
!= NULL
) {
554 /* Remove channel from application UST object descriptor. */
555 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
556 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
558 pthread_mutex_lock(&app
->sock_lock
);
559 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
560 pthread_mutex_unlock(&app
->sock_lock
);
561 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
562 ERR("UST app sock %d release channel obj failed with ret %d",
565 lttng_fd_put(LTTNG_FD_APPS
, 1);
568 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
571 int ust_app_register_done(struct ust_app
*app
)
575 pthread_mutex_lock(&app
->sock_lock
);
576 ret
= ustctl_register_done(app
->sock
);
577 pthread_mutex_unlock(&app
->sock_lock
);
581 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
586 pthread_mutex_lock(&app
->sock_lock
);
591 ret
= ustctl_release_object(sock
, data
);
593 pthread_mutex_unlock(&app
->sock_lock
);
599 * Push metadata to consumer socket.
601 * RCU read-side lock must be held to guarantee existance of socket.
602 * Must be called with the ust app session lock held.
603 * Must be called with the registry lock held.
605 * On success, return the len of metadata pushed or else a negative value.
606 * Returning a -EPIPE return value means we could not send the metadata,
607 * but it can be caused by recoverable errors (e.g. the application has
608 * terminated concurrently).
610 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
611 struct consumer_socket
*socket
, int send_zero_data
)
614 char *metadata_str
= NULL
;
615 size_t len
, offset
, new_metadata_len_sent
;
617 uint64_t metadata_key
, metadata_version
;
622 metadata_key
= registry
->metadata_key
;
625 * Means that no metadata was assigned to the session. This can
626 * happens if no start has been done previously.
632 offset
= registry
->metadata_len_sent
;
633 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
634 new_metadata_len_sent
= registry
->metadata_len
;
635 metadata_version
= registry
->metadata_version
;
637 DBG3("No metadata to push for metadata key %" PRIu64
,
638 registry
->metadata_key
);
640 if (send_zero_data
) {
641 DBG("No metadata to push");
647 /* Allocate only what we have to send. */
648 metadata_str
= zmalloc(len
);
650 PERROR("zmalloc ust app metadata string");
654 /* Copy what we haven't sent out. */
655 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
658 pthread_mutex_unlock(®istry
->lock
);
660 * We need to unlock the registry while we push metadata to
661 * break a circular dependency between the consumerd metadata
662 * lock and the sessiond registry lock. Indeed, pushing metadata
663 * to the consumerd awaits that it gets pushed all the way to
664 * relayd, but doing so requires grabbing the metadata lock. If
665 * a concurrent metadata request is being performed by
666 * consumerd, this can try to grab the registry lock on the
667 * sessiond while holding the metadata lock on the consumer
668 * daemon. Those push and pull schemes are performed on two
669 * different bidirectionnal communication sockets.
671 ret
= consumer_push_metadata(socket
, metadata_key
,
672 metadata_str
, len
, offset
, metadata_version
);
673 pthread_mutex_lock(®istry
->lock
);
676 * There is an acceptable race here between the registry
677 * metadata key assignment and the creation on the
678 * consumer. The session daemon can concurrently push
679 * metadata for this registry while being created on the
680 * consumer since the metadata key of the registry is
681 * assigned *before* it is setup to avoid the consumer
682 * to ask for metadata that could possibly be not found
683 * in the session daemon.
685 * The metadata will get pushed either by the session
686 * being stopped or the consumer requesting metadata if
687 * that race is triggered.
689 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
692 ERR("Error pushing metadata to consumer");
698 * Metadata may have been concurrently pushed, since
699 * we're not holding the registry lock while pushing to
700 * consumer. This is handled by the fact that we send
701 * the metadata content, size, and the offset at which
702 * that metadata belongs. This may arrive out of order
703 * on the consumer side, and the consumer is able to
704 * deal with overlapping fragments. The consumer
705 * supports overlapping fragments, which must be
706 * contiguous starting from offset 0. We keep the
707 * largest metadata_len_sent value of the concurrent
710 registry
->metadata_len_sent
=
711 max_t(size_t, registry
->metadata_len_sent
,
712 new_metadata_len_sent
);
721 * On error, flag the registry that the metadata is
722 * closed. We were unable to push anything and this
723 * means that either the consumer is not responding or
724 * the metadata cache has been destroyed on the
727 registry
->metadata_closed
= 1;
735 * For a given application and session, push metadata to consumer.
736 * Either sock or consumer is required : if sock is NULL, the default
737 * socket to send the metadata is retrieved from consumer, if sock
738 * is not NULL we use it to send the metadata.
739 * RCU read-side lock must be held while calling this function,
740 * therefore ensuring existance of registry. It also ensures existance
741 * of socket throughout this function.
743 * Return 0 on success else a negative error.
744 * Returning a -EPIPE return value means we could not send the metadata,
745 * but it can be caused by recoverable errors (e.g. the application has
746 * terminated concurrently).
748 static int push_metadata(struct ust_registry_session
*registry
,
749 struct consumer_output
*consumer
)
753 struct consumer_socket
*socket
;
758 pthread_mutex_lock(®istry
->lock
);
759 if (registry
->metadata_closed
) {
764 /* Get consumer socket to use to push the metadata.*/
765 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
772 ret
= ust_app_push_metadata(registry
, socket
, 0);
777 pthread_mutex_unlock(®istry
->lock
);
781 pthread_mutex_unlock(®istry
->lock
);
786 * Send to the consumer a close metadata command for the given session. Once
787 * done, the metadata channel is deleted and the session metadata pointer is
788 * nullified. The session lock MUST be held unless the application is
789 * in the destroy path.
791 * Do not hold the registry lock while communicating with the consumerd, because
792 * doing so causes inter-process deadlocks between consumerd and sessiond with
793 * the metadata request notification.
795 * Return 0 on success else a negative value.
797 static int close_metadata(struct ust_registry_session
*registry
,
798 struct consumer_output
*consumer
)
801 struct consumer_socket
*socket
;
802 uint64_t metadata_key
;
803 bool registry_was_already_closed
;
810 pthread_mutex_lock(®istry
->lock
);
811 metadata_key
= registry
->metadata_key
;
812 registry_was_already_closed
= registry
->metadata_closed
;
813 if (metadata_key
!= 0) {
815 * Metadata closed. Even on error this means that the consumer
816 * is not responding or not found so either way a second close
817 * should NOT be emit for this registry.
819 registry
->metadata_closed
= 1;
821 pthread_mutex_unlock(®istry
->lock
);
823 if (metadata_key
== 0 || registry_was_already_closed
) {
828 /* Get consumer socket to use to push the metadata.*/
829 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
836 ret
= consumer_close_metadata(socket
, metadata_key
);
847 * We need to execute ht_destroy outside of RCU read-side critical
848 * section and outside of call_rcu thread, so we postpone its execution
849 * using ht_cleanup_push. It is simpler than to change the semantic of
850 * the many callers of delete_ust_app_session().
853 void delete_ust_app_session_rcu(struct rcu_head
*head
)
855 struct ust_app_session
*ua_sess
=
856 caa_container_of(head
, struct ust_app_session
, rcu_head
);
858 ht_cleanup_push(ua_sess
->channels
);
863 * Delete ust app session safely. RCU read lock must be held before calling
866 * The session list lock must be held by the caller.
869 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
873 struct lttng_ht_iter iter
;
874 struct ust_app_channel
*ua_chan
;
875 struct ust_registry_session
*registry
;
879 pthread_mutex_lock(&ua_sess
->lock
);
881 assert(!ua_sess
->deleted
);
882 ua_sess
->deleted
= true;
884 registry
= get_session_registry(ua_sess
);
885 /* Registry can be null on error path during initialization. */
887 /* Push metadata for application before freeing the application. */
888 (void) push_metadata(registry
, ua_sess
->consumer
);
891 * Don't ask to close metadata for global per UID buffers. Close
892 * metadata only on destroy trace session in this case. Also, the
893 * previous push metadata could have flag the metadata registry to
894 * close so don't send a close command if closed.
896 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
897 /* And ask to close it for this session registry. */
898 (void) close_metadata(registry
, ua_sess
->consumer
);
902 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
904 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
906 delete_ust_app_channel(sock
, ua_chan
, app
);
909 /* In case of per PID, the registry is kept in the session. */
910 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
911 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
914 * Registry can be null on error path during
917 buffer_reg_pid_remove(reg_pid
);
918 buffer_reg_pid_destroy(reg_pid
);
922 if (ua_sess
->handle
!= -1) {
923 pthread_mutex_lock(&app
->sock_lock
);
924 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
925 pthread_mutex_unlock(&app
->sock_lock
);
926 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
927 ERR("UST app sock %d release session handle failed with ret %d",
930 /* Remove session from application UST object descriptor. */
931 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
932 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
936 pthread_mutex_unlock(&ua_sess
->lock
);
938 consumer_output_put(ua_sess
->consumer
);
940 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
944 * Delete a traceable application structure from the global list. Never call
945 * this function outside of a call_rcu call.
947 * RCU read side lock should _NOT_ be held when calling this function.
950 void delete_ust_app(struct ust_app
*app
)
953 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
954 struct lttng_ht_iter iter
;
955 struct ust_app_event_notifier_rule
*event_notifier_rule
;
956 bool event_notifier_write_fd_is_open
;
959 * The session list lock must be held during this function to guarantee
960 * the existence of ua_sess.
963 /* Delete ust app sessions info */
968 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
970 /* Free every object in the session and the session. */
972 delete_ust_app_session(sock
, ua_sess
, app
);
976 /* Remove the event notifier rules associated with this app. */
978 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
979 &iter
.iter
, event_notifier_rule
, node
.node
) {
980 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
983 delete_ust_app_event_notifier_rule(
984 app
->sock
, event_notifier_rule
, app
);
989 ht_cleanup_push(app
->sessions
);
990 ht_cleanup_push(app
->ust_sessions_objd
);
991 ht_cleanup_push(app
->ust_objd
);
992 ht_cleanup_push(app
->token_to_event_notifier_rule_ht
);
995 * This could be NULL if the event notifier setup failed (e.g the app
996 * was killed or the tracer does not support this feature).
998 if (app
->event_notifier_group
.object
) {
999 enum lttng_error_code ret_code
;
1000 const int event_notifier_read_fd
= lttng_pipe_get_readfd(
1001 app
->event_notifier_group
.event_pipe
);
1003 ret_code
= notification_thread_command_remove_tracer_event_source(
1004 notification_thread_handle
,
1005 event_notifier_read_fd
);
1006 if (ret_code
!= LTTNG_OK
) {
1007 ERR("Failed to remove application tracer event source from notification thread");
1010 ustctl_release_object(sock
, app
->event_notifier_group
.object
);
1011 free(app
->event_notifier_group
.object
);
1014 event_notifier_write_fd_is_open
= lttng_pipe_is_write_open(
1015 app
->event_notifier_group
.event_pipe
);
1016 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1018 * Release the file descriptors reserved for the event notifier pipe.
1019 * The app could be destroyed before the write end of the pipe could be
1020 * passed to the application (and closed). In that case, both file
1021 * descriptors must be released.
1023 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1026 * Wait until we have deleted the application from the sock hash table
1027 * before closing this socket, otherwise an application could re-use the
1028 * socket ID and race with the teardown, using the same hash table entry.
1030 * It's OK to leave the close in call_rcu. We want it to stay unique for
1031 * all RCU readers that could run concurrently with unregister app,
1032 * therefore we _need_ to only close that socket after a grace period. So
1033 * it should stay in this RCU callback.
1035 * This close() is a very important step of the synchronization model so
1036 * every modification to this function must be carefully reviewed.
1042 lttng_fd_put(LTTNG_FD_APPS
, 1);
1044 DBG2("UST app pid %d deleted", app
->pid
);
1046 session_unlock_list();
1050 * URCU intermediate call to delete an UST app.
1053 void delete_ust_app_rcu(struct rcu_head
*head
)
1055 struct lttng_ht_node_ulong
*node
=
1056 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1057 struct ust_app
*app
=
1058 caa_container_of(node
, struct ust_app
, pid_n
);
1060 DBG3("Call RCU deleting app PID %d", app
->pid
);
1061 delete_ust_app(app
);
1065 * Delete the session from the application ht and delete the data structure by
1066 * freeing every object inside and releasing them.
1068 * The session list lock must be held by the caller.
1070 static void destroy_app_session(struct ust_app
*app
,
1071 struct ust_app_session
*ua_sess
)
1074 struct lttng_ht_iter iter
;
1079 iter
.iter
.node
= &ua_sess
->node
.node
;
1080 ret
= lttng_ht_del(app
->sessions
, &iter
);
1082 /* Already scheduled for teardown. */
1086 /* Once deleted, free the data structure. */
1087 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1094 * Alloc new UST app session.
1097 struct ust_app_session
*alloc_ust_app_session(void)
1099 struct ust_app_session
*ua_sess
;
1101 /* Init most of the default value by allocating and zeroing */
1102 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1103 if (ua_sess
== NULL
) {
1108 ua_sess
->handle
= -1;
1109 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1110 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1111 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1120 * Alloc new UST app channel.
1123 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1124 struct ust_app_session
*ua_sess
,
1125 struct lttng_ust_channel_attr
*attr
)
1127 struct ust_app_channel
*ua_chan
;
1129 /* Init most of the default value by allocating and zeroing */
1130 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1131 if (ua_chan
== NULL
) {
1136 /* Setup channel name */
1137 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1138 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1140 ua_chan
->enabled
= 1;
1141 ua_chan
->handle
= -1;
1142 ua_chan
->session
= ua_sess
;
1143 ua_chan
->key
= get_next_channel_key();
1144 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1145 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1146 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1148 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1149 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1151 /* Copy attributes */
1153 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1154 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1155 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1156 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1157 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1158 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1159 ua_chan
->attr
.output
= attr
->output
;
1160 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1162 /* By default, the channel is a per cpu channel. */
1163 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1165 DBG3("UST app channel %s allocated", ua_chan
->name
);
1174 * Allocate and initialize a UST app stream.
1176 * Return newly allocated stream pointer or NULL on error.
1178 struct ust_app_stream
*ust_app_alloc_stream(void)
1180 struct ust_app_stream
*stream
= NULL
;
1182 stream
= zmalloc(sizeof(*stream
));
1183 if (stream
== NULL
) {
1184 PERROR("zmalloc ust app stream");
1188 /* Zero could be a valid value for a handle so flag it to -1. */
1189 stream
->handle
= -1;
1196 * Alloc new UST app event.
1199 struct ust_app_event
*alloc_ust_app_event(char *name
,
1200 struct lttng_ust_event
*attr
)
1202 struct ust_app_event
*ua_event
;
1204 /* Init most of the default value by allocating and zeroing */
1205 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1206 if (ua_event
== NULL
) {
1207 PERROR("Failed to allocate ust_app_event structure");
1211 ua_event
->enabled
= 1;
1212 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1213 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1214 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1216 /* Copy attributes */
1218 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1221 DBG3("UST app event %s allocated", ua_event
->name
);
1230 * Allocate a new UST app event notifier rule.
1232 static struct ust_app_event_notifier_rule
*alloc_ust_app_event_notifier_rule(
1233 struct lttng_event_rule
*event_rule
, uint64_t token
)
1235 enum lttng_event_rule_generate_exclusions_status
1236 generate_exclusion_status
;
1237 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1239 ua_event_notifier_rule
= zmalloc(sizeof(struct ust_app_event_notifier_rule
));
1240 if (ua_event_notifier_rule
== NULL
) {
1241 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1245 ua_event_notifier_rule
->enabled
= 1;
1246 ua_event_notifier_rule
->token
= token
;
1247 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
, token
);
1249 /* Get reference of the event rule. */
1250 if (!lttng_event_rule_get(event_rule
)) {
1254 ua_event_notifier_rule
->event_rule
= event_rule
;
1255 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1256 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1257 event_rule
, &ua_event_notifier_rule
->exclusion
);
1258 switch (generate_exclusion_status
) {
1259 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1260 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1263 /* Error occured. */
1264 ERR("Failed to generate exclusions from event rule while allocating an event notifier rule");
1265 goto error_put_event_rule
;
1268 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1269 ua_event_notifier_rule
->token
);
1271 return ua_event_notifier_rule
;
1273 error_put_event_rule
:
1274 lttng_event_rule_put(event_rule
);
1276 free(ua_event_notifier_rule
);
1281 * Alloc new UST app context.
1284 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1286 struct ust_app_ctx
*ua_ctx
;
1288 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1289 if (ua_ctx
== NULL
) {
1293 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1296 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1297 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1298 char *provider_name
= NULL
, *ctx_name
= NULL
;
1300 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1301 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1302 if (!provider_name
|| !ctx_name
) {
1303 free(provider_name
);
1308 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1309 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1313 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1321 * Create a liblttng-ust filter bytecode from given bytecode.
1323 * Return allocated filter or NULL on error.
1325 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1326 const struct lttng_filter_bytecode
*orig_f
)
1328 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1330 /* Copy filter bytecode */
1331 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1333 PERROR("zmalloc alloc ust filter bytecode");
1337 assert(sizeof(struct lttng_filter_bytecode
) ==
1338 sizeof(struct lttng_ust_filter_bytecode
));
1339 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1345 * Find an ust_app using the sock and return it. RCU read side lock must be
1346 * held before calling this helper function.
1348 struct ust_app
*ust_app_find_by_sock(int sock
)
1350 struct lttng_ht_node_ulong
*node
;
1351 struct lttng_ht_iter iter
;
1353 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1354 node
= lttng_ht_iter_get_node_ulong(&iter
);
1356 DBG2("UST app find by sock %d not found", sock
);
1360 return caa_container_of(node
, struct ust_app
, sock_n
);
1367 * Find an ust_app using the notify sock and return it. RCU read side lock must
1368 * be held before calling this helper function.
1370 static struct ust_app
*find_app_by_notify_sock(int sock
)
1372 struct lttng_ht_node_ulong
*node
;
1373 struct lttng_ht_iter iter
;
1375 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1377 node
= lttng_ht_iter_get_node_ulong(&iter
);
1379 DBG2("UST app find by notify sock %d not found", sock
);
1383 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1390 * Lookup for an ust app event based on event name, filter bytecode and the
1393 * Return an ust_app_event object or NULL on error.
1395 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1396 const char *name
, const struct lttng_filter_bytecode
*filter
,
1398 const struct lttng_event_exclusion
*exclusion
)
1400 struct lttng_ht_iter iter
;
1401 struct lttng_ht_node_str
*node
;
1402 struct ust_app_event
*event
= NULL
;
1403 struct ust_app_ht_key key
;
1408 /* Setup key for event lookup. */
1410 key
.filter
= filter
;
1411 key
.loglevel_type
= loglevel_value
;
1412 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1413 key
.exclusion
= exclusion
;
1415 /* Lookup using the event name as hash and a custom match fct. */
1416 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1417 ht_match_ust_app_event
, &key
, &iter
.iter
);
1418 node
= lttng_ht_iter_get_node_str(&iter
);
1423 event
= caa_container_of(node
, struct ust_app_event
, node
);
1430 * Look-up an event notifier rule based on its token id.
1432 * Must be called with the RCU read lock held.
1433 * Return an ust_app_event_notifier_rule object or NULL on error.
1435 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(
1436 struct lttng_ht
*ht
, uint64_t token
)
1438 struct lttng_ht_iter iter
;
1439 struct lttng_ht_node_u64
*node
;
1440 struct ust_app_event_notifier_rule
*event_notifier_rule
= NULL
;
1444 lttng_ht_lookup(ht
, &token
, &iter
);
1445 node
= lttng_ht_iter_get_node_u64(&iter
);
1447 DBG2("UST app event notifier rule token not found: token = %" PRIu64
,
1452 event_notifier_rule
= caa_container_of(
1453 node
, struct ust_app_event_notifier_rule
, node
);
1455 return event_notifier_rule
;
1459 * Create the channel context on the tracer.
1461 * Called with UST app session lock held.
1464 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1465 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1469 health_code_update();
1471 pthread_mutex_lock(&app
->sock_lock
);
1472 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1473 ua_chan
->obj
, &ua_ctx
->obj
);
1474 pthread_mutex_unlock(&app
->sock_lock
);
1476 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1477 ERR("UST app create channel context failed for app (pid: %d) "
1478 "with ret %d", app
->pid
, ret
);
1481 * This is normal behavior, an application can die during the
1482 * creation process. Don't report an error so the execution can
1483 * continue normally.
1486 DBG3("UST app add context failed. Application is dead.");
1491 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1493 DBG2("UST app context handle %d created successfully for channel %s",
1494 ua_ctx
->handle
, ua_chan
->name
);
1497 health_code_update();
1502 * Set the filter on the tracer.
1504 static int set_ust_object_filter(struct ust_app
*app
,
1505 const struct lttng_filter_bytecode
*bytecode
,
1506 struct lttng_ust_object_data
*ust_object
)
1509 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1511 health_code_update();
1513 ust_bytecode
= create_ust_bytecode_from_bytecode(bytecode
);
1514 if (!ust_bytecode
) {
1515 ret
= -LTTNG_ERR_NOMEM
;
1518 pthread_mutex_lock(&app
->sock_lock
);
1519 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1521 pthread_mutex_unlock(&app
->sock_lock
);
1523 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1524 ERR("UST app set object filter failed for object %p of app (pid: %d) "
1525 "with ret %d", ust_object
, app
->pid
, ret
);
1528 * This is normal behavior, an application can die during the
1529 * creation process. Don't report an error so the execution can
1530 * continue normally.
1533 DBG3("Failed to set UST app object filter. Application is dead.");
1538 DBG2("UST filter successfully set for object %p", ust_object
);
1541 health_code_update();
1547 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1548 const struct lttng_event_exclusion
*exclusion
)
1550 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1551 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1552 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1554 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1555 if (!ust_exclusion
) {
1560 assert(sizeof(struct lttng_event_exclusion
) ==
1561 sizeof(struct lttng_ust_event_exclusion
));
1562 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1564 return ust_exclusion
;
1568 * Set event exclusions on the tracer.
1570 static int set_ust_object_exclusions(struct ust_app
*app
,
1571 const struct lttng_event_exclusion
*exclusions
,
1572 struct lttng_ust_object_data
*ust_object
)
1575 struct lttng_ust_event_exclusion
*ust_exclusions
= NULL
;
1577 assert(exclusions
&& exclusions
->count
> 0);
1579 health_code_update();
1581 ust_exclusions
= create_ust_exclusion_from_exclusion(
1583 if (!ust_exclusions
) {
1584 ret
= -LTTNG_ERR_NOMEM
;
1587 pthread_mutex_lock(&app
->sock_lock
);
1588 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1589 pthread_mutex_unlock(&app
->sock_lock
);
1591 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1592 ERR("Failed to set UST app exclusions for object %p of app (pid: %d) "
1593 "with ret %d", ust_object
, app
->pid
, ret
);
1596 * This is normal behavior, an application can die during the
1597 * creation process. Don't report an error so the execution can
1598 * continue normally.
1601 DBG3("Failed to set UST app object exclusions. Application is dead.");
1606 DBG2("UST exclusions set successfully for object %p", ust_object
);
1609 health_code_update();
1610 free(ust_exclusions
);
1615 * Disable the specified event on to UST tracer for the UST session.
1617 static int disable_ust_object(struct ust_app
*app
,
1618 struct lttng_ust_object_data
*object
)
1622 health_code_update();
1624 pthread_mutex_lock(&app
->sock_lock
);
1625 ret
= ustctl_disable(app
->sock
, object
);
1626 pthread_mutex_unlock(&app
->sock_lock
);
1628 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1629 ERR("Failed to disable UST app object %p app (pid: %d) with ret %d",
1630 object
, app
->pid
, ret
);
1633 * This is normal behavior, an application can die during the
1634 * creation process. Don't report an error so the execution can
1635 * continue normally.
1638 DBG3("Failed to disable UST app object. Application is dead.");
1643 DBG2("UST app object %p disabled successfully for app (pid: %d)",
1647 health_code_update();
1652 * Disable the specified channel on to UST tracer for the UST session.
1654 static int disable_ust_channel(struct ust_app
*app
,
1655 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1659 health_code_update();
1661 pthread_mutex_lock(&app
->sock_lock
);
1662 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1663 pthread_mutex_unlock(&app
->sock_lock
);
1665 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1666 ERR("UST app channel %s disable failed for app (pid: %d) "
1667 "and session handle %d with ret %d",
1668 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1671 * This is normal behavior, an application can die during the
1672 * creation process. Don't report an error so the execution can
1673 * continue normally.
1676 DBG3("UST app disable channel failed. Application is dead.");
1681 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1682 ua_chan
->name
, app
->pid
);
1685 health_code_update();
1690 * Enable the specified channel on to UST tracer for the UST session.
1692 static int enable_ust_channel(struct ust_app
*app
,
1693 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1697 health_code_update();
1699 pthread_mutex_lock(&app
->sock_lock
);
1700 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1701 pthread_mutex_unlock(&app
->sock_lock
);
1703 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1704 ERR("UST app channel %s enable failed for app (pid: %d) "
1705 "and session handle %d with ret %d",
1706 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1709 * This is normal behavior, an application can die during the
1710 * creation process. Don't report an error so the execution can
1711 * continue normally.
1714 DBG3("UST app enable channel failed. Application is dead.");
1719 ua_chan
->enabled
= 1;
1721 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1722 ua_chan
->name
, app
->pid
);
1725 health_code_update();
1730 * Enable the specified event on to UST tracer for the UST session.
1732 static int enable_ust_object(
1733 struct ust_app
*app
, struct lttng_ust_object_data
*ust_object
)
1737 health_code_update();
1739 pthread_mutex_lock(&app
->sock_lock
);
1740 ret
= ustctl_enable(app
->sock
, ust_object
);
1741 pthread_mutex_unlock(&app
->sock_lock
);
1743 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1744 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
1745 ust_object
, app
->pid
, ret
);
1748 * This is normal behavior, an application can die during the
1749 * creation process. Don't report an error so the execution can
1750 * continue normally.
1753 DBG3("Failed to enable UST app object. Application is dead.");
1758 DBG2("UST app object %p enabled successfully for app (pid: %d)",
1759 ust_object
, app
->pid
);
1762 health_code_update();
1767 * Send channel and stream buffer to application.
1769 * Return 0 on success. On error, a negative value is returned.
1771 static int send_channel_pid_to_ust(struct ust_app
*app
,
1772 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1775 struct ust_app_stream
*stream
, *stmp
;
1781 health_code_update();
1783 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1786 /* Send channel to the application. */
1787 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1788 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1789 ret
= -ENOTCONN
; /* Caused by app exiting. */
1791 } else if (ret
< 0) {
1795 health_code_update();
1797 /* Send all streams to application. */
1798 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1799 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1800 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1801 ret
= -ENOTCONN
; /* Caused by app exiting. */
1803 } else if (ret
< 0) {
1806 /* We don't need the stream anymore once sent to the tracer. */
1807 cds_list_del(&stream
->list
);
1808 delete_ust_app_stream(-1, stream
, app
);
1810 /* Flag the channel that it is sent to the application. */
1811 ua_chan
->is_sent
= 1;
1814 health_code_update();
1819 * Create the specified event onto the UST tracer for a UST session.
1821 * Should be called with session mutex held.
1824 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1825 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1829 health_code_update();
1831 /* Create UST event on tracer */
1832 pthread_mutex_lock(&app
->sock_lock
);
1833 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1835 pthread_mutex_unlock(&app
->sock_lock
);
1837 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1839 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1840 ua_event
->attr
.name
, app
->pid
, ret
);
1843 * This is normal behavior, an application can die during the
1844 * creation process. Don't report an error so the execution can
1845 * continue normally.
1848 DBG3("UST app create event failed. Application is dead.");
1853 ua_event
->handle
= ua_event
->obj
->handle
;
1855 DBG2("UST app event %s created successfully for pid:%d object: %p",
1856 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
1858 health_code_update();
1860 /* Set filter if one is present. */
1861 if (ua_event
->filter
) {
1862 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
1868 /* Set exclusions for the event */
1869 if (ua_event
->exclusion
) {
1870 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
1876 /* If event not enabled, disable it on the tracer */
1877 if (ua_event
->enabled
) {
1879 * We now need to explicitly enable the event, since it
1880 * is now disabled at creation.
1882 ret
= enable_ust_object(app
, ua_event
->obj
);
1885 * If we hit an EPERM, something is wrong with our enable call. If
1886 * we get an EEXIST, there is a problem on the tracer side since we
1890 case -LTTNG_UST_ERR_PERM
:
1891 /* Code flow problem */
1893 case -LTTNG_UST_ERR_EXIST
:
1894 /* It's OK for our use case. */
1905 health_code_update();
1909 static int init_ust_event_notifier_from_event_rule(
1910 const struct lttng_event_rule
*rule
,
1911 struct lttng_ust_event_notifier
*event_notifier
)
1913 enum lttng_event_rule_status status
;
1914 enum lttng_loglevel_type loglevel_type
;
1915 enum lttng_ust_loglevel_type ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1916 int loglevel
= -1, ret
= 0;
1917 const char *pattern
;
1919 /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported. */
1920 assert(lttng_event_rule_get_type(rule
) ==
1921 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
1923 memset(event_notifier
, 0, sizeof(*event_notifier
));
1925 if (lttng_event_rule_targets_agent_domain(rule
)) {
1927 * Special event for agents
1928 * The actual meat of the event is in the filter that will be
1929 * attached later on.
1930 * Set the default values for the agent event.
1932 pattern
= event_get_default_agent_ust_name(
1933 lttng_event_rule_get_domain_type(rule
));
1935 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1937 status
= lttng_event_rule_tracepoint_get_pattern(
1939 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1940 /* At this point, this is a fatal error. */
1944 status
= lttng_event_rule_tracepoint_get_log_level_type(
1945 rule
, &loglevel_type
);
1946 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1947 /* At this point, this is a fatal error. */
1951 switch (loglevel_type
) {
1952 case LTTNG_EVENT_LOGLEVEL_ALL
:
1953 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1955 case LTTNG_EVENT_LOGLEVEL_RANGE
:
1956 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
1958 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
1959 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
1962 /* Unknown log level specification type. */
1966 if (loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1967 status
= lttng_event_rule_tracepoint_get_log_level(
1969 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
1973 event_notifier
->event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
1974 ret
= lttng_strncpy(event_notifier
->event
.name
, pattern
,
1975 LTTNG_UST_SYM_NAME_LEN
- 1);
1977 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
1982 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
1983 event_notifier
->event
.loglevel
= loglevel
;
1989 * Create the specified event notifier against the user space tracer of a
1990 * given application.
1992 static int create_ust_event_notifier(struct ust_app
*app
,
1993 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
1996 struct lttng_ust_event_notifier event_notifier
;
1998 health_code_update();
1999 assert(app
->event_notifier_group
.object
);
2001 ret
= init_ust_event_notifier_from_event_rule(
2002 ua_event_notifier_rule
->event_rule
, &event_notifier
);
2004 ERR("Failed to initialize UST event notifier from event rule: app = '%s' (ppid: %d)",
2005 app
->name
, app
->ppid
);
2009 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2011 /* Create UST event notifier against the tracer. */
2012 pthread_mutex_lock(&app
->sock_lock
);
2013 ret
= ustctl_create_event_notifier(app
->sock
, &event_notifier
,
2014 app
->event_notifier_group
.object
,
2015 &ua_event_notifier_rule
->obj
);
2016 pthread_mutex_unlock(&app
->sock_lock
);
2018 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2019 ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d",
2020 event_notifier
.event
.name
, app
->name
,
2024 * This is normal behavior, an application can die
2025 * during the creation process. Don't report an error so
2026 * the execution can continue normally.
2029 DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)",
2030 app
->name
, app
->ppid
);
2036 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2038 DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p",
2039 event_notifier
.event
.name
, app
->name
, app
->ppid
,
2040 ua_event_notifier_rule
->obj
);
2042 health_code_update();
2044 /* Set filter if one is present. */
2045 if (ua_event_notifier_rule
->filter
) {
2046 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2047 ua_event_notifier_rule
->obj
);
2053 /* Set exclusions for the event. */
2054 if (ua_event_notifier_rule
->exclusion
) {
2055 ret
= set_ust_object_exclusions(app
,
2056 ua_event_notifier_rule
->exclusion
,
2057 ua_event_notifier_rule
->obj
);
2064 * We now need to explicitly enable the event, since it
2065 * is disabled at creation.
2067 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2070 * If we hit an EPERM, something is wrong with our enable call.
2071 * If we get an EEXIST, there is a problem on the tracer side
2072 * since we just created it.
2075 case -LTTNG_UST_ERR_PERM
:
2076 /* Code flow problem. */
2078 case -LTTNG_UST_ERR_EXIST
:
2079 /* It's OK for our use case. */
2089 ua_event_notifier_rule
->enabled
= true;
2092 health_code_update();
2097 * Copy data between an UST app event and a LTT event.
2099 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2100 struct ltt_ust_event
*uevent
)
2102 size_t exclusion_alloc_size
;
2104 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2105 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2107 ua_event
->enabled
= uevent
->enabled
;
2109 /* Copy event attributes */
2110 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2112 /* Copy filter bytecode */
2113 if (uevent
->filter
) {
2114 ua_event
->filter
= lttng_filter_bytecode_copy(uevent
->filter
);
2115 /* Filter might be NULL here in case of ENONEM. */
2118 /* Copy exclusion data */
2119 if (uevent
->exclusion
) {
2120 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2121 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2122 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
2123 if (ua_event
->exclusion
== NULL
) {
2126 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2127 exclusion_alloc_size
);
2133 * Copy data between an UST app channel and a LTT channel.
2135 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2136 struct ltt_ust_channel
*uchan
)
2138 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2140 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2141 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2143 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2144 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2146 /* Copy event attributes since the layout is different. */
2147 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2148 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2149 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2150 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2151 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2152 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2153 ua_chan
->attr
.output
= uchan
->attr
.output
;
2154 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2157 * Note that the attribute channel type is not set since the channel on the
2158 * tracing registry side does not have this information.
2161 ua_chan
->enabled
= uchan
->enabled
;
2162 ua_chan
->tracing_channel_id
= uchan
->id
;
2164 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2168 * Copy data between a UST app session and a regular LTT session.
2170 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2171 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2173 struct tm
*timeinfo
;
2176 char tmp_shm_path
[PATH_MAX
];
2178 timeinfo
= localtime(&app
->registration_time
);
2179 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2181 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2183 ua_sess
->tracing_id
= usess
->id
;
2184 ua_sess
->id
= get_next_session_id();
2185 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2186 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2187 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2188 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2189 ua_sess
->buffer_type
= usess
->buffer_type
;
2190 ua_sess
->bits_per_long
= app
->bits_per_long
;
2192 /* There is only one consumer object per session possible. */
2193 consumer_output_get(usess
->consumer
);
2194 ua_sess
->consumer
= usess
->consumer
;
2196 ua_sess
->output_traces
= usess
->output_traces
;
2197 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2198 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2199 &usess
->metadata_attr
);
2201 switch (ua_sess
->buffer_type
) {
2202 case LTTNG_BUFFER_PER_PID
:
2203 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2204 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2207 case LTTNG_BUFFER_PER_UID
:
2208 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2209 DEFAULT_UST_TRACE_UID_PATH
,
2210 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2211 app
->bits_per_long
);
2218 PERROR("asprintf UST shadow copy session");
2223 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2224 sizeof(ua_sess
->root_shm_path
));
2225 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2226 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2227 sizeof(ua_sess
->shm_path
));
2228 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2229 if (ua_sess
->shm_path
[0]) {
2230 switch (ua_sess
->buffer_type
) {
2231 case LTTNG_BUFFER_PER_PID
:
2232 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2233 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2234 app
->name
, app
->pid
, datetime
);
2236 case LTTNG_BUFFER_PER_UID
:
2237 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2238 "/" DEFAULT_UST_TRACE_UID_PATH
,
2239 app
->uid
, app
->bits_per_long
);
2246 PERROR("sprintf UST shadow copy session");
2250 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2251 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2252 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2257 consumer_output_put(ua_sess
->consumer
);
2261 * Lookup sesison wrapper.
2264 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2265 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2267 /* Get right UST app session from app */
2268 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2272 * Return ust app session from the app session hashtable using the UST session
2275 static struct ust_app_session
*lookup_session_by_app(
2276 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2278 struct lttng_ht_iter iter
;
2279 struct lttng_ht_node_u64
*node
;
2281 __lookup_session_by_app(usess
, app
, &iter
);
2282 node
= lttng_ht_iter_get_node_u64(&iter
);
2287 return caa_container_of(node
, struct ust_app_session
, node
);
2294 * Setup buffer registry per PID for the given session and application. If none
2295 * is found, a new one is created, added to the global registry and
2296 * initialized. If regp is valid, it's set with the newly created object.
2298 * Return 0 on success or else a negative value.
2300 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2301 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2304 struct buffer_reg_pid
*reg_pid
;
2311 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2314 * This is the create channel path meaning that if there is NO
2315 * registry available, we have to create one for this session.
2317 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2318 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2326 /* Initialize registry. */
2327 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2328 app
->bits_per_long
, app
->uint8_t_alignment
,
2329 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2330 app
->uint64_t_alignment
, app
->long_alignment
,
2331 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2332 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2333 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2334 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2335 ua_sess
->tracing_id
,
2339 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2340 * destroy the buffer registry, because it is always expected
2341 * that if the buffer registry can be found, its ust registry is
2344 buffer_reg_pid_destroy(reg_pid
);
2348 buffer_reg_pid_add(reg_pid
);
2350 DBG3("UST app buffer registry per PID created successfully");
2362 * Setup buffer registry per UID for the given session and application. If none
2363 * is found, a new one is created, added to the global registry and
2364 * initialized. If regp is valid, it's set with the newly created object.
2366 * Return 0 on success or else a negative value.
2368 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2369 struct ust_app_session
*ua_sess
,
2370 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2373 struct buffer_reg_uid
*reg_uid
;
2380 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2383 * This is the create channel path meaning that if there is NO
2384 * registry available, we have to create one for this session.
2386 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2387 LTTNG_DOMAIN_UST
, ®_uid
,
2388 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2396 /* Initialize registry. */
2397 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2398 app
->bits_per_long
, app
->uint8_t_alignment
,
2399 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2400 app
->uint64_t_alignment
, app
->long_alignment
,
2401 app
->byte_order
, app
->version
.major
,
2402 app
->version
.minor
, reg_uid
->root_shm_path
,
2403 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2404 ua_sess
->tracing_id
, app
->uid
);
2407 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2408 * destroy the buffer registry, because it is always expected
2409 * that if the buffer registry can be found, its ust registry is
2412 buffer_reg_uid_destroy(reg_uid
, NULL
);
2415 /* Add node to teardown list of the session. */
2416 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2418 buffer_reg_uid_add(reg_uid
);
2420 DBG3("UST app buffer registry per UID created successfully");
2431 * Create a session on the tracer side for the given app.
2433 * On success, ua_sess_ptr is populated with the session pointer or else left
2434 * untouched. If the session was created, is_created is set to 1. On error,
2435 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2438 * Returns 0 on success or else a negative code which is either -ENOMEM or
2439 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2441 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2442 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2445 int ret
, created
= 0;
2446 struct ust_app_session
*ua_sess
;
2450 assert(ua_sess_ptr
);
2452 health_code_update();
2454 ua_sess
= lookup_session_by_app(usess
, app
);
2455 if (ua_sess
== NULL
) {
2456 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2457 app
->pid
, usess
->id
);
2458 ua_sess
= alloc_ust_app_session();
2459 if (ua_sess
== NULL
) {
2460 /* Only malloc can failed so something is really wrong */
2464 shadow_copy_session(ua_sess
, usess
, app
);
2468 switch (usess
->buffer_type
) {
2469 case LTTNG_BUFFER_PER_PID
:
2470 /* Init local registry. */
2471 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2473 delete_ust_app_session(-1, ua_sess
, app
);
2477 case LTTNG_BUFFER_PER_UID
:
2478 /* Look for a global registry. If none exists, create one. */
2479 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2481 delete_ust_app_session(-1, ua_sess
, app
);
2491 health_code_update();
2493 if (ua_sess
->handle
== -1) {
2494 pthread_mutex_lock(&app
->sock_lock
);
2495 ret
= ustctl_create_session(app
->sock
);
2496 pthread_mutex_unlock(&app
->sock_lock
);
2498 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2499 ERR("Creating session for app pid %d with ret %d",
2502 DBG("UST app creating session failed. Application is dead");
2504 * This is normal behavior, an application can die during the
2505 * creation process. Don't report an error so the execution can
2506 * continue normally. This will get flagged ENOTCONN and the
2507 * caller will handle it.
2511 delete_ust_app_session(-1, ua_sess
, app
);
2512 if (ret
!= -ENOMEM
) {
2514 * Tracer is probably gone or got an internal error so let's
2515 * behave like it will soon unregister or not usable.
2522 ua_sess
->handle
= ret
;
2524 /* Add ust app session to app's HT */
2525 lttng_ht_node_init_u64(&ua_sess
->node
,
2526 ua_sess
->tracing_id
);
2527 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2528 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2529 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2530 &ua_sess
->ust_objd_node
);
2532 DBG2("UST app session created successfully with handle %d", ret
);
2535 *ua_sess_ptr
= ua_sess
;
2537 *is_created
= created
;
2540 /* Everything went well. */
2544 health_code_update();
2549 * Match function for a hash table lookup of ust_app_ctx.
2551 * It matches an ust app context based on the context type and, in the case
2552 * of perf counters, their name.
2554 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2556 struct ust_app_ctx
*ctx
;
2557 const struct lttng_ust_context_attr
*key
;
2562 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2566 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2571 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2572 if (strncmp(key
->u
.perf_counter
.name
,
2573 ctx
->ctx
.u
.perf_counter
.name
,
2574 sizeof(key
->u
.perf_counter
.name
))) {
2578 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2579 if (strcmp(key
->u
.app_ctx
.provider_name
,
2580 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2581 strcmp(key
->u
.app_ctx
.ctx_name
,
2582 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2598 * Lookup for an ust app context from an lttng_ust_context.
2600 * Must be called while holding RCU read side lock.
2601 * Return an ust_app_ctx object or NULL on error.
2604 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2605 struct lttng_ust_context_attr
*uctx
)
2607 struct lttng_ht_iter iter
;
2608 struct lttng_ht_node_ulong
*node
;
2609 struct ust_app_ctx
*app_ctx
= NULL
;
2614 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2615 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2616 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2617 node
= lttng_ht_iter_get_node_ulong(&iter
);
2622 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2629 * Create a context for the channel on the tracer.
2631 * Called with UST app session lock held and a RCU read side lock.
2634 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2635 struct lttng_ust_context_attr
*uctx
,
2636 struct ust_app
*app
)
2639 struct ust_app_ctx
*ua_ctx
;
2641 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2643 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2649 ua_ctx
= alloc_ust_app_ctx(uctx
);
2650 if (ua_ctx
== NULL
) {
2656 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2657 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2658 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2660 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2670 * Enable on the tracer side a ust app event for the session and channel.
2672 * Called with UST app session lock held.
2675 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2676 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2680 ret
= enable_ust_object(app
, ua_event
->obj
);
2685 ua_event
->enabled
= 1;
2692 * Disable on the tracer side a ust app event for the session and channel.
2694 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2695 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2699 ret
= disable_ust_object(app
, ua_event
->obj
);
2704 ua_event
->enabled
= 0;
2711 * Lookup ust app channel for session and disable it on the tracer side.
2714 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2715 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2719 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2724 ua_chan
->enabled
= 0;
2731 * Lookup ust app channel for session and enable it on the tracer side. This
2732 * MUST be called with a RCU read side lock acquired.
2734 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2735 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2738 struct lttng_ht_iter iter
;
2739 struct lttng_ht_node_str
*ua_chan_node
;
2740 struct ust_app_channel
*ua_chan
;
2742 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2743 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2744 if (ua_chan_node
== NULL
) {
2745 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2746 uchan
->name
, ua_sess
->tracing_id
);
2750 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2752 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2762 * Ask the consumer to create a channel and get it if successful.
2764 * Called with UST app session lock held.
2766 * Return 0 on success or else a negative value.
2768 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2769 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2770 int bitness
, struct ust_registry_session
*registry
,
2771 uint64_t trace_archive_id
)
2774 unsigned int nb_fd
= 0;
2775 struct consumer_socket
*socket
;
2783 health_code_update();
2785 /* Get the right consumer socket for the application. */
2786 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2792 health_code_update();
2794 /* Need one fd for the channel. */
2795 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2797 ERR("Exhausted number of available FD upon create channel");
2802 * Ask consumer to create channel. The consumer will return the number of
2803 * stream we have to expect.
2805 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2806 registry
, usess
->current_trace_chunk
);
2812 * Compute the number of fd needed before receiving them. It must be 2 per
2813 * stream (2 being the default value here).
2815 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2817 /* Reserve the amount of file descriptor we need. */
2818 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2820 ERR("Exhausted number of available FD upon create channel");
2821 goto error_fd_get_stream
;
2824 health_code_update();
2827 * Now get the channel from the consumer. This call wil populate the stream
2828 * list of that channel and set the ust objects.
2830 if (usess
->consumer
->enabled
) {
2831 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2841 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2842 error_fd_get_stream
:
2844 * Initiate a destroy channel on the consumer since we had an error
2845 * handling it on our side. The return value is of no importance since we
2846 * already have a ret value set by the previous error that we need to
2849 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2851 lttng_fd_put(LTTNG_FD_APPS
, 1);
2853 health_code_update();
2859 * Duplicate the ust data object of the ust app stream and save it in the
2860 * buffer registry stream.
2862 * Return 0 on success or else a negative value.
2864 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2865 struct ust_app_stream
*stream
)
2872 /* Reserve the amount of file descriptor we need. */
2873 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2875 ERR("Exhausted number of available FD upon duplicate stream");
2879 /* Duplicate object for stream once the original is in the registry. */
2880 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2881 reg_stream
->obj
.ust
);
2883 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2884 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2885 lttng_fd_put(LTTNG_FD_APPS
, 2);
2888 stream
->handle
= stream
->obj
->handle
;
2895 * Duplicate the ust data object of the ust app. channel and save it in the
2896 * buffer registry channel.
2898 * Return 0 on success or else a negative value.
2900 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2901 struct ust_app_channel
*ua_chan
)
2908 /* Need two fds for the channel. */
2909 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2911 ERR("Exhausted number of available FD upon duplicate channel");
2915 /* Duplicate object for stream once the original is in the registry. */
2916 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2918 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2919 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2922 ua_chan
->handle
= ua_chan
->obj
->handle
;
2927 lttng_fd_put(LTTNG_FD_APPS
, 1);
2933 * For a given channel buffer registry, setup all streams of the given ust
2934 * application channel.
2936 * Return 0 on success or else a negative value.
2938 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2939 struct ust_app_channel
*ua_chan
,
2940 struct ust_app
*app
)
2943 struct ust_app_stream
*stream
, *stmp
;
2948 DBG2("UST app setup buffer registry stream");
2950 /* Send all streams to application. */
2951 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2952 struct buffer_reg_stream
*reg_stream
;
2954 ret
= buffer_reg_stream_create(®_stream
);
2960 * Keep original pointer and nullify it in the stream so the delete
2961 * stream call does not release the object.
2963 reg_stream
->obj
.ust
= stream
->obj
;
2965 buffer_reg_stream_add(reg_stream
, reg_chan
);
2967 /* We don't need the streams anymore. */
2968 cds_list_del(&stream
->list
);
2969 delete_ust_app_stream(-1, stream
, app
);
2977 * Create a buffer registry channel for the given session registry and
2978 * application channel object. If regp pointer is valid, it's set with the
2979 * created object. Important, the created object is NOT added to the session
2980 * registry hash table.
2982 * Return 0 on success else a negative value.
2984 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2985 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2988 struct buffer_reg_channel
*reg_chan
= NULL
;
2993 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2995 /* Create buffer registry channel. */
2996 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
3001 reg_chan
->consumer_key
= ua_chan
->key
;
3002 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3003 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3005 /* Create and add a channel registry to session. */
3006 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
3007 ua_chan
->tracing_channel_id
);
3011 buffer_reg_channel_add(reg_sess
, reg_chan
);
3020 /* Safe because the registry channel object was not added to any HT. */
3021 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3027 * Setup buffer registry channel for the given session registry and application
3028 * channel object. If regp pointer is valid, it's set with the created object.
3030 * Return 0 on success else a negative value.
3032 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3033 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
3034 struct ust_app
*app
)
3041 assert(ua_chan
->obj
);
3043 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3045 /* Setup all streams for the registry. */
3046 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
3051 reg_chan
->obj
.ust
= ua_chan
->obj
;
3052 ua_chan
->obj
= NULL
;
3057 buffer_reg_channel_remove(reg_sess
, reg_chan
);
3058 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3063 * Send buffer registry channel to the application.
3065 * Return 0 on success else a negative value.
3067 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
3068 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
3069 struct ust_app_channel
*ua_chan
)
3072 struct buffer_reg_stream
*reg_stream
;
3079 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3081 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
3086 /* Send channel to the application. */
3087 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3088 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3089 ret
= -ENOTCONN
; /* Caused by app exiting. */
3091 } else if (ret
< 0) {
3095 health_code_update();
3097 /* Send all streams to application. */
3098 pthread_mutex_lock(®_chan
->stream_list_lock
);
3099 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
3100 struct ust_app_stream stream
;
3102 ret
= duplicate_stream_object(reg_stream
, &stream
);
3104 goto error_stream_unlock
;
3107 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3109 (void) release_ust_app_stream(-1, &stream
, app
);
3110 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3111 ret
= -ENOTCONN
; /* Caused by app exiting. */
3113 goto error_stream_unlock
;
3117 * The return value is not important here. This function will output an
3120 (void) release_ust_app_stream(-1, &stream
, app
);
3122 ua_chan
->is_sent
= 1;
3124 error_stream_unlock
:
3125 pthread_mutex_unlock(®_chan
->stream_list_lock
);
3131 * Create and send to the application the created buffers with per UID buffers.
3133 * This MUST be called with a RCU read side lock acquired.
3134 * The session list lock and the session's lock must be acquired.
3136 * Return 0 on success else a negative value.
3138 static int create_channel_per_uid(struct ust_app
*app
,
3139 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3140 struct ust_app_channel
*ua_chan
)
3143 struct buffer_reg_uid
*reg_uid
;
3144 struct buffer_reg_channel
*reg_chan
;
3145 struct ltt_session
*session
= NULL
;
3146 enum lttng_error_code notification_ret
;
3147 struct ust_registry_channel
*chan_reg
;
3154 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3156 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3158 * The session creation handles the creation of this global registry
3159 * object. If none can be find, there is a code flow problem or a
3164 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3170 /* Create the buffer registry channel object. */
3171 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
3173 ERR("Error creating the UST channel \"%s\" registry instance",
3178 session
= session_find_by_id(ua_sess
->tracing_id
);
3180 assert(pthread_mutex_trylock(&session
->lock
));
3181 assert(session_trylock_list());
3184 * Create the buffers on the consumer side. This call populates the
3185 * ust app channel object with all streams and data object.
3187 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3188 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
3189 session
->most_recent_chunk_id
.value
);
3191 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3195 * Let's remove the previously created buffer registry channel so
3196 * it's not visible anymore in the session registry.
3198 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3199 ua_chan
->tracing_channel_id
, false);
3200 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
3201 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3206 * Setup the streams and add it to the session registry.
3208 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3209 ua_chan
, reg_chan
, app
);
3211 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3215 /* Notify the notification subsystem of the channel's creation. */
3216 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
3217 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3218 ua_chan
->tracing_channel_id
);
3220 chan_reg
->consumer_key
= ua_chan
->key
;
3222 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
3224 notification_ret
= notification_thread_command_add_channel(
3225 notification_thread_handle
, session
->name
,
3226 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3227 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3229 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3230 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3231 if (notification_ret
!= LTTNG_OK
) {
3232 ret
= - (int) notification_ret
;
3233 ERR("Failed to add channel to notification thread");
3238 /* Send buffers to the application. */
3239 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
3241 if (ret
!= -ENOTCONN
) {
3242 ERR("Error sending channel to application");
3249 session_put(session
);
3255 * Create and send to the application the created buffers with per PID buffers.
3257 * Called with UST app session lock held.
3258 * The session list lock and the session's lock must be acquired.
3260 * Return 0 on success else a negative value.
3262 static int create_channel_per_pid(struct ust_app
*app
,
3263 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3264 struct ust_app_channel
*ua_chan
)
3267 struct ust_registry_session
*registry
;
3268 enum lttng_error_code cmd_ret
;
3269 struct ltt_session
*session
= NULL
;
3270 uint64_t chan_reg_key
;
3271 struct ust_registry_channel
*chan_reg
;
3278 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3282 registry
= get_session_registry(ua_sess
);
3283 /* The UST app session lock is held, registry shall not be null. */
3286 /* Create and add a new channel registry to session. */
3287 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3289 ERR("Error creating the UST channel \"%s\" registry instance",
3294 session
= session_find_by_id(ua_sess
->tracing_id
);
3297 assert(pthread_mutex_trylock(&session
->lock
));
3298 assert(session_trylock_list());
3300 /* Create and get channel on the consumer side. */
3301 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3302 app
->bits_per_long
, registry
,
3303 session
->most_recent_chunk_id
.value
);
3305 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3307 goto error_remove_from_registry
;
3310 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3312 if (ret
!= -ENOTCONN
) {
3313 ERR("Error sending channel to application");
3315 goto error_remove_from_registry
;
3318 chan_reg_key
= ua_chan
->key
;
3319 pthread_mutex_lock(®istry
->lock
);
3320 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3322 chan_reg
->consumer_key
= ua_chan
->key
;
3323 pthread_mutex_unlock(®istry
->lock
);
3325 cmd_ret
= notification_thread_command_add_channel(
3326 notification_thread_handle
, session
->name
,
3327 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3328 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3330 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3331 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3332 if (cmd_ret
!= LTTNG_OK
) {
3333 ret
= - (int) cmd_ret
;
3334 ERR("Failed to add channel to notification thread");
3335 goto error_remove_from_registry
;
3338 error_remove_from_registry
:
3340 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3345 session_put(session
);
3351 * From an already allocated ust app channel, create the channel buffers if
3352 * needed and send them to the application. This MUST be called with a RCU read
3353 * side lock acquired.
3355 * Called with UST app session lock held.
3357 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3358 * the application exited concurrently.
3360 static int ust_app_channel_send(struct ust_app
*app
,
3361 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3362 struct ust_app_channel
*ua_chan
)
3368 assert(usess
->active
);
3372 /* Handle buffer type before sending the channel to the application. */
3373 switch (usess
->buffer_type
) {
3374 case LTTNG_BUFFER_PER_UID
:
3376 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3382 case LTTNG_BUFFER_PER_PID
:
3384 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3396 /* Initialize ust objd object using the received handle and add it. */
3397 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3398 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3400 /* If channel is not enabled, disable it on the tracer */
3401 if (!ua_chan
->enabled
) {
3402 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3413 * Create UST app channel and return it through ua_chanp if not NULL.
3415 * Called with UST app session lock and RCU read-side lock held.
3417 * Return 0 on success or else a negative value.
3419 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3420 struct ltt_ust_channel
*uchan
,
3421 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3422 struct ust_app_channel
**ua_chanp
)
3425 struct lttng_ht_iter iter
;
3426 struct lttng_ht_node_str
*ua_chan_node
;
3427 struct ust_app_channel
*ua_chan
;
3429 /* Lookup channel in the ust app session */
3430 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3431 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3432 if (ua_chan_node
!= NULL
) {
3433 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3437 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3438 if (ua_chan
== NULL
) {
3439 /* Only malloc can fail here */
3443 shadow_copy_channel(ua_chan
, uchan
);
3445 /* Set channel type. */
3446 ua_chan
->attr
.type
= type
;
3448 /* Only add the channel if successful on the tracer side. */
3449 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3452 *ua_chanp
= ua_chan
;
3455 /* Everything went well. */
3463 * Create UST app event and create it on the tracer side.
3465 * Must be called with the RCU read side lock held.
3466 * Called with ust app session mutex held.
3469 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3470 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3471 struct ust_app
*app
)
3474 struct ust_app_event
*ua_event
;
3476 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3477 if (ua_event
== NULL
) {
3478 /* Only failure mode of alloc_ust_app_event(). */
3482 shadow_copy_event(ua_event
, uevent
);
3484 /* Create it on the tracer side */
3485 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3488 * Not found previously means that it does not exist on the
3489 * tracer. If the application reports that the event existed,
3490 * it means there is a bug in the sessiond or lttng-ust
3491 * (or corruption, etc.)
3493 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3494 ERR("Tracer for application reported that an event being created already existed: "
3495 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3497 app
->pid
, app
->ppid
, app
->uid
,
3503 add_unique_ust_app_event(ua_chan
, ua_event
);
3505 DBG2("UST app create event completed: app = '%s' (ppid: %d)",
3506 app
->name
, app
->ppid
);
3512 /* Valid. Calling here is already in a read side lock */
3513 delete_ust_app_event(-1, ua_event
, app
);
3518 * Create UST app event notifier rule and create it on the tracer side.
3520 * Must be called with the RCU read side lock held.
3521 * Called with ust app session mutex held.
3524 int create_ust_app_event_notifier_rule(struct lttng_event_rule
*rule
,
3525 struct ust_app
*app
, uint64_t token
)
3528 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3530 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(rule
, token
);
3531 if (ua_event_notifier_rule
== NULL
) {
3536 /* Create it on the tracer side. */
3537 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3540 * Not found previously means that it does not exist on the
3541 * tracer. If the application reports that the event existed,
3542 * it means there is a bug in the sessiond or lttng-ust
3543 * (or corruption, etc.)
3545 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3546 ERR("Tracer for application reported that an event notifier being created already exists: "
3547 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3549 app
->pid
, app
->ppid
, app
->uid
,
3555 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3556 &ua_event_notifier_rule
->node
);
3558 DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64
,
3559 app
->name
, app
->ppid
, token
);
3565 /* The RCU read side lock is already being held by the caller. */
3566 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3571 * Create UST metadata and open it on the tracer side.
3573 * Called with UST app session lock held and RCU read side lock.
3575 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3576 struct ust_app
*app
, struct consumer_output
*consumer
)
3579 struct ust_app_channel
*metadata
;
3580 struct consumer_socket
*socket
;
3581 struct ust_registry_session
*registry
;
3582 struct ltt_session
*session
= NULL
;
3588 registry
= get_session_registry(ua_sess
);
3589 /* The UST app session is held registry shall not be null. */
3592 pthread_mutex_lock(®istry
->lock
);
3594 /* Metadata already exists for this registry or it was closed previously */
3595 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3600 /* Allocate UST metadata */
3601 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3603 /* malloc() failed */
3608 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3610 /* Need one fd for the channel. */
3611 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3613 ERR("Exhausted number of available FD upon create metadata");
3617 /* Get the right consumer socket for the application. */
3618 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3621 goto error_consumer
;
3625 * Keep metadata key so we can identify it on the consumer side. Assign it
3626 * to the registry *before* we ask the consumer so we avoid the race of the
3627 * consumer requesting the metadata and the ask_channel call on our side
3628 * did not returned yet.
3630 registry
->metadata_key
= metadata
->key
;
3632 session
= session_find_by_id(ua_sess
->tracing_id
);
3635 assert(pthread_mutex_trylock(&session
->lock
));
3636 assert(session_trylock_list());
3639 * Ask the metadata channel creation to the consumer. The metadata object
3640 * will be created by the consumer and kept their. However, the stream is
3641 * never added or monitored until we do a first push metadata to the
3644 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3645 registry
, session
->current_trace_chunk
);
3647 /* Nullify the metadata key so we don't try to close it later on. */
3648 registry
->metadata_key
= 0;
3649 goto error_consumer
;
3653 * The setup command will make the metadata stream be sent to the relayd,
3654 * if applicable, and the thread managing the metadatas. This is important
3655 * because after this point, if an error occurs, the only way the stream
3656 * can be deleted is to be monitored in the consumer.
3658 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3660 /* Nullify the metadata key so we don't try to close it later on. */
3661 registry
->metadata_key
= 0;
3662 goto error_consumer
;
3665 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3666 metadata
->key
, app
->pid
);
3669 lttng_fd_put(LTTNG_FD_APPS
, 1);
3670 delete_ust_app_channel(-1, metadata
, app
);
3672 pthread_mutex_unlock(®istry
->lock
);
3674 session_put(session
);
3680 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3681 * acquired before calling this function.
3683 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3685 struct ust_app
*app
= NULL
;
3686 struct lttng_ht_node_ulong
*node
;
3687 struct lttng_ht_iter iter
;
3689 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3690 node
= lttng_ht_iter_get_node_ulong(&iter
);
3692 DBG2("UST app no found with pid %d", pid
);
3696 DBG2("Found UST app by pid %d", pid
);
3698 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3705 * Allocate and init an UST app object using the registration information and
3706 * the command socket. This is called when the command socket connects to the
3709 * The object is returned on success or else NULL.
3711 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3714 struct ust_app
*lta
= NULL
;
3715 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3720 DBG3("UST app creating application for socket %d", sock
);
3722 if ((msg
->bits_per_long
== 64 &&
3723 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3724 || (msg
->bits_per_long
== 32 &&
3725 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3726 ERR("Registration failed: application \"%s\" (pid: %d) has "
3727 "%d-bit long, but no consumerd for this size is available.\n",
3728 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3733 * Reserve the two file descriptors of the event source pipe. The write
3734 * end will be closed once it is passed to the application, at which
3735 * point a single 'put' will be performed.
3737 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3739 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s' (ppid: %d)",
3740 msg
->name
, (int) msg
->ppid
);
3744 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3745 if (!event_notifier_event_source_pipe
) {
3746 PERROR("Failed to open application event source pipe: '%s' (ppid = %d)",
3747 msg
->name
, msg
->ppid
);
3751 lta
= zmalloc(sizeof(struct ust_app
));
3754 goto error_free_pipe
;
3757 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
3759 lta
->ppid
= msg
->ppid
;
3760 lta
->uid
= msg
->uid
;
3761 lta
->gid
= msg
->gid
;
3763 lta
->bits_per_long
= msg
->bits_per_long
;
3764 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3765 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3766 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3767 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3768 lta
->long_alignment
= msg
->long_alignment
;
3769 lta
->byte_order
= msg
->byte_order
;
3771 lta
->v_major
= msg
->major
;
3772 lta
->v_minor
= msg
->minor
;
3773 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3774 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3775 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3776 lta
->notify_sock
= -1;
3777 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3779 /* Copy name and make sure it's NULL terminated. */
3780 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3781 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3784 * Before this can be called, when receiving the registration information,
3785 * the application compatibility is checked. So, at this point, the
3786 * application can work with this session daemon.
3788 lta
->compatible
= 1;
3790 lta
->pid
= msg
->pid
;
3791 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3793 pthread_mutex_init(<a
->sock_lock
, NULL
);
3794 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3796 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3800 lttng_pipe_destroy(event_notifier_event_source_pipe
);
3801 lttng_fd_put(LTTNG_FD_APPS
, 2);
3807 * For a given application object, add it to every hash table.
3809 void ust_app_add(struct ust_app
*app
)
3812 assert(app
->notify_sock
>= 0);
3814 app
->registration_time
= time(NULL
);
3819 * On a re-registration, we want to kick out the previous registration of
3822 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3825 * The socket _should_ be unique until _we_ call close. So, a add_unique
3826 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3827 * already in the table.
3829 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3831 /* Add application to the notify socket hash table. */
3832 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3833 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3835 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3836 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3837 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3844 * Set the application version into the object.
3846 * Return 0 on success else a negative value either an errno code or a
3847 * LTTng-UST error code.
3849 int ust_app_version(struct ust_app
*app
)
3855 pthread_mutex_lock(&app
->sock_lock
);
3856 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3857 pthread_mutex_unlock(&app
->sock_lock
);
3859 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3860 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3862 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3870 * Setup the base event notifier group.
3872 * Return 0 on success else a negative value either an errno code or a
3873 * LTTng-UST error code.
3875 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
3878 int event_pipe_write_fd
;
3879 struct lttng_ust_object_data
*event_notifier_group
= NULL
;
3880 enum lttng_error_code lttng_ret
;
3884 /* Get the write side of the pipe. */
3885 event_pipe_write_fd
= lttng_pipe_get_writefd(
3886 app
->event_notifier_group
.event_pipe
);
3888 pthread_mutex_lock(&app
->sock_lock
);
3889 ret
= ustctl_create_event_notifier_group(app
->sock
,
3890 event_pipe_write_fd
, &event_notifier_group
);
3891 pthread_mutex_unlock(&app
->sock_lock
);
3893 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3894 ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d",
3895 ret
, app
->sock
, event_pipe_write_fd
);
3897 DBG("Failed to create application event notifier group (application is dead): app socket fd = %d",
3904 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
3906 ERR("Failed to close write end of the application's event source pipe: app = '%s' (ppid = %d)",
3907 app
->name
, app
->ppid
);
3912 * Release the file descriptor that was reserved for the write-end of
3915 lttng_fd_put(LTTNG_FD_APPS
, 1);
3917 lttng_ret
= notification_thread_command_add_tracer_event_source(
3918 notification_thread_handle
,
3919 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
),
3921 if (lttng_ret
!= LTTNG_OK
) {
3922 ERR("Failed to add tracer event source to notification thread");
3927 /* Assign handle only when the complete setup is valid. */
3928 app
->event_notifier_group
.object
= event_notifier_group
;
3932 ustctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
3933 free(app
->event_notifier_group
.object
);
3938 * Unregister app by removing it from the global traceable app list and freeing
3941 * The socket is already closed at this point so no close to sock.
3943 void ust_app_unregister(int sock
)
3945 struct ust_app
*lta
;
3946 struct lttng_ht_node_ulong
*node
;
3947 struct lttng_ht_iter ust_app_sock_iter
;
3948 struct lttng_ht_iter iter
;
3949 struct ust_app_session
*ua_sess
;
3954 /* Get the node reference for a call_rcu */
3955 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3956 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3959 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3960 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3963 * For per-PID buffers, perform "push metadata" and flush all
3964 * application streams before removing app from hash tables,
3965 * ensuring proper behavior of data_pending check.
3966 * Remove sessions so they are not visible during deletion.
3968 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3970 struct ust_registry_session
*registry
;
3972 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3974 /* The session was already removed so scheduled for teardown. */
3978 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3979 (void) ust_app_flush_app_session(lta
, ua_sess
);
3983 * Add session to list for teardown. This is safe since at this point we
3984 * are the only one using this list.
3986 pthread_mutex_lock(&ua_sess
->lock
);
3988 if (ua_sess
->deleted
) {
3989 pthread_mutex_unlock(&ua_sess
->lock
);
3994 * Normally, this is done in the delete session process which is
3995 * executed in the call rcu below. However, upon registration we can't
3996 * afford to wait for the grace period before pushing data or else the
3997 * data pending feature can race between the unregistration and stop
3998 * command where the data pending command is sent *before* the grace
4001 * The close metadata below nullifies the metadata pointer in the
4002 * session so the delete session will NOT push/close a second time.
4004 registry
= get_session_registry(ua_sess
);
4006 /* Push metadata for application before freeing the application. */
4007 (void) push_metadata(registry
, ua_sess
->consumer
);
4010 * Don't ask to close metadata for global per UID buffers. Close
4011 * metadata only on destroy trace session in this case. Also, the
4012 * previous push metadata could have flag the metadata registry to
4013 * close so don't send a close command if closed.
4015 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4016 /* And ask to close it for this session registry. */
4017 (void) close_metadata(registry
, ua_sess
->consumer
);
4020 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4022 pthread_mutex_unlock(&ua_sess
->lock
);
4025 /* Remove application from PID hash table */
4026 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4030 * Remove application from notify hash table. The thread handling the
4031 * notify socket could have deleted the node so ignore on error because
4032 * either way it's valid. The close of that socket is handled by the
4033 * apps_notify_thread.
4035 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4036 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4039 * Ignore return value since the node might have been removed before by an
4040 * add replace during app registration because the PID can be reassigned by
4043 iter
.iter
.node
= <a
->pid_n
.node
;
4044 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4046 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4051 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4058 * Fill events array with all events name of all registered apps.
4060 int ust_app_list_events(struct lttng_event
**events
)
4063 size_t nbmem
, count
= 0;
4064 struct lttng_ht_iter iter
;
4065 struct ust_app
*app
;
4066 struct lttng_event
*tmp_event
;
4068 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4069 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
4070 if (tmp_event
== NULL
) {
4071 PERROR("zmalloc ust app events");
4078 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4079 struct lttng_ust_tracepoint_iter uiter
;
4081 health_code_update();
4083 if (!app
->compatible
) {
4085 * TODO: In time, we should notice the caller of this error by
4086 * telling him that this is a version error.
4090 pthread_mutex_lock(&app
->sock_lock
);
4091 handle
= ustctl_tracepoint_list(app
->sock
);
4093 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4094 ERR("UST app list events getting handle failed for app pid %d",
4097 pthread_mutex_unlock(&app
->sock_lock
);
4101 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
4102 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4103 /* Handle ustctl error. */
4107 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4108 ERR("UST app tp list get failed for app %d with ret %d",
4111 DBG3("UST app tp list get failed. Application is dead");
4113 * This is normal behavior, an application can die during the
4114 * creation process. Don't report an error so the execution can
4115 * continue normally. Continue normal execution.
4120 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4121 if (release_ret
< 0 &&
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
);
4126 pthread_mutex_unlock(&app
->sock_lock
);
4130 health_code_update();
4131 if (count
>= nbmem
) {
4132 /* In case the realloc fails, we free the memory */
4133 struct lttng_event
*new_tmp_event
;
4136 new_nbmem
= nbmem
<< 1;
4137 DBG2("Reallocating event list from %zu to %zu entries",
4139 new_tmp_event
= realloc(tmp_event
,
4140 new_nbmem
* sizeof(struct lttng_event
));
4141 if (new_tmp_event
== NULL
) {
4144 PERROR("realloc ust app events");
4147 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4148 if (release_ret
< 0 &&
4149 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4150 release_ret
!= -EPIPE
) {
4151 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4153 pthread_mutex_unlock(&app
->sock_lock
);
4156 /* Zero the new memory */
4157 memset(new_tmp_event
+ nbmem
, 0,
4158 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4160 tmp_event
= new_tmp_event
;
4162 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
4163 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4164 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
4165 tmp_event
[count
].pid
= app
->pid
;
4166 tmp_event
[count
].enabled
= -1;
4169 ret
= ustctl_release_handle(app
->sock
, handle
);
4170 pthread_mutex_unlock(&app
->sock_lock
);
4171 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4172 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4177 *events
= tmp_event
;
4179 DBG2("UST app list events done (%zu events)", count
);
4184 health_code_update();
4189 * Fill events array with all events name of all registered apps.
4191 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4194 size_t nbmem
, count
= 0;
4195 struct lttng_ht_iter iter
;
4196 struct ust_app
*app
;
4197 struct lttng_event_field
*tmp_event
;
4199 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4200 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
4201 if (tmp_event
== NULL
) {
4202 PERROR("zmalloc ust app event fields");
4209 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4210 struct lttng_ust_field_iter uiter
;
4212 health_code_update();
4214 if (!app
->compatible
) {
4216 * TODO: In time, we should notice the caller of this error by
4217 * telling him that this is a version error.
4221 pthread_mutex_lock(&app
->sock_lock
);
4222 handle
= ustctl_tracepoint_field_list(app
->sock
);
4224 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4225 ERR("UST app list field getting handle failed for app pid %d",
4228 pthread_mutex_unlock(&app
->sock_lock
);
4232 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
4233 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4234 /* Handle ustctl error. */
4238 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4239 ERR("UST app tp list field failed for app %d with ret %d",
4242 DBG3("UST app tp list field failed. Application is dead");
4244 * This is normal behavior, an application can die during the
4245 * creation process. Don't report an error so the execution can
4246 * continue normally. Reset list and count for next app.
4251 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4252 pthread_mutex_unlock(&app
->sock_lock
);
4253 if (release_ret
< 0 &&
4254 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4255 release_ret
!= -EPIPE
) {
4256 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4261 health_code_update();
4262 if (count
>= nbmem
) {
4263 /* In case the realloc fails, we free the memory */
4264 struct lttng_event_field
*new_tmp_event
;
4267 new_nbmem
= nbmem
<< 1;
4268 DBG2("Reallocating event field list from %zu to %zu entries",
4270 new_tmp_event
= realloc(tmp_event
,
4271 new_nbmem
* sizeof(struct lttng_event_field
));
4272 if (new_tmp_event
== NULL
) {
4275 PERROR("realloc ust app event fields");
4278 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4279 pthread_mutex_unlock(&app
->sock_lock
);
4281 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4282 release_ret
!= -EPIPE
) {
4283 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4287 /* Zero the new memory */
4288 memset(new_tmp_event
+ nbmem
, 0,
4289 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4291 tmp_event
= new_tmp_event
;
4294 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
4295 /* Mapping between these enums matches 1 to 1. */
4296 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4297 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4299 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
4300 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4301 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4302 tmp_event
[count
].event
.pid
= app
->pid
;
4303 tmp_event
[count
].event
.enabled
= -1;
4306 ret
= ustctl_release_handle(app
->sock
, handle
);
4307 pthread_mutex_unlock(&app
->sock_lock
);
4309 ret
!= -LTTNG_UST_ERR_EXITING
&&
4311 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4316 *fields
= tmp_event
;
4318 DBG2("UST app list event fields done (%zu events)", count
);
4323 health_code_update();
4328 * Free and clean all traceable apps of the global list.
4330 * Should _NOT_ be called with RCU read-side lock held.
4332 void ust_app_clean_list(void)
4335 struct ust_app
*app
;
4336 struct lttng_ht_iter iter
;
4338 DBG2("UST app cleaning registered apps hash table");
4342 /* Cleanup notify socket hash table */
4343 if (ust_app_ht_by_notify_sock
) {
4344 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4345 notify_sock_n
.node
) {
4347 * Assert that all notifiers are gone as all triggers
4348 * are unregistered prior to this clean-up.
4350 assert(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4352 ust_app_notify_sock_unregister(app
->notify_sock
);
4357 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4358 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4360 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4364 /* Cleanup socket hash table */
4365 if (ust_app_ht_by_sock
) {
4366 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4368 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4375 /* Destroy is done only when the ht is empty */
4377 ht_cleanup_push(ust_app_ht
);
4379 if (ust_app_ht_by_sock
) {
4380 ht_cleanup_push(ust_app_ht_by_sock
);
4382 if (ust_app_ht_by_notify_sock
) {
4383 ht_cleanup_push(ust_app_ht_by_notify_sock
);
4388 * Init UST app hash table.
4390 int ust_app_ht_alloc(void)
4392 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4396 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4397 if (!ust_app_ht_by_sock
) {
4400 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4401 if (!ust_app_ht_by_notify_sock
) {
4408 * For a specific UST session, disable the channel for all registered apps.
4410 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4411 struct ltt_ust_channel
*uchan
)
4414 struct lttng_ht_iter iter
;
4415 struct lttng_ht_node_str
*ua_chan_node
;
4416 struct ust_app
*app
;
4417 struct ust_app_session
*ua_sess
;
4418 struct ust_app_channel
*ua_chan
;
4420 assert(usess
->active
);
4421 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4422 uchan
->name
, usess
->id
);
4426 /* For every registered applications */
4427 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4428 struct lttng_ht_iter uiter
;
4429 if (!app
->compatible
) {
4431 * TODO: In time, we should notice the caller of this error by
4432 * telling him that this is a version error.
4436 ua_sess
= lookup_session_by_app(usess
, app
);
4437 if (ua_sess
== NULL
) {
4442 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4443 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4444 /* If the session if found for the app, the channel must be there */
4445 assert(ua_chan_node
);
4447 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4448 /* The channel must not be already disabled */
4449 assert(ua_chan
->enabled
== 1);
4451 /* Disable channel onto application */
4452 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4454 /* XXX: We might want to report this error at some point... */
4464 * For a specific UST session, enable the channel for all registered apps.
4466 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4467 struct ltt_ust_channel
*uchan
)
4470 struct lttng_ht_iter iter
;
4471 struct ust_app
*app
;
4472 struct ust_app_session
*ua_sess
;
4474 assert(usess
->active
);
4475 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4476 uchan
->name
, usess
->id
);
4480 /* For every registered applications */
4481 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4482 if (!app
->compatible
) {
4484 * TODO: In time, we should notice the caller of this error by
4485 * telling him that this is a version error.
4489 ua_sess
= lookup_session_by_app(usess
, app
);
4490 if (ua_sess
== NULL
) {
4494 /* Enable channel onto application */
4495 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4497 /* XXX: We might want to report this error at some point... */
4507 * Disable an event in a channel and for a specific session.
4509 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4510 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4513 struct lttng_ht_iter iter
, uiter
;
4514 struct lttng_ht_node_str
*ua_chan_node
;
4515 struct ust_app
*app
;
4516 struct ust_app_session
*ua_sess
;
4517 struct ust_app_channel
*ua_chan
;
4518 struct ust_app_event
*ua_event
;
4520 assert(usess
->active
);
4521 DBG("UST app disabling event %s for all apps in channel "
4522 "%s for session id %" PRIu64
,
4523 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4527 /* For all registered applications */
4528 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4529 if (!app
->compatible
) {
4531 * TODO: In time, we should notice the caller of this error by
4532 * telling him that this is a version error.
4536 ua_sess
= lookup_session_by_app(usess
, app
);
4537 if (ua_sess
== NULL
) {
4542 /* Lookup channel in the ust app session */
4543 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4544 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4545 if (ua_chan_node
== NULL
) {
4546 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4547 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4550 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4552 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4553 uevent
->filter
, uevent
->attr
.loglevel
,
4555 if (ua_event
== NULL
) {
4556 DBG2("Event %s not found in channel %s for app pid %d."
4557 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4561 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4563 /* XXX: Report error someday... */
4572 /* The ua_sess lock must be held by the caller. */
4574 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4575 struct ust_app_session
*ua_sess
,
4576 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4577 struct ust_app_channel
**_ua_chan
)
4580 struct ust_app_channel
*ua_chan
= NULL
;
4583 ASSERT_LOCKED(ua_sess
->lock
);
4585 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4586 sizeof(uchan
->name
))) {
4587 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4591 struct ltt_ust_context
*uctx
= NULL
;
4594 * Create channel onto application and synchronize its
4597 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4598 LTTNG_UST_CHAN_PER_CPU
, usess
,
4604 ret
= ust_app_channel_send(app
, usess
,
4611 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4612 ret
= create_ust_app_channel_context(ua_chan
,
4625 * The application's socket is not valid. Either a bad socket
4626 * or a timeout on it. We can't inform the caller that for a
4627 * specific app, the session failed so lets continue here.
4629 ret
= 0; /* Not an error. */
4637 if (ret
== 0 && _ua_chan
) {
4639 * Only return the application's channel on success. Note
4640 * that the channel can still be part of the application's
4641 * channel hashtable on error.
4643 *_ua_chan
= ua_chan
;
4649 * Enable event for a specific session and channel on the tracer.
4651 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4652 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4655 struct lttng_ht_iter iter
, uiter
;
4656 struct lttng_ht_node_str
*ua_chan_node
;
4657 struct ust_app
*app
;
4658 struct ust_app_session
*ua_sess
;
4659 struct ust_app_channel
*ua_chan
;
4660 struct ust_app_event
*ua_event
;
4662 assert(usess
->active
);
4663 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4664 uevent
->attr
.name
, usess
->id
);
4667 * NOTE: At this point, this function is called only if the session and
4668 * channel passed are already created for all apps. and enabled on the
4674 /* For all registered applications */
4675 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4676 if (!app
->compatible
) {
4678 * TODO: In time, we should notice the caller of this error by
4679 * telling him that this is a version error.
4683 ua_sess
= lookup_session_by_app(usess
, app
);
4685 /* The application has problem or is probably dead. */
4689 pthread_mutex_lock(&ua_sess
->lock
);
4691 if (ua_sess
->deleted
) {
4692 pthread_mutex_unlock(&ua_sess
->lock
);
4696 /* Lookup channel in the ust app session */
4697 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4698 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4700 * It is possible that the channel cannot be found is
4701 * the channel/event creation occurs concurrently with
4702 * an application exit.
4704 if (!ua_chan_node
) {
4705 pthread_mutex_unlock(&ua_sess
->lock
);
4709 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4711 /* Get event node */
4712 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4713 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4714 if (ua_event
== NULL
) {
4715 DBG3("UST app enable event %s not found for app PID %d."
4716 "Skipping app", uevent
->attr
.name
, app
->pid
);
4720 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4722 pthread_mutex_unlock(&ua_sess
->lock
);
4726 pthread_mutex_unlock(&ua_sess
->lock
);
4735 * For a specific existing UST session and UST channel, creates the event for
4736 * all registered apps.
4738 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4739 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4742 struct lttng_ht_iter iter
, uiter
;
4743 struct lttng_ht_node_str
*ua_chan_node
;
4744 struct ust_app
*app
;
4745 struct ust_app_session
*ua_sess
;
4746 struct ust_app_channel
*ua_chan
;
4748 assert(usess
->active
);
4749 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4750 uevent
->attr
.name
, usess
->id
);
4754 /* For all registered applications */
4755 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4756 if (!app
->compatible
) {
4758 * TODO: In time, we should notice the caller of this error by
4759 * telling him that this is a version error.
4763 ua_sess
= lookup_session_by_app(usess
, app
);
4765 /* The application has problem or is probably dead. */
4769 pthread_mutex_lock(&ua_sess
->lock
);
4771 if (ua_sess
->deleted
) {
4772 pthread_mutex_unlock(&ua_sess
->lock
);
4776 /* Lookup channel in the ust app session */
4777 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4778 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4779 /* If the channel is not found, there is a code flow error */
4780 assert(ua_chan_node
);
4782 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4784 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4785 pthread_mutex_unlock(&ua_sess
->lock
);
4787 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4788 /* Possible value at this point: -ENOMEM. If so, we stop! */
4791 DBG2("UST app event %s already exist on app PID %d",
4792 uevent
->attr
.name
, app
->pid
);
4802 * Start tracing for a specific UST session and app.
4804 * Called with UST app session lock held.
4808 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4811 struct ust_app_session
*ua_sess
;
4813 DBG("Starting tracing for ust app pid %d", app
->pid
);
4817 if (!app
->compatible
) {
4821 ua_sess
= lookup_session_by_app(usess
, app
);
4822 if (ua_sess
== NULL
) {
4823 /* The session is in teardown process. Ignore and continue. */
4827 pthread_mutex_lock(&ua_sess
->lock
);
4829 if (ua_sess
->deleted
) {
4830 pthread_mutex_unlock(&ua_sess
->lock
);
4834 if (ua_sess
->enabled
) {
4835 pthread_mutex_unlock(&ua_sess
->lock
);
4839 /* Upon restart, we skip the setup, already done */
4840 if (ua_sess
->started
) {
4844 health_code_update();
4847 /* This starts the UST tracing */
4848 pthread_mutex_lock(&app
->sock_lock
);
4849 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4850 pthread_mutex_unlock(&app
->sock_lock
);
4852 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4853 ERR("Error starting tracing for app pid: %d (ret: %d)",
4856 DBG("UST app start session failed. Application is dead.");
4858 * This is normal behavior, an application can die during the
4859 * creation process. Don't report an error so the execution can
4860 * continue normally.
4862 pthread_mutex_unlock(&ua_sess
->lock
);
4868 /* Indicate that the session has been started once */
4869 ua_sess
->started
= 1;
4870 ua_sess
->enabled
= 1;
4872 pthread_mutex_unlock(&ua_sess
->lock
);
4874 health_code_update();
4876 /* Quiescent wait after starting trace */
4877 pthread_mutex_lock(&app
->sock_lock
);
4878 ret
= ustctl_wait_quiescent(app
->sock
);
4879 pthread_mutex_unlock(&app
->sock_lock
);
4880 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4881 ERR("UST app wait quiescent failed for app pid %d ret %d",
4887 health_code_update();
4891 pthread_mutex_unlock(&ua_sess
->lock
);
4893 health_code_update();
4898 * Stop tracing for a specific UST session and app.
4901 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4904 struct ust_app_session
*ua_sess
;
4905 struct ust_registry_session
*registry
;
4907 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4911 if (!app
->compatible
) {
4912 goto end_no_session
;
4915 ua_sess
= lookup_session_by_app(usess
, app
);
4916 if (ua_sess
== NULL
) {
4917 goto end_no_session
;
4920 pthread_mutex_lock(&ua_sess
->lock
);
4922 if (ua_sess
->deleted
) {
4923 pthread_mutex_unlock(&ua_sess
->lock
);
4924 goto end_no_session
;
4928 * If started = 0, it means that stop trace has been called for a session
4929 * that was never started. It's possible since we can have a fail start
4930 * from either the application manager thread or the command thread. Simply
4931 * indicate that this is a stop error.
4933 if (!ua_sess
->started
) {
4934 goto error_rcu_unlock
;
4937 health_code_update();
4939 /* This inhibits UST tracing */
4940 pthread_mutex_lock(&app
->sock_lock
);
4941 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4942 pthread_mutex_unlock(&app
->sock_lock
);
4944 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4945 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4948 DBG("UST app stop session failed. Application is dead.");
4950 * This is normal behavior, an application can die during the
4951 * creation process. Don't report an error so the execution can
4952 * continue normally.
4956 goto error_rcu_unlock
;
4959 health_code_update();
4960 ua_sess
->enabled
= 0;
4962 /* Quiescent wait after stopping trace */
4963 pthread_mutex_lock(&app
->sock_lock
);
4964 ret
= ustctl_wait_quiescent(app
->sock
);
4965 pthread_mutex_unlock(&app
->sock_lock
);
4966 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4967 ERR("UST app wait quiescent failed for app pid %d ret %d",
4971 health_code_update();
4973 registry
= get_session_registry(ua_sess
);
4975 /* The UST app session is held registry shall not be null. */
4978 /* Push metadata for application before freeing the application. */
4979 (void) push_metadata(registry
, ua_sess
->consumer
);
4982 pthread_mutex_unlock(&ua_sess
->lock
);
4985 health_code_update();
4989 pthread_mutex_unlock(&ua_sess
->lock
);
4991 health_code_update();
4996 int ust_app_flush_app_session(struct ust_app
*app
,
4997 struct ust_app_session
*ua_sess
)
4999 int ret
, retval
= 0;
5000 struct lttng_ht_iter iter
;
5001 struct ust_app_channel
*ua_chan
;
5002 struct consumer_socket
*socket
;
5004 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5008 if (!app
->compatible
) {
5009 goto end_not_compatible
;
5012 pthread_mutex_lock(&ua_sess
->lock
);
5014 if (ua_sess
->deleted
) {
5018 health_code_update();
5020 /* Flushing buffers */
5021 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5024 /* Flush buffers and push metadata. */
5025 switch (ua_sess
->buffer_type
) {
5026 case LTTNG_BUFFER_PER_PID
:
5027 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5029 health_code_update();
5030 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5032 ERR("Error flushing consumer channel");
5038 case LTTNG_BUFFER_PER_UID
:
5044 health_code_update();
5047 pthread_mutex_unlock(&ua_sess
->lock
);
5051 health_code_update();
5056 * Flush buffers for all applications for a specific UST session.
5057 * Called with UST session lock held.
5060 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5065 DBG("Flushing session buffers for all ust apps");
5069 /* Flush buffers and push metadata. */
5070 switch (usess
->buffer_type
) {
5071 case LTTNG_BUFFER_PER_UID
:
5073 struct buffer_reg_uid
*reg
;
5074 struct lttng_ht_iter iter
;
5076 /* Flush all per UID buffers associated to that session. */
5077 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5078 struct ust_registry_session
*ust_session_reg
;
5079 struct buffer_reg_channel
*reg_chan
;
5080 struct consumer_socket
*socket
;
5082 /* Get consumer socket to use to push the metadata.*/
5083 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5086 /* Ignore request if no consumer is found for the session. */
5090 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5091 reg_chan
, node
.node
) {
5093 * The following call will print error values so the return
5094 * code is of little importance because whatever happens, we
5095 * have to try them all.
5097 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
5100 ust_session_reg
= reg
->registry
->reg
.ust
;
5101 /* Push metadata. */
5102 (void) push_metadata(ust_session_reg
, usess
->consumer
);
5106 case LTTNG_BUFFER_PER_PID
:
5108 struct ust_app_session
*ua_sess
;
5109 struct lttng_ht_iter iter
;
5110 struct ust_app
*app
;
5112 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5113 ua_sess
= lookup_session_by_app(usess
, app
);
5114 if (ua_sess
== NULL
) {
5117 (void) ust_app_flush_app_session(app
, ua_sess
);
5128 health_code_update();
5133 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5134 struct ust_app_session
*ua_sess
)
5137 struct lttng_ht_iter iter
;
5138 struct ust_app_channel
*ua_chan
;
5139 struct consumer_socket
*socket
;
5141 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5145 if (!app
->compatible
) {
5146 goto end_not_compatible
;
5149 pthread_mutex_lock(&ua_sess
->lock
);
5151 if (ua_sess
->deleted
) {
5155 health_code_update();
5157 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5160 ERR("Failed to find consumer (%" PRIu32
") socket",
5161 app
->bits_per_long
);
5166 /* Clear quiescent state. */
5167 switch (ua_sess
->buffer_type
) {
5168 case LTTNG_BUFFER_PER_PID
:
5169 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5170 ua_chan
, node
.node
) {
5171 health_code_update();
5172 ret
= consumer_clear_quiescent_channel(socket
,
5175 ERR("Error clearing quiescent state for consumer channel");
5181 case LTTNG_BUFFER_PER_UID
:
5188 health_code_update();
5191 pthread_mutex_unlock(&ua_sess
->lock
);
5195 health_code_update();
5200 * Clear quiescent state in each stream for all applications for a
5201 * specific UST session.
5202 * Called with UST session lock held.
5205 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5210 DBG("Clearing stream quiescent state for all ust apps");
5214 switch (usess
->buffer_type
) {
5215 case LTTNG_BUFFER_PER_UID
:
5217 struct lttng_ht_iter iter
;
5218 struct buffer_reg_uid
*reg
;
5221 * Clear quiescent for all per UID buffers associated to
5224 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5225 struct consumer_socket
*socket
;
5226 struct buffer_reg_channel
*reg_chan
;
5228 /* Get associated consumer socket.*/
5229 socket
= consumer_find_socket_by_bitness(
5230 reg
->bits_per_long
, usess
->consumer
);
5233 * Ignore request if no consumer is found for
5239 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5240 &iter
.iter
, reg_chan
, node
.node
) {
5242 * The following call will print error values so
5243 * the return code is of little importance
5244 * because whatever happens, we have to try them
5247 (void) consumer_clear_quiescent_channel(socket
,
5248 reg_chan
->consumer_key
);
5253 case LTTNG_BUFFER_PER_PID
:
5255 struct ust_app_session
*ua_sess
;
5256 struct lttng_ht_iter iter
;
5257 struct ust_app
*app
;
5259 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5261 ua_sess
= lookup_session_by_app(usess
, app
);
5262 if (ua_sess
== NULL
) {
5265 (void) ust_app_clear_quiescent_app_session(app
,
5277 health_code_update();
5282 * Destroy a specific UST session in apps.
5284 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5287 struct ust_app_session
*ua_sess
;
5288 struct lttng_ht_iter iter
;
5289 struct lttng_ht_node_u64
*node
;
5291 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5295 if (!app
->compatible
) {
5299 __lookup_session_by_app(usess
, app
, &iter
);
5300 node
= lttng_ht_iter_get_node_u64(&iter
);
5302 /* Session is being or is deleted. */
5305 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
5307 health_code_update();
5308 destroy_app_session(app
, ua_sess
);
5310 health_code_update();
5312 /* Quiescent wait after stopping trace */
5313 pthread_mutex_lock(&app
->sock_lock
);
5314 ret
= ustctl_wait_quiescent(app
->sock
);
5315 pthread_mutex_unlock(&app
->sock_lock
);
5316 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5317 ERR("UST app wait quiescent failed for app pid %d ret %d",
5322 health_code_update();
5327 * Start tracing for the UST session.
5329 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5331 struct lttng_ht_iter iter
;
5332 struct ust_app
*app
;
5334 DBG("Starting all UST traces");
5337 * Even though the start trace might fail, flag this session active so
5338 * other application coming in are started by default.
5345 * In a start-stop-start use-case, we need to clear the quiescent state
5346 * of each channel set by the prior stop command, thus ensuring that a
5347 * following stop or destroy is sure to grab a timestamp_end near those
5348 * operations, even if the packet is empty.
5350 (void) ust_app_clear_quiescent_session(usess
);
5352 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5353 ust_app_global_update(usess
, app
);
5362 * Start tracing for the UST session.
5363 * Called with UST session lock held.
5365 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5368 struct lttng_ht_iter iter
;
5369 struct ust_app
*app
;
5371 DBG("Stopping all UST traces");
5374 * Even though the stop trace might fail, flag this session inactive so
5375 * other application coming in are not started by default.
5381 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5382 ret
= ust_app_stop_trace(usess
, app
);
5384 /* Continue to next apps even on error */
5389 (void) ust_app_flush_session(usess
);
5397 * Destroy app UST session.
5399 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5402 struct lttng_ht_iter iter
;
5403 struct ust_app
*app
;
5405 DBG("Destroy all UST traces");
5409 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5410 ret
= destroy_trace(usess
, app
);
5412 /* Continue to next apps even on error */
5422 /* The ua_sess lock must be held by the caller. */
5424 int find_or_create_ust_app_channel(
5425 struct ltt_ust_session
*usess
,
5426 struct ust_app_session
*ua_sess
,
5427 struct ust_app
*app
,
5428 struct ltt_ust_channel
*uchan
,
5429 struct ust_app_channel
**ua_chan
)
5432 struct lttng_ht_iter iter
;
5433 struct lttng_ht_node_str
*ua_chan_node
;
5435 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5436 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5438 *ua_chan
= caa_container_of(ua_chan_node
,
5439 struct ust_app_channel
, node
);
5443 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5452 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5453 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5454 struct ust_app
*app
)
5457 struct ust_app_event
*ua_event
= NULL
;
5459 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5460 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5462 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5467 if (ua_event
->enabled
!= uevent
->enabled
) {
5468 ret
= uevent
->enabled
?
5469 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5470 disable_ust_app_event(ua_sess
, ua_event
, app
);
5478 /* Called with RCU read-side lock held. */
5480 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5483 enum lttng_error_code ret_code
;
5484 enum lttng_trigger_status t_status
;
5485 struct lttng_ht_iter app_trigger_iter
;
5486 struct lttng_triggers
*triggers
= NULL
;
5487 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5488 unsigned int count
, i
;
5491 * Currrently, registering or unregistering a trigger with an
5492 * event rule condition causes a full synchronization of the event
5495 * The first step attempts to add an event notifier for all registered
5496 * triggers that apply to the user space tracers. Then, the
5497 * application's event notifiers rules are all checked against the list
5498 * of registered triggers. Any event notifier that doesn't have a
5499 * matching trigger can be assumed to have been disabled.
5501 * All of this is inefficient, but is put in place to get the feature
5502 * rolling as it is simpler at this moment. It will be optimized Soon™
5503 * to allow the state of enabled
5504 * event notifiers to be synchronized in a piece-wise way.
5507 /* Get all triggers using uid 0 (root) */
5508 ret_code
= notification_thread_command_list_triggers(
5509 notification_thread_handle
, 0, &triggers
);
5510 if (ret_code
!= LTTNG_OK
) {
5517 t_status
= lttng_triggers_get_count(triggers
, &count
);
5518 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5523 for (i
= 0; i
< count
; i
++) {
5524 struct lttng_condition
*condition
;
5525 struct lttng_event_rule
*event_rule
;
5526 struct lttng_trigger
*trigger
;
5527 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5528 enum lttng_condition_status condition_status
;
5531 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5534 token
= lttng_trigger_get_tracer_token(trigger
);
5535 condition
= lttng_trigger_get_condition(trigger
);
5537 if (lttng_condition_get_type(condition
) != LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
5538 /* Does not apply */
5542 condition_status
= lttng_condition_event_rule_borrow_rule_mutable(condition
, &event_rule
);
5543 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5545 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5546 /* Skip kernel related triggers. */
5551 * Find or create the associated token event rule. The caller
5552 * holds the RCU read lock, so this is safe to call without
5553 * explicitly acquiring it here.
5555 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
5556 app
->token_to_event_notifier_rule_ht
, token
);
5557 if (!looked_up_event_notifier_rule
) {
5558 ret
= create_ust_app_event_notifier_rule(event_rule
, app
, token
);
5566 /* Remove all unknown event sources from the app. */
5567 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
5568 &app_trigger_iter
.iter
, event_notifier_rule
,
5570 const uint64_t app_token
= event_notifier_rule
->token
;
5574 * Check if the app event trigger still exists on the
5575 * notification side.
5577 for (i
= 0; i
< count
; i
++) {
5578 uint64_t notification_thread_token
;
5579 const struct lttng_trigger
*trigger
=
5580 lttng_triggers_get_at_index(
5585 notification_thread_token
=
5586 lttng_trigger_get_tracer_token(trigger
);
5588 if (notification_thread_token
== app_token
) {
5600 * This trigger was unregistered, disable it on the tracer's
5603 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
5607 /* Callee logs errors. */
5608 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
5610 delete_ust_app_event_notifier_rule(
5611 app
->sock
, event_notifier_rule
, app
);
5617 lttng_triggers_destroy(triggers
);
5622 * The caller must ensure that the application is compatible and is tracked
5623 * by the process attribute trackers.
5626 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5627 struct ust_app
*app
)
5630 struct cds_lfht_iter uchan_iter
;
5631 struct ltt_ust_channel
*uchan
;
5632 struct ust_app_session
*ua_sess
= NULL
;
5635 * The application's configuration should only be synchronized for
5638 assert(usess
->active
);
5640 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5642 /* Tracer is probably gone or ENOMEM. */
5647 pthread_mutex_lock(&ua_sess
->lock
);
5648 if (ua_sess
->deleted
) {
5649 pthread_mutex_unlock(&ua_sess
->lock
);
5655 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5657 struct ust_app_channel
*ua_chan
;
5658 struct cds_lfht_iter uevent_iter
;
5659 struct ltt_ust_event
*uevent
;
5662 * Search for a matching ust_app_channel. If none is found,
5663 * create it. Creating the channel will cause the ua_chan
5664 * structure to be allocated, the channel buffers to be
5665 * allocated (if necessary) and sent to the application, and
5666 * all enabled contexts will be added to the channel.
5668 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5669 app
, uchan
, &ua_chan
);
5671 /* Tracer is probably gone or ENOMEM. */
5676 /* ua_chan will be NULL for the metadata channel */
5680 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5682 ret
= ust_app_channel_synchronize_event(ua_chan
,
5683 uevent
, ua_sess
, app
);
5689 if (ua_chan
->enabled
!= uchan
->enabled
) {
5690 ret
= uchan
->enabled
?
5691 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5692 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5700 * Create the metadata for the application. This returns gracefully if a
5701 * metadata was already set for the session.
5703 * The metadata channel must be created after the data channels as the
5704 * consumer daemon assumes this ordering. When interacting with a relay
5705 * daemon, the consumer will use this assumption to send the
5706 * "STREAMS_SENT" message to the relay daemon.
5708 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5716 pthread_mutex_unlock(&ua_sess
->lock
);
5717 /* Everything went well at this point. */
5722 pthread_mutex_unlock(&ua_sess
->lock
);
5725 destroy_app_session(app
, ua_sess
);
5731 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5733 struct ust_app_session
*ua_sess
;
5735 ua_sess
= lookup_session_by_app(usess
, app
);
5736 if (ua_sess
== NULL
) {
5739 destroy_app_session(app
, ua_sess
);
5743 * Add channels/events from UST global domain to registered apps at sock.
5745 * Called with session lock held.
5746 * Called with RCU read-side lock held.
5748 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5751 assert(usess
->active
);
5753 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5754 app
->sock
, usess
->id
);
5756 if (!app
->compatible
) {
5759 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5761 trace_ust_id_tracker_lookup(
5762 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5764 trace_ust_id_tracker_lookup(
5765 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5768 * Synchronize the application's internal tracing configuration
5769 * and start tracing.
5771 ust_app_synchronize(usess
, app
);
5772 ust_app_start_trace(usess
, app
);
5774 ust_app_global_destroy(usess
, app
);
5779 * Add all event notifiers to an application.
5781 * Called with session lock held.
5782 * Called with RCU read-side lock held.
5784 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
5786 DBG2("UST application global event notifier rules update: app = '%s' (ppid: %d)",
5787 app
->name
, app
->ppid
);
5789 if (!app
->compatible
) {
5793 if (app
->event_notifier_group
.object
== NULL
) {
5794 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s' (ppid: %d)",
5795 app
->name
, app
->ppid
);
5799 ust_app_synchronize_event_notifier_rules(app
);
5803 * Called with session lock held.
5805 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5807 struct lttng_ht_iter iter
;
5808 struct ust_app
*app
;
5811 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5812 ust_app_global_update(usess
, app
);
5817 void ust_app_global_update_all_event_notifier_rules(void)
5819 struct lttng_ht_iter iter
;
5820 struct ust_app
*app
;
5823 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5824 ust_app_global_update_event_notifier_rules(app
);
5831 * Add context to a specific channel for global UST domain.
5833 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5834 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5837 struct lttng_ht_node_str
*ua_chan_node
;
5838 struct lttng_ht_iter iter
, uiter
;
5839 struct ust_app_channel
*ua_chan
= NULL
;
5840 struct ust_app_session
*ua_sess
;
5841 struct ust_app
*app
;
5843 assert(usess
->active
);
5846 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5847 if (!app
->compatible
) {
5849 * TODO: In time, we should notice the caller of this error by
5850 * telling him that this is a version error.
5854 ua_sess
= lookup_session_by_app(usess
, app
);
5855 if (ua_sess
== NULL
) {
5859 pthread_mutex_lock(&ua_sess
->lock
);
5861 if (ua_sess
->deleted
) {
5862 pthread_mutex_unlock(&ua_sess
->lock
);
5866 /* Lookup channel in the ust app session */
5867 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5868 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5869 if (ua_chan_node
== NULL
) {
5872 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5874 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5879 pthread_mutex_unlock(&ua_sess
->lock
);
5887 * Receive registration and populate the given msg structure.
5889 * On success return 0 else a negative value returned by the ustctl call.
5891 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5894 uint32_t pid
, ppid
, uid
, gid
;
5898 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5899 &pid
, &ppid
, &uid
, &gid
,
5900 &msg
->bits_per_long
,
5901 &msg
->uint8_t_alignment
,
5902 &msg
->uint16_t_alignment
,
5903 &msg
->uint32_t_alignment
,
5904 &msg
->uint64_t_alignment
,
5905 &msg
->long_alignment
,
5912 case LTTNG_UST_ERR_EXITING
:
5913 DBG3("UST app recv reg message failed. Application died");
5915 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5916 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5917 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5918 LTTNG_UST_ABI_MINOR_VERSION
);
5921 ERR("UST app recv reg message failed with ret %d", ret
);
5926 msg
->pid
= (pid_t
) pid
;
5927 msg
->ppid
= (pid_t
) ppid
;
5928 msg
->uid
= (uid_t
) uid
;
5929 msg
->gid
= (gid_t
) gid
;
5936 * Return a ust app session object using the application object and the
5937 * session object descriptor has a key. If not found, NULL is returned.
5938 * A RCU read side lock MUST be acquired when calling this function.
5940 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5943 struct lttng_ht_node_ulong
*node
;
5944 struct lttng_ht_iter iter
;
5945 struct ust_app_session
*ua_sess
= NULL
;
5949 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5950 node
= lttng_ht_iter_get_node_ulong(&iter
);
5952 DBG2("UST app session find by objd %d not found", objd
);
5956 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5963 * Return a ust app channel object using the application object and the channel
5964 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5965 * lock MUST be acquired before calling this function.
5967 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5970 struct lttng_ht_node_ulong
*node
;
5971 struct lttng_ht_iter iter
;
5972 struct ust_app_channel
*ua_chan
= NULL
;
5976 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5977 node
= lttng_ht_iter_get_node_ulong(&iter
);
5979 DBG2("UST app channel find by objd %d not found", objd
);
5983 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5990 * Reply to a register channel notification from an application on the notify
5991 * socket. The channel metadata is also created.
5993 * The session UST registry lock is acquired in this function.
5995 * On success 0 is returned else a negative value.
5997 static int reply_ust_register_channel(int sock
, int cobjd
,
5998 size_t nr_fields
, struct ustctl_field
*fields
)
6000 int ret
, ret_code
= 0;
6002 uint64_t chan_reg_key
;
6003 enum ustctl_channel_header type
;
6004 struct ust_app
*app
;
6005 struct ust_app_channel
*ua_chan
;
6006 struct ust_app_session
*ua_sess
;
6007 struct ust_registry_session
*registry
;
6008 struct ust_registry_channel
*chan_reg
;
6012 /* Lookup application. If not found, there is a code flow error. */
6013 app
= find_app_by_notify_sock(sock
);
6015 DBG("Application socket %d is being torn down. Abort event notify",
6018 goto error_rcu_unlock
;
6021 /* Lookup channel by UST object descriptor. */
6022 ua_chan
= find_channel_by_objd(app
, cobjd
);
6024 DBG("Application channel is being torn down. Abort event notify");
6026 goto error_rcu_unlock
;
6029 assert(ua_chan
->session
);
6030 ua_sess
= ua_chan
->session
;
6032 /* Get right session registry depending on the session buffer type. */
6033 registry
= get_session_registry(ua_sess
);
6035 DBG("Application session is being torn down. Abort event notify");
6037 goto error_rcu_unlock
;
6040 /* Depending on the buffer type, a different channel key is used. */
6041 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6042 chan_reg_key
= ua_chan
->tracing_channel_id
;
6044 chan_reg_key
= ua_chan
->key
;
6047 pthread_mutex_lock(®istry
->lock
);
6049 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
6052 if (!chan_reg
->register_done
) {
6054 * TODO: eventually use the registry event count for
6055 * this channel to better guess header type for per-pid
6058 type
= USTCTL_CHANNEL_HEADER_LARGE
;
6059 chan_reg
->nr_ctx_fields
= nr_fields
;
6060 chan_reg
->ctx_fields
= fields
;
6062 chan_reg
->header_type
= type
;
6064 /* Get current already assigned values. */
6065 type
= chan_reg
->header_type
;
6067 /* Channel id is set during the object creation. */
6068 chan_id
= chan_reg
->chan_id
;
6070 /* Append to metadata */
6071 if (!chan_reg
->metadata_dumped
) {
6072 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
6074 ERR("Error appending channel metadata (errno = %d)", ret_code
);
6080 DBG3("UST app replying to register channel key %" PRIu64
6081 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
6084 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
6086 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6087 ERR("UST app reply channel failed with ret %d", ret
);
6089 DBG3("UST app reply channel failed. Application died");
6094 /* This channel registry registration is completed. */
6095 chan_reg
->register_done
= 1;
6098 pthread_mutex_unlock(®istry
->lock
);
6106 * Add event to the UST channel registry. When the event is added to the
6107 * registry, the metadata is also created. Once done, this replies to the
6108 * application with the appropriate error code.
6110 * The session UST registry lock is acquired in the function.
6112 * On success 0 is returned else a negative value.
6114 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
6115 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
6116 int loglevel_value
, char *model_emf_uri
)
6119 uint32_t event_id
= 0;
6120 uint64_t chan_reg_key
;
6121 struct ust_app
*app
;
6122 struct ust_app_channel
*ua_chan
;
6123 struct ust_app_session
*ua_sess
;
6124 struct ust_registry_session
*registry
;
6128 /* Lookup application. If not found, there is a code flow error. */
6129 app
= find_app_by_notify_sock(sock
);
6131 DBG("Application socket %d is being torn down. Abort event notify",
6134 goto error_rcu_unlock
;
6137 /* Lookup channel by UST object descriptor. */
6138 ua_chan
= find_channel_by_objd(app
, cobjd
);
6140 DBG("Application channel is being torn down. Abort event notify");
6142 goto error_rcu_unlock
;
6145 assert(ua_chan
->session
);
6146 ua_sess
= ua_chan
->session
;
6148 registry
= get_session_registry(ua_sess
);
6150 DBG("Application session is being torn down. Abort event notify");
6152 goto error_rcu_unlock
;
6155 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6156 chan_reg_key
= ua_chan
->tracing_channel_id
;
6158 chan_reg_key
= ua_chan
->key
;
6161 pthread_mutex_lock(®istry
->lock
);
6164 * From this point on, this call acquires the ownership of the sig, fields
6165 * and model_emf_uri meaning any free are done inside it if needed. These
6166 * three variables MUST NOT be read/write after this.
6168 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
6169 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
6170 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
6174 model_emf_uri
= NULL
;
6177 * The return value is returned to ustctl so in case of an error, the
6178 * application can be notified. In case of an error, it's important not to
6179 * return a negative error or else the application will get closed.
6181 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
6183 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6184 ERR("UST app reply event failed with ret %d", ret
);
6186 DBG3("UST app reply event failed. Application died");
6189 * No need to wipe the create event since the application socket will
6190 * get close on error hence cleaning up everything by itself.
6195 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6199 pthread_mutex_unlock(®istry
->lock
);
6204 free(model_emf_uri
);
6209 * Add enum to the UST session registry. Once done, this replies to the
6210 * application with the appropriate error code.
6212 * The session UST registry lock is acquired within this function.
6214 * On success 0 is returned else a negative value.
6216 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
6217 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
6219 int ret
= 0, ret_code
;
6220 struct ust_app
*app
;
6221 struct ust_app_session
*ua_sess
;
6222 struct ust_registry_session
*registry
;
6223 uint64_t enum_id
= -1ULL;
6227 /* Lookup application. If not found, there is a code flow error. */
6228 app
= find_app_by_notify_sock(sock
);
6230 /* Return an error since this is not an error */
6231 DBG("Application socket %d is being torn down. Aborting enum registration",
6234 goto error_rcu_unlock
;
6237 /* Lookup session by UST object descriptor. */
6238 ua_sess
= find_session_by_objd(app
, sobjd
);
6240 /* Return an error since this is not an error */
6241 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6243 goto error_rcu_unlock
;
6246 registry
= get_session_registry(ua_sess
);
6248 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6250 goto error_rcu_unlock
;
6253 pthread_mutex_lock(®istry
->lock
);
6256 * From this point on, the callee acquires the ownership of
6257 * entries. The variable entries MUST NOT be read/written after
6260 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
6261 entries
, nr_entries
, &enum_id
);
6265 * The return value is returned to ustctl so in case of an error, the
6266 * application can be notified. In case of an error, it's important not to
6267 * return a negative error or else the application will get closed.
6269 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
6271 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6272 ERR("UST app reply enum failed with ret %d", ret
);
6274 DBG3("UST app reply enum failed. Application died");
6277 * No need to wipe the create enum since the application socket will
6278 * get close on error hence cleaning up everything by itself.
6283 DBG3("UST registry enum %s added successfully or already found", name
);
6286 pthread_mutex_unlock(®istry
->lock
);
6293 * Handle application notification through the given notify socket.
6295 * Return 0 on success or else a negative value.
6297 int ust_app_recv_notify(int sock
)
6300 enum ustctl_notify_cmd cmd
;
6302 DBG3("UST app receiving notify from sock %d", sock
);
6304 ret
= ustctl_recv_notify(sock
, &cmd
);
6306 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6307 ERR("UST app recv notify failed with ret %d", ret
);
6309 DBG3("UST app recv notify failed. Application died");
6315 case USTCTL_NOTIFY_CMD_EVENT
:
6317 int sobjd
, cobjd
, loglevel_value
;
6318 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6320 struct ustctl_field
*fields
;
6322 DBG2("UST app ustctl register event received");
6324 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
6325 &loglevel_value
, &sig
, &nr_fields
, &fields
,
6328 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6329 ERR("UST app recv event failed with ret %d", ret
);
6331 DBG3("UST app recv event failed. Application died");
6337 * Add event to the UST registry coming from the notify socket. This
6338 * call will free if needed the sig, fields and model_emf_uri. This
6339 * code path loses the ownsership of these variables and transfer them
6340 * to the this function.
6342 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6343 fields
, loglevel_value
, model_emf_uri
);
6350 case USTCTL_NOTIFY_CMD_CHANNEL
:
6354 struct ustctl_field
*fields
;
6356 DBG2("UST app ustctl register channel received");
6358 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
6361 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6362 ERR("UST app recv channel failed with ret %d", ret
);
6364 DBG3("UST app recv channel failed. Application died");
6370 * The fields ownership are transfered to this function call meaning
6371 * that if needed it will be freed. After this, it's invalid to access
6372 * fields or clean it up.
6374 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
6382 case USTCTL_NOTIFY_CMD_ENUM
:
6385 char name
[LTTNG_UST_SYM_NAME_LEN
];
6387 struct ustctl_enum_entry
*entries
;
6389 DBG2("UST app ustctl register enum received");
6391 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
6392 &entries
, &nr_entries
);
6394 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6395 ERR("UST app recv enum failed with ret %d", ret
);
6397 DBG3("UST app recv enum failed. Application died");
6402 /* Callee assumes ownership of entries */
6403 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6404 entries
, nr_entries
);
6412 /* Should NEVER happen. */
6421 * Once the notify socket hangs up, this is called. First, it tries to find the
6422 * corresponding application. On failure, the call_rcu to close the socket is
6423 * executed. If an application is found, it tries to delete it from the notify
6424 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6426 * Note that an object needs to be allocated here so on ENOMEM failure, the
6427 * call RCU is not done but the rest of the cleanup is.
6429 void ust_app_notify_sock_unregister(int sock
)
6432 struct lttng_ht_iter iter
;
6433 struct ust_app
*app
;
6434 struct ust_app_notify_sock_obj
*obj
;
6440 obj
= zmalloc(sizeof(*obj
));
6443 * An ENOMEM is kind of uncool. If this strikes we continue the
6444 * procedure but the call_rcu will not be called. In this case, we
6445 * accept the fd leak rather than possibly creating an unsynchronized
6446 * state between threads.
6448 * TODO: The notify object should be created once the notify socket is
6449 * registered and stored independantely from the ust app object. The
6450 * tricky part is to synchronize the teardown of the application and
6451 * this notify object. Let's keep that in mind so we can avoid this
6452 * kind of shenanigans with ENOMEM in the teardown path.
6459 DBG("UST app notify socket unregister %d", sock
);
6462 * Lookup application by notify socket. If this fails, this means that the
6463 * hash table delete has already been done by the application
6464 * unregistration process so we can safely close the notify socket in a
6467 app
= find_app_by_notify_sock(sock
);
6472 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6475 * Whatever happens here either we fail or succeed, in both cases we have
6476 * to close the socket after a grace period to continue to the call RCU
6477 * here. If the deletion is successful, the application is not visible
6478 * anymore by other threads and is it fails it means that it was already
6479 * deleted from the hash table so either way we just have to close the
6482 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6488 * Close socket after a grace period to avoid for the socket to be reused
6489 * before the application object is freed creating potential race between
6490 * threads trying to add unique in the global hash table.
6493 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6498 * Destroy a ust app data structure and free its memory.
6500 void ust_app_destroy(struct ust_app
*app
)
6506 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6510 * Take a snapshot for a given UST session. The snapshot is sent to the given
6513 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6515 enum lttng_error_code
ust_app_snapshot_record(
6516 const struct ltt_ust_session
*usess
,
6517 const struct consumer_output
*output
, int wait
,
6518 uint64_t nb_packets_per_stream
)
6521 enum lttng_error_code status
= LTTNG_OK
;
6522 struct lttng_ht_iter iter
;
6523 struct ust_app
*app
;
6524 char *trace_path
= NULL
;
6531 switch (usess
->buffer_type
) {
6532 case LTTNG_BUFFER_PER_UID
:
6534 struct buffer_reg_uid
*reg
;
6536 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6537 struct buffer_reg_channel
*reg_chan
;
6538 struct consumer_socket
*socket
;
6539 char pathname
[PATH_MAX
];
6540 size_t consumer_path_offset
= 0;
6542 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6543 /* Skip since no metadata is present */
6547 /* Get consumer socket to use to push the metadata.*/
6548 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6551 status
= LTTNG_ERR_INVALID
;
6555 memset(pathname
, 0, sizeof(pathname
));
6556 ret
= snprintf(pathname
, sizeof(pathname
),
6557 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
6558 reg
->uid
, reg
->bits_per_long
);
6560 PERROR("snprintf snapshot path");
6561 status
= LTTNG_ERR_INVALID
;
6564 /* Free path allowed on previous iteration. */
6566 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6567 &consumer_path_offset
);
6569 status
= LTTNG_ERR_INVALID
;
6572 /* Add the UST default trace dir to path. */
6573 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6574 reg_chan
, node
.node
) {
6575 status
= consumer_snapshot_channel(socket
,
6576 reg_chan
->consumer_key
,
6577 output
, 0, usess
->uid
,
6578 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
6579 nb_packets_per_stream
);
6580 if (status
!= LTTNG_OK
) {
6584 status
= consumer_snapshot_channel(socket
,
6585 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6586 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6588 if (status
!= LTTNG_OK
) {
6594 case LTTNG_BUFFER_PER_PID
:
6596 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6597 struct consumer_socket
*socket
;
6598 struct lttng_ht_iter chan_iter
;
6599 struct ust_app_channel
*ua_chan
;
6600 struct ust_app_session
*ua_sess
;
6601 struct ust_registry_session
*registry
;
6602 char pathname
[PATH_MAX
];
6603 size_t consumer_path_offset
= 0;
6605 ua_sess
= lookup_session_by_app(usess
, app
);
6607 /* Session not associated with this app. */
6611 /* Get the right consumer socket for the application. */
6612 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6615 status
= LTTNG_ERR_INVALID
;
6619 /* Add the UST default trace dir to path. */
6620 memset(pathname
, 0, sizeof(pathname
));
6621 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
6624 status
= LTTNG_ERR_INVALID
;
6625 PERROR("snprintf snapshot path");
6628 /* Free path allowed on previous iteration. */
6630 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6631 &consumer_path_offset
);
6633 status
= LTTNG_ERR_INVALID
;
6636 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6637 ua_chan
, node
.node
) {
6638 status
= consumer_snapshot_channel(socket
,
6639 ua_chan
->key
, output
, 0,
6640 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6641 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6642 &trace_path
[consumer_path_offset
], wait
,
6643 nb_packets_per_stream
);
6647 case LTTNG_ERR_CHAN_NOT_FOUND
:
6654 registry
= get_session_registry(ua_sess
);
6656 DBG("Application session is being torn down. Skip application.");
6659 status
= consumer_snapshot_channel(socket
,
6660 registry
->metadata_key
, output
, 1,
6661 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6662 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6663 &trace_path
[consumer_path_offset
], wait
, 0);
6667 case LTTNG_ERR_CHAN_NOT_FOUND
:
6687 * Return the size taken by one more packet per stream.
6689 uint64_t ust_app_get_size_one_more_packet_per_stream(
6690 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6692 uint64_t tot_size
= 0;
6693 struct ust_app
*app
;
6694 struct lttng_ht_iter iter
;
6698 switch (usess
->buffer_type
) {
6699 case LTTNG_BUFFER_PER_UID
:
6701 struct buffer_reg_uid
*reg
;
6703 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6704 struct buffer_reg_channel
*reg_chan
;
6707 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6708 reg_chan
, node
.node
) {
6709 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6711 * Don't take channel into account if we
6712 * already grab all its packets.
6716 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6722 case LTTNG_BUFFER_PER_PID
:
6725 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6726 struct ust_app_channel
*ua_chan
;
6727 struct ust_app_session
*ua_sess
;
6728 struct lttng_ht_iter chan_iter
;
6730 ua_sess
= lookup_session_by_app(usess
, app
);
6732 /* Session not associated with this app. */
6736 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6737 ua_chan
, node
.node
) {
6738 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6740 * Don't take channel into account if we
6741 * already grab all its packets.
6745 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6759 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6760 struct cds_list_head
*buffer_reg_uid_list
,
6761 struct consumer_output
*consumer
, uint64_t uchan_id
,
6762 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6765 uint64_t consumer_chan_key
;
6770 ret
= buffer_reg_uid_consumer_channel_key(
6771 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6779 ret
= consumer_get_lost_packets(ust_session_id
,
6780 consumer_chan_key
, consumer
, lost
);
6782 ret
= consumer_get_discarded_events(ust_session_id
,
6783 consumer_chan_key
, consumer
, discarded
);
6790 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6791 struct ltt_ust_channel
*uchan
,
6792 struct consumer_output
*consumer
, int overwrite
,
6793 uint64_t *discarded
, uint64_t *lost
)
6796 struct lttng_ht_iter iter
;
6797 struct lttng_ht_node_str
*ua_chan_node
;
6798 struct ust_app
*app
;
6799 struct ust_app_session
*ua_sess
;
6800 struct ust_app_channel
*ua_chan
;
6807 * Iterate over every registered applications. Sum counters for
6808 * all applications containing requested session and channel.
6810 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6811 struct lttng_ht_iter uiter
;
6813 ua_sess
= lookup_session_by_app(usess
, app
);
6814 if (ua_sess
== NULL
) {
6819 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6820 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6821 /* If the session is found for the app, the channel must be there */
6822 assert(ua_chan_node
);
6824 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6829 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6836 uint64_t _discarded
;
6838 ret
= consumer_get_discarded_events(usess
->id
,
6839 ua_chan
->key
, consumer
, &_discarded
);
6843 (*discarded
) += _discarded
;
6852 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6853 struct ust_app
*app
)
6856 struct ust_app_session
*ua_sess
;
6858 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6862 ua_sess
= lookup_session_by_app(usess
, app
);
6863 if (ua_sess
== NULL
) {
6864 /* The session is in teardown process. Ignore and continue. */
6868 pthread_mutex_lock(&ua_sess
->lock
);
6870 if (ua_sess
->deleted
) {
6874 pthread_mutex_lock(&app
->sock_lock
);
6875 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6876 pthread_mutex_unlock(&app
->sock_lock
);
6879 pthread_mutex_unlock(&ua_sess
->lock
);
6883 health_code_update();
6888 * Regenerate the statedump for each app in the session.
6890 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6893 struct lttng_ht_iter iter
;
6894 struct ust_app
*app
;
6896 DBG("Regenerating the metadata for all UST apps");
6900 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6901 if (!app
->compatible
) {
6905 ret
= ust_app_regenerate_statedump(usess
, app
);
6907 /* Continue to the next app even on error */
6918 * Rotate all the channels of a session.
6920 * Return LTTNG_OK on success or else an LTTng error code.
6922 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
6925 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6926 struct lttng_ht_iter iter
;
6927 struct ust_app
*app
;
6928 struct ltt_ust_session
*usess
= session
->ust_session
;
6934 switch (usess
->buffer_type
) {
6935 case LTTNG_BUFFER_PER_UID
:
6937 struct buffer_reg_uid
*reg
;
6939 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6940 struct buffer_reg_channel
*reg_chan
;
6941 struct consumer_socket
*socket
;
6943 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6944 /* Skip since no metadata is present */
6948 /* Get consumer socket to use to push the metadata.*/
6949 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6952 cmd_ret
= LTTNG_ERR_INVALID
;
6956 /* Rotate the data channels. */
6957 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6958 reg_chan
, node
.node
) {
6959 ret
= consumer_rotate_channel(socket
,
6960 reg_chan
->consumer_key
,
6961 usess
->uid
, usess
->gid
,
6963 /* is_metadata_channel */ false);
6965 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6970 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6972 ret
= consumer_rotate_channel(socket
,
6973 reg
->registry
->reg
.ust
->metadata_key
,
6974 usess
->uid
, usess
->gid
,
6976 /* is_metadata_channel */ true);
6978 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6984 case LTTNG_BUFFER_PER_PID
:
6986 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6987 struct consumer_socket
*socket
;
6988 struct lttng_ht_iter chan_iter
;
6989 struct ust_app_channel
*ua_chan
;
6990 struct ust_app_session
*ua_sess
;
6991 struct ust_registry_session
*registry
;
6993 ua_sess
= lookup_session_by_app(usess
, app
);
6995 /* Session not associated with this app. */
6999 /* Get the right consumer socket for the application. */
7000 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7003 cmd_ret
= LTTNG_ERR_INVALID
;
7007 registry
= get_session_registry(ua_sess
);
7009 DBG("Application session is being torn down. Skip application.");
7013 /* Rotate the data channels. */
7014 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7015 ua_chan
, node
.node
) {
7016 ret
= consumer_rotate_channel(socket
,
7018 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7019 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7021 /* is_metadata_channel */ false);
7023 /* Per-PID buffer and application going away. */
7024 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7026 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7031 /* Rotate the metadata channel. */
7032 (void) push_metadata(registry
, usess
->consumer
);
7033 ret
= consumer_rotate_channel(socket
,
7034 registry
->metadata_key
,
7035 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7036 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7038 /* is_metadata_channel */ true);
7040 /* Per-PID buffer and application going away. */
7041 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7043 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7061 enum lttng_error_code
ust_app_create_channel_subdirectories(
7062 const struct ltt_ust_session
*usess
)
7064 enum lttng_error_code ret
= LTTNG_OK
;
7065 struct lttng_ht_iter iter
;
7066 enum lttng_trace_chunk_status chunk_status
;
7067 char *pathname_index
;
7070 assert(usess
->current_trace_chunk
);
7073 switch (usess
->buffer_type
) {
7074 case LTTNG_BUFFER_PER_UID
:
7076 struct buffer_reg_uid
*reg
;
7078 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7079 fmt_ret
= asprintf(&pathname_index
,
7080 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7081 reg
->uid
, reg
->bits_per_long
);
7083 ERR("Failed to format channel index directory");
7084 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7089 * Create the index subdirectory which will take care
7090 * of implicitly creating the channel's path.
7092 chunk_status
= lttng_trace_chunk_create_subdirectory(
7093 usess
->current_trace_chunk
,
7095 free(pathname_index
);
7096 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7097 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7103 case LTTNG_BUFFER_PER_PID
:
7105 struct ust_app
*app
;
7108 * Create the toplevel ust/ directory in case no apps are running.
7110 chunk_status
= lttng_trace_chunk_create_subdirectory(
7111 usess
->current_trace_chunk
,
7112 DEFAULT_UST_TRACE_DIR
);
7113 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7114 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7118 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7120 struct ust_app_session
*ua_sess
;
7121 struct ust_registry_session
*registry
;
7123 ua_sess
= lookup_session_by_app(usess
, app
);
7125 /* Session not associated with this app. */
7129 registry
= get_session_registry(ua_sess
);
7131 DBG("Application session is being torn down. Skip application.");
7135 fmt_ret
= asprintf(&pathname_index
,
7136 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7139 ERR("Failed to format channel index directory");
7140 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7144 * Create the index subdirectory which will take care
7145 * of implicitly creating the channel's path.
7147 chunk_status
= lttng_trace_chunk_create_subdirectory(
7148 usess
->current_trace_chunk
,
7150 free(pathname_index
);
7151 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7152 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7169 * Clear all the channels of a session.
7171 * Return LTTNG_OK on success or else an LTTng error code.
7173 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7176 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7177 struct lttng_ht_iter iter
;
7178 struct ust_app
*app
;
7179 struct ltt_ust_session
*usess
= session
->ust_session
;
7185 if (usess
->active
) {
7186 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7187 cmd_ret
= LTTNG_ERR_FATAL
;
7191 switch (usess
->buffer_type
) {
7192 case LTTNG_BUFFER_PER_UID
:
7194 struct buffer_reg_uid
*reg
;
7196 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7197 struct buffer_reg_channel
*reg_chan
;
7198 struct consumer_socket
*socket
;
7200 /* Get consumer socket to use to push the metadata.*/
7201 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7204 cmd_ret
= LTTNG_ERR_INVALID
;
7208 /* Clear the data channels. */
7209 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7210 reg_chan
, node
.node
) {
7211 ret
= consumer_clear_channel(socket
,
7212 reg_chan
->consumer_key
);
7218 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7221 * Clear the metadata channel.
7222 * Metadata channel is not cleared per se but we still need to
7223 * perform a rotation operation on it behind the scene.
7225 ret
= consumer_clear_channel(socket
,
7226 reg
->registry
->reg
.ust
->metadata_key
);
7233 case LTTNG_BUFFER_PER_PID
:
7235 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7236 struct consumer_socket
*socket
;
7237 struct lttng_ht_iter chan_iter
;
7238 struct ust_app_channel
*ua_chan
;
7239 struct ust_app_session
*ua_sess
;
7240 struct ust_registry_session
*registry
;
7242 ua_sess
= lookup_session_by_app(usess
, app
);
7244 /* Session not associated with this app. */
7248 /* Get the right consumer socket for the application. */
7249 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7252 cmd_ret
= LTTNG_ERR_INVALID
;
7256 registry
= get_session_registry(ua_sess
);
7258 DBG("Application session is being torn down. Skip application.");
7262 /* Clear the data channels. */
7263 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7264 ua_chan
, node
.node
) {
7265 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7267 /* Per-PID buffer and application going away. */
7268 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7275 (void) push_metadata(registry
, usess
->consumer
);
7278 * Clear the metadata channel.
7279 * Metadata channel is not cleared per se but we still need to
7280 * perform rotation operation on it behind the scene.
7282 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
7284 /* Per-PID buffer and application going away. */
7285 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7303 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7304 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7307 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7317 * This function skips the metadata channel as the begin/end timestamps of a
7318 * metadata packet are useless.
7320 * Moreover, opening a packet after a "clear" will cause problems for live
7321 * sessions as it will introduce padding that was not part of the first trace
7322 * chunk. The relay daemon expects the content of the metadata stream of
7323 * successive metadata trace chunks to be strict supersets of one another.
7325 * For example, flushing a packet at the beginning of the metadata stream of
7326 * a trace chunk resulting from a "clear" session command will cause the
7327 * size of the metadata stream of the new trace chunk to not match the size of
7328 * the metadata stream of the original chunk. This will confuse the relay
7329 * daemon as the same "offset" in a metadata stream will no longer point
7330 * to the same content.
7332 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7334 enum lttng_error_code ret
= LTTNG_OK
;
7335 struct lttng_ht_iter iter
;
7336 struct ltt_ust_session
*usess
= session
->ust_session
;
7342 switch (usess
->buffer_type
) {
7343 case LTTNG_BUFFER_PER_UID
:
7345 struct buffer_reg_uid
*reg
;
7347 cds_list_for_each_entry (
7348 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7349 struct buffer_reg_channel
*reg_chan
;
7350 struct consumer_socket
*socket
;
7352 socket
= consumer_find_socket_by_bitness(
7353 reg
->bits_per_long
, usess
->consumer
);
7355 ret
= LTTNG_ERR_FATAL
;
7359 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7360 &iter
.iter
, reg_chan
, node
.node
) {
7361 const int open_ret
=
7362 consumer_open_channel_packets(
7364 reg_chan
->consumer_key
);
7367 ret
= LTTNG_ERR_UNK
;
7374 case LTTNG_BUFFER_PER_PID
:
7376 struct ust_app
*app
;
7378 cds_lfht_for_each_entry (
7379 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7380 struct consumer_socket
*socket
;
7381 struct lttng_ht_iter chan_iter
;
7382 struct ust_app_channel
*ua_chan
;
7383 struct ust_app_session
*ua_sess
;
7384 struct ust_registry_session
*registry
;
7386 ua_sess
= lookup_session_by_app(usess
, app
);
7388 /* Session not associated with this app. */
7392 /* Get the right consumer socket for the application. */
7393 socket
= consumer_find_socket_by_bitness(
7394 app
->bits_per_long
, usess
->consumer
);
7396 ret
= LTTNG_ERR_FATAL
;
7400 registry
= get_session_registry(ua_sess
);
7402 DBG("Application session is being torn down. Skip application.");
7406 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
7407 &chan_iter
.iter
, ua_chan
, node
.node
) {
7408 const int open_ret
=
7409 consumer_open_channel_packets(
7415 * Per-PID buffer and application going
7418 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7422 ret
= LTTNG_ERR_UNK
;