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
;
199 assert(ua_chan
->events
);
202 ht
= ua_chan
->events
;
203 key
.name
= event
->attr
.name
;
204 key
.filter
= event
->filter
;
205 key
.loglevel_type
= event
->attr
.loglevel
;
206 key
.exclusion
= event
->exclusion
;
208 node_ptr
= cds_lfht_add_unique(ht
->ht
,
209 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
210 ht_match_ust_app_event
, &key
, &event
->node
.node
);
211 assert(node_ptr
== &event
->node
.node
);
215 * Close the notify socket from the given RCU head object. This MUST be called
216 * through a call_rcu().
218 static void close_notify_sock_rcu(struct rcu_head
*head
)
221 struct ust_app_notify_sock_obj
*obj
=
222 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
224 /* Must have a valid fd here. */
225 assert(obj
->fd
>= 0);
227 ret
= close(obj
->fd
);
229 ERR("close notify sock %d RCU", obj
->fd
);
231 lttng_fd_put(LTTNG_FD_APPS
, 1);
237 * Return the session registry according to the buffer type of the given
240 * A registry per UID object MUST exists before calling this function or else
241 * it assert() if not found. RCU read side lock must be acquired.
243 static struct ust_registry_session
*get_session_registry(
244 struct ust_app_session
*ua_sess
)
246 struct ust_registry_session
*registry
= NULL
;
250 switch (ua_sess
->buffer_type
) {
251 case LTTNG_BUFFER_PER_PID
:
253 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
257 registry
= reg_pid
->registry
->reg
.ust
;
260 case LTTNG_BUFFER_PER_UID
:
262 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
263 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
264 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
268 registry
= reg_uid
->registry
->reg
.ust
;
280 * Delete ust context safely. RCU read lock must be held before calling
284 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
292 pthread_mutex_lock(&app
->sock_lock
);
293 ret
= 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
,
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 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
,
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
,
414 (void) release_ust_app_stream(sock
, stream
, app
);
419 * We need to execute ht_destroy outside of RCU read-side critical
420 * section and outside of call_rcu thread, so we postpone its execution
421 * using ht_cleanup_push. It is simpler than to change the semantic of
422 * the many callers of delete_ust_app_session().
425 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
427 struct ust_app_channel
*ua_chan
=
428 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
430 ht_cleanup_push(ua_chan
->ctx
);
431 ht_cleanup_push(ua_chan
->events
);
436 * Extract the lost packet or discarded events counter when the channel is
437 * being deleted and store the value in the parent channel so we can
438 * access it from lttng list and at stop/destroy.
440 * The session list lock must be held by the caller.
443 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
445 uint64_t discarded
= 0, lost
= 0;
446 struct ltt_session
*session
;
447 struct ltt_ust_channel
*uchan
;
449 if (ua_chan
->attr
.type
!= LTTNG_UST_ABI_CHAN_PER_CPU
) {
454 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
455 if (!session
|| !session
->ust_session
) {
457 * Not finding the session is not an error because there are
458 * multiple ways the channels can be torn down.
460 * 1) The session daemon can initiate the destruction of the
461 * ust app session after receiving a destroy command or
462 * during its shutdown/teardown.
463 * 2) The application, since we are in per-pid tracing, is
464 * unregistering and tearing down its ust app session.
466 * Both paths are protected by the session list lock which
467 * ensures that the accounting of lost packets and discarded
468 * events is done exactly once. The session is then unpublished
469 * from the session list, resulting in this condition.
474 if (ua_chan
->attr
.overwrite
) {
475 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
476 ua_chan
->key
, session
->ust_session
->consumer
,
479 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
480 ua_chan
->key
, session
->ust_session
->consumer
,
483 uchan
= trace_ust_find_channel_by_name(
484 session
->ust_session
->domain_global
.channels
,
487 ERR("Missing UST channel to store discarded counters");
491 uchan
->per_pid_closed_app_discarded
+= discarded
;
492 uchan
->per_pid_closed_app_lost
+= lost
;
497 session_put(session
);
502 * Delete ust app channel safely. RCU read lock must be held before calling
505 * The session list lock must be held by the caller.
508 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
512 struct lttng_ht_iter iter
;
513 struct ust_app_event
*ua_event
;
514 struct ust_app_ctx
*ua_ctx
;
515 struct ust_app_stream
*stream
, *stmp
;
516 struct ust_registry_session
*registry
;
520 DBG3("UST app deleting channel %s", ua_chan
->name
);
523 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
524 cds_list_del(&stream
->list
);
525 delete_ust_app_stream(sock
, stream
, app
);
529 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
530 cds_list_del(&ua_ctx
->list
);
531 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
533 delete_ust_app_ctx(sock
, ua_ctx
, app
);
537 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
539 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
541 delete_ust_app_event(sock
, ua_event
, app
);
544 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
545 /* Wipe and free registry from session registry. */
546 registry
= get_session_registry(ua_chan
->session
);
548 ust_registry_channel_del_free(registry
, ua_chan
->key
,
552 * A negative socket can be used by the caller when
553 * cleaning-up a ua_chan in an error path. Skip the
554 * accounting in this case.
557 save_per_pid_lost_discarded_counters(ua_chan
);
561 if (ua_chan
->obj
!= NULL
) {
562 /* Remove channel from application UST object descriptor. */
563 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
564 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
566 pthread_mutex_lock(&app
->sock_lock
);
567 ret
= 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
;
630 metadata_key
= registry
->metadata_key
;
633 * Means that no metadata was assigned to the session. This can
634 * happens if no start has been done previously.
640 offset
= registry
->metadata_len_sent
;
641 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
642 new_metadata_len_sent
= registry
->metadata_len
;
643 metadata_version
= registry
->metadata_version
;
645 DBG3("No metadata to push for metadata key %" PRIu64
,
646 registry
->metadata_key
);
648 if (send_zero_data
) {
649 DBG("No metadata to push");
655 /* Allocate only what we have to send. */
656 metadata_str
= zmalloc(len
);
658 PERROR("zmalloc ust app metadata string");
662 /* Copy what we haven't sent out. */
663 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
666 pthread_mutex_unlock(®istry
->lock
);
668 * We need to unlock the registry while we push metadata to
669 * break a circular dependency between the consumerd metadata
670 * lock and the sessiond registry lock. Indeed, pushing metadata
671 * to the consumerd awaits that it gets pushed all the way to
672 * relayd, but doing so requires grabbing the metadata lock. If
673 * a concurrent metadata request is being performed by
674 * consumerd, this can try to grab the registry lock on the
675 * sessiond while holding the metadata lock on the consumer
676 * daemon. Those push and pull schemes are performed on two
677 * different bidirectionnal communication sockets.
679 ret
= consumer_push_metadata(socket
, metadata_key
,
680 metadata_str
, len
, offset
, metadata_version
);
681 pthread_mutex_lock(®istry
->lock
);
684 * There is an acceptable race here between the registry
685 * metadata key assignment and the creation on the
686 * consumer. The session daemon can concurrently push
687 * metadata for this registry while being created on the
688 * consumer since the metadata key of the registry is
689 * assigned *before* it is setup to avoid the consumer
690 * to ask for metadata that could possibly be not found
691 * in the session daemon.
693 * The metadata will get pushed either by the session
694 * being stopped or the consumer requesting metadata if
695 * that race is triggered.
697 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
700 ERR("Error pushing metadata to consumer");
706 * Metadata may have been concurrently pushed, since
707 * we're not holding the registry lock while pushing to
708 * consumer. This is handled by the fact that we send
709 * the metadata content, size, and the offset at which
710 * that metadata belongs. This may arrive out of order
711 * on the consumer side, and the consumer is able to
712 * deal with overlapping fragments. The consumer
713 * supports overlapping fragments, which must be
714 * contiguous starting from offset 0. We keep the
715 * largest metadata_len_sent value of the concurrent
718 registry
->metadata_len_sent
=
719 max_t(size_t, registry
->metadata_len_sent
,
720 new_metadata_len_sent
);
729 * On error, flag the registry that the metadata is
730 * closed. We were unable to push anything and this
731 * means that either the consumer is not responding or
732 * the metadata cache has been destroyed on the
735 registry
->metadata_closed
= 1;
743 * For a given application and session, push metadata to consumer.
744 * Either sock or consumer is required : if sock is NULL, the default
745 * socket to send the metadata is retrieved from consumer, if sock
746 * is not NULL we use it to send the metadata.
747 * RCU read-side lock must be held while calling this function,
748 * therefore ensuring existance of registry. It also ensures existance
749 * of socket throughout this function.
751 * Return 0 on success else a negative error.
752 * Returning a -EPIPE return value means we could not send the metadata,
753 * but it can be caused by recoverable errors (e.g. the application has
754 * terminated concurrently).
756 static int push_metadata(struct ust_registry_session
*registry
,
757 struct consumer_output
*consumer
)
761 struct consumer_socket
*socket
;
766 pthread_mutex_lock(®istry
->lock
);
767 if (registry
->metadata_closed
) {
772 /* Get consumer socket to use to push the metadata.*/
773 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
780 ret
= ust_app_push_metadata(registry
, socket
, 0);
785 pthread_mutex_unlock(®istry
->lock
);
789 pthread_mutex_unlock(®istry
->lock
);
794 * Send to the consumer a close metadata command for the given session. Once
795 * done, the metadata channel is deleted and the session metadata pointer is
796 * nullified. The session lock MUST be held unless the application is
797 * in the destroy path.
799 * Do not hold the registry lock while communicating with the consumerd, because
800 * doing so causes inter-process deadlocks between consumerd and sessiond with
801 * the metadata request notification.
803 * Return 0 on success else a negative value.
805 static int close_metadata(struct ust_registry_session
*registry
,
806 struct consumer_output
*consumer
)
809 struct consumer_socket
*socket
;
810 uint64_t metadata_key
;
811 bool registry_was_already_closed
;
818 pthread_mutex_lock(®istry
->lock
);
819 metadata_key
= registry
->metadata_key
;
820 registry_was_already_closed
= registry
->metadata_closed
;
821 if (metadata_key
!= 0) {
823 * Metadata closed. Even on error this means that the consumer
824 * is not responding or not found so either way a second close
825 * should NOT be emit for this registry.
827 registry
->metadata_closed
= 1;
829 pthread_mutex_unlock(®istry
->lock
);
831 if (metadata_key
== 0 || registry_was_already_closed
) {
836 /* Get consumer socket to use to push the metadata.*/
837 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
844 ret
= consumer_close_metadata(socket
, metadata_key
);
855 * We need to execute ht_destroy outside of RCU read-side critical
856 * section and outside of call_rcu thread, so we postpone its execution
857 * using ht_cleanup_push. It is simpler than to change the semantic of
858 * the many callers of delete_ust_app_session().
861 void delete_ust_app_session_rcu(struct rcu_head
*head
)
863 struct ust_app_session
*ua_sess
=
864 caa_container_of(head
, struct ust_app_session
, rcu_head
);
866 ht_cleanup_push(ua_sess
->channels
);
871 * Delete ust app session safely. RCU read lock must be held before calling
874 * The session list lock must be held by the caller.
877 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
881 struct lttng_ht_iter iter
;
882 struct ust_app_channel
*ua_chan
;
883 struct ust_registry_session
*registry
;
887 pthread_mutex_lock(&ua_sess
->lock
);
889 assert(!ua_sess
->deleted
);
890 ua_sess
->deleted
= true;
892 registry
= get_session_registry(ua_sess
);
893 /* Registry can be null on error path during initialization. */
895 /* Push metadata for application before freeing the application. */
896 (void) push_metadata(registry
, ua_sess
->consumer
);
899 * Don't ask to close metadata for global per UID buffers. Close
900 * metadata only on destroy trace session in this case. Also, the
901 * previous push metadata could have flag the metadata registry to
902 * close so don't send a close command if closed.
904 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
905 /* And ask to close it for this session registry. */
906 (void) close_metadata(registry
, ua_sess
->consumer
);
910 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
912 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
914 delete_ust_app_channel(sock
, ua_chan
, app
);
917 /* In case of per PID, the registry is kept in the session. */
918 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
919 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
922 * Registry can be null on error path during
925 buffer_reg_pid_remove(reg_pid
);
926 buffer_reg_pid_destroy(reg_pid
);
930 if (ua_sess
->handle
!= -1) {
931 pthread_mutex_lock(&app
->sock_lock
);
932 ret
= 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
;
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 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1253 struct lttng_condition
*condition
= NULL
;
1254 const struct lttng_event_rule
*event_rule
= NULL
;
1256 ua_event_notifier_rule
= zmalloc(sizeof(struct ust_app_event_notifier_rule
));
1257 if (ua_event_notifier_rule
== NULL
) {
1258 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1262 ua_event_notifier_rule
->enabled
= 1;
1263 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1264 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
,
1265 ua_event_notifier_rule
->token
);
1267 condition
= lttng_trigger_get_condition(trigger
);
1269 assert(lttng_condition_get_type(condition
) ==
1270 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1272 assert(LTTNG_CONDITION_STATUS_OK
==
1273 lttng_condition_event_rule_matches_get_rule(
1274 condition
, &event_rule
));
1277 ua_event_notifier_rule
->error_counter_index
=
1278 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
1279 /* Acquire the event notifier's reference to the trigger. */
1280 lttng_trigger_get(trigger
);
1282 ua_event_notifier_rule
->trigger
= trigger
;
1283 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1284 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1285 event_rule
, &ua_event_notifier_rule
->exclusion
);
1286 switch (generate_exclusion_status
) {
1287 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1288 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1291 /* Error occured. */
1292 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1293 goto error_put_trigger
;
1296 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1297 ua_event_notifier_rule
->token
);
1299 return ua_event_notifier_rule
;
1302 lttng_trigger_put(trigger
);
1304 free(ua_event_notifier_rule
);
1309 * Alloc new UST app context.
1312 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1314 struct ust_app_ctx
*ua_ctx
;
1316 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1317 if (ua_ctx
== NULL
) {
1321 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1324 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1325 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1326 char *provider_name
= NULL
, *ctx_name
= NULL
;
1328 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1329 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1330 if (!provider_name
|| !ctx_name
) {
1331 free(provider_name
);
1336 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1337 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1341 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1349 * Create a liblttng-ust filter bytecode from given bytecode.
1351 * Return allocated filter or NULL on error.
1353 static struct lttng_ust_abi_filter_bytecode
*create_ust_filter_bytecode_from_bytecode(
1354 const struct lttng_bytecode
*orig_f
)
1356 struct lttng_ust_abi_filter_bytecode
*filter
= NULL
;
1358 /* Copy filter bytecode. */
1359 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1361 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1365 assert(sizeof(struct lttng_bytecode
) ==
1366 sizeof(struct lttng_ust_abi_filter_bytecode
));
1367 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1373 * Create a liblttng-ust capture bytecode from given bytecode.
1375 * Return allocated filter or NULL on error.
1377 static struct lttng_ust_abi_capture_bytecode
*
1378 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1380 struct lttng_ust_abi_capture_bytecode
*capture
= NULL
;
1382 /* Copy capture bytecode. */
1383 capture
= zmalloc(sizeof(*capture
) + orig_f
->len
);
1385 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1389 assert(sizeof(struct lttng_bytecode
) ==
1390 sizeof(struct lttng_ust_abi_capture_bytecode
));
1391 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1397 * Find an ust_app using the sock and return it. RCU read side lock must be
1398 * held before calling this helper function.
1400 struct ust_app
*ust_app_find_by_sock(int sock
)
1402 struct lttng_ht_node_ulong
*node
;
1403 struct lttng_ht_iter iter
;
1405 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1406 node
= lttng_ht_iter_get_node_ulong(&iter
);
1408 DBG2("UST app find by sock %d not found", sock
);
1412 return caa_container_of(node
, struct ust_app
, sock_n
);
1419 * Find an ust_app using the notify sock and return it. RCU read side lock must
1420 * be held before calling this helper function.
1422 static struct ust_app
*find_app_by_notify_sock(int sock
)
1424 struct lttng_ht_node_ulong
*node
;
1425 struct lttng_ht_iter iter
;
1427 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1429 node
= lttng_ht_iter_get_node_ulong(&iter
);
1431 DBG2("UST app find by notify sock %d not found", sock
);
1435 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1442 * Lookup for an ust app event based on event name, filter bytecode and the
1445 * Return an ust_app_event object or NULL on error.
1447 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1448 const char *name
, const struct lttng_bytecode
*filter
,
1450 const struct lttng_event_exclusion
*exclusion
)
1452 struct lttng_ht_iter iter
;
1453 struct lttng_ht_node_str
*node
;
1454 struct ust_app_event
*event
= NULL
;
1455 struct ust_app_ht_key key
;
1460 /* Setup key for event lookup. */
1462 key
.filter
= filter
;
1463 key
.loglevel_type
= loglevel_value
;
1464 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1465 key
.exclusion
= exclusion
;
1467 /* Lookup using the event name as hash and a custom match fct. */
1468 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1469 ht_match_ust_app_event
, &key
, &iter
.iter
);
1470 node
= lttng_ht_iter_get_node_str(&iter
);
1475 event
= caa_container_of(node
, struct ust_app_event
, node
);
1482 * Look-up an event notifier rule based on its token id.
1484 * Must be called with the RCU read lock held.
1485 * Return an ust_app_event_notifier_rule object or NULL on error.
1487 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(
1488 struct lttng_ht
*ht
, uint64_t token
)
1490 struct lttng_ht_iter iter
;
1491 struct lttng_ht_node_u64
*node
;
1492 struct ust_app_event_notifier_rule
*event_notifier_rule
= NULL
;
1496 lttng_ht_lookup(ht
, &token
, &iter
);
1497 node
= lttng_ht_iter_get_node_u64(&iter
);
1499 DBG2("UST app event notifier rule token not found: token = %" PRIu64
,
1504 event_notifier_rule
= caa_container_of(
1505 node
, struct ust_app_event_notifier_rule
, node
);
1507 return event_notifier_rule
;
1511 * Create the channel context on the tracer.
1513 * Called with UST app session lock held.
1516 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1517 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1521 health_code_update();
1523 pthread_mutex_lock(&app
->sock_lock
);
1524 ret
= lttng_ust_ctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1525 ua_chan
->obj
, &ua_ctx
->obj
);
1526 pthread_mutex_unlock(&app
->sock_lock
);
1528 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1529 ERR("UST app create channel context failed for app (pid: %d) "
1530 "with ret %d", app
->pid
, ret
);
1533 * This is normal behavior, an application can die during the
1534 * creation process. Don't report an error so the execution can
1535 * continue normally.
1538 DBG3("UST app add context failed. Application is dead.");
1543 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1545 DBG2("UST app context handle %d created successfully for channel %s",
1546 ua_ctx
->handle
, ua_chan
->name
);
1549 health_code_update();
1554 * Set the filter on the tracer.
1556 static int set_ust_object_filter(struct ust_app
*app
,
1557 const struct lttng_bytecode
*bytecode
,
1558 struct lttng_ust_abi_object_data
*ust_object
)
1561 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= NULL
;
1563 health_code_update();
1565 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1566 if (!ust_bytecode
) {
1567 ret
= -LTTNG_ERR_NOMEM
;
1570 pthread_mutex_lock(&app
->sock_lock
);
1571 ret
= lttng_ust_ctl_set_filter(app
->sock
, ust_bytecode
,
1573 pthread_mutex_unlock(&app
->sock_lock
);
1575 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1576 ERR("UST app set object filter failed: object = %p of app pid = %d, ret = %d",
1577 ust_object
, app
->pid
, ret
);
1580 * This is normal behavior, an application can die during the
1581 * creation process. Don't report an error so the execution can
1582 * continue normally.
1585 DBG3("Failed to set UST app object filter. Application is dead.");
1590 DBG2("UST filter successfully set: object = %p", ust_object
);
1593 health_code_update();
1599 * Set a capture bytecode for the passed object.
1600 * The sequence number enforces the ordering at runtime and on reception of
1601 * the captured payloads.
1603 static int set_ust_capture(struct ust_app
*app
,
1604 const struct lttng_bytecode
*bytecode
,
1605 unsigned int capture_seqnum
,
1606 struct lttng_ust_abi_object_data
*ust_object
)
1609 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= NULL
;
1611 health_code_update();
1613 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1614 if (!ust_bytecode
) {
1615 ret
= -LTTNG_ERR_NOMEM
;
1620 * Set the sequence number to ensure the capture of fields is ordered.
1622 ust_bytecode
->seqnum
= capture_seqnum
;
1624 pthread_mutex_lock(&app
->sock_lock
);
1625 ret
= lttng_ust_ctl_set_capture(app
->sock
, ust_bytecode
,
1627 pthread_mutex_unlock(&app
->sock_lock
);
1629 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1630 ERR("UST app set object capture failed: object = %p of app pid = %d, ret = %d",
1631 ust_object
, app
->pid
, ret
);
1634 * This is normal behavior, an application can die during the
1635 * creation process. Don't report an error so the execution can
1636 * continue normally.
1639 DBG3("Failed to set UST app object capture. Application is dead.");
1645 DBG2("UST capture successfully set: object = %p", ust_object
);
1648 health_code_update();
1654 struct lttng_ust_abi_event_exclusion
*create_ust_exclusion_from_exclusion(
1655 const struct lttng_event_exclusion
*exclusion
)
1657 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= NULL
;
1658 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1659 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1661 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1662 if (!ust_exclusion
) {
1667 assert(sizeof(struct lttng_event_exclusion
) ==
1668 sizeof(struct lttng_ust_abi_event_exclusion
));
1669 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1671 return ust_exclusion
;
1675 * Set event exclusions on the tracer.
1677 static int set_ust_object_exclusions(struct ust_app
*app
,
1678 const struct lttng_event_exclusion
*exclusions
,
1679 struct lttng_ust_abi_object_data
*ust_object
)
1682 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= NULL
;
1684 assert(exclusions
&& exclusions
->count
> 0);
1686 health_code_update();
1688 ust_exclusions
= create_ust_exclusion_from_exclusion(
1690 if (!ust_exclusions
) {
1691 ret
= -LTTNG_ERR_NOMEM
;
1694 pthread_mutex_lock(&app
->sock_lock
);
1695 ret
= lttng_ust_ctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1696 pthread_mutex_unlock(&app
->sock_lock
);
1698 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1699 ERR("Failed to set UST app exclusions for object %p of app (pid: %d) "
1700 "with ret %d", ust_object
, app
->pid
, ret
);
1703 * This is normal behavior, an application can die during the
1704 * creation process. Don't report an error so the execution can
1705 * continue normally.
1708 DBG3("Failed to set UST app object exclusions. Application is dead.");
1713 DBG2("UST exclusions set successfully for object %p", ust_object
);
1716 health_code_update();
1717 free(ust_exclusions
);
1722 * Disable the specified event on to UST tracer for the UST session.
1724 static int disable_ust_object(struct ust_app
*app
,
1725 struct lttng_ust_abi_object_data
*object
)
1729 health_code_update();
1731 pthread_mutex_lock(&app
->sock_lock
);
1732 ret
= lttng_ust_ctl_disable(app
->sock
, object
);
1733 pthread_mutex_unlock(&app
->sock_lock
);
1735 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1736 ERR("Failed to disable UST app object %p app (pid: %d) with ret %d",
1737 object
, app
->pid
, ret
);
1740 * This is normal behavior, an application can die during the
1741 * creation process. Don't report an error so the execution can
1742 * continue normally.
1745 DBG3("Failed to disable UST app object. Application is dead.");
1750 DBG2("UST app object %p disabled successfully for app (pid: %d)",
1754 health_code_update();
1759 * Disable the specified channel on to UST tracer for the UST session.
1761 static int disable_ust_channel(struct ust_app
*app
,
1762 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1766 health_code_update();
1768 pthread_mutex_lock(&app
->sock_lock
);
1769 ret
= lttng_ust_ctl_disable(app
->sock
, ua_chan
->obj
);
1770 pthread_mutex_unlock(&app
->sock_lock
);
1772 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1773 ERR("UST app channel %s disable failed for app (pid: %d) "
1774 "and session handle %d with ret %d",
1775 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1778 * This is normal behavior, an application can die during the
1779 * creation process. Don't report an error so the execution can
1780 * continue normally.
1783 DBG3("UST app disable channel failed. Application is dead.");
1788 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1789 ua_chan
->name
, app
->pid
);
1792 health_code_update();
1797 * Enable the specified channel on to UST tracer for the UST session.
1799 static int enable_ust_channel(struct ust_app
*app
,
1800 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1804 health_code_update();
1806 pthread_mutex_lock(&app
->sock_lock
);
1807 ret
= lttng_ust_ctl_enable(app
->sock
, ua_chan
->obj
);
1808 pthread_mutex_unlock(&app
->sock_lock
);
1810 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1811 ERR("UST app channel %s enable failed for app (pid: %d) "
1812 "and session handle %d with ret %d",
1813 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1816 * This is normal behavior, an application can die during the
1817 * creation process. Don't report an error so the execution can
1818 * continue normally.
1821 DBG3("UST app enable channel failed. Application is dead.");
1826 ua_chan
->enabled
= 1;
1828 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1829 ua_chan
->name
, app
->pid
);
1832 health_code_update();
1837 * Enable the specified event on to UST tracer for the UST session.
1839 static int enable_ust_object(
1840 struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1844 health_code_update();
1846 pthread_mutex_lock(&app
->sock_lock
);
1847 ret
= lttng_ust_ctl_enable(app
->sock
, ust_object
);
1848 pthread_mutex_unlock(&app
->sock_lock
);
1850 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1851 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
1852 ust_object
, app
->pid
, ret
);
1855 * This is normal behavior, an application can die during the
1856 * creation process. Don't report an error so the execution can
1857 * continue normally.
1860 DBG3("Failed to enable UST app object. Application is dead.");
1865 DBG2("UST app object %p enabled successfully for app (pid: %d)",
1866 ust_object
, app
->pid
);
1869 health_code_update();
1874 * Send channel and stream buffer to application.
1876 * Return 0 on success. On error, a negative value is returned.
1878 static int send_channel_pid_to_ust(struct ust_app
*app
,
1879 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1882 struct ust_app_stream
*stream
, *stmp
;
1888 health_code_update();
1890 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1893 /* Send channel to the application. */
1894 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1895 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1896 ret
= -ENOTCONN
; /* Caused by app exiting. */
1898 } else if (ret
< 0) {
1902 health_code_update();
1904 /* Send all streams to application. */
1905 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1906 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1907 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1908 ret
= -ENOTCONN
; /* Caused by app exiting. */
1910 } else if (ret
< 0) {
1913 /* We don't need the stream anymore once sent to the tracer. */
1914 cds_list_del(&stream
->list
);
1915 delete_ust_app_stream(-1, stream
, app
);
1917 /* Flag the channel that it is sent to the application. */
1918 ua_chan
->is_sent
= 1;
1921 health_code_update();
1926 * Create the specified event onto the UST tracer for a UST session.
1928 * Should be called with session mutex held.
1931 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1932 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1936 health_code_update();
1938 /* Create UST event on tracer */
1939 pthread_mutex_lock(&app
->sock_lock
);
1940 ret
= lttng_ust_ctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1942 pthread_mutex_unlock(&app
->sock_lock
);
1944 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1946 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1947 ua_event
->attr
.name
, app
->pid
, ret
);
1950 * This is normal behavior, an application can die during the
1951 * creation process. Don't report an error so the execution can
1952 * continue normally.
1955 DBG3("UST app create event failed. Application is dead.");
1960 ua_event
->handle
= ua_event
->obj
->handle
;
1962 DBG2("UST app event %s created successfully for pid:%d object: %p",
1963 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
1965 health_code_update();
1967 /* Set filter if one is present. */
1968 if (ua_event
->filter
) {
1969 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
1975 /* Set exclusions for the event */
1976 if (ua_event
->exclusion
) {
1977 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
1983 /* If event not enabled, disable it on the tracer */
1984 if (ua_event
->enabled
) {
1986 * We now need to explicitly enable the event, since it
1987 * is now disabled at creation.
1989 ret
= enable_ust_object(app
, ua_event
->obj
);
1992 * If we hit an EPERM, something is wrong with our enable call. If
1993 * we get an EEXIST, there is a problem on the tracer side since we
1997 case -LTTNG_UST_ERR_PERM
:
1998 /* Code flow problem */
2000 case -LTTNG_UST_ERR_EXIST
:
2001 /* It's OK for our use case. */
2012 health_code_update();
2016 static int init_ust_event_notifier_from_event_rule(
2017 const struct lttng_event_rule
*rule
,
2018 struct lttng_ust_abi_event_notifier
*event_notifier
)
2020 enum lttng_event_rule_status status
;
2021 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2022 int loglevel
= -1, ret
= 0;
2023 const char *pattern
;
2026 memset(event_notifier
, 0, sizeof(*event_notifier
));
2028 if (lttng_event_rule_targets_agent_domain(rule
)) {
2030 * Special event for agents
2031 * The actual meat of the event is in the filter that will be
2032 * attached later on.
2033 * Set the default values for the agent event.
2035 pattern
= event_get_default_agent_ust_name(
2036 lttng_event_rule_get_domain_type(rule
));
2038 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2040 const struct lttng_log_level_rule
*log_level_rule
;
2042 assert(lttng_event_rule_get_type(rule
) ==
2043 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
);
2045 status
= lttng_event_rule_user_tracepoint_get_name_pattern(rule
, &pattern
);
2046 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2047 /* At this point, this is a fatal error. */
2051 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(
2052 rule
, &log_level_rule
);
2053 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2054 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2055 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2056 enum lttng_log_level_rule_status llr_status
;
2058 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2059 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2060 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2061 llr_status
= lttng_log_level_rule_exactly_get_level(
2062 log_level_rule
, &loglevel
);
2064 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2065 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2066 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2067 log_level_rule
, &loglevel
);
2073 assert(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2075 /* At this point this is a fatal error. */
2080 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2081 ret
= lttng_strncpy(event_notifier
->event
.name
, pattern
,
2082 LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
2084 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
2089 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2090 event_notifier
->event
.loglevel
= loglevel
;
2096 * Create the specified event notifier against the user space tracer of a
2097 * given application.
2099 static int create_ust_event_notifier(struct ust_app
*app
,
2100 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2103 enum lttng_condition_status condition_status
;
2104 const struct lttng_condition
*condition
= NULL
;
2105 struct lttng_ust_abi_event_notifier event_notifier
;
2106 const struct lttng_event_rule
*event_rule
= NULL
;
2107 unsigned int capture_bytecode_count
= 0, i
;
2108 enum lttng_condition_status cond_status
;
2109 enum lttng_event_rule_type event_rule_type
;
2111 health_code_update();
2112 assert(app
->event_notifier_group
.object
);
2114 condition
= lttng_trigger_get_const_condition(
2115 ua_event_notifier_rule
->trigger
);
2117 assert(lttng_condition_get_type(condition
) ==
2118 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2120 condition_status
= lttng_condition_event_rule_matches_get_rule(
2121 condition
, &event_rule
);
2122 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2126 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2127 assert(event_rule_type
== LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
||
2128 event_rule_type
== LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
||
2130 LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
||
2132 LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
);
2134 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2135 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2136 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2138 /* Create UST event notifier against the tracer. */
2139 pthread_mutex_lock(&app
->sock_lock
);
2140 ret
= lttng_ust_ctl_create_event_notifier(app
->sock
, &event_notifier
,
2141 app
->event_notifier_group
.object
,
2142 &ua_event_notifier_rule
->obj
);
2143 pthread_mutex_unlock(&app
->sock_lock
);
2145 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2146 ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d",
2147 event_notifier
.event
.name
, app
->name
,
2151 * This is normal behavior, an application can die
2152 * during the creation process. Don't report an error so
2153 * the execution can continue normally.
2156 DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)",
2157 app
->name
, app
->ppid
);
2163 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2165 DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p",
2166 event_notifier
.event
.name
, app
->name
, app
->ppid
,
2167 ua_event_notifier_rule
->obj
);
2169 health_code_update();
2171 /* Set filter if one is present. */
2172 if (ua_event_notifier_rule
->filter
) {
2173 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2174 ua_event_notifier_rule
->obj
);
2180 /* Set exclusions for the event. */
2181 if (ua_event_notifier_rule
->exclusion
) {
2182 ret
= set_ust_object_exclusions(app
,
2183 ua_event_notifier_rule
->exclusion
,
2184 ua_event_notifier_rule
->obj
);
2190 /* Set the capture bytecodes. */
2191 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2192 condition
, &capture_bytecode_count
);
2193 assert(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2195 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2196 const struct lttng_bytecode
*capture_bytecode
=
2197 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
2200 ret
= set_ust_capture(app
, capture_bytecode
, i
,
2201 ua_event_notifier_rule
->obj
);
2208 * We now need to explicitly enable the event, since it
2209 * is disabled at creation.
2211 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2214 * If we hit an EPERM, something is wrong with our enable call.
2215 * If we get an EEXIST, there is a problem on the tracer side
2216 * since we just created it.
2219 case -LTTNG_UST_ERR_PERM
:
2220 /* Code flow problem. */
2222 case -LTTNG_UST_ERR_EXIST
:
2223 /* It's OK for our use case. */
2233 ua_event_notifier_rule
->enabled
= true;
2236 health_code_update();
2241 * Copy data between an UST app event and a LTT event.
2243 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2244 struct ltt_ust_event
*uevent
)
2246 size_t exclusion_alloc_size
;
2248 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2249 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2251 ua_event
->enabled
= uevent
->enabled
;
2253 /* Copy event attributes */
2254 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2256 /* Copy filter bytecode */
2257 if (uevent
->filter
) {
2258 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2259 /* Filter might be NULL here in case of ENONEM. */
2262 /* Copy exclusion data */
2263 if (uevent
->exclusion
) {
2264 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2265 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2266 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
2267 if (ua_event
->exclusion
== NULL
) {
2270 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2271 exclusion_alloc_size
);
2277 * Copy data between an UST app channel and a LTT channel.
2279 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2280 struct ltt_ust_channel
*uchan
)
2282 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2284 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2285 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2287 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2288 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2290 /* Copy event attributes since the layout is different. */
2291 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2292 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2293 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2294 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2295 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2296 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2297 ua_chan
->attr
.output
= uchan
->attr
.output
;
2298 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2301 * Note that the attribute channel type is not set since the channel on the
2302 * tracing registry side does not have this information.
2305 ua_chan
->enabled
= uchan
->enabled
;
2306 ua_chan
->tracing_channel_id
= uchan
->id
;
2308 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2312 * Copy data between a UST app session and a regular LTT session.
2314 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2315 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2317 struct tm
*timeinfo
;
2320 char tmp_shm_path
[PATH_MAX
];
2322 timeinfo
= localtime(&app
->registration_time
);
2323 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2325 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2327 ua_sess
->tracing_id
= usess
->id
;
2328 ua_sess
->id
= get_next_session_id();
2329 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2330 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2331 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2332 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2333 ua_sess
->buffer_type
= usess
->buffer_type
;
2334 ua_sess
->bits_per_long
= app
->bits_per_long
;
2336 /* There is only one consumer object per session possible. */
2337 consumer_output_get(usess
->consumer
);
2338 ua_sess
->consumer
= usess
->consumer
;
2340 ua_sess
->output_traces
= usess
->output_traces
;
2341 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2342 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2343 &usess
->metadata_attr
);
2345 switch (ua_sess
->buffer_type
) {
2346 case LTTNG_BUFFER_PER_PID
:
2347 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2348 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2351 case LTTNG_BUFFER_PER_UID
:
2352 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2353 DEFAULT_UST_TRACE_UID_PATH
,
2354 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2355 app
->bits_per_long
);
2362 PERROR("asprintf UST shadow copy session");
2367 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2368 sizeof(ua_sess
->root_shm_path
));
2369 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2370 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2371 sizeof(ua_sess
->shm_path
));
2372 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2373 if (ua_sess
->shm_path
[0]) {
2374 switch (ua_sess
->buffer_type
) {
2375 case LTTNG_BUFFER_PER_PID
:
2376 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2377 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2378 app
->name
, app
->pid
, datetime
);
2380 case LTTNG_BUFFER_PER_UID
:
2381 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2382 "/" DEFAULT_UST_TRACE_UID_PATH
,
2383 app
->uid
, app
->bits_per_long
);
2390 PERROR("sprintf UST shadow copy session");
2394 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2395 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2396 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2401 consumer_output_put(ua_sess
->consumer
);
2405 * Lookup sesison wrapper.
2408 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2409 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2411 /* Get right UST app session from app */
2412 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2416 * Return ust app session from the app session hashtable using the UST session
2419 static struct ust_app_session
*lookup_session_by_app(
2420 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2422 struct lttng_ht_iter iter
;
2423 struct lttng_ht_node_u64
*node
;
2425 __lookup_session_by_app(usess
, app
, &iter
);
2426 node
= lttng_ht_iter_get_node_u64(&iter
);
2431 return caa_container_of(node
, struct ust_app_session
, node
);
2438 * Setup buffer registry per PID for the given session and application. If none
2439 * is found, a new one is created, added to the global registry and
2440 * initialized. If regp is valid, it's set with the newly created object.
2442 * Return 0 on success or else a negative value.
2444 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2445 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2448 struct buffer_reg_pid
*reg_pid
;
2455 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2458 * This is the create channel path meaning that if there is NO
2459 * registry available, we have to create one for this session.
2461 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2462 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2470 /* Initialize registry. */
2471 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2472 app
->bits_per_long
, app
->uint8_t_alignment
,
2473 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2474 app
->uint64_t_alignment
, app
->long_alignment
,
2475 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2476 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2477 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2478 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2479 ua_sess
->tracing_id
,
2483 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2484 * destroy the buffer registry, because it is always expected
2485 * that if the buffer registry can be found, its ust registry is
2488 buffer_reg_pid_destroy(reg_pid
);
2492 buffer_reg_pid_add(reg_pid
);
2494 DBG3("UST app buffer registry per PID created successfully");
2506 * Setup buffer registry per UID for the given session and application. If none
2507 * is found, a new one is created, added to the global registry and
2508 * initialized. If regp is valid, it's set with the newly created object.
2510 * Return 0 on success or else a negative value.
2512 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2513 struct ust_app_session
*ua_sess
,
2514 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2517 struct buffer_reg_uid
*reg_uid
;
2524 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2527 * This is the create channel path meaning that if there is NO
2528 * registry available, we have to create one for this session.
2530 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2531 LTTNG_DOMAIN_UST
, ®_uid
,
2532 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2540 /* Initialize registry. */
2541 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2542 app
->bits_per_long
, app
->uint8_t_alignment
,
2543 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2544 app
->uint64_t_alignment
, app
->long_alignment
,
2545 app
->byte_order
, app
->version
.major
,
2546 app
->version
.minor
, reg_uid
->root_shm_path
,
2547 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2548 ua_sess
->tracing_id
, app
->uid
);
2551 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2552 * destroy the buffer registry, because it is always expected
2553 * that if the buffer registry can be found, its ust registry is
2556 buffer_reg_uid_destroy(reg_uid
, NULL
);
2559 /* Add node to teardown list of the session. */
2560 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2562 buffer_reg_uid_add(reg_uid
);
2564 DBG3("UST app buffer registry per UID created successfully");
2575 * Create a session on the tracer side for the given app.
2577 * On success, ua_sess_ptr is populated with the session pointer or else left
2578 * untouched. If the session was created, is_created is set to 1. On error,
2579 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2582 * Returns 0 on success or else a negative code which is either -ENOMEM or
2583 * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails.
2585 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2586 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2589 int ret
, created
= 0;
2590 struct ust_app_session
*ua_sess
;
2594 assert(ua_sess_ptr
);
2596 health_code_update();
2598 ua_sess
= lookup_session_by_app(usess
, app
);
2599 if (ua_sess
== NULL
) {
2600 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2601 app
->pid
, usess
->id
);
2602 ua_sess
= alloc_ust_app_session();
2603 if (ua_sess
== NULL
) {
2604 /* Only malloc can failed so something is really wrong */
2608 shadow_copy_session(ua_sess
, usess
, app
);
2612 switch (usess
->buffer_type
) {
2613 case LTTNG_BUFFER_PER_PID
:
2614 /* Init local registry. */
2615 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2617 delete_ust_app_session(-1, ua_sess
, app
);
2621 case LTTNG_BUFFER_PER_UID
:
2622 /* Look for a global registry. If none exists, create one. */
2623 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2625 delete_ust_app_session(-1, ua_sess
, app
);
2635 health_code_update();
2637 if (ua_sess
->handle
== -1) {
2638 pthread_mutex_lock(&app
->sock_lock
);
2639 ret
= lttng_ust_ctl_create_session(app
->sock
);
2640 pthread_mutex_unlock(&app
->sock_lock
);
2642 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2643 ERR("Creating session for app pid %d with ret %d",
2646 DBG("UST app creating session failed. Application is dead");
2648 * This is normal behavior, an application can die during the
2649 * creation process. Don't report an error so the execution can
2650 * continue normally. This will get flagged ENOTCONN and the
2651 * caller will handle it.
2655 delete_ust_app_session(-1, ua_sess
, app
);
2656 if (ret
!= -ENOMEM
) {
2658 * Tracer is probably gone or got an internal error so let's
2659 * behave like it will soon unregister or not usable.
2666 ua_sess
->handle
= ret
;
2668 /* Add ust app session to app's HT */
2669 lttng_ht_node_init_u64(&ua_sess
->node
,
2670 ua_sess
->tracing_id
);
2671 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2672 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2673 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2674 &ua_sess
->ust_objd_node
);
2676 DBG2("UST app session created successfully with handle %d", ret
);
2679 *ua_sess_ptr
= ua_sess
;
2681 *is_created
= created
;
2684 /* Everything went well. */
2688 health_code_update();
2693 * Match function for a hash table lookup of ust_app_ctx.
2695 * It matches an ust app context based on the context type and, in the case
2696 * of perf counters, their name.
2698 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2700 struct ust_app_ctx
*ctx
;
2701 const struct lttng_ust_context_attr
*key
;
2706 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2710 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2715 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2716 if (strncmp(key
->u
.perf_counter
.name
,
2717 ctx
->ctx
.u
.perf_counter
.name
,
2718 sizeof(key
->u
.perf_counter
.name
))) {
2722 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2723 if (strcmp(key
->u
.app_ctx
.provider_name
,
2724 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2725 strcmp(key
->u
.app_ctx
.ctx_name
,
2726 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2742 * Lookup for an ust app context from an lttng_ust_context.
2744 * Must be called while holding RCU read side lock.
2745 * Return an ust_app_ctx object or NULL on error.
2748 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2749 struct lttng_ust_context_attr
*uctx
)
2751 struct lttng_ht_iter iter
;
2752 struct lttng_ht_node_ulong
*node
;
2753 struct ust_app_ctx
*app_ctx
= NULL
;
2758 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2759 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2760 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2761 node
= lttng_ht_iter_get_node_ulong(&iter
);
2766 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2773 * Create a context for the channel on the tracer.
2775 * Called with UST app session lock held and a RCU read side lock.
2778 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2779 struct lttng_ust_context_attr
*uctx
,
2780 struct ust_app
*app
)
2783 struct ust_app_ctx
*ua_ctx
;
2785 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2787 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2793 ua_ctx
= alloc_ust_app_ctx(uctx
);
2794 if (ua_ctx
== NULL
) {
2800 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2801 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2802 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2804 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2814 * Enable on the tracer side a ust app event for the session and channel.
2816 * Called with UST app session lock held.
2819 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2820 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2824 ret
= enable_ust_object(app
, ua_event
->obj
);
2829 ua_event
->enabled
= 1;
2836 * Disable on the tracer side a ust app event for the session and channel.
2838 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2839 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2843 ret
= disable_ust_object(app
, ua_event
->obj
);
2848 ua_event
->enabled
= 0;
2855 * Lookup ust app channel for session and disable it on the tracer side.
2858 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2859 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2863 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2868 ua_chan
->enabled
= 0;
2875 * Lookup ust app channel for session and enable it on the tracer side. This
2876 * MUST be called with a RCU read side lock acquired.
2878 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2879 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2882 struct lttng_ht_iter iter
;
2883 struct lttng_ht_node_str
*ua_chan_node
;
2884 struct ust_app_channel
*ua_chan
;
2886 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2887 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2888 if (ua_chan_node
== NULL
) {
2889 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2890 uchan
->name
, ua_sess
->tracing_id
);
2894 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2896 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2906 * Ask the consumer to create a channel and get it if successful.
2908 * Called with UST app session lock held.
2910 * Return 0 on success or else a negative value.
2912 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2913 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2914 int bitness
, struct ust_registry_session
*registry
,
2915 uint64_t trace_archive_id
)
2918 unsigned int nb_fd
= 0;
2919 struct consumer_socket
*socket
;
2927 health_code_update();
2929 /* Get the right consumer socket for the application. */
2930 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2936 health_code_update();
2938 /* Need one fd for the channel. */
2939 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2941 ERR("Exhausted number of available FD upon create channel");
2946 * Ask consumer to create channel. The consumer will return the number of
2947 * stream we have to expect.
2949 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2950 registry
, usess
->current_trace_chunk
);
2956 * Compute the number of fd needed before receiving them. It must be 2 per
2957 * stream (2 being the default value here).
2959 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2961 /* Reserve the amount of file descriptor we need. */
2962 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2964 ERR("Exhausted number of available FD upon create channel");
2965 goto error_fd_get_stream
;
2968 health_code_update();
2971 * Now get the channel from the consumer. This call will populate the stream
2972 * list of that channel and set the ust objects.
2974 if (usess
->consumer
->enabled
) {
2975 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2985 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2986 error_fd_get_stream
:
2988 * Initiate a destroy channel on the consumer since we had an error
2989 * handling it on our side. The return value is of no importance since we
2990 * already have a ret value set by the previous error that we need to
2993 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2995 lttng_fd_put(LTTNG_FD_APPS
, 1);
2997 health_code_update();
3003 * Duplicate the ust data object of the ust app stream and save it in the
3004 * buffer registry stream.
3006 * Return 0 on success or else a negative value.
3008 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
3009 struct ust_app_stream
*stream
)
3016 /* Reserve the amount of file descriptor we need. */
3017 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3019 ERR("Exhausted number of available FD upon duplicate stream");
3023 /* Duplicate object for stream once the original is in the registry. */
3024 ret
= lttng_ust_ctl_duplicate_ust_object_data(&stream
->obj
,
3025 reg_stream
->obj
.ust
);
3027 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3028 reg_stream
->obj
.ust
, stream
->obj
, ret
);
3029 lttng_fd_put(LTTNG_FD_APPS
, 2);
3032 stream
->handle
= stream
->obj
->handle
;
3039 * Duplicate the ust data object of the ust app. channel and save it in the
3040 * buffer registry channel.
3042 * Return 0 on success or else a negative value.
3044 static int duplicate_channel_object(struct buffer_reg_channel
*buf_reg_chan
,
3045 struct ust_app_channel
*ua_chan
)
3049 assert(buf_reg_chan
);
3052 /* Need two fds for the channel. */
3053 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3055 ERR("Exhausted number of available FD upon duplicate channel");
3059 /* Duplicate object for stream once the original is in the registry. */
3060 ret
= lttng_ust_ctl_duplicate_ust_object_data(&ua_chan
->obj
, buf_reg_chan
->obj
.ust
);
3062 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3063 buf_reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
3066 ua_chan
->handle
= ua_chan
->obj
->handle
;
3071 lttng_fd_put(LTTNG_FD_APPS
, 1);
3077 * For a given channel buffer registry, setup all streams of the given ust
3078 * application channel.
3080 * Return 0 on success or else a negative value.
3082 static int setup_buffer_reg_streams(struct buffer_reg_channel
*buf_reg_chan
,
3083 struct ust_app_channel
*ua_chan
,
3084 struct ust_app
*app
)
3087 struct ust_app_stream
*stream
, *stmp
;
3089 assert(buf_reg_chan
);
3092 DBG2("UST app setup buffer registry stream");
3094 /* Send all streams to application. */
3095 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3096 struct buffer_reg_stream
*reg_stream
;
3098 ret
= buffer_reg_stream_create(®_stream
);
3104 * Keep original pointer and nullify it in the stream so the delete
3105 * stream call does not release the object.
3107 reg_stream
->obj
.ust
= stream
->obj
;
3109 buffer_reg_stream_add(reg_stream
, buf_reg_chan
);
3111 /* We don't need the streams anymore. */
3112 cds_list_del(&stream
->list
);
3113 delete_ust_app_stream(-1, stream
, app
);
3121 * Create a buffer registry channel for the given session registry and
3122 * application channel object. If regp pointer is valid, it's set with the
3123 * created object. Important, the created object is NOT added to the session
3124 * registry hash table.
3126 * Return 0 on success else a negative value.
3128 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3129 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
3132 struct buffer_reg_channel
*buf_reg_chan
= NULL
;
3137 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3139 /* Create buffer registry channel. */
3140 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, &buf_reg_chan
);
3144 assert(buf_reg_chan
);
3145 buf_reg_chan
->consumer_key
= ua_chan
->key
;
3146 buf_reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3147 buf_reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3149 /* Create and add a channel registry to session. */
3150 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
3151 ua_chan
->tracing_channel_id
);
3155 buffer_reg_channel_add(reg_sess
, buf_reg_chan
);
3158 *regp
= buf_reg_chan
;
3164 /* Safe because the registry channel object was not added to any HT. */
3165 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3171 * Setup buffer registry channel for the given session registry and application
3172 * channel object. If regp pointer is valid, it's set with the created object.
3174 * Return 0 on success else a negative value.
3176 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3177 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*buf_reg_chan
,
3178 struct ust_app
*app
)
3183 assert(buf_reg_chan
);
3185 assert(ua_chan
->obj
);
3187 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3189 /* Setup all streams for the registry. */
3190 ret
= setup_buffer_reg_streams(buf_reg_chan
, ua_chan
, app
);
3195 buf_reg_chan
->obj
.ust
= ua_chan
->obj
;
3196 ua_chan
->obj
= NULL
;
3201 buffer_reg_channel_remove(reg_sess
, buf_reg_chan
);
3202 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3207 * Send buffer registry channel to the application.
3209 * Return 0 on success else a negative value.
3211 static int send_channel_uid_to_ust(struct buffer_reg_channel
*buf_reg_chan
,
3212 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
3213 struct ust_app_channel
*ua_chan
)
3216 struct buffer_reg_stream
*reg_stream
;
3218 assert(buf_reg_chan
);
3223 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3225 ret
= duplicate_channel_object(buf_reg_chan
, ua_chan
);
3230 /* Send channel to the application. */
3231 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3232 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3233 ret
= -ENOTCONN
; /* Caused by app exiting. */
3235 } else if (ret
< 0) {
3239 health_code_update();
3241 /* Send all streams to application. */
3242 pthread_mutex_lock(&buf_reg_chan
->stream_list_lock
);
3243 cds_list_for_each_entry(reg_stream
, &buf_reg_chan
->streams
, lnode
) {
3244 struct ust_app_stream stream
;
3246 ret
= duplicate_stream_object(reg_stream
, &stream
);
3248 goto error_stream_unlock
;
3251 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3253 (void) release_ust_app_stream(-1, &stream
, app
);
3254 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3255 ret
= -ENOTCONN
; /* Caused by app exiting. */
3257 goto error_stream_unlock
;
3261 * The return value is not important here. This function will output an
3264 (void) release_ust_app_stream(-1, &stream
, app
);
3266 ua_chan
->is_sent
= 1;
3268 error_stream_unlock
:
3269 pthread_mutex_unlock(&buf_reg_chan
->stream_list_lock
);
3275 * Create and send to the application the created buffers with per UID buffers.
3277 * This MUST be called with a RCU read side lock acquired.
3278 * The session list lock and the session's lock must be acquired.
3280 * Return 0 on success else a negative value.
3282 static int create_channel_per_uid(struct ust_app
*app
,
3283 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3284 struct ust_app_channel
*ua_chan
)
3287 struct buffer_reg_uid
*reg_uid
;
3288 struct buffer_reg_channel
*buf_reg_chan
;
3289 struct ltt_session
*session
= NULL
;
3290 enum lttng_error_code notification_ret
;
3291 struct ust_registry_channel
*ust_reg_chan
;
3298 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3300 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3302 * The session creation handles the creation of this global registry
3303 * object. If none can be find, there is a code flow problem or a
3308 buf_reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3314 /* Create the buffer registry channel object. */
3315 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, &buf_reg_chan
);
3317 ERR("Error creating the UST channel \"%s\" registry instance",
3322 session
= session_find_by_id(ua_sess
->tracing_id
);
3324 assert(pthread_mutex_trylock(&session
->lock
));
3325 assert(session_trylock_list());
3328 * Create the buffers on the consumer side. This call populates the
3329 * ust app channel object with all streams and data object.
3331 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3332 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
3333 session
->most_recent_chunk_id
.value
);
3335 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3339 * Let's remove the previously created buffer registry channel so
3340 * it's not visible anymore in the session registry.
3342 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3343 ua_chan
->tracing_channel_id
, false);
3344 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
3345 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3350 * Setup the streams and add it to the session registry.
3352 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3353 ua_chan
, buf_reg_chan
, app
);
3355 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3359 /* Notify the notification subsystem of the channel's creation. */
3360 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
3361 ust_reg_chan
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3362 ua_chan
->tracing_channel_id
);
3363 assert(ust_reg_chan
);
3364 ust_reg_chan
->consumer_key
= ua_chan
->key
;
3365 ust_reg_chan
= NULL
;
3366 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
3368 notification_ret
= notification_thread_command_add_channel(
3369 the_notification_thread_handle
, session
->name
,
3370 lttng_credentials_get_uid(
3371 &ua_sess
->effective_credentials
),
3372 lttng_credentials_get_gid(
3373 &ua_sess
->effective_credentials
),
3374 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3375 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3376 if (notification_ret
!= LTTNG_OK
) {
3377 ret
= - (int) notification_ret
;
3378 ERR("Failed to add channel to notification thread");
3383 /* Send buffers to the application. */
3384 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3386 if (ret
!= -ENOTCONN
) {
3387 ERR("Error sending channel to application");
3394 session_put(session
);
3400 * Create and send to the application the created buffers with per PID buffers.
3402 * Called with UST app session lock held.
3403 * The session list lock and the session's lock must be acquired.
3405 * Return 0 on success else a negative value.
3407 static int create_channel_per_pid(struct ust_app
*app
,
3408 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3409 struct ust_app_channel
*ua_chan
)
3412 struct ust_registry_session
*registry
;
3413 enum lttng_error_code cmd_ret
;
3414 struct ltt_session
*session
= NULL
;
3415 uint64_t chan_reg_key
;
3416 struct ust_registry_channel
*ust_reg_chan
;
3423 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3427 registry
= get_session_registry(ua_sess
);
3428 /* The UST app session lock is held, registry shall not be null. */
3431 /* Create and add a new channel registry to session. */
3432 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3434 ERR("Error creating the UST channel \"%s\" registry instance",
3439 session
= session_find_by_id(ua_sess
->tracing_id
);
3442 assert(pthread_mutex_trylock(&session
->lock
));
3443 assert(session_trylock_list());
3445 /* Create and get channel on the consumer side. */
3446 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3447 app
->bits_per_long
, registry
,
3448 session
->most_recent_chunk_id
.value
);
3450 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3452 goto error_remove_from_registry
;
3455 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3457 if (ret
!= -ENOTCONN
) {
3458 ERR("Error sending channel to application");
3460 goto error_remove_from_registry
;
3463 chan_reg_key
= ua_chan
->key
;
3464 pthread_mutex_lock(®istry
->lock
);
3465 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
3466 assert(ust_reg_chan
);
3467 ust_reg_chan
->consumer_key
= ua_chan
->key
;
3468 pthread_mutex_unlock(®istry
->lock
);
3470 cmd_ret
= notification_thread_command_add_channel(
3471 the_notification_thread_handle
, session
->name
,
3472 lttng_credentials_get_uid(
3473 &ua_sess
->effective_credentials
),
3474 lttng_credentials_get_gid(
3475 &ua_sess
->effective_credentials
),
3476 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3477 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3478 if (cmd_ret
!= LTTNG_OK
) {
3479 ret
= - (int) cmd_ret
;
3480 ERR("Failed to add channel to notification thread");
3481 goto error_remove_from_registry
;
3484 error_remove_from_registry
:
3486 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3491 session_put(session
);
3497 * From an already allocated ust app channel, create the channel buffers if
3498 * needed and send them to the application. This MUST be called with a RCU read
3499 * side lock acquired.
3501 * Called with UST app session lock held.
3503 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3504 * the application exited concurrently.
3506 static int ust_app_channel_send(struct ust_app
*app
,
3507 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3508 struct ust_app_channel
*ua_chan
)
3514 assert(usess
->active
);
3518 /* Handle buffer type before sending the channel to the application. */
3519 switch (usess
->buffer_type
) {
3520 case LTTNG_BUFFER_PER_UID
:
3522 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3528 case LTTNG_BUFFER_PER_PID
:
3530 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3542 /* Initialize ust objd object using the received handle and add it. */
3543 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3544 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3546 /* If channel is not enabled, disable it on the tracer */
3547 if (!ua_chan
->enabled
) {
3548 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3559 * Create UST app channel and return it through ua_chanp if not NULL.
3561 * Called with UST app session lock and RCU read-side lock held.
3563 * Return 0 on success or else a negative value.
3565 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3566 struct ltt_ust_channel
*uchan
,
3567 enum lttng_ust_abi_chan_type type
, struct ltt_ust_session
*usess
,
3568 struct ust_app_channel
**ua_chanp
)
3571 struct lttng_ht_iter iter
;
3572 struct lttng_ht_node_str
*ua_chan_node
;
3573 struct ust_app_channel
*ua_chan
;
3575 /* Lookup channel in the ust app session */
3576 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3577 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3578 if (ua_chan_node
!= NULL
) {
3579 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3583 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3584 if (ua_chan
== NULL
) {
3585 /* Only malloc can fail here */
3589 shadow_copy_channel(ua_chan
, uchan
);
3591 /* Set channel type. */
3592 ua_chan
->attr
.type
= type
;
3594 /* Only add the channel if successful on the tracer side. */
3595 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3598 *ua_chanp
= ua_chan
;
3601 /* Everything went well. */
3609 * Create UST app event and create it on the tracer side.
3611 * Must be called with the RCU read side lock held.
3612 * Called with ust app session mutex held.
3615 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3616 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3617 struct ust_app
*app
)
3620 struct ust_app_event
*ua_event
;
3622 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3623 if (ua_event
== NULL
) {
3624 /* Only failure mode of alloc_ust_app_event(). */
3628 shadow_copy_event(ua_event
, uevent
);
3630 /* Create it on the tracer side */
3631 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3634 * Not found previously means that it does not exist on the
3635 * tracer. If the application reports that the event existed,
3636 * it means there is a bug in the sessiond or lttng-ust
3637 * (or corruption, etc.)
3639 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3640 ERR("Tracer for application reported that an event being created already existed: "
3641 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3643 app
->pid
, app
->ppid
, app
->uid
,
3649 add_unique_ust_app_event(ua_chan
, ua_event
);
3651 DBG2("UST app create event completed: app = '%s' (ppid: %d)",
3652 app
->name
, app
->ppid
);
3658 /* Valid. Calling here is already in a read side lock */
3659 delete_ust_app_event(-1, ua_event
, app
);
3664 * Create UST app event notifier rule and create it on the tracer side.
3666 * Must be called with the RCU read side lock held.
3667 * Called with ust app session mutex held.
3670 int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
,
3671 struct ust_app
*app
)
3674 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3676 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3677 if (ua_event_notifier_rule
== NULL
) {
3682 /* Create it on the tracer side. */
3683 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3686 * Not found previously means that it does not exist on the
3687 * tracer. If the application reports that the event existed,
3688 * it means there is a bug in the sessiond or lttng-ust
3689 * (or corruption, etc.)
3691 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3692 ERR("Tracer for application reported that an event notifier being created already exists: "
3693 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3694 lttng_trigger_get_tracer_token(trigger
),
3695 app
->pid
, app
->ppid
, app
->uid
,
3701 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3702 &ua_event_notifier_rule
->node
);
3704 DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64
,
3705 app
->name
, app
->ppid
, lttng_trigger_get_tracer_token(trigger
));
3710 /* The RCU read side lock is already being held by the caller. */
3711 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3717 * Create UST metadata and open it on the tracer side.
3719 * Called with UST app session lock held and RCU read side lock.
3721 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3722 struct ust_app
*app
, struct consumer_output
*consumer
)
3725 struct ust_app_channel
*metadata
;
3726 struct consumer_socket
*socket
;
3727 struct ust_registry_session
*registry
;
3728 struct ltt_session
*session
= NULL
;
3734 registry
= get_session_registry(ua_sess
);
3735 /* The UST app session is held registry shall not be null. */
3738 pthread_mutex_lock(®istry
->lock
);
3740 /* Metadata already exists for this registry or it was closed previously */
3741 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3746 /* Allocate UST metadata */
3747 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3749 /* malloc() failed */
3754 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3756 /* Need one fd for the channel. */
3757 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3759 ERR("Exhausted number of available FD upon create metadata");
3763 /* Get the right consumer socket for the application. */
3764 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3767 goto error_consumer
;
3771 * Keep metadata key so we can identify it on the consumer side. Assign it
3772 * to the registry *before* we ask the consumer so we avoid the race of the
3773 * consumer requesting the metadata and the ask_channel call on our side
3774 * did not returned yet.
3776 registry
->metadata_key
= metadata
->key
;
3778 session
= session_find_by_id(ua_sess
->tracing_id
);
3781 assert(pthread_mutex_trylock(&session
->lock
));
3782 assert(session_trylock_list());
3785 * Ask the metadata channel creation to the consumer. The metadata object
3786 * will be created by the consumer and kept their. However, the stream is
3787 * never added or monitored until we do a first push metadata to the
3790 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3791 registry
, session
->current_trace_chunk
);
3793 /* Nullify the metadata key so we don't try to close it later on. */
3794 registry
->metadata_key
= 0;
3795 goto error_consumer
;
3799 * The setup command will make the metadata stream be sent to the relayd,
3800 * if applicable, and the thread managing the metadatas. This is important
3801 * because after this point, if an error occurs, the only way the stream
3802 * can be deleted is to be monitored in the consumer.
3804 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3806 /* Nullify the metadata key so we don't try to close it later on. */
3807 registry
->metadata_key
= 0;
3808 goto error_consumer
;
3811 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3812 metadata
->key
, app
->pid
);
3815 lttng_fd_put(LTTNG_FD_APPS
, 1);
3816 delete_ust_app_channel(-1, metadata
, app
);
3818 pthread_mutex_unlock(®istry
->lock
);
3820 session_put(session
);
3826 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3827 * acquired before calling this function.
3829 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3831 struct ust_app
*app
= NULL
;
3832 struct lttng_ht_node_ulong
*node
;
3833 struct lttng_ht_iter iter
;
3835 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3836 node
= lttng_ht_iter_get_node_ulong(&iter
);
3838 DBG2("UST app no found with pid %d", pid
);
3842 DBG2("Found UST app by pid %d", pid
);
3844 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3851 * Allocate and init an UST app object using the registration information and
3852 * the command socket. This is called when the command socket connects to the
3855 * The object is returned on success or else NULL.
3857 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3860 struct ust_app
*lta
= NULL
;
3861 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3866 DBG3("UST app creating application for socket %d", sock
);
3868 if ((msg
->bits_per_long
== 64 &&
3869 (uatomic_read(&the_ust_consumerd64_fd
) ==
3871 (msg
->bits_per_long
== 32 &&
3872 (uatomic_read(&the_ust_consumerd32_fd
) ==
3874 ERR("Registration failed: application \"%s\" (pid: %d) has "
3875 "%d-bit long, but no consumerd for this size is available.\n",
3876 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3881 * Reserve the two file descriptors of the event source pipe. The write
3882 * end will be closed once it is passed to the application, at which
3883 * point a single 'put' will be performed.
3885 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3887 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s' (ppid: %d)",
3888 msg
->name
, (int) msg
->ppid
);
3892 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3893 if (!event_notifier_event_source_pipe
) {
3894 PERROR("Failed to open application event source pipe: '%s' (ppid = %d)",
3895 msg
->name
, msg
->ppid
);
3899 lta
= zmalloc(sizeof(struct ust_app
));
3902 goto error_free_pipe
;
3905 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
3907 lta
->ppid
= msg
->ppid
;
3908 lta
->uid
= msg
->uid
;
3909 lta
->gid
= msg
->gid
;
3911 lta
->bits_per_long
= msg
->bits_per_long
;
3912 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3913 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3914 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3915 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3916 lta
->long_alignment
= msg
->long_alignment
;
3917 lta
->byte_order
= msg
->byte_order
;
3919 lta
->v_major
= msg
->major
;
3920 lta
->v_minor
= msg
->minor
;
3921 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3922 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3923 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3924 lta
->notify_sock
= -1;
3925 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3927 /* Copy name and make sure it's NULL terminated. */
3928 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3929 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3932 * Before this can be called, when receiving the registration information,
3933 * the application compatibility is checked. So, at this point, the
3934 * application can work with this session daemon.
3936 lta
->compatible
= 1;
3938 lta
->pid
= msg
->pid
;
3939 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3941 pthread_mutex_init(<a
->sock_lock
, NULL
);
3942 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3944 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3948 lttng_pipe_destroy(event_notifier_event_source_pipe
);
3949 lttng_fd_put(LTTNG_FD_APPS
, 2);
3955 * For a given application object, add it to every hash table.
3957 void ust_app_add(struct ust_app
*app
)
3960 assert(app
->notify_sock
>= 0);
3962 app
->registration_time
= time(NULL
);
3967 * On a re-registration, we want to kick out the previous registration of
3970 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3973 * The socket _should_ be unique until _we_ call close. So, a add_unique
3974 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3975 * already in the table.
3977 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3979 /* Add application to the notify socket hash table. */
3980 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3981 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3983 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3984 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3985 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3992 * Set the application version into the object.
3994 * Return 0 on success else a negative value either an errno code or a
3995 * LTTng-UST error code.
3997 int ust_app_version(struct ust_app
*app
)
4003 pthread_mutex_lock(&app
->sock_lock
);
4004 ret
= lttng_ust_ctl_tracer_version(app
->sock
, &app
->version
);
4005 pthread_mutex_unlock(&app
->sock_lock
);
4007 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4008 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
4010 DBG3("UST app %d version failed. Application is dead", app
->sock
);
4017 bool ust_app_supports_notifiers(const struct ust_app
*app
)
4019 return app
->v_major
>= 9;
4022 bool ust_app_supports_counters(const struct ust_app
*app
)
4024 return app
->v_major
>= 9;
4028 * Setup the base event notifier group.
4030 * Return 0 on success else a negative value either an errno code or a
4031 * LTTng-UST error code.
4033 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4036 int event_pipe_write_fd
;
4037 struct lttng_ust_abi_object_data
*event_notifier_group
= NULL
;
4038 enum lttng_error_code lttng_ret
;
4039 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4043 if (!ust_app_supports_notifiers(app
)) {
4048 /* Get the write side of the pipe. */
4049 event_pipe_write_fd
= lttng_pipe_get_writefd(
4050 app
->event_notifier_group
.event_pipe
);
4052 pthread_mutex_lock(&app
->sock_lock
);
4053 ret
= lttng_ust_ctl_create_event_notifier_group(app
->sock
,
4054 event_pipe_write_fd
, &event_notifier_group
);
4055 pthread_mutex_unlock(&app
->sock_lock
);
4057 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4058 ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d",
4059 ret
, app
->sock
, event_pipe_write_fd
);
4061 DBG("Failed to create application event notifier group (application is dead): app socket fd = %d",
4068 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4070 ERR("Failed to close write end of the application's event source pipe: app = '%s' (ppid = %d)",
4071 app
->name
, app
->ppid
);
4076 * Release the file descriptor that was reserved for the write-end of
4079 lttng_fd_put(LTTNG_FD_APPS
, 1);
4081 lttng_ret
= notification_thread_command_add_tracer_event_source(
4082 the_notification_thread_handle
,
4083 lttng_pipe_get_readfd(
4084 app
->event_notifier_group
.event_pipe
),
4086 if (lttng_ret
!= LTTNG_OK
) {
4087 ERR("Failed to add tracer event source to notification thread");
4092 /* Assign handle only when the complete setup is valid. */
4093 app
->event_notifier_group
.object
= event_notifier_group
;
4095 event_notifier_error_accounting_status
=
4096 event_notifier_error_accounting_register_app(app
);
4097 switch (event_notifier_error_accounting_status
) {
4098 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
4100 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
:
4101 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",
4102 app
->sock
, app
->name
, (int) app
->ppid
);
4104 goto error_accounting
;
4105 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
4106 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app ppid = %d",
4107 app
->sock
, app
->name
, (int) app
->ppid
);
4109 goto error_accounting
;
4111 ERR("Failed to setup event notifier error accounting for app");
4113 goto error_accounting
;
4119 lttng_ret
= notification_thread_command_remove_tracer_event_source(
4120 the_notification_thread_handle
,
4121 lttng_pipe_get_readfd(
4122 app
->event_notifier_group
.event_pipe
));
4123 if (lttng_ret
!= LTTNG_OK
) {
4124 ERR("Failed to remove application tracer event source from notification thread");
4128 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4129 free(app
->event_notifier_group
.object
);
4130 app
->event_notifier_group
.object
= NULL
;
4135 * Unregister app by removing it from the global traceable app list and freeing
4138 * The socket is already closed at this point so no close to sock.
4140 void ust_app_unregister(int sock
)
4142 struct ust_app
*lta
;
4143 struct lttng_ht_node_ulong
*node
;
4144 struct lttng_ht_iter ust_app_sock_iter
;
4145 struct lttng_ht_iter iter
;
4146 struct ust_app_session
*ua_sess
;
4151 /* Get the node reference for a call_rcu */
4152 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
4153 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4156 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
4157 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4160 * For per-PID buffers, perform "push metadata" and flush all
4161 * application streams before removing app from hash tables,
4162 * ensuring proper behavior of data_pending check.
4163 * Remove sessions so they are not visible during deletion.
4165 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
4167 struct ust_registry_session
*registry
;
4169 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4171 /* The session was already removed so scheduled for teardown. */
4175 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4176 (void) ust_app_flush_app_session(lta
, ua_sess
);
4180 * Add session to list for teardown. This is safe since at this point we
4181 * are the only one using this list.
4183 pthread_mutex_lock(&ua_sess
->lock
);
4185 if (ua_sess
->deleted
) {
4186 pthread_mutex_unlock(&ua_sess
->lock
);
4191 * Normally, this is done in the delete session process which is
4192 * executed in the call rcu below. However, upon registration we can't
4193 * afford to wait for the grace period before pushing data or else the
4194 * data pending feature can race between the unregistration and stop
4195 * command where the data pending command is sent *before* the grace
4198 * The close metadata below nullifies the metadata pointer in the
4199 * session so the delete session will NOT push/close a second time.
4201 registry
= get_session_registry(ua_sess
);
4203 /* Push metadata for application before freeing the application. */
4204 (void) push_metadata(registry
, ua_sess
->consumer
);
4207 * Don't ask to close metadata for global per UID buffers. Close
4208 * metadata only on destroy trace session in this case. Also, the
4209 * previous push metadata could have flag the metadata registry to
4210 * close so don't send a close command if closed.
4212 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4213 /* And ask to close it for this session registry. */
4214 (void) close_metadata(registry
, ua_sess
->consumer
);
4217 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4219 pthread_mutex_unlock(&ua_sess
->lock
);
4222 /* Remove application from PID hash table */
4223 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4227 * Remove application from notify hash table. The thread handling the
4228 * notify socket could have deleted the node so ignore on error because
4229 * either way it's valid. The close of that socket is handled by the
4230 * apps_notify_thread.
4232 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4233 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4236 * Ignore return value since the node might have been removed before by an
4237 * add replace during app registration because the PID can be reassigned by
4240 iter
.iter
.node
= <a
->pid_n
.node
;
4241 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4243 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4248 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4255 * Fill events array with all events name of all registered apps.
4257 int ust_app_list_events(struct lttng_event
**events
)
4260 size_t nbmem
, count
= 0;
4261 struct lttng_ht_iter iter
;
4262 struct ust_app
*app
;
4263 struct lttng_event
*tmp_event
;
4265 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4266 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
4267 if (tmp_event
== NULL
) {
4268 PERROR("zmalloc ust app events");
4275 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4276 struct lttng_ust_abi_tracepoint_iter uiter
;
4278 health_code_update();
4280 if (!app
->compatible
) {
4282 * TODO: In time, we should notice the caller of this error by
4283 * telling him that this is a version error.
4287 pthread_mutex_lock(&app
->sock_lock
);
4288 handle
= lttng_ust_ctl_tracepoint_list(app
->sock
);
4290 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4291 ERR("UST app list events getting handle failed for app pid %d",
4294 pthread_mutex_unlock(&app
->sock_lock
);
4298 while ((ret
= lttng_ust_ctl_tracepoint_list_get(app
->sock
, handle
,
4299 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4300 /* Handle ustctl error. */
4304 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4305 ERR("UST app tp list get failed for app %d with ret %d",
4308 DBG3("UST app tp list get failed. Application is dead");
4310 * This is normal behavior, an application can die during the
4311 * creation process. Don't report an error so the execution can
4312 * continue normally. Continue normal execution.
4317 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4318 if (release_ret
< 0 &&
4319 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4320 release_ret
!= -EPIPE
) {
4321 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4323 pthread_mutex_unlock(&app
->sock_lock
);
4327 health_code_update();
4328 if (count
>= nbmem
) {
4329 /* In case the realloc fails, we free the memory */
4330 struct lttng_event
*new_tmp_event
;
4333 new_nbmem
= nbmem
<< 1;
4334 DBG2("Reallocating event list from %zu to %zu entries",
4336 new_tmp_event
= realloc(tmp_event
,
4337 new_nbmem
* sizeof(struct lttng_event
));
4338 if (new_tmp_event
== NULL
) {
4341 PERROR("realloc ust app events");
4344 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4345 if (release_ret
< 0 &&
4346 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4347 release_ret
!= -EPIPE
) {
4348 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4350 pthread_mutex_unlock(&app
->sock_lock
);
4353 /* Zero the new memory */
4354 memset(new_tmp_event
+ nbmem
, 0,
4355 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4357 tmp_event
= new_tmp_event
;
4359 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4360 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4361 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4362 tmp_event
[count
].pid
= app
->pid
;
4363 tmp_event
[count
].enabled
= -1;
4366 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4367 pthread_mutex_unlock(&app
->sock_lock
);
4368 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4369 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4374 *events
= tmp_event
;
4376 DBG2("UST app list events done (%zu events)", count
);
4381 health_code_update();
4386 * Fill events array with all events name of all registered apps.
4388 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4391 size_t nbmem
, count
= 0;
4392 struct lttng_ht_iter iter
;
4393 struct ust_app
*app
;
4394 struct lttng_event_field
*tmp_event
;
4396 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4397 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
4398 if (tmp_event
== NULL
) {
4399 PERROR("zmalloc ust app event fields");
4406 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4407 struct lttng_ust_abi_field_iter uiter
;
4409 health_code_update();
4411 if (!app
->compatible
) {
4413 * TODO: In time, we should notice the caller of this error by
4414 * telling him that this is a version error.
4418 pthread_mutex_lock(&app
->sock_lock
);
4419 handle
= lttng_ust_ctl_tracepoint_field_list(app
->sock
);
4421 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4422 ERR("UST app list field getting handle failed for app pid %d",
4425 pthread_mutex_unlock(&app
->sock_lock
);
4429 while ((ret
= lttng_ust_ctl_tracepoint_field_list_get(app
->sock
, handle
,
4430 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4431 /* Handle ustctl error. */
4435 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4436 ERR("UST app tp list field failed for app %d with ret %d",
4439 DBG3("UST app tp list field failed. Application is dead");
4441 * This is normal behavior, an application can die during the
4442 * creation process. Don't report an error so the execution can
4443 * continue normally. Reset list and count for next app.
4448 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4449 pthread_mutex_unlock(&app
->sock_lock
);
4450 if (release_ret
< 0 &&
4451 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4452 release_ret
!= -EPIPE
) {
4453 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4458 health_code_update();
4459 if (count
>= nbmem
) {
4460 /* In case the realloc fails, we free the memory */
4461 struct lttng_event_field
*new_tmp_event
;
4464 new_nbmem
= nbmem
<< 1;
4465 DBG2("Reallocating event field list from %zu to %zu entries",
4467 new_tmp_event
= realloc(tmp_event
,
4468 new_nbmem
* sizeof(struct lttng_event_field
));
4469 if (new_tmp_event
== NULL
) {
4472 PERROR("realloc ust app event fields");
4475 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4476 pthread_mutex_unlock(&app
->sock_lock
);
4478 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4479 release_ret
!= -EPIPE
) {
4480 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4484 /* Zero the new memory */
4485 memset(new_tmp_event
+ nbmem
, 0,
4486 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4488 tmp_event
= new_tmp_event
;
4491 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4492 /* Mapping between these enums matches 1 to 1. */
4493 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4494 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4496 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4497 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4498 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4499 tmp_event
[count
].event
.pid
= app
->pid
;
4500 tmp_event
[count
].event
.enabled
= -1;
4503 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4504 pthread_mutex_unlock(&app
->sock_lock
);
4506 ret
!= -LTTNG_UST_ERR_EXITING
&&
4508 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4513 *fields
= tmp_event
;
4515 DBG2("UST app list event fields done (%zu events)", count
);
4520 health_code_update();
4525 * Free and clean all traceable apps of the global list.
4527 * Should _NOT_ be called with RCU read-side lock held.
4529 void ust_app_clean_list(void)
4532 struct ust_app
*app
;
4533 struct lttng_ht_iter iter
;
4535 DBG2("UST app cleaning registered apps hash table");
4539 /* Cleanup notify socket hash table */
4540 if (ust_app_ht_by_notify_sock
) {
4541 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4542 notify_sock_n
.node
) {
4544 * Assert that all notifiers are gone as all triggers
4545 * are unregistered prior to this clean-up.
4547 assert(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4549 ust_app_notify_sock_unregister(app
->notify_sock
);
4554 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4555 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4557 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4561 /* Cleanup socket hash table */
4562 if (ust_app_ht_by_sock
) {
4563 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4565 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4572 /* Destroy is done only when the ht is empty */
4574 ht_cleanup_push(ust_app_ht
);
4576 if (ust_app_ht_by_sock
) {
4577 ht_cleanup_push(ust_app_ht_by_sock
);
4579 if (ust_app_ht_by_notify_sock
) {
4580 ht_cleanup_push(ust_app_ht_by_notify_sock
);
4585 * Init UST app hash table.
4587 int ust_app_ht_alloc(void)
4589 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4593 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4594 if (!ust_app_ht_by_sock
) {
4597 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4598 if (!ust_app_ht_by_notify_sock
) {
4605 * For a specific UST session, disable the channel for all registered apps.
4607 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4608 struct ltt_ust_channel
*uchan
)
4611 struct lttng_ht_iter iter
;
4612 struct lttng_ht_node_str
*ua_chan_node
;
4613 struct ust_app
*app
;
4614 struct ust_app_session
*ua_sess
;
4615 struct ust_app_channel
*ua_chan
;
4617 assert(usess
->active
);
4618 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4619 uchan
->name
, usess
->id
);
4623 /* For every registered applications */
4624 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4625 struct lttng_ht_iter uiter
;
4626 if (!app
->compatible
) {
4628 * TODO: In time, we should notice the caller of this error by
4629 * telling him that this is a version error.
4633 ua_sess
= lookup_session_by_app(usess
, app
);
4634 if (ua_sess
== NULL
) {
4639 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4640 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4641 /* If the session if found for the app, the channel must be there */
4642 assert(ua_chan_node
);
4644 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4645 /* The channel must not be already disabled */
4646 assert(ua_chan
->enabled
== 1);
4648 /* Disable channel onto application */
4649 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4651 /* XXX: We might want to report this error at some point... */
4661 * For a specific UST session, enable the channel for all registered apps.
4663 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4664 struct ltt_ust_channel
*uchan
)
4667 struct lttng_ht_iter iter
;
4668 struct ust_app
*app
;
4669 struct ust_app_session
*ua_sess
;
4671 assert(usess
->active
);
4672 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4673 uchan
->name
, usess
->id
);
4677 /* For every registered applications */
4678 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4679 if (!app
->compatible
) {
4681 * TODO: In time, we should notice the caller of this error by
4682 * telling him that this is a version error.
4686 ua_sess
= lookup_session_by_app(usess
, app
);
4687 if (ua_sess
== NULL
) {
4691 /* Enable channel onto application */
4692 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4694 /* XXX: We might want to report this error at some point... */
4704 * Disable an event in a channel and for a specific session.
4706 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4707 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4710 struct lttng_ht_iter iter
, uiter
;
4711 struct lttng_ht_node_str
*ua_chan_node
;
4712 struct ust_app
*app
;
4713 struct ust_app_session
*ua_sess
;
4714 struct ust_app_channel
*ua_chan
;
4715 struct ust_app_event
*ua_event
;
4717 assert(usess
->active
);
4718 DBG("UST app disabling event %s for all apps in channel "
4719 "%s for session id %" PRIu64
,
4720 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4724 /* For all registered applications */
4725 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4726 if (!app
->compatible
) {
4728 * TODO: In time, we should notice the caller of this error by
4729 * telling him that this is a version error.
4733 ua_sess
= lookup_session_by_app(usess
, app
);
4734 if (ua_sess
== NULL
) {
4739 /* Lookup channel in the ust app session */
4740 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4741 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4742 if (ua_chan_node
== NULL
) {
4743 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4744 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4747 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4749 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4750 uevent
->filter
, uevent
->attr
.loglevel
,
4752 if (ua_event
== NULL
) {
4753 DBG2("Event %s not found in channel %s for app pid %d."
4754 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4758 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4760 /* XXX: Report error someday... */
4769 /* The ua_sess lock must be held by the caller. */
4771 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4772 struct ust_app_session
*ua_sess
,
4773 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4774 struct ust_app_channel
**_ua_chan
)
4777 struct ust_app_channel
*ua_chan
= NULL
;
4780 ASSERT_LOCKED(ua_sess
->lock
);
4782 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4783 sizeof(uchan
->name
))) {
4784 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4788 struct ltt_ust_context
*uctx
= NULL
;
4791 * Create channel onto application and synchronize its
4794 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4795 LTTNG_UST_ABI_CHAN_PER_CPU
, usess
,
4801 ret
= ust_app_channel_send(app
, usess
,
4808 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4809 ret
= create_ust_app_channel_context(ua_chan
,
4822 * The application's socket is not valid. Either a bad socket
4823 * or a timeout on it. We can't inform the caller that for a
4824 * specific app, the session failed so lets continue here.
4826 ret
= 0; /* Not an error. */
4834 if (ret
== 0 && _ua_chan
) {
4836 * Only return the application's channel on success. Note
4837 * that the channel can still be part of the application's
4838 * channel hashtable on error.
4840 *_ua_chan
= ua_chan
;
4846 * Enable event for a specific session and channel on the tracer.
4848 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4849 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4852 struct lttng_ht_iter iter
, uiter
;
4853 struct lttng_ht_node_str
*ua_chan_node
;
4854 struct ust_app
*app
;
4855 struct ust_app_session
*ua_sess
;
4856 struct ust_app_channel
*ua_chan
;
4857 struct ust_app_event
*ua_event
;
4859 assert(usess
->active
);
4860 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4861 uevent
->attr
.name
, usess
->id
);
4864 * NOTE: At this point, this function is called only if the session and
4865 * channel passed are already created for all apps. and enabled on the
4871 /* For all registered applications */
4872 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4873 if (!app
->compatible
) {
4875 * TODO: In time, we should notice the caller of this error by
4876 * telling him that this is a version error.
4880 ua_sess
= lookup_session_by_app(usess
, app
);
4882 /* The application has problem or is probably dead. */
4886 pthread_mutex_lock(&ua_sess
->lock
);
4888 if (ua_sess
->deleted
) {
4889 pthread_mutex_unlock(&ua_sess
->lock
);
4893 /* Lookup channel in the ust app session */
4894 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4895 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4897 * It is possible that the channel cannot be found is
4898 * the channel/event creation occurs concurrently with
4899 * an application exit.
4901 if (!ua_chan_node
) {
4902 pthread_mutex_unlock(&ua_sess
->lock
);
4906 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4908 /* Get event node */
4909 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4910 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4911 if (ua_event
== NULL
) {
4912 DBG3("UST app enable event %s not found for app PID %d."
4913 "Skipping app", uevent
->attr
.name
, app
->pid
);
4917 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4919 pthread_mutex_unlock(&ua_sess
->lock
);
4923 pthread_mutex_unlock(&ua_sess
->lock
);
4932 * For a specific existing UST session and UST channel, creates the event for
4933 * all registered apps.
4935 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4936 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4939 struct lttng_ht_iter iter
, uiter
;
4940 struct lttng_ht_node_str
*ua_chan_node
;
4941 struct ust_app
*app
;
4942 struct ust_app_session
*ua_sess
;
4943 struct ust_app_channel
*ua_chan
;
4945 assert(usess
->active
);
4946 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4947 uevent
->attr
.name
, usess
->id
);
4951 /* For all registered applications */
4952 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4953 if (!app
->compatible
) {
4955 * TODO: In time, we should notice the caller of this error by
4956 * telling him that this is a version error.
4960 ua_sess
= lookup_session_by_app(usess
, app
);
4962 /* The application has problem or is probably dead. */
4966 pthread_mutex_lock(&ua_sess
->lock
);
4968 if (ua_sess
->deleted
) {
4969 pthread_mutex_unlock(&ua_sess
->lock
);
4973 /* Lookup channel in the ust app session */
4974 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4975 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4976 /* If the channel is not found, there is a code flow error */
4977 assert(ua_chan_node
);
4979 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4981 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4982 pthread_mutex_unlock(&ua_sess
->lock
);
4984 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4985 /* Possible value at this point: -ENOMEM. If so, we stop! */
4988 DBG2("UST app event %s already exist on app PID %d",
4989 uevent
->attr
.name
, app
->pid
);
4999 * Start tracing for a specific UST session and app.
5001 * Called with UST app session lock held.
5005 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5008 struct ust_app_session
*ua_sess
;
5010 DBG("Starting tracing for ust app pid %d", app
->pid
);
5014 if (!app
->compatible
) {
5018 ua_sess
= lookup_session_by_app(usess
, app
);
5019 if (ua_sess
== NULL
) {
5020 /* The session is in teardown process. Ignore and continue. */
5024 pthread_mutex_lock(&ua_sess
->lock
);
5026 if (ua_sess
->deleted
) {
5027 pthread_mutex_unlock(&ua_sess
->lock
);
5031 if (ua_sess
->enabled
) {
5032 pthread_mutex_unlock(&ua_sess
->lock
);
5036 /* Upon restart, we skip the setup, already done */
5037 if (ua_sess
->started
) {
5041 health_code_update();
5044 /* This starts the UST tracing */
5045 pthread_mutex_lock(&app
->sock_lock
);
5046 ret
= lttng_ust_ctl_start_session(app
->sock
, ua_sess
->handle
);
5047 pthread_mutex_unlock(&app
->sock_lock
);
5049 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5050 ERR("Error starting tracing for app pid: %d (ret: %d)",
5053 DBG("UST app start session failed. Application is dead.");
5055 * This is normal behavior, an application can die during the
5056 * creation process. Don't report an error so the execution can
5057 * continue normally.
5059 pthread_mutex_unlock(&ua_sess
->lock
);
5065 /* Indicate that the session has been started once */
5066 ua_sess
->started
= 1;
5067 ua_sess
->enabled
= 1;
5069 pthread_mutex_unlock(&ua_sess
->lock
);
5071 health_code_update();
5073 /* Quiescent wait after starting trace */
5074 pthread_mutex_lock(&app
->sock_lock
);
5075 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5076 pthread_mutex_unlock(&app
->sock_lock
);
5077 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5078 ERR("UST app wait quiescent failed for app pid %d ret %d",
5084 health_code_update();
5088 pthread_mutex_unlock(&ua_sess
->lock
);
5090 health_code_update();
5095 * Stop tracing for a specific UST session and app.
5098 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5101 struct ust_app_session
*ua_sess
;
5102 struct ust_registry_session
*registry
;
5104 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5108 if (!app
->compatible
) {
5109 goto end_no_session
;
5112 ua_sess
= lookup_session_by_app(usess
, app
);
5113 if (ua_sess
== NULL
) {
5114 goto end_no_session
;
5117 pthread_mutex_lock(&ua_sess
->lock
);
5119 if (ua_sess
->deleted
) {
5120 pthread_mutex_unlock(&ua_sess
->lock
);
5121 goto end_no_session
;
5125 * If started = 0, it means that stop trace has been called for a session
5126 * that was never started. It's possible since we can have a fail start
5127 * from either the application manager thread or the command thread. Simply
5128 * indicate that this is a stop error.
5130 if (!ua_sess
->started
) {
5131 goto error_rcu_unlock
;
5134 health_code_update();
5136 /* This inhibits UST tracing */
5137 pthread_mutex_lock(&app
->sock_lock
);
5138 ret
= lttng_ust_ctl_stop_session(app
->sock
, ua_sess
->handle
);
5139 pthread_mutex_unlock(&app
->sock_lock
);
5141 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5142 ERR("Error stopping tracing for app pid: %d (ret: %d)",
5145 DBG("UST app stop session failed. Application is dead.");
5147 * This is normal behavior, an application can die during the
5148 * creation process. Don't report an error so the execution can
5149 * continue normally.
5153 goto error_rcu_unlock
;
5156 health_code_update();
5157 ua_sess
->enabled
= 0;
5159 /* Quiescent wait after stopping trace */
5160 pthread_mutex_lock(&app
->sock_lock
);
5161 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5162 pthread_mutex_unlock(&app
->sock_lock
);
5163 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5164 ERR("UST app wait quiescent failed for app pid %d ret %d",
5168 health_code_update();
5170 registry
= get_session_registry(ua_sess
);
5172 /* The UST app session is held registry shall not be null. */
5175 /* Push metadata for application before freeing the application. */
5176 (void) push_metadata(registry
, ua_sess
->consumer
);
5179 pthread_mutex_unlock(&ua_sess
->lock
);
5182 health_code_update();
5186 pthread_mutex_unlock(&ua_sess
->lock
);
5188 health_code_update();
5193 int ust_app_flush_app_session(struct ust_app
*app
,
5194 struct ust_app_session
*ua_sess
)
5196 int ret
, retval
= 0;
5197 struct lttng_ht_iter iter
;
5198 struct ust_app_channel
*ua_chan
;
5199 struct consumer_socket
*socket
;
5201 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5205 if (!app
->compatible
) {
5206 goto end_not_compatible
;
5209 pthread_mutex_lock(&ua_sess
->lock
);
5211 if (ua_sess
->deleted
) {
5215 health_code_update();
5217 /* Flushing buffers */
5218 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5221 /* Flush buffers and push metadata. */
5222 switch (ua_sess
->buffer_type
) {
5223 case LTTNG_BUFFER_PER_PID
:
5224 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5226 health_code_update();
5227 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5229 ERR("Error flushing consumer channel");
5235 case LTTNG_BUFFER_PER_UID
:
5241 health_code_update();
5244 pthread_mutex_unlock(&ua_sess
->lock
);
5248 health_code_update();
5253 * Flush buffers for all applications for a specific UST session.
5254 * Called with UST session lock held.
5257 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5262 DBG("Flushing session buffers for all ust apps");
5266 /* Flush buffers and push metadata. */
5267 switch (usess
->buffer_type
) {
5268 case LTTNG_BUFFER_PER_UID
:
5270 struct buffer_reg_uid
*reg
;
5271 struct lttng_ht_iter iter
;
5273 /* Flush all per UID buffers associated to that session. */
5274 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5275 struct ust_registry_session
*ust_session_reg
;
5276 struct buffer_reg_channel
*buf_reg_chan
;
5277 struct consumer_socket
*socket
;
5279 /* Get consumer socket to use to push the metadata.*/
5280 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5283 /* Ignore request if no consumer is found for the session. */
5287 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5288 buf_reg_chan
, node
.node
) {
5290 * The following call will print error values so the return
5291 * code is of little importance because whatever happens, we
5292 * have to try them all.
5294 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5297 ust_session_reg
= reg
->registry
->reg
.ust
;
5298 /* Push metadata. */
5299 (void) push_metadata(ust_session_reg
, usess
->consumer
);
5303 case LTTNG_BUFFER_PER_PID
:
5305 struct ust_app_session
*ua_sess
;
5306 struct lttng_ht_iter iter
;
5307 struct ust_app
*app
;
5309 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5310 ua_sess
= lookup_session_by_app(usess
, app
);
5311 if (ua_sess
== NULL
) {
5314 (void) ust_app_flush_app_session(app
, ua_sess
);
5325 health_code_update();
5330 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5331 struct ust_app_session
*ua_sess
)
5334 struct lttng_ht_iter iter
;
5335 struct ust_app_channel
*ua_chan
;
5336 struct consumer_socket
*socket
;
5338 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5342 if (!app
->compatible
) {
5343 goto end_not_compatible
;
5346 pthread_mutex_lock(&ua_sess
->lock
);
5348 if (ua_sess
->deleted
) {
5352 health_code_update();
5354 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5357 ERR("Failed to find consumer (%" PRIu32
") socket",
5358 app
->bits_per_long
);
5363 /* Clear quiescent state. */
5364 switch (ua_sess
->buffer_type
) {
5365 case LTTNG_BUFFER_PER_PID
:
5366 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5367 ua_chan
, node
.node
) {
5368 health_code_update();
5369 ret
= consumer_clear_quiescent_channel(socket
,
5372 ERR("Error clearing quiescent state for consumer channel");
5378 case LTTNG_BUFFER_PER_UID
:
5385 health_code_update();
5388 pthread_mutex_unlock(&ua_sess
->lock
);
5392 health_code_update();
5397 * Clear quiescent state in each stream for all applications for a
5398 * specific UST session.
5399 * Called with UST session lock held.
5402 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5407 DBG("Clearing stream quiescent state for all ust apps");
5411 switch (usess
->buffer_type
) {
5412 case LTTNG_BUFFER_PER_UID
:
5414 struct lttng_ht_iter iter
;
5415 struct buffer_reg_uid
*reg
;
5418 * Clear quiescent for all per UID buffers associated to
5421 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5422 struct consumer_socket
*socket
;
5423 struct buffer_reg_channel
*buf_reg_chan
;
5425 /* Get associated consumer socket.*/
5426 socket
= consumer_find_socket_by_bitness(
5427 reg
->bits_per_long
, usess
->consumer
);
5430 * Ignore request if no consumer is found for
5436 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5437 &iter
.iter
, buf_reg_chan
, node
.node
) {
5439 * The following call will print error values so
5440 * the return code is of little importance
5441 * because whatever happens, we have to try them
5444 (void) consumer_clear_quiescent_channel(socket
,
5445 buf_reg_chan
->consumer_key
);
5450 case LTTNG_BUFFER_PER_PID
:
5452 struct ust_app_session
*ua_sess
;
5453 struct lttng_ht_iter iter
;
5454 struct ust_app
*app
;
5456 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5458 ua_sess
= lookup_session_by_app(usess
, app
);
5459 if (ua_sess
== NULL
) {
5462 (void) ust_app_clear_quiescent_app_session(app
,
5474 health_code_update();
5479 * Destroy a specific UST session in apps.
5481 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5484 struct ust_app_session
*ua_sess
;
5485 struct lttng_ht_iter iter
;
5486 struct lttng_ht_node_u64
*node
;
5488 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5492 if (!app
->compatible
) {
5496 __lookup_session_by_app(usess
, app
, &iter
);
5497 node
= lttng_ht_iter_get_node_u64(&iter
);
5499 /* Session is being or is deleted. */
5502 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
5504 health_code_update();
5505 destroy_app_session(app
, ua_sess
);
5507 health_code_update();
5509 /* Quiescent wait after stopping trace */
5510 pthread_mutex_lock(&app
->sock_lock
);
5511 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5512 pthread_mutex_unlock(&app
->sock_lock
);
5513 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5514 ERR("UST app wait quiescent failed for app pid %d ret %d",
5519 health_code_update();
5524 * Start tracing for the UST session.
5526 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5528 struct lttng_ht_iter iter
;
5529 struct ust_app
*app
;
5531 DBG("Starting all UST traces");
5534 * Even though the start trace might fail, flag this session active so
5535 * other application coming in are started by default.
5542 * In a start-stop-start use-case, we need to clear the quiescent state
5543 * of each channel set by the prior stop command, thus ensuring that a
5544 * following stop or destroy is sure to grab a timestamp_end near those
5545 * operations, even if the packet is empty.
5547 (void) ust_app_clear_quiescent_session(usess
);
5549 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5550 ust_app_global_update(usess
, app
);
5559 * Start tracing for the UST session.
5560 * Called with UST session lock held.
5562 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5565 struct lttng_ht_iter iter
;
5566 struct ust_app
*app
;
5568 DBG("Stopping all UST traces");
5571 * Even though the stop trace might fail, flag this session inactive so
5572 * other application coming in are not started by default.
5578 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5579 ret
= ust_app_stop_trace(usess
, app
);
5581 /* Continue to next apps even on error */
5586 (void) ust_app_flush_session(usess
);
5594 * Destroy app UST session.
5596 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5599 struct lttng_ht_iter iter
;
5600 struct ust_app
*app
;
5602 DBG("Destroy all UST traces");
5606 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5607 ret
= destroy_trace(usess
, app
);
5609 /* Continue to next apps even on error */
5619 /* The ua_sess lock must be held by the caller. */
5621 int find_or_create_ust_app_channel(
5622 struct ltt_ust_session
*usess
,
5623 struct ust_app_session
*ua_sess
,
5624 struct ust_app
*app
,
5625 struct ltt_ust_channel
*uchan
,
5626 struct ust_app_channel
**ua_chan
)
5629 struct lttng_ht_iter iter
;
5630 struct lttng_ht_node_str
*ua_chan_node
;
5632 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5633 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5635 *ua_chan
= caa_container_of(ua_chan_node
,
5636 struct ust_app_channel
, node
);
5640 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5649 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5650 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5651 struct ust_app
*app
)
5654 struct ust_app_event
*ua_event
= NULL
;
5656 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5657 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5659 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5664 if (ua_event
->enabled
!= uevent
->enabled
) {
5665 ret
= uevent
->enabled
?
5666 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5667 disable_ust_app_event(ua_sess
, ua_event
, app
);
5675 /* Called with RCU read-side lock held. */
5677 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5680 enum lttng_error_code ret_code
;
5681 enum lttng_trigger_status t_status
;
5682 struct lttng_ht_iter app_trigger_iter
;
5683 struct lttng_triggers
*triggers
= NULL
;
5684 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5685 unsigned int count
, i
;
5687 if (!ust_app_supports_notifiers(app
)) {
5692 * Currrently, registering or unregistering a trigger with an
5693 * event rule condition causes a full synchronization of the event
5696 * The first step attempts to add an event notifier for all registered
5697 * triggers that apply to the user space tracers. Then, the
5698 * application's event notifiers rules are all checked against the list
5699 * of registered triggers. Any event notifier that doesn't have a
5700 * matching trigger can be assumed to have been disabled.
5702 * All of this is inefficient, but is put in place to get the feature
5703 * rolling as it is simpler at this moment. It will be optimized Soon™
5704 * to allow the state of enabled
5705 * event notifiers to be synchronized in a piece-wise way.
5708 /* Get all triggers using uid 0 (root) */
5709 ret_code
= notification_thread_command_list_triggers(
5710 the_notification_thread_handle
, 0, &triggers
);
5711 if (ret_code
!= LTTNG_OK
) {
5717 t_status
= lttng_triggers_get_count(triggers
, &count
);
5718 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5722 for (i
= 0; i
< count
; i
++) {
5723 struct lttng_condition
*condition
;
5724 struct lttng_event_rule
*event_rule
;
5725 struct lttng_trigger
*trigger
;
5726 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5727 enum lttng_condition_status condition_status
;
5730 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5733 token
= lttng_trigger_get_tracer_token(trigger
);
5734 condition
= lttng_trigger_get_condition(trigger
);
5736 if (lttng_condition_get_type(condition
) !=
5737 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
) {
5738 /* Does not apply */
5743 lttng_condition_event_rule_matches_borrow_rule_mutable(
5744 condition
, &event_rule
);
5745 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5747 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5748 /* Skip kernel related triggers. */
5753 * Find or create the associated token event rule. The caller
5754 * holds the RCU read lock, so this is safe to call without
5755 * explicitly acquiring it here.
5757 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
5758 app
->token_to_event_notifier_rule_ht
, token
);
5759 if (!looked_up_event_notifier_rule
) {
5760 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
5768 /* Remove all unknown event sources from the app. */
5769 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
5770 &app_trigger_iter
.iter
, event_notifier_rule
,
5772 const uint64_t app_token
= event_notifier_rule
->token
;
5776 * Check if the app event trigger still exists on the
5777 * notification side.
5779 for (i
= 0; i
< count
; i
++) {
5780 uint64_t notification_thread_token
;
5781 const struct lttng_trigger
*trigger
=
5782 lttng_triggers_get_at_index(
5787 notification_thread_token
=
5788 lttng_trigger_get_tracer_token(trigger
);
5790 if (notification_thread_token
== app_token
) {
5802 * This trigger was unregistered, disable it on the tracer's
5805 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
5809 /* Callee logs errors. */
5810 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
5812 delete_ust_app_event_notifier_rule(
5813 app
->sock
, event_notifier_rule
, app
);
5819 lttng_triggers_destroy(triggers
);
5824 * RCU read lock must be held by the caller.
5827 void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
5828 struct ust_app_session
*ua_sess
,
5829 struct ust_app
*app
)
5832 struct cds_lfht_iter uchan_iter
;
5833 struct ltt_ust_channel
*uchan
;
5839 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5841 struct ust_app_channel
*ua_chan
;
5842 struct cds_lfht_iter uevent_iter
;
5843 struct ltt_ust_event
*uevent
;
5846 * Search for a matching ust_app_channel. If none is found,
5847 * create it. Creating the channel will cause the ua_chan
5848 * structure to be allocated, the channel buffers to be
5849 * allocated (if necessary) and sent to the application, and
5850 * all enabled contexts will be added to the channel.
5852 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5853 app
, uchan
, &ua_chan
);
5855 /* Tracer is probably gone or ENOMEM. */
5860 /* ua_chan will be NULL for the metadata channel */
5864 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5866 ret
= ust_app_channel_synchronize_event(ua_chan
,
5867 uevent
, ua_sess
, app
);
5873 if (ua_chan
->enabled
!= uchan
->enabled
) {
5874 ret
= uchan
->enabled
?
5875 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5876 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5887 * The caller must ensure that the application is compatible and is tracked
5888 * by the process attribute trackers.
5891 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5892 struct ust_app
*app
)
5895 struct ust_app_session
*ua_sess
= NULL
;
5898 * The application's configuration should only be synchronized for
5901 assert(usess
->active
);
5903 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5905 /* Tracer is probably gone or ENOMEM. */
5907 destroy_app_session(app
, ua_sess
);
5913 pthread_mutex_lock(&ua_sess
->lock
);
5914 if (ua_sess
->deleted
) {
5915 goto deleted_session
;
5920 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
5923 * Create the metadata for the application. This returns gracefully if a
5924 * metadata was already set for the session.
5926 * The metadata channel must be created after the data channels as the
5927 * consumer daemon assumes this ordering. When interacting with a relay
5928 * daemon, the consumer will use this assumption to send the
5929 * "STREAMS_SENT" message to the relay daemon.
5931 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5933 ERR("Metadata creation failed for app sock %d for session id %" PRIu64
,
5934 app
->sock
, usess
->id
);
5940 pthread_mutex_unlock(&ua_sess
->lock
);
5946 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5948 struct ust_app_session
*ua_sess
;
5950 ua_sess
= lookup_session_by_app(usess
, app
);
5951 if (ua_sess
== NULL
) {
5954 destroy_app_session(app
, ua_sess
);
5958 * Add channels/events from UST global domain to registered apps at sock.
5960 * Called with session lock held.
5961 * Called with RCU read-side lock held.
5963 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5966 assert(usess
->active
);
5968 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5969 app
->sock
, usess
->id
);
5971 if (!app
->compatible
) {
5974 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5976 trace_ust_id_tracker_lookup(
5977 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5979 trace_ust_id_tracker_lookup(
5980 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5983 * Synchronize the application's internal tracing configuration
5984 * and start tracing.
5986 ust_app_synchronize(usess
, app
);
5987 ust_app_start_trace(usess
, app
);
5989 ust_app_global_destroy(usess
, app
);
5994 * Add all event notifiers to an application.
5996 * Called with session lock held.
5997 * Called with RCU read-side lock held.
5999 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
6001 DBG2("UST application global event notifier rules update: app = '%s' (ppid: %d)",
6002 app
->name
, app
->ppid
);
6004 if (!app
->compatible
|| !ust_app_supports_notifiers(app
)) {
6008 if (app
->event_notifier_group
.object
== NULL
) {
6009 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s' (ppid: %d)",
6010 app
->name
, app
->ppid
);
6014 ust_app_synchronize_event_notifier_rules(app
);
6018 * Called with session lock held.
6020 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
6022 struct lttng_ht_iter iter
;
6023 struct ust_app
*app
;
6026 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6027 ust_app_global_update(usess
, app
);
6032 void ust_app_global_update_all_event_notifier_rules(void)
6034 struct lttng_ht_iter iter
;
6035 struct ust_app
*app
;
6038 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6039 ust_app_global_update_event_notifier_rules(app
);
6046 * Add context to a specific channel for global UST domain.
6048 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
6049 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
6052 struct lttng_ht_node_str
*ua_chan_node
;
6053 struct lttng_ht_iter iter
, uiter
;
6054 struct ust_app_channel
*ua_chan
= NULL
;
6055 struct ust_app_session
*ua_sess
;
6056 struct ust_app
*app
;
6058 assert(usess
->active
);
6061 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6062 if (!app
->compatible
) {
6064 * TODO: In time, we should notice the caller of this error by
6065 * telling him that this is a version error.
6069 ua_sess
= lookup_session_by_app(usess
, app
);
6070 if (ua_sess
== NULL
) {
6074 pthread_mutex_lock(&ua_sess
->lock
);
6076 if (ua_sess
->deleted
) {
6077 pthread_mutex_unlock(&ua_sess
->lock
);
6081 /* Lookup channel in the ust app session */
6082 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
6083 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6084 if (ua_chan_node
== NULL
) {
6087 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
6089 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6094 pthread_mutex_unlock(&ua_sess
->lock
);
6102 * Receive registration and populate the given msg structure.
6104 * On success return 0 else a negative value returned by the ustctl call.
6106 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6109 uint32_t pid
, ppid
, uid
, gid
;
6113 ret
= lttng_ust_ctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
6114 &pid
, &ppid
, &uid
, &gid
,
6115 &msg
->bits_per_long
,
6116 &msg
->uint8_t_alignment
,
6117 &msg
->uint16_t_alignment
,
6118 &msg
->uint32_t_alignment
,
6119 &msg
->uint64_t_alignment
,
6120 &msg
->long_alignment
,
6127 case LTTNG_UST_ERR_EXITING
:
6128 DBG3("UST app recv reg message failed. Application died");
6130 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6131 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6132 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
6133 LTTNG_UST_ABI_MINOR_VERSION
);
6136 ERR("UST app recv reg message failed with ret %d", ret
);
6141 msg
->pid
= (pid_t
) pid
;
6142 msg
->ppid
= (pid_t
) ppid
;
6143 msg
->uid
= (uid_t
) uid
;
6144 msg
->gid
= (gid_t
) gid
;
6151 * Return a ust app session object using the application object and the
6152 * session object descriptor has a key. If not found, NULL is returned.
6153 * A RCU read side lock MUST be acquired when calling this function.
6155 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
6158 struct lttng_ht_node_ulong
*node
;
6159 struct lttng_ht_iter iter
;
6160 struct ust_app_session
*ua_sess
= NULL
;
6164 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
6165 node
= lttng_ht_iter_get_node_ulong(&iter
);
6167 DBG2("UST app session find by objd %d not found", objd
);
6171 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
6178 * Return a ust app channel object using the application object and the channel
6179 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6180 * lock MUST be acquired before calling this function.
6182 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
6185 struct lttng_ht_node_ulong
*node
;
6186 struct lttng_ht_iter iter
;
6187 struct ust_app_channel
*ua_chan
= NULL
;
6191 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
6192 node
= lttng_ht_iter_get_node_ulong(&iter
);
6194 DBG2("UST app channel find by objd %d not found", objd
);
6198 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
6205 * Reply to a register channel notification from an application on the notify
6206 * socket. The channel metadata is also created.
6208 * The session UST registry lock is acquired in this function.
6210 * On success 0 is returned else a negative value.
6212 static int reply_ust_register_channel(int sock
, int cobjd
,
6213 size_t nr_fields
, struct lttng_ust_ctl_field
*fields
)
6215 int ret
, ret_code
= 0;
6217 uint64_t chan_reg_key
;
6218 enum lttng_ust_ctl_channel_header type
;
6219 struct ust_app
*app
;
6220 struct ust_app_channel
*ua_chan
;
6221 struct ust_app_session
*ua_sess
;
6222 struct ust_registry_session
*registry
;
6223 struct ust_registry_channel
*ust_reg_chan
;
6227 /* Lookup application. If not found, there is a code flow error. */
6228 app
= find_app_by_notify_sock(sock
);
6230 DBG("Application socket %d is being torn down. Abort event notify",
6233 goto error_rcu_unlock
;
6236 /* Lookup channel by UST object descriptor. */
6237 ua_chan
= find_channel_by_objd(app
, cobjd
);
6239 DBG("Application channel is being torn down. Abort event notify");
6241 goto error_rcu_unlock
;
6244 assert(ua_chan
->session
);
6245 ua_sess
= ua_chan
->session
;
6247 /* Get right session registry depending on the session buffer type. */
6248 registry
= get_session_registry(ua_sess
);
6250 DBG("Application session is being torn down. Abort event notify");
6252 goto error_rcu_unlock
;
6255 /* Depending on the buffer type, a different channel key is used. */
6256 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6257 chan_reg_key
= ua_chan
->tracing_channel_id
;
6259 chan_reg_key
= ua_chan
->key
;
6262 pthread_mutex_lock(®istry
->lock
);
6264 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
6265 assert(ust_reg_chan
);
6267 if (!ust_reg_chan
->register_done
) {
6269 * TODO: eventually use the registry event count for
6270 * this channel to better guess header type for per-pid
6273 type
= LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
;
6274 ust_reg_chan
->nr_ctx_fields
= nr_fields
;
6275 ust_reg_chan
->ctx_fields
= fields
;
6277 ust_reg_chan
->header_type
= type
;
6279 /* Get current already assigned values. */
6280 type
= ust_reg_chan
->header_type
;
6282 /* Channel id is set during the object creation. */
6283 chan_id
= ust_reg_chan
->chan_id
;
6285 /* Append to metadata */
6286 if (!ust_reg_chan
->metadata_dumped
) {
6287 ret_code
= ust_metadata_channel_statedump(registry
, ust_reg_chan
);
6289 ERR("Error appending channel metadata (errno = %d)", ret_code
);
6295 DBG3("UST app replying to register channel key %" PRIu64
6296 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
6299 ret
= lttng_ust_ctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
6301 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6302 ERR("UST app reply channel failed with ret %d", ret
);
6304 DBG3("UST app reply channel failed. Application died");
6309 /* This channel registry registration is completed. */
6310 ust_reg_chan
->register_done
= 1;
6313 pthread_mutex_unlock(®istry
->lock
);
6321 * Add event to the UST channel registry. When the event is added to the
6322 * registry, the metadata is also created. Once done, this replies to the
6323 * application with the appropriate error code.
6325 * The session UST registry lock is acquired in the function.
6327 * On success 0 is returned else a negative value.
6329 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
6330 char *sig
, size_t nr_fields
, struct lttng_ust_ctl_field
*fields
,
6331 int loglevel_value
, char *model_emf_uri
)
6334 uint32_t event_id
= 0;
6335 uint64_t chan_reg_key
;
6336 struct ust_app
*app
;
6337 struct ust_app_channel
*ua_chan
;
6338 struct ust_app_session
*ua_sess
;
6339 struct ust_registry_session
*registry
;
6343 /* Lookup application. If not found, there is a code flow error. */
6344 app
= find_app_by_notify_sock(sock
);
6346 DBG("Application socket %d is being torn down. Abort event notify",
6349 goto error_rcu_unlock
;
6352 /* Lookup channel by UST object descriptor. */
6353 ua_chan
= find_channel_by_objd(app
, cobjd
);
6355 DBG("Application channel is being torn down. Abort event notify");
6357 goto error_rcu_unlock
;
6360 assert(ua_chan
->session
);
6361 ua_sess
= ua_chan
->session
;
6363 registry
= get_session_registry(ua_sess
);
6365 DBG("Application session is being torn down. Abort event notify");
6367 goto error_rcu_unlock
;
6370 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6371 chan_reg_key
= ua_chan
->tracing_channel_id
;
6373 chan_reg_key
= ua_chan
->key
;
6376 pthread_mutex_lock(®istry
->lock
);
6379 * From this point on, this call acquires the ownership of the sig, fields
6380 * and model_emf_uri meaning any free are done inside it if needed. These
6381 * three variables MUST NOT be read/write after this.
6383 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
6384 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
6385 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
6389 model_emf_uri
= NULL
;
6392 * The return value is returned to ustctl so in case of an error, the
6393 * application can be notified. In case of an error, it's important not to
6394 * return a negative error or else the application will get closed.
6396 ret
= lttng_ust_ctl_reply_register_event(sock
, event_id
, ret_code
);
6398 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6399 ERR("UST app reply event failed with ret %d", ret
);
6401 DBG3("UST app reply event failed. Application died");
6404 * No need to wipe the create event since the application socket will
6405 * get close on error hence cleaning up everything by itself.
6410 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6414 pthread_mutex_unlock(®istry
->lock
);
6419 free(model_emf_uri
);
6424 * Add enum to the UST session registry. Once done, this replies to the
6425 * application with the appropriate error code.
6427 * The session UST registry lock is acquired within this function.
6429 * On success 0 is returned else a negative value.
6431 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
6432 struct lttng_ust_ctl_enum_entry
*entries
, size_t nr_entries
)
6434 int ret
= 0, ret_code
;
6435 struct ust_app
*app
;
6436 struct ust_app_session
*ua_sess
;
6437 struct ust_registry_session
*registry
;
6438 uint64_t enum_id
= -1ULL;
6442 /* Lookup application. If not found, there is a code flow error. */
6443 app
= find_app_by_notify_sock(sock
);
6445 /* Return an error since this is not an error */
6446 DBG("Application socket %d is being torn down. Aborting enum registration",
6449 goto error_rcu_unlock
;
6452 /* Lookup session by UST object descriptor. */
6453 ua_sess
= find_session_by_objd(app
, sobjd
);
6455 /* Return an error since this is not an error */
6456 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6458 goto error_rcu_unlock
;
6461 registry
= get_session_registry(ua_sess
);
6463 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6465 goto error_rcu_unlock
;
6468 pthread_mutex_lock(®istry
->lock
);
6471 * From this point on, the callee acquires the ownership of
6472 * entries. The variable entries MUST NOT be read/written after
6475 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
6476 entries
, nr_entries
, &enum_id
);
6480 * The return value is returned to ustctl so in case of an error, the
6481 * application can be notified. In case of an error, it's important not to
6482 * return a negative error or else the application will get closed.
6484 ret
= lttng_ust_ctl_reply_register_enum(sock
, enum_id
, ret_code
);
6486 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6487 ERR("UST app reply enum failed with ret %d", ret
);
6489 DBG3("UST app reply enum failed. Application died");
6492 * No need to wipe the create enum since the application socket will
6493 * get close on error hence cleaning up everything by itself.
6498 DBG3("UST registry enum %s added successfully or already found", name
);
6501 pthread_mutex_unlock(®istry
->lock
);
6508 * Handle application notification through the given notify socket.
6510 * Return 0 on success or else a negative value.
6512 int ust_app_recv_notify(int sock
)
6515 enum lttng_ust_ctl_notify_cmd cmd
;
6517 DBG3("UST app receiving notify from sock %d", sock
);
6519 ret
= lttng_ust_ctl_recv_notify(sock
, &cmd
);
6521 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6522 ERR("UST app recv notify failed with ret %d", ret
);
6524 DBG3("UST app recv notify failed. Application died");
6530 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT
:
6532 int sobjd
, cobjd
, loglevel_value
;
6533 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6535 struct lttng_ust_ctl_field
*fields
;
6537 DBG2("UST app ustctl register event received");
6539 ret
= lttng_ust_ctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
6540 &loglevel_value
, &sig
, &nr_fields
, &fields
,
6543 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6544 ERR("UST app recv event failed with ret %d", ret
);
6546 DBG3("UST app recv event failed. Application died");
6552 * Add event to the UST registry coming from the notify socket. This
6553 * call will free if needed the sig, fields and model_emf_uri. This
6554 * code path loses the ownsership of these variables and transfer them
6555 * to the this function.
6557 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6558 fields
, loglevel_value
, model_emf_uri
);
6565 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
:
6569 struct lttng_ust_ctl_field
*fields
;
6571 DBG2("UST app ustctl register channel received");
6573 ret
= lttng_ust_ctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
6576 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6577 ERR("UST app recv channel failed with ret %d", ret
);
6579 DBG3("UST app recv channel failed. Application died");
6585 * The fields ownership are transfered to this function call meaning
6586 * that if needed it will be freed. After this, it's invalid to access
6587 * fields or clean it up.
6589 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
6597 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM
:
6600 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
6602 struct lttng_ust_ctl_enum_entry
*entries
;
6604 DBG2("UST app ustctl register enum received");
6606 ret
= lttng_ust_ctl_recv_register_enum(sock
, &sobjd
, name
,
6607 &entries
, &nr_entries
);
6609 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6610 ERR("UST app recv enum failed with ret %d", ret
);
6612 DBG3("UST app recv enum failed. Application died");
6617 /* Callee assumes ownership of entries */
6618 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6619 entries
, nr_entries
);
6627 /* Should NEVER happen. */
6636 * Once the notify socket hangs up, this is called. First, it tries to find the
6637 * corresponding application. On failure, the call_rcu to close the socket is
6638 * executed. If an application is found, it tries to delete it from the notify
6639 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6641 * Note that an object needs to be allocated here so on ENOMEM failure, the
6642 * call RCU is not done but the rest of the cleanup is.
6644 void ust_app_notify_sock_unregister(int sock
)
6647 struct lttng_ht_iter iter
;
6648 struct ust_app
*app
;
6649 struct ust_app_notify_sock_obj
*obj
;
6655 obj
= zmalloc(sizeof(*obj
));
6658 * An ENOMEM is kind of uncool. If this strikes we continue the
6659 * procedure but the call_rcu will not be called. In this case, we
6660 * accept the fd leak rather than possibly creating an unsynchronized
6661 * state between threads.
6663 * TODO: The notify object should be created once the notify socket is
6664 * registered and stored independantely from the ust app object. The
6665 * tricky part is to synchronize the teardown of the application and
6666 * this notify object. Let's keep that in mind so we can avoid this
6667 * kind of shenanigans with ENOMEM in the teardown path.
6674 DBG("UST app notify socket unregister %d", sock
);
6677 * Lookup application by notify socket. If this fails, this means that the
6678 * hash table delete has already been done by the application
6679 * unregistration process so we can safely close the notify socket in a
6682 app
= find_app_by_notify_sock(sock
);
6687 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6690 * Whatever happens here either we fail or succeed, in both cases we have
6691 * to close the socket after a grace period to continue to the call RCU
6692 * here. If the deletion is successful, the application is not visible
6693 * anymore by other threads and is it fails it means that it was already
6694 * deleted from the hash table so either way we just have to close the
6697 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6703 * Close socket after a grace period to avoid for the socket to be reused
6704 * before the application object is freed creating potential race between
6705 * threads trying to add unique in the global hash table.
6708 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6713 * Destroy a ust app data structure and free its memory.
6715 void ust_app_destroy(struct ust_app
*app
)
6721 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6725 * Take a snapshot for a given UST session. The snapshot is sent to the given
6728 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6730 enum lttng_error_code
ust_app_snapshot_record(
6731 const struct ltt_ust_session
*usess
,
6732 const struct consumer_output
*output
, int wait
,
6733 uint64_t nb_packets_per_stream
)
6736 enum lttng_error_code status
= LTTNG_OK
;
6737 struct lttng_ht_iter iter
;
6738 struct ust_app
*app
;
6739 char *trace_path
= NULL
;
6746 switch (usess
->buffer_type
) {
6747 case LTTNG_BUFFER_PER_UID
:
6749 struct buffer_reg_uid
*reg
;
6751 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6752 struct buffer_reg_channel
*buf_reg_chan
;
6753 struct consumer_socket
*socket
;
6754 char pathname
[PATH_MAX
];
6755 size_t consumer_path_offset
= 0;
6757 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6758 /* Skip since no metadata is present */
6762 /* Get consumer socket to use to push the metadata.*/
6763 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6766 status
= LTTNG_ERR_INVALID
;
6770 memset(pathname
, 0, sizeof(pathname
));
6771 ret
= snprintf(pathname
, sizeof(pathname
),
6772 DEFAULT_UST_TRACE_UID_PATH
,
6773 reg
->uid
, reg
->bits_per_long
);
6775 PERROR("snprintf snapshot path");
6776 status
= LTTNG_ERR_INVALID
;
6779 /* Free path allowed on previous iteration. */
6781 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6782 &consumer_path_offset
);
6784 status
= LTTNG_ERR_INVALID
;
6787 /* Add the UST default trace dir to path. */
6788 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6789 buf_reg_chan
, node
.node
) {
6790 status
= consumer_snapshot_channel(socket
,
6791 buf_reg_chan
->consumer_key
,
6792 output
, 0, usess
->uid
,
6793 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
6794 nb_packets_per_stream
);
6795 if (status
!= LTTNG_OK
) {
6799 status
= consumer_snapshot_channel(socket
,
6800 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6801 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6803 if (status
!= LTTNG_OK
) {
6809 case LTTNG_BUFFER_PER_PID
:
6811 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6812 struct consumer_socket
*socket
;
6813 struct lttng_ht_iter chan_iter
;
6814 struct ust_app_channel
*ua_chan
;
6815 struct ust_app_session
*ua_sess
;
6816 struct ust_registry_session
*registry
;
6817 char pathname
[PATH_MAX
];
6818 size_t consumer_path_offset
= 0;
6820 ua_sess
= lookup_session_by_app(usess
, app
);
6822 /* Session not associated with this app. */
6826 /* Get the right consumer socket for the application. */
6827 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6830 status
= LTTNG_ERR_INVALID
;
6834 /* Add the UST default trace dir to path. */
6835 memset(pathname
, 0, sizeof(pathname
));
6836 ret
= snprintf(pathname
, sizeof(pathname
), "%s",
6839 status
= LTTNG_ERR_INVALID
;
6840 PERROR("snprintf snapshot path");
6843 /* Free path allowed on previous iteration. */
6845 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6846 &consumer_path_offset
);
6848 status
= LTTNG_ERR_INVALID
;
6851 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6852 ua_chan
, node
.node
) {
6853 status
= consumer_snapshot_channel(socket
,
6854 ua_chan
->key
, output
, 0,
6855 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6856 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6857 &trace_path
[consumer_path_offset
], wait
,
6858 nb_packets_per_stream
);
6862 case LTTNG_ERR_CHAN_NOT_FOUND
:
6869 registry
= get_session_registry(ua_sess
);
6871 DBG("Application session is being torn down. Skip application.");
6874 status
= consumer_snapshot_channel(socket
,
6875 registry
->metadata_key
, output
, 1,
6876 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6877 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6878 &trace_path
[consumer_path_offset
], wait
, 0);
6882 case LTTNG_ERR_CHAN_NOT_FOUND
:
6902 * Return the size taken by one more packet per stream.
6904 uint64_t ust_app_get_size_one_more_packet_per_stream(
6905 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6907 uint64_t tot_size
= 0;
6908 struct ust_app
*app
;
6909 struct lttng_ht_iter iter
;
6913 switch (usess
->buffer_type
) {
6914 case LTTNG_BUFFER_PER_UID
:
6916 struct buffer_reg_uid
*reg
;
6918 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6919 struct buffer_reg_channel
*buf_reg_chan
;
6922 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6923 buf_reg_chan
, node
.node
) {
6924 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
6926 * Don't take channel into account if we
6927 * already grab all its packets.
6931 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
6937 case LTTNG_BUFFER_PER_PID
:
6940 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6941 struct ust_app_channel
*ua_chan
;
6942 struct ust_app_session
*ua_sess
;
6943 struct lttng_ht_iter chan_iter
;
6945 ua_sess
= lookup_session_by_app(usess
, app
);
6947 /* Session not associated with this app. */
6951 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6952 ua_chan
, node
.node
) {
6953 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6955 * Don't take channel into account if we
6956 * already grab all its packets.
6960 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6974 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6975 struct cds_list_head
*buffer_reg_uid_list
,
6976 struct consumer_output
*consumer
, uint64_t uchan_id
,
6977 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6980 uint64_t consumer_chan_key
;
6985 ret
= buffer_reg_uid_consumer_channel_key(
6986 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6994 ret
= consumer_get_lost_packets(ust_session_id
,
6995 consumer_chan_key
, consumer
, lost
);
6997 ret
= consumer_get_discarded_events(ust_session_id
,
6998 consumer_chan_key
, consumer
, discarded
);
7005 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
7006 struct ltt_ust_channel
*uchan
,
7007 struct consumer_output
*consumer
, int overwrite
,
7008 uint64_t *discarded
, uint64_t *lost
)
7011 struct lttng_ht_iter iter
;
7012 struct lttng_ht_node_str
*ua_chan_node
;
7013 struct ust_app
*app
;
7014 struct ust_app_session
*ua_sess
;
7015 struct ust_app_channel
*ua_chan
;
7022 * Iterate over every registered applications. Sum counters for
7023 * all applications containing requested session and channel.
7025 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7026 struct lttng_ht_iter uiter
;
7028 ua_sess
= lookup_session_by_app(usess
, app
);
7029 if (ua_sess
== NULL
) {
7034 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
7035 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
7036 /* If the session is found for the app, the channel must be there */
7037 assert(ua_chan_node
);
7039 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
7044 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
7051 uint64_t _discarded
;
7053 ret
= consumer_get_discarded_events(usess
->id
,
7054 ua_chan
->key
, consumer
, &_discarded
);
7058 (*discarded
) += _discarded
;
7067 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
7068 struct ust_app
*app
)
7071 struct ust_app_session
*ua_sess
;
7073 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7077 ua_sess
= lookup_session_by_app(usess
, app
);
7078 if (ua_sess
== NULL
) {
7079 /* The session is in teardown process. Ignore and continue. */
7083 pthread_mutex_lock(&ua_sess
->lock
);
7085 if (ua_sess
->deleted
) {
7089 pthread_mutex_lock(&app
->sock_lock
);
7090 ret
= lttng_ust_ctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7091 pthread_mutex_unlock(&app
->sock_lock
);
7094 pthread_mutex_unlock(&ua_sess
->lock
);
7098 health_code_update();
7103 * Regenerate the statedump for each app in the session.
7105 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7108 struct lttng_ht_iter iter
;
7109 struct ust_app
*app
;
7111 DBG("Regenerating the metadata for all UST apps");
7115 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7116 if (!app
->compatible
) {
7120 ret
= ust_app_regenerate_statedump(usess
, app
);
7122 /* Continue to the next app even on error */
7133 * Rotate all the channels of a session.
7135 * Return LTTNG_OK on success or else an LTTng error code.
7137 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7140 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7141 struct lttng_ht_iter iter
;
7142 struct ust_app
*app
;
7143 struct ltt_ust_session
*usess
= session
->ust_session
;
7149 switch (usess
->buffer_type
) {
7150 case LTTNG_BUFFER_PER_UID
:
7152 struct buffer_reg_uid
*reg
;
7154 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7155 struct buffer_reg_channel
*buf_reg_chan
;
7156 struct consumer_socket
*socket
;
7158 /* Get consumer socket to use to push the metadata.*/
7159 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7162 cmd_ret
= LTTNG_ERR_INVALID
;
7166 /* Rotate the data channels. */
7167 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7168 buf_reg_chan
, node
.node
) {
7169 ret
= consumer_rotate_channel(socket
,
7170 buf_reg_chan
->consumer_key
,
7171 usess
->uid
, usess
->gid
,
7173 /* is_metadata_channel */ false);
7175 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7181 * The metadata channel might not be present.
7183 * Consumer stream allocation can be done
7184 * asynchronously and can fail on intermediary
7185 * operations (i.e add context) and lead to data
7186 * channels created with no metadata channel.
7188 if (!reg
->registry
->reg
.ust
->metadata_key
) {
7189 /* Skip since no metadata is present. */
7193 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7195 ret
= consumer_rotate_channel(socket
,
7196 reg
->registry
->reg
.ust
->metadata_key
,
7197 usess
->uid
, usess
->gid
,
7199 /* is_metadata_channel */ true);
7201 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7207 case LTTNG_BUFFER_PER_PID
:
7209 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7210 struct consumer_socket
*socket
;
7211 struct lttng_ht_iter chan_iter
;
7212 struct ust_app_channel
*ua_chan
;
7213 struct ust_app_session
*ua_sess
;
7214 struct ust_registry_session
*registry
;
7216 ua_sess
= lookup_session_by_app(usess
, app
);
7218 /* Session not associated with this app. */
7222 /* Get the right consumer socket for the application. */
7223 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7226 cmd_ret
= LTTNG_ERR_INVALID
;
7230 registry
= get_session_registry(ua_sess
);
7232 DBG("Application session is being torn down. Skip application.");
7236 /* Rotate the data channels. */
7237 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7238 ua_chan
, node
.node
) {
7239 ret
= consumer_rotate_channel(socket
,
7241 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7242 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7244 /* is_metadata_channel */ false);
7246 /* Per-PID buffer and application going away. */
7247 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7249 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7254 /* Rotate the metadata channel. */
7255 (void) push_metadata(registry
, usess
->consumer
);
7256 ret
= consumer_rotate_channel(socket
,
7257 registry
->metadata_key
,
7258 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7259 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7261 /* is_metadata_channel */ true);
7263 /* Per-PID buffer and application going away. */
7264 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7266 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7284 enum lttng_error_code
ust_app_create_channel_subdirectories(
7285 const struct ltt_ust_session
*usess
)
7287 enum lttng_error_code ret
= LTTNG_OK
;
7288 struct lttng_ht_iter iter
;
7289 enum lttng_trace_chunk_status chunk_status
;
7290 char *pathname_index
;
7293 assert(usess
->current_trace_chunk
);
7296 switch (usess
->buffer_type
) {
7297 case LTTNG_BUFFER_PER_UID
:
7299 struct buffer_reg_uid
*reg
;
7301 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7302 fmt_ret
= asprintf(&pathname_index
,
7303 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7304 reg
->uid
, reg
->bits_per_long
);
7306 ERR("Failed to format channel index directory");
7307 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7312 * Create the index subdirectory which will take care
7313 * of implicitly creating the channel's path.
7315 chunk_status
= lttng_trace_chunk_create_subdirectory(
7316 usess
->current_trace_chunk
,
7318 free(pathname_index
);
7319 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7320 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7326 case LTTNG_BUFFER_PER_PID
:
7328 struct ust_app
*app
;
7331 * Create the toplevel ust/ directory in case no apps are running.
7333 chunk_status
= lttng_trace_chunk_create_subdirectory(
7334 usess
->current_trace_chunk
,
7335 DEFAULT_UST_TRACE_DIR
);
7336 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7337 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7341 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7343 struct ust_app_session
*ua_sess
;
7344 struct ust_registry_session
*registry
;
7346 ua_sess
= lookup_session_by_app(usess
, app
);
7348 /* Session not associated with this app. */
7352 registry
= get_session_registry(ua_sess
);
7354 DBG("Application session is being torn down. Skip application.");
7358 fmt_ret
= asprintf(&pathname_index
,
7359 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7362 ERR("Failed to format channel index directory");
7363 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7367 * Create the index subdirectory which will take care
7368 * of implicitly creating the channel's path.
7370 chunk_status
= lttng_trace_chunk_create_subdirectory(
7371 usess
->current_trace_chunk
,
7373 free(pathname_index
);
7374 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7375 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7392 * Clear all the channels of a session.
7394 * Return LTTNG_OK on success or else an LTTng error code.
7396 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7399 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7400 struct lttng_ht_iter iter
;
7401 struct ust_app
*app
;
7402 struct ltt_ust_session
*usess
= session
->ust_session
;
7408 if (usess
->active
) {
7409 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7410 cmd_ret
= LTTNG_ERR_FATAL
;
7414 switch (usess
->buffer_type
) {
7415 case LTTNG_BUFFER_PER_UID
:
7417 struct buffer_reg_uid
*reg
;
7419 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7420 struct buffer_reg_channel
*buf_reg_chan
;
7421 struct consumer_socket
*socket
;
7423 /* Get consumer socket to use to push the metadata.*/
7424 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7427 cmd_ret
= LTTNG_ERR_INVALID
;
7431 /* Clear the data channels. */
7432 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7433 buf_reg_chan
, node
.node
) {
7434 ret
= consumer_clear_channel(socket
,
7435 buf_reg_chan
->consumer_key
);
7441 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7444 * Clear the metadata channel.
7445 * Metadata channel is not cleared per se but we still need to
7446 * perform a rotation operation on it behind the scene.
7448 ret
= consumer_clear_channel(socket
,
7449 reg
->registry
->reg
.ust
->metadata_key
);
7456 case LTTNG_BUFFER_PER_PID
:
7458 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7459 struct consumer_socket
*socket
;
7460 struct lttng_ht_iter chan_iter
;
7461 struct ust_app_channel
*ua_chan
;
7462 struct ust_app_session
*ua_sess
;
7463 struct ust_registry_session
*registry
;
7465 ua_sess
= lookup_session_by_app(usess
, app
);
7467 /* Session not associated with this app. */
7471 /* Get the right consumer socket for the application. */
7472 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7475 cmd_ret
= LTTNG_ERR_INVALID
;
7479 registry
= get_session_registry(ua_sess
);
7481 DBG("Application session is being torn down. Skip application.");
7485 /* Clear the data channels. */
7486 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7487 ua_chan
, node
.node
) {
7488 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7490 /* Per-PID buffer and application going away. */
7491 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7498 (void) push_metadata(registry
, usess
->consumer
);
7501 * Clear the metadata channel.
7502 * Metadata channel is not cleared per se but we still need to
7503 * perform rotation operation on it behind the scene.
7505 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
7507 /* Per-PID buffer and application going away. */
7508 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7526 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7527 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7530 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7540 * This function skips the metadata channel as the begin/end timestamps of a
7541 * metadata packet are useless.
7543 * Moreover, opening a packet after a "clear" will cause problems for live
7544 * sessions as it will introduce padding that was not part of the first trace
7545 * chunk. The relay daemon expects the content of the metadata stream of
7546 * successive metadata trace chunks to be strict supersets of one another.
7548 * For example, flushing a packet at the beginning of the metadata stream of
7549 * a trace chunk resulting from a "clear" session command will cause the
7550 * size of the metadata stream of the new trace chunk to not match the size of
7551 * the metadata stream of the original chunk. This will confuse the relay
7552 * daemon as the same "offset" in a metadata stream will no longer point
7553 * to the same content.
7555 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7557 enum lttng_error_code ret
= LTTNG_OK
;
7558 struct lttng_ht_iter iter
;
7559 struct ltt_ust_session
*usess
= session
->ust_session
;
7565 switch (usess
->buffer_type
) {
7566 case LTTNG_BUFFER_PER_UID
:
7568 struct buffer_reg_uid
*reg
;
7570 cds_list_for_each_entry (
7571 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7572 struct buffer_reg_channel
*buf_reg_chan
;
7573 struct consumer_socket
*socket
;
7575 socket
= consumer_find_socket_by_bitness(
7576 reg
->bits_per_long
, usess
->consumer
);
7578 ret
= LTTNG_ERR_FATAL
;
7582 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7583 &iter
.iter
, buf_reg_chan
, node
.node
) {
7584 const int open_ret
=
7585 consumer_open_channel_packets(
7587 buf_reg_chan
->consumer_key
);
7590 ret
= LTTNG_ERR_UNK
;
7597 case LTTNG_BUFFER_PER_PID
:
7599 struct ust_app
*app
;
7601 cds_lfht_for_each_entry (
7602 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7603 struct consumer_socket
*socket
;
7604 struct lttng_ht_iter chan_iter
;
7605 struct ust_app_channel
*ua_chan
;
7606 struct ust_app_session
*ua_sess
;
7607 struct ust_registry_session
*registry
;
7609 ua_sess
= lookup_session_by_app(usess
, app
);
7611 /* Session not associated with this app. */
7615 /* Get the right consumer socket for the application. */
7616 socket
= consumer_find_socket_by_bitness(
7617 app
->bits_per_long
, usess
->consumer
);
7619 ret
= LTTNG_ERR_FATAL
;
7623 registry
= get_session_registry(ua_sess
);
7625 DBG("Application session is being torn down. Skip application.");
7629 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
7630 &chan_iter
.iter
, ua_chan
, node
.node
) {
7631 const int open_ret
=
7632 consumer_open_channel_packets(
7638 * Per-PID buffer and application going
7641 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7645 ret
= LTTNG_ERR_UNK
;