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_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_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_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_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_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_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_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_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_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_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_filter_bytecode
*
1336 create_ust_filter_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1338 struct lttng_ust_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_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_capture_bytecode
*
1360 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1362 struct lttng_ust_capture_bytecode
*capture
= NULL
;
1364 /* Copy capture bytecode. */
1365 capture
= zmalloc(sizeof(*capture
) + orig_f
->len
);
1367 PERROR("Failed to allocate lttng_ust_capture_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1371 assert(sizeof(struct lttng_bytecode
) ==
1372 sizeof(struct lttng_ust_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_object_data
*ust_object
)
1543 struct lttng_ust_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.
1583 static int set_ust_capture(struct ust_app
*app
,
1584 const struct lttng_bytecode
*bytecode
,
1585 struct lttng_ust_object_data
*ust_object
)
1588 struct lttng_ust_capture_bytecode
*ust_bytecode
= NULL
;
1590 health_code_update();
1592 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1593 if (!ust_bytecode
) {
1594 ret
= -LTTNG_ERR_NOMEM
;
1598 pthread_mutex_lock(&app
->sock_lock
);
1599 ret
= ustctl_set_capture(app
->sock
, ust_bytecode
,
1601 pthread_mutex_unlock(&app
->sock_lock
);
1603 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1604 ERR("UST app set object capture failed: object = %p of app pid = %d, ret = %d",
1605 ust_object
, app
->pid
, ret
);
1608 * This is normal behavior, an application can die during the
1609 * creation process. Don't report an error so the execution can
1610 * continue normally.
1613 DBG3("Failed to set UST app object capture. Application is dead.");
1619 DBG2("UST capture successfully set: object = %p", ust_object
);
1622 health_code_update();
1628 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1629 const struct lttng_event_exclusion
*exclusion
)
1631 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1632 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1633 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1635 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1636 if (!ust_exclusion
) {
1641 assert(sizeof(struct lttng_event_exclusion
) ==
1642 sizeof(struct lttng_ust_event_exclusion
));
1643 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1645 return ust_exclusion
;
1649 * Set event exclusions on the tracer.
1651 static int set_ust_object_exclusions(struct ust_app
*app
,
1652 const struct lttng_event_exclusion
*exclusions
,
1653 struct lttng_ust_object_data
*ust_object
)
1656 struct lttng_ust_event_exclusion
*ust_exclusions
= NULL
;
1658 assert(exclusions
&& exclusions
->count
> 0);
1660 health_code_update();
1662 ust_exclusions
= create_ust_exclusion_from_exclusion(
1664 if (!ust_exclusions
) {
1665 ret
= -LTTNG_ERR_NOMEM
;
1668 pthread_mutex_lock(&app
->sock_lock
);
1669 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1670 pthread_mutex_unlock(&app
->sock_lock
);
1672 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1673 ERR("Failed to set UST app exclusions for object %p of app (pid: %d) "
1674 "with ret %d", ust_object
, app
->pid
, ret
);
1677 * This is normal behavior, an application can die during the
1678 * creation process. Don't report an error so the execution can
1679 * continue normally.
1682 DBG3("Failed to set UST app object exclusions. Application is dead.");
1687 DBG2("UST exclusions set successfully for object %p", ust_object
);
1690 health_code_update();
1691 free(ust_exclusions
);
1696 * Disable the specified event on to UST tracer for the UST session.
1698 static int disable_ust_object(struct ust_app
*app
,
1699 struct lttng_ust_object_data
*object
)
1703 health_code_update();
1705 pthread_mutex_lock(&app
->sock_lock
);
1706 ret
= ustctl_disable(app
->sock
, object
);
1707 pthread_mutex_unlock(&app
->sock_lock
);
1709 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1710 ERR("Failed to disable UST app object %p app (pid: %d) with ret %d",
1711 object
, app
->pid
, ret
);
1714 * This is normal behavior, an application can die during the
1715 * creation process. Don't report an error so the execution can
1716 * continue normally.
1719 DBG3("Failed to disable UST app object. Application is dead.");
1724 DBG2("UST app object %p disabled successfully for app (pid: %d)",
1728 health_code_update();
1733 * Disable the specified channel on to UST tracer for the UST session.
1735 static int disable_ust_channel(struct ust_app
*app
,
1736 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1740 health_code_update();
1742 pthread_mutex_lock(&app
->sock_lock
);
1743 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1744 pthread_mutex_unlock(&app
->sock_lock
);
1746 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1747 ERR("UST app channel %s disable failed for app (pid: %d) "
1748 "and session handle %d with ret %d",
1749 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1752 * This is normal behavior, an application can die during the
1753 * creation process. Don't report an error so the execution can
1754 * continue normally.
1757 DBG3("UST app disable channel failed. Application is dead.");
1762 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1763 ua_chan
->name
, app
->pid
);
1766 health_code_update();
1771 * Enable the specified channel on to UST tracer for the UST session.
1773 static int enable_ust_channel(struct ust_app
*app
,
1774 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1778 health_code_update();
1780 pthread_mutex_lock(&app
->sock_lock
);
1781 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1782 pthread_mutex_unlock(&app
->sock_lock
);
1784 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1785 ERR("UST app channel %s enable failed for app (pid: %d) "
1786 "and session handle %d with ret %d",
1787 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1790 * This is normal behavior, an application can die during the
1791 * creation process. Don't report an error so the execution can
1792 * continue normally.
1795 DBG3("UST app enable channel failed. Application is dead.");
1800 ua_chan
->enabled
= 1;
1802 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1803 ua_chan
->name
, app
->pid
);
1806 health_code_update();
1811 * Enable the specified event on to UST tracer for the UST session.
1813 static int enable_ust_object(
1814 struct ust_app
*app
, struct lttng_ust_object_data
*ust_object
)
1818 health_code_update();
1820 pthread_mutex_lock(&app
->sock_lock
);
1821 ret
= ustctl_enable(app
->sock
, ust_object
);
1822 pthread_mutex_unlock(&app
->sock_lock
);
1824 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1825 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
1826 ust_object
, app
->pid
, ret
);
1829 * This is normal behavior, an application can die during the
1830 * creation process. Don't report an error so the execution can
1831 * continue normally.
1834 DBG3("Failed to enable UST app object. Application is dead.");
1839 DBG2("UST app object %p enabled successfully for app (pid: %d)",
1840 ust_object
, app
->pid
);
1843 health_code_update();
1848 * Send channel and stream buffer to application.
1850 * Return 0 on success. On error, a negative value is returned.
1852 static int send_channel_pid_to_ust(struct ust_app
*app
,
1853 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1856 struct ust_app_stream
*stream
, *stmp
;
1862 health_code_update();
1864 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1867 /* Send channel to the application. */
1868 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1869 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1870 ret
= -ENOTCONN
; /* Caused by app exiting. */
1872 } else if (ret
< 0) {
1876 health_code_update();
1878 /* Send all streams to application. */
1879 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1880 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1881 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1882 ret
= -ENOTCONN
; /* Caused by app exiting. */
1884 } else if (ret
< 0) {
1887 /* We don't need the stream anymore once sent to the tracer. */
1888 cds_list_del(&stream
->list
);
1889 delete_ust_app_stream(-1, stream
, app
);
1891 /* Flag the channel that it is sent to the application. */
1892 ua_chan
->is_sent
= 1;
1895 health_code_update();
1900 * Create the specified event onto the UST tracer for a UST session.
1902 * Should be called with session mutex held.
1905 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1906 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1910 health_code_update();
1912 /* Create UST event on tracer */
1913 pthread_mutex_lock(&app
->sock_lock
);
1914 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1916 pthread_mutex_unlock(&app
->sock_lock
);
1918 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1920 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1921 ua_event
->attr
.name
, app
->pid
, ret
);
1924 * This is normal behavior, an application can die during the
1925 * creation process. Don't report an error so the execution can
1926 * continue normally.
1929 DBG3("UST app create event failed. Application is dead.");
1934 ua_event
->handle
= ua_event
->obj
->handle
;
1936 DBG2("UST app event %s created successfully for pid:%d object: %p",
1937 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
1939 health_code_update();
1941 /* Set filter if one is present. */
1942 if (ua_event
->filter
) {
1943 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
1949 /* Set exclusions for the event */
1950 if (ua_event
->exclusion
) {
1951 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
1957 /* If event not enabled, disable it on the tracer */
1958 if (ua_event
->enabled
) {
1960 * We now need to explicitly enable the event, since it
1961 * is now disabled at creation.
1963 ret
= enable_ust_object(app
, ua_event
->obj
);
1966 * If we hit an EPERM, something is wrong with our enable call. If
1967 * we get an EEXIST, there is a problem on the tracer side since we
1971 case -LTTNG_UST_ERR_PERM
:
1972 /* Code flow problem */
1974 case -LTTNG_UST_ERR_EXIST
:
1975 /* It's OK for our use case. */
1986 health_code_update();
1990 static int init_ust_event_notifier_from_event_rule(
1991 const struct lttng_event_rule
*rule
,
1992 struct lttng_ust_event_notifier
*event_notifier
)
1994 enum lttng_event_rule_status status
;
1995 enum lttng_loglevel_type loglevel_type
;
1996 enum lttng_ust_loglevel_type ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1997 int loglevel
= -1, ret
= 0;
1998 const char *pattern
;
2000 /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported. */
2001 assert(lttng_event_rule_get_type(rule
) ==
2002 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
2004 memset(event_notifier
, 0, sizeof(*event_notifier
));
2006 if (lttng_event_rule_targets_agent_domain(rule
)) {
2008 * Special event for agents
2009 * The actual meat of the event is in the filter that will be
2010 * attached later on.
2011 * Set the default values for the agent event.
2013 pattern
= event_get_default_agent_ust_name(
2014 lttng_event_rule_get_domain_type(rule
));
2016 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
2018 status
= lttng_event_rule_tracepoint_get_pattern(
2020 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2021 /* At this point, this is a fatal error. */
2025 status
= lttng_event_rule_tracepoint_get_log_level_type(
2026 rule
, &loglevel_type
);
2027 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2028 /* At this point, this is a fatal error. */
2032 switch (loglevel_type
) {
2033 case LTTNG_EVENT_LOGLEVEL_ALL
:
2034 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
2036 case LTTNG_EVENT_LOGLEVEL_RANGE
:
2037 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
2039 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
2040 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
2043 /* Unknown log level specification type. */
2047 if (loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
2048 status
= lttng_event_rule_tracepoint_get_log_level(
2050 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
2054 event_notifier
->event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
2055 ret
= lttng_strncpy(event_notifier
->event
.name
, pattern
,
2056 LTTNG_UST_SYM_NAME_LEN
- 1);
2058 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
2063 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2064 event_notifier
->event
.loglevel
= loglevel
;
2070 * Create the specified event notifier against the user space tracer of a
2071 * given application.
2073 static int create_ust_event_notifier(struct ust_app
*app
,
2074 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2077 enum lttng_condition_status condition_status
;
2078 const struct lttng_condition
*condition
= NULL
;
2079 struct lttng_ust_event_notifier event_notifier
;
2080 const struct lttng_event_rule
*event_rule
= NULL
;
2081 unsigned int capture_bytecode_count
= 0, i
;
2082 enum lttng_condition_status cond_status
;
2084 health_code_update();
2085 assert(app
->event_notifier_group
.object
);
2087 condition
= lttng_trigger_get_const_condition(
2088 ua_event_notifier_rule
->trigger
);
2090 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
);
2092 condition_status
= lttng_condition_event_rule_get_rule(condition
, &event_rule
);
2093 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2095 assert(lttng_event_rule_get_type(event_rule
) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
2097 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2098 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2100 /* Create UST event notifier against the tracer. */
2101 pthread_mutex_lock(&app
->sock_lock
);
2102 ret
= ustctl_create_event_notifier(app
->sock
, &event_notifier
,
2103 app
->event_notifier_group
.object
,
2104 &ua_event_notifier_rule
->obj
);
2105 pthread_mutex_unlock(&app
->sock_lock
);
2107 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2108 ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d",
2109 event_notifier
.event
.name
, app
->name
,
2113 * This is normal behavior, an application can die
2114 * during the creation process. Don't report an error so
2115 * the execution can continue normally.
2118 DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)",
2119 app
->name
, app
->ppid
);
2125 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2127 DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p",
2128 event_notifier
.event
.name
, app
->name
, app
->ppid
,
2129 ua_event_notifier_rule
->obj
);
2131 health_code_update();
2133 /* Set filter if one is present. */
2134 if (ua_event_notifier_rule
->filter
) {
2135 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2136 ua_event_notifier_rule
->obj
);
2142 /* Set exclusions for the event. */
2143 if (ua_event_notifier_rule
->exclusion
) {
2144 ret
= set_ust_object_exclusions(app
,
2145 ua_event_notifier_rule
->exclusion
,
2146 ua_event_notifier_rule
->obj
);
2152 /* Set the capture bytecodes. */
2153 cond_status
= lttng_condition_event_rule_get_capture_descriptor_count(
2154 condition
, &capture_bytecode_count
);
2155 assert(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2157 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2158 const struct lttng_bytecode
*capture_bytecode
=
2159 lttng_condition_event_rule_get_capture_bytecode_at_index(
2162 ret
= set_ust_capture(app
, capture_bytecode
,
2163 ua_event_notifier_rule
->obj
);
2170 * We now need to explicitly enable the event, since it
2171 * is disabled at creation.
2173 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2176 * If we hit an EPERM, something is wrong with our enable call.
2177 * If we get an EEXIST, there is a problem on the tracer side
2178 * since we just created it.
2181 case -LTTNG_UST_ERR_PERM
:
2182 /* Code flow problem. */
2184 case -LTTNG_UST_ERR_EXIST
:
2185 /* It's OK for our use case. */
2195 ua_event_notifier_rule
->enabled
= true;
2198 health_code_update();
2203 * Copy data between an UST app event and a LTT event.
2205 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2206 struct ltt_ust_event
*uevent
)
2208 size_t exclusion_alloc_size
;
2210 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2211 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2213 ua_event
->enabled
= uevent
->enabled
;
2215 /* Copy event attributes */
2216 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2218 /* Copy filter bytecode */
2219 if (uevent
->filter
) {
2220 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2221 /* Filter might be NULL here in case of ENONEM. */
2224 /* Copy exclusion data */
2225 if (uevent
->exclusion
) {
2226 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2227 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2228 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
2229 if (ua_event
->exclusion
== NULL
) {
2232 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2233 exclusion_alloc_size
);
2239 * Copy data between an UST app channel and a LTT channel.
2241 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2242 struct ltt_ust_channel
*uchan
)
2244 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2246 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2247 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2249 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2250 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2252 /* Copy event attributes since the layout is different. */
2253 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2254 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2255 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2256 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2257 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2258 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2259 ua_chan
->attr
.output
= uchan
->attr
.output
;
2260 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2263 * Note that the attribute channel type is not set since the channel on the
2264 * tracing registry side does not have this information.
2267 ua_chan
->enabled
= uchan
->enabled
;
2268 ua_chan
->tracing_channel_id
= uchan
->id
;
2270 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2274 * Copy data between a UST app session and a regular LTT session.
2276 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2277 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2279 struct tm
*timeinfo
;
2282 char tmp_shm_path
[PATH_MAX
];
2284 timeinfo
= localtime(&app
->registration_time
);
2285 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2287 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2289 ua_sess
->tracing_id
= usess
->id
;
2290 ua_sess
->id
= get_next_session_id();
2291 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2292 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2293 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2294 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2295 ua_sess
->buffer_type
= usess
->buffer_type
;
2296 ua_sess
->bits_per_long
= app
->bits_per_long
;
2298 /* There is only one consumer object per session possible. */
2299 consumer_output_get(usess
->consumer
);
2300 ua_sess
->consumer
= usess
->consumer
;
2302 ua_sess
->output_traces
= usess
->output_traces
;
2303 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2304 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2305 &usess
->metadata_attr
);
2307 switch (ua_sess
->buffer_type
) {
2308 case LTTNG_BUFFER_PER_PID
:
2309 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2310 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2313 case LTTNG_BUFFER_PER_UID
:
2314 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2315 DEFAULT_UST_TRACE_UID_PATH
,
2316 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2317 app
->bits_per_long
);
2324 PERROR("asprintf UST shadow copy session");
2329 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2330 sizeof(ua_sess
->root_shm_path
));
2331 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2332 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2333 sizeof(ua_sess
->shm_path
));
2334 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2335 if (ua_sess
->shm_path
[0]) {
2336 switch (ua_sess
->buffer_type
) {
2337 case LTTNG_BUFFER_PER_PID
:
2338 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2339 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2340 app
->name
, app
->pid
, datetime
);
2342 case LTTNG_BUFFER_PER_UID
:
2343 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2344 "/" DEFAULT_UST_TRACE_UID_PATH
,
2345 app
->uid
, app
->bits_per_long
);
2352 PERROR("sprintf UST shadow copy session");
2356 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2357 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2358 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2363 consumer_output_put(ua_sess
->consumer
);
2367 * Lookup sesison wrapper.
2370 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2371 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2373 /* Get right UST app session from app */
2374 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2378 * Return ust app session from the app session hashtable using the UST session
2381 static struct ust_app_session
*lookup_session_by_app(
2382 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2384 struct lttng_ht_iter iter
;
2385 struct lttng_ht_node_u64
*node
;
2387 __lookup_session_by_app(usess
, app
, &iter
);
2388 node
= lttng_ht_iter_get_node_u64(&iter
);
2393 return caa_container_of(node
, struct ust_app_session
, node
);
2400 * Setup buffer registry per PID for the given session and application. If none
2401 * is found, a new one is created, added to the global registry and
2402 * initialized. If regp is valid, it's set with the newly created object.
2404 * Return 0 on success or else a negative value.
2406 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2407 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2410 struct buffer_reg_pid
*reg_pid
;
2417 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2420 * This is the create channel path meaning that if there is NO
2421 * registry available, we have to create one for this session.
2423 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2424 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2432 /* Initialize registry. */
2433 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2434 app
->bits_per_long
, app
->uint8_t_alignment
,
2435 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2436 app
->uint64_t_alignment
, app
->long_alignment
,
2437 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2438 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2439 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2440 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2441 ua_sess
->tracing_id
,
2445 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2446 * destroy the buffer registry, because it is always expected
2447 * that if the buffer registry can be found, its ust registry is
2450 buffer_reg_pid_destroy(reg_pid
);
2454 buffer_reg_pid_add(reg_pid
);
2456 DBG3("UST app buffer registry per PID created successfully");
2468 * Setup buffer registry per UID for the given session and application. If none
2469 * is found, a new one is created, added to the global registry and
2470 * initialized. If regp is valid, it's set with the newly created object.
2472 * Return 0 on success or else a negative value.
2474 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2475 struct ust_app_session
*ua_sess
,
2476 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2479 struct buffer_reg_uid
*reg_uid
;
2486 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2489 * This is the create channel path meaning that if there is NO
2490 * registry available, we have to create one for this session.
2492 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2493 LTTNG_DOMAIN_UST
, ®_uid
,
2494 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2502 /* Initialize registry. */
2503 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2504 app
->bits_per_long
, app
->uint8_t_alignment
,
2505 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2506 app
->uint64_t_alignment
, app
->long_alignment
,
2507 app
->byte_order
, app
->version
.major
,
2508 app
->version
.minor
, reg_uid
->root_shm_path
,
2509 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2510 ua_sess
->tracing_id
, app
->uid
);
2513 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2514 * destroy the buffer registry, because it is always expected
2515 * that if the buffer registry can be found, its ust registry is
2518 buffer_reg_uid_destroy(reg_uid
, NULL
);
2521 /* Add node to teardown list of the session. */
2522 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2524 buffer_reg_uid_add(reg_uid
);
2526 DBG3("UST app buffer registry per UID created successfully");
2537 * Create a session on the tracer side for the given app.
2539 * On success, ua_sess_ptr is populated with the session pointer or else left
2540 * untouched. If the session was created, is_created is set to 1. On error,
2541 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2544 * Returns 0 on success or else a negative code which is either -ENOMEM or
2545 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2547 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2548 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2551 int ret
, created
= 0;
2552 struct ust_app_session
*ua_sess
;
2556 assert(ua_sess_ptr
);
2558 health_code_update();
2560 ua_sess
= lookup_session_by_app(usess
, app
);
2561 if (ua_sess
== NULL
) {
2562 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2563 app
->pid
, usess
->id
);
2564 ua_sess
= alloc_ust_app_session();
2565 if (ua_sess
== NULL
) {
2566 /* Only malloc can failed so something is really wrong */
2570 shadow_copy_session(ua_sess
, usess
, app
);
2574 switch (usess
->buffer_type
) {
2575 case LTTNG_BUFFER_PER_PID
:
2576 /* Init local registry. */
2577 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2579 delete_ust_app_session(-1, ua_sess
, app
);
2583 case LTTNG_BUFFER_PER_UID
:
2584 /* Look for a global registry. If none exists, create one. */
2585 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2587 delete_ust_app_session(-1, ua_sess
, app
);
2597 health_code_update();
2599 if (ua_sess
->handle
== -1) {
2600 pthread_mutex_lock(&app
->sock_lock
);
2601 ret
= ustctl_create_session(app
->sock
);
2602 pthread_mutex_unlock(&app
->sock_lock
);
2604 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2605 ERR("Creating session for app pid %d with ret %d",
2608 DBG("UST app creating session failed. Application is dead");
2610 * This is normal behavior, an application can die during the
2611 * creation process. Don't report an error so the execution can
2612 * continue normally. This will get flagged ENOTCONN and the
2613 * caller will handle it.
2617 delete_ust_app_session(-1, ua_sess
, app
);
2618 if (ret
!= -ENOMEM
) {
2620 * Tracer is probably gone or got an internal error so let's
2621 * behave like it will soon unregister or not usable.
2628 ua_sess
->handle
= ret
;
2630 /* Add ust app session to app's HT */
2631 lttng_ht_node_init_u64(&ua_sess
->node
,
2632 ua_sess
->tracing_id
);
2633 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2634 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2635 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2636 &ua_sess
->ust_objd_node
);
2638 DBG2("UST app session created successfully with handle %d", ret
);
2641 *ua_sess_ptr
= ua_sess
;
2643 *is_created
= created
;
2646 /* Everything went well. */
2650 health_code_update();
2655 * Match function for a hash table lookup of ust_app_ctx.
2657 * It matches an ust app context based on the context type and, in the case
2658 * of perf counters, their name.
2660 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2662 struct ust_app_ctx
*ctx
;
2663 const struct lttng_ust_context_attr
*key
;
2668 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2672 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2677 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2678 if (strncmp(key
->u
.perf_counter
.name
,
2679 ctx
->ctx
.u
.perf_counter
.name
,
2680 sizeof(key
->u
.perf_counter
.name
))) {
2684 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2685 if (strcmp(key
->u
.app_ctx
.provider_name
,
2686 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2687 strcmp(key
->u
.app_ctx
.ctx_name
,
2688 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2704 * Lookup for an ust app context from an lttng_ust_context.
2706 * Must be called while holding RCU read side lock.
2707 * Return an ust_app_ctx object or NULL on error.
2710 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2711 struct lttng_ust_context_attr
*uctx
)
2713 struct lttng_ht_iter iter
;
2714 struct lttng_ht_node_ulong
*node
;
2715 struct ust_app_ctx
*app_ctx
= NULL
;
2720 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2721 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2722 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2723 node
= lttng_ht_iter_get_node_ulong(&iter
);
2728 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2735 * Create a context for the channel on the tracer.
2737 * Called with UST app session lock held and a RCU read side lock.
2740 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2741 struct lttng_ust_context_attr
*uctx
,
2742 struct ust_app
*app
)
2745 struct ust_app_ctx
*ua_ctx
;
2747 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2749 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2755 ua_ctx
= alloc_ust_app_ctx(uctx
);
2756 if (ua_ctx
== NULL
) {
2762 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2763 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2764 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2766 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2776 * Enable on the tracer side a ust app event for the session and channel.
2778 * Called with UST app session lock held.
2781 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2782 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2786 ret
= enable_ust_object(app
, ua_event
->obj
);
2791 ua_event
->enabled
= 1;
2798 * Disable on the tracer side a ust app event for the session and channel.
2800 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2801 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2805 ret
= disable_ust_object(app
, ua_event
->obj
);
2810 ua_event
->enabled
= 0;
2817 * Lookup ust app channel for session and disable it on the tracer side.
2820 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2821 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2825 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2830 ua_chan
->enabled
= 0;
2837 * Lookup ust app channel for session and enable it on the tracer side. This
2838 * MUST be called with a RCU read side lock acquired.
2840 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2841 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2844 struct lttng_ht_iter iter
;
2845 struct lttng_ht_node_str
*ua_chan_node
;
2846 struct ust_app_channel
*ua_chan
;
2848 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2849 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2850 if (ua_chan_node
== NULL
) {
2851 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2852 uchan
->name
, ua_sess
->tracing_id
);
2856 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2858 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2868 * Ask the consumer to create a channel and get it if successful.
2870 * Called with UST app session lock held.
2872 * Return 0 on success or else a negative value.
2874 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2875 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2876 int bitness
, struct ust_registry_session
*registry
,
2877 uint64_t trace_archive_id
)
2880 unsigned int nb_fd
= 0;
2881 struct consumer_socket
*socket
;
2889 health_code_update();
2891 /* Get the right consumer socket for the application. */
2892 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2898 health_code_update();
2900 /* Need one fd for the channel. */
2901 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2903 ERR("Exhausted number of available FD upon create channel");
2908 * Ask consumer to create channel. The consumer will return the number of
2909 * stream we have to expect.
2911 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2912 registry
, usess
->current_trace_chunk
);
2918 * Compute the number of fd needed before receiving them. It must be 2 per
2919 * stream (2 being the default value here).
2921 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2923 /* Reserve the amount of file descriptor we need. */
2924 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2926 ERR("Exhausted number of available FD upon create channel");
2927 goto error_fd_get_stream
;
2930 health_code_update();
2933 * Now get the channel from the consumer. This call wil populate the stream
2934 * list of that channel and set the ust objects.
2936 if (usess
->consumer
->enabled
) {
2937 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2947 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2948 error_fd_get_stream
:
2950 * Initiate a destroy channel on the consumer since we had an error
2951 * handling it on our side. The return value is of no importance since we
2952 * already have a ret value set by the previous error that we need to
2955 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2957 lttng_fd_put(LTTNG_FD_APPS
, 1);
2959 health_code_update();
2965 * Duplicate the ust data object of the ust app stream and save it in the
2966 * buffer registry stream.
2968 * Return 0 on success or else a negative value.
2970 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2971 struct ust_app_stream
*stream
)
2978 /* Reserve the amount of file descriptor we need. */
2979 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2981 ERR("Exhausted number of available FD upon duplicate stream");
2985 /* Duplicate object for stream once the original is in the registry. */
2986 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2987 reg_stream
->obj
.ust
);
2989 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2990 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2991 lttng_fd_put(LTTNG_FD_APPS
, 2);
2994 stream
->handle
= stream
->obj
->handle
;
3001 * Duplicate the ust data object of the ust app. channel and save it in the
3002 * buffer registry channel.
3004 * Return 0 on success or else a negative value.
3006 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
3007 struct ust_app_channel
*ua_chan
)
3014 /* Need two fds for the channel. */
3015 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3017 ERR("Exhausted number of available FD upon duplicate channel");
3021 /* Duplicate object for stream once the original is in the registry. */
3022 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
3024 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3025 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
3028 ua_chan
->handle
= ua_chan
->obj
->handle
;
3033 lttng_fd_put(LTTNG_FD_APPS
, 1);
3039 * For a given channel buffer registry, setup all streams of the given ust
3040 * application channel.
3042 * Return 0 on success or else a negative value.
3044 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
3045 struct ust_app_channel
*ua_chan
,
3046 struct ust_app
*app
)
3049 struct ust_app_stream
*stream
, *stmp
;
3054 DBG2("UST app setup buffer registry stream");
3056 /* Send all streams to application. */
3057 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3058 struct buffer_reg_stream
*reg_stream
;
3060 ret
= buffer_reg_stream_create(®_stream
);
3066 * Keep original pointer and nullify it in the stream so the delete
3067 * stream call does not release the object.
3069 reg_stream
->obj
.ust
= stream
->obj
;
3071 buffer_reg_stream_add(reg_stream
, reg_chan
);
3073 /* We don't need the streams anymore. */
3074 cds_list_del(&stream
->list
);
3075 delete_ust_app_stream(-1, stream
, app
);
3083 * Create a buffer registry channel for the given session registry and
3084 * application channel object. If regp pointer is valid, it's set with the
3085 * created object. Important, the created object is NOT added to the session
3086 * registry hash table.
3088 * Return 0 on success else a negative value.
3090 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3091 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
3094 struct buffer_reg_channel
*reg_chan
= NULL
;
3099 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3101 /* Create buffer registry channel. */
3102 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
3107 reg_chan
->consumer_key
= ua_chan
->key
;
3108 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3109 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3111 /* Create and add a channel registry to session. */
3112 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
3113 ua_chan
->tracing_channel_id
);
3117 buffer_reg_channel_add(reg_sess
, reg_chan
);
3126 /* Safe because the registry channel object was not added to any HT. */
3127 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3133 * Setup buffer registry channel for the given session registry and application
3134 * channel object. If regp pointer is valid, it's set with the created object.
3136 * Return 0 on success else a negative value.
3138 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3139 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
3140 struct ust_app
*app
)
3147 assert(ua_chan
->obj
);
3149 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3151 /* Setup all streams for the registry. */
3152 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
3157 reg_chan
->obj
.ust
= ua_chan
->obj
;
3158 ua_chan
->obj
= NULL
;
3163 buffer_reg_channel_remove(reg_sess
, reg_chan
);
3164 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3169 * Send buffer registry channel to the application.
3171 * Return 0 on success else a negative value.
3173 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
3174 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
3175 struct ust_app_channel
*ua_chan
)
3178 struct buffer_reg_stream
*reg_stream
;
3185 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3187 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
3192 /* Send channel to the application. */
3193 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3194 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3195 ret
= -ENOTCONN
; /* Caused by app exiting. */
3197 } else if (ret
< 0) {
3201 health_code_update();
3203 /* Send all streams to application. */
3204 pthread_mutex_lock(®_chan
->stream_list_lock
);
3205 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
3206 struct ust_app_stream stream
;
3208 ret
= duplicate_stream_object(reg_stream
, &stream
);
3210 goto error_stream_unlock
;
3213 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3215 (void) release_ust_app_stream(-1, &stream
, app
);
3216 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3217 ret
= -ENOTCONN
; /* Caused by app exiting. */
3219 goto error_stream_unlock
;
3223 * The return value is not important here. This function will output an
3226 (void) release_ust_app_stream(-1, &stream
, app
);
3228 ua_chan
->is_sent
= 1;
3230 error_stream_unlock
:
3231 pthread_mutex_unlock(®_chan
->stream_list_lock
);
3237 * Create and send to the application the created buffers with per UID buffers.
3239 * This MUST be called with a RCU read side lock acquired.
3240 * The session list lock and the session's lock must be acquired.
3242 * Return 0 on success else a negative value.
3244 static int create_channel_per_uid(struct ust_app
*app
,
3245 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3246 struct ust_app_channel
*ua_chan
)
3249 struct buffer_reg_uid
*reg_uid
;
3250 struct buffer_reg_channel
*reg_chan
;
3251 struct ltt_session
*session
= NULL
;
3252 enum lttng_error_code notification_ret
;
3253 struct ust_registry_channel
*chan_reg
;
3260 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3262 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3264 * The session creation handles the creation of this global registry
3265 * object. If none can be find, there is a code flow problem or a
3270 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3276 /* Create the buffer registry channel object. */
3277 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
3279 ERR("Error creating the UST channel \"%s\" registry instance",
3284 session
= session_find_by_id(ua_sess
->tracing_id
);
3286 assert(pthread_mutex_trylock(&session
->lock
));
3287 assert(session_trylock_list());
3290 * Create the buffers on the consumer side. This call populates the
3291 * ust app channel object with all streams and data object.
3293 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3294 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
3295 session
->most_recent_chunk_id
.value
);
3297 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3301 * Let's remove the previously created buffer registry channel so
3302 * it's not visible anymore in the session registry.
3304 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3305 ua_chan
->tracing_channel_id
, false);
3306 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
3307 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3312 * Setup the streams and add it to the session registry.
3314 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3315 ua_chan
, reg_chan
, app
);
3317 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3321 /* Notify the notification subsystem of the channel's creation. */
3322 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
3323 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3324 ua_chan
->tracing_channel_id
);
3326 chan_reg
->consumer_key
= ua_chan
->key
;
3328 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
3330 notification_ret
= notification_thread_command_add_channel(
3331 notification_thread_handle
, session
->name
,
3332 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3333 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3335 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3336 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3337 if (notification_ret
!= LTTNG_OK
) {
3338 ret
= - (int) notification_ret
;
3339 ERR("Failed to add channel to notification thread");
3344 /* Send buffers to the application. */
3345 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
3347 if (ret
!= -ENOTCONN
) {
3348 ERR("Error sending channel to application");
3355 session_put(session
);
3361 * Create and send to the application the created buffers with per PID buffers.
3363 * Called with UST app session lock held.
3364 * The session list lock and the session's lock must be acquired.
3366 * Return 0 on success else a negative value.
3368 static int create_channel_per_pid(struct ust_app
*app
,
3369 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3370 struct ust_app_channel
*ua_chan
)
3373 struct ust_registry_session
*registry
;
3374 enum lttng_error_code cmd_ret
;
3375 struct ltt_session
*session
= NULL
;
3376 uint64_t chan_reg_key
;
3377 struct ust_registry_channel
*chan_reg
;
3384 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3388 registry
= get_session_registry(ua_sess
);
3389 /* The UST app session lock is held, registry shall not be null. */
3392 /* Create and add a new channel registry to session. */
3393 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3395 ERR("Error creating the UST channel \"%s\" registry instance",
3400 session
= session_find_by_id(ua_sess
->tracing_id
);
3403 assert(pthread_mutex_trylock(&session
->lock
));
3404 assert(session_trylock_list());
3406 /* Create and get channel on the consumer side. */
3407 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3408 app
->bits_per_long
, registry
,
3409 session
->most_recent_chunk_id
.value
);
3411 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3413 goto error_remove_from_registry
;
3416 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3418 if (ret
!= -ENOTCONN
) {
3419 ERR("Error sending channel to application");
3421 goto error_remove_from_registry
;
3424 chan_reg_key
= ua_chan
->key
;
3425 pthread_mutex_lock(®istry
->lock
);
3426 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3428 chan_reg
->consumer_key
= ua_chan
->key
;
3429 pthread_mutex_unlock(®istry
->lock
);
3431 cmd_ret
= notification_thread_command_add_channel(
3432 notification_thread_handle
, session
->name
,
3433 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3434 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3436 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3437 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3438 if (cmd_ret
!= LTTNG_OK
) {
3439 ret
= - (int) cmd_ret
;
3440 ERR("Failed to add channel to notification thread");
3441 goto error_remove_from_registry
;
3444 error_remove_from_registry
:
3446 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3451 session_put(session
);
3457 * From an already allocated ust app channel, create the channel buffers if
3458 * needed and send them to the application. This MUST be called with a RCU read
3459 * side lock acquired.
3461 * Called with UST app session lock held.
3463 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3464 * the application exited concurrently.
3466 static int ust_app_channel_send(struct ust_app
*app
,
3467 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3468 struct ust_app_channel
*ua_chan
)
3474 assert(usess
->active
);
3478 /* Handle buffer type before sending the channel to the application. */
3479 switch (usess
->buffer_type
) {
3480 case LTTNG_BUFFER_PER_UID
:
3482 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3488 case LTTNG_BUFFER_PER_PID
:
3490 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3502 /* Initialize ust objd object using the received handle and add it. */
3503 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3504 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3506 /* If channel is not enabled, disable it on the tracer */
3507 if (!ua_chan
->enabled
) {
3508 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3519 * Create UST app channel and return it through ua_chanp if not NULL.
3521 * Called with UST app session lock and RCU read-side lock held.
3523 * Return 0 on success or else a negative value.
3525 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3526 struct ltt_ust_channel
*uchan
,
3527 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3528 struct ust_app_channel
**ua_chanp
)
3531 struct lttng_ht_iter iter
;
3532 struct lttng_ht_node_str
*ua_chan_node
;
3533 struct ust_app_channel
*ua_chan
;
3535 /* Lookup channel in the ust app session */
3536 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3537 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3538 if (ua_chan_node
!= NULL
) {
3539 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3543 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3544 if (ua_chan
== NULL
) {
3545 /* Only malloc can fail here */
3549 shadow_copy_channel(ua_chan
, uchan
);
3551 /* Set channel type. */
3552 ua_chan
->attr
.type
= type
;
3554 /* Only add the channel if successful on the tracer side. */
3555 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3558 *ua_chanp
= ua_chan
;
3561 /* Everything went well. */
3569 * Create UST app event and create it on the tracer side.
3571 * Must be called with the RCU read side lock held.
3572 * Called with ust app session mutex held.
3575 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3576 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3577 struct ust_app
*app
)
3580 struct ust_app_event
*ua_event
;
3582 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3583 if (ua_event
== NULL
) {
3584 /* Only failure mode of alloc_ust_app_event(). */
3588 shadow_copy_event(ua_event
, uevent
);
3590 /* Create it on the tracer side */
3591 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3594 * Not found previously means that it does not exist on the
3595 * tracer. If the application reports that the event existed,
3596 * it means there is a bug in the sessiond or lttng-ust
3597 * (or corruption, etc.)
3599 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3600 ERR("Tracer for application reported that an event being created already existed: "
3601 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3603 app
->pid
, app
->ppid
, app
->uid
,
3609 add_unique_ust_app_event(ua_chan
, ua_event
);
3611 DBG2("UST app create event completed: app = '%s' (ppid: %d)",
3612 app
->name
, app
->ppid
);
3618 /* Valid. Calling here is already in a read side lock */
3619 delete_ust_app_event(-1, ua_event
, app
);
3624 * Create UST app event notifier rule and create it on the tracer side.
3626 * Must be called with the RCU read side lock held.
3627 * Called with ust app session mutex held.
3630 int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
,
3631 struct ust_app
*app
)
3634 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3636 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3637 if (ua_event_notifier_rule
== NULL
) {
3642 /* Create it on the tracer side. */
3643 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3646 * Not found previously means that it does not exist on the
3647 * tracer. If the application reports that the event existed,
3648 * it means there is a bug in the sessiond or lttng-ust
3649 * (or corruption, etc.)
3651 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3652 ERR("Tracer for application reported that an event notifier being created already exists: "
3653 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3654 lttng_trigger_get_tracer_token(trigger
),
3655 app
->pid
, app
->ppid
, app
->uid
,
3661 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3662 &ua_event_notifier_rule
->node
);
3664 DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64
,
3665 app
->name
, app
->ppid
, lttng_trigger_get_tracer_token(trigger
));
3671 /* The RCU read side lock is already being held by the caller. */
3672 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3677 * Create UST metadata and open it on the tracer side.
3679 * Called with UST app session lock held and RCU read side lock.
3681 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3682 struct ust_app
*app
, struct consumer_output
*consumer
)
3685 struct ust_app_channel
*metadata
;
3686 struct consumer_socket
*socket
;
3687 struct ust_registry_session
*registry
;
3688 struct ltt_session
*session
= NULL
;
3694 registry
= get_session_registry(ua_sess
);
3695 /* The UST app session is held registry shall not be null. */
3698 pthread_mutex_lock(®istry
->lock
);
3700 /* Metadata already exists for this registry or it was closed previously */
3701 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3706 /* Allocate UST metadata */
3707 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3709 /* malloc() failed */
3714 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3716 /* Need one fd for the channel. */
3717 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3719 ERR("Exhausted number of available FD upon create metadata");
3723 /* Get the right consumer socket for the application. */
3724 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3727 goto error_consumer
;
3731 * Keep metadata key so we can identify it on the consumer side. Assign it
3732 * to the registry *before* we ask the consumer so we avoid the race of the
3733 * consumer requesting the metadata and the ask_channel call on our side
3734 * did not returned yet.
3736 registry
->metadata_key
= metadata
->key
;
3738 session
= session_find_by_id(ua_sess
->tracing_id
);
3741 assert(pthread_mutex_trylock(&session
->lock
));
3742 assert(session_trylock_list());
3745 * Ask the metadata channel creation to the consumer. The metadata object
3746 * will be created by the consumer and kept their. However, the stream is
3747 * never added or monitored until we do a first push metadata to the
3750 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3751 registry
, session
->current_trace_chunk
);
3753 /* Nullify the metadata key so we don't try to close it later on. */
3754 registry
->metadata_key
= 0;
3755 goto error_consumer
;
3759 * The setup command will make the metadata stream be sent to the relayd,
3760 * if applicable, and the thread managing the metadatas. This is important
3761 * because after this point, if an error occurs, the only way the stream
3762 * can be deleted is to be monitored in the consumer.
3764 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3766 /* Nullify the metadata key so we don't try to close it later on. */
3767 registry
->metadata_key
= 0;
3768 goto error_consumer
;
3771 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3772 metadata
->key
, app
->pid
);
3775 lttng_fd_put(LTTNG_FD_APPS
, 1);
3776 delete_ust_app_channel(-1, metadata
, app
);
3778 pthread_mutex_unlock(®istry
->lock
);
3780 session_put(session
);
3786 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3787 * acquired before calling this function.
3789 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3791 struct ust_app
*app
= NULL
;
3792 struct lttng_ht_node_ulong
*node
;
3793 struct lttng_ht_iter iter
;
3795 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3796 node
= lttng_ht_iter_get_node_ulong(&iter
);
3798 DBG2("UST app no found with pid %d", pid
);
3802 DBG2("Found UST app by pid %d", pid
);
3804 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3811 * Allocate and init an UST app object using the registration information and
3812 * the command socket. This is called when the command socket connects to the
3815 * The object is returned on success or else NULL.
3817 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3820 struct ust_app
*lta
= NULL
;
3821 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3826 DBG3("UST app creating application for socket %d", sock
);
3828 if ((msg
->bits_per_long
== 64 &&
3829 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3830 || (msg
->bits_per_long
== 32 &&
3831 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3832 ERR("Registration failed: application \"%s\" (pid: %d) has "
3833 "%d-bit long, but no consumerd for this size is available.\n",
3834 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3839 * Reserve the two file descriptors of the event source pipe. The write
3840 * end will be closed once it is passed to the application, at which
3841 * point a single 'put' will be performed.
3843 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3845 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s' (ppid: %d)",
3846 msg
->name
, (int) msg
->ppid
);
3850 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3851 if (!event_notifier_event_source_pipe
) {
3852 PERROR("Failed to open application event source pipe: '%s' (ppid = %d)",
3853 msg
->name
, msg
->ppid
);
3857 lta
= zmalloc(sizeof(struct ust_app
));
3860 goto error_free_pipe
;
3863 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
3865 lta
->ppid
= msg
->ppid
;
3866 lta
->uid
= msg
->uid
;
3867 lta
->gid
= msg
->gid
;
3869 lta
->bits_per_long
= msg
->bits_per_long
;
3870 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3871 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3872 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3873 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3874 lta
->long_alignment
= msg
->long_alignment
;
3875 lta
->byte_order
= msg
->byte_order
;
3877 lta
->v_major
= msg
->major
;
3878 lta
->v_minor
= msg
->minor
;
3879 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3880 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3881 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3882 lta
->notify_sock
= -1;
3883 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3885 /* Copy name and make sure it's NULL terminated. */
3886 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3887 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3890 * Before this can be called, when receiving the registration information,
3891 * the application compatibility is checked. So, at this point, the
3892 * application can work with this session daemon.
3894 lta
->compatible
= 1;
3896 lta
->pid
= msg
->pid
;
3897 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3899 pthread_mutex_init(<a
->sock_lock
, NULL
);
3900 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3902 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3906 lttng_pipe_destroy(event_notifier_event_source_pipe
);
3907 lttng_fd_put(LTTNG_FD_APPS
, 2);
3913 * For a given application object, add it to every hash table.
3915 void ust_app_add(struct ust_app
*app
)
3918 assert(app
->notify_sock
>= 0);
3920 app
->registration_time
= time(NULL
);
3925 * On a re-registration, we want to kick out the previous registration of
3928 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3931 * The socket _should_ be unique until _we_ call close. So, a add_unique
3932 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3933 * already in the table.
3935 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3937 /* Add application to the notify socket hash table. */
3938 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3939 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3941 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3942 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3943 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3950 * Set the application version into the object.
3952 * Return 0 on success else a negative value either an errno code or a
3953 * LTTng-UST error code.
3955 int ust_app_version(struct ust_app
*app
)
3961 pthread_mutex_lock(&app
->sock_lock
);
3962 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3963 pthread_mutex_unlock(&app
->sock_lock
);
3965 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3966 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3968 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3976 * Setup the base event notifier group.
3978 * Return 0 on success else a negative value either an errno code or a
3979 * LTTng-UST error code.
3981 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
3984 int event_pipe_write_fd
;
3985 struct lttng_ust_object_data
*event_notifier_group
= NULL
;
3986 enum lttng_error_code lttng_ret
;
3990 /* Get the write side of the pipe. */
3991 event_pipe_write_fd
= lttng_pipe_get_writefd(
3992 app
->event_notifier_group
.event_pipe
);
3994 pthread_mutex_lock(&app
->sock_lock
);
3995 ret
= ustctl_create_event_notifier_group(app
->sock
,
3996 event_pipe_write_fd
, &event_notifier_group
);
3997 pthread_mutex_unlock(&app
->sock_lock
);
3999 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4000 ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d",
4001 ret
, app
->sock
, event_pipe_write_fd
);
4003 DBG("Failed to create application event notifier group (application is dead): app socket fd = %d",
4010 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4012 ERR("Failed to close write end of the application's event source pipe: app = '%s' (ppid = %d)",
4013 app
->name
, app
->ppid
);
4018 * Release the file descriptor that was reserved for the write-end of
4021 lttng_fd_put(LTTNG_FD_APPS
, 1);
4023 lttng_ret
= notification_thread_command_add_tracer_event_source(
4024 notification_thread_handle
,
4025 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
),
4027 if (lttng_ret
!= LTTNG_OK
) {
4028 ERR("Failed to add tracer event source to notification thread");
4033 /* Assign handle only when the complete setup is valid. */
4034 app
->event_notifier_group
.object
= event_notifier_group
;
4038 ustctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4039 free(app
->event_notifier_group
.object
);
4044 * Unregister app by removing it from the global traceable app list and freeing
4047 * The socket is already closed at this point so no close to sock.
4049 void ust_app_unregister(int sock
)
4051 struct ust_app
*lta
;
4052 struct lttng_ht_node_ulong
*node
;
4053 struct lttng_ht_iter ust_app_sock_iter
;
4054 struct lttng_ht_iter iter
;
4055 struct ust_app_session
*ua_sess
;
4060 /* Get the node reference for a call_rcu */
4061 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
4062 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4065 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
4066 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4069 * For per-PID buffers, perform "push metadata" and flush all
4070 * application streams before removing app from hash tables,
4071 * ensuring proper behavior of data_pending check.
4072 * Remove sessions so they are not visible during deletion.
4074 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
4076 struct ust_registry_session
*registry
;
4078 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4080 /* The session was already removed so scheduled for teardown. */
4084 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4085 (void) ust_app_flush_app_session(lta
, ua_sess
);
4089 * Add session to list for teardown. This is safe since at this point we
4090 * are the only one using this list.
4092 pthread_mutex_lock(&ua_sess
->lock
);
4094 if (ua_sess
->deleted
) {
4095 pthread_mutex_unlock(&ua_sess
->lock
);
4100 * Normally, this is done in the delete session process which is
4101 * executed in the call rcu below. However, upon registration we can't
4102 * afford to wait for the grace period before pushing data or else the
4103 * data pending feature can race between the unregistration and stop
4104 * command where the data pending command is sent *before* the grace
4107 * The close metadata below nullifies the metadata pointer in the
4108 * session so the delete session will NOT push/close a second time.
4110 registry
= get_session_registry(ua_sess
);
4112 /* Push metadata for application before freeing the application. */
4113 (void) push_metadata(registry
, ua_sess
->consumer
);
4116 * Don't ask to close metadata for global per UID buffers. Close
4117 * metadata only on destroy trace session in this case. Also, the
4118 * previous push metadata could have flag the metadata registry to
4119 * close so don't send a close command if closed.
4121 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4122 /* And ask to close it for this session registry. */
4123 (void) close_metadata(registry
, ua_sess
->consumer
);
4126 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4128 pthread_mutex_unlock(&ua_sess
->lock
);
4131 /* Remove application from PID hash table */
4132 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4136 * Remove application from notify hash table. The thread handling the
4137 * notify socket could have deleted the node so ignore on error because
4138 * either way it's valid. The close of that socket is handled by the
4139 * apps_notify_thread.
4141 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4142 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4145 * Ignore return value since the node might have been removed before by an
4146 * add replace during app registration because the PID can be reassigned by
4149 iter
.iter
.node
= <a
->pid_n
.node
;
4150 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4152 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4157 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4164 * Fill events array with all events name of all registered apps.
4166 int ust_app_list_events(struct lttng_event
**events
)
4169 size_t nbmem
, count
= 0;
4170 struct lttng_ht_iter iter
;
4171 struct ust_app
*app
;
4172 struct lttng_event
*tmp_event
;
4174 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4175 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
4176 if (tmp_event
== NULL
) {
4177 PERROR("zmalloc ust app events");
4184 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4185 struct lttng_ust_tracepoint_iter uiter
;
4187 health_code_update();
4189 if (!app
->compatible
) {
4191 * TODO: In time, we should notice the caller of this error by
4192 * telling him that this is a version error.
4196 pthread_mutex_lock(&app
->sock_lock
);
4197 handle
= ustctl_tracepoint_list(app
->sock
);
4199 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4200 ERR("UST app list events getting handle failed for app pid %d",
4203 pthread_mutex_unlock(&app
->sock_lock
);
4207 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
4208 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4209 /* Handle ustctl error. */
4213 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4214 ERR("UST app tp list get failed for app %d with ret %d",
4217 DBG3("UST app tp list get failed. Application is dead");
4219 * This is normal behavior, an application can die during the
4220 * creation process. Don't report an error so the execution can
4221 * continue normally. Continue normal execution.
4226 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4227 if (release_ret
< 0 &&
4228 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4229 release_ret
!= -EPIPE
) {
4230 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4232 pthread_mutex_unlock(&app
->sock_lock
);
4236 health_code_update();
4237 if (count
>= nbmem
) {
4238 /* In case the realloc fails, we free the memory */
4239 struct lttng_event
*new_tmp_event
;
4242 new_nbmem
= nbmem
<< 1;
4243 DBG2("Reallocating event list from %zu to %zu entries",
4245 new_tmp_event
= realloc(tmp_event
,
4246 new_nbmem
* sizeof(struct lttng_event
));
4247 if (new_tmp_event
== NULL
) {
4250 PERROR("realloc ust app events");
4253 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4254 if (release_ret
< 0 &&
4255 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4256 release_ret
!= -EPIPE
) {
4257 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4259 pthread_mutex_unlock(&app
->sock_lock
);
4262 /* Zero the new memory */
4263 memset(new_tmp_event
+ nbmem
, 0,
4264 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4266 tmp_event
= new_tmp_event
;
4268 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
4269 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4270 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
4271 tmp_event
[count
].pid
= app
->pid
;
4272 tmp_event
[count
].enabled
= -1;
4275 ret
= ustctl_release_handle(app
->sock
, handle
);
4276 pthread_mutex_unlock(&app
->sock_lock
);
4277 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4278 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4283 *events
= tmp_event
;
4285 DBG2("UST app list events done (%zu events)", count
);
4290 health_code_update();
4295 * Fill events array with all events name of all registered apps.
4297 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4300 size_t nbmem
, count
= 0;
4301 struct lttng_ht_iter iter
;
4302 struct ust_app
*app
;
4303 struct lttng_event_field
*tmp_event
;
4305 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4306 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
4307 if (tmp_event
== NULL
) {
4308 PERROR("zmalloc ust app event fields");
4315 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4316 struct lttng_ust_field_iter uiter
;
4318 health_code_update();
4320 if (!app
->compatible
) {
4322 * TODO: In time, we should notice the caller of this error by
4323 * telling him that this is a version error.
4327 pthread_mutex_lock(&app
->sock_lock
);
4328 handle
= ustctl_tracepoint_field_list(app
->sock
);
4330 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4331 ERR("UST app list field getting handle failed for app pid %d",
4334 pthread_mutex_unlock(&app
->sock_lock
);
4338 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
4339 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4340 /* Handle ustctl error. */
4344 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4345 ERR("UST app tp list field failed for app %d with ret %d",
4348 DBG3("UST app tp list field failed. Application is dead");
4350 * This is normal behavior, an application can die during the
4351 * creation process. Don't report an error so the execution can
4352 * continue normally. Reset list and count for next app.
4357 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4358 pthread_mutex_unlock(&app
->sock_lock
);
4359 if (release_ret
< 0 &&
4360 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4361 release_ret
!= -EPIPE
) {
4362 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4367 health_code_update();
4368 if (count
>= nbmem
) {
4369 /* In case the realloc fails, we free the memory */
4370 struct lttng_event_field
*new_tmp_event
;
4373 new_nbmem
= nbmem
<< 1;
4374 DBG2("Reallocating event field list from %zu to %zu entries",
4376 new_tmp_event
= realloc(tmp_event
,
4377 new_nbmem
* sizeof(struct lttng_event_field
));
4378 if (new_tmp_event
== NULL
) {
4381 PERROR("realloc ust app event fields");
4384 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4385 pthread_mutex_unlock(&app
->sock_lock
);
4387 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4388 release_ret
!= -EPIPE
) {
4389 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4393 /* Zero the new memory */
4394 memset(new_tmp_event
+ nbmem
, 0,
4395 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4397 tmp_event
= new_tmp_event
;
4400 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
4401 /* Mapping between these enums matches 1 to 1. */
4402 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4403 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4405 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
4406 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4407 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4408 tmp_event
[count
].event
.pid
= app
->pid
;
4409 tmp_event
[count
].event
.enabled
= -1;
4412 ret
= ustctl_release_handle(app
->sock
, handle
);
4413 pthread_mutex_unlock(&app
->sock_lock
);
4415 ret
!= -LTTNG_UST_ERR_EXITING
&&
4417 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4422 *fields
= tmp_event
;
4424 DBG2("UST app list event fields done (%zu events)", count
);
4429 health_code_update();
4434 * Free and clean all traceable apps of the global list.
4436 * Should _NOT_ be called with RCU read-side lock held.
4438 void ust_app_clean_list(void)
4441 struct ust_app
*app
;
4442 struct lttng_ht_iter iter
;
4444 DBG2("UST app cleaning registered apps hash table");
4448 /* Cleanup notify socket hash table */
4449 if (ust_app_ht_by_notify_sock
) {
4450 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4451 notify_sock_n
.node
) {
4453 * Assert that all notifiers are gone as all triggers
4454 * are unregistered prior to this clean-up.
4456 assert(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4458 ust_app_notify_sock_unregister(app
->notify_sock
);
4463 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4464 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4466 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4470 /* Cleanup socket hash table */
4471 if (ust_app_ht_by_sock
) {
4472 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4474 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4481 /* Destroy is done only when the ht is empty */
4483 ht_cleanup_push(ust_app_ht
);
4485 if (ust_app_ht_by_sock
) {
4486 ht_cleanup_push(ust_app_ht_by_sock
);
4488 if (ust_app_ht_by_notify_sock
) {
4489 ht_cleanup_push(ust_app_ht_by_notify_sock
);
4494 * Init UST app hash table.
4496 int ust_app_ht_alloc(void)
4498 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4502 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4503 if (!ust_app_ht_by_sock
) {
4506 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4507 if (!ust_app_ht_by_notify_sock
) {
4514 * For a specific UST session, disable the channel for all registered apps.
4516 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4517 struct ltt_ust_channel
*uchan
)
4520 struct lttng_ht_iter iter
;
4521 struct lttng_ht_node_str
*ua_chan_node
;
4522 struct ust_app
*app
;
4523 struct ust_app_session
*ua_sess
;
4524 struct ust_app_channel
*ua_chan
;
4526 assert(usess
->active
);
4527 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4528 uchan
->name
, usess
->id
);
4532 /* For every registered applications */
4533 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4534 struct lttng_ht_iter uiter
;
4535 if (!app
->compatible
) {
4537 * TODO: In time, we should notice the caller of this error by
4538 * telling him that this is a version error.
4542 ua_sess
= lookup_session_by_app(usess
, app
);
4543 if (ua_sess
== NULL
) {
4548 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4549 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4550 /* If the session if found for the app, the channel must be there */
4551 assert(ua_chan_node
);
4553 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4554 /* The channel must not be already disabled */
4555 assert(ua_chan
->enabled
== 1);
4557 /* Disable channel onto application */
4558 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4560 /* XXX: We might want to report this error at some point... */
4570 * For a specific UST session, enable the channel for all registered apps.
4572 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4573 struct ltt_ust_channel
*uchan
)
4576 struct lttng_ht_iter iter
;
4577 struct ust_app
*app
;
4578 struct ust_app_session
*ua_sess
;
4580 assert(usess
->active
);
4581 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4582 uchan
->name
, usess
->id
);
4586 /* For every registered applications */
4587 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4588 if (!app
->compatible
) {
4590 * TODO: In time, we should notice the caller of this error by
4591 * telling him that this is a version error.
4595 ua_sess
= lookup_session_by_app(usess
, app
);
4596 if (ua_sess
== NULL
) {
4600 /* Enable channel onto application */
4601 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4603 /* XXX: We might want to report this error at some point... */
4613 * Disable an event in a channel and for a specific session.
4615 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4616 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4619 struct lttng_ht_iter iter
, uiter
;
4620 struct lttng_ht_node_str
*ua_chan_node
;
4621 struct ust_app
*app
;
4622 struct ust_app_session
*ua_sess
;
4623 struct ust_app_channel
*ua_chan
;
4624 struct ust_app_event
*ua_event
;
4626 assert(usess
->active
);
4627 DBG("UST app disabling event %s for all apps in channel "
4628 "%s for session id %" PRIu64
,
4629 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4633 /* For all registered applications */
4634 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4635 if (!app
->compatible
) {
4637 * TODO: In time, we should notice the caller of this error by
4638 * telling him that this is a version error.
4642 ua_sess
= lookup_session_by_app(usess
, app
);
4643 if (ua_sess
== NULL
) {
4648 /* Lookup channel in the ust app session */
4649 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4650 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4651 if (ua_chan_node
== NULL
) {
4652 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4653 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4656 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4658 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4659 uevent
->filter
, uevent
->attr
.loglevel
,
4661 if (ua_event
== NULL
) {
4662 DBG2("Event %s not found in channel %s for app pid %d."
4663 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4667 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4669 /* XXX: Report error someday... */
4678 /* The ua_sess lock must be held by the caller. */
4680 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4681 struct ust_app_session
*ua_sess
,
4682 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4683 struct ust_app_channel
**_ua_chan
)
4686 struct ust_app_channel
*ua_chan
= NULL
;
4689 ASSERT_LOCKED(ua_sess
->lock
);
4691 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4692 sizeof(uchan
->name
))) {
4693 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4697 struct ltt_ust_context
*uctx
= NULL
;
4700 * Create channel onto application and synchronize its
4703 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4704 LTTNG_UST_CHAN_PER_CPU
, usess
,
4710 ret
= ust_app_channel_send(app
, usess
,
4717 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4718 ret
= create_ust_app_channel_context(ua_chan
,
4731 * The application's socket is not valid. Either a bad socket
4732 * or a timeout on it. We can't inform the caller that for a
4733 * specific app, the session failed so lets continue here.
4735 ret
= 0; /* Not an error. */
4743 if (ret
== 0 && _ua_chan
) {
4745 * Only return the application's channel on success. Note
4746 * that the channel can still be part of the application's
4747 * channel hashtable on error.
4749 *_ua_chan
= ua_chan
;
4755 * Enable event for a specific session and channel on the tracer.
4757 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4758 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4761 struct lttng_ht_iter iter
, uiter
;
4762 struct lttng_ht_node_str
*ua_chan_node
;
4763 struct ust_app
*app
;
4764 struct ust_app_session
*ua_sess
;
4765 struct ust_app_channel
*ua_chan
;
4766 struct ust_app_event
*ua_event
;
4768 assert(usess
->active
);
4769 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4770 uevent
->attr
.name
, usess
->id
);
4773 * NOTE: At this point, this function is called only if the session and
4774 * channel passed are already created for all apps. and enabled on the
4780 /* For all registered applications */
4781 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4782 if (!app
->compatible
) {
4784 * TODO: In time, we should notice the caller of this error by
4785 * telling him that this is a version error.
4789 ua_sess
= lookup_session_by_app(usess
, app
);
4791 /* The application has problem or is probably dead. */
4795 pthread_mutex_lock(&ua_sess
->lock
);
4797 if (ua_sess
->deleted
) {
4798 pthread_mutex_unlock(&ua_sess
->lock
);
4802 /* Lookup channel in the ust app session */
4803 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4804 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4806 * It is possible that the channel cannot be found is
4807 * the channel/event creation occurs concurrently with
4808 * an application exit.
4810 if (!ua_chan_node
) {
4811 pthread_mutex_unlock(&ua_sess
->lock
);
4815 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4817 /* Get event node */
4818 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4819 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4820 if (ua_event
== NULL
) {
4821 DBG3("UST app enable event %s not found for app PID %d."
4822 "Skipping app", uevent
->attr
.name
, app
->pid
);
4826 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4828 pthread_mutex_unlock(&ua_sess
->lock
);
4832 pthread_mutex_unlock(&ua_sess
->lock
);
4841 * For a specific existing UST session and UST channel, creates the event for
4842 * all registered apps.
4844 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4845 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4848 struct lttng_ht_iter iter
, uiter
;
4849 struct lttng_ht_node_str
*ua_chan_node
;
4850 struct ust_app
*app
;
4851 struct ust_app_session
*ua_sess
;
4852 struct ust_app_channel
*ua_chan
;
4854 assert(usess
->active
);
4855 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4856 uevent
->attr
.name
, usess
->id
);
4860 /* For all registered applications */
4861 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4862 if (!app
->compatible
) {
4864 * TODO: In time, we should notice the caller of this error by
4865 * telling him that this is a version error.
4869 ua_sess
= lookup_session_by_app(usess
, app
);
4871 /* The application has problem or is probably dead. */
4875 pthread_mutex_lock(&ua_sess
->lock
);
4877 if (ua_sess
->deleted
) {
4878 pthread_mutex_unlock(&ua_sess
->lock
);
4882 /* Lookup channel in the ust app session */
4883 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4884 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4885 /* If the channel is not found, there is a code flow error */
4886 assert(ua_chan_node
);
4888 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4890 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4891 pthread_mutex_unlock(&ua_sess
->lock
);
4893 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4894 /* Possible value at this point: -ENOMEM. If so, we stop! */
4897 DBG2("UST app event %s already exist on app PID %d",
4898 uevent
->attr
.name
, app
->pid
);
4908 * Start tracing for a specific UST session and app.
4910 * Called with UST app session lock held.
4914 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4917 struct ust_app_session
*ua_sess
;
4919 DBG("Starting tracing for ust app pid %d", app
->pid
);
4923 if (!app
->compatible
) {
4927 ua_sess
= lookup_session_by_app(usess
, app
);
4928 if (ua_sess
== NULL
) {
4929 /* The session is in teardown process. Ignore and continue. */
4933 pthread_mutex_lock(&ua_sess
->lock
);
4935 if (ua_sess
->deleted
) {
4936 pthread_mutex_unlock(&ua_sess
->lock
);
4940 if (ua_sess
->enabled
) {
4941 pthread_mutex_unlock(&ua_sess
->lock
);
4945 /* Upon restart, we skip the setup, already done */
4946 if (ua_sess
->started
) {
4950 health_code_update();
4953 /* This starts the UST tracing */
4954 pthread_mutex_lock(&app
->sock_lock
);
4955 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4956 pthread_mutex_unlock(&app
->sock_lock
);
4958 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4959 ERR("Error starting tracing for app pid: %d (ret: %d)",
4962 DBG("UST app start session failed. Application is dead.");
4964 * This is normal behavior, an application can die during the
4965 * creation process. Don't report an error so the execution can
4966 * continue normally.
4968 pthread_mutex_unlock(&ua_sess
->lock
);
4974 /* Indicate that the session has been started once */
4975 ua_sess
->started
= 1;
4976 ua_sess
->enabled
= 1;
4978 pthread_mutex_unlock(&ua_sess
->lock
);
4980 health_code_update();
4982 /* Quiescent wait after starting trace */
4983 pthread_mutex_lock(&app
->sock_lock
);
4984 ret
= ustctl_wait_quiescent(app
->sock
);
4985 pthread_mutex_unlock(&app
->sock_lock
);
4986 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4987 ERR("UST app wait quiescent failed for app pid %d ret %d",
4993 health_code_update();
4997 pthread_mutex_unlock(&ua_sess
->lock
);
4999 health_code_update();
5004 * Stop tracing for a specific UST session and app.
5007 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5010 struct ust_app_session
*ua_sess
;
5011 struct ust_registry_session
*registry
;
5013 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5017 if (!app
->compatible
) {
5018 goto end_no_session
;
5021 ua_sess
= lookup_session_by_app(usess
, app
);
5022 if (ua_sess
== NULL
) {
5023 goto end_no_session
;
5026 pthread_mutex_lock(&ua_sess
->lock
);
5028 if (ua_sess
->deleted
) {
5029 pthread_mutex_unlock(&ua_sess
->lock
);
5030 goto end_no_session
;
5034 * If started = 0, it means that stop trace has been called for a session
5035 * that was never started. It's possible since we can have a fail start
5036 * from either the application manager thread or the command thread. Simply
5037 * indicate that this is a stop error.
5039 if (!ua_sess
->started
) {
5040 goto error_rcu_unlock
;
5043 health_code_update();
5045 /* This inhibits UST tracing */
5046 pthread_mutex_lock(&app
->sock_lock
);
5047 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
5048 pthread_mutex_unlock(&app
->sock_lock
);
5050 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5051 ERR("Error stopping tracing for app pid: %d (ret: %d)",
5054 DBG("UST app stop session failed. Application is dead.");
5056 * This is normal behavior, an application can die during the
5057 * creation process. Don't report an error so the execution can
5058 * continue normally.
5062 goto error_rcu_unlock
;
5065 health_code_update();
5066 ua_sess
->enabled
= 0;
5068 /* Quiescent wait after stopping trace */
5069 pthread_mutex_lock(&app
->sock_lock
);
5070 ret
= ustctl_wait_quiescent(app
->sock
);
5071 pthread_mutex_unlock(&app
->sock_lock
);
5072 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5073 ERR("UST app wait quiescent failed for app pid %d ret %d",
5077 health_code_update();
5079 registry
= get_session_registry(ua_sess
);
5081 /* The UST app session is held registry shall not be null. */
5084 /* Push metadata for application before freeing the application. */
5085 (void) push_metadata(registry
, ua_sess
->consumer
);
5088 pthread_mutex_unlock(&ua_sess
->lock
);
5091 health_code_update();
5095 pthread_mutex_unlock(&ua_sess
->lock
);
5097 health_code_update();
5102 int ust_app_flush_app_session(struct ust_app
*app
,
5103 struct ust_app_session
*ua_sess
)
5105 int ret
, retval
= 0;
5106 struct lttng_ht_iter iter
;
5107 struct ust_app_channel
*ua_chan
;
5108 struct consumer_socket
*socket
;
5110 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5114 if (!app
->compatible
) {
5115 goto end_not_compatible
;
5118 pthread_mutex_lock(&ua_sess
->lock
);
5120 if (ua_sess
->deleted
) {
5124 health_code_update();
5126 /* Flushing buffers */
5127 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5130 /* Flush buffers and push metadata. */
5131 switch (ua_sess
->buffer_type
) {
5132 case LTTNG_BUFFER_PER_PID
:
5133 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5135 health_code_update();
5136 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5138 ERR("Error flushing consumer channel");
5144 case LTTNG_BUFFER_PER_UID
:
5150 health_code_update();
5153 pthread_mutex_unlock(&ua_sess
->lock
);
5157 health_code_update();
5162 * Flush buffers for all applications for a specific UST session.
5163 * Called with UST session lock held.
5166 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5171 DBG("Flushing session buffers for all ust apps");
5175 /* Flush buffers and push metadata. */
5176 switch (usess
->buffer_type
) {
5177 case LTTNG_BUFFER_PER_UID
:
5179 struct buffer_reg_uid
*reg
;
5180 struct lttng_ht_iter iter
;
5182 /* Flush all per UID buffers associated to that session. */
5183 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5184 struct ust_registry_session
*ust_session_reg
;
5185 struct buffer_reg_channel
*reg_chan
;
5186 struct consumer_socket
*socket
;
5188 /* Get consumer socket to use to push the metadata.*/
5189 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5192 /* Ignore request if no consumer is found for the session. */
5196 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5197 reg_chan
, node
.node
) {
5199 * The following call will print error values so the return
5200 * code is of little importance because whatever happens, we
5201 * have to try them all.
5203 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
5206 ust_session_reg
= reg
->registry
->reg
.ust
;
5207 /* Push metadata. */
5208 (void) push_metadata(ust_session_reg
, usess
->consumer
);
5212 case LTTNG_BUFFER_PER_PID
:
5214 struct ust_app_session
*ua_sess
;
5215 struct lttng_ht_iter iter
;
5216 struct ust_app
*app
;
5218 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5219 ua_sess
= lookup_session_by_app(usess
, app
);
5220 if (ua_sess
== NULL
) {
5223 (void) ust_app_flush_app_session(app
, ua_sess
);
5234 health_code_update();
5239 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5240 struct ust_app_session
*ua_sess
)
5243 struct lttng_ht_iter iter
;
5244 struct ust_app_channel
*ua_chan
;
5245 struct consumer_socket
*socket
;
5247 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5251 if (!app
->compatible
) {
5252 goto end_not_compatible
;
5255 pthread_mutex_lock(&ua_sess
->lock
);
5257 if (ua_sess
->deleted
) {
5261 health_code_update();
5263 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5266 ERR("Failed to find consumer (%" PRIu32
") socket",
5267 app
->bits_per_long
);
5272 /* Clear quiescent state. */
5273 switch (ua_sess
->buffer_type
) {
5274 case LTTNG_BUFFER_PER_PID
:
5275 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5276 ua_chan
, node
.node
) {
5277 health_code_update();
5278 ret
= consumer_clear_quiescent_channel(socket
,
5281 ERR("Error clearing quiescent state for consumer channel");
5287 case LTTNG_BUFFER_PER_UID
:
5294 health_code_update();
5297 pthread_mutex_unlock(&ua_sess
->lock
);
5301 health_code_update();
5306 * Clear quiescent state in each stream for all applications for a
5307 * specific UST session.
5308 * Called with UST session lock held.
5311 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5316 DBG("Clearing stream quiescent state for all ust apps");
5320 switch (usess
->buffer_type
) {
5321 case LTTNG_BUFFER_PER_UID
:
5323 struct lttng_ht_iter iter
;
5324 struct buffer_reg_uid
*reg
;
5327 * Clear quiescent for all per UID buffers associated to
5330 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5331 struct consumer_socket
*socket
;
5332 struct buffer_reg_channel
*reg_chan
;
5334 /* Get associated consumer socket.*/
5335 socket
= consumer_find_socket_by_bitness(
5336 reg
->bits_per_long
, usess
->consumer
);
5339 * Ignore request if no consumer is found for
5345 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5346 &iter
.iter
, reg_chan
, node
.node
) {
5348 * The following call will print error values so
5349 * the return code is of little importance
5350 * because whatever happens, we have to try them
5353 (void) consumer_clear_quiescent_channel(socket
,
5354 reg_chan
->consumer_key
);
5359 case LTTNG_BUFFER_PER_PID
:
5361 struct ust_app_session
*ua_sess
;
5362 struct lttng_ht_iter iter
;
5363 struct ust_app
*app
;
5365 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5367 ua_sess
= lookup_session_by_app(usess
, app
);
5368 if (ua_sess
== NULL
) {
5371 (void) ust_app_clear_quiescent_app_session(app
,
5383 health_code_update();
5388 * Destroy a specific UST session in apps.
5390 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5393 struct ust_app_session
*ua_sess
;
5394 struct lttng_ht_iter iter
;
5395 struct lttng_ht_node_u64
*node
;
5397 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5401 if (!app
->compatible
) {
5405 __lookup_session_by_app(usess
, app
, &iter
);
5406 node
= lttng_ht_iter_get_node_u64(&iter
);
5408 /* Session is being or is deleted. */
5411 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
5413 health_code_update();
5414 destroy_app_session(app
, ua_sess
);
5416 health_code_update();
5418 /* Quiescent wait after stopping trace */
5419 pthread_mutex_lock(&app
->sock_lock
);
5420 ret
= ustctl_wait_quiescent(app
->sock
);
5421 pthread_mutex_unlock(&app
->sock_lock
);
5422 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5423 ERR("UST app wait quiescent failed for app pid %d ret %d",
5428 health_code_update();
5433 * Start tracing for the UST session.
5435 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5437 struct lttng_ht_iter iter
;
5438 struct ust_app
*app
;
5440 DBG("Starting all UST traces");
5443 * Even though the start trace might fail, flag this session active so
5444 * other application coming in are started by default.
5451 * In a start-stop-start use-case, we need to clear the quiescent state
5452 * of each channel set by the prior stop command, thus ensuring that a
5453 * following stop or destroy is sure to grab a timestamp_end near those
5454 * operations, even if the packet is empty.
5456 (void) ust_app_clear_quiescent_session(usess
);
5458 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5459 ust_app_global_update(usess
, app
);
5468 * Start tracing for the UST session.
5469 * Called with UST session lock held.
5471 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5474 struct lttng_ht_iter iter
;
5475 struct ust_app
*app
;
5477 DBG("Stopping all UST traces");
5480 * Even though the stop trace might fail, flag this session inactive so
5481 * other application coming in are not started by default.
5487 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5488 ret
= ust_app_stop_trace(usess
, app
);
5490 /* Continue to next apps even on error */
5495 (void) ust_app_flush_session(usess
);
5503 * Destroy app UST session.
5505 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5508 struct lttng_ht_iter iter
;
5509 struct ust_app
*app
;
5511 DBG("Destroy all UST traces");
5515 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5516 ret
= destroy_trace(usess
, app
);
5518 /* Continue to next apps even on error */
5528 /* The ua_sess lock must be held by the caller. */
5530 int find_or_create_ust_app_channel(
5531 struct ltt_ust_session
*usess
,
5532 struct ust_app_session
*ua_sess
,
5533 struct ust_app
*app
,
5534 struct ltt_ust_channel
*uchan
,
5535 struct ust_app_channel
**ua_chan
)
5538 struct lttng_ht_iter iter
;
5539 struct lttng_ht_node_str
*ua_chan_node
;
5541 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5542 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5544 *ua_chan
= caa_container_of(ua_chan_node
,
5545 struct ust_app_channel
, node
);
5549 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5558 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5559 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5560 struct ust_app
*app
)
5563 struct ust_app_event
*ua_event
= NULL
;
5565 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5566 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5568 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5573 if (ua_event
->enabled
!= uevent
->enabled
) {
5574 ret
= uevent
->enabled
?
5575 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5576 disable_ust_app_event(ua_sess
, ua_event
, app
);
5584 /* Called with RCU read-side lock held. */
5586 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5589 enum lttng_error_code ret_code
;
5590 enum lttng_trigger_status t_status
;
5591 struct lttng_ht_iter app_trigger_iter
;
5592 struct lttng_triggers
*triggers
= NULL
;
5593 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5594 unsigned int count
, i
;
5597 * Currrently, registering or unregistering a trigger with an
5598 * event rule condition causes a full synchronization of the event
5601 * The first step attempts to add an event notifier for all registered
5602 * triggers that apply to the user space tracers. Then, the
5603 * application's event notifiers rules are all checked against the list
5604 * of registered triggers. Any event notifier that doesn't have a
5605 * matching trigger can be assumed to have been disabled.
5607 * All of this is inefficient, but is put in place to get the feature
5608 * rolling as it is simpler at this moment. It will be optimized Soon™
5609 * to allow the state of enabled
5610 * event notifiers to be synchronized in a piece-wise way.
5613 /* Get all triggers using uid 0 (root) */
5614 ret_code
= notification_thread_command_list_triggers(
5615 notification_thread_handle
, 0, &triggers
);
5616 if (ret_code
!= LTTNG_OK
) {
5623 t_status
= lttng_triggers_get_count(triggers
, &count
);
5624 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5629 for (i
= 0; i
< count
; i
++) {
5630 struct lttng_condition
*condition
;
5631 struct lttng_event_rule
*event_rule
;
5632 struct lttng_trigger
*trigger
;
5633 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5634 enum lttng_condition_status condition_status
;
5637 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5640 token
= lttng_trigger_get_tracer_token(trigger
);
5641 condition
= lttng_trigger_get_condition(trigger
);
5643 if (lttng_condition_get_type(condition
) != LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
5644 /* Does not apply */
5648 condition_status
= lttng_condition_event_rule_borrow_rule_mutable(condition
, &event_rule
);
5649 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5651 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5652 /* Skip kernel related triggers. */
5657 * Find or create the associated token event rule. The caller
5658 * holds the RCU read lock, so this is safe to call without
5659 * explicitly acquiring it here.
5661 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
5662 app
->token_to_event_notifier_rule_ht
, token
);
5663 if (!looked_up_event_notifier_rule
) {
5664 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
5672 /* Remove all unknown event sources from the app. */
5673 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
5674 &app_trigger_iter
.iter
, event_notifier_rule
,
5676 const uint64_t app_token
= event_notifier_rule
->token
;
5680 * Check if the app event trigger still exists on the
5681 * notification side.
5683 for (i
= 0; i
< count
; i
++) {
5684 uint64_t notification_thread_token
;
5685 const struct lttng_trigger
*trigger
=
5686 lttng_triggers_get_at_index(
5691 notification_thread_token
=
5692 lttng_trigger_get_tracer_token(trigger
);
5694 if (notification_thread_token
== app_token
) {
5706 * This trigger was unregistered, disable it on the tracer's
5709 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
5713 /* Callee logs errors. */
5714 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
5716 delete_ust_app_event_notifier_rule(
5717 app
->sock
, event_notifier_rule
, app
);
5723 lttng_triggers_destroy(triggers
);
5728 * The caller must ensure that the application is compatible and is tracked
5729 * by the process attribute trackers.
5732 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5733 struct ust_app
*app
)
5736 struct cds_lfht_iter uchan_iter
;
5737 struct ltt_ust_channel
*uchan
;
5738 struct ust_app_session
*ua_sess
= NULL
;
5741 * The application's configuration should only be synchronized for
5744 assert(usess
->active
);
5746 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5748 /* Tracer is probably gone or ENOMEM. */
5753 pthread_mutex_lock(&ua_sess
->lock
);
5754 if (ua_sess
->deleted
) {
5755 pthread_mutex_unlock(&ua_sess
->lock
);
5761 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5763 struct ust_app_channel
*ua_chan
;
5764 struct cds_lfht_iter uevent_iter
;
5765 struct ltt_ust_event
*uevent
;
5768 * Search for a matching ust_app_channel. If none is found,
5769 * create it. Creating the channel will cause the ua_chan
5770 * structure to be allocated, the channel buffers to be
5771 * allocated (if necessary) and sent to the application, and
5772 * all enabled contexts will be added to the channel.
5774 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5775 app
, uchan
, &ua_chan
);
5777 /* Tracer is probably gone or ENOMEM. */
5782 /* ua_chan will be NULL for the metadata channel */
5786 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5788 ret
= ust_app_channel_synchronize_event(ua_chan
,
5789 uevent
, ua_sess
, app
);
5795 if (ua_chan
->enabled
!= uchan
->enabled
) {
5796 ret
= uchan
->enabled
?
5797 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5798 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5806 * Create the metadata for the application. This returns gracefully if a
5807 * metadata was already set for the session.
5809 * The metadata channel must be created after the data channels as the
5810 * consumer daemon assumes this ordering. When interacting with a relay
5811 * daemon, the consumer will use this assumption to send the
5812 * "STREAMS_SENT" message to the relay daemon.
5814 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5822 pthread_mutex_unlock(&ua_sess
->lock
);
5823 /* Everything went well at this point. */
5828 pthread_mutex_unlock(&ua_sess
->lock
);
5831 destroy_app_session(app
, ua_sess
);
5837 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5839 struct ust_app_session
*ua_sess
;
5841 ua_sess
= lookup_session_by_app(usess
, app
);
5842 if (ua_sess
== NULL
) {
5845 destroy_app_session(app
, ua_sess
);
5849 * Add channels/events from UST global domain to registered apps at sock.
5851 * Called with session lock held.
5852 * Called with RCU read-side lock held.
5854 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5857 assert(usess
->active
);
5859 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5860 app
->sock
, usess
->id
);
5862 if (!app
->compatible
) {
5865 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5867 trace_ust_id_tracker_lookup(
5868 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5870 trace_ust_id_tracker_lookup(
5871 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5874 * Synchronize the application's internal tracing configuration
5875 * and start tracing.
5877 ust_app_synchronize(usess
, app
);
5878 ust_app_start_trace(usess
, app
);
5880 ust_app_global_destroy(usess
, app
);
5885 * Add all event notifiers to an application.
5887 * Called with session lock held.
5888 * Called with RCU read-side lock held.
5890 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
5892 DBG2("UST application global event notifier rules update: app = '%s' (ppid: %d)",
5893 app
->name
, app
->ppid
);
5895 if (!app
->compatible
) {
5899 if (app
->event_notifier_group
.object
== NULL
) {
5900 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s' (ppid: %d)",
5901 app
->name
, app
->ppid
);
5905 ust_app_synchronize_event_notifier_rules(app
);
5909 * Called with session lock held.
5911 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5913 struct lttng_ht_iter iter
;
5914 struct ust_app
*app
;
5917 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5918 ust_app_global_update(usess
, app
);
5923 void ust_app_global_update_all_event_notifier_rules(void)
5925 struct lttng_ht_iter iter
;
5926 struct ust_app
*app
;
5929 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5930 ust_app_global_update_event_notifier_rules(app
);
5937 * Add context to a specific channel for global UST domain.
5939 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5940 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5943 struct lttng_ht_node_str
*ua_chan_node
;
5944 struct lttng_ht_iter iter
, uiter
;
5945 struct ust_app_channel
*ua_chan
= NULL
;
5946 struct ust_app_session
*ua_sess
;
5947 struct ust_app
*app
;
5949 assert(usess
->active
);
5952 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5953 if (!app
->compatible
) {
5955 * TODO: In time, we should notice the caller of this error by
5956 * telling him that this is a version error.
5960 ua_sess
= lookup_session_by_app(usess
, app
);
5961 if (ua_sess
== NULL
) {
5965 pthread_mutex_lock(&ua_sess
->lock
);
5967 if (ua_sess
->deleted
) {
5968 pthread_mutex_unlock(&ua_sess
->lock
);
5972 /* Lookup channel in the ust app session */
5973 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5974 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5975 if (ua_chan_node
== NULL
) {
5978 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5980 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5985 pthread_mutex_unlock(&ua_sess
->lock
);
5993 * Receive registration and populate the given msg structure.
5995 * On success return 0 else a negative value returned by the ustctl call.
5997 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6000 uint32_t pid
, ppid
, uid
, gid
;
6004 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
6005 &pid
, &ppid
, &uid
, &gid
,
6006 &msg
->bits_per_long
,
6007 &msg
->uint8_t_alignment
,
6008 &msg
->uint16_t_alignment
,
6009 &msg
->uint32_t_alignment
,
6010 &msg
->uint64_t_alignment
,
6011 &msg
->long_alignment
,
6018 case LTTNG_UST_ERR_EXITING
:
6019 DBG3("UST app recv reg message failed. Application died");
6021 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6022 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6023 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
6024 LTTNG_UST_ABI_MINOR_VERSION
);
6027 ERR("UST app recv reg message failed with ret %d", ret
);
6032 msg
->pid
= (pid_t
) pid
;
6033 msg
->ppid
= (pid_t
) ppid
;
6034 msg
->uid
= (uid_t
) uid
;
6035 msg
->gid
= (gid_t
) gid
;
6042 * Return a ust app session object using the application object and the
6043 * session object descriptor has a key. If not found, NULL is returned.
6044 * A RCU read side lock MUST be acquired when calling this function.
6046 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
6049 struct lttng_ht_node_ulong
*node
;
6050 struct lttng_ht_iter iter
;
6051 struct ust_app_session
*ua_sess
= NULL
;
6055 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
6056 node
= lttng_ht_iter_get_node_ulong(&iter
);
6058 DBG2("UST app session find by objd %d not found", objd
);
6062 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
6069 * Return a ust app channel object using the application object and the channel
6070 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6071 * lock MUST be acquired before calling this function.
6073 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
6076 struct lttng_ht_node_ulong
*node
;
6077 struct lttng_ht_iter iter
;
6078 struct ust_app_channel
*ua_chan
= NULL
;
6082 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
6083 node
= lttng_ht_iter_get_node_ulong(&iter
);
6085 DBG2("UST app channel find by objd %d not found", objd
);
6089 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
6096 * Reply to a register channel notification from an application on the notify
6097 * socket. The channel metadata is also created.
6099 * The session UST registry lock is acquired in this function.
6101 * On success 0 is returned else a negative value.
6103 static int reply_ust_register_channel(int sock
, int cobjd
,
6104 size_t nr_fields
, struct ustctl_field
*fields
)
6106 int ret
, ret_code
= 0;
6108 uint64_t chan_reg_key
;
6109 enum ustctl_channel_header type
;
6110 struct ust_app
*app
;
6111 struct ust_app_channel
*ua_chan
;
6112 struct ust_app_session
*ua_sess
;
6113 struct ust_registry_session
*registry
;
6114 struct ust_registry_channel
*chan_reg
;
6118 /* Lookup application. If not found, there is a code flow error. */
6119 app
= find_app_by_notify_sock(sock
);
6121 DBG("Application socket %d is being torn down. Abort event notify",
6124 goto error_rcu_unlock
;
6127 /* Lookup channel by UST object descriptor. */
6128 ua_chan
= find_channel_by_objd(app
, cobjd
);
6130 DBG("Application channel is being torn down. Abort event notify");
6132 goto error_rcu_unlock
;
6135 assert(ua_chan
->session
);
6136 ua_sess
= ua_chan
->session
;
6138 /* Get right session registry depending on the session buffer type. */
6139 registry
= get_session_registry(ua_sess
);
6141 DBG("Application session is being torn down. Abort event notify");
6143 goto error_rcu_unlock
;
6146 /* Depending on the buffer type, a different channel key is used. */
6147 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6148 chan_reg_key
= ua_chan
->tracing_channel_id
;
6150 chan_reg_key
= ua_chan
->key
;
6153 pthread_mutex_lock(®istry
->lock
);
6155 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
6158 if (!chan_reg
->register_done
) {
6160 * TODO: eventually use the registry event count for
6161 * this channel to better guess header type for per-pid
6164 type
= USTCTL_CHANNEL_HEADER_LARGE
;
6165 chan_reg
->nr_ctx_fields
= nr_fields
;
6166 chan_reg
->ctx_fields
= fields
;
6168 chan_reg
->header_type
= type
;
6170 /* Get current already assigned values. */
6171 type
= chan_reg
->header_type
;
6173 /* Channel id is set during the object creation. */
6174 chan_id
= chan_reg
->chan_id
;
6176 /* Append to metadata */
6177 if (!chan_reg
->metadata_dumped
) {
6178 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
6180 ERR("Error appending channel metadata (errno = %d)", ret_code
);
6186 DBG3("UST app replying to register channel key %" PRIu64
6187 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
6190 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
6192 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6193 ERR("UST app reply channel failed with ret %d", ret
);
6195 DBG3("UST app reply channel failed. Application died");
6200 /* This channel registry registration is completed. */
6201 chan_reg
->register_done
= 1;
6204 pthread_mutex_unlock(®istry
->lock
);
6212 * Add event to the UST channel registry. When the event is added to the
6213 * registry, the metadata is also created. Once done, this replies to the
6214 * application with the appropriate error code.
6216 * The session UST registry lock is acquired in the function.
6218 * On success 0 is returned else a negative value.
6220 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
6221 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
6222 int loglevel_value
, char *model_emf_uri
)
6225 uint32_t event_id
= 0;
6226 uint64_t chan_reg_key
;
6227 struct ust_app
*app
;
6228 struct ust_app_channel
*ua_chan
;
6229 struct ust_app_session
*ua_sess
;
6230 struct ust_registry_session
*registry
;
6234 /* Lookup application. If not found, there is a code flow error. */
6235 app
= find_app_by_notify_sock(sock
);
6237 DBG("Application socket %d is being torn down. Abort event notify",
6240 goto error_rcu_unlock
;
6243 /* Lookup channel by UST object descriptor. */
6244 ua_chan
= find_channel_by_objd(app
, cobjd
);
6246 DBG("Application channel is being torn down. Abort event notify");
6248 goto error_rcu_unlock
;
6251 assert(ua_chan
->session
);
6252 ua_sess
= ua_chan
->session
;
6254 registry
= get_session_registry(ua_sess
);
6256 DBG("Application session is being torn down. Abort event notify");
6258 goto error_rcu_unlock
;
6261 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6262 chan_reg_key
= ua_chan
->tracing_channel_id
;
6264 chan_reg_key
= ua_chan
->key
;
6267 pthread_mutex_lock(®istry
->lock
);
6270 * From this point on, this call acquires the ownership of the sig, fields
6271 * and model_emf_uri meaning any free are done inside it if needed. These
6272 * three variables MUST NOT be read/write after this.
6274 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
6275 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
6276 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
6280 model_emf_uri
= NULL
;
6283 * The return value is returned to ustctl so in case of an error, the
6284 * application can be notified. In case of an error, it's important not to
6285 * return a negative error or else the application will get closed.
6287 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
6289 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6290 ERR("UST app reply event failed with ret %d", ret
);
6292 DBG3("UST app reply event failed. Application died");
6295 * No need to wipe the create event since the application socket will
6296 * get close on error hence cleaning up everything by itself.
6301 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6305 pthread_mutex_unlock(®istry
->lock
);
6310 free(model_emf_uri
);
6315 * Add enum to the UST session registry. Once done, this replies to the
6316 * application with the appropriate error code.
6318 * The session UST registry lock is acquired within this function.
6320 * On success 0 is returned else a negative value.
6322 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
6323 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
6325 int ret
= 0, ret_code
;
6326 struct ust_app
*app
;
6327 struct ust_app_session
*ua_sess
;
6328 struct ust_registry_session
*registry
;
6329 uint64_t enum_id
= -1ULL;
6333 /* Lookup application. If not found, there is a code flow error. */
6334 app
= find_app_by_notify_sock(sock
);
6336 /* Return an error since this is not an error */
6337 DBG("Application socket %d is being torn down. Aborting enum registration",
6340 goto error_rcu_unlock
;
6343 /* Lookup session by UST object descriptor. */
6344 ua_sess
= find_session_by_objd(app
, sobjd
);
6346 /* Return an error since this is not an error */
6347 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6349 goto error_rcu_unlock
;
6352 registry
= get_session_registry(ua_sess
);
6354 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6356 goto error_rcu_unlock
;
6359 pthread_mutex_lock(®istry
->lock
);
6362 * From this point on, the callee acquires the ownership of
6363 * entries. The variable entries MUST NOT be read/written after
6366 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
6367 entries
, nr_entries
, &enum_id
);
6371 * The return value is returned to ustctl so in case of an error, the
6372 * application can be notified. In case of an error, it's important not to
6373 * return a negative error or else the application will get closed.
6375 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
6377 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6378 ERR("UST app reply enum failed with ret %d", ret
);
6380 DBG3("UST app reply enum failed. Application died");
6383 * No need to wipe the create enum since the application socket will
6384 * get close on error hence cleaning up everything by itself.
6389 DBG3("UST registry enum %s added successfully or already found", name
);
6392 pthread_mutex_unlock(®istry
->lock
);
6399 * Handle application notification through the given notify socket.
6401 * Return 0 on success or else a negative value.
6403 int ust_app_recv_notify(int sock
)
6406 enum ustctl_notify_cmd cmd
;
6408 DBG3("UST app receiving notify from sock %d", sock
);
6410 ret
= ustctl_recv_notify(sock
, &cmd
);
6412 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6413 ERR("UST app recv notify failed with ret %d", ret
);
6415 DBG3("UST app recv notify failed. Application died");
6421 case USTCTL_NOTIFY_CMD_EVENT
:
6423 int sobjd
, cobjd
, loglevel_value
;
6424 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6426 struct ustctl_field
*fields
;
6428 DBG2("UST app ustctl register event received");
6430 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
6431 &loglevel_value
, &sig
, &nr_fields
, &fields
,
6434 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6435 ERR("UST app recv event failed with ret %d", ret
);
6437 DBG3("UST app recv event failed. Application died");
6443 * Add event to the UST registry coming from the notify socket. This
6444 * call will free if needed the sig, fields and model_emf_uri. This
6445 * code path loses the ownsership of these variables and transfer them
6446 * to the this function.
6448 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6449 fields
, loglevel_value
, model_emf_uri
);
6456 case USTCTL_NOTIFY_CMD_CHANNEL
:
6460 struct ustctl_field
*fields
;
6462 DBG2("UST app ustctl register channel received");
6464 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
6467 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6468 ERR("UST app recv channel failed with ret %d", ret
);
6470 DBG3("UST app recv channel failed. Application died");
6476 * The fields ownership are transfered to this function call meaning
6477 * that if needed it will be freed. After this, it's invalid to access
6478 * fields or clean it up.
6480 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
6488 case USTCTL_NOTIFY_CMD_ENUM
:
6491 char name
[LTTNG_UST_SYM_NAME_LEN
];
6493 struct ustctl_enum_entry
*entries
;
6495 DBG2("UST app ustctl register enum received");
6497 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
6498 &entries
, &nr_entries
);
6500 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6501 ERR("UST app recv enum failed with ret %d", ret
);
6503 DBG3("UST app recv enum failed. Application died");
6508 /* Callee assumes ownership of entries */
6509 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6510 entries
, nr_entries
);
6518 /* Should NEVER happen. */
6527 * Once the notify socket hangs up, this is called. First, it tries to find the
6528 * corresponding application. On failure, the call_rcu to close the socket is
6529 * executed. If an application is found, it tries to delete it from the notify
6530 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6532 * Note that an object needs to be allocated here so on ENOMEM failure, the
6533 * call RCU is not done but the rest of the cleanup is.
6535 void ust_app_notify_sock_unregister(int sock
)
6538 struct lttng_ht_iter iter
;
6539 struct ust_app
*app
;
6540 struct ust_app_notify_sock_obj
*obj
;
6546 obj
= zmalloc(sizeof(*obj
));
6549 * An ENOMEM is kind of uncool. If this strikes we continue the
6550 * procedure but the call_rcu will not be called. In this case, we
6551 * accept the fd leak rather than possibly creating an unsynchronized
6552 * state between threads.
6554 * TODO: The notify object should be created once the notify socket is
6555 * registered and stored independantely from the ust app object. The
6556 * tricky part is to synchronize the teardown of the application and
6557 * this notify object. Let's keep that in mind so we can avoid this
6558 * kind of shenanigans with ENOMEM in the teardown path.
6565 DBG("UST app notify socket unregister %d", sock
);
6568 * Lookup application by notify socket. If this fails, this means that the
6569 * hash table delete has already been done by the application
6570 * unregistration process so we can safely close the notify socket in a
6573 app
= find_app_by_notify_sock(sock
);
6578 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6581 * Whatever happens here either we fail or succeed, in both cases we have
6582 * to close the socket after a grace period to continue to the call RCU
6583 * here. If the deletion is successful, the application is not visible
6584 * anymore by other threads and is it fails it means that it was already
6585 * deleted from the hash table so either way we just have to close the
6588 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6594 * Close socket after a grace period to avoid for the socket to be reused
6595 * before the application object is freed creating potential race between
6596 * threads trying to add unique in the global hash table.
6599 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6604 * Destroy a ust app data structure and free its memory.
6606 void ust_app_destroy(struct ust_app
*app
)
6612 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6616 * Take a snapshot for a given UST session. The snapshot is sent to the given
6619 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6621 enum lttng_error_code
ust_app_snapshot_record(
6622 const struct ltt_ust_session
*usess
,
6623 const struct consumer_output
*output
, int wait
,
6624 uint64_t nb_packets_per_stream
)
6627 enum lttng_error_code status
= LTTNG_OK
;
6628 struct lttng_ht_iter iter
;
6629 struct ust_app
*app
;
6630 char *trace_path
= NULL
;
6637 switch (usess
->buffer_type
) {
6638 case LTTNG_BUFFER_PER_UID
:
6640 struct buffer_reg_uid
*reg
;
6642 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6643 struct buffer_reg_channel
*reg_chan
;
6644 struct consumer_socket
*socket
;
6645 char pathname
[PATH_MAX
];
6646 size_t consumer_path_offset
= 0;
6648 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6649 /* Skip since no metadata is present */
6653 /* Get consumer socket to use to push the metadata.*/
6654 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6657 status
= LTTNG_ERR_INVALID
;
6661 memset(pathname
, 0, sizeof(pathname
));
6662 ret
= snprintf(pathname
, sizeof(pathname
),
6663 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
6664 reg
->uid
, reg
->bits_per_long
);
6666 PERROR("snprintf snapshot path");
6667 status
= LTTNG_ERR_INVALID
;
6670 /* Free path allowed on previous iteration. */
6672 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6673 &consumer_path_offset
);
6675 status
= LTTNG_ERR_INVALID
;
6678 /* Add the UST default trace dir to path. */
6679 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6680 reg_chan
, node
.node
) {
6681 status
= consumer_snapshot_channel(socket
,
6682 reg_chan
->consumer_key
,
6683 output
, 0, usess
->uid
,
6684 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
6685 nb_packets_per_stream
);
6686 if (status
!= LTTNG_OK
) {
6690 status
= consumer_snapshot_channel(socket
,
6691 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6692 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6694 if (status
!= LTTNG_OK
) {
6700 case LTTNG_BUFFER_PER_PID
:
6702 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6703 struct consumer_socket
*socket
;
6704 struct lttng_ht_iter chan_iter
;
6705 struct ust_app_channel
*ua_chan
;
6706 struct ust_app_session
*ua_sess
;
6707 struct ust_registry_session
*registry
;
6708 char pathname
[PATH_MAX
];
6709 size_t consumer_path_offset
= 0;
6711 ua_sess
= lookup_session_by_app(usess
, app
);
6713 /* Session not associated with this app. */
6717 /* Get the right consumer socket for the application. */
6718 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6721 status
= LTTNG_ERR_INVALID
;
6725 /* Add the UST default trace dir to path. */
6726 memset(pathname
, 0, sizeof(pathname
));
6727 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
6730 status
= LTTNG_ERR_INVALID
;
6731 PERROR("snprintf snapshot path");
6734 /* Free path allowed on previous iteration. */
6736 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6737 &consumer_path_offset
);
6739 status
= LTTNG_ERR_INVALID
;
6742 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6743 ua_chan
, node
.node
) {
6744 status
= consumer_snapshot_channel(socket
,
6745 ua_chan
->key
, output
, 0,
6746 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6747 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6748 &trace_path
[consumer_path_offset
], wait
,
6749 nb_packets_per_stream
);
6753 case LTTNG_ERR_CHAN_NOT_FOUND
:
6760 registry
= get_session_registry(ua_sess
);
6762 DBG("Application session is being torn down. Skip application.");
6765 status
= consumer_snapshot_channel(socket
,
6766 registry
->metadata_key
, output
, 1,
6767 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6768 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6769 &trace_path
[consumer_path_offset
], wait
, 0);
6773 case LTTNG_ERR_CHAN_NOT_FOUND
:
6793 * Return the size taken by one more packet per stream.
6795 uint64_t ust_app_get_size_one_more_packet_per_stream(
6796 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6798 uint64_t tot_size
= 0;
6799 struct ust_app
*app
;
6800 struct lttng_ht_iter iter
;
6804 switch (usess
->buffer_type
) {
6805 case LTTNG_BUFFER_PER_UID
:
6807 struct buffer_reg_uid
*reg
;
6809 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6810 struct buffer_reg_channel
*reg_chan
;
6813 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6814 reg_chan
, node
.node
) {
6815 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6817 * Don't take channel into account if we
6818 * already grab all its packets.
6822 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6828 case LTTNG_BUFFER_PER_PID
:
6831 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6832 struct ust_app_channel
*ua_chan
;
6833 struct ust_app_session
*ua_sess
;
6834 struct lttng_ht_iter chan_iter
;
6836 ua_sess
= lookup_session_by_app(usess
, app
);
6838 /* Session not associated with this app. */
6842 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6843 ua_chan
, node
.node
) {
6844 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6846 * Don't take channel into account if we
6847 * already grab all its packets.
6851 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6865 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6866 struct cds_list_head
*buffer_reg_uid_list
,
6867 struct consumer_output
*consumer
, uint64_t uchan_id
,
6868 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6871 uint64_t consumer_chan_key
;
6876 ret
= buffer_reg_uid_consumer_channel_key(
6877 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6885 ret
= consumer_get_lost_packets(ust_session_id
,
6886 consumer_chan_key
, consumer
, lost
);
6888 ret
= consumer_get_discarded_events(ust_session_id
,
6889 consumer_chan_key
, consumer
, discarded
);
6896 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6897 struct ltt_ust_channel
*uchan
,
6898 struct consumer_output
*consumer
, int overwrite
,
6899 uint64_t *discarded
, uint64_t *lost
)
6902 struct lttng_ht_iter iter
;
6903 struct lttng_ht_node_str
*ua_chan_node
;
6904 struct ust_app
*app
;
6905 struct ust_app_session
*ua_sess
;
6906 struct ust_app_channel
*ua_chan
;
6913 * Iterate over every registered applications. Sum counters for
6914 * all applications containing requested session and channel.
6916 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6917 struct lttng_ht_iter uiter
;
6919 ua_sess
= lookup_session_by_app(usess
, app
);
6920 if (ua_sess
== NULL
) {
6925 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6926 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6927 /* If the session is found for the app, the channel must be there */
6928 assert(ua_chan_node
);
6930 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6935 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6942 uint64_t _discarded
;
6944 ret
= consumer_get_discarded_events(usess
->id
,
6945 ua_chan
->key
, consumer
, &_discarded
);
6949 (*discarded
) += _discarded
;
6958 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6959 struct ust_app
*app
)
6962 struct ust_app_session
*ua_sess
;
6964 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6968 ua_sess
= lookup_session_by_app(usess
, app
);
6969 if (ua_sess
== NULL
) {
6970 /* The session is in teardown process. Ignore and continue. */
6974 pthread_mutex_lock(&ua_sess
->lock
);
6976 if (ua_sess
->deleted
) {
6980 pthread_mutex_lock(&app
->sock_lock
);
6981 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6982 pthread_mutex_unlock(&app
->sock_lock
);
6985 pthread_mutex_unlock(&ua_sess
->lock
);
6989 health_code_update();
6994 * Regenerate the statedump for each app in the session.
6996 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6999 struct lttng_ht_iter iter
;
7000 struct ust_app
*app
;
7002 DBG("Regenerating the metadata for all UST apps");
7006 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7007 if (!app
->compatible
) {
7011 ret
= ust_app_regenerate_statedump(usess
, app
);
7013 /* Continue to the next app even on error */
7024 * Rotate all the channels of a session.
7026 * Return LTTNG_OK on success or else an LTTng error code.
7028 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7031 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7032 struct lttng_ht_iter iter
;
7033 struct ust_app
*app
;
7034 struct ltt_ust_session
*usess
= session
->ust_session
;
7040 switch (usess
->buffer_type
) {
7041 case LTTNG_BUFFER_PER_UID
:
7043 struct buffer_reg_uid
*reg
;
7045 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7046 struct buffer_reg_channel
*reg_chan
;
7047 struct consumer_socket
*socket
;
7049 if (!reg
->registry
->reg
.ust
->metadata_key
) {
7050 /* Skip since no metadata is present */
7054 /* Get consumer socket to use to push the metadata.*/
7055 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7058 cmd_ret
= LTTNG_ERR_INVALID
;
7062 /* Rotate the data channels. */
7063 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7064 reg_chan
, node
.node
) {
7065 ret
= consumer_rotate_channel(socket
,
7066 reg_chan
->consumer_key
,
7067 usess
->uid
, usess
->gid
,
7069 /* is_metadata_channel */ false);
7071 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7076 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7078 ret
= consumer_rotate_channel(socket
,
7079 reg
->registry
->reg
.ust
->metadata_key
,
7080 usess
->uid
, usess
->gid
,
7082 /* is_metadata_channel */ true);
7084 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7090 case LTTNG_BUFFER_PER_PID
:
7092 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7093 struct consumer_socket
*socket
;
7094 struct lttng_ht_iter chan_iter
;
7095 struct ust_app_channel
*ua_chan
;
7096 struct ust_app_session
*ua_sess
;
7097 struct ust_registry_session
*registry
;
7099 ua_sess
= lookup_session_by_app(usess
, app
);
7101 /* Session not associated with this app. */
7105 /* Get the right consumer socket for the application. */
7106 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7109 cmd_ret
= LTTNG_ERR_INVALID
;
7113 registry
= get_session_registry(ua_sess
);
7115 DBG("Application session is being torn down. Skip application.");
7119 /* Rotate the data channels. */
7120 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7121 ua_chan
, node
.node
) {
7122 ret
= consumer_rotate_channel(socket
,
7124 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7125 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7127 /* is_metadata_channel */ false);
7129 /* Per-PID buffer and application going away. */
7130 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7132 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7137 /* Rotate the metadata channel. */
7138 (void) push_metadata(registry
, usess
->consumer
);
7139 ret
= consumer_rotate_channel(socket
,
7140 registry
->metadata_key
,
7141 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7142 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7144 /* is_metadata_channel */ true);
7146 /* Per-PID buffer and application going away. */
7147 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7149 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7167 enum lttng_error_code
ust_app_create_channel_subdirectories(
7168 const struct ltt_ust_session
*usess
)
7170 enum lttng_error_code ret
= LTTNG_OK
;
7171 struct lttng_ht_iter iter
;
7172 enum lttng_trace_chunk_status chunk_status
;
7173 char *pathname_index
;
7176 assert(usess
->current_trace_chunk
);
7179 switch (usess
->buffer_type
) {
7180 case LTTNG_BUFFER_PER_UID
:
7182 struct buffer_reg_uid
*reg
;
7184 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7185 fmt_ret
= asprintf(&pathname_index
,
7186 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7187 reg
->uid
, reg
->bits_per_long
);
7189 ERR("Failed to format channel index directory");
7190 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7195 * Create the index subdirectory which will take care
7196 * of implicitly creating the channel's path.
7198 chunk_status
= lttng_trace_chunk_create_subdirectory(
7199 usess
->current_trace_chunk
,
7201 free(pathname_index
);
7202 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7203 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7209 case LTTNG_BUFFER_PER_PID
:
7211 struct ust_app
*app
;
7214 * Create the toplevel ust/ directory in case no apps are running.
7216 chunk_status
= lttng_trace_chunk_create_subdirectory(
7217 usess
->current_trace_chunk
,
7218 DEFAULT_UST_TRACE_DIR
);
7219 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7220 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7224 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7226 struct ust_app_session
*ua_sess
;
7227 struct ust_registry_session
*registry
;
7229 ua_sess
= lookup_session_by_app(usess
, app
);
7231 /* Session not associated with this app. */
7235 registry
= get_session_registry(ua_sess
);
7237 DBG("Application session is being torn down. Skip application.");
7241 fmt_ret
= asprintf(&pathname_index
,
7242 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7245 ERR("Failed to format channel index directory");
7246 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7250 * Create the index subdirectory which will take care
7251 * of implicitly creating the channel's path.
7253 chunk_status
= lttng_trace_chunk_create_subdirectory(
7254 usess
->current_trace_chunk
,
7256 free(pathname_index
);
7257 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7258 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7275 * Clear all the channels of a session.
7277 * Return LTTNG_OK on success or else an LTTng error code.
7279 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7282 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7283 struct lttng_ht_iter iter
;
7284 struct ust_app
*app
;
7285 struct ltt_ust_session
*usess
= session
->ust_session
;
7291 if (usess
->active
) {
7292 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7293 cmd_ret
= LTTNG_ERR_FATAL
;
7297 switch (usess
->buffer_type
) {
7298 case LTTNG_BUFFER_PER_UID
:
7300 struct buffer_reg_uid
*reg
;
7302 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7303 struct buffer_reg_channel
*reg_chan
;
7304 struct consumer_socket
*socket
;
7306 /* Get consumer socket to use to push the metadata.*/
7307 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7310 cmd_ret
= LTTNG_ERR_INVALID
;
7314 /* Clear the data channels. */
7315 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7316 reg_chan
, node
.node
) {
7317 ret
= consumer_clear_channel(socket
,
7318 reg_chan
->consumer_key
);
7324 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7327 * Clear the metadata channel.
7328 * Metadata channel is not cleared per se but we still need to
7329 * perform a rotation operation on it behind the scene.
7331 ret
= consumer_clear_channel(socket
,
7332 reg
->registry
->reg
.ust
->metadata_key
);
7339 case LTTNG_BUFFER_PER_PID
:
7341 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7342 struct consumer_socket
*socket
;
7343 struct lttng_ht_iter chan_iter
;
7344 struct ust_app_channel
*ua_chan
;
7345 struct ust_app_session
*ua_sess
;
7346 struct ust_registry_session
*registry
;
7348 ua_sess
= lookup_session_by_app(usess
, app
);
7350 /* Session not associated with this app. */
7354 /* Get the right consumer socket for the application. */
7355 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7358 cmd_ret
= LTTNG_ERR_INVALID
;
7362 registry
= get_session_registry(ua_sess
);
7364 DBG("Application session is being torn down. Skip application.");
7368 /* Clear the data channels. */
7369 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7370 ua_chan
, node
.node
) {
7371 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7373 /* Per-PID buffer and application going away. */
7374 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7381 (void) push_metadata(registry
, usess
->consumer
);
7384 * Clear the metadata channel.
7385 * Metadata channel is not cleared per se but we still need to
7386 * perform rotation operation on it behind the scene.
7388 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
7390 /* Per-PID buffer and application going away. */
7391 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7409 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7410 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7413 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7423 * This function skips the metadata channel as the begin/end timestamps of a
7424 * metadata packet are useless.
7426 * Moreover, opening a packet after a "clear" will cause problems for live
7427 * sessions as it will introduce padding that was not part of the first trace
7428 * chunk. The relay daemon expects the content of the metadata stream of
7429 * successive metadata trace chunks to be strict supersets of one another.
7431 * For example, flushing a packet at the beginning of the metadata stream of
7432 * a trace chunk resulting from a "clear" session command will cause the
7433 * size of the metadata stream of the new trace chunk to not match the size of
7434 * the metadata stream of the original chunk. This will confuse the relay
7435 * daemon as the same "offset" in a metadata stream will no longer point
7436 * to the same content.
7438 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7440 enum lttng_error_code ret
= LTTNG_OK
;
7441 struct lttng_ht_iter iter
;
7442 struct ltt_ust_session
*usess
= session
->ust_session
;
7448 switch (usess
->buffer_type
) {
7449 case LTTNG_BUFFER_PER_UID
:
7451 struct buffer_reg_uid
*reg
;
7453 cds_list_for_each_entry (
7454 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7455 struct buffer_reg_channel
*reg_chan
;
7456 struct consumer_socket
*socket
;
7458 socket
= consumer_find_socket_by_bitness(
7459 reg
->bits_per_long
, usess
->consumer
);
7461 ret
= LTTNG_ERR_FATAL
;
7465 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7466 &iter
.iter
, reg_chan
, node
.node
) {
7467 const int open_ret
=
7468 consumer_open_channel_packets(
7470 reg_chan
->consumer_key
);
7473 ret
= LTTNG_ERR_UNK
;
7480 case LTTNG_BUFFER_PER_PID
:
7482 struct ust_app
*app
;
7484 cds_lfht_for_each_entry (
7485 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7486 struct consumer_socket
*socket
;
7487 struct lttng_ht_iter chan_iter
;
7488 struct ust_app_channel
*ua_chan
;
7489 struct ust_app_session
*ua_sess
;
7490 struct ust_registry_session
*registry
;
7492 ua_sess
= lookup_session_by_app(usess
, app
);
7494 /* Session not associated with this app. */
7498 /* Get the right consumer socket for the application. */
7499 socket
= consumer_find_socket_by_bitness(
7500 app
->bits_per_long
, usess
->consumer
);
7502 ret
= LTTNG_ERR_FATAL
;
7506 registry
= get_session_registry(ua_sess
);
7508 DBG("Application session is being torn down. Skip application.");
7512 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
7513 &chan_iter
.iter
, ua_chan
, node
.node
) {
7514 const int open_ret
=
7515 consumer_open_channel_packets(
7521 * Per-PID buffer and application going
7524 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7528 ret
= LTTNG_ERR_UNK
;