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.h>
25 #include <common/compat/errno.h>
26 #include <common/common.h>
27 #include <common/hashtable/utils.h>
28 #include <lttng/event-rule/event-rule.h>
29 #include <lttng/event-rule/event-rule-internal.h>
30 #include <lttng/event-rule/user-tracepoint.h>
31 #include <lttng/condition/condition.h>
32 #include <lttng/condition/event-rule-matches-internal.h>
33 #include <lttng/condition/event-rule-matches.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
37 #include "buffer-registry.h"
38 #include "condition-internal.h"
40 #include "health-sessiond.h"
42 #include "ust-consumer.h"
43 #include "lttng-ust-ctl.h"
44 #include "lttng-ust-error.h"
47 #include "lttng-sessiond.h"
48 #include "notification-thread-commands.h"
51 #include "event-notifier-error-accounting.h"
52 #include "ust-field-utils.h"
54 struct lttng_ht
*ust_app_ht
;
55 struct lttng_ht
*ust_app_ht_by_sock
;
56 struct lttng_ht
*ust_app_ht_by_notify_sock
;
59 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
61 /* Next available channel key. Access under next_channel_key_lock. */
62 static uint64_t _next_channel_key
;
63 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
65 /* Next available session ID. Access under next_session_id_lock. */
66 static uint64_t _next_session_id
;
67 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
70 * Return the incremented value of next_channel_key.
72 static uint64_t get_next_channel_key(void)
76 pthread_mutex_lock(&next_channel_key_lock
);
77 ret
= ++_next_channel_key
;
78 pthread_mutex_unlock(&next_channel_key_lock
);
83 * Return the atomically incremented value of next_session_id.
85 static uint64_t get_next_session_id(void)
89 pthread_mutex_lock(&next_session_id_lock
);
90 ret
= ++_next_session_id
;
91 pthread_mutex_unlock(&next_session_id_lock
);
95 static void copy_channel_attr_to_ustctl(
96 struct lttng_ust_ctl_consumer_channel_attr
*attr
,
97 struct lttng_ust_abi_channel_attr
*uattr
)
99 /* Copy event attributes since the layout is different. */
100 attr
->subbuf_size
= uattr
->subbuf_size
;
101 attr
->num_subbuf
= uattr
->num_subbuf
;
102 attr
->overwrite
= uattr
->overwrite
;
103 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
104 attr
->read_timer_interval
= uattr
->read_timer_interval
;
105 attr
->output
= (lttng_ust_abi_output
) uattr
->output
;
106 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
110 * Match function for the hash table lookup.
112 * It matches an ust app event based on three attributes which are the event
113 * name, the filter bytecode and the loglevel.
115 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
117 struct ust_app_event
*event
;
118 const struct ust_app_ht_key
*key
;
119 int ev_loglevel_value
;
124 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
125 key
= (ust_app_ht_key
*) _key
;
126 ev_loglevel_value
= event
->attr
.loglevel
;
128 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
131 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
135 /* Event loglevel. */
136 if (ev_loglevel_value
!= key
->loglevel_type
) {
137 if (event
->attr
.loglevel_type
== LTTNG_UST_ABI_LOGLEVEL_ALL
138 && key
->loglevel_type
== 0 &&
139 ev_loglevel_value
== -1) {
141 * Match is accepted. This is because on event creation, the
142 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
143 * -1 are accepted for this loglevel type since 0 is the one set by
144 * the API when receiving an enable event.
151 /* One of the filters is NULL, fail. */
152 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
156 if (key
->filter
&& event
->filter
) {
157 /* Both filters exists, check length followed by the bytecode. */
158 if (event
->filter
->len
!= key
->filter
->len
||
159 memcmp(event
->filter
->data
, key
->filter
->data
,
160 event
->filter
->len
) != 0) {
165 /* One of the exclusions is NULL, fail. */
166 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
170 if (key
->exclusion
&& event
->exclusion
) {
171 /* Both exclusions exists, check count followed by the names. */
172 if (event
->exclusion
->count
!= key
->exclusion
->count
||
173 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
174 event
->exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) != 0) {
188 * Unique add of an ust app event in the given ht. This uses the custom
189 * ht_match_ust_app_event match function and the event name as hash.
191 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
192 struct ust_app_event
*event
)
194 struct cds_lfht_node
*node_ptr
;
195 struct ust_app_ht_key key
;
198 LTTNG_ASSERT(ua_chan
);
199 LTTNG_ASSERT(ua_chan
->events
);
202 ht
= ua_chan
->events
;
203 key
.name
= event
->attr
.name
;
204 key
.filter
= event
->filter
;
205 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) event
->attr
.loglevel
;
206 key
.exclusion
= event
->exclusion
;
208 node_ptr
= cds_lfht_add_unique(ht
->ht
,
209 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
210 ht_match_ust_app_event
, &key
, &event
->node
.node
);
211 LTTNG_ASSERT(node_ptr
== &event
->node
.node
);
215 * Close the notify socket from the given RCU head object. This MUST be called
216 * through a call_rcu().
218 static void close_notify_sock_rcu(struct rcu_head
*head
)
221 struct ust_app_notify_sock_obj
*obj
=
222 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
224 /* Must have a valid fd here. */
225 LTTNG_ASSERT(obj
->fd
>= 0);
227 ret
= close(obj
->fd
);
229 ERR("close notify sock %d RCU", obj
->fd
);
231 lttng_fd_put(LTTNG_FD_APPS
, 1);
237 * Return the session registry according to the buffer type of the given
240 * A registry per UID object MUST exists before calling this function or else
241 * it LTTNG_ASSERT() if not found. RCU read side lock must be acquired.
243 static struct ust_registry_session
*get_session_registry(
244 struct ust_app_session
*ua_sess
)
246 struct ust_registry_session
*registry
= NULL
;
248 LTTNG_ASSERT(ua_sess
);
250 switch (ua_sess
->buffer_type
) {
251 case LTTNG_BUFFER_PER_PID
:
253 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
257 registry
= reg_pid
->registry
->reg
.ust
;
260 case LTTNG_BUFFER_PER_UID
:
262 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
263 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
264 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
268 registry
= reg_uid
->registry
->reg
.ust
;
280 * Delete ust context safely. RCU read lock must be held before calling
284 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
289 LTTNG_ASSERT(ua_ctx
);
292 pthread_mutex_lock(&app
->sock_lock
);
293 ret
= lttng_ust_ctl_release_object(sock
, ua_ctx
->obj
);
294 pthread_mutex_unlock(&app
->sock_lock
);
296 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
297 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
298 app
->pid
, app
->sock
);
299 } else if (ret
== -EAGAIN
) {
300 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
301 app
->pid
, app
->sock
);
303 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
304 ua_ctx
->obj
->handle
, ret
,
305 app
->pid
, app
->sock
);
314 * Delete ust app event safely. RCU read lock must be held before calling
318 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
323 LTTNG_ASSERT(ua_event
);
325 free(ua_event
->filter
);
326 if (ua_event
->exclusion
!= NULL
)
327 free(ua_event
->exclusion
);
328 if (ua_event
->obj
!= NULL
) {
329 pthread_mutex_lock(&app
->sock_lock
);
330 ret
= lttng_ust_ctl_release_object(sock
, ua_event
->obj
);
331 pthread_mutex_unlock(&app
->sock_lock
);
333 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
334 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
335 app
->pid
, app
->sock
);
336 } else if (ret
== -EAGAIN
) {
337 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
338 app
->pid
, app
->sock
);
340 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
341 ret
, app
->pid
, app
->sock
);
350 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
351 * through a call_rcu().
354 void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
356 struct ust_app_event_notifier_rule
*obj
= caa_container_of(
357 head
, struct ust_app_event_notifier_rule
, rcu_head
);
363 * Delete ust app event notifier rule safely.
365 static void delete_ust_app_event_notifier_rule(int sock
,
366 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
,
371 LTTNG_ASSERT(ua_event_notifier_rule
);
373 if (ua_event_notifier_rule
->exclusion
!= NULL
) {
374 free(ua_event_notifier_rule
->exclusion
);
377 if (ua_event_notifier_rule
->obj
!= NULL
) {
378 pthread_mutex_lock(&app
->sock_lock
);
379 ret
= lttng_ust_ctl_release_object(sock
, ua_event_notifier_rule
->obj
);
380 pthread_mutex_unlock(&app
->sock_lock
);
382 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
383 DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d",
384 app
->pid
, app
->sock
);
385 } else if (ret
== -EAGAIN
) {
386 WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d",
387 app
->pid
, app
->sock
);
389 ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d",
390 ret
, app
->pid
, app
->sock
);
394 free(ua_event_notifier_rule
->obj
);
397 lttng_trigger_put(ua_event_notifier_rule
->trigger
);
398 call_rcu(&ua_event_notifier_rule
->rcu_head
,
399 free_ust_app_event_notifier_rule_rcu
);
403 * Release ust data object of the given stream.
405 * Return 0 on success or else a negative value.
407 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
412 LTTNG_ASSERT(stream
);
415 pthread_mutex_lock(&app
->sock_lock
);
416 ret
= lttng_ust_ctl_release_object(sock
, stream
->obj
);
417 pthread_mutex_unlock(&app
->sock_lock
);
419 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
420 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
421 app
->pid
, app
->sock
);
422 } else if (ret
== -EAGAIN
) {
423 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
424 app
->pid
, app
->sock
);
426 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
427 ret
, app
->pid
, app
->sock
);
430 lttng_fd_put(LTTNG_FD_APPS
, 2);
438 * Delete ust app stream safely. RCU read lock must be held before calling
442 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
445 LTTNG_ASSERT(stream
);
447 (void) release_ust_app_stream(sock
, stream
, app
);
452 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
454 struct ust_app_channel
*ua_chan
=
455 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
457 lttng_ht_destroy(ua_chan
->ctx
);
458 lttng_ht_destroy(ua_chan
->events
);
463 * Extract the lost packet or discarded events counter when the channel is
464 * being deleted and store the value in the parent channel so we can
465 * access it from lttng list and at stop/destroy.
467 * The session list lock must be held by the caller.
470 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
472 uint64_t discarded
= 0, lost
= 0;
473 struct ltt_session
*session
;
474 struct ltt_ust_channel
*uchan
;
476 if (ua_chan
->attr
.type
!= LTTNG_UST_ABI_CHAN_PER_CPU
) {
481 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
482 if (!session
|| !session
->ust_session
) {
484 * Not finding the session is not an error because there are
485 * multiple ways the channels can be torn down.
487 * 1) The session daemon can initiate the destruction of the
488 * ust app session after receiving a destroy command or
489 * during its shutdown/teardown.
490 * 2) The application, since we are in per-pid tracing, is
491 * unregistering and tearing down its ust app session.
493 * Both paths are protected by the session list lock which
494 * ensures that the accounting of lost packets and discarded
495 * events is done exactly once. The session is then unpublished
496 * from the session list, resulting in this condition.
501 if (ua_chan
->attr
.overwrite
) {
502 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
503 ua_chan
->key
, session
->ust_session
->consumer
,
506 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
507 ua_chan
->key
, session
->ust_session
->consumer
,
510 uchan
= trace_ust_find_channel_by_name(
511 session
->ust_session
->domain_global
.channels
,
514 ERR("Missing UST channel to store discarded counters");
518 uchan
->per_pid_closed_app_discarded
+= discarded
;
519 uchan
->per_pid_closed_app_lost
+= lost
;
524 session_put(session
);
529 * Delete ust app channel safely. RCU read lock must be held before calling
532 * The session list lock must be held by the caller.
535 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
539 struct lttng_ht_iter iter
;
540 struct ust_app_event
*ua_event
;
541 struct ust_app_ctx
*ua_ctx
;
542 struct ust_app_stream
*stream
, *stmp
;
543 struct ust_registry_session
*registry
;
545 LTTNG_ASSERT(ua_chan
);
547 DBG3("UST app deleting channel %s", ua_chan
->name
);
550 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
551 cds_list_del(&stream
->list
);
552 delete_ust_app_stream(sock
, stream
, app
);
556 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
557 cds_list_del(&ua_ctx
->list
);
558 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
560 delete_ust_app_ctx(sock
, ua_ctx
, app
);
564 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
566 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
568 delete_ust_app_event(sock
, ua_event
, app
);
571 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
572 /* Wipe and free registry from session registry. */
573 registry
= get_session_registry(ua_chan
->session
);
575 ust_registry_channel_del_free(registry
, ua_chan
->key
,
579 * A negative socket can be used by the caller when
580 * cleaning-up a ua_chan in an error path. Skip the
581 * accounting in this case.
584 save_per_pid_lost_discarded_counters(ua_chan
);
588 if (ua_chan
->obj
!= NULL
) {
589 /* Remove channel from application UST object descriptor. */
590 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
591 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
593 pthread_mutex_lock(&app
->sock_lock
);
594 ret
= lttng_ust_ctl_release_object(sock
, ua_chan
->obj
);
595 pthread_mutex_unlock(&app
->sock_lock
);
597 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
598 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
599 ua_chan
->name
, app
->pid
,
601 } else if (ret
== -EAGAIN
) {
602 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
603 ua_chan
->name
, app
->pid
,
606 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
607 ua_chan
->name
, ret
, app
->pid
,
611 lttng_fd_put(LTTNG_FD_APPS
, 1);
614 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
617 int ust_app_register_done(struct ust_app
*app
)
621 pthread_mutex_lock(&app
->sock_lock
);
622 ret
= lttng_ust_ctl_register_done(app
->sock
);
623 pthread_mutex_unlock(&app
->sock_lock
);
627 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*data
)
632 pthread_mutex_lock(&app
->sock_lock
);
637 ret
= lttng_ust_ctl_release_object(sock
, data
);
639 pthread_mutex_unlock(&app
->sock_lock
);
645 * Push metadata to consumer socket.
647 * RCU read-side lock must be held to guarantee existence of socket.
648 * Must be called with the ust app session lock held.
649 * Must be called with the registry lock held.
651 * On success, return the len of metadata pushed or else a negative value.
652 * Returning a -EPIPE return value means we could not send the metadata,
653 * but it can be caused by recoverable errors (e.g. the application has
654 * terminated concurrently).
656 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
657 struct consumer_socket
*socket
, int send_zero_data
)
660 char *metadata_str
= NULL
;
661 size_t len
, offset
, new_metadata_len_sent
;
663 uint64_t metadata_key
, metadata_version
;
665 LTTNG_ASSERT(registry
);
666 LTTNG_ASSERT(socket
);
668 metadata_key
= registry
->metadata_key
;
671 * Means that no metadata was assigned to the session. This can
672 * happens if no start has been done previously.
678 offset
= registry
->metadata_len_sent
;
679 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
680 new_metadata_len_sent
= registry
->metadata_len
;
681 metadata_version
= registry
->metadata_version
;
683 DBG3("No metadata to push for metadata key %" PRIu64
,
684 registry
->metadata_key
);
686 if (send_zero_data
) {
687 DBG("No metadata to push");
693 /* Allocate only what we have to send. */
694 metadata_str
= (char *) zmalloc(len
);
696 PERROR("zmalloc ust app metadata string");
700 /* Copy what we haven't sent out. */
701 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
704 pthread_mutex_unlock(®istry
->lock
);
706 * We need to unlock the registry while we push metadata to
707 * break a circular dependency between the consumerd metadata
708 * lock and the sessiond registry lock. Indeed, pushing metadata
709 * to the consumerd awaits that it gets pushed all the way to
710 * relayd, but doing so requires grabbing the metadata lock. If
711 * a concurrent metadata request is being performed by
712 * consumerd, this can try to grab the registry lock on the
713 * sessiond while holding the metadata lock on the consumer
714 * daemon. Those push and pull schemes are performed on two
715 * different bidirectionnal communication sockets.
717 ret
= consumer_push_metadata(socket
, metadata_key
,
718 metadata_str
, len
, offset
, metadata_version
);
719 pthread_mutex_lock(®istry
->lock
);
722 * There is an acceptable race here between the registry
723 * metadata key assignment and the creation on the
724 * consumer. The session daemon can concurrently push
725 * metadata for this registry while being created on the
726 * consumer since the metadata key of the registry is
727 * assigned *before* it is setup to avoid the consumer
728 * to ask for metadata that could possibly be not found
729 * in the session daemon.
731 * The metadata will get pushed either by the session
732 * being stopped or the consumer requesting metadata if
733 * that race is triggered.
735 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
738 ERR("Error pushing metadata to consumer");
744 * Metadata may have been concurrently pushed, since
745 * we're not holding the registry lock while pushing to
746 * consumer. This is handled by the fact that we send
747 * the metadata content, size, and the offset at which
748 * that metadata belongs. This may arrive out of order
749 * on the consumer side, and the consumer is able to
750 * deal with overlapping fragments. The consumer
751 * supports overlapping fragments, which must be
752 * contiguous starting from offset 0. We keep the
753 * largest metadata_len_sent value of the concurrent
756 registry
->metadata_len_sent
=
757 std::max(registry
->metadata_len_sent
,
758 new_metadata_len_sent
);
767 * On error, flag the registry that the metadata is
768 * closed. We were unable to push anything and this
769 * means that either the consumer is not responding or
770 * the metadata cache has been destroyed on the
773 registry
->metadata_closed
= 1;
781 * For a given application and session, push metadata to consumer.
782 * Either sock or consumer is required : if sock is NULL, the default
783 * socket to send the metadata is retrieved from consumer, if sock
784 * is not NULL we use it to send the metadata.
785 * RCU read-side lock must be held while calling this function,
786 * therefore ensuring existence of registry. It also ensures existence
787 * of socket throughout this function.
789 * Return 0 on success else a negative error.
790 * Returning a -EPIPE return value means we could not send the metadata,
791 * but it can be caused by recoverable errors (e.g. the application has
792 * terminated concurrently).
794 static int push_metadata(struct ust_registry_session
*registry
,
795 struct consumer_output
*consumer
)
799 struct consumer_socket
*socket
;
801 LTTNG_ASSERT(registry
);
802 LTTNG_ASSERT(consumer
);
804 pthread_mutex_lock(®istry
->lock
);
805 if (registry
->metadata_closed
) {
810 /* Get consumer socket to use to push the metadata.*/
811 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
818 ret
= ust_app_push_metadata(registry
, socket
, 0);
823 pthread_mutex_unlock(®istry
->lock
);
827 pthread_mutex_unlock(®istry
->lock
);
832 * Send to the consumer a close metadata command for the given session. Once
833 * done, the metadata channel is deleted and the session metadata pointer is
834 * nullified. The session lock MUST be held unless the application is
835 * in the destroy path.
837 * Do not hold the registry lock while communicating with the consumerd, because
838 * doing so causes inter-process deadlocks between consumerd and sessiond with
839 * the metadata request notification.
841 * Return 0 on success else a negative value.
843 static int close_metadata(struct ust_registry_session
*registry
,
844 struct consumer_output
*consumer
)
847 struct consumer_socket
*socket
;
848 uint64_t metadata_key
;
849 bool registry_was_already_closed
;
851 LTTNG_ASSERT(registry
);
852 LTTNG_ASSERT(consumer
);
856 pthread_mutex_lock(®istry
->lock
);
857 metadata_key
= registry
->metadata_key
;
858 registry_was_already_closed
= registry
->metadata_closed
;
859 if (metadata_key
!= 0) {
861 * Metadata closed. Even on error this means that the consumer
862 * is not responding or not found so either way a second close
863 * should NOT be emit for this registry.
865 registry
->metadata_closed
= 1;
867 pthread_mutex_unlock(®istry
->lock
);
869 if (metadata_key
== 0 || registry_was_already_closed
) {
874 /* Get consumer socket to use to push the metadata.*/
875 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
882 ret
= consumer_close_metadata(socket
, metadata_key
);
893 void delete_ust_app_session_rcu(struct rcu_head
*head
)
895 struct ust_app_session
*ua_sess
=
896 caa_container_of(head
, struct ust_app_session
, rcu_head
);
898 lttng_ht_destroy(ua_sess
->channels
);
903 * Delete ust app session safely. RCU read lock must be held before calling
906 * The session list lock must be held by the caller.
909 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
913 struct lttng_ht_iter iter
;
914 struct ust_app_channel
*ua_chan
;
915 struct ust_registry_session
*registry
;
917 LTTNG_ASSERT(ua_sess
);
919 pthread_mutex_lock(&ua_sess
->lock
);
921 LTTNG_ASSERT(!ua_sess
->deleted
);
922 ua_sess
->deleted
= true;
924 registry
= get_session_registry(ua_sess
);
925 /* Registry can be null on error path during initialization. */
927 /* Push metadata for application before freeing the application. */
928 (void) push_metadata(registry
, ua_sess
->consumer
);
931 * Don't ask to close metadata for global per UID buffers. Close
932 * metadata only on destroy trace session in this case. Also, the
933 * previous push metadata could have flag the metadata registry to
934 * close so don't send a close command if closed.
936 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
937 /* And ask to close it for this session registry. */
938 (void) close_metadata(registry
, ua_sess
->consumer
);
942 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
944 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
946 delete_ust_app_channel(sock
, ua_chan
, app
);
949 /* In case of per PID, the registry is kept in the session. */
950 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
951 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
954 * Registry can be null on error path during
957 buffer_reg_pid_remove(reg_pid
);
958 buffer_reg_pid_destroy(reg_pid
);
962 if (ua_sess
->handle
!= -1) {
963 pthread_mutex_lock(&app
->sock_lock
);
964 ret
= lttng_ust_ctl_release_handle(sock
, ua_sess
->handle
);
965 pthread_mutex_unlock(&app
->sock_lock
);
967 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
968 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
969 app
->pid
, app
->sock
);
970 } else if (ret
== -EAGAIN
) {
971 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
972 app
->pid
, app
->sock
);
974 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
975 ret
, app
->pid
, app
->sock
);
979 /* Remove session from application UST object descriptor. */
980 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
981 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
985 pthread_mutex_unlock(&ua_sess
->lock
);
987 consumer_output_put(ua_sess
->consumer
);
989 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
993 * Delete a traceable application structure from the global list. Never call
994 * this function outside of a call_rcu call.
997 void delete_ust_app(struct ust_app
*app
)
1000 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
1001 struct lttng_ht_iter iter
;
1002 struct ust_app_event_notifier_rule
*event_notifier_rule
;
1003 bool event_notifier_write_fd_is_open
;
1006 * The session list lock must be held during this function to guarantee
1007 * the existence of ua_sess.
1009 session_lock_list();
1010 /* Delete ust app sessions info */
1015 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
1017 /* Free every object in the session and the session. */
1019 delete_ust_app_session(sock
, ua_sess
, app
);
1023 /* Remove the event notifier rules associated with this app. */
1025 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
1026 &iter
.iter
, event_notifier_rule
, node
.node
) {
1027 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
1030 delete_ust_app_event_notifier_rule(
1031 app
->sock
, event_notifier_rule
, app
);
1036 lttng_ht_destroy(app
->sessions
);
1037 lttng_ht_destroy(app
->ust_sessions_objd
);
1038 lttng_ht_destroy(app
->ust_objd
);
1039 lttng_ht_destroy(app
->token_to_event_notifier_rule_ht
);
1042 * This could be NULL if the event notifier setup failed (e.g the app
1043 * was killed or the tracer does not support this feature).
1045 if (app
->event_notifier_group
.object
) {
1046 enum lttng_error_code ret_code
;
1047 enum event_notifier_error_accounting_status status
;
1049 const int event_notifier_read_fd
= lttng_pipe_get_readfd(
1050 app
->event_notifier_group
.event_pipe
);
1052 ret_code
= notification_thread_command_remove_tracer_event_source(
1053 the_notification_thread_handle
,
1054 event_notifier_read_fd
);
1055 if (ret_code
!= LTTNG_OK
) {
1056 ERR("Failed to remove application tracer event source from notification thread");
1059 status
= event_notifier_error_accounting_unregister_app(app
);
1060 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1061 ERR("Error unregistering app from event notifier error accounting");
1064 lttng_ust_ctl_release_object(sock
, app
->event_notifier_group
.object
);
1065 free(app
->event_notifier_group
.object
);
1068 event_notifier_write_fd_is_open
= lttng_pipe_is_write_open(
1069 app
->event_notifier_group
.event_pipe
);
1070 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1072 * Release the file descriptors reserved for the event notifier pipe.
1073 * The app could be destroyed before the write end of the pipe could be
1074 * passed to the application (and closed). In that case, both file
1075 * descriptors must be released.
1077 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1080 * Wait until we have deleted the application from the sock hash table
1081 * before closing this socket, otherwise an application could re-use the
1082 * socket ID and race with the teardown, using the same hash table entry.
1084 * It's OK to leave the close in call_rcu. We want it to stay unique for
1085 * all RCU readers that could run concurrently with unregister app,
1086 * therefore we _need_ to only close that socket after a grace period. So
1087 * it should stay in this RCU callback.
1089 * This close() is a very important step of the synchronization model so
1090 * every modification to this function must be carefully reviewed.
1096 lttng_fd_put(LTTNG_FD_APPS
, 1);
1098 DBG2("UST app pid %d deleted", app
->pid
);
1100 session_unlock_list();
1104 * URCU intermediate call to delete an UST app.
1107 void delete_ust_app_rcu(struct rcu_head
*head
)
1109 struct lttng_ht_node_ulong
*node
=
1110 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1111 struct ust_app
*app
=
1112 caa_container_of(node
, struct ust_app
, pid_n
);
1114 DBG3("Call RCU deleting app PID %d", app
->pid
);
1115 delete_ust_app(app
);
1119 * Delete the session from the application ht and delete the data structure by
1120 * freeing every object inside and releasing them.
1122 * The session list lock must be held by the caller.
1124 static void destroy_app_session(struct ust_app
*app
,
1125 struct ust_app_session
*ua_sess
)
1128 struct lttng_ht_iter iter
;
1131 LTTNG_ASSERT(ua_sess
);
1133 iter
.iter
.node
= &ua_sess
->node
.node
;
1134 ret
= lttng_ht_del(app
->sessions
, &iter
);
1136 /* Already scheduled for teardown. */
1140 /* Once deleted, free the data structure. */
1141 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1148 * Alloc new UST app session.
1151 struct ust_app_session
*alloc_ust_app_session(void)
1153 struct ust_app_session
*ua_sess
;
1155 /* Init most of the default value by allocating and zeroing */
1156 ua_sess
= (ust_app_session
*) zmalloc(sizeof(struct ust_app_session
));
1157 if (ua_sess
== NULL
) {
1162 ua_sess
->handle
= -1;
1163 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1164 ua_sess
->metadata_attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1165 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1174 * Alloc new UST app channel.
1177 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1178 struct ust_app_session
*ua_sess
,
1179 struct lttng_ust_abi_channel_attr
*attr
)
1181 struct ust_app_channel
*ua_chan
;
1183 /* Init most of the default value by allocating and zeroing */
1184 ua_chan
= (ust_app_channel
*) zmalloc(sizeof(struct ust_app_channel
));
1185 if (ua_chan
== NULL
) {
1190 /* Setup channel name */
1191 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1192 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1194 ua_chan
->enabled
= 1;
1195 ua_chan
->handle
= -1;
1196 ua_chan
->session
= ua_sess
;
1197 ua_chan
->key
= get_next_channel_key();
1198 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1199 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1200 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1202 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1203 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1205 /* Copy attributes */
1207 /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */
1208 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1209 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1210 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1211 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1212 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1213 ua_chan
->attr
.output
= (lttng_ust_abi_output
) attr
->output
;
1214 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1216 /* By default, the channel is a per cpu channel. */
1217 ua_chan
->attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1219 DBG3("UST app channel %s allocated", ua_chan
->name
);
1228 * Allocate and initialize a UST app stream.
1230 * Return newly allocated stream pointer or NULL on error.
1232 struct ust_app_stream
*ust_app_alloc_stream(void)
1234 struct ust_app_stream
*stream
= NULL
;
1236 stream
= (ust_app_stream
*) zmalloc(sizeof(*stream
));
1237 if (stream
== NULL
) {
1238 PERROR("zmalloc ust app stream");
1242 /* Zero could be a valid value for a handle so flag it to -1. */
1243 stream
->handle
= -1;
1250 * Alloc new UST app event.
1253 struct ust_app_event
*alloc_ust_app_event(char *name
,
1254 struct lttng_ust_abi_event
*attr
)
1256 struct ust_app_event
*ua_event
;
1258 /* Init most of the default value by allocating and zeroing */
1259 ua_event
= (ust_app_event
*) zmalloc(sizeof(struct ust_app_event
));
1260 if (ua_event
== NULL
) {
1261 PERROR("Failed to allocate ust_app_event structure");
1265 ua_event
->enabled
= 1;
1266 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1267 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1268 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1270 /* Copy attributes */
1272 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1275 DBG3("UST app event %s allocated", ua_event
->name
);
1284 * Allocate a new UST app event notifier rule.
1286 static struct ust_app_event_notifier_rule
*alloc_ust_app_event_notifier_rule(
1287 struct lttng_trigger
*trigger
)
1289 enum lttng_event_rule_generate_exclusions_status
1290 generate_exclusion_status
;
1291 enum lttng_condition_status cond_status
;
1292 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1293 struct lttng_condition
*condition
= NULL
;
1294 const struct lttng_event_rule
*event_rule
= NULL
;
1296 ua_event_notifier_rule
= (ust_app_event_notifier_rule
*) zmalloc(sizeof(struct ust_app_event_notifier_rule
));
1297 if (ua_event_notifier_rule
== NULL
) {
1298 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1302 ua_event_notifier_rule
->enabled
= 1;
1303 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1304 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
,
1305 ua_event_notifier_rule
->token
);
1307 condition
= lttng_trigger_get_condition(trigger
);
1308 LTTNG_ASSERT(condition
);
1309 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
1310 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
1312 cond_status
= lttng_condition_event_rule_matches_get_rule(
1313 condition
, &event_rule
);
1314 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
1315 LTTNG_ASSERT(event_rule
);
1317 ua_event_notifier_rule
->error_counter_index
=
1318 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
1319 /* Acquire the event notifier's reference to the trigger. */
1320 lttng_trigger_get(trigger
);
1322 ua_event_notifier_rule
->trigger
= trigger
;
1323 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1324 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1325 event_rule
, &ua_event_notifier_rule
->exclusion
);
1326 switch (generate_exclusion_status
) {
1327 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1328 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1331 /* Error occurred. */
1332 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1333 goto error_put_trigger
;
1336 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1337 ua_event_notifier_rule
->token
);
1339 return ua_event_notifier_rule
;
1342 lttng_trigger_put(trigger
);
1344 free(ua_event_notifier_rule
);
1349 * Alloc new UST app context.
1352 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1354 struct ust_app_ctx
*ua_ctx
;
1356 ua_ctx
= (ust_app_ctx
*) zmalloc(sizeof(struct ust_app_ctx
));
1357 if (ua_ctx
== NULL
) {
1361 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1364 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1365 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1366 char *provider_name
= NULL
, *ctx_name
= NULL
;
1368 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1369 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1370 if (!provider_name
|| !ctx_name
) {
1371 free(provider_name
);
1376 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1377 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1381 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1389 * Create a liblttng-ust filter bytecode from given bytecode.
1391 * Return allocated filter or NULL on error.
1393 static struct lttng_ust_abi_filter_bytecode
*create_ust_filter_bytecode_from_bytecode(
1394 const struct lttng_bytecode
*orig_f
)
1396 struct lttng_ust_abi_filter_bytecode
*filter
= NULL
;
1398 /* Copy filter bytecode. */
1399 filter
= (lttng_ust_abi_filter_bytecode
*) zmalloc(sizeof(*filter
) + orig_f
->len
);
1401 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1405 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1406 sizeof(struct lttng_ust_abi_filter_bytecode
));
1407 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1413 * Create a liblttng-ust capture bytecode from given bytecode.
1415 * Return allocated filter or NULL on error.
1417 static struct lttng_ust_abi_capture_bytecode
*
1418 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1420 struct lttng_ust_abi_capture_bytecode
*capture
= NULL
;
1422 /* Copy capture bytecode. */
1423 capture
= (lttng_ust_abi_capture_bytecode
*) zmalloc(sizeof(*capture
) + orig_f
->len
);
1425 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1429 LTTNG_ASSERT(sizeof(struct lttng_bytecode
) ==
1430 sizeof(struct lttng_ust_abi_capture_bytecode
));
1431 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1437 * Find an ust_app using the sock and return it. RCU read side lock must be
1438 * held before calling this helper function.
1440 struct ust_app
*ust_app_find_by_sock(int sock
)
1442 struct lttng_ht_node_ulong
*node
;
1443 struct lttng_ht_iter iter
;
1445 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1446 node
= lttng_ht_iter_get_node_ulong(&iter
);
1448 DBG2("UST app find by sock %d not found", sock
);
1452 return caa_container_of(node
, struct ust_app
, sock_n
);
1459 * Find an ust_app using the notify sock and return it. RCU read side lock must
1460 * be held before calling this helper function.
1462 static struct ust_app
*find_app_by_notify_sock(int sock
)
1464 struct lttng_ht_node_ulong
*node
;
1465 struct lttng_ht_iter iter
;
1467 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1469 node
= lttng_ht_iter_get_node_ulong(&iter
);
1471 DBG2("UST app find by notify sock %d not found", sock
);
1475 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1482 * Lookup for an ust app event based on event name, filter bytecode and the
1485 * Return an ust_app_event object or NULL on error.
1487 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1488 const char *name
, const struct lttng_bytecode
*filter
,
1490 const struct lttng_event_exclusion
*exclusion
)
1492 struct lttng_ht_iter iter
;
1493 struct lttng_ht_node_str
*node
;
1494 struct ust_app_event
*event
= NULL
;
1495 struct ust_app_ht_key key
;
1500 /* Setup key for event lookup. */
1502 key
.filter
= filter
;
1503 key
.loglevel_type
= (lttng_ust_abi_loglevel_type
) loglevel_value
;
1504 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1505 key
.exclusion
= exclusion
;
1507 /* Lookup using the event name as hash and a custom match fct. */
1508 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1509 ht_match_ust_app_event
, &key
, &iter
.iter
);
1510 node
= lttng_ht_iter_get_node_str(&iter
);
1515 event
= caa_container_of(node
, struct ust_app_event
, node
);
1522 * Look-up an event notifier rule based on its token id.
1524 * Must be called with the RCU read lock held.
1525 * Return an ust_app_event_notifier_rule object or NULL on error.
1527 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(
1528 struct lttng_ht
*ht
, uint64_t token
)
1530 struct lttng_ht_iter iter
;
1531 struct lttng_ht_node_u64
*node
;
1532 struct ust_app_event_notifier_rule
*event_notifier_rule
= NULL
;
1536 lttng_ht_lookup(ht
, &token
, &iter
);
1537 node
= lttng_ht_iter_get_node_u64(&iter
);
1539 DBG2("UST app event notifier rule token not found: token = %" PRIu64
,
1544 event_notifier_rule
= caa_container_of(
1545 node
, struct ust_app_event_notifier_rule
, node
);
1547 return event_notifier_rule
;
1551 * Create the channel context on the tracer.
1553 * Called with UST app session lock held.
1556 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1557 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1561 health_code_update();
1563 pthread_mutex_lock(&app
->sock_lock
);
1564 ret
= lttng_ust_ctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1565 ua_chan
->obj
, &ua_ctx
->obj
);
1566 pthread_mutex_unlock(&app
->sock_lock
);
1568 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1570 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1571 app
->pid
, app
->sock
);
1572 } else if (ret
== -EAGAIN
) {
1574 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1575 app
->pid
, app
->sock
);
1577 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1578 ret
, app
->pid
, app
->sock
);
1583 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1585 DBG2("UST app context handle %d created successfully for channel %s",
1586 ua_ctx
->handle
, ua_chan
->name
);
1589 health_code_update();
1594 * Set the filter on the tracer.
1596 static int set_ust_object_filter(struct ust_app
*app
,
1597 const struct lttng_bytecode
*bytecode
,
1598 struct lttng_ust_abi_object_data
*ust_object
)
1601 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= NULL
;
1603 health_code_update();
1605 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1606 if (!ust_bytecode
) {
1607 ret
= -LTTNG_ERR_NOMEM
;
1610 pthread_mutex_lock(&app
->sock_lock
);
1611 ret
= lttng_ust_ctl_set_filter(app
->sock
, ust_bytecode
,
1613 pthread_mutex_unlock(&app
->sock_lock
);
1615 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1617 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1618 app
->pid
, app
->sock
);
1619 } else if (ret
== -EAGAIN
) {
1621 WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d",
1622 app
->pid
, app
->sock
);
1624 ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p",
1625 ret
, app
->pid
, app
->sock
, ust_object
);
1630 DBG2("UST filter successfully set: object = %p", ust_object
);
1633 health_code_update();
1639 * Set a capture bytecode for the passed object.
1640 * The sequence number enforces the ordering at runtime and on reception of
1641 * the captured payloads.
1643 static int set_ust_capture(struct ust_app
*app
,
1644 const struct lttng_bytecode
*bytecode
,
1645 unsigned int capture_seqnum
,
1646 struct lttng_ust_abi_object_data
*ust_object
)
1649 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= NULL
;
1651 health_code_update();
1653 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1654 if (!ust_bytecode
) {
1655 ret
= -LTTNG_ERR_NOMEM
;
1660 * Set the sequence number to ensure the capture of fields is ordered.
1662 ust_bytecode
->seqnum
= capture_seqnum
;
1664 pthread_mutex_lock(&app
->sock_lock
);
1665 ret
= lttng_ust_ctl_set_capture(app
->sock
, ust_bytecode
,
1667 pthread_mutex_unlock(&app
->sock_lock
);
1669 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1671 DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d",
1672 app
->pid
, app
->sock
);
1673 } else if (ret
== -EAGAIN
) {
1675 DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d",
1676 app
->pid
, app
->sock
);
1678 ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d",
1686 DBG2("UST capture successfully set: object = %p", ust_object
);
1689 health_code_update();
1695 struct lttng_ust_abi_event_exclusion
*create_ust_exclusion_from_exclusion(
1696 const struct lttng_event_exclusion
*exclusion
)
1698 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= NULL
;
1699 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1700 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1702 ust_exclusion
= (lttng_ust_abi_event_exclusion
*) zmalloc(exclusion_alloc_size
);
1703 if (!ust_exclusion
) {
1708 LTTNG_ASSERT(sizeof(struct lttng_event_exclusion
) ==
1709 sizeof(struct lttng_ust_abi_event_exclusion
));
1710 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1712 return ust_exclusion
;
1716 * Set event exclusions on the tracer.
1718 static int set_ust_object_exclusions(struct ust_app
*app
,
1719 const struct lttng_event_exclusion
*exclusions
,
1720 struct lttng_ust_abi_object_data
*ust_object
)
1723 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= NULL
;
1725 LTTNG_ASSERT(exclusions
&& exclusions
->count
> 0);
1727 health_code_update();
1729 ust_exclusions
= create_ust_exclusion_from_exclusion(
1731 if (!ust_exclusions
) {
1732 ret
= -LTTNG_ERR_NOMEM
;
1735 pthread_mutex_lock(&app
->sock_lock
);
1736 ret
= lttng_ust_ctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1737 pthread_mutex_unlock(&app
->sock_lock
);
1739 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1741 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1742 app
->pid
, app
->sock
);
1743 } else if (ret
== -EAGAIN
) {
1745 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1746 app
->pid
, app
->sock
);
1748 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p",
1749 ret
, app
->pid
, app
->sock
, ust_object
);
1754 DBG2("UST exclusions set successfully for object %p", ust_object
);
1757 health_code_update();
1758 free(ust_exclusions
);
1763 * Disable the specified event on to UST tracer for the UST session.
1765 static int disable_ust_object(struct ust_app
*app
,
1766 struct lttng_ust_abi_object_data
*object
)
1770 health_code_update();
1772 pthread_mutex_lock(&app
->sock_lock
);
1773 ret
= lttng_ust_ctl_disable(app
->sock
, object
);
1774 pthread_mutex_unlock(&app
->sock_lock
);
1776 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1778 DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d",
1779 app
->pid
, app
->sock
);
1780 } else if (ret
== -EAGAIN
) {
1782 WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d",
1783 app
->pid
, app
->sock
);
1785 ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p",
1786 ret
, app
->pid
, app
->sock
, object
);
1791 DBG2("UST app object %p disabled successfully for app: pid = %d",
1795 health_code_update();
1800 * Disable the specified channel on to UST tracer for the UST session.
1802 static int disable_ust_channel(struct ust_app
*app
,
1803 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1807 health_code_update();
1809 pthread_mutex_lock(&app
->sock_lock
);
1810 ret
= lttng_ust_ctl_disable(app
->sock
, ua_chan
->obj
);
1811 pthread_mutex_unlock(&app
->sock_lock
);
1813 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1815 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1816 app
->pid
, app
->sock
);
1817 } else if (ret
== -EAGAIN
) {
1819 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1820 app
->pid
, app
->sock
);
1822 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1823 ua_chan
->name
, ua_sess
->handle
, ret
,
1824 app
->pid
, app
->sock
);
1829 DBG2("UST app channel %s disabled successfully for app: pid = %d",
1830 ua_chan
->name
, app
->pid
);
1833 health_code_update();
1838 * Enable the specified channel on to UST tracer for the UST session.
1840 static int enable_ust_channel(struct ust_app
*app
,
1841 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1845 health_code_update();
1847 pthread_mutex_lock(&app
->sock_lock
);
1848 ret
= lttng_ust_ctl_enable(app
->sock
, ua_chan
->obj
);
1849 pthread_mutex_unlock(&app
->sock_lock
);
1851 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1853 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1854 ua_chan
->name
, app
->pid
, app
->sock
);
1855 } else if (ret
== -EAGAIN
) {
1857 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1858 ua_chan
->name
, app
->pid
, app
->sock
);
1860 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1861 ua_chan
->name
, ua_sess
->handle
, ret
,
1862 app
->pid
, app
->sock
);
1867 ua_chan
->enabled
= 1;
1869 DBG2("UST app channel %s enabled successfully for app: pid = %d",
1870 ua_chan
->name
, app
->pid
);
1873 health_code_update();
1878 * Enable the specified event on to UST tracer for the UST session.
1880 static int enable_ust_object(
1881 struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1885 health_code_update();
1887 pthread_mutex_lock(&app
->sock_lock
);
1888 ret
= lttng_ust_ctl_enable(app
->sock
, ust_object
);
1889 pthread_mutex_unlock(&app
->sock_lock
);
1891 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1893 DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d",
1894 app
->pid
, app
->sock
);
1895 } else if (ret
== -EAGAIN
) {
1897 WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d",
1898 app
->pid
, app
->sock
);
1900 ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p",
1901 ret
, app
->pid
, app
->sock
, ust_object
);
1906 DBG2("UST app object %p enabled successfully for app: pid = %d",
1907 ust_object
, app
->pid
);
1910 health_code_update();
1915 * Send channel and stream buffer to application.
1917 * Return 0 on success. On error, a negative value is returned.
1919 static int send_channel_pid_to_ust(struct ust_app
*app
,
1920 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1923 struct ust_app_stream
*stream
, *stmp
;
1926 LTTNG_ASSERT(ua_sess
);
1927 LTTNG_ASSERT(ua_chan
);
1929 health_code_update();
1931 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1934 /* Send channel to the application. */
1935 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1936 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1937 ret
= -ENOTCONN
; /* Caused by app exiting. */
1939 } else if (ret
== -EAGAIN
) {
1940 /* Caused by timeout. */
1941 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
"\".",
1942 app
->pid
, ua_chan
->name
, ua_sess
->tracing_id
);
1943 /* Treat this the same way as an application that is exiting. */
1946 } else if (ret
< 0) {
1950 health_code_update();
1952 /* Send all streams to application. */
1953 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1954 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1955 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1956 ret
= -ENOTCONN
; /* Caused by app exiting. */
1958 } else if (ret
== -EAGAIN
) {
1959 /* Caused by timeout. */
1960 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
"\".",
1961 app
->pid
, stream
->name
, ua_chan
->name
,
1962 ua_sess
->tracing_id
);
1964 * Treat this the same way as an application that is
1968 } else if (ret
< 0) {
1971 /* We don't need the stream anymore once sent to the tracer. */
1972 cds_list_del(&stream
->list
);
1973 delete_ust_app_stream(-1, stream
, app
);
1975 /* Flag the channel that it is sent to the application. */
1976 ua_chan
->is_sent
= 1;
1979 health_code_update();
1984 * Create the specified event onto the UST tracer for a UST session.
1986 * Should be called with session mutex held.
1989 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1990 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1994 health_code_update();
1996 /* Create UST event on tracer */
1997 pthread_mutex_lock(&app
->sock_lock
);
1998 ret
= lttng_ust_ctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
2000 pthread_mutex_unlock(&app
->sock_lock
);
2002 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2004 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
2005 app
->pid
, app
->sock
);
2006 } else if (ret
== -EAGAIN
) {
2008 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
2009 app
->pid
, app
->sock
);
2011 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
2012 ua_event
->attr
.name
, ret
, app
->pid
,
2018 ua_event
->handle
= ua_event
->obj
->handle
;
2020 DBG2("UST app event %s created successfully for pid:%d object = %p",
2021 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
2023 health_code_update();
2025 /* Set filter if one is present. */
2026 if (ua_event
->filter
) {
2027 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
2033 /* Set exclusions for the event */
2034 if (ua_event
->exclusion
) {
2035 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
2041 /* If event not enabled, disable it on the tracer */
2042 if (ua_event
->enabled
) {
2044 * We now need to explicitly enable the event, since it
2045 * is now disabled at creation.
2047 ret
= enable_ust_object(app
, ua_event
->obj
);
2050 * If we hit an EPERM, something is wrong with our enable call. If
2051 * we get an EEXIST, there is a problem on the tracer side since we
2055 case -LTTNG_UST_ERR_PERM
:
2056 /* Code flow problem */
2058 case -LTTNG_UST_ERR_EXIST
:
2059 /* It's OK for our use case. */
2070 health_code_update();
2074 static int init_ust_event_notifier_from_event_rule(
2075 const struct lttng_event_rule
*rule
,
2076 struct lttng_ust_abi_event_notifier
*event_notifier
)
2078 enum lttng_event_rule_status status
;
2079 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2080 int loglevel
= -1, ret
= 0;
2081 const char *pattern
;
2084 memset(event_notifier
, 0, sizeof(*event_notifier
));
2086 if (lttng_event_rule_targets_agent_domain(rule
)) {
2088 * Special event for agents
2089 * The actual meat of the event is in the filter that will be
2090 * attached later on.
2091 * Set the default values for the agent event.
2093 pattern
= event_get_default_agent_ust_name(
2094 lttng_event_rule_get_domain_type(rule
));
2096 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2098 const struct lttng_log_level_rule
*log_level_rule
;
2100 LTTNG_ASSERT(lttng_event_rule_get_type(rule
) ==
2101 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
);
2103 status
= lttng_event_rule_user_tracepoint_get_name_pattern(rule
, &pattern
);
2104 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2105 /* At this point, this is a fatal error. */
2109 status
= lttng_event_rule_user_tracepoint_get_log_level_rule(
2110 rule
, &log_level_rule
);
2111 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2112 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2113 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2114 enum lttng_log_level_rule_status llr_status
;
2116 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2117 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2118 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2119 llr_status
= lttng_log_level_rule_exactly_get_level(
2120 log_level_rule
, &loglevel
);
2122 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2123 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2124 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(
2125 log_level_rule
, &loglevel
);
2131 LTTNG_ASSERT(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2133 /* At this point this is a fatal error. */
2138 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2139 ret
= lttng_strncpy(event_notifier
->event
.name
, pattern
,
2140 LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
2142 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
2147 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2148 event_notifier
->event
.loglevel
= loglevel
;
2154 * Create the specified event notifier against the user space tracer of a
2155 * given application.
2157 static int create_ust_event_notifier(struct ust_app
*app
,
2158 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2161 enum lttng_condition_status condition_status
;
2162 const struct lttng_condition
*condition
= NULL
;
2163 struct lttng_ust_abi_event_notifier event_notifier
;
2164 const struct lttng_event_rule
*event_rule
= NULL
;
2165 unsigned int capture_bytecode_count
= 0, i
;
2166 enum lttng_condition_status cond_status
;
2167 enum lttng_event_rule_type event_rule_type
;
2169 health_code_update();
2170 LTTNG_ASSERT(app
->event_notifier_group
.object
);
2172 condition
= lttng_trigger_get_const_condition(
2173 ua_event_notifier_rule
->trigger
);
2174 LTTNG_ASSERT(condition
);
2175 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
2176 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2178 condition_status
= lttng_condition_event_rule_matches_get_rule(
2179 condition
, &event_rule
);
2180 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2182 LTTNG_ASSERT(event_rule
);
2184 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2185 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
||
2186 event_rule_type
== LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
||
2188 LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
||
2190 LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
);
2192 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2193 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2194 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2196 /* Create UST event notifier against the tracer. */
2197 pthread_mutex_lock(&app
->sock_lock
);
2198 ret
= lttng_ust_ctl_create_event_notifier(app
->sock
, &event_notifier
,
2199 app
->event_notifier_group
.object
,
2200 &ua_event_notifier_rule
->obj
);
2201 pthread_mutex_unlock(&app
->sock_lock
);
2203 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2205 DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d",
2206 app
->pid
, app
->sock
);
2207 } else if (ret
== -EAGAIN
) {
2209 WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d",
2210 app
->pid
, app
->sock
);
2212 ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d",
2213 event_notifier
.event
.name
, ret
, app
->pid
,
2219 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2221 DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p",
2222 event_notifier
.event
.name
, app
->name
, app
->pid
,
2223 ua_event_notifier_rule
->obj
);
2225 health_code_update();
2227 /* Set filter if one is present. */
2228 if (ua_event_notifier_rule
->filter
) {
2229 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2230 ua_event_notifier_rule
->obj
);
2236 /* Set exclusions for the event. */
2237 if (ua_event_notifier_rule
->exclusion
) {
2238 ret
= set_ust_object_exclusions(app
,
2239 ua_event_notifier_rule
->exclusion
,
2240 ua_event_notifier_rule
->obj
);
2246 /* Set the capture bytecodes. */
2247 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2248 condition
, &capture_bytecode_count
);
2249 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2251 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2252 const struct lttng_bytecode
*capture_bytecode
=
2253 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
2256 ret
= set_ust_capture(app
, capture_bytecode
, i
,
2257 ua_event_notifier_rule
->obj
);
2264 * We now need to explicitly enable the event, since it
2265 * is disabled at creation.
2267 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2270 * If we hit an EPERM, something is wrong with our enable call.
2271 * If we get an EEXIST, there is a problem on the tracer side
2272 * since we just created it.
2275 case -LTTNG_UST_ERR_PERM
:
2276 /* Code flow problem. */
2278 case -LTTNG_UST_ERR_EXIST
:
2279 /* It's OK for our use case. */
2289 ua_event_notifier_rule
->enabled
= true;
2292 health_code_update();
2297 * Copy data between an UST app event and a LTT event.
2299 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2300 struct ltt_ust_event
*uevent
)
2302 size_t exclusion_alloc_size
;
2304 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2305 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2307 ua_event
->enabled
= uevent
->enabled
;
2309 /* Copy event attributes */
2310 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2312 /* Copy filter bytecode */
2313 if (uevent
->filter
) {
2314 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2315 /* Filter might be NULL here in case of ENONEM. */
2318 /* Copy exclusion data */
2319 if (uevent
->exclusion
) {
2320 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2321 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2322 ua_event
->exclusion
= (lttng_event_exclusion
*) zmalloc(exclusion_alloc_size
);
2323 if (ua_event
->exclusion
== NULL
) {
2326 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2327 exclusion_alloc_size
);
2333 * Copy data between an UST app channel and a LTT channel.
2335 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2336 struct ltt_ust_channel
*uchan
)
2338 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2340 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2341 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2343 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2344 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2346 /* Copy event attributes since the layout is different. */
2347 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2348 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2349 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2350 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2351 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2352 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2353 ua_chan
->attr
.output
= (lttng_ust_abi_output
) uchan
->attr
.output
;
2354 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2357 * Note that the attribute channel type is not set since the channel on the
2358 * tracing registry side does not have this information.
2361 ua_chan
->enabled
= uchan
->enabled
;
2362 ua_chan
->tracing_channel_id
= uchan
->id
;
2364 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2368 * Copy data between a UST app session and a regular LTT session.
2370 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2371 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2373 struct tm
*timeinfo
;
2376 char tmp_shm_path
[PATH_MAX
];
2378 timeinfo
= localtime(&app
->registration_time
);
2379 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2381 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2383 ua_sess
->tracing_id
= usess
->id
;
2384 ua_sess
->id
= get_next_session_id();
2385 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2386 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2387 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2388 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2389 ua_sess
->buffer_type
= usess
->buffer_type
;
2390 ua_sess
->bits_per_long
= app
->bits_per_long
;
2392 /* There is only one consumer object per session possible. */
2393 consumer_output_get(usess
->consumer
);
2394 ua_sess
->consumer
= usess
->consumer
;
2396 ua_sess
->output_traces
= usess
->output_traces
;
2397 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2398 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2399 &usess
->metadata_attr
);
2401 switch (ua_sess
->buffer_type
) {
2402 case LTTNG_BUFFER_PER_PID
:
2403 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2404 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2407 case LTTNG_BUFFER_PER_UID
:
2408 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2409 DEFAULT_UST_TRACE_UID_PATH
,
2410 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2411 app
->bits_per_long
);
2418 PERROR("asprintf UST shadow copy session");
2423 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2424 sizeof(ua_sess
->root_shm_path
));
2425 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2426 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2427 sizeof(ua_sess
->shm_path
));
2428 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2429 if (ua_sess
->shm_path
[0]) {
2430 switch (ua_sess
->buffer_type
) {
2431 case LTTNG_BUFFER_PER_PID
:
2432 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2433 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2434 app
->name
, app
->pid
, datetime
);
2436 case LTTNG_BUFFER_PER_UID
:
2437 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2438 "/" DEFAULT_UST_TRACE_UID_PATH
,
2439 app
->uid
, app
->bits_per_long
);
2446 PERROR("sprintf UST shadow copy session");
2450 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2451 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2452 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2457 consumer_output_put(ua_sess
->consumer
);
2461 * Lookup sesison wrapper.
2464 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2465 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2467 /* Get right UST app session from app */
2468 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2472 * Return ust app session from the app session hashtable using the UST session
2475 static struct ust_app_session
*lookup_session_by_app(
2476 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2478 struct lttng_ht_iter iter
;
2479 struct lttng_ht_node_u64
*node
;
2481 __lookup_session_by_app(usess
, app
, &iter
);
2482 node
= lttng_ht_iter_get_node_u64(&iter
);
2487 return caa_container_of(node
, struct ust_app_session
, node
);
2494 * Setup buffer registry per PID for the given session and application. If none
2495 * is found, a new one is created, added to the global registry and
2496 * initialized. If regp is valid, it's set with the newly created object.
2498 * Return 0 on success or else a negative value.
2500 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2501 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2504 struct buffer_reg_pid
*reg_pid
;
2506 LTTNG_ASSERT(ua_sess
);
2511 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2514 * This is the create channel path meaning that if there is NO
2515 * registry available, we have to create one for this session.
2517 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2518 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2526 /* Initialize registry. */
2527 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2528 app
->bits_per_long
, app
->uint8_t_alignment
,
2529 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2530 app
->uint64_t_alignment
, app
->long_alignment
,
2531 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2532 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2533 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2534 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2535 ua_sess
->tracing_id
,
2539 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2540 * destroy the buffer registry, because it is always expected
2541 * that if the buffer registry can be found, its ust registry is
2544 buffer_reg_pid_destroy(reg_pid
);
2548 buffer_reg_pid_add(reg_pid
);
2550 DBG3("UST app buffer registry per PID created successfully");
2562 * Setup buffer registry per UID for the given session and application. If none
2563 * is found, a new one is created, added to the global registry and
2564 * initialized. If regp is valid, it's set with the newly created object.
2566 * Return 0 on success or else a negative value.
2568 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2569 struct ust_app_session
*ua_sess
,
2570 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2573 struct buffer_reg_uid
*reg_uid
;
2575 LTTNG_ASSERT(usess
);
2580 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2583 * This is the create channel path meaning that if there is NO
2584 * registry available, we have to create one for this session.
2586 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2587 LTTNG_DOMAIN_UST
, ®_uid
,
2588 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2596 /* Initialize registry. */
2597 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2598 app
->bits_per_long
, app
->uint8_t_alignment
,
2599 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2600 app
->uint64_t_alignment
, app
->long_alignment
,
2601 app
->byte_order
, app
->version
.major
,
2602 app
->version
.minor
, reg_uid
->root_shm_path
,
2603 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2604 ua_sess
->tracing_id
, app
->uid
);
2607 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2608 * destroy the buffer registry, because it is always expected
2609 * that if the buffer registry can be found, its ust registry is
2612 buffer_reg_uid_destroy(reg_uid
, NULL
);
2615 /* Add node to teardown list of the session. */
2616 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2618 buffer_reg_uid_add(reg_uid
);
2620 DBG3("UST app buffer registry per UID created successfully");
2631 * Create a session on the tracer side for the given app.
2633 * On success, ua_sess_ptr is populated with the session pointer or else left
2634 * untouched. If the session was created, is_created is set to 1. On error,
2635 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2638 * Returns 0 on success or else a negative code which is either -ENOMEM or
2639 * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails.
2641 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2642 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2645 int ret
, created
= 0;
2646 struct ust_app_session
*ua_sess
;
2648 LTTNG_ASSERT(usess
);
2650 LTTNG_ASSERT(ua_sess_ptr
);
2652 health_code_update();
2654 ua_sess
= lookup_session_by_app(usess
, app
);
2655 if (ua_sess
== NULL
) {
2656 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2657 app
->pid
, usess
->id
);
2658 ua_sess
= alloc_ust_app_session();
2659 if (ua_sess
== NULL
) {
2660 /* Only malloc can failed so something is really wrong */
2664 shadow_copy_session(ua_sess
, usess
, app
);
2668 switch (usess
->buffer_type
) {
2669 case LTTNG_BUFFER_PER_PID
:
2670 /* Init local registry. */
2671 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2673 delete_ust_app_session(-1, ua_sess
, app
);
2677 case LTTNG_BUFFER_PER_UID
:
2678 /* Look for a global registry. If none exists, create one. */
2679 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2681 delete_ust_app_session(-1, ua_sess
, app
);
2691 health_code_update();
2693 if (ua_sess
->handle
== -1) {
2694 pthread_mutex_lock(&app
->sock_lock
);
2695 ret
= lttng_ust_ctl_create_session(app
->sock
);
2696 pthread_mutex_unlock(&app
->sock_lock
);
2698 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2699 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2700 app
->pid
, app
->sock
);
2702 } else if (ret
== -EAGAIN
) {
2703 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2704 app
->pid
, app
->sock
);
2707 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2708 ret
, app
->pid
, app
->sock
);
2710 delete_ust_app_session(-1, ua_sess
, app
);
2711 if (ret
!= -ENOMEM
) {
2713 * Tracer is probably gone or got an internal error so let's
2714 * behave like it will soon unregister or not usable.
2721 ua_sess
->handle
= ret
;
2723 /* Add ust app session to app's HT */
2724 lttng_ht_node_init_u64(&ua_sess
->node
,
2725 ua_sess
->tracing_id
);
2726 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2727 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2728 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2729 &ua_sess
->ust_objd_node
);
2731 DBG2("UST app session created successfully with handle %d", ret
);
2734 *ua_sess_ptr
= ua_sess
;
2736 *is_created
= created
;
2739 /* Everything went well. */
2743 health_code_update();
2748 * Match function for a hash table lookup of ust_app_ctx.
2750 * It matches an ust app context based on the context type and, in the case
2751 * of perf counters, their name.
2753 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2755 struct ust_app_ctx
*ctx
;
2756 const struct lttng_ust_context_attr
*key
;
2761 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2762 key
= (lttng_ust_context_attr
*) _key
;
2765 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2770 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2771 if (strncmp(key
->u
.perf_counter
.name
,
2772 ctx
->ctx
.u
.perf_counter
.name
,
2773 sizeof(key
->u
.perf_counter
.name
))) {
2777 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2778 if (strcmp(key
->u
.app_ctx
.provider_name
,
2779 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2780 strcmp(key
->u
.app_ctx
.ctx_name
,
2781 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2797 * Lookup for an ust app context from an lttng_ust_context.
2799 * Must be called while holding RCU read side lock.
2800 * Return an ust_app_ctx object or NULL on error.
2803 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2804 struct lttng_ust_context_attr
*uctx
)
2806 struct lttng_ht_iter iter
;
2807 struct lttng_ht_node_ulong
*node
;
2808 struct ust_app_ctx
*app_ctx
= NULL
;
2813 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2814 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2815 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2816 node
= lttng_ht_iter_get_node_ulong(&iter
);
2821 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2828 * Create a context for the channel on the tracer.
2830 * Called with UST app session lock held and a RCU read side lock.
2833 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2834 struct lttng_ust_context_attr
*uctx
,
2835 struct ust_app
*app
)
2838 struct ust_app_ctx
*ua_ctx
;
2840 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2842 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2848 ua_ctx
= alloc_ust_app_ctx(uctx
);
2849 if (ua_ctx
== NULL
) {
2855 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2856 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2857 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2859 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2869 * Enable on the tracer side a ust app event for the session and channel.
2871 * Called with UST app session lock held.
2874 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2875 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2879 ret
= enable_ust_object(app
, ua_event
->obj
);
2884 ua_event
->enabled
= 1;
2891 * Disable on the tracer side a ust app event for the session and channel.
2893 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2894 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2898 ret
= disable_ust_object(app
, ua_event
->obj
);
2903 ua_event
->enabled
= 0;
2910 * Lookup ust app channel for session and disable it on the tracer side.
2913 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2914 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2918 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2923 ua_chan
->enabled
= 0;
2930 * Lookup ust app channel for session and enable it on the tracer side. This
2931 * MUST be called with a RCU read side lock acquired.
2933 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2934 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2937 struct lttng_ht_iter iter
;
2938 struct lttng_ht_node_str
*ua_chan_node
;
2939 struct ust_app_channel
*ua_chan
;
2941 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2942 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2943 if (ua_chan_node
== NULL
) {
2944 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2945 uchan
->name
, ua_sess
->tracing_id
);
2949 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2951 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2961 * Ask the consumer to create a channel and get it if successful.
2963 * Called with UST app session lock held.
2965 * Return 0 on success or else a negative value.
2967 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2968 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2969 int bitness
, struct ust_registry_session
*registry
,
2970 uint64_t trace_archive_id
)
2973 unsigned int nb_fd
= 0;
2974 struct consumer_socket
*socket
;
2976 LTTNG_ASSERT(usess
);
2977 LTTNG_ASSERT(ua_sess
);
2978 LTTNG_ASSERT(ua_chan
);
2979 LTTNG_ASSERT(registry
);
2982 health_code_update();
2984 /* Get the right consumer socket for the application. */
2985 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2991 health_code_update();
2993 /* Need one fd for the channel. */
2994 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2996 ERR("Exhausted number of available FD upon create channel");
3001 * Ask consumer to create channel. The consumer will return the number of
3002 * stream we have to expect.
3004 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
3005 registry
, usess
->current_trace_chunk
);
3011 * Compute the number of fd needed before receiving them. It must be 2 per
3012 * stream (2 being the default value here).
3014 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
3016 /* Reserve the amount of file descriptor we need. */
3017 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
3019 ERR("Exhausted number of available FD upon create channel");
3020 goto error_fd_get_stream
;
3023 health_code_update();
3026 * Now get the channel from the consumer. This call will populate the stream
3027 * list of that channel and set the ust objects.
3029 if (usess
->consumer
->enabled
) {
3030 ret
= ust_consumer_get_channel(socket
, ua_chan
);
3040 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
3041 error_fd_get_stream
:
3043 * Initiate a destroy channel on the consumer since we had an error
3044 * handling it on our side. The return value is of no importance since we
3045 * already have a ret value set by the previous error that we need to
3048 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
3050 lttng_fd_put(LTTNG_FD_APPS
, 1);
3052 health_code_update();
3058 * Duplicate the ust data object of the ust app stream and save it in the
3059 * buffer registry stream.
3061 * Return 0 on success or else a negative value.
3063 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
3064 struct ust_app_stream
*stream
)
3068 LTTNG_ASSERT(reg_stream
);
3069 LTTNG_ASSERT(stream
);
3071 /* Duplicating a stream requires 2 new fds. Reserve them. */
3072 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3074 ERR("Exhausted number of available FD upon duplicate stream");
3078 /* Duplicate object for stream once the original is in the registry. */
3079 ret
= lttng_ust_ctl_duplicate_ust_object_data(&stream
->obj
,
3080 reg_stream
->obj
.ust
);
3082 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3083 reg_stream
->obj
.ust
, stream
->obj
, ret
);
3084 lttng_fd_put(LTTNG_FD_APPS
, 2);
3087 stream
->handle
= stream
->obj
->handle
;
3094 * Duplicate the ust data object of the ust app. channel and save it in the
3095 * buffer registry channel.
3097 * Return 0 on success or else a negative value.
3099 static int duplicate_channel_object(struct buffer_reg_channel
*buf_reg_chan
,
3100 struct ust_app_channel
*ua_chan
)
3104 LTTNG_ASSERT(buf_reg_chan
);
3105 LTTNG_ASSERT(ua_chan
);
3107 /* Duplicating a channel requires 1 new fd. Reserve it. */
3108 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3110 ERR("Exhausted number of available FD upon duplicate channel");
3114 /* Duplicate object for stream once the original is in the registry. */
3115 ret
= lttng_ust_ctl_duplicate_ust_object_data(&ua_chan
->obj
, buf_reg_chan
->obj
.ust
);
3117 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3118 buf_reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
3121 ua_chan
->handle
= ua_chan
->obj
->handle
;
3126 lttng_fd_put(LTTNG_FD_APPS
, 1);
3132 * For a given channel buffer registry, setup all streams of the given ust
3133 * application channel.
3135 * Return 0 on success or else a negative value.
3137 static int setup_buffer_reg_streams(struct buffer_reg_channel
*buf_reg_chan
,
3138 struct ust_app_channel
*ua_chan
,
3139 struct ust_app
*app
)
3142 struct ust_app_stream
*stream
, *stmp
;
3144 LTTNG_ASSERT(buf_reg_chan
);
3145 LTTNG_ASSERT(ua_chan
);
3147 DBG2("UST app setup buffer registry stream");
3149 /* Send all streams to application. */
3150 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3151 struct buffer_reg_stream
*reg_stream
;
3153 ret
= buffer_reg_stream_create(®_stream
);
3159 * Keep original pointer and nullify it in the stream so the delete
3160 * stream call does not release the object.
3162 reg_stream
->obj
.ust
= stream
->obj
;
3164 buffer_reg_stream_add(reg_stream
, buf_reg_chan
);
3166 /* We don't need the streams anymore. */
3167 cds_list_del(&stream
->list
);
3168 delete_ust_app_stream(-1, stream
, app
);
3176 * Create a buffer registry channel for the given session registry and
3177 * application channel object. If regp pointer is valid, it's set with the
3178 * created object. Important, the created object is NOT added to the session
3179 * registry hash table.
3181 * Return 0 on success else a negative value.
3183 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3184 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
3187 struct buffer_reg_channel
*buf_reg_chan
= NULL
;
3189 LTTNG_ASSERT(reg_sess
);
3190 LTTNG_ASSERT(ua_chan
);
3192 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3194 /* Create buffer registry channel. */
3195 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, &buf_reg_chan
);
3199 LTTNG_ASSERT(buf_reg_chan
);
3200 buf_reg_chan
->consumer_key
= ua_chan
->key
;
3201 buf_reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3202 buf_reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3204 /* Create and add a channel registry to session. */
3205 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
3206 ua_chan
->tracing_channel_id
);
3210 buffer_reg_channel_add(reg_sess
, buf_reg_chan
);
3213 *regp
= buf_reg_chan
;
3219 /* Safe because the registry channel object was not added to any HT. */
3220 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3226 * Setup buffer registry channel for the given session registry and application
3227 * channel object. If regp pointer is valid, it's set with the created object.
3229 * Return 0 on success else a negative value.
3231 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3232 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*buf_reg_chan
,
3233 struct ust_app
*app
)
3237 LTTNG_ASSERT(reg_sess
);
3238 LTTNG_ASSERT(buf_reg_chan
);
3239 LTTNG_ASSERT(ua_chan
);
3240 LTTNG_ASSERT(ua_chan
->obj
);
3242 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3244 /* Setup all streams for the registry. */
3245 ret
= setup_buffer_reg_streams(buf_reg_chan
, ua_chan
, app
);
3250 buf_reg_chan
->obj
.ust
= ua_chan
->obj
;
3251 ua_chan
->obj
= NULL
;
3256 buffer_reg_channel_remove(reg_sess
, buf_reg_chan
);
3257 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3262 * Send buffer registry channel to the application.
3264 * Return 0 on success else a negative value.
3266 static int send_channel_uid_to_ust(struct buffer_reg_channel
*buf_reg_chan
,
3267 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
3268 struct ust_app_channel
*ua_chan
)
3271 struct buffer_reg_stream
*reg_stream
;
3273 LTTNG_ASSERT(buf_reg_chan
);
3275 LTTNG_ASSERT(ua_sess
);
3276 LTTNG_ASSERT(ua_chan
);
3278 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3280 ret
= duplicate_channel_object(buf_reg_chan
, ua_chan
);
3285 /* Send channel to the application. */
3286 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3287 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3288 ret
= -ENOTCONN
; /* Caused by app exiting. */
3290 } else if (ret
== -EAGAIN
) {
3291 /* Caused by timeout. */
3292 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
"\".",
3293 app
->pid
, ua_chan
->name
, ua_sess
->tracing_id
);
3294 /* Treat this the same way as an application that is exiting. */
3297 } else if (ret
< 0) {
3301 health_code_update();
3303 /* Send all streams to application. */
3304 pthread_mutex_lock(&buf_reg_chan
->stream_list_lock
);
3305 cds_list_for_each_entry(reg_stream
, &buf_reg_chan
->streams
, lnode
) {
3306 struct ust_app_stream stream
;
3308 ret
= duplicate_stream_object(reg_stream
, &stream
);
3310 goto error_stream_unlock
;
3313 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3315 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3316 ret
= -ENOTCONN
; /* Caused by app exiting. */
3317 } else if (ret
== -EAGAIN
) {
3319 * Caused by timeout.
3320 * Treat this the same way as an application
3323 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
"\".",
3324 app
->pid
, stream
.name
,
3326 ua_sess
->tracing_id
);
3329 (void) release_ust_app_stream(-1, &stream
, app
);
3330 goto error_stream_unlock
;
3334 * The return value is not important here. This function will output an
3337 (void) release_ust_app_stream(-1, &stream
, app
);
3339 ua_chan
->is_sent
= 1;
3341 error_stream_unlock
:
3342 pthread_mutex_unlock(&buf_reg_chan
->stream_list_lock
);
3348 * Create and send to the application the created buffers with per UID buffers.
3350 * This MUST be called with a RCU read side lock acquired.
3351 * The session list lock and the session's lock must be acquired.
3353 * Return 0 on success else a negative value.
3355 static int create_channel_per_uid(struct ust_app
*app
,
3356 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3357 struct ust_app_channel
*ua_chan
)
3360 struct buffer_reg_uid
*reg_uid
;
3361 struct buffer_reg_channel
*buf_reg_chan
;
3362 struct ltt_session
*session
= NULL
;
3363 enum lttng_error_code notification_ret
;
3364 struct ust_registry_channel
*ust_reg_chan
;
3367 LTTNG_ASSERT(usess
);
3368 LTTNG_ASSERT(ua_sess
);
3369 LTTNG_ASSERT(ua_chan
);
3371 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3373 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3375 * The session creation handles the creation of this global registry
3376 * object. If none can be find, there is a code flow problem or a
3379 LTTNG_ASSERT(reg_uid
);
3381 buf_reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3387 /* Create the buffer registry channel object. */
3388 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, &buf_reg_chan
);
3390 ERR("Error creating the UST channel \"%s\" registry instance",
3395 session
= session_find_by_id(ua_sess
->tracing_id
);
3396 LTTNG_ASSERT(session
);
3397 LTTNG_ASSERT(pthread_mutex_trylock(&session
->lock
));
3398 LTTNG_ASSERT(session_trylock_list());
3401 * Create the buffers on the consumer side. This call populates the
3402 * ust app channel object with all streams and data object.
3404 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3405 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
3406 session
->most_recent_chunk_id
.value
);
3408 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3412 * Let's remove the previously created buffer registry channel so
3413 * it's not visible anymore in the session registry.
3415 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3416 ua_chan
->tracing_channel_id
, false);
3417 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
3418 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
3423 * Setup the streams and add it to the session registry.
3425 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3426 ua_chan
, buf_reg_chan
, app
);
3428 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3432 /* Notify the notification subsystem of the channel's creation. */
3433 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
3434 ust_reg_chan
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3435 ua_chan
->tracing_channel_id
);
3436 LTTNG_ASSERT(ust_reg_chan
);
3437 ust_reg_chan
->consumer_key
= ua_chan
->key
;
3438 ust_reg_chan
= NULL
;
3439 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
3441 notification_ret
= notification_thread_command_add_channel(
3442 the_notification_thread_handle
, session
->name
,
3443 lttng_credentials_get_uid(
3444 &ua_sess
->effective_credentials
),
3445 lttng_credentials_get_gid(
3446 &ua_sess
->effective_credentials
),
3447 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3448 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3449 if (notification_ret
!= LTTNG_OK
) {
3450 ret
= - (int) notification_ret
;
3451 ERR("Failed to add channel to notification thread");
3456 /* Send buffers to the application. */
3457 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
3459 if (ret
!= -ENOTCONN
) {
3460 ERR("Error sending channel to application");
3467 session_put(session
);
3473 * Create and send to the application the created buffers with per PID buffers.
3475 * Called with UST app session lock held.
3476 * The session list lock and the session's lock must be acquired.
3478 * Return 0 on success else a negative value.
3480 static int create_channel_per_pid(struct ust_app
*app
,
3481 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3482 struct ust_app_channel
*ua_chan
)
3485 struct ust_registry_session
*registry
;
3486 enum lttng_error_code cmd_ret
;
3487 struct ltt_session
*session
= NULL
;
3488 uint64_t chan_reg_key
;
3489 struct ust_registry_channel
*ust_reg_chan
;
3492 LTTNG_ASSERT(usess
);
3493 LTTNG_ASSERT(ua_sess
);
3494 LTTNG_ASSERT(ua_chan
);
3496 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3500 registry
= get_session_registry(ua_sess
);
3501 /* The UST app session lock is held, registry shall not be null. */
3502 LTTNG_ASSERT(registry
);
3504 /* Create and add a new channel registry to session. */
3505 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3507 ERR("Error creating the UST channel \"%s\" registry instance",
3512 session
= session_find_by_id(ua_sess
->tracing_id
);
3513 LTTNG_ASSERT(session
);
3515 LTTNG_ASSERT(pthread_mutex_trylock(&session
->lock
));
3516 LTTNG_ASSERT(session_trylock_list());
3518 /* Create and get channel on the consumer side. */
3519 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3520 app
->bits_per_long
, registry
,
3521 session
->most_recent_chunk_id
.value
);
3523 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3525 goto error_remove_from_registry
;
3528 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3530 if (ret
!= -ENOTCONN
) {
3531 ERR("Error sending channel to application");
3533 goto error_remove_from_registry
;
3536 chan_reg_key
= ua_chan
->key
;
3537 pthread_mutex_lock(®istry
->lock
);
3538 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
3539 LTTNG_ASSERT(ust_reg_chan
);
3540 ust_reg_chan
->consumer_key
= ua_chan
->key
;
3541 pthread_mutex_unlock(®istry
->lock
);
3543 cmd_ret
= notification_thread_command_add_channel(
3544 the_notification_thread_handle
, session
->name
,
3545 lttng_credentials_get_uid(
3546 &ua_sess
->effective_credentials
),
3547 lttng_credentials_get_gid(
3548 &ua_sess
->effective_credentials
),
3549 ua_chan
->name
, ua_chan
->key
, LTTNG_DOMAIN_UST
,
3550 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3551 if (cmd_ret
!= LTTNG_OK
) {
3552 ret
= - (int) cmd_ret
;
3553 ERR("Failed to add channel to notification thread");
3554 goto error_remove_from_registry
;
3557 error_remove_from_registry
:
3559 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3564 session_put(session
);
3570 * From an already allocated ust app channel, create the channel buffers if
3571 * needed and send them to the application. This MUST be called with a RCU read
3572 * side lock acquired.
3574 * Called with UST app session lock held.
3576 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3577 * the application exited concurrently.
3579 static int ust_app_channel_send(struct ust_app
*app
,
3580 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3581 struct ust_app_channel
*ua_chan
)
3586 LTTNG_ASSERT(usess
);
3587 LTTNG_ASSERT(usess
->active
);
3588 LTTNG_ASSERT(ua_sess
);
3589 LTTNG_ASSERT(ua_chan
);
3591 /* Handle buffer type before sending the channel to the application. */
3592 switch (usess
->buffer_type
) {
3593 case LTTNG_BUFFER_PER_UID
:
3595 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3601 case LTTNG_BUFFER_PER_PID
:
3603 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3615 /* Initialize ust objd object using the received handle and add it. */
3616 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3617 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3619 /* If channel is not enabled, disable it on the tracer */
3620 if (!ua_chan
->enabled
) {
3621 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3632 * Create UST app channel and return it through ua_chanp if not NULL.
3634 * Called with UST app session lock and RCU read-side lock held.
3636 * Return 0 on success or else a negative value.
3638 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3639 struct ltt_ust_channel
*uchan
,
3640 enum lttng_ust_abi_chan_type type
, struct ltt_ust_session
*usess
,
3641 struct ust_app_channel
**ua_chanp
)
3644 struct lttng_ht_iter iter
;
3645 struct lttng_ht_node_str
*ua_chan_node
;
3646 struct ust_app_channel
*ua_chan
;
3648 /* Lookup channel in the ust app session */
3649 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3650 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3651 if (ua_chan_node
!= NULL
) {
3652 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3656 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3657 if (ua_chan
== NULL
) {
3658 /* Only malloc can fail here */
3662 shadow_copy_channel(ua_chan
, uchan
);
3664 /* Set channel type. */
3665 ua_chan
->attr
.type
= type
;
3667 /* Only add the channel if successful on the tracer side. */
3668 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3671 *ua_chanp
= ua_chan
;
3674 /* Everything went well. */
3682 * Create UST app event and create it on the tracer side.
3684 * Must be called with the RCU read side lock held.
3685 * Called with ust app session mutex held.
3688 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3689 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3690 struct ust_app
*app
)
3693 struct ust_app_event
*ua_event
;
3695 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3696 if (ua_event
== NULL
) {
3697 /* Only failure mode of alloc_ust_app_event(). */
3701 shadow_copy_event(ua_event
, uevent
);
3703 /* Create it on the tracer side */
3704 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3707 * Not found previously means that it does not exist on the
3708 * tracer. If the application reports that the event existed,
3709 * it means there is a bug in the sessiond or lttng-ust
3710 * (or corruption, etc.)
3712 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3713 ERR("Tracer for application reported that an event being created already existed: "
3714 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3716 app
->pid
, app
->ppid
, app
->uid
,
3722 add_unique_ust_app_event(ua_chan
, ua_event
);
3724 DBG2("UST app create event completed: app = '%s' pid = %d",
3725 app
->name
, app
->pid
);
3731 /* Valid. Calling here is already in a read side lock */
3732 delete_ust_app_event(-1, ua_event
, app
);
3737 * Create UST app event notifier rule and create it on the tracer side.
3739 * Must be called with the RCU read side lock held.
3740 * Called with ust app session mutex held.
3743 int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
,
3744 struct ust_app
*app
)
3747 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3749 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3750 if (ua_event_notifier_rule
== NULL
) {
3755 /* Create it on the tracer side. */
3756 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3759 * Not found previously means that it does not exist on the
3760 * tracer. If the application reports that the event existed,
3761 * it means there is a bug in the sessiond or lttng-ust
3762 * (or corruption, etc.)
3764 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3765 ERR("Tracer for application reported that an event notifier being created already exists: "
3766 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3767 lttng_trigger_get_tracer_token(trigger
),
3768 app
->pid
, app
->ppid
, app
->uid
,
3774 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3775 &ua_event_notifier_rule
->node
);
3777 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64
,
3778 app
->name
, app
->pid
, lttng_trigger_get_tracer_token(trigger
));
3783 /* The RCU read side lock is already being held by the caller. */
3784 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3790 * Create UST metadata and open it on the tracer side.
3792 * Called with UST app session lock held and RCU read side lock.
3794 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3795 struct ust_app
*app
, struct consumer_output
*consumer
)
3798 struct ust_app_channel
*metadata
;
3799 struct consumer_socket
*socket
;
3800 struct ust_registry_session
*registry
;
3801 struct ltt_session
*session
= NULL
;
3803 LTTNG_ASSERT(ua_sess
);
3805 LTTNG_ASSERT(consumer
);
3807 registry
= get_session_registry(ua_sess
);
3808 /* The UST app session is held registry shall not be null. */
3809 LTTNG_ASSERT(registry
);
3811 pthread_mutex_lock(®istry
->lock
);
3813 /* Metadata already exists for this registry or it was closed previously */
3814 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3819 /* Allocate UST metadata */
3820 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3822 /* malloc() failed */
3827 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3829 /* Need one fd for the channel. */
3830 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3832 ERR("Exhausted number of available FD upon create metadata");
3836 /* Get the right consumer socket for the application. */
3837 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3840 goto error_consumer
;
3844 * Keep metadata key so we can identify it on the consumer side. Assign it
3845 * to the registry *before* we ask the consumer so we avoid the race of the
3846 * consumer requesting the metadata and the ask_channel call on our side
3847 * did not returned yet.
3849 registry
->metadata_key
= metadata
->key
;
3851 session
= session_find_by_id(ua_sess
->tracing_id
);
3852 LTTNG_ASSERT(session
);
3854 LTTNG_ASSERT(pthread_mutex_trylock(&session
->lock
));
3855 LTTNG_ASSERT(session_trylock_list());
3858 * Ask the metadata channel creation to the consumer. The metadata object
3859 * will be created by the consumer and kept their. However, the stream is
3860 * never added or monitored until we do a first push metadata to the
3863 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3864 registry
, session
->current_trace_chunk
);
3866 /* Nullify the metadata key so we don't try to close it later on. */
3867 registry
->metadata_key
= 0;
3868 goto error_consumer
;
3872 * The setup command will make the metadata stream be sent to the relayd,
3873 * if applicable, and the thread managing the metadatas. This is important
3874 * because after this point, if an error occurs, the only way the stream
3875 * can be deleted is to be monitored in the consumer.
3877 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3879 /* Nullify the metadata key so we don't try to close it later on. */
3880 registry
->metadata_key
= 0;
3881 goto error_consumer
;
3884 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3885 metadata
->key
, app
->pid
);
3888 lttng_fd_put(LTTNG_FD_APPS
, 1);
3889 delete_ust_app_channel(-1, metadata
, app
);
3891 pthread_mutex_unlock(®istry
->lock
);
3893 session_put(session
);
3899 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3900 * acquired before calling this function.
3902 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3904 struct ust_app
*app
= NULL
;
3905 struct lttng_ht_node_ulong
*node
;
3906 struct lttng_ht_iter iter
;
3908 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3909 node
= lttng_ht_iter_get_node_ulong(&iter
);
3911 DBG2("UST app no found with pid %d", pid
);
3915 DBG2("Found UST app by pid %d", pid
);
3917 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3924 * Allocate and init an UST app object using the registration information and
3925 * the command socket. This is called when the command socket connects to the
3928 * The object is returned on success or else NULL.
3930 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3933 struct ust_app
*lta
= NULL
;
3934 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
3937 LTTNG_ASSERT(sock
>= 0);
3939 DBG3("UST app creating application for socket %d", sock
);
3941 if ((msg
->bits_per_long
== 64 &&
3942 (uatomic_read(&the_ust_consumerd64_fd
) ==
3944 (msg
->bits_per_long
== 32 &&
3945 (uatomic_read(&the_ust_consumerd32_fd
) ==
3947 ERR("Registration failed: application \"%s\" (pid: %d) has "
3948 "%d-bit long, but no consumerd for this size is available.\n",
3949 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3954 * Reserve the two file descriptors of the event source pipe. The write
3955 * end will be closed once it is passed to the application, at which
3956 * point a single 'put' will be performed.
3958 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3960 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
3961 msg
->name
, (int) msg
->pid
);
3965 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
3966 if (!event_notifier_event_source_pipe
) {
3967 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
3968 msg
->name
, msg
->pid
);
3972 lta
= (ust_app
*) zmalloc(sizeof(struct ust_app
));
3975 goto error_free_pipe
;
3978 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
3980 lta
->ppid
= msg
->ppid
;
3981 lta
->uid
= msg
->uid
;
3982 lta
->gid
= msg
->gid
;
3984 lta
->bits_per_long
= msg
->bits_per_long
;
3985 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3986 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3987 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3988 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3989 lta
->long_alignment
= msg
->long_alignment
;
3990 lta
->byte_order
= msg
->byte_order
;
3992 lta
->v_major
= msg
->major
;
3993 lta
->v_minor
= msg
->minor
;
3994 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3995 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3996 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3997 lta
->notify_sock
= -1;
3998 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
4000 /* Copy name and make sure it's NULL terminated. */
4001 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
4002 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
4005 * Before this can be called, when receiving the registration information,
4006 * the application compatibility is checked. So, at this point, the
4007 * application can work with this session daemon.
4009 lta
->compatible
= 1;
4011 lta
->pid
= msg
->pid
;
4012 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
4014 pthread_mutex_init(<a
->sock_lock
, NULL
);
4015 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
4017 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
4021 lttng_pipe_destroy(event_notifier_event_source_pipe
);
4022 lttng_fd_put(LTTNG_FD_APPS
, 2);
4028 * For a given application object, add it to every hash table.
4030 void ust_app_add(struct ust_app
*app
)
4033 LTTNG_ASSERT(app
->notify_sock
>= 0);
4035 app
->registration_time
= time(NULL
);
4040 * On a re-registration, we want to kick out the previous registration of
4043 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
4046 * The socket _should_ be unique until _we_ call close. So, a add_unique
4047 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4048 * already in the table.
4050 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
4052 /* Add application to the notify socket hash table. */
4053 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
4054 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
4056 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4057 "notify_sock =%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
4058 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
4065 * Set the application version into the object.
4067 * Return 0 on success else a negative value either an errno code or a
4068 * LTTng-UST error code.
4070 int ust_app_version(struct ust_app
*app
)
4076 pthread_mutex_lock(&app
->sock_lock
);
4077 ret
= lttng_ust_ctl_tracer_version(app
->sock
, &app
->version
);
4078 pthread_mutex_unlock(&app
->sock_lock
);
4080 if (ret
== -LTTNG_UST_ERR_EXITING
|| ret
== -EPIPE
) {
4081 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4082 app
->pid
, app
->sock
);
4083 } else if (ret
== -EAGAIN
) {
4084 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4085 app
->pid
, app
->sock
);
4087 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4088 ret
, app
->pid
, app
->sock
);
4095 bool ust_app_supports_notifiers(const struct ust_app
*app
)
4097 return app
->v_major
>= 9;
4100 bool ust_app_supports_counters(const struct ust_app
*app
)
4102 return app
->v_major
>= 9;
4106 * Setup the base event notifier group.
4108 * Return 0 on success else a negative value either an errno code or a
4109 * LTTng-UST error code.
4111 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
4114 int event_pipe_write_fd
;
4115 struct lttng_ust_abi_object_data
*event_notifier_group
= NULL
;
4116 enum lttng_error_code lttng_ret
;
4117 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
4121 if (!ust_app_supports_notifiers(app
)) {
4126 /* Get the write side of the pipe. */
4127 event_pipe_write_fd
= lttng_pipe_get_writefd(
4128 app
->event_notifier_group
.event_pipe
);
4130 pthread_mutex_lock(&app
->sock_lock
);
4131 ret
= lttng_ust_ctl_create_event_notifier_group(app
->sock
,
4132 event_pipe_write_fd
, &event_notifier_group
);
4133 pthread_mutex_unlock(&app
->sock_lock
);
4135 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4137 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4138 app
->pid
, app
->sock
);
4139 } else if (ret
== -EAGAIN
) {
4141 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4142 app
->pid
, app
->sock
);
4144 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4145 ret
, app
->pid
, app
->sock
, event_pipe_write_fd
);
4150 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
4152 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4153 app
->name
, app
->pid
);
4158 * Release the file descriptor that was reserved for the write-end of
4161 lttng_fd_put(LTTNG_FD_APPS
, 1);
4163 lttng_ret
= notification_thread_command_add_tracer_event_source(
4164 the_notification_thread_handle
,
4165 lttng_pipe_get_readfd(
4166 app
->event_notifier_group
.event_pipe
),
4168 if (lttng_ret
!= LTTNG_OK
) {
4169 ERR("Failed to add tracer event source to notification thread");
4174 /* Assign handle only when the complete setup is valid. */
4175 app
->event_notifier_group
.object
= event_notifier_group
;
4177 event_notifier_error_accounting_status
=
4178 event_notifier_error_accounting_register_app(app
);
4179 switch (event_notifier_error_accounting_status
) {
4180 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
:
4182 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED
:
4183 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",
4184 app
->sock
, app
->name
, (int) app
->pid
);
4186 goto error_accounting
;
4187 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD
:
4188 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4189 app
->sock
, app
->name
, (int) app
->pid
);
4191 goto error_accounting
;
4193 ERR("Failed to setup event notifier error accounting for app");
4195 goto error_accounting
;
4201 lttng_ret
= notification_thread_command_remove_tracer_event_source(
4202 the_notification_thread_handle
,
4203 lttng_pipe_get_readfd(
4204 app
->event_notifier_group
.event_pipe
));
4205 if (lttng_ret
!= LTTNG_OK
) {
4206 ERR("Failed to remove application tracer event source from notification thread");
4210 lttng_ust_ctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
4211 free(app
->event_notifier_group
.object
);
4212 app
->event_notifier_group
.object
= NULL
;
4217 * Unregister app by removing it from the global traceable app list and freeing
4220 * The socket is already closed at this point so no close to sock.
4222 void ust_app_unregister(int sock
)
4224 struct ust_app
*lta
;
4225 struct lttng_ht_node_ulong
*node
;
4226 struct lttng_ht_iter ust_app_sock_iter
;
4227 struct lttng_ht_iter iter
;
4228 struct ust_app_session
*ua_sess
;
4233 /* Get the node reference for a call_rcu */
4234 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
4235 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
4238 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
4239 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
4242 * For per-PID buffers, perform "push metadata" and flush all
4243 * application streams before removing app from hash tables,
4244 * ensuring proper behavior of data_pending check.
4245 * Remove sessions so they are not visible during deletion.
4247 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
4249 struct ust_registry_session
*registry
;
4251 ret
= lttng_ht_del(lta
->sessions
, &iter
);
4253 /* The session was already removed so scheduled for teardown. */
4257 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
4258 (void) ust_app_flush_app_session(lta
, ua_sess
);
4262 * Add session to list for teardown. This is safe since at this point we
4263 * are the only one using this list.
4265 pthread_mutex_lock(&ua_sess
->lock
);
4267 if (ua_sess
->deleted
) {
4268 pthread_mutex_unlock(&ua_sess
->lock
);
4273 * Normally, this is done in the delete session process which is
4274 * executed in the call rcu below. However, upon registration we can't
4275 * afford to wait for the grace period before pushing data or else the
4276 * data pending feature can race between the unregistration and stop
4277 * command where the data pending command is sent *before* the grace
4280 * The close metadata below nullifies the metadata pointer in the
4281 * session so the delete session will NOT push/close a second time.
4283 registry
= get_session_registry(ua_sess
);
4285 /* Push metadata for application before freeing the application. */
4286 (void) push_metadata(registry
, ua_sess
->consumer
);
4289 * Don't ask to close metadata for global per UID buffers. Close
4290 * metadata only on destroy trace session in this case. Also, the
4291 * previous push metadata could have flag the metadata registry to
4292 * close so don't send a close command if closed.
4294 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
4295 /* And ask to close it for this session registry. */
4296 (void) close_metadata(registry
, ua_sess
->consumer
);
4299 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
4301 pthread_mutex_unlock(&ua_sess
->lock
);
4304 /* Remove application from PID hash table */
4305 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
4309 * Remove application from notify hash table. The thread handling the
4310 * notify socket could have deleted the node so ignore on error because
4311 * either way it's valid. The close of that socket is handled by the
4312 * apps_notify_thread.
4314 iter
.iter
.node
= <a
->notify_sock_n
.node
;
4315 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4318 * Ignore return value since the node might have been removed before by an
4319 * add replace during app registration because the PID can be reassigned by
4322 iter
.iter
.node
= <a
->pid_n
.node
;
4323 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4325 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4330 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
4337 * Fill events array with all events name of all registered apps.
4339 int ust_app_list_events(struct lttng_event
**events
)
4342 size_t nbmem
, count
= 0;
4343 struct lttng_ht_iter iter
;
4344 struct ust_app
*app
;
4345 struct lttng_event
*tmp_event
;
4347 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4348 tmp_event
= (lttng_event
*) zmalloc(nbmem
* sizeof(struct lttng_event
));
4349 if (tmp_event
== NULL
) {
4350 PERROR("zmalloc ust app events");
4357 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4358 struct lttng_ust_abi_tracepoint_iter uiter
;
4360 health_code_update();
4362 if (!app
->compatible
) {
4364 * TODO: In time, we should notice the caller of this error by
4365 * telling him that this is a version error.
4369 pthread_mutex_lock(&app
->sock_lock
);
4370 handle
= lttng_ust_ctl_tracepoint_list(app
->sock
);
4372 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4373 ERR("UST app list events getting handle failed for app pid %d",
4376 pthread_mutex_unlock(&app
->sock_lock
);
4380 while ((ret
= lttng_ust_ctl_tracepoint_list_get(app
->sock
, handle
,
4381 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4382 /* Handle ustctl error. */
4386 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4387 ERR("UST app tp list get failed for app %d with ret %d",
4390 DBG3("UST app tp list get failed. Application is dead");
4394 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4395 if (release_ret
< 0 &&
4396 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4397 release_ret
!= -EPIPE
) {
4398 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4400 pthread_mutex_unlock(&app
->sock_lock
);
4404 health_code_update();
4405 if (count
>= nbmem
) {
4406 /* In case the realloc fails, we free the memory */
4407 struct lttng_event
*new_tmp_event
;
4410 new_nbmem
= nbmem
<< 1;
4411 DBG2("Reallocating event list from %zu to %zu entries",
4413 new_tmp_event
= (lttng_event
*) realloc(tmp_event
,
4414 new_nbmem
* sizeof(struct lttng_event
));
4415 if (new_tmp_event
== NULL
) {
4418 PERROR("realloc ust app events");
4421 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4422 if (release_ret
< 0 &&
4423 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4424 release_ret
!= -EPIPE
) {
4425 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4427 pthread_mutex_unlock(&app
->sock_lock
);
4430 /* Zero the new memory */
4431 memset(new_tmp_event
+ nbmem
, 0,
4432 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
4434 tmp_event
= new_tmp_event
;
4436 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4437 tmp_event
[count
].loglevel
= uiter
.loglevel
;
4438 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_ABI_TRACEPOINT
;
4439 tmp_event
[count
].pid
= app
->pid
;
4440 tmp_event
[count
].enabled
= -1;
4443 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4444 pthread_mutex_unlock(&app
->sock_lock
);
4446 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4447 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4448 app
->pid
, app
->sock
);
4449 } else if (ret
== -EAGAIN
) {
4450 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4451 app
->pid
, app
->sock
);
4453 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4454 ret
, app
->pid
, app
->sock
);
4460 *events
= tmp_event
;
4462 DBG2("UST app list events done (%zu events)", count
);
4467 health_code_update();
4472 * Fill events array with all events name of all registered apps.
4474 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
4477 size_t nbmem
, count
= 0;
4478 struct lttng_ht_iter iter
;
4479 struct ust_app
*app
;
4480 struct lttng_event_field
*tmp_event
;
4482 nbmem
= UST_APP_EVENT_LIST_SIZE
;
4483 tmp_event
= (lttng_event_field
*) zmalloc(nbmem
* sizeof(struct lttng_event_field
));
4484 if (tmp_event
== NULL
) {
4485 PERROR("zmalloc ust app event fields");
4492 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4493 struct lttng_ust_abi_field_iter uiter
;
4495 health_code_update();
4497 if (!app
->compatible
) {
4499 * TODO: In time, we should notice the caller of this error by
4500 * telling him that this is a version error.
4504 pthread_mutex_lock(&app
->sock_lock
);
4505 handle
= lttng_ust_ctl_tracepoint_field_list(app
->sock
);
4507 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
4508 ERR("UST app list field getting handle failed for app pid %d",
4511 pthread_mutex_unlock(&app
->sock_lock
);
4515 while ((ret
= lttng_ust_ctl_tracepoint_field_list_get(app
->sock
, handle
,
4516 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
4517 /* Handle ustctl error. */
4521 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
4522 ERR("UST app tp list field failed for app %d with ret %d",
4525 DBG3("UST app tp list field failed. Application is dead");
4529 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4530 pthread_mutex_unlock(&app
->sock_lock
);
4531 if (release_ret
< 0 &&
4532 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4533 release_ret
!= -EPIPE
) {
4534 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4539 health_code_update();
4540 if (count
>= nbmem
) {
4541 /* In case the realloc fails, we free the memory */
4542 struct lttng_event_field
*new_tmp_event
;
4545 new_nbmem
= nbmem
<< 1;
4546 DBG2("Reallocating event field list from %zu to %zu entries",
4548 new_tmp_event
= (lttng_event_field
*) realloc(tmp_event
,
4549 new_nbmem
* sizeof(struct lttng_event_field
));
4550 if (new_tmp_event
== NULL
) {
4553 PERROR("realloc ust app event fields");
4556 release_ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4557 pthread_mutex_unlock(&app
->sock_lock
);
4559 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
4560 release_ret
!= -EPIPE
) {
4561 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
4565 /* Zero the new memory */
4566 memset(new_tmp_event
+ nbmem
, 0,
4567 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
4569 tmp_event
= new_tmp_event
;
4572 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4573 /* Mapping between these enums matches 1 to 1. */
4574 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
4575 tmp_event
[count
].nowrite
= uiter
.nowrite
;
4577 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_ABI_SYM_NAME_LEN
);
4578 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
4579 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
4580 tmp_event
[count
].event
.pid
= app
->pid
;
4581 tmp_event
[count
].event
.enabled
= -1;
4584 ret
= lttng_ust_ctl_release_handle(app
->sock
, handle
);
4585 pthread_mutex_unlock(&app
->sock_lock
);
4587 ret
!= -LTTNG_UST_ERR_EXITING
&&
4589 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
4594 *fields
= tmp_event
;
4596 DBG2("UST app list event fields done (%zu events)", count
);
4601 health_code_update();
4606 * Free and clean all traceable apps of the global list.
4608 void ust_app_clean_list(void)
4611 struct ust_app
*app
;
4612 struct lttng_ht_iter iter
;
4614 DBG2("UST app cleaning registered apps hash table");
4618 /* Cleanup notify socket hash table */
4619 if (ust_app_ht_by_notify_sock
) {
4620 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
4621 notify_sock_n
.node
) {
4623 * Assert that all notifiers are gone as all triggers
4624 * are unregistered prior to this clean-up.
4626 LTTNG_ASSERT(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
4628 ust_app_notify_sock_unregister(app
->notify_sock
);
4633 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4634 ret
= lttng_ht_del(ust_app_ht
, &iter
);
4636 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
4640 /* Cleanup socket hash table */
4641 if (ust_app_ht_by_sock
) {
4642 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
4644 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
4651 /* Destroy is done only when the ht is empty */
4653 lttng_ht_destroy(ust_app_ht
);
4655 if (ust_app_ht_by_sock
) {
4656 lttng_ht_destroy(ust_app_ht_by_sock
);
4658 if (ust_app_ht_by_notify_sock
) {
4659 lttng_ht_destroy(ust_app_ht_by_notify_sock
);
4664 * Init UST app hash table.
4666 int ust_app_ht_alloc(void)
4668 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4672 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4673 if (!ust_app_ht_by_sock
) {
4676 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
4677 if (!ust_app_ht_by_notify_sock
) {
4684 * For a specific UST session, disable the channel for all registered apps.
4686 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
4687 struct ltt_ust_channel
*uchan
)
4690 struct lttng_ht_iter iter
;
4691 struct lttng_ht_node_str
*ua_chan_node
;
4692 struct ust_app
*app
;
4693 struct ust_app_session
*ua_sess
;
4694 struct ust_app_channel
*ua_chan
;
4696 LTTNG_ASSERT(usess
->active
);
4697 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
4698 uchan
->name
, usess
->id
);
4702 /* For every registered applications */
4703 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4704 struct lttng_ht_iter uiter
;
4705 if (!app
->compatible
) {
4707 * TODO: In time, we should notice the caller of this error by
4708 * telling him that this is a version error.
4712 ua_sess
= lookup_session_by_app(usess
, app
);
4713 if (ua_sess
== NULL
) {
4718 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4719 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4720 /* If the session if found for the app, the channel must be there */
4721 LTTNG_ASSERT(ua_chan_node
);
4723 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4724 /* The channel must not be already disabled */
4725 LTTNG_ASSERT(ua_chan
->enabled
== 1);
4727 /* Disable channel onto application */
4728 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
4730 /* XXX: We might want to report this error at some point... */
4740 * For a specific UST session, enable the channel for all registered apps.
4742 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
4743 struct ltt_ust_channel
*uchan
)
4746 struct lttng_ht_iter iter
;
4747 struct ust_app
*app
;
4748 struct ust_app_session
*ua_sess
;
4750 LTTNG_ASSERT(usess
->active
);
4751 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
4752 uchan
->name
, usess
->id
);
4756 /* For every registered applications */
4757 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4758 if (!app
->compatible
) {
4760 * TODO: In time, we should notice the caller of this error by
4761 * telling him that this is a version error.
4765 ua_sess
= lookup_session_by_app(usess
, app
);
4766 if (ua_sess
== NULL
) {
4770 /* Enable channel onto application */
4771 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
4773 /* XXX: We might want to report this error at some point... */
4783 * Disable an event in a channel and for a specific session.
4785 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
4786 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4789 struct lttng_ht_iter iter
, uiter
;
4790 struct lttng_ht_node_str
*ua_chan_node
;
4791 struct ust_app
*app
;
4792 struct ust_app_session
*ua_sess
;
4793 struct ust_app_channel
*ua_chan
;
4794 struct ust_app_event
*ua_event
;
4796 LTTNG_ASSERT(usess
->active
);
4797 DBG("UST app disabling event %s for all apps in channel "
4798 "%s for session id %" PRIu64
,
4799 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4803 /* For all registered applications */
4804 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4805 if (!app
->compatible
) {
4807 * TODO: In time, we should notice the caller of this error by
4808 * telling him that this is a version error.
4812 ua_sess
= lookup_session_by_app(usess
, app
);
4813 if (ua_sess
== NULL
) {
4818 /* Lookup channel in the ust app session */
4819 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4820 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4821 if (ua_chan_node
== NULL
) {
4822 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4823 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4826 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4828 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4829 uevent
->filter
, uevent
->attr
.loglevel
,
4831 if (ua_event
== NULL
) {
4832 DBG2("Event %s not found in channel %s for app pid %d."
4833 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4837 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4839 /* XXX: Report error someday... */
4848 /* The ua_sess lock must be held by the caller. */
4850 int ust_app_channel_create(struct ltt_ust_session
*usess
,
4851 struct ust_app_session
*ua_sess
,
4852 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
4853 struct ust_app_channel
**_ua_chan
)
4856 struct ust_app_channel
*ua_chan
= NULL
;
4858 LTTNG_ASSERT(ua_sess
);
4859 ASSERT_LOCKED(ua_sess
->lock
);
4861 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4862 sizeof(uchan
->name
))) {
4863 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
4867 struct ltt_ust_context
*uctx
= NULL
;
4870 * Create channel onto application and synchronize its
4873 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
4874 LTTNG_UST_ABI_CHAN_PER_CPU
, usess
,
4880 ret
= ust_app_channel_send(app
, usess
,
4887 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
4888 ret
= create_ust_app_channel_context(ua_chan
,
4901 * The application's socket is not valid. Either a bad socket
4902 * or a timeout on it. We can't inform the caller that for a
4903 * specific app, the session failed so lets continue here.
4905 ret
= 0; /* Not an error. */
4913 if (ret
== 0 && _ua_chan
) {
4915 * Only return the application's channel on success. Note
4916 * that the channel can still be part of the application's
4917 * channel hashtable on error.
4919 *_ua_chan
= ua_chan
;
4925 * Enable event for a specific session and channel on the tracer.
4927 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4928 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4931 struct lttng_ht_iter iter
, uiter
;
4932 struct lttng_ht_node_str
*ua_chan_node
;
4933 struct ust_app
*app
;
4934 struct ust_app_session
*ua_sess
;
4935 struct ust_app_channel
*ua_chan
;
4936 struct ust_app_event
*ua_event
;
4938 LTTNG_ASSERT(usess
->active
);
4939 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4940 uevent
->attr
.name
, usess
->id
);
4943 * NOTE: At this point, this function is called only if the session and
4944 * channel passed are already created for all apps. and enabled on the
4950 /* For all registered applications */
4951 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4952 if (!app
->compatible
) {
4954 * TODO: In time, we should notice the caller of this error by
4955 * telling him that this is a version error.
4959 ua_sess
= lookup_session_by_app(usess
, app
);
4961 /* The application has problem or is probably dead. */
4965 pthread_mutex_lock(&ua_sess
->lock
);
4967 if (ua_sess
->deleted
) {
4968 pthread_mutex_unlock(&ua_sess
->lock
);
4972 /* Lookup channel in the ust app session */
4973 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4974 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4976 * It is possible that the channel cannot be found is
4977 * the channel/event creation occurs concurrently with
4978 * an application exit.
4980 if (!ua_chan_node
) {
4981 pthread_mutex_unlock(&ua_sess
->lock
);
4985 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4987 /* Get event node */
4988 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4989 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4990 if (ua_event
== NULL
) {
4991 DBG3("UST app enable event %s not found for app PID %d."
4992 "Skipping app", uevent
->attr
.name
, app
->pid
);
4996 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4998 pthread_mutex_unlock(&ua_sess
->lock
);
5002 pthread_mutex_unlock(&ua_sess
->lock
);
5011 * For a specific existing UST session and UST channel, creates the event for
5012 * all registered apps.
5014 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
5015 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
5018 struct lttng_ht_iter iter
, uiter
;
5019 struct lttng_ht_node_str
*ua_chan_node
;
5020 struct ust_app
*app
;
5021 struct ust_app_session
*ua_sess
;
5022 struct ust_app_channel
*ua_chan
;
5024 LTTNG_ASSERT(usess
->active
);
5025 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
5026 uevent
->attr
.name
, usess
->id
);
5030 /* For all registered applications */
5031 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5032 if (!app
->compatible
) {
5034 * TODO: In time, we should notice the caller of this error by
5035 * telling him that this is a version error.
5039 ua_sess
= lookup_session_by_app(usess
, app
);
5041 /* The application has problem or is probably dead. */
5045 pthread_mutex_lock(&ua_sess
->lock
);
5047 if (ua_sess
->deleted
) {
5048 pthread_mutex_unlock(&ua_sess
->lock
);
5052 /* Lookup channel in the ust app session */
5053 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5054 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5055 /* If the channel is not found, there is a code flow error */
5056 LTTNG_ASSERT(ua_chan_node
);
5058 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5060 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5061 pthread_mutex_unlock(&ua_sess
->lock
);
5063 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
5064 /* Possible value at this point: -ENOMEM. If so, we stop! */
5067 DBG2("UST app event %s already exist on app PID %d",
5068 uevent
->attr
.name
, app
->pid
);
5078 * Start tracing for a specific UST session and app.
5080 * Called with UST app session lock held.
5084 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5087 struct ust_app_session
*ua_sess
;
5089 DBG("Starting tracing for ust app pid %d", app
->pid
);
5093 if (!app
->compatible
) {
5097 ua_sess
= lookup_session_by_app(usess
, app
);
5098 if (ua_sess
== NULL
) {
5099 /* The session is in teardown process. Ignore and continue. */
5103 pthread_mutex_lock(&ua_sess
->lock
);
5105 if (ua_sess
->deleted
) {
5106 pthread_mutex_unlock(&ua_sess
->lock
);
5110 if (ua_sess
->enabled
) {
5111 pthread_mutex_unlock(&ua_sess
->lock
);
5115 /* Upon restart, we skip the setup, already done */
5116 if (ua_sess
->started
) {
5120 health_code_update();
5123 /* This starts the UST tracing */
5124 pthread_mutex_lock(&app
->sock_lock
);
5125 ret
= lttng_ust_ctl_start_session(app
->sock
, ua_sess
->handle
);
5126 pthread_mutex_unlock(&app
->sock_lock
);
5128 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5129 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5130 app
->pid
, app
->sock
);
5131 pthread_mutex_unlock(&ua_sess
->lock
);
5133 } else if (ret
== -EAGAIN
) {
5134 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5135 app
->pid
, app
->sock
);
5136 pthread_mutex_unlock(&ua_sess
->lock
);
5140 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5141 ret
, app
->pid
, app
->sock
);
5146 /* Indicate that the session has been started once */
5147 ua_sess
->started
= 1;
5148 ua_sess
->enabled
= 1;
5150 pthread_mutex_unlock(&ua_sess
->lock
);
5152 health_code_update();
5154 /* Quiescent wait after starting trace */
5155 pthread_mutex_lock(&app
->sock_lock
);
5156 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5157 pthread_mutex_unlock(&app
->sock_lock
);
5159 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5160 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5161 app
->pid
, app
->sock
);
5162 } else if (ret
== -EAGAIN
) {
5163 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5164 app
->pid
, app
->sock
);
5166 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5167 ret
, app
->pid
, app
->sock
);
5173 health_code_update();
5177 pthread_mutex_unlock(&ua_sess
->lock
);
5179 health_code_update();
5184 * Stop tracing for a specific UST session and app.
5187 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5190 struct ust_app_session
*ua_sess
;
5191 struct ust_registry_session
*registry
;
5193 DBG("Stopping tracing for ust app pid %d", app
->pid
);
5197 if (!app
->compatible
) {
5198 goto end_no_session
;
5201 ua_sess
= lookup_session_by_app(usess
, app
);
5202 if (ua_sess
== NULL
) {
5203 goto end_no_session
;
5206 pthread_mutex_lock(&ua_sess
->lock
);
5208 if (ua_sess
->deleted
) {
5209 pthread_mutex_unlock(&ua_sess
->lock
);
5210 goto end_no_session
;
5214 * If started = 0, it means that stop trace has been called for a session
5215 * that was never started. It's possible since we can have a fail start
5216 * from either the application manager thread or the command thread. Simply
5217 * indicate that this is a stop error.
5219 if (!ua_sess
->started
) {
5220 goto error_rcu_unlock
;
5223 health_code_update();
5225 /* This inhibits UST tracing */
5226 pthread_mutex_lock(&app
->sock_lock
);
5227 ret
= lttng_ust_ctl_stop_session(app
->sock
, ua_sess
->handle
);
5228 pthread_mutex_unlock(&app
->sock_lock
);
5230 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5231 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5232 app
->pid
, app
->sock
);
5234 } else if (ret
== -EAGAIN
) {
5235 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5236 app
->pid
, app
->sock
);
5240 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5241 ret
, app
->pid
, app
->sock
);
5243 goto error_rcu_unlock
;
5246 health_code_update();
5247 ua_sess
->enabled
= 0;
5249 /* Quiescent wait after stopping trace */
5250 pthread_mutex_lock(&app
->sock_lock
);
5251 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5252 pthread_mutex_unlock(&app
->sock_lock
);
5254 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5255 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5256 app
->pid
, app
->sock
);
5257 } else if (ret
== -EAGAIN
) {
5258 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5259 app
->pid
, app
->sock
);
5261 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5262 ret
, app
->pid
, app
->sock
);
5266 health_code_update();
5268 registry
= get_session_registry(ua_sess
);
5270 /* The UST app session is held registry shall not be null. */
5271 LTTNG_ASSERT(registry
);
5273 /* Push metadata for application before freeing the application. */
5274 (void) push_metadata(registry
, ua_sess
->consumer
);
5277 pthread_mutex_unlock(&ua_sess
->lock
);
5280 health_code_update();
5284 pthread_mutex_unlock(&ua_sess
->lock
);
5286 health_code_update();
5291 int ust_app_flush_app_session(struct ust_app
*app
,
5292 struct ust_app_session
*ua_sess
)
5294 int ret
, retval
= 0;
5295 struct lttng_ht_iter iter
;
5296 struct ust_app_channel
*ua_chan
;
5297 struct consumer_socket
*socket
;
5299 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
5303 if (!app
->compatible
) {
5304 goto end_not_compatible
;
5307 pthread_mutex_lock(&ua_sess
->lock
);
5309 if (ua_sess
->deleted
) {
5313 health_code_update();
5315 /* Flushing buffers */
5316 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5319 /* Flush buffers and push metadata. */
5320 switch (ua_sess
->buffer_type
) {
5321 case LTTNG_BUFFER_PER_PID
:
5322 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
5324 health_code_update();
5325 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
5327 ERR("Error flushing consumer channel");
5333 case LTTNG_BUFFER_PER_UID
:
5339 health_code_update();
5342 pthread_mutex_unlock(&ua_sess
->lock
);
5346 health_code_update();
5351 * Flush buffers for all applications for a specific UST session.
5352 * Called with UST session lock held.
5355 int ust_app_flush_session(struct ltt_ust_session
*usess
)
5360 DBG("Flushing session buffers for all ust apps");
5364 /* Flush buffers and push metadata. */
5365 switch (usess
->buffer_type
) {
5366 case LTTNG_BUFFER_PER_UID
:
5368 struct buffer_reg_uid
*reg
;
5369 struct lttng_ht_iter iter
;
5371 /* Flush all per UID buffers associated to that session. */
5372 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5373 struct ust_registry_session
*ust_session_reg
;
5374 struct buffer_reg_channel
*buf_reg_chan
;
5375 struct consumer_socket
*socket
;
5377 /* Get consumer socket to use to push the metadata.*/
5378 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5381 /* Ignore request if no consumer is found for the session. */
5385 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5386 buf_reg_chan
, node
.node
) {
5388 * The following call will print error values so the return
5389 * code is of little importance because whatever happens, we
5390 * have to try them all.
5392 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
5395 ust_session_reg
= reg
->registry
->reg
.ust
;
5396 /* Push metadata. */
5397 (void) push_metadata(ust_session_reg
, usess
->consumer
);
5401 case LTTNG_BUFFER_PER_PID
:
5403 struct ust_app_session
*ua_sess
;
5404 struct lttng_ht_iter iter
;
5405 struct ust_app
*app
;
5407 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5408 ua_sess
= lookup_session_by_app(usess
, app
);
5409 if (ua_sess
== NULL
) {
5412 (void) ust_app_flush_app_session(app
, ua_sess
);
5423 health_code_update();
5428 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
5429 struct ust_app_session
*ua_sess
)
5432 struct lttng_ht_iter iter
;
5433 struct ust_app_channel
*ua_chan
;
5434 struct consumer_socket
*socket
;
5436 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
5440 if (!app
->compatible
) {
5441 goto end_not_compatible
;
5444 pthread_mutex_lock(&ua_sess
->lock
);
5446 if (ua_sess
->deleted
) {
5450 health_code_update();
5452 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5455 ERR("Failed to find consumer (%" PRIu32
") socket",
5456 app
->bits_per_long
);
5461 /* Clear quiescent state. */
5462 switch (ua_sess
->buffer_type
) {
5463 case LTTNG_BUFFER_PER_PID
:
5464 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
5465 ua_chan
, node
.node
) {
5466 health_code_update();
5467 ret
= consumer_clear_quiescent_channel(socket
,
5470 ERR("Error clearing quiescent state for consumer channel");
5476 case LTTNG_BUFFER_PER_UID
:
5483 health_code_update();
5486 pthread_mutex_unlock(&ua_sess
->lock
);
5490 health_code_update();
5495 * Clear quiescent state in each stream for all applications for a
5496 * specific UST session.
5497 * Called with UST session lock held.
5500 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
5505 DBG("Clearing stream quiescent state for all ust apps");
5509 switch (usess
->buffer_type
) {
5510 case LTTNG_BUFFER_PER_UID
:
5512 struct lttng_ht_iter iter
;
5513 struct buffer_reg_uid
*reg
;
5516 * Clear quiescent for all per UID buffers associated to
5519 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5520 struct consumer_socket
*socket
;
5521 struct buffer_reg_channel
*buf_reg_chan
;
5523 /* Get associated consumer socket.*/
5524 socket
= consumer_find_socket_by_bitness(
5525 reg
->bits_per_long
, usess
->consumer
);
5528 * Ignore request if no consumer is found for
5534 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
5535 &iter
.iter
, buf_reg_chan
, node
.node
) {
5537 * The following call will print error values so
5538 * the return code is of little importance
5539 * because whatever happens, we have to try them
5542 (void) consumer_clear_quiescent_channel(socket
,
5543 buf_reg_chan
->consumer_key
);
5548 case LTTNG_BUFFER_PER_PID
:
5550 struct ust_app_session
*ua_sess
;
5551 struct lttng_ht_iter iter
;
5552 struct ust_app
*app
;
5554 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
5556 ua_sess
= lookup_session_by_app(usess
, app
);
5557 if (ua_sess
== NULL
) {
5560 (void) ust_app_clear_quiescent_app_session(app
,
5572 health_code_update();
5577 * Destroy a specific UST session in apps.
5579 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
5582 struct ust_app_session
*ua_sess
;
5583 struct lttng_ht_iter iter
;
5584 struct lttng_ht_node_u64
*node
;
5586 DBG("Destroy tracing for ust app pid %d", app
->pid
);
5590 if (!app
->compatible
) {
5594 __lookup_session_by_app(usess
, app
, &iter
);
5595 node
= lttng_ht_iter_get_node_u64(&iter
);
5597 /* Session is being or is deleted. */
5600 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
5602 health_code_update();
5603 destroy_app_session(app
, ua_sess
);
5605 health_code_update();
5607 /* Quiescent wait after stopping trace */
5608 pthread_mutex_lock(&app
->sock_lock
);
5609 ret
= lttng_ust_ctl_wait_quiescent(app
->sock
);
5610 pthread_mutex_unlock(&app
->sock_lock
);
5612 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
5613 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5614 app
->pid
, app
->sock
);
5615 } else if (ret
== -EAGAIN
) {
5616 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5617 app
->pid
, app
->sock
);
5619 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5620 ret
, app
->pid
, app
->sock
);
5625 health_code_update();
5630 * Start tracing for the UST session.
5632 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
5634 struct lttng_ht_iter iter
;
5635 struct ust_app
*app
;
5637 DBG("Starting all UST traces");
5640 * Even though the start trace might fail, flag this session active so
5641 * other application coming in are started by default.
5648 * In a start-stop-start use-case, we need to clear the quiescent state
5649 * of each channel set by the prior stop command, thus ensuring that a
5650 * following stop or destroy is sure to grab a timestamp_end near those
5651 * operations, even if the packet is empty.
5653 (void) ust_app_clear_quiescent_session(usess
);
5655 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5656 ust_app_global_update(usess
, app
);
5665 * Start tracing for the UST session.
5666 * Called with UST session lock held.
5668 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
5671 struct lttng_ht_iter iter
;
5672 struct ust_app
*app
;
5674 DBG("Stopping all UST traces");
5677 * Even though the stop trace might fail, flag this session inactive so
5678 * other application coming in are not started by default.
5684 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5685 ret
= ust_app_stop_trace(usess
, app
);
5687 /* Continue to next apps even on error */
5692 (void) ust_app_flush_session(usess
);
5700 * Destroy app UST session.
5702 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
5705 struct lttng_ht_iter iter
;
5706 struct ust_app
*app
;
5708 DBG("Destroy all UST traces");
5712 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5713 ret
= destroy_trace(usess
, app
);
5715 /* Continue to next apps even on error */
5725 /* The ua_sess lock must be held by the caller. */
5727 int find_or_create_ust_app_channel(
5728 struct ltt_ust_session
*usess
,
5729 struct ust_app_session
*ua_sess
,
5730 struct ust_app
*app
,
5731 struct ltt_ust_channel
*uchan
,
5732 struct ust_app_channel
**ua_chan
)
5735 struct lttng_ht_iter iter
;
5736 struct lttng_ht_node_str
*ua_chan_node
;
5738 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
5739 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5741 *ua_chan
= caa_container_of(ua_chan_node
,
5742 struct ust_app_channel
, node
);
5746 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
5755 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
5756 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
5757 struct ust_app
*app
)
5760 struct ust_app_event
*ua_event
= NULL
;
5762 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5763 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5765 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5770 if (ua_event
->enabled
!= uevent
->enabled
) {
5771 ret
= uevent
->enabled
?
5772 enable_ust_app_event(ua_sess
, ua_event
, app
) :
5773 disable_ust_app_event(ua_sess
, ua_event
, app
);
5781 /* Called with RCU read-side lock held. */
5783 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
5786 enum lttng_error_code ret_code
;
5787 enum lttng_trigger_status t_status
;
5788 struct lttng_ht_iter app_trigger_iter
;
5789 struct lttng_triggers
*triggers
= NULL
;
5790 struct ust_app_event_notifier_rule
*event_notifier_rule
;
5791 unsigned int count
, i
;
5793 if (!ust_app_supports_notifiers(app
)) {
5798 * Currrently, registering or unregistering a trigger with an
5799 * event rule condition causes a full synchronization of the event
5802 * The first step attempts to add an event notifier for all registered
5803 * triggers that apply to the user space tracers. Then, the
5804 * application's event notifiers rules are all checked against the list
5805 * of registered triggers. Any event notifier that doesn't have a
5806 * matching trigger can be assumed to have been disabled.
5808 * All of this is inefficient, but is put in place to get the feature
5809 * rolling as it is simpler at this moment. It will be optimized Soon™
5810 * to allow the state of enabled
5811 * event notifiers to be synchronized in a piece-wise way.
5814 /* Get all triggers using uid 0 (root) */
5815 ret_code
= notification_thread_command_list_triggers(
5816 the_notification_thread_handle
, 0, &triggers
);
5817 if (ret_code
!= LTTNG_OK
) {
5821 LTTNG_ASSERT(triggers
);
5823 t_status
= lttng_triggers_get_count(triggers
, &count
);
5824 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
5828 for (i
= 0; i
< count
; i
++) {
5829 struct lttng_condition
*condition
;
5830 struct lttng_event_rule
*event_rule
;
5831 struct lttng_trigger
*trigger
;
5832 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
5833 enum lttng_condition_status condition_status
;
5836 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
5837 LTTNG_ASSERT(trigger
);
5839 token
= lttng_trigger_get_tracer_token(trigger
);
5840 condition
= lttng_trigger_get_condition(trigger
);
5842 if (lttng_condition_get_type(condition
) !=
5843 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
) {
5844 /* Does not apply */
5849 lttng_condition_event_rule_matches_borrow_rule_mutable(
5850 condition
, &event_rule
);
5851 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
5853 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
5854 /* Skip kernel related triggers. */
5859 * Find or create the associated token event rule. The caller
5860 * holds the RCU read lock, so this is safe to call without
5861 * explicitly acquiring it here.
5863 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
5864 app
->token_to_event_notifier_rule_ht
, token
);
5865 if (!looked_up_event_notifier_rule
) {
5866 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
5874 /* Remove all unknown event sources from the app. */
5875 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
5876 &app_trigger_iter
.iter
, event_notifier_rule
,
5878 const uint64_t app_token
= event_notifier_rule
->token
;
5882 * Check if the app event trigger still exists on the
5883 * notification side.
5885 for (i
= 0; i
< count
; i
++) {
5886 uint64_t notification_thread_token
;
5887 const struct lttng_trigger
*trigger
=
5888 lttng_triggers_get_at_index(
5891 LTTNG_ASSERT(trigger
);
5893 notification_thread_token
=
5894 lttng_trigger_get_tracer_token(trigger
);
5896 if (notification_thread_token
== app_token
) {
5908 * This trigger was unregistered, disable it on the tracer's
5911 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
5913 LTTNG_ASSERT(ret
== 0);
5915 /* Callee logs errors. */
5916 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
5918 delete_ust_app_event_notifier_rule(
5919 app
->sock
, event_notifier_rule
, app
);
5925 lttng_triggers_destroy(triggers
);
5930 * RCU read lock must be held by the caller.
5933 void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
5934 struct ust_app_session
*ua_sess
,
5935 struct ust_app
*app
)
5938 struct cds_lfht_iter uchan_iter
;
5939 struct ltt_ust_channel
*uchan
;
5941 LTTNG_ASSERT(usess
);
5942 LTTNG_ASSERT(ua_sess
);
5945 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
5947 struct ust_app_channel
*ua_chan
;
5948 struct cds_lfht_iter uevent_iter
;
5949 struct ltt_ust_event
*uevent
;
5952 * Search for a matching ust_app_channel. If none is found,
5953 * create it. Creating the channel will cause the ua_chan
5954 * structure to be allocated, the channel buffers to be
5955 * allocated (if necessary) and sent to the application, and
5956 * all enabled contexts will be added to the channel.
5958 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
5959 app
, uchan
, &ua_chan
);
5961 /* Tracer is probably gone or ENOMEM. */
5966 /* ua_chan will be NULL for the metadata channel */
5970 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
5972 ret
= ust_app_channel_synchronize_event(ua_chan
,
5973 uevent
, ua_sess
, app
);
5979 if (ua_chan
->enabled
!= uchan
->enabled
) {
5980 ret
= uchan
->enabled
?
5981 enable_ust_app_channel(ua_sess
, uchan
, app
) :
5982 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
5993 * The caller must ensure that the application is compatible and is tracked
5994 * by the process attribute trackers.
5997 void ust_app_synchronize(struct ltt_ust_session
*usess
,
5998 struct ust_app
*app
)
6001 struct ust_app_session
*ua_sess
= NULL
;
6004 * The application's configuration should only be synchronized for
6007 LTTNG_ASSERT(usess
->active
);
6009 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
6011 /* Tracer is probably gone or ENOMEM. */
6013 destroy_app_session(app
, ua_sess
);
6017 LTTNG_ASSERT(ua_sess
);
6019 pthread_mutex_lock(&ua_sess
->lock
);
6020 if (ua_sess
->deleted
) {
6021 goto deleted_session
;
6026 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
6029 * Create the metadata for the application. This returns gracefully if a
6030 * metadata was already set for the session.
6032 * The metadata channel must be created after the data channels as the
6033 * consumer daemon assumes this ordering. When interacting with a relay
6034 * daemon, the consumer will use this assumption to send the
6035 * "STREAMS_SENT" message to the relay daemon.
6037 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
6039 ERR("Metadata creation failed for app sock %d for session id %" PRIu64
,
6040 app
->sock
, usess
->id
);
6046 pthread_mutex_unlock(&ua_sess
->lock
);
6052 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6054 struct ust_app_session
*ua_sess
;
6056 ua_sess
= lookup_session_by_app(usess
, app
);
6057 if (ua_sess
== NULL
) {
6060 destroy_app_session(app
, ua_sess
);
6064 * Add channels/events from UST global domain to registered apps at sock.
6066 * Called with session lock held.
6067 * Called with RCU read-side lock held.
6069 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
6071 LTTNG_ASSERT(usess
);
6072 LTTNG_ASSERT(usess
->active
);
6074 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
6075 app
->sock
, usess
->id
);
6077 if (!app
->compatible
) {
6080 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
6082 trace_ust_id_tracker_lookup(
6083 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
6085 trace_ust_id_tracker_lookup(
6086 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
6089 * Synchronize the application's internal tracing configuration
6090 * and start tracing.
6092 ust_app_synchronize(usess
, app
);
6093 ust_app_start_trace(usess
, app
);
6095 ust_app_global_destroy(usess
, app
);
6100 * Add all event notifiers to an application.
6102 * Called with session lock held.
6103 * Called with RCU read-side lock held.
6105 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
6107 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6108 app
->name
, app
->pid
);
6110 if (!app
->compatible
|| !ust_app_supports_notifiers(app
)) {
6114 if (app
->event_notifier_group
.object
== NULL
) {
6115 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6116 app
->name
, app
->pid
);
6120 ust_app_synchronize_event_notifier_rules(app
);
6124 * Called with session lock held.
6126 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
6128 struct lttng_ht_iter iter
;
6129 struct ust_app
*app
;
6132 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6133 ust_app_global_update(usess
, app
);
6138 void ust_app_global_update_all_event_notifier_rules(void)
6140 struct lttng_ht_iter iter
;
6141 struct ust_app
*app
;
6144 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6145 ust_app_global_update_event_notifier_rules(app
);
6152 * Add context to a specific channel for global UST domain.
6154 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
6155 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
6158 struct lttng_ht_node_str
*ua_chan_node
;
6159 struct lttng_ht_iter iter
, uiter
;
6160 struct ust_app_channel
*ua_chan
= NULL
;
6161 struct ust_app_session
*ua_sess
;
6162 struct ust_app
*app
;
6164 LTTNG_ASSERT(usess
->active
);
6167 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6168 if (!app
->compatible
) {
6170 * TODO: In time, we should notice the caller of this error by
6171 * telling him that this is a version error.
6175 ua_sess
= lookup_session_by_app(usess
, app
);
6176 if (ua_sess
== NULL
) {
6180 pthread_mutex_lock(&ua_sess
->lock
);
6182 if (ua_sess
->deleted
) {
6183 pthread_mutex_unlock(&ua_sess
->lock
);
6187 /* Lookup channel in the ust app session */
6188 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
6189 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6190 if (ua_chan_node
== NULL
) {
6193 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
6195 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
6200 pthread_mutex_unlock(&ua_sess
->lock
);
6208 * Receive registration and populate the given msg structure.
6210 * On success return 0 else a negative value returned by the ustctl call.
6212 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
6215 uint32_t pid
, ppid
, uid
, gid
;
6219 ret
= lttng_ust_ctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
6220 &pid
, &ppid
, &uid
, &gid
,
6221 &msg
->bits_per_long
,
6222 &msg
->uint8_t_alignment
,
6223 &msg
->uint16_t_alignment
,
6224 &msg
->uint32_t_alignment
,
6225 &msg
->uint64_t_alignment
,
6226 &msg
->long_alignment
,
6233 case LTTNG_UST_ERR_EXITING
:
6234 DBG3("UST app recv reg message failed. Application died");
6236 case LTTNG_UST_ERR_UNSUP_MAJOR
:
6237 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6238 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
6239 LTTNG_UST_ABI_MINOR_VERSION
);
6242 ERR("UST app recv reg message failed with ret %d", ret
);
6247 msg
->pid
= (pid_t
) pid
;
6248 msg
->ppid
= (pid_t
) ppid
;
6249 msg
->uid
= (uid_t
) uid
;
6250 msg
->gid
= (gid_t
) gid
;
6257 * Return a ust app session object using the application object and the
6258 * session object descriptor has a key. If not found, NULL is returned.
6259 * A RCU read side lock MUST be acquired when calling this function.
6261 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
6264 struct lttng_ht_node_ulong
*node
;
6265 struct lttng_ht_iter iter
;
6266 struct ust_app_session
*ua_sess
= NULL
;
6270 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
6271 node
= lttng_ht_iter_get_node_ulong(&iter
);
6273 DBG2("UST app session find by objd %d not found", objd
);
6277 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
6284 * Return a ust app channel object using the application object and the channel
6285 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6286 * lock MUST be acquired before calling this function.
6288 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
6291 struct lttng_ht_node_ulong
*node
;
6292 struct lttng_ht_iter iter
;
6293 struct ust_app_channel
*ua_chan
= NULL
;
6297 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
6298 node
= lttng_ht_iter_get_node_ulong(&iter
);
6300 DBG2("UST app channel find by objd %d not found", objd
);
6304 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
6311 * Fixup legacy context fields for comparison:
6312 * - legacy array becomes array_nestable,
6313 * - legacy struct becomes struct_nestable,
6314 * - legacy variant becomes variant_nestable,
6315 * legacy sequences are not emitted in LTTng-UST contexts.
6317 static int ust_app_fixup_legacy_context_fields(size_t *_nr_fields
,
6318 struct lttng_ust_ctl_field
**_fields
)
6320 struct lttng_ust_ctl_field
*fields
= *_fields
, *new_fields
= NULL
;
6321 size_t nr_fields
= *_nr_fields
, new_nr_fields
= 0, i
, j
;
6325 for (i
= 0; i
< nr_fields
; i
++) {
6326 const struct lttng_ust_ctl_field
*field
= &fields
[i
];
6328 switch (field
->type
.atype
) {
6329 case lttng_ust_ctl_atype_sequence
:
6330 ERR("Unexpected legacy sequence context.");
6333 case lttng_ust_ctl_atype_array
:
6334 switch (field
->type
.u
.legacy
.array
.elem_type
.atype
) {
6335 case lttng_ust_ctl_atype_integer
:
6338 ERR("Unexpected legacy array element type in context.");
6343 /* One field for array_nested, one field for elem type. */
6347 case lttng_ust_ctl_atype_struct
: /* Fallthrough */
6348 case lttng_ust_ctl_atype_variant
:
6360 new_fields
= (struct lttng_ust_ctl_field
*) zmalloc(sizeof(*new_fields
) * new_nr_fields
);
6365 for (i
= 0, j
= 0; i
< nr_fields
; i
++, j
++) {
6366 const struct lttng_ust_ctl_field
*field
= &fields
[i
];
6367 struct lttng_ust_ctl_field
*new_field
= &new_fields
[j
];
6369 switch (field
->type
.atype
) {
6370 case lttng_ust_ctl_atype_array
:
6371 /* One field for array_nested, one field for elem type. */
6372 strncpy(new_field
->name
, field
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
6373 new_field
->type
.atype
= lttng_ust_ctl_atype_array_nestable
;
6374 new_field
->type
.u
.array_nestable
.length
= field
->type
.u
.legacy
.array
.length
;
6375 new_field
->type
.u
.array_nestable
.alignment
= 0;
6376 new_field
= &new_fields
[++j
]; /* elem type */
6377 new_field
->type
.atype
= field
->type
.u
.legacy
.array
.elem_type
.atype
;
6378 assert(new_field
->type
.atype
== lttng_ust_ctl_atype_integer
);
6379 new_field
->type
.u
.integer
= field
->type
.u
.legacy
.array
.elem_type
.u
.basic
.integer
;
6381 case lttng_ust_ctl_atype_struct
:
6382 strncpy(new_field
->name
, field
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
6383 new_field
->type
.atype
= lttng_ust_ctl_atype_struct_nestable
;
6384 new_field
->type
.u
.struct_nestable
.nr_fields
= field
->type
.u
.legacy
._struct
.nr_fields
;
6385 new_field
->type
.u
.struct_nestable
.alignment
= 0;
6387 case lttng_ust_ctl_atype_variant
:
6388 strncpy(new_field
->name
, field
->name
, LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
6389 new_field
->type
.atype
= lttng_ust_ctl_atype_variant_nestable
;
6390 new_field
->type
.u
.variant_nestable
.nr_choices
= field
->type
.u
.legacy
.variant
.nr_choices
;
6391 strncpy(new_field
->type
.u
.variant_nestable
.tag_name
,
6392 field
->type
.u
.legacy
.variant
.tag_name
,
6393 LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
6394 new_field
->type
.u
.variant_nestable
.alignment
= 0;
6397 *new_field
= *field
;
6402 *_fields
= new_fields
;
6403 *_nr_fields
= new_nr_fields
;
6409 * Reply to a register channel notification from an application on the notify
6410 * socket. The channel metadata is also created.
6412 * The session UST registry lock is acquired in this function.
6414 * On success 0 is returned else a negative value.
6416 static int reply_ust_register_channel(int sock
, int cobjd
,
6417 size_t nr_fields
, struct lttng_ust_ctl_field
*fields
)
6419 int ret
, ret_code
= 0;
6421 uint64_t chan_reg_key
;
6422 enum lttng_ust_ctl_channel_header type
= LTTNG_UST_CTL_CHANNEL_HEADER_UNKNOWN
;
6423 struct ust_app
*app
;
6424 struct ust_app_channel
*ua_chan
;
6425 struct ust_app_session
*ua_sess
;
6426 struct ust_registry_session
*registry
;
6427 struct ust_registry_channel
*ust_reg_chan
;
6431 /* Lookup application. If not found, there is a code flow error. */
6432 app
= find_app_by_notify_sock(sock
);
6434 DBG("Application socket %d is being torn down. Abort event notify",
6437 goto error_rcu_unlock
;
6440 /* Lookup channel by UST object descriptor. */
6441 ua_chan
= find_channel_by_objd(app
, cobjd
);
6443 DBG("Application channel is being torn down. Abort event notify");
6445 goto error_rcu_unlock
;
6448 LTTNG_ASSERT(ua_chan
->session
);
6449 ua_sess
= ua_chan
->session
;
6451 /* Get right session registry depending on the session buffer type. */
6452 registry
= get_session_registry(ua_sess
);
6454 DBG("Application session is being torn down. Abort event notify");
6456 goto error_rcu_unlock
;
6459 /* Depending on the buffer type, a different channel key is used. */
6460 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6461 chan_reg_key
= ua_chan
->tracing_channel_id
;
6463 chan_reg_key
= ua_chan
->key
;
6466 pthread_mutex_lock(®istry
->lock
);
6468 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
6469 LTTNG_ASSERT(ust_reg_chan
);
6471 /* Channel id is set during the object creation. */
6472 chan_id
= ust_reg_chan
->chan_id
;
6474 ret
= ust_app_fixup_legacy_context_fields(&nr_fields
, &fields
);
6476 ERR("Registering application channel due to legacy context fields fixup error: pid = %d, sock = %d",
6477 app
->pid
, app
->sock
);
6481 if (!ust_reg_chan
->register_done
) {
6483 * TODO: eventually use the registry event count for
6484 * this channel to better guess header type for per-pid
6487 type
= LTTNG_UST_CTL_CHANNEL_HEADER_LARGE
;
6488 ust_reg_chan
->nr_ctx_fields
= nr_fields
;
6489 ust_reg_chan
->ctx_fields
= fields
;
6491 ust_reg_chan
->header_type
= type
;
6493 /* Get current already assigned values. */
6494 type
= ust_reg_chan
->header_type
;
6496 * Validate that the context fields match between
6497 * registry and newcoming application.
6499 if (!match_lttng_ust_ctl_field_array(ust_reg_chan
->ctx_fields
,
6500 ust_reg_chan
->nr_ctx_fields
,
6501 fields
, nr_fields
)) {
6502 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6503 app
->pid
, app
->sock
);
6509 /* Append to metadata */
6510 if (!ust_reg_chan
->metadata_dumped
) {
6511 ret_code
= ust_metadata_channel_statedump(registry
, ust_reg_chan
);
6513 ERR("Error appending channel metadata (errno = %d)", ret_code
);
6519 DBG3("UST app replying to register channel key %" PRIu64
6520 " with id %u, type = %d, ret = %d", chan_reg_key
, chan_id
, type
,
6523 ret
= lttng_ust_ctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
6525 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6526 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6527 app
->pid
, app
->sock
);
6528 } else if (ret
== -EAGAIN
) {
6529 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6530 app
->pid
, app
->sock
);
6532 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6533 ret
, app
->pid
, app
->sock
);
6538 /* This channel registry registration is completed. */
6539 ust_reg_chan
->register_done
= 1;
6542 pthread_mutex_unlock(®istry
->lock
);
6550 * Add event to the UST channel registry. When the event is added to the
6551 * registry, the metadata is also created. Once done, this replies to the
6552 * application with the appropriate error code.
6554 * The session UST registry lock is acquired in the function.
6556 * On success 0 is returned else a negative value.
6558 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
6559 char *sig
, size_t nr_fields
, struct lttng_ust_ctl_field
*fields
,
6560 int loglevel_value
, char *model_emf_uri
)
6563 uint32_t event_id
= 0;
6564 uint64_t chan_reg_key
;
6565 struct ust_app
*app
;
6566 struct ust_app_channel
*ua_chan
;
6567 struct ust_app_session
*ua_sess
;
6568 struct ust_registry_session
*registry
;
6572 /* Lookup application. If not found, there is a code flow error. */
6573 app
= find_app_by_notify_sock(sock
);
6575 DBG("Application socket %d is being torn down. Abort event notify",
6578 goto error_rcu_unlock
;
6581 /* Lookup channel by UST object descriptor. */
6582 ua_chan
= find_channel_by_objd(app
, cobjd
);
6584 DBG("Application channel is being torn down. Abort event notify");
6586 goto error_rcu_unlock
;
6589 LTTNG_ASSERT(ua_chan
->session
);
6590 ua_sess
= ua_chan
->session
;
6592 registry
= get_session_registry(ua_sess
);
6594 DBG("Application session is being torn down. Abort event notify");
6596 goto error_rcu_unlock
;
6599 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
6600 chan_reg_key
= ua_chan
->tracing_channel_id
;
6602 chan_reg_key
= ua_chan
->key
;
6605 pthread_mutex_lock(®istry
->lock
);
6608 * From this point on, this call acquires the ownership of the sig, fields
6609 * and model_emf_uri meaning any free are done inside it if needed. These
6610 * three variables MUST NOT be read/write after this.
6612 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
6613 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
6614 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
6618 model_emf_uri
= NULL
;
6621 * The return value is returned to ustctl so in case of an error, the
6622 * application can be notified. In case of an error, it's important not to
6623 * return a negative error or else the application will get closed.
6625 ret
= lttng_ust_ctl_reply_register_event(sock
, event_id
, ret_code
);
6627 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6628 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6629 app
->pid
, app
->sock
);
6630 } else if (ret
== -EAGAIN
) {
6631 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6632 app
->pid
, app
->sock
);
6634 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6635 ret
, app
->pid
, app
->sock
);
6638 * No need to wipe the create event since the application socket will
6639 * get close on error hence cleaning up everything by itself.
6644 DBG3("UST registry event %s with id %" PRId32
" added successfully",
6648 pthread_mutex_unlock(®istry
->lock
);
6653 free(model_emf_uri
);
6658 * Add enum to the UST session registry. Once done, this replies to the
6659 * application with the appropriate error code.
6661 * The session UST registry lock is acquired within this function.
6663 * On success 0 is returned else a negative value.
6665 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
6666 struct lttng_ust_ctl_enum_entry
*entries
, size_t nr_entries
)
6668 int ret
= 0, ret_code
;
6669 struct ust_app
*app
;
6670 struct ust_app_session
*ua_sess
;
6671 struct ust_registry_session
*registry
;
6672 uint64_t enum_id
= -1ULL;
6676 /* Lookup application. If not found, there is a code flow error. */
6677 app
= find_app_by_notify_sock(sock
);
6679 /* Return an error since this is not an error */
6680 DBG("Application socket %d is being torn down. Aborting enum registration",
6684 goto error_rcu_unlock
;
6687 /* Lookup session by UST object descriptor. */
6688 ua_sess
= find_session_by_objd(app
, sobjd
);
6690 /* Return an error since this is not an error */
6691 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6693 goto error_rcu_unlock
;
6696 registry
= get_session_registry(ua_sess
);
6698 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6700 goto error_rcu_unlock
;
6703 pthread_mutex_lock(®istry
->lock
);
6706 * From this point on, the callee acquires the ownership of
6707 * entries. The variable entries MUST NOT be read/written after
6710 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
6711 entries
, nr_entries
, &enum_id
);
6715 * The return value is returned to ustctl so in case of an error, the
6716 * application can be notified. In case of an error, it's important not to
6717 * return a negative error or else the application will get closed.
6719 ret
= lttng_ust_ctl_reply_register_enum(sock
, enum_id
, ret_code
);
6721 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6722 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6723 app
->pid
, app
->sock
);
6724 } else if (ret
== -EAGAIN
) {
6725 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6726 app
->pid
, app
->sock
);
6728 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6729 ret
, app
->pid
, app
->sock
);
6732 * No need to wipe the create enum since the application socket will
6733 * get close on error hence cleaning up everything by itself.
6738 DBG3("UST registry enum %s added successfully or already found", name
);
6741 pthread_mutex_unlock(®istry
->lock
);
6748 * Handle application notification through the given notify socket.
6750 * Return 0 on success or else a negative value.
6752 int ust_app_recv_notify(int sock
)
6755 enum lttng_ust_ctl_notify_cmd cmd
;
6757 DBG3("UST app receiving notify from sock %d", sock
);
6759 ret
= lttng_ust_ctl_recv_notify(sock
, &cmd
);
6761 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6762 DBG3("UST app recv notify failed. Application died: sock = %d",
6764 } else if (ret
== -EAGAIN
) {
6765 WARN("UST app recv notify failed. Communication time out: sock = %d",
6768 ERR("UST app recv notify failed with ret %d: sock = %d",
6775 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT
:
6777 int sobjd
, cobjd
, loglevel_value
;
6778 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
6780 struct lttng_ust_ctl_field
*fields
;
6782 DBG2("UST app ustctl register event received");
6784 ret
= lttng_ust_ctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
6785 &loglevel_value
, &sig
, &nr_fields
, &fields
,
6788 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6789 DBG3("UST app recv event failed. Application died: sock = %d",
6791 } else if (ret
== -EAGAIN
) {
6792 WARN("UST app recv event failed. Communication time out: sock = %d",
6795 ERR("UST app recv event failed with ret %d: sock = %d",
6802 * Add event to the UST registry coming from the notify socket. This
6803 * call will free if needed the sig, fields and model_emf_uri. This
6804 * code path loses the ownsership of these variables and transfer them
6805 * to the this function.
6807 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
6808 fields
, loglevel_value
, model_emf_uri
);
6815 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL
:
6819 struct lttng_ust_ctl_field
*fields
;
6821 DBG2("UST app ustctl register channel received");
6823 ret
= lttng_ust_ctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
6826 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6827 DBG3("UST app recv channel failed. Application died: sock = %d",
6829 } else if (ret
== -EAGAIN
) {
6830 WARN("UST app recv channel failed. Communication time out: sock = %d",
6833 ERR("UST app recv channel failed with ret %d: sock = %d",
6840 * The fields ownership are transfered to this function call meaning
6841 * that if needed it will be freed. After this, it's invalid to access
6842 * fields or clean it up.
6844 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
6852 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM
:
6855 char name
[LTTNG_UST_ABI_SYM_NAME_LEN
];
6857 struct lttng_ust_ctl_enum_entry
*entries
;
6859 DBG2("UST app ustctl register enum received");
6861 ret
= lttng_ust_ctl_recv_register_enum(sock
, &sobjd
, name
,
6862 &entries
, &nr_entries
);
6864 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
6865 DBG3("UST app recv enum failed. Application died: sock = %d",
6867 } else if (ret
== -EAGAIN
) {
6868 WARN("UST app recv enum failed. Communication time out: sock = %d",
6871 ERR("UST app recv enum failed with ret %d: sock = %d",
6877 /* Callee assumes ownership of entries */
6878 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
6879 entries
, nr_entries
);
6887 /* Should NEVER happen. */
6896 * Once the notify socket hangs up, this is called. First, it tries to find the
6897 * corresponding application. On failure, the call_rcu to close the socket is
6898 * executed. If an application is found, it tries to delete it from the notify
6899 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6901 * Note that an object needs to be allocated here so on ENOMEM failure, the
6902 * call RCU is not done but the rest of the cleanup is.
6904 void ust_app_notify_sock_unregister(int sock
)
6907 struct lttng_ht_iter iter
;
6908 struct ust_app
*app
;
6909 struct ust_app_notify_sock_obj
*obj
;
6911 LTTNG_ASSERT(sock
>= 0);
6915 obj
= (ust_app_notify_sock_obj
*) zmalloc(sizeof(*obj
));
6918 * An ENOMEM is kind of uncool. If this strikes we continue the
6919 * procedure but the call_rcu will not be called. In this case, we
6920 * accept the fd leak rather than possibly creating an unsynchronized
6921 * state between threads.
6923 * TODO: The notify object should be created once the notify socket is
6924 * registered and stored independantely from the ust app object. The
6925 * tricky part is to synchronize the teardown of the application and
6926 * this notify object. Let's keep that in mind so we can avoid this
6927 * kind of shenanigans with ENOMEM in the teardown path.
6934 DBG("UST app notify socket unregister %d", sock
);
6937 * Lookup application by notify socket. If this fails, this means that the
6938 * hash table delete has already been done by the application
6939 * unregistration process so we can safely close the notify socket in a
6942 app
= find_app_by_notify_sock(sock
);
6947 iter
.iter
.node
= &app
->notify_sock_n
.node
;
6950 * Whatever happens here either we fail or succeed, in both cases we have
6951 * to close the socket after a grace period to continue to the call RCU
6952 * here. If the deletion is successful, the application is not visible
6953 * anymore by other threads and is it fails it means that it was already
6954 * deleted from the hash table so either way we just have to close the
6957 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
6963 * Close socket after a grace period to avoid for the socket to be reused
6964 * before the application object is freed creating potential race between
6965 * threads trying to add unique in the global hash table.
6968 call_rcu(&obj
->head
, close_notify_sock_rcu
);
6973 * Destroy a ust app data structure and free its memory.
6975 void ust_app_destroy(struct ust_app
*app
)
6981 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
6985 * Take a snapshot for a given UST session. The snapshot is sent to the given
6988 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6990 enum lttng_error_code
ust_app_snapshot_record(
6991 const struct ltt_ust_session
*usess
,
6992 const struct consumer_output
*output
, int wait
,
6993 uint64_t nb_packets_per_stream
)
6996 enum lttng_error_code status
= LTTNG_OK
;
6997 struct lttng_ht_iter iter
;
6998 struct ust_app
*app
;
6999 char *trace_path
= NULL
;
7001 LTTNG_ASSERT(usess
);
7002 LTTNG_ASSERT(output
);
7006 switch (usess
->buffer_type
) {
7007 case LTTNG_BUFFER_PER_UID
:
7009 struct buffer_reg_uid
*reg
;
7011 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7012 struct buffer_reg_channel
*buf_reg_chan
;
7013 struct consumer_socket
*socket
;
7014 char pathname
[PATH_MAX
];
7015 size_t consumer_path_offset
= 0;
7017 if (!reg
->registry
->reg
.ust
->metadata_key
) {
7018 /* Skip since no metadata is present */
7022 /* Get consumer socket to use to push the metadata.*/
7023 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7026 status
= LTTNG_ERR_INVALID
;
7030 memset(pathname
, 0, sizeof(pathname
));
7031 ret
= snprintf(pathname
, sizeof(pathname
),
7032 DEFAULT_UST_TRACE_UID_PATH
,
7033 reg
->uid
, reg
->bits_per_long
);
7035 PERROR("snprintf snapshot path");
7036 status
= LTTNG_ERR_INVALID
;
7039 /* Free path allowed on previous iteration. */
7041 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
7042 &consumer_path_offset
);
7044 status
= LTTNG_ERR_INVALID
;
7047 /* Add the UST default trace dir to path. */
7048 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7049 buf_reg_chan
, node
.node
) {
7050 status
= consumer_snapshot_channel(socket
,
7051 buf_reg_chan
->consumer_key
,
7052 output
, 0, usess
->uid
,
7053 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
7054 nb_packets_per_stream
);
7055 if (status
!= LTTNG_OK
) {
7059 status
= consumer_snapshot_channel(socket
,
7060 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
7061 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
7063 if (status
!= LTTNG_OK
) {
7069 case LTTNG_BUFFER_PER_PID
:
7071 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7072 struct consumer_socket
*socket
;
7073 struct lttng_ht_iter chan_iter
;
7074 struct ust_app_channel
*ua_chan
;
7075 struct ust_app_session
*ua_sess
;
7076 struct ust_registry_session
*registry
;
7077 char pathname
[PATH_MAX
];
7078 size_t consumer_path_offset
= 0;
7080 ua_sess
= lookup_session_by_app(usess
, app
);
7082 /* Session not associated with this app. */
7086 /* Get the right consumer socket for the application. */
7087 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7090 status
= LTTNG_ERR_INVALID
;
7094 /* Add the UST default trace dir to path. */
7095 memset(pathname
, 0, sizeof(pathname
));
7096 ret
= snprintf(pathname
, sizeof(pathname
), "%s",
7099 status
= LTTNG_ERR_INVALID
;
7100 PERROR("snprintf snapshot path");
7103 /* Free path allowed on previous iteration. */
7105 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
7106 &consumer_path_offset
);
7108 status
= LTTNG_ERR_INVALID
;
7111 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7112 ua_chan
, node
.node
) {
7113 status
= consumer_snapshot_channel(socket
,
7114 ua_chan
->key
, output
, 0,
7115 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7116 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7117 &trace_path
[consumer_path_offset
], wait
,
7118 nb_packets_per_stream
);
7122 case LTTNG_ERR_CHAN_NOT_FOUND
:
7129 registry
= get_session_registry(ua_sess
);
7131 DBG("Application session is being torn down. Skip application.");
7134 status
= consumer_snapshot_channel(socket
,
7135 registry
->metadata_key
, output
, 1,
7136 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7137 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7138 &trace_path
[consumer_path_offset
], wait
, 0);
7142 case LTTNG_ERR_CHAN_NOT_FOUND
:
7162 * Return the size taken by one more packet per stream.
7164 uint64_t ust_app_get_size_one_more_packet_per_stream(
7165 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
7167 uint64_t tot_size
= 0;
7168 struct ust_app
*app
;
7169 struct lttng_ht_iter iter
;
7171 LTTNG_ASSERT(usess
);
7173 switch (usess
->buffer_type
) {
7174 case LTTNG_BUFFER_PER_UID
:
7176 struct buffer_reg_uid
*reg
;
7178 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7179 struct buffer_reg_channel
*buf_reg_chan
;
7182 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7183 buf_reg_chan
, node
.node
) {
7184 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
7186 * Don't take channel into account if we
7187 * already grab all its packets.
7191 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
7197 case LTTNG_BUFFER_PER_PID
:
7200 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7201 struct ust_app_channel
*ua_chan
;
7202 struct ust_app_session
*ua_sess
;
7203 struct lttng_ht_iter chan_iter
;
7205 ua_sess
= lookup_session_by_app(usess
, app
);
7207 /* Session not associated with this app. */
7211 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7212 ua_chan
, node
.node
) {
7213 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
7215 * Don't take channel into account if we
7216 * already grab all its packets.
7220 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
7234 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
7235 struct cds_list_head
*buffer_reg_uid_list
,
7236 struct consumer_output
*consumer
, uint64_t uchan_id
,
7237 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
7240 uint64_t consumer_chan_key
;
7245 ret
= buffer_reg_uid_consumer_channel_key(
7246 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
7254 ret
= consumer_get_lost_packets(ust_session_id
,
7255 consumer_chan_key
, consumer
, lost
);
7257 ret
= consumer_get_discarded_events(ust_session_id
,
7258 consumer_chan_key
, consumer
, discarded
);
7265 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
7266 struct ltt_ust_channel
*uchan
,
7267 struct consumer_output
*consumer
, int overwrite
,
7268 uint64_t *discarded
, uint64_t *lost
)
7271 struct lttng_ht_iter iter
;
7272 struct lttng_ht_node_str
*ua_chan_node
;
7273 struct ust_app
*app
;
7274 struct ust_app_session
*ua_sess
;
7275 struct ust_app_channel
*ua_chan
;
7282 * Iterate over every registered applications. Sum counters for
7283 * all applications containing requested session and channel.
7285 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7286 struct lttng_ht_iter uiter
;
7288 ua_sess
= lookup_session_by_app(usess
, app
);
7289 if (ua_sess
== NULL
) {
7294 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
7295 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
7296 /* If the session is found for the app, the channel must be there */
7297 LTTNG_ASSERT(ua_chan_node
);
7299 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
7304 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
7311 uint64_t _discarded
;
7313 ret
= consumer_get_discarded_events(usess
->id
,
7314 ua_chan
->key
, consumer
, &_discarded
);
7318 (*discarded
) += _discarded
;
7327 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
7328 struct ust_app
*app
)
7331 struct ust_app_session
*ua_sess
;
7333 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
7337 ua_sess
= lookup_session_by_app(usess
, app
);
7338 if (ua_sess
== NULL
) {
7339 /* The session is in teardown process. Ignore and continue. */
7343 pthread_mutex_lock(&ua_sess
->lock
);
7345 if (ua_sess
->deleted
) {
7349 pthread_mutex_lock(&app
->sock_lock
);
7350 ret
= lttng_ust_ctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
7351 pthread_mutex_unlock(&app
->sock_lock
);
7354 pthread_mutex_unlock(&ua_sess
->lock
);
7358 health_code_update();
7363 * Regenerate the statedump for each app in the session.
7365 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
7368 struct lttng_ht_iter iter
;
7369 struct ust_app
*app
;
7371 DBG("Regenerating the metadata for all UST apps");
7375 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7376 if (!app
->compatible
) {
7380 ret
= ust_app_regenerate_statedump(usess
, app
);
7382 /* Continue to the next app even on error */
7393 * Rotate all the channels of a session.
7395 * Return LTTNG_OK on success or else an LTTng error code.
7397 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
7400 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7401 struct lttng_ht_iter iter
;
7402 struct ust_app
*app
;
7403 struct ltt_ust_session
*usess
= session
->ust_session
;
7405 LTTNG_ASSERT(usess
);
7409 switch (usess
->buffer_type
) {
7410 case LTTNG_BUFFER_PER_UID
:
7412 struct buffer_reg_uid
*reg
;
7414 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7415 struct buffer_reg_channel
*buf_reg_chan
;
7416 struct consumer_socket
*socket
;
7418 /* Get consumer socket to use to push the metadata.*/
7419 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7422 cmd_ret
= LTTNG_ERR_INVALID
;
7426 /* Rotate the data channels. */
7427 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7428 buf_reg_chan
, node
.node
) {
7429 ret
= consumer_rotate_channel(socket
,
7430 buf_reg_chan
->consumer_key
,
7431 usess
->uid
, usess
->gid
,
7433 /* is_metadata_channel */ false);
7435 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7441 * The metadata channel might not be present.
7443 * Consumer stream allocation can be done
7444 * asynchronously and can fail on intermediary
7445 * operations (i.e add context) and lead to data
7446 * channels created with no metadata channel.
7448 if (!reg
->registry
->reg
.ust
->metadata_key
) {
7449 /* Skip since no metadata is present. */
7453 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7455 ret
= consumer_rotate_channel(socket
,
7456 reg
->registry
->reg
.ust
->metadata_key
,
7457 usess
->uid
, usess
->gid
,
7459 /* is_metadata_channel */ true);
7461 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7467 case LTTNG_BUFFER_PER_PID
:
7469 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7470 struct consumer_socket
*socket
;
7471 struct lttng_ht_iter chan_iter
;
7472 struct ust_app_channel
*ua_chan
;
7473 struct ust_app_session
*ua_sess
;
7474 struct ust_registry_session
*registry
;
7476 ua_sess
= lookup_session_by_app(usess
, app
);
7478 /* Session not associated with this app. */
7482 /* Get the right consumer socket for the application. */
7483 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7486 cmd_ret
= LTTNG_ERR_INVALID
;
7490 registry
= get_session_registry(ua_sess
);
7492 DBG("Application session is being torn down. Skip application.");
7496 /* Rotate the data channels. */
7497 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7498 ua_chan
, node
.node
) {
7499 ret
= consumer_rotate_channel(socket
,
7501 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7502 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7504 /* is_metadata_channel */ false);
7506 /* Per-PID buffer and application going away. */
7507 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7509 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7514 /* Rotate the metadata channel. */
7515 (void) push_metadata(registry
, usess
->consumer
);
7516 ret
= consumer_rotate_channel(socket
,
7517 registry
->metadata_key
,
7518 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
7519 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
7521 /* is_metadata_channel */ true);
7523 /* Per-PID buffer and application going away. */
7524 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
7526 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
7544 enum lttng_error_code
ust_app_create_channel_subdirectories(
7545 const struct ltt_ust_session
*usess
)
7547 enum lttng_error_code ret
= LTTNG_OK
;
7548 struct lttng_ht_iter iter
;
7549 enum lttng_trace_chunk_status chunk_status
;
7550 char *pathname_index
;
7553 LTTNG_ASSERT(usess
->current_trace_chunk
);
7556 switch (usess
->buffer_type
) {
7557 case LTTNG_BUFFER_PER_UID
:
7559 struct buffer_reg_uid
*reg
;
7561 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7562 fmt_ret
= asprintf(&pathname_index
,
7563 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
7564 reg
->uid
, reg
->bits_per_long
);
7566 ERR("Failed to format channel index directory");
7567 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7572 * Create the index subdirectory which will take care
7573 * of implicitly creating the channel's path.
7575 chunk_status
= lttng_trace_chunk_create_subdirectory(
7576 usess
->current_trace_chunk
,
7578 free(pathname_index
);
7579 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7580 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7586 case LTTNG_BUFFER_PER_PID
:
7588 struct ust_app
*app
;
7591 * Create the toplevel ust/ directory in case no apps are running.
7593 chunk_status
= lttng_trace_chunk_create_subdirectory(
7594 usess
->current_trace_chunk
,
7595 DEFAULT_UST_TRACE_DIR
);
7596 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7597 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7601 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7603 struct ust_app_session
*ua_sess
;
7604 struct ust_registry_session
*registry
;
7606 ua_sess
= lookup_session_by_app(usess
, app
);
7608 /* Session not associated with this app. */
7612 registry
= get_session_registry(ua_sess
);
7614 DBG("Application session is being torn down. Skip application.");
7618 fmt_ret
= asprintf(&pathname_index
,
7619 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
7622 ERR("Failed to format channel index directory");
7623 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7627 * Create the index subdirectory which will take care
7628 * of implicitly creating the channel's path.
7630 chunk_status
= lttng_trace_chunk_create_subdirectory(
7631 usess
->current_trace_chunk
,
7633 free(pathname_index
);
7634 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
7635 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
7652 * Clear all the channels of a session.
7654 * Return LTTNG_OK on success or else an LTTng error code.
7656 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
7659 enum lttng_error_code cmd_ret
= LTTNG_OK
;
7660 struct lttng_ht_iter iter
;
7661 struct ust_app
*app
;
7662 struct ltt_ust_session
*usess
= session
->ust_session
;
7664 LTTNG_ASSERT(usess
);
7668 if (usess
->active
) {
7669 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
7670 cmd_ret
= LTTNG_ERR_FATAL
;
7674 switch (usess
->buffer_type
) {
7675 case LTTNG_BUFFER_PER_UID
:
7677 struct buffer_reg_uid
*reg
;
7679 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7680 struct buffer_reg_channel
*buf_reg_chan
;
7681 struct consumer_socket
*socket
;
7683 /* Get consumer socket to use to push the metadata.*/
7684 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7687 cmd_ret
= LTTNG_ERR_INVALID
;
7691 /* Clear the data channels. */
7692 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7693 buf_reg_chan
, node
.node
) {
7694 ret
= consumer_clear_channel(socket
,
7695 buf_reg_chan
->consumer_key
);
7701 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
7704 * Clear the metadata channel.
7705 * Metadata channel is not cleared per se but we still need to
7706 * perform a rotation operation on it behind the scene.
7708 ret
= consumer_clear_channel(socket
,
7709 reg
->registry
->reg
.ust
->metadata_key
);
7716 case LTTNG_BUFFER_PER_PID
:
7718 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7719 struct consumer_socket
*socket
;
7720 struct lttng_ht_iter chan_iter
;
7721 struct ust_app_channel
*ua_chan
;
7722 struct ust_app_session
*ua_sess
;
7723 struct ust_registry_session
*registry
;
7725 ua_sess
= lookup_session_by_app(usess
, app
);
7727 /* Session not associated with this app. */
7731 /* Get the right consumer socket for the application. */
7732 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7735 cmd_ret
= LTTNG_ERR_INVALID
;
7739 registry
= get_session_registry(ua_sess
);
7741 DBG("Application session is being torn down. Skip application.");
7745 /* Clear the data channels. */
7746 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
7747 ua_chan
, node
.node
) {
7748 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
7750 /* Per-PID buffer and application going away. */
7751 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7758 (void) push_metadata(registry
, usess
->consumer
);
7761 * Clear the metadata channel.
7762 * Metadata channel is not cleared per se but we still need to
7763 * perform rotation operation on it behind the scene.
7765 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
7767 /* Per-PID buffer and application going away. */
7768 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7786 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
7787 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
7790 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
7800 * This function skips the metadata channel as the begin/end timestamps of a
7801 * metadata packet are useless.
7803 * Moreover, opening a packet after a "clear" will cause problems for live
7804 * sessions as it will introduce padding that was not part of the first trace
7805 * chunk. The relay daemon expects the content of the metadata stream of
7806 * successive metadata trace chunks to be strict supersets of one another.
7808 * For example, flushing a packet at the beginning of the metadata stream of
7809 * a trace chunk resulting from a "clear" session command will cause the
7810 * size of the metadata stream of the new trace chunk to not match the size of
7811 * the metadata stream of the original chunk. This will confuse the relay
7812 * daemon as the same "offset" in a metadata stream will no longer point
7813 * to the same content.
7815 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
7817 enum lttng_error_code ret
= LTTNG_OK
;
7818 struct lttng_ht_iter iter
;
7819 struct ltt_ust_session
*usess
= session
->ust_session
;
7821 LTTNG_ASSERT(usess
);
7825 switch (usess
->buffer_type
) {
7826 case LTTNG_BUFFER_PER_UID
:
7828 struct buffer_reg_uid
*reg
;
7830 cds_list_for_each_entry (
7831 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7832 struct buffer_reg_channel
*buf_reg_chan
;
7833 struct consumer_socket
*socket
;
7835 socket
= consumer_find_socket_by_bitness(
7836 reg
->bits_per_long
, usess
->consumer
);
7838 ret
= LTTNG_ERR_FATAL
;
7842 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7843 &iter
.iter
, buf_reg_chan
, node
.node
) {
7844 const int open_ret
=
7845 consumer_open_channel_packets(
7847 buf_reg_chan
->consumer_key
);
7850 ret
= LTTNG_ERR_UNK
;
7857 case LTTNG_BUFFER_PER_PID
:
7859 struct ust_app
*app
;
7861 cds_lfht_for_each_entry (
7862 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7863 struct consumer_socket
*socket
;
7864 struct lttng_ht_iter chan_iter
;
7865 struct ust_app_channel
*ua_chan
;
7866 struct ust_app_session
*ua_sess
;
7867 struct ust_registry_session
*registry
;
7869 ua_sess
= lookup_session_by_app(usess
, app
);
7871 /* Session not associated with this app. */
7875 /* Get the right consumer socket for the application. */
7876 socket
= consumer_find_socket_by_bitness(
7877 app
->bits_per_long
, usess
->consumer
);
7879 ret
= LTTNG_ERR_FATAL
;
7883 registry
= get_session_registry(ua_sess
);
7885 DBG("Application session is being torn down. Skip application.");
7889 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
7890 &chan_iter
.iter
, ua_chan
, node
.node
) {
7891 const int open_ret
=
7892 consumer_open_channel_packets(
7898 * Per-PID buffer and application going
7901 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
7905 ret
= LTTNG_ERR_UNK
;