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
19 #include <sys/types.h>
21 #include <urcu/compiler.h>
24 #include <common/bytecode/bytecode.h>
25 #include <common/compat/errno.h>
26 #include <common/common.h>
27 #include <common/hashtable/utils.h>
28 #include <lttng/event-rule/event-rule.h>
29 #include <lttng/event-rule/event-rule-internal.h>
30 #include <lttng/event-rule/tracepoint.h>
31 #include <lttng/condition/condition.h>
32 #include <lttng/condition/on-event-internal.h>
33 #include <lttng/condition/on-event.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
37 #include "buffer-registry.h"
38 #include "condition-internal.h"
40 #include "health-sessiond.h"
42 #include "ust-consumer.h"
43 #include "lttng-ust-ctl.h"
44 #include "lttng-ust-error.h"
47 #include "lttng-sessiond.h"
48 #include "notification-thread-commands.h"
51 #include "event-notifier-error-accounting.h"
54 struct lttng_ht
*ust_app_ht
;
55 struct lttng_ht
*ust_app_ht_by_sock
;
56 struct lttng_ht
*ust_app_ht_by_notify_sock
;
59 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
61 /* Next available channel key. Access under next_channel_key_lock. */
62 static uint64_t _next_channel_key
;
63 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
65 /* Next available session ID. Access under next_session_id_lock. */
66 static uint64_t _next_session_id
;
67 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
70 * Return the incremented value of next_channel_key.
72 static uint64_t get_next_channel_key(void)
76 pthread_mutex_lock(&next_channel_key_lock
);
77 ret
= ++_next_channel_key
;
78 pthread_mutex_unlock(&next_channel_key_lock
);
83 * Return the atomically incremented value of next_session_id.
85 static uint64_t get_next_session_id(void)
89 pthread_mutex_lock(&next_session_id_lock
);
90 ret
= ++_next_session_id
;
91 pthread_mutex_unlock(&next_session_id_lock
);
95 static void copy_channel_attr_to_ustctl(
96 struct ustctl_consumer_channel_attr
*attr
,
97 struct lttng_ust_abi_channel_attr
*uattr
)
99 /* Copy event attributes since the layout is different. */
100 attr
->subbuf_size
= uattr
->subbuf_size
;
101 attr
->num_subbuf
= uattr
->num_subbuf
;
102 attr
->overwrite
= uattr
->overwrite
;
103 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
104 attr
->read_timer_interval
= uattr
->read_timer_interval
;
105 attr
->output
= uattr
->output
;
106 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
110 * Match function for the hash table lookup.
112 * It matches an ust app event based on three attributes which are the event
113 * name, the filter bytecode and the loglevel.
115 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
117 struct ust_app_event
*event
;
118 const struct ust_app_ht_key
*key
;
119 int ev_loglevel_value
;
124 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
126 ev_loglevel_value
= event
->attr
.loglevel
;
128 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
131 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
135 /* Event loglevel. */
136 if (ev_loglevel_value
!= key
->loglevel_type
) {
137 if (event
->attr
.loglevel_type
== LTTNG_UST_ABI_LOGLEVEL_ALL
138 && key
->loglevel_type
== 0 &&
139 ev_loglevel_value
== -1) {
141 * Match is accepted. This is because on event creation, the
142 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
143 * -1 are accepted for this loglevel type since 0 is the one set by
144 * the API when receiving an enable event.
151 /* One of the filters is NULL, fail. */
152 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
156 if (key
->filter
&& event
->filter
) {
157 /* Both filters exists, check length followed by the bytecode. */
158 if (event
->filter
->len
!= key
->filter
->len
||
159 memcmp(event
->filter
->data
, key
->filter
->data
,
160 event
->filter
->len
) != 0) {
165 /* One of the exclusions is NULL, fail. */
166 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
170 if (key
->exclusion
&& event
->exclusion
) {
171 /* Both exclusions exists, check count followed by the names. */
172 if (event
->exclusion
->count
!= key
->exclusion
->count
||
173 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
174 event
->exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) != 0) {
188 * Unique add of an ust app event in the given ht. This uses the custom
189 * ht_match_ust_app_event match function and the event name as hash.
191 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
192 struct ust_app_event
*event
)
194 struct cds_lfht_node
*node_ptr
;
195 struct ust_app_ht_key key
;
199 assert(ua_chan
->events
);
202 ht
= ua_chan
->events
;
203 key
.name
= event
->attr
.name
;
204 key
.filter
= event
->filter
;
205 key
.loglevel_type
= event
->attr
.loglevel
;
206 key
.exclusion
= event
->exclusion
;
208 node_ptr
= cds_lfht_add_unique(ht
->ht
,
209 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
210 ht_match_ust_app_event
, &key
, &event
->node
.node
);
211 assert(node_ptr
== &event
->node
.node
);
215 * Close the notify socket from the given RCU head object. This MUST be called
216 * through a call_rcu().
218 static void close_notify_sock_rcu(struct rcu_head
*head
)
221 struct ust_app_notify_sock_obj
*obj
=
222 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
224 /* Must have a valid fd here. */
225 assert(obj
->fd
>= 0);
227 ret
= close(obj
->fd
);
229 ERR("close notify sock %d RCU", obj
->fd
);
231 lttng_fd_put(LTTNG_FD_APPS
, 1);
237 * Return the session registry according to the buffer type of the given
240 * A registry per UID object MUST exists before calling this function or else
241 * it assert() if not found. RCU read side lock must be acquired.
243 static struct ust_registry_session
*get_session_registry(
244 struct ust_app_session
*ua_sess
)
246 struct ust_registry_session
*registry
= NULL
;
250 switch (ua_sess
->buffer_type
) {
251 case LTTNG_BUFFER_PER_PID
:
253 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
257 registry
= reg_pid
->registry
->reg
.ust
;
260 case LTTNG_BUFFER_PER_UID
:
262 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
263 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
264 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
268 registry
= reg_uid
->registry
->reg
.ust
;
280 * Delete ust context safely. RCU read lock must be held before calling
284 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
292 pthread_mutex_lock(&app
->sock_lock
);
293 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
294 pthread_mutex_unlock(&app
->sock_lock
);
295 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
296 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
297 sock
, ua_ctx
->obj
->handle
, ret
);
305 * Delete ust app event safely. RCU read lock must be held before calling
309 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
316 free(ua_event
->filter
);
317 if (ua_event
->exclusion
!= NULL
)
318 free(ua_event
->exclusion
);
319 if (ua_event
->obj
!= NULL
) {
320 pthread_mutex_lock(&app
->sock_lock
);
321 ret
= ustctl_release_object(sock
, ua_event
->obj
);
322 pthread_mutex_unlock(&app
->sock_lock
);
323 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
324 ERR("UST app sock %d release event obj failed with ret %d",
333 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
334 * through a call_rcu().
337 void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
339 struct ust_app_event_notifier_rule
*obj
= caa_container_of(
340 head
, struct ust_app_event_notifier_rule
, rcu_head
);
346 * Delete ust app event notifier rule safely.
348 static void delete_ust_app_event_notifier_rule(int sock
,
349 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
,
354 assert(ua_event_notifier_rule
);
356 if (ua_event_notifier_rule
->exclusion
!= NULL
) {
357 free(ua_event_notifier_rule
->exclusion
);
360 if (ua_event_notifier_rule
->obj
!= NULL
) {
361 pthread_mutex_lock(&app
->sock_lock
);
362 ret
= ustctl_release_object(sock
, ua_event_notifier_rule
->obj
);
363 pthread_mutex_unlock(&app
->sock_lock
);
364 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
365 ERR("Failed to release event notifier object: app = '%s' (ppid %d), ret = %d",
366 app
->name
, (int) app
->ppid
, ret
);
369 free(ua_event_notifier_rule
->obj
);
372 lttng_trigger_put(ua_event_notifier_rule
->trigger
);
373 call_rcu(&ua_event_notifier_rule
->rcu_head
,
374 free_ust_app_event_notifier_rule_rcu
);
378 * Release ust data object of the given stream.
380 * Return 0 on success or else a negative value.
382 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
390 pthread_mutex_lock(&app
->sock_lock
);
391 ret
= ustctl_release_object(sock
, stream
->obj
);
392 pthread_mutex_unlock(&app
->sock_lock
);
393 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
394 ERR("UST app sock %d release stream obj failed with ret %d",
397 lttng_fd_put(LTTNG_FD_APPS
, 2);
405 * Delete ust app stream safely. RCU read lock must be held before calling
409 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
414 (void) release_ust_app_stream(sock
, stream
, app
);
419 * We need to execute ht_destroy outside of RCU read-side critical
420 * section and outside of call_rcu thread, so we postpone its execution
421 * using ht_cleanup_push. It is simpler than to change the semantic of
422 * the many callers of delete_ust_app_session().
425 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
427 struct ust_app_channel
*ua_chan
=
428 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
430 ht_cleanup_push(ua_chan
->ctx
);
431 ht_cleanup_push(ua_chan
->events
);
436 * Extract the lost packet or discarded events counter when the channel is
437 * being deleted and store the value in the parent channel so we can
438 * access it from lttng list and at stop/destroy.
440 * The session list lock must be held by the caller.
443 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
445 uint64_t discarded
= 0, lost
= 0;
446 struct ltt_session
*session
;
447 struct ltt_ust_channel
*uchan
;
449 if (ua_chan
->attr
.type
!= LTTNG_UST_ABI_CHAN_PER_CPU
) {
454 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
455 if (!session
|| !session
->ust_session
) {
457 * Not finding the session is not an error because there are
458 * multiple ways the channels can be torn down.
460 * 1) The session daemon can initiate the destruction of the
461 * ust app session after receiving a destroy command or
462 * during its shutdown/teardown.
463 * 2) The application, since we are in per-pid tracing, is
464 * unregistering and tearing down its ust app session.
466 * Both paths are protected by the session list lock which
467 * ensures that the accounting of lost packets and discarded
468 * events is done exactly once. The session is then unpublished
469 * from the session list, resulting in this condition.
474 if (ua_chan
->attr
.overwrite
) {
475 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
476 ua_chan
->key
, session
->ust_session
->consumer
,
479 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
480 ua_chan
->key
, session
->ust_session
->consumer
,
483 uchan
= trace_ust_find_channel_by_name(
484 session
->ust_session
->domain_global
.channels
,
487 ERR("Missing UST channel to store discarded counters");
491 uchan
->per_pid_closed_app_discarded
+= discarded
;
492 uchan
->per_pid_closed_app_lost
+= lost
;
497 session_put(session
);
502 * Delete ust app channel safely. RCU read lock must be held before calling
505 * The session list lock must be held by the caller.
508 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
512 struct lttng_ht_iter iter
;
513 struct ust_app_event
*ua_event
;
514 struct ust_app_ctx
*ua_ctx
;
515 struct ust_app_stream
*stream
, *stmp
;
516 struct ust_registry_session
*registry
;
520 DBG3("UST app deleting channel %s", ua_chan
->name
);
523 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
524 cds_list_del(&stream
->list
);
525 delete_ust_app_stream(sock
, stream
, app
);
529 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
530 cds_list_del(&ua_ctx
->list
);
531 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
533 delete_ust_app_ctx(sock
, ua_ctx
, app
);
537 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
539 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
541 delete_ust_app_event(sock
, ua_event
, app
);
544 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
545 /* Wipe and free registry from session registry. */
546 registry
= get_session_registry(ua_chan
->session
);
548 ust_registry_channel_del_free(registry
, ua_chan
->key
,
552 * A negative socket can be used by the caller when
553 * cleaning-up a ua_chan in an error path. Skip the
554 * accounting in this case.
557 save_per_pid_lost_discarded_counters(ua_chan
);
561 if (ua_chan
->obj
!= NULL
) {
562 /* Remove channel from application UST object descriptor. */
563 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
564 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
566 pthread_mutex_lock(&app
->sock_lock
);
567 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
568 pthread_mutex_unlock(&app
->sock_lock
);
569 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
570 ERR("UST app sock %d release channel obj failed with ret %d",
573 lttng_fd_put(LTTNG_FD_APPS
, 1);
576 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
579 int ust_app_register_done(struct ust_app
*app
)
583 pthread_mutex_lock(&app
->sock_lock
);
584 ret
= ustctl_register_done(app
->sock
);
585 pthread_mutex_unlock(&app
->sock_lock
);
589 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*data
)
594 pthread_mutex_lock(&app
->sock_lock
);
599 ret
= ustctl_release_object(sock
, data
);
601 pthread_mutex_unlock(&app
->sock_lock
);
607 * Push metadata to consumer socket.
609 * RCU read-side lock must be held to guarantee existance of socket.
610 * Must be called with the ust app session lock held.
611 * Must be called with the registry lock held.
613 * On success, return the len of metadata pushed or else a negative value.
614 * Returning a -EPIPE return value means we could not send the metadata,
615 * but it can be caused by recoverable errors (e.g. the application has
616 * terminated concurrently).
618 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
619 struct consumer_socket
*socket
, int send_zero_data
)
622 char *metadata_str
= NULL
;
623 size_t len
, offset
, new_metadata_len_sent
;
625 uint64_t metadata_key
, metadata_version
;
630 metadata_key
= registry
->metadata_key
;
633 * Means that no metadata was assigned to the session. This can
634 * happens if no start has been done previously.
640 offset
= registry
->metadata_len_sent
;
641 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
642 new_metadata_len_sent
= registry
->metadata_len
;
643 metadata_version
= registry
->metadata_version
;
645 DBG3("No metadata to push for metadata key %" PRIu64
,
646 registry
->metadata_key
);
648 if (send_zero_data
) {
649 DBG("No metadata to push");
655 /* Allocate only what we have to send. */
656 metadata_str
= zmalloc(len
);
658 PERROR("zmalloc ust app metadata string");
662 /* Copy what we haven't sent out. */
663 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
666 pthread_mutex_unlock(®istry
->lock
);
668 * We need to unlock the registry while we push metadata to
669 * break a circular dependency between the consumerd metadata
670 * lock and the sessiond registry lock. Indeed, pushing metadata
671 * to the consumerd awaits that it gets pushed all the way to
672 * relayd, but doing so requires grabbing the metadata lock. If
673 * a concurrent metadata request is being performed by
674 * consumerd, this can try to grab the registry lock on the
675 * sessiond while holding the metadata lock on the consumer
676 * daemon. Those push and pull schemes are performed on two
677 * different bidirectionnal communication sockets.
679 ret
= consumer_push_metadata(socket
, metadata_key
,
680 metadata_str
, len
, offset
, metadata_version
);
681 pthread_mutex_lock(®istry
->lock
);
684 * There is an acceptable race here between the registry
685 * metadata key assignment and the creation on the
686 * consumer. The session daemon can concurrently push
687 * metadata for this registry while being created on the
688 * consumer since the metadata key of the registry is
689 * assigned *before* it is setup to avoid the consumer
690 * to ask for metadata that could possibly be not found
691 * in the session daemon.
693 * The metadata will get pushed either by the session
694 * being stopped or the consumer requesting metadata if
695 * that race is triggered.
697 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
700 ERR("Error pushing metadata to consumer");
706 * Metadata may have been concurrently pushed, since
707 * we're not holding the registry lock while pushing to
708 * consumer. This is handled by the fact that we send
709 * the metadata content, size, and the offset at which
710 * that metadata belongs. This may arrive out of order
711 * on the consumer side, and the consumer is able to
712 * deal with overlapping fragments. The consumer
713 * supports overlapping fragments, which must be
714 * contiguous starting from offset 0. We keep the
715 * largest metadata_len_sent value of the concurrent
718 registry
->metadata_len_sent
=
719 max_t(size_t, registry
->metadata_len_sent
,
720 new_metadata_len_sent
);
729 * On error, flag the registry that the metadata is
730 * closed. We were unable to push anything and this
731 * means that either the consumer is not responding or
732 * the metadata cache has been destroyed on the
735 registry
->metadata_closed
= 1;
743 * For a given application and session, push metadata to consumer.
744 * Either sock or consumer is required : if sock is NULL, the default
745 * socket to send the metadata is retrieved from consumer, if sock
746 * is not NULL we use it to send the metadata.
747 * RCU read-side lock must be held while calling this function,
748 * therefore ensuring existance of registry. It also ensures existance
749 * of socket throughout this function.
751 * Return 0 on success else a negative error.
752 * Returning a -EPIPE return value means we could not send the metadata,
753 * but it can be caused by recoverable errors (e.g. the application has
754 * terminated concurrently).
756 static int push_metadata(struct ust_registry_session
*registry
,
757 struct consumer_output
*consumer
)
761 struct consumer_socket
*socket
;
766 pthread_mutex_lock(®istry
->lock
);
767 if (registry
->metadata_closed
) {
772 /* Get consumer socket to use to push the metadata.*/
773 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
780 ret
= ust_app_push_metadata(registry
, socket
, 0);
785 pthread_mutex_unlock(®istry
->lock
);
789 pthread_mutex_unlock(®istry
->lock
);
794 * Send to the consumer a close metadata command for the given session. Once
795 * done, the metadata channel is deleted and the session metadata pointer is
796 * nullified. The session lock MUST be held unless the application is
797 * in the destroy path.
799 * Do not hold the registry lock while communicating with the consumerd, because
800 * doing so causes inter-process deadlocks between consumerd and sessiond with
801 * the metadata request notification.
803 * Return 0 on success else a negative value.
805 static int close_metadata(struct ust_registry_session
*registry
,
806 struct consumer_output
*consumer
)
809 struct consumer_socket
*socket
;
810 uint64_t metadata_key
;
811 bool registry_was_already_closed
;
818 pthread_mutex_lock(®istry
->lock
);
819 metadata_key
= registry
->metadata_key
;
820 registry_was_already_closed
= registry
->metadata_closed
;
821 if (metadata_key
!= 0) {
823 * Metadata closed. Even on error this means that the consumer
824 * is not responding or not found so either way a second close
825 * should NOT be emit for this registry.
827 registry
->metadata_closed
= 1;
829 pthread_mutex_unlock(®istry
->lock
);
831 if (metadata_key
== 0 || registry_was_already_closed
) {
836 /* Get consumer socket to use to push the metadata.*/
837 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
844 ret
= consumer_close_metadata(socket
, metadata_key
);
855 * We need to execute ht_destroy outside of RCU read-side critical
856 * section and outside of call_rcu thread, so we postpone its execution
857 * using ht_cleanup_push. It is simpler than to change the semantic of
858 * the many callers of delete_ust_app_session().
861 void delete_ust_app_session_rcu(struct rcu_head
*head
)
863 struct ust_app_session
*ua_sess
=
864 caa_container_of(head
, struct ust_app_session
, rcu_head
);
866 ht_cleanup_push(ua_sess
->channels
);
871 * Delete ust app session safely. RCU read lock must be held before calling
874 * The session list lock must be held by the caller.
877 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
881 struct lttng_ht_iter iter
;
882 struct ust_app_channel
*ua_chan
;
883 struct ust_registry_session
*registry
;
887 pthread_mutex_lock(&ua_sess
->lock
);
889 assert(!ua_sess
->deleted
);
890 ua_sess
->deleted
= true;
892 registry
= get_session_registry(ua_sess
);
893 /* Registry can be null on error path during initialization. */
895 /* Push metadata for application before freeing the application. */
896 (void) push_metadata(registry
, ua_sess
->consumer
);
899 * Don't ask to close metadata for global per UID buffers. Close
900 * metadata only on destroy trace session in this case. Also, the
901 * previous push metadata could have flag the metadata registry to
902 * close so don't send a close command if closed.
904 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
905 /* And ask to close it for this session registry. */
906 (void) close_metadata(registry
, ua_sess
->consumer
);
910 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
912 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
914 delete_ust_app_channel(sock
, ua_chan
, app
);
917 /* In case of per PID, the registry is kept in the session. */
918 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
919 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
922 * Registry can be null on error path during
925 buffer_reg_pid_remove(reg_pid
);
926 buffer_reg_pid_destroy(reg_pid
);
930 if (ua_sess
->handle
!= -1) {
931 pthread_mutex_lock(&app
->sock_lock
);
932 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
933 pthread_mutex_unlock(&app
->sock_lock
);
934 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
935 ERR("UST app sock %d release session handle failed with ret %d",
938 /* Remove session from application UST object descriptor. */
939 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
940 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
944 pthread_mutex_unlock(&ua_sess
->lock
);
946 consumer_output_put(ua_sess
->consumer
);
948 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
952 * Delete a traceable application structure from the global list. Never call
953 * this function outside of a call_rcu call.
955 * RCU read side lock should _NOT_ be held when calling this function.
958 void delete_ust_app(struct ust_app
*app
)
961 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
962 struct lttng_ht_iter iter
;
963 struct ust_app_event_notifier_rule
*event_notifier_rule
;
964 bool event_notifier_write_fd_is_open
;
967 * The session list lock must be held during this function to guarantee
968 * the existence of ua_sess.
971 /* Delete ust app sessions info */
976 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
978 /* Free every object in the session and the session. */
980 delete_ust_app_session(sock
, ua_sess
, app
);
984 /* Remove the event notifier rules associated with this app. */
986 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
987 &iter
.iter
, event_notifier_rule
, node
.node
) {
988 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
991 delete_ust_app_event_notifier_rule(
992 app
->sock
, event_notifier_rule
, app
);
997 ht_cleanup_push(app
->sessions
);
998 ht_cleanup_push(app
->ust_sessions_objd
);
999 ht_cleanup_push(app
->ust_objd
);
1000 ht_cleanup_push(app
->token_to_event_notifier_rule_ht
);
1003 * This could be NULL if the event notifier setup failed (e.g the app
1004 * was killed or the tracer does not support this feature).
1006 if (app
->event_notifier_group
.object
) {
1007 enum lttng_error_code ret_code
;
1008 enum event_notifier_error_accounting_status status
;
1010 const int event_notifier_read_fd
= lttng_pipe_get_readfd(
1011 app
->event_notifier_group
.event_pipe
);
1013 ret_code
= notification_thread_command_remove_tracer_event_source(
1014 the_notification_thread_handle
,
1015 event_notifier_read_fd
);
1016 if (ret_code
!= LTTNG_OK
) {
1017 ERR("Failed to remove application tracer event source from notification thread");
1020 status
= event_notifier_error_accounting_unregister_app(app
);
1021 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1022 ERR("Error unregistering app from event notifier error accounting");
1025 ustctl_release_object(sock
, app
->event_notifier_group
.object
);
1026 free(app
->event_notifier_group
.object
);
1029 event_notifier_write_fd_is_open
= lttng_pipe_is_write_open(
1030 app
->event_notifier_group
.event_pipe
);
1031 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1033 * Release the file descriptors reserved for the event notifier pipe.
1034 * The app could be destroyed before the write end of the pipe could be
1035 * passed to the application (and closed). In that case, both file
1036 * descriptors must be released.
1038 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1041 * Wait until we have deleted the application from the sock hash table
1042 * before closing this socket, otherwise an application could re-use the
1043 * socket ID and race with the teardown, using the same hash table entry.
1045 * It's OK to leave the close in call_rcu. We want it to stay unique for
1046 * all RCU readers that could run concurrently with unregister app,
1047 * therefore we _need_ to only close that socket after a grace period. So
1048 * it should stay in this RCU callback.
1050 * This close() is a very important step of the synchronization model so
1051 * every modification to this function must be carefully reviewed.
1057 lttng_fd_put(LTTNG_FD_APPS
, 1);
1059 DBG2("UST app pid %d deleted", app
->pid
);
1061 session_unlock_list();
1065 * URCU intermediate call to delete an UST app.
1068 void delete_ust_app_rcu(struct rcu_head
*head
)
1070 struct lttng_ht_node_ulong
*node
=
1071 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1072 struct ust_app
*app
=
1073 caa_container_of(node
, struct ust_app
, pid_n
);
1075 DBG3("Call RCU deleting app PID %d", app
->pid
);
1076 delete_ust_app(app
);
1080 * Delete the session from the application ht and delete the data structure by
1081 * freeing every object inside and releasing them.
1083 * The session list lock must be held by the caller.
1085 static void destroy_app_session(struct ust_app
*app
,
1086 struct ust_app_session
*ua_sess
)
1089 struct lttng_ht_iter iter
;
1094 iter
.iter
.node
= &ua_sess
->node
.node
;
1095 ret
= lttng_ht_del(app
->sessions
, &iter
);
1097 /* Already scheduled for teardown. */
1101 /* Once deleted, free the data structure. */
1102 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1109 * Alloc new UST app session.
1112 struct ust_app_session
*alloc_ust_app_session(void)
1114 struct ust_app_session
*ua_sess
;
1116 /* Init most of the default value by allocating and zeroing */
1117 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1118 if (ua_sess
== NULL
) {
1123 ua_sess
->handle
= -1;
1124 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1125 ua_sess
->metadata_attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1126 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1135 * Alloc new UST app channel.
1138 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1139 struct ust_app_session
*ua_sess
,
1140 struct lttng_ust_abi_channel_attr
*attr
)
1142 struct ust_app_channel
*ua_chan
;
1144 /* Init most of the default value by allocating and zeroing */
1145 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1146 if (ua_chan
== NULL
) {
1151 /* Setup channel name */
1152 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1153 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1155 ua_chan
->enabled
= 1;
1156 ua_chan
->handle
= -1;
1157 ua_chan
->session
= ua_sess
;
1158 ua_chan
->key
= get_next_channel_key();
1159 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1160 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1161 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1163 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1164 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1166 /* Copy attributes */
1168 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1169 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1170 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1171 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1172 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1173 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1174 ua_chan
->attr
.output
= attr
->output
;
1175 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1177 /* By default, the channel is a per cpu channel. */
1178 ua_chan
->attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1180 DBG3("UST app channel %s allocated", ua_chan
->name
);
1189 * Allocate and initialize a UST app stream.
1191 * Return newly allocated stream pointer or NULL on error.
1193 struct ust_app_stream
*ust_app_alloc_stream(void)
1195 struct ust_app_stream
*stream
= NULL
;
1197 stream
= zmalloc(sizeof(*stream
));
1198 if (stream
== NULL
) {
1199 PERROR("zmalloc ust app stream");
1203 /* Zero could be a valid value for a handle so flag it to -1. */
1204 stream
->handle
= -1;
1211 * Alloc new UST app event.
1214 struct ust_app_event
*alloc_ust_app_event(char *name
,
1215 struct lttng_ust_abi_event
*attr
)
1217 struct ust_app_event
*ua_event
;
1219 /* Init most of the default value by allocating and zeroing */
1220 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1221 if (ua_event
== NULL
) {
1222 PERROR("Failed to allocate ust_app_event structure");
1226 ua_event
->enabled
= 1;
1227 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1228 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1229 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1231 /* Copy attributes */
1233 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1236 DBG3("UST app event %s allocated", ua_event
->name
);
1245 * Allocate a new UST app event notifier rule.
1247 static struct ust_app_event_notifier_rule
*alloc_ust_app_event_notifier_rule(
1248 struct lttng_trigger
*trigger
)
1250 enum lttng_event_rule_generate_exclusions_status
1251 generate_exclusion_status
;
1252 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1253 struct lttng_condition
*condition
= NULL
;
1254 const struct lttng_event_rule
*event_rule
= NULL
;
1256 ua_event_notifier_rule
= zmalloc(sizeof(struct ust_app_event_notifier_rule
));
1257 if (ua_event_notifier_rule
== NULL
) {
1258 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1262 ua_event_notifier_rule
->enabled
= 1;
1263 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1264 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
,
1265 ua_event_notifier_rule
->token
);
1267 condition
= lttng_trigger_get_condition(trigger
);
1269 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_ON_EVENT
);
1271 assert(LTTNG_CONDITION_STATUS_OK
== lttng_condition_on_event_get_rule(condition
, &event_rule
));
1274 /* Acquire the event notifier's reference to the trigger. */
1275 lttng_trigger_get(trigger
);
1277 ua_event_notifier_rule
->trigger
= trigger
;
1278 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1279 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1280 event_rule
, &ua_event_notifier_rule
->exclusion
);
1281 switch (generate_exclusion_status
) {
1282 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1283 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1286 /* Error occured. */
1287 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1288 goto error_put_trigger
;
1291 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1292 ua_event_notifier_rule
->token
);
1294 return ua_event_notifier_rule
;
1297 lttng_trigger_put(trigger
);
1299 free(ua_event_notifier_rule
);
1304 * Alloc new UST app context.
1307 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1309 struct ust_app_ctx
*ua_ctx
;
1311 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1312 if (ua_ctx
== NULL
) {
1316 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1319 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1320 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1321 char *provider_name
= NULL
, *ctx_name
= NULL
;
1323 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1324 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1325 if (!provider_name
|| !ctx_name
) {
1326 free(provider_name
);
1331 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1332 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1336 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1344 * Create a liblttng-ust filter bytecode from given bytecode.
1346 * Return allocated filter or NULL on error.
1348 static struct lttng_ust_abi_filter_bytecode
*create_ust_filter_bytecode_from_bytecode(
1349 const struct lttng_bytecode
*orig_f
)
1351 struct lttng_ust_abi_filter_bytecode
*filter
= NULL
;
1353 /* Copy filter bytecode. */
1354 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1356 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1360 assert(sizeof(struct lttng_bytecode
) ==
1361 sizeof(struct lttng_ust_abi_filter_bytecode
));
1362 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1368 * Create a liblttng-ust capture bytecode from given bytecode.
1370 * Return allocated filter or NULL on error.
1372 static struct lttng_ust_abi_capture_bytecode
*
1373 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1375 struct lttng_ust_abi_capture_bytecode
*capture
= NULL
;
1377 /* Copy capture bytecode. */
1378 capture
= zmalloc(sizeof(*capture
) + orig_f
->len
);
1380 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1384 assert(sizeof(struct lttng_bytecode
) ==
1385 sizeof(struct lttng_ust_abi_capture_bytecode
));
1386 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1392 * Find an ust_app using the sock and return it. RCU read side lock must be
1393 * held before calling this helper function.
1395 struct ust_app
*ust_app_find_by_sock(int sock
)
1397 struct lttng_ht_node_ulong
*node
;
1398 struct lttng_ht_iter iter
;
1400 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1401 node
= lttng_ht_iter_get_node_ulong(&iter
);
1403 DBG2("UST app find by sock %d not found", sock
);
1407 return caa_container_of(node
, struct ust_app
, sock_n
);
1414 * Find an ust_app using the notify sock and return it. RCU read side lock must
1415 * be held before calling this helper function.
1417 static struct ust_app
*find_app_by_notify_sock(int sock
)
1419 struct lttng_ht_node_ulong
*node
;
1420 struct lttng_ht_iter iter
;
1422 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1424 node
= lttng_ht_iter_get_node_ulong(&iter
);
1426 DBG2("UST app find by notify sock %d not found", sock
);
1430 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1437 * Lookup for an ust app event based on event name, filter bytecode and the
1440 * Return an ust_app_event object or NULL on error.
1442 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1443 const char *name
, const struct lttng_bytecode
*filter
,
1445 const struct lttng_event_exclusion
*exclusion
)
1447 struct lttng_ht_iter iter
;
1448 struct lttng_ht_node_str
*node
;
1449 struct ust_app_event
*event
= NULL
;
1450 struct ust_app_ht_key key
;
1455 /* Setup key for event lookup. */
1457 key
.filter
= filter
;
1458 key
.loglevel_type
= loglevel_value
;
1459 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1460 key
.exclusion
= exclusion
;
1462 /* Lookup using the event name as hash and a custom match fct. */
1463 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1464 ht_match_ust_app_event
, &key
, &iter
.iter
);
1465 node
= lttng_ht_iter_get_node_str(&iter
);
1470 event
= caa_container_of(node
, struct ust_app_event
, node
);
1477 * Look-up an event notifier rule based on its token id.
1479 * Must be called with the RCU read lock held.
1480 * Return an ust_app_event_notifier_rule object or NULL on error.
1482 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(
1483 struct lttng_ht
*ht
, uint64_t token
)
1485 struct lttng_ht_iter iter
;
1486 struct lttng_ht_node_u64
*node
;
1487 struct ust_app_event_notifier_rule
*event_notifier_rule
= NULL
;
1491 lttng_ht_lookup(ht
, &token
, &iter
);
1492 node
= lttng_ht_iter_get_node_u64(&iter
);
1494 DBG2("UST app event notifier rule token not found: token = %" PRIu64
,
1499 event_notifier_rule
= caa_container_of(
1500 node
, struct ust_app_event_notifier_rule
, node
);
1502 return event_notifier_rule
;
1506 * Create the channel context on the tracer.
1508 * Called with UST app session lock held.
1511 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1512 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1516 health_code_update();
1518 pthread_mutex_lock(&app
->sock_lock
);
1519 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1520 ua_chan
->obj
, &ua_ctx
->obj
);
1521 pthread_mutex_unlock(&app
->sock_lock
);
1523 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1524 ERR("UST app create channel context failed for app (pid: %d) "
1525 "with ret %d", 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("UST app add context failed. Application is dead.");
1538 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1540 DBG2("UST app context handle %d created successfully for channel %s",
1541 ua_ctx
->handle
, ua_chan
->name
);
1544 health_code_update();
1549 * Set the filter on the tracer.
1551 static int set_ust_object_filter(struct ust_app
*app
,
1552 const struct lttng_bytecode
*bytecode
,
1553 struct lttng_ust_abi_object_data
*ust_object
)
1556 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= NULL
;
1558 health_code_update();
1560 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1561 if (!ust_bytecode
) {
1562 ret
= -LTTNG_ERR_NOMEM
;
1565 pthread_mutex_lock(&app
->sock_lock
);
1566 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1568 pthread_mutex_unlock(&app
->sock_lock
);
1570 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1571 ERR("UST app set object filter failed: object = %p of app pid = %d, ret = %d",
1572 ust_object
, app
->pid
, ret
);
1575 * This is normal behavior, an application can die during the
1576 * creation process. Don't report an error so the execution can
1577 * continue normally.
1580 DBG3("Failed to set UST app object filter. Application is dead.");
1585 DBG2("UST filter successfully set: object = %p", ust_object
);
1588 health_code_update();
1594 * Set a capture bytecode for the passed object.
1595 * The sequence number enforces the ordering at runtime and on reception of
1596 * the captured payloads.
1598 static int set_ust_capture(struct ust_app
*app
,
1599 const struct lttng_bytecode
*bytecode
,
1600 unsigned int capture_seqnum
,
1601 struct lttng_ust_abi_object_data
*ust_object
)
1604 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= NULL
;
1606 health_code_update();
1608 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1609 if (!ust_bytecode
) {
1610 ret
= -LTTNG_ERR_NOMEM
;
1615 * Set the sequence number to ensure the capture of fields is ordered.
1617 ust_bytecode
->seqnum
= capture_seqnum
;
1619 pthread_mutex_lock(&app
->sock_lock
);
1620 ret
= ustctl_set_capture(app
->sock
, ust_bytecode
,
1622 pthread_mutex_unlock(&app
->sock_lock
);
1624 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1625 ERR("UST app set object capture failed: object = %p of app pid = %d, ret = %d",
1626 ust_object
, app
->pid
, ret
);
1629 * This is normal behavior, an application can die during the
1630 * creation process. Don't report an error so the execution can
1631 * continue normally.
1634 DBG3("Failed to set UST app object capture. Application is dead.");
1640 DBG2("UST capture successfully set: object = %p", ust_object
);
1643 health_code_update();
1649 struct lttng_ust_abi_event_exclusion
*create_ust_exclusion_from_exclusion(
1650 const struct lttng_event_exclusion
*exclusion
)
1652 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= NULL
;
1653 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1654 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1656 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1657 if (!ust_exclusion
) {
1662 assert(sizeof(struct lttng_event_exclusion
) ==
1663 sizeof(struct lttng_ust_abi_event_exclusion
));
1664 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1666 return ust_exclusion
;
1670 * Set event exclusions on the tracer.
1672 static int set_ust_object_exclusions(struct ust_app
*app
,
1673 const struct lttng_event_exclusion
*exclusions
,
1674 struct lttng_ust_abi_object_data
*ust_object
)
1677 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= NULL
;
1679 assert(exclusions
&& exclusions
->count
> 0);
1681 health_code_update();
1683 ust_exclusions
= create_ust_exclusion_from_exclusion(
1685 if (!ust_exclusions
) {
1686 ret
= -LTTNG_ERR_NOMEM
;
1689 pthread_mutex_lock(&app
->sock_lock
);
1690 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1691 pthread_mutex_unlock(&app
->sock_lock
);
1693 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1694 ERR("Failed to set UST app exclusions for object %p of app (pid: %d) "
1695 "with ret %d", ust_object
, app
->pid
, ret
);
1698 * This is normal behavior, an application can die during the
1699 * creation process. Don't report an error so the execution can
1700 * continue normally.
1703 DBG3("Failed to set UST app object exclusions. Application is dead.");
1708 DBG2("UST exclusions set successfully for object %p", ust_object
);
1711 health_code_update();
1712 free(ust_exclusions
);
1717 * Disable the specified event on to UST tracer for the UST session.
1719 static int disable_ust_object(struct ust_app
*app
,
1720 struct lttng_ust_abi_object_data
*object
)
1724 health_code_update();
1726 pthread_mutex_lock(&app
->sock_lock
);
1727 ret
= ustctl_disable(app
->sock
, object
);
1728 pthread_mutex_unlock(&app
->sock_lock
);
1730 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1731 ERR("Failed to disable UST app object %p app (pid: %d) with ret %d",
1732 object
, app
->pid
, ret
);
1735 * This is normal behavior, an application can die during the
1736 * creation process. Don't report an error so the execution can
1737 * continue normally.
1740 DBG3("Failed to disable UST app object. Application is dead.");
1745 DBG2("UST app object %p disabled successfully for app (pid: %d)",
1749 health_code_update();
1754 * Disable the specified channel on to UST tracer for the UST session.
1756 static int disable_ust_channel(struct ust_app
*app
,
1757 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1761 health_code_update();
1763 pthread_mutex_lock(&app
->sock_lock
);
1764 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1765 pthread_mutex_unlock(&app
->sock_lock
);
1767 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1768 ERR("UST app channel %s disable failed for app (pid: %d) "
1769 "and session handle %d with ret %d",
1770 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1773 * This is normal behavior, an application can die during the
1774 * creation process. Don't report an error so the execution can
1775 * continue normally.
1778 DBG3("UST app disable channel failed. Application is dead.");
1783 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1784 ua_chan
->name
, app
->pid
);
1787 health_code_update();
1792 * Enable the specified channel on to UST tracer for the UST session.
1794 static int enable_ust_channel(struct ust_app
*app
,
1795 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1799 health_code_update();
1801 pthread_mutex_lock(&app
->sock_lock
);
1802 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1803 pthread_mutex_unlock(&app
->sock_lock
);
1805 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1806 ERR("UST app channel %s enable failed for app (pid: %d) "
1807 "and session handle %d with ret %d",
1808 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1811 * This is normal behavior, an application can die during the
1812 * creation process. Don't report an error so the execution can
1813 * continue normally.
1816 DBG3("UST app enable channel failed. Application is dead.");
1821 ua_chan
->enabled
= 1;
1823 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1824 ua_chan
->name
, app
->pid
);
1827 health_code_update();
1832 * Enable the specified event on to UST tracer for the UST session.
1834 static int enable_ust_object(
1835 struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1839 health_code_update();
1841 pthread_mutex_lock(&app
->sock_lock
);
1842 ret
= ustctl_enable(app
->sock
, ust_object
);
1843 pthread_mutex_unlock(&app
->sock_lock
);
1845 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1846 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
1847 ust_object
, app
->pid
, ret
);
1850 * This is normal behavior, an application can die during the
1851 * creation process. Don't report an error so the execution can
1852 * continue normally.
1855 DBG3("Failed to enable UST app object. Application is dead.");
1860 DBG2("UST app object %p enabled successfully for app (pid: %d)",
1861 ust_object
, app
->pid
);
1864 health_code_update();
1869 * Send channel and stream buffer to application.
1871 * Return 0 on success. On error, a negative value is returned.
1873 static int send_channel_pid_to_ust(struct ust_app
*app
,
1874 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1877 struct ust_app_stream
*stream
, *stmp
;
1883 health_code_update();
1885 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1888 /* Send channel to the application. */
1889 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1890 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1891 ret
= -ENOTCONN
; /* Caused by app exiting. */
1893 } else if (ret
< 0) {
1897 health_code_update();
1899 /* Send all streams to application. */
1900 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1901 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1902 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1903 ret
= -ENOTCONN
; /* Caused by app exiting. */
1905 } else if (ret
< 0) {
1908 /* We don't need the stream anymore once sent to the tracer. */
1909 cds_list_del(&stream
->list
);
1910 delete_ust_app_stream(-1, stream
, app
);
1912 /* Flag the channel that it is sent to the application. */
1913 ua_chan
->is_sent
= 1;
1916 health_code_update();
1921 * Create the specified event onto the UST tracer for a UST session.
1923 * Should be called with session mutex held.
1926 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1927 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1931 health_code_update();
1933 /* Create UST event on tracer */
1934 pthread_mutex_lock(&app
->sock_lock
);
1935 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1937 pthread_mutex_unlock(&app
->sock_lock
);
1939 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1941 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1942 ua_event
->attr
.name
, app
->pid
, ret
);
1945 * This is normal behavior, an application can die during the
1946 * creation process. Don't report an error so the execution can
1947 * continue normally.
1950 DBG3("UST app create event failed. Application is dead.");
1955 ua_event
->handle
= ua_event
->obj
->handle
;
1957 DBG2("UST app event %s created successfully for pid:%d object: %p",
1958 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
1960 health_code_update();
1962 /* Set filter if one is present. */
1963 if (ua_event
->filter
) {
1964 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
1970 /* Set exclusions for the event */
1971 if (ua_event
->exclusion
) {
1972 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
1978 /* If event not enabled, disable it on the tracer */
1979 if (ua_event
->enabled
) {
1981 * We now need to explicitly enable the event, since it
1982 * is now disabled at creation.
1984 ret
= enable_ust_object(app
, ua_event
->obj
);
1987 * If we hit an EPERM, something is wrong with our enable call. If
1988 * we get an EEXIST, there is a problem on the tracer side since we
1992 case -LTTNG_UST_ERR_PERM
:
1993 /* Code flow problem */
1995 case -LTTNG_UST_ERR_EXIST
:
1996 /* It's OK for our use case. */
2007 health_code_update();
2011 static int init_ust_event_notifier_from_event_rule(
2012 const struct lttng_event_rule
*rule
,
2013 struct lttng_ust_abi_event_notifier
*event_notifier
)
2015 enum lttng_event_rule_status status
;
2016 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2017 int loglevel
= -1, ret
= 0;
2018 const char *pattern
;
2020 /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported. */
2021 assert(lttng_event_rule_get_type(rule
) ==
2022 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
2024 memset(event_notifier
, 0, sizeof(*event_notifier
));
2026 if (lttng_event_rule_targets_agent_domain(rule
)) {
2028 * Special event for agents
2029 * The actual meat of the event is in the filter that will be
2030 * attached later on.
2031 * Set the default values for the agent event.
2033 pattern
= event_get_default_agent_ust_name(
2034 lttng_event_rule_get_domain_type(rule
));
2036 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2038 const struct lttng_log_level_rule
*log_level_rule
;
2040 status
= lttng_event_rule_tracepoint_get_pattern(rule
, &pattern
);
2041 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2042 /* At this point, this is a fatal error. */
2046 status
= lttng_event_rule_tracepoint_get_log_level_rule(
2047 rule
, &log_level_rule
);
2048 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2049 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2050 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2051 enum lttng_log_level_rule_status llr_status
;
2053 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2054 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2055 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2056 llr_status
= lttng_log_level_rule_exactly_get_level(
2057 log_level_rule
, &loglevel
);
2059 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2060 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2061 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2062 log_level_rule
, &loglevel
);
2068 assert(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2070 /* At this point this is a fatal error. */
2075 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2076 ret
= lttng_strncpy(event_notifier
->event
.name
, pattern
,
2077 LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
2079 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
2084 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2085 event_notifier
->event
.loglevel
= loglevel
;
2091 * Create the specified event notifier against the user space tracer of a
2092 * given application.
2094 static int create_ust_event_notifier(struct ust_app
*app
,
2095 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2098 enum lttng_condition_status condition_status
;
2099 const struct lttng_condition
*condition
= NULL
;
2100 struct lttng_ust_abi_event_notifier event_notifier
;
2101 const struct lttng_event_rule
*event_rule
= NULL
;
2102 unsigned int capture_bytecode_count
= 0, i
;
2103 enum lttng_condition_status cond_status
;
2105 health_code_update();
2106 assert(app
->event_notifier_group
.object
);
2108 condition
= lttng_trigger_get_const_condition(
2109 ua_event_notifier_rule
->trigger
);
2111 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_ON_EVENT
);
2113 condition_status
= lttng_condition_on_event_get_rule(
2114 condition
, &event_rule
);
2115 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2118 assert(lttng_event_rule_get_type(event_rule
) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
2120 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2121 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2122 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2124 /* Create UST event notifier against the tracer. */
2125 pthread_mutex_lock(&app
->sock_lock
);
2126 ret
= ustctl_create_event_notifier(app
->sock
, &event_notifier
,
2127 app
->event_notifier_group
.object
,
2128 &ua_event_notifier_rule
->obj
);
2129 pthread_mutex_unlock(&app
->sock_lock
);
2131 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2132 ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d",
2133 event_notifier
.event
.name
, app
->name
,
2137 * This is normal behavior, an application can die
2138 * during the creation process. Don't report an error so
2139 * the execution can continue normally.
2142 DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)",
2143 app
->name
, app
->ppid
);
2149 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2151 DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p",
2152 event_notifier
.event
.name
, app
->name
, app
->ppid
,
2153 ua_event_notifier_rule
->obj
);
2155 health_code_update();
2157 /* Set filter if one is present. */
2158 if (ua_event_notifier_rule
->filter
) {
2159 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2160 ua_event_notifier_rule
->obj
);
2166 /* Set exclusions for the event. */
2167 if (ua_event_notifier_rule
->exclusion
) {
2168 ret
= set_ust_object_exclusions(app
,
2169 ua_event_notifier_rule
->exclusion
,
2170 ua_event_notifier_rule
->obj
);
2176 /* Set the capture bytecodes. */
2177 cond_status
= lttng_condition_on_event_get_capture_descriptor_count(
2178 condition
, &capture_bytecode_count
);
2179 assert(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2181 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2182 const struct lttng_bytecode
*capture_bytecode
=
2183 lttng_condition_on_event_get_capture_bytecode_at_index(
2186 ret
= set_ust_capture(app
, capture_bytecode
, i
,
2187 ua_event_notifier_rule
->obj
);
2194 * We now need to explicitly enable the event, since it
2195 * is disabled at creation.
2197 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2200 * If we hit an EPERM, something is wrong with our enable call.
2201 * If we get an EEXIST, there is a problem on the tracer side
2202 * since we just created it.
2205 case -LTTNG_UST_ERR_PERM
:
2206 /* Code flow problem. */
2208 case -LTTNG_UST_ERR_EXIST
:
2209 /* It's OK for our use case. */
2219 ua_event_notifier_rule
->enabled
= true;
2222 health_code_update();
2227 * Copy data between an UST app event and a LTT event.
2229 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2230 struct ltt_ust_event
*uevent
)
2232 size_t exclusion_alloc_size
;
2234 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2235 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2237 ua_event
->enabled
= uevent
->enabled
;
2239 /* Copy event attributes */
2240 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2242 /* Copy filter bytecode */
2243 if (uevent
->filter
) {
2244 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2245 /* Filter might be NULL here in case of ENONEM. */
2248 /* Copy exclusion data */
2249 if (uevent
->exclusion
) {
2250 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2251 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2252 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
2253 if (ua_event
->exclusion
== NULL
) {
2256 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2257 exclusion_alloc_size
);
2263 * Copy data between an UST app channel and a LTT channel.
2265 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2266 struct ltt_ust_channel
*uchan
)
2268 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2270 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2271 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2273 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2274 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2276 /* Copy event attributes since the layout is different. */
2277 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2278 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2279 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2280 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2281 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2282 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2283 ua_chan
->attr
.output
= uchan
->attr
.output
;
2284 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2287 * Note that the attribute channel type is not set since the channel on the
2288 * tracing registry side does not have this information.
2291 ua_chan
->enabled
= uchan
->enabled
;
2292 ua_chan
->tracing_channel_id
= uchan
->id
;
2294 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2298 * Copy data between a UST app session and a regular LTT session.
2300 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2301 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2303 struct tm
*timeinfo
;
2306 char tmp_shm_path
[PATH_MAX
];
2308 timeinfo
= localtime(&app
->registration_time
);
2309 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2311 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2313 ua_sess
->tracing_id
= usess
->id
;
2314 ua_sess
->id
= get_next_session_id();
2315 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2316 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2317 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2318 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2319 ua_sess
->buffer_type
= usess
->buffer_type
;
2320 ua_sess
->bits_per_long
= app
->bits_per_long
;
2322 /* There is only one consumer object per session possible. */
2323 consumer_output_get(usess
->consumer
);
2324 ua_sess
->consumer
= usess
->consumer
;
2326 ua_sess
->output_traces
= usess
->output_traces
;
2327 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2328 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2329 &usess
->metadata_attr
);
2331 switch (ua_sess
->buffer_type
) {
2332 case LTTNG_BUFFER_PER_PID
:
2333 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2334 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2337 case LTTNG_BUFFER_PER_UID
:
2338 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2339 DEFAULT_UST_TRACE_UID_PATH
,
2340 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2341 app
->bits_per_long
);
2348 PERROR("asprintf UST shadow copy session");
2353 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2354 sizeof(ua_sess
->root_shm_path
));
2355 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2356 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2357 sizeof(ua_sess
->shm_path
));
2358 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2359 if (ua_sess
->shm_path
[0]) {
2360 switch (ua_sess
->buffer_type
) {
2361 case LTTNG_BUFFER_PER_PID
:
2362 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2363 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2364 app
->name
, app
->pid
, datetime
);
2366 case LTTNG_BUFFER_PER_UID
:
2367 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2368 "/" DEFAULT_UST_TRACE_UID_PATH
,
2369 app
->uid
, app
->bits_per_long
);
2376 PERROR("sprintf UST shadow copy session");
2380 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2381 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2382 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2387 consumer_output_put(ua_sess
->consumer
);
2391 * Lookup sesison wrapper.
2394 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2395 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2397 /* Get right UST app session from app */
2398 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2402 * Return ust app session from the app session hashtable using the UST session
2405 static struct ust_app_session
*lookup_session_by_app(
2406 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2408 struct lttng_ht_iter iter
;
2409 struct lttng_ht_node_u64
*node
;
2411 __lookup_session_by_app(usess
, app
, &iter
);
2412 node
= lttng_ht_iter_get_node_u64(&iter
);
2417 return caa_container_of(node
, struct ust_app_session
, node
);
2424 * Setup buffer registry per PID for the given session and application. If none
2425 * is found, a new one is created, added to the global registry and
2426 * initialized. If regp is valid, it's set with the newly created object.
2428 * Return 0 on success or else a negative value.
2430 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2431 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2434 struct buffer_reg_pid
*reg_pid
;
2441 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2444 * This is the create channel path meaning that if there is NO
2445 * registry available, we have to create one for this session.
2447 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2448 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2456 /* Initialize registry. */
2457 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2458 app
->bits_per_long
, app
->uint8_t_alignment
,
2459 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2460 app
->uint64_t_alignment
, app
->long_alignment
,
2461 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2462 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2463 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2464 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2465 ua_sess
->tracing_id
,
2469 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2470 * destroy the buffer registry, because it is always expected
2471 * that if the buffer registry can be found, its ust registry is
2474 buffer_reg_pid_destroy(reg_pid
);
2478 buffer_reg_pid_add(reg_pid
);
2480 DBG3("UST app buffer registry per PID created successfully");
2492 * Setup buffer registry per UID for the given session and application. If none
2493 * is found, a new one is created, added to the global registry and
2494 * initialized. If regp is valid, it's set with the newly created object.
2496 * Return 0 on success or else a negative value.
2498 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2499 struct ust_app_session
*ua_sess
,
2500 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2503 struct buffer_reg_uid
*reg_uid
;
2510 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2513 * This is the create channel path meaning that if there is NO
2514 * registry available, we have to create one for this session.
2516 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2517 LTTNG_DOMAIN_UST
, ®_uid
,
2518 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2526 /* Initialize registry. */
2527 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2528 app
->bits_per_long
, app
->uint8_t_alignment
,
2529 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2530 app
->uint64_t_alignment
, app
->long_alignment
,
2531 app
->byte_order
, app
->version
.major
,
2532 app
->version
.minor
, reg_uid
->root_shm_path
,
2533 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2534 ua_sess
->tracing_id
, app
->uid
);
2537 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2538 * destroy the buffer registry, because it is always expected
2539 * that if the buffer registry can be found, its ust registry is
2542 buffer_reg_uid_destroy(reg_uid
, NULL
);
2545 /* Add node to teardown list of the session. */
2546 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2548 buffer_reg_uid_add(reg_uid
);
2550 DBG3("UST app buffer registry per UID created successfully");
2561 * Create a session on the tracer side for the given app.
2563 * On success, ua_sess_ptr is populated with the session pointer or else left
2564 * untouched. If the session was created, is_created is set to 1. On error,
2565 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2568 * Returns 0 on success or else a negative code which is either -ENOMEM or
2569 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2571 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2572 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2575 int ret
, created
= 0;
2576 struct ust_app_session
*ua_sess
;
2580 assert(ua_sess_ptr
);
2582 health_code_update();
2584 ua_sess
= lookup_session_by_app(usess
, app
);
2585 if (ua_sess
== NULL
) {
2586 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2587 app
->pid
, usess
->id
);
2588 ua_sess
= alloc_ust_app_session();
2589 if (ua_sess
== NULL
) {
2590 /* Only malloc can failed so something is really wrong */
2594 shadow_copy_session(ua_sess
, usess
, app
);
2598 switch (usess
->buffer_type
) {
2599 case LTTNG_BUFFER_PER_PID
:
2600 /* Init local registry. */
2601 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2603 delete_ust_app_session(-1, ua_sess
, app
);
2607 case LTTNG_BUFFER_PER_UID
:
2608 /* Look for a global registry. If none exists, create one. */
2609 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2611 delete_ust_app_session(-1, ua_sess
, app
);
2621 health_code_update();
2623 if (ua_sess
->handle
== -1) {
2624 pthread_mutex_lock(&app
->sock_lock
);
2625 ret
= ustctl_create_session(app
->sock
);
2626 pthread_mutex_unlock(&app
->sock_lock
);
2628 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2629 ERR("Creating session for app pid %d with ret %d",
2632 DBG("UST app creating session failed. Application is dead");
2634 * This is normal behavior, an application can die during the
2635 * creation process. Don't report an error so the execution can
2636 * continue normally. This will get flagged ENOTCONN and the
2637 * caller will handle it.
2641 delete_ust_app_session(-1, ua_sess
, app
);
2642 if (ret
!= -ENOMEM
) {
2644 * Tracer is probably gone or got an internal error so let's
2645 * behave like it will soon unregister or not usable.
2652 ua_sess
->handle
= ret
;
2654 /* Add ust app session to app's HT */
2655 lttng_ht_node_init_u64(&ua_sess
->node
,
2656 ua_sess
->tracing_id
);
2657 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2658 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2659 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2660 &ua_sess
->ust_objd_node
);
2662 DBG2("UST app session created successfully with handle %d", ret
);
2665 *ua_sess_ptr
= ua_sess
;
2667 *is_created
= created
;
2670 /* Everything went well. */
2674 health_code_update();
2679 * Match function for a hash table lookup of ust_app_ctx.
2681 * It matches an ust app context based on the context type and, in the case
2682 * of perf counters, their name.
2684 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2686 struct ust_app_ctx
*ctx
;
2687 const struct lttng_ust_context_attr
*key
;
2692 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2696 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2701 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2702 if (strncmp(key
->u
.perf_counter
.name
,
2703 ctx
->ctx
.u
.perf_counter
.name
,
2704 sizeof(key
->u
.perf_counter
.name
))) {
2708 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2709 if (strcmp(key
->u
.app_ctx
.provider_name
,
2710 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2711 strcmp(key
->u
.app_ctx
.ctx_name
,
2712 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2728 * Lookup for an ust app context from an lttng_ust_context.
2730 * Must be called while holding RCU read side lock.
2731 * Return an ust_app_ctx object or NULL on error.
2734 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2735 struct lttng_ust_context_attr
*uctx
)
2737 struct lttng_ht_iter iter
;
2738 struct lttng_ht_node_ulong
*node
;
2739 struct ust_app_ctx
*app_ctx
= NULL
;
2744 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2745 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2746 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2747 node
= lttng_ht_iter_get_node_ulong(&iter
);
2752 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2759 * Create a context for the channel on the tracer.
2761 * Called with UST app session lock held and a RCU read side lock.
2764 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2765 struct lttng_ust_context_attr
*uctx
,
2766 struct ust_app
*app
)
2769 struct ust_app_ctx
*ua_ctx
;
2771 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2773 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2779 ua_ctx
= alloc_ust_app_ctx(uctx
);
2780 if (ua_ctx
== NULL
) {
2786 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2787 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2788 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2790 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2800 * Enable on the tracer side a ust app event for the session and channel.
2802 * Called with UST app session lock held.
2805 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2806 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2810 ret
= enable_ust_object(app
, ua_event
->obj
);
2815 ua_event
->enabled
= 1;
2822 * Disable on the tracer side a ust app event for the session and channel.
2824 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2825 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2829 ret
= disable_ust_object(app
, ua_event
->obj
);
2834 ua_event
->enabled
= 0;
2841 * Lookup ust app channel for session and disable it on the tracer side.
2844 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2845 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2849 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2854 ua_chan
->enabled
= 0;
2861 * Lookup ust app channel for session and enable it on the tracer side. This
2862 * MUST be called with a RCU read side lock acquired.
2864 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2865 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2868 struct lttng_ht_iter iter
;
2869 struct lttng_ht_node_str
*ua_chan_node
;
2870 struct ust_app_channel
*ua_chan
;
2872 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2873 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2874 if (ua_chan_node
== NULL
) {
2875 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2876 uchan
->name
, ua_sess
->tracing_id
);
2880 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2882 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2892 * Ask the consumer to create a channel and get it if successful.
2894 * Called with UST app session lock held.
2896 * Return 0 on success or else a negative value.
2898 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2899 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2900 int bitness
, struct ust_registry_session
*registry
,
2901 uint64_t trace_archive_id
)
2904 unsigned int nb_fd
= 0;
2905 struct consumer_socket
*socket
;
2913 health_code_update();
2915 /* Get the right consumer socket for the application. */
2916 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2922 health_code_update();
2924 /* Need one fd for the channel. */
2925 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2927 ERR("Exhausted number of available FD upon create channel");
2932 * Ask consumer to create channel. The consumer will return the number of
2933 * stream we have to expect.
2935 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2936 registry
, usess
->current_trace_chunk
);
2942 * Compute the number of fd needed before receiving them. It must be 2 per
2943 * stream (2 being the default value here).
2945 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2947 /* Reserve the amount of file descriptor we need. */
2948 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2950 ERR("Exhausted number of available FD upon create channel");
2951 goto error_fd_get_stream
;
2954 health_code_update();
2957 * Now get the channel from the consumer. This call will populate the stream
2958 * list of that channel and set the ust objects.
2960 if (usess
->consumer
->enabled
) {
2961 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2971 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2972 error_fd_get_stream
:
2974 * Initiate a destroy channel on the consumer since we had an error
2975 * handling it on our side. The return value is of no importance since we
2976 * already have a ret value set by the previous error that we need to
2979 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2981 lttng_fd_put(LTTNG_FD_APPS
, 1);
2983 health_code_update();
2989 * Duplicate the ust data object of the ust app stream and save it in the
2990 * buffer registry stream.
2992 * Return 0 on success or else a negative value.
2994 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2995 struct ust_app_stream
*stream
)
3002 /* Reserve the amount of file descriptor we need. */
3003 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3005 ERR("Exhausted number of available FD upon duplicate stream");
3009 /* Duplicate object for stream once the original is in the registry. */
3010 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
3011 reg_stream
->obj
.ust
);
3013 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3014 reg_stream
->obj
.ust
, stream
->obj
, ret
);
3015 lttng_fd_put(LTTNG_FD_APPS
, 2);
3018 stream
->handle
= stream
->obj
->handle
;
3025 * Duplicate the ust data object of the ust app. channel and save it in the
3026 * buffer registry channel.
3028 * Return 0 on success or else a negative value.
3030 static int duplicate_channel_object(struct buffer_reg_channel
*buf_reg_chan
,
3031 struct ust_app_channel
*ua_chan
)
3035 assert(buf_reg_chan
);
3038 /* Need two fds for the channel. */
3039 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3041 ERR("Exhausted number of available FD upon duplicate channel");
3045 /* Duplicate object for stream once the original is in the registry. */
3046 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, buf_reg_chan
->obj
.ust
);
3048 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3049 buf_reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
3052 ua_chan
->handle
= ua_chan
->obj
->handle
;
3057 lttng_fd_put(LTTNG_FD_APPS
, 1);
3063 * For a given channel buffer registry, setup all streams of the given ust
3064 * application channel.
3066 * Return 0 on success or else a negative value.
3068 static int setup_buffer_reg_streams(struct buffer_reg_channel
*buf_reg_chan
,
3069 struct ust_app_channel
*ua_chan
,
3070 struct ust_app
*app
)
3073 struct ust_app_stream
*stream
, *stmp
;
3075 assert(buf_reg_chan
);
3078 DBG2("UST app setup buffer registry stream");
3080 /* Send all streams to application. */
3081 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3082 struct buffer_reg_stream
*reg_stream
;
3084 ret
= buffer_reg_stream_create(®_stream
);
3090 * Keep original pointer and nullify it in the stream so the delete
3091 * stream call does not release the object.
3093 reg_stream
->obj
.ust
= stream
->obj
;
3095 buffer_reg_stream_add(reg_stream
, buf_reg_chan
);
3097 /* We don't need the streams anymore. */
3098 cds_list_del(&stream
->list
);
3099 delete_ust_app_stream(-1, stream
, app
);
3107 * Create a buffer registry channel for the given session registry and
3108 * application channel object. If regp pointer is valid, it's set with the
3109 * created object. Important, the created object is NOT added to the session
3110 * registry hash table.
3112 * Return 0 on success else a negative value.
3114 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3115 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
3118 struct buffer_reg_channel
*buf_reg_chan
= NULL
;
3123 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3125 /* Create buffer registry channel. */
3126 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, &buf_reg_chan
);
3130 assert(buf_reg_chan
);
3131 buf_reg_chan
->consumer_key
= ua_chan
->key
;
3132 buf_reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3133 buf_reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3135 /* Create and add a channel registry to session. */
3136 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
3137 ua_chan
->tracing_channel_id
);
3141 buffer_reg_channel_add(reg_sess
, buf_reg_chan
);
3144 *regp
= buf_reg_chan
;
3150 /* Safe because the registry channel object was not added to any HT. */
3151 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3157 * Setup buffer registry channel for the given session registry and application
3158 * channel object. If regp pointer is valid, it's set with the created object.
3160 * Return 0 on success else a negative value.
3162 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3163 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*buf_reg_chan
,
3164 struct ust_app
*app
)
3169 assert(buf_reg_chan
);
3171 assert(ua_chan
->obj
);
3173 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3175 /* Setup all streams for the registry. */
3176 ret
= setup_buffer_reg_streams(buf_reg_chan
, ua_chan
, app
);
3181 buf_reg_chan
->obj
.ust
= ua_chan
->obj
;
3182 ua_chan
->obj
= NULL
;
3187 buffer_reg_channel_remove(reg_sess
, buf_reg_chan
);
3188 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3193 * Send buffer registry channel to the application.
3195 * Return 0 on success else a negative value.
3197 static int send_channel_uid_to_ust(struct buffer_reg_channel
*buf_reg_chan
,
3198 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
3199 struct ust_app_channel
*ua_chan
)
3202 struct buffer_reg_stream
*reg_stream
;
3204 assert(buf_reg_chan
);
3209 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3211 ret
= duplicate_channel_object(buf_reg_chan
, ua_chan
);
3216 /* Send channel to the application. */
3217 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3218 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3219 ret
= -ENOTCONN
; /* Caused by app exiting. */
3221 } else if (ret
< 0) {
3225 health_code_update();
3227 /* Send all streams to application. */
3228 pthread_mutex_lock(&buf_reg_chan
->stream_list_lock
);
3229 cds_list_for_each_entry(reg_stream
, &buf_reg_chan
->streams
, lnode
) {
3230 struct ust_app_stream stream
;
3232 ret
= duplicate_stream_object(reg_stream
, &stream
);
3234 goto error_stream_unlock
;
3237 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3239 (void) release_ust_app_stream(-1, &stream
, app
);
3240 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3241 ret
= -ENOTCONN
; /* Caused by app exiting. */
3243 goto error_stream_unlock
;
3247 * The return value is not important here. This function will output an
3250 (void) release_ust_app_stream(-1, &stream
, app
);
3252 ua_chan
->is_sent
= 1;
3254 error_stream_unlock
:
3255 pthread_mutex_unlock(&buf_reg_chan
->stream_list_lock
);
3261 * Create and send to the application the created buffers with per UID buffers.
3263 * This MUST be called with a RCU read side lock acquired.
3264 * The session list lock and the session's lock must be acquired.
3266 * Return 0 on success else a negative value.
3268 static int create_channel_per_uid(struct ust_app
*app
,
3269 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3270 struct ust_app_channel
*ua_chan
)
3273 struct buffer_reg_uid
*reg_uid
;
3274 struct buffer_reg_channel
*buf_reg_chan
;
3275 struct ltt_session
*session
= NULL
;
3276 enum lttng_error_code notification_ret
;
3277 struct ust_registry_channel
*ust_reg_chan
;
3284 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3286 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3288 * The session creation handles the creation of this global registry
3289 * object. If none can be find, there is a code flow problem or a
3294 buf_reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3300 /* Create the buffer registry channel object. */
3301 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, &buf_reg_chan
);
3303 ERR("Error creating the UST channel \"%s\" registry instance",
3308 session
= session_find_by_id(ua_sess
->tracing_id
);
3310 assert(pthread_mutex_trylock(&session
->lock
));
3311 assert(session_trylock_list());
3314 * Create the buffers on the consumer side. This call populates the
3315 * ust app channel object with all streams and data object.
3317 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3318 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
3319 session
->most_recent_chunk_id
.value
);
3321 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3325 * Let's remove the previously created buffer registry channel so
3326 * it's not visible anymore in the session registry.
3328 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3329 ua_chan
->tracing_channel_id
, false);
3330 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
3331 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3336 * Setup the streams and add it to the session registry.
3338 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3339 ua_chan
, buf_reg_chan
, app
);
3341 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3345 /* Notify the notification subsystem of the channel's creation. */
3346 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
3347 ust_reg_chan
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3348 ua_chan
->tracing_channel_id
);
3349 assert(ust_reg_chan
);
3350 ust_reg_chan
->consumer_key
= ua_chan
->key
;
3351 ust_reg_chan
= NULL
;
3352 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
3354 notification_ret
= notification_thread_command_add_channel(
3355 the_notification_thread_handle
, session
->name
,
3356 lttng_credentials_get_uid(
3357 &ua_sess
->effective_credentials
),
3358 lttng_credentials_get_gid(
3359 &ua_sess
->effective_credentials
),
3360 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3361 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3362 if (notification_ret
!= LTTNG_OK
) {
3363 ret
= - (int) notification_ret
;
3364 ERR("Failed to add channel to notification thread");
3369 /* Send buffers to the application. */
3370 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3372 if (ret
!= -ENOTCONN
) {
3373 ERR("Error sending channel to application");
3380 session_put(session
);
3386 * Create and send to the application the created buffers with per PID buffers.
3388 * Called with UST app session lock held.
3389 * The session list lock and the session's lock must be acquired.
3391 * Return 0 on success else a negative value.
3393 static int create_channel_per_pid(struct ust_app
*app
,
3394 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3395 struct ust_app_channel
*ua_chan
)
3398 struct ust_registry_session
*registry
;
3399 enum lttng_error_code cmd_ret
;
3400 struct ltt_session
*session
= NULL
;
3401 uint64_t chan_reg_key
;
3402 struct ust_registry_channel
*ust_reg_chan
;
3409 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3413 registry
= get_session_registry(ua_sess
);
3414 /* The UST app session lock is held, registry shall not be null. */
3417 /* Create and add a new channel registry to session. */
3418 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3420 ERR("Error creating the UST channel \"%s\" registry instance",
3425 session
= session_find_by_id(ua_sess
->tracing_id
);
3428 assert(pthread_mutex_trylock(&session
->lock
));
3429 assert(session_trylock_list());
3431 /* Create and get channel on the consumer side. */
3432 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3433 app
->bits_per_long
, registry
,
3434 session
->most_recent_chunk_id
.value
);
3436 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3438 goto error_remove_from_registry
;
3441 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3443 if (ret
!= -ENOTCONN
) {
3444 ERR("Error sending channel to application");
3446 goto error_remove_from_registry
;
3449 chan_reg_key
= ua_chan
->key
;
3450 pthread_mutex_lock(®istry
->lock
);
3451 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
3452 assert(ust_reg_chan
);
3453 ust_reg_chan
->consumer_key
= ua_chan
->key
;
3454 pthread_mutex_unlock(®istry
->lock
);
3456 cmd_ret
= notification_thread_command_add_channel(
3457 the_notification_thread_handle
, session
->name
,
3458 lttng_credentials_get_uid(
3459 &ua_sess
->effective_credentials
),
3460 lttng_credentials_get_gid(
3461 &ua_sess
->effective_credentials
),
3462 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3463 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3464 if (cmd_ret
!= LTTNG_OK
) {
3465 ret
= - (int) cmd_ret
;
3466 ERR("Failed to add channel to notification thread");
3467 goto error_remove_from_registry
;
3470 error_remove_from_registry
:
3472 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3477 session_put(session
);
3483 * From an already allocated ust app channel, create the channel buffers if
3484 * needed and send them to the application. This MUST be called with a RCU read
3485 * side lock acquired.
3487 * Called with UST app session lock held.
3489 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3490 * the application exited concurrently.
3492 static int ust_app_channel_send(struct ust_app
*app
,
3493 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3494 struct ust_app_channel
*ua_chan
)
3500 assert(usess
->active
);
3504 /* Handle buffer type before sending the channel to the application. */
3505 switch (usess
->buffer_type
) {
3506 case LTTNG_BUFFER_PER_UID
:
3508 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3514 case LTTNG_BUFFER_PER_PID
:
3516 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3528 /* Initialize ust objd object using the received handle and add it. */
3529 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3530 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3532 /* If channel is not enabled, disable it on the tracer */
3533 if (!ua_chan
->enabled
) {
3534 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3545 * Create UST app channel and return it through ua_chanp if not NULL.
3547 * Called with UST app session lock and RCU read-side lock held.
3549 * Return 0 on success or else a negative value.
3551 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3552 struct ltt_ust_channel
*uchan
,
3553 enum lttng_ust_abi_chan_type type
, struct ltt_ust_session
*usess
,
3554 struct ust_app_channel
**ua_chanp
)
3557 struct lttng_ht_iter iter
;
3558 struct lttng_ht_node_str
*ua_chan_node
;
3559 struct ust_app_channel
*ua_chan
;
3561 /* Lookup channel in the ust app session */
3562 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3563 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3564 if (ua_chan_node
!= NULL
) {
3565 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3569 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3570 if (ua_chan
== NULL
) {
3571 /* Only malloc can fail here */
3575 shadow_copy_channel(ua_chan
, uchan
);
3577 /* Set channel type. */
3578 ua_chan
->attr
.type
= type
;
3580 /* Only add the channel if successful on the tracer side. */
3581 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3584 *ua_chanp
= ua_chan
;
3587 /* Everything went well. */
3595 * Create UST app event and create it on the tracer side.
3597 * Must be called with the RCU read side lock held.
3598 * Called with ust app session mutex held.
3601 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3602 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3603 struct ust_app
*app
)
3606 struct ust_app_event
*ua_event
;
3608 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3609 if (ua_event
== NULL
) {
3610 /* Only failure mode of alloc_ust_app_event(). */
3614 shadow_copy_event(ua_event
, uevent
);
3616 /* Create it on the tracer side */
3617 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3620 * Not found previously means that it does not exist on the
3621 * tracer. If the application reports that the event existed,
3622 * it means there is a bug in the sessiond or lttng-ust
3623 * (or corruption, etc.)
3625 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3626 ERR("Tracer for application reported that an event being created already existed: "
3627 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3629 app
->pid
, app
->ppid
, app
->uid
,
3635 add_unique_ust_app_event(ua_chan
, ua_event
);
3637 DBG2("UST app create event completed: app = '%s' (ppid: %d)",
3638 app
->name
, app
->ppid
);
3644 /* Valid. Calling here is already in a read side lock */
3645 delete_ust_app_event(-1, ua_event
, app
);
3650 * Create UST app event notifier rule and create it on the tracer side.
3652 * Must be called with the RCU read side lock held.
3653 * Called with ust app session mutex held.
3656 int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
,
3657 struct ust_app
*app
)
3660 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3662 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3663 if (ua_event_notifier_rule
== NULL
) {
3668 /* Create it on the tracer side. */
3669 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3672 * Not found previously means that it does not exist on the
3673 * tracer. If the application reports that the event existed,
3674 * it means there is a bug in the sessiond or lttng-ust
3675 * (or corruption, etc.)
3677 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3678 ERR("Tracer for application reported that an event notifier being created already exists: "
3679 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3680 lttng_trigger_get_tracer_token(trigger
),
3681 app
->pid
, app
->ppid
, app
->uid
,
3687 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3688 &ua_event_notifier_rule
->node
);
3690 DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64
,
3691 app
->name
, app
->ppid
, lttng_trigger_get_tracer_token(trigger
));
3696 /* The RCU read side lock is already being held by the caller. */
3697 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3703 * Create UST metadata and open it on the tracer side.
3705 * Called with UST app session lock held and RCU read side lock.
3707 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3708 struct ust_app
*app
, struct consumer_output
*consumer
)
3711 struct ust_app_channel
*metadata
;
3712 struct consumer_socket
*socket
;
3713 struct ust_registry_session
*registry
;
3714 struct ltt_session
*session
= NULL
;
3720 registry
= get_session_registry(ua_sess
);
3721 /* The UST app session is held registry shall not be null. */
3724 pthread_mutex_lock(®istry
->lock
);
3726 /* Metadata already exists for this registry or it was closed previously */
3727 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3732 /* Allocate UST metadata */
3733 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3735 /* malloc() failed */
3740 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3742 /* Need one fd for the channel. */
3743 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3745 ERR("Exhausted number of available FD upon create metadata");
3749 /* Get the right consumer socket for the application. */
3750 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3753 goto error_consumer
;
3757 * Keep metadata key so we can identify it on the consumer side. Assign it
3758 * to the registry *before* we ask the consumer so we avoid the race of the
3759 * consumer requesting the metadata and the ask_channel call on our side
3760 * did not returned yet.
3762 registry
->metadata_key
= metadata
->key
;
3764 session
= session_find_by_id(ua_sess
->tracing_id
);
3767 assert(pthread_mutex_trylock(&session
->lock
));
3768 assert(session_trylock_list());
3771 * Ask the metadata channel creation to the consumer. The metadata object
3772 * will be created by the consumer and kept their. However, the stream is
3773 * never added or monitored until we do a first push metadata to the
3776 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3777 registry
, session
->current_trace_chunk
);
3779 /* Nullify the metadata key so we don't try to close it later on. */
3780 registry
->metadata_key
= 0;
3781 goto error_consumer
;
3785 * The setup command will make the metadata stream be sent to the relayd,
3786 * if applicable, and the thread managing the metadatas. This is important
3787 * because after this point, if an error occurs, the only way the stream
3788 * can be deleted is to be monitored in the consumer.
3790 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3792 /* Nullify the metadata key so we don't try to close it later on. */
3793 registry
->metadata_key
= 0;
3794 goto error_consumer
;
3797 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3798 metadata
->key
, app
->pid
);
3801 lttng_fd_put(LTTNG_FD_APPS
, 1);
3802 delete_ust_app_channel(-1, metadata
, app
);
3804 pthread_mutex_unlock(®istry
->lock
);
3806 session_put(session
);
3812 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3813 * acquired before calling this function.
3815 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3817 struct ust_app
*app
= NULL
;
3818 struct lttng_ht_node_ulong
*node
;
3819 struct lttng_ht_iter iter
;
3821 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3822 node
= lttng_ht_iter_get_node_ulong(&iter
);
3824 DBG2("UST app no found with pid %d", pid
);
3828 DBG2("Found UST app by pid %d", pid
);
3830 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3837 * Allocate and init an UST app object using the registration information and
3838 * the command socket. This is called when the command socket connects to the
3841 * The object is returned on success or else NULL.
3843 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3846 struct ust_app
*lta
= NULL
;
3847 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3852 DBG3("UST app creating application for socket %d", sock
);
3854 if ((msg
->bits_per_long
== 64 &&
3855 (uatomic_read(&the_ust_consumerd64_fd
) ==
3857 (msg
->bits_per_long
== 32 &&
3858 (uatomic_read(&the_ust_consumerd32_fd
) ==
3860 ERR("Registration failed: application \"%s\" (pid: %d) has "
3861 "%d-bit long, but no consumerd for this size is available.\n",
3862 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3867 * Reserve the two file descriptors of the event source pipe. The write
3868 * end will be closed once it is passed to the application, at which
3869 * point a single 'put' will be performed.
3871 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3873 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s' (ppid: %d)",
3874 msg
->name
, (int) msg
->ppid
);
3878 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3879 if (!event_notifier_event_source_pipe
) {
3880 PERROR("Failed to open application event source pipe: '%s' (ppid = %d)",
3881 msg
->name
, msg
->ppid
);
3885 lta
= zmalloc(sizeof(struct ust_app
));
3888 goto error_free_pipe
;
3891 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
3893 lta
->ppid
= msg
->ppid
;
3894 lta
->uid
= msg
->uid
;
3895 lta
->gid
= msg
->gid
;
3897 lta
->bits_per_long
= msg
->bits_per_long
;
3898 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3899 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3900 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3901 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3902 lta
->long_alignment
= msg
->long_alignment
;
3903 lta
->byte_order
= msg
->byte_order
;
3905 lta
->v_major
= msg
->major
;
3906 lta
->v_minor
= msg
->minor
;
3907 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3908 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3909 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3910 lta
->notify_sock
= -1;
3911 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3913 /* Copy name and make sure it's NULL terminated. */
3914 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3915 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3918 * Before this can be called, when receiving the registration information,
3919 * the application compatibility is checked. So, at this point, the
3920 * application can work with this session daemon.
3922 lta
->compatible
= 1;
3924 lta
->pid
= msg
->pid
;
3925 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3927 pthread_mutex_init(<a
->sock_lock
, NULL
);
3928 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3930 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3934 lttng_pipe_destroy(event_notifier_event_source_pipe
);
3935 lttng_fd_put(LTTNG_FD_APPS
, 2);
3941 * For a given application object, add it to every hash table.
3943 void ust_app_add(struct ust_app
*app
)
3946 assert(app
->notify_sock
>= 0);
3948 app
->registration_time
= time(NULL
);
3953 * On a re-registration, we want to kick out the previous registration of
3956 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3959 * The socket _should_ be unique until _we_ call close. So, a add_unique
3960 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3961 * already in the table.
3963 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3965 /* Add application to the notify socket hash table. */
3966 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3967 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3969 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3970 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3971 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3978 * Set the application version into the object.
3980 * Return 0 on success else a negative value either an errno code or a
3981 * LTTng-UST error code.
3983 int ust_app_version(struct ust_app
*app
)
3989 pthread_mutex_lock(&app
->sock_lock
);
3990 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3991 pthread_mutex_unlock(&app
->sock_lock
);
3993 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3994 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3996 DBG3("UST app %d version failed. Application is dead", app
->sock
);
4004 * Setup the base event notifier group.
4006 * Return 0 on success else a negative value either an errno code or a
4007 * LTTng-UST error code.
4009 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4012 int event_pipe_write_fd
;
4013 struct lttng_ust_abi_object_data
*event_notifier_group
= NULL
;
4014 enum lttng_error_code lttng_ret
;
4015 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4019 /* Get the write side of the pipe. */
4020 event_pipe_write_fd
= lttng_pipe_get_writefd(
4021 app
->event_notifier_group
.event_pipe
);
4023 pthread_mutex_lock(&app
->sock_lock
);
4024 ret
= ustctl_create_event_notifier_group(app
->sock
,
4025 event_pipe_write_fd
, &event_notifier_group
);
4026 pthread_mutex_unlock(&app
->sock_lock
);
4028 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4029 ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d",
4030 ret
, app
->sock
, event_pipe_write_fd
);
4032 DBG("Failed to create application event notifier group (application is dead): app socket fd = %d",
4039 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4041 ERR("Failed to close write end of the application's event source pipe: app = '%s' (ppid = %d)",
4042 app
->name
, app
->ppid
);
4047 * Release the file descriptor that was reserved for the write-end of
4050 lttng_fd_put(LTTNG_FD_APPS
, 1);
4052 lttng_ret
= notification_thread_command_add_tracer_event_source(
4053 the_notification_thread_handle
,
4054 lttng_pipe_get_readfd(
4055 app
->event_notifier_group
.event_pipe
),
4057 if (lttng_ret
!= LTTNG_OK
) {
4058 ERR("Failed to add tracer event source to notification thread");
4063 /* Assign handle only when the complete setup is valid. */
4064 app
->event_notifier_group
.object
= event_notifier_group
;
4066 event_notifier_error_accounting_status
= event_notifier_error_accounting_register_app(app
);
4067 if (event_notifier_error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
4068 ERR("Failed to setup event notifier error accounting for app");
4076 ustctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4077 free(app
->event_notifier_group
.object
);
4078 app
->event_notifier_group
.object
= NULL
;
4083 * Unregister app by removing it from the global traceable app list and freeing
4086 * The socket is already closed at this point so no close to sock.
4088 void ust_app_unregister(int sock
)
4090 struct ust_app
*lta
;
4091 struct lttng_ht_node_ulong
*node
;
4092 struct lttng_ht_iter ust_app_sock_iter
;
4093 struct lttng_ht_iter iter
;
4094 struct ust_app_session
*ua_sess
;
4099 /* Get the node reference for a call_rcu */
4100 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
4101 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4104 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
4105 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4108 * For per-PID buffers, perform "push metadata" and flush all
4109 * application streams before removing app from hash tables,
4110 * ensuring proper behavior of data_pending check.
4111 * Remove sessions so they are not visible during deletion.
4113 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
4115 struct ust_registry_session
*registry
;
4117 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4119 /* The session was already removed so scheduled for teardown. */
4123 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4124 (void) ust_app_flush_app_session(lta
, ua_sess
);
4128 * Add session to list for teardown. This is safe since at this point we
4129 * are the only one using this list.
4131 pthread_mutex_lock(&ua_sess
->lock
);
4133 if (ua_sess
->deleted
) {
4134 pthread_mutex_unlock(&ua_sess
->lock
);
4139 * Normally, this is done in the delete session process which is
4140 * executed in the call rcu below. However, upon registration we can't
4141 * afford to wait for the grace period before pushing data or else the
4142 * data pending feature can race between the unregistration and stop
4143 * command where the data pending command is sent *before* the grace
4146 * The close metadata below nullifies the metadata pointer in the
4147 * session so the delete session will NOT push/close a second time.
4149 registry
= get_session_registry(ua_sess
);
4151 /* Push metadata for application before freeing the application. */
4152 (void) push_metadata(registry
, ua_sess
->consumer
);
4155 * Don't ask to close metadata for global per UID buffers. Close
4156 * metadata only on destroy trace session in this case. Also, the
4157 * previous push metadata could have flag the metadata registry to
4158 * close so don't send a close command if closed.
4160 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4161 /* And ask to close it for this session registry. */
4162 (void) close_metadata(registry
, ua_sess
->consumer
);
4165 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4167 pthread_mutex_unlock(&ua_sess
->lock
);
4170 /* Remove application from PID hash table */
4171 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4175 * Remove application from notify hash table. The thread handling the
4176 * notify socket could have deleted the node so ignore on error because
4177 * either way it's valid. The close of that socket is handled by the
4178 * apps_notify_thread.
4180 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4181 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4184 * Ignore return value since the node might have been removed before by an
4185 * add replace during app registration because the PID can be reassigned by
4188 iter
.iter
.node
= <a
->pid_n
.node
;
4189 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4191 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4196 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4203 * Fill events array with all events name of all registered apps.
4205 int ust_app_list_events(struct lttng_event
**events
)
4208 size_t nbmem
, count
= 0;
4209 struct lttng_ht_iter iter
;
4210 struct ust_app
*app
;
4211 struct lttng_event
*tmp_event
;
4213 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4214 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
4215 if (tmp_event
== NULL
) {
4216 PERROR("zmalloc ust app events");
4223 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4224 struct lttng_ust_abi_tracepoint_iter uiter
;
4226 health_code_update();
4228 if (!app
->compatible
) {
4230 * TODO: In time, we should notice the caller of this error by
4231 * telling him that this is a version error.
4235 pthread_mutex_lock(&app
->sock_lock
);
4236 handle
= ustctl_tracepoint_list(app
->sock
);
4238 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4239 ERR("UST app list events getting handle failed for app pid %d",
4242 pthread_mutex_unlock(&app
->sock_lock
);
4246 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
4247 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4248 /* Handle ustctl error. */
4252 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4253 ERR("UST app tp list get failed for app %d with ret %d",
4256 DBG3("UST app tp list get failed. Application is dead");
4258 * This is normal behavior, an application can die during the
4259 * creation process. Don't report an error so the execution can
4260 * continue normally. Continue normal execution.
4265 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4266 if (release_ret
< 0 &&
4267 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4268 release_ret
!= -EPIPE
) {
4269 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4271 pthread_mutex_unlock(&app
->sock_lock
);
4275 health_code_update();
4276 if (count
>= nbmem
) {
4277 /* In case the realloc fails, we free the memory */
4278 struct lttng_event
*new_tmp_event
;
4281 new_nbmem
= nbmem
<< 1;
4282 DBG2("Reallocating event list from %zu to %zu entries",
4284 new_tmp_event
= realloc(tmp_event
,
4285 new_nbmem
* sizeof(struct lttng_event
));
4286 if (new_tmp_event
== NULL
) {
4289 PERROR("realloc ust app events");
4292 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4293 if (release_ret
< 0 &&
4294 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4295 release_ret
!= -EPIPE
) {
4296 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4298 pthread_mutex_unlock(&app
->sock_lock
);
4301 /* Zero the new memory */
4302 memset(new_tmp_event
+ nbmem
, 0,
4303 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4305 tmp_event
= new_tmp_event
;
4307 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4308 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4309 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4310 tmp_event
[count
].pid
= app
->pid
;
4311 tmp_event
[count
].enabled
= -1;
4314 ret
= ustctl_release_handle(app
->sock
, handle
);
4315 pthread_mutex_unlock(&app
->sock_lock
);
4316 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4317 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4322 *events
= tmp_event
;
4324 DBG2("UST app list events done (%zu events)", count
);
4329 health_code_update();
4334 * Fill events array with all events name of all registered apps.
4336 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4339 size_t nbmem
, count
= 0;
4340 struct lttng_ht_iter iter
;
4341 struct ust_app
*app
;
4342 struct lttng_event_field
*tmp_event
;
4344 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4345 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
4346 if (tmp_event
== NULL
) {
4347 PERROR("zmalloc ust app event fields");
4354 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4355 struct lttng_ust_abi_field_iter uiter
;
4357 health_code_update();
4359 if (!app
->compatible
) {
4361 * TODO: In time, we should notice the caller of this error by
4362 * telling him that this is a version error.
4366 pthread_mutex_lock(&app
->sock_lock
);
4367 handle
= ustctl_tracepoint_field_list(app
->sock
);
4369 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4370 ERR("UST app list field getting handle failed for app pid %d",
4373 pthread_mutex_unlock(&app
->sock_lock
);
4377 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
4378 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4379 /* Handle ustctl error. */
4383 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4384 ERR("UST app tp list field failed for app %d with ret %d",
4387 DBG3("UST app tp list field failed. Application is dead");
4389 * This is normal behavior, an application can die during the
4390 * creation process. Don't report an error so the execution can
4391 * continue normally. Reset list and count for next app.
4396 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4397 pthread_mutex_unlock(&app
->sock_lock
);
4398 if (release_ret
< 0 &&
4399 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4400 release_ret
!= -EPIPE
) {
4401 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4406 health_code_update();
4407 if (count
>= nbmem
) {
4408 /* In case the realloc fails, we free the memory */
4409 struct lttng_event_field
*new_tmp_event
;
4412 new_nbmem
= nbmem
<< 1;
4413 DBG2("Reallocating event field list from %zu to %zu entries",
4415 new_tmp_event
= realloc(tmp_event
,
4416 new_nbmem
* sizeof(struct lttng_event_field
));
4417 if (new_tmp_event
== NULL
) {
4420 PERROR("realloc ust app event fields");
4423 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4424 pthread_mutex_unlock(&app
->sock_lock
);
4426 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4427 release_ret
!= -EPIPE
) {
4428 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4432 /* Zero the new memory */
4433 memset(new_tmp_event
+ nbmem
, 0,
4434 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4436 tmp_event
= new_tmp_event
;
4439 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4440 /* Mapping between these enums matches 1 to 1. */
4441 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4442 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4444 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4445 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4446 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4447 tmp_event
[count
].event
.pid
= app
->pid
;
4448 tmp_event
[count
].event
.enabled
= -1;
4451 ret
= ustctl_release_handle(app
->sock
, handle
);
4452 pthread_mutex_unlock(&app
->sock_lock
);
4454 ret
!= -LTTNG_UST_ERR_EXITING
&&
4456 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4461 *fields
= tmp_event
;
4463 DBG2("UST app list event fields done (%zu events)", count
);
4468 health_code_update();
4473 * Free and clean all traceable apps of the global list.
4475 * Should _NOT_ be called with RCU read-side lock held.
4477 void ust_app_clean_list(void)
4480 struct ust_app
*app
;
4481 struct lttng_ht_iter iter
;
4483 DBG2("UST app cleaning registered apps hash table");
4487 /* Cleanup notify socket hash table */
4488 if (ust_app_ht_by_notify_sock
) {
4489 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4490 notify_sock_n
.node
) {
4492 * Assert that all notifiers are gone as all triggers
4493 * are unregistered prior to this clean-up.
4495 assert(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4497 ust_app_notify_sock_unregister(app
->notify_sock
);
4502 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4503 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4505 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4509 /* Cleanup socket hash table */
4510 if (ust_app_ht_by_sock
) {
4511 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4513 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4520 /* Destroy is done only when the ht is empty */
4522 ht_cleanup_push(ust_app_ht
);
4524 if (ust_app_ht_by_sock
) {
4525 ht_cleanup_push(ust_app_ht_by_sock
);
4527 if (ust_app_ht_by_notify_sock
) {
4528 ht_cleanup_push(ust_app_ht_by_notify_sock
);
4533 * Init UST app hash table.
4535 int ust_app_ht_alloc(void)
4537 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4541 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4542 if (!ust_app_ht_by_sock
) {
4545 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4546 if (!ust_app_ht_by_notify_sock
) {
4553 * For a specific UST session, disable the channel for all registered apps.
4555 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4556 struct ltt_ust_channel
*uchan
)
4559 struct lttng_ht_iter iter
;
4560 struct lttng_ht_node_str
*ua_chan_node
;
4561 struct ust_app
*app
;
4562 struct ust_app_session
*ua_sess
;
4563 struct ust_app_channel
*ua_chan
;
4565 assert(usess
->active
);
4566 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4567 uchan
->name
, usess
->id
);
4571 /* For every registered applications */
4572 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4573 struct lttng_ht_iter uiter
;
4574 if (!app
->compatible
) {
4576 * TODO: In time, we should notice the caller of this error by
4577 * telling him that this is a version error.
4581 ua_sess
= lookup_session_by_app(usess
, app
);
4582 if (ua_sess
== NULL
) {
4587 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4588 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4589 /* If the session if found for the app, the channel must be there */
4590 assert(ua_chan_node
);
4592 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4593 /* The channel must not be already disabled */
4594 assert(ua_chan
->enabled
== 1);
4596 /* Disable channel onto application */
4597 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4599 /* XXX: We might want to report this error at some point... */
4609 * For a specific UST session, enable the channel for all registered apps.
4611 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4612 struct ltt_ust_channel
*uchan
)
4615 struct lttng_ht_iter iter
;
4616 struct ust_app
*app
;
4617 struct ust_app_session
*ua_sess
;
4619 assert(usess
->active
);
4620 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4621 uchan
->name
, usess
->id
);
4625 /* For every registered applications */
4626 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4627 if (!app
->compatible
) {
4629 * TODO: In time, we should notice the caller of this error by
4630 * telling him that this is a version error.
4634 ua_sess
= lookup_session_by_app(usess
, app
);
4635 if (ua_sess
== NULL
) {
4639 /* Enable channel onto application */
4640 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4642 /* XXX: We might want to report this error at some point... */
4652 * Disable an event in a channel and for a specific session.
4654 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4655 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4658 struct lttng_ht_iter iter
, uiter
;
4659 struct lttng_ht_node_str
*ua_chan_node
;
4660 struct ust_app
*app
;
4661 struct ust_app_session
*ua_sess
;
4662 struct ust_app_channel
*ua_chan
;
4663 struct ust_app_event
*ua_event
;
4665 assert(usess
->active
);
4666 DBG("UST app disabling event %s for all apps in channel "
4667 "%s for session id %" PRIu64
,
4668 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4672 /* For all registered applications */
4673 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4674 if (!app
->compatible
) {
4676 * TODO: In time, we should notice the caller of this error by
4677 * telling him that this is a version error.
4681 ua_sess
= lookup_session_by_app(usess
, app
);
4682 if (ua_sess
== NULL
) {
4687 /* Lookup channel in the ust app session */
4688 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4689 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4690 if (ua_chan_node
== NULL
) {
4691 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4692 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4695 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4697 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4698 uevent
->filter
, uevent
->attr
.loglevel
,
4700 if (ua_event
== NULL
) {
4701 DBG2("Event %s not found in channel %s for app pid %d."
4702 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4706 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4708 /* XXX: Report error someday... */
4717 /* The ua_sess lock must be held by the caller. */
4719 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4720 struct ust_app_session
*ua_sess
,
4721 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4722 struct ust_app_channel
**_ua_chan
)
4725 struct ust_app_channel
*ua_chan
= NULL
;
4728 ASSERT_LOCKED(ua_sess
->lock
);
4730 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4731 sizeof(uchan
->name
))) {
4732 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4736 struct ltt_ust_context
*uctx
= NULL
;
4739 * Create channel onto application and synchronize its
4742 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4743 LTTNG_UST_ABI_CHAN_PER_CPU
, usess
,
4749 ret
= ust_app_channel_send(app
, usess
,
4756 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4757 ret
= create_ust_app_channel_context(ua_chan
,
4770 * The application's socket is not valid. Either a bad socket
4771 * or a timeout on it. We can't inform the caller that for a
4772 * specific app, the session failed so lets continue here.
4774 ret
= 0; /* Not an error. */
4782 if (ret
== 0 && _ua_chan
) {
4784 * Only return the application's channel on success. Note
4785 * that the channel can still be part of the application's
4786 * channel hashtable on error.
4788 *_ua_chan
= ua_chan
;
4794 * Enable event for a specific session and channel on the tracer.
4796 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4797 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4800 struct lttng_ht_iter iter
, uiter
;
4801 struct lttng_ht_node_str
*ua_chan_node
;
4802 struct ust_app
*app
;
4803 struct ust_app_session
*ua_sess
;
4804 struct ust_app_channel
*ua_chan
;
4805 struct ust_app_event
*ua_event
;
4807 assert(usess
->active
);
4808 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4809 uevent
->attr
.name
, usess
->id
);
4812 * NOTE: At this point, this function is called only if the session and
4813 * channel passed are already created for all apps. and enabled on the
4819 /* For all registered applications */
4820 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4821 if (!app
->compatible
) {
4823 * TODO: In time, we should notice the caller of this error by
4824 * telling him that this is a version error.
4828 ua_sess
= lookup_session_by_app(usess
, app
);
4830 /* The application has problem or is probably dead. */
4834 pthread_mutex_lock(&ua_sess
->lock
);
4836 if (ua_sess
->deleted
) {
4837 pthread_mutex_unlock(&ua_sess
->lock
);
4841 /* Lookup channel in the ust app session */
4842 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4843 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4845 * It is possible that the channel cannot be found is
4846 * the channel/event creation occurs concurrently with
4847 * an application exit.
4849 if (!ua_chan_node
) {
4850 pthread_mutex_unlock(&ua_sess
->lock
);
4854 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4856 /* Get event node */
4857 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4858 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4859 if (ua_event
== NULL
) {
4860 DBG3("UST app enable event %s not found for app PID %d."
4861 "Skipping app", uevent
->attr
.name
, app
->pid
);
4865 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4867 pthread_mutex_unlock(&ua_sess
->lock
);
4871 pthread_mutex_unlock(&ua_sess
->lock
);
4880 * For a specific existing UST session and UST channel, creates the event for
4881 * all registered apps.
4883 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4884 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4887 struct lttng_ht_iter iter
, uiter
;
4888 struct lttng_ht_node_str
*ua_chan_node
;
4889 struct ust_app
*app
;
4890 struct ust_app_session
*ua_sess
;
4891 struct ust_app_channel
*ua_chan
;
4893 assert(usess
->active
);
4894 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4895 uevent
->attr
.name
, usess
->id
);
4899 /* For all registered applications */
4900 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4901 if (!app
->compatible
) {
4903 * TODO: In time, we should notice the caller of this error by
4904 * telling him that this is a version error.
4908 ua_sess
= lookup_session_by_app(usess
, app
);
4910 /* The application has problem or is probably dead. */
4914 pthread_mutex_lock(&ua_sess
->lock
);
4916 if (ua_sess
->deleted
) {
4917 pthread_mutex_unlock(&ua_sess
->lock
);
4921 /* Lookup channel in the ust app session */
4922 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4923 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4924 /* If the channel is not found, there is a code flow error */
4925 assert(ua_chan_node
);
4927 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4929 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4930 pthread_mutex_unlock(&ua_sess
->lock
);
4932 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4933 /* Possible value at this point: -ENOMEM. If so, we stop! */
4936 DBG2("UST app event %s already exist on app PID %d",
4937 uevent
->attr
.name
, app
->pid
);
4947 * Start tracing for a specific UST session and app.
4949 * Called with UST app session lock held.
4953 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4956 struct ust_app_session
*ua_sess
;
4958 DBG("Starting tracing for ust app pid %d", app
->pid
);
4962 if (!app
->compatible
) {
4966 ua_sess
= lookup_session_by_app(usess
, app
);
4967 if (ua_sess
== NULL
) {
4968 /* The session is in teardown process. Ignore and continue. */
4972 pthread_mutex_lock(&ua_sess
->lock
);
4974 if (ua_sess
->deleted
) {
4975 pthread_mutex_unlock(&ua_sess
->lock
);
4979 if (ua_sess
->enabled
) {
4980 pthread_mutex_unlock(&ua_sess
->lock
);
4984 /* Upon restart, we skip the setup, already done */
4985 if (ua_sess
->started
) {
4989 health_code_update();
4992 /* This starts the UST tracing */
4993 pthread_mutex_lock(&app
->sock_lock
);
4994 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4995 pthread_mutex_unlock(&app
->sock_lock
);
4997 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4998 ERR("Error starting tracing for app pid: %d (ret: %d)",
5001 DBG("UST app start session failed. Application is dead.");
5003 * This is normal behavior, an application can die during the
5004 * creation process. Don't report an error so the execution can
5005 * continue normally.
5007 pthread_mutex_unlock(&ua_sess
->lock
);
5013 /* Indicate that the session has been started once */
5014 ua_sess
->started
= 1;
5015 ua_sess
->enabled
= 1;
5017 pthread_mutex_unlock(&ua_sess
->lock
);
5019 health_code_update();
5021 /* Quiescent wait after starting trace */
5022 pthread_mutex_lock(&app
->sock_lock
);
5023 ret
= ustctl_wait_quiescent(app
->sock
);
5024 pthread_mutex_unlock(&app
->sock_lock
);
5025 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5026 ERR("UST app wait quiescent failed for app pid %d ret %d",
5032 health_code_update();
5036 pthread_mutex_unlock(&ua_sess
->lock
);
5038 health_code_update();
5043 * Stop tracing for a specific UST session and app.
5046 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5049 struct ust_app_session
*ua_sess
;
5050 struct ust_registry_session
*registry
;
5052 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5056 if (!app
->compatible
) {
5057 goto end_no_session
;
5060 ua_sess
= lookup_session_by_app(usess
, app
);
5061 if (ua_sess
== NULL
) {
5062 goto end_no_session
;
5065 pthread_mutex_lock(&ua_sess
->lock
);
5067 if (ua_sess
->deleted
) {
5068 pthread_mutex_unlock(&ua_sess
->lock
);
5069 goto end_no_session
;
5073 * If started = 0, it means that stop trace has been called for a session
5074 * that was never started. It's possible since we can have a fail start
5075 * from either the application manager thread or the command thread. Simply
5076 * indicate that this is a stop error.
5078 if (!ua_sess
->started
) {
5079 goto error_rcu_unlock
;
5082 health_code_update();
5084 /* This inhibits UST tracing */
5085 pthread_mutex_lock(&app
->sock_lock
);
5086 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
5087 pthread_mutex_unlock(&app
->sock_lock
);
5089 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5090 ERR("Error stopping tracing for app pid: %d (ret: %d)",
5093 DBG("UST app stop session failed. Application is dead.");
5095 * This is normal behavior, an application can die during the
5096 * creation process. Don't report an error so the execution can
5097 * continue normally.
5101 goto error_rcu_unlock
;
5104 health_code_update();
5105 ua_sess
->enabled
= 0;
5107 /* Quiescent wait after stopping trace */
5108 pthread_mutex_lock(&app
->sock_lock
);
5109 ret
= ustctl_wait_quiescent(app
->sock
);
5110 pthread_mutex_unlock(&app
->sock_lock
);
5111 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5112 ERR("UST app wait quiescent failed for app pid %d ret %d",
5116 health_code_update();
5118 registry
= get_session_registry(ua_sess
);
5120 /* The UST app session is held registry shall not be null. */
5123 /* Push metadata for application before freeing the application. */
5124 (void) push_metadata(registry
, ua_sess
->consumer
);
5127 pthread_mutex_unlock(&ua_sess
->lock
);
5130 health_code_update();
5134 pthread_mutex_unlock(&ua_sess
->lock
);
5136 health_code_update();
5141 int ust_app_flush_app_session(struct ust_app
*app
,
5142 struct ust_app_session
*ua_sess
)
5144 int ret
, retval
= 0;
5145 struct lttng_ht_iter iter
;
5146 struct ust_app_channel
*ua_chan
;
5147 struct consumer_socket
*socket
;
5149 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5153 if (!app
->compatible
) {
5154 goto end_not_compatible
;
5157 pthread_mutex_lock(&ua_sess
->lock
);
5159 if (ua_sess
->deleted
) {
5163 health_code_update();
5165 /* Flushing buffers */
5166 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5169 /* Flush buffers and push metadata. */
5170 switch (ua_sess
->buffer_type
) {
5171 case LTTNG_BUFFER_PER_PID
:
5172 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5174 health_code_update();
5175 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5177 ERR("Error flushing consumer channel");
5183 case LTTNG_BUFFER_PER_UID
:
5189 health_code_update();
5192 pthread_mutex_unlock(&ua_sess
->lock
);
5196 health_code_update();
5201 * Flush buffers for all applications for a specific UST session.
5202 * Called with UST session lock held.
5205 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5210 DBG("Flushing session buffers for all ust apps");
5214 /* Flush buffers and push metadata. */
5215 switch (usess
->buffer_type
) {
5216 case LTTNG_BUFFER_PER_UID
:
5218 struct buffer_reg_uid
*reg
;
5219 struct lttng_ht_iter iter
;
5221 /* Flush all per UID buffers associated to that session. */
5222 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5223 struct ust_registry_session
*ust_session_reg
;
5224 struct buffer_reg_channel
*buf_reg_chan
;
5225 struct consumer_socket
*socket
;
5227 /* Get consumer socket to use to push the metadata.*/
5228 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5231 /* Ignore request if no consumer is found for the session. */
5235 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5236 buf_reg_chan
, node
.node
) {
5238 * The following call will print error values so the return
5239 * code is of little importance because whatever happens, we
5240 * have to try them all.
5242 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5245 ust_session_reg
= reg
->registry
->reg
.ust
;
5246 /* Push metadata. */
5247 (void) push_metadata(ust_session_reg
, usess
->consumer
);
5251 case LTTNG_BUFFER_PER_PID
:
5253 struct ust_app_session
*ua_sess
;
5254 struct lttng_ht_iter iter
;
5255 struct ust_app
*app
;
5257 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5258 ua_sess
= lookup_session_by_app(usess
, app
);
5259 if (ua_sess
== NULL
) {
5262 (void) ust_app_flush_app_session(app
, ua_sess
);
5273 health_code_update();
5278 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5279 struct ust_app_session
*ua_sess
)
5282 struct lttng_ht_iter iter
;
5283 struct ust_app_channel
*ua_chan
;
5284 struct consumer_socket
*socket
;
5286 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5290 if (!app
->compatible
) {
5291 goto end_not_compatible
;
5294 pthread_mutex_lock(&ua_sess
->lock
);
5296 if (ua_sess
->deleted
) {
5300 health_code_update();
5302 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5305 ERR("Failed to find consumer (%" PRIu32
") socket",
5306 app
->bits_per_long
);
5311 /* Clear quiescent state. */
5312 switch (ua_sess
->buffer_type
) {
5313 case LTTNG_BUFFER_PER_PID
:
5314 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5315 ua_chan
, node
.node
) {
5316 health_code_update();
5317 ret
= consumer_clear_quiescent_channel(socket
,
5320 ERR("Error clearing quiescent state for consumer channel");
5326 case LTTNG_BUFFER_PER_UID
:
5333 health_code_update();
5336 pthread_mutex_unlock(&ua_sess
->lock
);
5340 health_code_update();
5345 * Clear quiescent state in each stream for all applications for a
5346 * specific UST session.
5347 * Called with UST session lock held.
5350 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5355 DBG("Clearing stream quiescent state for all ust apps");
5359 switch (usess
->buffer_type
) {
5360 case LTTNG_BUFFER_PER_UID
:
5362 struct lttng_ht_iter iter
;
5363 struct buffer_reg_uid
*reg
;
5366 * Clear quiescent for all per UID buffers associated to
5369 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5370 struct consumer_socket
*socket
;
5371 struct buffer_reg_channel
*buf_reg_chan
;
5373 /* Get associated consumer socket.*/
5374 socket
= consumer_find_socket_by_bitness(
5375 reg
->bits_per_long
, usess
->consumer
);
5378 * Ignore request if no consumer is found for
5384 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5385 &iter
.iter
, buf_reg_chan
, node
.node
) {
5387 * The following call will print error values so
5388 * the return code is of little importance
5389 * because whatever happens, we have to try them
5392 (void) consumer_clear_quiescent_channel(socket
,
5393 buf_reg_chan
->consumer_key
);
5398 case LTTNG_BUFFER_PER_PID
:
5400 struct ust_app_session
*ua_sess
;
5401 struct lttng_ht_iter iter
;
5402 struct ust_app
*app
;
5404 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5406 ua_sess
= lookup_session_by_app(usess
, app
);
5407 if (ua_sess
== NULL
) {
5410 (void) ust_app_clear_quiescent_app_session(app
,
5422 health_code_update();
5427 * Destroy a specific UST session in apps.
5429 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5432 struct ust_app_session
*ua_sess
;
5433 struct lttng_ht_iter iter
;
5434 struct lttng_ht_node_u64
*node
;
5436 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5440 if (!app
->compatible
) {
5444 __lookup_session_by_app(usess
, app
, &iter
);
5445 node
= lttng_ht_iter_get_node_u64(&iter
);
5447 /* Session is being or is deleted. */
5450 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
5452 health_code_update();
5453 destroy_app_session(app
, ua_sess
);
5455 health_code_update();
5457 /* Quiescent wait after stopping trace */
5458 pthread_mutex_lock(&app
->sock_lock
);
5459 ret
= ustctl_wait_quiescent(app
->sock
);
5460 pthread_mutex_unlock(&app
->sock_lock
);
5461 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5462 ERR("UST app wait quiescent failed for app pid %d ret %d",
5467 health_code_update();
5472 * Start tracing for the UST session.
5474 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5476 struct lttng_ht_iter iter
;
5477 struct ust_app
*app
;
5479 DBG("Starting all UST traces");
5482 * Even though the start trace might fail, flag this session active so
5483 * other application coming in are started by default.
5490 * In a start-stop-start use-case, we need to clear the quiescent state
5491 * of each channel set by the prior stop command, thus ensuring that a
5492 * following stop or destroy is sure to grab a timestamp_end near those
5493 * operations, even if the packet is empty.
5495 (void) ust_app_clear_quiescent_session(usess
);
5497 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5498 ust_app_global_update(usess
, app
);
5507 * Start tracing for the UST session.
5508 * Called with UST session lock held.
5510 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5513 struct lttng_ht_iter iter
;
5514 struct ust_app
*app
;
5516 DBG("Stopping all UST traces");
5519 * Even though the stop trace might fail, flag this session inactive so
5520 * other application coming in are not started by default.
5526 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5527 ret
= ust_app_stop_trace(usess
, app
);
5529 /* Continue to next apps even on error */
5534 (void) ust_app_flush_session(usess
);
5542 * Destroy app UST session.
5544 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5547 struct lttng_ht_iter iter
;
5548 struct ust_app
*app
;
5550 DBG("Destroy all UST traces");
5554 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5555 ret
= destroy_trace(usess
, app
);
5557 /* Continue to next apps even on error */
5567 /* The ua_sess lock must be held by the caller. */
5569 int find_or_create_ust_app_channel(
5570 struct ltt_ust_session
*usess
,
5571 struct ust_app_session
*ua_sess
,
5572 struct ust_app
*app
,
5573 struct ltt_ust_channel
*uchan
,
5574 struct ust_app_channel
**ua_chan
)
5577 struct lttng_ht_iter iter
;
5578 struct lttng_ht_node_str
*ua_chan_node
;
5580 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5581 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5583 *ua_chan
= caa_container_of(ua_chan_node
,
5584 struct ust_app_channel
, node
);
5588 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5597 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5598 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5599 struct ust_app
*app
)
5602 struct ust_app_event
*ua_event
= NULL
;
5604 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5605 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5607 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5612 if (ua_event
->enabled
!= uevent
->enabled
) {
5613 ret
= uevent
->enabled
?
5614 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5615 disable_ust_app_event(ua_sess
, ua_event
, app
);
5623 /* Called with RCU read-side lock held. */
5625 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5628 enum lttng_error_code ret_code
;
5629 enum lttng_trigger_status t_status
;
5630 struct lttng_ht_iter app_trigger_iter
;
5631 struct lttng_triggers
*triggers
= NULL
;
5632 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5633 unsigned int count
, i
;
5636 * Currrently, registering or unregistering a trigger with an
5637 * event rule condition causes a full synchronization of the event
5640 * The first step attempts to add an event notifier for all registered
5641 * triggers that apply to the user space tracers. Then, the
5642 * application's event notifiers rules are all checked against the list
5643 * of registered triggers. Any event notifier that doesn't have a
5644 * matching trigger can be assumed to have been disabled.
5646 * All of this is inefficient, but is put in place to get the feature
5647 * rolling as it is simpler at this moment. It will be optimized Soon™
5648 * to allow the state of enabled
5649 * event notifiers to be synchronized in a piece-wise way.
5652 /* Get all triggers using uid 0 (root) */
5653 ret_code
= notification_thread_command_list_triggers(
5654 the_notification_thread_handle
, 0, &triggers
);
5655 if (ret_code
!= LTTNG_OK
) {
5662 t_status
= lttng_triggers_get_count(triggers
, &count
);
5663 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5668 for (i
= 0; i
< count
; i
++) {
5669 struct lttng_condition
*condition
;
5670 struct lttng_event_rule
*event_rule
;
5671 struct lttng_trigger
*trigger
;
5672 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5673 enum lttng_condition_status condition_status
;
5676 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5679 token
= lttng_trigger_get_tracer_token(trigger
);
5680 condition
= lttng_trigger_get_condition(trigger
);
5682 if (lttng_condition_get_type(condition
) != LTTNG_CONDITION_TYPE_ON_EVENT
) {
5683 /* Does not apply */
5687 condition_status
= lttng_condition_on_event_borrow_rule_mutable(condition
, &event_rule
);
5688 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5690 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5691 /* Skip kernel related triggers. */
5696 * Find or create the associated token event rule. The caller
5697 * holds the RCU read lock, so this is safe to call without
5698 * explicitly acquiring it here.
5700 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
5701 app
->token_to_event_notifier_rule_ht
, token
);
5702 if (!looked_up_event_notifier_rule
) {
5703 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
5711 /* Remove all unknown event sources from the app. */
5712 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
5713 &app_trigger_iter
.iter
, event_notifier_rule
,
5715 const uint64_t app_token
= event_notifier_rule
->token
;
5719 * Check if the app event trigger still exists on the
5720 * notification side.
5722 for (i
= 0; i
< count
; i
++) {
5723 uint64_t notification_thread_token
;
5724 const struct lttng_trigger
*trigger
=
5725 lttng_triggers_get_at_index(
5730 notification_thread_token
=
5731 lttng_trigger_get_tracer_token(trigger
);
5733 if (notification_thread_token
== app_token
) {
5745 * This trigger was unregistered, disable it on the tracer's
5748 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
5752 /* Callee logs errors. */
5753 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
5755 delete_ust_app_event_notifier_rule(
5756 app
->sock
, event_notifier_rule
, app
);
5762 lttng_triggers_destroy(triggers
);
5767 * RCU read lock must be held by the caller.
5770 void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
5771 struct ust_app_session
*ua_sess
,
5772 struct ust_app
*app
)
5775 struct cds_lfht_iter uchan_iter
;
5776 struct ltt_ust_channel
*uchan
;
5782 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5784 struct ust_app_channel
*ua_chan
;
5785 struct cds_lfht_iter uevent_iter
;
5786 struct ltt_ust_event
*uevent
;
5789 * Search for a matching ust_app_channel. If none is found,
5790 * create it. Creating the channel will cause the ua_chan
5791 * structure to be allocated, the channel buffers to be
5792 * allocated (if necessary) and sent to the application, and
5793 * all enabled contexts will be added to the channel.
5795 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5796 app
, uchan
, &ua_chan
);
5798 /* Tracer is probably gone or ENOMEM. */
5803 /* ua_chan will be NULL for the metadata channel */
5807 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5809 ret
= ust_app_channel_synchronize_event(ua_chan
,
5810 uevent
, ua_sess
, app
);
5816 if (ua_chan
->enabled
!= uchan
->enabled
) {
5817 ret
= uchan
->enabled
?
5818 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5819 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5830 * The caller must ensure that the application is compatible and is tracked
5831 * by the process attribute trackers.
5834 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5835 struct ust_app
*app
)
5838 struct ust_app_session
*ua_sess
= NULL
;
5841 * The application's configuration should only be synchronized for
5844 assert(usess
->active
);
5846 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5848 /* Tracer is probably gone or ENOMEM. */
5853 pthread_mutex_lock(&ua_sess
->lock
);
5854 if (ua_sess
->deleted
) {
5855 pthread_mutex_unlock(&ua_sess
->lock
);
5861 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
5864 * Create the metadata for the application. This returns gracefully if a
5865 * metadata was already set for the session.
5867 * The metadata channel must be created after the data channels as the
5868 * consumer daemon assumes this ordering. When interacting with a relay
5869 * daemon, the consumer will use this assumption to send the
5870 * "STREAMS_SENT" message to the relay daemon.
5872 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5880 pthread_mutex_unlock(&ua_sess
->lock
);
5881 /* Everything went well at this point. */
5886 pthread_mutex_unlock(&ua_sess
->lock
);
5889 destroy_app_session(app
, ua_sess
);
5895 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5897 struct ust_app_session
*ua_sess
;
5899 ua_sess
= lookup_session_by_app(usess
, app
);
5900 if (ua_sess
== NULL
) {
5903 destroy_app_session(app
, ua_sess
);
5907 * Add channels/events from UST global domain to registered apps at sock.
5909 * Called with session lock held.
5910 * Called with RCU read-side lock held.
5912 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5915 assert(usess
->active
);
5917 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5918 app
->sock
, usess
->id
);
5920 if (!app
->compatible
) {
5923 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5925 trace_ust_id_tracker_lookup(
5926 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5928 trace_ust_id_tracker_lookup(
5929 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5932 * Synchronize the application's internal tracing configuration
5933 * and start tracing.
5935 ust_app_synchronize(usess
, app
);
5936 ust_app_start_trace(usess
, app
);
5938 ust_app_global_destroy(usess
, app
);
5943 * Add all event notifiers to an application.
5945 * Called with session lock held.
5946 * Called with RCU read-side lock held.
5948 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
5950 DBG2("UST application global event notifier rules update: app = '%s' (ppid: %d)",
5951 app
->name
, app
->ppid
);
5953 if (!app
->compatible
) {
5957 if (app
->event_notifier_group
.object
== NULL
) {
5958 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s' (ppid: %d)",
5959 app
->name
, app
->ppid
);
5963 ust_app_synchronize_event_notifier_rules(app
);
5967 * Called with session lock held.
5969 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5971 struct lttng_ht_iter iter
;
5972 struct ust_app
*app
;
5975 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5976 ust_app_global_update(usess
, app
);
5981 void ust_app_global_update_all_event_notifier_rules(void)
5983 struct lttng_ht_iter iter
;
5984 struct ust_app
*app
;
5987 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5988 ust_app_global_update_event_notifier_rules(app
);
5995 * Add context to a specific channel for global UST domain.
5997 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5998 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
6001 struct lttng_ht_node_str
*ua_chan_node
;
6002 struct lttng_ht_iter iter
, uiter
;
6003 struct ust_app_channel
*ua_chan
= NULL
;
6004 struct ust_app_session
*ua_sess
;
6005 struct ust_app
*app
;
6007 assert(usess
->active
);
6010 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6011 if (!app
->compatible
) {
6013 * TODO: In time, we should notice the caller of this error by
6014 * telling him that this is a version error.
6018 ua_sess
= lookup_session_by_app(usess
, app
);
6019 if (ua_sess
== NULL
) {
6023 pthread_mutex_lock(&ua_sess
->lock
);
6025 if (ua_sess
->deleted
) {
6026 pthread_mutex_unlock(&ua_sess
->lock
);
6030 /* Lookup channel in the ust app session */
6031 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
6032 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6033 if (ua_chan_node
== NULL
) {
6036 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
6038 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6043 pthread_mutex_unlock(&ua_sess
->lock
);
6051 * Receive registration and populate the given msg structure.
6053 * On success return 0 else a negative value returned by the ustctl call.
6055 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6058 uint32_t pid
, ppid
, uid
, gid
;
6062 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
6063 &pid
, &ppid
, &uid
, &gid
,
6064 &msg
->bits_per_long
,
6065 &msg
->uint8_t_alignment
,
6066 &msg
->uint16_t_alignment
,
6067 &msg
->uint32_t_alignment
,
6068 &msg
->uint64_t_alignment
,
6069 &msg
->long_alignment
,
6076 case LTTNG_UST_ERR_EXITING
:
6077 DBG3("UST app recv reg message failed. Application died");
6079 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6080 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6081 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
6082 LTTNG_UST_ABI_MINOR_VERSION
);
6085 ERR("UST app recv reg message failed with ret %d", ret
);
6090 msg
->pid
= (pid_t
) pid
;
6091 msg
->ppid
= (pid_t
) ppid
;
6092 msg
->uid
= (uid_t
) uid
;
6093 msg
->gid
= (gid_t
) gid
;
6100 * Return a ust app session object using the application object and the
6101 * session object descriptor has a key. If not found, NULL is returned.
6102 * A RCU read side lock MUST be acquired when calling this function.
6104 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
6107 struct lttng_ht_node_ulong
*node
;
6108 struct lttng_ht_iter iter
;
6109 struct ust_app_session
*ua_sess
= NULL
;
6113 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
6114 node
= lttng_ht_iter_get_node_ulong(&iter
);
6116 DBG2("UST app session find by objd %d not found", objd
);
6120 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
6127 * Return a ust app channel object using the application object and the channel
6128 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6129 * lock MUST be acquired before calling this function.
6131 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
6134 struct lttng_ht_node_ulong
*node
;
6135 struct lttng_ht_iter iter
;
6136 struct ust_app_channel
*ua_chan
= NULL
;
6140 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
6141 node
= lttng_ht_iter_get_node_ulong(&iter
);
6143 DBG2("UST app channel find by objd %d not found", objd
);
6147 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
6154 * Reply to a register channel notification from an application on the notify
6155 * socket. The channel metadata is also created.
6157 * The session UST registry lock is acquired in this function.
6159 * On success 0 is returned else a negative value.
6161 static int reply_ust_register_channel(int sock
, int cobjd
,
6162 size_t nr_fields
, struct ustctl_field
*fields
)
6164 int ret
, ret_code
= 0;
6166 uint64_t chan_reg_key
;
6167 enum ustctl_channel_header type
;
6168 struct ust_app
*app
;
6169 struct ust_app_channel
*ua_chan
;
6170 struct ust_app_session
*ua_sess
;
6171 struct ust_registry_session
*registry
;
6172 struct ust_registry_channel
*ust_reg_chan
;
6176 /* Lookup application. If not found, there is a code flow error. */
6177 app
= find_app_by_notify_sock(sock
);
6179 DBG("Application socket %d is being torn down. Abort event notify",
6182 goto error_rcu_unlock
;
6185 /* Lookup channel by UST object descriptor. */
6186 ua_chan
= find_channel_by_objd(app
, cobjd
);
6188 DBG("Application channel is being torn down. Abort event notify");
6190 goto error_rcu_unlock
;
6193 assert(ua_chan
->session
);
6194 ua_sess
= ua_chan
->session
;
6196 /* Get right session registry depending on the session buffer type. */
6197 registry
= get_session_registry(ua_sess
);
6199 DBG("Application session is being torn down. Abort event notify");
6201 goto error_rcu_unlock
;
6204 /* Depending on the buffer type, a different channel key is used. */
6205 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6206 chan_reg_key
= ua_chan
->tracing_channel_id
;
6208 chan_reg_key
= ua_chan
->key
;
6211 pthread_mutex_lock(®istry
->lock
);
6213 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
6214 assert(ust_reg_chan
);
6216 if (!ust_reg_chan
->register_done
) {
6218 * TODO: eventually use the registry event count for
6219 * this channel to better guess header type for per-pid
6222 type
= USTCTL_CHANNEL_HEADER_LARGE
;
6223 ust_reg_chan
->nr_ctx_fields
= nr_fields
;
6224 ust_reg_chan
->ctx_fields
= fields
;
6226 ust_reg_chan
->header_type
= type
;
6228 /* Get current already assigned values. */
6229 type
= ust_reg_chan
->header_type
;
6231 /* Channel id is set during the object creation. */
6232 chan_id
= ust_reg_chan
->chan_id
;
6234 /* Append to metadata */
6235 if (!ust_reg_chan
->metadata_dumped
) {
6236 ret_code
= ust_metadata_channel_statedump(registry
, ust_reg_chan
);
6238 ERR("Error appending channel metadata (errno = %d)", ret_code
);
6244 DBG3("UST app replying to register channel key %" PRIu64
6245 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
6248 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
6250 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6251 ERR("UST app reply channel failed with ret %d", ret
);
6253 DBG3("UST app reply channel failed. Application died");
6258 /* This channel registry registration is completed. */
6259 ust_reg_chan
->register_done
= 1;
6262 pthread_mutex_unlock(®istry
->lock
);
6270 * Add event to the UST channel registry. When the event is added to the
6271 * registry, the metadata is also created. Once done, this replies to the
6272 * application with the appropriate error code.
6274 * The session UST registry lock is acquired in the function.
6276 * On success 0 is returned else a negative value.
6278 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
6279 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
6280 int loglevel_value
, char *model_emf_uri
)
6283 uint32_t event_id
= 0;
6284 uint64_t chan_reg_key
;
6285 struct ust_app
*app
;
6286 struct ust_app_channel
*ua_chan
;
6287 struct ust_app_session
*ua_sess
;
6288 struct ust_registry_session
*registry
;
6292 /* Lookup application. If not found, there is a code flow error. */
6293 app
= find_app_by_notify_sock(sock
);
6295 DBG("Application socket %d is being torn down. Abort event notify",
6298 goto error_rcu_unlock
;
6301 /* Lookup channel by UST object descriptor. */
6302 ua_chan
= find_channel_by_objd(app
, cobjd
);
6304 DBG("Application channel is being torn down. Abort event notify");
6306 goto error_rcu_unlock
;
6309 assert(ua_chan
->session
);
6310 ua_sess
= ua_chan
->session
;
6312 registry
= get_session_registry(ua_sess
);
6314 DBG("Application session is being torn down. Abort event notify");
6316 goto error_rcu_unlock
;
6319 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6320 chan_reg_key
= ua_chan
->tracing_channel_id
;
6322 chan_reg_key
= ua_chan
->key
;
6325 pthread_mutex_lock(®istry
->lock
);
6328 * From this point on, this call acquires the ownership of the sig, fields
6329 * and model_emf_uri meaning any free are done inside it if needed. These
6330 * three variables MUST NOT be read/write after this.
6332 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
6333 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
6334 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
6338 model_emf_uri
= NULL
;
6341 * The return value is returned to ustctl so in case of an error, the
6342 * application can be notified. In case of an error, it's important not to
6343 * return a negative error or else the application will get closed.
6345 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
6347 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6348 ERR("UST app reply event failed with ret %d", ret
);
6350 DBG3("UST app reply event failed. Application died");
6353 * No need to wipe the create event since the application socket will
6354 * get close on error hence cleaning up everything by itself.
6359 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6363 pthread_mutex_unlock(®istry
->lock
);
6368 free(model_emf_uri
);
6373 * Add enum to the UST session registry. Once done, this replies to the
6374 * application with the appropriate error code.
6376 * The session UST registry lock is acquired within this function.
6378 * On success 0 is returned else a negative value.
6380 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
6381 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
6383 int ret
= 0, ret_code
;
6384 struct ust_app
*app
;
6385 struct ust_app_session
*ua_sess
;
6386 struct ust_registry_session
*registry
;
6387 uint64_t enum_id
= -1ULL;
6391 /* Lookup application. If not found, there is a code flow error. */
6392 app
= find_app_by_notify_sock(sock
);
6394 /* Return an error since this is not an error */
6395 DBG("Application socket %d is being torn down. Aborting enum registration",
6398 goto error_rcu_unlock
;
6401 /* Lookup session by UST object descriptor. */
6402 ua_sess
= find_session_by_objd(app
, sobjd
);
6404 /* Return an error since this is not an error */
6405 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6407 goto error_rcu_unlock
;
6410 registry
= get_session_registry(ua_sess
);
6412 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6414 goto error_rcu_unlock
;
6417 pthread_mutex_lock(®istry
->lock
);
6420 * From this point on, the callee acquires the ownership of
6421 * entries. The variable entries MUST NOT be read/written after
6424 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
6425 entries
, nr_entries
, &enum_id
);
6429 * The return value is returned to ustctl so in case of an error, the
6430 * application can be notified. In case of an error, it's important not to
6431 * return a negative error or else the application will get closed.
6433 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
6435 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6436 ERR("UST app reply enum failed with ret %d", ret
);
6438 DBG3("UST app reply enum failed. Application died");
6441 * No need to wipe the create enum since the application socket will
6442 * get close on error hence cleaning up everything by itself.
6447 DBG3("UST registry enum %s added successfully or already found", name
);
6450 pthread_mutex_unlock(®istry
->lock
);
6457 * Handle application notification through the given notify socket.
6459 * Return 0 on success or else a negative value.
6461 int ust_app_recv_notify(int sock
)
6464 enum ustctl_notify_cmd cmd
;
6466 DBG3("UST app receiving notify from sock %d", sock
);
6468 ret
= ustctl_recv_notify(sock
, &cmd
);
6470 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6471 ERR("UST app recv notify failed with ret %d", ret
);
6473 DBG3("UST app recv notify failed. Application died");
6479 case USTCTL_NOTIFY_CMD_EVENT
:
6481 int sobjd
, cobjd
, loglevel_value
;
6482 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6484 struct ustctl_field
*fields
;
6486 DBG2("UST app ustctl register event received");
6488 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
6489 &loglevel_value
, &sig
, &nr_fields
, &fields
,
6492 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6493 ERR("UST app recv event failed with ret %d", ret
);
6495 DBG3("UST app recv event failed. Application died");
6501 * Add event to the UST registry coming from the notify socket. This
6502 * call will free if needed the sig, fields and model_emf_uri. This
6503 * code path loses the ownsership of these variables and transfer them
6504 * to the this function.
6506 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6507 fields
, loglevel_value
, model_emf_uri
);
6514 case USTCTL_NOTIFY_CMD_CHANNEL
:
6518 struct ustctl_field
*fields
;
6520 DBG2("UST app ustctl register channel received");
6522 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
6525 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6526 ERR("UST app recv channel failed with ret %d", ret
);
6528 DBG3("UST app recv channel failed. Application died");
6534 * The fields ownership are transfered to this function call meaning
6535 * that if needed it will be freed. After this, it's invalid to access
6536 * fields or clean it up.
6538 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
6546 case USTCTL_NOTIFY_CMD_ENUM
:
6549 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
6551 struct ustctl_enum_entry
*entries
;
6553 DBG2("UST app ustctl register enum received");
6555 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
6556 &entries
, &nr_entries
);
6558 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6559 ERR("UST app recv enum failed with ret %d", ret
);
6561 DBG3("UST app recv enum failed. Application died");
6566 /* Callee assumes ownership of entries */
6567 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6568 entries
, nr_entries
);
6576 /* Should NEVER happen. */
6585 * Once the notify socket hangs up, this is called. First, it tries to find the
6586 * corresponding application. On failure, the call_rcu to close the socket is
6587 * executed. If an application is found, it tries to delete it from the notify
6588 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6590 * Note that an object needs to be allocated here so on ENOMEM failure, the
6591 * call RCU is not done but the rest of the cleanup is.
6593 void ust_app_notify_sock_unregister(int sock
)
6596 struct lttng_ht_iter iter
;
6597 struct ust_app
*app
;
6598 struct ust_app_notify_sock_obj
*obj
;
6604 obj
= zmalloc(sizeof(*obj
));
6607 * An ENOMEM is kind of uncool. If this strikes we continue the
6608 * procedure but the call_rcu will not be called. In this case, we
6609 * accept the fd leak rather than possibly creating an unsynchronized
6610 * state between threads.
6612 * TODO: The notify object should be created once the notify socket is
6613 * registered and stored independantely from the ust app object. The
6614 * tricky part is to synchronize the teardown of the application and
6615 * this notify object. Let's keep that in mind so we can avoid this
6616 * kind of shenanigans with ENOMEM in the teardown path.
6623 DBG("UST app notify socket unregister %d", sock
);
6626 * Lookup application by notify socket. If this fails, this means that the
6627 * hash table delete has already been done by the application
6628 * unregistration process so we can safely close the notify socket in a
6631 app
= find_app_by_notify_sock(sock
);
6636 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6639 * Whatever happens here either we fail or succeed, in both cases we have
6640 * to close the socket after a grace period to continue to the call RCU
6641 * here. If the deletion is successful, the application is not visible
6642 * anymore by other threads and is it fails it means that it was already
6643 * deleted from the hash table so either way we just have to close the
6646 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6652 * Close socket after a grace period to avoid for the socket to be reused
6653 * before the application object is freed creating potential race between
6654 * threads trying to add unique in the global hash table.
6657 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6662 * Destroy a ust app data structure and free its memory.
6664 void ust_app_destroy(struct ust_app
*app
)
6670 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6674 * Take a snapshot for a given UST session. The snapshot is sent to the given
6677 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6679 enum lttng_error_code
ust_app_snapshot_record(
6680 const struct ltt_ust_session
*usess
,
6681 const struct consumer_output
*output
, int wait
,
6682 uint64_t nb_packets_per_stream
)
6685 enum lttng_error_code status
= LTTNG_OK
;
6686 struct lttng_ht_iter iter
;
6687 struct ust_app
*app
;
6688 char *trace_path
= NULL
;
6695 switch (usess
->buffer_type
) {
6696 case LTTNG_BUFFER_PER_UID
:
6698 struct buffer_reg_uid
*reg
;
6700 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6701 struct buffer_reg_channel
*buf_reg_chan
;
6702 struct consumer_socket
*socket
;
6703 char pathname
[PATH_MAX
];
6704 size_t consumer_path_offset
= 0;
6706 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6707 /* Skip since no metadata is present */
6711 /* Get consumer socket to use to push the metadata.*/
6712 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6715 status
= LTTNG_ERR_INVALID
;
6719 memset(pathname
, 0, sizeof(pathname
));
6720 ret
= snprintf(pathname
, sizeof(pathname
),
6721 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
6722 reg
->uid
, reg
->bits_per_long
);
6724 PERROR("snprintf snapshot path");
6725 status
= LTTNG_ERR_INVALID
;
6728 /* Free path allowed on previous iteration. */
6730 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6731 &consumer_path_offset
);
6733 status
= LTTNG_ERR_INVALID
;
6736 /* Add the UST default trace dir to path. */
6737 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6738 buf_reg_chan
, node
.node
) {
6739 status
= consumer_snapshot_channel(socket
,
6740 buf_reg_chan
->consumer_key
,
6741 output
, 0, usess
->uid
,
6742 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
6743 nb_packets_per_stream
);
6744 if (status
!= LTTNG_OK
) {
6748 status
= consumer_snapshot_channel(socket
,
6749 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6750 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6752 if (status
!= LTTNG_OK
) {
6758 case LTTNG_BUFFER_PER_PID
:
6760 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6761 struct consumer_socket
*socket
;
6762 struct lttng_ht_iter chan_iter
;
6763 struct ust_app_channel
*ua_chan
;
6764 struct ust_app_session
*ua_sess
;
6765 struct ust_registry_session
*registry
;
6766 char pathname
[PATH_MAX
];
6767 size_t consumer_path_offset
= 0;
6769 ua_sess
= lookup_session_by_app(usess
, app
);
6771 /* Session not associated with this app. */
6775 /* Get the right consumer socket for the application. */
6776 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6779 status
= LTTNG_ERR_INVALID
;
6783 /* Add the UST default trace dir to path. */
6784 memset(pathname
, 0, sizeof(pathname
));
6785 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
6788 status
= LTTNG_ERR_INVALID
;
6789 PERROR("snprintf snapshot path");
6792 /* Free path allowed on previous iteration. */
6794 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6795 &consumer_path_offset
);
6797 status
= LTTNG_ERR_INVALID
;
6800 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6801 ua_chan
, node
.node
) {
6802 status
= consumer_snapshot_channel(socket
,
6803 ua_chan
->key
, output
, 0,
6804 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6805 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6806 &trace_path
[consumer_path_offset
], wait
,
6807 nb_packets_per_stream
);
6811 case LTTNG_ERR_CHAN_NOT_FOUND
:
6818 registry
= get_session_registry(ua_sess
);
6820 DBG("Application session is being torn down. Skip application.");
6823 status
= consumer_snapshot_channel(socket
,
6824 registry
->metadata_key
, output
, 1,
6825 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6826 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6827 &trace_path
[consumer_path_offset
], wait
, 0);
6831 case LTTNG_ERR_CHAN_NOT_FOUND
:
6851 * Return the size taken by one more packet per stream.
6853 uint64_t ust_app_get_size_one_more_packet_per_stream(
6854 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6856 uint64_t tot_size
= 0;
6857 struct ust_app
*app
;
6858 struct lttng_ht_iter iter
;
6862 switch (usess
->buffer_type
) {
6863 case LTTNG_BUFFER_PER_UID
:
6865 struct buffer_reg_uid
*reg
;
6867 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6868 struct buffer_reg_channel
*buf_reg_chan
;
6871 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6872 buf_reg_chan
, node
.node
) {
6873 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
6875 * Don't take channel into account if we
6876 * already grab all its packets.
6880 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
6886 case LTTNG_BUFFER_PER_PID
:
6889 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6890 struct ust_app_channel
*ua_chan
;
6891 struct ust_app_session
*ua_sess
;
6892 struct lttng_ht_iter chan_iter
;
6894 ua_sess
= lookup_session_by_app(usess
, app
);
6896 /* Session not associated with this app. */
6900 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6901 ua_chan
, node
.node
) {
6902 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6904 * Don't take channel into account if we
6905 * already grab all its packets.
6909 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6923 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6924 struct cds_list_head
*buffer_reg_uid_list
,
6925 struct consumer_output
*consumer
, uint64_t uchan_id
,
6926 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6929 uint64_t consumer_chan_key
;
6934 ret
= buffer_reg_uid_consumer_channel_key(
6935 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6943 ret
= consumer_get_lost_packets(ust_session_id
,
6944 consumer_chan_key
, consumer
, lost
);
6946 ret
= consumer_get_discarded_events(ust_session_id
,
6947 consumer_chan_key
, consumer
, discarded
);
6954 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6955 struct ltt_ust_channel
*uchan
,
6956 struct consumer_output
*consumer
, int overwrite
,
6957 uint64_t *discarded
, uint64_t *lost
)
6960 struct lttng_ht_iter iter
;
6961 struct lttng_ht_node_str
*ua_chan_node
;
6962 struct ust_app
*app
;
6963 struct ust_app_session
*ua_sess
;
6964 struct ust_app_channel
*ua_chan
;
6971 * Iterate over every registered applications. Sum counters for
6972 * all applications containing requested session and channel.
6974 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6975 struct lttng_ht_iter uiter
;
6977 ua_sess
= lookup_session_by_app(usess
, app
);
6978 if (ua_sess
== NULL
) {
6983 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6984 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6985 /* If the session is found for the app, the channel must be there */
6986 assert(ua_chan_node
);
6988 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6993 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
7000 uint64_t _discarded
;
7002 ret
= consumer_get_discarded_events(usess
->id
,
7003 ua_chan
->key
, consumer
, &_discarded
);
7007 (*discarded
) += _discarded
;
7016 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
7017 struct ust_app
*app
)
7020 struct ust_app_session
*ua_sess
;
7022 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7026 ua_sess
= lookup_session_by_app(usess
, app
);
7027 if (ua_sess
== NULL
) {
7028 /* The session is in teardown process. Ignore and continue. */
7032 pthread_mutex_lock(&ua_sess
->lock
);
7034 if (ua_sess
->deleted
) {
7038 pthread_mutex_lock(&app
->sock_lock
);
7039 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7040 pthread_mutex_unlock(&app
->sock_lock
);
7043 pthread_mutex_unlock(&ua_sess
->lock
);
7047 health_code_update();
7052 * Regenerate the statedump for each app in the session.
7054 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7057 struct lttng_ht_iter iter
;
7058 struct ust_app
*app
;
7060 DBG("Regenerating the metadata for all UST apps");
7064 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7065 if (!app
->compatible
) {
7069 ret
= ust_app_regenerate_statedump(usess
, app
);
7071 /* Continue to the next app even on error */
7082 * Rotate all the channels of a session.
7084 * Return LTTNG_OK on success or else an LTTng error code.
7086 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7089 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7090 struct lttng_ht_iter iter
;
7091 struct ust_app
*app
;
7092 struct ltt_ust_session
*usess
= session
->ust_session
;
7098 switch (usess
->buffer_type
) {
7099 case LTTNG_BUFFER_PER_UID
:
7101 struct buffer_reg_uid
*reg
;
7103 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7104 struct buffer_reg_channel
*buf_reg_chan
;
7105 struct consumer_socket
*socket
;
7107 /* Get consumer socket to use to push the metadata.*/
7108 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7111 cmd_ret
= LTTNG_ERR_INVALID
;
7115 /* Rotate the data channels. */
7116 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7117 buf_reg_chan
, node
.node
) {
7118 ret
= consumer_rotate_channel(socket
,
7119 buf_reg_chan
->consumer_key
,
7120 usess
->uid
, usess
->gid
,
7122 /* is_metadata_channel */ false);
7124 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7130 * The metadata channel might not be present.
7132 * Consumer stream allocation can be done
7133 * asynchronously and can fail on intermediary
7134 * operations (i.e add context) and lead to data
7135 * channels created with no metadata channel.
7137 if (!reg
->registry
->reg
.ust
->metadata_key
) {
7138 /* Skip since no metadata is present. */
7142 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7144 ret
= consumer_rotate_channel(socket
,
7145 reg
->registry
->reg
.ust
->metadata_key
,
7146 usess
->uid
, usess
->gid
,
7148 /* is_metadata_channel */ true);
7150 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7156 case LTTNG_BUFFER_PER_PID
:
7158 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7159 struct consumer_socket
*socket
;
7160 struct lttng_ht_iter chan_iter
;
7161 struct ust_app_channel
*ua_chan
;
7162 struct ust_app_session
*ua_sess
;
7163 struct ust_registry_session
*registry
;
7165 ua_sess
= lookup_session_by_app(usess
, app
);
7167 /* Session not associated with this app. */
7171 /* Get the right consumer socket for the application. */
7172 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7175 cmd_ret
= LTTNG_ERR_INVALID
;
7179 registry
= get_session_registry(ua_sess
);
7181 DBG("Application session is being torn down. Skip application.");
7185 /* Rotate the data channels. */
7186 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7187 ua_chan
, node
.node
) {
7188 ret
= consumer_rotate_channel(socket
,
7190 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7191 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7193 /* is_metadata_channel */ false);
7195 /* Per-PID buffer and application going away. */
7196 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7198 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7203 /* Rotate the metadata channel. */
7204 (void) push_metadata(registry
, usess
->consumer
);
7205 ret
= consumer_rotate_channel(socket
,
7206 registry
->metadata_key
,
7207 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7208 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7210 /* is_metadata_channel */ true);
7212 /* Per-PID buffer and application going away. */
7213 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7215 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7233 enum lttng_error_code
ust_app_create_channel_subdirectories(
7234 const struct ltt_ust_session
*usess
)
7236 enum lttng_error_code ret
= LTTNG_OK
;
7237 struct lttng_ht_iter iter
;
7238 enum lttng_trace_chunk_status chunk_status
;
7239 char *pathname_index
;
7242 assert(usess
->current_trace_chunk
);
7245 switch (usess
->buffer_type
) {
7246 case LTTNG_BUFFER_PER_UID
:
7248 struct buffer_reg_uid
*reg
;
7250 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7251 fmt_ret
= asprintf(&pathname_index
,
7252 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7253 reg
->uid
, reg
->bits_per_long
);
7255 ERR("Failed to format channel index directory");
7256 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7261 * Create the index subdirectory which will take care
7262 * of implicitly creating the channel's path.
7264 chunk_status
= lttng_trace_chunk_create_subdirectory(
7265 usess
->current_trace_chunk
,
7267 free(pathname_index
);
7268 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7269 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7275 case LTTNG_BUFFER_PER_PID
:
7277 struct ust_app
*app
;
7280 * Create the toplevel ust/ directory in case no apps are running.
7282 chunk_status
= lttng_trace_chunk_create_subdirectory(
7283 usess
->current_trace_chunk
,
7284 DEFAULT_UST_TRACE_DIR
);
7285 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7286 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7290 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7292 struct ust_app_session
*ua_sess
;
7293 struct ust_registry_session
*registry
;
7295 ua_sess
= lookup_session_by_app(usess
, app
);
7297 /* Session not associated with this app. */
7301 registry
= get_session_registry(ua_sess
);
7303 DBG("Application session is being torn down. Skip application.");
7307 fmt_ret
= asprintf(&pathname_index
,
7308 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7311 ERR("Failed to format channel index directory");
7312 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7316 * Create the index subdirectory which will take care
7317 * of implicitly creating the channel's path.
7319 chunk_status
= lttng_trace_chunk_create_subdirectory(
7320 usess
->current_trace_chunk
,
7322 free(pathname_index
);
7323 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7324 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7341 * Clear all the channels of a session.
7343 * Return LTTNG_OK on success or else an LTTng error code.
7345 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7348 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7349 struct lttng_ht_iter iter
;
7350 struct ust_app
*app
;
7351 struct ltt_ust_session
*usess
= session
->ust_session
;
7357 if (usess
->active
) {
7358 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7359 cmd_ret
= LTTNG_ERR_FATAL
;
7363 switch (usess
->buffer_type
) {
7364 case LTTNG_BUFFER_PER_UID
:
7366 struct buffer_reg_uid
*reg
;
7368 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7369 struct buffer_reg_channel
*buf_reg_chan
;
7370 struct consumer_socket
*socket
;
7372 /* Get consumer socket to use to push the metadata.*/
7373 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7376 cmd_ret
= LTTNG_ERR_INVALID
;
7380 /* Clear the data channels. */
7381 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7382 buf_reg_chan
, node
.node
) {
7383 ret
= consumer_clear_channel(socket
,
7384 buf_reg_chan
->consumer_key
);
7390 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7393 * Clear the metadata channel.
7394 * Metadata channel is not cleared per se but we still need to
7395 * perform a rotation operation on it behind the scene.
7397 ret
= consumer_clear_channel(socket
,
7398 reg
->registry
->reg
.ust
->metadata_key
);
7405 case LTTNG_BUFFER_PER_PID
:
7407 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7408 struct consumer_socket
*socket
;
7409 struct lttng_ht_iter chan_iter
;
7410 struct ust_app_channel
*ua_chan
;
7411 struct ust_app_session
*ua_sess
;
7412 struct ust_registry_session
*registry
;
7414 ua_sess
= lookup_session_by_app(usess
, app
);
7416 /* Session not associated with this app. */
7420 /* Get the right consumer socket for the application. */
7421 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7424 cmd_ret
= LTTNG_ERR_INVALID
;
7428 registry
= get_session_registry(ua_sess
);
7430 DBG("Application session is being torn down. Skip application.");
7434 /* Clear the data channels. */
7435 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7436 ua_chan
, node
.node
) {
7437 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7439 /* Per-PID buffer and application going away. */
7440 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7447 (void) push_metadata(registry
, usess
->consumer
);
7450 * Clear the metadata channel.
7451 * Metadata channel is not cleared per se but we still need to
7452 * perform rotation operation on it behind the scene.
7454 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
7456 /* Per-PID buffer and application going away. */
7457 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7475 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7476 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7479 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7489 * This function skips the metadata channel as the begin/end timestamps of a
7490 * metadata packet are useless.
7492 * Moreover, opening a packet after a "clear" will cause problems for live
7493 * sessions as it will introduce padding that was not part of the first trace
7494 * chunk. The relay daemon expects the content of the metadata stream of
7495 * successive metadata trace chunks to be strict supersets of one another.
7497 * For example, flushing a packet at the beginning of the metadata stream of
7498 * a trace chunk resulting from a "clear" session command will cause the
7499 * size of the metadata stream of the new trace chunk to not match the size of
7500 * the metadata stream of the original chunk. This will confuse the relay
7501 * daemon as the same "offset" in a metadata stream will no longer point
7502 * to the same content.
7504 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7506 enum lttng_error_code ret
= LTTNG_OK
;
7507 struct lttng_ht_iter iter
;
7508 struct ltt_ust_session
*usess
= session
->ust_session
;
7514 switch (usess
->buffer_type
) {
7515 case LTTNG_BUFFER_PER_UID
:
7517 struct buffer_reg_uid
*reg
;
7519 cds_list_for_each_entry (
7520 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7521 struct buffer_reg_channel
*buf_reg_chan
;
7522 struct consumer_socket
*socket
;
7524 socket
= consumer_find_socket_by_bitness(
7525 reg
->bits_per_long
, usess
->consumer
);
7527 ret
= LTTNG_ERR_FATAL
;
7531 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7532 &iter
.iter
, buf_reg_chan
, node
.node
) {
7533 const int open_ret
=
7534 consumer_open_channel_packets(
7536 buf_reg_chan
->consumer_key
);
7539 ret
= LTTNG_ERR_UNK
;
7546 case LTTNG_BUFFER_PER_PID
:
7548 struct ust_app
*app
;
7550 cds_lfht_for_each_entry (
7551 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7552 struct consumer_socket
*socket
;
7553 struct lttng_ht_iter chan_iter
;
7554 struct ust_app_channel
*ua_chan
;
7555 struct ust_app_session
*ua_sess
;
7556 struct ust_registry_session
*registry
;
7558 ua_sess
= lookup_session_by_app(usess
, app
);
7560 /* Session not associated with this app. */
7564 /* Get the right consumer socket for the application. */
7565 socket
= consumer_find_socket_by_bitness(
7566 app
->bits_per_long
, usess
->consumer
);
7568 ret
= LTTNG_ERR_FATAL
;
7572 registry
= get_session_registry(ua_sess
);
7574 DBG("Application session is being torn down. Skip application.");
7578 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
7579 &chan_iter
.iter
, ua_chan
, node
.node
) {
7580 const int open_ret
=
7581 consumer_open_channel_packets(
7587 * Per-PID buffer and application going
7590 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7594 ret
= LTTNG_ERR_UNK
;