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/event-rule-internal.h>
30 #include <lttng/condition/event-rule.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_EVENT_RULE_HIT
);
1258 assert(LTTNG_CONDITION_STATUS_OK
== lttng_condition_event_rule_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_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_loglevel_type loglevel_type
;
2004 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2005 int loglevel
= -1, ret
= 0;
2006 const char *pattern
;
2008 /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported. */
2009 assert(lttng_event_rule_get_type(rule
) ==
2010 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
2012 memset(event_notifier
, 0, sizeof(*event_notifier
));
2014 if (lttng_event_rule_targets_agent_domain(rule
)) {
2016 * Special event for agents
2017 * The actual meat of the event is in the filter that will be
2018 * attached later on.
2019 * Set the default values for the agent event.
2021 pattern
= event_get_default_agent_ust_name(
2022 lttng_event_rule_get_domain_type(rule
));
2024 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2026 status
= lttng_event_rule_tracepoint_get_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_type(
2034 rule
, &loglevel_type
);
2035 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2036 /* At this point, this is a fatal error. */
2040 switch (loglevel_type
) {
2041 case LTTNG_EVENT_LOGLEVEL_ALL
:
2042 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2044 case LTTNG_EVENT_LOGLEVEL_RANGE
:
2045 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2047 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
2048 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2051 /* Unknown log level specification type. */
2055 if (loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
2056 status
= lttng_event_rule_tracepoint_get_log_level(
2058 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
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_EVENT_RULE_HIT
);
2100 condition_status
= lttng_condition_event_rule_get_rule(condition
, &event_rule
);
2101 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2103 assert(lttng_event_rule_get_type(event_rule
) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
2105 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2106 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2108 /* Create UST event notifier against the tracer. */
2109 pthread_mutex_lock(&app
->sock_lock
);
2110 ret
= ustctl_create_event_notifier(app
->sock
, &event_notifier
,
2111 app
->event_notifier_group
.object
,
2112 &ua_event_notifier_rule
->obj
);
2113 pthread_mutex_unlock(&app
->sock_lock
);
2115 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2116 ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d",
2117 event_notifier
.event
.name
, app
->name
,
2121 * This is normal behavior, an application can die
2122 * during the creation process. Don't report an error so
2123 * the execution can continue normally.
2126 DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)",
2127 app
->name
, app
->ppid
);
2133 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2135 DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p",
2136 event_notifier
.event
.name
, app
->name
, app
->ppid
,
2137 ua_event_notifier_rule
->obj
);
2139 health_code_update();
2141 /* Set filter if one is present. */
2142 if (ua_event_notifier_rule
->filter
) {
2143 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2144 ua_event_notifier_rule
->obj
);
2150 /* Set exclusions for the event. */
2151 if (ua_event_notifier_rule
->exclusion
) {
2152 ret
= set_ust_object_exclusions(app
,
2153 ua_event_notifier_rule
->exclusion
,
2154 ua_event_notifier_rule
->obj
);
2160 /* Set the capture bytecodes. */
2161 cond_status
= lttng_condition_event_rule_get_capture_descriptor_count(
2162 condition
, &capture_bytecode_count
);
2163 assert(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2165 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2166 const struct lttng_bytecode
*capture_bytecode
=
2167 lttng_condition_event_rule_get_capture_bytecode_at_index(
2170 ret
= set_ust_capture(app
, capture_bytecode
, i
,
2171 ua_event_notifier_rule
->obj
);
2178 * We now need to explicitly enable the event, since it
2179 * is disabled at creation.
2181 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2184 * If we hit an EPERM, something is wrong with our enable call.
2185 * If we get an EEXIST, there is a problem on the tracer side
2186 * since we just created it.
2189 case -LTTNG_UST_ERR_PERM
:
2190 /* Code flow problem. */
2192 case -LTTNG_UST_ERR_EXIST
:
2193 /* It's OK for our use case. */
2203 ua_event_notifier_rule
->enabled
= true;
2206 health_code_update();
2211 * Copy data between an UST app event and a LTT event.
2213 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2214 struct ltt_ust_event
*uevent
)
2216 size_t exclusion_alloc_size
;
2218 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2219 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2221 ua_event
->enabled
= uevent
->enabled
;
2223 /* Copy event attributes */
2224 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2226 /* Copy filter bytecode */
2227 if (uevent
->filter
) {
2228 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2229 /* Filter might be NULL here in case of ENONEM. */
2232 /* Copy exclusion data */
2233 if (uevent
->exclusion
) {
2234 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2235 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2236 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
2237 if (ua_event
->exclusion
== NULL
) {
2240 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2241 exclusion_alloc_size
);
2247 * Copy data between an UST app channel and a LTT channel.
2249 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2250 struct ltt_ust_channel
*uchan
)
2252 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2254 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2255 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2257 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2258 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2260 /* Copy event attributes since the layout is different. */
2261 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2262 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2263 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2264 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2265 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2266 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2267 ua_chan
->attr
.output
= uchan
->attr
.output
;
2268 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2271 * Note that the attribute channel type is not set since the channel on the
2272 * tracing registry side does not have this information.
2275 ua_chan
->enabled
= uchan
->enabled
;
2276 ua_chan
->tracing_channel_id
= uchan
->id
;
2278 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2282 * Copy data between a UST app session and a regular LTT session.
2284 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2285 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2287 struct tm
*timeinfo
;
2290 char tmp_shm_path
[PATH_MAX
];
2292 timeinfo
= localtime(&app
->registration_time
);
2293 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2295 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2297 ua_sess
->tracing_id
= usess
->id
;
2298 ua_sess
->id
= get_next_session_id();
2299 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2300 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2301 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2302 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2303 ua_sess
->buffer_type
= usess
->buffer_type
;
2304 ua_sess
->bits_per_long
= app
->bits_per_long
;
2306 /* There is only one consumer object per session possible. */
2307 consumer_output_get(usess
->consumer
);
2308 ua_sess
->consumer
= usess
->consumer
;
2310 ua_sess
->output_traces
= usess
->output_traces
;
2311 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2312 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2313 &usess
->metadata_attr
);
2315 switch (ua_sess
->buffer_type
) {
2316 case LTTNG_BUFFER_PER_PID
:
2317 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2318 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2321 case LTTNG_BUFFER_PER_UID
:
2322 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2323 DEFAULT_UST_TRACE_UID_PATH
,
2324 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2325 app
->bits_per_long
);
2332 PERROR("asprintf UST shadow copy session");
2337 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2338 sizeof(ua_sess
->root_shm_path
));
2339 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2340 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2341 sizeof(ua_sess
->shm_path
));
2342 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2343 if (ua_sess
->shm_path
[0]) {
2344 switch (ua_sess
->buffer_type
) {
2345 case LTTNG_BUFFER_PER_PID
:
2346 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2347 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2348 app
->name
, app
->pid
, datetime
);
2350 case LTTNG_BUFFER_PER_UID
:
2351 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2352 "/" DEFAULT_UST_TRACE_UID_PATH
,
2353 app
->uid
, app
->bits_per_long
);
2360 PERROR("sprintf UST shadow copy session");
2364 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2365 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2366 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2371 consumer_output_put(ua_sess
->consumer
);
2375 * Lookup sesison wrapper.
2378 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2379 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2381 /* Get right UST app session from app */
2382 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2386 * Return ust app session from the app session hashtable using the UST session
2389 static struct ust_app_session
*lookup_session_by_app(
2390 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2392 struct lttng_ht_iter iter
;
2393 struct lttng_ht_node_u64
*node
;
2395 __lookup_session_by_app(usess
, app
, &iter
);
2396 node
= lttng_ht_iter_get_node_u64(&iter
);
2401 return caa_container_of(node
, struct ust_app_session
, node
);
2408 * Setup buffer registry per PID for the given session and application. If none
2409 * is found, a new one is created, added to the global registry and
2410 * initialized. If regp is valid, it's set with the newly created object.
2412 * Return 0 on success or else a negative value.
2414 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2415 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2418 struct buffer_reg_pid
*reg_pid
;
2425 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2428 * This is the create channel path meaning that if there is NO
2429 * registry available, we have to create one for this session.
2431 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2432 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2440 /* Initialize registry. */
2441 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2442 app
->bits_per_long
, app
->uint8_t_alignment
,
2443 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2444 app
->uint64_t_alignment
, app
->long_alignment
,
2445 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2446 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2447 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2448 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2449 ua_sess
->tracing_id
,
2453 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2454 * destroy the buffer registry, because it is always expected
2455 * that if the buffer registry can be found, its ust registry is
2458 buffer_reg_pid_destroy(reg_pid
);
2462 buffer_reg_pid_add(reg_pid
);
2464 DBG3("UST app buffer registry per PID created successfully");
2476 * Setup buffer registry per UID for the given session and application. If none
2477 * is found, a new one is created, added to the global registry and
2478 * initialized. If regp is valid, it's set with the newly created object.
2480 * Return 0 on success or else a negative value.
2482 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2483 struct ust_app_session
*ua_sess
,
2484 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2487 struct buffer_reg_uid
*reg_uid
;
2494 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2497 * This is the create channel path meaning that if there is NO
2498 * registry available, we have to create one for this session.
2500 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2501 LTTNG_DOMAIN_UST
, ®_uid
,
2502 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2510 /* Initialize registry. */
2511 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2512 app
->bits_per_long
, app
->uint8_t_alignment
,
2513 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2514 app
->uint64_t_alignment
, app
->long_alignment
,
2515 app
->byte_order
, app
->version
.major
,
2516 app
->version
.minor
, reg_uid
->root_shm_path
,
2517 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2518 ua_sess
->tracing_id
, app
->uid
);
2521 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2522 * destroy the buffer registry, because it is always expected
2523 * that if the buffer registry can be found, its ust registry is
2526 buffer_reg_uid_destroy(reg_uid
, NULL
);
2529 /* Add node to teardown list of the session. */
2530 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2532 buffer_reg_uid_add(reg_uid
);
2534 DBG3("UST app buffer registry per UID created successfully");
2545 * Create a session on the tracer side for the given app.
2547 * On success, ua_sess_ptr is populated with the session pointer or else left
2548 * untouched. If the session was created, is_created is set to 1. On error,
2549 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2552 * Returns 0 on success or else a negative code which is either -ENOMEM or
2553 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2555 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2556 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2559 int ret
, created
= 0;
2560 struct ust_app_session
*ua_sess
;
2564 assert(ua_sess_ptr
);
2566 health_code_update();
2568 ua_sess
= lookup_session_by_app(usess
, app
);
2569 if (ua_sess
== NULL
) {
2570 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2571 app
->pid
, usess
->id
);
2572 ua_sess
= alloc_ust_app_session();
2573 if (ua_sess
== NULL
) {
2574 /* Only malloc can failed so something is really wrong */
2578 shadow_copy_session(ua_sess
, usess
, app
);
2582 switch (usess
->buffer_type
) {
2583 case LTTNG_BUFFER_PER_PID
:
2584 /* Init local registry. */
2585 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2587 delete_ust_app_session(-1, ua_sess
, app
);
2591 case LTTNG_BUFFER_PER_UID
:
2592 /* Look for a global registry. If none exists, create one. */
2593 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2595 delete_ust_app_session(-1, ua_sess
, app
);
2605 health_code_update();
2607 if (ua_sess
->handle
== -1) {
2608 pthread_mutex_lock(&app
->sock_lock
);
2609 ret
= ustctl_create_session(app
->sock
);
2610 pthread_mutex_unlock(&app
->sock_lock
);
2612 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2613 ERR("Creating session for app pid %d with ret %d",
2616 DBG("UST app creating session failed. Application is dead");
2618 * This is normal behavior, an application can die during the
2619 * creation process. Don't report an error so the execution can
2620 * continue normally. This will get flagged ENOTCONN and the
2621 * caller will handle it.
2625 delete_ust_app_session(-1, ua_sess
, app
);
2626 if (ret
!= -ENOMEM
) {
2628 * Tracer is probably gone or got an internal error so let's
2629 * behave like it will soon unregister or not usable.
2636 ua_sess
->handle
= ret
;
2638 /* Add ust app session to app's HT */
2639 lttng_ht_node_init_u64(&ua_sess
->node
,
2640 ua_sess
->tracing_id
);
2641 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2642 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2643 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2644 &ua_sess
->ust_objd_node
);
2646 DBG2("UST app session created successfully with handle %d", ret
);
2649 *ua_sess_ptr
= ua_sess
;
2651 *is_created
= created
;
2654 /* Everything went well. */
2658 health_code_update();
2663 * Match function for a hash table lookup of ust_app_ctx.
2665 * It matches an ust app context based on the context type and, in the case
2666 * of perf counters, their name.
2668 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2670 struct ust_app_ctx
*ctx
;
2671 const struct lttng_ust_context_attr
*key
;
2676 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2680 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2685 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2686 if (strncmp(key
->u
.perf_counter
.name
,
2687 ctx
->ctx
.u
.perf_counter
.name
,
2688 sizeof(key
->u
.perf_counter
.name
))) {
2692 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2693 if (strcmp(key
->u
.app_ctx
.provider_name
,
2694 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2695 strcmp(key
->u
.app_ctx
.ctx_name
,
2696 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2712 * Lookup for an ust app context from an lttng_ust_context.
2714 * Must be called while holding RCU read side lock.
2715 * Return an ust_app_ctx object or NULL on error.
2718 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2719 struct lttng_ust_context_attr
*uctx
)
2721 struct lttng_ht_iter iter
;
2722 struct lttng_ht_node_ulong
*node
;
2723 struct ust_app_ctx
*app_ctx
= NULL
;
2728 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2729 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2730 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2731 node
= lttng_ht_iter_get_node_ulong(&iter
);
2736 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2743 * Create a context for the channel on the tracer.
2745 * Called with UST app session lock held and a RCU read side lock.
2748 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2749 struct lttng_ust_context_attr
*uctx
,
2750 struct ust_app
*app
)
2753 struct ust_app_ctx
*ua_ctx
;
2755 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2757 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2763 ua_ctx
= alloc_ust_app_ctx(uctx
);
2764 if (ua_ctx
== NULL
) {
2770 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2771 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2772 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2774 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2784 * Enable on the tracer side a ust app event for the session and channel.
2786 * Called with UST app session lock held.
2789 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2790 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2794 ret
= enable_ust_object(app
, ua_event
->obj
);
2799 ua_event
->enabled
= 1;
2806 * Disable on the tracer side a ust app event for the session and channel.
2808 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2809 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2813 ret
= disable_ust_object(app
, ua_event
->obj
);
2818 ua_event
->enabled
= 0;
2825 * Lookup ust app channel for session and disable it on the tracer side.
2828 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2829 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2833 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2838 ua_chan
->enabled
= 0;
2845 * Lookup ust app channel for session and enable it on the tracer side. This
2846 * MUST be called with a RCU read side lock acquired.
2848 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2849 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2852 struct lttng_ht_iter iter
;
2853 struct lttng_ht_node_str
*ua_chan_node
;
2854 struct ust_app_channel
*ua_chan
;
2856 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2857 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2858 if (ua_chan_node
== NULL
) {
2859 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2860 uchan
->name
, ua_sess
->tracing_id
);
2864 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2866 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2876 * Ask the consumer to create a channel and get it if successful.
2878 * Called with UST app session lock held.
2880 * Return 0 on success or else a negative value.
2882 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2883 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2884 int bitness
, struct ust_registry_session
*registry
,
2885 uint64_t trace_archive_id
)
2888 unsigned int nb_fd
= 0;
2889 struct consumer_socket
*socket
;
2897 health_code_update();
2899 /* Get the right consumer socket for the application. */
2900 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2906 health_code_update();
2908 /* Need one fd for the channel. */
2909 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2911 ERR("Exhausted number of available FD upon create channel");
2916 * Ask consumer to create channel. The consumer will return the number of
2917 * stream we have to expect.
2919 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2920 registry
, usess
->current_trace_chunk
);
2926 * Compute the number of fd needed before receiving them. It must be 2 per
2927 * stream (2 being the default value here).
2929 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2931 /* Reserve the amount of file descriptor we need. */
2932 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2934 ERR("Exhausted number of available FD upon create channel");
2935 goto error_fd_get_stream
;
2938 health_code_update();
2941 * Now get the channel from the consumer. This call wil populate the stream
2942 * list of that channel and set the ust objects.
2944 if (usess
->consumer
->enabled
) {
2945 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2955 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2956 error_fd_get_stream
:
2958 * Initiate a destroy channel on the consumer since we had an error
2959 * handling it on our side. The return value is of no importance since we
2960 * already have a ret value set by the previous error that we need to
2963 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2965 lttng_fd_put(LTTNG_FD_APPS
, 1);
2967 health_code_update();
2973 * Duplicate the ust data object of the ust app stream and save it in the
2974 * buffer registry stream.
2976 * Return 0 on success or else a negative value.
2978 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2979 struct ust_app_stream
*stream
)
2986 /* Reserve the amount of file descriptor we need. */
2987 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2989 ERR("Exhausted number of available FD upon duplicate stream");
2993 /* Duplicate object for stream once the original is in the registry. */
2994 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2995 reg_stream
->obj
.ust
);
2997 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2998 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2999 lttng_fd_put(LTTNG_FD_APPS
, 2);
3002 stream
->handle
= stream
->obj
->handle
;
3009 * Duplicate the ust data object of the ust app. channel and save it in the
3010 * buffer registry channel.
3012 * Return 0 on success or else a negative value.
3014 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
3015 struct ust_app_channel
*ua_chan
)
3022 /* Need two fds for the channel. */
3023 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3025 ERR("Exhausted number of available FD upon duplicate channel");
3029 /* Duplicate object for stream once the original is in the registry. */
3030 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
3032 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3033 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
3036 ua_chan
->handle
= ua_chan
->obj
->handle
;
3041 lttng_fd_put(LTTNG_FD_APPS
, 1);
3047 * For a given channel buffer registry, setup all streams of the given ust
3048 * application channel.
3050 * Return 0 on success or else a negative value.
3052 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
3053 struct ust_app_channel
*ua_chan
,
3054 struct ust_app
*app
)
3057 struct ust_app_stream
*stream
, *stmp
;
3062 DBG2("UST app setup buffer registry stream");
3064 /* Send all streams to application. */
3065 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3066 struct buffer_reg_stream
*reg_stream
;
3068 ret
= buffer_reg_stream_create(®_stream
);
3074 * Keep original pointer and nullify it in the stream so the delete
3075 * stream call does not release the object.
3077 reg_stream
->obj
.ust
= stream
->obj
;
3079 buffer_reg_stream_add(reg_stream
, reg_chan
);
3081 /* We don't need the streams anymore. */
3082 cds_list_del(&stream
->list
);
3083 delete_ust_app_stream(-1, stream
, app
);
3091 * Create a buffer registry channel for the given session registry and
3092 * application channel object. If regp pointer is valid, it's set with the
3093 * created object. Important, the created object is NOT added to the session
3094 * registry hash table.
3096 * Return 0 on success else a negative value.
3098 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3099 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
3102 struct buffer_reg_channel
*reg_chan
= NULL
;
3107 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3109 /* Create buffer registry channel. */
3110 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
3115 reg_chan
->consumer_key
= ua_chan
->key
;
3116 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3117 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3119 /* Create and add a channel registry to session. */
3120 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
3121 ua_chan
->tracing_channel_id
);
3125 buffer_reg_channel_add(reg_sess
, reg_chan
);
3134 /* Safe because the registry channel object was not added to any HT. */
3135 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3141 * Setup buffer registry channel for the given session registry and application
3142 * channel object. If regp pointer is valid, it's set with the created object.
3144 * Return 0 on success else a negative value.
3146 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3147 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
3148 struct ust_app
*app
)
3155 assert(ua_chan
->obj
);
3157 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3159 /* Setup all streams for the registry. */
3160 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
3165 reg_chan
->obj
.ust
= ua_chan
->obj
;
3166 ua_chan
->obj
= NULL
;
3171 buffer_reg_channel_remove(reg_sess
, reg_chan
);
3172 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3177 * Send buffer registry channel to the application.
3179 * Return 0 on success else a negative value.
3181 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
3182 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
3183 struct ust_app_channel
*ua_chan
)
3186 struct buffer_reg_stream
*reg_stream
;
3193 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3195 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
3200 /* Send channel to the application. */
3201 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3202 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3203 ret
= -ENOTCONN
; /* Caused by app exiting. */
3205 } else if (ret
< 0) {
3209 health_code_update();
3211 /* Send all streams to application. */
3212 pthread_mutex_lock(®_chan
->stream_list_lock
);
3213 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
3214 struct ust_app_stream stream
;
3216 ret
= duplicate_stream_object(reg_stream
, &stream
);
3218 goto error_stream_unlock
;
3221 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3223 (void) release_ust_app_stream(-1, &stream
, app
);
3224 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3225 ret
= -ENOTCONN
; /* Caused by app exiting. */
3227 goto error_stream_unlock
;
3231 * The return value is not important here. This function will output an
3234 (void) release_ust_app_stream(-1, &stream
, app
);
3236 ua_chan
->is_sent
= 1;
3238 error_stream_unlock
:
3239 pthread_mutex_unlock(®_chan
->stream_list_lock
);
3245 * Create and send to the application the created buffers with per UID buffers.
3247 * This MUST be called with a RCU read side lock acquired.
3248 * The session list lock and the session's lock must be acquired.
3250 * Return 0 on success else a negative value.
3252 static int create_channel_per_uid(struct ust_app
*app
,
3253 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3254 struct ust_app_channel
*ua_chan
)
3257 struct buffer_reg_uid
*reg_uid
;
3258 struct buffer_reg_channel
*reg_chan
;
3259 struct ltt_session
*session
= NULL
;
3260 enum lttng_error_code notification_ret
;
3261 struct ust_registry_channel
*chan_reg
;
3268 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3270 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3272 * The session creation handles the creation of this global registry
3273 * object. If none can be find, there is a code flow problem or a
3278 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3284 /* Create the buffer registry channel object. */
3285 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
3287 ERR("Error creating the UST channel \"%s\" registry instance",
3292 session
= session_find_by_id(ua_sess
->tracing_id
);
3294 assert(pthread_mutex_trylock(&session
->lock
));
3295 assert(session_trylock_list());
3298 * Create the buffers on the consumer side. This call populates the
3299 * ust app channel object with all streams and data object.
3301 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3302 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
3303 session
->most_recent_chunk_id
.value
);
3305 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3309 * Let's remove the previously created buffer registry channel so
3310 * it's not visible anymore in the session registry.
3312 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3313 ua_chan
->tracing_channel_id
, false);
3314 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
3315 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3320 * Setup the streams and add it to the session registry.
3322 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3323 ua_chan
, reg_chan
, app
);
3325 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3329 /* Notify the notification subsystem of the channel's creation. */
3330 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
3331 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3332 ua_chan
->tracing_channel_id
);
3334 chan_reg
->consumer_key
= ua_chan
->key
;
3336 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
3338 notification_ret
= notification_thread_command_add_channel(
3339 notification_thread_handle
, session
->name
,
3340 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3341 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3343 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3344 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3345 if (notification_ret
!= LTTNG_OK
) {
3346 ret
= - (int) notification_ret
;
3347 ERR("Failed to add channel to notification thread");
3352 /* Send buffers to the application. */
3353 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
3355 if (ret
!= -ENOTCONN
) {
3356 ERR("Error sending channel to application");
3363 session_put(session
);
3369 * Create and send to the application the created buffers with per PID buffers.
3371 * Called with UST app session lock held.
3372 * The session list lock and the session's lock must be acquired.
3374 * Return 0 on success else a negative value.
3376 static int create_channel_per_pid(struct ust_app
*app
,
3377 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3378 struct ust_app_channel
*ua_chan
)
3381 struct ust_registry_session
*registry
;
3382 enum lttng_error_code cmd_ret
;
3383 struct ltt_session
*session
= NULL
;
3384 uint64_t chan_reg_key
;
3385 struct ust_registry_channel
*chan_reg
;
3392 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3396 registry
= get_session_registry(ua_sess
);
3397 /* The UST app session lock is held, registry shall not be null. */
3400 /* Create and add a new channel registry to session. */
3401 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3403 ERR("Error creating the UST channel \"%s\" registry instance",
3408 session
= session_find_by_id(ua_sess
->tracing_id
);
3411 assert(pthread_mutex_trylock(&session
->lock
));
3412 assert(session_trylock_list());
3414 /* Create and get channel on the consumer side. */
3415 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3416 app
->bits_per_long
, registry
,
3417 session
->most_recent_chunk_id
.value
);
3419 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3421 goto error_remove_from_registry
;
3424 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3426 if (ret
!= -ENOTCONN
) {
3427 ERR("Error sending channel to application");
3429 goto error_remove_from_registry
;
3432 chan_reg_key
= ua_chan
->key
;
3433 pthread_mutex_lock(®istry
->lock
);
3434 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3436 chan_reg
->consumer_key
= ua_chan
->key
;
3437 pthread_mutex_unlock(®istry
->lock
);
3439 cmd_ret
= notification_thread_command_add_channel(
3440 notification_thread_handle
, session
->name
,
3441 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3442 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3444 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3445 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3446 if (cmd_ret
!= LTTNG_OK
) {
3447 ret
= - (int) cmd_ret
;
3448 ERR("Failed to add channel to notification thread");
3449 goto error_remove_from_registry
;
3452 error_remove_from_registry
:
3454 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3459 session_put(session
);
3465 * From an already allocated ust app channel, create the channel buffers if
3466 * needed and send them to the application. This MUST be called with a RCU read
3467 * side lock acquired.
3469 * Called with UST app session lock held.
3471 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3472 * the application exited concurrently.
3474 static int ust_app_channel_send(struct ust_app
*app
,
3475 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3476 struct ust_app_channel
*ua_chan
)
3482 assert(usess
->active
);
3486 /* Handle buffer type before sending the channel to the application. */
3487 switch (usess
->buffer_type
) {
3488 case LTTNG_BUFFER_PER_UID
:
3490 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3496 case LTTNG_BUFFER_PER_PID
:
3498 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3510 /* Initialize ust objd object using the received handle and add it. */
3511 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3512 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3514 /* If channel is not enabled, disable it on the tracer */
3515 if (!ua_chan
->enabled
) {
3516 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3527 * Create UST app channel and return it through ua_chanp if not NULL.
3529 * Called with UST app session lock and RCU read-side lock held.
3531 * Return 0 on success or else a negative value.
3533 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3534 struct ltt_ust_channel
*uchan
,
3535 enum lttng_ust_abi_chan_type type
, struct ltt_ust_session
*usess
,
3536 struct ust_app_channel
**ua_chanp
)
3539 struct lttng_ht_iter iter
;
3540 struct lttng_ht_node_str
*ua_chan_node
;
3541 struct ust_app_channel
*ua_chan
;
3543 /* Lookup channel in the ust app session */
3544 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3545 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3546 if (ua_chan_node
!= NULL
) {
3547 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3551 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3552 if (ua_chan
== NULL
) {
3553 /* Only malloc can fail here */
3557 shadow_copy_channel(ua_chan
, uchan
);
3559 /* Set channel type. */
3560 ua_chan
->attr
.type
= type
;
3562 /* Only add the channel if successful on the tracer side. */
3563 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3566 *ua_chanp
= ua_chan
;
3569 /* Everything went well. */
3577 * Create UST app event and create it on the tracer side.
3579 * Must be called with the RCU read side lock held.
3580 * Called with ust app session mutex held.
3583 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3584 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3585 struct ust_app
*app
)
3588 struct ust_app_event
*ua_event
;
3590 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3591 if (ua_event
== NULL
) {
3592 /* Only failure mode of alloc_ust_app_event(). */
3596 shadow_copy_event(ua_event
, uevent
);
3598 /* Create it on the tracer side */
3599 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3602 * Not found previously means that it does not exist on the
3603 * tracer. If the application reports that the event existed,
3604 * it means there is a bug in the sessiond or lttng-ust
3605 * (or corruption, etc.)
3607 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3608 ERR("Tracer for application reported that an event being created already existed: "
3609 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3611 app
->pid
, app
->ppid
, app
->uid
,
3617 add_unique_ust_app_event(ua_chan
, ua_event
);
3619 DBG2("UST app create event completed: app = '%s' (ppid: %d)",
3620 app
->name
, app
->ppid
);
3626 /* Valid. Calling here is already in a read side lock */
3627 delete_ust_app_event(-1, ua_event
, app
);
3632 * Create UST app event notifier rule and create it on the tracer side.
3634 * Must be called with the RCU read side lock held.
3635 * Called with ust app session mutex held.
3638 int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
,
3639 struct ust_app
*app
)
3642 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3644 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3645 if (ua_event_notifier_rule
== NULL
) {
3650 /* Create it on the tracer side. */
3651 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3654 * Not found previously means that it does not exist on the
3655 * tracer. If the application reports that the event existed,
3656 * it means there is a bug in the sessiond or lttng-ust
3657 * (or corruption, etc.)
3659 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3660 ERR("Tracer for application reported that an event notifier being created already exists: "
3661 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3662 lttng_trigger_get_tracer_token(trigger
),
3663 app
->pid
, app
->ppid
, app
->uid
,
3669 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3670 &ua_event_notifier_rule
->node
);
3672 DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64
,
3673 app
->name
, app
->ppid
, lttng_trigger_get_tracer_token(trigger
));
3679 /* The RCU read side lock is already being held by the caller. */
3680 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3685 * Create UST metadata and open it on the tracer side.
3687 * Called with UST app session lock held and RCU read side lock.
3689 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3690 struct ust_app
*app
, struct consumer_output
*consumer
)
3693 struct ust_app_channel
*metadata
;
3694 struct consumer_socket
*socket
;
3695 struct ust_registry_session
*registry
;
3696 struct ltt_session
*session
= NULL
;
3702 registry
= get_session_registry(ua_sess
);
3703 /* The UST app session is held registry shall not be null. */
3706 pthread_mutex_lock(®istry
->lock
);
3708 /* Metadata already exists for this registry or it was closed previously */
3709 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3714 /* Allocate UST metadata */
3715 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3717 /* malloc() failed */
3722 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3724 /* Need one fd for the channel. */
3725 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3727 ERR("Exhausted number of available FD upon create metadata");
3731 /* Get the right consumer socket for the application. */
3732 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3735 goto error_consumer
;
3739 * Keep metadata key so we can identify it on the consumer side. Assign it
3740 * to the registry *before* we ask the consumer so we avoid the race of the
3741 * consumer requesting the metadata and the ask_channel call on our side
3742 * did not returned yet.
3744 registry
->metadata_key
= metadata
->key
;
3746 session
= session_find_by_id(ua_sess
->tracing_id
);
3749 assert(pthread_mutex_trylock(&session
->lock
));
3750 assert(session_trylock_list());
3753 * Ask the metadata channel creation to the consumer. The metadata object
3754 * will be created by the consumer and kept their. However, the stream is
3755 * never added or monitored until we do a first push metadata to the
3758 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3759 registry
, session
->current_trace_chunk
);
3761 /* Nullify the metadata key so we don't try to close it later on. */
3762 registry
->metadata_key
= 0;
3763 goto error_consumer
;
3767 * The setup command will make the metadata stream be sent to the relayd,
3768 * if applicable, and the thread managing the metadatas. This is important
3769 * because after this point, if an error occurs, the only way the stream
3770 * can be deleted is to be monitored in the consumer.
3772 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3774 /* Nullify the metadata key so we don't try to close it later on. */
3775 registry
->metadata_key
= 0;
3776 goto error_consumer
;
3779 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3780 metadata
->key
, app
->pid
);
3783 lttng_fd_put(LTTNG_FD_APPS
, 1);
3784 delete_ust_app_channel(-1, metadata
, app
);
3786 pthread_mutex_unlock(®istry
->lock
);
3788 session_put(session
);
3794 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3795 * acquired before calling this function.
3797 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3799 struct ust_app
*app
= NULL
;
3800 struct lttng_ht_node_ulong
*node
;
3801 struct lttng_ht_iter iter
;
3803 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3804 node
= lttng_ht_iter_get_node_ulong(&iter
);
3806 DBG2("UST app no found with pid %d", pid
);
3810 DBG2("Found UST app by pid %d", pid
);
3812 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3819 * Allocate and init an UST app object using the registration information and
3820 * the command socket. This is called when the command socket connects to the
3823 * The object is returned on success or else NULL.
3825 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3828 struct ust_app
*lta
= NULL
;
3829 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3834 DBG3("UST app creating application for socket %d", sock
);
3836 if ((msg
->bits_per_long
== 64 &&
3837 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3838 || (msg
->bits_per_long
== 32 &&
3839 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3840 ERR("Registration failed: application \"%s\" (pid: %d) has "
3841 "%d-bit long, but no consumerd for this size is available.\n",
3842 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3847 * Reserve the two file descriptors of the event source pipe. The write
3848 * end will be closed once it is passed to the application, at which
3849 * point a single 'put' will be performed.
3851 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3853 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s' (ppid: %d)",
3854 msg
->name
, (int) msg
->ppid
);
3858 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3859 if (!event_notifier_event_source_pipe
) {
3860 PERROR("Failed to open application event source pipe: '%s' (ppid = %d)",
3861 msg
->name
, msg
->ppid
);
3865 lta
= zmalloc(sizeof(struct ust_app
));
3868 goto error_free_pipe
;
3871 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
3873 lta
->ppid
= msg
->ppid
;
3874 lta
->uid
= msg
->uid
;
3875 lta
->gid
= msg
->gid
;
3877 lta
->bits_per_long
= msg
->bits_per_long
;
3878 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3879 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3880 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3881 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3882 lta
->long_alignment
= msg
->long_alignment
;
3883 lta
->byte_order
= msg
->byte_order
;
3885 lta
->v_major
= msg
->major
;
3886 lta
->v_minor
= msg
->minor
;
3887 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3888 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3889 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3890 lta
->notify_sock
= -1;
3891 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3893 /* Copy name and make sure it's NULL terminated. */
3894 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3895 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3898 * Before this can be called, when receiving the registration information,
3899 * the application compatibility is checked. So, at this point, the
3900 * application can work with this session daemon.
3902 lta
->compatible
= 1;
3904 lta
->pid
= msg
->pid
;
3905 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3907 pthread_mutex_init(<a
->sock_lock
, NULL
);
3908 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3910 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3914 lttng_pipe_destroy(event_notifier_event_source_pipe
);
3915 lttng_fd_put(LTTNG_FD_APPS
, 2);
3921 * For a given application object, add it to every hash table.
3923 void ust_app_add(struct ust_app
*app
)
3926 assert(app
->notify_sock
>= 0);
3928 app
->registration_time
= time(NULL
);
3933 * On a re-registration, we want to kick out the previous registration of
3936 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3939 * The socket _should_ be unique until _we_ call close. So, a add_unique
3940 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3941 * already in the table.
3943 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3945 /* Add application to the notify socket hash table. */
3946 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3947 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3949 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3950 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3951 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3958 * Set the application version into the object.
3960 * Return 0 on success else a negative value either an errno code or a
3961 * LTTng-UST error code.
3963 int ust_app_version(struct ust_app
*app
)
3969 pthread_mutex_lock(&app
->sock_lock
);
3970 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3971 pthread_mutex_unlock(&app
->sock_lock
);
3973 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3974 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3976 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3984 * Setup the base event notifier group.
3986 * Return 0 on success else a negative value either an errno code or a
3987 * LTTng-UST error code.
3989 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
3992 int event_pipe_write_fd
;
3993 struct lttng_ust_abi_object_data
*event_notifier_group
= NULL
;
3994 enum lttng_error_code lttng_ret
;
3998 /* Get the write side of the pipe. */
3999 event_pipe_write_fd
= lttng_pipe_get_writefd(
4000 app
->event_notifier_group
.event_pipe
);
4002 pthread_mutex_lock(&app
->sock_lock
);
4003 ret
= ustctl_create_event_notifier_group(app
->sock
,
4004 event_pipe_write_fd
, &event_notifier_group
);
4005 pthread_mutex_unlock(&app
->sock_lock
);
4007 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4008 ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d",
4009 ret
, app
->sock
, event_pipe_write_fd
);
4011 DBG("Failed to create application event notifier group (application is dead): app socket fd = %d",
4018 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4020 ERR("Failed to close write end of the application's event source pipe: app = '%s' (ppid = %d)",
4021 app
->name
, app
->ppid
);
4026 * Release the file descriptor that was reserved for the write-end of
4029 lttng_fd_put(LTTNG_FD_APPS
, 1);
4031 lttng_ret
= notification_thread_command_add_tracer_event_source(
4032 notification_thread_handle
,
4033 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
),
4035 if (lttng_ret
!= LTTNG_OK
) {
4036 ERR("Failed to add tracer event source to notification thread");
4041 /* Assign handle only when the complete setup is valid. */
4042 app
->event_notifier_group
.object
= event_notifier_group
;
4046 ustctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4047 free(app
->event_notifier_group
.object
);
4052 * Unregister app by removing it from the global traceable app list and freeing
4055 * The socket is already closed at this point so no close to sock.
4057 void ust_app_unregister(int sock
)
4059 struct ust_app
*lta
;
4060 struct lttng_ht_node_ulong
*node
;
4061 struct lttng_ht_iter ust_app_sock_iter
;
4062 struct lttng_ht_iter iter
;
4063 struct ust_app_session
*ua_sess
;
4068 /* Get the node reference for a call_rcu */
4069 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
4070 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4073 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
4074 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4077 * For per-PID buffers, perform "push metadata" and flush all
4078 * application streams before removing app from hash tables,
4079 * ensuring proper behavior of data_pending check.
4080 * Remove sessions so they are not visible during deletion.
4082 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
4084 struct ust_registry_session
*registry
;
4086 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4088 /* The session was already removed so scheduled for teardown. */
4092 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4093 (void) ust_app_flush_app_session(lta
, ua_sess
);
4097 * Add session to list for teardown. This is safe since at this point we
4098 * are the only one using this list.
4100 pthread_mutex_lock(&ua_sess
->lock
);
4102 if (ua_sess
->deleted
) {
4103 pthread_mutex_unlock(&ua_sess
->lock
);
4108 * Normally, this is done in the delete session process which is
4109 * executed in the call rcu below. However, upon registration we can't
4110 * afford to wait for the grace period before pushing data or else the
4111 * data pending feature can race between the unregistration and stop
4112 * command where the data pending command is sent *before* the grace
4115 * The close metadata below nullifies the metadata pointer in the
4116 * session so the delete session will NOT push/close a second time.
4118 registry
= get_session_registry(ua_sess
);
4120 /* Push metadata for application before freeing the application. */
4121 (void) push_metadata(registry
, ua_sess
->consumer
);
4124 * Don't ask to close metadata for global per UID buffers. Close
4125 * metadata only on destroy trace session in this case. Also, the
4126 * previous push metadata could have flag the metadata registry to
4127 * close so don't send a close command if closed.
4129 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4130 /* And ask to close it for this session registry. */
4131 (void) close_metadata(registry
, ua_sess
->consumer
);
4134 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4136 pthread_mutex_unlock(&ua_sess
->lock
);
4139 /* Remove application from PID hash table */
4140 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4144 * Remove application from notify hash table. The thread handling the
4145 * notify socket could have deleted the node so ignore on error because
4146 * either way it's valid. The close of that socket is handled by the
4147 * apps_notify_thread.
4149 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4150 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4153 * Ignore return value since the node might have been removed before by an
4154 * add replace during app registration because the PID can be reassigned by
4157 iter
.iter
.node
= <a
->pid_n
.node
;
4158 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4160 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4165 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4172 * Fill events array with all events name of all registered apps.
4174 int ust_app_list_events(struct lttng_event
**events
)
4177 size_t nbmem
, count
= 0;
4178 struct lttng_ht_iter iter
;
4179 struct ust_app
*app
;
4180 struct lttng_event
*tmp_event
;
4182 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4183 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
4184 if (tmp_event
== NULL
) {
4185 PERROR("zmalloc ust app events");
4192 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4193 struct lttng_ust_abi_tracepoint_iter uiter
;
4195 health_code_update();
4197 if (!app
->compatible
) {
4199 * TODO: In time, we should notice the caller of this error by
4200 * telling him that this is a version error.
4204 pthread_mutex_lock(&app
->sock_lock
);
4205 handle
= ustctl_tracepoint_list(app
->sock
);
4207 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4208 ERR("UST app list events getting handle failed for app pid %d",
4211 pthread_mutex_unlock(&app
->sock_lock
);
4215 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
4216 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4217 /* Handle ustctl error. */
4221 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4222 ERR("UST app tp list get failed for app %d with ret %d",
4225 DBG3("UST app tp list get failed. Application is dead");
4227 * This is normal behavior, an application can die during the
4228 * creation process. Don't report an error so the execution can
4229 * continue normally. Continue normal execution.
4234 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4235 if (release_ret
< 0 &&
4236 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4237 release_ret
!= -EPIPE
) {
4238 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4240 pthread_mutex_unlock(&app
->sock_lock
);
4244 health_code_update();
4245 if (count
>= nbmem
) {
4246 /* In case the realloc fails, we free the memory */
4247 struct lttng_event
*new_tmp_event
;
4250 new_nbmem
= nbmem
<< 1;
4251 DBG2("Reallocating event list from %zu to %zu entries",
4253 new_tmp_event
= realloc(tmp_event
,
4254 new_nbmem
* sizeof(struct lttng_event
));
4255 if (new_tmp_event
== NULL
) {
4258 PERROR("realloc ust app events");
4261 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4262 if (release_ret
< 0 &&
4263 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4264 release_ret
!= -EPIPE
) {
4265 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4267 pthread_mutex_unlock(&app
->sock_lock
);
4270 /* Zero the new memory */
4271 memset(new_tmp_event
+ nbmem
, 0,
4272 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4274 tmp_event
= new_tmp_event
;
4276 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4277 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4278 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4279 tmp_event
[count
].pid
= app
->pid
;
4280 tmp_event
[count
].enabled
= -1;
4283 ret
= ustctl_release_handle(app
->sock
, handle
);
4284 pthread_mutex_unlock(&app
->sock_lock
);
4285 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4286 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4291 *events
= tmp_event
;
4293 DBG2("UST app list events done (%zu events)", count
);
4298 health_code_update();
4303 * Fill events array with all events name of all registered apps.
4305 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4308 size_t nbmem
, count
= 0;
4309 struct lttng_ht_iter iter
;
4310 struct ust_app
*app
;
4311 struct lttng_event_field
*tmp_event
;
4313 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4314 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
4315 if (tmp_event
== NULL
) {
4316 PERROR("zmalloc ust app event fields");
4323 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4324 struct lttng_ust_abi_field_iter uiter
;
4326 health_code_update();
4328 if (!app
->compatible
) {
4330 * TODO: In time, we should notice the caller of this error by
4331 * telling him that this is a version error.
4335 pthread_mutex_lock(&app
->sock_lock
);
4336 handle
= ustctl_tracepoint_field_list(app
->sock
);
4338 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4339 ERR("UST app list field getting handle failed for app pid %d",
4342 pthread_mutex_unlock(&app
->sock_lock
);
4346 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
4347 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4348 /* Handle ustctl error. */
4352 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4353 ERR("UST app tp list field failed for app %d with ret %d",
4356 DBG3("UST app tp list field failed. Application is dead");
4358 * This is normal behavior, an application can die during the
4359 * creation process. Don't report an error so the execution can
4360 * continue normally. Reset list and count for next app.
4365 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4366 pthread_mutex_unlock(&app
->sock_lock
);
4367 if (release_ret
< 0 &&
4368 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4369 release_ret
!= -EPIPE
) {
4370 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4375 health_code_update();
4376 if (count
>= nbmem
) {
4377 /* In case the realloc fails, we free the memory */
4378 struct lttng_event_field
*new_tmp_event
;
4381 new_nbmem
= nbmem
<< 1;
4382 DBG2("Reallocating event field list from %zu to %zu entries",
4384 new_tmp_event
= realloc(tmp_event
,
4385 new_nbmem
* sizeof(struct lttng_event_field
));
4386 if (new_tmp_event
== NULL
) {
4389 PERROR("realloc ust app event fields");
4392 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4393 pthread_mutex_unlock(&app
->sock_lock
);
4395 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4396 release_ret
!= -EPIPE
) {
4397 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4401 /* Zero the new memory */
4402 memset(new_tmp_event
+ nbmem
, 0,
4403 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4405 tmp_event
= new_tmp_event
;
4408 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4409 /* Mapping between these enums matches 1 to 1. */
4410 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4411 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4413 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4414 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4415 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4416 tmp_event
[count
].event
.pid
= app
->pid
;
4417 tmp_event
[count
].event
.enabled
= -1;
4420 ret
= ustctl_release_handle(app
->sock
, handle
);
4421 pthread_mutex_unlock(&app
->sock_lock
);
4423 ret
!= -LTTNG_UST_ERR_EXITING
&&
4425 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4430 *fields
= tmp_event
;
4432 DBG2("UST app list event fields done (%zu events)", count
);
4437 health_code_update();
4442 * Free and clean all traceable apps of the global list.
4444 * Should _NOT_ be called with RCU read-side lock held.
4446 void ust_app_clean_list(void)
4449 struct ust_app
*app
;
4450 struct lttng_ht_iter iter
;
4452 DBG2("UST app cleaning registered apps hash table");
4456 /* Cleanup notify socket hash table */
4457 if (ust_app_ht_by_notify_sock
) {
4458 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4459 notify_sock_n
.node
) {
4461 * Assert that all notifiers are gone as all triggers
4462 * are unregistered prior to this clean-up.
4464 assert(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4466 ust_app_notify_sock_unregister(app
->notify_sock
);
4471 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4472 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4474 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4478 /* Cleanup socket hash table */
4479 if (ust_app_ht_by_sock
) {
4480 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4482 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4489 /* Destroy is done only when the ht is empty */
4491 ht_cleanup_push(ust_app_ht
);
4493 if (ust_app_ht_by_sock
) {
4494 ht_cleanup_push(ust_app_ht_by_sock
);
4496 if (ust_app_ht_by_notify_sock
) {
4497 ht_cleanup_push(ust_app_ht_by_notify_sock
);
4502 * Init UST app hash table.
4504 int ust_app_ht_alloc(void)
4506 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4510 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4511 if (!ust_app_ht_by_sock
) {
4514 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4515 if (!ust_app_ht_by_notify_sock
) {
4522 * For a specific UST session, disable the channel for all registered apps.
4524 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4525 struct ltt_ust_channel
*uchan
)
4528 struct lttng_ht_iter iter
;
4529 struct lttng_ht_node_str
*ua_chan_node
;
4530 struct ust_app
*app
;
4531 struct ust_app_session
*ua_sess
;
4532 struct ust_app_channel
*ua_chan
;
4534 assert(usess
->active
);
4535 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4536 uchan
->name
, usess
->id
);
4540 /* For every registered applications */
4541 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4542 struct lttng_ht_iter uiter
;
4543 if (!app
->compatible
) {
4545 * TODO: In time, we should notice the caller of this error by
4546 * telling him that this is a version error.
4550 ua_sess
= lookup_session_by_app(usess
, app
);
4551 if (ua_sess
== NULL
) {
4556 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4557 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4558 /* If the session if found for the app, the channel must be there */
4559 assert(ua_chan_node
);
4561 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4562 /* The channel must not be already disabled */
4563 assert(ua_chan
->enabled
== 1);
4565 /* Disable channel onto application */
4566 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4568 /* XXX: We might want to report this error at some point... */
4578 * For a specific UST session, enable the channel for all registered apps.
4580 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4581 struct ltt_ust_channel
*uchan
)
4584 struct lttng_ht_iter iter
;
4585 struct ust_app
*app
;
4586 struct ust_app_session
*ua_sess
;
4588 assert(usess
->active
);
4589 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4590 uchan
->name
, usess
->id
);
4594 /* For every registered applications */
4595 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4596 if (!app
->compatible
) {
4598 * TODO: In time, we should notice the caller of this error by
4599 * telling him that this is a version error.
4603 ua_sess
= lookup_session_by_app(usess
, app
);
4604 if (ua_sess
== NULL
) {
4608 /* Enable channel onto application */
4609 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4611 /* XXX: We might want to report this error at some point... */
4621 * Disable an event in a channel and for a specific session.
4623 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4624 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4627 struct lttng_ht_iter iter
, uiter
;
4628 struct lttng_ht_node_str
*ua_chan_node
;
4629 struct ust_app
*app
;
4630 struct ust_app_session
*ua_sess
;
4631 struct ust_app_channel
*ua_chan
;
4632 struct ust_app_event
*ua_event
;
4634 assert(usess
->active
);
4635 DBG("UST app disabling event %s for all apps in channel "
4636 "%s for session id %" PRIu64
,
4637 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4641 /* For all registered applications */
4642 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4643 if (!app
->compatible
) {
4645 * TODO: In time, we should notice the caller of this error by
4646 * telling him that this is a version error.
4650 ua_sess
= lookup_session_by_app(usess
, app
);
4651 if (ua_sess
== NULL
) {
4656 /* Lookup channel in the ust app session */
4657 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4658 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4659 if (ua_chan_node
== NULL
) {
4660 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4661 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4664 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4666 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4667 uevent
->filter
, uevent
->attr
.loglevel
,
4669 if (ua_event
== NULL
) {
4670 DBG2("Event %s not found in channel %s for app pid %d."
4671 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4675 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4677 /* XXX: Report error someday... */
4686 /* The ua_sess lock must be held by the caller. */
4688 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4689 struct ust_app_session
*ua_sess
,
4690 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4691 struct ust_app_channel
**_ua_chan
)
4694 struct ust_app_channel
*ua_chan
= NULL
;
4697 ASSERT_LOCKED(ua_sess
->lock
);
4699 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4700 sizeof(uchan
->name
))) {
4701 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4705 struct ltt_ust_context
*uctx
= NULL
;
4708 * Create channel onto application and synchronize its
4711 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4712 LTTNG_UST_ABI_CHAN_PER_CPU
, usess
,
4718 ret
= ust_app_channel_send(app
, usess
,
4725 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4726 ret
= create_ust_app_channel_context(ua_chan
,
4739 * The application's socket is not valid. Either a bad socket
4740 * or a timeout on it. We can't inform the caller that for a
4741 * specific app, the session failed so lets continue here.
4743 ret
= 0; /* Not an error. */
4751 if (ret
== 0 && _ua_chan
) {
4753 * Only return the application's channel on success. Note
4754 * that the channel can still be part of the application's
4755 * channel hashtable on error.
4757 *_ua_chan
= ua_chan
;
4763 * Enable event for a specific session and channel on the tracer.
4765 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4766 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4769 struct lttng_ht_iter iter
, uiter
;
4770 struct lttng_ht_node_str
*ua_chan_node
;
4771 struct ust_app
*app
;
4772 struct ust_app_session
*ua_sess
;
4773 struct ust_app_channel
*ua_chan
;
4774 struct ust_app_event
*ua_event
;
4776 assert(usess
->active
);
4777 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4778 uevent
->attr
.name
, usess
->id
);
4781 * NOTE: At this point, this function is called only if the session and
4782 * channel passed are already created for all apps. and enabled on the
4788 /* For all registered applications */
4789 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4790 if (!app
->compatible
) {
4792 * TODO: In time, we should notice the caller of this error by
4793 * telling him that this is a version error.
4797 ua_sess
= lookup_session_by_app(usess
, app
);
4799 /* The application has problem or is probably dead. */
4803 pthread_mutex_lock(&ua_sess
->lock
);
4805 if (ua_sess
->deleted
) {
4806 pthread_mutex_unlock(&ua_sess
->lock
);
4810 /* Lookup channel in the ust app session */
4811 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4812 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4814 * It is possible that the channel cannot be found is
4815 * the channel/event creation occurs concurrently with
4816 * an application exit.
4818 if (!ua_chan_node
) {
4819 pthread_mutex_unlock(&ua_sess
->lock
);
4823 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4825 /* Get event node */
4826 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4827 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4828 if (ua_event
== NULL
) {
4829 DBG3("UST app enable event %s not found for app PID %d."
4830 "Skipping app", uevent
->attr
.name
, app
->pid
);
4834 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4836 pthread_mutex_unlock(&ua_sess
->lock
);
4840 pthread_mutex_unlock(&ua_sess
->lock
);
4849 * For a specific existing UST session and UST channel, creates the event for
4850 * all registered apps.
4852 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4853 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4856 struct lttng_ht_iter iter
, uiter
;
4857 struct lttng_ht_node_str
*ua_chan_node
;
4858 struct ust_app
*app
;
4859 struct ust_app_session
*ua_sess
;
4860 struct ust_app_channel
*ua_chan
;
4862 assert(usess
->active
);
4863 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4864 uevent
->attr
.name
, usess
->id
);
4868 /* For all registered applications */
4869 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4870 if (!app
->compatible
) {
4872 * TODO: In time, we should notice the caller of this error by
4873 * telling him that this is a version error.
4877 ua_sess
= lookup_session_by_app(usess
, app
);
4879 /* The application has problem or is probably dead. */
4883 pthread_mutex_lock(&ua_sess
->lock
);
4885 if (ua_sess
->deleted
) {
4886 pthread_mutex_unlock(&ua_sess
->lock
);
4890 /* Lookup channel in the ust app session */
4891 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4892 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4893 /* If the channel is not found, there is a code flow error */
4894 assert(ua_chan_node
);
4896 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4898 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4899 pthread_mutex_unlock(&ua_sess
->lock
);
4901 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4902 /* Possible value at this point: -ENOMEM. If so, we stop! */
4905 DBG2("UST app event %s already exist on app PID %d",
4906 uevent
->attr
.name
, app
->pid
);
4916 * Start tracing for a specific UST session and app.
4918 * Called with UST app session lock held.
4922 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4925 struct ust_app_session
*ua_sess
;
4927 DBG("Starting tracing for ust app pid %d", app
->pid
);
4931 if (!app
->compatible
) {
4935 ua_sess
= lookup_session_by_app(usess
, app
);
4936 if (ua_sess
== NULL
) {
4937 /* The session is in teardown process. Ignore and continue. */
4941 pthread_mutex_lock(&ua_sess
->lock
);
4943 if (ua_sess
->deleted
) {
4944 pthread_mutex_unlock(&ua_sess
->lock
);
4948 if (ua_sess
->enabled
) {
4949 pthread_mutex_unlock(&ua_sess
->lock
);
4953 /* Upon restart, we skip the setup, already done */
4954 if (ua_sess
->started
) {
4958 health_code_update();
4961 /* This starts the UST tracing */
4962 pthread_mutex_lock(&app
->sock_lock
);
4963 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4964 pthread_mutex_unlock(&app
->sock_lock
);
4966 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4967 ERR("Error starting tracing for app pid: %d (ret: %d)",
4970 DBG("UST app start session failed. Application is dead.");
4972 * This is normal behavior, an application can die during the
4973 * creation process. Don't report an error so the execution can
4974 * continue normally.
4976 pthread_mutex_unlock(&ua_sess
->lock
);
4982 /* Indicate that the session has been started once */
4983 ua_sess
->started
= 1;
4984 ua_sess
->enabled
= 1;
4986 pthread_mutex_unlock(&ua_sess
->lock
);
4988 health_code_update();
4990 /* Quiescent wait after starting trace */
4991 pthread_mutex_lock(&app
->sock_lock
);
4992 ret
= ustctl_wait_quiescent(app
->sock
);
4993 pthread_mutex_unlock(&app
->sock_lock
);
4994 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4995 ERR("UST app wait quiescent failed for app pid %d ret %d",
5001 health_code_update();
5005 pthread_mutex_unlock(&ua_sess
->lock
);
5007 health_code_update();
5012 * Stop tracing for a specific UST session and app.
5015 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5018 struct ust_app_session
*ua_sess
;
5019 struct ust_registry_session
*registry
;
5021 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5025 if (!app
->compatible
) {
5026 goto end_no_session
;
5029 ua_sess
= lookup_session_by_app(usess
, app
);
5030 if (ua_sess
== NULL
) {
5031 goto end_no_session
;
5034 pthread_mutex_lock(&ua_sess
->lock
);
5036 if (ua_sess
->deleted
) {
5037 pthread_mutex_unlock(&ua_sess
->lock
);
5038 goto end_no_session
;
5042 * If started = 0, it means that stop trace has been called for a session
5043 * that was never started. It's possible since we can have a fail start
5044 * from either the application manager thread or the command thread. Simply
5045 * indicate that this is a stop error.
5047 if (!ua_sess
->started
) {
5048 goto error_rcu_unlock
;
5051 health_code_update();
5053 /* This inhibits UST tracing */
5054 pthread_mutex_lock(&app
->sock_lock
);
5055 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
5056 pthread_mutex_unlock(&app
->sock_lock
);
5058 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5059 ERR("Error stopping tracing for app pid: %d (ret: %d)",
5062 DBG("UST app stop session failed. Application is dead.");
5064 * This is normal behavior, an application can die during the
5065 * creation process. Don't report an error so the execution can
5066 * continue normally.
5070 goto error_rcu_unlock
;
5073 health_code_update();
5074 ua_sess
->enabled
= 0;
5076 /* Quiescent wait after stopping trace */
5077 pthread_mutex_lock(&app
->sock_lock
);
5078 ret
= ustctl_wait_quiescent(app
->sock
);
5079 pthread_mutex_unlock(&app
->sock_lock
);
5080 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5081 ERR("UST app wait quiescent failed for app pid %d ret %d",
5085 health_code_update();
5087 registry
= get_session_registry(ua_sess
);
5089 /* The UST app session is held registry shall not be null. */
5092 /* Push metadata for application before freeing the application. */
5093 (void) push_metadata(registry
, ua_sess
->consumer
);
5096 pthread_mutex_unlock(&ua_sess
->lock
);
5099 health_code_update();
5103 pthread_mutex_unlock(&ua_sess
->lock
);
5105 health_code_update();
5110 int ust_app_flush_app_session(struct ust_app
*app
,
5111 struct ust_app_session
*ua_sess
)
5113 int ret
, retval
= 0;
5114 struct lttng_ht_iter iter
;
5115 struct ust_app_channel
*ua_chan
;
5116 struct consumer_socket
*socket
;
5118 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5122 if (!app
->compatible
) {
5123 goto end_not_compatible
;
5126 pthread_mutex_lock(&ua_sess
->lock
);
5128 if (ua_sess
->deleted
) {
5132 health_code_update();
5134 /* Flushing buffers */
5135 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5138 /* Flush buffers and push metadata. */
5139 switch (ua_sess
->buffer_type
) {
5140 case LTTNG_BUFFER_PER_PID
:
5141 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5143 health_code_update();
5144 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5146 ERR("Error flushing consumer channel");
5152 case LTTNG_BUFFER_PER_UID
:
5158 health_code_update();
5161 pthread_mutex_unlock(&ua_sess
->lock
);
5165 health_code_update();
5170 * Flush buffers for all applications for a specific UST session.
5171 * Called with UST session lock held.
5174 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5179 DBG("Flushing session buffers for all ust apps");
5183 /* Flush buffers and push metadata. */
5184 switch (usess
->buffer_type
) {
5185 case LTTNG_BUFFER_PER_UID
:
5187 struct buffer_reg_uid
*reg
;
5188 struct lttng_ht_iter iter
;
5190 /* Flush all per UID buffers associated to that session. */
5191 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5192 struct ust_registry_session
*ust_session_reg
;
5193 struct buffer_reg_channel
*reg_chan
;
5194 struct consumer_socket
*socket
;
5196 /* Get consumer socket to use to push the metadata.*/
5197 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5200 /* Ignore request if no consumer is found for the session. */
5204 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5205 reg_chan
, node
.node
) {
5207 * The following call will print error values so the return
5208 * code is of little importance because whatever happens, we
5209 * have to try them all.
5211 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
5214 ust_session_reg
= reg
->registry
->reg
.ust
;
5215 /* Push metadata. */
5216 (void) push_metadata(ust_session_reg
, usess
->consumer
);
5220 case LTTNG_BUFFER_PER_PID
:
5222 struct ust_app_session
*ua_sess
;
5223 struct lttng_ht_iter iter
;
5224 struct ust_app
*app
;
5226 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5227 ua_sess
= lookup_session_by_app(usess
, app
);
5228 if (ua_sess
== NULL
) {
5231 (void) ust_app_flush_app_session(app
, ua_sess
);
5242 health_code_update();
5247 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5248 struct ust_app_session
*ua_sess
)
5251 struct lttng_ht_iter iter
;
5252 struct ust_app_channel
*ua_chan
;
5253 struct consumer_socket
*socket
;
5255 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5259 if (!app
->compatible
) {
5260 goto end_not_compatible
;
5263 pthread_mutex_lock(&ua_sess
->lock
);
5265 if (ua_sess
->deleted
) {
5269 health_code_update();
5271 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5274 ERR("Failed to find consumer (%" PRIu32
") socket",
5275 app
->bits_per_long
);
5280 /* Clear quiescent state. */
5281 switch (ua_sess
->buffer_type
) {
5282 case LTTNG_BUFFER_PER_PID
:
5283 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5284 ua_chan
, node
.node
) {
5285 health_code_update();
5286 ret
= consumer_clear_quiescent_channel(socket
,
5289 ERR("Error clearing quiescent state for consumer channel");
5295 case LTTNG_BUFFER_PER_UID
:
5302 health_code_update();
5305 pthread_mutex_unlock(&ua_sess
->lock
);
5309 health_code_update();
5314 * Clear quiescent state in each stream for all applications for a
5315 * specific UST session.
5316 * Called with UST session lock held.
5319 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5324 DBG("Clearing stream quiescent state for all ust apps");
5328 switch (usess
->buffer_type
) {
5329 case LTTNG_BUFFER_PER_UID
:
5331 struct lttng_ht_iter iter
;
5332 struct buffer_reg_uid
*reg
;
5335 * Clear quiescent for all per UID buffers associated to
5338 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5339 struct consumer_socket
*socket
;
5340 struct buffer_reg_channel
*reg_chan
;
5342 /* Get associated consumer socket.*/
5343 socket
= consumer_find_socket_by_bitness(
5344 reg
->bits_per_long
, usess
->consumer
);
5347 * Ignore request if no consumer is found for
5353 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5354 &iter
.iter
, reg_chan
, node
.node
) {
5356 * The following call will print error values so
5357 * the return code is of little importance
5358 * because whatever happens, we have to try them
5361 (void) consumer_clear_quiescent_channel(socket
,
5362 reg_chan
->consumer_key
);
5367 case LTTNG_BUFFER_PER_PID
:
5369 struct ust_app_session
*ua_sess
;
5370 struct lttng_ht_iter iter
;
5371 struct ust_app
*app
;
5373 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5375 ua_sess
= lookup_session_by_app(usess
, app
);
5376 if (ua_sess
== NULL
) {
5379 (void) ust_app_clear_quiescent_app_session(app
,
5391 health_code_update();
5396 * Destroy a specific UST session in apps.
5398 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5401 struct ust_app_session
*ua_sess
;
5402 struct lttng_ht_iter iter
;
5403 struct lttng_ht_node_u64
*node
;
5405 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5409 if (!app
->compatible
) {
5413 __lookup_session_by_app(usess
, app
, &iter
);
5414 node
= lttng_ht_iter_get_node_u64(&iter
);
5416 /* Session is being or is deleted. */
5419 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
5421 health_code_update();
5422 destroy_app_session(app
, ua_sess
);
5424 health_code_update();
5426 /* Quiescent wait after stopping trace */
5427 pthread_mutex_lock(&app
->sock_lock
);
5428 ret
= ustctl_wait_quiescent(app
->sock
);
5429 pthread_mutex_unlock(&app
->sock_lock
);
5430 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5431 ERR("UST app wait quiescent failed for app pid %d ret %d",
5436 health_code_update();
5441 * Start tracing for the UST session.
5443 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5445 struct lttng_ht_iter iter
;
5446 struct ust_app
*app
;
5448 DBG("Starting all UST traces");
5451 * Even though the start trace might fail, flag this session active so
5452 * other application coming in are started by default.
5459 * In a start-stop-start use-case, we need to clear the quiescent state
5460 * of each channel set by the prior stop command, thus ensuring that a
5461 * following stop or destroy is sure to grab a timestamp_end near those
5462 * operations, even if the packet is empty.
5464 (void) ust_app_clear_quiescent_session(usess
);
5466 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5467 ust_app_global_update(usess
, app
);
5476 * Start tracing for the UST session.
5477 * Called with UST session lock held.
5479 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5482 struct lttng_ht_iter iter
;
5483 struct ust_app
*app
;
5485 DBG("Stopping all UST traces");
5488 * Even though the stop trace might fail, flag this session inactive so
5489 * other application coming in are not started by default.
5495 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5496 ret
= ust_app_stop_trace(usess
, app
);
5498 /* Continue to next apps even on error */
5503 (void) ust_app_flush_session(usess
);
5511 * Destroy app UST session.
5513 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5516 struct lttng_ht_iter iter
;
5517 struct ust_app
*app
;
5519 DBG("Destroy all UST traces");
5523 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5524 ret
= destroy_trace(usess
, app
);
5526 /* Continue to next apps even on error */
5536 /* The ua_sess lock must be held by the caller. */
5538 int find_or_create_ust_app_channel(
5539 struct ltt_ust_session
*usess
,
5540 struct ust_app_session
*ua_sess
,
5541 struct ust_app
*app
,
5542 struct ltt_ust_channel
*uchan
,
5543 struct ust_app_channel
**ua_chan
)
5546 struct lttng_ht_iter iter
;
5547 struct lttng_ht_node_str
*ua_chan_node
;
5549 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5550 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5552 *ua_chan
= caa_container_of(ua_chan_node
,
5553 struct ust_app_channel
, node
);
5557 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5566 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5567 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5568 struct ust_app
*app
)
5571 struct ust_app_event
*ua_event
= NULL
;
5573 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5574 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5576 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5581 if (ua_event
->enabled
!= uevent
->enabled
) {
5582 ret
= uevent
->enabled
?
5583 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5584 disable_ust_app_event(ua_sess
, ua_event
, app
);
5592 /* Called with RCU read-side lock held. */
5594 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5597 enum lttng_error_code ret_code
;
5598 enum lttng_trigger_status t_status
;
5599 struct lttng_ht_iter app_trigger_iter
;
5600 struct lttng_triggers
*triggers
= NULL
;
5601 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5602 unsigned int count
, i
;
5605 * Currrently, registering or unregistering a trigger with an
5606 * event rule condition causes a full synchronization of the event
5609 * The first step attempts to add an event notifier for all registered
5610 * triggers that apply to the user space tracers. Then, the
5611 * application's event notifiers rules are all checked against the list
5612 * of registered triggers. Any event notifier that doesn't have a
5613 * matching trigger can be assumed to have been disabled.
5615 * All of this is inefficient, but is put in place to get the feature
5616 * rolling as it is simpler at this moment. It will be optimized Soon™
5617 * to allow the state of enabled
5618 * event notifiers to be synchronized in a piece-wise way.
5621 /* Get all triggers using uid 0 (root) */
5622 ret_code
= notification_thread_command_list_triggers(
5623 notification_thread_handle
, 0, &triggers
);
5624 if (ret_code
!= LTTNG_OK
) {
5631 t_status
= lttng_triggers_get_count(triggers
, &count
);
5632 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5637 for (i
= 0; i
< count
; i
++) {
5638 struct lttng_condition
*condition
;
5639 struct lttng_event_rule
*event_rule
;
5640 struct lttng_trigger
*trigger
;
5641 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5642 enum lttng_condition_status condition_status
;
5645 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5648 token
= lttng_trigger_get_tracer_token(trigger
);
5649 condition
= lttng_trigger_get_condition(trigger
);
5651 if (lttng_condition_get_type(condition
) != LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
5652 /* Does not apply */
5656 condition_status
= lttng_condition_event_rule_borrow_rule_mutable(condition
, &event_rule
);
5657 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5659 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5660 /* Skip kernel related triggers. */
5665 * Find or create the associated token event rule. The caller
5666 * holds the RCU read lock, so this is safe to call without
5667 * explicitly acquiring it here.
5669 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
5670 app
->token_to_event_notifier_rule_ht
, token
);
5671 if (!looked_up_event_notifier_rule
) {
5672 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
5680 /* Remove all unknown event sources from the app. */
5681 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
5682 &app_trigger_iter
.iter
, event_notifier_rule
,
5684 const uint64_t app_token
= event_notifier_rule
->token
;
5688 * Check if the app event trigger still exists on the
5689 * notification side.
5691 for (i
= 0; i
< count
; i
++) {
5692 uint64_t notification_thread_token
;
5693 const struct lttng_trigger
*trigger
=
5694 lttng_triggers_get_at_index(
5699 notification_thread_token
=
5700 lttng_trigger_get_tracer_token(trigger
);
5702 if (notification_thread_token
== app_token
) {
5714 * This trigger was unregistered, disable it on the tracer's
5717 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
5721 /* Callee logs errors. */
5722 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
5724 delete_ust_app_event_notifier_rule(
5725 app
->sock
, event_notifier_rule
, app
);
5731 lttng_triggers_destroy(triggers
);
5736 * The caller must ensure that the application is compatible and is tracked
5737 * by the process attribute trackers.
5740 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5741 struct ust_app
*app
)
5744 struct cds_lfht_iter uchan_iter
;
5745 struct ltt_ust_channel
*uchan
;
5746 struct ust_app_session
*ua_sess
= NULL
;
5749 * The application's configuration should only be synchronized for
5752 assert(usess
->active
);
5754 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5756 /* Tracer is probably gone or ENOMEM. */
5761 pthread_mutex_lock(&ua_sess
->lock
);
5762 if (ua_sess
->deleted
) {
5763 pthread_mutex_unlock(&ua_sess
->lock
);
5769 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5771 struct ust_app_channel
*ua_chan
;
5772 struct cds_lfht_iter uevent_iter
;
5773 struct ltt_ust_event
*uevent
;
5776 * Search for a matching ust_app_channel. If none is found,
5777 * create it. Creating the channel will cause the ua_chan
5778 * structure to be allocated, the channel buffers to be
5779 * allocated (if necessary) and sent to the application, and
5780 * all enabled contexts will be added to the channel.
5782 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5783 app
, uchan
, &ua_chan
);
5785 /* Tracer is probably gone or ENOMEM. */
5790 /* ua_chan will be NULL for the metadata channel */
5794 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5796 ret
= ust_app_channel_synchronize_event(ua_chan
,
5797 uevent
, ua_sess
, app
);
5803 if (ua_chan
->enabled
!= uchan
->enabled
) {
5804 ret
= uchan
->enabled
?
5805 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5806 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5814 * Create the metadata for the application. This returns gracefully if a
5815 * metadata was already set for the session.
5817 * The metadata channel must be created after the data channels as the
5818 * consumer daemon assumes this ordering. When interacting with a relay
5819 * daemon, the consumer will use this assumption to send the
5820 * "STREAMS_SENT" message to the relay daemon.
5822 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5830 pthread_mutex_unlock(&ua_sess
->lock
);
5831 /* Everything went well at this point. */
5836 pthread_mutex_unlock(&ua_sess
->lock
);
5839 destroy_app_session(app
, ua_sess
);
5845 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5847 struct ust_app_session
*ua_sess
;
5849 ua_sess
= lookup_session_by_app(usess
, app
);
5850 if (ua_sess
== NULL
) {
5853 destroy_app_session(app
, ua_sess
);
5857 * Add channels/events from UST global domain to registered apps at sock.
5859 * Called with session lock held.
5860 * Called with RCU read-side lock held.
5862 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5865 assert(usess
->active
);
5867 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5868 app
->sock
, usess
->id
);
5870 if (!app
->compatible
) {
5873 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5875 trace_ust_id_tracker_lookup(
5876 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5878 trace_ust_id_tracker_lookup(
5879 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5882 * Synchronize the application's internal tracing configuration
5883 * and start tracing.
5885 ust_app_synchronize(usess
, app
);
5886 ust_app_start_trace(usess
, app
);
5888 ust_app_global_destroy(usess
, app
);
5893 * Add all event notifiers to an application.
5895 * Called with session lock held.
5896 * Called with RCU read-side lock held.
5898 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
5900 DBG2("UST application global event notifier rules update: app = '%s' (ppid: %d)",
5901 app
->name
, app
->ppid
);
5903 if (!app
->compatible
) {
5907 if (app
->event_notifier_group
.object
== NULL
) {
5908 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s' (ppid: %d)",
5909 app
->name
, app
->ppid
);
5913 ust_app_synchronize_event_notifier_rules(app
);
5917 * Called with session lock held.
5919 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5921 struct lttng_ht_iter iter
;
5922 struct ust_app
*app
;
5925 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5926 ust_app_global_update(usess
, app
);
5931 void ust_app_global_update_all_event_notifier_rules(void)
5933 struct lttng_ht_iter iter
;
5934 struct ust_app
*app
;
5937 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5938 ust_app_global_update_event_notifier_rules(app
);
5945 * Add context to a specific channel for global UST domain.
5947 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5948 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5951 struct lttng_ht_node_str
*ua_chan_node
;
5952 struct lttng_ht_iter iter
, uiter
;
5953 struct ust_app_channel
*ua_chan
= NULL
;
5954 struct ust_app_session
*ua_sess
;
5955 struct ust_app
*app
;
5957 assert(usess
->active
);
5960 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5961 if (!app
->compatible
) {
5963 * TODO: In time, we should notice the caller of this error by
5964 * telling him that this is a version error.
5968 ua_sess
= lookup_session_by_app(usess
, app
);
5969 if (ua_sess
== NULL
) {
5973 pthread_mutex_lock(&ua_sess
->lock
);
5975 if (ua_sess
->deleted
) {
5976 pthread_mutex_unlock(&ua_sess
->lock
);
5980 /* Lookup channel in the ust app session */
5981 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5982 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5983 if (ua_chan_node
== NULL
) {
5986 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5988 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5993 pthread_mutex_unlock(&ua_sess
->lock
);
6001 * Receive registration and populate the given msg structure.
6003 * On success return 0 else a negative value returned by the ustctl call.
6005 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6008 uint32_t pid
, ppid
, uid
, gid
;
6012 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
6013 &pid
, &ppid
, &uid
, &gid
,
6014 &msg
->bits_per_long
,
6015 &msg
->uint8_t_alignment
,
6016 &msg
->uint16_t_alignment
,
6017 &msg
->uint32_t_alignment
,
6018 &msg
->uint64_t_alignment
,
6019 &msg
->long_alignment
,
6026 case LTTNG_UST_ERR_EXITING
:
6027 DBG3("UST app recv reg message failed. Application died");
6029 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6030 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6031 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
6032 LTTNG_UST_ABI_MINOR_VERSION
);
6035 ERR("UST app recv reg message failed with ret %d", ret
);
6040 msg
->pid
= (pid_t
) pid
;
6041 msg
->ppid
= (pid_t
) ppid
;
6042 msg
->uid
= (uid_t
) uid
;
6043 msg
->gid
= (gid_t
) gid
;
6050 * Return a ust app session object using the application object and the
6051 * session object descriptor has a key. If not found, NULL is returned.
6052 * A RCU read side lock MUST be acquired when calling this function.
6054 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
6057 struct lttng_ht_node_ulong
*node
;
6058 struct lttng_ht_iter iter
;
6059 struct ust_app_session
*ua_sess
= NULL
;
6063 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
6064 node
= lttng_ht_iter_get_node_ulong(&iter
);
6066 DBG2("UST app session find by objd %d not found", objd
);
6070 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
6077 * Return a ust app channel object using the application object and the channel
6078 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6079 * lock MUST be acquired before calling this function.
6081 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
6084 struct lttng_ht_node_ulong
*node
;
6085 struct lttng_ht_iter iter
;
6086 struct ust_app_channel
*ua_chan
= NULL
;
6090 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
6091 node
= lttng_ht_iter_get_node_ulong(&iter
);
6093 DBG2("UST app channel find by objd %d not found", objd
);
6097 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
6104 * Reply to a register channel notification from an application on the notify
6105 * socket. The channel metadata is also created.
6107 * The session UST registry lock is acquired in this function.
6109 * On success 0 is returned else a negative value.
6111 static int reply_ust_register_channel(int sock
, int cobjd
,
6112 size_t nr_fields
, struct ustctl_field
*fields
)
6114 int ret
, ret_code
= 0;
6116 uint64_t chan_reg_key
;
6117 enum ustctl_channel_header type
;
6118 struct ust_app
*app
;
6119 struct ust_app_channel
*ua_chan
;
6120 struct ust_app_session
*ua_sess
;
6121 struct ust_registry_session
*registry
;
6122 struct ust_registry_channel
*chan_reg
;
6126 /* Lookup application. If not found, there is a code flow error. */
6127 app
= find_app_by_notify_sock(sock
);
6129 DBG("Application socket %d is being torn down. Abort event notify",
6132 goto error_rcu_unlock
;
6135 /* Lookup channel by UST object descriptor. */
6136 ua_chan
= find_channel_by_objd(app
, cobjd
);
6138 DBG("Application channel is being torn down. Abort event notify");
6140 goto error_rcu_unlock
;
6143 assert(ua_chan
->session
);
6144 ua_sess
= ua_chan
->session
;
6146 /* Get right session registry depending on the session buffer type. */
6147 registry
= get_session_registry(ua_sess
);
6149 DBG("Application session is being torn down. Abort event notify");
6151 goto error_rcu_unlock
;
6154 /* Depending on the buffer type, a different channel key is used. */
6155 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6156 chan_reg_key
= ua_chan
->tracing_channel_id
;
6158 chan_reg_key
= ua_chan
->key
;
6161 pthread_mutex_lock(®istry
->lock
);
6163 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
6166 if (!chan_reg
->register_done
) {
6168 * TODO: eventually use the registry event count for
6169 * this channel to better guess header type for per-pid
6172 type
= USTCTL_CHANNEL_HEADER_LARGE
;
6173 chan_reg
->nr_ctx_fields
= nr_fields
;
6174 chan_reg
->ctx_fields
= fields
;
6176 chan_reg
->header_type
= type
;
6178 /* Get current already assigned values. */
6179 type
= chan_reg
->header_type
;
6181 /* Channel id is set during the object creation. */
6182 chan_id
= chan_reg
->chan_id
;
6184 /* Append to metadata */
6185 if (!chan_reg
->metadata_dumped
) {
6186 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
6188 ERR("Error appending channel metadata (errno = %d)", ret_code
);
6194 DBG3("UST app replying to register channel key %" PRIu64
6195 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
6198 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
6200 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6201 ERR("UST app reply channel failed with ret %d", ret
);
6203 DBG3("UST app reply channel failed. Application died");
6208 /* This channel registry registration is completed. */
6209 chan_reg
->register_done
= 1;
6212 pthread_mutex_unlock(®istry
->lock
);
6220 * Add event to the UST channel registry. When the event is added to the
6221 * registry, the metadata is also created. Once done, this replies to the
6222 * application with the appropriate error code.
6224 * The session UST registry lock is acquired in the function.
6226 * On success 0 is returned else a negative value.
6228 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
6229 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
6230 int loglevel_value
, char *model_emf_uri
)
6233 uint32_t event_id
= 0;
6234 uint64_t chan_reg_key
;
6235 struct ust_app
*app
;
6236 struct ust_app_channel
*ua_chan
;
6237 struct ust_app_session
*ua_sess
;
6238 struct ust_registry_session
*registry
;
6242 /* Lookup application. If not found, there is a code flow error. */
6243 app
= find_app_by_notify_sock(sock
);
6245 DBG("Application socket %d is being torn down. Abort event notify",
6248 goto error_rcu_unlock
;
6251 /* Lookup channel by UST object descriptor. */
6252 ua_chan
= find_channel_by_objd(app
, cobjd
);
6254 DBG("Application channel is being torn down. Abort event notify");
6256 goto error_rcu_unlock
;
6259 assert(ua_chan
->session
);
6260 ua_sess
= ua_chan
->session
;
6262 registry
= get_session_registry(ua_sess
);
6264 DBG("Application session is being torn down. Abort event notify");
6266 goto error_rcu_unlock
;
6269 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6270 chan_reg_key
= ua_chan
->tracing_channel_id
;
6272 chan_reg_key
= ua_chan
->key
;
6275 pthread_mutex_lock(®istry
->lock
);
6278 * From this point on, this call acquires the ownership of the sig, fields
6279 * and model_emf_uri meaning any free are done inside it if needed. These
6280 * three variables MUST NOT be read/write after this.
6282 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
6283 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
6284 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
6288 model_emf_uri
= NULL
;
6291 * The return value is returned to ustctl so in case of an error, the
6292 * application can be notified. In case of an error, it's important not to
6293 * return a negative error or else the application will get closed.
6295 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
6297 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6298 ERR("UST app reply event failed with ret %d", ret
);
6300 DBG3("UST app reply event failed. Application died");
6303 * No need to wipe the create event since the application socket will
6304 * get close on error hence cleaning up everything by itself.
6309 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6313 pthread_mutex_unlock(®istry
->lock
);
6318 free(model_emf_uri
);
6323 * Add enum to the UST session registry. Once done, this replies to the
6324 * application with the appropriate error code.
6326 * The session UST registry lock is acquired within this function.
6328 * On success 0 is returned else a negative value.
6330 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
6331 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
6333 int ret
= 0, ret_code
;
6334 struct ust_app
*app
;
6335 struct ust_app_session
*ua_sess
;
6336 struct ust_registry_session
*registry
;
6337 uint64_t enum_id
= -1ULL;
6341 /* Lookup application. If not found, there is a code flow error. */
6342 app
= find_app_by_notify_sock(sock
);
6344 /* Return an error since this is not an error */
6345 DBG("Application socket %d is being torn down. Aborting enum registration",
6348 goto error_rcu_unlock
;
6351 /* Lookup session by UST object descriptor. */
6352 ua_sess
= find_session_by_objd(app
, sobjd
);
6354 /* Return an error since this is not an error */
6355 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6357 goto error_rcu_unlock
;
6360 registry
= get_session_registry(ua_sess
);
6362 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6364 goto error_rcu_unlock
;
6367 pthread_mutex_lock(®istry
->lock
);
6370 * From this point on, the callee acquires the ownership of
6371 * entries. The variable entries MUST NOT be read/written after
6374 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
6375 entries
, nr_entries
, &enum_id
);
6379 * The return value is returned to ustctl so in case of an error, the
6380 * application can be notified. In case of an error, it's important not to
6381 * return a negative error or else the application will get closed.
6383 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
6385 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6386 ERR("UST app reply enum failed with ret %d", ret
);
6388 DBG3("UST app reply enum failed. Application died");
6391 * No need to wipe the create enum since the application socket will
6392 * get close on error hence cleaning up everything by itself.
6397 DBG3("UST registry enum %s added successfully or already found", name
);
6400 pthread_mutex_unlock(®istry
->lock
);
6407 * Handle application notification through the given notify socket.
6409 * Return 0 on success or else a negative value.
6411 int ust_app_recv_notify(int sock
)
6414 enum ustctl_notify_cmd cmd
;
6416 DBG3("UST app receiving notify from sock %d", sock
);
6418 ret
= ustctl_recv_notify(sock
, &cmd
);
6420 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6421 ERR("UST app recv notify failed with ret %d", ret
);
6423 DBG3("UST app recv notify failed. Application died");
6429 case USTCTL_NOTIFY_CMD_EVENT
:
6431 int sobjd
, cobjd
, loglevel_value
;
6432 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6434 struct ustctl_field
*fields
;
6436 DBG2("UST app ustctl register event received");
6438 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
6439 &loglevel_value
, &sig
, &nr_fields
, &fields
,
6442 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6443 ERR("UST app recv event failed with ret %d", ret
);
6445 DBG3("UST app recv event failed. Application died");
6451 * Add event to the UST registry coming from the notify socket. This
6452 * call will free if needed the sig, fields and model_emf_uri. This
6453 * code path loses the ownsership of these variables and transfer them
6454 * to the this function.
6456 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6457 fields
, loglevel_value
, model_emf_uri
);
6464 case USTCTL_NOTIFY_CMD_CHANNEL
:
6468 struct ustctl_field
*fields
;
6470 DBG2("UST app ustctl register channel received");
6472 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
6475 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6476 ERR("UST app recv channel failed with ret %d", ret
);
6478 DBG3("UST app recv channel failed. Application died");
6484 * The fields ownership are transfered to this function call meaning
6485 * that if needed it will be freed. After this, it's invalid to access
6486 * fields or clean it up.
6488 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
6496 case USTCTL_NOTIFY_CMD_ENUM
:
6499 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
6501 struct ustctl_enum_entry
*entries
;
6503 DBG2("UST app ustctl register enum received");
6505 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
6506 &entries
, &nr_entries
);
6508 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6509 ERR("UST app recv enum failed with ret %d", ret
);
6511 DBG3("UST app recv enum failed. Application died");
6516 /* Callee assumes ownership of entries */
6517 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6518 entries
, nr_entries
);
6526 /* Should NEVER happen. */
6535 * Once the notify socket hangs up, this is called. First, it tries to find the
6536 * corresponding application. On failure, the call_rcu to close the socket is
6537 * executed. If an application is found, it tries to delete it from the notify
6538 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6540 * Note that an object needs to be allocated here so on ENOMEM failure, the
6541 * call RCU is not done but the rest of the cleanup is.
6543 void ust_app_notify_sock_unregister(int sock
)
6546 struct lttng_ht_iter iter
;
6547 struct ust_app
*app
;
6548 struct ust_app_notify_sock_obj
*obj
;
6554 obj
= zmalloc(sizeof(*obj
));
6557 * An ENOMEM is kind of uncool. If this strikes we continue the
6558 * procedure but the call_rcu will not be called. In this case, we
6559 * accept the fd leak rather than possibly creating an unsynchronized
6560 * state between threads.
6562 * TODO: The notify object should be created once the notify socket is
6563 * registered and stored independantely from the ust app object. The
6564 * tricky part is to synchronize the teardown of the application and
6565 * this notify object. Let's keep that in mind so we can avoid this
6566 * kind of shenanigans with ENOMEM in the teardown path.
6573 DBG("UST app notify socket unregister %d", sock
);
6576 * Lookup application by notify socket. If this fails, this means that the
6577 * hash table delete has already been done by the application
6578 * unregistration process so we can safely close the notify socket in a
6581 app
= find_app_by_notify_sock(sock
);
6586 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6589 * Whatever happens here either we fail or succeed, in both cases we have
6590 * to close the socket after a grace period to continue to the call RCU
6591 * here. If the deletion is successful, the application is not visible
6592 * anymore by other threads and is it fails it means that it was already
6593 * deleted from the hash table so either way we just have to close the
6596 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6602 * Close socket after a grace period to avoid for the socket to be reused
6603 * before the application object is freed creating potential race between
6604 * threads trying to add unique in the global hash table.
6607 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6612 * Destroy a ust app data structure and free its memory.
6614 void ust_app_destroy(struct ust_app
*app
)
6620 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6624 * Take a snapshot for a given UST session. The snapshot is sent to the given
6627 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6629 enum lttng_error_code
ust_app_snapshot_record(
6630 const struct ltt_ust_session
*usess
,
6631 const struct consumer_output
*output
, int wait
,
6632 uint64_t nb_packets_per_stream
)
6635 enum lttng_error_code status
= LTTNG_OK
;
6636 struct lttng_ht_iter iter
;
6637 struct ust_app
*app
;
6638 char *trace_path
= NULL
;
6645 switch (usess
->buffer_type
) {
6646 case LTTNG_BUFFER_PER_UID
:
6648 struct buffer_reg_uid
*reg
;
6650 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6651 struct buffer_reg_channel
*reg_chan
;
6652 struct consumer_socket
*socket
;
6653 char pathname
[PATH_MAX
];
6654 size_t consumer_path_offset
= 0;
6656 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6657 /* Skip since no metadata is present */
6661 /* Get consumer socket to use to push the metadata.*/
6662 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6665 status
= LTTNG_ERR_INVALID
;
6669 memset(pathname
, 0, sizeof(pathname
));
6670 ret
= snprintf(pathname
, sizeof(pathname
),
6671 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
6672 reg
->uid
, reg
->bits_per_long
);
6674 PERROR("snprintf snapshot path");
6675 status
= LTTNG_ERR_INVALID
;
6678 /* Free path allowed on previous iteration. */
6680 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6681 &consumer_path_offset
);
6683 status
= LTTNG_ERR_INVALID
;
6686 /* Add the UST default trace dir to path. */
6687 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6688 reg_chan
, node
.node
) {
6689 status
= consumer_snapshot_channel(socket
,
6690 reg_chan
->consumer_key
,
6691 output
, 0, usess
->uid
,
6692 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
6693 nb_packets_per_stream
);
6694 if (status
!= LTTNG_OK
) {
6698 status
= consumer_snapshot_channel(socket
,
6699 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6700 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6702 if (status
!= LTTNG_OK
) {
6708 case LTTNG_BUFFER_PER_PID
:
6710 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6711 struct consumer_socket
*socket
;
6712 struct lttng_ht_iter chan_iter
;
6713 struct ust_app_channel
*ua_chan
;
6714 struct ust_app_session
*ua_sess
;
6715 struct ust_registry_session
*registry
;
6716 char pathname
[PATH_MAX
];
6717 size_t consumer_path_offset
= 0;
6719 ua_sess
= lookup_session_by_app(usess
, app
);
6721 /* Session not associated with this app. */
6725 /* Get the right consumer socket for the application. */
6726 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6729 status
= LTTNG_ERR_INVALID
;
6733 /* Add the UST default trace dir to path. */
6734 memset(pathname
, 0, sizeof(pathname
));
6735 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
6738 status
= LTTNG_ERR_INVALID
;
6739 PERROR("snprintf snapshot path");
6742 /* Free path allowed on previous iteration. */
6744 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6745 &consumer_path_offset
);
6747 status
= LTTNG_ERR_INVALID
;
6750 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6751 ua_chan
, node
.node
) {
6752 status
= consumer_snapshot_channel(socket
,
6753 ua_chan
->key
, output
, 0,
6754 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6755 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6756 &trace_path
[consumer_path_offset
], wait
,
6757 nb_packets_per_stream
);
6761 case LTTNG_ERR_CHAN_NOT_FOUND
:
6768 registry
= get_session_registry(ua_sess
);
6770 DBG("Application session is being torn down. Skip application.");
6773 status
= consumer_snapshot_channel(socket
,
6774 registry
->metadata_key
, output
, 1,
6775 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6776 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6777 &trace_path
[consumer_path_offset
], wait
, 0);
6781 case LTTNG_ERR_CHAN_NOT_FOUND
:
6801 * Return the size taken by one more packet per stream.
6803 uint64_t ust_app_get_size_one_more_packet_per_stream(
6804 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6806 uint64_t tot_size
= 0;
6807 struct ust_app
*app
;
6808 struct lttng_ht_iter iter
;
6812 switch (usess
->buffer_type
) {
6813 case LTTNG_BUFFER_PER_UID
:
6815 struct buffer_reg_uid
*reg
;
6817 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6818 struct buffer_reg_channel
*reg_chan
;
6821 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6822 reg_chan
, node
.node
) {
6823 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6825 * Don't take channel into account if we
6826 * already grab all its packets.
6830 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6836 case LTTNG_BUFFER_PER_PID
:
6839 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6840 struct ust_app_channel
*ua_chan
;
6841 struct ust_app_session
*ua_sess
;
6842 struct lttng_ht_iter chan_iter
;
6844 ua_sess
= lookup_session_by_app(usess
, app
);
6846 /* Session not associated with this app. */
6850 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6851 ua_chan
, node
.node
) {
6852 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6854 * Don't take channel into account if we
6855 * already grab all its packets.
6859 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6873 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6874 struct cds_list_head
*buffer_reg_uid_list
,
6875 struct consumer_output
*consumer
, uint64_t uchan_id
,
6876 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6879 uint64_t consumer_chan_key
;
6884 ret
= buffer_reg_uid_consumer_channel_key(
6885 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6893 ret
= consumer_get_lost_packets(ust_session_id
,
6894 consumer_chan_key
, consumer
, lost
);
6896 ret
= consumer_get_discarded_events(ust_session_id
,
6897 consumer_chan_key
, consumer
, discarded
);
6904 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6905 struct ltt_ust_channel
*uchan
,
6906 struct consumer_output
*consumer
, int overwrite
,
6907 uint64_t *discarded
, uint64_t *lost
)
6910 struct lttng_ht_iter iter
;
6911 struct lttng_ht_node_str
*ua_chan_node
;
6912 struct ust_app
*app
;
6913 struct ust_app_session
*ua_sess
;
6914 struct ust_app_channel
*ua_chan
;
6921 * Iterate over every registered applications. Sum counters for
6922 * all applications containing requested session and channel.
6924 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6925 struct lttng_ht_iter uiter
;
6927 ua_sess
= lookup_session_by_app(usess
, app
);
6928 if (ua_sess
== NULL
) {
6933 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6934 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6935 /* If the session is found for the app, the channel must be there */
6936 assert(ua_chan_node
);
6938 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6943 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6950 uint64_t _discarded
;
6952 ret
= consumer_get_discarded_events(usess
->id
,
6953 ua_chan
->key
, consumer
, &_discarded
);
6957 (*discarded
) += _discarded
;
6966 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6967 struct ust_app
*app
)
6970 struct ust_app_session
*ua_sess
;
6972 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6976 ua_sess
= lookup_session_by_app(usess
, app
);
6977 if (ua_sess
== NULL
) {
6978 /* The session is in teardown process. Ignore and continue. */
6982 pthread_mutex_lock(&ua_sess
->lock
);
6984 if (ua_sess
->deleted
) {
6988 pthread_mutex_lock(&app
->sock_lock
);
6989 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6990 pthread_mutex_unlock(&app
->sock_lock
);
6993 pthread_mutex_unlock(&ua_sess
->lock
);
6997 health_code_update();
7002 * Regenerate the statedump for each app in the session.
7004 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7007 struct lttng_ht_iter iter
;
7008 struct ust_app
*app
;
7010 DBG("Regenerating the metadata for all UST apps");
7014 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7015 if (!app
->compatible
) {
7019 ret
= ust_app_regenerate_statedump(usess
, app
);
7021 /* Continue to the next app even on error */
7032 * Rotate all the channels of a session.
7034 * Return LTTNG_OK on success or else an LTTng error code.
7036 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7039 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7040 struct lttng_ht_iter iter
;
7041 struct ust_app
*app
;
7042 struct ltt_ust_session
*usess
= session
->ust_session
;
7048 switch (usess
->buffer_type
) {
7049 case LTTNG_BUFFER_PER_UID
:
7051 struct buffer_reg_uid
*reg
;
7053 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7054 struct buffer_reg_channel
*reg_chan
;
7055 struct consumer_socket
*socket
;
7057 if (!reg
->registry
->reg
.ust
->metadata_key
) {
7058 /* Skip since no metadata is present */
7062 /* Get consumer socket to use to push the metadata.*/
7063 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7066 cmd_ret
= LTTNG_ERR_INVALID
;
7070 /* Rotate the data channels. */
7071 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7072 reg_chan
, node
.node
) {
7073 ret
= consumer_rotate_channel(socket
,
7074 reg_chan
->consumer_key
,
7075 usess
->uid
, usess
->gid
,
7077 /* is_metadata_channel */ false);
7079 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7084 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7086 ret
= consumer_rotate_channel(socket
,
7087 reg
->registry
->reg
.ust
->metadata_key
,
7088 usess
->uid
, usess
->gid
,
7090 /* is_metadata_channel */ true);
7092 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7098 case LTTNG_BUFFER_PER_PID
:
7100 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7101 struct consumer_socket
*socket
;
7102 struct lttng_ht_iter chan_iter
;
7103 struct ust_app_channel
*ua_chan
;
7104 struct ust_app_session
*ua_sess
;
7105 struct ust_registry_session
*registry
;
7107 ua_sess
= lookup_session_by_app(usess
, app
);
7109 /* Session not associated with this app. */
7113 /* Get the right consumer socket for the application. */
7114 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7117 cmd_ret
= LTTNG_ERR_INVALID
;
7121 registry
= get_session_registry(ua_sess
);
7123 DBG("Application session is being torn down. Skip application.");
7127 /* Rotate the data channels. */
7128 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7129 ua_chan
, node
.node
) {
7130 ret
= consumer_rotate_channel(socket
,
7132 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7133 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7135 /* is_metadata_channel */ false);
7137 /* Per-PID buffer and application going away. */
7138 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7140 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7145 /* Rotate the metadata channel. */
7146 (void) push_metadata(registry
, usess
->consumer
);
7147 ret
= consumer_rotate_channel(socket
,
7148 registry
->metadata_key
,
7149 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7150 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7152 /* is_metadata_channel */ true);
7154 /* Per-PID buffer and application going away. */
7155 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7157 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7175 enum lttng_error_code
ust_app_create_channel_subdirectories(
7176 const struct ltt_ust_session
*usess
)
7178 enum lttng_error_code ret
= LTTNG_OK
;
7179 struct lttng_ht_iter iter
;
7180 enum lttng_trace_chunk_status chunk_status
;
7181 char *pathname_index
;
7184 assert(usess
->current_trace_chunk
);
7187 switch (usess
->buffer_type
) {
7188 case LTTNG_BUFFER_PER_UID
:
7190 struct buffer_reg_uid
*reg
;
7192 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7193 fmt_ret
= asprintf(&pathname_index
,
7194 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7195 reg
->uid
, reg
->bits_per_long
);
7197 ERR("Failed to format channel index directory");
7198 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7203 * Create the index subdirectory which will take care
7204 * of implicitly creating the channel's path.
7206 chunk_status
= lttng_trace_chunk_create_subdirectory(
7207 usess
->current_trace_chunk
,
7209 free(pathname_index
);
7210 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7211 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7217 case LTTNG_BUFFER_PER_PID
:
7219 struct ust_app
*app
;
7222 * Create the toplevel ust/ directory in case no apps are running.
7224 chunk_status
= lttng_trace_chunk_create_subdirectory(
7225 usess
->current_trace_chunk
,
7226 DEFAULT_UST_TRACE_DIR
);
7227 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7228 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7232 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7234 struct ust_app_session
*ua_sess
;
7235 struct ust_registry_session
*registry
;
7237 ua_sess
= lookup_session_by_app(usess
, app
);
7239 /* Session not associated with this app. */
7243 registry
= get_session_registry(ua_sess
);
7245 DBG("Application session is being torn down. Skip application.");
7249 fmt_ret
= asprintf(&pathname_index
,
7250 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7253 ERR("Failed to format channel index directory");
7254 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7258 * Create the index subdirectory which will take care
7259 * of implicitly creating the channel's path.
7261 chunk_status
= lttng_trace_chunk_create_subdirectory(
7262 usess
->current_trace_chunk
,
7264 free(pathname_index
);
7265 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7266 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7283 * Clear all the channels of a session.
7285 * Return LTTNG_OK on success or else an LTTng error code.
7287 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7290 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7291 struct lttng_ht_iter iter
;
7292 struct ust_app
*app
;
7293 struct ltt_ust_session
*usess
= session
->ust_session
;
7299 if (usess
->active
) {
7300 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7301 cmd_ret
= LTTNG_ERR_FATAL
;
7305 switch (usess
->buffer_type
) {
7306 case LTTNG_BUFFER_PER_UID
:
7308 struct buffer_reg_uid
*reg
;
7310 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7311 struct buffer_reg_channel
*reg_chan
;
7312 struct consumer_socket
*socket
;
7314 /* Get consumer socket to use to push the metadata.*/
7315 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7318 cmd_ret
= LTTNG_ERR_INVALID
;
7322 /* Clear the data channels. */
7323 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7324 reg_chan
, node
.node
) {
7325 ret
= consumer_clear_channel(socket
,
7326 reg_chan
->consumer_key
);
7332 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7335 * Clear the metadata channel.
7336 * Metadata channel is not cleared per se but we still need to
7337 * perform a rotation operation on it behind the scene.
7339 ret
= consumer_clear_channel(socket
,
7340 reg
->registry
->reg
.ust
->metadata_key
);
7347 case LTTNG_BUFFER_PER_PID
:
7349 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7350 struct consumer_socket
*socket
;
7351 struct lttng_ht_iter chan_iter
;
7352 struct ust_app_channel
*ua_chan
;
7353 struct ust_app_session
*ua_sess
;
7354 struct ust_registry_session
*registry
;
7356 ua_sess
= lookup_session_by_app(usess
, app
);
7358 /* Session not associated with this app. */
7362 /* Get the right consumer socket for the application. */
7363 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7366 cmd_ret
= LTTNG_ERR_INVALID
;
7370 registry
= get_session_registry(ua_sess
);
7372 DBG("Application session is being torn down. Skip application.");
7376 /* Clear the data channels. */
7377 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7378 ua_chan
, node
.node
) {
7379 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7381 /* Per-PID buffer and application going away. */
7382 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7389 (void) push_metadata(registry
, usess
->consumer
);
7392 * Clear the metadata channel.
7393 * Metadata channel is not cleared per se but we still need to
7394 * perform rotation operation on it behind the scene.
7396 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
7398 /* Per-PID buffer and application going away. */
7399 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7417 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7418 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7421 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7431 * This function skips the metadata channel as the begin/end timestamps of a
7432 * metadata packet are useless.
7434 * Moreover, opening a packet after a "clear" will cause problems for live
7435 * sessions as it will introduce padding that was not part of the first trace
7436 * chunk. The relay daemon expects the content of the metadata stream of
7437 * successive metadata trace chunks to be strict supersets of one another.
7439 * For example, flushing a packet at the beginning of the metadata stream of
7440 * a trace chunk resulting from a "clear" session command will cause the
7441 * size of the metadata stream of the new trace chunk to not match the size of
7442 * the metadata stream of the original chunk. This will confuse the relay
7443 * daemon as the same "offset" in a metadata stream will no longer point
7444 * to the same content.
7446 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7448 enum lttng_error_code ret
= LTTNG_OK
;
7449 struct lttng_ht_iter iter
;
7450 struct ltt_ust_session
*usess
= session
->ust_session
;
7456 switch (usess
->buffer_type
) {
7457 case LTTNG_BUFFER_PER_UID
:
7459 struct buffer_reg_uid
*reg
;
7461 cds_list_for_each_entry (
7462 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7463 struct buffer_reg_channel
*reg_chan
;
7464 struct consumer_socket
*socket
;
7466 socket
= consumer_find_socket_by_bitness(
7467 reg
->bits_per_long
, usess
->consumer
);
7469 ret
= LTTNG_ERR_FATAL
;
7473 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7474 &iter
.iter
, reg_chan
, node
.node
) {
7475 const int open_ret
=
7476 consumer_open_channel_packets(
7478 reg_chan
->consumer_key
);
7481 ret
= LTTNG_ERR_UNK
;
7488 case LTTNG_BUFFER_PER_PID
:
7490 struct ust_app
*app
;
7492 cds_lfht_for_each_entry (
7493 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7494 struct consumer_socket
*socket
;
7495 struct lttng_ht_iter chan_iter
;
7496 struct ust_app_channel
*ua_chan
;
7497 struct ust_app_session
*ua_sess
;
7498 struct ust_registry_session
*registry
;
7500 ua_sess
= lookup_session_by_app(usess
, app
);
7502 /* Session not associated with this app. */
7506 /* Get the right consumer socket for the application. */
7507 socket
= consumer_find_socket_by_bitness(
7508 app
->bits_per_long
, usess
->consumer
);
7510 ret
= LTTNG_ERR_FATAL
;
7514 registry
= get_session_registry(ua_sess
);
7516 DBG("Application session is being torn down. Skip application.");
7520 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
7521 &chan_iter
.iter
, ua_chan
, node
.node
) {
7522 const int open_ret
=
7523 consumer_open_channel_packets(
7529 * Per-PID buffer and application going
7532 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7536 ret
= LTTNG_ERR_UNK
;