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/user-tracepoint.h>
31 #include <lttng/condition/condition.h>
32 #include <lttng/condition/event-rule-matches-internal.h>
33 #include <lttng/condition/event-rule-matches.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 lttng_ust_ctl_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
;
198 LTTNG_ASSERT(ua_chan
);
199 LTTNG_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 LTTNG_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 LTTNG_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 LTTNG_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
;
248 LTTNG_ASSERT(ua_sess
);
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
,
289 LTTNG_ASSERT(ua_ctx
);
292 pthread_mutex_lock(&app
->sock_lock
);
293 ret
= lttng_ust_ctl_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
,
314 LTTNG_ASSERT(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
= lttng_ust_ctl_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 LTTNG_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
= lttng_ust_ctl_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
,
387 LTTNG_ASSERT(stream
);
390 pthread_mutex_lock(&app
->sock_lock
);
391 ret
= lttng_ust_ctl_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
,
412 LTTNG_ASSERT(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
;
518 LTTNG_ASSERT(ua_chan
);
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
= lttng_ust_ctl_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
= lttng_ust_ctl_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
= lttng_ust_ctl_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
;
627 LTTNG_ASSERT(registry
);
628 LTTNG_ASSERT(socket
);
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
;
763 LTTNG_ASSERT(registry
);
764 LTTNG_ASSERT(consumer
);
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
;
813 LTTNG_ASSERT(registry
);
814 LTTNG_ASSERT(consumer
);
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
;
885 LTTNG_ASSERT(ua_sess
);
887 pthread_mutex_lock(&ua_sess
->lock
);
889 LTTNG_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
= lttng_ust_ctl_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 lttng_ust_ctl_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
;
1092 LTTNG_ASSERT(ua_sess
);
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 lttng_ust_ctl_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 enum lttng_condition_status cond_status
;
1253 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1254 struct lttng_condition
*condition
= NULL
;
1255 const struct lttng_event_rule
*event_rule
= NULL
;
1257 ua_event_notifier_rule
= zmalloc(sizeof(struct ust_app_event_notifier_rule
));
1258 if (ua_event_notifier_rule
== NULL
) {
1259 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1263 ua_event_notifier_rule
->enabled
= 1;
1264 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1265 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
,
1266 ua_event_notifier_rule
->token
);
1268 condition
= lttng_trigger_get_condition(trigger
);
1269 LTTNG_ASSERT(condition
);
1270 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1271 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1273 cond_status
= lttng_condition_event_rule_matches_get_rule(
1274 condition
, &event_rule
);
1275 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
1276 LTTNG_ASSERT(event_rule
);
1278 ua_event_notifier_rule
->error_counter_index
=
1279 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
1280 /* Acquire the event notifier's reference to the trigger. */
1281 lttng_trigger_get(trigger
);
1283 ua_event_notifier_rule
->trigger
= trigger
;
1284 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1285 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1286 event_rule
, &ua_event_notifier_rule
->exclusion
);
1287 switch (generate_exclusion_status
) {
1288 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1289 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1292 /* Error occured. */
1293 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1294 goto error_put_trigger
;
1297 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1298 ua_event_notifier_rule
->token
);
1300 return ua_event_notifier_rule
;
1303 lttng_trigger_put(trigger
);
1305 free(ua_event_notifier_rule
);
1310 * Alloc new UST app context.
1313 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1315 struct ust_app_ctx
*ua_ctx
;
1317 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1318 if (ua_ctx
== NULL
) {
1322 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1325 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1326 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1327 char *provider_name
= NULL
, *ctx_name
= NULL
;
1329 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1330 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1331 if (!provider_name
|| !ctx_name
) {
1332 free(provider_name
);
1337 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1338 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1342 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1350 * Create a liblttng-ust filter bytecode from given bytecode.
1352 * Return allocated filter or NULL on error.
1354 static struct lttng_ust_abi_filter_bytecode
*create_ust_filter_bytecode_from_bytecode(
1355 const struct lttng_bytecode
*orig_f
)
1357 struct lttng_ust_abi_filter_bytecode
*filter
= NULL
;
1359 /* Copy filter bytecode. */
1360 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1362 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1366 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1367 sizeof(struct lttng_ust_abi_filter_bytecode
));
1368 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1374 * Create a liblttng-ust capture bytecode from given bytecode.
1376 * Return allocated filter or NULL on error.
1378 static struct lttng_ust_abi_capture_bytecode
*
1379 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1381 struct lttng_ust_abi_capture_bytecode
*capture
= NULL
;
1383 /* Copy capture bytecode. */
1384 capture
= zmalloc(sizeof(*capture
) + orig_f
->len
);
1386 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1390 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1391 sizeof(struct lttng_ust_abi_capture_bytecode
));
1392 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1398 * Find an ust_app using the sock and return it. RCU read side lock must be
1399 * held before calling this helper function.
1401 struct ust_app
*ust_app_find_by_sock(int sock
)
1403 struct lttng_ht_node_ulong
*node
;
1404 struct lttng_ht_iter iter
;
1406 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1407 node
= lttng_ht_iter_get_node_ulong(&iter
);
1409 DBG2("UST app find by sock %d not found", sock
);
1413 return caa_container_of(node
, struct ust_app
, sock_n
);
1420 * Find an ust_app using the notify sock and return it. RCU read side lock must
1421 * be held before calling this helper function.
1423 static struct ust_app
*find_app_by_notify_sock(int sock
)
1425 struct lttng_ht_node_ulong
*node
;
1426 struct lttng_ht_iter iter
;
1428 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1430 node
= lttng_ht_iter_get_node_ulong(&iter
);
1432 DBG2("UST app find by notify sock %d not found", sock
);
1436 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1443 * Lookup for an ust app event based on event name, filter bytecode and the
1446 * Return an ust_app_event object or NULL on error.
1448 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1449 const char *name
, const struct lttng_bytecode
*filter
,
1451 const struct lttng_event_exclusion
*exclusion
)
1453 struct lttng_ht_iter iter
;
1454 struct lttng_ht_node_str
*node
;
1455 struct ust_app_event
*event
= NULL
;
1456 struct ust_app_ht_key key
;
1461 /* Setup key for event lookup. */
1463 key
.filter
= filter
;
1464 key
.loglevel_type
= loglevel_value
;
1465 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1466 key
.exclusion
= exclusion
;
1468 /* Lookup using the event name as hash and a custom match fct. */
1469 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1470 ht_match_ust_app_event
, &key
, &iter
.iter
);
1471 node
= lttng_ht_iter_get_node_str(&iter
);
1476 event
= caa_container_of(node
, struct ust_app_event
, node
);
1483 * Look-up an event notifier rule based on its token id.
1485 * Must be called with the RCU read lock held.
1486 * Return an ust_app_event_notifier_rule object or NULL on error.
1488 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(
1489 struct lttng_ht
*ht
, uint64_t token
)
1491 struct lttng_ht_iter iter
;
1492 struct lttng_ht_node_u64
*node
;
1493 struct ust_app_event_notifier_rule
*event_notifier_rule
= NULL
;
1497 lttng_ht_lookup(ht
, &token
, &iter
);
1498 node
= lttng_ht_iter_get_node_u64(&iter
);
1500 DBG2("UST app event notifier rule token not found: token = %" PRIu64
,
1505 event_notifier_rule
= caa_container_of(
1506 node
, struct ust_app_event_notifier_rule
, node
);
1508 return event_notifier_rule
;
1512 * Create the channel context on the tracer.
1514 * Called with UST app session lock held.
1517 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1518 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1522 health_code_update();
1524 pthread_mutex_lock(&app
->sock_lock
);
1525 ret
= lttng_ust_ctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1526 ua_chan
->obj
, &ua_ctx
->obj
);
1527 pthread_mutex_unlock(&app
->sock_lock
);
1529 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1530 ERR("UST app create channel context failed for app (pid: %d) "
1531 "with ret %d", app
->pid
, ret
);
1534 * This is normal behavior, an application can die during the
1535 * creation process. Don't report an error so the execution can
1536 * continue normally.
1539 DBG3("UST app add context failed. Application is dead.");
1544 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1546 DBG2("UST app context handle %d created successfully for channel %s",
1547 ua_ctx
->handle
, ua_chan
->name
);
1550 health_code_update();
1555 * Set the filter on the tracer.
1557 static int set_ust_object_filter(struct ust_app
*app
,
1558 const struct lttng_bytecode
*bytecode
,
1559 struct lttng_ust_abi_object_data
*ust_object
)
1562 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= NULL
;
1564 health_code_update();
1566 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1567 if (!ust_bytecode
) {
1568 ret
= -LTTNG_ERR_NOMEM
;
1571 pthread_mutex_lock(&app
->sock_lock
);
1572 ret
= lttng_ust_ctl_set_filter(app
->sock
, ust_bytecode
,
1574 pthread_mutex_unlock(&app
->sock_lock
);
1576 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1577 ERR("UST app set object filter failed: object = %p of app pid = %d, ret = %d",
1578 ust_object
, app
->pid
, ret
);
1581 * This is normal behavior, an application can die during the
1582 * creation process. Don't report an error so the execution can
1583 * continue normally.
1586 DBG3("Failed to set UST app object filter. Application is dead.");
1591 DBG2("UST filter successfully set: object = %p", ust_object
);
1594 health_code_update();
1600 * Set a capture bytecode for the passed object.
1601 * The sequence number enforces the ordering at runtime and on reception of
1602 * the captured payloads.
1604 static int set_ust_capture(struct ust_app
*app
,
1605 const struct lttng_bytecode
*bytecode
,
1606 unsigned int capture_seqnum
,
1607 struct lttng_ust_abi_object_data
*ust_object
)
1610 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= NULL
;
1612 health_code_update();
1614 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1615 if (!ust_bytecode
) {
1616 ret
= -LTTNG_ERR_NOMEM
;
1621 * Set the sequence number to ensure the capture of fields is ordered.
1623 ust_bytecode
->seqnum
= capture_seqnum
;
1625 pthread_mutex_lock(&app
->sock_lock
);
1626 ret
= lttng_ust_ctl_set_capture(app
->sock
, ust_bytecode
,
1628 pthread_mutex_unlock(&app
->sock_lock
);
1630 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1631 ERR("UST app set object capture failed: object = %p of app pid = %d, ret = %d",
1632 ust_object
, app
->pid
, ret
);
1635 * This is normal behavior, an application can die during the
1636 * creation process. Don't report an error so the execution can
1637 * continue normally.
1640 DBG3("Failed to set UST app object capture. Application is dead.");
1646 DBG2("UST capture successfully set: object = %p", ust_object
);
1649 health_code_update();
1655 struct lttng_ust_abi_event_exclusion
*create_ust_exclusion_from_exclusion(
1656 const struct lttng_event_exclusion
*exclusion
)
1658 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= NULL
;
1659 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1660 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1662 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1663 if (!ust_exclusion
) {
1668 LTTNG_ASSERT(sizeof(struct lttng_event_exclusion
) ==
1669 sizeof(struct lttng_ust_abi_event_exclusion
));
1670 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1672 return ust_exclusion
;
1676 * Set event exclusions on the tracer.
1678 static int set_ust_object_exclusions(struct ust_app
*app
,
1679 const struct lttng_event_exclusion
*exclusions
,
1680 struct lttng_ust_abi_object_data
*ust_object
)
1683 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= NULL
;
1685 LTTNG_ASSERT(exclusions
&& exclusions
->count
> 0);
1687 health_code_update();
1689 ust_exclusions
= create_ust_exclusion_from_exclusion(
1691 if (!ust_exclusions
) {
1692 ret
= -LTTNG_ERR_NOMEM
;
1695 pthread_mutex_lock(&app
->sock_lock
);
1696 ret
= lttng_ust_ctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1697 pthread_mutex_unlock(&app
->sock_lock
);
1699 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1700 ERR("Failed to set UST app exclusions for object %p of app (pid: %d) "
1701 "with ret %d", ust_object
, app
->pid
, ret
);
1704 * This is normal behavior, an application can die during the
1705 * creation process. Don't report an error so the execution can
1706 * continue normally.
1709 DBG3("Failed to set UST app object exclusions. Application is dead.");
1714 DBG2("UST exclusions set successfully for object %p", ust_object
);
1717 health_code_update();
1718 free(ust_exclusions
);
1723 * Disable the specified event on to UST tracer for the UST session.
1725 static int disable_ust_object(struct ust_app
*app
,
1726 struct lttng_ust_abi_object_data
*object
)
1730 health_code_update();
1732 pthread_mutex_lock(&app
->sock_lock
);
1733 ret
= lttng_ust_ctl_disable(app
->sock
, object
);
1734 pthread_mutex_unlock(&app
->sock_lock
);
1736 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1737 ERR("Failed to disable UST app object %p app (pid: %d) with ret %d",
1738 object
, app
->pid
, ret
);
1741 * This is normal behavior, an application can die during the
1742 * creation process. Don't report an error so the execution can
1743 * continue normally.
1746 DBG3("Failed to disable UST app object. Application is dead.");
1751 DBG2("UST app object %p disabled successfully for app (pid: %d)",
1755 health_code_update();
1760 * Disable the specified channel on to UST tracer for the UST session.
1762 static int disable_ust_channel(struct ust_app
*app
,
1763 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1767 health_code_update();
1769 pthread_mutex_lock(&app
->sock_lock
);
1770 ret
= lttng_ust_ctl_disable(app
->sock
, ua_chan
->obj
);
1771 pthread_mutex_unlock(&app
->sock_lock
);
1773 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1774 ERR("UST app channel %s disable failed for app (pid: %d) "
1775 "and session handle %d with ret %d",
1776 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1779 * This is normal behavior, an application can die during the
1780 * creation process. Don't report an error so the execution can
1781 * continue normally.
1784 DBG3("UST app disable channel failed. Application is dead.");
1789 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1790 ua_chan
->name
, app
->pid
);
1793 health_code_update();
1798 * Enable the specified channel on to UST tracer for the UST session.
1800 static int enable_ust_channel(struct ust_app
*app
,
1801 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1805 health_code_update();
1807 pthread_mutex_lock(&app
->sock_lock
);
1808 ret
= lttng_ust_ctl_enable(app
->sock
, ua_chan
->obj
);
1809 pthread_mutex_unlock(&app
->sock_lock
);
1811 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1812 ERR("UST app channel %s enable failed for app (pid: %d) "
1813 "and session handle %d with ret %d",
1814 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1817 * This is normal behavior, an application can die during the
1818 * creation process. Don't report an error so the execution can
1819 * continue normally.
1822 DBG3("UST app enable channel failed. Application is dead.");
1827 ua_chan
->enabled
= 1;
1829 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1830 ua_chan
->name
, app
->pid
);
1833 health_code_update();
1838 * Enable the specified event on to UST tracer for the UST session.
1840 static int enable_ust_object(
1841 struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1845 health_code_update();
1847 pthread_mutex_lock(&app
->sock_lock
);
1848 ret
= lttng_ust_ctl_enable(app
->sock
, ust_object
);
1849 pthread_mutex_unlock(&app
->sock_lock
);
1851 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1852 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
1853 ust_object
, app
->pid
, ret
);
1856 * This is normal behavior, an application can die during the
1857 * creation process. Don't report an error so the execution can
1858 * continue normally.
1861 DBG3("Failed to enable UST app object. Application is dead.");
1866 DBG2("UST app object %p enabled successfully for app (pid: %d)",
1867 ust_object
, app
->pid
);
1870 health_code_update();
1875 * Send channel and stream buffer to application.
1877 * Return 0 on success. On error, a negative value is returned.
1879 static int send_channel_pid_to_ust(struct ust_app
*app
,
1880 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1883 struct ust_app_stream
*stream
, *stmp
;
1886 LTTNG_ASSERT(ua_sess
);
1887 LTTNG_ASSERT(ua_chan
);
1889 health_code_update();
1891 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1894 /* Send channel to the application. */
1895 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1896 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1897 ret
= -ENOTCONN
; /* Caused by app exiting. */
1899 } else if (ret
< 0) {
1903 health_code_update();
1905 /* Send all streams to application. */
1906 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1907 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1908 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1909 ret
= -ENOTCONN
; /* Caused by app exiting. */
1911 } else if (ret
< 0) {
1914 /* We don't need the stream anymore once sent to the tracer. */
1915 cds_list_del(&stream
->list
);
1916 delete_ust_app_stream(-1, stream
, app
);
1918 /* Flag the channel that it is sent to the application. */
1919 ua_chan
->is_sent
= 1;
1922 health_code_update();
1927 * Create the specified event onto the UST tracer for a UST session.
1929 * Should be called with session mutex held.
1932 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1933 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1937 health_code_update();
1939 /* Create UST event on tracer */
1940 pthread_mutex_lock(&app
->sock_lock
);
1941 ret
= lttng_ust_ctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1943 pthread_mutex_unlock(&app
->sock_lock
);
1945 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1947 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1948 ua_event
->attr
.name
, app
->pid
, ret
);
1951 * This is normal behavior, an application can die during the
1952 * creation process. Don't report an error so the execution can
1953 * continue normally.
1956 DBG3("UST app create event failed. Application is dead.");
1961 ua_event
->handle
= ua_event
->obj
->handle
;
1963 DBG2("UST app event %s created successfully for pid:%d object: %p",
1964 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
1966 health_code_update();
1968 /* Set filter if one is present. */
1969 if (ua_event
->filter
) {
1970 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
1976 /* Set exclusions for the event */
1977 if (ua_event
->exclusion
) {
1978 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
1984 /* If event not enabled, disable it on the tracer */
1985 if (ua_event
->enabled
) {
1987 * We now need to explicitly enable the event, since it
1988 * is now disabled at creation.
1990 ret
= enable_ust_object(app
, ua_event
->obj
);
1993 * If we hit an EPERM, something is wrong with our enable call. If
1994 * we get an EEXIST, there is a problem on the tracer side since we
1998 case -LTTNG_UST_ERR_PERM
:
1999 /* Code flow problem */
2001 case -LTTNG_UST_ERR_EXIST
:
2002 /* It's OK for our use case. */
2013 health_code_update();
2017 static int init_ust_event_notifier_from_event_rule(
2018 const struct lttng_event_rule
*rule
,
2019 struct lttng_ust_abi_event_notifier
*event_notifier
)
2021 enum lttng_event_rule_status status
;
2022 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2023 int loglevel
= -1, ret
= 0;
2024 const char *pattern
;
2027 memset(event_notifier
, 0, sizeof(*event_notifier
));
2029 if (lttng_event_rule_targets_agent_domain(rule
)) {
2031 * Special event for agents
2032 * The actual meat of the event is in the filter that will be
2033 * attached later on.
2034 * Set the default values for the agent event.
2036 pattern
= event_get_default_agent_ust_name(
2037 lttng_event_rule_get_domain_type(rule
));
2039 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2041 const struct lttng_log_level_rule
*log_level_rule
;
2043 LTTNG_ASSERT(lttng_event_rule_get_type(rule
) ==
2044 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
);
2046 status
= lttng_event_rule_user_tracepoint_get_name_pattern(rule
, &pattern
);
2047 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2048 /* At this point, this is a fatal error. */
2052 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(
2053 rule
, &log_level_rule
);
2054 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2055 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2056 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2057 enum lttng_log_level_rule_status llr_status
;
2059 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2060 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2061 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2062 llr_status
= lttng_log_level_rule_exactly_get_level(
2063 log_level_rule
, &loglevel
);
2065 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2066 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2067 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2068 log_level_rule
, &loglevel
);
2074 LTTNG_ASSERT(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2076 /* At this point this is a fatal error. */
2081 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2082 ret
= lttng_strncpy(event_notifier
->event
.name
, pattern
,
2083 LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
2085 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
2090 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2091 event_notifier
->event
.loglevel
= loglevel
;
2097 * Create the specified event notifier against the user space tracer of a
2098 * given application.
2100 static int create_ust_event_notifier(struct ust_app
*app
,
2101 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2104 enum lttng_condition_status condition_status
;
2105 const struct lttng_condition
*condition
= NULL
;
2106 struct lttng_ust_abi_event_notifier event_notifier
;
2107 const struct lttng_event_rule
*event_rule
= NULL
;
2108 unsigned int capture_bytecode_count
= 0, i
;
2109 enum lttng_condition_status cond_status
;
2110 enum lttng_event_rule_type event_rule_type
;
2112 health_code_update();
2113 LTTNG_ASSERT(app
->event_notifier_group
.object
);
2115 condition
= lttng_trigger_get_const_condition(
2116 ua_event_notifier_rule
->trigger
);
2117 LTTNG_ASSERT(condition
);
2118 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
2119 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2121 condition_status
= lttng_condition_event_rule_matches_get_rule(
2122 condition
, &event_rule
);
2123 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2125 LTTNG_ASSERT(event_rule
);
2127 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2128 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
||
2129 event_rule_type
== LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
||
2131 LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
||
2133 LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
);
2135 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2136 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2137 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2139 /* Create UST event notifier against the tracer. */
2140 pthread_mutex_lock(&app
->sock_lock
);
2141 ret
= lttng_ust_ctl_create_event_notifier(app
->sock
, &event_notifier
,
2142 app
->event_notifier_group
.object
,
2143 &ua_event_notifier_rule
->obj
);
2144 pthread_mutex_unlock(&app
->sock_lock
);
2146 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2147 ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d",
2148 event_notifier
.event
.name
, app
->name
,
2152 * This is normal behavior, an application can die
2153 * during the creation process. Don't report an error so
2154 * the execution can continue normally.
2157 DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)",
2158 app
->name
, app
->ppid
);
2164 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2166 DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p",
2167 event_notifier
.event
.name
, app
->name
, app
->ppid
,
2168 ua_event_notifier_rule
->obj
);
2170 health_code_update();
2172 /* Set filter if one is present. */
2173 if (ua_event_notifier_rule
->filter
) {
2174 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2175 ua_event_notifier_rule
->obj
);
2181 /* Set exclusions for the event. */
2182 if (ua_event_notifier_rule
->exclusion
) {
2183 ret
= set_ust_object_exclusions(app
,
2184 ua_event_notifier_rule
->exclusion
,
2185 ua_event_notifier_rule
->obj
);
2191 /* Set the capture bytecodes. */
2192 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2193 condition
, &capture_bytecode_count
);
2194 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2196 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2197 const struct lttng_bytecode
*capture_bytecode
=
2198 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
2201 ret
= set_ust_capture(app
, capture_bytecode
, i
,
2202 ua_event_notifier_rule
->obj
);
2209 * We now need to explicitly enable the event, since it
2210 * is disabled at creation.
2212 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2215 * If we hit an EPERM, something is wrong with our enable call.
2216 * If we get an EEXIST, there is a problem on the tracer side
2217 * since we just created it.
2220 case -LTTNG_UST_ERR_PERM
:
2221 /* Code flow problem. */
2223 case -LTTNG_UST_ERR_EXIST
:
2224 /* It's OK for our use case. */
2234 ua_event_notifier_rule
->enabled
= true;
2237 health_code_update();
2242 * Copy data between an UST app event and a LTT event.
2244 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2245 struct ltt_ust_event
*uevent
)
2247 size_t exclusion_alloc_size
;
2249 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2250 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2252 ua_event
->enabled
= uevent
->enabled
;
2254 /* Copy event attributes */
2255 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2257 /* Copy filter bytecode */
2258 if (uevent
->filter
) {
2259 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2260 /* Filter might be NULL here in case of ENONEM. */
2263 /* Copy exclusion data */
2264 if (uevent
->exclusion
) {
2265 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2266 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2267 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
2268 if (ua_event
->exclusion
== NULL
) {
2271 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2272 exclusion_alloc_size
);
2278 * Copy data between an UST app channel and a LTT channel.
2280 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2281 struct ltt_ust_channel
*uchan
)
2283 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2285 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2286 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2288 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2289 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2291 /* Copy event attributes since the layout is different. */
2292 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2293 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2294 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2295 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2296 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2297 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2298 ua_chan
->attr
.output
= uchan
->attr
.output
;
2299 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2302 * Note that the attribute channel type is not set since the channel on the
2303 * tracing registry side does not have this information.
2306 ua_chan
->enabled
= uchan
->enabled
;
2307 ua_chan
->tracing_channel_id
= uchan
->id
;
2309 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2313 * Copy data between a UST app session and a regular LTT session.
2315 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2316 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2318 struct tm
*timeinfo
;
2321 char tmp_shm_path
[PATH_MAX
];
2323 timeinfo
= localtime(&app
->registration_time
);
2324 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2326 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2328 ua_sess
->tracing_id
= usess
->id
;
2329 ua_sess
->id
= get_next_session_id();
2330 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2331 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2332 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2333 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2334 ua_sess
->buffer_type
= usess
->buffer_type
;
2335 ua_sess
->bits_per_long
= app
->bits_per_long
;
2337 /* There is only one consumer object per session possible. */
2338 consumer_output_get(usess
->consumer
);
2339 ua_sess
->consumer
= usess
->consumer
;
2341 ua_sess
->output_traces
= usess
->output_traces
;
2342 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2343 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2344 &usess
->metadata_attr
);
2346 switch (ua_sess
->buffer_type
) {
2347 case LTTNG_BUFFER_PER_PID
:
2348 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2349 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2352 case LTTNG_BUFFER_PER_UID
:
2353 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2354 DEFAULT_UST_TRACE_UID_PATH
,
2355 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2356 app
->bits_per_long
);
2363 PERROR("asprintf UST shadow copy session");
2368 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2369 sizeof(ua_sess
->root_shm_path
));
2370 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2371 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2372 sizeof(ua_sess
->shm_path
));
2373 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2374 if (ua_sess
->shm_path
[0]) {
2375 switch (ua_sess
->buffer_type
) {
2376 case LTTNG_BUFFER_PER_PID
:
2377 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2378 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2379 app
->name
, app
->pid
, datetime
);
2381 case LTTNG_BUFFER_PER_UID
:
2382 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2383 "/" DEFAULT_UST_TRACE_UID_PATH
,
2384 app
->uid
, app
->bits_per_long
);
2391 PERROR("sprintf UST shadow copy session");
2395 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2396 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2397 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2402 consumer_output_put(ua_sess
->consumer
);
2406 * Lookup sesison wrapper.
2409 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2410 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2412 /* Get right UST app session from app */
2413 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2417 * Return ust app session from the app session hashtable using the UST session
2420 static struct ust_app_session
*lookup_session_by_app(
2421 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2423 struct lttng_ht_iter iter
;
2424 struct lttng_ht_node_u64
*node
;
2426 __lookup_session_by_app(usess
, app
, &iter
);
2427 node
= lttng_ht_iter_get_node_u64(&iter
);
2432 return caa_container_of(node
, struct ust_app_session
, node
);
2439 * Setup buffer registry per PID for the given session and application. If none
2440 * is found, a new one is created, added to the global registry and
2441 * initialized. If regp is valid, it's set with the newly created object.
2443 * Return 0 on success or else a negative value.
2445 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2446 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2449 struct buffer_reg_pid
*reg_pid
;
2451 LTTNG_ASSERT(ua_sess
);
2456 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2459 * This is the create channel path meaning that if there is NO
2460 * registry available, we have to create one for this session.
2462 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2463 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2471 /* Initialize registry. */
2472 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2473 app
->bits_per_long
, app
->uint8_t_alignment
,
2474 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2475 app
->uint64_t_alignment
, app
->long_alignment
,
2476 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2477 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2478 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2479 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2480 ua_sess
->tracing_id
,
2484 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2485 * destroy the buffer registry, because it is always expected
2486 * that if the buffer registry can be found, its ust registry is
2489 buffer_reg_pid_destroy(reg_pid
);
2493 buffer_reg_pid_add(reg_pid
);
2495 DBG3("UST app buffer registry per PID created successfully");
2507 * Setup buffer registry per UID for the given session and application. If none
2508 * is found, a new one is created, added to the global registry and
2509 * initialized. If regp is valid, it's set with the newly created object.
2511 * Return 0 on success or else a negative value.
2513 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2514 struct ust_app_session
*ua_sess
,
2515 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2518 struct buffer_reg_uid
*reg_uid
;
2520 LTTNG_ASSERT(usess
);
2525 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2528 * This is the create channel path meaning that if there is NO
2529 * registry available, we have to create one for this session.
2531 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2532 LTTNG_DOMAIN_UST
, ®_uid
,
2533 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2541 /* Initialize registry. */
2542 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2543 app
->bits_per_long
, app
->uint8_t_alignment
,
2544 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2545 app
->uint64_t_alignment
, app
->long_alignment
,
2546 app
->byte_order
, app
->version
.major
,
2547 app
->version
.minor
, reg_uid
->root_shm_path
,
2548 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2549 ua_sess
->tracing_id
, app
->uid
);
2552 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2553 * destroy the buffer registry, because it is always expected
2554 * that if the buffer registry can be found, its ust registry is
2557 buffer_reg_uid_destroy(reg_uid
, NULL
);
2560 /* Add node to teardown list of the session. */
2561 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2563 buffer_reg_uid_add(reg_uid
);
2565 DBG3("UST app buffer registry per UID created successfully");
2576 * Create a session on the tracer side for the given app.
2578 * On success, ua_sess_ptr is populated with the session pointer or else left
2579 * untouched. If the session was created, is_created is set to 1. On error,
2580 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2583 * Returns 0 on success or else a negative code which is either -ENOMEM or
2584 * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails.
2586 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2587 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2590 int ret
, created
= 0;
2591 struct ust_app_session
*ua_sess
;
2593 LTTNG_ASSERT(usess
);
2595 LTTNG_ASSERT(ua_sess_ptr
);
2597 health_code_update();
2599 ua_sess
= lookup_session_by_app(usess
, app
);
2600 if (ua_sess
== NULL
) {
2601 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2602 app
->pid
, usess
->id
);
2603 ua_sess
= alloc_ust_app_session();
2604 if (ua_sess
== NULL
) {
2605 /* Only malloc can failed so something is really wrong */
2609 shadow_copy_session(ua_sess
, usess
, app
);
2613 switch (usess
->buffer_type
) {
2614 case LTTNG_BUFFER_PER_PID
:
2615 /* Init local registry. */
2616 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2618 delete_ust_app_session(-1, ua_sess
, app
);
2622 case LTTNG_BUFFER_PER_UID
:
2623 /* Look for a global registry. If none exists, create one. */
2624 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2626 delete_ust_app_session(-1, ua_sess
, app
);
2636 health_code_update();
2638 if (ua_sess
->handle
== -1) {
2639 pthread_mutex_lock(&app
->sock_lock
);
2640 ret
= lttng_ust_ctl_create_session(app
->sock
);
2641 pthread_mutex_unlock(&app
->sock_lock
);
2643 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2644 ERR("Creating session for app pid %d with ret %d",
2647 DBG("UST app creating session failed. Application is dead");
2649 * This is normal behavior, an application can die during the
2650 * creation process. Don't report an error so the execution can
2651 * continue normally. This will get flagged ENOTCONN and the
2652 * caller will handle it.
2656 delete_ust_app_session(-1, ua_sess
, app
);
2657 if (ret
!= -ENOMEM
) {
2659 * Tracer is probably gone or got an internal error so let's
2660 * behave like it will soon unregister or not usable.
2667 ua_sess
->handle
= ret
;
2669 /* Add ust app session to app's HT */
2670 lttng_ht_node_init_u64(&ua_sess
->node
,
2671 ua_sess
->tracing_id
);
2672 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2673 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2674 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2675 &ua_sess
->ust_objd_node
);
2677 DBG2("UST app session created successfully with handle %d", ret
);
2680 *ua_sess_ptr
= ua_sess
;
2682 *is_created
= created
;
2685 /* Everything went well. */
2689 health_code_update();
2694 * Match function for a hash table lookup of ust_app_ctx.
2696 * It matches an ust app context based on the context type and, in the case
2697 * of perf counters, their name.
2699 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2701 struct ust_app_ctx
*ctx
;
2702 const struct lttng_ust_context_attr
*key
;
2707 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2711 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2716 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2717 if (strncmp(key
->u
.perf_counter
.name
,
2718 ctx
->ctx
.u
.perf_counter
.name
,
2719 sizeof(key
->u
.perf_counter
.name
))) {
2723 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2724 if (strcmp(key
->u
.app_ctx
.provider_name
,
2725 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2726 strcmp(key
->u
.app_ctx
.ctx_name
,
2727 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2743 * Lookup for an ust app context from an lttng_ust_context.
2745 * Must be called while holding RCU read side lock.
2746 * Return an ust_app_ctx object or NULL on error.
2749 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2750 struct lttng_ust_context_attr
*uctx
)
2752 struct lttng_ht_iter iter
;
2753 struct lttng_ht_node_ulong
*node
;
2754 struct ust_app_ctx
*app_ctx
= NULL
;
2759 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2760 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2761 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2762 node
= lttng_ht_iter_get_node_ulong(&iter
);
2767 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2774 * Create a context for the channel on the tracer.
2776 * Called with UST app session lock held and a RCU read side lock.
2779 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2780 struct lttng_ust_context_attr
*uctx
,
2781 struct ust_app
*app
)
2784 struct ust_app_ctx
*ua_ctx
;
2786 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2788 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2794 ua_ctx
= alloc_ust_app_ctx(uctx
);
2795 if (ua_ctx
== NULL
) {
2801 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2802 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2803 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2805 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2815 * Enable on the tracer side a ust app event for the session and channel.
2817 * Called with UST app session lock held.
2820 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2821 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2825 ret
= enable_ust_object(app
, ua_event
->obj
);
2830 ua_event
->enabled
= 1;
2837 * Disable on the tracer side a ust app event for the session and channel.
2839 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2840 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2844 ret
= disable_ust_object(app
, ua_event
->obj
);
2849 ua_event
->enabled
= 0;
2856 * Lookup ust app channel for session and disable it on the tracer side.
2859 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2860 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2864 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2869 ua_chan
->enabled
= 0;
2876 * Lookup ust app channel for session and enable it on the tracer side. This
2877 * MUST be called with a RCU read side lock acquired.
2879 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2880 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2883 struct lttng_ht_iter iter
;
2884 struct lttng_ht_node_str
*ua_chan_node
;
2885 struct ust_app_channel
*ua_chan
;
2887 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2888 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2889 if (ua_chan_node
== NULL
) {
2890 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2891 uchan
->name
, ua_sess
->tracing_id
);
2895 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2897 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2907 * Ask the consumer to create a channel and get it if successful.
2909 * Called with UST app session lock held.
2911 * Return 0 on success or else a negative value.
2913 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2914 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2915 int bitness
, struct ust_registry_session
*registry
,
2916 uint64_t trace_archive_id
)
2919 unsigned int nb_fd
= 0;
2920 struct consumer_socket
*socket
;
2922 LTTNG_ASSERT(usess
);
2923 LTTNG_ASSERT(ua_sess
);
2924 LTTNG_ASSERT(ua_chan
);
2925 LTTNG_ASSERT(registry
);
2928 health_code_update();
2930 /* Get the right consumer socket for the application. */
2931 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2937 health_code_update();
2939 /* Need one fd for the channel. */
2940 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2942 ERR("Exhausted number of available FD upon create channel");
2947 * Ask consumer to create channel. The consumer will return the number of
2948 * stream we have to expect.
2950 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2951 registry
, usess
->current_trace_chunk
);
2957 * Compute the number of fd needed before receiving them. It must be 2 per
2958 * stream (2 being the default value here).
2960 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2962 /* Reserve the amount of file descriptor we need. */
2963 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2965 ERR("Exhausted number of available FD upon create channel");
2966 goto error_fd_get_stream
;
2969 health_code_update();
2972 * Now get the channel from the consumer. This call will populate the stream
2973 * list of that channel and set the ust objects.
2975 if (usess
->consumer
->enabled
) {
2976 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2986 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2987 error_fd_get_stream
:
2989 * Initiate a destroy channel on the consumer since we had an error
2990 * handling it on our side. The return value is of no importance since we
2991 * already have a ret value set by the previous error that we need to
2994 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2996 lttng_fd_put(LTTNG_FD_APPS
, 1);
2998 health_code_update();
3004 * Duplicate the ust data object of the ust app stream and save it in the
3005 * buffer registry stream.
3007 * Return 0 on success or else a negative value.
3009 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
3010 struct ust_app_stream
*stream
)
3014 LTTNG_ASSERT(reg_stream
);
3015 LTTNG_ASSERT(stream
);
3017 /* Duplicating a stream requires 2 new fds. Reserve them. */
3018 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3020 ERR("Exhausted number of available FD upon duplicate stream");
3024 /* Duplicate object for stream once the original is in the registry. */
3025 ret
= lttng_ust_ctl_duplicate_ust_object_data(&stream
->obj
,
3026 reg_stream
->obj
.ust
);
3028 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3029 reg_stream
->obj
.ust
, stream
->obj
, ret
);
3030 lttng_fd_put(LTTNG_FD_APPS
, 2);
3033 stream
->handle
= stream
->obj
->handle
;
3040 * Duplicate the ust data object of the ust app. channel and save it in the
3041 * buffer registry channel.
3043 * Return 0 on success or else a negative value.
3045 static int duplicate_channel_object(struct buffer_reg_channel
*buf_reg_chan
,
3046 struct ust_app_channel
*ua_chan
)
3050 LTTNG_ASSERT(buf_reg_chan
);
3051 LTTNG_ASSERT(ua_chan
);
3053 /* Duplicating a channel requires 1 new fd. Reserve it. */
3054 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3056 ERR("Exhausted number of available FD upon duplicate channel");
3060 /* Duplicate object for stream once the original is in the registry. */
3061 ret
= lttng_ust_ctl_duplicate_ust_object_data(&ua_chan
->obj
, buf_reg_chan
->obj
.ust
);
3063 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3064 buf_reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
3067 ua_chan
->handle
= ua_chan
->obj
->handle
;
3072 lttng_fd_put(LTTNG_FD_APPS
, 1);
3078 * For a given channel buffer registry, setup all streams of the given ust
3079 * application channel.
3081 * Return 0 on success or else a negative value.
3083 static int setup_buffer_reg_streams(struct buffer_reg_channel
*buf_reg_chan
,
3084 struct ust_app_channel
*ua_chan
,
3085 struct ust_app
*app
)
3088 struct ust_app_stream
*stream
, *stmp
;
3090 LTTNG_ASSERT(buf_reg_chan
);
3091 LTTNG_ASSERT(ua_chan
);
3093 DBG2("UST app setup buffer registry stream");
3095 /* Send all streams to application. */
3096 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3097 struct buffer_reg_stream
*reg_stream
;
3099 ret
= buffer_reg_stream_create(®_stream
);
3105 * Keep original pointer and nullify it in the stream so the delete
3106 * stream call does not release the object.
3108 reg_stream
->obj
.ust
= stream
->obj
;
3110 buffer_reg_stream_add(reg_stream
, buf_reg_chan
);
3112 /* We don't need the streams anymore. */
3113 cds_list_del(&stream
->list
);
3114 delete_ust_app_stream(-1, stream
, app
);
3122 * Create a buffer registry channel for the given session registry and
3123 * application channel object. If regp pointer is valid, it's set with the
3124 * created object. Important, the created object is NOT added to the session
3125 * registry hash table.
3127 * Return 0 on success else a negative value.
3129 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3130 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
3133 struct buffer_reg_channel
*buf_reg_chan
= NULL
;
3135 LTTNG_ASSERT(reg_sess
);
3136 LTTNG_ASSERT(ua_chan
);
3138 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3140 /* Create buffer registry channel. */
3141 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, &buf_reg_chan
);
3145 LTTNG_ASSERT(buf_reg_chan
);
3146 buf_reg_chan
->consumer_key
= ua_chan
->key
;
3147 buf_reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3148 buf_reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3150 /* Create and add a channel registry to session. */
3151 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
3152 ua_chan
->tracing_channel_id
);
3156 buffer_reg_channel_add(reg_sess
, buf_reg_chan
);
3159 *regp
= buf_reg_chan
;
3165 /* Safe because the registry channel object was not added to any HT. */
3166 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3172 * Setup buffer registry channel for the given session registry and application
3173 * channel object. If regp pointer is valid, it's set with the created object.
3175 * Return 0 on success else a negative value.
3177 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3178 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*buf_reg_chan
,
3179 struct ust_app
*app
)
3183 LTTNG_ASSERT(reg_sess
);
3184 LTTNG_ASSERT(buf_reg_chan
);
3185 LTTNG_ASSERT(ua_chan
);
3186 LTTNG_ASSERT(ua_chan
->obj
);
3188 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3190 /* Setup all streams for the registry. */
3191 ret
= setup_buffer_reg_streams(buf_reg_chan
, ua_chan
, app
);
3196 buf_reg_chan
->obj
.ust
= ua_chan
->obj
;
3197 ua_chan
->obj
= NULL
;
3202 buffer_reg_channel_remove(reg_sess
, buf_reg_chan
);
3203 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3208 * Send buffer registry channel to the application.
3210 * Return 0 on success else a negative value.
3212 static int send_channel_uid_to_ust(struct buffer_reg_channel
*buf_reg_chan
,
3213 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
3214 struct ust_app_channel
*ua_chan
)
3217 struct buffer_reg_stream
*reg_stream
;
3219 LTTNG_ASSERT(buf_reg_chan
);
3221 LTTNG_ASSERT(ua_sess
);
3222 LTTNG_ASSERT(ua_chan
);
3224 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3226 ret
= duplicate_channel_object(buf_reg_chan
, ua_chan
);
3231 /* Send channel to the application. */
3232 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3233 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3234 ret
= -ENOTCONN
; /* Caused by app exiting. */
3236 } else if (ret
< 0) {
3240 health_code_update();
3242 /* Send all streams to application. */
3243 pthread_mutex_lock(&buf_reg_chan
->stream_list_lock
);
3244 cds_list_for_each_entry(reg_stream
, &buf_reg_chan
->streams
, lnode
) {
3245 struct ust_app_stream stream
;
3247 ret
= duplicate_stream_object(reg_stream
, &stream
);
3249 goto error_stream_unlock
;
3252 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3254 (void) release_ust_app_stream(-1, &stream
, app
);
3255 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3256 ret
= -ENOTCONN
; /* Caused by app exiting. */
3258 goto error_stream_unlock
;
3262 * The return value is not important here. This function will output an
3265 (void) release_ust_app_stream(-1, &stream
, app
);
3267 ua_chan
->is_sent
= 1;
3269 error_stream_unlock
:
3270 pthread_mutex_unlock(&buf_reg_chan
->stream_list_lock
);
3276 * Create and send to the application the created buffers with per UID buffers.
3278 * This MUST be called with a RCU read side lock acquired.
3279 * The session list lock and the session's lock must be acquired.
3281 * Return 0 on success else a negative value.
3283 static int create_channel_per_uid(struct ust_app
*app
,
3284 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3285 struct ust_app_channel
*ua_chan
)
3288 struct buffer_reg_uid
*reg_uid
;
3289 struct buffer_reg_channel
*buf_reg_chan
;
3290 struct ltt_session
*session
= NULL
;
3291 enum lttng_error_code notification_ret
;
3292 struct ust_registry_channel
*ust_reg_chan
;
3295 LTTNG_ASSERT(usess
);
3296 LTTNG_ASSERT(ua_sess
);
3297 LTTNG_ASSERT(ua_chan
);
3299 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3301 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3303 * The session creation handles the creation of this global registry
3304 * object. If none can be find, there is a code flow problem or a
3307 LTTNG_ASSERT(reg_uid
);
3309 buf_reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3315 /* Create the buffer registry channel object. */
3316 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, &buf_reg_chan
);
3318 ERR("Error creating the UST channel \"%s\" registry instance",
3323 session
= session_find_by_id(ua_sess
->tracing_id
);
3324 LTTNG_ASSERT(session
);
3325 LTTNG_ASSERT(pthread_mutex_trylock(&session
->lock
));
3326 LTTNG_ASSERT(session_trylock_list());
3329 * Create the buffers on the consumer side. This call populates the
3330 * ust app channel object with all streams and data object.
3332 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3333 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
3334 session
->most_recent_chunk_id
.value
);
3336 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3340 * Let's remove the previously created buffer registry channel so
3341 * it's not visible anymore in the session registry.
3343 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3344 ua_chan
->tracing_channel_id
, false);
3345 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
3346 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3351 * Setup the streams and add it to the session registry.
3353 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3354 ua_chan
, buf_reg_chan
, app
);
3356 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3360 /* Notify the notification subsystem of the channel's creation. */
3361 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
3362 ust_reg_chan
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3363 ua_chan
->tracing_channel_id
);
3364 LTTNG_ASSERT(ust_reg_chan
);
3365 ust_reg_chan
->consumer_key
= ua_chan
->key
;
3366 ust_reg_chan
= NULL
;
3367 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
3369 notification_ret
= notification_thread_command_add_channel(
3370 the_notification_thread_handle
, session
->name
,
3371 lttng_credentials_get_uid(
3372 &ua_sess
->effective_credentials
),
3373 lttng_credentials_get_gid(
3374 &ua_sess
->effective_credentials
),
3375 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3376 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3377 if (notification_ret
!= LTTNG_OK
) {
3378 ret
= - (int) notification_ret
;
3379 ERR("Failed to add channel to notification thread");
3384 /* Send buffers to the application. */
3385 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3387 if (ret
!= -ENOTCONN
) {
3388 ERR("Error sending channel to application");
3395 session_put(session
);
3401 * Create and send to the application the created buffers with per PID buffers.
3403 * Called with UST app session lock held.
3404 * The session list lock and the session's lock must be acquired.
3406 * Return 0 on success else a negative value.
3408 static int create_channel_per_pid(struct ust_app
*app
,
3409 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3410 struct ust_app_channel
*ua_chan
)
3413 struct ust_registry_session
*registry
;
3414 enum lttng_error_code cmd_ret
;
3415 struct ltt_session
*session
= NULL
;
3416 uint64_t chan_reg_key
;
3417 struct ust_registry_channel
*ust_reg_chan
;
3420 LTTNG_ASSERT(usess
);
3421 LTTNG_ASSERT(ua_sess
);
3422 LTTNG_ASSERT(ua_chan
);
3424 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3428 registry
= get_session_registry(ua_sess
);
3429 /* The UST app session lock is held, registry shall not be null. */
3430 LTTNG_ASSERT(registry
);
3432 /* Create and add a new channel registry to session. */
3433 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3435 ERR("Error creating the UST channel \"%s\" registry instance",
3440 session
= session_find_by_id(ua_sess
->tracing_id
);
3441 LTTNG_ASSERT(session
);
3443 LTTNG_ASSERT(pthread_mutex_trylock(&session
->lock
));
3444 LTTNG_ASSERT(session_trylock_list());
3446 /* Create and get channel on the consumer side. */
3447 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3448 app
->bits_per_long
, registry
,
3449 session
->most_recent_chunk_id
.value
);
3451 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3453 goto error_remove_from_registry
;
3456 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3458 if (ret
!= -ENOTCONN
) {
3459 ERR("Error sending channel to application");
3461 goto error_remove_from_registry
;
3464 chan_reg_key
= ua_chan
->key
;
3465 pthread_mutex_lock(®istry
->lock
);
3466 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
3467 LTTNG_ASSERT(ust_reg_chan
);
3468 ust_reg_chan
->consumer_key
= ua_chan
->key
;
3469 pthread_mutex_unlock(®istry
->lock
);
3471 cmd_ret
= notification_thread_command_add_channel(
3472 the_notification_thread_handle
, session
->name
,
3473 lttng_credentials_get_uid(
3474 &ua_sess
->effective_credentials
),
3475 lttng_credentials_get_gid(
3476 &ua_sess
->effective_credentials
),
3477 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3478 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3479 if (cmd_ret
!= LTTNG_OK
) {
3480 ret
= - (int) cmd_ret
;
3481 ERR("Failed to add channel to notification thread");
3482 goto error_remove_from_registry
;
3485 error_remove_from_registry
:
3487 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3492 session_put(session
);
3498 * From an already allocated ust app channel, create the channel buffers if
3499 * needed and send them to the application. This MUST be called with a RCU read
3500 * side lock acquired.
3502 * Called with UST app session lock held.
3504 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3505 * the application exited concurrently.
3507 static int ust_app_channel_send(struct ust_app
*app
,
3508 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3509 struct ust_app_channel
*ua_chan
)
3514 LTTNG_ASSERT(usess
);
3515 LTTNG_ASSERT(usess
->active
);
3516 LTTNG_ASSERT(ua_sess
);
3517 LTTNG_ASSERT(ua_chan
);
3519 /* Handle buffer type before sending the channel to the application. */
3520 switch (usess
->buffer_type
) {
3521 case LTTNG_BUFFER_PER_UID
:
3523 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3529 case LTTNG_BUFFER_PER_PID
:
3531 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3543 /* Initialize ust objd object using the received handle and add it. */
3544 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3545 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3547 /* If channel is not enabled, disable it on the tracer */
3548 if (!ua_chan
->enabled
) {
3549 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3560 * Create UST app channel and return it through ua_chanp if not NULL.
3562 * Called with UST app session lock and RCU read-side lock held.
3564 * Return 0 on success or else a negative value.
3566 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3567 struct ltt_ust_channel
*uchan
,
3568 enum lttng_ust_abi_chan_type type
, struct ltt_ust_session
*usess
,
3569 struct ust_app_channel
**ua_chanp
)
3572 struct lttng_ht_iter iter
;
3573 struct lttng_ht_node_str
*ua_chan_node
;
3574 struct ust_app_channel
*ua_chan
;
3576 /* Lookup channel in the ust app session */
3577 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3578 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3579 if (ua_chan_node
!= NULL
) {
3580 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3584 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3585 if (ua_chan
== NULL
) {
3586 /* Only malloc can fail here */
3590 shadow_copy_channel(ua_chan
, uchan
);
3592 /* Set channel type. */
3593 ua_chan
->attr
.type
= type
;
3595 /* Only add the channel if successful on the tracer side. */
3596 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3599 *ua_chanp
= ua_chan
;
3602 /* Everything went well. */
3610 * Create UST app event and create it on the tracer side.
3612 * Must be called with the RCU read side lock held.
3613 * Called with ust app session mutex held.
3616 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3617 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3618 struct ust_app
*app
)
3621 struct ust_app_event
*ua_event
;
3623 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3624 if (ua_event
== NULL
) {
3625 /* Only failure mode of alloc_ust_app_event(). */
3629 shadow_copy_event(ua_event
, uevent
);
3631 /* Create it on the tracer side */
3632 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3635 * Not found previously means that it does not exist on the
3636 * tracer. If the application reports that the event existed,
3637 * it means there is a bug in the sessiond or lttng-ust
3638 * (or corruption, etc.)
3640 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3641 ERR("Tracer for application reported that an event being created already existed: "
3642 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3644 app
->pid
, app
->ppid
, app
->uid
,
3650 add_unique_ust_app_event(ua_chan
, ua_event
);
3652 DBG2("UST app create event completed: app = '%s' (ppid: %d)",
3653 app
->name
, app
->ppid
);
3659 /* Valid. Calling here is already in a read side lock */
3660 delete_ust_app_event(-1, ua_event
, app
);
3665 * Create UST app event notifier rule and create it on the tracer side.
3667 * Must be called with the RCU read side lock held.
3668 * Called with ust app session mutex held.
3671 int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
,
3672 struct ust_app
*app
)
3675 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3677 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3678 if (ua_event_notifier_rule
== NULL
) {
3683 /* Create it on the tracer side. */
3684 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3687 * Not found previously means that it does not exist on the
3688 * tracer. If the application reports that the event existed,
3689 * it means there is a bug in the sessiond or lttng-ust
3690 * (or corruption, etc.)
3692 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3693 ERR("Tracer for application reported that an event notifier being created already exists: "
3694 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3695 lttng_trigger_get_tracer_token(trigger
),
3696 app
->pid
, app
->ppid
, app
->uid
,
3702 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3703 &ua_event_notifier_rule
->node
);
3705 DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64
,
3706 app
->name
, app
->ppid
, lttng_trigger_get_tracer_token(trigger
));
3711 /* The RCU read side lock is already being held by the caller. */
3712 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3718 * Create UST metadata and open it on the tracer side.
3720 * Called with UST app session lock held and RCU read side lock.
3722 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3723 struct ust_app
*app
, struct consumer_output
*consumer
)
3726 struct ust_app_channel
*metadata
;
3727 struct consumer_socket
*socket
;
3728 struct ust_registry_session
*registry
;
3729 struct ltt_session
*session
= NULL
;
3731 LTTNG_ASSERT(ua_sess
);
3733 LTTNG_ASSERT(consumer
);
3735 registry
= get_session_registry(ua_sess
);
3736 /* The UST app session is held registry shall not be null. */
3737 LTTNG_ASSERT(registry
);
3739 pthread_mutex_lock(®istry
->lock
);
3741 /* Metadata already exists for this registry or it was closed previously */
3742 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3747 /* Allocate UST metadata */
3748 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3750 /* malloc() failed */
3755 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3757 /* Need one fd for the channel. */
3758 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3760 ERR("Exhausted number of available FD upon create metadata");
3764 /* Get the right consumer socket for the application. */
3765 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3768 goto error_consumer
;
3772 * Keep metadata key so we can identify it on the consumer side. Assign it
3773 * to the registry *before* we ask the consumer so we avoid the race of the
3774 * consumer requesting the metadata and the ask_channel call on our side
3775 * did not returned yet.
3777 registry
->metadata_key
= metadata
->key
;
3779 session
= session_find_by_id(ua_sess
->tracing_id
);
3780 LTTNG_ASSERT(session
);
3782 LTTNG_ASSERT(pthread_mutex_trylock(&session
->lock
));
3783 LTTNG_ASSERT(session_trylock_list());
3786 * Ask the metadata channel creation to the consumer. The metadata object
3787 * will be created by the consumer and kept their. However, the stream is
3788 * never added or monitored until we do a first push metadata to the
3791 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3792 registry
, session
->current_trace_chunk
);
3794 /* Nullify the metadata key so we don't try to close it later on. */
3795 registry
->metadata_key
= 0;
3796 goto error_consumer
;
3800 * The setup command will make the metadata stream be sent to the relayd,
3801 * if applicable, and the thread managing the metadatas. This is important
3802 * because after this point, if an error occurs, the only way the stream
3803 * can be deleted is to be monitored in the consumer.
3805 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3807 /* Nullify the metadata key so we don't try to close it later on. */
3808 registry
->metadata_key
= 0;
3809 goto error_consumer
;
3812 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3813 metadata
->key
, app
->pid
);
3816 lttng_fd_put(LTTNG_FD_APPS
, 1);
3817 delete_ust_app_channel(-1, metadata
, app
);
3819 pthread_mutex_unlock(®istry
->lock
);
3821 session_put(session
);
3827 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3828 * acquired before calling this function.
3830 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3832 struct ust_app
*app
= NULL
;
3833 struct lttng_ht_node_ulong
*node
;
3834 struct lttng_ht_iter iter
;
3836 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3837 node
= lttng_ht_iter_get_node_ulong(&iter
);
3839 DBG2("UST app no found with pid %d", pid
);
3843 DBG2("Found UST app by pid %d", pid
);
3845 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3852 * Allocate and init an UST app object using the registration information and
3853 * the command socket. This is called when the command socket connects to the
3856 * The object is returned on success or else NULL.
3858 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3861 struct ust_app
*lta
= NULL
;
3862 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3865 LTTNG_ASSERT(sock
>= 0);
3867 DBG3("UST app creating application for socket %d", sock
);
3869 if ((msg
->bits_per_long
== 64 &&
3870 (uatomic_read(&the_ust_consumerd64_fd
) ==
3872 (msg
->bits_per_long
== 32 &&
3873 (uatomic_read(&the_ust_consumerd32_fd
) ==
3875 ERR("Registration failed: application \"%s\" (pid: %d) has "
3876 "%d-bit long, but no consumerd for this size is available.\n",
3877 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3882 * Reserve the two file descriptors of the event source pipe. The write
3883 * end will be closed once it is passed to the application, at which
3884 * point a single 'put' will be performed.
3886 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3888 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s' (ppid: %d)",
3889 msg
->name
, (int) msg
->ppid
);
3893 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3894 if (!event_notifier_event_source_pipe
) {
3895 PERROR("Failed to open application event source pipe: '%s' (ppid = %d)",
3896 msg
->name
, msg
->ppid
);
3900 lta
= zmalloc(sizeof(struct ust_app
));
3903 goto error_free_pipe
;
3906 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
3908 lta
->ppid
= msg
->ppid
;
3909 lta
->uid
= msg
->uid
;
3910 lta
->gid
= msg
->gid
;
3912 lta
->bits_per_long
= msg
->bits_per_long
;
3913 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3914 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3915 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3916 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3917 lta
->long_alignment
= msg
->long_alignment
;
3918 lta
->byte_order
= msg
->byte_order
;
3920 lta
->v_major
= msg
->major
;
3921 lta
->v_minor
= msg
->minor
;
3922 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3923 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3924 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3925 lta
->notify_sock
= -1;
3926 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3928 /* Copy name and make sure it's NULL terminated. */
3929 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3930 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3933 * Before this can be called, when receiving the registration information,
3934 * the application compatibility is checked. So, at this point, the
3935 * application can work with this session daemon.
3937 lta
->compatible
= 1;
3939 lta
->pid
= msg
->pid
;
3940 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3942 pthread_mutex_init(<a
->sock_lock
, NULL
);
3943 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3945 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3949 lttng_pipe_destroy(event_notifier_event_source_pipe
);
3950 lttng_fd_put(LTTNG_FD_APPS
, 2);
3956 * For a given application object, add it to every hash table.
3958 void ust_app_add(struct ust_app
*app
)
3961 LTTNG_ASSERT(app
->notify_sock
>= 0);
3963 app
->registration_time
= time(NULL
);
3968 * On a re-registration, we want to kick out the previous registration of
3971 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3974 * The socket _should_ be unique until _we_ call close. So, a add_unique
3975 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3976 * already in the table.
3978 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3980 /* Add application to the notify socket hash table. */
3981 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3982 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3984 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3985 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3986 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3993 * Set the application version into the object.
3995 * Return 0 on success else a negative value either an errno code or a
3996 * LTTng-UST error code.
3998 int ust_app_version(struct ust_app
*app
)
4004 pthread_mutex_lock(&app
->sock_lock
);
4005 ret
= lttng_ust_ctl_tracer_version(app
->sock
, &app
->version
);
4006 pthread_mutex_unlock(&app
->sock_lock
);
4008 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4009 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
4011 DBG3("UST app %d version failed. Application is dead", app
->sock
);
4018 bool ust_app_supports_notifiers(const struct ust_app
*app
)
4020 return app
->v_major
>= 9;
4023 bool ust_app_supports_counters(const struct ust_app
*app
)
4025 return app
->v_major
>= 9;
4029 * Setup the base event notifier group.
4031 * Return 0 on success else a negative value either an errno code or a
4032 * LTTng-UST error code.
4034 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4037 int event_pipe_write_fd
;
4038 struct lttng_ust_abi_object_data
*event_notifier_group
= NULL
;
4039 enum lttng_error_code lttng_ret
;
4040 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4044 if (!ust_app_supports_notifiers(app
)) {
4049 /* Get the write side of the pipe. */
4050 event_pipe_write_fd
= lttng_pipe_get_writefd(
4051 app
->event_notifier_group
.event_pipe
);
4053 pthread_mutex_lock(&app
->sock_lock
);
4054 ret
= lttng_ust_ctl_create_event_notifier_group(app
->sock
,
4055 event_pipe_write_fd
, &event_notifier_group
);
4056 pthread_mutex_unlock(&app
->sock_lock
);
4058 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4059 ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d",
4060 ret
, app
->sock
, event_pipe_write_fd
);
4062 DBG("Failed to create application event notifier group (application is dead): app socket fd = %d",
4069 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4071 ERR("Failed to close write end of the application's event source pipe: app = '%s' (ppid = %d)",
4072 app
->name
, app
->ppid
);
4077 * Release the file descriptor that was reserved for the write-end of
4080 lttng_fd_put(LTTNG_FD_APPS
, 1);
4082 lttng_ret
= notification_thread_command_add_tracer_event_source(
4083 the_notification_thread_handle
,
4084 lttng_pipe_get_readfd(
4085 app
->event_notifier_group
.event_pipe
),
4087 if (lttng_ret
!= LTTNG_OK
) {
4088 ERR("Failed to add tracer event source to notification thread");
4093 /* Assign handle only when the complete setup is valid. */
4094 app
->event_notifier_group
.object
= event_notifier_group
;
4096 event_notifier_error_accounting_status
=
4097 event_notifier_error_accounting_register_app(app
);
4098 switch (event_notifier_error_accounting_status
) {
4099 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
4101 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
:
4102 DBG3("Failed to setup event notifier error accounting (application does not support notifier error accounting): app socket fd = %d, app name = '%s', app ppid = %d",
4103 app
->sock
, app
->name
, (int) app
->ppid
);
4105 goto error_accounting
;
4106 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
4107 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app ppid = %d",
4108 app
->sock
, app
->name
, (int) app
->ppid
);
4110 goto error_accounting
;
4112 ERR("Failed to setup event notifier error accounting for app");
4114 goto error_accounting
;
4120 lttng_ret
= notification_thread_command_remove_tracer_event_source(
4121 the_notification_thread_handle
,
4122 lttng_pipe_get_readfd(
4123 app
->event_notifier_group
.event_pipe
));
4124 if (lttng_ret
!= LTTNG_OK
) {
4125 ERR("Failed to remove application tracer event source from notification thread");
4129 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4130 free(app
->event_notifier_group
.object
);
4131 app
->event_notifier_group
.object
= NULL
;
4136 * Unregister app by removing it from the global traceable app list and freeing
4139 * The socket is already closed at this point so no close to sock.
4141 void ust_app_unregister(int sock
)
4143 struct ust_app
*lta
;
4144 struct lttng_ht_node_ulong
*node
;
4145 struct lttng_ht_iter ust_app_sock_iter
;
4146 struct lttng_ht_iter iter
;
4147 struct ust_app_session
*ua_sess
;
4152 /* Get the node reference for a call_rcu */
4153 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
4154 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4157 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
4158 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4161 * For per-PID buffers, perform "push metadata" and flush all
4162 * application streams before removing app from hash tables,
4163 * ensuring proper behavior of data_pending check.
4164 * Remove sessions so they are not visible during deletion.
4166 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
4168 struct ust_registry_session
*registry
;
4170 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4172 /* The session was already removed so scheduled for teardown. */
4176 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4177 (void) ust_app_flush_app_session(lta
, ua_sess
);
4181 * Add session to list for teardown. This is safe since at this point we
4182 * are the only one using this list.
4184 pthread_mutex_lock(&ua_sess
->lock
);
4186 if (ua_sess
->deleted
) {
4187 pthread_mutex_unlock(&ua_sess
->lock
);
4192 * Normally, this is done in the delete session process which is
4193 * executed in the call rcu below. However, upon registration we can't
4194 * afford to wait for the grace period before pushing data or else the
4195 * data pending feature can race between the unregistration and stop
4196 * command where the data pending command is sent *before* the grace
4199 * The close metadata below nullifies the metadata pointer in the
4200 * session so the delete session will NOT push/close a second time.
4202 registry
= get_session_registry(ua_sess
);
4204 /* Push metadata for application before freeing the application. */
4205 (void) push_metadata(registry
, ua_sess
->consumer
);
4208 * Don't ask to close metadata for global per UID buffers. Close
4209 * metadata only on destroy trace session in this case. Also, the
4210 * previous push metadata could have flag the metadata registry to
4211 * close so don't send a close command if closed.
4213 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4214 /* And ask to close it for this session registry. */
4215 (void) close_metadata(registry
, ua_sess
->consumer
);
4218 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4220 pthread_mutex_unlock(&ua_sess
->lock
);
4223 /* Remove application from PID hash table */
4224 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4228 * Remove application from notify hash table. The thread handling the
4229 * notify socket could have deleted the node so ignore on error because
4230 * either way it's valid. The close of that socket is handled by the
4231 * apps_notify_thread.
4233 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4234 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4237 * Ignore return value since the node might have been removed before by an
4238 * add replace during app registration because the PID can be reassigned by
4241 iter
.iter
.node
= <a
->pid_n
.node
;
4242 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4244 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4249 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4256 * Fill events array with all events name of all registered apps.
4258 int ust_app_list_events(struct lttng_event
**events
)
4261 size_t nbmem
, count
= 0;
4262 struct lttng_ht_iter iter
;
4263 struct ust_app
*app
;
4264 struct lttng_event
*tmp_event
;
4266 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4267 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
4268 if (tmp_event
== NULL
) {
4269 PERROR("zmalloc ust app events");
4276 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4277 struct lttng_ust_abi_tracepoint_iter uiter
;
4279 health_code_update();
4281 if (!app
->compatible
) {
4283 * TODO: In time, we should notice the caller of this error by
4284 * telling him that this is a version error.
4288 pthread_mutex_lock(&app
->sock_lock
);
4289 handle
= lttng_ust_ctl_tracepoint_list(app
->sock
);
4291 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4292 ERR("UST app list events getting handle failed for app pid %d",
4295 pthread_mutex_unlock(&app
->sock_lock
);
4299 while ((ret
= lttng_ust_ctl_tracepoint_list_get(app
->sock
, handle
,
4300 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4301 /* Handle ustctl error. */
4305 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4306 ERR("UST app tp list get failed for app %d with ret %d",
4309 DBG3("UST app tp list get failed. Application is dead");
4311 * This is normal behavior, an application can die during the
4312 * creation process. Don't report an error so the execution can
4313 * continue normally. Continue normal execution.
4318 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4319 if (release_ret
< 0 &&
4320 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4321 release_ret
!= -EPIPE
) {
4322 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4324 pthread_mutex_unlock(&app
->sock_lock
);
4328 health_code_update();
4329 if (count
>= nbmem
) {
4330 /* In case the realloc fails, we free the memory */
4331 struct lttng_event
*new_tmp_event
;
4334 new_nbmem
= nbmem
<< 1;
4335 DBG2("Reallocating event list from %zu to %zu entries",
4337 new_tmp_event
= realloc(tmp_event
,
4338 new_nbmem
* sizeof(struct lttng_event
));
4339 if (new_tmp_event
== NULL
) {
4342 PERROR("realloc ust app events");
4345 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4346 if (release_ret
< 0 &&
4347 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4348 release_ret
!= -EPIPE
) {
4349 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4351 pthread_mutex_unlock(&app
->sock_lock
);
4354 /* Zero the new memory */
4355 memset(new_tmp_event
+ nbmem
, 0,
4356 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4358 tmp_event
= new_tmp_event
;
4360 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4361 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4362 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4363 tmp_event
[count
].pid
= app
->pid
;
4364 tmp_event
[count
].enabled
= -1;
4367 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4368 pthread_mutex_unlock(&app
->sock_lock
);
4369 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4370 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4375 *events
= tmp_event
;
4377 DBG2("UST app list events done (%zu events)", count
);
4382 health_code_update();
4387 * Fill events array with all events name of all registered apps.
4389 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4392 size_t nbmem
, count
= 0;
4393 struct lttng_ht_iter iter
;
4394 struct ust_app
*app
;
4395 struct lttng_event_field
*tmp_event
;
4397 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4398 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
4399 if (tmp_event
== NULL
) {
4400 PERROR("zmalloc ust app event fields");
4407 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4408 struct lttng_ust_abi_field_iter uiter
;
4410 health_code_update();
4412 if (!app
->compatible
) {
4414 * TODO: In time, we should notice the caller of this error by
4415 * telling him that this is a version error.
4419 pthread_mutex_lock(&app
->sock_lock
);
4420 handle
= lttng_ust_ctl_tracepoint_field_list(app
->sock
);
4422 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4423 ERR("UST app list field getting handle failed for app pid %d",
4426 pthread_mutex_unlock(&app
->sock_lock
);
4430 while ((ret
= lttng_ust_ctl_tracepoint_field_list_get(app
->sock
, handle
,
4431 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4432 /* Handle ustctl error. */
4436 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4437 ERR("UST app tp list field failed for app %d with ret %d",
4440 DBG3("UST app tp list field failed. Application is dead");
4442 * This is normal behavior, an application can die during the
4443 * creation process. Don't report an error so the execution can
4444 * continue normally. Reset list and count for next app.
4449 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4450 pthread_mutex_unlock(&app
->sock_lock
);
4451 if (release_ret
< 0 &&
4452 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4453 release_ret
!= -EPIPE
) {
4454 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4459 health_code_update();
4460 if (count
>= nbmem
) {
4461 /* In case the realloc fails, we free the memory */
4462 struct lttng_event_field
*new_tmp_event
;
4465 new_nbmem
= nbmem
<< 1;
4466 DBG2("Reallocating event field list from %zu to %zu entries",
4468 new_tmp_event
= realloc(tmp_event
,
4469 new_nbmem
* sizeof(struct lttng_event_field
));
4470 if (new_tmp_event
== NULL
) {
4473 PERROR("realloc ust app event fields");
4476 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4477 pthread_mutex_unlock(&app
->sock_lock
);
4479 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4480 release_ret
!= -EPIPE
) {
4481 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4485 /* Zero the new memory */
4486 memset(new_tmp_event
+ nbmem
, 0,
4487 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4489 tmp_event
= new_tmp_event
;
4492 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4493 /* Mapping between these enums matches 1 to 1. */
4494 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4495 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4497 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4498 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4499 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4500 tmp_event
[count
].event
.pid
= app
->pid
;
4501 tmp_event
[count
].event
.enabled
= -1;
4504 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4505 pthread_mutex_unlock(&app
->sock_lock
);
4507 ret
!= -LTTNG_UST_ERR_EXITING
&&
4509 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4514 *fields
= tmp_event
;
4516 DBG2("UST app list event fields done (%zu events)", count
);
4521 health_code_update();
4526 * Free and clean all traceable apps of the global list.
4528 * Should _NOT_ be called with RCU read-side lock held.
4530 void ust_app_clean_list(void)
4533 struct ust_app
*app
;
4534 struct lttng_ht_iter iter
;
4536 DBG2("UST app cleaning registered apps hash table");
4540 /* Cleanup notify socket hash table */
4541 if (ust_app_ht_by_notify_sock
) {
4542 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4543 notify_sock_n
.node
) {
4545 * Assert that all notifiers are gone as all triggers
4546 * are unregistered prior to this clean-up.
4548 LTTNG_ASSERT(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4550 ust_app_notify_sock_unregister(app
->notify_sock
);
4555 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4556 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4558 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4562 /* Cleanup socket hash table */
4563 if (ust_app_ht_by_sock
) {
4564 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4566 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4573 /* Destroy is done only when the ht is empty */
4575 ht_cleanup_push(ust_app_ht
);
4577 if (ust_app_ht_by_sock
) {
4578 ht_cleanup_push(ust_app_ht_by_sock
);
4580 if (ust_app_ht_by_notify_sock
) {
4581 ht_cleanup_push(ust_app_ht_by_notify_sock
);
4586 * Init UST app hash table.
4588 int ust_app_ht_alloc(void)
4590 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4594 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4595 if (!ust_app_ht_by_sock
) {
4598 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4599 if (!ust_app_ht_by_notify_sock
) {
4606 * For a specific UST session, disable the channel for all registered apps.
4608 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4609 struct ltt_ust_channel
*uchan
)
4612 struct lttng_ht_iter iter
;
4613 struct lttng_ht_node_str
*ua_chan_node
;
4614 struct ust_app
*app
;
4615 struct ust_app_session
*ua_sess
;
4616 struct ust_app_channel
*ua_chan
;
4618 LTTNG_ASSERT(usess
->active
);
4619 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4620 uchan
->name
, usess
->id
);
4624 /* For every registered applications */
4625 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4626 struct lttng_ht_iter uiter
;
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
) {
4640 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4641 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4642 /* If the session if found for the app, the channel must be there */
4643 LTTNG_ASSERT(ua_chan_node
);
4645 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4646 /* The channel must not be already disabled */
4647 LTTNG_ASSERT(ua_chan
->enabled
== 1);
4649 /* Disable channel onto application */
4650 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4652 /* XXX: We might want to report this error at some point... */
4662 * For a specific UST session, enable the channel for all registered apps.
4664 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4665 struct ltt_ust_channel
*uchan
)
4668 struct lttng_ht_iter iter
;
4669 struct ust_app
*app
;
4670 struct ust_app_session
*ua_sess
;
4672 LTTNG_ASSERT(usess
->active
);
4673 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4674 uchan
->name
, usess
->id
);
4678 /* For every registered applications */
4679 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4680 if (!app
->compatible
) {
4682 * TODO: In time, we should notice the caller of this error by
4683 * telling him that this is a version error.
4687 ua_sess
= lookup_session_by_app(usess
, app
);
4688 if (ua_sess
== NULL
) {
4692 /* Enable channel onto application */
4693 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4695 /* XXX: We might want to report this error at some point... */
4705 * Disable an event in a channel and for a specific session.
4707 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4708 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4711 struct lttng_ht_iter iter
, uiter
;
4712 struct lttng_ht_node_str
*ua_chan_node
;
4713 struct ust_app
*app
;
4714 struct ust_app_session
*ua_sess
;
4715 struct ust_app_channel
*ua_chan
;
4716 struct ust_app_event
*ua_event
;
4718 LTTNG_ASSERT(usess
->active
);
4719 DBG("UST app disabling event %s for all apps in channel "
4720 "%s for session id %" PRIu64
,
4721 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4725 /* For all registered applications */
4726 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4727 if (!app
->compatible
) {
4729 * TODO: In time, we should notice the caller of this error by
4730 * telling him that this is a version error.
4734 ua_sess
= lookup_session_by_app(usess
, app
);
4735 if (ua_sess
== NULL
) {
4740 /* Lookup channel in the ust app session */
4741 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4742 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4743 if (ua_chan_node
== NULL
) {
4744 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4745 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4748 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4750 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4751 uevent
->filter
, uevent
->attr
.loglevel
,
4753 if (ua_event
== NULL
) {
4754 DBG2("Event %s not found in channel %s for app pid %d."
4755 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4759 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4761 /* XXX: Report error someday... */
4770 /* The ua_sess lock must be held by the caller. */
4772 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4773 struct ust_app_session
*ua_sess
,
4774 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4775 struct ust_app_channel
**_ua_chan
)
4778 struct ust_app_channel
*ua_chan
= NULL
;
4780 LTTNG_ASSERT(ua_sess
);
4781 ASSERT_LOCKED(ua_sess
->lock
);
4783 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4784 sizeof(uchan
->name
))) {
4785 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4789 struct ltt_ust_context
*uctx
= NULL
;
4792 * Create channel onto application and synchronize its
4795 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4796 LTTNG_UST_ABI_CHAN_PER_CPU
, usess
,
4802 ret
= ust_app_channel_send(app
, usess
,
4809 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4810 ret
= create_ust_app_channel_context(ua_chan
,
4823 * The application's socket is not valid. Either a bad socket
4824 * or a timeout on it. We can't inform the caller that for a
4825 * specific app, the session failed so lets continue here.
4827 ret
= 0; /* Not an error. */
4835 if (ret
== 0 && _ua_chan
) {
4837 * Only return the application's channel on success. Note
4838 * that the channel can still be part of the application's
4839 * channel hashtable on error.
4841 *_ua_chan
= ua_chan
;
4847 * Enable event for a specific session and channel on the tracer.
4849 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4850 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4853 struct lttng_ht_iter iter
, uiter
;
4854 struct lttng_ht_node_str
*ua_chan_node
;
4855 struct ust_app
*app
;
4856 struct ust_app_session
*ua_sess
;
4857 struct ust_app_channel
*ua_chan
;
4858 struct ust_app_event
*ua_event
;
4860 LTTNG_ASSERT(usess
->active
);
4861 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4862 uevent
->attr
.name
, usess
->id
);
4865 * NOTE: At this point, this function is called only if the session and
4866 * channel passed are already created for all apps. and enabled on the
4872 /* For all registered applications */
4873 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4874 if (!app
->compatible
) {
4876 * TODO: In time, we should notice the caller of this error by
4877 * telling him that this is a version error.
4881 ua_sess
= lookup_session_by_app(usess
, app
);
4883 /* The application has problem or is probably dead. */
4887 pthread_mutex_lock(&ua_sess
->lock
);
4889 if (ua_sess
->deleted
) {
4890 pthread_mutex_unlock(&ua_sess
->lock
);
4894 /* Lookup channel in the ust app session */
4895 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4896 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4898 * It is possible that the channel cannot be found is
4899 * the channel/event creation occurs concurrently with
4900 * an application exit.
4902 if (!ua_chan_node
) {
4903 pthread_mutex_unlock(&ua_sess
->lock
);
4907 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4909 /* Get event node */
4910 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4911 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4912 if (ua_event
== NULL
) {
4913 DBG3("UST app enable event %s not found for app PID %d."
4914 "Skipping app", uevent
->attr
.name
, app
->pid
);
4918 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4920 pthread_mutex_unlock(&ua_sess
->lock
);
4924 pthread_mutex_unlock(&ua_sess
->lock
);
4933 * For a specific existing UST session and UST channel, creates the event for
4934 * all registered apps.
4936 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4937 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4940 struct lttng_ht_iter iter
, uiter
;
4941 struct lttng_ht_node_str
*ua_chan_node
;
4942 struct ust_app
*app
;
4943 struct ust_app_session
*ua_sess
;
4944 struct ust_app_channel
*ua_chan
;
4946 LTTNG_ASSERT(usess
->active
);
4947 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4948 uevent
->attr
.name
, usess
->id
);
4952 /* For all registered applications */
4953 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4954 if (!app
->compatible
) {
4956 * TODO: In time, we should notice the caller of this error by
4957 * telling him that this is a version error.
4961 ua_sess
= lookup_session_by_app(usess
, app
);
4963 /* The application has problem or is probably dead. */
4967 pthread_mutex_lock(&ua_sess
->lock
);
4969 if (ua_sess
->deleted
) {
4970 pthread_mutex_unlock(&ua_sess
->lock
);
4974 /* Lookup channel in the ust app session */
4975 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4976 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4977 /* If the channel is not found, there is a code flow error */
4978 LTTNG_ASSERT(ua_chan_node
);
4980 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4982 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4983 pthread_mutex_unlock(&ua_sess
->lock
);
4985 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4986 /* Possible value at this point: -ENOMEM. If so, we stop! */
4989 DBG2("UST app event %s already exist on app PID %d",
4990 uevent
->attr
.name
, app
->pid
);
5000 * Start tracing for a specific UST session and app.
5002 * Called with UST app session lock held.
5006 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5009 struct ust_app_session
*ua_sess
;
5011 DBG("Starting tracing for ust app pid %d", app
->pid
);
5015 if (!app
->compatible
) {
5019 ua_sess
= lookup_session_by_app(usess
, app
);
5020 if (ua_sess
== NULL
) {
5021 /* The session is in teardown process. Ignore and continue. */
5025 pthread_mutex_lock(&ua_sess
->lock
);
5027 if (ua_sess
->deleted
) {
5028 pthread_mutex_unlock(&ua_sess
->lock
);
5032 if (ua_sess
->enabled
) {
5033 pthread_mutex_unlock(&ua_sess
->lock
);
5037 /* Upon restart, we skip the setup, already done */
5038 if (ua_sess
->started
) {
5042 health_code_update();
5045 /* This starts the UST tracing */
5046 pthread_mutex_lock(&app
->sock_lock
);
5047 ret
= lttng_ust_ctl_start_session(app
->sock
, ua_sess
->handle
);
5048 pthread_mutex_unlock(&app
->sock_lock
);
5050 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5051 ERR("Error starting tracing for app pid: %d (ret: %d)",
5054 DBG("UST app start session failed. Application is dead.");
5056 * This is normal behavior, an application can die during the
5057 * creation process. Don't report an error so the execution can
5058 * continue normally.
5060 pthread_mutex_unlock(&ua_sess
->lock
);
5066 /* Indicate that the session has been started once */
5067 ua_sess
->started
= 1;
5068 ua_sess
->enabled
= 1;
5070 pthread_mutex_unlock(&ua_sess
->lock
);
5072 health_code_update();
5074 /* Quiescent wait after starting trace */
5075 pthread_mutex_lock(&app
->sock_lock
);
5076 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5077 pthread_mutex_unlock(&app
->sock_lock
);
5078 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5079 ERR("UST app wait quiescent failed for app pid %d ret %d",
5085 health_code_update();
5089 pthread_mutex_unlock(&ua_sess
->lock
);
5091 health_code_update();
5096 * Stop tracing for a specific UST session and app.
5099 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5102 struct ust_app_session
*ua_sess
;
5103 struct ust_registry_session
*registry
;
5105 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5109 if (!app
->compatible
) {
5110 goto end_no_session
;
5113 ua_sess
= lookup_session_by_app(usess
, app
);
5114 if (ua_sess
== NULL
) {
5115 goto end_no_session
;
5118 pthread_mutex_lock(&ua_sess
->lock
);
5120 if (ua_sess
->deleted
) {
5121 pthread_mutex_unlock(&ua_sess
->lock
);
5122 goto end_no_session
;
5126 * If started = 0, it means that stop trace has been called for a session
5127 * that was never started. It's possible since we can have a fail start
5128 * from either the application manager thread or the command thread. Simply
5129 * indicate that this is a stop error.
5131 if (!ua_sess
->started
) {
5132 goto error_rcu_unlock
;
5135 health_code_update();
5137 /* This inhibits UST tracing */
5138 pthread_mutex_lock(&app
->sock_lock
);
5139 ret
= lttng_ust_ctl_stop_session(app
->sock
, ua_sess
->handle
);
5140 pthread_mutex_unlock(&app
->sock_lock
);
5142 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5143 ERR("Error stopping tracing for app pid: %d (ret: %d)",
5146 DBG("UST app stop session failed. Application is dead.");
5148 * This is normal behavior, an application can die during the
5149 * creation process. Don't report an error so the execution can
5150 * continue normally.
5154 goto error_rcu_unlock
;
5157 health_code_update();
5158 ua_sess
->enabled
= 0;
5160 /* Quiescent wait after stopping trace */
5161 pthread_mutex_lock(&app
->sock_lock
);
5162 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5163 pthread_mutex_unlock(&app
->sock_lock
);
5164 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5165 ERR("UST app wait quiescent failed for app pid %d ret %d",
5169 health_code_update();
5171 registry
= get_session_registry(ua_sess
);
5173 /* The UST app session is held registry shall not be null. */
5174 LTTNG_ASSERT(registry
);
5176 /* Push metadata for application before freeing the application. */
5177 (void) push_metadata(registry
, ua_sess
->consumer
);
5180 pthread_mutex_unlock(&ua_sess
->lock
);
5183 health_code_update();
5187 pthread_mutex_unlock(&ua_sess
->lock
);
5189 health_code_update();
5194 int ust_app_flush_app_session(struct ust_app
*app
,
5195 struct ust_app_session
*ua_sess
)
5197 int ret
, retval
= 0;
5198 struct lttng_ht_iter iter
;
5199 struct ust_app_channel
*ua_chan
;
5200 struct consumer_socket
*socket
;
5202 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5206 if (!app
->compatible
) {
5207 goto end_not_compatible
;
5210 pthread_mutex_lock(&ua_sess
->lock
);
5212 if (ua_sess
->deleted
) {
5216 health_code_update();
5218 /* Flushing buffers */
5219 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5222 /* Flush buffers and push metadata. */
5223 switch (ua_sess
->buffer_type
) {
5224 case LTTNG_BUFFER_PER_PID
:
5225 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5227 health_code_update();
5228 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5230 ERR("Error flushing consumer channel");
5236 case LTTNG_BUFFER_PER_UID
:
5242 health_code_update();
5245 pthread_mutex_unlock(&ua_sess
->lock
);
5249 health_code_update();
5254 * Flush buffers for all applications for a specific UST session.
5255 * Called with UST session lock held.
5258 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5263 DBG("Flushing session buffers for all ust apps");
5267 /* Flush buffers and push metadata. */
5268 switch (usess
->buffer_type
) {
5269 case LTTNG_BUFFER_PER_UID
:
5271 struct buffer_reg_uid
*reg
;
5272 struct lttng_ht_iter iter
;
5274 /* Flush all per UID buffers associated to that session. */
5275 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5276 struct ust_registry_session
*ust_session_reg
;
5277 struct buffer_reg_channel
*buf_reg_chan
;
5278 struct consumer_socket
*socket
;
5280 /* Get consumer socket to use to push the metadata.*/
5281 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5284 /* Ignore request if no consumer is found for the session. */
5288 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5289 buf_reg_chan
, node
.node
) {
5291 * The following call will print error values so the return
5292 * code is of little importance because whatever happens, we
5293 * have to try them all.
5295 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5298 ust_session_reg
= reg
->registry
->reg
.ust
;
5299 /* Push metadata. */
5300 (void) push_metadata(ust_session_reg
, usess
->consumer
);
5304 case LTTNG_BUFFER_PER_PID
:
5306 struct ust_app_session
*ua_sess
;
5307 struct lttng_ht_iter iter
;
5308 struct ust_app
*app
;
5310 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5311 ua_sess
= lookup_session_by_app(usess
, app
);
5312 if (ua_sess
== NULL
) {
5315 (void) ust_app_flush_app_session(app
, ua_sess
);
5326 health_code_update();
5331 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5332 struct ust_app_session
*ua_sess
)
5335 struct lttng_ht_iter iter
;
5336 struct ust_app_channel
*ua_chan
;
5337 struct consumer_socket
*socket
;
5339 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5343 if (!app
->compatible
) {
5344 goto end_not_compatible
;
5347 pthread_mutex_lock(&ua_sess
->lock
);
5349 if (ua_sess
->deleted
) {
5353 health_code_update();
5355 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5358 ERR("Failed to find consumer (%" PRIu32
") socket",
5359 app
->bits_per_long
);
5364 /* Clear quiescent state. */
5365 switch (ua_sess
->buffer_type
) {
5366 case LTTNG_BUFFER_PER_PID
:
5367 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5368 ua_chan
, node
.node
) {
5369 health_code_update();
5370 ret
= consumer_clear_quiescent_channel(socket
,
5373 ERR("Error clearing quiescent state for consumer channel");
5379 case LTTNG_BUFFER_PER_UID
:
5386 health_code_update();
5389 pthread_mutex_unlock(&ua_sess
->lock
);
5393 health_code_update();
5398 * Clear quiescent state in each stream for all applications for a
5399 * specific UST session.
5400 * Called with UST session lock held.
5403 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5408 DBG("Clearing stream quiescent state for all ust apps");
5412 switch (usess
->buffer_type
) {
5413 case LTTNG_BUFFER_PER_UID
:
5415 struct lttng_ht_iter iter
;
5416 struct buffer_reg_uid
*reg
;
5419 * Clear quiescent for all per UID buffers associated to
5422 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5423 struct consumer_socket
*socket
;
5424 struct buffer_reg_channel
*buf_reg_chan
;
5426 /* Get associated consumer socket.*/
5427 socket
= consumer_find_socket_by_bitness(
5428 reg
->bits_per_long
, usess
->consumer
);
5431 * Ignore request if no consumer is found for
5437 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5438 &iter
.iter
, buf_reg_chan
, node
.node
) {
5440 * The following call will print error values so
5441 * the return code is of little importance
5442 * because whatever happens, we have to try them
5445 (void) consumer_clear_quiescent_channel(socket
,
5446 buf_reg_chan
->consumer_key
);
5451 case LTTNG_BUFFER_PER_PID
:
5453 struct ust_app_session
*ua_sess
;
5454 struct lttng_ht_iter iter
;
5455 struct ust_app
*app
;
5457 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5459 ua_sess
= lookup_session_by_app(usess
, app
);
5460 if (ua_sess
== NULL
) {
5463 (void) ust_app_clear_quiescent_app_session(app
,
5475 health_code_update();
5480 * Destroy a specific UST session in apps.
5482 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5485 struct ust_app_session
*ua_sess
;
5486 struct lttng_ht_iter iter
;
5487 struct lttng_ht_node_u64
*node
;
5489 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5493 if (!app
->compatible
) {
5497 __lookup_session_by_app(usess
, app
, &iter
);
5498 node
= lttng_ht_iter_get_node_u64(&iter
);
5500 /* Session is being or is deleted. */
5503 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
5505 health_code_update();
5506 destroy_app_session(app
, ua_sess
);
5508 health_code_update();
5510 /* Quiescent wait after stopping trace */
5511 pthread_mutex_lock(&app
->sock_lock
);
5512 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5513 pthread_mutex_unlock(&app
->sock_lock
);
5514 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5515 ERR("UST app wait quiescent failed for app pid %d ret %d",
5520 health_code_update();
5525 * Start tracing for the UST session.
5527 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5529 struct lttng_ht_iter iter
;
5530 struct ust_app
*app
;
5532 DBG("Starting all UST traces");
5535 * Even though the start trace might fail, flag this session active so
5536 * other application coming in are started by default.
5543 * In a start-stop-start use-case, we need to clear the quiescent state
5544 * of each channel set by the prior stop command, thus ensuring that a
5545 * following stop or destroy is sure to grab a timestamp_end near those
5546 * operations, even if the packet is empty.
5548 (void) ust_app_clear_quiescent_session(usess
);
5550 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5551 ust_app_global_update(usess
, app
);
5560 * Start tracing for the UST session.
5561 * Called with UST session lock held.
5563 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5566 struct lttng_ht_iter iter
;
5567 struct ust_app
*app
;
5569 DBG("Stopping all UST traces");
5572 * Even though the stop trace might fail, flag this session inactive so
5573 * other application coming in are not started by default.
5579 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5580 ret
= ust_app_stop_trace(usess
, app
);
5582 /* Continue to next apps even on error */
5587 (void) ust_app_flush_session(usess
);
5595 * Destroy app UST session.
5597 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5600 struct lttng_ht_iter iter
;
5601 struct ust_app
*app
;
5603 DBG("Destroy all UST traces");
5607 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5608 ret
= destroy_trace(usess
, app
);
5610 /* Continue to next apps even on error */
5620 /* The ua_sess lock must be held by the caller. */
5622 int find_or_create_ust_app_channel(
5623 struct ltt_ust_session
*usess
,
5624 struct ust_app_session
*ua_sess
,
5625 struct ust_app
*app
,
5626 struct ltt_ust_channel
*uchan
,
5627 struct ust_app_channel
**ua_chan
)
5630 struct lttng_ht_iter iter
;
5631 struct lttng_ht_node_str
*ua_chan_node
;
5633 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5634 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5636 *ua_chan
= caa_container_of(ua_chan_node
,
5637 struct ust_app_channel
, node
);
5641 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5650 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5651 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5652 struct ust_app
*app
)
5655 struct ust_app_event
*ua_event
= NULL
;
5657 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5658 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5660 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5665 if (ua_event
->enabled
!= uevent
->enabled
) {
5666 ret
= uevent
->enabled
?
5667 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5668 disable_ust_app_event(ua_sess
, ua_event
, app
);
5676 /* Called with RCU read-side lock held. */
5678 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5681 enum lttng_error_code ret_code
;
5682 enum lttng_trigger_status t_status
;
5683 struct lttng_ht_iter app_trigger_iter
;
5684 struct lttng_triggers
*triggers
= NULL
;
5685 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5686 unsigned int count
, i
;
5688 if (!ust_app_supports_notifiers(app
)) {
5693 * Currrently, registering or unregistering a trigger with an
5694 * event rule condition causes a full synchronization of the event
5697 * The first step attempts to add an event notifier for all registered
5698 * triggers that apply to the user space tracers. Then, the
5699 * application's event notifiers rules are all checked against the list
5700 * of registered triggers. Any event notifier that doesn't have a
5701 * matching trigger can be assumed to have been disabled.
5703 * All of this is inefficient, but is put in place to get the feature
5704 * rolling as it is simpler at this moment. It will be optimized Soon™
5705 * to allow the state of enabled
5706 * event notifiers to be synchronized in a piece-wise way.
5709 /* Get all triggers using uid 0 (root) */
5710 ret_code
= notification_thread_command_list_triggers(
5711 the_notification_thread_handle
, 0, &triggers
);
5712 if (ret_code
!= LTTNG_OK
) {
5716 LTTNG_ASSERT(triggers
);
5718 t_status
= lttng_triggers_get_count(triggers
, &count
);
5719 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5723 for (i
= 0; i
< count
; i
++) {
5724 struct lttng_condition
*condition
;
5725 struct lttng_event_rule
*event_rule
;
5726 struct lttng_trigger
*trigger
;
5727 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5728 enum lttng_condition_status condition_status
;
5731 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5732 LTTNG_ASSERT(trigger
);
5734 token
= lttng_trigger_get_tracer_token(trigger
);
5735 condition
= lttng_trigger_get_condition(trigger
);
5737 if (lttng_condition_get_type(condition
) !=
5738 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
) {
5739 /* Does not apply */
5744 lttng_condition_event_rule_matches_borrow_rule_mutable(
5745 condition
, &event_rule
);
5746 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5748 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5749 /* Skip kernel related triggers. */
5754 * Find or create the associated token event rule. The caller
5755 * holds the RCU read lock, so this is safe to call without
5756 * explicitly acquiring it here.
5758 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
5759 app
->token_to_event_notifier_rule_ht
, token
);
5760 if (!looked_up_event_notifier_rule
) {
5761 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
5769 /* Remove all unknown event sources from the app. */
5770 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
5771 &app_trigger_iter
.iter
, event_notifier_rule
,
5773 const uint64_t app_token
= event_notifier_rule
->token
;
5777 * Check if the app event trigger still exists on the
5778 * notification side.
5780 for (i
= 0; i
< count
; i
++) {
5781 uint64_t notification_thread_token
;
5782 const struct lttng_trigger
*trigger
=
5783 lttng_triggers_get_at_index(
5786 LTTNG_ASSERT(trigger
);
5788 notification_thread_token
=
5789 lttng_trigger_get_tracer_token(trigger
);
5791 if (notification_thread_token
== app_token
) {
5803 * This trigger was unregistered, disable it on the tracer's
5806 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
5808 LTTNG_ASSERT(ret
== 0);
5810 /* Callee logs errors. */
5811 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
5813 delete_ust_app_event_notifier_rule(
5814 app
->sock
, event_notifier_rule
, app
);
5820 lttng_triggers_destroy(triggers
);
5825 * RCU read lock must be held by the caller.
5828 void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
5829 struct ust_app_session
*ua_sess
,
5830 struct ust_app
*app
)
5833 struct cds_lfht_iter uchan_iter
;
5834 struct ltt_ust_channel
*uchan
;
5836 LTTNG_ASSERT(usess
);
5837 LTTNG_ASSERT(ua_sess
);
5840 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5842 struct ust_app_channel
*ua_chan
;
5843 struct cds_lfht_iter uevent_iter
;
5844 struct ltt_ust_event
*uevent
;
5847 * Search for a matching ust_app_channel. If none is found,
5848 * create it. Creating the channel will cause the ua_chan
5849 * structure to be allocated, the channel buffers to be
5850 * allocated (if necessary) and sent to the application, and
5851 * all enabled contexts will be added to the channel.
5853 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5854 app
, uchan
, &ua_chan
);
5856 /* Tracer is probably gone or ENOMEM. */
5861 /* ua_chan will be NULL for the metadata channel */
5865 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5867 ret
= ust_app_channel_synchronize_event(ua_chan
,
5868 uevent
, ua_sess
, app
);
5874 if (ua_chan
->enabled
!= uchan
->enabled
) {
5875 ret
= uchan
->enabled
?
5876 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5877 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5888 * The caller must ensure that the application is compatible and is tracked
5889 * by the process attribute trackers.
5892 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5893 struct ust_app
*app
)
5896 struct ust_app_session
*ua_sess
= NULL
;
5899 * The application's configuration should only be synchronized for
5902 LTTNG_ASSERT(usess
->active
);
5904 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5906 /* Tracer is probably gone or ENOMEM. */
5908 destroy_app_session(app
, ua_sess
);
5912 LTTNG_ASSERT(ua_sess
);
5914 pthread_mutex_lock(&ua_sess
->lock
);
5915 if (ua_sess
->deleted
) {
5916 goto deleted_session
;
5921 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
5924 * Create the metadata for the application. This returns gracefully if a
5925 * metadata was already set for the session.
5927 * The metadata channel must be created after the data channels as the
5928 * consumer daemon assumes this ordering. When interacting with a relay
5929 * daemon, the consumer will use this assumption to send the
5930 * "STREAMS_SENT" message to the relay daemon.
5932 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5934 ERR("Metadata creation failed for app sock %d for session id %" PRIu64
,
5935 app
->sock
, usess
->id
);
5941 pthread_mutex_unlock(&ua_sess
->lock
);
5947 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5949 struct ust_app_session
*ua_sess
;
5951 ua_sess
= lookup_session_by_app(usess
, app
);
5952 if (ua_sess
== NULL
) {
5955 destroy_app_session(app
, ua_sess
);
5959 * Add channels/events from UST global domain to registered apps at sock.
5961 * Called with session lock held.
5962 * Called with RCU read-side lock held.
5964 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5966 LTTNG_ASSERT(usess
);
5967 LTTNG_ASSERT(usess
->active
);
5969 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5970 app
->sock
, usess
->id
);
5972 if (!app
->compatible
) {
5975 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5977 trace_ust_id_tracker_lookup(
5978 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5980 trace_ust_id_tracker_lookup(
5981 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5984 * Synchronize the application's internal tracing configuration
5985 * and start tracing.
5987 ust_app_synchronize(usess
, app
);
5988 ust_app_start_trace(usess
, app
);
5990 ust_app_global_destroy(usess
, app
);
5995 * Add all event notifiers to an application.
5997 * Called with session lock held.
5998 * Called with RCU read-side lock held.
6000 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
6002 DBG2("UST application global event notifier rules update: app = '%s' (ppid: %d)",
6003 app
->name
, app
->ppid
);
6005 if (!app
->compatible
|| !ust_app_supports_notifiers(app
)) {
6009 if (app
->event_notifier_group
.object
== NULL
) {
6010 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s' (ppid: %d)",
6011 app
->name
, app
->ppid
);
6015 ust_app_synchronize_event_notifier_rules(app
);
6019 * Called with session lock held.
6021 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
6023 struct lttng_ht_iter iter
;
6024 struct ust_app
*app
;
6027 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6028 ust_app_global_update(usess
, app
);
6033 void ust_app_global_update_all_event_notifier_rules(void)
6035 struct lttng_ht_iter iter
;
6036 struct ust_app
*app
;
6039 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6040 ust_app_global_update_event_notifier_rules(app
);
6047 * Add context to a specific channel for global UST domain.
6049 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
6050 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
6053 struct lttng_ht_node_str
*ua_chan_node
;
6054 struct lttng_ht_iter iter
, uiter
;
6055 struct ust_app_channel
*ua_chan
= NULL
;
6056 struct ust_app_session
*ua_sess
;
6057 struct ust_app
*app
;
6059 LTTNG_ASSERT(usess
->active
);
6062 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6063 if (!app
->compatible
) {
6065 * TODO: In time, we should notice the caller of this error by
6066 * telling him that this is a version error.
6070 ua_sess
= lookup_session_by_app(usess
, app
);
6071 if (ua_sess
== NULL
) {
6075 pthread_mutex_lock(&ua_sess
->lock
);
6077 if (ua_sess
->deleted
) {
6078 pthread_mutex_unlock(&ua_sess
->lock
);
6082 /* Lookup channel in the ust app session */
6083 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
6084 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6085 if (ua_chan_node
== NULL
) {
6088 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
6090 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6095 pthread_mutex_unlock(&ua_sess
->lock
);
6103 * Receive registration and populate the given msg structure.
6105 * On success return 0 else a negative value returned by the ustctl call.
6107 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6110 uint32_t pid
, ppid
, uid
, gid
;
6114 ret
= lttng_ust_ctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
6115 &pid
, &ppid
, &uid
, &gid
,
6116 &msg
->bits_per_long
,
6117 &msg
->uint8_t_alignment
,
6118 &msg
->uint16_t_alignment
,
6119 &msg
->uint32_t_alignment
,
6120 &msg
->uint64_t_alignment
,
6121 &msg
->long_alignment
,
6128 case LTTNG_UST_ERR_EXITING
:
6129 DBG3("UST app recv reg message failed. Application died");
6131 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6132 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6133 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
6134 LTTNG_UST_ABI_MINOR_VERSION
);
6137 ERR("UST app recv reg message failed with ret %d", ret
);
6142 msg
->pid
= (pid_t
) pid
;
6143 msg
->ppid
= (pid_t
) ppid
;
6144 msg
->uid
= (uid_t
) uid
;
6145 msg
->gid
= (gid_t
) gid
;
6152 * Return a ust app session object using the application object and the
6153 * session object descriptor has a key. If not found, NULL is returned.
6154 * A RCU read side lock MUST be acquired when calling this function.
6156 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
6159 struct lttng_ht_node_ulong
*node
;
6160 struct lttng_ht_iter iter
;
6161 struct ust_app_session
*ua_sess
= NULL
;
6165 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
6166 node
= lttng_ht_iter_get_node_ulong(&iter
);
6168 DBG2("UST app session find by objd %d not found", objd
);
6172 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
6179 * Return a ust app channel object using the application object and the channel
6180 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6181 * lock MUST be acquired before calling this function.
6183 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
6186 struct lttng_ht_node_ulong
*node
;
6187 struct lttng_ht_iter iter
;
6188 struct ust_app_channel
*ua_chan
= NULL
;
6192 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
6193 node
= lttng_ht_iter_get_node_ulong(&iter
);
6195 DBG2("UST app channel find by objd %d not found", objd
);
6199 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
6206 * Reply to a register channel notification from an application on the notify
6207 * socket. The channel metadata is also created.
6209 * The session UST registry lock is acquired in this function.
6211 * On success 0 is returned else a negative value.
6213 static int reply_ust_register_channel(int sock
, int cobjd
,
6214 size_t nr_fields
, struct lttng_ust_ctl_field
*fields
)
6216 int ret
, ret_code
= 0;
6218 uint64_t chan_reg_key
;
6219 enum lttng_ust_ctl_channel_header type
;
6220 struct ust_app
*app
;
6221 struct ust_app_channel
*ua_chan
;
6222 struct ust_app_session
*ua_sess
;
6223 struct ust_registry_session
*registry
;
6224 struct ust_registry_channel
*ust_reg_chan
;
6228 /* Lookup application. If not found, there is a code flow error. */
6229 app
= find_app_by_notify_sock(sock
);
6231 DBG("Application socket %d is being torn down. Abort event notify",
6234 goto error_rcu_unlock
;
6237 /* Lookup channel by UST object descriptor. */
6238 ua_chan
= find_channel_by_objd(app
, cobjd
);
6240 DBG("Application channel is being torn down. Abort event notify");
6242 goto error_rcu_unlock
;
6245 LTTNG_ASSERT(ua_chan
->session
);
6246 ua_sess
= ua_chan
->session
;
6248 /* Get right session registry depending on the session buffer type. */
6249 registry
= get_session_registry(ua_sess
);
6251 DBG("Application session is being torn down. Abort event notify");
6253 goto error_rcu_unlock
;
6256 /* Depending on the buffer type, a different channel key is used. */
6257 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6258 chan_reg_key
= ua_chan
->tracing_channel_id
;
6260 chan_reg_key
= ua_chan
->key
;
6263 pthread_mutex_lock(®istry
->lock
);
6265 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
6266 LTTNG_ASSERT(ust_reg_chan
);
6268 if (!ust_reg_chan
->register_done
) {
6270 * TODO: eventually use the registry event count for
6271 * this channel to better guess header type for per-pid
6274 type
= LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
;
6275 ust_reg_chan
->nr_ctx_fields
= nr_fields
;
6276 ust_reg_chan
->ctx_fields
= fields
;
6278 ust_reg_chan
->header_type
= type
;
6280 /* Get current already assigned values. */
6281 type
= ust_reg_chan
->header_type
;
6283 /* Channel id is set during the object creation. */
6284 chan_id
= ust_reg_chan
->chan_id
;
6286 /* Append to metadata */
6287 if (!ust_reg_chan
->metadata_dumped
) {
6288 ret_code
= ust_metadata_channel_statedump(registry
, ust_reg_chan
);
6290 ERR("Error appending channel metadata (errno = %d)", ret_code
);
6296 DBG3("UST app replying to register channel key %" PRIu64
6297 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
6300 ret
= lttng_ust_ctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
6302 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6303 ERR("UST app reply channel failed with ret %d", ret
);
6305 DBG3("UST app reply channel failed. Application died");
6310 /* This channel registry registration is completed. */
6311 ust_reg_chan
->register_done
= 1;
6314 pthread_mutex_unlock(®istry
->lock
);
6322 * Add event to the UST channel registry. When the event is added to the
6323 * registry, the metadata is also created. Once done, this replies to the
6324 * application with the appropriate error code.
6326 * The session UST registry lock is acquired in the function.
6328 * On success 0 is returned else a negative value.
6330 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
6331 char *sig
, size_t nr_fields
, struct lttng_ust_ctl_field
*fields
,
6332 int loglevel_value
, char *model_emf_uri
)
6335 uint32_t event_id
= 0;
6336 uint64_t chan_reg_key
;
6337 struct ust_app
*app
;
6338 struct ust_app_channel
*ua_chan
;
6339 struct ust_app_session
*ua_sess
;
6340 struct ust_registry_session
*registry
;
6344 /* Lookup application. If not found, there is a code flow error. */
6345 app
= find_app_by_notify_sock(sock
);
6347 DBG("Application socket %d is being torn down. Abort event notify",
6350 goto error_rcu_unlock
;
6353 /* Lookup channel by UST object descriptor. */
6354 ua_chan
= find_channel_by_objd(app
, cobjd
);
6356 DBG("Application channel is being torn down. Abort event notify");
6358 goto error_rcu_unlock
;
6361 LTTNG_ASSERT(ua_chan
->session
);
6362 ua_sess
= ua_chan
->session
;
6364 registry
= get_session_registry(ua_sess
);
6366 DBG("Application session is being torn down. Abort event notify");
6368 goto error_rcu_unlock
;
6371 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6372 chan_reg_key
= ua_chan
->tracing_channel_id
;
6374 chan_reg_key
= ua_chan
->key
;
6377 pthread_mutex_lock(®istry
->lock
);
6380 * From this point on, this call acquires the ownership of the sig, fields
6381 * and model_emf_uri meaning any free are done inside it if needed. These
6382 * three variables MUST NOT be read/write after this.
6384 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
6385 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
6386 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
6390 model_emf_uri
= NULL
;
6393 * The return value is returned to ustctl so in case of an error, the
6394 * application can be notified. In case of an error, it's important not to
6395 * return a negative error or else the application will get closed.
6397 ret
= lttng_ust_ctl_reply_register_event(sock
, event_id
, ret_code
);
6399 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6400 ERR("UST app reply event failed with ret %d", ret
);
6402 DBG3("UST app reply event failed. Application died");
6405 * No need to wipe the create event since the application socket will
6406 * get close on error hence cleaning up everything by itself.
6411 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6415 pthread_mutex_unlock(®istry
->lock
);
6420 free(model_emf_uri
);
6425 * Add enum to the UST session registry. Once done, this replies to the
6426 * application with the appropriate error code.
6428 * The session UST registry lock is acquired within this function.
6430 * On success 0 is returned else a negative value.
6432 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
6433 struct lttng_ust_ctl_enum_entry
*entries
, size_t nr_entries
)
6435 int ret
= 0, ret_code
;
6436 struct ust_app
*app
;
6437 struct ust_app_session
*ua_sess
;
6438 struct ust_registry_session
*registry
;
6439 uint64_t enum_id
= -1ULL;
6443 /* Lookup application. If not found, there is a code flow error. */
6444 app
= find_app_by_notify_sock(sock
);
6446 /* Return an error since this is not an error */
6447 DBG("Application socket %d is being torn down. Aborting enum registration",
6450 goto error_rcu_unlock
;
6453 /* Lookup session by UST object descriptor. */
6454 ua_sess
= find_session_by_objd(app
, sobjd
);
6456 /* Return an error since this is not an error */
6457 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6459 goto error_rcu_unlock
;
6462 registry
= get_session_registry(ua_sess
);
6464 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6466 goto error_rcu_unlock
;
6469 pthread_mutex_lock(®istry
->lock
);
6472 * From this point on, the callee acquires the ownership of
6473 * entries. The variable entries MUST NOT be read/written after
6476 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
6477 entries
, nr_entries
, &enum_id
);
6481 * The return value is returned to ustctl so in case of an error, the
6482 * application can be notified. In case of an error, it's important not to
6483 * return a negative error or else the application will get closed.
6485 ret
= lttng_ust_ctl_reply_register_enum(sock
, enum_id
, ret_code
);
6487 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6488 ERR("UST app reply enum failed with ret %d", ret
);
6490 DBG3("UST app reply enum failed. Application died");
6493 * No need to wipe the create enum since the application socket will
6494 * get close on error hence cleaning up everything by itself.
6499 DBG3("UST registry enum %s added successfully or already found", name
);
6502 pthread_mutex_unlock(®istry
->lock
);
6509 * Handle application notification through the given notify socket.
6511 * Return 0 on success or else a negative value.
6513 int ust_app_recv_notify(int sock
)
6516 enum lttng_ust_ctl_notify_cmd cmd
;
6518 DBG3("UST app receiving notify from sock %d", sock
);
6520 ret
= lttng_ust_ctl_recv_notify(sock
, &cmd
);
6522 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6523 ERR("UST app recv notify failed with ret %d", ret
);
6525 DBG3("UST app recv notify failed. Application died");
6531 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT
:
6533 int sobjd
, cobjd
, loglevel_value
;
6534 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6536 struct lttng_ust_ctl_field
*fields
;
6538 DBG2("UST app ustctl register event received");
6540 ret
= lttng_ust_ctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
6541 &loglevel_value
, &sig
, &nr_fields
, &fields
,
6544 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6545 ERR("UST app recv event failed with ret %d", ret
);
6547 DBG3("UST app recv event failed. Application died");
6553 * Add event to the UST registry coming from the notify socket. This
6554 * call will free if needed the sig, fields and model_emf_uri. This
6555 * code path loses the ownsership of these variables and transfer them
6556 * to the this function.
6558 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6559 fields
, loglevel_value
, model_emf_uri
);
6566 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
:
6570 struct lttng_ust_ctl_field
*fields
;
6572 DBG2("UST app ustctl register channel received");
6574 ret
= lttng_ust_ctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
6577 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6578 ERR("UST app recv channel failed with ret %d", ret
);
6580 DBG3("UST app recv channel failed. Application died");
6586 * The fields ownership are transfered to this function call meaning
6587 * that if needed it will be freed. After this, it's invalid to access
6588 * fields or clean it up.
6590 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
6598 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM
:
6601 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
6603 struct lttng_ust_ctl_enum_entry
*entries
;
6605 DBG2("UST app ustctl register enum received");
6607 ret
= lttng_ust_ctl_recv_register_enum(sock
, &sobjd
, name
,
6608 &entries
, &nr_entries
);
6610 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6611 ERR("UST app recv enum failed with ret %d", ret
);
6613 DBG3("UST app recv enum failed. Application died");
6618 /* Callee assumes ownership of entries */
6619 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6620 entries
, nr_entries
);
6628 /* Should NEVER happen. */
6637 * Once the notify socket hangs up, this is called. First, it tries to find the
6638 * corresponding application. On failure, the call_rcu to close the socket is
6639 * executed. If an application is found, it tries to delete it from the notify
6640 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6642 * Note that an object needs to be allocated here so on ENOMEM failure, the
6643 * call RCU is not done but the rest of the cleanup is.
6645 void ust_app_notify_sock_unregister(int sock
)
6648 struct lttng_ht_iter iter
;
6649 struct ust_app
*app
;
6650 struct ust_app_notify_sock_obj
*obj
;
6652 LTTNG_ASSERT(sock
>= 0);
6656 obj
= zmalloc(sizeof(*obj
));
6659 * An ENOMEM is kind of uncool. If this strikes we continue the
6660 * procedure but the call_rcu will not be called. In this case, we
6661 * accept the fd leak rather than possibly creating an unsynchronized
6662 * state between threads.
6664 * TODO: The notify object should be created once the notify socket is
6665 * registered and stored independantely from the ust app object. The
6666 * tricky part is to synchronize the teardown of the application and
6667 * this notify object. Let's keep that in mind so we can avoid this
6668 * kind of shenanigans with ENOMEM in the teardown path.
6675 DBG("UST app notify socket unregister %d", sock
);
6678 * Lookup application by notify socket. If this fails, this means that the
6679 * hash table delete has already been done by the application
6680 * unregistration process so we can safely close the notify socket in a
6683 app
= find_app_by_notify_sock(sock
);
6688 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6691 * Whatever happens here either we fail or succeed, in both cases we have
6692 * to close the socket after a grace period to continue to the call RCU
6693 * here. If the deletion is successful, the application is not visible
6694 * anymore by other threads and is it fails it means that it was already
6695 * deleted from the hash table so either way we just have to close the
6698 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6704 * Close socket after a grace period to avoid for the socket to be reused
6705 * before the application object is freed creating potential race between
6706 * threads trying to add unique in the global hash table.
6709 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6714 * Destroy a ust app data structure and free its memory.
6716 void ust_app_destroy(struct ust_app
*app
)
6722 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6726 * Take a snapshot for a given UST session. The snapshot is sent to the given
6729 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6731 enum lttng_error_code
ust_app_snapshot_record(
6732 const struct ltt_ust_session
*usess
,
6733 const struct consumer_output
*output
, int wait
,
6734 uint64_t nb_packets_per_stream
)
6737 enum lttng_error_code status
= LTTNG_OK
;
6738 struct lttng_ht_iter iter
;
6739 struct ust_app
*app
;
6740 char *trace_path
= NULL
;
6742 LTTNG_ASSERT(usess
);
6743 LTTNG_ASSERT(output
);
6747 switch (usess
->buffer_type
) {
6748 case LTTNG_BUFFER_PER_UID
:
6750 struct buffer_reg_uid
*reg
;
6752 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6753 struct buffer_reg_channel
*buf_reg_chan
;
6754 struct consumer_socket
*socket
;
6755 char pathname
[PATH_MAX
];
6756 size_t consumer_path_offset
= 0;
6758 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6759 /* Skip since no metadata is present */
6763 /* Get consumer socket to use to push the metadata.*/
6764 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6767 status
= LTTNG_ERR_INVALID
;
6771 memset(pathname
, 0, sizeof(pathname
));
6772 ret
= snprintf(pathname
, sizeof(pathname
),
6773 DEFAULT_UST_TRACE_UID_PATH
,
6774 reg
->uid
, reg
->bits_per_long
);
6776 PERROR("snprintf snapshot path");
6777 status
= LTTNG_ERR_INVALID
;
6780 /* Free path allowed on previous iteration. */
6782 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6783 &consumer_path_offset
);
6785 status
= LTTNG_ERR_INVALID
;
6788 /* Add the UST default trace dir to path. */
6789 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6790 buf_reg_chan
, node
.node
) {
6791 status
= consumer_snapshot_channel(socket
,
6792 buf_reg_chan
->consumer_key
,
6793 output
, 0, usess
->uid
,
6794 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
6795 nb_packets_per_stream
);
6796 if (status
!= LTTNG_OK
) {
6800 status
= consumer_snapshot_channel(socket
,
6801 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6802 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6804 if (status
!= LTTNG_OK
) {
6810 case LTTNG_BUFFER_PER_PID
:
6812 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6813 struct consumer_socket
*socket
;
6814 struct lttng_ht_iter chan_iter
;
6815 struct ust_app_channel
*ua_chan
;
6816 struct ust_app_session
*ua_sess
;
6817 struct ust_registry_session
*registry
;
6818 char pathname
[PATH_MAX
];
6819 size_t consumer_path_offset
= 0;
6821 ua_sess
= lookup_session_by_app(usess
, app
);
6823 /* Session not associated with this app. */
6827 /* Get the right consumer socket for the application. */
6828 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6831 status
= LTTNG_ERR_INVALID
;
6835 /* Add the UST default trace dir to path. */
6836 memset(pathname
, 0, sizeof(pathname
));
6837 ret
= snprintf(pathname
, sizeof(pathname
), "%s",
6840 status
= LTTNG_ERR_INVALID
;
6841 PERROR("snprintf snapshot path");
6844 /* Free path allowed on previous iteration. */
6846 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6847 &consumer_path_offset
);
6849 status
= LTTNG_ERR_INVALID
;
6852 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6853 ua_chan
, node
.node
) {
6854 status
= consumer_snapshot_channel(socket
,
6855 ua_chan
->key
, output
, 0,
6856 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6857 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6858 &trace_path
[consumer_path_offset
], wait
,
6859 nb_packets_per_stream
);
6863 case LTTNG_ERR_CHAN_NOT_FOUND
:
6870 registry
= get_session_registry(ua_sess
);
6872 DBG("Application session is being torn down. Skip application.");
6875 status
= consumer_snapshot_channel(socket
,
6876 registry
->metadata_key
, output
, 1,
6877 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6878 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6879 &trace_path
[consumer_path_offset
], wait
, 0);
6883 case LTTNG_ERR_CHAN_NOT_FOUND
:
6903 * Return the size taken by one more packet per stream.
6905 uint64_t ust_app_get_size_one_more_packet_per_stream(
6906 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6908 uint64_t tot_size
= 0;
6909 struct ust_app
*app
;
6910 struct lttng_ht_iter iter
;
6912 LTTNG_ASSERT(usess
);
6914 switch (usess
->buffer_type
) {
6915 case LTTNG_BUFFER_PER_UID
:
6917 struct buffer_reg_uid
*reg
;
6919 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6920 struct buffer_reg_channel
*buf_reg_chan
;
6923 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6924 buf_reg_chan
, node
.node
) {
6925 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
6927 * Don't take channel into account if we
6928 * already grab all its packets.
6932 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
6938 case LTTNG_BUFFER_PER_PID
:
6941 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6942 struct ust_app_channel
*ua_chan
;
6943 struct ust_app_session
*ua_sess
;
6944 struct lttng_ht_iter chan_iter
;
6946 ua_sess
= lookup_session_by_app(usess
, app
);
6948 /* Session not associated with this app. */
6952 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6953 ua_chan
, node
.node
) {
6954 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6956 * Don't take channel into account if we
6957 * already grab all its packets.
6961 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6975 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6976 struct cds_list_head
*buffer_reg_uid_list
,
6977 struct consumer_output
*consumer
, uint64_t uchan_id
,
6978 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6981 uint64_t consumer_chan_key
;
6986 ret
= buffer_reg_uid_consumer_channel_key(
6987 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6995 ret
= consumer_get_lost_packets(ust_session_id
,
6996 consumer_chan_key
, consumer
, lost
);
6998 ret
= consumer_get_discarded_events(ust_session_id
,
6999 consumer_chan_key
, consumer
, discarded
);
7006 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
7007 struct ltt_ust_channel
*uchan
,
7008 struct consumer_output
*consumer
, int overwrite
,
7009 uint64_t *discarded
, uint64_t *lost
)
7012 struct lttng_ht_iter iter
;
7013 struct lttng_ht_node_str
*ua_chan_node
;
7014 struct ust_app
*app
;
7015 struct ust_app_session
*ua_sess
;
7016 struct ust_app_channel
*ua_chan
;
7023 * Iterate over every registered applications. Sum counters for
7024 * all applications containing requested session and channel.
7026 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7027 struct lttng_ht_iter uiter
;
7029 ua_sess
= lookup_session_by_app(usess
, app
);
7030 if (ua_sess
== NULL
) {
7035 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
7036 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
7037 /* If the session is found for the app, the channel must be there */
7038 LTTNG_ASSERT(ua_chan_node
);
7040 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
7045 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
7052 uint64_t _discarded
;
7054 ret
= consumer_get_discarded_events(usess
->id
,
7055 ua_chan
->key
, consumer
, &_discarded
);
7059 (*discarded
) += _discarded
;
7068 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
7069 struct ust_app
*app
)
7072 struct ust_app_session
*ua_sess
;
7074 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7078 ua_sess
= lookup_session_by_app(usess
, app
);
7079 if (ua_sess
== NULL
) {
7080 /* The session is in teardown process. Ignore and continue. */
7084 pthread_mutex_lock(&ua_sess
->lock
);
7086 if (ua_sess
->deleted
) {
7090 pthread_mutex_lock(&app
->sock_lock
);
7091 ret
= lttng_ust_ctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7092 pthread_mutex_unlock(&app
->sock_lock
);
7095 pthread_mutex_unlock(&ua_sess
->lock
);
7099 health_code_update();
7104 * Regenerate the statedump for each app in the session.
7106 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7109 struct lttng_ht_iter iter
;
7110 struct ust_app
*app
;
7112 DBG("Regenerating the metadata for all UST apps");
7116 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7117 if (!app
->compatible
) {
7121 ret
= ust_app_regenerate_statedump(usess
, app
);
7123 /* Continue to the next app even on error */
7134 * Rotate all the channels of a session.
7136 * Return LTTNG_OK on success or else an LTTng error code.
7138 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7141 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7142 struct lttng_ht_iter iter
;
7143 struct ust_app
*app
;
7144 struct ltt_ust_session
*usess
= session
->ust_session
;
7146 LTTNG_ASSERT(usess
);
7150 switch (usess
->buffer_type
) {
7151 case LTTNG_BUFFER_PER_UID
:
7153 struct buffer_reg_uid
*reg
;
7155 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7156 struct buffer_reg_channel
*buf_reg_chan
;
7157 struct consumer_socket
*socket
;
7159 /* Get consumer socket to use to push the metadata.*/
7160 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7163 cmd_ret
= LTTNG_ERR_INVALID
;
7167 /* Rotate the data channels. */
7168 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7169 buf_reg_chan
, node
.node
) {
7170 ret
= consumer_rotate_channel(socket
,
7171 buf_reg_chan
->consumer_key
,
7172 usess
->uid
, usess
->gid
,
7174 /* is_metadata_channel */ false);
7176 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7182 * The metadata channel might not be present.
7184 * Consumer stream allocation can be done
7185 * asynchronously and can fail on intermediary
7186 * operations (i.e add context) and lead to data
7187 * channels created with no metadata channel.
7189 if (!reg
->registry
->reg
.ust
->metadata_key
) {
7190 /* Skip since no metadata is present. */
7194 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7196 ret
= consumer_rotate_channel(socket
,
7197 reg
->registry
->reg
.ust
->metadata_key
,
7198 usess
->uid
, usess
->gid
,
7200 /* is_metadata_channel */ true);
7202 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7208 case LTTNG_BUFFER_PER_PID
:
7210 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7211 struct consumer_socket
*socket
;
7212 struct lttng_ht_iter chan_iter
;
7213 struct ust_app_channel
*ua_chan
;
7214 struct ust_app_session
*ua_sess
;
7215 struct ust_registry_session
*registry
;
7217 ua_sess
= lookup_session_by_app(usess
, app
);
7219 /* Session not associated with this app. */
7223 /* Get the right consumer socket for the application. */
7224 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7227 cmd_ret
= LTTNG_ERR_INVALID
;
7231 registry
= get_session_registry(ua_sess
);
7233 DBG("Application session is being torn down. Skip application.");
7237 /* Rotate the data channels. */
7238 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7239 ua_chan
, node
.node
) {
7240 ret
= consumer_rotate_channel(socket
,
7242 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7243 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7245 /* is_metadata_channel */ false);
7247 /* Per-PID buffer and application going away. */
7248 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7250 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7255 /* Rotate the metadata channel. */
7256 (void) push_metadata(registry
, usess
->consumer
);
7257 ret
= consumer_rotate_channel(socket
,
7258 registry
->metadata_key
,
7259 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7260 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7262 /* is_metadata_channel */ true);
7264 /* Per-PID buffer and application going away. */
7265 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7267 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7285 enum lttng_error_code
ust_app_create_channel_subdirectories(
7286 const struct ltt_ust_session
*usess
)
7288 enum lttng_error_code ret
= LTTNG_OK
;
7289 struct lttng_ht_iter iter
;
7290 enum lttng_trace_chunk_status chunk_status
;
7291 char *pathname_index
;
7294 LTTNG_ASSERT(usess
->current_trace_chunk
);
7297 switch (usess
->buffer_type
) {
7298 case LTTNG_BUFFER_PER_UID
:
7300 struct buffer_reg_uid
*reg
;
7302 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7303 fmt_ret
= asprintf(&pathname_index
,
7304 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7305 reg
->uid
, reg
->bits_per_long
);
7307 ERR("Failed to format channel index directory");
7308 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7313 * Create the index subdirectory which will take care
7314 * of implicitly creating the channel's path.
7316 chunk_status
= lttng_trace_chunk_create_subdirectory(
7317 usess
->current_trace_chunk
,
7319 free(pathname_index
);
7320 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7321 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7327 case LTTNG_BUFFER_PER_PID
:
7329 struct ust_app
*app
;
7332 * Create the toplevel ust/ directory in case no apps are running.
7334 chunk_status
= lttng_trace_chunk_create_subdirectory(
7335 usess
->current_trace_chunk
,
7336 DEFAULT_UST_TRACE_DIR
);
7337 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7338 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7342 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7344 struct ust_app_session
*ua_sess
;
7345 struct ust_registry_session
*registry
;
7347 ua_sess
= lookup_session_by_app(usess
, app
);
7349 /* Session not associated with this app. */
7353 registry
= get_session_registry(ua_sess
);
7355 DBG("Application session is being torn down. Skip application.");
7359 fmt_ret
= asprintf(&pathname_index
,
7360 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7363 ERR("Failed to format channel index directory");
7364 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7368 * Create the index subdirectory which will take care
7369 * of implicitly creating the channel's path.
7371 chunk_status
= lttng_trace_chunk_create_subdirectory(
7372 usess
->current_trace_chunk
,
7374 free(pathname_index
);
7375 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7376 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7393 * Clear all the channels of a session.
7395 * Return LTTNG_OK on success or else an LTTng error code.
7397 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7400 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7401 struct lttng_ht_iter iter
;
7402 struct ust_app
*app
;
7403 struct ltt_ust_session
*usess
= session
->ust_session
;
7405 LTTNG_ASSERT(usess
);
7409 if (usess
->active
) {
7410 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7411 cmd_ret
= LTTNG_ERR_FATAL
;
7415 switch (usess
->buffer_type
) {
7416 case LTTNG_BUFFER_PER_UID
:
7418 struct buffer_reg_uid
*reg
;
7420 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7421 struct buffer_reg_channel
*buf_reg_chan
;
7422 struct consumer_socket
*socket
;
7424 /* Get consumer socket to use to push the metadata.*/
7425 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7428 cmd_ret
= LTTNG_ERR_INVALID
;
7432 /* Clear the data channels. */
7433 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7434 buf_reg_chan
, node
.node
) {
7435 ret
= consumer_clear_channel(socket
,
7436 buf_reg_chan
->consumer_key
);
7442 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7445 * Clear the metadata channel.
7446 * Metadata channel is not cleared per se but we still need to
7447 * perform a rotation operation on it behind the scene.
7449 ret
= consumer_clear_channel(socket
,
7450 reg
->registry
->reg
.ust
->metadata_key
);
7457 case LTTNG_BUFFER_PER_PID
:
7459 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7460 struct consumer_socket
*socket
;
7461 struct lttng_ht_iter chan_iter
;
7462 struct ust_app_channel
*ua_chan
;
7463 struct ust_app_session
*ua_sess
;
7464 struct ust_registry_session
*registry
;
7466 ua_sess
= lookup_session_by_app(usess
, app
);
7468 /* Session not associated with this app. */
7472 /* Get the right consumer socket for the application. */
7473 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7476 cmd_ret
= LTTNG_ERR_INVALID
;
7480 registry
= get_session_registry(ua_sess
);
7482 DBG("Application session is being torn down. Skip application.");
7486 /* Clear the data channels. */
7487 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7488 ua_chan
, node
.node
) {
7489 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7491 /* Per-PID buffer and application going away. */
7492 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7499 (void) push_metadata(registry
, usess
->consumer
);
7502 * Clear the metadata channel.
7503 * Metadata channel is not cleared per se but we still need to
7504 * perform rotation operation on it behind the scene.
7506 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
7508 /* Per-PID buffer and application going away. */
7509 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7527 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7528 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7531 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7541 * This function skips the metadata channel as the begin/end timestamps of a
7542 * metadata packet are useless.
7544 * Moreover, opening a packet after a "clear" will cause problems for live
7545 * sessions as it will introduce padding that was not part of the first trace
7546 * chunk. The relay daemon expects the content of the metadata stream of
7547 * successive metadata trace chunks to be strict supersets of one another.
7549 * For example, flushing a packet at the beginning of the metadata stream of
7550 * a trace chunk resulting from a "clear" session command will cause the
7551 * size of the metadata stream of the new trace chunk to not match the size of
7552 * the metadata stream of the original chunk. This will confuse the relay
7553 * daemon as the same "offset" in a metadata stream will no longer point
7554 * to the same content.
7556 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7558 enum lttng_error_code ret
= LTTNG_OK
;
7559 struct lttng_ht_iter iter
;
7560 struct ltt_ust_session
*usess
= session
->ust_session
;
7562 LTTNG_ASSERT(usess
);
7566 switch (usess
->buffer_type
) {
7567 case LTTNG_BUFFER_PER_UID
:
7569 struct buffer_reg_uid
*reg
;
7571 cds_list_for_each_entry (
7572 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7573 struct buffer_reg_channel
*buf_reg_chan
;
7574 struct consumer_socket
*socket
;
7576 socket
= consumer_find_socket_by_bitness(
7577 reg
->bits_per_long
, usess
->consumer
);
7579 ret
= LTTNG_ERR_FATAL
;
7583 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7584 &iter
.iter
, buf_reg_chan
, node
.node
) {
7585 const int open_ret
=
7586 consumer_open_channel_packets(
7588 buf_reg_chan
->consumer_key
);
7591 ret
= LTTNG_ERR_UNK
;
7598 case LTTNG_BUFFER_PER_PID
:
7600 struct ust_app
*app
;
7602 cds_lfht_for_each_entry (
7603 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7604 struct consumer_socket
*socket
;
7605 struct lttng_ht_iter chan_iter
;
7606 struct ust_app_channel
*ua_chan
;
7607 struct ust_app_session
*ua_sess
;
7608 struct ust_registry_session
*registry
;
7610 ua_sess
= lookup_session_by_app(usess
, app
);
7612 /* Session not associated with this app. */
7616 /* Get the right consumer socket for the application. */
7617 socket
= consumer_find_socket_by_bitness(
7618 app
->bits_per_long
, usess
->consumer
);
7620 ret
= LTTNG_ERR_FATAL
;
7624 registry
= get_session_registry(ua_sess
);
7626 DBG("Application session is being torn down. Skip application.");
7630 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
7631 &chan_iter
.iter
, ua_chan
, node
.node
) {
7632 const int open_ret
=
7633 consumer_open_channel_packets(
7639 * Per-PID buffer and application going
7642 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7646 ret
= LTTNG_ERR_UNK
;