2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
16 #include <sys/types.h>
18 #include <urcu/compiler.h>
21 #include <common/bytecode/bytecode.h>
22 #include <common/compat/errno.h>
23 #include <common/common.h>
24 #include <common/hashtable/utils.h>
25 #include <lttng/event-rule/event-rule.h>
26 #include <lttng/event-rule/event-rule-internal.h>
27 #include <lttng/event-rule/tracepoint.h>
28 #include <lttng/condition/condition.h>
29 #include <lttng/condition/on-event-internal.h>
30 #include <lttng/condition/on-event.h>
31 #include <lttng/trigger/trigger-internal.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
34 #include "buffer-registry.h"
36 #include "health-sessiond.h"
38 #include "ust-consumer.h"
39 #include "lttng-ust-ctl.h"
40 #include "lttng-ust-error.h"
43 #include "lttng-sessiond.h"
44 #include "notification-thread-commands.h"
48 struct lttng_ht
*ust_app_ht
;
49 struct lttng_ht
*ust_app_ht_by_sock
;
50 struct lttng_ht
*ust_app_ht_by_notify_sock
;
53 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
55 /* Next available channel key. Access under next_channel_key_lock. */
56 static uint64_t _next_channel_key
;
57 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
59 /* Next available session ID. Access under next_session_id_lock. */
60 static uint64_t _next_session_id
;
61 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
64 * Return the incremented value of next_channel_key.
66 static uint64_t get_next_channel_key(void)
70 pthread_mutex_lock(&next_channel_key_lock
);
71 ret
= ++_next_channel_key
;
72 pthread_mutex_unlock(&next_channel_key_lock
);
77 * Return the atomically incremented value of next_session_id.
79 static uint64_t get_next_session_id(void)
83 pthread_mutex_lock(&next_session_id_lock
);
84 ret
= ++_next_session_id
;
85 pthread_mutex_unlock(&next_session_id_lock
);
89 static void copy_channel_attr_to_ustctl(
90 struct ustctl_consumer_channel_attr
*attr
,
91 struct lttng_ust_abi_channel_attr
*uattr
)
93 /* Copy event attributes since the layout is different. */
94 attr
->subbuf_size
= uattr
->subbuf_size
;
95 attr
->num_subbuf
= uattr
->num_subbuf
;
96 attr
->overwrite
= uattr
->overwrite
;
97 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
98 attr
->read_timer_interval
= uattr
->read_timer_interval
;
99 attr
->output
= uattr
->output
;
100 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
104 * Match function for the hash table lookup.
106 * It matches an ust app event based on three attributes which are the event
107 * name, the filter bytecode and the loglevel.
109 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
111 struct ust_app_event
*event
;
112 const struct ust_app_ht_key
*key
;
113 int ev_loglevel_value
;
118 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
120 ev_loglevel_value
= event
->attr
.loglevel
;
122 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
125 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
129 /* Event loglevel. */
130 if (ev_loglevel_value
!= key
->loglevel_type
) {
131 if (event
->attr
.loglevel_type
== LTTNG_UST_ABI_LOGLEVEL_ALL
132 && key
->loglevel_type
== 0 &&
133 ev_loglevel_value
== -1) {
135 * Match is accepted. This is because on event creation, the
136 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
137 * -1 are accepted for this loglevel type since 0 is the one set by
138 * the API when receiving an enable event.
145 /* One of the filters is NULL, fail. */
146 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
150 if (key
->filter
&& event
->filter
) {
151 /* Both filters exists, check length followed by the bytecode. */
152 if (event
->filter
->len
!= key
->filter
->len
||
153 memcmp(event
->filter
->data
, key
->filter
->data
,
154 event
->filter
->len
) != 0) {
159 /* One of the exclusions is NULL, fail. */
160 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
164 if (key
->exclusion
&& event
->exclusion
) {
165 /* Both exclusions exists, check count followed by the names. */
166 if (event
->exclusion
->count
!= key
->exclusion
->count
||
167 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
168 event
->exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) != 0) {
182 * Unique add of an ust app event in the given ht. This uses the custom
183 * ht_match_ust_app_event match function and the event name as hash.
185 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
186 struct ust_app_event
*event
)
188 struct cds_lfht_node
*node_ptr
;
189 struct ust_app_ht_key key
;
193 assert(ua_chan
->events
);
196 ht
= ua_chan
->events
;
197 key
.name
= event
->attr
.name
;
198 key
.filter
= event
->filter
;
199 key
.loglevel_type
= event
->attr
.loglevel
;
200 key
.exclusion
= event
->exclusion
;
202 node_ptr
= cds_lfht_add_unique(ht
->ht
,
203 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
204 ht_match_ust_app_event
, &key
, &event
->node
.node
);
205 assert(node_ptr
== &event
->node
.node
);
209 * Close the notify socket from the given RCU head object. This MUST be called
210 * through a call_rcu().
212 static void close_notify_sock_rcu(struct rcu_head
*head
)
215 struct ust_app_notify_sock_obj
*obj
=
216 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
218 /* Must have a valid fd here. */
219 assert(obj
->fd
>= 0);
221 ret
= close(obj
->fd
);
223 ERR("close notify sock %d RCU", obj
->fd
);
225 lttng_fd_put(LTTNG_FD_APPS
, 1);
231 * Return the session registry according to the buffer type of the given
234 * A registry per UID object MUST exists before calling this function or else
235 * it assert() if not found. RCU read side lock must be acquired.
237 static struct ust_registry_session
*get_session_registry(
238 struct ust_app_session
*ua_sess
)
240 struct ust_registry_session
*registry
= NULL
;
244 switch (ua_sess
->buffer_type
) {
245 case LTTNG_BUFFER_PER_PID
:
247 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
251 registry
= reg_pid
->registry
->reg
.ust
;
254 case LTTNG_BUFFER_PER_UID
:
256 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
257 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
258 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
262 registry
= reg_uid
->registry
->reg
.ust
;
274 * Delete ust context safely. RCU read lock must be held before calling
278 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
286 pthread_mutex_lock(&app
->sock_lock
);
287 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
288 pthread_mutex_unlock(&app
->sock_lock
);
289 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
290 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
291 sock
, ua_ctx
->obj
->handle
, ret
);
299 * Delete ust app event safely. RCU read lock must be held before calling
303 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
310 free(ua_event
->filter
);
311 if (ua_event
->exclusion
!= NULL
)
312 free(ua_event
->exclusion
);
313 if (ua_event
->obj
!= NULL
) {
314 pthread_mutex_lock(&app
->sock_lock
);
315 ret
= ustctl_release_object(sock
, ua_event
->obj
);
316 pthread_mutex_unlock(&app
->sock_lock
);
317 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
318 ERR("UST app sock %d release event obj failed with ret %d",
327 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
328 * through a call_rcu().
331 void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
333 struct ust_app_event_notifier_rule
*obj
= caa_container_of(
334 head
, struct ust_app_event_notifier_rule
, rcu_head
);
340 * Delete ust app event notifier rule safely.
342 static void delete_ust_app_event_notifier_rule(int sock
,
343 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
,
348 assert(ua_event_notifier_rule
);
350 if (ua_event_notifier_rule
->exclusion
!= NULL
) {
351 free(ua_event_notifier_rule
->exclusion
);
354 if (ua_event_notifier_rule
->obj
!= NULL
) {
355 pthread_mutex_lock(&app
->sock_lock
);
356 ret
= ustctl_release_object(sock
, ua_event_notifier_rule
->obj
);
357 pthread_mutex_unlock(&app
->sock_lock
);
358 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
359 ERR("Failed to release event notifier object: app = '%s' (ppid %d), ret = %d",
360 app
->name
, (int) app
->ppid
, ret
);
363 free(ua_event_notifier_rule
->obj
);
366 lttng_trigger_put(ua_event_notifier_rule
->trigger
);
367 call_rcu(&ua_event_notifier_rule
->rcu_head
,
368 free_ust_app_event_notifier_rule_rcu
);
372 * Release ust data object of the given stream.
374 * Return 0 on success or else a negative value.
376 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
384 pthread_mutex_lock(&app
->sock_lock
);
385 ret
= ustctl_release_object(sock
, stream
->obj
);
386 pthread_mutex_unlock(&app
->sock_lock
);
387 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
388 ERR("UST app sock %d release stream obj failed with ret %d",
391 lttng_fd_put(LTTNG_FD_APPS
, 2);
399 * Delete ust app stream safely. RCU read lock must be held before calling
403 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
408 (void) release_ust_app_stream(sock
, stream
, app
);
413 * We need to execute ht_destroy outside of RCU read-side critical
414 * section and outside of call_rcu thread, so we postpone its execution
415 * using ht_cleanup_push. It is simpler than to change the semantic of
416 * the many callers of delete_ust_app_session().
419 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
421 struct ust_app_channel
*ua_chan
=
422 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
424 ht_cleanup_push(ua_chan
->ctx
);
425 ht_cleanup_push(ua_chan
->events
);
430 * Extract the lost packet or discarded events counter when the channel is
431 * being deleted and store the value in the parent channel so we can
432 * access it from lttng list and at stop/destroy.
434 * The session list lock must be held by the caller.
437 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
439 uint64_t discarded
= 0, lost
= 0;
440 struct ltt_session
*session
;
441 struct ltt_ust_channel
*uchan
;
443 if (ua_chan
->attr
.type
!= LTTNG_UST_ABI_CHAN_PER_CPU
) {
448 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
449 if (!session
|| !session
->ust_session
) {
451 * Not finding the session is not an error because there are
452 * multiple ways the channels can be torn down.
454 * 1) The session daemon can initiate the destruction of the
455 * ust app session after receiving a destroy command or
456 * during its shutdown/teardown.
457 * 2) The application, since we are in per-pid tracing, is
458 * unregistering and tearing down its ust app session.
460 * Both paths are protected by the session list lock which
461 * ensures that the accounting of lost packets and discarded
462 * events is done exactly once. The session is then unpublished
463 * from the session list, resulting in this condition.
468 if (ua_chan
->attr
.overwrite
) {
469 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
470 ua_chan
->key
, session
->ust_session
->consumer
,
473 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
474 ua_chan
->key
, session
->ust_session
->consumer
,
477 uchan
= trace_ust_find_channel_by_name(
478 session
->ust_session
->domain_global
.channels
,
481 ERR("Missing UST channel to store discarded counters");
485 uchan
->per_pid_closed_app_discarded
+= discarded
;
486 uchan
->per_pid_closed_app_lost
+= lost
;
491 session_put(session
);
496 * Delete ust app channel safely. RCU read lock must be held before calling
499 * The session list lock must be held by the caller.
502 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
506 struct lttng_ht_iter iter
;
507 struct ust_app_event
*ua_event
;
508 struct ust_app_ctx
*ua_ctx
;
509 struct ust_app_stream
*stream
, *stmp
;
510 struct ust_registry_session
*registry
;
514 DBG3("UST app deleting channel %s", ua_chan
->name
);
517 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
518 cds_list_del(&stream
->list
);
519 delete_ust_app_stream(sock
, stream
, app
);
523 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
524 cds_list_del(&ua_ctx
->list
);
525 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
527 delete_ust_app_ctx(sock
, ua_ctx
, app
);
531 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
533 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
535 delete_ust_app_event(sock
, ua_event
, app
);
538 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
539 /* Wipe and free registry from session registry. */
540 registry
= get_session_registry(ua_chan
->session
);
542 ust_registry_channel_del_free(registry
, ua_chan
->key
,
546 * A negative socket can be used by the caller when
547 * cleaning-up a ua_chan in an error path. Skip the
548 * accounting in this case.
551 save_per_pid_lost_discarded_counters(ua_chan
);
555 if (ua_chan
->obj
!= NULL
) {
556 /* Remove channel from application UST object descriptor. */
557 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
558 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
560 pthread_mutex_lock(&app
->sock_lock
);
561 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
562 pthread_mutex_unlock(&app
->sock_lock
);
563 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
564 ERR("UST app sock %d release channel obj failed with ret %d",
567 lttng_fd_put(LTTNG_FD_APPS
, 1);
570 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
573 int ust_app_register_done(struct ust_app
*app
)
577 pthread_mutex_lock(&app
->sock_lock
);
578 ret
= ustctl_register_done(app
->sock
);
579 pthread_mutex_unlock(&app
->sock_lock
);
583 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*data
)
588 pthread_mutex_lock(&app
->sock_lock
);
593 ret
= ustctl_release_object(sock
, data
);
595 pthread_mutex_unlock(&app
->sock_lock
);
601 * Push metadata to consumer socket.
603 * RCU read-side lock must be held to guarantee existance of socket.
604 * Must be called with the ust app session lock held.
605 * Must be called with the registry lock held.
607 * On success, return the len of metadata pushed or else a negative value.
608 * Returning a -EPIPE return value means we could not send the metadata,
609 * but it can be caused by recoverable errors (e.g. the application has
610 * terminated concurrently).
612 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
613 struct consumer_socket
*socket
, int send_zero_data
)
616 char *metadata_str
= NULL
;
617 size_t len
, offset
, new_metadata_len_sent
;
619 uint64_t metadata_key
, metadata_version
;
624 metadata_key
= registry
->metadata_key
;
627 * Means that no metadata was assigned to the session. This can
628 * happens if no start has been done previously.
634 offset
= registry
->metadata_len_sent
;
635 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
636 new_metadata_len_sent
= registry
->metadata_len
;
637 metadata_version
= registry
->metadata_version
;
639 DBG3("No metadata to push for metadata key %" PRIu64
,
640 registry
->metadata_key
);
642 if (send_zero_data
) {
643 DBG("No metadata to push");
649 /* Allocate only what we have to send. */
650 metadata_str
= zmalloc(len
);
652 PERROR("zmalloc ust app metadata string");
656 /* Copy what we haven't sent out. */
657 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
660 pthread_mutex_unlock(®istry
->lock
);
662 * We need to unlock the registry while we push metadata to
663 * break a circular dependency between the consumerd metadata
664 * lock and the sessiond registry lock. Indeed, pushing metadata
665 * to the consumerd awaits that it gets pushed all the way to
666 * relayd, but doing so requires grabbing the metadata lock. If
667 * a concurrent metadata request is being performed by
668 * consumerd, this can try to grab the registry lock on the
669 * sessiond while holding the metadata lock on the consumer
670 * daemon. Those push and pull schemes are performed on two
671 * different bidirectionnal communication sockets.
673 ret
= consumer_push_metadata(socket
, metadata_key
,
674 metadata_str
, len
, offset
, metadata_version
);
675 pthread_mutex_lock(®istry
->lock
);
678 * There is an acceptable race here between the registry
679 * metadata key assignment and the creation on the
680 * consumer. The session daemon can concurrently push
681 * metadata for this registry while being created on the
682 * consumer since the metadata key of the registry is
683 * assigned *before* it is setup to avoid the consumer
684 * to ask for metadata that could possibly be not found
685 * in the session daemon.
687 * The metadata will get pushed either by the session
688 * being stopped or the consumer requesting metadata if
689 * that race is triggered.
691 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
694 ERR("Error pushing metadata to consumer");
700 * Metadata may have been concurrently pushed, since
701 * we're not holding the registry lock while pushing to
702 * consumer. This is handled by the fact that we send
703 * the metadata content, size, and the offset at which
704 * that metadata belongs. This may arrive out of order
705 * on the consumer side, and the consumer is able to
706 * deal with overlapping fragments. The consumer
707 * supports overlapping fragments, which must be
708 * contiguous starting from offset 0. We keep the
709 * largest metadata_len_sent value of the concurrent
712 registry
->metadata_len_sent
=
713 max_t(size_t, registry
->metadata_len_sent
,
714 new_metadata_len_sent
);
723 * On error, flag the registry that the metadata is
724 * closed. We were unable to push anything and this
725 * means that either the consumer is not responding or
726 * the metadata cache has been destroyed on the
729 registry
->metadata_closed
= 1;
737 * For a given application and session, push metadata to consumer.
738 * Either sock or consumer is required : if sock is NULL, the default
739 * socket to send the metadata is retrieved from consumer, if sock
740 * is not NULL we use it to send the metadata.
741 * RCU read-side lock must be held while calling this function,
742 * therefore ensuring existance of registry. It also ensures existance
743 * of socket throughout this function.
745 * Return 0 on success else a negative error.
746 * Returning a -EPIPE return value means we could not send the metadata,
747 * but it can be caused by recoverable errors (e.g. the application has
748 * terminated concurrently).
750 static int push_metadata(struct ust_registry_session
*registry
,
751 struct consumer_output
*consumer
)
755 struct consumer_socket
*socket
;
760 pthread_mutex_lock(®istry
->lock
);
761 if (registry
->metadata_closed
) {
766 /* Get consumer socket to use to push the metadata.*/
767 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
774 ret
= ust_app_push_metadata(registry
, socket
, 0);
779 pthread_mutex_unlock(®istry
->lock
);
783 pthread_mutex_unlock(®istry
->lock
);
788 * Send to the consumer a close metadata command for the given session. Once
789 * done, the metadata channel is deleted and the session metadata pointer is
790 * nullified. The session lock MUST be held unless the application is
791 * in the destroy path.
793 * Do not hold the registry lock while communicating with the consumerd, because
794 * doing so causes inter-process deadlocks between consumerd and sessiond with
795 * the metadata request notification.
797 * Return 0 on success else a negative value.
799 static int close_metadata(struct ust_registry_session
*registry
,
800 struct consumer_output
*consumer
)
803 struct consumer_socket
*socket
;
804 uint64_t metadata_key
;
805 bool registry_was_already_closed
;
812 pthread_mutex_lock(®istry
->lock
);
813 metadata_key
= registry
->metadata_key
;
814 registry_was_already_closed
= registry
->metadata_closed
;
815 if (metadata_key
!= 0) {
817 * Metadata closed. Even on error this means that the consumer
818 * is not responding or not found so either way a second close
819 * should NOT be emit for this registry.
821 registry
->metadata_closed
= 1;
823 pthread_mutex_unlock(®istry
->lock
);
825 if (metadata_key
== 0 || registry_was_already_closed
) {
830 /* Get consumer socket to use to push the metadata.*/
831 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
838 ret
= consumer_close_metadata(socket
, metadata_key
);
849 * We need to execute ht_destroy outside of RCU read-side critical
850 * section and outside of call_rcu thread, so we postpone its execution
851 * using ht_cleanup_push. It is simpler than to change the semantic of
852 * the many callers of delete_ust_app_session().
855 void delete_ust_app_session_rcu(struct rcu_head
*head
)
857 struct ust_app_session
*ua_sess
=
858 caa_container_of(head
, struct ust_app_session
, rcu_head
);
860 ht_cleanup_push(ua_sess
->channels
);
865 * Delete ust app session safely. RCU read lock must be held before calling
868 * The session list lock must be held by the caller.
871 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
875 struct lttng_ht_iter iter
;
876 struct ust_app_channel
*ua_chan
;
877 struct ust_registry_session
*registry
;
881 pthread_mutex_lock(&ua_sess
->lock
);
883 assert(!ua_sess
->deleted
);
884 ua_sess
->deleted
= true;
886 registry
= get_session_registry(ua_sess
);
887 /* Registry can be null on error path during initialization. */
889 /* Push metadata for application before freeing the application. */
890 (void) push_metadata(registry
, ua_sess
->consumer
);
893 * Don't ask to close metadata for global per UID buffers. Close
894 * metadata only on destroy trace session in this case. Also, the
895 * previous push metadata could have flag the metadata registry to
896 * close so don't send a close command if closed.
898 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
899 /* And ask to close it for this session registry. */
900 (void) close_metadata(registry
, ua_sess
->consumer
);
904 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
906 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
908 delete_ust_app_channel(sock
, ua_chan
, app
);
911 /* In case of per PID, the registry is kept in the session. */
912 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
913 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
916 * Registry can be null on error path during
919 buffer_reg_pid_remove(reg_pid
);
920 buffer_reg_pid_destroy(reg_pid
);
924 if (ua_sess
->handle
!= -1) {
925 pthread_mutex_lock(&app
->sock_lock
);
926 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
927 pthread_mutex_unlock(&app
->sock_lock
);
928 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
929 ERR("UST app sock %d release session handle failed with ret %d",
932 /* Remove session from application UST object descriptor. */
933 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
934 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
938 pthread_mutex_unlock(&ua_sess
->lock
);
940 consumer_output_put(ua_sess
->consumer
);
942 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
946 * Delete a traceable application structure from the global list. Never call
947 * this function outside of a call_rcu call.
949 * RCU read side lock should _NOT_ be held when calling this function.
952 void delete_ust_app(struct ust_app
*app
)
955 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
956 struct lttng_ht_iter iter
;
957 struct ust_app_event_notifier_rule
*event_notifier_rule
;
958 bool event_notifier_write_fd_is_open
;
961 * The session list lock must be held during this function to guarantee
962 * the existence of ua_sess.
965 /* Delete ust app sessions info */
970 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
972 /* Free every object in the session and the session. */
974 delete_ust_app_session(sock
, ua_sess
, app
);
978 /* Remove the event notifier rules associated with this app. */
980 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
981 &iter
.iter
, event_notifier_rule
, node
.node
) {
982 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
985 delete_ust_app_event_notifier_rule(
986 app
->sock
, event_notifier_rule
, app
);
991 ht_cleanup_push(app
->sessions
);
992 ht_cleanup_push(app
->ust_sessions_objd
);
993 ht_cleanup_push(app
->ust_objd
);
994 ht_cleanup_push(app
->token_to_event_notifier_rule_ht
);
997 * This could be NULL if the event notifier setup failed (e.g the app
998 * was killed or the tracer does not support this feature).
1000 if (app
->event_notifier_group
.object
) {
1001 enum lttng_error_code ret_code
;
1002 const int event_notifier_read_fd
= lttng_pipe_get_readfd(
1003 app
->event_notifier_group
.event_pipe
);
1005 ret_code
= notification_thread_command_remove_tracer_event_source(
1006 notification_thread_handle
,
1007 event_notifier_read_fd
);
1008 if (ret_code
!= LTTNG_OK
) {
1009 ERR("Failed to remove application tracer event source from notification thread");
1012 ustctl_release_object(sock
, app
->event_notifier_group
.object
);
1013 free(app
->event_notifier_group
.object
);
1016 event_notifier_write_fd_is_open
= lttng_pipe_is_write_open(
1017 app
->event_notifier_group
.event_pipe
);
1018 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1020 * Release the file descriptors reserved for the event notifier pipe.
1021 * The app could be destroyed before the write end of the pipe could be
1022 * passed to the application (and closed). In that case, both file
1023 * descriptors must be released.
1025 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1028 * Wait until we have deleted the application from the sock hash table
1029 * before closing this socket, otherwise an application could re-use the
1030 * socket ID and race with the teardown, using the same hash table entry.
1032 * It's OK to leave the close in call_rcu. We want it to stay unique for
1033 * all RCU readers that could run concurrently with unregister app,
1034 * therefore we _need_ to only close that socket after a grace period. So
1035 * it should stay in this RCU callback.
1037 * This close() is a very important step of the synchronization model so
1038 * every modification to this function must be carefully reviewed.
1044 lttng_fd_put(LTTNG_FD_APPS
, 1);
1046 DBG2("UST app pid %d deleted", app
->pid
);
1048 session_unlock_list();
1052 * URCU intermediate call to delete an UST app.
1055 void delete_ust_app_rcu(struct rcu_head
*head
)
1057 struct lttng_ht_node_ulong
*node
=
1058 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1059 struct ust_app
*app
=
1060 caa_container_of(node
, struct ust_app
, pid_n
);
1062 DBG3("Call RCU deleting app PID %d", app
->pid
);
1063 delete_ust_app(app
);
1067 * Delete the session from the application ht and delete the data structure by
1068 * freeing every object inside and releasing them.
1070 * The session list lock must be held by the caller.
1072 static void destroy_app_session(struct ust_app
*app
,
1073 struct ust_app_session
*ua_sess
)
1076 struct lttng_ht_iter iter
;
1081 iter
.iter
.node
= &ua_sess
->node
.node
;
1082 ret
= lttng_ht_del(app
->sessions
, &iter
);
1084 /* Already scheduled for teardown. */
1088 /* Once deleted, free the data structure. */
1089 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1096 * Alloc new UST app session.
1099 struct ust_app_session
*alloc_ust_app_session(void)
1101 struct ust_app_session
*ua_sess
;
1103 /* Init most of the default value by allocating and zeroing */
1104 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1105 if (ua_sess
== NULL
) {
1110 ua_sess
->handle
= -1;
1111 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1112 ua_sess
->metadata_attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1113 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1122 * Alloc new UST app channel.
1125 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1126 struct ust_app_session
*ua_sess
,
1127 struct lttng_ust_abi_channel_attr
*attr
)
1129 struct ust_app_channel
*ua_chan
;
1131 /* Init most of the default value by allocating and zeroing */
1132 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1133 if (ua_chan
== NULL
) {
1138 /* Setup channel name */
1139 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1140 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1142 ua_chan
->enabled
= 1;
1143 ua_chan
->handle
= -1;
1144 ua_chan
->session
= ua_sess
;
1145 ua_chan
->key
= get_next_channel_key();
1146 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1147 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1148 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1150 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1151 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1153 /* Copy attributes */
1155 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1156 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1157 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1158 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1159 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1160 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1161 ua_chan
->attr
.output
= attr
->output
;
1162 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1164 /* By default, the channel is a per cpu channel. */
1165 ua_chan
->attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1167 DBG3("UST app channel %s allocated", ua_chan
->name
);
1176 * Allocate and initialize a UST app stream.
1178 * Return newly allocated stream pointer or NULL on error.
1180 struct ust_app_stream
*ust_app_alloc_stream(void)
1182 struct ust_app_stream
*stream
= NULL
;
1184 stream
= zmalloc(sizeof(*stream
));
1185 if (stream
== NULL
) {
1186 PERROR("zmalloc ust app stream");
1190 /* Zero could be a valid value for a handle so flag it to -1. */
1191 stream
->handle
= -1;
1198 * Alloc new UST app event.
1201 struct ust_app_event
*alloc_ust_app_event(char *name
,
1202 struct lttng_ust_abi_event
*attr
)
1204 struct ust_app_event
*ua_event
;
1206 /* Init most of the default value by allocating and zeroing */
1207 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1208 if (ua_event
== NULL
) {
1209 PERROR("Failed to allocate ust_app_event structure");
1213 ua_event
->enabled
= 1;
1214 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1215 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1216 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1218 /* Copy attributes */
1220 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1223 DBG3("UST app event %s allocated", ua_event
->name
);
1232 * Allocate a new UST app event notifier rule.
1234 static struct ust_app_event_notifier_rule
*alloc_ust_app_event_notifier_rule(
1235 struct lttng_trigger
*trigger
)
1237 enum lttng_event_rule_generate_exclusions_status
1238 generate_exclusion_status
;
1239 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1240 struct lttng_condition
*condition
= NULL
;
1241 const struct lttng_event_rule
*event_rule
= NULL
;
1243 ua_event_notifier_rule
= zmalloc(sizeof(struct ust_app_event_notifier_rule
));
1244 if (ua_event_notifier_rule
== NULL
) {
1245 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1249 ua_event_notifier_rule
->enabled
= 1;
1250 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1251 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
,
1252 ua_event_notifier_rule
->token
);
1254 condition
= lttng_trigger_get_condition(trigger
);
1256 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_ON_EVENT
);
1258 assert(LTTNG_CONDITION_STATUS_OK
== lttng_condition_on_event_get_rule(condition
, &event_rule
));
1261 /* Acquire the event notifier's reference to the trigger. */
1262 lttng_trigger_get(trigger
);
1264 ua_event_notifier_rule
->trigger
= trigger
;
1265 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1266 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1267 event_rule
, &ua_event_notifier_rule
->exclusion
);
1268 switch (generate_exclusion_status
) {
1269 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1270 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1273 /* Error occured. */
1274 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1275 goto error_put_trigger
;
1278 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1279 ua_event_notifier_rule
->token
);
1281 return ua_event_notifier_rule
;
1284 lttng_trigger_put(trigger
);
1286 free(ua_event_notifier_rule
);
1291 * Alloc new UST app context.
1294 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1296 struct ust_app_ctx
*ua_ctx
;
1298 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1299 if (ua_ctx
== NULL
) {
1303 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1306 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1307 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1308 char *provider_name
= NULL
, *ctx_name
= NULL
;
1310 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1311 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1312 if (!provider_name
|| !ctx_name
) {
1313 free(provider_name
);
1318 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1319 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1323 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1331 * Create a liblttng-ust filter bytecode from given bytecode.
1333 * Return allocated filter or NULL on error.
1335 static struct lttng_ust_abi_filter_bytecode
*create_ust_filter_bytecode_from_bytecode(
1336 const struct lttng_bytecode
*orig_f
)
1338 struct lttng_ust_abi_filter_bytecode
*filter
= NULL
;
1340 /* Copy filter bytecode. */
1341 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1343 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1347 assert(sizeof(struct lttng_bytecode
) ==
1348 sizeof(struct lttng_ust_abi_filter_bytecode
));
1349 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1355 * Create a liblttng-ust capture bytecode from given bytecode.
1357 * Return allocated filter or NULL on error.
1359 static struct lttng_ust_abi_capture_bytecode
*
1360 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1362 struct lttng_ust_abi_capture_bytecode
*capture
= NULL
;
1364 /* Copy capture bytecode. */
1365 capture
= zmalloc(sizeof(*capture
) + orig_f
->len
);
1367 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1371 assert(sizeof(struct lttng_bytecode
) ==
1372 sizeof(struct lttng_ust_abi_capture_bytecode
));
1373 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1379 * Find an ust_app using the sock and return it. RCU read side lock must be
1380 * held before calling this helper function.
1382 struct ust_app
*ust_app_find_by_sock(int sock
)
1384 struct lttng_ht_node_ulong
*node
;
1385 struct lttng_ht_iter iter
;
1387 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1388 node
= lttng_ht_iter_get_node_ulong(&iter
);
1390 DBG2("UST app find by sock %d not found", sock
);
1394 return caa_container_of(node
, struct ust_app
, sock_n
);
1401 * Find an ust_app using the notify sock and return it. RCU read side lock must
1402 * be held before calling this helper function.
1404 static struct ust_app
*find_app_by_notify_sock(int sock
)
1406 struct lttng_ht_node_ulong
*node
;
1407 struct lttng_ht_iter iter
;
1409 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1411 node
= lttng_ht_iter_get_node_ulong(&iter
);
1413 DBG2("UST app find by notify sock %d not found", sock
);
1417 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1424 * Lookup for an ust app event based on event name, filter bytecode and the
1427 * Return an ust_app_event object or NULL on error.
1429 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1430 const char *name
, const struct lttng_bytecode
*filter
,
1432 const struct lttng_event_exclusion
*exclusion
)
1434 struct lttng_ht_iter iter
;
1435 struct lttng_ht_node_str
*node
;
1436 struct ust_app_event
*event
= NULL
;
1437 struct ust_app_ht_key key
;
1442 /* Setup key for event lookup. */
1444 key
.filter
= filter
;
1445 key
.loglevel_type
= loglevel_value
;
1446 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1447 key
.exclusion
= exclusion
;
1449 /* Lookup using the event name as hash and a custom match fct. */
1450 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1451 ht_match_ust_app_event
, &key
, &iter
.iter
);
1452 node
= lttng_ht_iter_get_node_str(&iter
);
1457 event
= caa_container_of(node
, struct ust_app_event
, node
);
1464 * Look-up an event notifier rule based on its token id.
1466 * Must be called with the RCU read lock held.
1467 * Return an ust_app_event_notifier_rule object or NULL on error.
1469 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(
1470 struct lttng_ht
*ht
, uint64_t token
)
1472 struct lttng_ht_iter iter
;
1473 struct lttng_ht_node_u64
*node
;
1474 struct ust_app_event_notifier_rule
*event_notifier_rule
= NULL
;
1478 lttng_ht_lookup(ht
, &token
, &iter
);
1479 node
= lttng_ht_iter_get_node_u64(&iter
);
1481 DBG2("UST app event notifier rule token not found: token = %" PRIu64
,
1486 event_notifier_rule
= caa_container_of(
1487 node
, struct ust_app_event_notifier_rule
, node
);
1489 return event_notifier_rule
;
1493 * Create the channel context on the tracer.
1495 * Called with UST app session lock held.
1498 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1499 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1503 health_code_update();
1505 pthread_mutex_lock(&app
->sock_lock
);
1506 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1507 ua_chan
->obj
, &ua_ctx
->obj
);
1508 pthread_mutex_unlock(&app
->sock_lock
);
1510 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1511 ERR("UST app create channel context failed for app (pid: %d) "
1512 "with ret %d", app
->pid
, ret
);
1515 * This is normal behavior, an application can die during the
1516 * creation process. Don't report an error so the execution can
1517 * continue normally.
1520 DBG3("UST app add context failed. Application is dead.");
1525 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1527 DBG2("UST app context handle %d created successfully for channel %s",
1528 ua_ctx
->handle
, ua_chan
->name
);
1531 health_code_update();
1536 * Set the filter on the tracer.
1538 static int set_ust_object_filter(struct ust_app
*app
,
1539 const struct lttng_bytecode
*bytecode
,
1540 struct lttng_ust_abi_object_data
*ust_object
)
1543 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= NULL
;
1545 health_code_update();
1547 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1548 if (!ust_bytecode
) {
1549 ret
= -LTTNG_ERR_NOMEM
;
1552 pthread_mutex_lock(&app
->sock_lock
);
1553 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1555 pthread_mutex_unlock(&app
->sock_lock
);
1557 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1558 ERR("UST app set object filter failed: object = %p of app pid = %d, ret = %d",
1559 ust_object
, app
->pid
, ret
);
1562 * This is normal behavior, an application can die during the
1563 * creation process. Don't report an error so the execution can
1564 * continue normally.
1567 DBG3("Failed to set UST app object filter. Application is dead.");
1572 DBG2("UST filter successfully set: object = %p", ust_object
);
1575 health_code_update();
1581 * Set a capture bytecode for the passed object.
1582 * The sequence number enforces the ordering at runtime and on reception of
1583 * the captured payloads.
1585 static int set_ust_capture(struct ust_app
*app
,
1586 const struct lttng_bytecode
*bytecode
,
1587 unsigned int capture_seqnum
,
1588 struct lttng_ust_abi_object_data
*ust_object
)
1591 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= NULL
;
1593 health_code_update();
1595 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1596 if (!ust_bytecode
) {
1597 ret
= -LTTNG_ERR_NOMEM
;
1602 * Set the sequence number to ensure the capture of fields is ordered.
1604 ust_bytecode
->seqnum
= capture_seqnum
;
1606 pthread_mutex_lock(&app
->sock_lock
);
1607 ret
= ustctl_set_capture(app
->sock
, ust_bytecode
,
1609 pthread_mutex_unlock(&app
->sock_lock
);
1611 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1612 ERR("UST app set object capture failed: object = %p of app pid = %d, ret = %d",
1613 ust_object
, app
->pid
, ret
);
1616 * This is normal behavior, an application can die during the
1617 * creation process. Don't report an error so the execution can
1618 * continue normally.
1621 DBG3("Failed to set UST app object capture. Application is dead.");
1627 DBG2("UST capture successfully set: object = %p", ust_object
);
1630 health_code_update();
1636 struct lttng_ust_abi_event_exclusion
*create_ust_exclusion_from_exclusion(
1637 const struct lttng_event_exclusion
*exclusion
)
1639 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= NULL
;
1640 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1641 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1643 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1644 if (!ust_exclusion
) {
1649 assert(sizeof(struct lttng_event_exclusion
) ==
1650 sizeof(struct lttng_ust_abi_event_exclusion
));
1651 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1653 return ust_exclusion
;
1657 * Set event exclusions on the tracer.
1659 static int set_ust_object_exclusions(struct ust_app
*app
,
1660 const struct lttng_event_exclusion
*exclusions
,
1661 struct lttng_ust_abi_object_data
*ust_object
)
1664 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= NULL
;
1666 assert(exclusions
&& exclusions
->count
> 0);
1668 health_code_update();
1670 ust_exclusions
= create_ust_exclusion_from_exclusion(
1672 if (!ust_exclusions
) {
1673 ret
= -LTTNG_ERR_NOMEM
;
1676 pthread_mutex_lock(&app
->sock_lock
);
1677 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1678 pthread_mutex_unlock(&app
->sock_lock
);
1680 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1681 ERR("Failed to set UST app exclusions for object %p of app (pid: %d) "
1682 "with ret %d", ust_object
, app
->pid
, ret
);
1685 * This is normal behavior, an application can die during the
1686 * creation process. Don't report an error so the execution can
1687 * continue normally.
1690 DBG3("Failed to set UST app object exclusions. Application is dead.");
1695 DBG2("UST exclusions set successfully for object %p", ust_object
);
1698 health_code_update();
1699 free(ust_exclusions
);
1704 * Disable the specified event on to UST tracer for the UST session.
1706 static int disable_ust_object(struct ust_app
*app
,
1707 struct lttng_ust_abi_object_data
*object
)
1711 health_code_update();
1713 pthread_mutex_lock(&app
->sock_lock
);
1714 ret
= ustctl_disable(app
->sock
, object
);
1715 pthread_mutex_unlock(&app
->sock_lock
);
1717 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1718 ERR("Failed to disable UST app object %p app (pid: %d) with ret %d",
1719 object
, app
->pid
, ret
);
1722 * This is normal behavior, an application can die during the
1723 * creation process. Don't report an error so the execution can
1724 * continue normally.
1727 DBG3("Failed to disable UST app object. Application is dead.");
1732 DBG2("UST app object %p disabled successfully for app (pid: %d)",
1736 health_code_update();
1741 * Disable the specified channel on to UST tracer for the UST session.
1743 static int disable_ust_channel(struct ust_app
*app
,
1744 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1748 health_code_update();
1750 pthread_mutex_lock(&app
->sock_lock
);
1751 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1752 pthread_mutex_unlock(&app
->sock_lock
);
1754 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1755 ERR("UST app channel %s disable failed for app (pid: %d) "
1756 "and session handle %d with ret %d",
1757 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1760 * This is normal behavior, an application can die during the
1761 * creation process. Don't report an error so the execution can
1762 * continue normally.
1765 DBG3("UST app disable channel failed. Application is dead.");
1770 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1771 ua_chan
->name
, app
->pid
);
1774 health_code_update();
1779 * Enable the specified channel on to UST tracer for the UST session.
1781 static int enable_ust_channel(struct ust_app
*app
,
1782 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1786 health_code_update();
1788 pthread_mutex_lock(&app
->sock_lock
);
1789 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1790 pthread_mutex_unlock(&app
->sock_lock
);
1792 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1793 ERR("UST app channel %s enable failed for app (pid: %d) "
1794 "and session handle %d with ret %d",
1795 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1798 * This is normal behavior, an application can die during the
1799 * creation process. Don't report an error so the execution can
1800 * continue normally.
1803 DBG3("UST app enable channel failed. Application is dead.");
1808 ua_chan
->enabled
= 1;
1810 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1811 ua_chan
->name
, app
->pid
);
1814 health_code_update();
1819 * Enable the specified event on to UST tracer for the UST session.
1821 static int enable_ust_object(
1822 struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1826 health_code_update();
1828 pthread_mutex_lock(&app
->sock_lock
);
1829 ret
= ustctl_enable(app
->sock
, ust_object
);
1830 pthread_mutex_unlock(&app
->sock_lock
);
1832 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1833 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
1834 ust_object
, app
->pid
, ret
);
1837 * This is normal behavior, an application can die during the
1838 * creation process. Don't report an error so the execution can
1839 * continue normally.
1842 DBG3("Failed to enable UST app object. Application is dead.");
1847 DBG2("UST app object %p enabled successfully for app (pid: %d)",
1848 ust_object
, app
->pid
);
1851 health_code_update();
1856 * Send channel and stream buffer to application.
1858 * Return 0 on success. On error, a negative value is returned.
1860 static int send_channel_pid_to_ust(struct ust_app
*app
,
1861 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1864 struct ust_app_stream
*stream
, *stmp
;
1870 health_code_update();
1872 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1875 /* Send channel to the application. */
1876 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1877 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1878 ret
= -ENOTCONN
; /* Caused by app exiting. */
1880 } else if (ret
< 0) {
1884 health_code_update();
1886 /* Send all streams to application. */
1887 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1888 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1889 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1890 ret
= -ENOTCONN
; /* Caused by app exiting. */
1892 } else if (ret
< 0) {
1895 /* We don't need the stream anymore once sent to the tracer. */
1896 cds_list_del(&stream
->list
);
1897 delete_ust_app_stream(-1, stream
, app
);
1899 /* Flag the channel that it is sent to the application. */
1900 ua_chan
->is_sent
= 1;
1903 health_code_update();
1908 * Create the specified event onto the UST tracer for a UST session.
1910 * Should be called with session mutex held.
1913 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1914 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1918 health_code_update();
1920 /* Create UST event on tracer */
1921 pthread_mutex_lock(&app
->sock_lock
);
1922 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1924 pthread_mutex_unlock(&app
->sock_lock
);
1926 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1928 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1929 ua_event
->attr
.name
, app
->pid
, ret
);
1932 * This is normal behavior, an application can die during the
1933 * creation process. Don't report an error so the execution can
1934 * continue normally.
1937 DBG3("UST app create event failed. Application is dead.");
1942 ua_event
->handle
= ua_event
->obj
->handle
;
1944 DBG2("UST app event %s created successfully for pid:%d object: %p",
1945 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
1947 health_code_update();
1949 /* Set filter if one is present. */
1950 if (ua_event
->filter
) {
1951 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
1957 /* Set exclusions for the event */
1958 if (ua_event
->exclusion
) {
1959 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
1965 /* If event not enabled, disable it on the tracer */
1966 if (ua_event
->enabled
) {
1968 * We now need to explicitly enable the event, since it
1969 * is now disabled at creation.
1971 ret
= enable_ust_object(app
, ua_event
->obj
);
1974 * If we hit an EPERM, something is wrong with our enable call. If
1975 * we get an EEXIST, there is a problem on the tracer side since we
1979 case -LTTNG_UST_ERR_PERM
:
1980 /* Code flow problem */
1982 case -LTTNG_UST_ERR_EXIST
:
1983 /* It's OK for our use case. */
1994 health_code_update();
1998 static int init_ust_event_notifier_from_event_rule(
1999 const struct lttng_event_rule
*rule
,
2000 struct lttng_ust_abi_event_notifier
*event_notifier
)
2002 enum lttng_event_rule_status status
;
2003 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2004 int loglevel
= -1, ret
= 0;
2005 const char *pattern
;
2007 /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported. */
2008 assert(lttng_event_rule_get_type(rule
) ==
2009 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
2011 memset(event_notifier
, 0, sizeof(*event_notifier
));
2013 if (lttng_event_rule_targets_agent_domain(rule
)) {
2015 * Special event for agents
2016 * The actual meat of the event is in the filter that will be
2017 * attached later on.
2018 * Set the default values for the agent event.
2020 pattern
= event_get_default_agent_ust_name(
2021 lttng_event_rule_get_domain_type(rule
));
2023 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2025 const struct lttng_log_level_rule
*log_level_rule
;
2027 status
= lttng_event_rule_tracepoint_get_pattern(rule
, &pattern
);
2028 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2029 /* At this point, this is a fatal error. */
2033 status
= lttng_event_rule_tracepoint_get_log_level_rule(
2034 rule
, &log_level_rule
);
2035 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2036 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2037 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2038 enum lttng_log_level_rule_status llr_status
;
2040 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2041 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2042 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2043 llr_status
= lttng_log_level_rule_exactly_get_level(
2044 log_level_rule
, &loglevel
);
2046 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2047 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2048 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2049 log_level_rule
, &loglevel
);
2055 assert(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2057 /* At this point this is a fatal error. */
2062 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2063 ret
= lttng_strncpy(event_notifier
->event
.name
, pattern
,
2064 LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
2066 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
2071 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2072 event_notifier
->event
.loglevel
= loglevel
;
2078 * Create the specified event notifier against the user space tracer of a
2079 * given application.
2081 static int create_ust_event_notifier(struct ust_app
*app
,
2082 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2085 enum lttng_condition_status condition_status
;
2086 const struct lttng_condition
*condition
= NULL
;
2087 struct lttng_ust_abi_event_notifier event_notifier
;
2088 const struct lttng_event_rule
*event_rule
= NULL
;
2089 unsigned int capture_bytecode_count
= 0, i
;
2090 enum lttng_condition_status cond_status
;
2092 health_code_update();
2093 assert(app
->event_notifier_group
.object
);
2095 condition
= lttng_trigger_get_const_condition(
2096 ua_event_notifier_rule
->trigger
);
2098 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_ON_EVENT
);
2100 condition_status
= lttng_condition_on_event_get_rule(
2101 condition
, &event_rule
);
2102 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2105 assert(lttng_event_rule_get_type(event_rule
) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
2107 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2108 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2110 /* Create UST event notifier against the tracer. */
2111 pthread_mutex_lock(&app
->sock_lock
);
2112 ret
= ustctl_create_event_notifier(app
->sock
, &event_notifier
,
2113 app
->event_notifier_group
.object
,
2114 &ua_event_notifier_rule
->obj
);
2115 pthread_mutex_unlock(&app
->sock_lock
);
2117 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2118 ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d",
2119 event_notifier
.event
.name
, app
->name
,
2123 * This is normal behavior, an application can die
2124 * during the creation process. Don't report an error so
2125 * the execution can continue normally.
2128 DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)",
2129 app
->name
, app
->ppid
);
2135 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2137 DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p",
2138 event_notifier
.event
.name
, app
->name
, app
->ppid
,
2139 ua_event_notifier_rule
->obj
);
2141 health_code_update();
2143 /* Set filter if one is present. */
2144 if (ua_event_notifier_rule
->filter
) {
2145 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2146 ua_event_notifier_rule
->obj
);
2152 /* Set exclusions for the event. */
2153 if (ua_event_notifier_rule
->exclusion
) {
2154 ret
= set_ust_object_exclusions(app
,
2155 ua_event_notifier_rule
->exclusion
,
2156 ua_event_notifier_rule
->obj
);
2162 /* Set the capture bytecodes. */
2163 cond_status
= lttng_condition_on_event_get_capture_descriptor_count(
2164 condition
, &capture_bytecode_count
);
2165 assert(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2167 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2168 const struct lttng_bytecode
*capture_bytecode
=
2169 lttng_condition_on_event_get_capture_bytecode_at_index(
2172 ret
= set_ust_capture(app
, capture_bytecode
, i
,
2173 ua_event_notifier_rule
->obj
);
2180 * We now need to explicitly enable the event, since it
2181 * is disabled at creation.
2183 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2186 * If we hit an EPERM, something is wrong with our enable call.
2187 * If we get an EEXIST, there is a problem on the tracer side
2188 * since we just created it.
2191 case -LTTNG_UST_ERR_PERM
:
2192 /* Code flow problem. */
2194 case -LTTNG_UST_ERR_EXIST
:
2195 /* It's OK for our use case. */
2205 ua_event_notifier_rule
->enabled
= true;
2208 health_code_update();
2213 * Copy data between an UST app event and a LTT event.
2215 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2216 struct ltt_ust_event
*uevent
)
2218 size_t exclusion_alloc_size
;
2220 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2221 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2223 ua_event
->enabled
= uevent
->enabled
;
2225 /* Copy event attributes */
2226 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2228 /* Copy filter bytecode */
2229 if (uevent
->filter
) {
2230 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2231 /* Filter might be NULL here in case of ENONEM. */
2234 /* Copy exclusion data */
2235 if (uevent
->exclusion
) {
2236 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2237 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2238 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
2239 if (ua_event
->exclusion
== NULL
) {
2242 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2243 exclusion_alloc_size
);
2249 * Copy data between an UST app channel and a LTT channel.
2251 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2252 struct ltt_ust_channel
*uchan
)
2254 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2256 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2257 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2259 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2260 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2262 /* Copy event attributes since the layout is different. */
2263 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2264 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2265 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2266 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2267 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2268 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2269 ua_chan
->attr
.output
= uchan
->attr
.output
;
2270 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2273 * Note that the attribute channel type is not set since the channel on the
2274 * tracing registry side does not have this information.
2277 ua_chan
->enabled
= uchan
->enabled
;
2278 ua_chan
->tracing_channel_id
= uchan
->id
;
2280 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2284 * Copy data between a UST app session and a regular LTT session.
2286 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2287 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2289 struct tm
*timeinfo
;
2292 char tmp_shm_path
[PATH_MAX
];
2294 timeinfo
= localtime(&app
->registration_time
);
2295 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2297 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2299 ua_sess
->tracing_id
= usess
->id
;
2300 ua_sess
->id
= get_next_session_id();
2301 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2302 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2303 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2304 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2305 ua_sess
->buffer_type
= usess
->buffer_type
;
2306 ua_sess
->bits_per_long
= app
->bits_per_long
;
2308 /* There is only one consumer object per session possible. */
2309 consumer_output_get(usess
->consumer
);
2310 ua_sess
->consumer
= usess
->consumer
;
2312 ua_sess
->output_traces
= usess
->output_traces
;
2313 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2314 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2315 &usess
->metadata_attr
);
2317 switch (ua_sess
->buffer_type
) {
2318 case LTTNG_BUFFER_PER_PID
:
2319 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2320 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2323 case LTTNG_BUFFER_PER_UID
:
2324 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2325 DEFAULT_UST_TRACE_UID_PATH
,
2326 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2327 app
->bits_per_long
);
2334 PERROR("asprintf UST shadow copy session");
2339 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2340 sizeof(ua_sess
->root_shm_path
));
2341 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2342 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2343 sizeof(ua_sess
->shm_path
));
2344 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2345 if (ua_sess
->shm_path
[0]) {
2346 switch (ua_sess
->buffer_type
) {
2347 case LTTNG_BUFFER_PER_PID
:
2348 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2349 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2350 app
->name
, app
->pid
, datetime
);
2352 case LTTNG_BUFFER_PER_UID
:
2353 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2354 "/" DEFAULT_UST_TRACE_UID_PATH
,
2355 app
->uid
, app
->bits_per_long
);
2362 PERROR("sprintf UST shadow copy session");
2366 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2367 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2368 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2373 consumer_output_put(ua_sess
->consumer
);
2377 * Lookup sesison wrapper.
2380 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2381 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2383 /* Get right UST app session from app */
2384 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2388 * Return ust app session from the app session hashtable using the UST session
2391 static struct ust_app_session
*lookup_session_by_app(
2392 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2394 struct lttng_ht_iter iter
;
2395 struct lttng_ht_node_u64
*node
;
2397 __lookup_session_by_app(usess
, app
, &iter
);
2398 node
= lttng_ht_iter_get_node_u64(&iter
);
2403 return caa_container_of(node
, struct ust_app_session
, node
);
2410 * Setup buffer registry per PID for the given session and application. If none
2411 * is found, a new one is created, added to the global registry and
2412 * initialized. If regp is valid, it's set with the newly created object.
2414 * Return 0 on success or else a negative value.
2416 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2417 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2420 struct buffer_reg_pid
*reg_pid
;
2427 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2430 * This is the create channel path meaning that if there is NO
2431 * registry available, we have to create one for this session.
2433 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2434 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2442 /* Initialize registry. */
2443 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2444 app
->bits_per_long
, app
->uint8_t_alignment
,
2445 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2446 app
->uint64_t_alignment
, app
->long_alignment
,
2447 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2448 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2449 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2450 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2451 ua_sess
->tracing_id
,
2455 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2456 * destroy the buffer registry, because it is always expected
2457 * that if the buffer registry can be found, its ust registry is
2460 buffer_reg_pid_destroy(reg_pid
);
2464 buffer_reg_pid_add(reg_pid
);
2466 DBG3("UST app buffer registry per PID created successfully");
2478 * Setup buffer registry per UID for the given session and application. If none
2479 * is found, a new one is created, added to the global registry and
2480 * initialized. If regp is valid, it's set with the newly created object.
2482 * Return 0 on success or else a negative value.
2484 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2485 struct ust_app_session
*ua_sess
,
2486 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2489 struct buffer_reg_uid
*reg_uid
;
2496 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2499 * This is the create channel path meaning that if there is NO
2500 * registry available, we have to create one for this session.
2502 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2503 LTTNG_DOMAIN_UST
, ®_uid
,
2504 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2512 /* Initialize registry. */
2513 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2514 app
->bits_per_long
, app
->uint8_t_alignment
,
2515 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2516 app
->uint64_t_alignment
, app
->long_alignment
,
2517 app
->byte_order
, app
->version
.major
,
2518 app
->version
.minor
, reg_uid
->root_shm_path
,
2519 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2520 ua_sess
->tracing_id
, app
->uid
);
2523 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2524 * destroy the buffer registry, because it is always expected
2525 * that if the buffer registry can be found, its ust registry is
2528 buffer_reg_uid_destroy(reg_uid
, NULL
);
2531 /* Add node to teardown list of the session. */
2532 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2534 buffer_reg_uid_add(reg_uid
);
2536 DBG3("UST app buffer registry per UID created successfully");
2547 * Create a session on the tracer side for the given app.
2549 * On success, ua_sess_ptr is populated with the session pointer or else left
2550 * untouched. If the session was created, is_created is set to 1. On error,
2551 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2554 * Returns 0 on success or else a negative code which is either -ENOMEM or
2555 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2557 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2558 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2561 int ret
, created
= 0;
2562 struct ust_app_session
*ua_sess
;
2566 assert(ua_sess_ptr
);
2568 health_code_update();
2570 ua_sess
= lookup_session_by_app(usess
, app
);
2571 if (ua_sess
== NULL
) {
2572 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2573 app
->pid
, usess
->id
);
2574 ua_sess
= alloc_ust_app_session();
2575 if (ua_sess
== NULL
) {
2576 /* Only malloc can failed so something is really wrong */
2580 shadow_copy_session(ua_sess
, usess
, app
);
2584 switch (usess
->buffer_type
) {
2585 case LTTNG_BUFFER_PER_PID
:
2586 /* Init local registry. */
2587 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2589 delete_ust_app_session(-1, ua_sess
, app
);
2593 case LTTNG_BUFFER_PER_UID
:
2594 /* Look for a global registry. If none exists, create one. */
2595 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2597 delete_ust_app_session(-1, ua_sess
, app
);
2607 health_code_update();
2609 if (ua_sess
->handle
== -1) {
2610 pthread_mutex_lock(&app
->sock_lock
);
2611 ret
= ustctl_create_session(app
->sock
);
2612 pthread_mutex_unlock(&app
->sock_lock
);
2614 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2615 ERR("Creating session for app pid %d with ret %d",
2618 DBG("UST app creating session failed. Application is dead");
2620 * This is normal behavior, an application can die during the
2621 * creation process. Don't report an error so the execution can
2622 * continue normally. This will get flagged ENOTCONN and the
2623 * caller will handle it.
2627 delete_ust_app_session(-1, ua_sess
, app
);
2628 if (ret
!= -ENOMEM
) {
2630 * Tracer is probably gone or got an internal error so let's
2631 * behave like it will soon unregister or not usable.
2638 ua_sess
->handle
= ret
;
2640 /* Add ust app session to app's HT */
2641 lttng_ht_node_init_u64(&ua_sess
->node
,
2642 ua_sess
->tracing_id
);
2643 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2644 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2645 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2646 &ua_sess
->ust_objd_node
);
2648 DBG2("UST app session created successfully with handle %d", ret
);
2651 *ua_sess_ptr
= ua_sess
;
2653 *is_created
= created
;
2656 /* Everything went well. */
2660 health_code_update();
2665 * Match function for a hash table lookup of ust_app_ctx.
2667 * It matches an ust app context based on the context type and, in the case
2668 * of perf counters, their name.
2670 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2672 struct ust_app_ctx
*ctx
;
2673 const struct lttng_ust_context_attr
*key
;
2678 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2682 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2687 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2688 if (strncmp(key
->u
.perf_counter
.name
,
2689 ctx
->ctx
.u
.perf_counter
.name
,
2690 sizeof(key
->u
.perf_counter
.name
))) {
2694 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2695 if (strcmp(key
->u
.app_ctx
.provider_name
,
2696 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2697 strcmp(key
->u
.app_ctx
.ctx_name
,
2698 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2714 * Lookup for an ust app context from an lttng_ust_context.
2716 * Must be called while holding RCU read side lock.
2717 * Return an ust_app_ctx object or NULL on error.
2720 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2721 struct lttng_ust_context_attr
*uctx
)
2723 struct lttng_ht_iter iter
;
2724 struct lttng_ht_node_ulong
*node
;
2725 struct ust_app_ctx
*app_ctx
= NULL
;
2730 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2731 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2732 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2733 node
= lttng_ht_iter_get_node_ulong(&iter
);
2738 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2745 * Create a context for the channel on the tracer.
2747 * Called with UST app session lock held and a RCU read side lock.
2750 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2751 struct lttng_ust_context_attr
*uctx
,
2752 struct ust_app
*app
)
2755 struct ust_app_ctx
*ua_ctx
;
2757 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2759 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2765 ua_ctx
= alloc_ust_app_ctx(uctx
);
2766 if (ua_ctx
== NULL
) {
2772 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2773 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2774 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2776 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2786 * Enable on the tracer side a ust app event for the session and channel.
2788 * Called with UST app session lock held.
2791 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2792 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2796 ret
= enable_ust_object(app
, ua_event
->obj
);
2801 ua_event
->enabled
= 1;
2808 * Disable on the tracer side a ust app event for the session and channel.
2810 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2811 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2815 ret
= disable_ust_object(app
, ua_event
->obj
);
2820 ua_event
->enabled
= 0;
2827 * Lookup ust app channel for session and disable it on the tracer side.
2830 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2831 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2835 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2840 ua_chan
->enabled
= 0;
2847 * Lookup ust app channel for session and enable it on the tracer side. This
2848 * MUST be called with a RCU read side lock acquired.
2850 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2851 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2854 struct lttng_ht_iter iter
;
2855 struct lttng_ht_node_str
*ua_chan_node
;
2856 struct ust_app_channel
*ua_chan
;
2858 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2859 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2860 if (ua_chan_node
== NULL
) {
2861 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2862 uchan
->name
, ua_sess
->tracing_id
);
2866 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2868 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2878 * Ask the consumer to create a channel and get it if successful.
2880 * Called with UST app session lock held.
2882 * Return 0 on success or else a negative value.
2884 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2885 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2886 int bitness
, struct ust_registry_session
*registry
,
2887 uint64_t trace_archive_id
)
2890 unsigned int nb_fd
= 0;
2891 struct consumer_socket
*socket
;
2899 health_code_update();
2901 /* Get the right consumer socket for the application. */
2902 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2908 health_code_update();
2910 /* Need one fd for the channel. */
2911 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2913 ERR("Exhausted number of available FD upon create channel");
2918 * Ask consumer to create channel. The consumer will return the number of
2919 * stream we have to expect.
2921 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2922 registry
, usess
->current_trace_chunk
);
2928 * Compute the number of fd needed before receiving them. It must be 2 per
2929 * stream (2 being the default value here).
2931 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2933 /* Reserve the amount of file descriptor we need. */
2934 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2936 ERR("Exhausted number of available FD upon create channel");
2937 goto error_fd_get_stream
;
2940 health_code_update();
2943 * Now get the channel from the consumer. This call wil populate the stream
2944 * list of that channel and set the ust objects.
2946 if (usess
->consumer
->enabled
) {
2947 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2957 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2958 error_fd_get_stream
:
2960 * Initiate a destroy channel on the consumer since we had an error
2961 * handling it on our side. The return value is of no importance since we
2962 * already have a ret value set by the previous error that we need to
2965 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2967 lttng_fd_put(LTTNG_FD_APPS
, 1);
2969 health_code_update();
2975 * Duplicate the ust data object of the ust app stream and save it in the
2976 * buffer registry stream.
2978 * Return 0 on success or else a negative value.
2980 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2981 struct ust_app_stream
*stream
)
2988 /* Reserve the amount of file descriptor we need. */
2989 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2991 ERR("Exhausted number of available FD upon duplicate stream");
2995 /* Duplicate object for stream once the original is in the registry. */
2996 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2997 reg_stream
->obj
.ust
);
2999 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3000 reg_stream
->obj
.ust
, stream
->obj
, ret
);
3001 lttng_fd_put(LTTNG_FD_APPS
, 2);
3004 stream
->handle
= stream
->obj
->handle
;
3011 * Duplicate the ust data object of the ust app. channel and save it in the
3012 * buffer registry channel.
3014 * Return 0 on success or else a negative value.
3016 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
3017 struct ust_app_channel
*ua_chan
)
3024 /* Need two fds for the channel. */
3025 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3027 ERR("Exhausted number of available FD upon duplicate channel");
3031 /* Duplicate object for stream once the original is in the registry. */
3032 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
3034 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3035 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
3038 ua_chan
->handle
= ua_chan
->obj
->handle
;
3043 lttng_fd_put(LTTNG_FD_APPS
, 1);
3049 * For a given channel buffer registry, setup all streams of the given ust
3050 * application channel.
3052 * Return 0 on success or else a negative value.
3054 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
3055 struct ust_app_channel
*ua_chan
,
3056 struct ust_app
*app
)
3059 struct ust_app_stream
*stream
, *stmp
;
3064 DBG2("UST app setup buffer registry stream");
3066 /* Send all streams to application. */
3067 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3068 struct buffer_reg_stream
*reg_stream
;
3070 ret
= buffer_reg_stream_create(®_stream
);
3076 * Keep original pointer and nullify it in the stream so the delete
3077 * stream call does not release the object.
3079 reg_stream
->obj
.ust
= stream
->obj
;
3081 buffer_reg_stream_add(reg_stream
, reg_chan
);
3083 /* We don't need the streams anymore. */
3084 cds_list_del(&stream
->list
);
3085 delete_ust_app_stream(-1, stream
, app
);
3093 * Create a buffer registry channel for the given session registry and
3094 * application channel object. If regp pointer is valid, it's set with the
3095 * created object. Important, the created object is NOT added to the session
3096 * registry hash table.
3098 * Return 0 on success else a negative value.
3100 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3101 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
3104 struct buffer_reg_channel
*reg_chan
= NULL
;
3109 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3111 /* Create buffer registry channel. */
3112 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
3117 reg_chan
->consumer_key
= ua_chan
->key
;
3118 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3119 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3121 /* Create and add a channel registry to session. */
3122 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
3123 ua_chan
->tracing_channel_id
);
3127 buffer_reg_channel_add(reg_sess
, reg_chan
);
3136 /* Safe because the registry channel object was not added to any HT. */
3137 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3143 * Setup buffer registry channel for the given session registry and application
3144 * channel object. If regp pointer is valid, it's set with the created object.
3146 * Return 0 on success else a negative value.
3148 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3149 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
3150 struct ust_app
*app
)
3157 assert(ua_chan
->obj
);
3159 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3161 /* Setup all streams for the registry. */
3162 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
3167 reg_chan
->obj
.ust
= ua_chan
->obj
;
3168 ua_chan
->obj
= NULL
;
3173 buffer_reg_channel_remove(reg_sess
, reg_chan
);
3174 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3179 * Send buffer registry channel to the application.
3181 * Return 0 on success else a negative value.
3183 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
3184 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
3185 struct ust_app_channel
*ua_chan
)
3188 struct buffer_reg_stream
*reg_stream
;
3195 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3197 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
3202 /* Send channel to the application. */
3203 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3204 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3205 ret
= -ENOTCONN
; /* Caused by app exiting. */
3207 } else if (ret
< 0) {
3211 health_code_update();
3213 /* Send all streams to application. */
3214 pthread_mutex_lock(®_chan
->stream_list_lock
);
3215 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
3216 struct ust_app_stream stream
;
3218 ret
= duplicate_stream_object(reg_stream
, &stream
);
3220 goto error_stream_unlock
;
3223 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3225 (void) release_ust_app_stream(-1, &stream
, app
);
3226 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3227 ret
= -ENOTCONN
; /* Caused by app exiting. */
3229 goto error_stream_unlock
;
3233 * The return value is not important here. This function will output an
3236 (void) release_ust_app_stream(-1, &stream
, app
);
3238 ua_chan
->is_sent
= 1;
3240 error_stream_unlock
:
3241 pthread_mutex_unlock(®_chan
->stream_list_lock
);
3247 * Create and send to the application the created buffers with per UID buffers.
3249 * This MUST be called with a RCU read side lock acquired.
3250 * The session list lock and the session's lock must be acquired.
3252 * Return 0 on success else a negative value.
3254 static int create_channel_per_uid(struct ust_app
*app
,
3255 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3256 struct ust_app_channel
*ua_chan
)
3259 struct buffer_reg_uid
*reg_uid
;
3260 struct buffer_reg_channel
*reg_chan
;
3261 struct ltt_session
*session
= NULL
;
3262 enum lttng_error_code notification_ret
;
3263 struct ust_registry_channel
*chan_reg
;
3270 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3272 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3274 * The session creation handles the creation of this global registry
3275 * object. If none can be find, there is a code flow problem or a
3280 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3286 /* Create the buffer registry channel object. */
3287 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
3289 ERR("Error creating the UST channel \"%s\" registry instance",
3294 session
= session_find_by_id(ua_sess
->tracing_id
);
3296 assert(pthread_mutex_trylock(&session
->lock
));
3297 assert(session_trylock_list());
3300 * Create the buffers on the consumer side. This call populates the
3301 * ust app channel object with all streams and data object.
3303 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3304 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
3305 session
->most_recent_chunk_id
.value
);
3307 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3311 * Let's remove the previously created buffer registry channel so
3312 * it's not visible anymore in the session registry.
3314 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3315 ua_chan
->tracing_channel_id
, false);
3316 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
3317 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3322 * Setup the streams and add it to the session registry.
3324 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3325 ua_chan
, reg_chan
, app
);
3327 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3331 /* Notify the notification subsystem of the channel's creation. */
3332 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
3333 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3334 ua_chan
->tracing_channel_id
);
3336 chan_reg
->consumer_key
= ua_chan
->key
;
3338 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
3340 notification_ret
= notification_thread_command_add_channel(
3341 notification_thread_handle
, session
->name
,
3342 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3343 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3345 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3346 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3347 if (notification_ret
!= LTTNG_OK
) {
3348 ret
= - (int) notification_ret
;
3349 ERR("Failed to add channel to notification thread");
3354 /* Send buffers to the application. */
3355 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
3357 if (ret
!= -ENOTCONN
) {
3358 ERR("Error sending channel to application");
3365 session_put(session
);
3371 * Create and send to the application the created buffers with per PID buffers.
3373 * Called with UST app session lock held.
3374 * The session list lock and the session's lock must be acquired.
3376 * Return 0 on success else a negative value.
3378 static int create_channel_per_pid(struct ust_app
*app
,
3379 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3380 struct ust_app_channel
*ua_chan
)
3383 struct ust_registry_session
*registry
;
3384 enum lttng_error_code cmd_ret
;
3385 struct ltt_session
*session
= NULL
;
3386 uint64_t chan_reg_key
;
3387 struct ust_registry_channel
*chan_reg
;
3394 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3398 registry
= get_session_registry(ua_sess
);
3399 /* The UST app session lock is held, registry shall not be null. */
3402 /* Create and add a new channel registry to session. */
3403 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3405 ERR("Error creating the UST channel \"%s\" registry instance",
3410 session
= session_find_by_id(ua_sess
->tracing_id
);
3413 assert(pthread_mutex_trylock(&session
->lock
));
3414 assert(session_trylock_list());
3416 /* Create and get channel on the consumer side. */
3417 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3418 app
->bits_per_long
, registry
,
3419 session
->most_recent_chunk_id
.value
);
3421 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3423 goto error_remove_from_registry
;
3426 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3428 if (ret
!= -ENOTCONN
) {
3429 ERR("Error sending channel to application");
3431 goto error_remove_from_registry
;
3434 chan_reg_key
= ua_chan
->key
;
3435 pthread_mutex_lock(®istry
->lock
);
3436 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3438 chan_reg
->consumer_key
= ua_chan
->key
;
3439 pthread_mutex_unlock(®istry
->lock
);
3441 cmd_ret
= notification_thread_command_add_channel(
3442 notification_thread_handle
, session
->name
,
3443 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3444 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3446 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3447 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3448 if (cmd_ret
!= LTTNG_OK
) {
3449 ret
= - (int) cmd_ret
;
3450 ERR("Failed to add channel to notification thread");
3451 goto error_remove_from_registry
;
3454 error_remove_from_registry
:
3456 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3461 session_put(session
);
3467 * From an already allocated ust app channel, create the channel buffers if
3468 * needed and send them to the application. This MUST be called with a RCU read
3469 * side lock acquired.
3471 * Called with UST app session lock held.
3473 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3474 * the application exited concurrently.
3476 static int ust_app_channel_send(struct ust_app
*app
,
3477 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3478 struct ust_app_channel
*ua_chan
)
3484 assert(usess
->active
);
3488 /* Handle buffer type before sending the channel to the application. */
3489 switch (usess
->buffer_type
) {
3490 case LTTNG_BUFFER_PER_UID
:
3492 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3498 case LTTNG_BUFFER_PER_PID
:
3500 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3512 /* Initialize ust objd object using the received handle and add it. */
3513 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3514 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3516 /* If channel is not enabled, disable it on the tracer */
3517 if (!ua_chan
->enabled
) {
3518 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3529 * Create UST app channel and return it through ua_chanp if not NULL.
3531 * Called with UST app session lock and RCU read-side lock held.
3533 * Return 0 on success or else a negative value.
3535 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3536 struct ltt_ust_channel
*uchan
,
3537 enum lttng_ust_abi_chan_type type
, struct ltt_ust_session
*usess
,
3538 struct ust_app_channel
**ua_chanp
)
3541 struct lttng_ht_iter iter
;
3542 struct lttng_ht_node_str
*ua_chan_node
;
3543 struct ust_app_channel
*ua_chan
;
3545 /* Lookup channel in the ust app session */
3546 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3547 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3548 if (ua_chan_node
!= NULL
) {
3549 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3553 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3554 if (ua_chan
== NULL
) {
3555 /* Only malloc can fail here */
3559 shadow_copy_channel(ua_chan
, uchan
);
3561 /* Set channel type. */
3562 ua_chan
->attr
.type
= type
;
3564 /* Only add the channel if successful on the tracer side. */
3565 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3568 *ua_chanp
= ua_chan
;
3571 /* Everything went well. */
3579 * Create UST app event and create it on the tracer side.
3581 * Must be called with the RCU read side lock held.
3582 * Called with ust app session mutex held.
3585 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3586 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3587 struct ust_app
*app
)
3590 struct ust_app_event
*ua_event
;
3592 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3593 if (ua_event
== NULL
) {
3594 /* Only failure mode of alloc_ust_app_event(). */
3598 shadow_copy_event(ua_event
, uevent
);
3600 /* Create it on the tracer side */
3601 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3604 * Not found previously means that it does not exist on the
3605 * tracer. If the application reports that the event existed,
3606 * it means there is a bug in the sessiond or lttng-ust
3607 * (or corruption, etc.)
3609 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3610 ERR("Tracer for application reported that an event being created already existed: "
3611 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3613 app
->pid
, app
->ppid
, app
->uid
,
3619 add_unique_ust_app_event(ua_chan
, ua_event
);
3621 DBG2("UST app create event completed: app = '%s' (ppid: %d)",
3622 app
->name
, app
->ppid
);
3628 /* Valid. Calling here is already in a read side lock */
3629 delete_ust_app_event(-1, ua_event
, app
);
3634 * Create UST app event notifier rule and create it on the tracer side.
3636 * Must be called with the RCU read side lock held.
3637 * Called with ust app session mutex held.
3640 int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
,
3641 struct ust_app
*app
)
3644 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3646 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3647 if (ua_event_notifier_rule
== NULL
) {
3652 /* Create it on the tracer side. */
3653 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3656 * Not found previously means that it does not exist on the
3657 * tracer. If the application reports that the event existed,
3658 * it means there is a bug in the sessiond or lttng-ust
3659 * (or corruption, etc.)
3661 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3662 ERR("Tracer for application reported that an event notifier being created already exists: "
3663 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3664 lttng_trigger_get_tracer_token(trigger
),
3665 app
->pid
, app
->ppid
, app
->uid
,
3671 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3672 &ua_event_notifier_rule
->node
);
3674 DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64
,
3675 app
->name
, app
->ppid
, lttng_trigger_get_tracer_token(trigger
));
3681 /* The RCU read side lock is already being held by the caller. */
3682 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3687 * Create UST metadata and open it on the tracer side.
3689 * Called with UST app session lock held and RCU read side lock.
3691 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3692 struct ust_app
*app
, struct consumer_output
*consumer
)
3695 struct ust_app_channel
*metadata
;
3696 struct consumer_socket
*socket
;
3697 struct ust_registry_session
*registry
;
3698 struct ltt_session
*session
= NULL
;
3704 registry
= get_session_registry(ua_sess
);
3705 /* The UST app session is held registry shall not be null. */
3708 pthread_mutex_lock(®istry
->lock
);
3710 /* Metadata already exists for this registry or it was closed previously */
3711 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3716 /* Allocate UST metadata */
3717 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3719 /* malloc() failed */
3724 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3726 /* Need one fd for the channel. */
3727 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3729 ERR("Exhausted number of available FD upon create metadata");
3733 /* Get the right consumer socket for the application. */
3734 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3737 goto error_consumer
;
3741 * Keep metadata key so we can identify it on the consumer side. Assign it
3742 * to the registry *before* we ask the consumer so we avoid the race of the
3743 * consumer requesting the metadata and the ask_channel call on our side
3744 * did not returned yet.
3746 registry
->metadata_key
= metadata
->key
;
3748 session
= session_find_by_id(ua_sess
->tracing_id
);
3751 assert(pthread_mutex_trylock(&session
->lock
));
3752 assert(session_trylock_list());
3755 * Ask the metadata channel creation to the consumer. The metadata object
3756 * will be created by the consumer and kept their. However, the stream is
3757 * never added or monitored until we do a first push metadata to the
3760 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3761 registry
, session
->current_trace_chunk
);
3763 /* Nullify the metadata key so we don't try to close it later on. */
3764 registry
->metadata_key
= 0;
3765 goto error_consumer
;
3769 * The setup command will make the metadata stream be sent to the relayd,
3770 * if applicable, and the thread managing the metadatas. This is important
3771 * because after this point, if an error occurs, the only way the stream
3772 * can be deleted is to be monitored in the consumer.
3774 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3776 /* Nullify the metadata key so we don't try to close it later on. */
3777 registry
->metadata_key
= 0;
3778 goto error_consumer
;
3781 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3782 metadata
->key
, app
->pid
);
3785 lttng_fd_put(LTTNG_FD_APPS
, 1);
3786 delete_ust_app_channel(-1, metadata
, app
);
3788 pthread_mutex_unlock(®istry
->lock
);
3790 session_put(session
);
3796 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3797 * acquired before calling this function.
3799 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3801 struct ust_app
*app
= NULL
;
3802 struct lttng_ht_node_ulong
*node
;
3803 struct lttng_ht_iter iter
;
3805 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3806 node
= lttng_ht_iter_get_node_ulong(&iter
);
3808 DBG2("UST app no found with pid %d", pid
);
3812 DBG2("Found UST app by pid %d", pid
);
3814 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3821 * Allocate and init an UST app object using the registration information and
3822 * the command socket. This is called when the command socket connects to the
3825 * The object is returned on success or else NULL.
3827 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3830 struct ust_app
*lta
= NULL
;
3831 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3836 DBG3("UST app creating application for socket %d", sock
);
3838 if ((msg
->bits_per_long
== 64 &&
3839 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3840 || (msg
->bits_per_long
== 32 &&
3841 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3842 ERR("Registration failed: application \"%s\" (pid: %d) has "
3843 "%d-bit long, but no consumerd for this size is available.\n",
3844 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3849 * Reserve the two file descriptors of the event source pipe. The write
3850 * end will be closed once it is passed to the application, at which
3851 * point a single 'put' will be performed.
3853 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3855 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s' (ppid: %d)",
3856 msg
->name
, (int) msg
->ppid
);
3860 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3861 if (!event_notifier_event_source_pipe
) {
3862 PERROR("Failed to open application event source pipe: '%s' (ppid = %d)",
3863 msg
->name
, msg
->ppid
);
3867 lta
= zmalloc(sizeof(struct ust_app
));
3870 goto error_free_pipe
;
3873 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
3875 lta
->ppid
= msg
->ppid
;
3876 lta
->uid
= msg
->uid
;
3877 lta
->gid
= msg
->gid
;
3879 lta
->bits_per_long
= msg
->bits_per_long
;
3880 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3881 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3882 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3883 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3884 lta
->long_alignment
= msg
->long_alignment
;
3885 lta
->byte_order
= msg
->byte_order
;
3887 lta
->v_major
= msg
->major
;
3888 lta
->v_minor
= msg
->minor
;
3889 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3890 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3891 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3892 lta
->notify_sock
= -1;
3893 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3895 /* Copy name and make sure it's NULL terminated. */
3896 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3897 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3900 * Before this can be called, when receiving the registration information,
3901 * the application compatibility is checked. So, at this point, the
3902 * application can work with this session daemon.
3904 lta
->compatible
= 1;
3906 lta
->pid
= msg
->pid
;
3907 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3909 pthread_mutex_init(<a
->sock_lock
, NULL
);
3910 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3912 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3916 lttng_pipe_destroy(event_notifier_event_source_pipe
);
3917 lttng_fd_put(LTTNG_FD_APPS
, 2);
3923 * For a given application object, add it to every hash table.
3925 void ust_app_add(struct ust_app
*app
)
3928 assert(app
->notify_sock
>= 0);
3930 app
->registration_time
= time(NULL
);
3935 * On a re-registration, we want to kick out the previous registration of
3938 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3941 * The socket _should_ be unique until _we_ call close. So, a add_unique
3942 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3943 * already in the table.
3945 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3947 /* Add application to the notify socket hash table. */
3948 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3949 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3951 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3952 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3953 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3960 * Set the application version into the object.
3962 * Return 0 on success else a negative value either an errno code or a
3963 * LTTng-UST error code.
3965 int ust_app_version(struct ust_app
*app
)
3971 pthread_mutex_lock(&app
->sock_lock
);
3972 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3973 pthread_mutex_unlock(&app
->sock_lock
);
3975 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3976 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3978 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3986 * Setup the base event notifier group.
3988 * Return 0 on success else a negative value either an errno code or a
3989 * LTTng-UST error code.
3991 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
3994 int event_pipe_write_fd
;
3995 struct lttng_ust_abi_object_data
*event_notifier_group
= NULL
;
3996 enum lttng_error_code lttng_ret
;
4000 /* Get the write side of the pipe. */
4001 event_pipe_write_fd
= lttng_pipe_get_writefd(
4002 app
->event_notifier_group
.event_pipe
);
4004 pthread_mutex_lock(&app
->sock_lock
);
4005 ret
= ustctl_create_event_notifier_group(app
->sock
,
4006 event_pipe_write_fd
, &event_notifier_group
);
4007 pthread_mutex_unlock(&app
->sock_lock
);
4009 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4010 ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d",
4011 ret
, app
->sock
, event_pipe_write_fd
);
4013 DBG("Failed to create application event notifier group (application is dead): app socket fd = %d",
4020 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4022 ERR("Failed to close write end of the application's event source pipe: app = '%s' (ppid = %d)",
4023 app
->name
, app
->ppid
);
4028 * Release the file descriptor that was reserved for the write-end of
4031 lttng_fd_put(LTTNG_FD_APPS
, 1);
4033 lttng_ret
= notification_thread_command_add_tracer_event_source(
4034 notification_thread_handle
,
4035 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
),
4037 if (lttng_ret
!= LTTNG_OK
) {
4038 ERR("Failed to add tracer event source to notification thread");
4043 /* Assign handle only when the complete setup is valid. */
4044 app
->event_notifier_group
.object
= event_notifier_group
;
4048 ustctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4049 free(app
->event_notifier_group
.object
);
4054 * Unregister app by removing it from the global traceable app list and freeing
4057 * The socket is already closed at this point so no close to sock.
4059 void ust_app_unregister(int sock
)
4061 struct ust_app
*lta
;
4062 struct lttng_ht_node_ulong
*node
;
4063 struct lttng_ht_iter ust_app_sock_iter
;
4064 struct lttng_ht_iter iter
;
4065 struct ust_app_session
*ua_sess
;
4070 /* Get the node reference for a call_rcu */
4071 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
4072 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4075 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
4076 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4079 * For per-PID buffers, perform "push metadata" and flush all
4080 * application streams before removing app from hash tables,
4081 * ensuring proper behavior of data_pending check.
4082 * Remove sessions so they are not visible during deletion.
4084 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
4086 struct ust_registry_session
*registry
;
4088 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4090 /* The session was already removed so scheduled for teardown. */
4094 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4095 (void) ust_app_flush_app_session(lta
, ua_sess
);
4099 * Add session to list for teardown. This is safe since at this point we
4100 * are the only one using this list.
4102 pthread_mutex_lock(&ua_sess
->lock
);
4104 if (ua_sess
->deleted
) {
4105 pthread_mutex_unlock(&ua_sess
->lock
);
4110 * Normally, this is done in the delete session process which is
4111 * executed in the call rcu below. However, upon registration we can't
4112 * afford to wait for the grace period before pushing data or else the
4113 * data pending feature can race between the unregistration and stop
4114 * command where the data pending command is sent *before* the grace
4117 * The close metadata below nullifies the metadata pointer in the
4118 * session so the delete session will NOT push/close a second time.
4120 registry
= get_session_registry(ua_sess
);
4122 /* Push metadata for application before freeing the application. */
4123 (void) push_metadata(registry
, ua_sess
->consumer
);
4126 * Don't ask to close metadata for global per UID buffers. Close
4127 * metadata only on destroy trace session in this case. Also, the
4128 * previous push metadata could have flag the metadata registry to
4129 * close so don't send a close command if closed.
4131 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4132 /* And ask to close it for this session registry. */
4133 (void) close_metadata(registry
, ua_sess
->consumer
);
4136 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4138 pthread_mutex_unlock(&ua_sess
->lock
);
4141 /* Remove application from PID hash table */
4142 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4146 * Remove application from notify hash table. The thread handling the
4147 * notify socket could have deleted the node so ignore on error because
4148 * either way it's valid. The close of that socket is handled by the
4149 * apps_notify_thread.
4151 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4152 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4155 * Ignore return value since the node might have been removed before by an
4156 * add replace during app registration because the PID can be reassigned by
4159 iter
.iter
.node
= <a
->pid_n
.node
;
4160 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4162 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4167 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4174 * Fill events array with all events name of all registered apps.
4176 int ust_app_list_events(struct lttng_event
**events
)
4179 size_t nbmem
, count
= 0;
4180 struct lttng_ht_iter iter
;
4181 struct ust_app
*app
;
4182 struct lttng_event
*tmp_event
;
4184 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4185 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
4186 if (tmp_event
== NULL
) {
4187 PERROR("zmalloc ust app events");
4194 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4195 struct lttng_ust_abi_tracepoint_iter uiter
;
4197 health_code_update();
4199 if (!app
->compatible
) {
4201 * TODO: In time, we should notice the caller of this error by
4202 * telling him that this is a version error.
4206 pthread_mutex_lock(&app
->sock_lock
);
4207 handle
= ustctl_tracepoint_list(app
->sock
);
4209 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4210 ERR("UST app list events getting handle failed for app pid %d",
4213 pthread_mutex_unlock(&app
->sock_lock
);
4217 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
4218 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4219 /* Handle ustctl error. */
4223 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4224 ERR("UST app tp list get failed for app %d with ret %d",
4227 DBG3("UST app tp list get failed. Application is dead");
4229 * This is normal behavior, an application can die during the
4230 * creation process. Don't report an error so the execution can
4231 * continue normally. Continue normal execution.
4236 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4237 if (release_ret
< 0 &&
4238 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4239 release_ret
!= -EPIPE
) {
4240 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4242 pthread_mutex_unlock(&app
->sock_lock
);
4246 health_code_update();
4247 if (count
>= nbmem
) {
4248 /* In case the realloc fails, we free the memory */
4249 struct lttng_event
*new_tmp_event
;
4252 new_nbmem
= nbmem
<< 1;
4253 DBG2("Reallocating event list from %zu to %zu entries",
4255 new_tmp_event
= realloc(tmp_event
,
4256 new_nbmem
* sizeof(struct lttng_event
));
4257 if (new_tmp_event
== NULL
) {
4260 PERROR("realloc ust app events");
4263 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4264 if (release_ret
< 0 &&
4265 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4266 release_ret
!= -EPIPE
) {
4267 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4269 pthread_mutex_unlock(&app
->sock_lock
);
4272 /* Zero the new memory */
4273 memset(new_tmp_event
+ nbmem
, 0,
4274 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4276 tmp_event
= new_tmp_event
;
4278 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4279 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4280 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4281 tmp_event
[count
].pid
= app
->pid
;
4282 tmp_event
[count
].enabled
= -1;
4285 ret
= ustctl_release_handle(app
->sock
, handle
);
4286 pthread_mutex_unlock(&app
->sock_lock
);
4287 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4288 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4293 *events
= tmp_event
;
4295 DBG2("UST app list events done (%zu events)", count
);
4300 health_code_update();
4305 * Fill events array with all events name of all registered apps.
4307 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4310 size_t nbmem
, count
= 0;
4311 struct lttng_ht_iter iter
;
4312 struct ust_app
*app
;
4313 struct lttng_event_field
*tmp_event
;
4315 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4316 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
4317 if (tmp_event
== NULL
) {
4318 PERROR("zmalloc ust app event fields");
4325 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4326 struct lttng_ust_abi_field_iter uiter
;
4328 health_code_update();
4330 if (!app
->compatible
) {
4332 * TODO: In time, we should notice the caller of this error by
4333 * telling him that this is a version error.
4337 pthread_mutex_lock(&app
->sock_lock
);
4338 handle
= ustctl_tracepoint_field_list(app
->sock
);
4340 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4341 ERR("UST app list field getting handle failed for app pid %d",
4344 pthread_mutex_unlock(&app
->sock_lock
);
4348 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
4349 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4350 /* Handle ustctl error. */
4354 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4355 ERR("UST app tp list field failed for app %d with ret %d",
4358 DBG3("UST app tp list field failed. Application is dead");
4360 * This is normal behavior, an application can die during the
4361 * creation process. Don't report an error so the execution can
4362 * continue normally. Reset list and count for next app.
4367 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4368 pthread_mutex_unlock(&app
->sock_lock
);
4369 if (release_ret
< 0 &&
4370 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4371 release_ret
!= -EPIPE
) {
4372 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4377 health_code_update();
4378 if (count
>= nbmem
) {
4379 /* In case the realloc fails, we free the memory */
4380 struct lttng_event_field
*new_tmp_event
;
4383 new_nbmem
= nbmem
<< 1;
4384 DBG2("Reallocating event field list from %zu to %zu entries",
4386 new_tmp_event
= realloc(tmp_event
,
4387 new_nbmem
* sizeof(struct lttng_event_field
));
4388 if (new_tmp_event
== NULL
) {
4391 PERROR("realloc ust app event fields");
4394 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4395 pthread_mutex_unlock(&app
->sock_lock
);
4397 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4398 release_ret
!= -EPIPE
) {
4399 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4403 /* Zero the new memory */
4404 memset(new_tmp_event
+ nbmem
, 0,
4405 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4407 tmp_event
= new_tmp_event
;
4410 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4411 /* Mapping between these enums matches 1 to 1. */
4412 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4413 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4415 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4416 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4417 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4418 tmp_event
[count
].event
.pid
= app
->pid
;
4419 tmp_event
[count
].event
.enabled
= -1;
4422 ret
= ustctl_release_handle(app
->sock
, handle
);
4423 pthread_mutex_unlock(&app
->sock_lock
);
4425 ret
!= -LTTNG_UST_ERR_EXITING
&&
4427 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4432 *fields
= tmp_event
;
4434 DBG2("UST app list event fields done (%zu events)", count
);
4439 health_code_update();
4444 * Free and clean all traceable apps of the global list.
4446 * Should _NOT_ be called with RCU read-side lock held.
4448 void ust_app_clean_list(void)
4451 struct ust_app
*app
;
4452 struct lttng_ht_iter iter
;
4454 DBG2("UST app cleaning registered apps hash table");
4458 /* Cleanup notify socket hash table */
4459 if (ust_app_ht_by_notify_sock
) {
4460 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4461 notify_sock_n
.node
) {
4463 * Assert that all notifiers are gone as all triggers
4464 * are unregistered prior to this clean-up.
4466 assert(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4468 ust_app_notify_sock_unregister(app
->notify_sock
);
4473 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4474 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4476 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4480 /* Cleanup socket hash table */
4481 if (ust_app_ht_by_sock
) {
4482 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4484 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4491 /* Destroy is done only when the ht is empty */
4493 ht_cleanup_push(ust_app_ht
);
4495 if (ust_app_ht_by_sock
) {
4496 ht_cleanup_push(ust_app_ht_by_sock
);
4498 if (ust_app_ht_by_notify_sock
) {
4499 ht_cleanup_push(ust_app_ht_by_notify_sock
);
4504 * Init UST app hash table.
4506 int ust_app_ht_alloc(void)
4508 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4512 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4513 if (!ust_app_ht_by_sock
) {
4516 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4517 if (!ust_app_ht_by_notify_sock
) {
4524 * For a specific UST session, disable the channel for all registered apps.
4526 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4527 struct ltt_ust_channel
*uchan
)
4530 struct lttng_ht_iter iter
;
4531 struct lttng_ht_node_str
*ua_chan_node
;
4532 struct ust_app
*app
;
4533 struct ust_app_session
*ua_sess
;
4534 struct ust_app_channel
*ua_chan
;
4536 assert(usess
->active
);
4537 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4538 uchan
->name
, usess
->id
);
4542 /* For every registered applications */
4543 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4544 struct lttng_ht_iter uiter
;
4545 if (!app
->compatible
) {
4547 * TODO: In time, we should notice the caller of this error by
4548 * telling him that this is a version error.
4552 ua_sess
= lookup_session_by_app(usess
, app
);
4553 if (ua_sess
== NULL
) {
4558 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4559 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4560 /* If the session if found for the app, the channel must be there */
4561 assert(ua_chan_node
);
4563 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4564 /* The channel must not be already disabled */
4565 assert(ua_chan
->enabled
== 1);
4567 /* Disable channel onto application */
4568 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4570 /* XXX: We might want to report this error at some point... */
4580 * For a specific UST session, enable the channel for all registered apps.
4582 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4583 struct ltt_ust_channel
*uchan
)
4586 struct lttng_ht_iter iter
;
4587 struct ust_app
*app
;
4588 struct ust_app_session
*ua_sess
;
4590 assert(usess
->active
);
4591 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4592 uchan
->name
, usess
->id
);
4596 /* For every registered applications */
4597 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4598 if (!app
->compatible
) {
4600 * TODO: In time, we should notice the caller of this error by
4601 * telling him that this is a version error.
4605 ua_sess
= lookup_session_by_app(usess
, app
);
4606 if (ua_sess
== NULL
) {
4610 /* Enable channel onto application */
4611 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4613 /* XXX: We might want to report this error at some point... */
4623 * Disable an event in a channel and for a specific session.
4625 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4626 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4629 struct lttng_ht_iter iter
, uiter
;
4630 struct lttng_ht_node_str
*ua_chan_node
;
4631 struct ust_app
*app
;
4632 struct ust_app_session
*ua_sess
;
4633 struct ust_app_channel
*ua_chan
;
4634 struct ust_app_event
*ua_event
;
4636 assert(usess
->active
);
4637 DBG("UST app disabling event %s for all apps in channel "
4638 "%s for session id %" PRIu64
,
4639 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4643 /* For all registered applications */
4644 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4645 if (!app
->compatible
) {
4647 * TODO: In time, we should notice the caller of this error by
4648 * telling him that this is a version error.
4652 ua_sess
= lookup_session_by_app(usess
, app
);
4653 if (ua_sess
== NULL
) {
4658 /* Lookup channel in the ust app session */
4659 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4660 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4661 if (ua_chan_node
== NULL
) {
4662 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4663 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4666 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4668 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4669 uevent
->filter
, uevent
->attr
.loglevel
,
4671 if (ua_event
== NULL
) {
4672 DBG2("Event %s not found in channel %s for app pid %d."
4673 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4677 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4679 /* XXX: Report error someday... */
4688 /* The ua_sess lock must be held by the caller. */
4690 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4691 struct ust_app_session
*ua_sess
,
4692 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4693 struct ust_app_channel
**_ua_chan
)
4696 struct ust_app_channel
*ua_chan
= NULL
;
4699 ASSERT_LOCKED(ua_sess
->lock
);
4701 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4702 sizeof(uchan
->name
))) {
4703 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4707 struct ltt_ust_context
*uctx
= NULL
;
4710 * Create channel onto application and synchronize its
4713 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4714 LTTNG_UST_ABI_CHAN_PER_CPU
, usess
,
4720 ret
= ust_app_channel_send(app
, usess
,
4727 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4728 ret
= create_ust_app_channel_context(ua_chan
,
4741 * The application's socket is not valid. Either a bad socket
4742 * or a timeout on it. We can't inform the caller that for a
4743 * specific app, the session failed so lets continue here.
4745 ret
= 0; /* Not an error. */
4753 if (ret
== 0 && _ua_chan
) {
4755 * Only return the application's channel on success. Note
4756 * that the channel can still be part of the application's
4757 * channel hashtable on error.
4759 *_ua_chan
= ua_chan
;
4765 * Enable event for a specific session and channel on the tracer.
4767 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4768 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4771 struct lttng_ht_iter iter
, uiter
;
4772 struct lttng_ht_node_str
*ua_chan_node
;
4773 struct ust_app
*app
;
4774 struct ust_app_session
*ua_sess
;
4775 struct ust_app_channel
*ua_chan
;
4776 struct ust_app_event
*ua_event
;
4778 assert(usess
->active
);
4779 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4780 uevent
->attr
.name
, usess
->id
);
4783 * NOTE: At this point, this function is called only if the session and
4784 * channel passed are already created for all apps. and enabled on the
4790 /* For all registered applications */
4791 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4792 if (!app
->compatible
) {
4794 * TODO: In time, we should notice the caller of this error by
4795 * telling him that this is a version error.
4799 ua_sess
= lookup_session_by_app(usess
, app
);
4801 /* The application has problem or is probably dead. */
4805 pthread_mutex_lock(&ua_sess
->lock
);
4807 if (ua_sess
->deleted
) {
4808 pthread_mutex_unlock(&ua_sess
->lock
);
4812 /* Lookup channel in the ust app session */
4813 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4814 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4816 * It is possible that the channel cannot be found is
4817 * the channel/event creation occurs concurrently with
4818 * an application exit.
4820 if (!ua_chan_node
) {
4821 pthread_mutex_unlock(&ua_sess
->lock
);
4825 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4827 /* Get event node */
4828 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4829 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4830 if (ua_event
== NULL
) {
4831 DBG3("UST app enable event %s not found for app PID %d."
4832 "Skipping app", uevent
->attr
.name
, app
->pid
);
4836 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4838 pthread_mutex_unlock(&ua_sess
->lock
);
4842 pthread_mutex_unlock(&ua_sess
->lock
);
4851 * For a specific existing UST session and UST channel, creates the event for
4852 * all registered apps.
4854 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4855 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4858 struct lttng_ht_iter iter
, uiter
;
4859 struct lttng_ht_node_str
*ua_chan_node
;
4860 struct ust_app
*app
;
4861 struct ust_app_session
*ua_sess
;
4862 struct ust_app_channel
*ua_chan
;
4864 assert(usess
->active
);
4865 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4866 uevent
->attr
.name
, usess
->id
);
4870 /* For all registered applications */
4871 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4872 if (!app
->compatible
) {
4874 * TODO: In time, we should notice the caller of this error by
4875 * telling him that this is a version error.
4879 ua_sess
= lookup_session_by_app(usess
, app
);
4881 /* The application has problem or is probably dead. */
4885 pthread_mutex_lock(&ua_sess
->lock
);
4887 if (ua_sess
->deleted
) {
4888 pthread_mutex_unlock(&ua_sess
->lock
);
4892 /* Lookup channel in the ust app session */
4893 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4894 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4895 /* If the channel is not found, there is a code flow error */
4896 assert(ua_chan_node
);
4898 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4900 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4901 pthread_mutex_unlock(&ua_sess
->lock
);
4903 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4904 /* Possible value at this point: -ENOMEM. If so, we stop! */
4907 DBG2("UST app event %s already exist on app PID %d",
4908 uevent
->attr
.name
, app
->pid
);
4918 * Start tracing for a specific UST session and app.
4920 * Called with UST app session lock held.
4924 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4927 struct ust_app_session
*ua_sess
;
4929 DBG("Starting tracing for ust app pid %d", app
->pid
);
4933 if (!app
->compatible
) {
4937 ua_sess
= lookup_session_by_app(usess
, app
);
4938 if (ua_sess
== NULL
) {
4939 /* The session is in teardown process. Ignore and continue. */
4943 pthread_mutex_lock(&ua_sess
->lock
);
4945 if (ua_sess
->deleted
) {
4946 pthread_mutex_unlock(&ua_sess
->lock
);
4950 if (ua_sess
->enabled
) {
4951 pthread_mutex_unlock(&ua_sess
->lock
);
4955 /* Upon restart, we skip the setup, already done */
4956 if (ua_sess
->started
) {
4960 health_code_update();
4963 /* This starts the UST tracing */
4964 pthread_mutex_lock(&app
->sock_lock
);
4965 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4966 pthread_mutex_unlock(&app
->sock_lock
);
4968 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4969 ERR("Error starting tracing for app pid: %d (ret: %d)",
4972 DBG("UST app start session failed. Application is dead.");
4974 * This is normal behavior, an application can die during the
4975 * creation process. Don't report an error so the execution can
4976 * continue normally.
4978 pthread_mutex_unlock(&ua_sess
->lock
);
4984 /* Indicate that the session has been started once */
4985 ua_sess
->started
= 1;
4986 ua_sess
->enabled
= 1;
4988 pthread_mutex_unlock(&ua_sess
->lock
);
4990 health_code_update();
4992 /* Quiescent wait after starting trace */
4993 pthread_mutex_lock(&app
->sock_lock
);
4994 ret
= ustctl_wait_quiescent(app
->sock
);
4995 pthread_mutex_unlock(&app
->sock_lock
);
4996 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4997 ERR("UST app wait quiescent failed for app pid %d ret %d",
5003 health_code_update();
5007 pthread_mutex_unlock(&ua_sess
->lock
);
5009 health_code_update();
5014 * Stop tracing for a specific UST session and app.
5017 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5020 struct ust_app_session
*ua_sess
;
5021 struct ust_registry_session
*registry
;
5023 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5027 if (!app
->compatible
) {
5028 goto end_no_session
;
5031 ua_sess
= lookup_session_by_app(usess
, app
);
5032 if (ua_sess
== NULL
) {
5033 goto end_no_session
;
5036 pthread_mutex_lock(&ua_sess
->lock
);
5038 if (ua_sess
->deleted
) {
5039 pthread_mutex_unlock(&ua_sess
->lock
);
5040 goto end_no_session
;
5044 * If started = 0, it means that stop trace has been called for a session
5045 * that was never started. It's possible since we can have a fail start
5046 * from either the application manager thread or the command thread. Simply
5047 * indicate that this is a stop error.
5049 if (!ua_sess
->started
) {
5050 goto error_rcu_unlock
;
5053 health_code_update();
5055 /* This inhibits UST tracing */
5056 pthread_mutex_lock(&app
->sock_lock
);
5057 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
5058 pthread_mutex_unlock(&app
->sock_lock
);
5060 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5061 ERR("Error stopping tracing for app pid: %d (ret: %d)",
5064 DBG("UST app stop session failed. Application is dead.");
5066 * This is normal behavior, an application can die during the
5067 * creation process. Don't report an error so the execution can
5068 * continue normally.
5072 goto error_rcu_unlock
;
5075 health_code_update();
5076 ua_sess
->enabled
= 0;
5078 /* Quiescent wait after stopping trace */
5079 pthread_mutex_lock(&app
->sock_lock
);
5080 ret
= ustctl_wait_quiescent(app
->sock
);
5081 pthread_mutex_unlock(&app
->sock_lock
);
5082 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5083 ERR("UST app wait quiescent failed for app pid %d ret %d",
5087 health_code_update();
5089 registry
= get_session_registry(ua_sess
);
5091 /* The UST app session is held registry shall not be null. */
5094 /* Push metadata for application before freeing the application. */
5095 (void) push_metadata(registry
, ua_sess
->consumer
);
5098 pthread_mutex_unlock(&ua_sess
->lock
);
5101 health_code_update();
5105 pthread_mutex_unlock(&ua_sess
->lock
);
5107 health_code_update();
5112 int ust_app_flush_app_session(struct ust_app
*app
,
5113 struct ust_app_session
*ua_sess
)
5115 int ret
, retval
= 0;
5116 struct lttng_ht_iter iter
;
5117 struct ust_app_channel
*ua_chan
;
5118 struct consumer_socket
*socket
;
5120 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5124 if (!app
->compatible
) {
5125 goto end_not_compatible
;
5128 pthread_mutex_lock(&ua_sess
->lock
);
5130 if (ua_sess
->deleted
) {
5134 health_code_update();
5136 /* Flushing buffers */
5137 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5140 /* Flush buffers and push metadata. */
5141 switch (ua_sess
->buffer_type
) {
5142 case LTTNG_BUFFER_PER_PID
:
5143 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5145 health_code_update();
5146 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5148 ERR("Error flushing consumer channel");
5154 case LTTNG_BUFFER_PER_UID
:
5160 health_code_update();
5163 pthread_mutex_unlock(&ua_sess
->lock
);
5167 health_code_update();
5172 * Flush buffers for all applications for a specific UST session.
5173 * Called with UST session lock held.
5176 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5181 DBG("Flushing session buffers for all ust apps");
5185 /* Flush buffers and push metadata. */
5186 switch (usess
->buffer_type
) {
5187 case LTTNG_BUFFER_PER_UID
:
5189 struct buffer_reg_uid
*reg
;
5190 struct lttng_ht_iter iter
;
5192 /* Flush all per UID buffers associated to that session. */
5193 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5194 struct ust_registry_session
*ust_session_reg
;
5195 struct buffer_reg_channel
*reg_chan
;
5196 struct consumer_socket
*socket
;
5198 /* Get consumer socket to use to push the metadata.*/
5199 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5202 /* Ignore request if no consumer is found for the session. */
5206 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5207 reg_chan
, node
.node
) {
5209 * The following call will print error values so the return
5210 * code is of little importance because whatever happens, we
5211 * have to try them all.
5213 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
5216 ust_session_reg
= reg
->registry
->reg
.ust
;
5217 /* Push metadata. */
5218 (void) push_metadata(ust_session_reg
, usess
->consumer
);
5222 case LTTNG_BUFFER_PER_PID
:
5224 struct ust_app_session
*ua_sess
;
5225 struct lttng_ht_iter iter
;
5226 struct ust_app
*app
;
5228 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5229 ua_sess
= lookup_session_by_app(usess
, app
);
5230 if (ua_sess
== NULL
) {
5233 (void) ust_app_flush_app_session(app
, ua_sess
);
5244 health_code_update();
5249 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5250 struct ust_app_session
*ua_sess
)
5253 struct lttng_ht_iter iter
;
5254 struct ust_app_channel
*ua_chan
;
5255 struct consumer_socket
*socket
;
5257 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5261 if (!app
->compatible
) {
5262 goto end_not_compatible
;
5265 pthread_mutex_lock(&ua_sess
->lock
);
5267 if (ua_sess
->deleted
) {
5271 health_code_update();
5273 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5276 ERR("Failed to find consumer (%" PRIu32
") socket",
5277 app
->bits_per_long
);
5282 /* Clear quiescent state. */
5283 switch (ua_sess
->buffer_type
) {
5284 case LTTNG_BUFFER_PER_PID
:
5285 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5286 ua_chan
, node
.node
) {
5287 health_code_update();
5288 ret
= consumer_clear_quiescent_channel(socket
,
5291 ERR("Error clearing quiescent state for consumer channel");
5297 case LTTNG_BUFFER_PER_UID
:
5304 health_code_update();
5307 pthread_mutex_unlock(&ua_sess
->lock
);
5311 health_code_update();
5316 * Clear quiescent state in each stream for all applications for a
5317 * specific UST session.
5318 * Called with UST session lock held.
5321 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5326 DBG("Clearing stream quiescent state for all ust apps");
5330 switch (usess
->buffer_type
) {
5331 case LTTNG_BUFFER_PER_UID
:
5333 struct lttng_ht_iter iter
;
5334 struct buffer_reg_uid
*reg
;
5337 * Clear quiescent for all per UID buffers associated to
5340 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5341 struct consumer_socket
*socket
;
5342 struct buffer_reg_channel
*reg_chan
;
5344 /* Get associated consumer socket.*/
5345 socket
= consumer_find_socket_by_bitness(
5346 reg
->bits_per_long
, usess
->consumer
);
5349 * Ignore request if no consumer is found for
5355 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5356 &iter
.iter
, reg_chan
, node
.node
) {
5358 * The following call will print error values so
5359 * the return code is of little importance
5360 * because whatever happens, we have to try them
5363 (void) consumer_clear_quiescent_channel(socket
,
5364 reg_chan
->consumer_key
);
5369 case LTTNG_BUFFER_PER_PID
:
5371 struct ust_app_session
*ua_sess
;
5372 struct lttng_ht_iter iter
;
5373 struct ust_app
*app
;
5375 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5377 ua_sess
= lookup_session_by_app(usess
, app
);
5378 if (ua_sess
== NULL
) {
5381 (void) ust_app_clear_quiescent_app_session(app
,
5393 health_code_update();
5398 * Destroy a specific UST session in apps.
5400 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5403 struct ust_app_session
*ua_sess
;
5404 struct lttng_ht_iter iter
;
5405 struct lttng_ht_node_u64
*node
;
5407 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5411 if (!app
->compatible
) {
5415 __lookup_session_by_app(usess
, app
, &iter
);
5416 node
= lttng_ht_iter_get_node_u64(&iter
);
5418 /* Session is being or is deleted. */
5421 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
5423 health_code_update();
5424 destroy_app_session(app
, ua_sess
);
5426 health_code_update();
5428 /* Quiescent wait after stopping trace */
5429 pthread_mutex_lock(&app
->sock_lock
);
5430 ret
= ustctl_wait_quiescent(app
->sock
);
5431 pthread_mutex_unlock(&app
->sock_lock
);
5432 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5433 ERR("UST app wait quiescent failed for app pid %d ret %d",
5438 health_code_update();
5443 * Start tracing for the UST session.
5445 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5447 struct lttng_ht_iter iter
;
5448 struct ust_app
*app
;
5450 DBG("Starting all UST traces");
5453 * Even though the start trace might fail, flag this session active so
5454 * other application coming in are started by default.
5461 * In a start-stop-start use-case, we need to clear the quiescent state
5462 * of each channel set by the prior stop command, thus ensuring that a
5463 * following stop or destroy is sure to grab a timestamp_end near those
5464 * operations, even if the packet is empty.
5466 (void) ust_app_clear_quiescent_session(usess
);
5468 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5469 ust_app_global_update(usess
, app
);
5478 * Start tracing for the UST session.
5479 * Called with UST session lock held.
5481 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5484 struct lttng_ht_iter iter
;
5485 struct ust_app
*app
;
5487 DBG("Stopping all UST traces");
5490 * Even though the stop trace might fail, flag this session inactive so
5491 * other application coming in are not started by default.
5497 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5498 ret
= ust_app_stop_trace(usess
, app
);
5500 /* Continue to next apps even on error */
5505 (void) ust_app_flush_session(usess
);
5513 * Destroy app UST session.
5515 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5518 struct lttng_ht_iter iter
;
5519 struct ust_app
*app
;
5521 DBG("Destroy all UST traces");
5525 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5526 ret
= destroy_trace(usess
, app
);
5528 /* Continue to next apps even on error */
5538 /* The ua_sess lock must be held by the caller. */
5540 int find_or_create_ust_app_channel(
5541 struct ltt_ust_session
*usess
,
5542 struct ust_app_session
*ua_sess
,
5543 struct ust_app
*app
,
5544 struct ltt_ust_channel
*uchan
,
5545 struct ust_app_channel
**ua_chan
)
5548 struct lttng_ht_iter iter
;
5549 struct lttng_ht_node_str
*ua_chan_node
;
5551 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5552 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5554 *ua_chan
= caa_container_of(ua_chan_node
,
5555 struct ust_app_channel
, node
);
5559 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5568 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5569 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5570 struct ust_app
*app
)
5573 struct ust_app_event
*ua_event
= NULL
;
5575 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5576 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5578 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5583 if (ua_event
->enabled
!= uevent
->enabled
) {
5584 ret
= uevent
->enabled
?
5585 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5586 disable_ust_app_event(ua_sess
, ua_event
, app
);
5594 /* Called with RCU read-side lock held. */
5596 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5599 enum lttng_error_code ret_code
;
5600 enum lttng_trigger_status t_status
;
5601 struct lttng_ht_iter app_trigger_iter
;
5602 struct lttng_triggers
*triggers
= NULL
;
5603 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5604 unsigned int count
, i
;
5607 * Currrently, registering or unregistering a trigger with an
5608 * event rule condition causes a full synchronization of the event
5611 * The first step attempts to add an event notifier for all registered
5612 * triggers that apply to the user space tracers. Then, the
5613 * application's event notifiers rules are all checked against the list
5614 * of registered triggers. Any event notifier that doesn't have a
5615 * matching trigger can be assumed to have been disabled.
5617 * All of this is inefficient, but is put in place to get the feature
5618 * rolling as it is simpler at this moment. It will be optimized Soon™
5619 * to allow the state of enabled
5620 * event notifiers to be synchronized in a piece-wise way.
5623 /* Get all triggers using uid 0 (root) */
5624 ret_code
= notification_thread_command_list_triggers(
5625 notification_thread_handle
, 0, &triggers
);
5626 if (ret_code
!= LTTNG_OK
) {
5633 t_status
= lttng_triggers_get_count(triggers
, &count
);
5634 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5639 for (i
= 0; i
< count
; i
++) {
5640 struct lttng_condition
*condition
;
5641 struct lttng_event_rule
*event_rule
;
5642 struct lttng_trigger
*trigger
;
5643 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5644 enum lttng_condition_status condition_status
;
5647 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5650 token
= lttng_trigger_get_tracer_token(trigger
);
5651 condition
= lttng_trigger_get_condition(trigger
);
5653 if (lttng_condition_get_type(condition
) != LTTNG_CONDITION_TYPE_ON_EVENT
) {
5654 /* Does not apply */
5658 condition_status
= lttng_condition_on_event_borrow_rule_mutable(condition
, &event_rule
);
5659 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5661 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5662 /* Skip kernel related triggers. */
5667 * Find or create the associated token event rule. The caller
5668 * holds the RCU read lock, so this is safe to call without
5669 * explicitly acquiring it here.
5671 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
5672 app
->token_to_event_notifier_rule_ht
, token
);
5673 if (!looked_up_event_notifier_rule
) {
5674 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
5682 /* Remove all unknown event sources from the app. */
5683 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
5684 &app_trigger_iter
.iter
, event_notifier_rule
,
5686 const uint64_t app_token
= event_notifier_rule
->token
;
5690 * Check if the app event trigger still exists on the
5691 * notification side.
5693 for (i
= 0; i
< count
; i
++) {
5694 uint64_t notification_thread_token
;
5695 const struct lttng_trigger
*trigger
=
5696 lttng_triggers_get_at_index(
5701 notification_thread_token
=
5702 lttng_trigger_get_tracer_token(trigger
);
5704 if (notification_thread_token
== app_token
) {
5716 * This trigger was unregistered, disable it on the tracer's
5719 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
5723 /* Callee logs errors. */
5724 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
5726 delete_ust_app_event_notifier_rule(
5727 app
->sock
, event_notifier_rule
, app
);
5733 lttng_triggers_destroy(triggers
);
5738 * The caller must ensure that the application is compatible and is tracked
5739 * by the process attribute trackers.
5742 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5743 struct ust_app
*app
)
5746 struct cds_lfht_iter uchan_iter
;
5747 struct ltt_ust_channel
*uchan
;
5748 struct ust_app_session
*ua_sess
= NULL
;
5751 * The application's configuration should only be synchronized for
5754 assert(usess
->active
);
5756 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5758 /* Tracer is probably gone or ENOMEM. */
5763 pthread_mutex_lock(&ua_sess
->lock
);
5764 if (ua_sess
->deleted
) {
5765 pthread_mutex_unlock(&ua_sess
->lock
);
5771 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5773 struct ust_app_channel
*ua_chan
;
5774 struct cds_lfht_iter uevent_iter
;
5775 struct ltt_ust_event
*uevent
;
5778 * Search for a matching ust_app_channel. If none is found,
5779 * create it. Creating the channel will cause the ua_chan
5780 * structure to be allocated, the channel buffers to be
5781 * allocated (if necessary) and sent to the application, and
5782 * all enabled contexts will be added to the channel.
5784 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5785 app
, uchan
, &ua_chan
);
5787 /* Tracer is probably gone or ENOMEM. */
5792 /* ua_chan will be NULL for the metadata channel */
5796 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5798 ret
= ust_app_channel_synchronize_event(ua_chan
,
5799 uevent
, ua_sess
, app
);
5805 if (ua_chan
->enabled
!= uchan
->enabled
) {
5806 ret
= uchan
->enabled
?
5807 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5808 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5816 * Create the metadata for the application. This returns gracefully if a
5817 * metadata was already set for the session.
5819 * The metadata channel must be created after the data channels as the
5820 * consumer daemon assumes this ordering. When interacting with a relay
5821 * daemon, the consumer will use this assumption to send the
5822 * "STREAMS_SENT" message to the relay daemon.
5824 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5832 pthread_mutex_unlock(&ua_sess
->lock
);
5833 /* Everything went well at this point. */
5838 pthread_mutex_unlock(&ua_sess
->lock
);
5841 destroy_app_session(app
, ua_sess
);
5847 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5849 struct ust_app_session
*ua_sess
;
5851 ua_sess
= lookup_session_by_app(usess
, app
);
5852 if (ua_sess
== NULL
) {
5855 destroy_app_session(app
, ua_sess
);
5859 * Add channels/events from UST global domain to registered apps at sock.
5861 * Called with session lock held.
5862 * Called with RCU read-side lock held.
5864 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5867 assert(usess
->active
);
5869 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5870 app
->sock
, usess
->id
);
5872 if (!app
->compatible
) {
5875 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5877 trace_ust_id_tracker_lookup(
5878 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5880 trace_ust_id_tracker_lookup(
5881 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5884 * Synchronize the application's internal tracing configuration
5885 * and start tracing.
5887 ust_app_synchronize(usess
, app
);
5888 ust_app_start_trace(usess
, app
);
5890 ust_app_global_destroy(usess
, app
);
5895 * Add all event notifiers to an application.
5897 * Called with session lock held.
5898 * Called with RCU read-side lock held.
5900 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
5902 DBG2("UST application global event notifier rules update: app = '%s' (ppid: %d)",
5903 app
->name
, app
->ppid
);
5905 if (!app
->compatible
) {
5909 if (app
->event_notifier_group
.object
== NULL
) {
5910 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s' (ppid: %d)",
5911 app
->name
, app
->ppid
);
5915 ust_app_synchronize_event_notifier_rules(app
);
5919 * Called with session lock held.
5921 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5923 struct lttng_ht_iter iter
;
5924 struct ust_app
*app
;
5927 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5928 ust_app_global_update(usess
, app
);
5933 void ust_app_global_update_all_event_notifier_rules(void)
5935 struct lttng_ht_iter iter
;
5936 struct ust_app
*app
;
5939 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5940 ust_app_global_update_event_notifier_rules(app
);
5947 * Add context to a specific channel for global UST domain.
5949 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5950 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5953 struct lttng_ht_node_str
*ua_chan_node
;
5954 struct lttng_ht_iter iter
, uiter
;
5955 struct ust_app_channel
*ua_chan
= NULL
;
5956 struct ust_app_session
*ua_sess
;
5957 struct ust_app
*app
;
5959 assert(usess
->active
);
5962 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5963 if (!app
->compatible
) {
5965 * TODO: In time, we should notice the caller of this error by
5966 * telling him that this is a version error.
5970 ua_sess
= lookup_session_by_app(usess
, app
);
5971 if (ua_sess
== NULL
) {
5975 pthread_mutex_lock(&ua_sess
->lock
);
5977 if (ua_sess
->deleted
) {
5978 pthread_mutex_unlock(&ua_sess
->lock
);
5982 /* Lookup channel in the ust app session */
5983 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5984 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5985 if (ua_chan_node
== NULL
) {
5988 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5990 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5995 pthread_mutex_unlock(&ua_sess
->lock
);
6003 * Receive registration and populate the given msg structure.
6005 * On success return 0 else a negative value returned by the ustctl call.
6007 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6010 uint32_t pid
, ppid
, uid
, gid
;
6014 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
6015 &pid
, &ppid
, &uid
, &gid
,
6016 &msg
->bits_per_long
,
6017 &msg
->uint8_t_alignment
,
6018 &msg
->uint16_t_alignment
,
6019 &msg
->uint32_t_alignment
,
6020 &msg
->uint64_t_alignment
,
6021 &msg
->long_alignment
,
6028 case LTTNG_UST_ERR_EXITING
:
6029 DBG3("UST app recv reg message failed. Application died");
6031 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6032 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6033 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
6034 LTTNG_UST_ABI_MINOR_VERSION
);
6037 ERR("UST app recv reg message failed with ret %d", ret
);
6042 msg
->pid
= (pid_t
) pid
;
6043 msg
->ppid
= (pid_t
) ppid
;
6044 msg
->uid
= (uid_t
) uid
;
6045 msg
->gid
= (gid_t
) gid
;
6052 * Return a ust app session object using the application object and the
6053 * session object descriptor has a key. If not found, NULL is returned.
6054 * A RCU read side lock MUST be acquired when calling this function.
6056 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
6059 struct lttng_ht_node_ulong
*node
;
6060 struct lttng_ht_iter iter
;
6061 struct ust_app_session
*ua_sess
= NULL
;
6065 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
6066 node
= lttng_ht_iter_get_node_ulong(&iter
);
6068 DBG2("UST app session find by objd %d not found", objd
);
6072 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
6079 * Return a ust app channel object using the application object and the channel
6080 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6081 * lock MUST be acquired before calling this function.
6083 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
6086 struct lttng_ht_node_ulong
*node
;
6087 struct lttng_ht_iter iter
;
6088 struct ust_app_channel
*ua_chan
= NULL
;
6092 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
6093 node
= lttng_ht_iter_get_node_ulong(&iter
);
6095 DBG2("UST app channel find by objd %d not found", objd
);
6099 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
6106 * Reply to a register channel notification from an application on the notify
6107 * socket. The channel metadata is also created.
6109 * The session UST registry lock is acquired in this function.
6111 * On success 0 is returned else a negative value.
6113 static int reply_ust_register_channel(int sock
, int cobjd
,
6114 size_t nr_fields
, struct ustctl_field
*fields
)
6116 int ret
, ret_code
= 0;
6118 uint64_t chan_reg_key
;
6119 enum ustctl_channel_header type
;
6120 struct ust_app
*app
;
6121 struct ust_app_channel
*ua_chan
;
6122 struct ust_app_session
*ua_sess
;
6123 struct ust_registry_session
*registry
;
6124 struct ust_registry_channel
*chan_reg
;
6128 /* Lookup application. If not found, there is a code flow error. */
6129 app
= find_app_by_notify_sock(sock
);
6131 DBG("Application socket %d is being torn down. Abort event notify",
6134 goto error_rcu_unlock
;
6137 /* Lookup channel by UST object descriptor. */
6138 ua_chan
= find_channel_by_objd(app
, cobjd
);
6140 DBG("Application channel is being torn down. Abort event notify");
6142 goto error_rcu_unlock
;
6145 assert(ua_chan
->session
);
6146 ua_sess
= ua_chan
->session
;
6148 /* Get right session registry depending on the session buffer type. */
6149 registry
= get_session_registry(ua_sess
);
6151 DBG("Application session is being torn down. Abort event notify");
6153 goto error_rcu_unlock
;
6156 /* Depending on the buffer type, a different channel key is used. */
6157 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6158 chan_reg_key
= ua_chan
->tracing_channel_id
;
6160 chan_reg_key
= ua_chan
->key
;
6163 pthread_mutex_lock(®istry
->lock
);
6165 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
6168 if (!chan_reg
->register_done
) {
6170 * TODO: eventually use the registry event count for
6171 * this channel to better guess header type for per-pid
6174 type
= USTCTL_CHANNEL_HEADER_LARGE
;
6175 chan_reg
->nr_ctx_fields
= nr_fields
;
6176 chan_reg
->ctx_fields
= fields
;
6178 chan_reg
->header_type
= type
;
6180 /* Get current already assigned values. */
6181 type
= chan_reg
->header_type
;
6183 /* Channel id is set during the object creation. */
6184 chan_id
= chan_reg
->chan_id
;
6186 /* Append to metadata */
6187 if (!chan_reg
->metadata_dumped
) {
6188 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
6190 ERR("Error appending channel metadata (errno = %d)", ret_code
);
6196 DBG3("UST app replying to register channel key %" PRIu64
6197 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
6200 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
6202 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6203 ERR("UST app reply channel failed with ret %d", ret
);
6205 DBG3("UST app reply channel failed. Application died");
6210 /* This channel registry registration is completed. */
6211 chan_reg
->register_done
= 1;
6214 pthread_mutex_unlock(®istry
->lock
);
6222 * Add event to the UST channel registry. When the event is added to the
6223 * registry, the metadata is also created. Once done, this replies to the
6224 * application with the appropriate error code.
6226 * The session UST registry lock is acquired in the function.
6228 * On success 0 is returned else a negative value.
6230 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
6231 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
6232 int loglevel_value
, char *model_emf_uri
)
6235 uint32_t event_id
= 0;
6236 uint64_t chan_reg_key
;
6237 struct ust_app
*app
;
6238 struct ust_app_channel
*ua_chan
;
6239 struct ust_app_session
*ua_sess
;
6240 struct ust_registry_session
*registry
;
6244 /* Lookup application. If not found, there is a code flow error. */
6245 app
= find_app_by_notify_sock(sock
);
6247 DBG("Application socket %d is being torn down. Abort event notify",
6250 goto error_rcu_unlock
;
6253 /* Lookup channel by UST object descriptor. */
6254 ua_chan
= find_channel_by_objd(app
, cobjd
);
6256 DBG("Application channel is being torn down. Abort event notify");
6258 goto error_rcu_unlock
;
6261 assert(ua_chan
->session
);
6262 ua_sess
= ua_chan
->session
;
6264 registry
= get_session_registry(ua_sess
);
6266 DBG("Application session is being torn down. Abort event notify");
6268 goto error_rcu_unlock
;
6271 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6272 chan_reg_key
= ua_chan
->tracing_channel_id
;
6274 chan_reg_key
= ua_chan
->key
;
6277 pthread_mutex_lock(®istry
->lock
);
6280 * From this point on, this call acquires the ownership of the sig, fields
6281 * and model_emf_uri meaning any free are done inside it if needed. These
6282 * three variables MUST NOT be read/write after this.
6284 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
6285 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
6286 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
6290 model_emf_uri
= NULL
;
6293 * The return value is returned to ustctl so in case of an error, the
6294 * application can be notified. In case of an error, it's important not to
6295 * return a negative error or else the application will get closed.
6297 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
6299 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6300 ERR("UST app reply event failed with ret %d", ret
);
6302 DBG3("UST app reply event failed. Application died");
6305 * No need to wipe the create event since the application socket will
6306 * get close on error hence cleaning up everything by itself.
6311 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6315 pthread_mutex_unlock(®istry
->lock
);
6320 free(model_emf_uri
);
6325 * Add enum to the UST session registry. Once done, this replies to the
6326 * application with the appropriate error code.
6328 * The session UST registry lock is acquired within this function.
6330 * On success 0 is returned else a negative value.
6332 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
6333 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
6335 int ret
= 0, ret_code
;
6336 struct ust_app
*app
;
6337 struct ust_app_session
*ua_sess
;
6338 struct ust_registry_session
*registry
;
6339 uint64_t enum_id
= -1ULL;
6343 /* Lookup application. If not found, there is a code flow error. */
6344 app
= find_app_by_notify_sock(sock
);
6346 /* Return an error since this is not an error */
6347 DBG("Application socket %d is being torn down. Aborting enum registration",
6350 goto error_rcu_unlock
;
6353 /* Lookup session by UST object descriptor. */
6354 ua_sess
= find_session_by_objd(app
, sobjd
);
6356 /* Return an error since this is not an error */
6357 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6359 goto error_rcu_unlock
;
6362 registry
= get_session_registry(ua_sess
);
6364 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6366 goto error_rcu_unlock
;
6369 pthread_mutex_lock(®istry
->lock
);
6372 * From this point on, the callee acquires the ownership of
6373 * entries. The variable entries MUST NOT be read/written after
6376 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
6377 entries
, nr_entries
, &enum_id
);
6381 * The return value is returned to ustctl so in case of an error, the
6382 * application can be notified. In case of an error, it's important not to
6383 * return a negative error or else the application will get closed.
6385 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
6387 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6388 ERR("UST app reply enum failed with ret %d", ret
);
6390 DBG3("UST app reply enum failed. Application died");
6393 * No need to wipe the create enum since the application socket will
6394 * get close on error hence cleaning up everything by itself.
6399 DBG3("UST registry enum %s added successfully or already found", name
);
6402 pthread_mutex_unlock(®istry
->lock
);
6409 * Handle application notification through the given notify socket.
6411 * Return 0 on success or else a negative value.
6413 int ust_app_recv_notify(int sock
)
6416 enum ustctl_notify_cmd cmd
;
6418 DBG3("UST app receiving notify from sock %d", sock
);
6420 ret
= ustctl_recv_notify(sock
, &cmd
);
6422 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6423 ERR("UST app recv notify failed with ret %d", ret
);
6425 DBG3("UST app recv notify failed. Application died");
6431 case USTCTL_NOTIFY_CMD_EVENT
:
6433 int sobjd
, cobjd
, loglevel_value
;
6434 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6436 struct ustctl_field
*fields
;
6438 DBG2("UST app ustctl register event received");
6440 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
6441 &loglevel_value
, &sig
, &nr_fields
, &fields
,
6444 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6445 ERR("UST app recv event failed with ret %d", ret
);
6447 DBG3("UST app recv event failed. Application died");
6453 * Add event to the UST registry coming from the notify socket. This
6454 * call will free if needed the sig, fields and model_emf_uri. This
6455 * code path loses the ownsership of these variables and transfer them
6456 * to the this function.
6458 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6459 fields
, loglevel_value
, model_emf_uri
);
6466 case USTCTL_NOTIFY_CMD_CHANNEL
:
6470 struct ustctl_field
*fields
;
6472 DBG2("UST app ustctl register channel received");
6474 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
6477 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6478 ERR("UST app recv channel failed with ret %d", ret
);
6480 DBG3("UST app recv channel failed. Application died");
6486 * The fields ownership are transfered to this function call meaning
6487 * that if needed it will be freed. After this, it's invalid to access
6488 * fields or clean it up.
6490 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
6498 case USTCTL_NOTIFY_CMD_ENUM
:
6501 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
6503 struct ustctl_enum_entry
*entries
;
6505 DBG2("UST app ustctl register enum received");
6507 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
6508 &entries
, &nr_entries
);
6510 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6511 ERR("UST app recv enum failed with ret %d", ret
);
6513 DBG3("UST app recv enum failed. Application died");
6518 /* Callee assumes ownership of entries */
6519 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6520 entries
, nr_entries
);
6528 /* Should NEVER happen. */
6537 * Once the notify socket hangs up, this is called. First, it tries to find the
6538 * corresponding application. On failure, the call_rcu to close the socket is
6539 * executed. If an application is found, it tries to delete it from the notify
6540 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6542 * Note that an object needs to be allocated here so on ENOMEM failure, the
6543 * call RCU is not done but the rest of the cleanup is.
6545 void ust_app_notify_sock_unregister(int sock
)
6548 struct lttng_ht_iter iter
;
6549 struct ust_app
*app
;
6550 struct ust_app_notify_sock_obj
*obj
;
6556 obj
= zmalloc(sizeof(*obj
));
6559 * An ENOMEM is kind of uncool. If this strikes we continue the
6560 * procedure but the call_rcu will not be called. In this case, we
6561 * accept the fd leak rather than possibly creating an unsynchronized
6562 * state between threads.
6564 * TODO: The notify object should be created once the notify socket is
6565 * registered and stored independantely from the ust app object. The
6566 * tricky part is to synchronize the teardown of the application and
6567 * this notify object. Let's keep that in mind so we can avoid this
6568 * kind of shenanigans with ENOMEM in the teardown path.
6575 DBG("UST app notify socket unregister %d", sock
);
6578 * Lookup application by notify socket. If this fails, this means that the
6579 * hash table delete has already been done by the application
6580 * unregistration process so we can safely close the notify socket in a
6583 app
= find_app_by_notify_sock(sock
);
6588 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6591 * Whatever happens here either we fail or succeed, in both cases we have
6592 * to close the socket after a grace period to continue to the call RCU
6593 * here. If the deletion is successful, the application is not visible
6594 * anymore by other threads and is it fails it means that it was already
6595 * deleted from the hash table so either way we just have to close the
6598 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6604 * Close socket after a grace period to avoid for the socket to be reused
6605 * before the application object is freed creating potential race between
6606 * threads trying to add unique in the global hash table.
6609 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6614 * Destroy a ust app data structure and free its memory.
6616 void ust_app_destroy(struct ust_app
*app
)
6622 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6626 * Take a snapshot for a given UST session. The snapshot is sent to the given
6629 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6631 enum lttng_error_code
ust_app_snapshot_record(
6632 const struct ltt_ust_session
*usess
,
6633 const struct consumer_output
*output
, int wait
,
6634 uint64_t nb_packets_per_stream
)
6637 enum lttng_error_code status
= LTTNG_OK
;
6638 struct lttng_ht_iter iter
;
6639 struct ust_app
*app
;
6640 char *trace_path
= NULL
;
6647 switch (usess
->buffer_type
) {
6648 case LTTNG_BUFFER_PER_UID
:
6650 struct buffer_reg_uid
*reg
;
6652 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6653 struct buffer_reg_channel
*reg_chan
;
6654 struct consumer_socket
*socket
;
6655 char pathname
[PATH_MAX
];
6656 size_t consumer_path_offset
= 0;
6658 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6659 /* Skip since no metadata is present */
6663 /* Get consumer socket to use to push the metadata.*/
6664 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6667 status
= LTTNG_ERR_INVALID
;
6671 memset(pathname
, 0, sizeof(pathname
));
6672 ret
= snprintf(pathname
, sizeof(pathname
),
6673 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
6674 reg
->uid
, reg
->bits_per_long
);
6676 PERROR("snprintf snapshot path");
6677 status
= LTTNG_ERR_INVALID
;
6680 /* Free path allowed on previous iteration. */
6682 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6683 &consumer_path_offset
);
6685 status
= LTTNG_ERR_INVALID
;
6688 /* Add the UST default trace dir to path. */
6689 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6690 reg_chan
, node
.node
) {
6691 status
= consumer_snapshot_channel(socket
,
6692 reg_chan
->consumer_key
,
6693 output
, 0, usess
->uid
,
6694 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
6695 nb_packets_per_stream
);
6696 if (status
!= LTTNG_OK
) {
6700 status
= consumer_snapshot_channel(socket
,
6701 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6702 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6704 if (status
!= LTTNG_OK
) {
6710 case LTTNG_BUFFER_PER_PID
:
6712 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6713 struct consumer_socket
*socket
;
6714 struct lttng_ht_iter chan_iter
;
6715 struct ust_app_channel
*ua_chan
;
6716 struct ust_app_session
*ua_sess
;
6717 struct ust_registry_session
*registry
;
6718 char pathname
[PATH_MAX
];
6719 size_t consumer_path_offset
= 0;
6721 ua_sess
= lookup_session_by_app(usess
, app
);
6723 /* Session not associated with this app. */
6727 /* Get the right consumer socket for the application. */
6728 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6731 status
= LTTNG_ERR_INVALID
;
6735 /* Add the UST default trace dir to path. */
6736 memset(pathname
, 0, sizeof(pathname
));
6737 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
6740 status
= LTTNG_ERR_INVALID
;
6741 PERROR("snprintf snapshot path");
6744 /* Free path allowed on previous iteration. */
6746 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6747 &consumer_path_offset
);
6749 status
= LTTNG_ERR_INVALID
;
6752 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6753 ua_chan
, node
.node
) {
6754 status
= consumer_snapshot_channel(socket
,
6755 ua_chan
->key
, output
, 0,
6756 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6757 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6758 &trace_path
[consumer_path_offset
], wait
,
6759 nb_packets_per_stream
);
6763 case LTTNG_ERR_CHAN_NOT_FOUND
:
6770 registry
= get_session_registry(ua_sess
);
6772 DBG("Application session is being torn down. Skip application.");
6775 status
= consumer_snapshot_channel(socket
,
6776 registry
->metadata_key
, output
, 1,
6777 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6778 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6779 &trace_path
[consumer_path_offset
], wait
, 0);
6783 case LTTNG_ERR_CHAN_NOT_FOUND
:
6803 * Return the size taken by one more packet per stream.
6805 uint64_t ust_app_get_size_one_more_packet_per_stream(
6806 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6808 uint64_t tot_size
= 0;
6809 struct ust_app
*app
;
6810 struct lttng_ht_iter iter
;
6814 switch (usess
->buffer_type
) {
6815 case LTTNG_BUFFER_PER_UID
:
6817 struct buffer_reg_uid
*reg
;
6819 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6820 struct buffer_reg_channel
*reg_chan
;
6823 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6824 reg_chan
, node
.node
) {
6825 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6827 * Don't take channel into account if we
6828 * already grab all its packets.
6832 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6838 case LTTNG_BUFFER_PER_PID
:
6841 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6842 struct ust_app_channel
*ua_chan
;
6843 struct ust_app_session
*ua_sess
;
6844 struct lttng_ht_iter chan_iter
;
6846 ua_sess
= lookup_session_by_app(usess
, app
);
6848 /* Session not associated with this app. */
6852 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6853 ua_chan
, node
.node
) {
6854 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6856 * Don't take channel into account if we
6857 * already grab all its packets.
6861 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6875 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6876 struct cds_list_head
*buffer_reg_uid_list
,
6877 struct consumer_output
*consumer
, uint64_t uchan_id
,
6878 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6881 uint64_t consumer_chan_key
;
6886 ret
= buffer_reg_uid_consumer_channel_key(
6887 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6895 ret
= consumer_get_lost_packets(ust_session_id
,
6896 consumer_chan_key
, consumer
, lost
);
6898 ret
= consumer_get_discarded_events(ust_session_id
,
6899 consumer_chan_key
, consumer
, discarded
);
6906 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6907 struct ltt_ust_channel
*uchan
,
6908 struct consumer_output
*consumer
, int overwrite
,
6909 uint64_t *discarded
, uint64_t *lost
)
6912 struct lttng_ht_iter iter
;
6913 struct lttng_ht_node_str
*ua_chan_node
;
6914 struct ust_app
*app
;
6915 struct ust_app_session
*ua_sess
;
6916 struct ust_app_channel
*ua_chan
;
6923 * Iterate over every registered applications. Sum counters for
6924 * all applications containing requested session and channel.
6926 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6927 struct lttng_ht_iter uiter
;
6929 ua_sess
= lookup_session_by_app(usess
, app
);
6930 if (ua_sess
== NULL
) {
6935 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6936 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6937 /* If the session is found for the app, the channel must be there */
6938 assert(ua_chan_node
);
6940 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6945 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6952 uint64_t _discarded
;
6954 ret
= consumer_get_discarded_events(usess
->id
,
6955 ua_chan
->key
, consumer
, &_discarded
);
6959 (*discarded
) += _discarded
;
6968 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6969 struct ust_app
*app
)
6972 struct ust_app_session
*ua_sess
;
6974 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6978 ua_sess
= lookup_session_by_app(usess
, app
);
6979 if (ua_sess
== NULL
) {
6980 /* The session is in teardown process. Ignore and continue. */
6984 pthread_mutex_lock(&ua_sess
->lock
);
6986 if (ua_sess
->deleted
) {
6990 pthread_mutex_lock(&app
->sock_lock
);
6991 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6992 pthread_mutex_unlock(&app
->sock_lock
);
6995 pthread_mutex_unlock(&ua_sess
->lock
);
6999 health_code_update();
7004 * Regenerate the statedump for each app in the session.
7006 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7009 struct lttng_ht_iter iter
;
7010 struct ust_app
*app
;
7012 DBG("Regenerating the metadata for all UST apps");
7016 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7017 if (!app
->compatible
) {
7021 ret
= ust_app_regenerate_statedump(usess
, app
);
7023 /* Continue to the next app even on error */
7034 * Rotate all the channels of a session.
7036 * Return LTTNG_OK on success or else an LTTng error code.
7038 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7041 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7042 struct lttng_ht_iter iter
;
7043 struct ust_app
*app
;
7044 struct ltt_ust_session
*usess
= session
->ust_session
;
7050 switch (usess
->buffer_type
) {
7051 case LTTNG_BUFFER_PER_UID
:
7053 struct buffer_reg_uid
*reg
;
7055 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7056 struct buffer_reg_channel
*reg_chan
;
7057 struct consumer_socket
*socket
;
7059 if (!reg
->registry
->reg
.ust
->metadata_key
) {
7060 /* Skip since no metadata is present */
7064 /* Get consumer socket to use to push the metadata.*/
7065 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7068 cmd_ret
= LTTNG_ERR_INVALID
;
7072 /* Rotate the data channels. */
7073 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7074 reg_chan
, node
.node
) {
7075 ret
= consumer_rotate_channel(socket
,
7076 reg_chan
->consumer_key
,
7077 usess
->uid
, usess
->gid
,
7079 /* is_metadata_channel */ false);
7081 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7086 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7088 ret
= consumer_rotate_channel(socket
,
7089 reg
->registry
->reg
.ust
->metadata_key
,
7090 usess
->uid
, usess
->gid
,
7092 /* is_metadata_channel */ true);
7094 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7100 case LTTNG_BUFFER_PER_PID
:
7102 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7103 struct consumer_socket
*socket
;
7104 struct lttng_ht_iter chan_iter
;
7105 struct ust_app_channel
*ua_chan
;
7106 struct ust_app_session
*ua_sess
;
7107 struct ust_registry_session
*registry
;
7109 ua_sess
= lookup_session_by_app(usess
, app
);
7111 /* Session not associated with this app. */
7115 /* Get the right consumer socket for the application. */
7116 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7119 cmd_ret
= LTTNG_ERR_INVALID
;
7123 registry
= get_session_registry(ua_sess
);
7125 DBG("Application session is being torn down. Skip application.");
7129 /* Rotate the data channels. */
7130 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7131 ua_chan
, node
.node
) {
7132 ret
= consumer_rotate_channel(socket
,
7134 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7135 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7137 /* is_metadata_channel */ false);
7139 /* Per-PID buffer and application going away. */
7140 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7142 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7147 /* Rotate the metadata channel. */
7148 (void) push_metadata(registry
, usess
->consumer
);
7149 ret
= consumer_rotate_channel(socket
,
7150 registry
->metadata_key
,
7151 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7152 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7154 /* is_metadata_channel */ true);
7156 /* Per-PID buffer and application going away. */
7157 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7159 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7177 enum lttng_error_code
ust_app_create_channel_subdirectories(
7178 const struct ltt_ust_session
*usess
)
7180 enum lttng_error_code ret
= LTTNG_OK
;
7181 struct lttng_ht_iter iter
;
7182 enum lttng_trace_chunk_status chunk_status
;
7183 char *pathname_index
;
7186 assert(usess
->current_trace_chunk
);
7189 switch (usess
->buffer_type
) {
7190 case LTTNG_BUFFER_PER_UID
:
7192 struct buffer_reg_uid
*reg
;
7194 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7195 fmt_ret
= asprintf(&pathname_index
,
7196 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7197 reg
->uid
, reg
->bits_per_long
);
7199 ERR("Failed to format channel index directory");
7200 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7205 * Create the index subdirectory which will take care
7206 * of implicitly creating the channel's path.
7208 chunk_status
= lttng_trace_chunk_create_subdirectory(
7209 usess
->current_trace_chunk
,
7211 free(pathname_index
);
7212 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7213 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7219 case LTTNG_BUFFER_PER_PID
:
7221 struct ust_app
*app
;
7224 * Create the toplevel ust/ directory in case no apps are running.
7226 chunk_status
= lttng_trace_chunk_create_subdirectory(
7227 usess
->current_trace_chunk
,
7228 DEFAULT_UST_TRACE_DIR
);
7229 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7230 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7234 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7236 struct ust_app_session
*ua_sess
;
7237 struct ust_registry_session
*registry
;
7239 ua_sess
= lookup_session_by_app(usess
, app
);
7241 /* Session not associated with this app. */
7245 registry
= get_session_registry(ua_sess
);
7247 DBG("Application session is being torn down. Skip application.");
7251 fmt_ret
= asprintf(&pathname_index
,
7252 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7255 ERR("Failed to format channel index directory");
7256 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7260 * Create the index subdirectory which will take care
7261 * of implicitly creating the channel's path.
7263 chunk_status
= lttng_trace_chunk_create_subdirectory(
7264 usess
->current_trace_chunk
,
7266 free(pathname_index
);
7267 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7268 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7285 * Clear all the channels of a session.
7287 * Return LTTNG_OK on success or else an LTTng error code.
7289 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7292 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7293 struct lttng_ht_iter iter
;
7294 struct ust_app
*app
;
7295 struct ltt_ust_session
*usess
= session
->ust_session
;
7301 if (usess
->active
) {
7302 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7303 cmd_ret
= LTTNG_ERR_FATAL
;
7307 switch (usess
->buffer_type
) {
7308 case LTTNG_BUFFER_PER_UID
:
7310 struct buffer_reg_uid
*reg
;
7312 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7313 struct buffer_reg_channel
*reg_chan
;
7314 struct consumer_socket
*socket
;
7316 /* Get consumer socket to use to push the metadata.*/
7317 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7320 cmd_ret
= LTTNG_ERR_INVALID
;
7324 /* Clear the data channels. */
7325 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7326 reg_chan
, node
.node
) {
7327 ret
= consumer_clear_channel(socket
,
7328 reg_chan
->consumer_key
);
7334 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7337 * Clear the metadata channel.
7338 * Metadata channel is not cleared per se but we still need to
7339 * perform a rotation operation on it behind the scene.
7341 ret
= consumer_clear_channel(socket
,
7342 reg
->registry
->reg
.ust
->metadata_key
);
7349 case LTTNG_BUFFER_PER_PID
:
7351 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7352 struct consumer_socket
*socket
;
7353 struct lttng_ht_iter chan_iter
;
7354 struct ust_app_channel
*ua_chan
;
7355 struct ust_app_session
*ua_sess
;
7356 struct ust_registry_session
*registry
;
7358 ua_sess
= lookup_session_by_app(usess
, app
);
7360 /* Session not associated with this app. */
7364 /* Get the right consumer socket for the application. */
7365 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7368 cmd_ret
= LTTNG_ERR_INVALID
;
7372 registry
= get_session_registry(ua_sess
);
7374 DBG("Application session is being torn down. Skip application.");
7378 /* Clear the data channels. */
7379 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7380 ua_chan
, node
.node
) {
7381 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7383 /* Per-PID buffer and application going away. */
7384 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7391 (void) push_metadata(registry
, usess
->consumer
);
7394 * Clear the metadata channel.
7395 * Metadata channel is not cleared per se but we still need to
7396 * perform rotation operation on it behind the scene.
7398 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
7400 /* Per-PID buffer and application going away. */
7401 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7419 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7420 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7423 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7433 * This function skips the metadata channel as the begin/end timestamps of a
7434 * metadata packet are useless.
7436 * Moreover, opening a packet after a "clear" will cause problems for live
7437 * sessions as it will introduce padding that was not part of the first trace
7438 * chunk. The relay daemon expects the content of the metadata stream of
7439 * successive metadata trace chunks to be strict supersets of one another.
7441 * For example, flushing a packet at the beginning of the metadata stream of
7442 * a trace chunk resulting from a "clear" session command will cause the
7443 * size of the metadata stream of the new trace chunk to not match the size of
7444 * the metadata stream of the original chunk. This will confuse the relay
7445 * daemon as the same "offset" in a metadata stream will no longer point
7446 * to the same content.
7448 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7450 enum lttng_error_code ret
= LTTNG_OK
;
7451 struct lttng_ht_iter iter
;
7452 struct ltt_ust_session
*usess
= session
->ust_session
;
7458 switch (usess
->buffer_type
) {
7459 case LTTNG_BUFFER_PER_UID
:
7461 struct buffer_reg_uid
*reg
;
7463 cds_list_for_each_entry (
7464 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7465 struct buffer_reg_channel
*reg_chan
;
7466 struct consumer_socket
*socket
;
7468 socket
= consumer_find_socket_by_bitness(
7469 reg
->bits_per_long
, usess
->consumer
);
7471 ret
= LTTNG_ERR_FATAL
;
7475 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7476 &iter
.iter
, reg_chan
, node
.node
) {
7477 const int open_ret
=
7478 consumer_open_channel_packets(
7480 reg_chan
->consumer_key
);
7483 ret
= LTTNG_ERR_UNK
;
7490 case LTTNG_BUFFER_PER_PID
:
7492 struct ust_app
*app
;
7494 cds_lfht_for_each_entry (
7495 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7496 struct consumer_socket
*socket
;
7497 struct lttng_ht_iter chan_iter
;
7498 struct ust_app_channel
*ua_chan
;
7499 struct ust_app_session
*ua_sess
;
7500 struct ust_registry_session
*registry
;
7502 ua_sess
= lookup_session_by_app(usess
, app
);
7504 /* Session not associated with this app. */
7508 /* Get the right consumer socket for the application. */
7509 socket
= consumer_find_socket_by_bitness(
7510 app
->bits_per_long
, usess
->consumer
);
7512 ret
= LTTNG_ERR_FATAL
;
7516 registry
= get_session_registry(ua_sess
);
7518 DBG("Application session is being torn down. Skip application.");
7522 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
7523 &chan_iter
.iter
, ua_chan
, node
.node
) {
7524 const int open_ret
=
7525 consumer_open_channel_packets(
7531 * Per-PID buffer and application going
7534 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7538 ret
= LTTNG_ERR_UNK
;