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/compat/errno.h>
22 #include <common/common.h>
23 #include <common/hashtable/utils.h>
24 #include <lttng/event-rule/event-rule.h>
25 #include <lttng/event-rule/event-rule-internal.h>
26 #include <lttng/event-rule/tracepoint.h>
27 #include <lttng/condition/condition.h>
28 #include <lttng/condition/event-rule-internal.h>
29 #include <lttng/condition/event-rule.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
32 #include "buffer-registry.h"
34 #include "health-sessiond.h"
36 #include "ust-consumer.h"
37 #include "lttng-ust-ctl.h"
38 #include "lttng-ust-error.h"
41 #include "lttng-sessiond.h"
42 #include "notification-thread-commands.h"
45 struct lttng_ht
*ust_app_ht
;
46 struct lttng_ht
*ust_app_ht_by_sock
;
47 struct lttng_ht
*ust_app_ht_by_notify_sock
;
50 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
52 /* Next available channel key. Access under next_channel_key_lock. */
53 static uint64_t _next_channel_key
;
54 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
56 /* Next available session ID. Access under next_session_id_lock. */
57 static uint64_t _next_session_id
;
58 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
61 * Return the incremented value of next_channel_key.
63 static uint64_t get_next_channel_key(void)
67 pthread_mutex_lock(&next_channel_key_lock
);
68 ret
= ++_next_channel_key
;
69 pthread_mutex_unlock(&next_channel_key_lock
);
74 * Return the atomically incremented value of next_session_id.
76 static uint64_t get_next_session_id(void)
80 pthread_mutex_lock(&next_session_id_lock
);
81 ret
= ++_next_session_id
;
82 pthread_mutex_unlock(&next_session_id_lock
);
86 static void copy_channel_attr_to_ustctl(
87 struct ustctl_consumer_channel_attr
*attr
,
88 struct lttng_ust_channel_attr
*uattr
)
90 /* Copy event attributes since the layout is different. */
91 attr
->subbuf_size
= uattr
->subbuf_size
;
92 attr
->num_subbuf
= uattr
->num_subbuf
;
93 attr
->overwrite
= uattr
->overwrite
;
94 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
95 attr
->read_timer_interval
= uattr
->read_timer_interval
;
96 attr
->output
= uattr
->output
;
97 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
101 * Match function for the hash table lookup.
103 * It matches an ust app event based on three attributes which are the event
104 * name, the filter bytecode and the loglevel.
106 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
108 struct ust_app_event
*event
;
109 const struct ust_app_ht_key
*key
;
110 int ev_loglevel_value
;
115 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
117 ev_loglevel_value
= event
->attr
.loglevel
;
119 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
122 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
126 /* Event loglevel. */
127 if (ev_loglevel_value
!= key
->loglevel_type
) {
128 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
129 && key
->loglevel_type
== 0 &&
130 ev_loglevel_value
== -1) {
132 * Match is accepted. This is because on event creation, the
133 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
134 * -1 are accepted for this loglevel type since 0 is the one set by
135 * the API when receiving an enable event.
142 /* One of the filters is NULL, fail. */
143 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
147 if (key
->filter
&& event
->filter
) {
148 /* Both filters exists, check length followed by the bytecode. */
149 if (event
->filter
->len
!= key
->filter
->len
||
150 memcmp(event
->filter
->data
, key
->filter
->data
,
151 event
->filter
->len
) != 0) {
156 /* One of the exclusions is NULL, fail. */
157 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
161 if (key
->exclusion
&& event
->exclusion
) {
162 /* Both exclusions exists, check count followed by the names. */
163 if (event
->exclusion
->count
!= key
->exclusion
->count
||
164 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
165 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
179 * Unique add of an ust app event in the given ht. This uses the custom
180 * ht_match_ust_app_event match function and the event name as hash.
182 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
183 struct ust_app_event
*event
)
185 struct cds_lfht_node
*node_ptr
;
186 struct ust_app_ht_key key
;
190 assert(ua_chan
->events
);
193 ht
= ua_chan
->events
;
194 key
.name
= event
->attr
.name
;
195 key
.filter
= event
->filter
;
196 key
.loglevel_type
= event
->attr
.loglevel
;
197 key
.exclusion
= event
->exclusion
;
199 node_ptr
= cds_lfht_add_unique(ht
->ht
,
200 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
201 ht_match_ust_app_event
, &key
, &event
->node
.node
);
202 assert(node_ptr
== &event
->node
.node
);
206 * Close the notify socket from the given RCU head object. This MUST be called
207 * through a call_rcu().
209 static void close_notify_sock_rcu(struct rcu_head
*head
)
212 struct ust_app_notify_sock_obj
*obj
=
213 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
215 /* Must have a valid fd here. */
216 assert(obj
->fd
>= 0);
218 ret
= close(obj
->fd
);
220 ERR("close notify sock %d RCU", obj
->fd
);
222 lttng_fd_put(LTTNG_FD_APPS
, 1);
228 * Return the session registry according to the buffer type of the given
231 * A registry per UID object MUST exists before calling this function or else
232 * it assert() if not found. RCU read side lock must be acquired.
234 static struct ust_registry_session
*get_session_registry(
235 struct ust_app_session
*ua_sess
)
237 struct ust_registry_session
*registry
= NULL
;
241 switch (ua_sess
->buffer_type
) {
242 case LTTNG_BUFFER_PER_PID
:
244 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
248 registry
= reg_pid
->registry
->reg
.ust
;
251 case LTTNG_BUFFER_PER_UID
:
253 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
254 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
255 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
259 registry
= reg_uid
->registry
->reg
.ust
;
271 * Delete ust context safely. RCU read lock must be held before calling
275 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
283 pthread_mutex_lock(&app
->sock_lock
);
284 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
285 pthread_mutex_unlock(&app
->sock_lock
);
286 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
287 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
288 sock
, ua_ctx
->obj
->handle
, ret
);
296 * Delete ust app event safely. RCU read lock must be held before calling
300 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
307 free(ua_event
->filter
);
308 if (ua_event
->exclusion
!= NULL
)
309 free(ua_event
->exclusion
);
310 if (ua_event
->obj
!= NULL
) {
311 pthread_mutex_lock(&app
->sock_lock
);
312 ret
= ustctl_release_object(sock
, ua_event
->obj
);
313 pthread_mutex_unlock(&app
->sock_lock
);
314 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
315 ERR("UST app sock %d release event obj failed with ret %d",
324 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
325 * through a call_rcu().
328 void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
330 struct ust_app_event_notifier_rule
*obj
= caa_container_of(
331 head
, struct ust_app_event_notifier_rule
, rcu_head
);
337 * Delete ust app event notifier rule safely.
339 static void delete_ust_app_event_notifier_rule(int sock
,
340 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
,
345 assert(ua_event_notifier_rule
);
347 if (ua_event_notifier_rule
->exclusion
!= NULL
) {
348 free(ua_event_notifier_rule
->exclusion
);
351 if (ua_event_notifier_rule
->obj
!= NULL
) {
352 pthread_mutex_lock(&app
->sock_lock
);
353 ret
= ustctl_release_object(sock
, ua_event_notifier_rule
->obj
);
354 pthread_mutex_unlock(&app
->sock_lock
);
355 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
356 ERR("Failed to release event notifier object: app = '%s' (ppid %d), ret = %d",
357 app
->name
, (int) app
->ppid
, ret
);
360 free(ua_event_notifier_rule
->obj
);
363 lttng_event_rule_put(ua_event_notifier_rule
->event_rule
);
364 call_rcu(&ua_event_notifier_rule
->rcu_head
,
365 free_ust_app_event_notifier_rule_rcu
);
369 * Release ust data object of the given stream.
371 * Return 0 on success or else a negative value.
373 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
381 pthread_mutex_lock(&app
->sock_lock
);
382 ret
= ustctl_release_object(sock
, stream
->obj
);
383 pthread_mutex_unlock(&app
->sock_lock
);
384 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
385 ERR("UST app sock %d release stream obj failed with ret %d",
388 lttng_fd_put(LTTNG_FD_APPS
, 2);
396 * Delete ust app stream safely. RCU read lock must be held before calling
400 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
405 (void) release_ust_app_stream(sock
, stream
, app
);
410 * We need to execute ht_destroy outside of RCU read-side critical
411 * section and outside of call_rcu thread, so we postpone its execution
412 * using ht_cleanup_push. It is simpler than to change the semantic of
413 * the many callers of delete_ust_app_session().
416 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
418 struct ust_app_channel
*ua_chan
=
419 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
421 ht_cleanup_push(ua_chan
->ctx
);
422 ht_cleanup_push(ua_chan
->events
);
427 * Extract the lost packet or discarded events counter when the channel is
428 * being deleted and store the value in the parent channel so we can
429 * access it from lttng list and at stop/destroy.
431 * The session list lock must be held by the caller.
434 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
436 uint64_t discarded
= 0, lost
= 0;
437 struct ltt_session
*session
;
438 struct ltt_ust_channel
*uchan
;
440 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
445 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
446 if (!session
|| !session
->ust_session
) {
448 * Not finding the session is not an error because there are
449 * multiple ways the channels can be torn down.
451 * 1) The session daemon can initiate the destruction of the
452 * ust app session after receiving a destroy command or
453 * during its shutdown/teardown.
454 * 2) The application, since we are in per-pid tracing, is
455 * unregistering and tearing down its ust app session.
457 * Both paths are protected by the session list lock which
458 * ensures that the accounting of lost packets and discarded
459 * events is done exactly once. The session is then unpublished
460 * from the session list, resulting in this condition.
465 if (ua_chan
->attr
.overwrite
) {
466 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
467 ua_chan
->key
, session
->ust_session
->consumer
,
470 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
471 ua_chan
->key
, session
->ust_session
->consumer
,
474 uchan
= trace_ust_find_channel_by_name(
475 session
->ust_session
->domain_global
.channels
,
478 ERR("Missing UST channel to store discarded counters");
482 uchan
->per_pid_closed_app_discarded
+= discarded
;
483 uchan
->per_pid_closed_app_lost
+= lost
;
488 session_put(session
);
493 * Delete ust app channel safely. RCU read lock must be held before calling
496 * The session list lock must be held by the caller.
499 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
503 struct lttng_ht_iter iter
;
504 struct ust_app_event
*ua_event
;
505 struct ust_app_ctx
*ua_ctx
;
506 struct ust_app_stream
*stream
, *stmp
;
507 struct ust_registry_session
*registry
;
511 DBG3("UST app deleting channel %s", ua_chan
->name
);
514 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
515 cds_list_del(&stream
->list
);
516 delete_ust_app_stream(sock
, stream
, app
);
520 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
521 cds_list_del(&ua_ctx
->list
);
522 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
524 delete_ust_app_ctx(sock
, ua_ctx
, app
);
528 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
530 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
532 delete_ust_app_event(sock
, ua_event
, app
);
535 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
536 /* Wipe and free registry from session registry. */
537 registry
= get_session_registry(ua_chan
->session
);
539 ust_registry_channel_del_free(registry
, ua_chan
->key
,
543 * A negative socket can be used by the caller when
544 * cleaning-up a ua_chan in an error path. Skip the
545 * accounting in this case.
548 save_per_pid_lost_discarded_counters(ua_chan
);
552 if (ua_chan
->obj
!= NULL
) {
553 /* Remove channel from application UST object descriptor. */
554 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
555 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
557 pthread_mutex_lock(&app
->sock_lock
);
558 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
559 pthread_mutex_unlock(&app
->sock_lock
);
560 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
561 ERR("UST app sock %d release channel obj failed with ret %d",
564 lttng_fd_put(LTTNG_FD_APPS
, 1);
567 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
570 int ust_app_register_done(struct ust_app
*app
)
574 pthread_mutex_lock(&app
->sock_lock
);
575 ret
= ustctl_register_done(app
->sock
);
576 pthread_mutex_unlock(&app
->sock_lock
);
580 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
585 pthread_mutex_lock(&app
->sock_lock
);
590 ret
= ustctl_release_object(sock
, data
);
592 pthread_mutex_unlock(&app
->sock_lock
);
598 * Push metadata to consumer socket.
600 * RCU read-side lock must be held to guarantee existance of socket.
601 * Must be called with the ust app session lock held.
602 * Must be called with the registry lock held.
604 * On success, return the len of metadata pushed or else a negative value.
605 * Returning a -EPIPE return value means we could not send the metadata,
606 * but it can be caused by recoverable errors (e.g. the application has
607 * terminated concurrently).
609 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
610 struct consumer_socket
*socket
, int send_zero_data
)
613 char *metadata_str
= NULL
;
614 size_t len
, offset
, new_metadata_len_sent
;
616 uint64_t metadata_key
, metadata_version
;
621 metadata_key
= registry
->metadata_key
;
624 * Means that no metadata was assigned to the session. This can
625 * happens if no start has been done previously.
631 offset
= registry
->metadata_len_sent
;
632 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
633 new_metadata_len_sent
= registry
->metadata_len
;
634 metadata_version
= registry
->metadata_version
;
636 DBG3("No metadata to push for metadata key %" PRIu64
,
637 registry
->metadata_key
);
639 if (send_zero_data
) {
640 DBG("No metadata to push");
646 /* Allocate only what we have to send. */
647 metadata_str
= zmalloc(len
);
649 PERROR("zmalloc ust app metadata string");
653 /* Copy what we haven't sent out. */
654 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
657 pthread_mutex_unlock(®istry
->lock
);
659 * We need to unlock the registry while we push metadata to
660 * break a circular dependency between the consumerd metadata
661 * lock and the sessiond registry lock. Indeed, pushing metadata
662 * to the consumerd awaits that it gets pushed all the way to
663 * relayd, but doing so requires grabbing the metadata lock. If
664 * a concurrent metadata request is being performed by
665 * consumerd, this can try to grab the registry lock on the
666 * sessiond while holding the metadata lock on the consumer
667 * daemon. Those push and pull schemes are performed on two
668 * different bidirectionnal communication sockets.
670 ret
= consumer_push_metadata(socket
, metadata_key
,
671 metadata_str
, len
, offset
, metadata_version
);
672 pthread_mutex_lock(®istry
->lock
);
675 * There is an acceptable race here between the registry
676 * metadata key assignment and the creation on the
677 * consumer. The session daemon can concurrently push
678 * metadata for this registry while being created on the
679 * consumer since the metadata key of the registry is
680 * assigned *before* it is setup to avoid the consumer
681 * to ask for metadata that could possibly be not found
682 * in the session daemon.
684 * The metadata will get pushed either by the session
685 * being stopped or the consumer requesting metadata if
686 * that race is triggered.
688 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
691 ERR("Error pushing metadata to consumer");
697 * Metadata may have been concurrently pushed, since
698 * we're not holding the registry lock while pushing to
699 * consumer. This is handled by the fact that we send
700 * the metadata content, size, and the offset at which
701 * that metadata belongs. This may arrive out of order
702 * on the consumer side, and the consumer is able to
703 * deal with overlapping fragments. The consumer
704 * supports overlapping fragments, which must be
705 * contiguous starting from offset 0. We keep the
706 * largest metadata_len_sent value of the concurrent
709 registry
->metadata_len_sent
=
710 max_t(size_t, registry
->metadata_len_sent
,
711 new_metadata_len_sent
);
720 * On error, flag the registry that the metadata is
721 * closed. We were unable to push anything and this
722 * means that either the consumer is not responding or
723 * the metadata cache has been destroyed on the
726 registry
->metadata_closed
= 1;
734 * For a given application and session, push metadata to consumer.
735 * Either sock or consumer is required : if sock is NULL, the default
736 * socket to send the metadata is retrieved from consumer, if sock
737 * is not NULL we use it to send the metadata.
738 * RCU read-side lock must be held while calling this function,
739 * therefore ensuring existance of registry. It also ensures existance
740 * of socket throughout this function.
742 * Return 0 on success else a negative error.
743 * Returning a -EPIPE return value means we could not send the metadata,
744 * but it can be caused by recoverable errors (e.g. the application has
745 * terminated concurrently).
747 static int push_metadata(struct ust_registry_session
*registry
,
748 struct consumer_output
*consumer
)
752 struct consumer_socket
*socket
;
757 pthread_mutex_lock(®istry
->lock
);
758 if (registry
->metadata_closed
) {
763 /* Get consumer socket to use to push the metadata.*/
764 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
771 ret
= ust_app_push_metadata(registry
, socket
, 0);
776 pthread_mutex_unlock(®istry
->lock
);
780 pthread_mutex_unlock(®istry
->lock
);
785 * Send to the consumer a close metadata command for the given session. Once
786 * done, the metadata channel is deleted and the session metadata pointer is
787 * nullified. The session lock MUST be held unless the application is
788 * in the destroy path.
790 * Do not hold the registry lock while communicating with the consumerd, because
791 * doing so causes inter-process deadlocks between consumerd and sessiond with
792 * the metadata request notification.
794 * Return 0 on success else a negative value.
796 static int close_metadata(struct ust_registry_session
*registry
,
797 struct consumer_output
*consumer
)
800 struct consumer_socket
*socket
;
801 uint64_t metadata_key
;
802 bool registry_was_already_closed
;
809 pthread_mutex_lock(®istry
->lock
);
810 metadata_key
= registry
->metadata_key
;
811 registry_was_already_closed
= registry
->metadata_closed
;
812 if (metadata_key
!= 0) {
814 * Metadata closed. Even on error this means that the consumer
815 * is not responding or not found so either way a second close
816 * should NOT be emit for this registry.
818 registry
->metadata_closed
= 1;
820 pthread_mutex_unlock(®istry
->lock
);
822 if (metadata_key
== 0 || registry_was_already_closed
) {
827 /* Get consumer socket to use to push the metadata.*/
828 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
835 ret
= consumer_close_metadata(socket
, metadata_key
);
846 * We need to execute ht_destroy outside of RCU read-side critical
847 * section and outside of call_rcu thread, so we postpone its execution
848 * using ht_cleanup_push. It is simpler than to change the semantic of
849 * the many callers of delete_ust_app_session().
852 void delete_ust_app_session_rcu(struct rcu_head
*head
)
854 struct ust_app_session
*ua_sess
=
855 caa_container_of(head
, struct ust_app_session
, rcu_head
);
857 ht_cleanup_push(ua_sess
->channels
);
862 * Delete ust app session safely. RCU read lock must be held before calling
865 * The session list lock must be held by the caller.
868 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
872 struct lttng_ht_iter iter
;
873 struct ust_app_channel
*ua_chan
;
874 struct ust_registry_session
*registry
;
878 pthread_mutex_lock(&ua_sess
->lock
);
880 assert(!ua_sess
->deleted
);
881 ua_sess
->deleted
= true;
883 registry
= get_session_registry(ua_sess
);
884 /* Registry can be null on error path during initialization. */
886 /* Push metadata for application before freeing the application. */
887 (void) push_metadata(registry
, ua_sess
->consumer
);
890 * Don't ask to close metadata for global per UID buffers. Close
891 * metadata only on destroy trace session in this case. Also, the
892 * previous push metadata could have flag the metadata registry to
893 * close so don't send a close command if closed.
895 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
896 /* And ask to close it for this session registry. */
897 (void) close_metadata(registry
, ua_sess
->consumer
);
901 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
903 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
905 delete_ust_app_channel(sock
, ua_chan
, app
);
908 /* In case of per PID, the registry is kept in the session. */
909 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
910 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
913 * Registry can be null on error path during
916 buffer_reg_pid_remove(reg_pid
);
917 buffer_reg_pid_destroy(reg_pid
);
921 if (ua_sess
->handle
!= -1) {
922 pthread_mutex_lock(&app
->sock_lock
);
923 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
924 pthread_mutex_unlock(&app
->sock_lock
);
925 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
926 ERR("UST app sock %d release session handle failed with ret %d",
929 /* Remove session from application UST object descriptor. */
930 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
931 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
935 pthread_mutex_unlock(&ua_sess
->lock
);
937 consumer_output_put(ua_sess
->consumer
);
939 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
943 * Delete a traceable application structure from the global list. Never call
944 * this function outside of a call_rcu call.
946 * RCU read side lock should _NOT_ be held when calling this function.
949 void delete_ust_app(struct ust_app
*app
)
952 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
953 struct lttng_ht_iter iter
;
954 struct ust_app_event_notifier_rule
*event_notifier_rule
;
955 bool event_notifier_write_fd_is_open
;
958 * The session list lock must be held during this function to guarantee
959 * the existence of ua_sess.
962 /* Delete ust app sessions info */
967 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
969 /* Free every object in the session and the session. */
971 delete_ust_app_session(sock
, ua_sess
, app
);
975 /* Remove the event notifier rules associated with this app. */
977 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
978 &iter
.iter
, event_notifier_rule
, node
.node
) {
979 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
982 delete_ust_app_event_notifier_rule(
983 app
->sock
, event_notifier_rule
, app
);
988 ht_cleanup_push(app
->sessions
);
989 ht_cleanup_push(app
->ust_sessions_objd
);
990 ht_cleanup_push(app
->ust_objd
);
991 ht_cleanup_push(app
->token_to_event_notifier_rule_ht
);
994 * This could be NULL if the event notifier setup failed (e.g the app
995 * was killed or the tracer does not support this feature).
997 if (app
->event_notifier_group
.object
) {
998 enum lttng_error_code ret_code
;
999 const int event_notifier_read_fd
= lttng_pipe_get_readfd(
1000 app
->event_notifier_group
.event_pipe
);
1002 ret_code
= notification_thread_command_remove_tracer_event_source(
1003 notification_thread_handle
,
1004 event_notifier_read_fd
);
1005 if (ret_code
!= LTTNG_OK
) {
1006 ERR("Failed to remove application tracer event source from notification thread");
1009 ustctl_release_object(sock
, app
->event_notifier_group
.object
);
1010 free(app
->event_notifier_group
.object
);
1013 event_notifier_write_fd_is_open
= lttng_pipe_is_write_open(
1014 app
->event_notifier_group
.event_pipe
);
1015 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1017 * Release the file descriptors reserved for the event notifier pipe.
1018 * The app could be destroyed before the write end of the pipe could be
1019 * passed to the application (and closed). In that case, both file
1020 * descriptors must be released.
1022 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1025 * Wait until we have deleted the application from the sock hash table
1026 * before closing this socket, otherwise an application could re-use the
1027 * socket ID and race with the teardown, using the same hash table entry.
1029 * It's OK to leave the close in call_rcu. We want it to stay unique for
1030 * all RCU readers that could run concurrently with unregister app,
1031 * therefore we _need_ to only close that socket after a grace period. So
1032 * it should stay in this RCU callback.
1034 * This close() is a very important step of the synchronization model so
1035 * every modification to this function must be carefully reviewed.
1041 lttng_fd_put(LTTNG_FD_APPS
, 1);
1043 DBG2("UST app pid %d deleted", app
->pid
);
1045 session_unlock_list();
1049 * URCU intermediate call to delete an UST app.
1052 void delete_ust_app_rcu(struct rcu_head
*head
)
1054 struct lttng_ht_node_ulong
*node
=
1055 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1056 struct ust_app
*app
=
1057 caa_container_of(node
, struct ust_app
, pid_n
);
1059 DBG3("Call RCU deleting app PID %d", app
->pid
);
1060 delete_ust_app(app
);
1064 * Delete the session from the application ht and delete the data structure by
1065 * freeing every object inside and releasing them.
1067 * The session list lock must be held by the caller.
1069 static void destroy_app_session(struct ust_app
*app
,
1070 struct ust_app_session
*ua_sess
)
1073 struct lttng_ht_iter iter
;
1078 iter
.iter
.node
= &ua_sess
->node
.node
;
1079 ret
= lttng_ht_del(app
->sessions
, &iter
);
1081 /* Already scheduled for teardown. */
1085 /* Once deleted, free the data structure. */
1086 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1093 * Alloc new UST app session.
1096 struct ust_app_session
*alloc_ust_app_session(void)
1098 struct ust_app_session
*ua_sess
;
1100 /* Init most of the default value by allocating and zeroing */
1101 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1102 if (ua_sess
== NULL
) {
1107 ua_sess
->handle
= -1;
1108 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1109 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1110 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1119 * Alloc new UST app channel.
1122 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1123 struct ust_app_session
*ua_sess
,
1124 struct lttng_ust_channel_attr
*attr
)
1126 struct ust_app_channel
*ua_chan
;
1128 /* Init most of the default value by allocating and zeroing */
1129 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1130 if (ua_chan
== NULL
) {
1135 /* Setup channel name */
1136 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1137 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1139 ua_chan
->enabled
= 1;
1140 ua_chan
->handle
= -1;
1141 ua_chan
->session
= ua_sess
;
1142 ua_chan
->key
= get_next_channel_key();
1143 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1144 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1145 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1147 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1148 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1150 /* Copy attributes */
1152 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1153 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1154 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1155 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1156 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1157 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1158 ua_chan
->attr
.output
= attr
->output
;
1159 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1161 /* By default, the channel is a per cpu channel. */
1162 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1164 DBG3("UST app channel %s allocated", ua_chan
->name
);
1173 * Allocate and initialize a UST app stream.
1175 * Return newly allocated stream pointer or NULL on error.
1177 struct ust_app_stream
*ust_app_alloc_stream(void)
1179 struct ust_app_stream
*stream
= NULL
;
1181 stream
= zmalloc(sizeof(*stream
));
1182 if (stream
== NULL
) {
1183 PERROR("zmalloc ust app stream");
1187 /* Zero could be a valid value for a handle so flag it to -1. */
1188 stream
->handle
= -1;
1195 * Alloc new UST app event.
1198 struct ust_app_event
*alloc_ust_app_event(char *name
,
1199 struct lttng_ust_event
*attr
)
1201 struct ust_app_event
*ua_event
;
1203 /* Init most of the default value by allocating and zeroing */
1204 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1205 if (ua_event
== NULL
) {
1206 PERROR("Failed to allocate ust_app_event structure");
1210 ua_event
->enabled
= 1;
1211 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1212 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1213 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1215 /* Copy attributes */
1217 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1220 DBG3("UST app event %s allocated", ua_event
->name
);
1229 * Allocate a new UST app event notifier rule.
1231 static struct ust_app_event_notifier_rule
*alloc_ust_app_event_notifier_rule(
1232 struct lttng_event_rule
*event_rule
, uint64_t token
)
1234 enum lttng_event_rule_generate_exclusions_status
1235 generate_exclusion_status
;
1236 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1238 ua_event_notifier_rule
= zmalloc(sizeof(struct ust_app_event_notifier_rule
));
1239 if (ua_event_notifier_rule
== NULL
) {
1240 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1244 ua_event_notifier_rule
->enabled
= 1;
1245 ua_event_notifier_rule
->token
= token
;
1246 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
, token
);
1248 /* Get reference of the event rule. */
1249 if (!lttng_event_rule_get(event_rule
)) {
1253 ua_event_notifier_rule
->event_rule
= event_rule
;
1254 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1255 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1256 event_rule
, &ua_event_notifier_rule
->exclusion
);
1257 switch (generate_exclusion_status
) {
1258 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1259 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1262 /* Error occured. */
1263 ERR("Failed to generate exclusions from event rule while allocating an event notifier rule");
1264 goto error_put_event_rule
;
1267 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1268 ua_event_notifier_rule
->token
);
1270 return ua_event_notifier_rule
;
1272 error_put_event_rule
:
1273 lttng_event_rule_put(event_rule
);
1275 free(ua_event_notifier_rule
);
1280 * Alloc new UST app context.
1283 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1285 struct ust_app_ctx
*ua_ctx
;
1287 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1288 if (ua_ctx
== NULL
) {
1292 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1295 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1296 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1297 char *provider_name
= NULL
, *ctx_name
= NULL
;
1299 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1300 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1301 if (!provider_name
|| !ctx_name
) {
1302 free(provider_name
);
1307 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1308 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1312 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1320 * Allocate a filter and copy the given original filter.
1322 * Return allocated filter or NULL on error.
1324 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1325 struct lttng_filter_bytecode
*orig_f
)
1327 struct lttng_filter_bytecode
*filter
= NULL
;
1329 /* Copy filter bytecode */
1330 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1332 PERROR("zmalloc alloc filter bytecode");
1336 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1343 * Create a liblttng-ust filter bytecode from given bytecode.
1345 * Return allocated filter or NULL on error.
1347 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1348 const struct lttng_filter_bytecode
*orig_f
)
1350 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1352 /* Copy filter bytecode */
1353 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1355 PERROR("zmalloc alloc ust filter bytecode");
1359 assert(sizeof(struct lttng_filter_bytecode
) ==
1360 sizeof(struct lttng_ust_filter_bytecode
));
1361 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1367 * Find an ust_app using the sock and return it. RCU read side lock must be
1368 * held before calling this helper function.
1370 struct ust_app
*ust_app_find_by_sock(int sock
)
1372 struct lttng_ht_node_ulong
*node
;
1373 struct lttng_ht_iter iter
;
1375 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1376 node
= lttng_ht_iter_get_node_ulong(&iter
);
1378 DBG2("UST app find by sock %d not found", sock
);
1382 return caa_container_of(node
, struct ust_app
, sock_n
);
1389 * Find an ust_app using the notify sock and return it. RCU read side lock must
1390 * be held before calling this helper function.
1392 static struct ust_app
*find_app_by_notify_sock(int sock
)
1394 struct lttng_ht_node_ulong
*node
;
1395 struct lttng_ht_iter iter
;
1397 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1399 node
= lttng_ht_iter_get_node_ulong(&iter
);
1401 DBG2("UST app find by notify sock %d not found", sock
);
1405 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1412 * Lookup for an ust app event based on event name, filter bytecode and the
1415 * Return an ust_app_event object or NULL on error.
1417 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1418 const char *name
, const struct lttng_filter_bytecode
*filter
,
1420 const struct lttng_event_exclusion
*exclusion
)
1422 struct lttng_ht_iter iter
;
1423 struct lttng_ht_node_str
*node
;
1424 struct ust_app_event
*event
= NULL
;
1425 struct ust_app_ht_key key
;
1430 /* Setup key for event lookup. */
1432 key
.filter
= filter
;
1433 key
.loglevel_type
= loglevel_value
;
1434 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1435 key
.exclusion
= exclusion
;
1437 /* Lookup using the event name as hash and a custom match fct. */
1438 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1439 ht_match_ust_app_event
, &key
, &iter
.iter
);
1440 node
= lttng_ht_iter_get_node_str(&iter
);
1445 event
= caa_container_of(node
, struct ust_app_event
, node
);
1452 * Look-up an event notifier rule based on its token id.
1454 * Must be called with the RCU read lock held.
1455 * Return an ust_app_event_notifier_rule object or NULL on error.
1457 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(
1458 struct lttng_ht
*ht
, uint64_t token
)
1460 struct lttng_ht_iter iter
;
1461 struct lttng_ht_node_u64
*node
;
1462 struct ust_app_event_notifier_rule
*event_notifier_rule
= NULL
;
1466 lttng_ht_lookup(ht
, &token
, &iter
);
1467 node
= lttng_ht_iter_get_node_u64(&iter
);
1469 DBG2("UST app event notifier rule token not found: token = %" PRIu64
,
1474 event_notifier_rule
= caa_container_of(
1475 node
, struct ust_app_event_notifier_rule
, node
);
1477 return event_notifier_rule
;
1481 * Create the channel context on the tracer.
1483 * Called with UST app session lock held.
1486 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1487 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1491 health_code_update();
1493 pthread_mutex_lock(&app
->sock_lock
);
1494 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1495 ua_chan
->obj
, &ua_ctx
->obj
);
1496 pthread_mutex_unlock(&app
->sock_lock
);
1498 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1499 ERR("UST app create channel context failed for app (pid: %d) "
1500 "with ret %d", app
->pid
, ret
);
1503 * This is normal behavior, an application can die during the
1504 * creation process. Don't report an error so the execution can
1505 * continue normally.
1508 DBG3("UST app add context failed. Application is dead.");
1513 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1515 DBG2("UST app context handle %d created successfully for channel %s",
1516 ua_ctx
->handle
, ua_chan
->name
);
1519 health_code_update();
1524 * Set the filter on the tracer.
1526 static int set_ust_object_filter(struct ust_app
*app
,
1527 const struct lttng_filter_bytecode
*bytecode
,
1528 struct lttng_ust_object_data
*ust_object
)
1531 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1533 health_code_update();
1535 ust_bytecode
= create_ust_bytecode_from_bytecode(bytecode
);
1536 if (!ust_bytecode
) {
1537 ret
= -LTTNG_ERR_NOMEM
;
1540 pthread_mutex_lock(&app
->sock_lock
);
1541 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1543 pthread_mutex_unlock(&app
->sock_lock
);
1545 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1546 ERR("UST app set object filter failed for object %p of app (pid: %d) "
1547 "with ret %d", ust_object
, app
->pid
, ret
);
1550 * This is normal behavior, an application can die during the
1551 * creation process. Don't report an error so the execution can
1552 * continue normally.
1555 DBG3("Failed to set UST app object filter. Application is dead.");
1560 DBG2("UST filter successfully set for object %p", ust_object
);
1563 health_code_update();
1569 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1570 const struct lttng_event_exclusion
*exclusion
)
1572 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1573 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1574 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1576 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1577 if (!ust_exclusion
) {
1582 assert(sizeof(struct lttng_event_exclusion
) ==
1583 sizeof(struct lttng_ust_event_exclusion
));
1584 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1586 return ust_exclusion
;
1590 * Set event exclusions on the tracer.
1592 static int set_ust_object_exclusions(struct ust_app
*app
,
1593 const struct lttng_event_exclusion
*exclusions
,
1594 struct lttng_ust_object_data
*ust_object
)
1597 struct lttng_ust_event_exclusion
*ust_exclusions
= NULL
;
1599 assert(exclusions
&& exclusions
->count
> 0);
1601 health_code_update();
1603 ust_exclusions
= create_ust_exclusion_from_exclusion(
1605 if (!ust_exclusions
) {
1606 ret
= -LTTNG_ERR_NOMEM
;
1609 pthread_mutex_lock(&app
->sock_lock
);
1610 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1611 pthread_mutex_unlock(&app
->sock_lock
);
1613 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1614 ERR("Failed to set UST app exclusions for object %p of app (pid: %d) "
1615 "with ret %d", ust_object
, app
->pid
, ret
);
1618 * This is normal behavior, an application can die during the
1619 * creation process. Don't report an error so the execution can
1620 * continue normally.
1623 DBG3("Failed to set UST app object exclusions. Application is dead.");
1628 DBG2("UST exclusions set successfully for object %p", ust_object
);
1631 health_code_update();
1632 free(ust_exclusions
);
1637 * Disable the specified event on to UST tracer for the UST session.
1639 static int disable_ust_object(struct ust_app
*app
,
1640 struct lttng_ust_object_data
*object
)
1644 health_code_update();
1646 pthread_mutex_lock(&app
->sock_lock
);
1647 ret
= ustctl_disable(app
->sock
, object
);
1648 pthread_mutex_unlock(&app
->sock_lock
);
1650 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1651 ERR("Failed to disable UST app object %p app (pid: %d) with ret %d",
1652 object
, app
->pid
, ret
);
1655 * This is normal behavior, an application can die during the
1656 * creation process. Don't report an error so the execution can
1657 * continue normally.
1660 DBG3("Failed to disable UST app object. Application is dead.");
1665 DBG2("UST app object %p disabled successfully for app (pid: %d)",
1669 health_code_update();
1674 * Disable the specified channel on to UST tracer for the UST session.
1676 static int disable_ust_channel(struct ust_app
*app
,
1677 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1681 health_code_update();
1683 pthread_mutex_lock(&app
->sock_lock
);
1684 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1685 pthread_mutex_unlock(&app
->sock_lock
);
1687 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1688 ERR("UST app channel %s disable failed for app (pid: %d) "
1689 "and session handle %d with ret %d",
1690 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1693 * This is normal behavior, an application can die during the
1694 * creation process. Don't report an error so the execution can
1695 * continue normally.
1698 DBG3("UST app disable channel failed. Application is dead.");
1703 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1704 ua_chan
->name
, app
->pid
);
1707 health_code_update();
1712 * Enable the specified channel on to UST tracer for the UST session.
1714 static int enable_ust_channel(struct ust_app
*app
,
1715 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1719 health_code_update();
1721 pthread_mutex_lock(&app
->sock_lock
);
1722 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1723 pthread_mutex_unlock(&app
->sock_lock
);
1725 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1726 ERR("UST app channel %s enable failed for app (pid: %d) "
1727 "and session handle %d with ret %d",
1728 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1731 * This is normal behavior, an application can die during the
1732 * creation process. Don't report an error so the execution can
1733 * continue normally.
1736 DBG3("UST app enable channel failed. Application is dead.");
1741 ua_chan
->enabled
= 1;
1743 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1744 ua_chan
->name
, app
->pid
);
1747 health_code_update();
1752 * Enable the specified event on to UST tracer for the UST session.
1754 static int enable_ust_object(
1755 struct ust_app
*app
, struct lttng_ust_object_data
*ust_object
)
1759 health_code_update();
1761 pthread_mutex_lock(&app
->sock_lock
);
1762 ret
= ustctl_enable(app
->sock
, ust_object
);
1763 pthread_mutex_unlock(&app
->sock_lock
);
1765 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1766 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
1767 ust_object
, app
->pid
, ret
);
1770 * This is normal behavior, an application can die during the
1771 * creation process. Don't report an error so the execution can
1772 * continue normally.
1775 DBG3("Failed to enable UST app object. Application is dead.");
1780 DBG2("UST app object %p enabled successfully for app (pid: %d)",
1781 ust_object
, app
->pid
);
1784 health_code_update();
1789 * Send channel and stream buffer to application.
1791 * Return 0 on success. On error, a negative value is returned.
1793 static int send_channel_pid_to_ust(struct ust_app
*app
,
1794 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1797 struct ust_app_stream
*stream
, *stmp
;
1803 health_code_update();
1805 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1808 /* Send channel to the application. */
1809 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1810 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1811 ret
= -ENOTCONN
; /* Caused by app exiting. */
1813 } else if (ret
< 0) {
1817 health_code_update();
1819 /* Send all streams to application. */
1820 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1821 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1822 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1823 ret
= -ENOTCONN
; /* Caused by app exiting. */
1825 } else if (ret
< 0) {
1828 /* We don't need the stream anymore once sent to the tracer. */
1829 cds_list_del(&stream
->list
);
1830 delete_ust_app_stream(-1, stream
, app
);
1832 /* Flag the channel that it is sent to the application. */
1833 ua_chan
->is_sent
= 1;
1836 health_code_update();
1841 * Create the specified event onto the UST tracer for a UST session.
1843 * Should be called with session mutex held.
1846 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1847 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1851 health_code_update();
1853 /* Create UST event on tracer */
1854 pthread_mutex_lock(&app
->sock_lock
);
1855 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1857 pthread_mutex_unlock(&app
->sock_lock
);
1859 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1861 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1862 ua_event
->attr
.name
, app
->pid
, ret
);
1865 * This is normal behavior, an application can die during the
1866 * creation process. Don't report an error so the execution can
1867 * continue normally.
1870 DBG3("UST app create event failed. Application is dead.");
1875 ua_event
->handle
= ua_event
->obj
->handle
;
1877 DBG2("UST app event %s created successfully for pid:%d object: %p",
1878 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
1880 health_code_update();
1882 /* Set filter if one is present. */
1883 if (ua_event
->filter
) {
1884 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
1890 /* Set exclusions for the event */
1891 if (ua_event
->exclusion
) {
1892 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
1898 /* If event not enabled, disable it on the tracer */
1899 if (ua_event
->enabled
) {
1901 * We now need to explicitly enable the event, since it
1902 * is now disabled at creation.
1904 ret
= enable_ust_object(app
, ua_event
->obj
);
1907 * If we hit an EPERM, something is wrong with our enable call. If
1908 * we get an EEXIST, there is a problem on the tracer side since we
1912 case -LTTNG_UST_ERR_PERM
:
1913 /* Code flow problem */
1915 case -LTTNG_UST_ERR_EXIST
:
1916 /* It's OK for our use case. */
1927 health_code_update();
1931 static int init_ust_event_notifier_from_event_rule(
1932 const struct lttng_event_rule
*rule
,
1933 struct lttng_ust_event_notifier
*event_notifier
)
1935 enum lttng_event_rule_status status
;
1936 enum lttng_loglevel_type loglevel_type
;
1937 enum lttng_ust_loglevel_type ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1938 int loglevel
= -1, ret
= 0;
1939 const char *pattern
;
1941 /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported. */
1942 assert(lttng_event_rule_get_type(rule
) ==
1943 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
1945 memset(event_notifier
, 0, sizeof(*event_notifier
));
1947 status
= lttng_event_rule_tracepoint_get_pattern(rule
, &pattern
);
1948 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1949 /* At this point, this is a fatal error. */
1953 status
= lttng_event_rule_tracepoint_get_log_level_type(
1954 rule
, &loglevel_type
);
1955 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
1956 /* At this point, this is a fatal error. */
1960 switch (loglevel_type
) {
1961 case LTTNG_EVENT_LOGLEVEL_ALL
:
1962 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
1964 case LTTNG_EVENT_LOGLEVEL_RANGE
:
1965 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
1967 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
1968 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
1971 /* Unknown log level specification type. */
1975 if (loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
1976 status
= lttng_event_rule_tracepoint_get_log_level(
1978 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
1981 event_notifier
->event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
1982 ret
= lttng_strncpy(event_notifier
->event
.name
, pattern
,
1983 LTTNG_UST_SYM_NAME_LEN
- 1);
1985 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
1990 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
1991 event_notifier
->event
.loglevel
= loglevel
;
1997 * Create the specified event notifier against the user space tracer of a
1998 * given application.
2000 static int create_ust_event_notifier(struct ust_app
*app
,
2001 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2004 struct lttng_ust_event_notifier event_notifier
;
2006 health_code_update();
2007 assert(app
->event_notifier_group
.object
);
2009 ret
= init_ust_event_notifier_from_event_rule(
2010 ua_event_notifier_rule
->event_rule
, &event_notifier
);
2012 ERR("Failed to initialize UST event notifier from event rule: app = '%s' (ppid: %d)",
2013 app
->name
, app
->ppid
);
2017 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2019 /* Create UST event notifier against the tracer. */
2020 pthread_mutex_lock(&app
->sock_lock
);
2021 ret
= ustctl_create_event_notifier(app
->sock
, &event_notifier
,
2022 app
->event_notifier_group
.object
,
2023 &ua_event_notifier_rule
->obj
);
2024 pthread_mutex_unlock(&app
->sock_lock
);
2026 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2027 ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d",
2028 event_notifier
.event
.name
, app
->name
,
2032 * This is normal behavior, an application can die
2033 * during the creation process. Don't report an error so
2034 * the execution can continue normally.
2037 DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)",
2038 app
->name
, app
->ppid
);
2044 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2046 DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p",
2047 event_notifier
.event
.name
, app
->name
, app
->ppid
,
2048 ua_event_notifier_rule
->obj
);
2050 health_code_update();
2052 /* Set filter if one is present. */
2053 if (ua_event_notifier_rule
->filter
) {
2054 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2055 ua_event_notifier_rule
->obj
);
2061 /* Set exclusions for the event. */
2062 if (ua_event_notifier_rule
->exclusion
) {
2063 ret
= set_ust_object_exclusions(app
,
2064 ua_event_notifier_rule
->exclusion
,
2065 ua_event_notifier_rule
->obj
);
2072 * We now need to explicitly enable the event, since it
2073 * is disabled at creation.
2075 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2078 * If we hit an EPERM, something is wrong with our enable call.
2079 * If we get an EEXIST, there is a problem on the tracer side
2080 * since we just created it.
2083 case -LTTNG_UST_ERR_PERM
:
2084 /* Code flow problem. */
2086 case -LTTNG_UST_ERR_EXIST
:
2087 /* It's OK for our use case. */
2097 ua_event_notifier_rule
->enabled
= true;
2100 health_code_update();
2105 * Copy data between an UST app event and a LTT event.
2107 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2108 struct ltt_ust_event
*uevent
)
2110 size_t exclusion_alloc_size
;
2112 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2113 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2115 ua_event
->enabled
= uevent
->enabled
;
2117 /* Copy event attributes */
2118 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2120 /* Copy filter bytecode */
2121 if (uevent
->filter
) {
2122 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
2123 /* Filter might be NULL here in case of ENONEM. */
2126 /* Copy exclusion data */
2127 if (uevent
->exclusion
) {
2128 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2129 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2130 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
2131 if (ua_event
->exclusion
== NULL
) {
2134 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2135 exclusion_alloc_size
);
2141 * Copy data between an UST app channel and a LTT channel.
2143 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2144 struct ltt_ust_channel
*uchan
)
2146 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2148 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2149 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2151 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2152 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2154 /* Copy event attributes since the layout is different. */
2155 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2156 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2157 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2158 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2159 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2160 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2161 ua_chan
->attr
.output
= uchan
->attr
.output
;
2162 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2165 * Note that the attribute channel type is not set since the channel on the
2166 * tracing registry side does not have this information.
2169 ua_chan
->enabled
= uchan
->enabled
;
2170 ua_chan
->tracing_channel_id
= uchan
->id
;
2172 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2176 * Copy data between a UST app session and a regular LTT session.
2178 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2179 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2181 struct tm
*timeinfo
;
2184 char tmp_shm_path
[PATH_MAX
];
2186 timeinfo
= localtime(&app
->registration_time
);
2187 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2189 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2191 ua_sess
->tracing_id
= usess
->id
;
2192 ua_sess
->id
= get_next_session_id();
2193 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2194 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2195 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2196 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2197 ua_sess
->buffer_type
= usess
->buffer_type
;
2198 ua_sess
->bits_per_long
= app
->bits_per_long
;
2200 /* There is only one consumer object per session possible. */
2201 consumer_output_get(usess
->consumer
);
2202 ua_sess
->consumer
= usess
->consumer
;
2204 ua_sess
->output_traces
= usess
->output_traces
;
2205 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2206 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2207 &usess
->metadata_attr
);
2209 switch (ua_sess
->buffer_type
) {
2210 case LTTNG_BUFFER_PER_PID
:
2211 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2212 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2215 case LTTNG_BUFFER_PER_UID
:
2216 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2217 DEFAULT_UST_TRACE_UID_PATH
,
2218 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2219 app
->bits_per_long
);
2226 PERROR("asprintf UST shadow copy session");
2231 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2232 sizeof(ua_sess
->root_shm_path
));
2233 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2234 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2235 sizeof(ua_sess
->shm_path
));
2236 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2237 if (ua_sess
->shm_path
[0]) {
2238 switch (ua_sess
->buffer_type
) {
2239 case LTTNG_BUFFER_PER_PID
:
2240 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2241 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2242 app
->name
, app
->pid
, datetime
);
2244 case LTTNG_BUFFER_PER_UID
:
2245 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2246 "/" DEFAULT_UST_TRACE_UID_PATH
,
2247 app
->uid
, app
->bits_per_long
);
2254 PERROR("sprintf UST shadow copy session");
2258 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2259 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2260 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2265 consumer_output_put(ua_sess
->consumer
);
2269 * Lookup sesison wrapper.
2272 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2273 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2275 /* Get right UST app session from app */
2276 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2280 * Return ust app session from the app session hashtable using the UST session
2283 static struct ust_app_session
*lookup_session_by_app(
2284 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2286 struct lttng_ht_iter iter
;
2287 struct lttng_ht_node_u64
*node
;
2289 __lookup_session_by_app(usess
, app
, &iter
);
2290 node
= lttng_ht_iter_get_node_u64(&iter
);
2295 return caa_container_of(node
, struct ust_app_session
, node
);
2302 * Setup buffer registry per PID for the given session and application. If none
2303 * is found, a new one is created, added to the global registry and
2304 * initialized. If regp is valid, it's set with the newly created object.
2306 * Return 0 on success or else a negative value.
2308 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2309 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2312 struct buffer_reg_pid
*reg_pid
;
2319 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2322 * This is the create channel path meaning that if there is NO
2323 * registry available, we have to create one for this session.
2325 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2326 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2334 /* Initialize registry. */
2335 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2336 app
->bits_per_long
, app
->uint8_t_alignment
,
2337 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2338 app
->uint64_t_alignment
, app
->long_alignment
,
2339 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2340 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2341 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2342 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2343 ua_sess
->tracing_id
,
2347 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2348 * destroy the buffer registry, because it is always expected
2349 * that if the buffer registry can be found, its ust registry is
2352 buffer_reg_pid_destroy(reg_pid
);
2356 buffer_reg_pid_add(reg_pid
);
2358 DBG3("UST app buffer registry per PID created successfully");
2370 * Setup buffer registry per UID for the given session and application. If none
2371 * is found, a new one is created, added to the global registry and
2372 * initialized. If regp is valid, it's set with the newly created object.
2374 * Return 0 on success or else a negative value.
2376 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2377 struct ust_app_session
*ua_sess
,
2378 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2381 struct buffer_reg_uid
*reg_uid
;
2388 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2391 * This is the create channel path meaning that if there is NO
2392 * registry available, we have to create one for this session.
2394 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2395 LTTNG_DOMAIN_UST
, ®_uid
,
2396 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2404 /* Initialize registry. */
2405 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2406 app
->bits_per_long
, app
->uint8_t_alignment
,
2407 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2408 app
->uint64_t_alignment
, app
->long_alignment
,
2409 app
->byte_order
, app
->version
.major
,
2410 app
->version
.minor
, reg_uid
->root_shm_path
,
2411 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2412 ua_sess
->tracing_id
, app
->uid
);
2415 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2416 * destroy the buffer registry, because it is always expected
2417 * that if the buffer registry can be found, its ust registry is
2420 buffer_reg_uid_destroy(reg_uid
, NULL
);
2423 /* Add node to teardown list of the session. */
2424 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2426 buffer_reg_uid_add(reg_uid
);
2428 DBG3("UST app buffer registry per UID created successfully");
2439 * Create a session on the tracer side for the given app.
2441 * On success, ua_sess_ptr is populated with the session pointer or else left
2442 * untouched. If the session was created, is_created is set to 1. On error,
2443 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2446 * Returns 0 on success or else a negative code which is either -ENOMEM or
2447 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2449 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2450 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2453 int ret
, created
= 0;
2454 struct ust_app_session
*ua_sess
;
2458 assert(ua_sess_ptr
);
2460 health_code_update();
2462 ua_sess
= lookup_session_by_app(usess
, app
);
2463 if (ua_sess
== NULL
) {
2464 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2465 app
->pid
, usess
->id
);
2466 ua_sess
= alloc_ust_app_session();
2467 if (ua_sess
== NULL
) {
2468 /* Only malloc can failed so something is really wrong */
2472 shadow_copy_session(ua_sess
, usess
, app
);
2476 switch (usess
->buffer_type
) {
2477 case LTTNG_BUFFER_PER_PID
:
2478 /* Init local registry. */
2479 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2481 delete_ust_app_session(-1, ua_sess
, app
);
2485 case LTTNG_BUFFER_PER_UID
:
2486 /* Look for a global registry. If none exists, create one. */
2487 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2489 delete_ust_app_session(-1, ua_sess
, app
);
2499 health_code_update();
2501 if (ua_sess
->handle
== -1) {
2502 pthread_mutex_lock(&app
->sock_lock
);
2503 ret
= ustctl_create_session(app
->sock
);
2504 pthread_mutex_unlock(&app
->sock_lock
);
2506 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2507 ERR("Creating session for app pid %d with ret %d",
2510 DBG("UST app creating session failed. Application is dead");
2512 * This is normal behavior, an application can die during the
2513 * creation process. Don't report an error so the execution can
2514 * continue normally. This will get flagged ENOTCONN and the
2515 * caller will handle it.
2519 delete_ust_app_session(-1, ua_sess
, app
);
2520 if (ret
!= -ENOMEM
) {
2522 * Tracer is probably gone or got an internal error so let's
2523 * behave like it will soon unregister or not usable.
2530 ua_sess
->handle
= ret
;
2532 /* Add ust app session to app's HT */
2533 lttng_ht_node_init_u64(&ua_sess
->node
,
2534 ua_sess
->tracing_id
);
2535 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2536 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2537 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2538 &ua_sess
->ust_objd_node
);
2540 DBG2("UST app session created successfully with handle %d", ret
);
2543 *ua_sess_ptr
= ua_sess
;
2545 *is_created
= created
;
2548 /* Everything went well. */
2552 health_code_update();
2557 * Match function for a hash table lookup of ust_app_ctx.
2559 * It matches an ust app context based on the context type and, in the case
2560 * of perf counters, their name.
2562 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2564 struct ust_app_ctx
*ctx
;
2565 const struct lttng_ust_context_attr
*key
;
2570 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2574 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2579 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2580 if (strncmp(key
->u
.perf_counter
.name
,
2581 ctx
->ctx
.u
.perf_counter
.name
,
2582 sizeof(key
->u
.perf_counter
.name
))) {
2586 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2587 if (strcmp(key
->u
.app_ctx
.provider_name
,
2588 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2589 strcmp(key
->u
.app_ctx
.ctx_name
,
2590 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2606 * Lookup for an ust app context from an lttng_ust_context.
2608 * Must be called while holding RCU read side lock.
2609 * Return an ust_app_ctx object or NULL on error.
2612 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2613 struct lttng_ust_context_attr
*uctx
)
2615 struct lttng_ht_iter iter
;
2616 struct lttng_ht_node_ulong
*node
;
2617 struct ust_app_ctx
*app_ctx
= NULL
;
2622 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2623 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2624 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2625 node
= lttng_ht_iter_get_node_ulong(&iter
);
2630 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2637 * Create a context for the channel on the tracer.
2639 * Called with UST app session lock held and a RCU read side lock.
2642 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2643 struct lttng_ust_context_attr
*uctx
,
2644 struct ust_app
*app
)
2647 struct ust_app_ctx
*ua_ctx
;
2649 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2651 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2657 ua_ctx
= alloc_ust_app_ctx(uctx
);
2658 if (ua_ctx
== NULL
) {
2664 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2665 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2666 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2668 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2678 * Enable on the tracer side a ust app event for the session and channel.
2680 * Called with UST app session lock held.
2683 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2684 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2688 ret
= enable_ust_object(app
, ua_event
->obj
);
2693 ua_event
->enabled
= 1;
2700 * Disable on the tracer side a ust app event for the session and channel.
2702 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2703 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2707 ret
= disable_ust_object(app
, ua_event
->obj
);
2712 ua_event
->enabled
= 0;
2719 * Lookup ust app channel for session and disable it on the tracer side.
2722 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2723 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2727 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2732 ua_chan
->enabled
= 0;
2739 * Lookup ust app channel for session and enable it on the tracer side. This
2740 * MUST be called with a RCU read side lock acquired.
2742 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2743 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2746 struct lttng_ht_iter iter
;
2747 struct lttng_ht_node_str
*ua_chan_node
;
2748 struct ust_app_channel
*ua_chan
;
2750 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2751 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2752 if (ua_chan_node
== NULL
) {
2753 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2754 uchan
->name
, ua_sess
->tracing_id
);
2758 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2760 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2770 * Ask the consumer to create a channel and get it if successful.
2772 * Called with UST app session lock held.
2774 * Return 0 on success or else a negative value.
2776 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2777 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2778 int bitness
, struct ust_registry_session
*registry
,
2779 uint64_t trace_archive_id
)
2782 unsigned int nb_fd
= 0;
2783 struct consumer_socket
*socket
;
2791 health_code_update();
2793 /* Get the right consumer socket for the application. */
2794 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2800 health_code_update();
2802 /* Need one fd for the channel. */
2803 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2805 ERR("Exhausted number of available FD upon create channel");
2810 * Ask consumer to create channel. The consumer will return the number of
2811 * stream we have to expect.
2813 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2814 registry
, usess
->current_trace_chunk
);
2820 * Compute the number of fd needed before receiving them. It must be 2 per
2821 * stream (2 being the default value here).
2823 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2825 /* Reserve the amount of file descriptor we need. */
2826 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2828 ERR("Exhausted number of available FD upon create channel");
2829 goto error_fd_get_stream
;
2832 health_code_update();
2835 * Now get the channel from the consumer. This call wil populate the stream
2836 * list of that channel and set the ust objects.
2838 if (usess
->consumer
->enabled
) {
2839 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2849 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2850 error_fd_get_stream
:
2852 * Initiate a destroy channel on the consumer since we had an error
2853 * handling it on our side. The return value is of no importance since we
2854 * already have a ret value set by the previous error that we need to
2857 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2859 lttng_fd_put(LTTNG_FD_APPS
, 1);
2861 health_code_update();
2867 * Duplicate the ust data object of the ust app stream and save it in the
2868 * buffer registry stream.
2870 * Return 0 on success or else a negative value.
2872 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2873 struct ust_app_stream
*stream
)
2880 /* Reserve the amount of file descriptor we need. */
2881 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2883 ERR("Exhausted number of available FD upon duplicate stream");
2887 /* Duplicate object for stream once the original is in the registry. */
2888 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2889 reg_stream
->obj
.ust
);
2891 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2892 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2893 lttng_fd_put(LTTNG_FD_APPS
, 2);
2896 stream
->handle
= stream
->obj
->handle
;
2903 * Duplicate the ust data object of the ust app. channel and save it in the
2904 * buffer registry channel.
2906 * Return 0 on success or else a negative value.
2908 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2909 struct ust_app_channel
*ua_chan
)
2916 /* Need two fds for the channel. */
2917 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2919 ERR("Exhausted number of available FD upon duplicate channel");
2923 /* Duplicate object for stream once the original is in the registry. */
2924 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2926 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2927 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2930 ua_chan
->handle
= ua_chan
->obj
->handle
;
2935 lttng_fd_put(LTTNG_FD_APPS
, 1);
2941 * For a given channel buffer registry, setup all streams of the given ust
2942 * application channel.
2944 * Return 0 on success or else a negative value.
2946 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2947 struct ust_app_channel
*ua_chan
,
2948 struct ust_app
*app
)
2951 struct ust_app_stream
*stream
, *stmp
;
2956 DBG2("UST app setup buffer registry stream");
2958 /* Send all streams to application. */
2959 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2960 struct buffer_reg_stream
*reg_stream
;
2962 ret
= buffer_reg_stream_create(®_stream
);
2968 * Keep original pointer and nullify it in the stream so the delete
2969 * stream call does not release the object.
2971 reg_stream
->obj
.ust
= stream
->obj
;
2973 buffer_reg_stream_add(reg_stream
, reg_chan
);
2975 /* We don't need the streams anymore. */
2976 cds_list_del(&stream
->list
);
2977 delete_ust_app_stream(-1, stream
, app
);
2985 * Create a buffer registry channel for the given session registry and
2986 * application channel object. If regp pointer is valid, it's set with the
2987 * created object. Important, the created object is NOT added to the session
2988 * registry hash table.
2990 * Return 0 on success else a negative value.
2992 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2993 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2996 struct buffer_reg_channel
*reg_chan
= NULL
;
3001 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3003 /* Create buffer registry channel. */
3004 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
3009 reg_chan
->consumer_key
= ua_chan
->key
;
3010 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3011 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3013 /* Create and add a channel registry to session. */
3014 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
3015 ua_chan
->tracing_channel_id
);
3019 buffer_reg_channel_add(reg_sess
, reg_chan
);
3028 /* Safe because the registry channel object was not added to any HT. */
3029 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3035 * Setup buffer registry channel for the given session registry and application
3036 * channel object. If regp pointer is valid, it's set with the created object.
3038 * Return 0 on success else a negative value.
3040 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3041 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
3042 struct ust_app
*app
)
3049 assert(ua_chan
->obj
);
3051 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3053 /* Setup all streams for the registry. */
3054 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
3059 reg_chan
->obj
.ust
= ua_chan
->obj
;
3060 ua_chan
->obj
= NULL
;
3065 buffer_reg_channel_remove(reg_sess
, reg_chan
);
3066 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3071 * Send buffer registry channel to the application.
3073 * Return 0 on success else a negative value.
3075 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
3076 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
3077 struct ust_app_channel
*ua_chan
)
3080 struct buffer_reg_stream
*reg_stream
;
3087 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3089 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
3094 /* Send channel to the application. */
3095 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3096 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3097 ret
= -ENOTCONN
; /* Caused by app exiting. */
3099 } else if (ret
< 0) {
3103 health_code_update();
3105 /* Send all streams to application. */
3106 pthread_mutex_lock(®_chan
->stream_list_lock
);
3107 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
3108 struct ust_app_stream stream
;
3110 ret
= duplicate_stream_object(reg_stream
, &stream
);
3112 goto error_stream_unlock
;
3115 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3117 (void) release_ust_app_stream(-1, &stream
, app
);
3118 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3119 ret
= -ENOTCONN
; /* Caused by app exiting. */
3121 goto error_stream_unlock
;
3125 * The return value is not important here. This function will output an
3128 (void) release_ust_app_stream(-1, &stream
, app
);
3130 ua_chan
->is_sent
= 1;
3132 error_stream_unlock
:
3133 pthread_mutex_unlock(®_chan
->stream_list_lock
);
3139 * Create and send to the application the created buffers with per UID buffers.
3141 * This MUST be called with a RCU read side lock acquired.
3142 * The session list lock and the session's lock must be acquired.
3144 * Return 0 on success else a negative value.
3146 static int create_channel_per_uid(struct ust_app
*app
,
3147 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3148 struct ust_app_channel
*ua_chan
)
3151 struct buffer_reg_uid
*reg_uid
;
3152 struct buffer_reg_channel
*reg_chan
;
3153 struct ltt_session
*session
= NULL
;
3154 enum lttng_error_code notification_ret
;
3155 struct ust_registry_channel
*chan_reg
;
3162 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3164 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3166 * The session creation handles the creation of this global registry
3167 * object. If none can be find, there is a code flow problem or a
3172 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3178 /* Create the buffer registry channel object. */
3179 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
3181 ERR("Error creating the UST channel \"%s\" registry instance",
3186 session
= session_find_by_id(ua_sess
->tracing_id
);
3188 assert(pthread_mutex_trylock(&session
->lock
));
3189 assert(session_trylock_list());
3192 * Create the buffers on the consumer side. This call populates the
3193 * ust app channel object with all streams and data object.
3195 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3196 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
3197 session
->most_recent_chunk_id
.value
);
3199 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3203 * Let's remove the previously created buffer registry channel so
3204 * it's not visible anymore in the session registry.
3206 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3207 ua_chan
->tracing_channel_id
, false);
3208 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
3209 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3214 * Setup the streams and add it to the session registry.
3216 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3217 ua_chan
, reg_chan
, app
);
3219 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3223 /* Notify the notification subsystem of the channel's creation. */
3224 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
3225 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3226 ua_chan
->tracing_channel_id
);
3228 chan_reg
->consumer_key
= ua_chan
->key
;
3230 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
3232 notification_ret
= notification_thread_command_add_channel(
3233 notification_thread_handle
, session
->name
,
3234 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3235 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3237 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3238 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3239 if (notification_ret
!= LTTNG_OK
) {
3240 ret
= - (int) notification_ret
;
3241 ERR("Failed to add channel to notification thread");
3246 /* Send buffers to the application. */
3247 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
3249 if (ret
!= -ENOTCONN
) {
3250 ERR("Error sending channel to application");
3257 session_put(session
);
3263 * Create and send to the application the created buffers with per PID buffers.
3265 * Called with UST app session lock held.
3266 * The session list lock and the session's lock must be acquired.
3268 * Return 0 on success else a negative value.
3270 static int create_channel_per_pid(struct ust_app
*app
,
3271 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3272 struct ust_app_channel
*ua_chan
)
3275 struct ust_registry_session
*registry
;
3276 enum lttng_error_code cmd_ret
;
3277 struct ltt_session
*session
= NULL
;
3278 uint64_t chan_reg_key
;
3279 struct ust_registry_channel
*chan_reg
;
3286 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3290 registry
= get_session_registry(ua_sess
);
3291 /* The UST app session lock is held, registry shall not be null. */
3294 /* Create and add a new channel registry to session. */
3295 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3297 ERR("Error creating the UST channel \"%s\" registry instance",
3302 session
= session_find_by_id(ua_sess
->tracing_id
);
3305 assert(pthread_mutex_trylock(&session
->lock
));
3306 assert(session_trylock_list());
3308 /* Create and get channel on the consumer side. */
3309 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3310 app
->bits_per_long
, registry
,
3311 session
->most_recent_chunk_id
.value
);
3313 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3315 goto error_remove_from_registry
;
3318 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3320 if (ret
!= -ENOTCONN
) {
3321 ERR("Error sending channel to application");
3323 goto error_remove_from_registry
;
3326 chan_reg_key
= ua_chan
->key
;
3327 pthread_mutex_lock(®istry
->lock
);
3328 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3330 chan_reg
->consumer_key
= ua_chan
->key
;
3331 pthread_mutex_unlock(®istry
->lock
);
3333 cmd_ret
= notification_thread_command_add_channel(
3334 notification_thread_handle
, session
->name
,
3335 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3336 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3338 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3339 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3340 if (cmd_ret
!= LTTNG_OK
) {
3341 ret
= - (int) cmd_ret
;
3342 ERR("Failed to add channel to notification thread");
3343 goto error_remove_from_registry
;
3346 error_remove_from_registry
:
3348 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3353 session_put(session
);
3359 * From an already allocated ust app channel, create the channel buffers if
3360 * needed and send them to the application. This MUST be called with a RCU read
3361 * side lock acquired.
3363 * Called with UST app session lock held.
3365 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3366 * the application exited concurrently.
3368 static int ust_app_channel_send(struct ust_app
*app
,
3369 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3370 struct ust_app_channel
*ua_chan
)
3376 assert(usess
->active
);
3380 /* Handle buffer type before sending the channel to the application. */
3381 switch (usess
->buffer_type
) {
3382 case LTTNG_BUFFER_PER_UID
:
3384 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3390 case LTTNG_BUFFER_PER_PID
:
3392 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3404 /* Initialize ust objd object using the received handle and add it. */
3405 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3406 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3408 /* If channel is not enabled, disable it on the tracer */
3409 if (!ua_chan
->enabled
) {
3410 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3421 * Create UST app channel and return it through ua_chanp if not NULL.
3423 * Called with UST app session lock and RCU read-side lock held.
3425 * Return 0 on success or else a negative value.
3427 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3428 struct ltt_ust_channel
*uchan
,
3429 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3430 struct ust_app_channel
**ua_chanp
)
3433 struct lttng_ht_iter iter
;
3434 struct lttng_ht_node_str
*ua_chan_node
;
3435 struct ust_app_channel
*ua_chan
;
3437 /* Lookup channel in the ust app session */
3438 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3439 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3440 if (ua_chan_node
!= NULL
) {
3441 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3445 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3446 if (ua_chan
== NULL
) {
3447 /* Only malloc can fail here */
3451 shadow_copy_channel(ua_chan
, uchan
);
3453 /* Set channel type. */
3454 ua_chan
->attr
.type
= type
;
3456 /* Only add the channel if successful on the tracer side. */
3457 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3460 *ua_chanp
= ua_chan
;
3463 /* Everything went well. */
3471 * Create UST app event and create it on the tracer side.
3473 * Must be called with the RCU read side lock held.
3474 * Called with ust app session mutex held.
3477 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3478 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3479 struct ust_app
*app
)
3482 struct ust_app_event
*ua_event
;
3484 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3485 if (ua_event
== NULL
) {
3486 /* Only failure mode of alloc_ust_app_event(). */
3490 shadow_copy_event(ua_event
, uevent
);
3492 /* Create it on the tracer side */
3493 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3496 * Not found previously means that it does not exist on the
3497 * tracer. If the application reports that the event existed,
3498 * it means there is a bug in the sessiond or lttng-ust
3499 * (or corruption, etc.)
3501 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3502 ERR("Tracer for application reported that an event being created already existed: "
3503 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3505 app
->pid
, app
->ppid
, app
->uid
,
3511 add_unique_ust_app_event(ua_chan
, ua_event
);
3513 DBG2("UST app create event completed: app = '%s' (ppid: %d)",
3514 app
->name
, app
->ppid
);
3520 /* Valid. Calling here is already in a read side lock */
3521 delete_ust_app_event(-1, ua_event
, app
);
3526 * Create UST app event notifier rule and create it on the tracer side.
3528 * Must be called with the RCU read side lock held.
3529 * Called with ust app session mutex held.
3532 int create_ust_app_event_notifier_rule(struct lttng_event_rule
*rule
,
3533 struct ust_app
*app
, uint64_t token
)
3536 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3538 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(rule
, token
);
3539 if (ua_event_notifier_rule
== NULL
) {
3544 /* Create it on the tracer side. */
3545 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3548 * Not found previously means that it does not exist on the
3549 * tracer. If the application reports that the event existed,
3550 * it means there is a bug in the sessiond or lttng-ust
3551 * (or corruption, etc.)
3553 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3554 ERR("Tracer for application reported that an event notifier being created already exists: "
3555 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3557 app
->pid
, app
->ppid
, app
->uid
,
3563 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3564 &ua_event_notifier_rule
->node
);
3566 DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64
,
3567 app
->name
, app
->ppid
, token
);
3573 /* The RCU read side lock is already being held by the caller. */
3574 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3579 * Create UST metadata and open it on the tracer side.
3581 * Called with UST app session lock held and RCU read side lock.
3583 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3584 struct ust_app
*app
, struct consumer_output
*consumer
)
3587 struct ust_app_channel
*metadata
;
3588 struct consumer_socket
*socket
;
3589 struct ust_registry_session
*registry
;
3590 struct ltt_session
*session
= NULL
;
3596 registry
= get_session_registry(ua_sess
);
3597 /* The UST app session is held registry shall not be null. */
3600 pthread_mutex_lock(®istry
->lock
);
3602 /* Metadata already exists for this registry or it was closed previously */
3603 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3608 /* Allocate UST metadata */
3609 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3611 /* malloc() failed */
3616 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3618 /* Need one fd for the channel. */
3619 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3621 ERR("Exhausted number of available FD upon create metadata");
3625 /* Get the right consumer socket for the application. */
3626 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3629 goto error_consumer
;
3633 * Keep metadata key so we can identify it on the consumer side. Assign it
3634 * to the registry *before* we ask the consumer so we avoid the race of the
3635 * consumer requesting the metadata and the ask_channel call on our side
3636 * did not returned yet.
3638 registry
->metadata_key
= metadata
->key
;
3640 session
= session_find_by_id(ua_sess
->tracing_id
);
3643 assert(pthread_mutex_trylock(&session
->lock
));
3644 assert(session_trylock_list());
3647 * Ask the metadata channel creation to the consumer. The metadata object
3648 * will be created by the consumer and kept their. However, the stream is
3649 * never added or monitored until we do a first push metadata to the
3652 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3653 registry
, session
->current_trace_chunk
);
3655 /* Nullify the metadata key so we don't try to close it later on. */
3656 registry
->metadata_key
= 0;
3657 goto error_consumer
;
3661 * The setup command will make the metadata stream be sent to the relayd,
3662 * if applicable, and the thread managing the metadatas. This is important
3663 * because after this point, if an error occurs, the only way the stream
3664 * can be deleted is to be monitored in the consumer.
3666 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3668 /* Nullify the metadata key so we don't try to close it later on. */
3669 registry
->metadata_key
= 0;
3670 goto error_consumer
;
3673 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3674 metadata
->key
, app
->pid
);
3677 lttng_fd_put(LTTNG_FD_APPS
, 1);
3678 delete_ust_app_channel(-1, metadata
, app
);
3680 pthread_mutex_unlock(®istry
->lock
);
3682 session_put(session
);
3688 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3689 * acquired before calling this function.
3691 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3693 struct ust_app
*app
= NULL
;
3694 struct lttng_ht_node_ulong
*node
;
3695 struct lttng_ht_iter iter
;
3697 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3698 node
= lttng_ht_iter_get_node_ulong(&iter
);
3700 DBG2("UST app no found with pid %d", pid
);
3704 DBG2("Found UST app by pid %d", pid
);
3706 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3713 * Allocate and init an UST app object using the registration information and
3714 * the command socket. This is called when the command socket connects to the
3717 * The object is returned on success or else NULL.
3719 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3722 struct ust_app
*lta
= NULL
;
3723 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3728 DBG3("UST app creating application for socket %d", sock
);
3730 if ((msg
->bits_per_long
== 64 &&
3731 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3732 || (msg
->bits_per_long
== 32 &&
3733 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3734 ERR("Registration failed: application \"%s\" (pid: %d) has "
3735 "%d-bit long, but no consumerd for this size is available.\n",
3736 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3741 * Reserve the two file descriptors of the event source pipe. The write
3742 * end will be closed once it is passed to the application, at which
3743 * point a single 'put' will be performed.
3745 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3747 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s' (ppid: %d)",
3748 msg
->name
, (int) msg
->ppid
);
3752 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3753 if (!event_notifier_event_source_pipe
) {
3754 PERROR("Failed to open application event source pipe: '%s' (ppid = %d)",
3755 msg
->name
, msg
->ppid
);
3759 lta
= zmalloc(sizeof(struct ust_app
));
3762 goto error_free_pipe
;
3765 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
3767 lta
->ppid
= msg
->ppid
;
3768 lta
->uid
= msg
->uid
;
3769 lta
->gid
= msg
->gid
;
3771 lta
->bits_per_long
= msg
->bits_per_long
;
3772 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3773 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3774 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3775 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3776 lta
->long_alignment
= msg
->long_alignment
;
3777 lta
->byte_order
= msg
->byte_order
;
3779 lta
->v_major
= msg
->major
;
3780 lta
->v_minor
= msg
->minor
;
3781 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3782 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3783 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3784 lta
->notify_sock
= -1;
3785 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3787 /* Copy name and make sure it's NULL terminated. */
3788 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3789 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3792 * Before this can be called, when receiving the registration information,
3793 * the application compatibility is checked. So, at this point, the
3794 * application can work with this session daemon.
3796 lta
->compatible
= 1;
3798 lta
->pid
= msg
->pid
;
3799 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3801 pthread_mutex_init(<a
->sock_lock
, NULL
);
3802 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3804 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3808 lttng_pipe_destroy(event_notifier_event_source_pipe
);
3809 lttng_fd_put(LTTNG_FD_APPS
, 2);
3815 * For a given application object, add it to every hash table.
3817 void ust_app_add(struct ust_app
*app
)
3820 assert(app
->notify_sock
>= 0);
3822 app
->registration_time
= time(NULL
);
3827 * On a re-registration, we want to kick out the previous registration of
3830 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3833 * The socket _should_ be unique until _we_ call close. So, a add_unique
3834 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3835 * already in the table.
3837 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3839 /* Add application to the notify socket hash table. */
3840 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3841 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3843 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3844 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3845 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3852 * Set the application version into the object.
3854 * Return 0 on success else a negative value either an errno code or a
3855 * LTTng-UST error code.
3857 int ust_app_version(struct ust_app
*app
)
3863 pthread_mutex_lock(&app
->sock_lock
);
3864 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3865 pthread_mutex_unlock(&app
->sock_lock
);
3867 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3868 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3870 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3878 * Setup the base event notifier group.
3880 * Return 0 on success else a negative value either an errno code or a
3881 * LTTng-UST error code.
3883 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
3886 int event_pipe_write_fd
;
3887 struct lttng_ust_object_data
*event_notifier_group
= NULL
;
3888 enum lttng_error_code lttng_ret
;
3892 /* Get the write side of the pipe. */
3893 event_pipe_write_fd
= lttng_pipe_get_writefd(
3894 app
->event_notifier_group
.event_pipe
);
3896 pthread_mutex_lock(&app
->sock_lock
);
3897 ret
= ustctl_create_event_notifier_group(app
->sock
,
3898 event_pipe_write_fd
, &event_notifier_group
);
3899 pthread_mutex_unlock(&app
->sock_lock
);
3901 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3902 ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d",
3903 ret
, app
->sock
, event_pipe_write_fd
);
3905 DBG("Failed to create application event notifier group (application is dead): app socket fd = %d",
3912 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
3914 ERR("Failed to close write end of the application's event source pipe: app = '%s' (ppid = %d)",
3915 app
->name
, app
->ppid
);
3920 * Release the file descriptor that was reserved for the write-end of
3923 lttng_fd_put(LTTNG_FD_APPS
, 1);
3925 lttng_ret
= notification_thread_command_add_tracer_event_source(
3926 notification_thread_handle
,
3927 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
),
3929 if (lttng_ret
!= LTTNG_OK
) {
3930 ERR("Failed to add tracer event source to notification thread");
3935 /* Assign handle only when the complete setup is valid. */
3936 app
->event_notifier_group
.object
= event_notifier_group
;
3940 ustctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
3941 free(app
->event_notifier_group
.object
);
3946 * Unregister app by removing it from the global traceable app list and freeing
3949 * The socket is already closed at this point so no close to sock.
3951 void ust_app_unregister(int sock
)
3953 struct ust_app
*lta
;
3954 struct lttng_ht_node_ulong
*node
;
3955 struct lttng_ht_iter ust_app_sock_iter
;
3956 struct lttng_ht_iter iter
;
3957 struct ust_app_session
*ua_sess
;
3962 /* Get the node reference for a call_rcu */
3963 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3964 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3967 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3968 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3971 * For per-PID buffers, perform "push metadata" and flush all
3972 * application streams before removing app from hash tables,
3973 * ensuring proper behavior of data_pending check.
3974 * Remove sessions so they are not visible during deletion.
3976 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3978 struct ust_registry_session
*registry
;
3980 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3982 /* The session was already removed so scheduled for teardown. */
3986 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3987 (void) ust_app_flush_app_session(lta
, ua_sess
);
3991 * Add session to list for teardown. This is safe since at this point we
3992 * are the only one using this list.
3994 pthread_mutex_lock(&ua_sess
->lock
);
3996 if (ua_sess
->deleted
) {
3997 pthread_mutex_unlock(&ua_sess
->lock
);
4002 * Normally, this is done in the delete session process which is
4003 * executed in the call rcu below. However, upon registration we can't
4004 * afford to wait for the grace period before pushing data or else the
4005 * data pending feature can race between the unregistration and stop
4006 * command where the data pending command is sent *before* the grace
4009 * The close metadata below nullifies the metadata pointer in the
4010 * session so the delete session will NOT push/close a second time.
4012 registry
= get_session_registry(ua_sess
);
4014 /* Push metadata for application before freeing the application. */
4015 (void) push_metadata(registry
, ua_sess
->consumer
);
4018 * Don't ask to close metadata for global per UID buffers. Close
4019 * metadata only on destroy trace session in this case. Also, the
4020 * previous push metadata could have flag the metadata registry to
4021 * close so don't send a close command if closed.
4023 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4024 /* And ask to close it for this session registry. */
4025 (void) close_metadata(registry
, ua_sess
->consumer
);
4028 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4030 pthread_mutex_unlock(&ua_sess
->lock
);
4033 /* Remove application from PID hash table */
4034 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4038 * Remove application from notify hash table. The thread handling the
4039 * notify socket could have deleted the node so ignore on error because
4040 * either way it's valid. The close of that socket is handled by the
4041 * apps_notify_thread.
4043 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4044 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4047 * Ignore return value since the node might have been removed before by an
4048 * add replace during app registration because the PID can be reassigned by
4051 iter
.iter
.node
= <a
->pid_n
.node
;
4052 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4054 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4059 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4066 * Fill events array with all events name of all registered apps.
4068 int ust_app_list_events(struct lttng_event
**events
)
4071 size_t nbmem
, count
= 0;
4072 struct lttng_ht_iter iter
;
4073 struct ust_app
*app
;
4074 struct lttng_event
*tmp_event
;
4076 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4077 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
4078 if (tmp_event
== NULL
) {
4079 PERROR("zmalloc ust app events");
4086 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4087 struct lttng_ust_tracepoint_iter uiter
;
4089 health_code_update();
4091 if (!app
->compatible
) {
4093 * TODO: In time, we should notice the caller of this error by
4094 * telling him that this is a version error.
4098 pthread_mutex_lock(&app
->sock_lock
);
4099 handle
= ustctl_tracepoint_list(app
->sock
);
4101 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4102 ERR("UST app list events getting handle failed for app pid %d",
4105 pthread_mutex_unlock(&app
->sock_lock
);
4109 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
4110 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4111 /* Handle ustctl error. */
4115 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4116 ERR("UST app tp list get failed for app %d with ret %d",
4119 DBG3("UST app tp list get failed. Application is dead");
4121 * This is normal behavior, an application can die during the
4122 * creation process. Don't report an error so the execution can
4123 * continue normally. Continue normal execution.
4128 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4129 if (release_ret
< 0 &&
4130 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4131 release_ret
!= -EPIPE
) {
4132 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4134 pthread_mutex_unlock(&app
->sock_lock
);
4138 health_code_update();
4139 if (count
>= nbmem
) {
4140 /* In case the realloc fails, we free the memory */
4141 struct lttng_event
*new_tmp_event
;
4144 new_nbmem
= nbmem
<< 1;
4145 DBG2("Reallocating event list from %zu to %zu entries",
4147 new_tmp_event
= realloc(tmp_event
,
4148 new_nbmem
* sizeof(struct lttng_event
));
4149 if (new_tmp_event
== NULL
) {
4152 PERROR("realloc ust app events");
4155 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4156 if (release_ret
< 0 &&
4157 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4158 release_ret
!= -EPIPE
) {
4159 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4161 pthread_mutex_unlock(&app
->sock_lock
);
4164 /* Zero the new memory */
4165 memset(new_tmp_event
+ nbmem
, 0,
4166 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4168 tmp_event
= new_tmp_event
;
4170 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
4171 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4172 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
4173 tmp_event
[count
].pid
= app
->pid
;
4174 tmp_event
[count
].enabled
= -1;
4177 ret
= ustctl_release_handle(app
->sock
, handle
);
4178 pthread_mutex_unlock(&app
->sock_lock
);
4179 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4180 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4185 *events
= tmp_event
;
4187 DBG2("UST app list events done (%zu events)", count
);
4192 health_code_update();
4197 * Fill events array with all events name of all registered apps.
4199 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4202 size_t nbmem
, count
= 0;
4203 struct lttng_ht_iter iter
;
4204 struct ust_app
*app
;
4205 struct lttng_event_field
*tmp_event
;
4207 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4208 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
4209 if (tmp_event
== NULL
) {
4210 PERROR("zmalloc ust app event fields");
4217 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4218 struct lttng_ust_field_iter uiter
;
4220 health_code_update();
4222 if (!app
->compatible
) {
4224 * TODO: In time, we should notice the caller of this error by
4225 * telling him that this is a version error.
4229 pthread_mutex_lock(&app
->sock_lock
);
4230 handle
= ustctl_tracepoint_field_list(app
->sock
);
4232 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4233 ERR("UST app list field getting handle failed for app pid %d",
4236 pthread_mutex_unlock(&app
->sock_lock
);
4240 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
4241 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4242 /* Handle ustctl error. */
4246 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4247 ERR("UST app tp list field failed for app %d with ret %d",
4250 DBG3("UST app tp list field failed. Application is dead");
4252 * This is normal behavior, an application can die during the
4253 * creation process. Don't report an error so the execution can
4254 * continue normally. Reset list and count for next app.
4259 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4260 pthread_mutex_unlock(&app
->sock_lock
);
4261 if (release_ret
< 0 &&
4262 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4263 release_ret
!= -EPIPE
) {
4264 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4269 health_code_update();
4270 if (count
>= nbmem
) {
4271 /* In case the realloc fails, we free the memory */
4272 struct lttng_event_field
*new_tmp_event
;
4275 new_nbmem
= nbmem
<< 1;
4276 DBG2("Reallocating event field list from %zu to %zu entries",
4278 new_tmp_event
= realloc(tmp_event
,
4279 new_nbmem
* sizeof(struct lttng_event_field
));
4280 if (new_tmp_event
== NULL
) {
4283 PERROR("realloc ust app event fields");
4286 release_ret
= ustctl_release_handle(app
->sock
, handle
);
4287 pthread_mutex_unlock(&app
->sock_lock
);
4289 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4290 release_ret
!= -EPIPE
) {
4291 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4295 /* Zero the new memory */
4296 memset(new_tmp_event
+ nbmem
, 0,
4297 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4299 tmp_event
= new_tmp_event
;
4302 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
4303 /* Mapping between these enums matches 1 to 1. */
4304 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4305 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4307 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
4308 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4309 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4310 tmp_event
[count
].event
.pid
= app
->pid
;
4311 tmp_event
[count
].event
.enabled
= -1;
4314 ret
= ustctl_release_handle(app
->sock
, handle
);
4315 pthread_mutex_unlock(&app
->sock_lock
);
4317 ret
!= -LTTNG_UST_ERR_EXITING
&&
4319 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4324 *fields
= tmp_event
;
4326 DBG2("UST app list event fields done (%zu events)", count
);
4331 health_code_update();
4336 * Free and clean all traceable apps of the global list.
4338 * Should _NOT_ be called with RCU read-side lock held.
4340 void ust_app_clean_list(void)
4343 struct ust_app
*app
;
4344 struct lttng_ht_iter iter
;
4346 DBG2("UST app cleaning registered apps hash table");
4350 /* Cleanup notify socket hash table */
4351 if (ust_app_ht_by_notify_sock
) {
4352 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4353 notify_sock_n
.node
) {
4355 * Assert that all notifiers are gone as all triggers
4356 * are unregistered prior to this clean-up.
4358 assert(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4360 ust_app_notify_sock_unregister(app
->notify_sock
);
4365 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4366 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4368 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4372 /* Cleanup socket hash table */
4373 if (ust_app_ht_by_sock
) {
4374 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4376 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4383 /* Destroy is done only when the ht is empty */
4385 ht_cleanup_push(ust_app_ht
);
4387 if (ust_app_ht_by_sock
) {
4388 ht_cleanup_push(ust_app_ht_by_sock
);
4390 if (ust_app_ht_by_notify_sock
) {
4391 ht_cleanup_push(ust_app_ht_by_notify_sock
);
4396 * Init UST app hash table.
4398 int ust_app_ht_alloc(void)
4400 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4404 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4405 if (!ust_app_ht_by_sock
) {
4408 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4409 if (!ust_app_ht_by_notify_sock
) {
4416 * For a specific UST session, disable the channel for all registered apps.
4418 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4419 struct ltt_ust_channel
*uchan
)
4422 struct lttng_ht_iter iter
;
4423 struct lttng_ht_node_str
*ua_chan_node
;
4424 struct ust_app
*app
;
4425 struct ust_app_session
*ua_sess
;
4426 struct ust_app_channel
*ua_chan
;
4428 assert(usess
->active
);
4429 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4430 uchan
->name
, usess
->id
);
4434 /* For every registered applications */
4435 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4436 struct lttng_ht_iter uiter
;
4437 if (!app
->compatible
) {
4439 * TODO: In time, we should notice the caller of this error by
4440 * telling him that this is a version error.
4444 ua_sess
= lookup_session_by_app(usess
, app
);
4445 if (ua_sess
== NULL
) {
4450 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4451 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4452 /* If the session if found for the app, the channel must be there */
4453 assert(ua_chan_node
);
4455 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4456 /* The channel must not be already disabled */
4457 assert(ua_chan
->enabled
== 1);
4459 /* Disable channel onto application */
4460 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4462 /* XXX: We might want to report this error at some point... */
4472 * For a specific UST session, enable the channel for all registered apps.
4474 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4475 struct ltt_ust_channel
*uchan
)
4478 struct lttng_ht_iter iter
;
4479 struct ust_app
*app
;
4480 struct ust_app_session
*ua_sess
;
4482 assert(usess
->active
);
4483 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4484 uchan
->name
, usess
->id
);
4488 /* For every registered applications */
4489 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4490 if (!app
->compatible
) {
4492 * TODO: In time, we should notice the caller of this error by
4493 * telling him that this is a version error.
4497 ua_sess
= lookup_session_by_app(usess
, app
);
4498 if (ua_sess
== NULL
) {
4502 /* Enable channel onto application */
4503 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4505 /* XXX: We might want to report this error at some point... */
4515 * Disable an event in a channel and for a specific session.
4517 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4518 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4521 struct lttng_ht_iter iter
, uiter
;
4522 struct lttng_ht_node_str
*ua_chan_node
;
4523 struct ust_app
*app
;
4524 struct ust_app_session
*ua_sess
;
4525 struct ust_app_channel
*ua_chan
;
4526 struct ust_app_event
*ua_event
;
4528 assert(usess
->active
);
4529 DBG("UST app disabling event %s for all apps in channel "
4530 "%s for session id %" PRIu64
,
4531 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4535 /* For all registered applications */
4536 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4537 if (!app
->compatible
) {
4539 * TODO: In time, we should notice the caller of this error by
4540 * telling him that this is a version error.
4544 ua_sess
= lookup_session_by_app(usess
, app
);
4545 if (ua_sess
== NULL
) {
4550 /* Lookup channel in the ust app session */
4551 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4552 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4553 if (ua_chan_node
== NULL
) {
4554 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4555 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4558 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4560 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4561 uevent
->filter
, uevent
->attr
.loglevel
,
4563 if (ua_event
== NULL
) {
4564 DBG2("Event %s not found in channel %s for app pid %d."
4565 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4569 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4571 /* XXX: Report error someday... */
4580 /* The ua_sess lock must be held by the caller. */
4582 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4583 struct ust_app_session
*ua_sess
,
4584 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4585 struct ust_app_channel
**_ua_chan
)
4588 struct ust_app_channel
*ua_chan
= NULL
;
4591 ASSERT_LOCKED(ua_sess
->lock
);
4593 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4594 sizeof(uchan
->name
))) {
4595 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4599 struct ltt_ust_context
*uctx
= NULL
;
4602 * Create channel onto application and synchronize its
4605 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4606 LTTNG_UST_CHAN_PER_CPU
, usess
,
4612 ret
= ust_app_channel_send(app
, usess
,
4619 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4620 ret
= create_ust_app_channel_context(ua_chan
,
4633 * The application's socket is not valid. Either a bad socket
4634 * or a timeout on it. We can't inform the caller that for a
4635 * specific app, the session failed so lets continue here.
4637 ret
= 0; /* Not an error. */
4645 if (ret
== 0 && _ua_chan
) {
4647 * Only return the application's channel on success. Note
4648 * that the channel can still be part of the application's
4649 * channel hashtable on error.
4651 *_ua_chan
= ua_chan
;
4657 * Enable event for a specific session and channel on the tracer.
4659 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4660 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4663 struct lttng_ht_iter iter
, uiter
;
4664 struct lttng_ht_node_str
*ua_chan_node
;
4665 struct ust_app
*app
;
4666 struct ust_app_session
*ua_sess
;
4667 struct ust_app_channel
*ua_chan
;
4668 struct ust_app_event
*ua_event
;
4670 assert(usess
->active
);
4671 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4672 uevent
->attr
.name
, usess
->id
);
4675 * NOTE: At this point, this function is called only if the session and
4676 * channel passed are already created for all apps. and enabled on the
4682 /* For all registered applications */
4683 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4684 if (!app
->compatible
) {
4686 * TODO: In time, we should notice the caller of this error by
4687 * telling him that this is a version error.
4691 ua_sess
= lookup_session_by_app(usess
, app
);
4693 /* The application has problem or is probably dead. */
4697 pthread_mutex_lock(&ua_sess
->lock
);
4699 if (ua_sess
->deleted
) {
4700 pthread_mutex_unlock(&ua_sess
->lock
);
4704 /* Lookup channel in the ust app session */
4705 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4706 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4708 * It is possible that the channel cannot be found is
4709 * the channel/event creation occurs concurrently with
4710 * an application exit.
4712 if (!ua_chan_node
) {
4713 pthread_mutex_unlock(&ua_sess
->lock
);
4717 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4719 /* Get event node */
4720 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4721 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4722 if (ua_event
== NULL
) {
4723 DBG3("UST app enable event %s not found for app PID %d."
4724 "Skipping app", uevent
->attr
.name
, app
->pid
);
4728 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4730 pthread_mutex_unlock(&ua_sess
->lock
);
4734 pthread_mutex_unlock(&ua_sess
->lock
);
4743 * For a specific existing UST session and UST channel, creates the event for
4744 * all registered apps.
4746 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4747 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4750 struct lttng_ht_iter iter
, uiter
;
4751 struct lttng_ht_node_str
*ua_chan_node
;
4752 struct ust_app
*app
;
4753 struct ust_app_session
*ua_sess
;
4754 struct ust_app_channel
*ua_chan
;
4756 assert(usess
->active
);
4757 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4758 uevent
->attr
.name
, usess
->id
);
4762 /* For all registered applications */
4763 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4764 if (!app
->compatible
) {
4766 * TODO: In time, we should notice the caller of this error by
4767 * telling him that this is a version error.
4771 ua_sess
= lookup_session_by_app(usess
, app
);
4773 /* The application has problem or is probably dead. */
4777 pthread_mutex_lock(&ua_sess
->lock
);
4779 if (ua_sess
->deleted
) {
4780 pthread_mutex_unlock(&ua_sess
->lock
);
4784 /* Lookup channel in the ust app session */
4785 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4786 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4787 /* If the channel is not found, there is a code flow error */
4788 assert(ua_chan_node
);
4790 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4792 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4793 pthread_mutex_unlock(&ua_sess
->lock
);
4795 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4796 /* Possible value at this point: -ENOMEM. If so, we stop! */
4799 DBG2("UST app event %s already exist on app PID %d",
4800 uevent
->attr
.name
, app
->pid
);
4810 * Start tracing for a specific UST session and app.
4812 * Called with UST app session lock held.
4816 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4819 struct ust_app_session
*ua_sess
;
4821 DBG("Starting tracing for ust app pid %d", app
->pid
);
4825 if (!app
->compatible
) {
4829 ua_sess
= lookup_session_by_app(usess
, app
);
4830 if (ua_sess
== NULL
) {
4831 /* The session is in teardown process. Ignore and continue. */
4835 pthread_mutex_lock(&ua_sess
->lock
);
4837 if (ua_sess
->deleted
) {
4838 pthread_mutex_unlock(&ua_sess
->lock
);
4842 if (ua_sess
->enabled
) {
4843 pthread_mutex_unlock(&ua_sess
->lock
);
4847 /* Upon restart, we skip the setup, already done */
4848 if (ua_sess
->started
) {
4852 health_code_update();
4855 /* This starts the UST tracing */
4856 pthread_mutex_lock(&app
->sock_lock
);
4857 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4858 pthread_mutex_unlock(&app
->sock_lock
);
4860 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4861 ERR("Error starting tracing for app pid: %d (ret: %d)",
4864 DBG("UST app start session failed. Application is dead.");
4866 * This is normal behavior, an application can die during the
4867 * creation process. Don't report an error so the execution can
4868 * continue normally.
4870 pthread_mutex_unlock(&ua_sess
->lock
);
4876 /* Indicate that the session has been started once */
4877 ua_sess
->started
= 1;
4878 ua_sess
->enabled
= 1;
4880 pthread_mutex_unlock(&ua_sess
->lock
);
4882 health_code_update();
4884 /* Quiescent wait after starting trace */
4885 pthread_mutex_lock(&app
->sock_lock
);
4886 ret
= ustctl_wait_quiescent(app
->sock
);
4887 pthread_mutex_unlock(&app
->sock_lock
);
4888 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4889 ERR("UST app wait quiescent failed for app pid %d ret %d",
4895 health_code_update();
4899 pthread_mutex_unlock(&ua_sess
->lock
);
4901 health_code_update();
4906 * Stop tracing for a specific UST session and app.
4909 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4912 struct ust_app_session
*ua_sess
;
4913 struct ust_registry_session
*registry
;
4915 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4919 if (!app
->compatible
) {
4920 goto end_no_session
;
4923 ua_sess
= lookup_session_by_app(usess
, app
);
4924 if (ua_sess
== NULL
) {
4925 goto end_no_session
;
4928 pthread_mutex_lock(&ua_sess
->lock
);
4930 if (ua_sess
->deleted
) {
4931 pthread_mutex_unlock(&ua_sess
->lock
);
4932 goto end_no_session
;
4936 * If started = 0, it means that stop trace has been called for a session
4937 * that was never started. It's possible since we can have a fail start
4938 * from either the application manager thread or the command thread. Simply
4939 * indicate that this is a stop error.
4941 if (!ua_sess
->started
) {
4942 goto error_rcu_unlock
;
4945 health_code_update();
4947 /* This inhibits UST tracing */
4948 pthread_mutex_lock(&app
->sock_lock
);
4949 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4950 pthread_mutex_unlock(&app
->sock_lock
);
4952 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4953 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4956 DBG("UST app stop session failed. Application is dead.");
4958 * This is normal behavior, an application can die during the
4959 * creation process. Don't report an error so the execution can
4960 * continue normally.
4964 goto error_rcu_unlock
;
4967 health_code_update();
4968 ua_sess
->enabled
= 0;
4970 /* Quiescent wait after stopping trace */
4971 pthread_mutex_lock(&app
->sock_lock
);
4972 ret
= ustctl_wait_quiescent(app
->sock
);
4973 pthread_mutex_unlock(&app
->sock_lock
);
4974 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4975 ERR("UST app wait quiescent failed for app pid %d ret %d",
4979 health_code_update();
4981 registry
= get_session_registry(ua_sess
);
4983 /* The UST app session is held registry shall not be null. */
4986 /* Push metadata for application before freeing the application. */
4987 (void) push_metadata(registry
, ua_sess
->consumer
);
4990 pthread_mutex_unlock(&ua_sess
->lock
);
4993 health_code_update();
4997 pthread_mutex_unlock(&ua_sess
->lock
);
4999 health_code_update();
5004 int ust_app_flush_app_session(struct ust_app
*app
,
5005 struct ust_app_session
*ua_sess
)
5007 int ret
, retval
= 0;
5008 struct lttng_ht_iter iter
;
5009 struct ust_app_channel
*ua_chan
;
5010 struct consumer_socket
*socket
;
5012 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5016 if (!app
->compatible
) {
5017 goto end_not_compatible
;
5020 pthread_mutex_lock(&ua_sess
->lock
);
5022 if (ua_sess
->deleted
) {
5026 health_code_update();
5028 /* Flushing buffers */
5029 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5032 /* Flush buffers and push metadata. */
5033 switch (ua_sess
->buffer_type
) {
5034 case LTTNG_BUFFER_PER_PID
:
5035 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5037 health_code_update();
5038 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5040 ERR("Error flushing consumer channel");
5046 case LTTNG_BUFFER_PER_UID
:
5052 health_code_update();
5055 pthread_mutex_unlock(&ua_sess
->lock
);
5059 health_code_update();
5064 * Flush buffers for all applications for a specific UST session.
5065 * Called with UST session lock held.
5068 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5073 DBG("Flushing session buffers for all ust apps");
5077 /* Flush buffers and push metadata. */
5078 switch (usess
->buffer_type
) {
5079 case LTTNG_BUFFER_PER_UID
:
5081 struct buffer_reg_uid
*reg
;
5082 struct lttng_ht_iter iter
;
5084 /* Flush all per UID buffers associated to that session. */
5085 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5086 struct ust_registry_session
*ust_session_reg
;
5087 struct buffer_reg_channel
*reg_chan
;
5088 struct consumer_socket
*socket
;
5090 /* Get consumer socket to use to push the metadata.*/
5091 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5094 /* Ignore request if no consumer is found for the session. */
5098 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5099 reg_chan
, node
.node
) {
5101 * The following call will print error values so the return
5102 * code is of little importance because whatever happens, we
5103 * have to try them all.
5105 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
5108 ust_session_reg
= reg
->registry
->reg
.ust
;
5109 /* Push metadata. */
5110 (void) push_metadata(ust_session_reg
, usess
->consumer
);
5114 case LTTNG_BUFFER_PER_PID
:
5116 struct ust_app_session
*ua_sess
;
5117 struct lttng_ht_iter iter
;
5118 struct ust_app
*app
;
5120 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5121 ua_sess
= lookup_session_by_app(usess
, app
);
5122 if (ua_sess
== NULL
) {
5125 (void) ust_app_flush_app_session(app
, ua_sess
);
5136 health_code_update();
5141 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5142 struct ust_app_session
*ua_sess
)
5145 struct lttng_ht_iter iter
;
5146 struct ust_app_channel
*ua_chan
;
5147 struct consumer_socket
*socket
;
5149 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5153 if (!app
->compatible
) {
5154 goto end_not_compatible
;
5157 pthread_mutex_lock(&ua_sess
->lock
);
5159 if (ua_sess
->deleted
) {
5163 health_code_update();
5165 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5168 ERR("Failed to find consumer (%" PRIu32
") socket",
5169 app
->bits_per_long
);
5174 /* Clear quiescent state. */
5175 switch (ua_sess
->buffer_type
) {
5176 case LTTNG_BUFFER_PER_PID
:
5177 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5178 ua_chan
, node
.node
) {
5179 health_code_update();
5180 ret
= consumer_clear_quiescent_channel(socket
,
5183 ERR("Error clearing quiescent state for consumer channel");
5189 case LTTNG_BUFFER_PER_UID
:
5196 health_code_update();
5199 pthread_mutex_unlock(&ua_sess
->lock
);
5203 health_code_update();
5208 * Clear quiescent state in each stream for all applications for a
5209 * specific UST session.
5210 * Called with UST session lock held.
5213 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5218 DBG("Clearing stream quiescent state for all ust apps");
5222 switch (usess
->buffer_type
) {
5223 case LTTNG_BUFFER_PER_UID
:
5225 struct lttng_ht_iter iter
;
5226 struct buffer_reg_uid
*reg
;
5229 * Clear quiescent for all per UID buffers associated to
5232 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5233 struct consumer_socket
*socket
;
5234 struct buffer_reg_channel
*reg_chan
;
5236 /* Get associated consumer socket.*/
5237 socket
= consumer_find_socket_by_bitness(
5238 reg
->bits_per_long
, usess
->consumer
);
5241 * Ignore request if no consumer is found for
5247 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5248 &iter
.iter
, reg_chan
, node
.node
) {
5250 * The following call will print error values so
5251 * the return code is of little importance
5252 * because whatever happens, we have to try them
5255 (void) consumer_clear_quiescent_channel(socket
,
5256 reg_chan
->consumer_key
);
5261 case LTTNG_BUFFER_PER_PID
:
5263 struct ust_app_session
*ua_sess
;
5264 struct lttng_ht_iter iter
;
5265 struct ust_app
*app
;
5267 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5269 ua_sess
= lookup_session_by_app(usess
, app
);
5270 if (ua_sess
== NULL
) {
5273 (void) ust_app_clear_quiescent_app_session(app
,
5285 health_code_update();
5290 * Destroy a specific UST session in apps.
5292 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5295 struct ust_app_session
*ua_sess
;
5296 struct lttng_ht_iter iter
;
5297 struct lttng_ht_node_u64
*node
;
5299 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5303 if (!app
->compatible
) {
5307 __lookup_session_by_app(usess
, app
, &iter
);
5308 node
= lttng_ht_iter_get_node_u64(&iter
);
5310 /* Session is being or is deleted. */
5313 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
5315 health_code_update();
5316 destroy_app_session(app
, ua_sess
);
5318 health_code_update();
5320 /* Quiescent wait after stopping trace */
5321 pthread_mutex_lock(&app
->sock_lock
);
5322 ret
= ustctl_wait_quiescent(app
->sock
);
5323 pthread_mutex_unlock(&app
->sock_lock
);
5324 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5325 ERR("UST app wait quiescent failed for app pid %d ret %d",
5330 health_code_update();
5335 * Start tracing for the UST session.
5337 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5339 struct lttng_ht_iter iter
;
5340 struct ust_app
*app
;
5342 DBG("Starting all UST traces");
5345 * Even though the start trace might fail, flag this session active so
5346 * other application coming in are started by default.
5353 * In a start-stop-start use-case, we need to clear the quiescent state
5354 * of each channel set by the prior stop command, thus ensuring that a
5355 * following stop or destroy is sure to grab a timestamp_end near those
5356 * operations, even if the packet is empty.
5358 (void) ust_app_clear_quiescent_session(usess
);
5360 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5361 ust_app_global_update(usess
, app
);
5370 * Start tracing for the UST session.
5371 * Called with UST session lock held.
5373 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5376 struct lttng_ht_iter iter
;
5377 struct ust_app
*app
;
5379 DBG("Stopping all UST traces");
5382 * Even though the stop trace might fail, flag this session inactive so
5383 * other application coming in are not started by default.
5389 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5390 ret
= ust_app_stop_trace(usess
, app
);
5392 /* Continue to next apps even on error */
5397 (void) ust_app_flush_session(usess
);
5405 * Destroy app UST session.
5407 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5410 struct lttng_ht_iter iter
;
5411 struct ust_app
*app
;
5413 DBG("Destroy all UST traces");
5417 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5418 ret
= destroy_trace(usess
, app
);
5420 /* Continue to next apps even on error */
5430 /* The ua_sess lock must be held by the caller. */
5432 int find_or_create_ust_app_channel(
5433 struct ltt_ust_session
*usess
,
5434 struct ust_app_session
*ua_sess
,
5435 struct ust_app
*app
,
5436 struct ltt_ust_channel
*uchan
,
5437 struct ust_app_channel
**ua_chan
)
5440 struct lttng_ht_iter iter
;
5441 struct lttng_ht_node_str
*ua_chan_node
;
5443 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5444 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5446 *ua_chan
= caa_container_of(ua_chan_node
,
5447 struct ust_app_channel
, node
);
5451 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5460 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5461 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5462 struct ust_app
*app
)
5465 struct ust_app_event
*ua_event
= NULL
;
5467 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5468 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5470 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5475 if (ua_event
->enabled
!= uevent
->enabled
) {
5476 ret
= uevent
->enabled
?
5477 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5478 disable_ust_app_event(ua_sess
, ua_event
, app
);
5486 /* Called with RCU read-side lock held. */
5488 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5491 enum lttng_error_code ret_code
;
5492 enum lttng_trigger_status t_status
;
5493 struct lttng_ht_iter app_trigger_iter
;
5494 struct lttng_triggers
*triggers
= NULL
;
5495 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5496 unsigned int count
, i
;
5499 * Currrently, registering or unregistering a trigger with an
5500 * event rule condition causes a full synchronization of the event
5503 * The first step attempts to add an event notifier for all registered
5504 * triggers that apply to the user space tracers. Then, the
5505 * application's event notifiers rules are all checked against the list
5506 * of registered triggers. Any event notifier that doesn't have a
5507 * matching trigger can be assumed to have been disabled.
5509 * All of this is inefficient, but is put in place to get the feature
5510 * rolling as it is simpler at this moment. It will be optimized Soon™
5511 * to allow the state of enabled
5512 * event notifiers to be synchronized in a piece-wise way.
5515 /* Get all triggers using uid 0 (root) */
5516 ret_code
= notification_thread_command_list_triggers(
5517 notification_thread_handle
, 0, &triggers
);
5518 if (ret_code
!= LTTNG_OK
) {
5525 t_status
= lttng_triggers_get_count(triggers
, &count
);
5526 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5531 for (i
= 0; i
< count
; i
++) {
5532 struct lttng_condition
*condition
;
5533 struct lttng_event_rule
*event_rule
;
5534 struct lttng_trigger
*trigger
;
5535 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5536 enum lttng_condition_status condition_status
;
5539 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5542 token
= lttng_trigger_get_tracer_token(trigger
);
5543 condition
= lttng_trigger_get_condition(trigger
);
5545 if (lttng_condition_get_type(condition
) != LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
5546 /* Does not apply */
5550 condition_status
= lttng_condition_event_rule_borrow_rule_mutable(condition
, &event_rule
);
5551 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5553 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5554 /* Skip kernel related triggers. */
5559 * Find or create the associated token event rule. The caller
5560 * holds the RCU read lock, so this is safe to call without
5561 * explicitly acquiring it here.
5563 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
5564 app
->token_to_event_notifier_rule_ht
, token
);
5565 if (!looked_up_event_notifier_rule
) {
5566 ret
= create_ust_app_event_notifier_rule(event_rule
, app
, token
);
5574 /* Remove all unknown event sources from the app. */
5575 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
5576 &app_trigger_iter
.iter
, event_notifier_rule
,
5578 const uint64_t app_token
= event_notifier_rule
->token
;
5582 * Check if the app event trigger still exists on the
5583 * notification side.
5585 for (i
= 0; i
< count
; i
++) {
5586 uint64_t notification_thread_token
;
5587 const struct lttng_trigger
*trigger
=
5588 lttng_triggers_get_at_index(
5593 notification_thread_token
=
5594 lttng_trigger_get_tracer_token(trigger
);
5596 if (notification_thread_token
== app_token
) {
5608 * This trigger was unregistered, disable it on the tracer's
5611 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
5615 /* Callee logs errors. */
5616 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
5618 delete_ust_app_event_notifier_rule(
5619 app
->sock
, event_notifier_rule
, app
);
5625 lttng_triggers_destroy(triggers
);
5630 * The caller must ensure that the application is compatible and is tracked
5631 * by the process attribute trackers.
5634 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5635 struct ust_app
*app
)
5638 struct cds_lfht_iter uchan_iter
;
5639 struct ltt_ust_channel
*uchan
;
5640 struct ust_app_session
*ua_sess
= NULL
;
5643 * The application's configuration should only be synchronized for
5646 assert(usess
->active
);
5648 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
5650 /* Tracer is probably gone or ENOMEM. */
5655 pthread_mutex_lock(&ua_sess
->lock
);
5656 if (ua_sess
->deleted
) {
5657 pthread_mutex_unlock(&ua_sess
->lock
);
5663 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5665 struct ust_app_channel
*ua_chan
;
5666 struct cds_lfht_iter uevent_iter
;
5667 struct ltt_ust_event
*uevent
;
5670 * Search for a matching ust_app_channel. If none is found,
5671 * create it. Creating the channel will cause the ua_chan
5672 * structure to be allocated, the channel buffers to be
5673 * allocated (if necessary) and sent to the application, and
5674 * all enabled contexts will be added to the channel.
5676 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5677 app
, uchan
, &ua_chan
);
5679 /* Tracer is probably gone or ENOMEM. */
5684 /* ua_chan will be NULL for the metadata channel */
5688 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5690 ret
= ust_app_channel_synchronize_event(ua_chan
,
5691 uevent
, ua_sess
, app
);
5697 if (ua_chan
->enabled
!= uchan
->enabled
) {
5698 ret
= uchan
->enabled
?
5699 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5700 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5708 * Create the metadata for the application. This returns gracefully if a
5709 * metadata was already set for the session.
5711 * The metadata channel must be created after the data channels as the
5712 * consumer daemon assumes this ordering. When interacting with a relay
5713 * daemon, the consumer will use this assumption to send the
5714 * "STREAMS_SENT" message to the relay daemon.
5716 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
5724 pthread_mutex_unlock(&ua_sess
->lock
);
5725 /* Everything went well at this point. */
5730 pthread_mutex_unlock(&ua_sess
->lock
);
5733 destroy_app_session(app
, ua_sess
);
5739 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5741 struct ust_app_session
*ua_sess
;
5743 ua_sess
= lookup_session_by_app(usess
, app
);
5744 if (ua_sess
== NULL
) {
5747 destroy_app_session(app
, ua_sess
);
5751 * Add channels/events from UST global domain to registered apps at sock.
5753 * Called with session lock held.
5754 * Called with RCU read-side lock held.
5756 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5759 assert(usess
->active
);
5761 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
5762 app
->sock
, usess
->id
);
5764 if (!app
->compatible
) {
5767 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
5769 trace_ust_id_tracker_lookup(
5770 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
5772 trace_ust_id_tracker_lookup(
5773 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
5776 * Synchronize the application's internal tracing configuration
5777 * and start tracing.
5779 ust_app_synchronize(usess
, app
);
5780 ust_app_start_trace(usess
, app
);
5782 ust_app_global_destroy(usess
, app
);
5787 * Add all event notifiers to an application.
5789 * Called with session lock held.
5790 * Called with RCU read-side lock held.
5792 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
5794 DBG2("UST application global event notifier rules update: app = '%s' (ppid: %d)",
5795 app
->name
, app
->ppid
);
5797 if (!app
->compatible
) {
5801 if (app
->event_notifier_group
.object
== NULL
) {
5802 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s' (ppid: %d)",
5803 app
->name
, app
->ppid
);
5807 ust_app_synchronize_event_notifier_rules(app
);
5811 * Called with session lock held.
5813 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
5815 struct lttng_ht_iter iter
;
5816 struct ust_app
*app
;
5819 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5820 ust_app_global_update(usess
, app
);
5825 void ust_app_global_update_all_event_notifier_rules(void)
5827 struct lttng_ht_iter iter
;
5828 struct ust_app
*app
;
5831 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5832 ust_app_global_update_event_notifier_rules(app
);
5839 * Add context to a specific channel for global UST domain.
5841 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
5842 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
5845 struct lttng_ht_node_str
*ua_chan_node
;
5846 struct lttng_ht_iter iter
, uiter
;
5847 struct ust_app_channel
*ua_chan
= NULL
;
5848 struct ust_app_session
*ua_sess
;
5849 struct ust_app
*app
;
5851 assert(usess
->active
);
5854 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5855 if (!app
->compatible
) {
5857 * TODO: In time, we should notice the caller of this error by
5858 * telling him that this is a version error.
5862 ua_sess
= lookup_session_by_app(usess
, app
);
5863 if (ua_sess
== NULL
) {
5867 pthread_mutex_lock(&ua_sess
->lock
);
5869 if (ua_sess
->deleted
) {
5870 pthread_mutex_unlock(&ua_sess
->lock
);
5874 /* Lookup channel in the ust app session */
5875 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5876 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5877 if (ua_chan_node
== NULL
) {
5880 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
5882 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
5887 pthread_mutex_unlock(&ua_sess
->lock
);
5895 * Receive registration and populate the given msg structure.
5897 * On success return 0 else a negative value returned by the ustctl call.
5899 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5902 uint32_t pid
, ppid
, uid
, gid
;
5906 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5907 &pid
, &ppid
, &uid
, &gid
,
5908 &msg
->bits_per_long
,
5909 &msg
->uint8_t_alignment
,
5910 &msg
->uint16_t_alignment
,
5911 &msg
->uint32_t_alignment
,
5912 &msg
->uint64_t_alignment
,
5913 &msg
->long_alignment
,
5920 case LTTNG_UST_ERR_EXITING
:
5921 DBG3("UST app recv reg message failed. Application died");
5923 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5924 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5925 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5926 LTTNG_UST_ABI_MINOR_VERSION
);
5929 ERR("UST app recv reg message failed with ret %d", ret
);
5934 msg
->pid
= (pid_t
) pid
;
5935 msg
->ppid
= (pid_t
) ppid
;
5936 msg
->uid
= (uid_t
) uid
;
5937 msg
->gid
= (gid_t
) gid
;
5944 * Return a ust app session object using the application object and the
5945 * session object descriptor has a key. If not found, NULL is returned.
5946 * A RCU read side lock MUST be acquired when calling this function.
5948 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5951 struct lttng_ht_node_ulong
*node
;
5952 struct lttng_ht_iter iter
;
5953 struct ust_app_session
*ua_sess
= NULL
;
5957 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5958 node
= lttng_ht_iter_get_node_ulong(&iter
);
5960 DBG2("UST app session find by objd %d not found", objd
);
5964 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5971 * Return a ust app channel object using the application object and the channel
5972 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5973 * lock MUST be acquired before calling this function.
5975 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5978 struct lttng_ht_node_ulong
*node
;
5979 struct lttng_ht_iter iter
;
5980 struct ust_app_channel
*ua_chan
= NULL
;
5984 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5985 node
= lttng_ht_iter_get_node_ulong(&iter
);
5987 DBG2("UST app channel find by objd %d not found", objd
);
5991 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5998 * Reply to a register channel notification from an application on the notify
5999 * socket. The channel metadata is also created.
6001 * The session UST registry lock is acquired in this function.
6003 * On success 0 is returned else a negative value.
6005 static int reply_ust_register_channel(int sock
, int cobjd
,
6006 size_t nr_fields
, struct ustctl_field
*fields
)
6008 int ret
, ret_code
= 0;
6010 uint64_t chan_reg_key
;
6011 enum ustctl_channel_header type
;
6012 struct ust_app
*app
;
6013 struct ust_app_channel
*ua_chan
;
6014 struct ust_app_session
*ua_sess
;
6015 struct ust_registry_session
*registry
;
6016 struct ust_registry_channel
*chan_reg
;
6020 /* Lookup application. If not found, there is a code flow error. */
6021 app
= find_app_by_notify_sock(sock
);
6023 DBG("Application socket %d is being torn down. Abort event notify",
6026 goto error_rcu_unlock
;
6029 /* Lookup channel by UST object descriptor. */
6030 ua_chan
= find_channel_by_objd(app
, cobjd
);
6032 DBG("Application channel is being torn down. Abort event notify");
6034 goto error_rcu_unlock
;
6037 assert(ua_chan
->session
);
6038 ua_sess
= ua_chan
->session
;
6040 /* Get right session registry depending on the session buffer type. */
6041 registry
= get_session_registry(ua_sess
);
6043 DBG("Application session is being torn down. Abort event notify");
6045 goto error_rcu_unlock
;
6048 /* Depending on the buffer type, a different channel key is used. */
6049 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6050 chan_reg_key
= ua_chan
->tracing_channel_id
;
6052 chan_reg_key
= ua_chan
->key
;
6055 pthread_mutex_lock(®istry
->lock
);
6057 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
6060 if (!chan_reg
->register_done
) {
6062 * TODO: eventually use the registry event count for
6063 * this channel to better guess header type for per-pid
6066 type
= USTCTL_CHANNEL_HEADER_LARGE
;
6067 chan_reg
->nr_ctx_fields
= nr_fields
;
6068 chan_reg
->ctx_fields
= fields
;
6070 chan_reg
->header_type
= type
;
6072 /* Get current already assigned values. */
6073 type
= chan_reg
->header_type
;
6075 /* Channel id is set during the object creation. */
6076 chan_id
= chan_reg
->chan_id
;
6078 /* Append to metadata */
6079 if (!chan_reg
->metadata_dumped
) {
6080 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
6082 ERR("Error appending channel metadata (errno = %d)", ret_code
);
6088 DBG3("UST app replying to register channel key %" PRIu64
6089 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
6092 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
6094 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6095 ERR("UST app reply channel failed with ret %d", ret
);
6097 DBG3("UST app reply channel failed. Application died");
6102 /* This channel registry registration is completed. */
6103 chan_reg
->register_done
= 1;
6106 pthread_mutex_unlock(®istry
->lock
);
6114 * Add event to the UST channel registry. When the event is added to the
6115 * registry, the metadata is also created. Once done, this replies to the
6116 * application with the appropriate error code.
6118 * The session UST registry lock is acquired in the function.
6120 * On success 0 is returned else a negative value.
6122 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
6123 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
6124 int loglevel_value
, char *model_emf_uri
)
6127 uint32_t event_id
= 0;
6128 uint64_t chan_reg_key
;
6129 struct ust_app
*app
;
6130 struct ust_app_channel
*ua_chan
;
6131 struct ust_app_session
*ua_sess
;
6132 struct ust_registry_session
*registry
;
6136 /* Lookup application. If not found, there is a code flow error. */
6137 app
= find_app_by_notify_sock(sock
);
6139 DBG("Application socket %d is being torn down. Abort event notify",
6142 goto error_rcu_unlock
;
6145 /* Lookup channel by UST object descriptor. */
6146 ua_chan
= find_channel_by_objd(app
, cobjd
);
6148 DBG("Application channel is being torn down. Abort event notify");
6150 goto error_rcu_unlock
;
6153 assert(ua_chan
->session
);
6154 ua_sess
= ua_chan
->session
;
6156 registry
= get_session_registry(ua_sess
);
6158 DBG("Application session is being torn down. Abort event notify");
6160 goto error_rcu_unlock
;
6163 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6164 chan_reg_key
= ua_chan
->tracing_channel_id
;
6166 chan_reg_key
= ua_chan
->key
;
6169 pthread_mutex_lock(®istry
->lock
);
6172 * From this point on, this call acquires the ownership of the sig, fields
6173 * and model_emf_uri meaning any free are done inside it if needed. These
6174 * three variables MUST NOT be read/write after this.
6176 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
6177 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
6178 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
6182 model_emf_uri
= NULL
;
6185 * The return value is returned to ustctl so in case of an error, the
6186 * application can be notified. In case of an error, it's important not to
6187 * return a negative error or else the application will get closed.
6189 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
6191 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6192 ERR("UST app reply event failed with ret %d", ret
);
6194 DBG3("UST app reply event failed. Application died");
6197 * No need to wipe the create event since the application socket will
6198 * get close on error hence cleaning up everything by itself.
6203 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6207 pthread_mutex_unlock(®istry
->lock
);
6212 free(model_emf_uri
);
6217 * Add enum to the UST session registry. Once done, this replies to the
6218 * application with the appropriate error code.
6220 * The session UST registry lock is acquired within this function.
6222 * On success 0 is returned else a negative value.
6224 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
6225 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
6227 int ret
= 0, ret_code
;
6228 struct ust_app
*app
;
6229 struct ust_app_session
*ua_sess
;
6230 struct ust_registry_session
*registry
;
6231 uint64_t enum_id
= -1ULL;
6235 /* Lookup application. If not found, there is a code flow error. */
6236 app
= find_app_by_notify_sock(sock
);
6238 /* Return an error since this is not an error */
6239 DBG("Application socket %d is being torn down. Aborting enum registration",
6242 goto error_rcu_unlock
;
6245 /* Lookup session by UST object descriptor. */
6246 ua_sess
= find_session_by_objd(app
, sobjd
);
6248 /* Return an error since this is not an error */
6249 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6251 goto error_rcu_unlock
;
6254 registry
= get_session_registry(ua_sess
);
6256 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6258 goto error_rcu_unlock
;
6261 pthread_mutex_lock(®istry
->lock
);
6264 * From this point on, the callee acquires the ownership of
6265 * entries. The variable entries MUST NOT be read/written after
6268 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
6269 entries
, nr_entries
, &enum_id
);
6273 * The return value is returned to ustctl so in case of an error, the
6274 * application can be notified. In case of an error, it's important not to
6275 * return a negative error or else the application will get closed.
6277 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
6279 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6280 ERR("UST app reply enum failed with ret %d", ret
);
6282 DBG3("UST app reply enum failed. Application died");
6285 * No need to wipe the create enum since the application socket will
6286 * get close on error hence cleaning up everything by itself.
6291 DBG3("UST registry enum %s added successfully or already found", name
);
6294 pthread_mutex_unlock(®istry
->lock
);
6301 * Handle application notification through the given notify socket.
6303 * Return 0 on success or else a negative value.
6305 int ust_app_recv_notify(int sock
)
6308 enum ustctl_notify_cmd cmd
;
6310 DBG3("UST app receiving notify from sock %d", sock
);
6312 ret
= ustctl_recv_notify(sock
, &cmd
);
6314 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6315 ERR("UST app recv notify failed with ret %d", ret
);
6317 DBG3("UST app recv notify failed. Application died");
6323 case USTCTL_NOTIFY_CMD_EVENT
:
6325 int sobjd
, cobjd
, loglevel_value
;
6326 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6328 struct ustctl_field
*fields
;
6330 DBG2("UST app ustctl register event received");
6332 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
6333 &loglevel_value
, &sig
, &nr_fields
, &fields
,
6336 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6337 ERR("UST app recv event failed with ret %d", ret
);
6339 DBG3("UST app recv event failed. Application died");
6345 * Add event to the UST registry coming from the notify socket. This
6346 * call will free if needed the sig, fields and model_emf_uri. This
6347 * code path loses the ownsership of these variables and transfer them
6348 * to the this function.
6350 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6351 fields
, loglevel_value
, model_emf_uri
);
6358 case USTCTL_NOTIFY_CMD_CHANNEL
:
6362 struct ustctl_field
*fields
;
6364 DBG2("UST app ustctl register channel received");
6366 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
6369 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6370 ERR("UST app recv channel failed with ret %d", ret
);
6372 DBG3("UST app recv channel failed. Application died");
6378 * The fields ownership are transfered to this function call meaning
6379 * that if needed it will be freed. After this, it's invalid to access
6380 * fields or clean it up.
6382 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
6390 case USTCTL_NOTIFY_CMD_ENUM
:
6393 char name
[LTTNG_UST_SYM_NAME_LEN
];
6395 struct ustctl_enum_entry
*entries
;
6397 DBG2("UST app ustctl register enum received");
6399 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
6400 &entries
, &nr_entries
);
6402 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
6403 ERR("UST app recv enum failed with ret %d", ret
);
6405 DBG3("UST app recv enum failed. Application died");
6410 /* Callee assumes ownership of entries */
6411 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6412 entries
, nr_entries
);
6420 /* Should NEVER happen. */
6429 * Once the notify socket hangs up, this is called. First, it tries to find the
6430 * corresponding application. On failure, the call_rcu to close the socket is
6431 * executed. If an application is found, it tries to delete it from the notify
6432 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6434 * Note that an object needs to be allocated here so on ENOMEM failure, the
6435 * call RCU is not done but the rest of the cleanup is.
6437 void ust_app_notify_sock_unregister(int sock
)
6440 struct lttng_ht_iter iter
;
6441 struct ust_app
*app
;
6442 struct ust_app_notify_sock_obj
*obj
;
6448 obj
= zmalloc(sizeof(*obj
));
6451 * An ENOMEM is kind of uncool. If this strikes we continue the
6452 * procedure but the call_rcu will not be called. In this case, we
6453 * accept the fd leak rather than possibly creating an unsynchronized
6454 * state between threads.
6456 * TODO: The notify object should be created once the notify socket is
6457 * registered and stored independantely from the ust app object. The
6458 * tricky part is to synchronize the teardown of the application and
6459 * this notify object. Let's keep that in mind so we can avoid this
6460 * kind of shenanigans with ENOMEM in the teardown path.
6467 DBG("UST app notify socket unregister %d", sock
);
6470 * Lookup application by notify socket. If this fails, this means that the
6471 * hash table delete has already been done by the application
6472 * unregistration process so we can safely close the notify socket in a
6475 app
= find_app_by_notify_sock(sock
);
6480 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6483 * Whatever happens here either we fail or succeed, in both cases we have
6484 * to close the socket after a grace period to continue to the call RCU
6485 * here. If the deletion is successful, the application is not visible
6486 * anymore by other threads and is it fails it means that it was already
6487 * deleted from the hash table so either way we just have to close the
6490 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6496 * Close socket after a grace period to avoid for the socket to be reused
6497 * before the application object is freed creating potential race between
6498 * threads trying to add unique in the global hash table.
6501 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6506 * Destroy a ust app data structure and free its memory.
6508 void ust_app_destroy(struct ust_app
*app
)
6514 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6518 * Take a snapshot for a given UST session. The snapshot is sent to the given
6521 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6523 enum lttng_error_code
ust_app_snapshot_record(
6524 const struct ltt_ust_session
*usess
,
6525 const struct consumer_output
*output
, int wait
,
6526 uint64_t nb_packets_per_stream
)
6529 enum lttng_error_code status
= LTTNG_OK
;
6530 struct lttng_ht_iter iter
;
6531 struct ust_app
*app
;
6532 char *trace_path
= NULL
;
6539 switch (usess
->buffer_type
) {
6540 case LTTNG_BUFFER_PER_UID
:
6542 struct buffer_reg_uid
*reg
;
6544 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6545 struct buffer_reg_channel
*reg_chan
;
6546 struct consumer_socket
*socket
;
6547 char pathname
[PATH_MAX
];
6548 size_t consumer_path_offset
= 0;
6550 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6551 /* Skip since no metadata is present */
6555 /* Get consumer socket to use to push the metadata.*/
6556 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6559 status
= LTTNG_ERR_INVALID
;
6563 memset(pathname
, 0, sizeof(pathname
));
6564 ret
= snprintf(pathname
, sizeof(pathname
),
6565 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
6566 reg
->uid
, reg
->bits_per_long
);
6568 PERROR("snprintf snapshot path");
6569 status
= LTTNG_ERR_INVALID
;
6572 /* Free path allowed on previous iteration. */
6574 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6575 &consumer_path_offset
);
6577 status
= LTTNG_ERR_INVALID
;
6580 /* Add the UST default trace dir to path. */
6581 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6582 reg_chan
, node
.node
) {
6583 status
= consumer_snapshot_channel(socket
,
6584 reg_chan
->consumer_key
,
6585 output
, 0, usess
->uid
,
6586 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
6587 nb_packets_per_stream
);
6588 if (status
!= LTTNG_OK
) {
6592 status
= consumer_snapshot_channel(socket
,
6593 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6594 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6596 if (status
!= LTTNG_OK
) {
6602 case LTTNG_BUFFER_PER_PID
:
6604 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6605 struct consumer_socket
*socket
;
6606 struct lttng_ht_iter chan_iter
;
6607 struct ust_app_channel
*ua_chan
;
6608 struct ust_app_session
*ua_sess
;
6609 struct ust_registry_session
*registry
;
6610 char pathname
[PATH_MAX
];
6611 size_t consumer_path_offset
= 0;
6613 ua_sess
= lookup_session_by_app(usess
, app
);
6615 /* Session not associated with this app. */
6619 /* Get the right consumer socket for the application. */
6620 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6623 status
= LTTNG_ERR_INVALID
;
6627 /* Add the UST default trace dir to path. */
6628 memset(pathname
, 0, sizeof(pathname
));
6629 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
6632 status
= LTTNG_ERR_INVALID
;
6633 PERROR("snprintf snapshot path");
6636 /* Free path allowed on previous iteration. */
6638 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6639 &consumer_path_offset
);
6641 status
= LTTNG_ERR_INVALID
;
6644 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6645 ua_chan
, node
.node
) {
6646 status
= consumer_snapshot_channel(socket
,
6647 ua_chan
->key
, output
, 0,
6648 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6649 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6650 &trace_path
[consumer_path_offset
], wait
,
6651 nb_packets_per_stream
);
6655 case LTTNG_ERR_CHAN_NOT_FOUND
:
6662 registry
= get_session_registry(ua_sess
);
6664 DBG("Application session is being torn down. Skip application.");
6667 status
= consumer_snapshot_channel(socket
,
6668 registry
->metadata_key
, output
, 1,
6669 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
6670 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
6671 &trace_path
[consumer_path_offset
], wait
, 0);
6675 case LTTNG_ERR_CHAN_NOT_FOUND
:
6695 * Return the size taken by one more packet per stream.
6697 uint64_t ust_app_get_size_one_more_packet_per_stream(
6698 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
6700 uint64_t tot_size
= 0;
6701 struct ust_app
*app
;
6702 struct lttng_ht_iter iter
;
6706 switch (usess
->buffer_type
) {
6707 case LTTNG_BUFFER_PER_UID
:
6709 struct buffer_reg_uid
*reg
;
6711 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6712 struct buffer_reg_channel
*reg_chan
;
6715 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6716 reg_chan
, node
.node
) {
6717 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
6719 * Don't take channel into account if we
6720 * already grab all its packets.
6724 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
6730 case LTTNG_BUFFER_PER_PID
:
6733 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6734 struct ust_app_channel
*ua_chan
;
6735 struct ust_app_session
*ua_sess
;
6736 struct lttng_ht_iter chan_iter
;
6738 ua_sess
= lookup_session_by_app(usess
, app
);
6740 /* Session not associated with this app. */
6744 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
6745 ua_chan
, node
.node
) {
6746 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
6748 * Don't take channel into account if we
6749 * already grab all its packets.
6753 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
6767 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
6768 struct cds_list_head
*buffer_reg_uid_list
,
6769 struct consumer_output
*consumer
, uint64_t uchan_id
,
6770 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
6773 uint64_t consumer_chan_key
;
6778 ret
= buffer_reg_uid_consumer_channel_key(
6779 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
6787 ret
= consumer_get_lost_packets(ust_session_id
,
6788 consumer_chan_key
, consumer
, lost
);
6790 ret
= consumer_get_discarded_events(ust_session_id
,
6791 consumer_chan_key
, consumer
, discarded
);
6798 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
6799 struct ltt_ust_channel
*uchan
,
6800 struct consumer_output
*consumer
, int overwrite
,
6801 uint64_t *discarded
, uint64_t *lost
)
6804 struct lttng_ht_iter iter
;
6805 struct lttng_ht_node_str
*ua_chan_node
;
6806 struct ust_app
*app
;
6807 struct ust_app_session
*ua_sess
;
6808 struct ust_app_channel
*ua_chan
;
6815 * Iterate over every registered applications. Sum counters for
6816 * all applications containing requested session and channel.
6818 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6819 struct lttng_ht_iter uiter
;
6821 ua_sess
= lookup_session_by_app(usess
, app
);
6822 if (ua_sess
== NULL
) {
6827 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
6828 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6829 /* If the session is found for the app, the channel must be there */
6830 assert(ua_chan_node
);
6832 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6837 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
6844 uint64_t _discarded
;
6846 ret
= consumer_get_discarded_events(usess
->id
,
6847 ua_chan
->key
, consumer
, &_discarded
);
6851 (*discarded
) += _discarded
;
6860 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
6861 struct ust_app
*app
)
6864 struct ust_app_session
*ua_sess
;
6866 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
6870 ua_sess
= lookup_session_by_app(usess
, app
);
6871 if (ua_sess
== NULL
) {
6872 /* The session is in teardown process. Ignore and continue. */
6876 pthread_mutex_lock(&ua_sess
->lock
);
6878 if (ua_sess
->deleted
) {
6882 pthread_mutex_lock(&app
->sock_lock
);
6883 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
6884 pthread_mutex_unlock(&app
->sock_lock
);
6887 pthread_mutex_unlock(&ua_sess
->lock
);
6891 health_code_update();
6896 * Regenerate the statedump for each app in the session.
6898 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
6901 struct lttng_ht_iter iter
;
6902 struct ust_app
*app
;
6904 DBG("Regenerating the metadata for all UST apps");
6908 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6909 if (!app
->compatible
) {
6913 ret
= ust_app_regenerate_statedump(usess
, app
);
6915 /* Continue to the next app even on error */
6926 * Rotate all the channels of a session.
6928 * Return LTTNG_OK on success or else an LTTng error code.
6930 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
6933 enum lttng_error_code cmd_ret
= LTTNG_OK
;
6934 struct lttng_ht_iter iter
;
6935 struct ust_app
*app
;
6936 struct ltt_ust_session
*usess
= session
->ust_session
;
6942 switch (usess
->buffer_type
) {
6943 case LTTNG_BUFFER_PER_UID
:
6945 struct buffer_reg_uid
*reg
;
6947 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6948 struct buffer_reg_channel
*reg_chan
;
6949 struct consumer_socket
*socket
;
6951 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6952 /* Skip since no metadata is present */
6956 /* Get consumer socket to use to push the metadata.*/
6957 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6960 cmd_ret
= LTTNG_ERR_INVALID
;
6964 /* Rotate the data channels. */
6965 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6966 reg_chan
, node
.node
) {
6967 ret
= consumer_rotate_channel(socket
,
6968 reg_chan
->consumer_key
,
6969 usess
->uid
, usess
->gid
,
6971 /* is_metadata_channel */ false);
6973 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6978 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
6980 ret
= consumer_rotate_channel(socket
,
6981 reg
->registry
->reg
.ust
->metadata_key
,
6982 usess
->uid
, usess
->gid
,
6984 /* is_metadata_channel */ true);
6986 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
6992 case LTTNG_BUFFER_PER_PID
:
6994 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6995 struct consumer_socket
*socket
;
6996 struct lttng_ht_iter chan_iter
;
6997 struct ust_app_channel
*ua_chan
;
6998 struct ust_app_session
*ua_sess
;
6999 struct ust_registry_session
*registry
;
7001 ua_sess
= lookup_session_by_app(usess
, app
);
7003 /* Session not associated with this app. */
7007 /* Get the right consumer socket for the application. */
7008 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7011 cmd_ret
= LTTNG_ERR_INVALID
;
7015 registry
= get_session_registry(ua_sess
);
7017 DBG("Application session is being torn down. Skip application.");
7021 /* Rotate the data channels. */
7022 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7023 ua_chan
, node
.node
) {
7024 ret
= consumer_rotate_channel(socket
,
7026 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7027 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7029 /* is_metadata_channel */ false);
7031 /* Per-PID buffer and application going away. */
7032 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7034 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7039 /* Rotate the metadata channel. */
7040 (void) push_metadata(registry
, usess
->consumer
);
7041 ret
= consumer_rotate_channel(socket
,
7042 registry
->metadata_key
,
7043 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7044 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7046 /* is_metadata_channel */ true);
7048 /* Per-PID buffer and application going away. */
7049 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7051 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7069 enum lttng_error_code
ust_app_create_channel_subdirectories(
7070 const struct ltt_ust_session
*usess
)
7072 enum lttng_error_code ret
= LTTNG_OK
;
7073 struct lttng_ht_iter iter
;
7074 enum lttng_trace_chunk_status chunk_status
;
7075 char *pathname_index
;
7078 assert(usess
->current_trace_chunk
);
7081 switch (usess
->buffer_type
) {
7082 case LTTNG_BUFFER_PER_UID
:
7084 struct buffer_reg_uid
*reg
;
7086 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7087 fmt_ret
= asprintf(&pathname_index
,
7088 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7089 reg
->uid
, reg
->bits_per_long
);
7091 ERR("Failed to format channel index directory");
7092 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7097 * Create the index subdirectory which will take care
7098 * of implicitly creating the channel's path.
7100 chunk_status
= lttng_trace_chunk_create_subdirectory(
7101 usess
->current_trace_chunk
,
7103 free(pathname_index
);
7104 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7105 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7111 case LTTNG_BUFFER_PER_PID
:
7113 struct ust_app
*app
;
7116 * Create the toplevel ust/ directory in case no apps are running.
7118 chunk_status
= lttng_trace_chunk_create_subdirectory(
7119 usess
->current_trace_chunk
,
7120 DEFAULT_UST_TRACE_DIR
);
7121 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7122 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7126 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7128 struct ust_app_session
*ua_sess
;
7129 struct ust_registry_session
*registry
;
7131 ua_sess
= lookup_session_by_app(usess
, app
);
7133 /* Session not associated with this app. */
7137 registry
= get_session_registry(ua_sess
);
7139 DBG("Application session is being torn down. Skip application.");
7143 fmt_ret
= asprintf(&pathname_index
,
7144 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7147 ERR("Failed to format channel index directory");
7148 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7152 * Create the index subdirectory which will take care
7153 * of implicitly creating the channel's path.
7155 chunk_status
= lttng_trace_chunk_create_subdirectory(
7156 usess
->current_trace_chunk
,
7158 free(pathname_index
);
7159 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7160 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7177 * Clear all the channels of a session.
7179 * Return LTTNG_OK on success or else an LTTng error code.
7181 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7184 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7185 struct lttng_ht_iter iter
;
7186 struct ust_app
*app
;
7187 struct ltt_ust_session
*usess
= session
->ust_session
;
7193 if (usess
->active
) {
7194 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7195 cmd_ret
= LTTNG_ERR_FATAL
;
7199 switch (usess
->buffer_type
) {
7200 case LTTNG_BUFFER_PER_UID
:
7202 struct buffer_reg_uid
*reg
;
7204 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7205 struct buffer_reg_channel
*reg_chan
;
7206 struct consumer_socket
*socket
;
7208 /* Get consumer socket to use to push the metadata.*/
7209 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7212 cmd_ret
= LTTNG_ERR_INVALID
;
7216 /* Clear the data channels. */
7217 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7218 reg_chan
, node
.node
) {
7219 ret
= consumer_clear_channel(socket
,
7220 reg_chan
->consumer_key
);
7226 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7229 * Clear the metadata channel.
7230 * Metadata channel is not cleared per se but we still need to
7231 * perform a rotation operation on it behind the scene.
7233 ret
= consumer_clear_channel(socket
,
7234 reg
->registry
->reg
.ust
->metadata_key
);
7241 case LTTNG_BUFFER_PER_PID
:
7243 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7244 struct consumer_socket
*socket
;
7245 struct lttng_ht_iter chan_iter
;
7246 struct ust_app_channel
*ua_chan
;
7247 struct ust_app_session
*ua_sess
;
7248 struct ust_registry_session
*registry
;
7250 ua_sess
= lookup_session_by_app(usess
, app
);
7252 /* Session not associated with this app. */
7256 /* Get the right consumer socket for the application. */
7257 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7260 cmd_ret
= LTTNG_ERR_INVALID
;
7264 registry
= get_session_registry(ua_sess
);
7266 DBG("Application session is being torn down. Skip application.");
7270 /* Clear the data channels. */
7271 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7272 ua_chan
, node
.node
) {
7273 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7275 /* Per-PID buffer and application going away. */
7276 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7283 (void) push_metadata(registry
, usess
->consumer
);
7286 * Clear the metadata channel.
7287 * Metadata channel is not cleared per se but we still need to
7288 * perform rotation operation on it behind the scene.
7290 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
7292 /* Per-PID buffer and application going away. */
7293 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7311 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7312 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7315 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7325 * This function skips the metadata channel as the begin/end timestamps of a
7326 * metadata packet are useless.
7328 * Moreover, opening a packet after a "clear" will cause problems for live
7329 * sessions as it will introduce padding that was not part of the first trace
7330 * chunk. The relay daemon expects the content of the metadata stream of
7331 * successive metadata trace chunks to be strict supersets of one another.
7333 * For example, flushing a packet at the beginning of the metadata stream of
7334 * a trace chunk resulting from a "clear" session command will cause the
7335 * size of the metadata stream of the new trace chunk to not match the size of
7336 * the metadata stream of the original chunk. This will confuse the relay
7337 * daemon as the same "offset" in a metadata stream will no longer point
7338 * to the same content.
7340 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7342 enum lttng_error_code ret
= LTTNG_OK
;
7343 struct lttng_ht_iter iter
;
7344 struct ltt_ust_session
*usess
= session
->ust_session
;
7350 switch (usess
->buffer_type
) {
7351 case LTTNG_BUFFER_PER_UID
:
7353 struct buffer_reg_uid
*reg
;
7355 cds_list_for_each_entry (
7356 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7357 struct buffer_reg_channel
*reg_chan
;
7358 struct consumer_socket
*socket
;
7360 socket
= consumer_find_socket_by_bitness(
7361 reg
->bits_per_long
, usess
->consumer
);
7363 ret
= LTTNG_ERR_FATAL
;
7367 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7368 &iter
.iter
, reg_chan
, node
.node
) {
7369 const int open_ret
=
7370 consumer_open_channel_packets(
7372 reg_chan
->consumer_key
);
7375 ret
= LTTNG_ERR_UNK
;
7382 case LTTNG_BUFFER_PER_PID
:
7384 struct ust_app
*app
;
7386 cds_lfht_for_each_entry (
7387 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7388 struct consumer_socket
*socket
;
7389 struct lttng_ht_iter chan_iter
;
7390 struct ust_app_channel
*ua_chan
;
7391 struct ust_app_session
*ua_sess
;
7392 struct ust_registry_session
*registry
;
7394 ua_sess
= lookup_session_by_app(usess
, app
);
7396 /* Session not associated with this app. */
7400 /* Get the right consumer socket for the application. */
7401 socket
= consumer_find_socket_by_bitness(
7402 app
->bits_per_long
, usess
->consumer
);
7404 ret
= LTTNG_ERR_FATAL
;
7408 registry
= get_session_registry(ua_sess
);
7410 DBG("Application session is being torn down. Skip application.");
7414 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
7415 &chan_iter
.iter
, ua_chan
, node
.node
) {
7416 const int open_ret
=
7417 consumer_open_channel_packets(
7423 * Per-PID buffer and application going
7426 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7430 ret
= LTTNG_ERR_UNK
;