2 * Copyright (C) 2011 EfficiOS Inc.
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.hpp>
25 #include <common/compat/errno.hpp>
26 #include <common/common.hpp>
27 #include <common/hashtable/utils.hpp>
28 #include <lttng/event-rule/event-rule.h>
29 #include <lttng/event-rule/event-rule-internal.hpp>
30 #include <lttng/event-rule/user-tracepoint.h>
31 #include <lttng/condition/condition.h>
32 #include <lttng/condition/event-rule-matches-internal.hpp>
33 #include <lttng/condition/event-rule-matches.h>
34 #include <lttng/trigger/trigger-internal.hpp>
35 #include <common/sessiond-comm/sessiond-comm.hpp>
37 #include "buffer-registry.hpp"
38 #include "condition-internal.hpp"
39 #include "fd-limit.hpp"
40 #include "health-sessiond.hpp"
41 #include "ust-app.hpp"
42 #include "ust-consumer.hpp"
43 #include "lttng-ust-ctl.hpp"
44 #include "lttng-ust-error.hpp"
46 #include "session.hpp"
47 #include "lttng-sessiond.hpp"
48 #include "notification-thread-commands.hpp"
51 #include "event-notifier-error-accounting.hpp"
52 #include "ust-field-utils.hpp"
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 ust_registry_session
*get_session_registry(
244 struct ust_app_session
*ua_sess
)
246 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
);
290 ASSERT_RCU_READ_LOCKED();
293 pthread_mutex_lock(&app
->sock_lock
);
294 ret
= lttng_ust_ctl_release_object(sock
, ua_ctx
->obj
);
295 pthread_mutex_unlock(&app
->sock_lock
);
297 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
298 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
299 app
->pid
, app
->sock
);
300 } else if (ret
== -EAGAIN
) {
301 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
302 app
->pid
, app
->sock
);
304 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
305 ua_ctx
->obj
->handle
, ret
,
306 app
->pid
, app
->sock
);
315 * Delete ust app event safely. RCU read lock must be held before calling
319 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
324 LTTNG_ASSERT(ua_event
);
325 ASSERT_RCU_READ_LOCKED();
327 free(ua_event
->filter
);
328 if (ua_event
->exclusion
!= NULL
)
329 free(ua_event
->exclusion
);
330 if (ua_event
->obj
!= NULL
) {
331 pthread_mutex_lock(&app
->sock_lock
);
332 ret
= lttng_ust_ctl_release_object(sock
, ua_event
->obj
);
333 pthread_mutex_unlock(&app
->sock_lock
);
335 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
336 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
337 app
->pid
, app
->sock
);
338 } else if (ret
== -EAGAIN
) {
339 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
340 app
->pid
, app
->sock
);
342 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
343 ret
, app
->pid
, app
->sock
);
352 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
353 * through a call_rcu().
356 void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
358 struct ust_app_event_notifier_rule
*obj
= caa_container_of(
359 head
, struct ust_app_event_notifier_rule
, rcu_head
);
365 * Delete ust app event notifier rule safely.
367 static void delete_ust_app_event_notifier_rule(int sock
,
368 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
,
373 LTTNG_ASSERT(ua_event_notifier_rule
);
375 if (ua_event_notifier_rule
->exclusion
!= NULL
) {
376 free(ua_event_notifier_rule
->exclusion
);
379 if (ua_event_notifier_rule
->obj
!= NULL
) {
380 pthread_mutex_lock(&app
->sock_lock
);
381 ret
= lttng_ust_ctl_release_object(sock
, ua_event_notifier_rule
->obj
);
382 pthread_mutex_unlock(&app
->sock_lock
);
384 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
385 DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d",
386 app
->pid
, app
->sock
);
387 } else if (ret
== -EAGAIN
) {
388 WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d",
389 app
->pid
, app
->sock
);
391 ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d",
392 ret
, app
->pid
, app
->sock
);
396 free(ua_event_notifier_rule
->obj
);
399 lttng_trigger_put(ua_event_notifier_rule
->trigger
);
400 call_rcu(&ua_event_notifier_rule
->rcu_head
,
401 free_ust_app_event_notifier_rule_rcu
);
405 * Release ust data object of the given stream.
407 * Return 0 on success or else a negative value.
409 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
414 LTTNG_ASSERT(stream
);
417 pthread_mutex_lock(&app
->sock_lock
);
418 ret
= lttng_ust_ctl_release_object(sock
, stream
->obj
);
419 pthread_mutex_unlock(&app
->sock_lock
);
421 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
422 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
423 app
->pid
, app
->sock
);
424 } else if (ret
== -EAGAIN
) {
425 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
426 app
->pid
, app
->sock
);
428 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
429 ret
, app
->pid
, app
->sock
);
432 lttng_fd_put(LTTNG_FD_APPS
, 2);
440 * Delete ust app stream safely. RCU read lock must be held before calling
444 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
447 LTTNG_ASSERT(stream
);
448 ASSERT_RCU_READ_LOCKED();
450 (void) release_ust_app_stream(sock
, stream
, app
);
455 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
457 struct ust_app_channel
*ua_chan
=
458 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
460 lttng_ht_destroy(ua_chan
->ctx
);
461 lttng_ht_destroy(ua_chan
->events
);
466 * Extract the lost packet or discarded events counter when the channel is
467 * being deleted and store the value in the parent channel so we can
468 * access it from lttng list and at stop/destroy.
470 * The session list lock must be held by the caller.
473 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
475 uint64_t discarded
= 0, lost
= 0;
476 struct ltt_session
*session
;
477 struct ltt_ust_channel
*uchan
;
479 if (ua_chan
->attr
.type
!= LTTNG_UST_ABI_CHAN_PER_CPU
) {
484 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
485 if (!session
|| !session
->ust_session
) {
487 * Not finding the session is not an error because there are
488 * multiple ways the channels can be torn down.
490 * 1) The session daemon can initiate the destruction of the
491 * ust app session after receiving a destroy command or
492 * during its shutdown/teardown.
493 * 2) The application, since we are in per-pid tracing, is
494 * unregistering and tearing down its ust app session.
496 * Both paths are protected by the session list lock which
497 * ensures that the accounting of lost packets and discarded
498 * events is done exactly once. The session is then unpublished
499 * from the session list, resulting in this condition.
504 if (ua_chan
->attr
.overwrite
) {
505 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
506 ua_chan
->key
, session
->ust_session
->consumer
,
509 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
510 ua_chan
->key
, session
->ust_session
->consumer
,
513 uchan
= trace_ust_find_channel_by_name(
514 session
->ust_session
->domain_global
.channels
,
517 ERR("Missing UST channel to store discarded counters");
521 uchan
->per_pid_closed_app_discarded
+= discarded
;
522 uchan
->per_pid_closed_app_lost
+= lost
;
527 session_put(session
);
532 * Delete ust app channel safely. RCU read lock must be held before calling
535 * The session list lock must be held by the caller.
538 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
542 struct lttng_ht_iter iter
;
543 struct ust_app_event
*ua_event
;
544 struct ust_app_ctx
*ua_ctx
;
545 struct ust_app_stream
*stream
, *stmp
;
546 ust_registry_session
*registry
;
548 LTTNG_ASSERT(ua_chan
);
549 ASSERT_RCU_READ_LOCKED();
551 DBG3("UST app deleting channel %s", ua_chan
->name
);
554 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
555 cds_list_del(&stream
->list
);
556 delete_ust_app_stream(sock
, stream
, app
);
560 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
561 cds_list_del(&ua_ctx
->list
);
562 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
564 delete_ust_app_ctx(sock
, ua_ctx
, app
);
568 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
570 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
572 delete_ust_app_event(sock
, ua_event
, app
);
575 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
576 /* Wipe and free registry from session registry. */
577 registry
= get_session_registry(ua_chan
->session
);
579 ust_registry_channel_del_free(registry
, ua_chan
->key
,
583 * A negative socket can be used by the caller when
584 * cleaning-up a ua_chan in an error path. Skip the
585 * accounting in this case.
588 save_per_pid_lost_discarded_counters(ua_chan
);
592 if (ua_chan
->obj
!= NULL
) {
593 /* Remove channel from application UST object descriptor. */
594 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
595 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
597 pthread_mutex_lock(&app
->sock_lock
);
598 ret
= lttng_ust_ctl_release_object(sock
, ua_chan
->obj
);
599 pthread_mutex_unlock(&app
->sock_lock
);
601 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
602 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
603 ua_chan
->name
, app
->pid
,
605 } else if (ret
== -EAGAIN
) {
606 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
607 ua_chan
->name
, app
->pid
,
610 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
611 ua_chan
->name
, ret
, app
->pid
,
615 lttng_fd_put(LTTNG_FD_APPS
, 1);
618 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
621 int ust_app_register_done(struct ust_app
*app
)
625 pthread_mutex_lock(&app
->sock_lock
);
626 ret
= lttng_ust_ctl_register_done(app
->sock
);
627 pthread_mutex_unlock(&app
->sock_lock
);
631 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*data
)
636 pthread_mutex_lock(&app
->sock_lock
);
641 ret
= lttng_ust_ctl_release_object(sock
, data
);
643 pthread_mutex_unlock(&app
->sock_lock
);
649 * Push metadata to consumer socket.
651 * RCU read-side lock must be held to guarantee existence of socket.
652 * Must be called with the ust app session lock held.
653 * Must be called with the registry lock held.
655 * On success, return the len of metadata pushed or else a negative value.
656 * Returning a -EPIPE return value means we could not send the metadata,
657 * but it can be caused by recoverable errors (e.g. the application has
658 * terminated concurrently).
660 ssize_t
ust_app_push_metadata(ust_registry_session
*registry
,
661 struct consumer_socket
*socket
, int send_zero_data
)
664 char *metadata_str
= NULL
;
665 size_t len
, offset
, new_metadata_len_sent
;
667 uint64_t metadata_key
, metadata_version
;
669 LTTNG_ASSERT(registry
);
670 LTTNG_ASSERT(socket
);
671 ASSERT_RCU_READ_LOCKED();
673 metadata_key
= registry
->_metadata_key
;
676 * Means that no metadata was assigned to the session. This can
677 * happens if no start has been done previously.
683 offset
= registry
->_metadata_len_sent
;
684 len
= registry
->_metadata_len
- registry
->_metadata_len_sent
;
685 new_metadata_len_sent
= registry
->_metadata_len
;
686 metadata_version
= registry
->_metadata_version
;
688 DBG3("No metadata to push for metadata key %" PRIu64
,
689 registry
->_metadata_key
);
691 if (send_zero_data
) {
692 DBG("No metadata to push");
698 /* Allocate only what we have to send. */
699 metadata_str
= calloc
<char>(len
);
701 PERROR("zmalloc ust app metadata string");
705 /* Copy what we haven't sent out. */
706 memcpy(metadata_str
, registry
->_metadata
+ offset
, len
);
709 pthread_mutex_unlock(®istry
->_lock
);
711 * We need to unlock the registry while we push metadata to
712 * break a circular dependency between the consumerd metadata
713 * lock and the sessiond registry lock. Indeed, pushing metadata
714 * to the consumerd awaits that it gets pushed all the way to
715 * relayd, but doing so requires grabbing the metadata lock. If
716 * a concurrent metadata request is being performed by
717 * consumerd, this can try to grab the registry lock on the
718 * sessiond while holding the metadata lock on the consumer
719 * daemon. Those push and pull schemes are performed on two
720 * different bidirectionnal communication sockets.
722 ret
= consumer_push_metadata(socket
, metadata_key
,
723 metadata_str
, len
, offset
, metadata_version
);
724 pthread_mutex_lock(®istry
->_lock
);
727 * There is an acceptable race here between the registry
728 * metadata key assignment and the creation on the
729 * consumer. The session daemon can concurrently push
730 * metadata for this registry while being created on the
731 * consumer since the metadata key of the registry is
732 * assigned *before* it is setup to avoid the consumer
733 * to ask for metadata that could possibly be not found
734 * in the session daemon.
736 * The metadata will get pushed either by the session
737 * being stopped or the consumer requesting metadata if
738 * that race is triggered.
740 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
743 ERR("Error pushing metadata to consumer");
749 * Metadata may have been concurrently pushed, since
750 * we're not holding the registry lock while pushing to
751 * consumer. This is handled by the fact that we send
752 * the metadata content, size, and the offset at which
753 * that metadata belongs. This may arrive out of order
754 * on the consumer side, and the consumer is able to
755 * deal with overlapping fragments. The consumer
756 * supports overlapping fragments, which must be
757 * contiguous starting from offset 0. We keep the
758 * largest metadata_len_sent value of the concurrent
761 registry
->_metadata_len_sent
=
762 std::max(registry
->_metadata_len_sent
,
763 new_metadata_len_sent
);
772 * On error, flag the registry that the metadata is
773 * closed. We were unable to push anything and this
774 * means that either the consumer is not responding or
775 * the metadata cache has been destroyed on the
778 registry
->_metadata_closed
= true;
786 * For a given application and session, push metadata to consumer.
787 * Either sock or consumer is required : if sock is NULL, the default
788 * socket to send the metadata is retrieved from consumer, if sock
789 * is not NULL we use it to send the metadata.
790 * RCU read-side lock must be held while calling this function,
791 * therefore ensuring existence of registry. It also ensures existence
792 * of socket throughout this function.
794 * Return 0 on success else a negative error.
795 * Returning a -EPIPE return value means we could not send the metadata,
796 * but it can be caused by recoverable errors (e.g. the application has
797 * terminated concurrently).
799 static int push_metadata(ust_registry_session
*registry
,
800 struct consumer_output
*consumer
)
804 struct consumer_socket
*socket
;
806 LTTNG_ASSERT(registry
);
807 LTTNG_ASSERT(consumer
);
808 ASSERT_RCU_READ_LOCKED();
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(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
= true;
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 void delete_ust_app_session_rcu(struct rcu_head
*head
)
901 struct ust_app_session
*ua_sess
=
902 caa_container_of(head
, struct ust_app_session
, rcu_head
);
904 lttng_ht_destroy(ua_sess
->channels
);
909 * Delete ust app session safely. RCU read lock must be held before calling
912 * The session list lock must be held by the caller.
915 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
919 struct lttng_ht_iter iter
;
920 struct ust_app_channel
*ua_chan
;
921 ust_registry_session
*registry
;
923 LTTNG_ASSERT(ua_sess
);
924 ASSERT_RCU_READ_LOCKED();
926 pthread_mutex_lock(&ua_sess
->lock
);
928 LTTNG_ASSERT(!ua_sess
->deleted
);
929 ua_sess
->deleted
= true;
931 registry
= get_session_registry(ua_sess
);
932 /* Registry can be null on error path during initialization. */
934 /* Push metadata for application before freeing the application. */
935 (void) push_metadata(registry
, ua_sess
->consumer
);
938 * Don't ask to close metadata for global per UID buffers. Close
939 * metadata only on destroy trace session in this case. Also, the
940 * previous push metadata could have flag the metadata registry to
941 * close so don't send a close command if closed.
943 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
944 /* And ask to close it for this session registry. */
945 (void) close_metadata(registry
, ua_sess
->consumer
);
949 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
951 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
953 delete_ust_app_channel(sock
, ua_chan
, app
);
956 /* In case of per PID, the registry is kept in the session. */
957 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
958 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
961 * Registry can be null on error path during
964 buffer_reg_pid_remove(reg_pid
);
965 buffer_reg_pid_destroy(reg_pid
);
969 if (ua_sess
->handle
!= -1) {
970 pthread_mutex_lock(&app
->sock_lock
);
971 ret
= lttng_ust_ctl_release_handle(sock
, ua_sess
->handle
);
972 pthread_mutex_unlock(&app
->sock_lock
);
974 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
975 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
976 app
->pid
, app
->sock
);
977 } else if (ret
== -EAGAIN
) {
978 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
979 app
->pid
, app
->sock
);
981 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
982 ret
, app
->pid
, app
->sock
);
986 /* Remove session from application UST object descriptor. */
987 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
988 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
992 pthread_mutex_unlock(&ua_sess
->lock
);
994 consumer_output_put(ua_sess
->consumer
);
996 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
1000 * Delete a traceable application structure from the global list. Never call
1001 * this function outside of a call_rcu call.
1004 void delete_ust_app(struct ust_app
*app
)
1007 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
1008 struct lttng_ht_iter iter
;
1009 struct ust_app_event_notifier_rule
*event_notifier_rule
;
1010 bool event_notifier_write_fd_is_open
;
1013 * The session list lock must be held during this function to guarantee
1014 * the existence of ua_sess.
1016 session_lock_list();
1017 /* Delete ust app sessions info */
1022 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
1024 /* Free every object in the session and the session. */
1026 delete_ust_app_session(sock
, ua_sess
, app
);
1030 /* Remove the event notifier rules associated with this app. */
1032 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
1033 &iter
.iter
, event_notifier_rule
, node
.node
) {
1034 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
1037 delete_ust_app_event_notifier_rule(
1038 app
->sock
, event_notifier_rule
, app
);
1043 lttng_ht_destroy(app
->sessions
);
1044 lttng_ht_destroy(app
->ust_sessions_objd
);
1045 lttng_ht_destroy(app
->ust_objd
);
1046 lttng_ht_destroy(app
->token_to_event_notifier_rule_ht
);
1049 * This could be NULL if the event notifier setup failed (e.g the app
1050 * was killed or the tracer does not support this feature).
1052 if (app
->event_notifier_group
.object
) {
1053 enum lttng_error_code ret_code
;
1054 enum event_notifier_error_accounting_status status
;
1056 const int event_notifier_read_fd
= lttng_pipe_get_readfd(
1057 app
->event_notifier_group
.event_pipe
);
1059 ret_code
= notification_thread_command_remove_tracer_event_source(
1060 the_notification_thread_handle
,
1061 event_notifier_read_fd
);
1062 if (ret_code
!= LTTNG_OK
) {
1063 ERR("Failed to remove application tracer event source from notification thread");
1066 status
= event_notifier_error_accounting_unregister_app(app
);
1067 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1068 ERR("Error unregistering app from event notifier error accounting");
1071 lttng_ust_ctl_release_object(sock
, app
->event_notifier_group
.object
);
1072 free(app
->event_notifier_group
.object
);
1075 event_notifier_write_fd_is_open
= lttng_pipe_is_write_open(
1076 app
->event_notifier_group
.event_pipe
);
1077 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1079 * Release the file descriptors reserved for the event notifier pipe.
1080 * The app could be destroyed before the write end of the pipe could be
1081 * passed to the application (and closed). In that case, both file
1082 * descriptors must be released.
1084 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1087 * Wait until we have deleted the application from the sock hash table
1088 * before closing this socket, otherwise an application could re-use the
1089 * socket ID and race with the teardown, using the same hash table entry.
1091 * It's OK to leave the close in call_rcu. We want it to stay unique for
1092 * all RCU readers that could run concurrently with unregister app,
1093 * therefore we _need_ to only close that socket after a grace period. So
1094 * it should stay in this RCU callback.
1096 * This close() is a very important step of the synchronization model so
1097 * every modification to this function must be carefully reviewed.
1103 lttng_fd_put(LTTNG_FD_APPS
, 1);
1105 DBG2("UST app pid %d deleted", app
->pid
);
1107 session_unlock_list();
1111 * URCU intermediate call to delete an UST app.
1114 void delete_ust_app_rcu(struct rcu_head
*head
)
1116 struct lttng_ht_node_ulong
*node
=
1117 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1118 struct ust_app
*app
=
1119 caa_container_of(node
, struct ust_app
, pid_n
);
1121 DBG3("Call RCU deleting app PID %d", app
->pid
);
1122 delete_ust_app(app
);
1126 * Delete the session from the application ht and delete the data structure by
1127 * freeing every object inside and releasing them.
1129 * The session list lock must be held by the caller.
1131 static void destroy_app_session(struct ust_app
*app
,
1132 struct ust_app_session
*ua_sess
)
1135 struct lttng_ht_iter iter
;
1138 LTTNG_ASSERT(ua_sess
);
1140 iter
.iter
.node
= &ua_sess
->node
.node
;
1141 ret
= lttng_ht_del(app
->sessions
, &iter
);
1143 /* Already scheduled for teardown. */
1147 /* Once deleted, free the data structure. */
1148 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1155 * Alloc new UST app session.
1158 struct ust_app_session
*alloc_ust_app_session(void)
1160 struct ust_app_session
*ua_sess
;
1162 /* Init most of the default value by allocating and zeroing */
1163 ua_sess
= zmalloc
<ust_app_session
>();
1164 if (ua_sess
== NULL
) {
1169 ua_sess
->handle
= -1;
1170 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1171 ua_sess
->metadata_attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1172 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1181 * Alloc new UST app channel.
1184 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1185 struct ust_app_session
*ua_sess
,
1186 struct lttng_ust_abi_channel_attr
*attr
)
1188 struct ust_app_channel
*ua_chan
;
1190 /* Init most of the default value by allocating and zeroing */
1191 ua_chan
= zmalloc
<ust_app_channel
>();
1192 if (ua_chan
== NULL
) {
1197 /* Setup channel name */
1198 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1199 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1201 ua_chan
->enabled
= 1;
1202 ua_chan
->handle
= -1;
1203 ua_chan
->session
= ua_sess
;
1204 ua_chan
->key
= get_next_channel_key();
1205 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1206 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1207 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1209 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1210 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1212 /* Copy attributes */
1214 /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */
1215 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1216 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1217 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1218 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1219 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1220 ua_chan
->attr
.output
= (lttng_ust_abi_output
) attr
->output
;
1221 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1223 /* By default, the channel is a per cpu channel. */
1224 ua_chan
->attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1226 DBG3("UST app channel %s allocated", ua_chan
->name
);
1235 * Allocate and initialize a UST app stream.
1237 * Return newly allocated stream pointer or NULL on error.
1239 struct ust_app_stream
*ust_app_alloc_stream(void)
1241 struct ust_app_stream
*stream
= NULL
;
1243 stream
= zmalloc
<ust_app_stream
>();
1244 if (stream
== NULL
) {
1245 PERROR("zmalloc ust app stream");
1249 /* Zero could be a valid value for a handle so flag it to -1. */
1250 stream
->handle
= -1;
1257 * Alloc new UST app event.
1260 struct ust_app_event
*alloc_ust_app_event(char *name
,
1261 struct lttng_ust_abi_event
*attr
)
1263 struct ust_app_event
*ua_event
;
1265 /* Init most of the default value by allocating and zeroing */
1266 ua_event
= zmalloc
<ust_app_event
>();
1267 if (ua_event
== NULL
) {
1268 PERROR("Failed to allocate ust_app_event structure");
1272 ua_event
->enabled
= 1;
1273 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1274 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1275 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1277 /* Copy attributes */
1279 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1282 DBG3("UST app event %s allocated", ua_event
->name
);
1291 * Allocate a new UST app event notifier rule.
1293 static struct ust_app_event_notifier_rule
*alloc_ust_app_event_notifier_rule(
1294 struct lttng_trigger
*trigger
)
1296 enum lttng_event_rule_generate_exclusions_status
1297 generate_exclusion_status
;
1298 enum lttng_condition_status cond_status
;
1299 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1300 struct lttng_condition
*condition
= NULL
;
1301 const struct lttng_event_rule
*event_rule
= NULL
;
1303 ua_event_notifier_rule
= zmalloc
<ust_app_event_notifier_rule
>();
1304 if (ua_event_notifier_rule
== NULL
) {
1305 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1309 ua_event_notifier_rule
->enabled
= 1;
1310 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1311 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
,
1312 ua_event_notifier_rule
->token
);
1314 condition
= lttng_trigger_get_condition(trigger
);
1315 LTTNG_ASSERT(condition
);
1316 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1317 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1319 cond_status
= lttng_condition_event_rule_matches_get_rule(
1320 condition
, &event_rule
);
1321 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
1322 LTTNG_ASSERT(event_rule
);
1324 ua_event_notifier_rule
->error_counter_index
=
1325 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
1326 /* Acquire the event notifier's reference to the trigger. */
1327 lttng_trigger_get(trigger
);
1329 ua_event_notifier_rule
->trigger
= trigger
;
1330 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1331 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1332 event_rule
, &ua_event_notifier_rule
->exclusion
);
1333 switch (generate_exclusion_status
) {
1334 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1335 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1338 /* Error occurred. */
1339 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1340 goto error_put_trigger
;
1343 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1344 ua_event_notifier_rule
->token
);
1346 return ua_event_notifier_rule
;
1349 lttng_trigger_put(trigger
);
1351 free(ua_event_notifier_rule
);
1356 * Alloc new UST app context.
1359 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1361 struct ust_app_ctx
*ua_ctx
;
1363 ua_ctx
= zmalloc
<ust_app_ctx
>();
1364 if (ua_ctx
== NULL
) {
1368 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1371 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1372 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1373 char *provider_name
= NULL
, *ctx_name
= NULL
;
1375 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1376 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1377 if (!provider_name
|| !ctx_name
) {
1378 free(provider_name
);
1383 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1384 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1388 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1396 * Create a liblttng-ust filter bytecode from given bytecode.
1398 * Return allocated filter or NULL on error.
1400 static struct lttng_ust_abi_filter_bytecode
*create_ust_filter_bytecode_from_bytecode(
1401 const struct lttng_bytecode
*orig_f
)
1403 struct lttng_ust_abi_filter_bytecode
*filter
= NULL
;
1405 /* Copy filter bytecode. */
1406 filter
= zmalloc
<lttng_ust_abi_filter_bytecode
>(sizeof(*filter
) + orig_f
->len
);
1408 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1412 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1413 sizeof(struct lttng_ust_abi_filter_bytecode
));
1414 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1420 * Create a liblttng-ust capture bytecode from given bytecode.
1422 * Return allocated filter or NULL on error.
1424 static struct lttng_ust_abi_capture_bytecode
*
1425 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1427 struct lttng_ust_abi_capture_bytecode
*capture
= NULL
;
1429 /* Copy capture bytecode. */
1430 capture
= zmalloc
<lttng_ust_abi_capture_bytecode
>(sizeof(*capture
) + orig_f
->len
);
1432 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1436 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1437 sizeof(struct lttng_ust_abi_capture_bytecode
));
1438 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1444 * Find an ust_app using the sock and return it. RCU read side lock must be
1445 * held before calling this helper function.
1447 struct ust_app
*ust_app_find_by_sock(int sock
)
1449 struct lttng_ht_node_ulong
*node
;
1450 struct lttng_ht_iter iter
;
1452 ASSERT_RCU_READ_LOCKED();
1454 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1455 node
= lttng_ht_iter_get_node_ulong(&iter
);
1457 DBG2("UST app find by sock %d not found", sock
);
1461 return caa_container_of(node
, struct ust_app
, sock_n
);
1468 * Find an ust_app using the notify sock and return it. RCU read side lock must
1469 * be held before calling this helper function.
1471 static struct ust_app
*find_app_by_notify_sock(int sock
)
1473 struct lttng_ht_node_ulong
*node
;
1474 struct lttng_ht_iter iter
;
1476 ASSERT_RCU_READ_LOCKED();
1478 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1480 node
= lttng_ht_iter_get_node_ulong(&iter
);
1482 DBG2("UST app find by notify sock %d not found", sock
);
1486 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1493 * Lookup for an ust app event based on event name, filter bytecode and the
1496 * Return an ust_app_event object or NULL on error.
1498 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1499 const char *name
, const struct lttng_bytecode
*filter
,
1501 const struct lttng_event_exclusion
*exclusion
)
1503 struct lttng_ht_iter iter
;
1504 struct lttng_ht_node_str
*node
;
1505 struct ust_app_event
*event
= NULL
;
1506 struct ust_app_ht_key key
;
1511 /* Setup key for event lookup. */
1513 key
.filter
= filter
;
1514 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) loglevel_value
;
1515 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1516 key
.exclusion
= exclusion
;
1518 /* Lookup using the event name as hash and a custom match fct. */
1519 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1520 ht_match_ust_app_event
, &key
, &iter
.iter
);
1521 node
= lttng_ht_iter_get_node_str(&iter
);
1526 event
= caa_container_of(node
, struct ust_app_event
, node
);
1533 * Look-up an event notifier rule based on its token id.
1535 * Must be called with the RCU read lock held.
1536 * Return an ust_app_event_notifier_rule object or NULL on error.
1538 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(
1539 struct lttng_ht
*ht
, uint64_t token
)
1541 struct lttng_ht_iter iter
;
1542 struct lttng_ht_node_u64
*node
;
1543 struct ust_app_event_notifier_rule
*event_notifier_rule
= NULL
;
1546 ASSERT_RCU_READ_LOCKED();
1548 lttng_ht_lookup(ht
, &token
, &iter
);
1549 node
= lttng_ht_iter_get_node_u64(&iter
);
1551 DBG2("UST app event notifier rule token not found: token = %" PRIu64
,
1556 event_notifier_rule
= caa_container_of(
1557 node
, struct ust_app_event_notifier_rule
, node
);
1559 return event_notifier_rule
;
1563 * Create the channel context on the tracer.
1565 * Called with UST app session lock held.
1568 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1569 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1573 health_code_update();
1575 pthread_mutex_lock(&app
->sock_lock
);
1576 ret
= lttng_ust_ctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1577 ua_chan
->obj
, &ua_ctx
->obj
);
1578 pthread_mutex_unlock(&app
->sock_lock
);
1580 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1582 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1583 app
->pid
, app
->sock
);
1584 } else if (ret
== -EAGAIN
) {
1586 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1587 app
->pid
, app
->sock
);
1589 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1590 ret
, app
->pid
, app
->sock
);
1595 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1597 DBG2("UST app context handle %d created successfully for channel %s",
1598 ua_ctx
->handle
, ua_chan
->name
);
1601 health_code_update();
1606 * Set the filter on the tracer.
1608 static int set_ust_object_filter(struct ust_app
*app
,
1609 const struct lttng_bytecode
*bytecode
,
1610 struct lttng_ust_abi_object_data
*ust_object
)
1613 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= NULL
;
1615 health_code_update();
1617 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1618 if (!ust_bytecode
) {
1619 ret
= -LTTNG_ERR_NOMEM
;
1622 pthread_mutex_lock(&app
->sock_lock
);
1623 ret
= lttng_ust_ctl_set_filter(app
->sock
, ust_bytecode
,
1625 pthread_mutex_unlock(&app
->sock_lock
);
1627 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1629 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1630 app
->pid
, app
->sock
);
1631 } else if (ret
== -EAGAIN
) {
1633 WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d",
1634 app
->pid
, app
->sock
);
1636 ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p",
1637 ret
, app
->pid
, app
->sock
, ust_object
);
1642 DBG2("UST filter successfully set: object = %p", ust_object
);
1645 health_code_update();
1651 * Set a capture bytecode for the passed object.
1652 * The sequence number enforces the ordering at runtime and on reception of
1653 * the captured payloads.
1655 static int set_ust_capture(struct ust_app
*app
,
1656 const struct lttng_bytecode
*bytecode
,
1657 unsigned int capture_seqnum
,
1658 struct lttng_ust_abi_object_data
*ust_object
)
1661 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= NULL
;
1663 health_code_update();
1665 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1666 if (!ust_bytecode
) {
1667 ret
= -LTTNG_ERR_NOMEM
;
1672 * Set the sequence number to ensure the capture of fields is ordered.
1674 ust_bytecode
->seqnum
= capture_seqnum
;
1676 pthread_mutex_lock(&app
->sock_lock
);
1677 ret
= lttng_ust_ctl_set_capture(app
->sock
, ust_bytecode
,
1679 pthread_mutex_unlock(&app
->sock_lock
);
1681 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1683 DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d",
1684 app
->pid
, app
->sock
);
1685 } else if (ret
== -EAGAIN
) {
1687 DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d",
1688 app
->pid
, app
->sock
);
1690 ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d",
1698 DBG2("UST capture successfully set: object = %p", ust_object
);
1701 health_code_update();
1707 struct lttng_ust_abi_event_exclusion
*create_ust_exclusion_from_exclusion(
1708 const struct lttng_event_exclusion
*exclusion
)
1710 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= NULL
;
1711 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1712 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1714 ust_exclusion
= zmalloc
<lttng_ust_abi_event_exclusion
>(exclusion_alloc_size
);
1715 if (!ust_exclusion
) {
1720 LTTNG_ASSERT(sizeof(struct lttng_event_exclusion
) ==
1721 sizeof(struct lttng_ust_abi_event_exclusion
));
1722 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1724 return ust_exclusion
;
1728 * Set event exclusions on the tracer.
1730 static int set_ust_object_exclusions(struct ust_app
*app
,
1731 const struct lttng_event_exclusion
*exclusions
,
1732 struct lttng_ust_abi_object_data
*ust_object
)
1735 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= NULL
;
1737 LTTNG_ASSERT(exclusions
&& exclusions
->count
> 0);
1739 health_code_update();
1741 ust_exclusions
= create_ust_exclusion_from_exclusion(
1743 if (!ust_exclusions
) {
1744 ret
= -LTTNG_ERR_NOMEM
;
1747 pthread_mutex_lock(&app
->sock_lock
);
1748 ret
= lttng_ust_ctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1749 pthread_mutex_unlock(&app
->sock_lock
);
1751 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1753 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1754 app
->pid
, app
->sock
);
1755 } else if (ret
== -EAGAIN
) {
1757 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1758 app
->pid
, app
->sock
);
1760 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p",
1761 ret
, app
->pid
, app
->sock
, ust_object
);
1766 DBG2("UST exclusions set successfully for object %p", ust_object
);
1769 health_code_update();
1770 free(ust_exclusions
);
1775 * Disable the specified event on to UST tracer for the UST session.
1777 static int disable_ust_object(struct ust_app
*app
,
1778 struct lttng_ust_abi_object_data
*object
)
1782 health_code_update();
1784 pthread_mutex_lock(&app
->sock_lock
);
1785 ret
= lttng_ust_ctl_disable(app
->sock
, object
);
1786 pthread_mutex_unlock(&app
->sock_lock
);
1788 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1790 DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d",
1791 app
->pid
, app
->sock
);
1792 } else if (ret
== -EAGAIN
) {
1794 WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d",
1795 app
->pid
, app
->sock
);
1797 ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p",
1798 ret
, app
->pid
, app
->sock
, object
);
1803 DBG2("UST app object %p disabled successfully for app: pid = %d",
1807 health_code_update();
1812 * Disable the specified channel on to UST tracer for the UST session.
1814 static int disable_ust_channel(struct ust_app
*app
,
1815 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1819 health_code_update();
1821 pthread_mutex_lock(&app
->sock_lock
);
1822 ret
= lttng_ust_ctl_disable(app
->sock
, ua_chan
->obj
);
1823 pthread_mutex_unlock(&app
->sock_lock
);
1825 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1827 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1828 app
->pid
, app
->sock
);
1829 } else if (ret
== -EAGAIN
) {
1831 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1832 app
->pid
, app
->sock
);
1834 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1835 ua_chan
->name
, ua_sess
->handle
, ret
,
1836 app
->pid
, app
->sock
);
1841 DBG2("UST app channel %s disabled successfully for app: pid = %d",
1842 ua_chan
->name
, app
->pid
);
1845 health_code_update();
1850 * Enable the specified channel on to UST tracer for the UST session.
1852 static int enable_ust_channel(struct ust_app
*app
,
1853 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1857 health_code_update();
1859 pthread_mutex_lock(&app
->sock_lock
);
1860 ret
= lttng_ust_ctl_enable(app
->sock
, ua_chan
->obj
);
1861 pthread_mutex_unlock(&app
->sock_lock
);
1863 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1865 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1866 ua_chan
->name
, app
->pid
, app
->sock
);
1867 } else if (ret
== -EAGAIN
) {
1869 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1870 ua_chan
->name
, app
->pid
, app
->sock
);
1872 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1873 ua_chan
->name
, ua_sess
->handle
, ret
,
1874 app
->pid
, app
->sock
);
1879 ua_chan
->enabled
= 1;
1881 DBG2("UST app channel %s enabled successfully for app: pid = %d",
1882 ua_chan
->name
, app
->pid
);
1885 health_code_update();
1890 * Enable the specified event on to UST tracer for the UST session.
1892 static int enable_ust_object(
1893 struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1897 health_code_update();
1899 pthread_mutex_lock(&app
->sock_lock
);
1900 ret
= lttng_ust_ctl_enable(app
->sock
, ust_object
);
1901 pthread_mutex_unlock(&app
->sock_lock
);
1903 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1905 DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d",
1906 app
->pid
, app
->sock
);
1907 } else if (ret
== -EAGAIN
) {
1909 WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d",
1910 app
->pid
, app
->sock
);
1912 ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p",
1913 ret
, app
->pid
, app
->sock
, ust_object
);
1918 DBG2("UST app object %p enabled successfully for app: pid = %d",
1919 ust_object
, app
->pid
);
1922 health_code_update();
1927 * Send channel and stream buffer to application.
1929 * Return 0 on success. On error, a negative value is returned.
1931 static int send_channel_pid_to_ust(struct ust_app
*app
,
1932 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1935 struct ust_app_stream
*stream
, *stmp
;
1938 LTTNG_ASSERT(ua_sess
);
1939 LTTNG_ASSERT(ua_chan
);
1941 health_code_update();
1943 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1946 /* Send channel to the application. */
1947 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1948 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1949 ret
= -ENOTCONN
; /* Caused by app exiting. */
1951 } else if (ret
== -EAGAIN
) {
1952 /* Caused by timeout. */
1953 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
"\".",
1954 app
->pid
, ua_chan
->name
, ua_sess
->tracing_id
);
1955 /* Treat this the same way as an application that is exiting. */
1958 } else if (ret
< 0) {
1962 health_code_update();
1964 /* Send all streams to application. */
1965 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1966 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1967 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1968 ret
= -ENOTCONN
; /* Caused by app exiting. */
1970 } else if (ret
== -EAGAIN
) {
1971 /* Caused by timeout. */
1972 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
"\".",
1973 app
->pid
, stream
->name
, ua_chan
->name
,
1974 ua_sess
->tracing_id
);
1976 * Treat this the same way as an application that is
1980 } else if (ret
< 0) {
1983 /* We don't need the stream anymore once sent to the tracer. */
1984 cds_list_del(&stream
->list
);
1985 delete_ust_app_stream(-1, stream
, app
);
1989 health_code_update();
1994 * Create the specified event onto the UST tracer for a UST session.
1996 * Should be called with session mutex held.
1999 int create_ust_event(struct ust_app
*app
,
2000 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
2004 health_code_update();
2006 /* Create UST event on tracer */
2007 pthread_mutex_lock(&app
->sock_lock
);
2008 ret
= lttng_ust_ctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
2010 pthread_mutex_unlock(&app
->sock_lock
);
2012 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2014 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
2015 app
->pid
, app
->sock
);
2016 } else if (ret
== -EAGAIN
) {
2018 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
2019 app
->pid
, app
->sock
);
2021 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
2022 ua_event
->attr
.name
, ret
, app
->pid
,
2028 ua_event
->handle
= ua_event
->obj
->handle
;
2030 DBG2("UST app event %s created successfully for pid:%d object = %p",
2031 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
2033 health_code_update();
2035 /* Set filter if one is present. */
2036 if (ua_event
->filter
) {
2037 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
2043 /* Set exclusions for the event */
2044 if (ua_event
->exclusion
) {
2045 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
2051 /* If event not enabled, disable it on the tracer */
2052 if (ua_event
->enabled
) {
2054 * We now need to explicitly enable the event, since it
2055 * is now disabled at creation.
2057 ret
= enable_ust_object(app
, ua_event
->obj
);
2060 * If we hit an EPERM, something is wrong with our enable call. If
2061 * we get an EEXIST, there is a problem on the tracer side since we
2065 case -LTTNG_UST_ERR_PERM
:
2066 /* Code flow problem */
2068 case -LTTNG_UST_ERR_EXIST
:
2069 /* It's OK for our use case. */
2080 health_code_update();
2084 static int init_ust_event_notifier_from_event_rule(
2085 const struct lttng_event_rule
*rule
,
2086 struct lttng_ust_abi_event_notifier
*event_notifier
)
2088 enum lttng_event_rule_status status
;
2089 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2090 int loglevel
= -1, ret
= 0;
2091 const char *pattern
;
2094 memset(event_notifier
, 0, sizeof(*event_notifier
));
2096 if (lttng_event_rule_targets_agent_domain(rule
)) {
2098 * Special event for agents
2099 * The actual meat of the event is in the filter that will be
2100 * attached later on.
2101 * Set the default values for the agent event.
2103 pattern
= event_get_default_agent_ust_name(
2104 lttng_event_rule_get_domain_type(rule
));
2106 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2108 const struct lttng_log_level_rule
*log_level_rule
;
2110 LTTNG_ASSERT(lttng_event_rule_get_type(rule
) ==
2111 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
);
2113 status
= lttng_event_rule_user_tracepoint_get_name_pattern(rule
, &pattern
);
2114 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2115 /* At this point, this is a fatal error. */
2119 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(
2120 rule
, &log_level_rule
);
2121 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2122 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2123 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2124 enum lttng_log_level_rule_status llr_status
;
2126 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2127 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2128 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2129 llr_status
= lttng_log_level_rule_exactly_get_level(
2130 log_level_rule
, &loglevel
);
2132 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2133 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2134 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2135 log_level_rule
, &loglevel
);
2141 LTTNG_ASSERT(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2143 /* At this point this is a fatal error. */
2148 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2149 ret
= lttng_strncpy(event_notifier
->event
.name
, pattern
,
2150 sizeof(event_notifier
->event
.name
));
2152 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
2157 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2158 event_notifier
->event
.loglevel
= loglevel
;
2164 * Create the specified event notifier against the user space tracer of a
2165 * given application.
2167 static int create_ust_event_notifier(struct ust_app
*app
,
2168 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2171 enum lttng_condition_status condition_status
;
2172 const struct lttng_condition
*condition
= NULL
;
2173 struct lttng_ust_abi_event_notifier event_notifier
;
2174 const struct lttng_event_rule
*event_rule
= NULL
;
2175 unsigned int capture_bytecode_count
= 0, i
;
2176 enum lttng_condition_status cond_status
;
2177 enum lttng_event_rule_type event_rule_type
;
2179 health_code_update();
2180 LTTNG_ASSERT(app
->event_notifier_group
.object
);
2182 condition
= lttng_trigger_get_const_condition(
2183 ua_event_notifier_rule
->trigger
);
2184 LTTNG_ASSERT(condition
);
2185 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
2186 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2188 condition_status
= lttng_condition_event_rule_matches_get_rule(
2189 condition
, &event_rule
);
2190 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2192 LTTNG_ASSERT(event_rule
);
2194 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2195 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
||
2196 event_rule_type
== LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
||
2198 LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
||
2200 LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
);
2202 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2203 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2204 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2206 /* Create UST event notifier against the tracer. */
2207 pthread_mutex_lock(&app
->sock_lock
);
2208 ret
= lttng_ust_ctl_create_event_notifier(app
->sock
, &event_notifier
,
2209 app
->event_notifier_group
.object
,
2210 &ua_event_notifier_rule
->obj
);
2211 pthread_mutex_unlock(&app
->sock_lock
);
2213 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2215 DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d",
2216 app
->pid
, app
->sock
);
2217 } else if (ret
== -EAGAIN
) {
2219 WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d",
2220 app
->pid
, app
->sock
);
2222 ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d",
2223 event_notifier
.event
.name
, ret
, app
->pid
,
2229 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2231 DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p",
2232 event_notifier
.event
.name
, app
->name
, app
->pid
,
2233 ua_event_notifier_rule
->obj
);
2235 health_code_update();
2237 /* Set filter if one is present. */
2238 if (ua_event_notifier_rule
->filter
) {
2239 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2240 ua_event_notifier_rule
->obj
);
2246 /* Set exclusions for the event. */
2247 if (ua_event_notifier_rule
->exclusion
) {
2248 ret
= set_ust_object_exclusions(app
,
2249 ua_event_notifier_rule
->exclusion
,
2250 ua_event_notifier_rule
->obj
);
2256 /* Set the capture bytecodes. */
2257 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2258 condition
, &capture_bytecode_count
);
2259 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2261 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2262 const struct lttng_bytecode
*capture_bytecode
=
2263 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
2266 ret
= set_ust_capture(app
, capture_bytecode
, i
,
2267 ua_event_notifier_rule
->obj
);
2274 * We now need to explicitly enable the event, since it
2275 * is disabled at creation.
2277 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2280 * If we hit an EPERM, something is wrong with our enable call.
2281 * If we get an EEXIST, there is a problem on the tracer side
2282 * since we just created it.
2285 case -LTTNG_UST_ERR_PERM
:
2286 /* Code flow problem. */
2288 case -LTTNG_UST_ERR_EXIST
:
2289 /* It's OK for our use case. */
2299 ua_event_notifier_rule
->enabled
= true;
2302 health_code_update();
2307 * Copy data between an UST app event and a LTT event.
2309 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2310 struct ltt_ust_event
*uevent
)
2312 size_t exclusion_alloc_size
;
2314 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2315 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2317 ua_event
->enabled
= uevent
->enabled
;
2319 /* Copy event attributes */
2320 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2322 /* Copy filter bytecode */
2323 if (uevent
->filter
) {
2324 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2325 /* Filter might be NULL here in case of ENONEM. */
2328 /* Copy exclusion data */
2329 if (uevent
->exclusion
) {
2330 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2331 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2332 ua_event
->exclusion
= zmalloc
<lttng_event_exclusion
>(exclusion_alloc_size
);
2333 if (ua_event
->exclusion
== NULL
) {
2336 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2337 exclusion_alloc_size
);
2343 * Copy data between an UST app channel and a LTT channel.
2345 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2346 struct ltt_ust_channel
*uchan
)
2348 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2350 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2351 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2353 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2354 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2356 /* Copy event attributes since the layout is different. */
2357 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2358 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2359 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2360 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2361 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2362 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2363 ua_chan
->attr
.output
= (lttng_ust_abi_output
) uchan
->attr
.output
;
2364 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2367 * Note that the attribute channel type is not set since the channel on the
2368 * tracing registry side does not have this information.
2371 ua_chan
->enabled
= uchan
->enabled
;
2372 ua_chan
->tracing_channel_id
= uchan
->id
;
2374 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2378 * Copy data between a UST app session and a regular LTT session.
2380 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2381 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2383 struct tm
*timeinfo
;
2386 char tmp_shm_path
[PATH_MAX
];
2388 timeinfo
= localtime(&app
->registration_time
);
2389 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2391 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2393 ua_sess
->tracing_id
= usess
->id
;
2394 ua_sess
->id
= get_next_session_id();
2395 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2396 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2397 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2398 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2399 ua_sess
->buffer_type
= usess
->buffer_type
;
2400 ua_sess
->bits_per_long
= app
->bits_per_long
;
2402 /* There is only one consumer object per session possible. */
2403 consumer_output_get(usess
->consumer
);
2404 ua_sess
->consumer
= usess
->consumer
;
2406 ua_sess
->output_traces
= usess
->output_traces
;
2407 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2408 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2409 &usess
->metadata_attr
);
2411 switch (ua_sess
->buffer_type
) {
2412 case LTTNG_BUFFER_PER_PID
:
2413 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2414 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2417 case LTTNG_BUFFER_PER_UID
:
2418 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2419 DEFAULT_UST_TRACE_UID_PATH
,
2420 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2421 app
->bits_per_long
);
2428 PERROR("asprintf UST shadow copy session");
2433 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2434 sizeof(ua_sess
->root_shm_path
));
2435 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2436 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2437 sizeof(ua_sess
->shm_path
));
2438 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2439 if (ua_sess
->shm_path
[0]) {
2440 switch (ua_sess
->buffer_type
) {
2441 case LTTNG_BUFFER_PER_PID
:
2442 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2443 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2444 app
->name
, app
->pid
, datetime
);
2446 case LTTNG_BUFFER_PER_UID
:
2447 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2448 "/" DEFAULT_UST_TRACE_UID_PATH
,
2449 app
->uid
, app
->bits_per_long
);
2456 PERROR("sprintf UST shadow copy session");
2460 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2461 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2462 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2467 consumer_output_put(ua_sess
->consumer
);
2471 * Lookup sesison wrapper.
2474 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2475 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2477 /* Get right UST app session from app */
2478 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2482 * Return ust app session from the app session hashtable using the UST session
2485 static struct ust_app_session
*lookup_session_by_app(
2486 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2488 struct lttng_ht_iter iter
;
2489 struct lttng_ht_node_u64
*node
;
2491 __lookup_session_by_app(usess
, app
, &iter
);
2492 node
= lttng_ht_iter_get_node_u64(&iter
);
2497 return caa_container_of(node
, struct ust_app_session
, node
);
2504 * Setup buffer registry per PID for the given session and application. If none
2505 * is found, a new one is created, added to the global registry and
2506 * initialized. If regp is valid, it's set with the newly created object.
2508 * Return 0 on success or else a negative value.
2510 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2511 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2514 struct buffer_reg_pid
*reg_pid
;
2516 LTTNG_ASSERT(ua_sess
);
2521 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2524 * This is the create channel path meaning that if there is NO
2525 * registry available, we have to create one for this session.
2527 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2528 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2536 /* Initialize registry. */
2537 reg_pid
->registry
->reg
.ust
= ust_registry_session_per_pid_create(app
,
2538 app
->bits_per_long
, app
->uint8_t_alignment
,
2539 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2540 app
->uint64_t_alignment
, app
->long_alignment
,
2541 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2542 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2543 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2544 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2545 ua_sess
->tracing_id
);
2546 if (!reg_pid
->registry
->reg
.ust
) {
2548 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2549 * destroy the buffer registry, because it is always expected
2550 * that if the buffer registry can be found, its ust registry is
2553 buffer_reg_pid_destroy(reg_pid
);
2557 buffer_reg_pid_add(reg_pid
);
2559 DBG3("UST app buffer registry per PID created successfully");
2571 * Setup buffer registry per UID for the given session and application. If none
2572 * is found, a new one is created, added to the global registry and
2573 * initialized. If regp is valid, it's set with the newly created object.
2575 * Return 0 on success or else a negative value.
2577 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2578 struct ust_app_session
*ua_sess
,
2579 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2582 struct buffer_reg_uid
*reg_uid
;
2584 LTTNG_ASSERT(usess
);
2589 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2592 * This is the create channel path meaning that if there is NO
2593 * registry available, we have to create one for this session.
2595 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2596 LTTNG_DOMAIN_UST
, ®_uid
,
2597 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2605 /* Initialize registry. */
2606 reg_uid
->registry
->reg
.ust
= ust_registry_session_per_uid_create(
2607 app
->bits_per_long
, app
->uint8_t_alignment
,
2608 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2609 app
->uint64_t_alignment
, app
->long_alignment
,
2610 app
->byte_order
, app
->version
.major
,
2611 app
->version
.minor
, reg_uid
->root_shm_path
,
2612 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2613 ua_sess
->tracing_id
, app
->uid
);
2614 if (!reg_uid
->registry
->reg
.ust
) {
2616 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2617 * destroy the buffer registry, because it is always expected
2618 * that if the buffer registry can be found, its ust registry is
2621 buffer_reg_uid_destroy(reg_uid
, NULL
);
2625 /* Add node to teardown list of the session. */
2626 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2628 buffer_reg_uid_add(reg_uid
);
2630 DBG3("UST app buffer registry per UID created successfully");
2641 * Create a session on the tracer side for the given app.
2643 * On success, ua_sess_ptr is populated with the session pointer or else left
2644 * untouched. If the session was created, is_created is set to 1. On error,
2645 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2648 * Returns 0 on success or else a negative code which is either -ENOMEM or
2649 * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails.
2651 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2652 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2655 int ret
, created
= 0;
2656 struct ust_app_session
*ua_sess
;
2658 LTTNG_ASSERT(usess
);
2660 LTTNG_ASSERT(ua_sess_ptr
);
2662 health_code_update();
2664 ua_sess
= lookup_session_by_app(usess
, app
);
2665 if (ua_sess
== NULL
) {
2666 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2667 app
->pid
, usess
->id
);
2668 ua_sess
= alloc_ust_app_session();
2669 if (ua_sess
== NULL
) {
2670 /* Only malloc can failed so something is really wrong */
2674 shadow_copy_session(ua_sess
, usess
, app
);
2678 switch (usess
->buffer_type
) {
2679 case LTTNG_BUFFER_PER_PID
:
2680 /* Init local registry. */
2681 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2683 delete_ust_app_session(-1, ua_sess
, app
);
2687 case LTTNG_BUFFER_PER_UID
:
2688 /* Look for a global registry. If none exists, create one. */
2689 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2691 delete_ust_app_session(-1, ua_sess
, app
);
2701 health_code_update();
2703 if (ua_sess
->handle
== -1) {
2704 pthread_mutex_lock(&app
->sock_lock
);
2705 ret
= lttng_ust_ctl_create_session(app
->sock
);
2706 pthread_mutex_unlock(&app
->sock_lock
);
2708 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2709 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2710 app
->pid
, app
->sock
);
2712 } else if (ret
== -EAGAIN
) {
2713 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2714 app
->pid
, app
->sock
);
2717 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2718 ret
, app
->pid
, app
->sock
);
2720 delete_ust_app_session(-1, ua_sess
, app
);
2721 if (ret
!= -ENOMEM
) {
2723 * Tracer is probably gone or got an internal error so let's
2724 * behave like it will soon unregister or not usable.
2731 ua_sess
->handle
= ret
;
2733 /* Add ust app session to app's HT */
2734 lttng_ht_node_init_u64(&ua_sess
->node
,
2735 ua_sess
->tracing_id
);
2736 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2737 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2738 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2739 &ua_sess
->ust_objd_node
);
2741 DBG2("UST app session created successfully with handle %d", ret
);
2744 *ua_sess_ptr
= ua_sess
;
2746 *is_created
= created
;
2749 /* Everything went well. */
2753 health_code_update();
2758 * Match function for a hash table lookup of ust_app_ctx.
2760 * It matches an ust app context based on the context type and, in the case
2761 * of perf counters, their name.
2763 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2765 struct ust_app_ctx
*ctx
;
2766 const struct lttng_ust_context_attr
*key
;
2771 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2772 key
= (lttng_ust_context_attr
*) _key
;
2775 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2780 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2781 if (strncmp(key
->u
.perf_counter
.name
,
2782 ctx
->ctx
.u
.perf_counter
.name
,
2783 sizeof(key
->u
.perf_counter
.name
))) {
2787 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2788 if (strcmp(key
->u
.app_ctx
.provider_name
,
2789 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2790 strcmp(key
->u
.app_ctx
.ctx_name
,
2791 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2807 * Lookup for an ust app context from an lttng_ust_context.
2809 * Must be called while holding RCU read side lock.
2810 * Return an ust_app_ctx object or NULL on error.
2813 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2814 struct lttng_ust_context_attr
*uctx
)
2816 struct lttng_ht_iter iter
;
2817 struct lttng_ht_node_ulong
*node
;
2818 struct ust_app_ctx
*app_ctx
= NULL
;
2822 ASSERT_RCU_READ_LOCKED();
2824 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2825 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2826 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2827 node
= lttng_ht_iter_get_node_ulong(&iter
);
2832 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2839 * Create a context for the channel on the tracer.
2841 * Called with UST app session lock held and a RCU read side lock.
2844 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2845 struct lttng_ust_context_attr
*uctx
,
2846 struct ust_app
*app
)
2849 struct ust_app_ctx
*ua_ctx
;
2851 ASSERT_RCU_READ_LOCKED();
2853 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2855 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2861 ua_ctx
= alloc_ust_app_ctx(uctx
);
2862 if (ua_ctx
== NULL
) {
2868 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2869 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2870 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2872 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2882 * Enable on the tracer side a ust app event for the session and channel.
2884 * Called with UST app session lock held.
2887 int enable_ust_app_event(struct ust_app_event
*ua_event
,
2888 struct ust_app
*app
)
2892 ret
= enable_ust_object(app
, ua_event
->obj
);
2897 ua_event
->enabled
= 1;
2904 * Disable on the tracer side a ust app event for the session and channel.
2906 static int disable_ust_app_event(struct ust_app_event
*ua_event
,
2907 struct ust_app
*app
)
2911 ret
= disable_ust_object(app
, ua_event
->obj
);
2916 ua_event
->enabled
= 0;
2923 * Lookup ust app channel for session and disable it on the tracer side.
2926 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2927 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2931 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2936 ua_chan
->enabled
= 0;
2943 * Lookup ust app channel for session and enable it on the tracer side. This
2944 * MUST be called with a RCU read side lock acquired.
2946 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2947 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2950 struct lttng_ht_iter iter
;
2951 struct lttng_ht_node_str
*ua_chan_node
;
2952 struct ust_app_channel
*ua_chan
;
2954 ASSERT_RCU_READ_LOCKED();
2956 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2957 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2958 if (ua_chan_node
== NULL
) {
2959 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2960 uchan
->name
, ua_sess
->tracing_id
);
2964 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2966 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2976 * Ask the consumer to create a channel and get it if successful.
2978 * Called with UST app session lock held.
2980 * Return 0 on success or else a negative value.
2982 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2983 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2984 int bitness
, ust_registry_session
*registry
)
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 of channel \"%s\" of session \"%" PRIu64
"\".",
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
);
3354 error_stream_unlock
:
3355 pthread_mutex_unlock(&buf_reg_chan
->stream_list_lock
);
3361 * Create and send to the application the created buffers with per UID buffers.
3363 * This MUST be called with a RCU read side lock acquired.
3364 * The session list lock and the session's lock must be acquired.
3366 * Return 0 on success else a negative value.
3368 static int create_channel_per_uid(struct ust_app
*app
,
3369 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3370 struct ust_app_channel
*ua_chan
)
3373 struct buffer_reg_uid
*reg_uid
;
3374 struct buffer_reg_channel
*buf_reg_chan
;
3375 struct ltt_session
*session
= NULL
;
3376 enum lttng_error_code notification_ret
;
3377 struct ust_registry_channel
*ust_reg_chan
;
3380 LTTNG_ASSERT(usess
);
3381 LTTNG_ASSERT(ua_sess
);
3382 LTTNG_ASSERT(ua_chan
);
3383 ASSERT_RCU_READ_LOCKED();
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 ASSERT_LOCKED(session
->lock
);
3412 ASSERT_SESSION_LIST_LOCKED();
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
);
3421 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3425 * Let's remove the previously created buffer registry channel so
3426 * it's not visible anymore in the session registry.
3428 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3429 ua_chan
->tracing_channel_id
, false);
3430 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
3431 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3436 * Setup the streams and add it to the session registry.
3438 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3439 ua_chan
, buf_reg_chan
, app
);
3441 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3445 /* Notify the notification subsystem of the channel's creation. */
3446 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->_lock
);
3447 ust_reg_chan
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3448 ua_chan
->tracing_channel_id
);
3449 LTTNG_ASSERT(ust_reg_chan
);
3450 ust_reg_chan
->consumer_key
= ua_chan
->key
;
3451 ust_reg_chan
= NULL
;
3452 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->_lock
);
3454 notification_ret
= notification_thread_command_add_channel(
3455 the_notification_thread_handle
, session
->name
,
3456 lttng_credentials_get_uid(
3457 &ua_sess
->effective_credentials
),
3458 lttng_credentials_get_gid(
3459 &ua_sess
->effective_credentials
),
3460 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3461 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3462 if (notification_ret
!= LTTNG_OK
) {
3463 ret
= - (int) notification_ret
;
3464 ERR("Failed to add channel to notification thread");
3469 /* Send buffers to the application. */
3470 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3472 if (ret
!= -ENOTCONN
) {
3473 ERR("Error sending channel to application");
3480 session_put(session
);
3486 * Create and send to the application the created buffers with per PID buffers.
3488 * Called with UST app session lock held.
3489 * The session list lock and the session's lock must be acquired.
3491 * Return 0 on success else a negative value.
3493 static int create_channel_per_pid(struct ust_app
*app
,
3494 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3495 struct ust_app_channel
*ua_chan
)
3498 ust_registry_session
*registry
;
3499 enum lttng_error_code cmd_ret
;
3500 struct ltt_session
*session
= NULL
;
3501 uint64_t chan_reg_key
;
3502 struct ust_registry_channel
*ust_reg_chan
;
3505 LTTNG_ASSERT(usess
);
3506 LTTNG_ASSERT(ua_sess
);
3507 LTTNG_ASSERT(ua_chan
);
3509 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3513 registry
= get_session_registry(ua_sess
);
3514 /* The UST app session lock is held, registry shall not be null. */
3515 LTTNG_ASSERT(registry
);
3517 /* Create and add a new channel registry to session. */
3518 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3520 ERR("Error creating the UST channel \"%s\" registry instance",
3525 session
= session_find_by_id(ua_sess
->tracing_id
);
3526 LTTNG_ASSERT(session
);
3527 ASSERT_LOCKED(session
->lock
);
3528 ASSERT_SESSION_LIST_LOCKED();
3530 /* Create and get channel on the consumer side. */
3531 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3532 app
->bits_per_long
, registry
);
3534 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3536 goto error_remove_from_registry
;
3539 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3541 if (ret
!= -ENOTCONN
) {
3542 ERR("Error sending channel to application");
3544 goto error_remove_from_registry
;
3547 chan_reg_key
= ua_chan
->key
;
3548 pthread_mutex_lock(®istry
->_lock
);
3549 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
3550 LTTNG_ASSERT(ust_reg_chan
);
3551 ust_reg_chan
->consumer_key
= ua_chan
->key
;
3552 pthread_mutex_unlock(®istry
->_lock
);
3554 cmd_ret
= notification_thread_command_add_channel(
3555 the_notification_thread_handle
, session
->name
,
3556 lttng_credentials_get_uid(
3557 &ua_sess
->effective_credentials
),
3558 lttng_credentials_get_gid(
3559 &ua_sess
->effective_credentials
),
3560 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3561 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3562 if (cmd_ret
!= LTTNG_OK
) {
3563 ret
= - (int) cmd_ret
;
3564 ERR("Failed to add channel to notification thread");
3565 goto error_remove_from_registry
;
3568 error_remove_from_registry
:
3570 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3575 session_put(session
);
3581 * From an already allocated ust app channel, create the channel buffers if
3582 * needed and send them to the application. This MUST be called with a RCU read
3583 * side lock acquired.
3585 * Called with UST app session lock held.
3587 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3588 * the application exited concurrently.
3590 static int ust_app_channel_send(struct ust_app
*app
,
3591 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3592 struct ust_app_channel
*ua_chan
)
3597 LTTNG_ASSERT(usess
);
3598 LTTNG_ASSERT(usess
->active
);
3599 LTTNG_ASSERT(ua_sess
);
3600 LTTNG_ASSERT(ua_chan
);
3601 ASSERT_RCU_READ_LOCKED();
3603 /* Handle buffer type before sending the channel to the application. */
3604 switch (usess
->buffer_type
) {
3605 case LTTNG_BUFFER_PER_UID
:
3607 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3613 case LTTNG_BUFFER_PER_PID
:
3615 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3627 /* Initialize ust objd object using the received handle and add it. */
3628 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3629 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3631 /* If channel is not enabled, disable it on the tracer */
3632 if (!ua_chan
->enabled
) {
3633 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3644 * Create UST app channel and return it through ua_chanp if not NULL.
3646 * Called with UST app session lock and RCU read-side lock held.
3648 * Return 0 on success or else a negative value.
3650 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3651 struct ltt_ust_channel
*uchan
,
3652 enum lttng_ust_abi_chan_type type
,
3653 struct ltt_ust_session
*usess
__attribute__((unused
)),
3654 struct ust_app_channel
**ua_chanp
)
3657 struct lttng_ht_iter iter
;
3658 struct lttng_ht_node_str
*ua_chan_node
;
3659 struct ust_app_channel
*ua_chan
;
3661 ASSERT_RCU_READ_LOCKED();
3663 /* Lookup channel in the ust app session */
3664 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3665 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3666 if (ua_chan_node
!= NULL
) {
3667 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3671 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3672 if (ua_chan
== NULL
) {
3673 /* Only malloc can fail here */
3677 shadow_copy_channel(ua_chan
, uchan
);
3679 /* Set channel type. */
3680 ua_chan
->attr
.type
= type
;
3682 /* Only add the channel if successful on the tracer side. */
3683 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3686 *ua_chanp
= ua_chan
;
3689 /* Everything went well. */
3697 * Create UST app event and create it on the tracer side.
3699 * Must be called with the RCU read side lock held.
3700 * Called with ust app session mutex held.
3703 int create_ust_app_event(struct ust_app_channel
*ua_chan
,
3704 struct ltt_ust_event
*uevent
,
3705 struct ust_app
*app
)
3708 struct ust_app_event
*ua_event
;
3710 ASSERT_RCU_READ_LOCKED();
3712 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3713 if (ua_event
== NULL
) {
3714 /* Only failure mode of alloc_ust_app_event(). */
3718 shadow_copy_event(ua_event
, uevent
);
3720 /* Create it on the tracer side */
3721 ret
= create_ust_event(app
, ua_chan
, ua_event
);
3724 * Not found previously means that it does not exist on the
3725 * tracer. If the application reports that the event existed,
3726 * it means there is a bug in the sessiond or lttng-ust
3727 * (or corruption, etc.)
3729 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3730 ERR("Tracer for application reported that an event being created already existed: "
3731 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3733 app
->pid
, app
->ppid
, app
->uid
,
3739 add_unique_ust_app_event(ua_chan
, ua_event
);
3741 DBG2("UST app create event completed: app = '%s' pid = %d",
3742 app
->name
, app
->pid
);
3748 /* Valid. Calling here is already in a read side lock */
3749 delete_ust_app_event(-1, ua_event
, app
);
3754 * Create UST app event notifier rule and create it on the tracer side.
3756 * Must be called with the RCU read side lock held.
3757 * Called with ust app session mutex held.
3760 int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
,
3761 struct ust_app
*app
)
3764 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3766 ASSERT_RCU_READ_LOCKED();
3768 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3769 if (ua_event_notifier_rule
== NULL
) {
3774 /* Create it on the tracer side. */
3775 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3778 * Not found previously means that it does not exist on the
3779 * tracer. If the application reports that the event existed,
3780 * it means there is a bug in the sessiond or lttng-ust
3781 * (or corruption, etc.)
3783 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3784 ERR("Tracer for application reported that an event notifier being created already exists: "
3785 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3786 lttng_trigger_get_tracer_token(trigger
),
3787 app
->pid
, app
->ppid
, app
->uid
,
3793 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3794 &ua_event_notifier_rule
->node
);
3796 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64
,
3797 app
->name
, app
->pid
, lttng_trigger_get_tracer_token(trigger
));
3802 /* The RCU read side lock is already being held by the caller. */
3803 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3809 * Create UST metadata and open it on the tracer side.
3811 * Called with UST app session lock held and RCU read side lock.
3813 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3814 struct ust_app
*app
, struct consumer_output
*consumer
)
3817 struct ust_app_channel
*metadata
;
3818 struct consumer_socket
*socket
;
3819 ust_registry_session
*registry
;
3820 struct ltt_session
*session
= NULL
;
3822 LTTNG_ASSERT(ua_sess
);
3824 LTTNG_ASSERT(consumer
);
3825 ASSERT_RCU_READ_LOCKED();
3827 registry
= get_session_registry(ua_sess
);
3828 /* The UST app session is held registry shall not be null. */
3829 LTTNG_ASSERT(registry
);
3831 pthread_mutex_lock(®istry
->_lock
);
3833 /* Metadata already exists for this registry or it was closed previously */
3834 if (registry
->_metadata_key
|| registry
->_metadata_closed
) {
3839 /* Allocate UST metadata */
3840 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3842 /* malloc() failed */
3847 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3849 /* Need one fd for the channel. */
3850 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3852 ERR("Exhausted number of available FD upon create metadata");
3856 /* Get the right consumer socket for the application. */
3857 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3860 goto error_consumer
;
3864 * Keep metadata key so we can identify it on the consumer side. Assign it
3865 * to the registry *before* we ask the consumer so we avoid the race of the
3866 * consumer requesting the metadata and the ask_channel call on our side
3867 * did not returned yet.
3869 registry
->_metadata_key
= metadata
->key
;
3871 session
= session_find_by_id(ua_sess
->tracing_id
);
3872 LTTNG_ASSERT(session
);
3873 ASSERT_LOCKED(session
->lock
);
3874 ASSERT_SESSION_LIST_LOCKED();
3877 * Ask the metadata channel creation to the consumer. The metadata object
3878 * will be created by the consumer and kept their. However, the stream is
3879 * never added or monitored until we do a first push metadata to the
3882 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3883 registry
, session
->current_trace_chunk
);
3885 /* Nullify the metadata key so we don't try to close it later on. */
3886 registry
->_metadata_key
= 0;
3887 goto error_consumer
;
3891 * The setup command will make the metadata stream be sent to the relayd,
3892 * if applicable, and the thread managing the metadatas. This is important
3893 * because after this point, if an error occurs, the only way the stream
3894 * can be deleted is to be monitored in the consumer.
3896 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3898 /* Nullify the metadata key so we don't try to close it later on. */
3899 registry
->_metadata_key
= 0;
3900 goto error_consumer
;
3903 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3904 metadata
->key
, app
->pid
);
3907 lttng_fd_put(LTTNG_FD_APPS
, 1);
3908 delete_ust_app_channel(-1, metadata
, app
);
3910 pthread_mutex_unlock(®istry
->_lock
);
3912 session_put(session
);
3918 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3919 * acquired before calling this function.
3921 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3923 struct ust_app
*app
= NULL
;
3924 struct lttng_ht_node_ulong
*node
;
3925 struct lttng_ht_iter iter
;
3927 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3928 node
= lttng_ht_iter_get_node_ulong(&iter
);
3930 DBG2("UST app no found with pid %d", pid
);
3934 DBG2("Found UST app by pid %d", pid
);
3936 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3943 * Allocate and init an UST app object using the registration information and
3944 * the command socket. This is called when the command socket connects to the
3947 * The object is returned on success or else NULL.
3949 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3952 struct ust_app
*lta
= NULL
;
3953 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3956 LTTNG_ASSERT(sock
>= 0);
3958 DBG3("UST app creating application for socket %d", sock
);
3960 if ((msg
->bits_per_long
== 64 &&
3961 (uatomic_read(&the_ust_consumerd64_fd
) ==
3963 (msg
->bits_per_long
== 32 &&
3964 (uatomic_read(&the_ust_consumerd32_fd
) ==
3966 ERR("Registration failed: application \"%s\" (pid: %d) has "
3967 "%d-bit long, but no consumerd for this size is available.\n",
3968 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3973 * Reserve the two file descriptors of the event source pipe. The write
3974 * end will be closed once it is passed to the application, at which
3975 * point a single 'put' will be performed.
3977 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3979 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
3980 msg
->name
, (int) msg
->pid
);
3984 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3985 if (!event_notifier_event_source_pipe
) {
3986 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
3987 msg
->name
, msg
->pid
);
3991 lta
= zmalloc
<ust_app
>();
3994 goto error_free_pipe
;
3997 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
3999 lta
->ppid
= msg
->ppid
;
4000 lta
->uid
= msg
->uid
;
4001 lta
->gid
= msg
->gid
;
4003 lta
->bits_per_long
= msg
->bits_per_long
;
4004 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
4005 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
4006 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
4007 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
4008 lta
->long_alignment
= msg
->long_alignment
;
4009 lta
->byte_order
= msg
->byte_order
;
4011 lta
->v_major
= msg
->major
;
4012 lta
->v_minor
= msg
->minor
;
4013 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4014 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4015 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4016 lta
->notify_sock
= -1;
4017 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4019 /* Copy name and make sure it's NULL terminated. */
4020 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
4021 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
4024 * Before this can be called, when receiving the registration information,
4025 * the application compatibility is checked. So, at this point, the
4026 * application can work with this session daemon.
4028 lta
->compatible
= 1;
4030 lta
->pid
= msg
->pid
;
4031 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
4033 pthread_mutex_init(<a
->sock_lock
, NULL
);
4034 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
4036 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
4040 lttng_pipe_destroy(event_notifier_event_source_pipe
);
4041 lttng_fd_put(LTTNG_FD_APPS
, 2);
4047 * For a given application object, add it to every hash table.
4049 void ust_app_add(struct ust_app
*app
)
4052 LTTNG_ASSERT(app
->notify_sock
>= 0);
4054 app
->registration_time
= time(NULL
);
4059 * On a re-registration, we want to kick out the previous registration of
4062 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
4065 * The socket _should_ be unique until _we_ call close. So, a add_unique
4066 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4067 * already in the table.
4069 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
4071 /* Add application to the notify socket hash table. */
4072 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
4073 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
4075 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4076 "notify_sock =%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
4077 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
4084 * Set the application version into the object.
4086 * Return 0 on success else a negative value either an errno code or a
4087 * LTTng-UST error code.
4089 int ust_app_version(struct ust_app
*app
)
4095 pthread_mutex_lock(&app
->sock_lock
);
4096 ret
= lttng_ust_ctl_tracer_version(app
->sock
, &app
->version
);
4097 pthread_mutex_unlock(&app
->sock_lock
);
4099 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
4100 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4101 app
->pid
, app
->sock
);
4102 } else if (ret
== -EAGAIN
) {
4103 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4104 app
->pid
, app
->sock
);
4106 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4107 ret
, app
->pid
, app
->sock
);
4114 bool ust_app_supports_notifiers(const struct ust_app
*app
)
4116 return app
->v_major
>= 9;
4119 bool ust_app_supports_counters(const struct ust_app
*app
)
4121 return app
->v_major
>= 9;
4125 * Setup the base event notifier group.
4127 * Return 0 on success else a negative value either an errno code or a
4128 * LTTng-UST error code.
4130 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4133 int event_pipe_write_fd
;
4134 struct lttng_ust_abi_object_data
*event_notifier_group
= NULL
;
4135 enum lttng_error_code lttng_ret
;
4136 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4140 if (!ust_app_supports_notifiers(app
)) {
4145 /* Get the write side of the pipe. */
4146 event_pipe_write_fd
= lttng_pipe_get_writefd(
4147 app
->event_notifier_group
.event_pipe
);
4149 pthread_mutex_lock(&app
->sock_lock
);
4150 ret
= lttng_ust_ctl_create_event_notifier_group(app
->sock
,
4151 event_pipe_write_fd
, &event_notifier_group
);
4152 pthread_mutex_unlock(&app
->sock_lock
);
4154 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4156 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4157 app
->pid
, app
->sock
);
4158 } else if (ret
== -EAGAIN
) {
4160 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4161 app
->pid
, app
->sock
);
4163 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4164 ret
, app
->pid
, app
->sock
, event_pipe_write_fd
);
4169 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4171 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4172 app
->name
, app
->pid
);
4177 * Release the file descriptor that was reserved for the write-end of
4180 lttng_fd_put(LTTNG_FD_APPS
, 1);
4182 lttng_ret
= notification_thread_command_add_tracer_event_source(
4183 the_notification_thread_handle
,
4184 lttng_pipe_get_readfd(
4185 app
->event_notifier_group
.event_pipe
),
4187 if (lttng_ret
!= LTTNG_OK
) {
4188 ERR("Failed to add tracer event source to notification thread");
4193 /* Assign handle only when the complete setup is valid. */
4194 app
->event_notifier_group
.object
= event_notifier_group
;
4196 event_notifier_error_accounting_status
=
4197 event_notifier_error_accounting_register_app(app
);
4198 switch (event_notifier_error_accounting_status
) {
4199 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
4201 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
:
4202 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",
4203 app
->sock
, app
->name
, (int) app
->pid
);
4205 goto error_accounting
;
4206 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
4207 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4208 app
->sock
, app
->name
, (int) app
->pid
);
4210 goto error_accounting
;
4212 ERR("Failed to setup event notifier error accounting for app");
4214 goto error_accounting
;
4220 lttng_ret
= notification_thread_command_remove_tracer_event_source(
4221 the_notification_thread_handle
,
4222 lttng_pipe_get_readfd(
4223 app
->event_notifier_group
.event_pipe
));
4224 if (lttng_ret
!= LTTNG_OK
) {
4225 ERR("Failed to remove application tracer event source from notification thread");
4229 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4230 free(app
->event_notifier_group
.object
);
4231 app
->event_notifier_group
.object
= NULL
;
4236 * Unregister app by removing it from the global traceable app list and freeing
4239 * The socket is already closed at this point so no close to sock.
4241 void ust_app_unregister(int sock
)
4243 struct ust_app
*lta
;
4244 struct lttng_ht_node_ulong
*node
;
4245 struct lttng_ht_iter ust_app_sock_iter
;
4246 struct lttng_ht_iter iter
;
4247 struct ust_app_session
*ua_sess
;
4252 /* Get the node reference for a call_rcu */
4253 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
4254 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4257 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
4258 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4261 * For per-PID buffers, perform "push metadata" and flush all
4262 * application streams before removing app from hash tables,
4263 * ensuring proper behavior of data_pending check.
4264 * Remove sessions so they are not visible during deletion.
4266 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
4268 ust_registry_session
*registry
;
4270 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4272 /* The session was already removed so scheduled for teardown. */
4276 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4277 (void) ust_app_flush_app_session(lta
, ua_sess
);
4281 * Add session to list for teardown. This is safe since at this point we
4282 * are the only one using this list.
4284 pthread_mutex_lock(&ua_sess
->lock
);
4286 if (ua_sess
->deleted
) {
4287 pthread_mutex_unlock(&ua_sess
->lock
);
4292 * Normally, this is done in the delete session process which is
4293 * executed in the call rcu below. However, upon registration we can't
4294 * afford to wait for the grace period before pushing data or else the
4295 * data pending feature can race between the unregistration and stop
4296 * command where the data pending command is sent *before* the grace
4299 * The close metadata below nullifies the metadata pointer in the
4300 * session so the delete session will NOT push/close a second time.
4302 registry
= get_session_registry(ua_sess
);
4304 /* Push metadata for application before freeing the application. */
4305 (void) push_metadata(registry
, ua_sess
->consumer
);
4308 * Don't ask to close metadata for global per UID buffers. Close
4309 * metadata only on destroy trace session in this case. Also, the
4310 * previous push metadata could have flag the metadata registry to
4311 * close so don't send a close command if closed.
4313 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4314 /* And ask to close it for this session registry. */
4315 (void) close_metadata(registry
, ua_sess
->consumer
);
4318 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4320 pthread_mutex_unlock(&ua_sess
->lock
);
4323 /* Remove application from PID hash table */
4324 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4328 * Remove application from notify hash table. The thread handling the
4329 * notify socket could have deleted the node so ignore on error because
4330 * either way it's valid. The close of that socket is handled by the
4331 * apps_notify_thread.
4333 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4334 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4337 * Ignore return value since the node might have been removed before by an
4338 * add replace during app registration because the PID can be reassigned by
4341 iter
.iter
.node
= <a
->pid_n
.node
;
4342 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4344 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4349 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4356 * Fill events array with all events name of all registered apps.
4358 int ust_app_list_events(struct lttng_event
**events
)
4361 size_t nbmem
, count
= 0;
4362 struct lttng_ht_iter iter
;
4363 struct ust_app
*app
;
4364 struct lttng_event
*tmp_event
;
4366 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4367 tmp_event
= calloc
<lttng_event
>(nbmem
);
4368 if (tmp_event
== NULL
) {
4369 PERROR("zmalloc ust app events");
4376 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4377 struct lttng_ust_abi_tracepoint_iter uiter
;
4379 health_code_update();
4381 if (!app
->compatible
) {
4383 * TODO: In time, we should notice the caller of this error by
4384 * telling him that this is a version error.
4388 pthread_mutex_lock(&app
->sock_lock
);
4389 handle
= lttng_ust_ctl_tracepoint_list(app
->sock
);
4391 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4392 ERR("UST app list events getting handle failed for app pid %d",
4395 pthread_mutex_unlock(&app
->sock_lock
);
4399 while ((ret
= lttng_ust_ctl_tracepoint_list_get(app
->sock
, handle
,
4400 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4401 /* Handle ustctl error. */
4405 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4406 ERR("UST app tp list get failed for app %d with ret %d",
4409 DBG3("UST app tp list get failed. Application is dead");
4413 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4414 if (release_ret
< 0 &&
4415 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4416 release_ret
!= -EPIPE
) {
4417 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4419 pthread_mutex_unlock(&app
->sock_lock
);
4423 health_code_update();
4424 if (count
>= nbmem
) {
4425 /* In case the realloc fails, we free the memory */
4426 struct lttng_event
*new_tmp_event
;
4429 new_nbmem
= nbmem
<< 1;
4430 DBG2("Reallocating event list from %zu to %zu entries",
4432 new_tmp_event
= (lttng_event
*) realloc(tmp_event
,
4433 new_nbmem
* sizeof(struct lttng_event
));
4434 if (new_tmp_event
== NULL
) {
4437 PERROR("realloc ust app events");
4440 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4441 if (release_ret
< 0 &&
4442 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4443 release_ret
!= -EPIPE
) {
4444 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4446 pthread_mutex_unlock(&app
->sock_lock
);
4449 /* Zero the new memory */
4450 memset(new_tmp_event
+ nbmem
, 0,
4451 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4453 tmp_event
= new_tmp_event
;
4455 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4456 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4457 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4458 tmp_event
[count
].pid
= app
->pid
;
4459 tmp_event
[count
].enabled
= -1;
4462 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4463 pthread_mutex_unlock(&app
->sock_lock
);
4465 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4466 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4467 app
->pid
, app
->sock
);
4468 } else if (ret
== -EAGAIN
) {
4469 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4470 app
->pid
, app
->sock
);
4472 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4473 ret
, app
->pid
, app
->sock
);
4479 *events
= tmp_event
;
4481 DBG2("UST app list events done (%zu events)", count
);
4486 health_code_update();
4491 * Fill events array with all events name of all registered apps.
4493 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4496 size_t nbmem
, count
= 0;
4497 struct lttng_ht_iter iter
;
4498 struct ust_app
*app
;
4499 struct lttng_event_field
*tmp_event
;
4501 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4502 tmp_event
= calloc
<lttng_event_field
>(nbmem
);
4503 if (tmp_event
== NULL
) {
4504 PERROR("zmalloc ust app event fields");
4511 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4512 struct lttng_ust_abi_field_iter uiter
;
4514 health_code_update();
4516 if (!app
->compatible
) {
4518 * TODO: In time, we should notice the caller of this error by
4519 * telling him that this is a version error.
4523 pthread_mutex_lock(&app
->sock_lock
);
4524 handle
= lttng_ust_ctl_tracepoint_field_list(app
->sock
);
4526 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4527 ERR("UST app list field getting handle failed for app pid %d",
4530 pthread_mutex_unlock(&app
->sock_lock
);
4534 while ((ret
= lttng_ust_ctl_tracepoint_field_list_get(app
->sock
, handle
,
4535 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4536 /* Handle ustctl error. */
4540 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4541 ERR("UST app tp list field failed for app %d with ret %d",
4544 DBG3("UST app tp list field failed. Application is dead");
4548 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4549 pthread_mutex_unlock(&app
->sock_lock
);
4550 if (release_ret
< 0 &&
4551 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4552 release_ret
!= -EPIPE
) {
4553 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4558 health_code_update();
4559 if (count
>= nbmem
) {
4560 /* In case the realloc fails, we free the memory */
4561 struct lttng_event_field
*new_tmp_event
;
4564 new_nbmem
= nbmem
<< 1;
4565 DBG2("Reallocating event field list from %zu to %zu entries",
4567 new_tmp_event
= (lttng_event_field
*) realloc(tmp_event
,
4568 new_nbmem
* sizeof(struct lttng_event_field
));
4569 if (new_tmp_event
== NULL
) {
4572 PERROR("realloc ust app event fields");
4575 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4576 pthread_mutex_unlock(&app
->sock_lock
);
4578 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4579 release_ret
!= -EPIPE
) {
4580 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4584 /* Zero the new memory */
4585 memset(new_tmp_event
+ nbmem
, 0,
4586 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4588 tmp_event
= new_tmp_event
;
4591 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4592 /* Mapping between these enums matches 1 to 1. */
4593 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4594 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4596 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4597 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4598 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4599 tmp_event
[count
].event
.pid
= app
->pid
;
4600 tmp_event
[count
].event
.enabled
= -1;
4603 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4604 pthread_mutex_unlock(&app
->sock_lock
);
4606 ret
!= -LTTNG_UST_ERR_EXITING
&&
4608 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4613 *fields
= tmp_event
;
4615 DBG2("UST app list event fields done (%zu events)", count
);
4620 health_code_update();
4625 * Free and clean all traceable apps of the global list.
4627 void ust_app_clean_list(void)
4630 struct ust_app
*app
;
4631 struct lttng_ht_iter iter
;
4633 DBG2("UST app cleaning registered apps hash table");
4637 /* Cleanup notify socket hash table */
4638 if (ust_app_ht_by_notify_sock
) {
4639 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4640 notify_sock_n
.node
) {
4642 * Assert that all notifiers are gone as all triggers
4643 * are unregistered prior to this clean-up.
4645 LTTNG_ASSERT(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4647 ust_app_notify_sock_unregister(app
->notify_sock
);
4652 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4653 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4655 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4659 /* Cleanup socket hash table */
4660 if (ust_app_ht_by_sock
) {
4661 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4663 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4670 /* Destroy is done only when the ht is empty */
4672 lttng_ht_destroy(ust_app_ht
);
4674 if (ust_app_ht_by_sock
) {
4675 lttng_ht_destroy(ust_app_ht_by_sock
);
4677 if (ust_app_ht_by_notify_sock
) {
4678 lttng_ht_destroy(ust_app_ht_by_notify_sock
);
4683 * Init UST app hash table.
4685 int ust_app_ht_alloc(void)
4687 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4691 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4692 if (!ust_app_ht_by_sock
) {
4695 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4696 if (!ust_app_ht_by_notify_sock
) {
4703 * For a specific UST session, disable the channel for all registered apps.
4705 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4706 struct ltt_ust_channel
*uchan
)
4709 struct lttng_ht_iter iter
;
4710 struct lttng_ht_node_str
*ua_chan_node
;
4711 struct ust_app
*app
;
4712 struct ust_app_session
*ua_sess
;
4713 struct ust_app_channel
*ua_chan
;
4715 LTTNG_ASSERT(usess
->active
);
4716 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4717 uchan
->name
, usess
->id
);
4721 /* For every registered applications */
4722 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4723 struct lttng_ht_iter uiter
;
4724 if (!app
->compatible
) {
4726 * TODO: In time, we should notice the caller of this error by
4727 * telling him that this is a version error.
4731 ua_sess
= lookup_session_by_app(usess
, app
);
4732 if (ua_sess
== NULL
) {
4737 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4738 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4739 /* If the session if found for the app, the channel must be there */
4740 LTTNG_ASSERT(ua_chan_node
);
4742 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4743 /* The channel must not be already disabled */
4744 LTTNG_ASSERT(ua_chan
->enabled
== 1);
4746 /* Disable channel onto application */
4747 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4749 /* XXX: We might want to report this error at some point... */
4759 * For a specific UST session, enable the channel for all registered apps.
4761 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4762 struct ltt_ust_channel
*uchan
)
4765 struct lttng_ht_iter iter
;
4766 struct ust_app
*app
;
4767 struct ust_app_session
*ua_sess
;
4769 LTTNG_ASSERT(usess
->active
);
4770 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4771 uchan
->name
, usess
->id
);
4775 /* For every registered applications */
4776 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4777 if (!app
->compatible
) {
4779 * TODO: In time, we should notice the caller of this error by
4780 * telling him that this is a version error.
4784 ua_sess
= lookup_session_by_app(usess
, app
);
4785 if (ua_sess
== NULL
) {
4789 /* Enable channel onto application */
4790 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4792 /* XXX: We might want to report this error at some point... */
4802 * Disable an event in a channel and for a specific session.
4804 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4805 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4808 struct lttng_ht_iter iter
, uiter
;
4809 struct lttng_ht_node_str
*ua_chan_node
;
4810 struct ust_app
*app
;
4811 struct ust_app_session
*ua_sess
;
4812 struct ust_app_channel
*ua_chan
;
4813 struct ust_app_event
*ua_event
;
4815 LTTNG_ASSERT(usess
->active
);
4816 DBG("UST app disabling event %s for all apps in channel "
4817 "%s for session id %" PRIu64
,
4818 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4822 /* For all registered applications */
4823 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4824 if (!app
->compatible
) {
4826 * TODO: In time, we should notice the caller of this error by
4827 * telling him that this is a version error.
4831 ua_sess
= lookup_session_by_app(usess
, app
);
4832 if (ua_sess
== NULL
) {
4837 /* Lookup channel in the ust app session */
4838 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4839 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4840 if (ua_chan_node
== NULL
) {
4841 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4842 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4845 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4847 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4848 uevent
->filter
, uevent
->attr
.loglevel
,
4850 if (ua_event
== NULL
) {
4851 DBG2("Event %s not found in channel %s for app pid %d."
4852 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4856 ret
= disable_ust_app_event(ua_event
, app
);
4858 /* XXX: Report error someday... */
4867 /* The ua_sess lock must be held by the caller. */
4869 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4870 struct ust_app_session
*ua_sess
,
4871 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4872 struct ust_app_channel
**_ua_chan
)
4875 struct ust_app_channel
*ua_chan
= NULL
;
4877 LTTNG_ASSERT(ua_sess
);
4878 ASSERT_LOCKED(ua_sess
->lock
);
4880 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4881 sizeof(uchan
->name
))) {
4882 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4886 struct ltt_ust_context
*uctx
= NULL
;
4889 * Create channel onto application and synchronize its
4892 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4893 LTTNG_UST_ABI_CHAN_PER_CPU
, usess
,
4899 ret
= ust_app_channel_send(app
, usess
,
4906 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4907 ret
= create_ust_app_channel_context(ua_chan
,
4920 * The application's socket is not valid. Either a bad socket
4921 * or a timeout on it. We can't inform the caller that for a
4922 * specific app, the session failed so lets continue here.
4924 ret
= 0; /* Not an error. */
4932 if (ret
== 0 && _ua_chan
) {
4934 * Only return the application's channel on success. Note
4935 * that the channel can still be part of the application's
4936 * channel hashtable on error.
4938 *_ua_chan
= ua_chan
;
4944 * Enable event for a specific session and channel on the tracer.
4946 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4947 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4950 struct lttng_ht_iter iter
, uiter
;
4951 struct lttng_ht_node_str
*ua_chan_node
;
4952 struct ust_app
*app
;
4953 struct ust_app_session
*ua_sess
;
4954 struct ust_app_channel
*ua_chan
;
4955 struct ust_app_event
*ua_event
;
4957 LTTNG_ASSERT(usess
->active
);
4958 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4959 uevent
->attr
.name
, usess
->id
);
4962 * NOTE: At this point, this function is called only if the session and
4963 * channel passed are already created for all apps. and enabled on the
4969 /* For all registered applications */
4970 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4971 if (!app
->compatible
) {
4973 * TODO: In time, we should notice the caller of this error by
4974 * telling him that this is a version error.
4978 ua_sess
= lookup_session_by_app(usess
, app
);
4980 /* The application has problem or is probably dead. */
4984 pthread_mutex_lock(&ua_sess
->lock
);
4986 if (ua_sess
->deleted
) {
4987 pthread_mutex_unlock(&ua_sess
->lock
);
4991 /* Lookup channel in the ust app session */
4992 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4993 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4995 * It is possible that the channel cannot be found is
4996 * the channel/event creation occurs concurrently with
4997 * an application exit.
4999 if (!ua_chan_node
) {
5000 pthread_mutex_unlock(&ua_sess
->lock
);
5004 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5006 /* Get event node */
5007 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5008 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5009 if (ua_event
== NULL
) {
5010 DBG3("UST app enable event %s not found for app PID %d."
5011 "Skipping app", uevent
->attr
.name
, app
->pid
);
5015 ret
= enable_ust_app_event(ua_event
, app
);
5017 pthread_mutex_unlock(&ua_sess
->lock
);
5021 pthread_mutex_unlock(&ua_sess
->lock
);
5030 * For a specific existing UST session and UST channel, creates the event for
5031 * all registered apps.
5033 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
5034 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
5037 struct lttng_ht_iter iter
, uiter
;
5038 struct lttng_ht_node_str
*ua_chan_node
;
5039 struct ust_app
*app
;
5040 struct ust_app_session
*ua_sess
;
5041 struct ust_app_channel
*ua_chan
;
5043 LTTNG_ASSERT(usess
->active
);
5044 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
5045 uevent
->attr
.name
, usess
->id
);
5049 /* For all registered applications */
5050 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5051 if (!app
->compatible
) {
5053 * TODO: In time, we should notice the caller of this error by
5054 * telling him that this is a version error.
5058 ua_sess
= lookup_session_by_app(usess
, app
);
5060 /* The application has problem or is probably dead. */
5064 pthread_mutex_lock(&ua_sess
->lock
);
5066 if (ua_sess
->deleted
) {
5067 pthread_mutex_unlock(&ua_sess
->lock
);
5071 /* Lookup channel in the ust app session */
5072 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5073 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5074 /* If the channel is not found, there is a code flow error */
5075 LTTNG_ASSERT(ua_chan_node
);
5077 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5079 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5080 pthread_mutex_unlock(&ua_sess
->lock
);
5082 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
5083 /* Possible value at this point: -ENOMEM. If so, we stop! */
5086 DBG2("UST app event %s already exist on app PID %d",
5087 uevent
->attr
.name
, app
->pid
);
5097 * Start tracing for a specific UST session and app.
5099 * Called with UST app session lock held.
5103 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5106 struct ust_app_session
*ua_sess
;
5108 DBG("Starting tracing for ust app pid %d", app
->pid
);
5112 if (!app
->compatible
) {
5116 ua_sess
= lookup_session_by_app(usess
, app
);
5117 if (ua_sess
== NULL
) {
5118 /* The session is in teardown process. Ignore and continue. */
5122 pthread_mutex_lock(&ua_sess
->lock
);
5124 if (ua_sess
->deleted
) {
5125 pthread_mutex_unlock(&ua_sess
->lock
);
5129 if (ua_sess
->enabled
) {
5130 pthread_mutex_unlock(&ua_sess
->lock
);
5134 /* Upon restart, we skip the setup, already done */
5135 if (ua_sess
->started
) {
5139 health_code_update();
5142 /* This starts the UST tracing */
5143 pthread_mutex_lock(&app
->sock_lock
);
5144 ret
= lttng_ust_ctl_start_session(app
->sock
, ua_sess
->handle
);
5145 pthread_mutex_unlock(&app
->sock_lock
);
5147 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5148 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5149 app
->pid
, app
->sock
);
5150 pthread_mutex_unlock(&ua_sess
->lock
);
5152 } else if (ret
== -EAGAIN
) {
5153 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5154 app
->pid
, app
->sock
);
5155 pthread_mutex_unlock(&ua_sess
->lock
);
5159 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5160 ret
, app
->pid
, app
->sock
);
5165 /* Indicate that the session has been started once */
5166 ua_sess
->started
= 1;
5167 ua_sess
->enabled
= 1;
5169 pthread_mutex_unlock(&ua_sess
->lock
);
5171 health_code_update();
5173 /* Quiescent wait after starting trace */
5174 pthread_mutex_lock(&app
->sock_lock
);
5175 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5176 pthread_mutex_unlock(&app
->sock_lock
);
5178 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5179 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5180 app
->pid
, app
->sock
);
5181 } else if (ret
== -EAGAIN
) {
5182 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5183 app
->pid
, app
->sock
);
5185 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5186 ret
, app
->pid
, app
->sock
);
5192 health_code_update();
5196 pthread_mutex_unlock(&ua_sess
->lock
);
5198 health_code_update();
5203 * Stop tracing for a specific UST session and app.
5206 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5209 struct ust_app_session
*ua_sess
;
5210 ust_registry_session
*registry
;
5212 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5216 if (!app
->compatible
) {
5217 goto end_no_session
;
5220 ua_sess
= lookup_session_by_app(usess
, app
);
5221 if (ua_sess
== NULL
) {
5222 goto end_no_session
;
5225 pthread_mutex_lock(&ua_sess
->lock
);
5227 if (ua_sess
->deleted
) {
5228 pthread_mutex_unlock(&ua_sess
->lock
);
5229 goto end_no_session
;
5233 * If started = 0, it means that stop trace has been called for a session
5234 * that was never started. It's possible since we can have a fail start
5235 * from either the application manager thread or the command thread. Simply
5236 * indicate that this is a stop error.
5238 if (!ua_sess
->started
) {
5239 goto error_rcu_unlock
;
5242 health_code_update();
5244 /* This inhibits UST tracing */
5245 pthread_mutex_lock(&app
->sock_lock
);
5246 ret
= lttng_ust_ctl_stop_session(app
->sock
, ua_sess
->handle
);
5247 pthread_mutex_unlock(&app
->sock_lock
);
5249 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5250 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5251 app
->pid
, app
->sock
);
5253 } else if (ret
== -EAGAIN
) {
5254 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5255 app
->pid
, app
->sock
);
5259 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5260 ret
, app
->pid
, app
->sock
);
5262 goto error_rcu_unlock
;
5265 health_code_update();
5266 ua_sess
->enabled
= 0;
5268 /* Quiescent wait after stopping trace */
5269 pthread_mutex_lock(&app
->sock_lock
);
5270 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5271 pthread_mutex_unlock(&app
->sock_lock
);
5273 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5274 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5275 app
->pid
, app
->sock
);
5276 } else if (ret
== -EAGAIN
) {
5277 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5278 app
->pid
, app
->sock
);
5280 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5281 ret
, app
->pid
, app
->sock
);
5285 health_code_update();
5287 registry
= get_session_registry(ua_sess
);
5289 /* The UST app session is held registry shall not be null. */
5290 LTTNG_ASSERT(registry
);
5292 /* Push metadata for application before freeing the application. */
5293 (void) push_metadata(registry
, ua_sess
->consumer
);
5296 pthread_mutex_unlock(&ua_sess
->lock
);
5299 health_code_update();
5303 pthread_mutex_unlock(&ua_sess
->lock
);
5305 health_code_update();
5310 int ust_app_flush_app_session(struct ust_app
*app
,
5311 struct ust_app_session
*ua_sess
)
5313 int ret
, retval
= 0;
5314 struct lttng_ht_iter iter
;
5315 struct ust_app_channel
*ua_chan
;
5316 struct consumer_socket
*socket
;
5318 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5322 if (!app
->compatible
) {
5323 goto end_not_compatible
;
5326 pthread_mutex_lock(&ua_sess
->lock
);
5328 if (ua_sess
->deleted
) {
5332 health_code_update();
5334 /* Flushing buffers */
5335 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5338 /* Flush buffers and push metadata. */
5339 switch (ua_sess
->buffer_type
) {
5340 case LTTNG_BUFFER_PER_PID
:
5341 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5343 health_code_update();
5344 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5346 ERR("Error flushing consumer channel");
5352 case LTTNG_BUFFER_PER_UID
:
5358 health_code_update();
5361 pthread_mutex_unlock(&ua_sess
->lock
);
5365 health_code_update();
5370 * Flush buffers for all applications for a specific UST session.
5371 * Called with UST session lock held.
5374 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5379 DBG("Flushing session buffers for all ust apps");
5383 /* Flush buffers and push metadata. */
5384 switch (usess
->buffer_type
) {
5385 case LTTNG_BUFFER_PER_UID
:
5387 struct buffer_reg_uid
*reg
;
5388 struct lttng_ht_iter iter
;
5390 /* Flush all per UID buffers associated to that session. */
5391 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5392 ust_registry_session
*ust_session_reg
;
5393 struct buffer_reg_channel
*buf_reg_chan
;
5394 struct consumer_socket
*socket
;
5396 /* Get consumer socket to use to push the metadata.*/
5397 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5400 /* Ignore request if no consumer is found for the session. */
5404 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5405 buf_reg_chan
, node
.node
) {
5407 * The following call will print error values so the return
5408 * code is of little importance because whatever happens, we
5409 * have to try them all.
5411 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5414 ust_session_reg
= reg
->registry
->reg
.ust
;
5415 /* Push metadata. */
5416 (void) push_metadata(ust_session_reg
, usess
->consumer
);
5420 case LTTNG_BUFFER_PER_PID
:
5422 struct ust_app_session
*ua_sess
;
5423 struct lttng_ht_iter iter
;
5424 struct ust_app
*app
;
5426 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5427 ua_sess
= lookup_session_by_app(usess
, app
);
5428 if (ua_sess
== NULL
) {
5431 (void) ust_app_flush_app_session(app
, ua_sess
);
5442 health_code_update();
5447 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5448 struct ust_app_session
*ua_sess
)
5451 struct lttng_ht_iter iter
;
5452 struct ust_app_channel
*ua_chan
;
5453 struct consumer_socket
*socket
;
5455 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5459 if (!app
->compatible
) {
5460 goto end_not_compatible
;
5463 pthread_mutex_lock(&ua_sess
->lock
);
5465 if (ua_sess
->deleted
) {
5469 health_code_update();
5471 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5474 ERR("Failed to find consumer (%" PRIu32
") socket",
5475 app
->bits_per_long
);
5480 /* Clear quiescent state. */
5481 switch (ua_sess
->buffer_type
) {
5482 case LTTNG_BUFFER_PER_PID
:
5483 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5484 ua_chan
, node
.node
) {
5485 health_code_update();
5486 ret
= consumer_clear_quiescent_channel(socket
,
5489 ERR("Error clearing quiescent state for consumer channel");
5495 case LTTNG_BUFFER_PER_UID
:
5502 health_code_update();
5505 pthread_mutex_unlock(&ua_sess
->lock
);
5509 health_code_update();
5514 * Clear quiescent state in each stream for all applications for a
5515 * specific UST session.
5516 * Called with UST session lock held.
5519 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5524 DBG("Clearing stream quiescent state for all ust apps");
5528 switch (usess
->buffer_type
) {
5529 case LTTNG_BUFFER_PER_UID
:
5531 struct lttng_ht_iter iter
;
5532 struct buffer_reg_uid
*reg
;
5535 * Clear quiescent for all per UID buffers associated to
5538 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5539 struct consumer_socket
*socket
;
5540 struct buffer_reg_channel
*buf_reg_chan
;
5542 /* Get associated consumer socket.*/
5543 socket
= consumer_find_socket_by_bitness(
5544 reg
->bits_per_long
, usess
->consumer
);
5547 * Ignore request if no consumer is found for
5553 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5554 &iter
.iter
, buf_reg_chan
, node
.node
) {
5556 * The following call will print error values so
5557 * the return code is of little importance
5558 * because whatever happens, we have to try them
5561 (void) consumer_clear_quiescent_channel(socket
,
5562 buf_reg_chan
->consumer_key
);
5567 case LTTNG_BUFFER_PER_PID
:
5569 struct ust_app_session
*ua_sess
;
5570 struct lttng_ht_iter iter
;
5571 struct ust_app
*app
;
5573 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5575 ua_sess
= lookup_session_by_app(usess
, app
);
5576 if (ua_sess
== NULL
) {
5579 (void) ust_app_clear_quiescent_app_session(app
,
5591 health_code_update();
5596 * Destroy a specific UST session in apps.
5598 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5601 struct ust_app_session
*ua_sess
;
5602 struct lttng_ht_iter iter
;
5603 struct lttng_ht_node_u64
*node
;
5605 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5609 if (!app
->compatible
) {
5613 __lookup_session_by_app(usess
, app
, &iter
);
5614 node
= lttng_ht_iter_get_node_u64(&iter
);
5616 /* Session is being or is deleted. */
5619 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
5621 health_code_update();
5622 destroy_app_session(app
, ua_sess
);
5624 health_code_update();
5626 /* Quiescent wait after stopping trace */
5627 pthread_mutex_lock(&app
->sock_lock
);
5628 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5629 pthread_mutex_unlock(&app
->sock_lock
);
5631 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5632 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5633 app
->pid
, app
->sock
);
5634 } else if (ret
== -EAGAIN
) {
5635 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5636 app
->pid
, app
->sock
);
5638 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5639 ret
, app
->pid
, app
->sock
);
5644 health_code_update();
5649 * Start tracing for the UST session.
5651 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5653 struct lttng_ht_iter iter
;
5654 struct ust_app
*app
;
5656 DBG("Starting all UST traces");
5659 * Even though the start trace might fail, flag this session active so
5660 * other application coming in are started by default.
5667 * In a start-stop-start use-case, we need to clear the quiescent state
5668 * of each channel set by the prior stop command, thus ensuring that a
5669 * following stop or destroy is sure to grab a timestamp_end near those
5670 * operations, even if the packet is empty.
5672 (void) ust_app_clear_quiescent_session(usess
);
5674 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5675 ust_app_global_update(usess
, app
);
5684 * Start tracing for the UST session.
5685 * Called with UST session lock held.
5687 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5690 struct lttng_ht_iter iter
;
5691 struct ust_app
*app
;
5693 DBG("Stopping all UST traces");
5696 * Even though the stop trace might fail, flag this session inactive so
5697 * other application coming in are not started by default.
5703 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5704 ret
= ust_app_stop_trace(usess
, app
);
5706 /* Continue to next apps even on error */
5711 (void) ust_app_flush_session(usess
);
5719 * Destroy app UST session.
5721 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5724 struct lttng_ht_iter iter
;
5725 struct ust_app
*app
;
5727 DBG("Destroy all UST traces");
5731 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5732 ret
= destroy_trace(usess
, app
);
5734 /* Continue to next apps even on error */
5744 /* The ua_sess lock must be held by the caller. */
5746 int find_or_create_ust_app_channel(
5747 struct ltt_ust_session
*usess
,
5748 struct ust_app_session
*ua_sess
,
5749 struct ust_app
*app
,
5750 struct ltt_ust_channel
*uchan
,
5751 struct ust_app_channel
**ua_chan
)
5754 struct lttng_ht_iter iter
;
5755 struct lttng_ht_node_str
*ua_chan_node
;
5757 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5758 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5760 *ua_chan
= caa_container_of(ua_chan_node
,
5761 struct ust_app_channel
, node
);
5765 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5774 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5775 struct ltt_ust_event
*uevent
,
5776 struct ust_app
*app
)
5779 struct ust_app_event
*ua_event
= NULL
;
5781 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5782 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5784 ret
= create_ust_app_event(ua_chan
, uevent
, app
);
5789 if (ua_event
->enabled
!= uevent
->enabled
) {
5790 ret
= uevent
->enabled
?
5791 enable_ust_app_event(ua_event
, app
) :
5792 disable_ust_app_event(ua_event
, app
);
5800 /* Called with RCU read-side lock held. */
5802 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5805 enum lttng_error_code ret_code
;
5806 enum lttng_trigger_status t_status
;
5807 struct lttng_ht_iter app_trigger_iter
;
5808 struct lttng_triggers
*triggers
= NULL
;
5809 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5810 unsigned int count
, i
;
5812 ASSERT_RCU_READ_LOCKED();
5814 if (!ust_app_supports_notifiers(app
)) {
5819 * Currrently, registering or unregistering a trigger with an
5820 * event rule condition causes a full synchronization of the event
5823 * The first step attempts to add an event notifier for all registered
5824 * triggers that apply to the user space tracers. Then, the
5825 * application's event notifiers rules are all checked against the list
5826 * of registered triggers. Any event notifier that doesn't have a
5827 * matching trigger can be assumed to have been disabled.
5829 * All of this is inefficient, but is put in place to get the feature
5830 * rolling as it is simpler at this moment. It will be optimized Soon™
5831 * to allow the state of enabled
5832 * event notifiers to be synchronized in a piece-wise way.
5835 /* Get all triggers using uid 0 (root) */
5836 ret_code
= notification_thread_command_list_triggers(
5837 the_notification_thread_handle
, 0, &triggers
);
5838 if (ret_code
!= LTTNG_OK
) {
5842 LTTNG_ASSERT(triggers
);
5844 t_status
= lttng_triggers_get_count(triggers
, &count
);
5845 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5849 for (i
= 0; i
< count
; i
++) {
5850 struct lttng_condition
*condition
;
5851 struct lttng_event_rule
*event_rule
;
5852 struct lttng_trigger
*trigger
;
5853 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5854 enum lttng_condition_status condition_status
;
5857 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5858 LTTNG_ASSERT(trigger
);
5860 token
= lttng_trigger_get_tracer_token(trigger
);
5861 condition
= lttng_trigger_get_condition(trigger
);
5863 if (lttng_condition_get_type(condition
) !=
5864 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
) {
5865 /* Does not apply */
5870 lttng_condition_event_rule_matches_borrow_rule_mutable(
5871 condition
, &event_rule
);
5872 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5874 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5875 /* Skip kernel related triggers. */
5880 * Find or create the associated token event rule. The caller
5881 * holds the RCU read lock, so this is safe to call without
5882 * explicitly acquiring it here.
5884 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
5885 app
->token_to_event_notifier_rule_ht
, token
);
5886 if (!looked_up_event_notifier_rule
) {
5887 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
5895 /* Remove all unknown event sources from the app. */
5896 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
5897 &app_trigger_iter
.iter
, event_notifier_rule
,
5899 const uint64_t app_token
= event_notifier_rule
->token
;
5903 * Check if the app event trigger still exists on the
5904 * notification side.
5906 for (i
= 0; i
< count
; i
++) {
5907 uint64_t notification_thread_token
;
5908 const struct lttng_trigger
*trigger
=
5909 lttng_triggers_get_at_index(
5912 LTTNG_ASSERT(trigger
);
5914 notification_thread_token
=
5915 lttng_trigger_get_tracer_token(trigger
);
5917 if (notification_thread_token
== app_token
) {
5929 * This trigger was unregistered, disable it on the tracer's
5932 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
5934 LTTNG_ASSERT(ret
== 0);
5936 /* Callee logs errors. */
5937 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
5939 delete_ust_app_event_notifier_rule(
5940 app
->sock
, event_notifier_rule
, app
);
5946 lttng_triggers_destroy(triggers
);
5951 * RCU read lock must be held by the caller.
5954 void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
5955 struct ust_app_session
*ua_sess
,
5956 struct ust_app
*app
)
5959 struct cds_lfht_iter uchan_iter
;
5960 struct ltt_ust_channel
*uchan
;
5962 LTTNG_ASSERT(usess
);
5963 LTTNG_ASSERT(ua_sess
);
5965 ASSERT_RCU_READ_LOCKED();
5967 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5969 struct ust_app_channel
*ua_chan
;
5970 struct cds_lfht_iter uevent_iter
;
5971 struct ltt_ust_event
*uevent
;
5974 * Search for a matching ust_app_channel. If none is found,
5975 * create it. Creating the channel will cause the ua_chan
5976 * structure to be allocated, the channel buffers to be
5977 * allocated (if necessary) and sent to the application, and
5978 * all enabled contexts will be added to the channel.
5980 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5981 app
, uchan
, &ua_chan
);
5983 /* Tracer is probably gone or ENOMEM. */
5988 /* ua_chan will be NULL for the metadata channel */
5992 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5994 ret
= ust_app_channel_synchronize_event(ua_chan
,
6001 if (ua_chan
->enabled
!= uchan
->enabled
) {
6002 ret
= uchan
->enabled
?
6003 enable_ust_app_channel(ua_sess
, uchan
, app
) :
6004 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
6015 * The caller must ensure that the application is compatible and is tracked
6016 * by the process attribute trackers.
6019 void ust_app_synchronize(struct ltt_ust_session
*usess
,
6020 struct ust_app
*app
)
6023 struct ust_app_session
*ua_sess
= NULL
;
6026 * The application's configuration should only be synchronized for
6029 LTTNG_ASSERT(usess
->active
);
6031 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
6033 /* Tracer is probably gone or ENOMEM. */
6035 destroy_app_session(app
, ua_sess
);
6039 LTTNG_ASSERT(ua_sess
);
6041 pthread_mutex_lock(&ua_sess
->lock
);
6042 if (ua_sess
->deleted
) {
6043 goto deleted_session
;
6048 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
6051 * Create the metadata for the application. This returns gracefully if a
6052 * metadata was already set for the session.
6054 * The metadata channel must be created after the data channels as the
6055 * consumer daemon assumes this ordering. When interacting with a relay
6056 * daemon, the consumer will use this assumption to send the
6057 * "STREAMS_SENT" message to the relay daemon.
6059 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
6061 ERR("Metadata creation failed for app sock %d for session id %" PRIu64
,
6062 app
->sock
, usess
->id
);
6068 pthread_mutex_unlock(&ua_sess
->lock
);
6074 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6076 struct ust_app_session
*ua_sess
;
6078 ua_sess
= lookup_session_by_app(usess
, app
);
6079 if (ua_sess
== NULL
) {
6082 destroy_app_session(app
, ua_sess
);
6086 * Add channels/events from UST global domain to registered apps at sock.
6088 * Called with session lock held.
6089 * Called with RCU read-side lock held.
6091 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6093 LTTNG_ASSERT(usess
);
6094 LTTNG_ASSERT(usess
->active
);
6095 ASSERT_RCU_READ_LOCKED();
6097 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
6098 app
->sock
, usess
->id
);
6100 if (!app
->compatible
) {
6103 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
6105 trace_ust_id_tracker_lookup(
6106 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
6108 trace_ust_id_tracker_lookup(
6109 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
6112 * Synchronize the application's internal tracing configuration
6113 * and start tracing.
6115 ust_app_synchronize(usess
, app
);
6116 ust_app_start_trace(usess
, app
);
6118 ust_app_global_destroy(usess
, app
);
6123 * Add all event notifiers to an application.
6125 * Called with session lock held.
6126 * Called with RCU read-side lock held.
6128 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
6130 ASSERT_RCU_READ_LOCKED();
6132 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6133 app
->name
, app
->pid
);
6135 if (!app
->compatible
|| !ust_app_supports_notifiers(app
)) {
6139 if (app
->event_notifier_group
.object
== NULL
) {
6140 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6141 app
->name
, app
->pid
);
6145 ust_app_synchronize_event_notifier_rules(app
);
6149 * Called with session lock held.
6151 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
6153 struct lttng_ht_iter iter
;
6154 struct ust_app
*app
;
6157 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6158 ust_app_global_update(usess
, app
);
6163 void ust_app_global_update_all_event_notifier_rules(void)
6165 struct lttng_ht_iter iter
;
6166 struct ust_app
*app
;
6169 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6170 ust_app_global_update_event_notifier_rules(app
);
6177 * Add context to a specific channel for global UST domain.
6179 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
6180 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
6183 struct lttng_ht_node_str
*ua_chan_node
;
6184 struct lttng_ht_iter iter
, uiter
;
6185 struct ust_app_channel
*ua_chan
= NULL
;
6186 struct ust_app_session
*ua_sess
;
6187 struct ust_app
*app
;
6189 LTTNG_ASSERT(usess
->active
);
6192 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6193 if (!app
->compatible
) {
6195 * TODO: In time, we should notice the caller of this error by
6196 * telling him that this is a version error.
6200 ua_sess
= lookup_session_by_app(usess
, app
);
6201 if (ua_sess
== NULL
) {
6205 pthread_mutex_lock(&ua_sess
->lock
);
6207 if (ua_sess
->deleted
) {
6208 pthread_mutex_unlock(&ua_sess
->lock
);
6212 /* Lookup channel in the ust app session */
6213 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
6214 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6215 if (ua_chan_node
== NULL
) {
6218 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
6220 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6225 pthread_mutex_unlock(&ua_sess
->lock
);
6233 * Receive registration and populate the given msg structure.
6235 * On success return 0 else a negative value returned by the ustctl call.
6237 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6240 uint32_t pid
, ppid
, uid
, gid
;
6244 ret
= lttng_ust_ctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
6245 &pid
, &ppid
, &uid
, &gid
,
6246 &msg
->bits_per_long
,
6247 &msg
->uint8_t_alignment
,
6248 &msg
->uint16_t_alignment
,
6249 &msg
->uint32_t_alignment
,
6250 &msg
->uint64_t_alignment
,
6251 &msg
->long_alignment
,
6258 case LTTNG_UST_ERR_EXITING
:
6259 DBG3("UST app recv reg message failed. Application died");
6261 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6262 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6263 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
6264 LTTNG_UST_ABI_MINOR_VERSION
);
6267 ERR("UST app recv reg message failed with ret %d", ret
);
6272 msg
->pid
= (pid_t
) pid
;
6273 msg
->ppid
= (pid_t
) ppid
;
6274 msg
->uid
= (uid_t
) uid
;
6275 msg
->gid
= (gid_t
) gid
;
6282 * Return a ust app session object using the application object and the
6283 * session object descriptor has a key. If not found, NULL is returned.
6284 * A RCU read side lock MUST be acquired when calling this function.
6286 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
6289 struct lttng_ht_node_ulong
*node
;
6290 struct lttng_ht_iter iter
;
6291 struct ust_app_session
*ua_sess
= NULL
;
6294 ASSERT_RCU_READ_LOCKED();
6296 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
6297 node
= lttng_ht_iter_get_node_ulong(&iter
);
6299 DBG2("UST app session find by objd %d not found", objd
);
6303 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
6310 * Return a ust app channel object using the application object and the channel
6311 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6312 * lock MUST be acquired before calling this function.
6314 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
6317 struct lttng_ht_node_ulong
*node
;
6318 struct lttng_ht_iter iter
;
6319 struct ust_app_channel
*ua_chan
= NULL
;
6322 ASSERT_RCU_READ_LOCKED();
6324 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
6325 node
= lttng_ht_iter_get_node_ulong(&iter
);
6327 DBG2("UST app channel find by objd %d not found", objd
);
6331 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
6338 * Fixup legacy context fields for comparison:
6339 * - legacy array becomes array_nestable,
6340 * - legacy struct becomes struct_nestable,
6341 * - legacy variant becomes variant_nestable,
6342 * legacy sequences are not emitted in LTTng-UST contexts.
6344 static int ust_app_fixup_legacy_context_fields(size_t *_nr_fields
,
6345 struct lttng_ust_ctl_field
**_fields
)
6347 struct lttng_ust_ctl_field
*fields
= *_fields
, *new_fields
= NULL
;
6348 size_t nr_fields
= *_nr_fields
, new_nr_fields
= 0, i
, j
;
6352 for (i
= 0; i
< nr_fields
; i
++) {
6353 const struct lttng_ust_ctl_field
*field
= &fields
[i
];
6355 switch (field
->type
.atype
) {
6356 case lttng_ust_ctl_atype_sequence
:
6357 ERR("Unexpected legacy sequence context.");
6360 case lttng_ust_ctl_atype_array
:
6361 switch (field
->type
.u
.legacy
.array
.elem_type
.atype
) {
6362 case lttng_ust_ctl_atype_integer
:
6365 ERR("Unexpected legacy array element type in context.");
6370 /* One field for array_nested, one field for elem type. */
6374 case lttng_ust_ctl_atype_struct
: /* Fallthrough */
6375 case lttng_ust_ctl_atype_variant
:
6388 new_fields
= calloc
<lttng_ust_ctl_field
>(new_nr_fields
);
6394 for (i
= 0, j
= 0; i
< nr_fields
; i
++, j
++) {
6395 const struct lttng_ust_ctl_field
*field
= &fields
[i
];
6396 struct lttng_ust_ctl_field
*new_field
= &new_fields
[j
];
6398 switch (field
->type
.atype
) {
6399 case lttng_ust_ctl_atype_array
:
6400 /* One field for array_nested, one field for elem type. */
6401 strncpy(new_field
->name
, field
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
6402 new_field
->type
.atype
= lttng_ust_ctl_atype_array_nestable
;
6403 new_field
->type
.u
.array_nestable
.length
= field
->type
.u
.legacy
.array
.length
;
6404 new_field
->type
.u
.array_nestable
.alignment
= 0;
6405 new_field
= &new_fields
[++j
]; /* elem type */
6406 new_field
->type
.atype
= field
->type
.u
.legacy
.array
.elem_type
.atype
;
6407 assert(new_field
->type
.atype
== lttng_ust_ctl_atype_integer
);
6408 new_field
->type
.u
.integer
= field
->type
.u
.legacy
.array
.elem_type
.u
.basic
.integer
;
6410 case lttng_ust_ctl_atype_struct
:
6411 strncpy(new_field
->name
, field
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
6412 new_field
->type
.atype
= lttng_ust_ctl_atype_struct_nestable
;
6413 new_field
->type
.u
.struct_nestable
.nr_fields
= field
->type
.u
.legacy
._struct
.nr_fields
;
6414 new_field
->type
.u
.struct_nestable
.alignment
= 0;
6416 case lttng_ust_ctl_atype_variant
:
6417 strncpy(new_field
->name
, field
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
6418 new_field
->type
.atype
= lttng_ust_ctl_atype_variant_nestable
;
6419 new_field
->type
.u
.variant_nestable
.nr_choices
= field
->type
.u
.legacy
.variant
.nr_choices
;
6420 strncpy(new_field
->type
.u
.variant_nestable
.tag_name
,
6421 field
->type
.u
.legacy
.variant
.tag_name
,
6422 LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
6423 new_field
->type
.u
.variant_nestable
.alignment
= 0;
6426 *new_field
= *field
;
6431 *_fields
= new_fields
;
6432 *_nr_fields
= new_nr_fields
;
6438 * Reply to a register channel notification from an application on the notify
6439 * socket. The channel metadata is also created.
6441 * The session UST registry lock is acquired in this function.
6443 * On success 0 is returned else a negative value.
6445 static int reply_ust_register_channel(int sock
, int cobjd
,
6446 size_t nr_fields
, struct lttng_ust_ctl_field
*fields
)
6448 int ret
, ret_code
= 0;
6450 uint64_t chan_reg_key
;
6451 enum lttng_ust_ctl_channel_header type
= LTTNG_UST_CTL_CHANNEL_HEADER_UNKNOWN
;
6452 struct ust_app
*app
;
6453 struct ust_app_channel
*ua_chan
;
6454 struct ust_app_session
*ua_sess
;
6455 ust_registry_session
*registry
;
6456 struct ust_registry_channel
*ust_reg_chan
;
6460 /* Lookup application. If not found, there is a code flow error. */
6461 app
= find_app_by_notify_sock(sock
);
6463 DBG("Application socket %d is being torn down. Abort event notify",
6466 goto error_rcu_unlock
;
6469 /* Lookup channel by UST object descriptor. */
6470 ua_chan
= find_channel_by_objd(app
, cobjd
);
6472 DBG("Application channel is being torn down. Abort event notify");
6474 goto error_rcu_unlock
;
6477 LTTNG_ASSERT(ua_chan
->session
);
6478 ua_sess
= ua_chan
->session
;
6480 /* Get right session registry depending on the session buffer type. */
6481 registry
= get_session_registry(ua_sess
);
6483 DBG("Application session is being torn down. Abort event notify");
6485 goto error_rcu_unlock
;
6488 /* Depending on the buffer type, a different channel key is used. */
6489 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6490 chan_reg_key
= ua_chan
->tracing_channel_id
;
6492 chan_reg_key
= ua_chan
->key
;
6495 pthread_mutex_lock(®istry
->_lock
);
6497 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
6498 LTTNG_ASSERT(ust_reg_chan
);
6500 /* Channel id is set during the object creation. */
6501 chan_id
= ust_reg_chan
->chan_id
;
6503 ret
= ust_app_fixup_legacy_context_fields(&nr_fields
, &fields
);
6505 ERR("Registering application channel due to legacy context fields fixup error: pid = %d, sock = %d",
6506 app
->pid
, app
->sock
);
6510 if (!ust_reg_chan
->register_done
) {
6512 * TODO: eventually use the registry event count for
6513 * this channel to better guess header type for per-pid
6516 type
= LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
;
6517 ust_reg_chan
->nr_ctx_fields
= nr_fields
;
6518 ust_reg_chan
->ctx_fields
= fields
;
6520 ust_reg_chan
->header_type
= type
;
6522 /* Get current already assigned values. */
6523 type
= ust_reg_chan
->header_type
;
6525 * Validate that the context fields match between
6526 * registry and newcoming application.
6528 if (!match_lttng_ust_ctl_field_array(ust_reg_chan
->ctx_fields
,
6529 ust_reg_chan
->nr_ctx_fields
,
6530 fields
, nr_fields
)) {
6531 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6532 app
->pid
, app
->sock
);
6538 /* Append to metadata */
6539 if (!ust_reg_chan
->metadata_dumped
) {
6540 ret_code
= ust_metadata_channel_statedump(registry
, ust_reg_chan
);
6542 ERR("Error appending channel metadata (errno = %d)", ret_code
);
6548 DBG3("UST app replying to register channel key %" PRIu64
6549 " with id %u, type = %d, ret = %d", chan_reg_key
, chan_id
, type
,
6552 ret
= lttng_ust_ctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
6554 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6555 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6556 app
->pid
, app
->sock
);
6557 } else if (ret
== -EAGAIN
) {
6558 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6559 app
->pid
, app
->sock
);
6561 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6562 ret
, app
->pid
, app
->sock
);
6567 /* This channel registry registration is completed. */
6568 ust_reg_chan
->register_done
= 1;
6571 pthread_mutex_unlock(®istry
->_lock
);
6579 * Add event to the UST channel registry. When the event is added to the
6580 * registry, the metadata is also created. Once done, this replies to the
6581 * application with the appropriate error code.
6583 * The session UST registry lock is acquired in the function.
6585 * On success 0 is returned else a negative value.
6587 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
6588 char *sig
, size_t nr_fields
, struct lttng_ust_ctl_field
*fields
,
6589 int loglevel_value
, char *model_emf_uri
)
6592 uint32_t event_id
= 0;
6593 uint64_t chan_reg_key
;
6594 struct ust_app
*app
;
6595 struct ust_app_channel
*ua_chan
;
6596 struct ust_app_session
*ua_sess
;
6597 ust_registry_session
*registry
;
6601 /* Lookup application. If not found, there is a code flow error. */
6602 app
= find_app_by_notify_sock(sock
);
6604 DBG("Application socket %d is being torn down. Abort event notify",
6607 goto error_rcu_unlock
;
6610 /* Lookup channel by UST object descriptor. */
6611 ua_chan
= find_channel_by_objd(app
, cobjd
);
6613 DBG("Application channel is being torn down. Abort event notify");
6615 goto error_rcu_unlock
;
6618 LTTNG_ASSERT(ua_chan
->session
);
6619 ua_sess
= ua_chan
->session
;
6621 registry
= get_session_registry(ua_sess
);
6623 DBG("Application session is being torn down. Abort event notify");
6625 goto error_rcu_unlock
;
6628 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6629 chan_reg_key
= ua_chan
->tracing_channel_id
;
6631 chan_reg_key
= ua_chan
->key
;
6634 pthread_mutex_lock(®istry
->_lock
);
6637 * From this point on, this call acquires the ownership of the sig, fields
6638 * and model_emf_uri meaning any free are done inside it if needed. These
6639 * three variables MUST NOT be read/write after this.
6641 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
6642 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
6643 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
6647 model_emf_uri
= NULL
;
6650 * The return value is returned to ustctl so in case of an error, the
6651 * application can be notified. In case of an error, it's important not to
6652 * return a negative error or else the application will get closed.
6654 ret
= lttng_ust_ctl_reply_register_event(sock
, event_id
, ret_code
);
6656 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6657 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6658 app
->pid
, app
->sock
);
6659 } else if (ret
== -EAGAIN
) {
6660 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6661 app
->pid
, app
->sock
);
6663 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6664 ret
, app
->pid
, app
->sock
);
6667 * No need to wipe the create event since the application socket will
6668 * get close on error hence cleaning up everything by itself.
6673 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6677 pthread_mutex_unlock(®istry
->_lock
);
6682 free(model_emf_uri
);
6687 * Add enum to the UST session registry. Once done, this replies to the
6688 * application with the appropriate error code.
6690 * The session UST registry lock is acquired within this function.
6692 * On success 0 is returned else a negative value.
6694 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
6695 struct lttng_ust_ctl_enum_entry
*entries
, size_t nr_entries
)
6697 int ret
= 0, ret_code
;
6698 struct ust_app
*app
;
6699 struct ust_app_session
*ua_sess
;
6700 ust_registry_session
*registry
;
6701 uint64_t enum_id
= -1ULL;
6705 /* Lookup application. If not found, there is a code flow error. */
6706 app
= find_app_by_notify_sock(sock
);
6708 /* Return an error since this is not an error */
6709 DBG("Application socket %d is being torn down. Aborting enum registration",
6713 goto error_rcu_unlock
;
6716 /* Lookup session by UST object descriptor. */
6717 ua_sess
= find_session_by_objd(app
, sobjd
);
6719 /* Return an error since this is not an error */
6720 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6722 goto error_rcu_unlock
;
6725 registry
= get_session_registry(ua_sess
);
6727 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6729 goto error_rcu_unlock
;
6732 pthread_mutex_lock(®istry
->_lock
);
6735 * From this point on, the callee acquires the ownership of
6736 * entries. The variable entries MUST NOT be read/written after
6739 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
6740 entries
, nr_entries
, &enum_id
);
6744 * The return value is returned to ustctl so in case of an error, the
6745 * application can be notified. In case of an error, it's important not to
6746 * return a negative error or else the application will get closed.
6748 ret
= lttng_ust_ctl_reply_register_enum(sock
, enum_id
, ret_code
);
6750 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6751 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6752 app
->pid
, app
->sock
);
6753 } else if (ret
== -EAGAIN
) {
6754 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6755 app
->pid
, app
->sock
);
6757 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6758 ret
, app
->pid
, app
->sock
);
6761 * No need to wipe the create enum since the application socket will
6762 * get close on error hence cleaning up everything by itself.
6767 DBG3("UST registry enum %s added successfully or already found", name
);
6770 pthread_mutex_unlock(®istry
->_lock
);
6777 * Handle application notification through the given notify socket.
6779 * Return 0 on success or else a negative value.
6781 int ust_app_recv_notify(int sock
)
6784 enum lttng_ust_ctl_notify_cmd cmd
;
6786 DBG3("UST app receiving notify from sock %d", sock
);
6788 ret
= lttng_ust_ctl_recv_notify(sock
, &cmd
);
6790 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6791 DBG3("UST app recv notify failed. Application died: sock = %d",
6793 } else if (ret
== -EAGAIN
) {
6794 WARN("UST app recv notify failed. Communication time out: sock = %d",
6797 ERR("UST app recv notify failed with ret %d: sock = %d",
6804 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT
:
6806 int sobjd
, cobjd
, loglevel_value
;
6807 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6809 struct lttng_ust_ctl_field
*fields
;
6811 DBG2("UST app ustctl register event received");
6813 ret
= lttng_ust_ctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
6814 &loglevel_value
, &sig
, &nr_fields
, &fields
,
6817 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6818 DBG3("UST app recv event failed. Application died: sock = %d",
6820 } else if (ret
== -EAGAIN
) {
6821 WARN("UST app recv event failed. Communication time out: sock = %d",
6824 ERR("UST app recv event failed with ret %d: sock = %d",
6831 * Add event to the UST registry coming from the notify socket. This
6832 * call will free if needed the sig, fields and model_emf_uri. This
6833 * code path loses the ownsership of these variables and transfer them
6834 * to the this function.
6836 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6837 fields
, loglevel_value
, model_emf_uri
);
6844 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
:
6848 struct lttng_ust_ctl_field
*fields
;
6850 DBG2("UST app ustctl register channel received");
6852 ret
= lttng_ust_ctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
6855 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6856 DBG3("UST app recv channel failed. Application died: sock = %d",
6858 } else if (ret
== -EAGAIN
) {
6859 WARN("UST app recv channel failed. Communication time out: sock = %d",
6862 ERR("UST app recv channel failed with ret %d: sock = %d",
6869 * The fields ownership are transfered to this function call meaning
6870 * that if needed it will be freed. After this, it's invalid to access
6871 * fields or clean it up.
6873 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
6881 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM
:
6884 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
6886 struct lttng_ust_ctl_enum_entry
*entries
;
6888 DBG2("UST app ustctl register enum received");
6890 ret
= lttng_ust_ctl_recv_register_enum(sock
, &sobjd
, name
,
6891 &entries
, &nr_entries
);
6893 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6894 DBG3("UST app recv enum failed. Application died: sock = %d",
6896 } else if (ret
== -EAGAIN
) {
6897 WARN("UST app recv enum failed. Communication time out: sock = %d",
6900 ERR("UST app recv enum failed with ret %d: sock = %d",
6906 /* Callee assumes ownership of entries */
6907 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6908 entries
, nr_entries
);
6916 /* Should NEVER happen. */
6925 * Once the notify socket hangs up, this is called. First, it tries to find the
6926 * corresponding application. On failure, the call_rcu to close the socket is
6927 * executed. If an application is found, it tries to delete it from the notify
6928 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6930 * Note that an object needs to be allocated here so on ENOMEM failure, the
6931 * call RCU is not done but the rest of the cleanup is.
6933 void ust_app_notify_sock_unregister(int sock
)
6936 struct lttng_ht_iter iter
;
6937 struct ust_app
*app
;
6938 struct ust_app_notify_sock_obj
*obj
;
6940 LTTNG_ASSERT(sock
>= 0);
6944 obj
= zmalloc
<ust_app_notify_sock_obj
>();
6947 * An ENOMEM is kind of uncool. If this strikes we continue the
6948 * procedure but the call_rcu will not be called. In this case, we
6949 * accept the fd leak rather than possibly creating an unsynchronized
6950 * state between threads.
6952 * TODO: The notify object should be created once the notify socket is
6953 * registered and stored independantely from the ust app object. The
6954 * tricky part is to synchronize the teardown of the application and
6955 * this notify object. Let's keep that in mind so we can avoid this
6956 * kind of shenanigans with ENOMEM in the teardown path.
6963 DBG("UST app notify socket unregister %d", sock
);
6966 * Lookup application by notify socket. If this fails, this means that the
6967 * hash table delete has already been done by the application
6968 * unregistration process so we can safely close the notify socket in a
6971 app
= find_app_by_notify_sock(sock
);
6976 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6979 * Whatever happens here either we fail or succeed, in both cases we have
6980 * to close the socket after a grace period to continue to the call RCU
6981 * here. If the deletion is successful, the application is not visible
6982 * anymore by other threads and is it fails it means that it was already
6983 * deleted from the hash table so either way we just have to close the
6986 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6992 * Close socket after a grace period to avoid for the socket to be reused
6993 * before the application object is freed creating potential race between
6994 * threads trying to add unique in the global hash table.
6997 call_rcu(&obj
->head
, close_notify_sock_rcu
);
7002 * Destroy a ust app data structure and free its memory.
7004 void ust_app_destroy(struct ust_app
*app
)
7010 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
7014 * Take a snapshot for a given UST session. The snapshot is sent to the given
7017 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
7019 enum lttng_error_code
ust_app_snapshot_record(
7020 const struct ltt_ust_session
*usess
,
7021 const struct consumer_output
*output
,
7022 uint64_t nb_packets_per_stream
)
7025 enum lttng_error_code status
= LTTNG_OK
;
7026 struct lttng_ht_iter iter
;
7027 struct ust_app
*app
;
7028 char *trace_path
= NULL
;
7030 LTTNG_ASSERT(usess
);
7031 LTTNG_ASSERT(output
);
7035 switch (usess
->buffer_type
) {
7036 case LTTNG_BUFFER_PER_UID
:
7038 struct buffer_reg_uid
*reg
;
7040 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7041 struct buffer_reg_channel
*buf_reg_chan
;
7042 struct consumer_socket
*socket
;
7043 char pathname
[PATH_MAX
];
7044 size_t consumer_path_offset
= 0;
7046 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7047 /* Skip since no metadata is present */
7051 /* Get consumer socket to use to push the metadata.*/
7052 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7055 status
= LTTNG_ERR_INVALID
;
7059 memset(pathname
, 0, sizeof(pathname
));
7060 ret
= snprintf(pathname
, sizeof(pathname
),
7061 DEFAULT_UST_TRACE_UID_PATH
,
7062 reg
->uid
, reg
->bits_per_long
);
7064 PERROR("snprintf snapshot path");
7065 status
= LTTNG_ERR_INVALID
;
7068 /* Free path allowed on previous iteration. */
7070 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
7071 &consumer_path_offset
);
7073 status
= LTTNG_ERR_INVALID
;
7076 /* Add the UST default trace dir to path. */
7077 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7078 buf_reg_chan
, node
.node
) {
7079 status
= consumer_snapshot_channel(socket
,
7080 buf_reg_chan
->consumer_key
,
7081 output
, 0, &trace_path
[consumer_path_offset
],
7082 nb_packets_per_stream
);
7083 if (status
!= LTTNG_OK
) {
7087 status
= consumer_snapshot_channel(socket
,
7088 reg
->registry
->reg
.ust
->_metadata_key
, output
, 1,
7089 &trace_path
[consumer_path_offset
], 0);
7090 if (status
!= LTTNG_OK
) {
7096 case LTTNG_BUFFER_PER_PID
:
7098 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7099 struct consumer_socket
*socket
;
7100 struct lttng_ht_iter chan_iter
;
7101 struct ust_app_channel
*ua_chan
;
7102 struct ust_app_session
*ua_sess
;
7103 ust_registry_session
*registry
;
7104 char pathname
[PATH_MAX
];
7105 size_t consumer_path_offset
= 0;
7107 ua_sess
= lookup_session_by_app(usess
, app
);
7109 /* Session not associated with this app. */
7113 /* Get the right consumer socket for the application. */
7114 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7117 status
= LTTNG_ERR_INVALID
;
7121 /* Add the UST default trace dir to path. */
7122 memset(pathname
, 0, sizeof(pathname
));
7123 ret
= snprintf(pathname
, sizeof(pathname
), "%s",
7126 status
= LTTNG_ERR_INVALID
;
7127 PERROR("snprintf snapshot path");
7130 /* Free path allowed on previous iteration. */
7132 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
7133 &consumer_path_offset
);
7135 status
= LTTNG_ERR_INVALID
;
7138 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7139 ua_chan
, node
.node
) {
7140 status
= consumer_snapshot_channel(socket
,
7141 ua_chan
->key
, output
, 0,
7142 &trace_path
[consumer_path_offset
],
7143 nb_packets_per_stream
);
7147 case LTTNG_ERR_CHAN_NOT_FOUND
:
7154 registry
= get_session_registry(ua_sess
);
7156 DBG("Application session is being torn down. Skip application.");
7159 status
= consumer_snapshot_channel(socket
,
7160 registry
->_metadata_key
, output
, 1,
7161 &trace_path
[consumer_path_offset
], 0);
7165 case LTTNG_ERR_CHAN_NOT_FOUND
:
7185 * Return the size taken by one more packet per stream.
7187 uint64_t ust_app_get_size_one_more_packet_per_stream(
7188 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
7190 uint64_t tot_size
= 0;
7191 struct ust_app
*app
;
7192 struct lttng_ht_iter iter
;
7194 LTTNG_ASSERT(usess
);
7196 switch (usess
->buffer_type
) {
7197 case LTTNG_BUFFER_PER_UID
:
7199 struct buffer_reg_uid
*reg
;
7201 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7202 struct buffer_reg_channel
*buf_reg_chan
;
7205 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7206 buf_reg_chan
, node
.node
) {
7207 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
7209 * Don't take channel into account if we
7210 * already grab all its packets.
7214 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
7220 case LTTNG_BUFFER_PER_PID
:
7223 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7224 struct ust_app_channel
*ua_chan
;
7225 struct ust_app_session
*ua_sess
;
7226 struct lttng_ht_iter chan_iter
;
7228 ua_sess
= lookup_session_by_app(usess
, app
);
7230 /* Session not associated with this app. */
7234 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7235 ua_chan
, node
.node
) {
7236 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
7238 * Don't take channel into account if we
7239 * already grab all its packets.
7243 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
7257 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
7258 struct cds_list_head
*buffer_reg_uid_list
,
7259 struct consumer_output
*consumer
, uint64_t uchan_id
,
7260 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
7263 uint64_t consumer_chan_key
;
7268 ret
= buffer_reg_uid_consumer_channel_key(
7269 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
7277 ret
= consumer_get_lost_packets(ust_session_id
,
7278 consumer_chan_key
, consumer
, lost
);
7280 ret
= consumer_get_discarded_events(ust_session_id
,
7281 consumer_chan_key
, consumer
, discarded
);
7288 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
7289 struct ltt_ust_channel
*uchan
,
7290 struct consumer_output
*consumer
, int overwrite
,
7291 uint64_t *discarded
, uint64_t *lost
)
7294 struct lttng_ht_iter iter
;
7295 struct lttng_ht_node_str
*ua_chan_node
;
7296 struct ust_app
*app
;
7297 struct ust_app_session
*ua_sess
;
7298 struct ust_app_channel
*ua_chan
;
7305 * Iterate over every registered applications. Sum counters for
7306 * all applications containing requested session and channel.
7308 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7309 struct lttng_ht_iter uiter
;
7311 ua_sess
= lookup_session_by_app(usess
, app
);
7312 if (ua_sess
== NULL
) {
7317 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
7318 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
7319 /* If the session is found for the app, the channel must be there */
7320 LTTNG_ASSERT(ua_chan_node
);
7322 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
7327 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
7334 uint64_t _discarded
;
7336 ret
= consumer_get_discarded_events(usess
->id
,
7337 ua_chan
->key
, consumer
, &_discarded
);
7341 (*discarded
) += _discarded
;
7350 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
7351 struct ust_app
*app
)
7354 struct ust_app_session
*ua_sess
;
7356 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7360 ua_sess
= lookup_session_by_app(usess
, app
);
7361 if (ua_sess
== NULL
) {
7362 /* The session is in teardown process. Ignore and continue. */
7366 pthread_mutex_lock(&ua_sess
->lock
);
7368 if (ua_sess
->deleted
) {
7372 pthread_mutex_lock(&app
->sock_lock
);
7373 ret
= lttng_ust_ctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7374 pthread_mutex_unlock(&app
->sock_lock
);
7377 pthread_mutex_unlock(&ua_sess
->lock
);
7381 health_code_update();
7386 * Regenerate the statedump for each app in the session.
7388 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7391 struct lttng_ht_iter iter
;
7392 struct ust_app
*app
;
7394 DBG("Regenerating the metadata for all UST apps");
7398 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7399 if (!app
->compatible
) {
7403 ret
= ust_app_regenerate_statedump(usess
, app
);
7405 /* Continue to the next app even on error */
7416 * Rotate all the channels of a session.
7418 * Return LTTNG_OK on success or else an LTTng error code.
7420 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7423 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7424 struct lttng_ht_iter iter
;
7425 struct ust_app
*app
;
7426 struct ltt_ust_session
*usess
= session
->ust_session
;
7428 LTTNG_ASSERT(usess
);
7432 switch (usess
->buffer_type
) {
7433 case LTTNG_BUFFER_PER_UID
:
7435 struct buffer_reg_uid
*reg
;
7437 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7438 struct buffer_reg_channel
*buf_reg_chan
;
7439 struct consumer_socket
*socket
;
7441 /* Get consumer socket to use to push the metadata.*/
7442 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7445 cmd_ret
= LTTNG_ERR_INVALID
;
7449 /* Rotate the data channels. */
7450 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7451 buf_reg_chan
, node
.node
) {
7452 ret
= consumer_rotate_channel(socket
,
7453 buf_reg_chan
->consumer_key
,
7455 /* is_metadata_channel */ false);
7457 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7463 * The metadata channel might not be present.
7465 * Consumer stream allocation can be done
7466 * asynchronously and can fail on intermediary
7467 * operations (i.e add context) and lead to data
7468 * channels created with no metadata channel.
7470 if (!reg
->registry
->reg
.ust
->_metadata_key
) {
7471 /* Skip since no metadata is present. */
7475 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7477 ret
= consumer_rotate_channel(socket
,
7478 reg
->registry
->reg
.ust
->_metadata_key
,
7480 /* is_metadata_channel */ true);
7482 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7488 case LTTNG_BUFFER_PER_PID
:
7490 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7491 struct consumer_socket
*socket
;
7492 struct lttng_ht_iter chan_iter
;
7493 struct ust_app_channel
*ua_chan
;
7494 struct ust_app_session
*ua_sess
;
7495 ust_registry_session
*registry
;
7497 ua_sess
= lookup_session_by_app(usess
, app
);
7499 /* Session not associated with this app. */
7503 /* Get the right consumer socket for the application. */
7504 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7507 cmd_ret
= LTTNG_ERR_INVALID
;
7511 registry
= get_session_registry(ua_sess
);
7513 DBG("Application session is being torn down. Skip application.");
7517 /* Rotate the data channels. */
7518 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7519 ua_chan
, node
.node
) {
7520 ret
= consumer_rotate_channel(socket
,
7523 /* is_metadata_channel */ false);
7525 /* Per-PID buffer and application going away. */
7526 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7528 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7533 /* Rotate the metadata channel. */
7534 (void) push_metadata(registry
, usess
->consumer
);
7535 ret
= consumer_rotate_channel(socket
,
7536 registry
->_metadata_key
,
7538 /* is_metadata_channel */ true);
7540 /* Per-PID buffer and application going away. */
7541 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7543 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7561 enum lttng_error_code
ust_app_create_channel_subdirectories(
7562 const struct ltt_ust_session
*usess
)
7564 enum lttng_error_code ret
= LTTNG_OK
;
7565 struct lttng_ht_iter iter
;
7566 enum lttng_trace_chunk_status chunk_status
;
7567 char *pathname_index
;
7570 LTTNG_ASSERT(usess
->current_trace_chunk
);
7573 switch (usess
->buffer_type
) {
7574 case LTTNG_BUFFER_PER_UID
:
7576 struct buffer_reg_uid
*reg
;
7578 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7579 fmt_ret
= asprintf(&pathname_index
,
7580 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7581 reg
->uid
, reg
->bits_per_long
);
7583 ERR("Failed to format channel index directory");
7584 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7589 * Create the index subdirectory which will take care
7590 * of implicitly creating the channel's path.
7592 chunk_status
= lttng_trace_chunk_create_subdirectory(
7593 usess
->current_trace_chunk
,
7595 free(pathname_index
);
7596 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7597 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7603 case LTTNG_BUFFER_PER_PID
:
7605 struct ust_app
*app
;
7608 * Create the toplevel ust/ directory in case no apps are running.
7610 chunk_status
= lttng_trace_chunk_create_subdirectory(
7611 usess
->current_trace_chunk
,
7612 DEFAULT_UST_TRACE_DIR
);
7613 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7614 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7618 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7620 struct ust_app_session
*ua_sess
;
7621 ust_registry_session
*registry
;
7623 ua_sess
= lookup_session_by_app(usess
, app
);
7625 /* Session not associated with this app. */
7629 registry
= get_session_registry(ua_sess
);
7631 DBG("Application session is being torn down. Skip application.");
7635 fmt_ret
= asprintf(&pathname_index
,
7636 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7639 ERR("Failed to format channel index directory");
7640 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7644 * Create the index subdirectory which will take care
7645 * of implicitly creating the channel's path.
7647 chunk_status
= lttng_trace_chunk_create_subdirectory(
7648 usess
->current_trace_chunk
,
7650 free(pathname_index
);
7651 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7652 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7669 * Clear all the channels of a session.
7671 * Return LTTNG_OK on success or else an LTTng error code.
7673 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7676 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7677 struct lttng_ht_iter iter
;
7678 struct ust_app
*app
;
7679 struct ltt_ust_session
*usess
= session
->ust_session
;
7681 LTTNG_ASSERT(usess
);
7685 if (usess
->active
) {
7686 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7687 cmd_ret
= LTTNG_ERR_FATAL
;
7691 switch (usess
->buffer_type
) {
7692 case LTTNG_BUFFER_PER_UID
:
7694 struct buffer_reg_uid
*reg
;
7696 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7697 struct buffer_reg_channel
*buf_reg_chan
;
7698 struct consumer_socket
*socket
;
7700 /* Get consumer socket to use to push the metadata.*/
7701 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7704 cmd_ret
= LTTNG_ERR_INVALID
;
7708 /* Clear the data channels. */
7709 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7710 buf_reg_chan
, node
.node
) {
7711 ret
= consumer_clear_channel(socket
,
7712 buf_reg_chan
->consumer_key
);
7718 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7721 * Clear the metadata channel.
7722 * Metadata channel is not cleared per se but we still need to
7723 * perform a rotation operation on it behind the scene.
7725 ret
= consumer_clear_channel(socket
,
7726 reg
->registry
->reg
.ust
->_metadata_key
);
7733 case LTTNG_BUFFER_PER_PID
:
7735 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7736 struct consumer_socket
*socket
;
7737 struct lttng_ht_iter chan_iter
;
7738 struct ust_app_channel
*ua_chan
;
7739 struct ust_app_session
*ua_sess
;
7740 ust_registry_session
*registry
;
7742 ua_sess
= lookup_session_by_app(usess
, app
);
7744 /* Session not associated with this app. */
7748 /* Get the right consumer socket for the application. */
7749 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7752 cmd_ret
= LTTNG_ERR_INVALID
;
7756 registry
= get_session_registry(ua_sess
);
7758 DBG("Application session is being torn down. Skip application.");
7762 /* Clear the data channels. */
7763 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7764 ua_chan
, node
.node
) {
7765 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7767 /* Per-PID buffer and application going away. */
7768 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7775 (void) push_metadata(registry
, usess
->consumer
);
7778 * Clear the metadata channel.
7779 * Metadata channel is not cleared per se but we still need to
7780 * perform rotation operation on it behind the scene.
7782 ret
= consumer_clear_channel(socket
, registry
->_metadata_key
);
7784 /* Per-PID buffer and application going away. */
7785 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7803 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7804 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7807 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7817 * This function skips the metadata channel as the begin/end timestamps of a
7818 * metadata packet are useless.
7820 * Moreover, opening a packet after a "clear" will cause problems for live
7821 * sessions as it will introduce padding that was not part of the first trace
7822 * chunk. The relay daemon expects the content of the metadata stream of
7823 * successive metadata trace chunks to be strict supersets of one another.
7825 * For example, flushing a packet at the beginning of the metadata stream of
7826 * a trace chunk resulting from a "clear" session command will cause the
7827 * size of the metadata stream of the new trace chunk to not match the size of
7828 * the metadata stream of the original chunk. This will confuse the relay
7829 * daemon as the same "offset" in a metadata stream will no longer point
7830 * to the same content.
7832 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7834 enum lttng_error_code ret
= LTTNG_OK
;
7835 struct lttng_ht_iter iter
;
7836 struct ltt_ust_session
*usess
= session
->ust_session
;
7838 LTTNG_ASSERT(usess
);
7842 switch (usess
->buffer_type
) {
7843 case LTTNG_BUFFER_PER_UID
:
7845 struct buffer_reg_uid
*reg
;
7847 cds_list_for_each_entry (
7848 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7849 struct buffer_reg_channel
*buf_reg_chan
;
7850 struct consumer_socket
*socket
;
7852 socket
= consumer_find_socket_by_bitness(
7853 reg
->bits_per_long
, usess
->consumer
);
7855 ret
= LTTNG_ERR_FATAL
;
7859 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7860 &iter
.iter
, buf_reg_chan
, node
.node
) {
7861 const int open_ret
=
7862 consumer_open_channel_packets(
7864 buf_reg_chan
->consumer_key
);
7867 ret
= LTTNG_ERR_UNK
;
7874 case LTTNG_BUFFER_PER_PID
:
7876 struct ust_app
*app
;
7878 cds_lfht_for_each_entry (
7879 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7880 struct consumer_socket
*socket
;
7881 struct lttng_ht_iter chan_iter
;
7882 struct ust_app_channel
*ua_chan
;
7883 struct ust_app_session
*ua_sess
;
7884 ust_registry_session
*registry
;
7886 ua_sess
= lookup_session_by_app(usess
, app
);
7888 /* Session not associated with this app. */
7892 /* Get the right consumer socket for the application. */
7893 socket
= consumer_find_socket_by_bitness(
7894 app
->bits_per_long
, usess
->consumer
);
7896 ret
= LTTNG_ERR_FATAL
;
7900 registry
= get_session_registry(ua_sess
);
7902 DBG("Application session is being torn down. Skip application.");
7906 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
7907 &chan_iter
.iter
, ua_chan
, node
.node
) {
7908 const int open_ret
=
7909 consumer_open_channel_packets(
7915 * Per-PID buffer and application going
7918 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7922 ret
= LTTNG_ERR_UNK
;