2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
19 #include <sys/types.h>
21 #include <urcu/compiler.h>
24 #include <common/bytecode/bytecode.h>
25 #include <common/compat/errno.h>
26 #include <common/common.h>
27 #include <common/hashtable/utils.h>
28 #include <lttng/event-rule/event-rule.h>
29 #include <lttng/event-rule/event-rule-internal.h>
30 #include <lttng/event-rule/user-tracepoint.h>
31 #include <lttng/condition/condition.h>
32 #include <lttng/condition/event-rule-matches-internal.h>
33 #include <lttng/condition/event-rule-matches.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
37 #include "buffer-registry.h"
38 #include "condition-internal.h"
40 #include "health-sessiond.h"
42 #include "ust-consumer.h"
43 #include "lttng-ust-ctl.h"
44 #include "lttng-ust-error.h"
47 #include "lttng-sessiond.h"
48 #include "notification-thread-commands.h"
51 #include "event-notifier-error-accounting.h"
54 struct lttng_ht
*ust_app_ht
;
55 struct lttng_ht
*ust_app_ht_by_sock
;
56 struct lttng_ht
*ust_app_ht_by_notify_sock
;
59 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
61 /* Next available channel key. Access under next_channel_key_lock. */
62 static uint64_t _next_channel_key
;
63 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
65 /* Next available session ID. Access under next_session_id_lock. */
66 static uint64_t _next_session_id
;
67 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
70 * Return the incremented value of next_channel_key.
72 static uint64_t get_next_channel_key(void)
76 pthread_mutex_lock(&next_channel_key_lock
);
77 ret
= ++_next_channel_key
;
78 pthread_mutex_unlock(&next_channel_key_lock
);
83 * Return the atomically incremented value of next_session_id.
85 static uint64_t get_next_session_id(void)
89 pthread_mutex_lock(&next_session_id_lock
);
90 ret
= ++_next_session_id
;
91 pthread_mutex_unlock(&next_session_id_lock
);
95 static void copy_channel_attr_to_ustctl(
96 struct lttng_ust_ctl_consumer_channel_attr
*attr
,
97 struct lttng_ust_abi_channel_attr
*uattr
)
99 /* Copy event attributes since the layout is different. */
100 attr
->subbuf_size
= uattr
->subbuf_size
;
101 attr
->num_subbuf
= uattr
->num_subbuf
;
102 attr
->overwrite
= uattr
->overwrite
;
103 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
104 attr
->read_timer_interval
= uattr
->read_timer_interval
;
105 attr
->output
= (lttng_ust_abi_output
) uattr
->output
;
106 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
110 * Match function for the hash table lookup.
112 * It matches an ust app event based on three attributes which are the event
113 * name, the filter bytecode and the loglevel.
115 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
117 struct ust_app_event
*event
;
118 const struct ust_app_ht_key
*key
;
119 int ev_loglevel_value
;
124 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
125 key
= (ust_app_ht_key
*) _key
;
126 ev_loglevel_value
= event
->attr
.loglevel
;
128 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
131 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
135 /* Event loglevel. */
136 if (ev_loglevel_value
!= key
->loglevel_type
) {
137 if (event
->attr
.loglevel_type
== LTTNG_UST_ABI_LOGLEVEL_ALL
138 && key
->loglevel_type
== 0 &&
139 ev_loglevel_value
== -1) {
141 * Match is accepted. This is because on event creation, the
142 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
143 * -1 are accepted for this loglevel type since 0 is the one set by
144 * the API when receiving an enable event.
151 /* One of the filters is NULL, fail. */
152 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
156 if (key
->filter
&& event
->filter
) {
157 /* Both filters exists, check length followed by the bytecode. */
158 if (event
->filter
->len
!= key
->filter
->len
||
159 memcmp(event
->filter
->data
, key
->filter
->data
,
160 event
->filter
->len
) != 0) {
165 /* One of the exclusions is NULL, fail. */
166 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
170 if (key
->exclusion
&& event
->exclusion
) {
171 /* Both exclusions exists, check count followed by the names. */
172 if (event
->exclusion
->count
!= key
->exclusion
->count
||
173 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
174 event
->exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) != 0) {
188 * Unique add of an ust app event in the given ht. This uses the custom
189 * ht_match_ust_app_event match function and the event name as hash.
191 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
192 struct ust_app_event
*event
)
194 struct cds_lfht_node
*node_ptr
;
195 struct ust_app_ht_key key
;
198 LTTNG_ASSERT(ua_chan
);
199 LTTNG_ASSERT(ua_chan
->events
);
202 ht
= ua_chan
->events
;
203 key
.name
= event
->attr
.name
;
204 key
.filter
= event
->filter
;
205 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) event
->attr
.loglevel
;
206 key
.exclusion
= event
->exclusion
;
208 node_ptr
= cds_lfht_add_unique(ht
->ht
,
209 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
210 ht_match_ust_app_event
, &key
, &event
->node
.node
);
211 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
215 * Close the notify socket from the given RCU head object. This MUST be called
216 * through a call_rcu().
218 static void close_notify_sock_rcu(struct rcu_head
*head
)
221 struct ust_app_notify_sock_obj
*obj
=
222 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
224 /* Must have a valid fd here. */
225 LTTNG_ASSERT(obj
->fd
>= 0);
227 ret
= close(obj
->fd
);
229 ERR("close notify sock %d RCU", obj
->fd
);
231 lttng_fd_put(LTTNG_FD_APPS
, 1);
237 * Return the session registry according to the buffer type of the given
240 * A registry per UID object MUST exists before calling this function or else
241 * it LTTNG_ASSERT() if not found. RCU read side lock must be acquired.
243 static struct ust_registry_session
*get_session_registry(
244 struct ust_app_session
*ua_sess
)
246 struct ust_registry_session
*registry
= NULL
;
248 LTTNG_ASSERT(ua_sess
);
250 switch (ua_sess
->buffer_type
) {
251 case LTTNG_BUFFER_PER_PID
:
253 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
257 registry
= reg_pid
->registry
->reg
.ust
;
260 case LTTNG_BUFFER_PER_UID
:
262 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
263 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
264 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
268 registry
= reg_uid
->registry
->reg
.ust
;
280 * Delete ust context safely. RCU read lock must be held before calling
284 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
289 LTTNG_ASSERT(ua_ctx
);
292 pthread_mutex_lock(&app
->sock_lock
);
293 ret
= lttng_ust_ctl_release_object(sock
, ua_ctx
->obj
);
294 pthread_mutex_unlock(&app
->sock_lock
);
296 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
297 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
298 app
->pid
, app
->sock
);
299 } else if (ret
== -EAGAIN
) {
300 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
301 app
->pid
, app
->sock
);
303 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
304 ua_ctx
->obj
->handle
, ret
,
305 app
->pid
, app
->sock
);
314 * Delete ust app event safely. RCU read lock must be held before calling
318 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
323 LTTNG_ASSERT(ua_event
);
325 free(ua_event
->filter
);
326 if (ua_event
->exclusion
!= NULL
)
327 free(ua_event
->exclusion
);
328 if (ua_event
->obj
!= NULL
) {
329 pthread_mutex_lock(&app
->sock_lock
);
330 ret
= lttng_ust_ctl_release_object(sock
, ua_event
->obj
);
331 pthread_mutex_unlock(&app
->sock_lock
);
333 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
334 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
335 app
->pid
, app
->sock
);
336 } else if (ret
== -EAGAIN
) {
337 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
338 app
->pid
, app
->sock
);
340 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
341 ret
, app
->pid
, app
->sock
);
350 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
351 * through a call_rcu().
354 void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
356 struct ust_app_event_notifier_rule
*obj
= caa_container_of(
357 head
, struct ust_app_event_notifier_rule
, rcu_head
);
363 * Delete ust app event notifier rule safely.
365 static void delete_ust_app_event_notifier_rule(int sock
,
366 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
,
371 LTTNG_ASSERT(ua_event_notifier_rule
);
373 if (ua_event_notifier_rule
->exclusion
!= NULL
) {
374 free(ua_event_notifier_rule
->exclusion
);
377 if (ua_event_notifier_rule
->obj
!= NULL
) {
378 pthread_mutex_lock(&app
->sock_lock
);
379 ret
= lttng_ust_ctl_release_object(sock
, ua_event_notifier_rule
->obj
);
380 pthread_mutex_unlock(&app
->sock_lock
);
382 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
383 DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d",
384 app
->pid
, app
->sock
);
385 } else if (ret
== -EAGAIN
) {
386 WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d",
387 app
->pid
, app
->sock
);
389 ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d",
390 ret
, app
->pid
, app
->sock
);
394 free(ua_event_notifier_rule
->obj
);
397 lttng_trigger_put(ua_event_notifier_rule
->trigger
);
398 call_rcu(&ua_event_notifier_rule
->rcu_head
,
399 free_ust_app_event_notifier_rule_rcu
);
403 * Release ust data object of the given stream.
405 * Return 0 on success or else a negative value.
407 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
412 LTTNG_ASSERT(stream
);
415 pthread_mutex_lock(&app
->sock_lock
);
416 ret
= lttng_ust_ctl_release_object(sock
, stream
->obj
);
417 pthread_mutex_unlock(&app
->sock_lock
);
419 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
420 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
421 app
->pid
, app
->sock
);
422 } else if (ret
== -EAGAIN
) {
423 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
424 app
->pid
, app
->sock
);
426 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
427 ret
, app
->pid
, app
->sock
);
430 lttng_fd_put(LTTNG_FD_APPS
, 2);
438 * Delete ust app stream safely. RCU read lock must be held before calling
442 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
445 LTTNG_ASSERT(stream
);
447 (void) release_ust_app_stream(sock
, stream
, app
);
452 * We need to execute ht_destroy outside of RCU read-side critical
453 * section and outside of call_rcu thread, so we postpone its execution
454 * using ht_cleanup_push. It is simpler than to change the semantic of
455 * the many callers of delete_ust_app_session().
458 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
460 struct ust_app_channel
*ua_chan
=
461 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
463 ht_cleanup_push(ua_chan
->ctx
);
464 ht_cleanup_push(ua_chan
->events
);
469 * Extract the lost packet or discarded events counter when the channel is
470 * being deleted and store the value in the parent channel so we can
471 * access it from lttng list and at stop/destroy.
473 * The session list lock must be held by the caller.
476 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
478 uint64_t discarded
= 0, lost
= 0;
479 struct ltt_session
*session
;
480 struct ltt_ust_channel
*uchan
;
482 if (ua_chan
->attr
.type
!= LTTNG_UST_ABI_CHAN_PER_CPU
) {
487 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
488 if (!session
|| !session
->ust_session
) {
490 * Not finding the session is not an error because there are
491 * multiple ways the channels can be torn down.
493 * 1) The session daemon can initiate the destruction of the
494 * ust app session after receiving a destroy command or
495 * during its shutdown/teardown.
496 * 2) The application, since we are in per-pid tracing, is
497 * unregistering and tearing down its ust app session.
499 * Both paths are protected by the session list lock which
500 * ensures that the accounting of lost packets and discarded
501 * events is done exactly once. The session is then unpublished
502 * from the session list, resulting in this condition.
507 if (ua_chan
->attr
.overwrite
) {
508 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
509 ua_chan
->key
, session
->ust_session
->consumer
,
512 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
513 ua_chan
->key
, session
->ust_session
->consumer
,
516 uchan
= trace_ust_find_channel_by_name(
517 session
->ust_session
->domain_global
.channels
,
520 ERR("Missing UST channel to store discarded counters");
524 uchan
->per_pid_closed_app_discarded
+= discarded
;
525 uchan
->per_pid_closed_app_lost
+= lost
;
530 session_put(session
);
535 * Delete ust app channel safely. RCU read lock must be held before calling
538 * The session list lock must be held by the caller.
541 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
545 struct lttng_ht_iter iter
;
546 struct ust_app_event
*ua_event
;
547 struct ust_app_ctx
*ua_ctx
;
548 struct ust_app_stream
*stream
, *stmp
;
549 struct ust_registry_session
*registry
;
551 LTTNG_ASSERT(ua_chan
);
553 DBG3("UST app deleting channel %s", ua_chan
->name
);
556 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
557 cds_list_del(&stream
->list
);
558 delete_ust_app_stream(sock
, stream
, app
);
562 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
563 cds_list_del(&ua_ctx
->list
);
564 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
566 delete_ust_app_ctx(sock
, ua_ctx
, app
);
570 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
572 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
574 delete_ust_app_event(sock
, ua_event
, app
);
577 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
578 /* Wipe and free registry from session registry. */
579 registry
= get_session_registry(ua_chan
->session
);
581 ust_registry_channel_del_free(registry
, ua_chan
->key
,
585 * A negative socket can be used by the caller when
586 * cleaning-up a ua_chan in an error path. Skip the
587 * accounting in this case.
590 save_per_pid_lost_discarded_counters(ua_chan
);
594 if (ua_chan
->obj
!= NULL
) {
595 /* Remove channel from application UST object descriptor. */
596 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
597 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
599 pthread_mutex_lock(&app
->sock_lock
);
600 ret
= lttng_ust_ctl_release_object(sock
, ua_chan
->obj
);
601 pthread_mutex_unlock(&app
->sock_lock
);
603 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
604 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
605 ua_chan
->name
, app
->pid
,
607 } else if (ret
== -EAGAIN
) {
608 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
609 ua_chan
->name
, app
->pid
,
612 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
613 ua_chan
->name
, ret
, app
->pid
,
617 lttng_fd_put(LTTNG_FD_APPS
, 1);
620 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
623 int ust_app_register_done(struct ust_app
*app
)
627 pthread_mutex_lock(&app
->sock_lock
);
628 ret
= lttng_ust_ctl_register_done(app
->sock
);
629 pthread_mutex_unlock(&app
->sock_lock
);
633 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*data
)
638 pthread_mutex_lock(&app
->sock_lock
);
643 ret
= lttng_ust_ctl_release_object(sock
, data
);
645 pthread_mutex_unlock(&app
->sock_lock
);
651 * Push metadata to consumer socket.
653 * RCU read-side lock must be held to guarantee existence of socket.
654 * Must be called with the ust app session lock held.
655 * Must be called with the registry lock held.
657 * On success, return the len of metadata pushed or else a negative value.
658 * Returning a -EPIPE return value means we could not send the metadata,
659 * but it can be caused by recoverable errors (e.g. the application has
660 * terminated concurrently).
662 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
663 struct consumer_socket
*socket
, int send_zero_data
)
666 char *metadata_str
= NULL
;
667 size_t len
, offset
, new_metadata_len_sent
;
669 uint64_t metadata_key
, metadata_version
;
671 LTTNG_ASSERT(registry
);
672 LTTNG_ASSERT(socket
);
674 metadata_key
= registry
->metadata_key
;
677 * Means that no metadata was assigned to the session. This can
678 * happens if no start has been done previously.
684 offset
= registry
->metadata_len_sent
;
685 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
686 new_metadata_len_sent
= registry
->metadata_len
;
687 metadata_version
= registry
->metadata_version
;
689 DBG3("No metadata to push for metadata key %" PRIu64
,
690 registry
->metadata_key
);
692 if (send_zero_data
) {
693 DBG("No metadata to push");
699 /* Allocate only what we have to send. */
700 metadata_str
= (char *) zmalloc(len
);
702 PERROR("zmalloc ust app metadata string");
706 /* Copy what we haven't sent out. */
707 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
710 pthread_mutex_unlock(®istry
->lock
);
712 * We need to unlock the registry while we push metadata to
713 * break a circular dependency between the consumerd metadata
714 * lock and the sessiond registry lock. Indeed, pushing metadata
715 * to the consumerd awaits that it gets pushed all the way to
716 * relayd, but doing so requires grabbing the metadata lock. If
717 * a concurrent metadata request is being performed by
718 * consumerd, this can try to grab the registry lock on the
719 * sessiond while holding the metadata lock on the consumer
720 * daemon. Those push and pull schemes are performed on two
721 * different bidirectionnal communication sockets.
723 ret
= consumer_push_metadata(socket
, metadata_key
,
724 metadata_str
, len
, offset
, metadata_version
);
725 pthread_mutex_lock(®istry
->lock
);
728 * There is an acceptable race here between the registry
729 * metadata key assignment and the creation on the
730 * consumer. The session daemon can concurrently push
731 * metadata for this registry while being created on the
732 * consumer since the metadata key of the registry is
733 * assigned *before* it is setup to avoid the consumer
734 * to ask for metadata that could possibly be not found
735 * in the session daemon.
737 * The metadata will get pushed either by the session
738 * being stopped or the consumer requesting metadata if
739 * that race is triggered.
741 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
744 ERR("Error pushing metadata to consumer");
750 * Metadata may have been concurrently pushed, since
751 * we're not holding the registry lock while pushing to
752 * consumer. This is handled by the fact that we send
753 * the metadata content, size, and the offset at which
754 * that metadata belongs. This may arrive out of order
755 * on the consumer side, and the consumer is able to
756 * deal with overlapping fragments. The consumer
757 * supports overlapping fragments, which must be
758 * contiguous starting from offset 0. We keep the
759 * largest metadata_len_sent value of the concurrent
762 registry
->metadata_len_sent
=
763 std::max(registry
->metadata_len_sent
,
764 new_metadata_len_sent
);
773 * On error, flag the registry that the metadata is
774 * closed. We were unable to push anything and this
775 * means that either the consumer is not responding or
776 * the metadata cache has been destroyed on the
779 registry
->metadata_closed
= 1;
787 * For a given application and session, push metadata to consumer.
788 * Either sock or consumer is required : if sock is NULL, the default
789 * socket to send the metadata is retrieved from consumer, if sock
790 * is not NULL we use it to send the metadata.
791 * RCU read-side lock must be held while calling this function,
792 * therefore ensuring existence of registry. It also ensures existence
793 * of socket throughout this function.
795 * Return 0 on success else a negative error.
796 * Returning a -EPIPE return value means we could not send the metadata,
797 * but it can be caused by recoverable errors (e.g. the application has
798 * terminated concurrently).
800 static int push_metadata(struct ust_registry_session
*registry
,
801 struct consumer_output
*consumer
)
805 struct consumer_socket
*socket
;
807 LTTNG_ASSERT(registry
);
808 LTTNG_ASSERT(consumer
);
810 pthread_mutex_lock(®istry
->lock
);
811 if (registry
->metadata_closed
) {
816 /* Get consumer socket to use to push the metadata.*/
817 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
824 ret
= ust_app_push_metadata(registry
, socket
, 0);
829 pthread_mutex_unlock(®istry
->lock
);
833 pthread_mutex_unlock(®istry
->lock
);
838 * Send to the consumer a close metadata command for the given session. Once
839 * done, the metadata channel is deleted and the session metadata pointer is
840 * nullified. The session lock MUST be held unless the application is
841 * in the destroy path.
843 * Do not hold the registry lock while communicating with the consumerd, because
844 * doing so causes inter-process deadlocks between consumerd and sessiond with
845 * the metadata request notification.
847 * Return 0 on success else a negative value.
849 static int close_metadata(struct ust_registry_session
*registry
,
850 struct consumer_output
*consumer
)
853 struct consumer_socket
*socket
;
854 uint64_t metadata_key
;
855 bool registry_was_already_closed
;
857 LTTNG_ASSERT(registry
);
858 LTTNG_ASSERT(consumer
);
862 pthread_mutex_lock(®istry
->lock
);
863 metadata_key
= registry
->metadata_key
;
864 registry_was_already_closed
= registry
->metadata_closed
;
865 if (metadata_key
!= 0) {
867 * Metadata closed. Even on error this means that the consumer
868 * is not responding or not found so either way a second close
869 * should NOT be emit for this registry.
871 registry
->metadata_closed
= 1;
873 pthread_mutex_unlock(®istry
->lock
);
875 if (metadata_key
== 0 || registry_was_already_closed
) {
880 /* Get consumer socket to use to push the metadata.*/
881 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
888 ret
= consumer_close_metadata(socket
, metadata_key
);
899 * We need to execute ht_destroy outside of RCU read-side critical
900 * section and outside of call_rcu thread, so we postpone its execution
901 * using ht_cleanup_push. It is simpler than to change the semantic of
902 * the many callers of delete_ust_app_session().
905 void delete_ust_app_session_rcu(struct rcu_head
*head
)
907 struct ust_app_session
*ua_sess
=
908 caa_container_of(head
, struct ust_app_session
, rcu_head
);
910 ht_cleanup_push(ua_sess
->channels
);
915 * Delete ust app session safely. RCU read lock must be held before calling
918 * The session list lock must be held by the caller.
921 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
925 struct lttng_ht_iter iter
;
926 struct ust_app_channel
*ua_chan
;
927 struct ust_registry_session
*registry
;
929 LTTNG_ASSERT(ua_sess
);
931 pthread_mutex_lock(&ua_sess
->lock
);
933 LTTNG_ASSERT(!ua_sess
->deleted
);
934 ua_sess
->deleted
= true;
936 registry
= get_session_registry(ua_sess
);
937 /* Registry can be null on error path during initialization. */
939 /* Push metadata for application before freeing the application. */
940 (void) push_metadata(registry
, ua_sess
->consumer
);
943 * Don't ask to close metadata for global per UID buffers. Close
944 * metadata only on destroy trace session in this case. Also, the
945 * previous push metadata could have flag the metadata registry to
946 * close so don't send a close command if closed.
948 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
949 /* And ask to close it for this session registry. */
950 (void) close_metadata(registry
, ua_sess
->consumer
);
954 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
956 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
958 delete_ust_app_channel(sock
, ua_chan
, app
);
961 /* In case of per PID, the registry is kept in the session. */
962 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
963 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
966 * Registry can be null on error path during
969 buffer_reg_pid_remove(reg_pid
);
970 buffer_reg_pid_destroy(reg_pid
);
974 if (ua_sess
->handle
!= -1) {
975 pthread_mutex_lock(&app
->sock_lock
);
976 ret
= lttng_ust_ctl_release_handle(sock
, ua_sess
->handle
);
977 pthread_mutex_unlock(&app
->sock_lock
);
979 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
980 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
981 app
->pid
, app
->sock
);
982 } else if (ret
== -EAGAIN
) {
983 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
984 app
->pid
, app
->sock
);
986 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
987 ret
, app
->pid
, app
->sock
);
991 /* Remove session from application UST object descriptor. */
992 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
993 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
997 pthread_mutex_unlock(&ua_sess
->lock
);
999 consumer_output_put(ua_sess
->consumer
);
1001 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
1005 * Delete a traceable application structure from the global list. Never call
1006 * this function outside of a call_rcu call.
1008 * RCU read side lock should _NOT_ be held when calling this function.
1011 void delete_ust_app(struct ust_app
*app
)
1014 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
1015 struct lttng_ht_iter iter
;
1016 struct ust_app_event_notifier_rule
*event_notifier_rule
;
1017 bool event_notifier_write_fd_is_open
;
1020 * The session list lock must be held during this function to guarantee
1021 * the existence of ua_sess.
1023 session_lock_list();
1024 /* Delete ust app sessions info */
1029 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
1031 /* Free every object in the session and the session. */
1033 delete_ust_app_session(sock
, ua_sess
, app
);
1037 /* Remove the event notifier rules associated with this app. */
1039 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
1040 &iter
.iter
, event_notifier_rule
, node
.node
) {
1041 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
1044 delete_ust_app_event_notifier_rule(
1045 app
->sock
, event_notifier_rule
, app
);
1050 ht_cleanup_push(app
->sessions
);
1051 ht_cleanup_push(app
->ust_sessions_objd
);
1052 ht_cleanup_push(app
->ust_objd
);
1053 ht_cleanup_push(app
->token_to_event_notifier_rule_ht
);
1056 * This could be NULL if the event notifier setup failed (e.g the app
1057 * was killed or the tracer does not support this feature).
1059 if (app
->event_notifier_group
.object
) {
1060 enum lttng_error_code ret_code
;
1061 enum event_notifier_error_accounting_status status
;
1063 const int event_notifier_read_fd
= lttng_pipe_get_readfd(
1064 app
->event_notifier_group
.event_pipe
);
1066 ret_code
= notification_thread_command_remove_tracer_event_source(
1067 the_notification_thread_handle
,
1068 event_notifier_read_fd
);
1069 if (ret_code
!= LTTNG_OK
) {
1070 ERR("Failed to remove application tracer event source from notification thread");
1073 status
= event_notifier_error_accounting_unregister_app(app
);
1074 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1075 ERR("Error unregistering app from event notifier error accounting");
1078 lttng_ust_ctl_release_object(sock
, app
->event_notifier_group
.object
);
1079 free(app
->event_notifier_group
.object
);
1082 event_notifier_write_fd_is_open
= lttng_pipe_is_write_open(
1083 app
->event_notifier_group
.event_pipe
);
1084 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1086 * Release the file descriptors reserved for the event notifier pipe.
1087 * The app could be destroyed before the write end of the pipe could be
1088 * passed to the application (and closed). In that case, both file
1089 * descriptors must be released.
1091 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1094 * Wait until we have deleted the application from the sock hash table
1095 * before closing this socket, otherwise an application could re-use the
1096 * socket ID and race with the teardown, using the same hash table entry.
1098 * It's OK to leave the close in call_rcu. We want it to stay unique for
1099 * all RCU readers that could run concurrently with unregister app,
1100 * therefore we _need_ to only close that socket after a grace period. So
1101 * it should stay in this RCU callback.
1103 * This close() is a very important step of the synchronization model so
1104 * every modification to this function must be carefully reviewed.
1110 lttng_fd_put(LTTNG_FD_APPS
, 1);
1112 DBG2("UST app pid %d deleted", app
->pid
);
1114 session_unlock_list();
1118 * URCU intermediate call to delete an UST app.
1121 void delete_ust_app_rcu(struct rcu_head
*head
)
1123 struct lttng_ht_node_ulong
*node
=
1124 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1125 struct ust_app
*app
=
1126 caa_container_of(node
, struct ust_app
, pid_n
);
1128 DBG3("Call RCU deleting app PID %d", app
->pid
);
1129 delete_ust_app(app
);
1133 * Delete the session from the application ht and delete the data structure by
1134 * freeing every object inside and releasing them.
1136 * The session list lock must be held by the caller.
1138 static void destroy_app_session(struct ust_app
*app
,
1139 struct ust_app_session
*ua_sess
)
1142 struct lttng_ht_iter iter
;
1145 LTTNG_ASSERT(ua_sess
);
1147 iter
.iter
.node
= &ua_sess
->node
.node
;
1148 ret
= lttng_ht_del(app
->sessions
, &iter
);
1150 /* Already scheduled for teardown. */
1154 /* Once deleted, free the data structure. */
1155 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1162 * Alloc new UST app session.
1165 struct ust_app_session
*alloc_ust_app_session(void)
1167 struct ust_app_session
*ua_sess
;
1169 /* Init most of the default value by allocating and zeroing */
1170 ua_sess
= (ust_app_session
*) zmalloc(sizeof(struct ust_app_session
));
1171 if (ua_sess
== NULL
) {
1176 ua_sess
->handle
= -1;
1177 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1178 ua_sess
->metadata_attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1179 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1188 * Alloc new UST app channel.
1191 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1192 struct ust_app_session
*ua_sess
,
1193 struct lttng_ust_abi_channel_attr
*attr
)
1195 struct ust_app_channel
*ua_chan
;
1197 /* Init most of the default value by allocating and zeroing */
1198 ua_chan
= (ust_app_channel
*) zmalloc(sizeof(struct ust_app_channel
));
1199 if (ua_chan
== NULL
) {
1204 /* Setup channel name */
1205 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1206 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1208 ua_chan
->enabled
= 1;
1209 ua_chan
->handle
= -1;
1210 ua_chan
->session
= ua_sess
;
1211 ua_chan
->key
= get_next_channel_key();
1212 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1213 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1214 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1216 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1217 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1219 /* Copy attributes */
1221 /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */
1222 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1223 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1224 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1225 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1226 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1227 ua_chan
->attr
.output
= (lttng_ust_abi_output
) attr
->output
;
1228 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1230 /* By default, the channel is a per cpu channel. */
1231 ua_chan
->attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1233 DBG3("UST app channel %s allocated", ua_chan
->name
);
1242 * Allocate and initialize a UST app stream.
1244 * Return newly allocated stream pointer or NULL on error.
1246 struct ust_app_stream
*ust_app_alloc_stream(void)
1248 struct ust_app_stream
*stream
= NULL
;
1250 stream
= (ust_app_stream
*) zmalloc(sizeof(*stream
));
1251 if (stream
== NULL
) {
1252 PERROR("zmalloc ust app stream");
1256 /* Zero could be a valid value for a handle so flag it to -1. */
1257 stream
->handle
= -1;
1264 * Alloc new UST app event.
1267 struct ust_app_event
*alloc_ust_app_event(char *name
,
1268 struct lttng_ust_abi_event
*attr
)
1270 struct ust_app_event
*ua_event
;
1272 /* Init most of the default value by allocating and zeroing */
1273 ua_event
= (ust_app_event
*) zmalloc(sizeof(struct ust_app_event
));
1274 if (ua_event
== NULL
) {
1275 PERROR("Failed to allocate ust_app_event structure");
1279 ua_event
->enabled
= 1;
1280 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1281 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1282 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1284 /* Copy attributes */
1286 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1289 DBG3("UST app event %s allocated", ua_event
->name
);
1298 * Allocate a new UST app event notifier rule.
1300 static struct ust_app_event_notifier_rule
*alloc_ust_app_event_notifier_rule(
1301 struct lttng_trigger
*trigger
)
1303 enum lttng_event_rule_generate_exclusions_status
1304 generate_exclusion_status
;
1305 enum lttng_condition_status cond_status
;
1306 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1307 struct lttng_condition
*condition
= NULL
;
1308 const struct lttng_event_rule
*event_rule
= NULL
;
1310 ua_event_notifier_rule
= (ust_app_event_notifier_rule
*) zmalloc(sizeof(struct ust_app_event_notifier_rule
));
1311 if (ua_event_notifier_rule
== NULL
) {
1312 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1316 ua_event_notifier_rule
->enabled
= 1;
1317 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1318 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
,
1319 ua_event_notifier_rule
->token
);
1321 condition
= lttng_trigger_get_condition(trigger
);
1322 LTTNG_ASSERT(condition
);
1323 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1324 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1326 cond_status
= lttng_condition_event_rule_matches_get_rule(
1327 condition
, &event_rule
);
1328 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
1329 LTTNG_ASSERT(event_rule
);
1331 ua_event_notifier_rule
->error_counter_index
=
1332 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
1333 /* Acquire the event notifier's reference to the trigger. */
1334 lttng_trigger_get(trigger
);
1336 ua_event_notifier_rule
->trigger
= trigger
;
1337 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1338 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1339 event_rule
, &ua_event_notifier_rule
->exclusion
);
1340 switch (generate_exclusion_status
) {
1341 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1342 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1345 /* Error occurred. */
1346 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1347 goto error_put_trigger
;
1350 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1351 ua_event_notifier_rule
->token
);
1353 return ua_event_notifier_rule
;
1356 lttng_trigger_put(trigger
);
1358 free(ua_event_notifier_rule
);
1363 * Alloc new UST app context.
1366 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1368 struct ust_app_ctx
*ua_ctx
;
1370 ua_ctx
= (ust_app_ctx
*) zmalloc(sizeof(struct ust_app_ctx
));
1371 if (ua_ctx
== NULL
) {
1375 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1378 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1379 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1380 char *provider_name
= NULL
, *ctx_name
= NULL
;
1382 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1383 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1384 if (!provider_name
|| !ctx_name
) {
1385 free(provider_name
);
1390 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1391 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1395 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1403 * Create a liblttng-ust filter bytecode from given bytecode.
1405 * Return allocated filter or NULL on error.
1407 static struct lttng_ust_abi_filter_bytecode
*create_ust_filter_bytecode_from_bytecode(
1408 const struct lttng_bytecode
*orig_f
)
1410 struct lttng_ust_abi_filter_bytecode
*filter
= NULL
;
1412 /* Copy filter bytecode. */
1413 filter
= (lttng_ust_abi_filter_bytecode
*) zmalloc(sizeof(*filter
) + orig_f
->len
);
1415 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1419 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1420 sizeof(struct lttng_ust_abi_filter_bytecode
));
1421 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1427 * Create a liblttng-ust capture bytecode from given bytecode.
1429 * Return allocated filter or NULL on error.
1431 static struct lttng_ust_abi_capture_bytecode
*
1432 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1434 struct lttng_ust_abi_capture_bytecode
*capture
= NULL
;
1436 /* Copy capture bytecode. */
1437 capture
= (lttng_ust_abi_capture_bytecode
*) zmalloc(sizeof(*capture
) + orig_f
->len
);
1439 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1443 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1444 sizeof(struct lttng_ust_abi_capture_bytecode
));
1445 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1451 * Find an ust_app using the sock and return it. RCU read side lock must be
1452 * held before calling this helper function.
1454 struct ust_app
*ust_app_find_by_sock(int sock
)
1456 struct lttng_ht_node_ulong
*node
;
1457 struct lttng_ht_iter iter
;
1459 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1460 node
= lttng_ht_iter_get_node_ulong(&iter
);
1462 DBG2("UST app find by sock %d not found", sock
);
1466 return caa_container_of(node
, struct ust_app
, sock_n
);
1473 * Find an ust_app using the notify sock and return it. RCU read side lock must
1474 * be held before calling this helper function.
1476 static struct ust_app
*find_app_by_notify_sock(int sock
)
1478 struct lttng_ht_node_ulong
*node
;
1479 struct lttng_ht_iter iter
;
1481 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1483 node
= lttng_ht_iter_get_node_ulong(&iter
);
1485 DBG2("UST app find by notify sock %d not found", sock
);
1489 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1496 * Lookup for an ust app event based on event name, filter bytecode and the
1499 * Return an ust_app_event object or NULL on error.
1501 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1502 const char *name
, const struct lttng_bytecode
*filter
,
1504 const struct lttng_event_exclusion
*exclusion
)
1506 struct lttng_ht_iter iter
;
1507 struct lttng_ht_node_str
*node
;
1508 struct ust_app_event
*event
= NULL
;
1509 struct ust_app_ht_key key
;
1514 /* Setup key for event lookup. */
1516 key
.filter
= filter
;
1517 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) loglevel_value
;
1518 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1519 key
.exclusion
= exclusion
;
1521 /* Lookup using the event name as hash and a custom match fct. */
1522 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1523 ht_match_ust_app_event
, &key
, &iter
.iter
);
1524 node
= lttng_ht_iter_get_node_str(&iter
);
1529 event
= caa_container_of(node
, struct ust_app_event
, node
);
1536 * Look-up an event notifier rule based on its token id.
1538 * Must be called with the RCU read lock held.
1539 * Return an ust_app_event_notifier_rule object or NULL on error.
1541 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(
1542 struct lttng_ht
*ht
, uint64_t token
)
1544 struct lttng_ht_iter iter
;
1545 struct lttng_ht_node_u64
*node
;
1546 struct ust_app_event_notifier_rule
*event_notifier_rule
= NULL
;
1550 lttng_ht_lookup(ht
, &token
, &iter
);
1551 node
= lttng_ht_iter_get_node_u64(&iter
);
1553 DBG2("UST app event notifier rule token not found: token = %" PRIu64
,
1558 event_notifier_rule
= caa_container_of(
1559 node
, struct ust_app_event_notifier_rule
, node
);
1561 return event_notifier_rule
;
1565 * Create the channel context on the tracer.
1567 * Called with UST app session lock held.
1570 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1571 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1575 health_code_update();
1577 pthread_mutex_lock(&app
->sock_lock
);
1578 ret
= lttng_ust_ctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1579 ua_chan
->obj
, &ua_ctx
->obj
);
1580 pthread_mutex_unlock(&app
->sock_lock
);
1582 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1584 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1585 app
->pid
, app
->sock
);
1586 } else if (ret
== -EAGAIN
) {
1588 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1589 app
->pid
, app
->sock
);
1591 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1592 ret
, app
->pid
, app
->sock
);
1597 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1599 DBG2("UST app context handle %d created successfully for channel %s",
1600 ua_ctx
->handle
, ua_chan
->name
);
1603 health_code_update();
1608 * Set the filter on the tracer.
1610 static int set_ust_object_filter(struct ust_app
*app
,
1611 const struct lttng_bytecode
*bytecode
,
1612 struct lttng_ust_abi_object_data
*ust_object
)
1615 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= NULL
;
1617 health_code_update();
1619 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1620 if (!ust_bytecode
) {
1621 ret
= -LTTNG_ERR_NOMEM
;
1624 pthread_mutex_lock(&app
->sock_lock
);
1625 ret
= lttng_ust_ctl_set_filter(app
->sock
, ust_bytecode
,
1627 pthread_mutex_unlock(&app
->sock_lock
);
1629 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1631 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1632 app
->pid
, app
->sock
);
1633 } else if (ret
== -EAGAIN
) {
1635 WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d",
1636 app
->pid
, app
->sock
);
1638 ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p",
1639 ret
, app
->pid
, app
->sock
, ust_object
);
1644 DBG2("UST filter successfully set: object = %p", ust_object
);
1647 health_code_update();
1653 * Set a capture bytecode for the passed object.
1654 * The sequence number enforces the ordering at runtime and on reception of
1655 * the captured payloads.
1657 static int set_ust_capture(struct ust_app
*app
,
1658 const struct lttng_bytecode
*bytecode
,
1659 unsigned int capture_seqnum
,
1660 struct lttng_ust_abi_object_data
*ust_object
)
1663 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= NULL
;
1665 health_code_update();
1667 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1668 if (!ust_bytecode
) {
1669 ret
= -LTTNG_ERR_NOMEM
;
1674 * Set the sequence number to ensure the capture of fields is ordered.
1676 ust_bytecode
->seqnum
= capture_seqnum
;
1678 pthread_mutex_lock(&app
->sock_lock
);
1679 ret
= lttng_ust_ctl_set_capture(app
->sock
, ust_bytecode
,
1681 pthread_mutex_unlock(&app
->sock_lock
);
1683 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1685 DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d",
1686 app
->pid
, app
->sock
);
1687 } else if (ret
== -EAGAIN
) {
1689 DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d",
1690 app
->pid
, app
->sock
);
1692 ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d",
1700 DBG2("UST capture successfully set: object = %p", ust_object
);
1703 health_code_update();
1709 struct lttng_ust_abi_event_exclusion
*create_ust_exclusion_from_exclusion(
1710 const struct lttng_event_exclusion
*exclusion
)
1712 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= NULL
;
1713 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1714 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1716 ust_exclusion
= (lttng_ust_abi_event_exclusion
*) zmalloc(exclusion_alloc_size
);
1717 if (!ust_exclusion
) {
1722 LTTNG_ASSERT(sizeof(struct lttng_event_exclusion
) ==
1723 sizeof(struct lttng_ust_abi_event_exclusion
));
1724 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1726 return ust_exclusion
;
1730 * Set event exclusions on the tracer.
1732 static int set_ust_object_exclusions(struct ust_app
*app
,
1733 const struct lttng_event_exclusion
*exclusions
,
1734 struct lttng_ust_abi_object_data
*ust_object
)
1737 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= NULL
;
1739 LTTNG_ASSERT(exclusions
&& exclusions
->count
> 0);
1741 health_code_update();
1743 ust_exclusions
= create_ust_exclusion_from_exclusion(
1745 if (!ust_exclusions
) {
1746 ret
= -LTTNG_ERR_NOMEM
;
1749 pthread_mutex_lock(&app
->sock_lock
);
1750 ret
= lttng_ust_ctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1751 pthread_mutex_unlock(&app
->sock_lock
);
1753 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1755 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1756 app
->pid
, app
->sock
);
1757 } else if (ret
== -EAGAIN
) {
1759 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1760 app
->pid
, app
->sock
);
1762 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p",
1763 ret
, app
->pid
, app
->sock
, ust_object
);
1768 DBG2("UST exclusions set successfully for object %p", ust_object
);
1771 health_code_update();
1772 free(ust_exclusions
);
1777 * Disable the specified event on to UST tracer for the UST session.
1779 static int disable_ust_object(struct ust_app
*app
,
1780 struct lttng_ust_abi_object_data
*object
)
1784 health_code_update();
1786 pthread_mutex_lock(&app
->sock_lock
);
1787 ret
= lttng_ust_ctl_disable(app
->sock
, object
);
1788 pthread_mutex_unlock(&app
->sock_lock
);
1790 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1792 DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d",
1793 app
->pid
, app
->sock
);
1794 } else if (ret
== -EAGAIN
) {
1796 WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d",
1797 app
->pid
, app
->sock
);
1799 ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p",
1800 ret
, app
->pid
, app
->sock
, object
);
1805 DBG2("UST app object %p disabled successfully for app: pid = %d",
1809 health_code_update();
1814 * Disable the specified channel on to UST tracer for the UST session.
1816 static int disable_ust_channel(struct ust_app
*app
,
1817 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1821 health_code_update();
1823 pthread_mutex_lock(&app
->sock_lock
);
1824 ret
= lttng_ust_ctl_disable(app
->sock
, ua_chan
->obj
);
1825 pthread_mutex_unlock(&app
->sock_lock
);
1827 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1829 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1830 app
->pid
, app
->sock
);
1831 } else if (ret
== -EAGAIN
) {
1833 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1834 app
->pid
, app
->sock
);
1836 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1837 ua_chan
->name
, ua_sess
->handle
, ret
,
1838 app
->pid
, app
->sock
);
1843 DBG2("UST app channel %s disabled successfully for app: pid = %d",
1844 ua_chan
->name
, app
->pid
);
1847 health_code_update();
1852 * Enable the specified channel on to UST tracer for the UST session.
1854 static int enable_ust_channel(struct ust_app
*app
,
1855 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1859 health_code_update();
1861 pthread_mutex_lock(&app
->sock_lock
);
1862 ret
= lttng_ust_ctl_enable(app
->sock
, ua_chan
->obj
);
1863 pthread_mutex_unlock(&app
->sock_lock
);
1865 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1867 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1868 ua_chan
->name
, app
->pid
, app
->sock
);
1869 } else if (ret
== -EAGAIN
) {
1871 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1872 ua_chan
->name
, app
->pid
, app
->sock
);
1874 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1875 ua_chan
->name
, ua_sess
->handle
, ret
,
1876 app
->pid
, app
->sock
);
1881 ua_chan
->enabled
= 1;
1883 DBG2("UST app channel %s enabled successfully for app: pid = %d",
1884 ua_chan
->name
, app
->pid
);
1887 health_code_update();
1892 * Enable the specified event on to UST tracer for the UST session.
1894 static int enable_ust_object(
1895 struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1899 health_code_update();
1901 pthread_mutex_lock(&app
->sock_lock
);
1902 ret
= lttng_ust_ctl_enable(app
->sock
, ust_object
);
1903 pthread_mutex_unlock(&app
->sock_lock
);
1905 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1907 DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d",
1908 app
->pid
, app
->sock
);
1909 } else if (ret
== -EAGAIN
) {
1911 WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d",
1912 app
->pid
, app
->sock
);
1914 ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p",
1915 ret
, app
->pid
, app
->sock
, ust_object
);
1920 DBG2("UST app object %p enabled successfully for app: pid = %d",
1921 ust_object
, app
->pid
);
1924 health_code_update();
1929 * Send channel and stream buffer to application.
1931 * Return 0 on success. On error, a negative value is returned.
1933 static int send_channel_pid_to_ust(struct ust_app
*app
,
1934 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1937 struct ust_app_stream
*stream
, *stmp
;
1940 LTTNG_ASSERT(ua_sess
);
1941 LTTNG_ASSERT(ua_chan
);
1943 health_code_update();
1945 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1948 /* Send channel to the application. */
1949 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1950 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1951 ret
= -ENOTCONN
; /* Caused by app exiting. */
1953 } else if (ret
== -EAGAIN
) {
1954 /* Caused by timeout. */
1955 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
"\".",
1956 app
->pid
, ua_chan
->name
, ua_sess
->tracing_id
);
1957 /* Treat this the same way as an application that is exiting. */
1960 } else if (ret
< 0) {
1964 health_code_update();
1966 /* Send all streams to application. */
1967 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1968 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1969 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1970 ret
= -ENOTCONN
; /* Caused by app exiting. */
1972 } else if (ret
== -EAGAIN
) {
1973 /* Caused by timeout. */
1974 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
"\".",
1975 app
->pid
, stream
->name
, ua_chan
->name
,
1976 ua_sess
->tracing_id
);
1978 * Treat this the same way as an application that is
1982 } else if (ret
< 0) {
1985 /* We don't need the stream anymore once sent to the tracer. */
1986 cds_list_del(&stream
->list
);
1987 delete_ust_app_stream(-1, stream
, app
);
1989 /* Flag the channel that it is sent to the application. */
1990 ua_chan
->is_sent
= 1;
1993 health_code_update();
1998 * Create the specified event onto the UST tracer for a UST session.
2000 * Should be called with session mutex held.
2003 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2004 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
2008 health_code_update();
2010 /* Create UST event on tracer */
2011 pthread_mutex_lock(&app
->sock_lock
);
2012 ret
= lttng_ust_ctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
2014 pthread_mutex_unlock(&app
->sock_lock
);
2016 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2018 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
2019 app
->pid
, app
->sock
);
2020 } else if (ret
== -EAGAIN
) {
2022 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
2023 app
->pid
, app
->sock
);
2025 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
2026 ua_event
->attr
.name
, ret
, app
->pid
,
2032 ua_event
->handle
= ua_event
->obj
->handle
;
2034 DBG2("UST app event %s created successfully for pid:%d object = %p",
2035 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
2037 health_code_update();
2039 /* Set filter if one is present. */
2040 if (ua_event
->filter
) {
2041 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
2047 /* Set exclusions for the event */
2048 if (ua_event
->exclusion
) {
2049 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
2055 /* If event not enabled, disable it on the tracer */
2056 if (ua_event
->enabled
) {
2058 * We now need to explicitly enable the event, since it
2059 * is now disabled at creation.
2061 ret
= enable_ust_object(app
, ua_event
->obj
);
2064 * If we hit an EPERM, something is wrong with our enable call. If
2065 * we get an EEXIST, there is a problem on the tracer side since we
2069 case -LTTNG_UST_ERR_PERM
:
2070 /* Code flow problem */
2072 case -LTTNG_UST_ERR_EXIST
:
2073 /* It's OK for our use case. */
2084 health_code_update();
2088 static int init_ust_event_notifier_from_event_rule(
2089 const struct lttng_event_rule
*rule
,
2090 struct lttng_ust_abi_event_notifier
*event_notifier
)
2092 enum lttng_event_rule_status status
;
2093 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2094 int loglevel
= -1, ret
= 0;
2095 const char *pattern
;
2098 memset(event_notifier
, 0, sizeof(*event_notifier
));
2100 if (lttng_event_rule_targets_agent_domain(rule
)) {
2102 * Special event for agents
2103 * The actual meat of the event is in the filter that will be
2104 * attached later on.
2105 * Set the default values for the agent event.
2107 pattern
= event_get_default_agent_ust_name(
2108 lttng_event_rule_get_domain_type(rule
));
2110 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2112 const struct lttng_log_level_rule
*log_level_rule
;
2114 LTTNG_ASSERT(lttng_event_rule_get_type(rule
) ==
2115 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
);
2117 status
= lttng_event_rule_user_tracepoint_get_name_pattern(rule
, &pattern
);
2118 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2119 /* At this point, this is a fatal error. */
2123 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(
2124 rule
, &log_level_rule
);
2125 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2126 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2127 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2128 enum lttng_log_level_rule_status llr_status
;
2130 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2131 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2132 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2133 llr_status
= lttng_log_level_rule_exactly_get_level(
2134 log_level_rule
, &loglevel
);
2136 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2137 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2138 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2139 log_level_rule
, &loglevel
);
2145 LTTNG_ASSERT(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2147 /* At this point this is a fatal error. */
2152 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2153 ret
= lttng_strncpy(event_notifier
->event
.name
, pattern
,
2154 LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
2156 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
2161 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2162 event_notifier
->event
.loglevel
= loglevel
;
2168 * Create the specified event notifier against the user space tracer of a
2169 * given application.
2171 static int create_ust_event_notifier(struct ust_app
*app
,
2172 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2175 enum lttng_condition_status condition_status
;
2176 const struct lttng_condition
*condition
= NULL
;
2177 struct lttng_ust_abi_event_notifier event_notifier
;
2178 const struct lttng_event_rule
*event_rule
= NULL
;
2179 unsigned int capture_bytecode_count
= 0, i
;
2180 enum lttng_condition_status cond_status
;
2181 enum lttng_event_rule_type event_rule_type
;
2183 health_code_update();
2184 LTTNG_ASSERT(app
->event_notifier_group
.object
);
2186 condition
= lttng_trigger_get_const_condition(
2187 ua_event_notifier_rule
->trigger
);
2188 LTTNG_ASSERT(condition
);
2189 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
2190 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2192 condition_status
= lttng_condition_event_rule_matches_get_rule(
2193 condition
, &event_rule
);
2194 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2196 LTTNG_ASSERT(event_rule
);
2198 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2199 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
||
2200 event_rule_type
== LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
||
2202 LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
||
2204 LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
);
2206 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2207 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2208 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2210 /* Create UST event notifier against the tracer. */
2211 pthread_mutex_lock(&app
->sock_lock
);
2212 ret
= lttng_ust_ctl_create_event_notifier(app
->sock
, &event_notifier
,
2213 app
->event_notifier_group
.object
,
2214 &ua_event_notifier_rule
->obj
);
2215 pthread_mutex_unlock(&app
->sock_lock
);
2217 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2219 DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d",
2220 app
->pid
, app
->sock
);
2221 } else if (ret
== -EAGAIN
) {
2223 WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d",
2224 app
->pid
, app
->sock
);
2226 ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d",
2227 event_notifier
.event
.name
, ret
, app
->pid
,
2233 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2235 DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p",
2236 event_notifier
.event
.name
, app
->name
, app
->pid
,
2237 ua_event_notifier_rule
->obj
);
2239 health_code_update();
2241 /* Set filter if one is present. */
2242 if (ua_event_notifier_rule
->filter
) {
2243 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2244 ua_event_notifier_rule
->obj
);
2250 /* Set exclusions for the event. */
2251 if (ua_event_notifier_rule
->exclusion
) {
2252 ret
= set_ust_object_exclusions(app
,
2253 ua_event_notifier_rule
->exclusion
,
2254 ua_event_notifier_rule
->obj
);
2260 /* Set the capture bytecodes. */
2261 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2262 condition
, &capture_bytecode_count
);
2263 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2265 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2266 const struct lttng_bytecode
*capture_bytecode
=
2267 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
2270 ret
= set_ust_capture(app
, capture_bytecode
, i
,
2271 ua_event_notifier_rule
->obj
);
2278 * We now need to explicitly enable the event, since it
2279 * is disabled at creation.
2281 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2284 * If we hit an EPERM, something is wrong with our enable call.
2285 * If we get an EEXIST, there is a problem on the tracer side
2286 * since we just created it.
2289 case -LTTNG_UST_ERR_PERM
:
2290 /* Code flow problem. */
2292 case -LTTNG_UST_ERR_EXIST
:
2293 /* It's OK for our use case. */
2303 ua_event_notifier_rule
->enabled
= true;
2306 health_code_update();
2311 * Copy data between an UST app event and a LTT event.
2313 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2314 struct ltt_ust_event
*uevent
)
2316 size_t exclusion_alloc_size
;
2318 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2319 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2321 ua_event
->enabled
= uevent
->enabled
;
2323 /* Copy event attributes */
2324 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2326 /* Copy filter bytecode */
2327 if (uevent
->filter
) {
2328 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2329 /* Filter might be NULL here in case of ENONEM. */
2332 /* Copy exclusion data */
2333 if (uevent
->exclusion
) {
2334 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2335 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2336 ua_event
->exclusion
= (lttng_event_exclusion
*) zmalloc(exclusion_alloc_size
);
2337 if (ua_event
->exclusion
== NULL
) {
2340 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2341 exclusion_alloc_size
);
2347 * Copy data between an UST app channel and a LTT channel.
2349 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2350 struct ltt_ust_channel
*uchan
)
2352 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2354 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2355 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2357 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2358 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2360 /* Copy event attributes since the layout is different. */
2361 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2362 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2363 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2364 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2365 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2366 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2367 ua_chan
->attr
.output
= (lttng_ust_abi_output
) uchan
->attr
.output
;
2368 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2371 * Note that the attribute channel type is not set since the channel on the
2372 * tracing registry side does not have this information.
2375 ua_chan
->enabled
= uchan
->enabled
;
2376 ua_chan
->tracing_channel_id
= uchan
->id
;
2378 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2382 * Copy data between a UST app session and a regular LTT session.
2384 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2385 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2387 struct tm
*timeinfo
;
2390 char tmp_shm_path
[PATH_MAX
];
2392 timeinfo
= localtime(&app
->registration_time
);
2393 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2395 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2397 ua_sess
->tracing_id
= usess
->id
;
2398 ua_sess
->id
= get_next_session_id();
2399 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2400 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2401 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2402 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2403 ua_sess
->buffer_type
= usess
->buffer_type
;
2404 ua_sess
->bits_per_long
= app
->bits_per_long
;
2406 /* There is only one consumer object per session possible. */
2407 consumer_output_get(usess
->consumer
);
2408 ua_sess
->consumer
= usess
->consumer
;
2410 ua_sess
->output_traces
= usess
->output_traces
;
2411 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2412 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2413 &usess
->metadata_attr
);
2415 switch (ua_sess
->buffer_type
) {
2416 case LTTNG_BUFFER_PER_PID
:
2417 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2418 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2421 case LTTNG_BUFFER_PER_UID
:
2422 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2423 DEFAULT_UST_TRACE_UID_PATH
,
2424 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2425 app
->bits_per_long
);
2432 PERROR("asprintf UST shadow copy session");
2437 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2438 sizeof(ua_sess
->root_shm_path
));
2439 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2440 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2441 sizeof(ua_sess
->shm_path
));
2442 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2443 if (ua_sess
->shm_path
[0]) {
2444 switch (ua_sess
->buffer_type
) {
2445 case LTTNG_BUFFER_PER_PID
:
2446 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2447 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2448 app
->name
, app
->pid
, datetime
);
2450 case LTTNG_BUFFER_PER_UID
:
2451 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2452 "/" DEFAULT_UST_TRACE_UID_PATH
,
2453 app
->uid
, app
->bits_per_long
);
2460 PERROR("sprintf UST shadow copy session");
2464 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2465 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2466 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2471 consumer_output_put(ua_sess
->consumer
);
2475 * Lookup sesison wrapper.
2478 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2479 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2481 /* Get right UST app session from app */
2482 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2486 * Return ust app session from the app session hashtable using the UST session
2489 static struct ust_app_session
*lookup_session_by_app(
2490 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2492 struct lttng_ht_iter iter
;
2493 struct lttng_ht_node_u64
*node
;
2495 __lookup_session_by_app(usess
, app
, &iter
);
2496 node
= lttng_ht_iter_get_node_u64(&iter
);
2501 return caa_container_of(node
, struct ust_app_session
, node
);
2508 * Setup buffer registry per PID for the given session and application. If none
2509 * is found, a new one is created, added to the global registry and
2510 * initialized. If regp is valid, it's set with the newly created object.
2512 * Return 0 on success or else a negative value.
2514 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2515 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2518 struct buffer_reg_pid
*reg_pid
;
2520 LTTNG_ASSERT(ua_sess
);
2525 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2528 * This is the create channel path meaning that if there is NO
2529 * registry available, we have to create one for this session.
2531 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2532 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2540 /* Initialize registry. */
2541 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2542 app
->bits_per_long
, app
->uint8_t_alignment
,
2543 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2544 app
->uint64_t_alignment
, app
->long_alignment
,
2545 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2546 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2547 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2548 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2549 ua_sess
->tracing_id
,
2553 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2554 * destroy the buffer registry, because it is always expected
2555 * that if the buffer registry can be found, its ust registry is
2558 buffer_reg_pid_destroy(reg_pid
);
2562 buffer_reg_pid_add(reg_pid
);
2564 DBG3("UST app buffer registry per PID created successfully");
2576 * Setup buffer registry per UID for the given session and application. If none
2577 * is found, a new one is created, added to the global registry and
2578 * initialized. If regp is valid, it's set with the newly created object.
2580 * Return 0 on success or else a negative value.
2582 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2583 struct ust_app_session
*ua_sess
,
2584 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2587 struct buffer_reg_uid
*reg_uid
;
2589 LTTNG_ASSERT(usess
);
2594 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2597 * This is the create channel path meaning that if there is NO
2598 * registry available, we have to create one for this session.
2600 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2601 LTTNG_DOMAIN_UST
, ®_uid
,
2602 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2610 /* Initialize registry. */
2611 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2612 app
->bits_per_long
, app
->uint8_t_alignment
,
2613 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2614 app
->uint64_t_alignment
, app
->long_alignment
,
2615 app
->byte_order
, app
->version
.major
,
2616 app
->version
.minor
, reg_uid
->root_shm_path
,
2617 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2618 ua_sess
->tracing_id
, app
->uid
);
2621 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2622 * destroy the buffer registry, because it is always expected
2623 * that if the buffer registry can be found, its ust registry is
2626 buffer_reg_uid_destroy(reg_uid
, NULL
);
2629 /* Add node to teardown list of the session. */
2630 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2632 buffer_reg_uid_add(reg_uid
);
2634 DBG3("UST app buffer registry per UID created successfully");
2645 * Create a session on the tracer side for the given app.
2647 * On success, ua_sess_ptr is populated with the session pointer or else left
2648 * untouched. If the session was created, is_created is set to 1. On error,
2649 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2652 * Returns 0 on success or else a negative code which is either -ENOMEM or
2653 * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails.
2655 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2656 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2659 int ret
, created
= 0;
2660 struct ust_app_session
*ua_sess
;
2662 LTTNG_ASSERT(usess
);
2664 LTTNG_ASSERT(ua_sess_ptr
);
2666 health_code_update();
2668 ua_sess
= lookup_session_by_app(usess
, app
);
2669 if (ua_sess
== NULL
) {
2670 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2671 app
->pid
, usess
->id
);
2672 ua_sess
= alloc_ust_app_session();
2673 if (ua_sess
== NULL
) {
2674 /* Only malloc can failed so something is really wrong */
2678 shadow_copy_session(ua_sess
, usess
, app
);
2682 switch (usess
->buffer_type
) {
2683 case LTTNG_BUFFER_PER_PID
:
2684 /* Init local registry. */
2685 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2687 delete_ust_app_session(-1, ua_sess
, app
);
2691 case LTTNG_BUFFER_PER_UID
:
2692 /* Look for a global registry. If none exists, create one. */
2693 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2695 delete_ust_app_session(-1, ua_sess
, app
);
2705 health_code_update();
2707 if (ua_sess
->handle
== -1) {
2708 pthread_mutex_lock(&app
->sock_lock
);
2709 ret
= lttng_ust_ctl_create_session(app
->sock
);
2710 pthread_mutex_unlock(&app
->sock_lock
);
2712 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2713 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2714 app
->pid
, app
->sock
);
2716 } else if (ret
== -EAGAIN
) {
2717 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2718 app
->pid
, app
->sock
);
2721 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2722 ret
, app
->pid
, app
->sock
);
2724 delete_ust_app_session(-1, ua_sess
, app
);
2725 if (ret
!= -ENOMEM
) {
2727 * Tracer is probably gone or got an internal error so let's
2728 * behave like it will soon unregister or not usable.
2735 ua_sess
->handle
= ret
;
2737 /* Add ust app session to app's HT */
2738 lttng_ht_node_init_u64(&ua_sess
->node
,
2739 ua_sess
->tracing_id
);
2740 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2741 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2742 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2743 &ua_sess
->ust_objd_node
);
2745 DBG2("UST app session created successfully with handle %d", ret
);
2748 *ua_sess_ptr
= ua_sess
;
2750 *is_created
= created
;
2753 /* Everything went well. */
2757 health_code_update();
2762 * Match function for a hash table lookup of ust_app_ctx.
2764 * It matches an ust app context based on the context type and, in the case
2765 * of perf counters, their name.
2767 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2769 struct ust_app_ctx
*ctx
;
2770 const struct lttng_ust_context_attr
*key
;
2775 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2776 key
= (lttng_ust_context_attr
*) _key
;
2779 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2784 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2785 if (strncmp(key
->u
.perf_counter
.name
,
2786 ctx
->ctx
.u
.perf_counter
.name
,
2787 sizeof(key
->u
.perf_counter
.name
))) {
2791 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2792 if (strcmp(key
->u
.app_ctx
.provider_name
,
2793 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2794 strcmp(key
->u
.app_ctx
.ctx_name
,
2795 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2811 * Lookup for an ust app context from an lttng_ust_context.
2813 * Must be called while holding RCU read side lock.
2814 * Return an ust_app_ctx object or NULL on error.
2817 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2818 struct lttng_ust_context_attr
*uctx
)
2820 struct lttng_ht_iter iter
;
2821 struct lttng_ht_node_ulong
*node
;
2822 struct ust_app_ctx
*app_ctx
= NULL
;
2827 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2828 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2829 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2830 node
= lttng_ht_iter_get_node_ulong(&iter
);
2835 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2842 * Create a context for the channel on the tracer.
2844 * Called with UST app session lock held and a RCU read side lock.
2847 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2848 struct lttng_ust_context_attr
*uctx
,
2849 struct ust_app
*app
)
2852 struct ust_app_ctx
*ua_ctx
;
2854 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2856 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2862 ua_ctx
= alloc_ust_app_ctx(uctx
);
2863 if (ua_ctx
== NULL
) {
2869 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2870 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2871 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2873 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2883 * Enable on the tracer side a ust app event for the session and channel.
2885 * Called with UST app session lock held.
2888 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2889 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2893 ret
= enable_ust_object(app
, ua_event
->obj
);
2898 ua_event
->enabled
= 1;
2905 * Disable on the tracer side a ust app event for the session and channel.
2907 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2908 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2912 ret
= disable_ust_object(app
, ua_event
->obj
);
2917 ua_event
->enabled
= 0;
2924 * Lookup ust app channel for session and disable it on the tracer side.
2927 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2928 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2932 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2937 ua_chan
->enabled
= 0;
2944 * Lookup ust app channel for session and enable it on the tracer side. This
2945 * MUST be called with a RCU read side lock acquired.
2947 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2948 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2951 struct lttng_ht_iter iter
;
2952 struct lttng_ht_node_str
*ua_chan_node
;
2953 struct ust_app_channel
*ua_chan
;
2955 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2956 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2957 if (ua_chan_node
== NULL
) {
2958 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2959 uchan
->name
, ua_sess
->tracing_id
);
2963 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2965 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2975 * Ask the consumer to create a channel and get it if successful.
2977 * Called with UST app session lock held.
2979 * Return 0 on success or else a negative value.
2981 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2982 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2983 int bitness
, struct ust_registry_session
*registry
,
2984 uint64_t trace_archive_id
)
2987 unsigned int nb_fd
= 0;
2988 struct consumer_socket
*socket
;
2990 LTTNG_ASSERT(usess
);
2991 LTTNG_ASSERT(ua_sess
);
2992 LTTNG_ASSERT(ua_chan
);
2993 LTTNG_ASSERT(registry
);
2996 health_code_update();
2998 /* Get the right consumer socket for the application. */
2999 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
3005 health_code_update();
3007 /* Need one fd for the channel. */
3008 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3010 ERR("Exhausted number of available FD upon create channel");
3015 * Ask consumer to create channel. The consumer will return the number of
3016 * stream we have to expect.
3018 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
3019 registry
, usess
->current_trace_chunk
);
3025 * Compute the number of fd needed before receiving them. It must be 2 per
3026 * stream (2 being the default value here).
3028 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
3030 /* Reserve the amount of file descriptor we need. */
3031 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
3033 ERR("Exhausted number of available FD upon create channel");
3034 goto error_fd_get_stream
;
3037 health_code_update();
3040 * Now get the channel from the consumer. This call will populate the stream
3041 * list of that channel and set the ust objects.
3043 if (usess
->consumer
->enabled
) {
3044 ret
= ust_consumer_get_channel(socket
, ua_chan
);
3054 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
3055 error_fd_get_stream
:
3057 * Initiate a destroy channel on the consumer since we had an error
3058 * handling it on our side. The return value is of no importance since we
3059 * already have a ret value set by the previous error that we need to
3062 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
3064 lttng_fd_put(LTTNG_FD_APPS
, 1);
3066 health_code_update();
3072 * Duplicate the ust data object of the ust app stream and save it in the
3073 * buffer registry stream.
3075 * Return 0 on success or else a negative value.
3077 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
3078 struct ust_app_stream
*stream
)
3082 LTTNG_ASSERT(reg_stream
);
3083 LTTNG_ASSERT(stream
);
3085 /* Duplicating a stream requires 2 new fds. Reserve them. */
3086 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3088 ERR("Exhausted number of available FD upon duplicate stream");
3092 /* Duplicate object for stream once the original is in the registry. */
3093 ret
= lttng_ust_ctl_duplicate_ust_object_data(&stream
->obj
,
3094 reg_stream
->obj
.ust
);
3096 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3097 reg_stream
->obj
.ust
, stream
->obj
, ret
);
3098 lttng_fd_put(LTTNG_FD_APPS
, 2);
3101 stream
->handle
= stream
->obj
->handle
;
3108 * Duplicate the ust data object of the ust app. channel and save it in the
3109 * buffer registry channel.
3111 * Return 0 on success or else a negative value.
3113 static int duplicate_channel_object(struct buffer_reg_channel
*buf_reg_chan
,
3114 struct ust_app_channel
*ua_chan
)
3118 LTTNG_ASSERT(buf_reg_chan
);
3119 LTTNG_ASSERT(ua_chan
);
3121 /* Duplicating a channel requires 1 new fd. Reserve it. */
3122 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3124 ERR("Exhausted number of available FD upon duplicate channel");
3128 /* Duplicate object for stream once the original is in the registry. */
3129 ret
= lttng_ust_ctl_duplicate_ust_object_data(&ua_chan
->obj
, buf_reg_chan
->obj
.ust
);
3131 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3132 buf_reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
3135 ua_chan
->handle
= ua_chan
->obj
->handle
;
3140 lttng_fd_put(LTTNG_FD_APPS
, 1);
3146 * For a given channel buffer registry, setup all streams of the given ust
3147 * application channel.
3149 * Return 0 on success or else a negative value.
3151 static int setup_buffer_reg_streams(struct buffer_reg_channel
*buf_reg_chan
,
3152 struct ust_app_channel
*ua_chan
,
3153 struct ust_app
*app
)
3156 struct ust_app_stream
*stream
, *stmp
;
3158 LTTNG_ASSERT(buf_reg_chan
);
3159 LTTNG_ASSERT(ua_chan
);
3161 DBG2("UST app setup buffer registry stream");
3163 /* Send all streams to application. */
3164 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3165 struct buffer_reg_stream
*reg_stream
;
3167 ret
= buffer_reg_stream_create(®_stream
);
3173 * Keep original pointer and nullify it in the stream so the delete
3174 * stream call does not release the object.
3176 reg_stream
->obj
.ust
= stream
->obj
;
3178 buffer_reg_stream_add(reg_stream
, buf_reg_chan
);
3180 /* We don't need the streams anymore. */
3181 cds_list_del(&stream
->list
);
3182 delete_ust_app_stream(-1, stream
, app
);
3190 * Create a buffer registry channel for the given session registry and
3191 * application channel object. If regp pointer is valid, it's set with the
3192 * created object. Important, the created object is NOT added to the session
3193 * registry hash table.
3195 * Return 0 on success else a negative value.
3197 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3198 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
3201 struct buffer_reg_channel
*buf_reg_chan
= NULL
;
3203 LTTNG_ASSERT(reg_sess
);
3204 LTTNG_ASSERT(ua_chan
);
3206 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3208 /* Create buffer registry channel. */
3209 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, &buf_reg_chan
);
3213 LTTNG_ASSERT(buf_reg_chan
);
3214 buf_reg_chan
->consumer_key
= ua_chan
->key
;
3215 buf_reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3216 buf_reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3218 /* Create and add a channel registry to session. */
3219 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
3220 ua_chan
->tracing_channel_id
);
3224 buffer_reg_channel_add(reg_sess
, buf_reg_chan
);
3227 *regp
= buf_reg_chan
;
3233 /* Safe because the registry channel object was not added to any HT. */
3234 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3240 * Setup buffer registry channel for the given session registry and application
3241 * channel object. If regp pointer is valid, it's set with the created object.
3243 * Return 0 on success else a negative value.
3245 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3246 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*buf_reg_chan
,
3247 struct ust_app
*app
)
3251 LTTNG_ASSERT(reg_sess
);
3252 LTTNG_ASSERT(buf_reg_chan
);
3253 LTTNG_ASSERT(ua_chan
);
3254 LTTNG_ASSERT(ua_chan
->obj
);
3256 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3258 /* Setup all streams for the registry. */
3259 ret
= setup_buffer_reg_streams(buf_reg_chan
, ua_chan
, app
);
3264 buf_reg_chan
->obj
.ust
= ua_chan
->obj
;
3265 ua_chan
->obj
= NULL
;
3270 buffer_reg_channel_remove(reg_sess
, buf_reg_chan
);
3271 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3276 * Send buffer registry channel to the application.
3278 * Return 0 on success else a negative value.
3280 static int send_channel_uid_to_ust(struct buffer_reg_channel
*buf_reg_chan
,
3281 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
3282 struct ust_app_channel
*ua_chan
)
3285 struct buffer_reg_stream
*reg_stream
;
3287 LTTNG_ASSERT(buf_reg_chan
);
3289 LTTNG_ASSERT(ua_sess
);
3290 LTTNG_ASSERT(ua_chan
);
3292 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3294 ret
= duplicate_channel_object(buf_reg_chan
, ua_chan
);
3299 /* Send channel to the application. */
3300 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3301 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3302 ret
= -ENOTCONN
; /* Caused by app exiting. */
3304 } else if (ret
== -EAGAIN
) {
3305 /* Caused by timeout. */
3306 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
"\".",
3307 app
->pid
, ua_chan
->name
, ua_sess
->tracing_id
);
3308 /* Treat this the same way as an application that is exiting. */
3311 } else if (ret
< 0) {
3315 health_code_update();
3317 /* Send all streams to application. */
3318 pthread_mutex_lock(&buf_reg_chan
->stream_list_lock
);
3319 cds_list_for_each_entry(reg_stream
, &buf_reg_chan
->streams
, lnode
) {
3320 struct ust_app_stream stream
;
3322 ret
= duplicate_stream_object(reg_stream
, &stream
);
3324 goto error_stream_unlock
;
3327 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3329 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3330 ret
= -ENOTCONN
; /* Caused by app exiting. */
3331 } else if (ret
== -EAGAIN
) {
3333 * Caused by timeout.
3334 * Treat this the same way as an application
3337 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
"\".",
3338 app
->pid
, stream
.name
,
3340 ua_sess
->tracing_id
);
3343 (void) release_ust_app_stream(-1, &stream
, app
);
3344 goto error_stream_unlock
;
3348 * The return value is not important here. This function will output an
3351 (void) release_ust_app_stream(-1, &stream
, app
);
3353 ua_chan
->is_sent
= 1;
3355 error_stream_unlock
:
3356 pthread_mutex_unlock(&buf_reg_chan
->stream_list_lock
);
3362 * Create and send to the application the created buffers with per UID buffers.
3364 * This MUST be called with a RCU read side lock acquired.
3365 * The session list lock and the session's lock must be acquired.
3367 * Return 0 on success else a negative value.
3369 static int create_channel_per_uid(struct ust_app
*app
,
3370 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3371 struct ust_app_channel
*ua_chan
)
3374 struct buffer_reg_uid
*reg_uid
;
3375 struct buffer_reg_channel
*buf_reg_chan
;
3376 struct ltt_session
*session
= NULL
;
3377 enum lttng_error_code notification_ret
;
3378 struct ust_registry_channel
*ust_reg_chan
;
3381 LTTNG_ASSERT(usess
);
3382 LTTNG_ASSERT(ua_sess
);
3383 LTTNG_ASSERT(ua_chan
);
3385 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3387 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3389 * The session creation handles the creation of this global registry
3390 * object. If none can be find, there is a code flow problem or a
3393 LTTNG_ASSERT(reg_uid
);
3395 buf_reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3401 /* Create the buffer registry channel object. */
3402 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, &buf_reg_chan
);
3404 ERR("Error creating the UST channel \"%s\" registry instance",
3409 session
= session_find_by_id(ua_sess
->tracing_id
);
3410 LTTNG_ASSERT(session
);
3411 LTTNG_ASSERT(pthread_mutex_trylock(&session
->lock
));
3412 LTTNG_ASSERT(session_trylock_list());
3415 * Create the buffers on the consumer side. This call populates the
3416 * ust app channel object with all streams and data object.
3418 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3419 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
3420 session
->most_recent_chunk_id
.value
);
3422 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3426 * Let's remove the previously created buffer registry channel so
3427 * it's not visible anymore in the session registry.
3429 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3430 ua_chan
->tracing_channel_id
, false);
3431 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
3432 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3437 * Setup the streams and add it to the session registry.
3439 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3440 ua_chan
, buf_reg_chan
, app
);
3442 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3446 /* Notify the notification subsystem of the channel's creation. */
3447 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
3448 ust_reg_chan
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3449 ua_chan
->tracing_channel_id
);
3450 LTTNG_ASSERT(ust_reg_chan
);
3451 ust_reg_chan
->consumer_key
= ua_chan
->key
;
3452 ust_reg_chan
= NULL
;
3453 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
3455 notification_ret
= notification_thread_command_add_channel(
3456 the_notification_thread_handle
, session
->name
,
3457 lttng_credentials_get_uid(
3458 &ua_sess
->effective_credentials
),
3459 lttng_credentials_get_gid(
3460 &ua_sess
->effective_credentials
),
3461 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3462 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3463 if (notification_ret
!= LTTNG_OK
) {
3464 ret
= - (int) notification_ret
;
3465 ERR("Failed to add channel to notification thread");
3470 /* Send buffers to the application. */
3471 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3473 if (ret
!= -ENOTCONN
) {
3474 ERR("Error sending channel to application");
3481 session_put(session
);
3487 * Create and send to the application the created buffers with per PID buffers.
3489 * Called with UST app session lock held.
3490 * The session list lock and the session's lock must be acquired.
3492 * Return 0 on success else a negative value.
3494 static int create_channel_per_pid(struct ust_app
*app
,
3495 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3496 struct ust_app_channel
*ua_chan
)
3499 struct ust_registry_session
*registry
;
3500 enum lttng_error_code cmd_ret
;
3501 struct ltt_session
*session
= NULL
;
3502 uint64_t chan_reg_key
;
3503 struct ust_registry_channel
*ust_reg_chan
;
3506 LTTNG_ASSERT(usess
);
3507 LTTNG_ASSERT(ua_sess
);
3508 LTTNG_ASSERT(ua_chan
);
3510 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3514 registry
= get_session_registry(ua_sess
);
3515 /* The UST app session lock is held, registry shall not be null. */
3516 LTTNG_ASSERT(registry
);
3518 /* Create and add a new channel registry to session. */
3519 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3521 ERR("Error creating the UST channel \"%s\" registry instance",
3526 session
= session_find_by_id(ua_sess
->tracing_id
);
3527 LTTNG_ASSERT(session
);
3529 LTTNG_ASSERT(pthread_mutex_trylock(&session
->lock
));
3530 LTTNG_ASSERT(session_trylock_list());
3532 /* Create and get channel on the consumer side. */
3533 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3534 app
->bits_per_long
, registry
,
3535 session
->most_recent_chunk_id
.value
);
3537 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3539 goto error_remove_from_registry
;
3542 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3544 if (ret
!= -ENOTCONN
) {
3545 ERR("Error sending channel to application");
3547 goto error_remove_from_registry
;
3550 chan_reg_key
= ua_chan
->key
;
3551 pthread_mutex_lock(®istry
->lock
);
3552 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
3553 LTTNG_ASSERT(ust_reg_chan
);
3554 ust_reg_chan
->consumer_key
= ua_chan
->key
;
3555 pthread_mutex_unlock(®istry
->lock
);
3557 cmd_ret
= notification_thread_command_add_channel(
3558 the_notification_thread_handle
, session
->name
,
3559 lttng_credentials_get_uid(
3560 &ua_sess
->effective_credentials
),
3561 lttng_credentials_get_gid(
3562 &ua_sess
->effective_credentials
),
3563 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3564 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3565 if (cmd_ret
!= LTTNG_OK
) {
3566 ret
= - (int) cmd_ret
;
3567 ERR("Failed to add channel to notification thread");
3568 goto error_remove_from_registry
;
3571 error_remove_from_registry
:
3573 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3578 session_put(session
);
3584 * From an already allocated ust app channel, create the channel buffers if
3585 * needed and send them to the application. This MUST be called with a RCU read
3586 * side lock acquired.
3588 * Called with UST app session lock held.
3590 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3591 * the application exited concurrently.
3593 static int ust_app_channel_send(struct ust_app
*app
,
3594 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3595 struct ust_app_channel
*ua_chan
)
3600 LTTNG_ASSERT(usess
);
3601 LTTNG_ASSERT(usess
->active
);
3602 LTTNG_ASSERT(ua_sess
);
3603 LTTNG_ASSERT(ua_chan
);
3605 /* Handle buffer type before sending the channel to the application. */
3606 switch (usess
->buffer_type
) {
3607 case LTTNG_BUFFER_PER_UID
:
3609 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3615 case LTTNG_BUFFER_PER_PID
:
3617 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3629 /* Initialize ust objd object using the received handle and add it. */
3630 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3631 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3633 /* If channel is not enabled, disable it on the tracer */
3634 if (!ua_chan
->enabled
) {
3635 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3646 * Create UST app channel and return it through ua_chanp if not NULL.
3648 * Called with UST app session lock and RCU read-side lock held.
3650 * Return 0 on success or else a negative value.
3652 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3653 struct ltt_ust_channel
*uchan
,
3654 enum lttng_ust_abi_chan_type type
, struct ltt_ust_session
*usess
,
3655 struct ust_app_channel
**ua_chanp
)
3658 struct lttng_ht_iter iter
;
3659 struct lttng_ht_node_str
*ua_chan_node
;
3660 struct ust_app_channel
*ua_chan
;
3662 /* Lookup channel in the ust app session */
3663 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3664 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3665 if (ua_chan_node
!= NULL
) {
3666 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3670 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3671 if (ua_chan
== NULL
) {
3672 /* Only malloc can fail here */
3676 shadow_copy_channel(ua_chan
, uchan
);
3678 /* Set channel type. */
3679 ua_chan
->attr
.type
= type
;
3681 /* Only add the channel if successful on the tracer side. */
3682 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3685 *ua_chanp
= ua_chan
;
3688 /* Everything went well. */
3696 * Create UST app event and create it on the tracer side.
3698 * Must be called with the RCU read side lock held.
3699 * Called with ust app session mutex held.
3702 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3703 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3704 struct ust_app
*app
)
3707 struct ust_app_event
*ua_event
;
3709 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3710 if (ua_event
== NULL
) {
3711 /* Only failure mode of alloc_ust_app_event(). */
3715 shadow_copy_event(ua_event
, uevent
);
3717 /* Create it on the tracer side */
3718 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3721 * Not found previously means that it does not exist on the
3722 * tracer. If the application reports that the event existed,
3723 * it means there is a bug in the sessiond or lttng-ust
3724 * (or corruption, etc.)
3726 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3727 ERR("Tracer for application reported that an event being created already existed: "
3728 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3730 app
->pid
, app
->ppid
, app
->uid
,
3736 add_unique_ust_app_event(ua_chan
, ua_event
);
3738 DBG2("UST app create event completed: app = '%s' pid = %d",
3739 app
->name
, app
->pid
);
3745 /* Valid. Calling here is already in a read side lock */
3746 delete_ust_app_event(-1, ua_event
, app
);
3751 * Create UST app event notifier rule and create it on the tracer side.
3753 * Must be called with the RCU read side lock held.
3754 * Called with ust app session mutex held.
3757 int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
,
3758 struct ust_app
*app
)
3761 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3763 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3764 if (ua_event_notifier_rule
== NULL
) {
3769 /* Create it on the tracer side. */
3770 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3773 * Not found previously means that it does not exist on the
3774 * tracer. If the application reports that the event existed,
3775 * it means there is a bug in the sessiond or lttng-ust
3776 * (or corruption, etc.)
3778 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3779 ERR("Tracer for application reported that an event notifier being created already exists: "
3780 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3781 lttng_trigger_get_tracer_token(trigger
),
3782 app
->pid
, app
->ppid
, app
->uid
,
3788 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3789 &ua_event_notifier_rule
->node
);
3791 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64
,
3792 app
->name
, app
->pid
, lttng_trigger_get_tracer_token(trigger
));
3797 /* The RCU read side lock is already being held by the caller. */
3798 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3804 * Create UST metadata and open it on the tracer side.
3806 * Called with UST app session lock held and RCU read side lock.
3808 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3809 struct ust_app
*app
, struct consumer_output
*consumer
)
3812 struct ust_app_channel
*metadata
;
3813 struct consumer_socket
*socket
;
3814 struct ust_registry_session
*registry
;
3815 struct ltt_session
*session
= NULL
;
3817 LTTNG_ASSERT(ua_sess
);
3819 LTTNG_ASSERT(consumer
);
3821 registry
= get_session_registry(ua_sess
);
3822 /* The UST app session is held registry shall not be null. */
3823 LTTNG_ASSERT(registry
);
3825 pthread_mutex_lock(®istry
->lock
);
3827 /* Metadata already exists for this registry or it was closed previously */
3828 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3833 /* Allocate UST metadata */
3834 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3836 /* malloc() failed */
3841 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3843 /* Need one fd for the channel. */
3844 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3846 ERR("Exhausted number of available FD upon create metadata");
3850 /* Get the right consumer socket for the application. */
3851 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3854 goto error_consumer
;
3858 * Keep metadata key so we can identify it on the consumer side. Assign it
3859 * to the registry *before* we ask the consumer so we avoid the race of the
3860 * consumer requesting the metadata and the ask_channel call on our side
3861 * did not returned yet.
3863 registry
->metadata_key
= metadata
->key
;
3865 session
= session_find_by_id(ua_sess
->tracing_id
);
3866 LTTNG_ASSERT(session
);
3868 LTTNG_ASSERT(pthread_mutex_trylock(&session
->lock
));
3869 LTTNG_ASSERT(session_trylock_list());
3872 * Ask the metadata channel creation to the consumer. The metadata object
3873 * will be created by the consumer and kept their. However, the stream is
3874 * never added or monitored until we do a first push metadata to the
3877 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3878 registry
, session
->current_trace_chunk
);
3880 /* Nullify the metadata key so we don't try to close it later on. */
3881 registry
->metadata_key
= 0;
3882 goto error_consumer
;
3886 * The setup command will make the metadata stream be sent to the relayd,
3887 * if applicable, and the thread managing the metadatas. This is important
3888 * because after this point, if an error occurs, the only way the stream
3889 * can be deleted is to be monitored in the consumer.
3891 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3893 /* Nullify the metadata key so we don't try to close it later on. */
3894 registry
->metadata_key
= 0;
3895 goto error_consumer
;
3898 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3899 metadata
->key
, app
->pid
);
3902 lttng_fd_put(LTTNG_FD_APPS
, 1);
3903 delete_ust_app_channel(-1, metadata
, app
);
3905 pthread_mutex_unlock(®istry
->lock
);
3907 session_put(session
);
3913 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3914 * acquired before calling this function.
3916 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3918 struct ust_app
*app
= NULL
;
3919 struct lttng_ht_node_ulong
*node
;
3920 struct lttng_ht_iter iter
;
3922 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3923 node
= lttng_ht_iter_get_node_ulong(&iter
);
3925 DBG2("UST app no found with pid %d", pid
);
3929 DBG2("Found UST app by pid %d", pid
);
3931 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3938 * Allocate and init an UST app object using the registration information and
3939 * the command socket. This is called when the command socket connects to the
3942 * The object is returned on success or else NULL.
3944 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3947 struct ust_app
*lta
= NULL
;
3948 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3951 LTTNG_ASSERT(sock
>= 0);
3953 DBG3("UST app creating application for socket %d", sock
);
3955 if ((msg
->bits_per_long
== 64 &&
3956 (uatomic_read(&the_ust_consumerd64_fd
) ==
3958 (msg
->bits_per_long
== 32 &&
3959 (uatomic_read(&the_ust_consumerd32_fd
) ==
3961 ERR("Registration failed: application \"%s\" (pid: %d) has "
3962 "%d-bit long, but no consumerd for this size is available.\n",
3963 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3968 * Reserve the two file descriptors of the event source pipe. The write
3969 * end will be closed once it is passed to the application, at which
3970 * point a single 'put' will be performed.
3972 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3974 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
3975 msg
->name
, (int) msg
->pid
);
3979 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3980 if (!event_notifier_event_source_pipe
) {
3981 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
3982 msg
->name
, msg
->pid
);
3986 lta
= (ust_app
*) zmalloc(sizeof(struct ust_app
));
3989 goto error_free_pipe
;
3992 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
3994 lta
->ppid
= msg
->ppid
;
3995 lta
->uid
= msg
->uid
;
3996 lta
->gid
= msg
->gid
;
3998 lta
->bits_per_long
= msg
->bits_per_long
;
3999 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
4000 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
4001 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
4002 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
4003 lta
->long_alignment
= msg
->long_alignment
;
4004 lta
->byte_order
= msg
->byte_order
;
4006 lta
->v_major
= msg
->major
;
4007 lta
->v_minor
= msg
->minor
;
4008 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4009 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4010 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4011 lta
->notify_sock
= -1;
4012 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4014 /* Copy name and make sure it's NULL terminated. */
4015 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
4016 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
4019 * Before this can be called, when receiving the registration information,
4020 * the application compatibility is checked. So, at this point, the
4021 * application can work with this session daemon.
4023 lta
->compatible
= 1;
4025 lta
->pid
= msg
->pid
;
4026 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
4028 pthread_mutex_init(<a
->sock_lock
, NULL
);
4029 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
4031 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
4035 lttng_pipe_destroy(event_notifier_event_source_pipe
);
4036 lttng_fd_put(LTTNG_FD_APPS
, 2);
4042 * For a given application object, add it to every hash table.
4044 void ust_app_add(struct ust_app
*app
)
4047 LTTNG_ASSERT(app
->notify_sock
>= 0);
4049 app
->registration_time
= time(NULL
);
4054 * On a re-registration, we want to kick out the previous registration of
4057 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
4060 * The socket _should_ be unique until _we_ call close. So, a add_unique
4061 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4062 * already in the table.
4064 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
4066 /* Add application to the notify socket hash table. */
4067 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
4068 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
4070 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4071 "notify_sock =%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
4072 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
4079 * Set the application version into the object.
4081 * Return 0 on success else a negative value either an errno code or a
4082 * LTTng-UST error code.
4084 int ust_app_version(struct ust_app
*app
)
4090 pthread_mutex_lock(&app
->sock_lock
);
4091 ret
= lttng_ust_ctl_tracer_version(app
->sock
, &app
->version
);
4092 pthread_mutex_unlock(&app
->sock_lock
);
4094 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
4095 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4096 app
->pid
, app
->sock
);
4097 } else if (ret
== -EAGAIN
) {
4098 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4099 app
->pid
, app
->sock
);
4101 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4102 ret
, app
->pid
, app
->sock
);
4109 bool ust_app_supports_notifiers(const struct ust_app
*app
)
4111 return app
->v_major
>= 9;
4114 bool ust_app_supports_counters(const struct ust_app
*app
)
4116 return app
->v_major
>= 9;
4120 * Setup the base event notifier group.
4122 * Return 0 on success else a negative value either an errno code or a
4123 * LTTng-UST error code.
4125 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4128 int event_pipe_write_fd
;
4129 struct lttng_ust_abi_object_data
*event_notifier_group
= NULL
;
4130 enum lttng_error_code lttng_ret
;
4131 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4135 if (!ust_app_supports_notifiers(app
)) {
4140 /* Get the write side of the pipe. */
4141 event_pipe_write_fd
= lttng_pipe_get_writefd(
4142 app
->event_notifier_group
.event_pipe
);
4144 pthread_mutex_lock(&app
->sock_lock
);
4145 ret
= lttng_ust_ctl_create_event_notifier_group(app
->sock
,
4146 event_pipe_write_fd
, &event_notifier_group
);
4147 pthread_mutex_unlock(&app
->sock_lock
);
4149 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4151 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4152 app
->pid
, app
->sock
);
4153 } else if (ret
== -EAGAIN
) {
4155 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4156 app
->pid
, app
->sock
);
4158 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4159 ret
, app
->pid
, app
->sock
, event_pipe_write_fd
);
4164 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4166 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4167 app
->name
, app
->pid
);
4172 * Release the file descriptor that was reserved for the write-end of
4175 lttng_fd_put(LTTNG_FD_APPS
, 1);
4177 lttng_ret
= notification_thread_command_add_tracer_event_source(
4178 the_notification_thread_handle
,
4179 lttng_pipe_get_readfd(
4180 app
->event_notifier_group
.event_pipe
),
4182 if (lttng_ret
!= LTTNG_OK
) {
4183 ERR("Failed to add tracer event source to notification thread");
4188 /* Assign handle only when the complete setup is valid. */
4189 app
->event_notifier_group
.object
= event_notifier_group
;
4191 event_notifier_error_accounting_status
=
4192 event_notifier_error_accounting_register_app(app
);
4193 switch (event_notifier_error_accounting_status
) {
4194 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
4196 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
:
4197 DBG3("Failed to setup event notifier error accounting (application does not support notifier error accounting): app socket fd = %d, app name = '%s', app pid = %d",
4198 app
->sock
, app
->name
, (int) app
->pid
);
4200 goto error_accounting
;
4201 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
4202 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4203 app
->sock
, app
->name
, (int) app
->pid
);
4205 goto error_accounting
;
4207 ERR("Failed to setup event notifier error accounting for app");
4209 goto error_accounting
;
4215 lttng_ret
= notification_thread_command_remove_tracer_event_source(
4216 the_notification_thread_handle
,
4217 lttng_pipe_get_readfd(
4218 app
->event_notifier_group
.event_pipe
));
4219 if (lttng_ret
!= LTTNG_OK
) {
4220 ERR("Failed to remove application tracer event source from notification thread");
4224 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4225 free(app
->event_notifier_group
.object
);
4226 app
->event_notifier_group
.object
= NULL
;
4231 * Unregister app by removing it from the global traceable app list and freeing
4234 * The socket is already closed at this point so no close to sock.
4236 void ust_app_unregister(int sock
)
4238 struct ust_app
*lta
;
4239 struct lttng_ht_node_ulong
*node
;
4240 struct lttng_ht_iter ust_app_sock_iter
;
4241 struct lttng_ht_iter iter
;
4242 struct ust_app_session
*ua_sess
;
4247 /* Get the node reference for a call_rcu */
4248 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
4249 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4252 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
4253 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4256 * For per-PID buffers, perform "push metadata" and flush all
4257 * application streams before removing app from hash tables,
4258 * ensuring proper behavior of data_pending check.
4259 * Remove sessions so they are not visible during deletion.
4261 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
4263 struct ust_registry_session
*registry
;
4265 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4267 /* The session was already removed so scheduled for teardown. */
4271 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4272 (void) ust_app_flush_app_session(lta
, ua_sess
);
4276 * Add session to list for teardown. This is safe since at this point we
4277 * are the only one using this list.
4279 pthread_mutex_lock(&ua_sess
->lock
);
4281 if (ua_sess
->deleted
) {
4282 pthread_mutex_unlock(&ua_sess
->lock
);
4287 * Normally, this is done in the delete session process which is
4288 * executed in the call rcu below. However, upon registration we can't
4289 * afford to wait for the grace period before pushing data or else the
4290 * data pending feature can race between the unregistration and stop
4291 * command where the data pending command is sent *before* the grace
4294 * The close metadata below nullifies the metadata pointer in the
4295 * session so the delete session will NOT push/close a second time.
4297 registry
= get_session_registry(ua_sess
);
4299 /* Push metadata for application before freeing the application. */
4300 (void) push_metadata(registry
, ua_sess
->consumer
);
4303 * Don't ask to close metadata for global per UID buffers. Close
4304 * metadata only on destroy trace session in this case. Also, the
4305 * previous push metadata could have flag the metadata registry to
4306 * close so don't send a close command if closed.
4308 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4309 /* And ask to close it for this session registry. */
4310 (void) close_metadata(registry
, ua_sess
->consumer
);
4313 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4315 pthread_mutex_unlock(&ua_sess
->lock
);
4318 /* Remove application from PID hash table */
4319 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4323 * Remove application from notify hash table. The thread handling the
4324 * notify socket could have deleted the node so ignore on error because
4325 * either way it's valid. The close of that socket is handled by the
4326 * apps_notify_thread.
4328 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4329 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4332 * Ignore return value since the node might have been removed before by an
4333 * add replace during app registration because the PID can be reassigned by
4336 iter
.iter
.node
= <a
->pid_n
.node
;
4337 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4339 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4344 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4351 * Fill events array with all events name of all registered apps.
4353 int ust_app_list_events(struct lttng_event
**events
)
4356 size_t nbmem
, count
= 0;
4357 struct lttng_ht_iter iter
;
4358 struct ust_app
*app
;
4359 struct lttng_event
*tmp_event
;
4361 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4362 tmp_event
= (lttng_event
*) zmalloc(nbmem
* sizeof(struct lttng_event
));
4363 if (tmp_event
== NULL
) {
4364 PERROR("zmalloc ust app events");
4371 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4372 struct lttng_ust_abi_tracepoint_iter uiter
;
4374 health_code_update();
4376 if (!app
->compatible
) {
4378 * TODO: In time, we should notice the caller of this error by
4379 * telling him that this is a version error.
4383 pthread_mutex_lock(&app
->sock_lock
);
4384 handle
= lttng_ust_ctl_tracepoint_list(app
->sock
);
4386 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4387 ERR("UST app list events getting handle failed for app pid %d",
4390 pthread_mutex_unlock(&app
->sock_lock
);
4394 while ((ret
= lttng_ust_ctl_tracepoint_list_get(app
->sock
, handle
,
4395 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4396 /* Handle ustctl error. */
4400 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4401 ERR("UST app tp list get failed for app %d with ret %d",
4404 DBG3("UST app tp list get failed. Application is dead");
4408 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4409 if (release_ret
< 0 &&
4410 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4411 release_ret
!= -EPIPE
) {
4412 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4414 pthread_mutex_unlock(&app
->sock_lock
);
4418 health_code_update();
4419 if (count
>= nbmem
) {
4420 /* In case the realloc fails, we free the memory */
4421 struct lttng_event
*new_tmp_event
;
4424 new_nbmem
= nbmem
<< 1;
4425 DBG2("Reallocating event list from %zu to %zu entries",
4427 new_tmp_event
= (lttng_event
*) realloc(tmp_event
,
4428 new_nbmem
* sizeof(struct lttng_event
));
4429 if (new_tmp_event
== NULL
) {
4432 PERROR("realloc ust app events");
4435 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4436 if (release_ret
< 0 &&
4437 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4438 release_ret
!= -EPIPE
) {
4439 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4441 pthread_mutex_unlock(&app
->sock_lock
);
4444 /* Zero the new memory */
4445 memset(new_tmp_event
+ nbmem
, 0,
4446 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4448 tmp_event
= new_tmp_event
;
4450 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4451 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4452 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4453 tmp_event
[count
].pid
= app
->pid
;
4454 tmp_event
[count
].enabled
= -1;
4457 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4458 pthread_mutex_unlock(&app
->sock_lock
);
4460 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4461 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4462 app
->pid
, app
->sock
);
4463 } else if (ret
== -EAGAIN
) {
4464 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4465 app
->pid
, app
->sock
);
4467 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4468 ret
, app
->pid
, app
->sock
);
4474 *events
= tmp_event
;
4476 DBG2("UST app list events done (%zu events)", count
);
4481 health_code_update();
4486 * Fill events array with all events name of all registered apps.
4488 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4491 size_t nbmem
, count
= 0;
4492 struct lttng_ht_iter iter
;
4493 struct ust_app
*app
;
4494 struct lttng_event_field
*tmp_event
;
4496 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4497 tmp_event
= (lttng_event_field
*) zmalloc(nbmem
* sizeof(struct lttng_event_field
));
4498 if (tmp_event
== NULL
) {
4499 PERROR("zmalloc ust app event fields");
4506 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4507 struct lttng_ust_abi_field_iter uiter
;
4509 health_code_update();
4511 if (!app
->compatible
) {
4513 * TODO: In time, we should notice the caller of this error by
4514 * telling him that this is a version error.
4518 pthread_mutex_lock(&app
->sock_lock
);
4519 handle
= lttng_ust_ctl_tracepoint_field_list(app
->sock
);
4521 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4522 ERR("UST app list field getting handle failed for app pid %d",
4525 pthread_mutex_unlock(&app
->sock_lock
);
4529 while ((ret
= lttng_ust_ctl_tracepoint_field_list_get(app
->sock
, handle
,
4530 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4531 /* Handle ustctl error. */
4535 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4536 ERR("UST app tp list field failed for app %d with ret %d",
4539 DBG3("UST app tp list field failed. Application is dead");
4543 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4544 pthread_mutex_unlock(&app
->sock_lock
);
4545 if (release_ret
< 0 &&
4546 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4547 release_ret
!= -EPIPE
) {
4548 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4553 health_code_update();
4554 if (count
>= nbmem
) {
4555 /* In case the realloc fails, we free the memory */
4556 struct lttng_event_field
*new_tmp_event
;
4559 new_nbmem
= nbmem
<< 1;
4560 DBG2("Reallocating event field list from %zu to %zu entries",
4562 new_tmp_event
= (lttng_event_field
*) realloc(tmp_event
,
4563 new_nbmem
* sizeof(struct lttng_event_field
));
4564 if (new_tmp_event
== NULL
) {
4567 PERROR("realloc ust app event fields");
4570 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4571 pthread_mutex_unlock(&app
->sock_lock
);
4573 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4574 release_ret
!= -EPIPE
) {
4575 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4579 /* Zero the new memory */
4580 memset(new_tmp_event
+ nbmem
, 0,
4581 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4583 tmp_event
= new_tmp_event
;
4586 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4587 /* Mapping between these enums matches 1 to 1. */
4588 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4589 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4591 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4592 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4593 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4594 tmp_event
[count
].event
.pid
= app
->pid
;
4595 tmp_event
[count
].event
.enabled
= -1;
4598 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4599 pthread_mutex_unlock(&app
->sock_lock
);
4601 ret
!= -LTTNG_UST_ERR_EXITING
&&
4603 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4608 *fields
= tmp_event
;
4610 DBG2("UST app list event fields done (%zu events)", count
);
4615 health_code_update();
4620 * Free and clean all traceable apps of the global list.
4622 * Should _NOT_ be called with RCU read-side lock held.
4624 void ust_app_clean_list(void)
4627 struct ust_app
*app
;
4628 struct lttng_ht_iter iter
;
4630 DBG2("UST app cleaning registered apps hash table");
4634 /* Cleanup notify socket hash table */
4635 if (ust_app_ht_by_notify_sock
) {
4636 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4637 notify_sock_n
.node
) {
4639 * Assert that all notifiers are gone as all triggers
4640 * are unregistered prior to this clean-up.
4642 LTTNG_ASSERT(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4644 ust_app_notify_sock_unregister(app
->notify_sock
);
4649 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4650 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4652 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4656 /* Cleanup socket hash table */
4657 if (ust_app_ht_by_sock
) {
4658 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4660 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4667 /* Destroy is done only when the ht is empty */
4669 ht_cleanup_push(ust_app_ht
);
4671 if (ust_app_ht_by_sock
) {
4672 ht_cleanup_push(ust_app_ht_by_sock
);
4674 if (ust_app_ht_by_notify_sock
) {
4675 ht_cleanup_push(ust_app_ht_by_notify_sock
);
4680 * Init UST app hash table.
4682 int ust_app_ht_alloc(void)
4684 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4688 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4689 if (!ust_app_ht_by_sock
) {
4692 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4693 if (!ust_app_ht_by_notify_sock
) {
4700 * For a specific UST session, disable the channel for all registered apps.
4702 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4703 struct ltt_ust_channel
*uchan
)
4706 struct lttng_ht_iter iter
;
4707 struct lttng_ht_node_str
*ua_chan_node
;
4708 struct ust_app
*app
;
4709 struct ust_app_session
*ua_sess
;
4710 struct ust_app_channel
*ua_chan
;
4712 LTTNG_ASSERT(usess
->active
);
4713 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4714 uchan
->name
, usess
->id
);
4718 /* For every registered applications */
4719 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4720 struct lttng_ht_iter uiter
;
4721 if (!app
->compatible
) {
4723 * TODO: In time, we should notice the caller of this error by
4724 * telling him that this is a version error.
4728 ua_sess
= lookup_session_by_app(usess
, app
);
4729 if (ua_sess
== NULL
) {
4734 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4735 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4736 /* If the session if found for the app, the channel must be there */
4737 LTTNG_ASSERT(ua_chan_node
);
4739 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4740 /* The channel must not be already disabled */
4741 LTTNG_ASSERT(ua_chan
->enabled
== 1);
4743 /* Disable channel onto application */
4744 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4746 /* XXX: We might want to report this error at some point... */
4756 * For a specific UST session, enable the channel for all registered apps.
4758 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4759 struct ltt_ust_channel
*uchan
)
4762 struct lttng_ht_iter iter
;
4763 struct ust_app
*app
;
4764 struct ust_app_session
*ua_sess
;
4766 LTTNG_ASSERT(usess
->active
);
4767 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4768 uchan
->name
, usess
->id
);
4772 /* For every registered applications */
4773 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4774 if (!app
->compatible
) {
4776 * TODO: In time, we should notice the caller of this error by
4777 * telling him that this is a version error.
4781 ua_sess
= lookup_session_by_app(usess
, app
);
4782 if (ua_sess
== NULL
) {
4786 /* Enable channel onto application */
4787 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4789 /* XXX: We might want to report this error at some point... */
4799 * Disable an event in a channel and for a specific session.
4801 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4802 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4805 struct lttng_ht_iter iter
, uiter
;
4806 struct lttng_ht_node_str
*ua_chan_node
;
4807 struct ust_app
*app
;
4808 struct ust_app_session
*ua_sess
;
4809 struct ust_app_channel
*ua_chan
;
4810 struct ust_app_event
*ua_event
;
4812 LTTNG_ASSERT(usess
->active
);
4813 DBG("UST app disabling event %s for all apps in channel "
4814 "%s for session id %" PRIu64
,
4815 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4819 /* For all registered applications */
4820 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4821 if (!app
->compatible
) {
4823 * TODO: In time, we should notice the caller of this error by
4824 * telling him that this is a version error.
4828 ua_sess
= lookup_session_by_app(usess
, app
);
4829 if (ua_sess
== NULL
) {
4834 /* Lookup channel in the ust app session */
4835 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4836 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4837 if (ua_chan_node
== NULL
) {
4838 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4839 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4842 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4844 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4845 uevent
->filter
, uevent
->attr
.loglevel
,
4847 if (ua_event
== NULL
) {
4848 DBG2("Event %s not found in channel %s for app pid %d."
4849 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4853 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4855 /* XXX: Report error someday... */
4864 /* The ua_sess lock must be held by the caller. */
4866 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4867 struct ust_app_session
*ua_sess
,
4868 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4869 struct ust_app_channel
**_ua_chan
)
4872 struct ust_app_channel
*ua_chan
= NULL
;
4874 LTTNG_ASSERT(ua_sess
);
4875 ASSERT_LOCKED(ua_sess
->lock
);
4877 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4878 sizeof(uchan
->name
))) {
4879 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4883 struct ltt_ust_context
*uctx
= NULL
;
4886 * Create channel onto application and synchronize its
4889 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4890 LTTNG_UST_ABI_CHAN_PER_CPU
, usess
,
4896 ret
= ust_app_channel_send(app
, usess
,
4903 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4904 ret
= create_ust_app_channel_context(ua_chan
,
4917 * The application's socket is not valid. Either a bad socket
4918 * or a timeout on it. We can't inform the caller that for a
4919 * specific app, the session failed so lets continue here.
4921 ret
= 0; /* Not an error. */
4929 if (ret
== 0 && _ua_chan
) {
4931 * Only return the application's channel on success. Note
4932 * that the channel can still be part of the application's
4933 * channel hashtable on error.
4935 *_ua_chan
= ua_chan
;
4941 * Enable event for a specific session and channel on the tracer.
4943 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4944 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4947 struct lttng_ht_iter iter
, uiter
;
4948 struct lttng_ht_node_str
*ua_chan_node
;
4949 struct ust_app
*app
;
4950 struct ust_app_session
*ua_sess
;
4951 struct ust_app_channel
*ua_chan
;
4952 struct ust_app_event
*ua_event
;
4954 LTTNG_ASSERT(usess
->active
);
4955 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4956 uevent
->attr
.name
, usess
->id
);
4959 * NOTE: At this point, this function is called only if the session and
4960 * channel passed are already created for all apps. and enabled on the
4966 /* For all registered applications */
4967 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4968 if (!app
->compatible
) {
4970 * TODO: In time, we should notice the caller of this error by
4971 * telling him that this is a version error.
4975 ua_sess
= lookup_session_by_app(usess
, app
);
4977 /* The application has problem or is probably dead. */
4981 pthread_mutex_lock(&ua_sess
->lock
);
4983 if (ua_sess
->deleted
) {
4984 pthread_mutex_unlock(&ua_sess
->lock
);
4988 /* Lookup channel in the ust app session */
4989 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4990 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4992 * It is possible that the channel cannot be found is
4993 * the channel/event creation occurs concurrently with
4994 * an application exit.
4996 if (!ua_chan_node
) {
4997 pthread_mutex_unlock(&ua_sess
->lock
);
5001 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5003 /* Get event node */
5004 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5005 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5006 if (ua_event
== NULL
) {
5007 DBG3("UST app enable event %s not found for app PID %d."
5008 "Skipping app", uevent
->attr
.name
, app
->pid
);
5012 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
5014 pthread_mutex_unlock(&ua_sess
->lock
);
5018 pthread_mutex_unlock(&ua_sess
->lock
);
5027 * For a specific existing UST session and UST channel, creates the event for
5028 * all registered apps.
5030 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
5031 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
5034 struct lttng_ht_iter iter
, uiter
;
5035 struct lttng_ht_node_str
*ua_chan_node
;
5036 struct ust_app
*app
;
5037 struct ust_app_session
*ua_sess
;
5038 struct ust_app_channel
*ua_chan
;
5040 LTTNG_ASSERT(usess
->active
);
5041 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
5042 uevent
->attr
.name
, usess
->id
);
5046 /* For all registered applications */
5047 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5048 if (!app
->compatible
) {
5050 * TODO: In time, we should notice the caller of this error by
5051 * telling him that this is a version error.
5055 ua_sess
= lookup_session_by_app(usess
, app
);
5057 /* The application has problem or is probably dead. */
5061 pthread_mutex_lock(&ua_sess
->lock
);
5063 if (ua_sess
->deleted
) {
5064 pthread_mutex_unlock(&ua_sess
->lock
);
5068 /* Lookup channel in the ust app session */
5069 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5070 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5071 /* If the channel is not found, there is a code flow error */
5072 LTTNG_ASSERT(ua_chan_node
);
5074 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5076 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5077 pthread_mutex_unlock(&ua_sess
->lock
);
5079 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
5080 /* Possible value at this point: -ENOMEM. If so, we stop! */
5083 DBG2("UST app event %s already exist on app PID %d",
5084 uevent
->attr
.name
, app
->pid
);
5094 * Start tracing for a specific UST session and app.
5096 * Called with UST app session lock held.
5100 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5103 struct ust_app_session
*ua_sess
;
5105 DBG("Starting tracing for ust app pid %d", app
->pid
);
5109 if (!app
->compatible
) {
5113 ua_sess
= lookup_session_by_app(usess
, app
);
5114 if (ua_sess
== NULL
) {
5115 /* The session is in teardown process. Ignore and continue. */
5119 pthread_mutex_lock(&ua_sess
->lock
);
5121 if (ua_sess
->deleted
) {
5122 pthread_mutex_unlock(&ua_sess
->lock
);
5126 if (ua_sess
->enabled
) {
5127 pthread_mutex_unlock(&ua_sess
->lock
);
5131 /* Upon restart, we skip the setup, already done */
5132 if (ua_sess
->started
) {
5136 health_code_update();
5139 /* This starts the UST tracing */
5140 pthread_mutex_lock(&app
->sock_lock
);
5141 ret
= lttng_ust_ctl_start_session(app
->sock
, ua_sess
->handle
);
5142 pthread_mutex_unlock(&app
->sock_lock
);
5144 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5145 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5146 app
->pid
, app
->sock
);
5147 pthread_mutex_unlock(&ua_sess
->lock
);
5149 } else if (ret
== -EAGAIN
) {
5150 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5151 app
->pid
, app
->sock
);
5152 pthread_mutex_unlock(&ua_sess
->lock
);
5156 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5157 ret
, app
->pid
, app
->sock
);
5162 /* Indicate that the session has been started once */
5163 ua_sess
->started
= 1;
5164 ua_sess
->enabled
= 1;
5166 pthread_mutex_unlock(&ua_sess
->lock
);
5168 health_code_update();
5170 /* Quiescent wait after starting trace */
5171 pthread_mutex_lock(&app
->sock_lock
);
5172 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5173 pthread_mutex_unlock(&app
->sock_lock
);
5175 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5176 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5177 app
->pid
, app
->sock
);
5178 } else if (ret
== -EAGAIN
) {
5179 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5180 app
->pid
, app
->sock
);
5182 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5183 ret
, app
->pid
, app
->sock
);
5189 health_code_update();
5193 pthread_mutex_unlock(&ua_sess
->lock
);
5195 health_code_update();
5200 * Stop tracing for a specific UST session and app.
5203 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5206 struct ust_app_session
*ua_sess
;
5207 struct ust_registry_session
*registry
;
5209 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5213 if (!app
->compatible
) {
5214 goto end_no_session
;
5217 ua_sess
= lookup_session_by_app(usess
, app
);
5218 if (ua_sess
== NULL
) {
5219 goto end_no_session
;
5222 pthread_mutex_lock(&ua_sess
->lock
);
5224 if (ua_sess
->deleted
) {
5225 pthread_mutex_unlock(&ua_sess
->lock
);
5226 goto end_no_session
;
5230 * If started = 0, it means that stop trace has been called for a session
5231 * that was never started. It's possible since we can have a fail start
5232 * from either the application manager thread or the command thread. Simply
5233 * indicate that this is a stop error.
5235 if (!ua_sess
->started
) {
5236 goto error_rcu_unlock
;
5239 health_code_update();
5241 /* This inhibits UST tracing */
5242 pthread_mutex_lock(&app
->sock_lock
);
5243 ret
= lttng_ust_ctl_stop_session(app
->sock
, ua_sess
->handle
);
5244 pthread_mutex_unlock(&app
->sock_lock
);
5246 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5247 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5248 app
->pid
, app
->sock
);
5250 } else if (ret
== -EAGAIN
) {
5251 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5252 app
->pid
, app
->sock
);
5256 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5257 ret
, app
->pid
, app
->sock
);
5259 goto error_rcu_unlock
;
5262 health_code_update();
5263 ua_sess
->enabled
= 0;
5265 /* Quiescent wait after stopping trace */
5266 pthread_mutex_lock(&app
->sock_lock
);
5267 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5268 pthread_mutex_unlock(&app
->sock_lock
);
5270 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5271 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5272 app
->pid
, app
->sock
);
5273 } else if (ret
== -EAGAIN
) {
5274 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5275 app
->pid
, app
->sock
);
5277 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5278 ret
, app
->pid
, app
->sock
);
5282 health_code_update();
5284 registry
= get_session_registry(ua_sess
);
5286 /* The UST app session is held registry shall not be null. */
5287 LTTNG_ASSERT(registry
);
5289 /* Push metadata for application before freeing the application. */
5290 (void) push_metadata(registry
, ua_sess
->consumer
);
5293 pthread_mutex_unlock(&ua_sess
->lock
);
5296 health_code_update();
5300 pthread_mutex_unlock(&ua_sess
->lock
);
5302 health_code_update();
5307 int ust_app_flush_app_session(struct ust_app
*app
,
5308 struct ust_app_session
*ua_sess
)
5310 int ret
, retval
= 0;
5311 struct lttng_ht_iter iter
;
5312 struct ust_app_channel
*ua_chan
;
5313 struct consumer_socket
*socket
;
5315 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5319 if (!app
->compatible
) {
5320 goto end_not_compatible
;
5323 pthread_mutex_lock(&ua_sess
->lock
);
5325 if (ua_sess
->deleted
) {
5329 health_code_update();
5331 /* Flushing buffers */
5332 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5335 /* Flush buffers and push metadata. */
5336 switch (ua_sess
->buffer_type
) {
5337 case LTTNG_BUFFER_PER_PID
:
5338 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5340 health_code_update();
5341 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5343 ERR("Error flushing consumer channel");
5349 case LTTNG_BUFFER_PER_UID
:
5355 health_code_update();
5358 pthread_mutex_unlock(&ua_sess
->lock
);
5362 health_code_update();
5367 * Flush buffers for all applications for a specific UST session.
5368 * Called with UST session lock held.
5371 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5376 DBG("Flushing session buffers for all ust apps");
5380 /* Flush buffers and push metadata. */
5381 switch (usess
->buffer_type
) {
5382 case LTTNG_BUFFER_PER_UID
:
5384 struct buffer_reg_uid
*reg
;
5385 struct lttng_ht_iter iter
;
5387 /* Flush all per UID buffers associated to that session. */
5388 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5389 struct ust_registry_session
*ust_session_reg
;
5390 struct buffer_reg_channel
*buf_reg_chan
;
5391 struct consumer_socket
*socket
;
5393 /* Get consumer socket to use to push the metadata.*/
5394 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5397 /* Ignore request if no consumer is found for the session. */
5401 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5402 buf_reg_chan
, node
.node
) {
5404 * The following call will print error values so the return
5405 * code is of little importance because whatever happens, we
5406 * have to try them all.
5408 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5411 ust_session_reg
= reg
->registry
->reg
.ust
;
5412 /* Push metadata. */
5413 (void) push_metadata(ust_session_reg
, usess
->consumer
);
5417 case LTTNG_BUFFER_PER_PID
:
5419 struct ust_app_session
*ua_sess
;
5420 struct lttng_ht_iter iter
;
5421 struct ust_app
*app
;
5423 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5424 ua_sess
= lookup_session_by_app(usess
, app
);
5425 if (ua_sess
== NULL
) {
5428 (void) ust_app_flush_app_session(app
, ua_sess
);
5439 health_code_update();
5444 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5445 struct ust_app_session
*ua_sess
)
5448 struct lttng_ht_iter iter
;
5449 struct ust_app_channel
*ua_chan
;
5450 struct consumer_socket
*socket
;
5452 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5456 if (!app
->compatible
) {
5457 goto end_not_compatible
;
5460 pthread_mutex_lock(&ua_sess
->lock
);
5462 if (ua_sess
->deleted
) {
5466 health_code_update();
5468 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5471 ERR("Failed to find consumer (%" PRIu32
") socket",
5472 app
->bits_per_long
);
5477 /* Clear quiescent state. */
5478 switch (ua_sess
->buffer_type
) {
5479 case LTTNG_BUFFER_PER_PID
:
5480 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5481 ua_chan
, node
.node
) {
5482 health_code_update();
5483 ret
= consumer_clear_quiescent_channel(socket
,
5486 ERR("Error clearing quiescent state for consumer channel");
5492 case LTTNG_BUFFER_PER_UID
:
5499 health_code_update();
5502 pthread_mutex_unlock(&ua_sess
->lock
);
5506 health_code_update();
5511 * Clear quiescent state in each stream for all applications for a
5512 * specific UST session.
5513 * Called with UST session lock held.
5516 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5521 DBG("Clearing stream quiescent state for all ust apps");
5525 switch (usess
->buffer_type
) {
5526 case LTTNG_BUFFER_PER_UID
:
5528 struct lttng_ht_iter iter
;
5529 struct buffer_reg_uid
*reg
;
5532 * Clear quiescent for all per UID buffers associated to
5535 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5536 struct consumer_socket
*socket
;
5537 struct buffer_reg_channel
*buf_reg_chan
;
5539 /* Get associated consumer socket.*/
5540 socket
= consumer_find_socket_by_bitness(
5541 reg
->bits_per_long
, usess
->consumer
);
5544 * Ignore request if no consumer is found for
5550 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5551 &iter
.iter
, buf_reg_chan
, node
.node
) {
5553 * The following call will print error values so
5554 * the return code is of little importance
5555 * because whatever happens, we have to try them
5558 (void) consumer_clear_quiescent_channel(socket
,
5559 buf_reg_chan
->consumer_key
);
5564 case LTTNG_BUFFER_PER_PID
:
5566 struct ust_app_session
*ua_sess
;
5567 struct lttng_ht_iter iter
;
5568 struct ust_app
*app
;
5570 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5572 ua_sess
= lookup_session_by_app(usess
, app
);
5573 if (ua_sess
== NULL
) {
5576 (void) ust_app_clear_quiescent_app_session(app
,
5588 health_code_update();
5593 * Destroy a specific UST session in apps.
5595 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5598 struct ust_app_session
*ua_sess
;
5599 struct lttng_ht_iter iter
;
5600 struct lttng_ht_node_u64
*node
;
5602 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5606 if (!app
->compatible
) {
5610 __lookup_session_by_app(usess
, app
, &iter
);
5611 node
= lttng_ht_iter_get_node_u64(&iter
);
5613 /* Session is being or is deleted. */
5616 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
5618 health_code_update();
5619 destroy_app_session(app
, ua_sess
);
5621 health_code_update();
5623 /* Quiescent wait after stopping trace */
5624 pthread_mutex_lock(&app
->sock_lock
);
5625 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5626 pthread_mutex_unlock(&app
->sock_lock
);
5628 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5629 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5630 app
->pid
, app
->sock
);
5631 } else if (ret
== -EAGAIN
) {
5632 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5633 app
->pid
, app
->sock
);
5635 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5636 ret
, app
->pid
, app
->sock
);
5641 health_code_update();
5646 * Start tracing for the UST session.
5648 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5650 struct lttng_ht_iter iter
;
5651 struct ust_app
*app
;
5653 DBG("Starting all UST traces");
5656 * Even though the start trace might fail, flag this session active so
5657 * other application coming in are started by default.
5664 * In a start-stop-start use-case, we need to clear the quiescent state
5665 * of each channel set by the prior stop command, thus ensuring that a
5666 * following stop or destroy is sure to grab a timestamp_end near those
5667 * operations, even if the packet is empty.
5669 (void) ust_app_clear_quiescent_session(usess
);
5671 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5672 ust_app_global_update(usess
, app
);
5681 * Start tracing for the UST session.
5682 * Called with UST session lock held.
5684 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5687 struct lttng_ht_iter iter
;
5688 struct ust_app
*app
;
5690 DBG("Stopping all UST traces");
5693 * Even though the stop trace might fail, flag this session inactive so
5694 * other application coming in are not started by default.
5700 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5701 ret
= ust_app_stop_trace(usess
, app
);
5703 /* Continue to next apps even on error */
5708 (void) ust_app_flush_session(usess
);
5716 * Destroy app UST session.
5718 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5721 struct lttng_ht_iter iter
;
5722 struct ust_app
*app
;
5724 DBG("Destroy all UST traces");
5728 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5729 ret
= destroy_trace(usess
, app
);
5731 /* Continue to next apps even on error */
5741 /* The ua_sess lock must be held by the caller. */
5743 int find_or_create_ust_app_channel(
5744 struct ltt_ust_session
*usess
,
5745 struct ust_app_session
*ua_sess
,
5746 struct ust_app
*app
,
5747 struct ltt_ust_channel
*uchan
,
5748 struct ust_app_channel
**ua_chan
)
5751 struct lttng_ht_iter iter
;
5752 struct lttng_ht_node_str
*ua_chan_node
;
5754 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5755 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5757 *ua_chan
= caa_container_of(ua_chan_node
,
5758 struct ust_app_channel
, node
);
5762 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5771 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5772 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5773 struct ust_app
*app
)
5776 struct ust_app_event
*ua_event
= NULL
;
5778 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5779 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5781 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5786 if (ua_event
->enabled
!= uevent
->enabled
) {
5787 ret
= uevent
->enabled
?
5788 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5789 disable_ust_app_event(ua_sess
, ua_event
, app
);
5797 /* Called with RCU read-side lock held. */
5799 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5802 enum lttng_error_code ret_code
;
5803 enum lttng_trigger_status t_status
;
5804 struct lttng_ht_iter app_trigger_iter
;
5805 struct lttng_triggers
*triggers
= NULL
;
5806 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5807 unsigned int count
, i
;
5809 if (!ust_app_supports_notifiers(app
)) {
5814 * Currrently, registering or unregistering a trigger with an
5815 * event rule condition causes a full synchronization of the event
5818 * The first step attempts to add an event notifier for all registered
5819 * triggers that apply to the user space tracers. Then, the
5820 * application's event notifiers rules are all checked against the list
5821 * of registered triggers. Any event notifier that doesn't have a
5822 * matching trigger can be assumed to have been disabled.
5824 * All of this is inefficient, but is put in place to get the feature
5825 * rolling as it is simpler at this moment. It will be optimized Soon™
5826 * to allow the state of enabled
5827 * event notifiers to be synchronized in a piece-wise way.
5830 /* Get all triggers using uid 0 (root) */
5831 ret_code
= notification_thread_command_list_triggers(
5832 the_notification_thread_handle
, 0, &triggers
);
5833 if (ret_code
!= LTTNG_OK
) {
5837 LTTNG_ASSERT(triggers
);
5839 t_status
= lttng_triggers_get_count(triggers
, &count
);
5840 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5844 for (i
= 0; i
< count
; i
++) {
5845 struct lttng_condition
*condition
;
5846 struct lttng_event_rule
*event_rule
;
5847 struct lttng_trigger
*trigger
;
5848 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5849 enum lttng_condition_status condition_status
;
5852 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5853 LTTNG_ASSERT(trigger
);
5855 token
= lttng_trigger_get_tracer_token(trigger
);
5856 condition
= lttng_trigger_get_condition(trigger
);
5858 if (lttng_condition_get_type(condition
) !=
5859 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
) {
5860 /* Does not apply */
5865 lttng_condition_event_rule_matches_borrow_rule_mutable(
5866 condition
, &event_rule
);
5867 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5869 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5870 /* Skip kernel related triggers. */
5875 * Find or create the associated token event rule. The caller
5876 * holds the RCU read lock, so this is safe to call without
5877 * explicitly acquiring it here.
5879 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
5880 app
->token_to_event_notifier_rule_ht
, token
);
5881 if (!looked_up_event_notifier_rule
) {
5882 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
5890 /* Remove all unknown event sources from the app. */
5891 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
5892 &app_trigger_iter
.iter
, event_notifier_rule
,
5894 const uint64_t app_token
= event_notifier_rule
->token
;
5898 * Check if the app event trigger still exists on the
5899 * notification side.
5901 for (i
= 0; i
< count
; i
++) {
5902 uint64_t notification_thread_token
;
5903 const struct lttng_trigger
*trigger
=
5904 lttng_triggers_get_at_index(
5907 LTTNG_ASSERT(trigger
);
5909 notification_thread_token
=
5910 lttng_trigger_get_tracer_token(trigger
);
5912 if (notification_thread_token
== app_token
) {
5924 * This trigger was unregistered, disable it on the tracer's
5927 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
5929 LTTNG_ASSERT(ret
== 0);
5931 /* Callee logs errors. */
5932 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
5934 delete_ust_app_event_notifier_rule(
5935 app
->sock
, event_notifier_rule
, app
);
5941 lttng_triggers_destroy(triggers
);
5946 * RCU read lock must be held by the caller.
5949 void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
5950 struct ust_app_session
*ua_sess
,
5951 struct ust_app
*app
)
5954 struct cds_lfht_iter uchan_iter
;
5955 struct ltt_ust_channel
*uchan
;
5957 LTTNG_ASSERT(usess
);
5958 LTTNG_ASSERT(ua_sess
);
5961 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5963 struct ust_app_channel
*ua_chan
;
5964 struct cds_lfht_iter uevent_iter
;
5965 struct ltt_ust_event
*uevent
;
5968 * Search for a matching ust_app_channel. If none is found,
5969 * create it. Creating the channel will cause the ua_chan
5970 * structure to be allocated, the channel buffers to be
5971 * allocated (if necessary) and sent to the application, and
5972 * all enabled contexts will be added to the channel.
5974 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5975 app
, uchan
, &ua_chan
);
5977 /* Tracer is probably gone or ENOMEM. */
5982 /* ua_chan will be NULL for the metadata channel */
5986 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5988 ret
= ust_app_channel_synchronize_event(ua_chan
,
5989 uevent
, ua_sess
, app
);
5995 if (ua_chan
->enabled
!= uchan
->enabled
) {
5996 ret
= uchan
->enabled
?
5997 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5998 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
6009 * The caller must ensure that the application is compatible and is tracked
6010 * by the process attribute trackers.
6013 void ust_app_synchronize(struct ltt_ust_session
*usess
,
6014 struct ust_app
*app
)
6017 struct ust_app_session
*ua_sess
= NULL
;
6020 * The application's configuration should only be synchronized for
6023 LTTNG_ASSERT(usess
->active
);
6025 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
6027 /* Tracer is probably gone or ENOMEM. */
6029 destroy_app_session(app
, ua_sess
);
6033 LTTNG_ASSERT(ua_sess
);
6035 pthread_mutex_lock(&ua_sess
->lock
);
6036 if (ua_sess
->deleted
) {
6037 goto deleted_session
;
6042 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
6045 * Create the metadata for the application. This returns gracefully if a
6046 * metadata was already set for the session.
6048 * The metadata channel must be created after the data channels as the
6049 * consumer daemon assumes this ordering. When interacting with a relay
6050 * daemon, the consumer will use this assumption to send the
6051 * "STREAMS_SENT" message to the relay daemon.
6053 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
6055 ERR("Metadata creation failed for app sock %d for session id %" PRIu64
,
6056 app
->sock
, usess
->id
);
6062 pthread_mutex_unlock(&ua_sess
->lock
);
6068 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6070 struct ust_app_session
*ua_sess
;
6072 ua_sess
= lookup_session_by_app(usess
, app
);
6073 if (ua_sess
== NULL
) {
6076 destroy_app_session(app
, ua_sess
);
6080 * Add channels/events from UST global domain to registered apps at sock.
6082 * Called with session lock held.
6083 * Called with RCU read-side lock held.
6085 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6087 LTTNG_ASSERT(usess
);
6088 LTTNG_ASSERT(usess
->active
);
6090 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
6091 app
->sock
, usess
->id
);
6093 if (!app
->compatible
) {
6096 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
6098 trace_ust_id_tracker_lookup(
6099 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
6101 trace_ust_id_tracker_lookup(
6102 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
6105 * Synchronize the application's internal tracing configuration
6106 * and start tracing.
6108 ust_app_synchronize(usess
, app
);
6109 ust_app_start_trace(usess
, app
);
6111 ust_app_global_destroy(usess
, app
);
6116 * Add all event notifiers to an application.
6118 * Called with session lock held.
6119 * Called with RCU read-side lock held.
6121 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
6123 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6124 app
->name
, app
->pid
);
6126 if (!app
->compatible
|| !ust_app_supports_notifiers(app
)) {
6130 if (app
->event_notifier_group
.object
== NULL
) {
6131 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6132 app
->name
, app
->pid
);
6136 ust_app_synchronize_event_notifier_rules(app
);
6140 * Called with session lock held.
6142 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
6144 struct lttng_ht_iter iter
;
6145 struct ust_app
*app
;
6148 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6149 ust_app_global_update(usess
, app
);
6154 void ust_app_global_update_all_event_notifier_rules(void)
6156 struct lttng_ht_iter iter
;
6157 struct ust_app
*app
;
6160 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6161 ust_app_global_update_event_notifier_rules(app
);
6168 * Add context to a specific channel for global UST domain.
6170 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
6171 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
6174 struct lttng_ht_node_str
*ua_chan_node
;
6175 struct lttng_ht_iter iter
, uiter
;
6176 struct ust_app_channel
*ua_chan
= NULL
;
6177 struct ust_app_session
*ua_sess
;
6178 struct ust_app
*app
;
6180 LTTNG_ASSERT(usess
->active
);
6183 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6184 if (!app
->compatible
) {
6186 * TODO: In time, we should notice the caller of this error by
6187 * telling him that this is a version error.
6191 ua_sess
= lookup_session_by_app(usess
, app
);
6192 if (ua_sess
== NULL
) {
6196 pthread_mutex_lock(&ua_sess
->lock
);
6198 if (ua_sess
->deleted
) {
6199 pthread_mutex_unlock(&ua_sess
->lock
);
6203 /* Lookup channel in the ust app session */
6204 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
6205 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6206 if (ua_chan_node
== NULL
) {
6209 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
6211 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6216 pthread_mutex_unlock(&ua_sess
->lock
);
6224 * Receive registration and populate the given msg structure.
6226 * On success return 0 else a negative value returned by the ustctl call.
6228 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6231 uint32_t pid
, ppid
, uid
, gid
;
6235 ret
= lttng_ust_ctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
6236 &pid
, &ppid
, &uid
, &gid
,
6237 &msg
->bits_per_long
,
6238 &msg
->uint8_t_alignment
,
6239 &msg
->uint16_t_alignment
,
6240 &msg
->uint32_t_alignment
,
6241 &msg
->uint64_t_alignment
,
6242 &msg
->long_alignment
,
6249 case LTTNG_UST_ERR_EXITING
:
6250 DBG3("UST app recv reg message failed. Application died");
6252 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6253 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6254 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
6255 LTTNG_UST_ABI_MINOR_VERSION
);
6258 ERR("UST app recv reg message failed with ret %d", ret
);
6263 msg
->pid
= (pid_t
) pid
;
6264 msg
->ppid
= (pid_t
) ppid
;
6265 msg
->uid
= (uid_t
) uid
;
6266 msg
->gid
= (gid_t
) gid
;
6273 * Return a ust app session object using the application object and the
6274 * session object descriptor has a key. If not found, NULL is returned.
6275 * A RCU read side lock MUST be acquired when calling this function.
6277 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
6280 struct lttng_ht_node_ulong
*node
;
6281 struct lttng_ht_iter iter
;
6282 struct ust_app_session
*ua_sess
= NULL
;
6286 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
6287 node
= lttng_ht_iter_get_node_ulong(&iter
);
6289 DBG2("UST app session find by objd %d not found", objd
);
6293 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
6300 * Return a ust app channel object using the application object and the channel
6301 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6302 * lock MUST be acquired before calling this function.
6304 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
6307 struct lttng_ht_node_ulong
*node
;
6308 struct lttng_ht_iter iter
;
6309 struct ust_app_channel
*ua_chan
= NULL
;
6313 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
6314 node
= lttng_ht_iter_get_node_ulong(&iter
);
6316 DBG2("UST app channel find by objd %d not found", objd
);
6320 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
6327 * Reply to a register channel notification from an application on the notify
6328 * socket. The channel metadata is also created.
6330 * The session UST registry lock is acquired in this function.
6332 * On success 0 is returned else a negative value.
6334 static int reply_ust_register_channel(int sock
, int cobjd
,
6335 size_t nr_fields
, struct lttng_ust_ctl_field
*fields
)
6337 int ret
, ret_code
= 0;
6339 uint64_t chan_reg_key
;
6340 enum lttng_ust_ctl_channel_header type
;
6341 struct ust_app
*app
;
6342 struct ust_app_channel
*ua_chan
;
6343 struct ust_app_session
*ua_sess
;
6344 struct ust_registry_session
*registry
;
6345 struct ust_registry_channel
*ust_reg_chan
;
6349 /* Lookup application. If not found, there is a code flow error. */
6350 app
= find_app_by_notify_sock(sock
);
6352 DBG("Application socket %d is being torn down. Abort event notify",
6355 goto error_rcu_unlock
;
6358 /* Lookup channel by UST object descriptor. */
6359 ua_chan
= find_channel_by_objd(app
, cobjd
);
6361 DBG("Application channel is being torn down. Abort event notify");
6363 goto error_rcu_unlock
;
6366 LTTNG_ASSERT(ua_chan
->session
);
6367 ua_sess
= ua_chan
->session
;
6369 /* Get right session registry depending on the session buffer type. */
6370 registry
= get_session_registry(ua_sess
);
6372 DBG("Application session is being torn down. Abort event notify");
6374 goto error_rcu_unlock
;
6377 /* Depending on the buffer type, a different channel key is used. */
6378 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6379 chan_reg_key
= ua_chan
->tracing_channel_id
;
6381 chan_reg_key
= ua_chan
->key
;
6384 pthread_mutex_lock(®istry
->lock
);
6386 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
6387 LTTNG_ASSERT(ust_reg_chan
);
6389 if (!ust_reg_chan
->register_done
) {
6391 * TODO: eventually use the registry event count for
6392 * this channel to better guess header type for per-pid
6395 type
= LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
;
6396 ust_reg_chan
->nr_ctx_fields
= nr_fields
;
6397 ust_reg_chan
->ctx_fields
= fields
;
6399 ust_reg_chan
->header_type
= type
;
6401 /* Get current already assigned values. */
6402 type
= ust_reg_chan
->header_type
;
6404 /* Channel id is set during the object creation. */
6405 chan_id
= ust_reg_chan
->chan_id
;
6407 /* Append to metadata */
6408 if (!ust_reg_chan
->metadata_dumped
) {
6409 ret_code
= ust_metadata_channel_statedump(registry
, ust_reg_chan
);
6411 ERR("Error appending channel metadata (errno = %d)", ret_code
);
6417 DBG3("UST app replying to register channel key %" PRIu64
6418 " with id %u, type = %d, ret = %d", chan_reg_key
, chan_id
, type
,
6421 ret
= lttng_ust_ctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
6423 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6424 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6425 app
->pid
, app
->sock
);
6426 } else if (ret
== -EAGAIN
) {
6427 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6428 app
->pid
, app
->sock
);
6430 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6431 ret
, app
->pid
, app
->sock
);
6436 /* This channel registry registration is completed. */
6437 ust_reg_chan
->register_done
= 1;
6440 pthread_mutex_unlock(®istry
->lock
);
6448 * Add event to the UST channel registry. When the event is added to the
6449 * registry, the metadata is also created. Once done, this replies to the
6450 * application with the appropriate error code.
6452 * The session UST registry lock is acquired in the function.
6454 * On success 0 is returned else a negative value.
6456 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
6457 char *sig
, size_t nr_fields
, struct lttng_ust_ctl_field
*fields
,
6458 int loglevel_value
, char *model_emf_uri
)
6461 uint32_t event_id
= 0;
6462 uint64_t chan_reg_key
;
6463 struct ust_app
*app
;
6464 struct ust_app_channel
*ua_chan
;
6465 struct ust_app_session
*ua_sess
;
6466 struct ust_registry_session
*registry
;
6470 /* Lookup application. If not found, there is a code flow error. */
6471 app
= find_app_by_notify_sock(sock
);
6473 DBG("Application socket %d is being torn down. Abort event notify",
6476 goto error_rcu_unlock
;
6479 /* Lookup channel by UST object descriptor. */
6480 ua_chan
= find_channel_by_objd(app
, cobjd
);
6482 DBG("Application channel is being torn down. Abort event notify");
6484 goto error_rcu_unlock
;
6487 LTTNG_ASSERT(ua_chan
->session
);
6488 ua_sess
= ua_chan
->session
;
6490 registry
= get_session_registry(ua_sess
);
6492 DBG("Application session is being torn down. Abort event notify");
6494 goto error_rcu_unlock
;
6497 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6498 chan_reg_key
= ua_chan
->tracing_channel_id
;
6500 chan_reg_key
= ua_chan
->key
;
6503 pthread_mutex_lock(®istry
->lock
);
6506 * From this point on, this call acquires the ownership of the sig, fields
6507 * and model_emf_uri meaning any free are done inside it if needed. These
6508 * three variables MUST NOT be read/write after this.
6510 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
6511 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
6512 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
6516 model_emf_uri
= NULL
;
6519 * The return value is returned to ustctl so in case of an error, the
6520 * application can be notified. In case of an error, it's important not to
6521 * return a negative error or else the application will get closed.
6523 ret
= lttng_ust_ctl_reply_register_event(sock
, event_id
, ret_code
);
6525 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6526 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6527 app
->pid
, app
->sock
);
6528 } else if (ret
== -EAGAIN
) {
6529 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6530 app
->pid
, app
->sock
);
6532 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6533 ret
, app
->pid
, app
->sock
);
6536 * No need to wipe the create event since the application socket will
6537 * get close on error hence cleaning up everything by itself.
6542 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6546 pthread_mutex_unlock(®istry
->lock
);
6551 free(model_emf_uri
);
6556 * Add enum to the UST session registry. Once done, this replies to the
6557 * application with the appropriate error code.
6559 * The session UST registry lock is acquired within this function.
6561 * On success 0 is returned else a negative value.
6563 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
6564 struct lttng_ust_ctl_enum_entry
*entries
, size_t nr_entries
)
6566 int ret
= 0, ret_code
;
6567 struct ust_app
*app
;
6568 struct ust_app_session
*ua_sess
;
6569 struct ust_registry_session
*registry
;
6570 uint64_t enum_id
= -1ULL;
6574 /* Lookup application. If not found, there is a code flow error. */
6575 app
= find_app_by_notify_sock(sock
);
6577 /* Return an error since this is not an error */
6578 DBG("Application socket %d is being torn down. Aborting enum registration",
6582 goto error_rcu_unlock
;
6585 /* Lookup session by UST object descriptor. */
6586 ua_sess
= find_session_by_objd(app
, sobjd
);
6588 /* Return an error since this is not an error */
6589 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6591 goto error_rcu_unlock
;
6594 registry
= get_session_registry(ua_sess
);
6596 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6598 goto error_rcu_unlock
;
6601 pthread_mutex_lock(®istry
->lock
);
6604 * From this point on, the callee acquires the ownership of
6605 * entries. The variable entries MUST NOT be read/written after
6608 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
6609 entries
, nr_entries
, &enum_id
);
6613 * The return value is returned to ustctl so in case of an error, the
6614 * application can be notified. In case of an error, it's important not to
6615 * return a negative error or else the application will get closed.
6617 ret
= lttng_ust_ctl_reply_register_enum(sock
, enum_id
, ret_code
);
6619 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6620 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6621 app
->pid
, app
->sock
);
6622 } else if (ret
== -EAGAIN
) {
6623 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6624 app
->pid
, app
->sock
);
6626 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6627 ret
, app
->pid
, app
->sock
);
6630 * No need to wipe the create enum since the application socket will
6631 * get close on error hence cleaning up everything by itself.
6636 DBG3("UST registry enum %s added successfully or already found", name
);
6639 pthread_mutex_unlock(®istry
->lock
);
6646 * Handle application notification through the given notify socket.
6648 * Return 0 on success or else a negative value.
6650 int ust_app_recv_notify(int sock
)
6653 enum lttng_ust_ctl_notify_cmd cmd
;
6655 DBG3("UST app receiving notify from sock %d", sock
);
6657 ret
= lttng_ust_ctl_recv_notify(sock
, &cmd
);
6659 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6660 DBG3("UST app recv notify failed. Application died: sock = %d",
6662 } else if (ret
== -EAGAIN
) {
6663 WARN("UST app recv notify failed. Communication time out: sock = %d",
6666 ERR("UST app recv notify failed with ret %d: sock = %d",
6673 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT
:
6675 int sobjd
, cobjd
, loglevel_value
;
6676 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6678 struct lttng_ust_ctl_field
*fields
;
6680 DBG2("UST app ustctl register event received");
6682 ret
= lttng_ust_ctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
6683 &loglevel_value
, &sig
, &nr_fields
, &fields
,
6686 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6687 DBG3("UST app recv event failed. Application died: sock = %d",
6689 } else if (ret
== -EAGAIN
) {
6690 WARN("UST app recv event failed. Communication time out: sock = %d",
6693 ERR("UST app recv event failed with ret %d: sock = %d",
6700 * Add event to the UST registry coming from the notify socket. This
6701 * call will free if needed the sig, fields and model_emf_uri. This
6702 * code path loses the ownsership of these variables and transfer them
6703 * to the this function.
6705 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6706 fields
, loglevel_value
, model_emf_uri
);
6713 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
:
6717 struct lttng_ust_ctl_field
*fields
;
6719 DBG2("UST app ustctl register channel received");
6721 ret
= lttng_ust_ctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
6724 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6725 DBG3("UST app recv channel failed. Application died: sock = %d",
6727 } else if (ret
== -EAGAIN
) {
6728 WARN("UST app recv channel failed. Communication time out: sock = %d",
6731 ERR("UST app recv channel failed with ret %d: sock = %d",
6738 * The fields ownership are transfered to this function call meaning
6739 * that if needed it will be freed. After this, it's invalid to access
6740 * fields or clean it up.
6742 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
6750 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM
:
6753 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
6755 struct lttng_ust_ctl_enum_entry
*entries
;
6757 DBG2("UST app ustctl register enum received");
6759 ret
= lttng_ust_ctl_recv_register_enum(sock
, &sobjd
, name
,
6760 &entries
, &nr_entries
);
6762 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6763 DBG3("UST app recv enum failed. Application died: sock = %d",
6765 } else if (ret
== -EAGAIN
) {
6766 WARN("UST app recv enum failed. Communication time out: sock = %d",
6769 ERR("UST app recv enum failed with ret %d: sock = %d",
6775 /* Callee assumes ownership of entries */
6776 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6777 entries
, nr_entries
);
6785 /* Should NEVER happen. */
6794 * Once the notify socket hangs up, this is called. First, it tries to find the
6795 * corresponding application. On failure, the call_rcu to close the socket is
6796 * executed. If an application is found, it tries to delete it from the notify
6797 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6799 * Note that an object needs to be allocated here so on ENOMEM failure, the
6800 * call RCU is not done but the rest of the cleanup is.
6802 void ust_app_notify_sock_unregister(int sock
)
6805 struct lttng_ht_iter iter
;
6806 struct ust_app
*app
;
6807 struct ust_app_notify_sock_obj
*obj
;
6809 LTTNG_ASSERT(sock
>= 0);
6813 obj
= (ust_app_notify_sock_obj
*) zmalloc(sizeof(*obj
));
6816 * An ENOMEM is kind of uncool. If this strikes we continue the
6817 * procedure but the call_rcu will not be called. In this case, we
6818 * accept the fd leak rather than possibly creating an unsynchronized
6819 * state between threads.
6821 * TODO: The notify object should be created once the notify socket is
6822 * registered and stored independantely from the ust app object. The
6823 * tricky part is to synchronize the teardown of the application and
6824 * this notify object. Let's keep that in mind so we can avoid this
6825 * kind of shenanigans with ENOMEM in the teardown path.
6832 DBG("UST app notify socket unregister %d", sock
);
6835 * Lookup application by notify socket. If this fails, this means that the
6836 * hash table delete has already been done by the application
6837 * unregistration process so we can safely close the notify socket in a
6840 app
= find_app_by_notify_sock(sock
);
6845 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6848 * Whatever happens here either we fail or succeed, in both cases we have
6849 * to close the socket after a grace period to continue to the call RCU
6850 * here. If the deletion is successful, the application is not visible
6851 * anymore by other threads and is it fails it means that it was already
6852 * deleted from the hash table so either way we just have to close the
6855 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6861 * Close socket after a grace period to avoid for the socket to be reused
6862 * before the application object is freed creating potential race between
6863 * threads trying to add unique in the global hash table.
6866 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6871 * Destroy a ust app data structure and free its memory.
6873 void ust_app_destroy(struct ust_app
*app
)
6879 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6883 * Take a snapshot for a given UST session. The snapshot is sent to the given
6886 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6888 enum lttng_error_code
ust_app_snapshot_record(
6889 const struct ltt_ust_session
*usess
,
6890 const struct consumer_output
*output
, int wait
,
6891 uint64_t nb_packets_per_stream
)
6894 enum lttng_error_code status
= LTTNG_OK
;
6895 struct lttng_ht_iter iter
;
6896 struct ust_app
*app
;
6897 char *trace_path
= NULL
;
6899 LTTNG_ASSERT(usess
);
6900 LTTNG_ASSERT(output
);
6904 switch (usess
->buffer_type
) {
6905 case LTTNG_BUFFER_PER_UID
:
6907 struct buffer_reg_uid
*reg
;
6909 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
6910 struct buffer_reg_channel
*buf_reg_chan
;
6911 struct consumer_socket
*socket
;
6912 char pathname
[PATH_MAX
];
6913 size_t consumer_path_offset
= 0;
6915 if (!reg
->registry
->reg
.ust
->metadata_key
) {
6916 /* Skip since no metadata is present */
6920 /* Get consumer socket to use to push the metadata.*/
6921 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
6924 status
= LTTNG_ERR_INVALID
;
6928 memset(pathname
, 0, sizeof(pathname
));
6929 ret
= snprintf(pathname
, sizeof(pathname
),
6930 DEFAULT_UST_TRACE_UID_PATH
,
6931 reg
->uid
, reg
->bits_per_long
);
6933 PERROR("snprintf snapshot path");
6934 status
= LTTNG_ERR_INVALID
;
6937 /* Free path allowed on previous iteration. */
6939 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
6940 &consumer_path_offset
);
6942 status
= LTTNG_ERR_INVALID
;
6945 /* Add the UST default trace dir to path. */
6946 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
6947 buf_reg_chan
, node
.node
) {
6948 status
= consumer_snapshot_channel(socket
,
6949 buf_reg_chan
->consumer_key
,
6950 output
, 0, usess
->uid
,
6951 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
6952 nb_packets_per_stream
);
6953 if (status
!= LTTNG_OK
) {
6957 status
= consumer_snapshot_channel(socket
,
6958 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
6959 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
6961 if (status
!= LTTNG_OK
) {
6967 case LTTNG_BUFFER_PER_PID
:
6969 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6970 struct consumer_socket
*socket
;
6971 struct lttng_ht_iter chan_iter
;
6972 struct ust_app_channel
*ua_chan
;
6973 struct ust_app_session
*ua_sess
;
6974 struct ust_registry_session
*registry
;
6975 char pathname
[PATH_MAX
];
6976 size_t consumer_path_offset
= 0;
6978 ua_sess
= lookup_session_by_app(usess
, app
);
6980 /* Session not associated with this app. */
6984 /* Get the right consumer socket for the application. */
6985 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
6988 status
= LTTNG_ERR_INVALID
;
6992 /* Add the UST default trace dir to path. */
6993 memset(pathname
, 0, sizeof(pathname
));
6994 ret
= snprintf(pathname
, sizeof(pathname
), "%s",
6997 status
= LTTNG_ERR_INVALID
;
6998 PERROR("snprintf snapshot path");
7001 /* Free path allowed on previous iteration. */
7003 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
7004 &consumer_path_offset
);
7006 status
= LTTNG_ERR_INVALID
;
7009 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7010 ua_chan
, node
.node
) {
7011 status
= consumer_snapshot_channel(socket
,
7012 ua_chan
->key
, output
, 0,
7013 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7014 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7015 &trace_path
[consumer_path_offset
], wait
,
7016 nb_packets_per_stream
);
7020 case LTTNG_ERR_CHAN_NOT_FOUND
:
7027 registry
= get_session_registry(ua_sess
);
7029 DBG("Application session is being torn down. Skip application.");
7032 status
= consumer_snapshot_channel(socket
,
7033 registry
->metadata_key
, output
, 1,
7034 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7035 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7036 &trace_path
[consumer_path_offset
], wait
, 0);
7040 case LTTNG_ERR_CHAN_NOT_FOUND
:
7060 * Return the size taken by one more packet per stream.
7062 uint64_t ust_app_get_size_one_more_packet_per_stream(
7063 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
7065 uint64_t tot_size
= 0;
7066 struct ust_app
*app
;
7067 struct lttng_ht_iter iter
;
7069 LTTNG_ASSERT(usess
);
7071 switch (usess
->buffer_type
) {
7072 case LTTNG_BUFFER_PER_UID
:
7074 struct buffer_reg_uid
*reg
;
7076 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7077 struct buffer_reg_channel
*buf_reg_chan
;
7080 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7081 buf_reg_chan
, node
.node
) {
7082 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
7084 * Don't take channel into account if we
7085 * already grab all its packets.
7089 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
7095 case LTTNG_BUFFER_PER_PID
:
7098 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7099 struct ust_app_channel
*ua_chan
;
7100 struct ust_app_session
*ua_sess
;
7101 struct lttng_ht_iter chan_iter
;
7103 ua_sess
= lookup_session_by_app(usess
, app
);
7105 /* Session not associated with this app. */
7109 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7110 ua_chan
, node
.node
) {
7111 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
7113 * Don't take channel into account if we
7114 * already grab all its packets.
7118 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
7132 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
7133 struct cds_list_head
*buffer_reg_uid_list
,
7134 struct consumer_output
*consumer
, uint64_t uchan_id
,
7135 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
7138 uint64_t consumer_chan_key
;
7143 ret
= buffer_reg_uid_consumer_channel_key(
7144 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
7152 ret
= consumer_get_lost_packets(ust_session_id
,
7153 consumer_chan_key
, consumer
, lost
);
7155 ret
= consumer_get_discarded_events(ust_session_id
,
7156 consumer_chan_key
, consumer
, discarded
);
7163 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
7164 struct ltt_ust_channel
*uchan
,
7165 struct consumer_output
*consumer
, int overwrite
,
7166 uint64_t *discarded
, uint64_t *lost
)
7169 struct lttng_ht_iter iter
;
7170 struct lttng_ht_node_str
*ua_chan_node
;
7171 struct ust_app
*app
;
7172 struct ust_app_session
*ua_sess
;
7173 struct ust_app_channel
*ua_chan
;
7180 * Iterate over every registered applications. Sum counters for
7181 * all applications containing requested session and channel.
7183 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7184 struct lttng_ht_iter uiter
;
7186 ua_sess
= lookup_session_by_app(usess
, app
);
7187 if (ua_sess
== NULL
) {
7192 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
7193 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
7194 /* If the session is found for the app, the channel must be there */
7195 LTTNG_ASSERT(ua_chan_node
);
7197 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
7202 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
7209 uint64_t _discarded
;
7211 ret
= consumer_get_discarded_events(usess
->id
,
7212 ua_chan
->key
, consumer
, &_discarded
);
7216 (*discarded
) += _discarded
;
7225 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
7226 struct ust_app
*app
)
7229 struct ust_app_session
*ua_sess
;
7231 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7235 ua_sess
= lookup_session_by_app(usess
, app
);
7236 if (ua_sess
== NULL
) {
7237 /* The session is in teardown process. Ignore and continue. */
7241 pthread_mutex_lock(&ua_sess
->lock
);
7243 if (ua_sess
->deleted
) {
7247 pthread_mutex_lock(&app
->sock_lock
);
7248 ret
= lttng_ust_ctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7249 pthread_mutex_unlock(&app
->sock_lock
);
7252 pthread_mutex_unlock(&ua_sess
->lock
);
7256 health_code_update();
7261 * Regenerate the statedump for each app in the session.
7263 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7266 struct lttng_ht_iter iter
;
7267 struct ust_app
*app
;
7269 DBG("Regenerating the metadata for all UST apps");
7273 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7274 if (!app
->compatible
) {
7278 ret
= ust_app_regenerate_statedump(usess
, app
);
7280 /* Continue to the next app even on error */
7291 * Rotate all the channels of a session.
7293 * Return LTTNG_OK on success or else an LTTng error code.
7295 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7298 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7299 struct lttng_ht_iter iter
;
7300 struct ust_app
*app
;
7301 struct ltt_ust_session
*usess
= session
->ust_session
;
7303 LTTNG_ASSERT(usess
);
7307 switch (usess
->buffer_type
) {
7308 case LTTNG_BUFFER_PER_UID
:
7310 struct buffer_reg_uid
*reg
;
7312 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7313 struct buffer_reg_channel
*buf_reg_chan
;
7314 struct consumer_socket
*socket
;
7316 /* Get consumer socket to use to push the metadata.*/
7317 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7320 cmd_ret
= LTTNG_ERR_INVALID
;
7324 /* Rotate the data channels. */
7325 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7326 buf_reg_chan
, node
.node
) {
7327 ret
= consumer_rotate_channel(socket
,
7328 buf_reg_chan
->consumer_key
,
7329 usess
->uid
, usess
->gid
,
7331 /* is_metadata_channel */ false);
7333 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7339 * The metadata channel might not be present.
7341 * Consumer stream allocation can be done
7342 * asynchronously and can fail on intermediary
7343 * operations (i.e add context) and lead to data
7344 * channels created with no metadata channel.
7346 if (!reg
->registry
->reg
.ust
->metadata_key
) {
7347 /* Skip since no metadata is present. */
7351 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7353 ret
= consumer_rotate_channel(socket
,
7354 reg
->registry
->reg
.ust
->metadata_key
,
7355 usess
->uid
, usess
->gid
,
7357 /* is_metadata_channel */ true);
7359 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7365 case LTTNG_BUFFER_PER_PID
:
7367 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7368 struct consumer_socket
*socket
;
7369 struct lttng_ht_iter chan_iter
;
7370 struct ust_app_channel
*ua_chan
;
7371 struct ust_app_session
*ua_sess
;
7372 struct ust_registry_session
*registry
;
7374 ua_sess
= lookup_session_by_app(usess
, app
);
7376 /* Session not associated with this app. */
7380 /* Get the right consumer socket for the application. */
7381 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7384 cmd_ret
= LTTNG_ERR_INVALID
;
7388 registry
= get_session_registry(ua_sess
);
7390 DBG("Application session is being torn down. Skip application.");
7394 /* Rotate the data channels. */
7395 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7396 ua_chan
, node
.node
) {
7397 ret
= consumer_rotate_channel(socket
,
7399 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7400 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7402 /* is_metadata_channel */ false);
7404 /* Per-PID buffer and application going away. */
7405 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7407 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7412 /* Rotate the metadata channel. */
7413 (void) push_metadata(registry
, usess
->consumer
);
7414 ret
= consumer_rotate_channel(socket
,
7415 registry
->metadata_key
,
7416 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7417 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7419 /* is_metadata_channel */ true);
7421 /* Per-PID buffer and application going away. */
7422 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7424 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7442 enum lttng_error_code
ust_app_create_channel_subdirectories(
7443 const struct ltt_ust_session
*usess
)
7445 enum lttng_error_code ret
= LTTNG_OK
;
7446 struct lttng_ht_iter iter
;
7447 enum lttng_trace_chunk_status chunk_status
;
7448 char *pathname_index
;
7451 LTTNG_ASSERT(usess
->current_trace_chunk
);
7454 switch (usess
->buffer_type
) {
7455 case LTTNG_BUFFER_PER_UID
:
7457 struct buffer_reg_uid
*reg
;
7459 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7460 fmt_ret
= asprintf(&pathname_index
,
7461 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7462 reg
->uid
, reg
->bits_per_long
);
7464 ERR("Failed to format channel index directory");
7465 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7470 * Create the index subdirectory which will take care
7471 * of implicitly creating the channel's path.
7473 chunk_status
= lttng_trace_chunk_create_subdirectory(
7474 usess
->current_trace_chunk
,
7476 free(pathname_index
);
7477 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7478 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7484 case LTTNG_BUFFER_PER_PID
:
7486 struct ust_app
*app
;
7489 * Create the toplevel ust/ directory in case no apps are running.
7491 chunk_status
= lttng_trace_chunk_create_subdirectory(
7492 usess
->current_trace_chunk
,
7493 DEFAULT_UST_TRACE_DIR
);
7494 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7495 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7499 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7501 struct ust_app_session
*ua_sess
;
7502 struct ust_registry_session
*registry
;
7504 ua_sess
= lookup_session_by_app(usess
, app
);
7506 /* Session not associated with this app. */
7510 registry
= get_session_registry(ua_sess
);
7512 DBG("Application session is being torn down. Skip application.");
7516 fmt_ret
= asprintf(&pathname_index
,
7517 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7520 ERR("Failed to format channel index directory");
7521 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7525 * Create the index subdirectory which will take care
7526 * of implicitly creating the channel's path.
7528 chunk_status
= lttng_trace_chunk_create_subdirectory(
7529 usess
->current_trace_chunk
,
7531 free(pathname_index
);
7532 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7533 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7550 * Clear all the channels of a session.
7552 * Return LTTNG_OK on success or else an LTTng error code.
7554 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7557 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7558 struct lttng_ht_iter iter
;
7559 struct ust_app
*app
;
7560 struct ltt_ust_session
*usess
= session
->ust_session
;
7562 LTTNG_ASSERT(usess
);
7566 if (usess
->active
) {
7567 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7568 cmd_ret
= LTTNG_ERR_FATAL
;
7572 switch (usess
->buffer_type
) {
7573 case LTTNG_BUFFER_PER_UID
:
7575 struct buffer_reg_uid
*reg
;
7577 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7578 struct buffer_reg_channel
*buf_reg_chan
;
7579 struct consumer_socket
*socket
;
7581 /* Get consumer socket to use to push the metadata.*/
7582 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7585 cmd_ret
= LTTNG_ERR_INVALID
;
7589 /* Clear the data channels. */
7590 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7591 buf_reg_chan
, node
.node
) {
7592 ret
= consumer_clear_channel(socket
,
7593 buf_reg_chan
->consumer_key
);
7599 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7602 * Clear the metadata channel.
7603 * Metadata channel is not cleared per se but we still need to
7604 * perform a rotation operation on it behind the scene.
7606 ret
= consumer_clear_channel(socket
,
7607 reg
->registry
->reg
.ust
->metadata_key
);
7614 case LTTNG_BUFFER_PER_PID
:
7616 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7617 struct consumer_socket
*socket
;
7618 struct lttng_ht_iter chan_iter
;
7619 struct ust_app_channel
*ua_chan
;
7620 struct ust_app_session
*ua_sess
;
7621 struct ust_registry_session
*registry
;
7623 ua_sess
= lookup_session_by_app(usess
, app
);
7625 /* Session not associated with this app. */
7629 /* Get the right consumer socket for the application. */
7630 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7633 cmd_ret
= LTTNG_ERR_INVALID
;
7637 registry
= get_session_registry(ua_sess
);
7639 DBG("Application session is being torn down. Skip application.");
7643 /* Clear the data channels. */
7644 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7645 ua_chan
, node
.node
) {
7646 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7648 /* Per-PID buffer and application going away. */
7649 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7656 (void) push_metadata(registry
, usess
->consumer
);
7659 * Clear the metadata channel.
7660 * Metadata channel is not cleared per se but we still need to
7661 * perform rotation operation on it behind the scene.
7663 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
7665 /* Per-PID buffer and application going away. */
7666 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7684 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7685 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7688 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7698 * This function skips the metadata channel as the begin/end timestamps of a
7699 * metadata packet are useless.
7701 * Moreover, opening a packet after a "clear" will cause problems for live
7702 * sessions as it will introduce padding that was not part of the first trace
7703 * chunk. The relay daemon expects the content of the metadata stream of
7704 * successive metadata trace chunks to be strict supersets of one another.
7706 * For example, flushing a packet at the beginning of the metadata stream of
7707 * a trace chunk resulting from a "clear" session command will cause the
7708 * size of the metadata stream of the new trace chunk to not match the size of
7709 * the metadata stream of the original chunk. This will confuse the relay
7710 * daemon as the same "offset" in a metadata stream will no longer point
7711 * to the same content.
7713 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7715 enum lttng_error_code ret
= LTTNG_OK
;
7716 struct lttng_ht_iter iter
;
7717 struct ltt_ust_session
*usess
= session
->ust_session
;
7719 LTTNG_ASSERT(usess
);
7723 switch (usess
->buffer_type
) {
7724 case LTTNG_BUFFER_PER_UID
:
7726 struct buffer_reg_uid
*reg
;
7728 cds_list_for_each_entry (
7729 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7730 struct buffer_reg_channel
*buf_reg_chan
;
7731 struct consumer_socket
*socket
;
7733 socket
= consumer_find_socket_by_bitness(
7734 reg
->bits_per_long
, usess
->consumer
);
7736 ret
= LTTNG_ERR_FATAL
;
7740 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7741 &iter
.iter
, buf_reg_chan
, node
.node
) {
7742 const int open_ret
=
7743 consumer_open_channel_packets(
7745 buf_reg_chan
->consumer_key
);
7748 ret
= LTTNG_ERR_UNK
;
7755 case LTTNG_BUFFER_PER_PID
:
7757 struct ust_app
*app
;
7759 cds_lfht_for_each_entry (
7760 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7761 struct consumer_socket
*socket
;
7762 struct lttng_ht_iter chan_iter
;
7763 struct ust_app_channel
*ua_chan
;
7764 struct ust_app_session
*ua_sess
;
7765 struct ust_registry_session
*registry
;
7767 ua_sess
= lookup_session_by_app(usess
, app
);
7769 /* Session not associated with this app. */
7773 /* Get the right consumer socket for the application. */
7774 socket
= consumer_find_socket_by_bitness(
7775 app
->bits_per_long
, usess
->consumer
);
7777 ret
= LTTNG_ERR_FATAL
;
7781 registry
= get_session_registry(ua_sess
);
7783 DBG("Application session is being torn down. Skip application.");
7787 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
7788 &chan_iter
.iter
, ua_chan
, node
.node
) {
7789 const int open_ret
=
7790 consumer_open_channel_packets(
7796 * Per-PID buffer and application going
7799 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7803 ret
= LTTNG_ERR_UNK
;