2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
16 #include <sys/types.h>
18 #include <urcu/compiler.h>
21 #include <common/bytecode/bytecode.h>
22 #include <common/compat/errno.h>
23 #include <common/common.h>
24 #include <common/hashtable/utils.h>
25 #include <lttng/event-rule/event-rule.h>
26 #include <lttng/event-rule/event-rule-internal.h>
27 #include <lttng/event-rule/tracepoint.h>
28 #include <lttng/condition/condition.h>
29 #include <lttng/condition/event-rule-internal.h>
30 #include <lttng/condition/event-rule.h>
31 #include <lttng/trigger/trigger-internal.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
34 #include "buffer-registry.h"
36 #include "health-sessiond.h"
38 #include "ust-consumer.h"
39 #include "lttng-ust-ctl.h"
40 #include "lttng-ust-error.h"
43 #include "lttng-sessiond.h"
44 #include "notification-thread-commands.h"
48 struct lttng_ht
*ust_app_ht
;
49 struct lttng_ht
*ust_app_ht_by_sock
;
50 struct lttng_ht
*ust_app_ht_by_notify_sock
;
53 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
55 /* Next available channel key. Access under next_channel_key_lock. */
56 static uint64_t _next_channel_key
;
57 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
59 /* Next available session ID. Access under next_session_id_lock. */
60 static uint64_t _next_session_id
;
61 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
64 * Return the incremented value of next_channel_key.
66 static uint64_t get_next_channel_key(void)
70 pthread_mutex_lock(&next_channel_key_lock
);
71 ret
= ++_next_channel_key
;
72 pthread_mutex_unlock(&next_channel_key_lock
);
77 * Return the atomically incremented value of next_session_id.
79 static uint64_t get_next_session_id(void)
83 pthread_mutex_lock(&next_session_id_lock
);
84 ret
= ++_next_session_id
;
85 pthread_mutex_unlock(&next_session_id_lock
);
89 static void copy_channel_attr_to_ustctl(
90 struct ustctl_consumer_channel_attr
*attr
,
91 struct lttng_ust_abi_channel_attr
*uattr
)
93 /* Copy event attributes since the layout is different. */
94 attr
->subbuf_size
= uattr
->subbuf_size
;
95 attr
->num_subbuf
= uattr
->num_subbuf
;
96 attr
->overwrite
= uattr
->overwrite
;
97 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
98 attr
->read_timer_interval
= uattr
->read_timer_interval
;
99 attr
->output
= uattr
->output
;
100 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
104 * Match function for the hash table lookup.
106 * It matches an ust app event based on three attributes which are the event
107 * name, the filter bytecode and the loglevel.
109 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
111 struct ust_app_event
*event
;
112 const struct ust_app_ht_key
*key
;
113 int ev_loglevel_value
;
118 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
120 ev_loglevel_value
= event
->attr
.loglevel
;
122 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
125 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
129 /* Event loglevel. */
130 if (ev_loglevel_value
!= key
->loglevel_type
) {
131 if (event
->attr
.loglevel_type
== LTTNG_UST_ABI_LOGLEVEL_ALL
132 && key
->loglevel_type
== 0 &&
133 ev_loglevel_value
== -1) {
135 * Match is accepted. This is because on event creation, the
136 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
137 * -1 are accepted for this loglevel type since 0 is the one set by
138 * the API when receiving an enable event.
145 /* One of the filters is NULL, fail. */
146 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
150 if (key
->filter
&& event
->filter
) {
151 /* Both filters exists, check length followed by the bytecode. */
152 if (event
->filter
->len
!= key
->filter
->len
||
153 memcmp(event
->filter
->data
, key
->filter
->data
,
154 event
->filter
->len
) != 0) {
159 /* One of the exclusions is NULL, fail. */
160 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
164 if (key
->exclusion
&& event
->exclusion
) {
165 /* Both exclusions exists, check count followed by the names. */
166 if (event
->exclusion
->count
!= key
->exclusion
->count
||
167 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
168 event
->exclusion
->count
* LTTNG_UST_ABI_SYM_NAME_LEN
) != 0) {
182 * Unique add of an ust app event in the given ht. This uses the custom
183 * ht_match_ust_app_event match function and the event name as hash.
185 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
186 struct ust_app_event
*event
)
188 struct cds_lfht_node
*node_ptr
;
189 struct ust_app_ht_key key
;
193 assert(ua_chan
->events
);
196 ht
= ua_chan
->events
;
197 key
.name
= event
->attr
.name
;
198 key
.filter
= event
->filter
;
199 key
.loglevel_type
= event
->attr
.loglevel
;
200 key
.exclusion
= event
->exclusion
;
202 node_ptr
= cds_lfht_add_unique(ht
->ht
,
203 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
204 ht_match_ust_app_event
, &key
, &event
->node
.node
);
205 assert(node_ptr
== &event
->node
.node
);
209 * Close the notify socket from the given RCU head object. This MUST be called
210 * through a call_rcu().
212 static void close_notify_sock_rcu(struct rcu_head
*head
)
215 struct ust_app_notify_sock_obj
*obj
=
216 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
218 /* Must have a valid fd here. */
219 assert(obj
->fd
>= 0);
221 ret
= close(obj
->fd
);
223 ERR("close notify sock %d RCU", obj
->fd
);
225 lttng_fd_put(LTTNG_FD_APPS
, 1);
231 * Return the session registry according to the buffer type of the given
234 * A registry per UID object MUST exists before calling this function or else
235 * it assert() if not found. RCU read side lock must be acquired.
237 static struct ust_registry_session
*get_session_registry(
238 struct ust_app_session
*ua_sess
)
240 struct ust_registry_session
*registry
= NULL
;
244 switch (ua_sess
->buffer_type
) {
245 case LTTNG_BUFFER_PER_PID
:
247 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
251 registry
= reg_pid
->registry
->reg
.ust
;
254 case LTTNG_BUFFER_PER_UID
:
256 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
257 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
258 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
262 registry
= reg_uid
->registry
->reg
.ust
;
274 * Delete ust context safely. RCU read lock must be held before calling
278 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
286 pthread_mutex_lock(&app
->sock_lock
);
287 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
288 pthread_mutex_unlock(&app
->sock_lock
);
289 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
290 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
291 sock
, ua_ctx
->obj
->handle
, ret
);
299 * Delete ust app event safely. RCU read lock must be held before calling
303 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
310 free(ua_event
->filter
);
311 if (ua_event
->exclusion
!= NULL
)
312 free(ua_event
->exclusion
);
313 if (ua_event
->obj
!= NULL
) {
314 pthread_mutex_lock(&app
->sock_lock
);
315 ret
= ustctl_release_object(sock
, ua_event
->obj
);
316 pthread_mutex_unlock(&app
->sock_lock
);
317 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
318 ERR("UST app sock %d release event obj failed with ret %d",
327 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
328 * through a call_rcu().
331 void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
333 struct ust_app_event_notifier_rule
*obj
= caa_container_of(
334 head
, struct ust_app_event_notifier_rule
, rcu_head
);
340 * Delete ust app event notifier rule safely.
342 static void delete_ust_app_event_notifier_rule(int sock
,
343 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
,
348 assert(ua_event_notifier_rule
);
350 if (ua_event_notifier_rule
->exclusion
!= NULL
) {
351 free(ua_event_notifier_rule
->exclusion
);
354 if (ua_event_notifier_rule
->obj
!= NULL
) {
355 pthread_mutex_lock(&app
->sock_lock
);
356 ret
= ustctl_release_object(sock
, ua_event_notifier_rule
->obj
);
357 pthread_mutex_unlock(&app
->sock_lock
);
358 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
359 ERR("Failed to release event notifier object: app = '%s' (ppid %d), ret = %d",
360 app
->name
, (int) app
->ppid
, ret
);
363 free(ua_event_notifier_rule
->obj
);
366 lttng_trigger_put(ua_event_notifier_rule
->trigger
);
367 call_rcu(&ua_event_notifier_rule
->rcu_head
,
368 free_ust_app_event_notifier_rule_rcu
);
372 * Release ust data object of the given stream.
374 * Return 0 on success or else a negative value.
376 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
384 pthread_mutex_lock(&app
->sock_lock
);
385 ret
= ustctl_release_object(sock
, stream
->obj
);
386 pthread_mutex_unlock(&app
->sock_lock
);
387 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
388 ERR("UST app sock %d release stream obj failed with ret %d",
391 lttng_fd_put(LTTNG_FD_APPS
, 2);
399 * Delete ust app stream safely. RCU read lock must be held before calling
403 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
408 (void) release_ust_app_stream(sock
, stream
, app
);
413 * We need to execute ht_destroy outside of RCU read-side critical
414 * section and outside of call_rcu thread, so we postpone its execution
415 * using ht_cleanup_push. It is simpler than to change the semantic of
416 * the many callers of delete_ust_app_session().
419 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
421 struct ust_app_channel
*ua_chan
=
422 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
424 ht_cleanup_push(ua_chan
->ctx
);
425 ht_cleanup_push(ua_chan
->events
);
430 * Extract the lost packet or discarded events counter when the channel is
431 * being deleted and store the value in the parent channel so we can
432 * access it from lttng list and at stop/destroy.
434 * The session list lock must be held by the caller.
437 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
439 uint64_t discarded
= 0, lost
= 0;
440 struct ltt_session
*session
;
441 struct ltt_ust_channel
*uchan
;
443 if (ua_chan
->attr
.type
!= LTTNG_UST_ABI_CHAN_PER_CPU
) {
448 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
449 if (!session
|| !session
->ust_session
) {
451 * Not finding the session is not an error because there are
452 * multiple ways the channels can be torn down.
454 * 1) The session daemon can initiate the destruction of the
455 * ust app session after receiving a destroy command or
456 * during its shutdown/teardown.
457 * 2) The application, since we are in per-pid tracing, is
458 * unregistering and tearing down its ust app session.
460 * Both paths are protected by the session list lock which
461 * ensures that the accounting of lost packets and discarded
462 * events is done exactly once. The session is then unpublished
463 * from the session list, resulting in this condition.
468 if (ua_chan
->attr
.overwrite
) {
469 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
470 ua_chan
->key
, session
->ust_session
->consumer
,
473 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
474 ua_chan
->key
, session
->ust_session
->consumer
,
477 uchan
= trace_ust_find_channel_by_name(
478 session
->ust_session
->domain_global
.channels
,
481 ERR("Missing UST channel to store discarded counters");
485 uchan
->per_pid_closed_app_discarded
+= discarded
;
486 uchan
->per_pid_closed_app_lost
+= lost
;
491 session_put(session
);
496 * Delete ust app channel safely. RCU read lock must be held before calling
499 * The session list lock must be held by the caller.
502 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
506 struct lttng_ht_iter iter
;
507 struct ust_app_event
*ua_event
;
508 struct ust_app_ctx
*ua_ctx
;
509 struct ust_app_stream
*stream
, *stmp
;
510 struct ust_registry_session
*registry
;
514 DBG3("UST app deleting channel %s", ua_chan
->name
);
517 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
518 cds_list_del(&stream
->list
);
519 delete_ust_app_stream(sock
, stream
, app
);
523 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
524 cds_list_del(&ua_ctx
->list
);
525 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
527 delete_ust_app_ctx(sock
, ua_ctx
, app
);
531 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
533 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
535 delete_ust_app_event(sock
, ua_event
, app
);
538 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
539 /* Wipe and free registry from session registry. */
540 registry
= get_session_registry(ua_chan
->session
);
542 ust_registry_channel_del_free(registry
, ua_chan
->key
,
546 * A negative socket can be used by the caller when
547 * cleaning-up a ua_chan in an error path. Skip the
548 * accounting in this case.
551 save_per_pid_lost_discarded_counters(ua_chan
);
555 if (ua_chan
->obj
!= NULL
) {
556 /* Remove channel from application UST object descriptor. */
557 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
558 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
560 pthread_mutex_lock(&app
->sock_lock
);
561 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
562 pthread_mutex_unlock(&app
->sock_lock
);
563 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
564 ERR("UST app sock %d release channel obj failed with ret %d",
567 lttng_fd_put(LTTNG_FD_APPS
, 1);
570 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
573 int ust_app_register_done(struct ust_app
*app
)
577 pthread_mutex_lock(&app
->sock_lock
);
578 ret
= ustctl_register_done(app
->sock
);
579 pthread_mutex_unlock(&app
->sock_lock
);
583 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_abi_object_data
*data
)
588 pthread_mutex_lock(&app
->sock_lock
);
593 ret
= ustctl_release_object(sock
, data
);
595 pthread_mutex_unlock(&app
->sock_lock
);
601 * Push metadata to consumer socket.
603 * RCU read-side lock must be held to guarantee existance of socket.
604 * Must be called with the ust app session lock held.
605 * Must be called with the registry lock held.
607 * On success, return the len of metadata pushed or else a negative value.
608 * Returning a -EPIPE return value means we could not send the metadata,
609 * but it can be caused by recoverable errors (e.g. the application has
610 * terminated concurrently).
612 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
613 struct consumer_socket
*socket
, int send_zero_data
)
616 char *metadata_str
= NULL
;
617 size_t len
, offset
, new_metadata_len_sent
;
619 uint64_t metadata_key
, metadata_version
;
624 metadata_key
= registry
->metadata_key
;
627 * Means that no metadata was assigned to the session. This can
628 * happens if no start has been done previously.
634 offset
= registry
->metadata_len_sent
;
635 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
636 new_metadata_len_sent
= registry
->metadata_len
;
637 metadata_version
= registry
->metadata_version
;
639 DBG3("No metadata to push for metadata key %" PRIu64
,
640 registry
->metadata_key
);
642 if (send_zero_data
) {
643 DBG("No metadata to push");
649 /* Allocate only what we have to send. */
650 metadata_str
= zmalloc(len
);
652 PERROR("zmalloc ust app metadata string");
656 /* Copy what we haven't sent out. */
657 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
660 pthread_mutex_unlock(®istry
->lock
);
662 * We need to unlock the registry while we push metadata to
663 * break a circular dependency between the consumerd metadata
664 * lock and the sessiond registry lock. Indeed, pushing metadata
665 * to the consumerd awaits that it gets pushed all the way to
666 * relayd, but doing so requires grabbing the metadata lock. If
667 * a concurrent metadata request is being performed by
668 * consumerd, this can try to grab the registry lock on the
669 * sessiond while holding the metadata lock on the consumer
670 * daemon. Those push and pull schemes are performed on two
671 * different bidirectionnal communication sockets.
673 ret
= consumer_push_metadata(socket
, metadata_key
,
674 metadata_str
, len
, offset
, metadata_version
);
675 pthread_mutex_lock(®istry
->lock
);
678 * There is an acceptable race here between the registry
679 * metadata key assignment and the creation on the
680 * consumer. The session daemon can concurrently push
681 * metadata for this registry while being created on the
682 * consumer since the metadata key of the registry is
683 * assigned *before* it is setup to avoid the consumer
684 * to ask for metadata that could possibly be not found
685 * in the session daemon.
687 * The metadata will get pushed either by the session
688 * being stopped or the consumer requesting metadata if
689 * that race is triggered.
691 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
694 ERR("Error pushing metadata to consumer");
700 * Metadata may have been concurrently pushed, since
701 * we're not holding the registry lock while pushing to
702 * consumer. This is handled by the fact that we send
703 * the metadata content, size, and the offset at which
704 * that metadata belongs. This may arrive out of order
705 * on the consumer side, and the consumer is able to
706 * deal with overlapping fragments. The consumer
707 * supports overlapping fragments, which must be
708 * contiguous starting from offset 0. We keep the
709 * largest metadata_len_sent value of the concurrent
712 registry
->metadata_len_sent
=
713 max_t(size_t, registry
->metadata_len_sent
,
714 new_metadata_len_sent
);
723 * On error, flag the registry that the metadata is
724 * closed. We were unable to push anything and this
725 * means that either the consumer is not responding or
726 * the metadata cache has been destroyed on the
729 registry
->metadata_closed
= 1;
737 * For a given application and session, push metadata to consumer.
738 * Either sock or consumer is required : if sock is NULL, the default
739 * socket to send the metadata is retrieved from consumer, if sock
740 * is not NULL we use it to send the metadata.
741 * RCU read-side lock must be held while calling this function,
742 * therefore ensuring existance of registry. It also ensures existance
743 * of socket throughout this function.
745 * Return 0 on success else a negative error.
746 * Returning a -EPIPE return value means we could not send the metadata,
747 * but it can be caused by recoverable errors (e.g. the application has
748 * terminated concurrently).
750 static int push_metadata(struct ust_registry_session
*registry
,
751 struct consumer_output
*consumer
)
755 struct consumer_socket
*socket
;
760 pthread_mutex_lock(®istry
->lock
);
761 if (registry
->metadata_closed
) {
766 /* Get consumer socket to use to push the metadata.*/
767 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
774 ret
= ust_app_push_metadata(registry
, socket
, 0);
779 pthread_mutex_unlock(®istry
->lock
);
783 pthread_mutex_unlock(®istry
->lock
);
788 * Send to the consumer a close metadata command for the given session. Once
789 * done, the metadata channel is deleted and the session metadata pointer is
790 * nullified. The session lock MUST be held unless the application is
791 * in the destroy path.
793 * Do not hold the registry lock while communicating with the consumerd, because
794 * doing so causes inter-process deadlocks between consumerd and sessiond with
795 * the metadata request notification.
797 * Return 0 on success else a negative value.
799 static int close_metadata(struct ust_registry_session
*registry
,
800 struct consumer_output
*consumer
)
803 struct consumer_socket
*socket
;
804 uint64_t metadata_key
;
805 bool registry_was_already_closed
;
812 pthread_mutex_lock(®istry
->lock
);
813 metadata_key
= registry
->metadata_key
;
814 registry_was_already_closed
= registry
->metadata_closed
;
815 if (metadata_key
!= 0) {
817 * Metadata closed. Even on error this means that the consumer
818 * is not responding or not found so either way a second close
819 * should NOT be emit for this registry.
821 registry
->metadata_closed
= 1;
823 pthread_mutex_unlock(®istry
->lock
);
825 if (metadata_key
== 0 || registry_was_already_closed
) {
830 /* Get consumer socket to use to push the metadata.*/
831 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
838 ret
= consumer_close_metadata(socket
, metadata_key
);
849 * We need to execute ht_destroy outside of RCU read-side critical
850 * section and outside of call_rcu thread, so we postpone its execution
851 * using ht_cleanup_push. It is simpler than to change the semantic of
852 * the many callers of delete_ust_app_session().
855 void delete_ust_app_session_rcu(struct rcu_head
*head
)
857 struct ust_app_session
*ua_sess
=
858 caa_container_of(head
, struct ust_app_session
, rcu_head
);
860 ht_cleanup_push(ua_sess
->channels
);
865 * Delete ust app session safely. RCU read lock must be held before calling
868 * The session list lock must be held by the caller.
871 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
875 struct lttng_ht_iter iter
;
876 struct ust_app_channel
*ua_chan
;
877 struct ust_registry_session
*registry
;
881 pthread_mutex_lock(&ua_sess
->lock
);
883 assert(!ua_sess
->deleted
);
884 ua_sess
->deleted
= true;
886 registry
= get_session_registry(ua_sess
);
887 /* Registry can be null on error path during initialization. */
889 /* Push metadata for application before freeing the application. */
890 (void) push_metadata(registry
, ua_sess
->consumer
);
893 * Don't ask to close metadata for global per UID buffers. Close
894 * metadata only on destroy trace session in this case. Also, the
895 * previous push metadata could have flag the metadata registry to
896 * close so don't send a close command if closed.
898 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
899 /* And ask to close it for this session registry. */
900 (void) close_metadata(registry
, ua_sess
->consumer
);
904 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
906 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
908 delete_ust_app_channel(sock
, ua_chan
, app
);
911 /* In case of per PID, the registry is kept in the session. */
912 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
913 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
916 * Registry can be null on error path during
919 buffer_reg_pid_remove(reg_pid
);
920 buffer_reg_pid_destroy(reg_pid
);
924 if (ua_sess
->handle
!= -1) {
925 pthread_mutex_lock(&app
->sock_lock
);
926 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
927 pthread_mutex_unlock(&app
->sock_lock
);
928 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
929 ERR("UST app sock %d release session handle failed with ret %d",
932 /* Remove session from application UST object descriptor. */
933 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
934 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
938 pthread_mutex_unlock(&ua_sess
->lock
);
940 consumer_output_put(ua_sess
->consumer
);
942 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
946 * Delete a traceable application structure from the global list. Never call
947 * this function outside of a call_rcu call.
949 * RCU read side lock should _NOT_ be held when calling this function.
952 void delete_ust_app(struct ust_app
*app
)
955 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
956 struct lttng_ht_iter iter
;
957 struct ust_app_event_notifier_rule
*event_notifier_rule
;
958 bool event_notifier_write_fd_is_open
;
961 * The session list lock must be held during this function to guarantee
962 * the existence of ua_sess.
965 /* Delete ust app sessions info */
970 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
972 /* Free every object in the session and the session. */
974 delete_ust_app_session(sock
, ua_sess
, app
);
978 /* Remove the event notifier rules associated with this app. */
980 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
981 &iter
.iter
, event_notifier_rule
, node
.node
) {
982 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
985 delete_ust_app_event_notifier_rule(
986 app
->sock
, event_notifier_rule
, app
);
991 ht_cleanup_push(app
->sessions
);
992 ht_cleanup_push(app
->ust_sessions_objd
);
993 ht_cleanup_push(app
->ust_objd
);
994 ht_cleanup_push(app
->token_to_event_notifier_rule_ht
);
997 * This could be NULL if the event notifier setup failed (e.g the app
998 * was killed or the tracer does not support this feature).
1000 if (app
->event_notifier_group
.object
) {
1001 enum lttng_error_code ret_code
;
1002 const int event_notifier_read_fd
= lttng_pipe_get_readfd(
1003 app
->event_notifier_group
.event_pipe
);
1005 ret_code
= notification_thread_command_remove_tracer_event_source(
1006 notification_thread_handle
,
1007 event_notifier_read_fd
);
1008 if (ret_code
!= LTTNG_OK
) {
1009 ERR("Failed to remove application tracer event source from notification thread");
1012 ustctl_release_object(sock
, app
->event_notifier_group
.object
);
1013 free(app
->event_notifier_group
.object
);
1016 event_notifier_write_fd_is_open
= lttng_pipe_is_write_open(
1017 app
->event_notifier_group
.event_pipe
);
1018 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1020 * Release the file descriptors reserved for the event notifier pipe.
1021 * The app could be destroyed before the write end of the pipe could be
1022 * passed to the application (and closed). In that case, both file
1023 * descriptors must be released.
1025 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1028 * Wait until we have deleted the application from the sock hash table
1029 * before closing this socket, otherwise an application could re-use the
1030 * socket ID and race with the teardown, using the same hash table entry.
1032 * It's OK to leave the close in call_rcu. We want it to stay unique for
1033 * all RCU readers that could run concurrently with unregister app,
1034 * therefore we _need_ to only close that socket after a grace period. So
1035 * it should stay in this RCU callback.
1037 * This close() is a very important step of the synchronization model so
1038 * every modification to this function must be carefully reviewed.
1044 lttng_fd_put(LTTNG_FD_APPS
, 1);
1046 DBG2("UST app pid %d deleted", app
->pid
);
1048 session_unlock_list();
1052 * URCU intermediate call to delete an UST app.
1055 void delete_ust_app_rcu(struct rcu_head
*head
)
1057 struct lttng_ht_node_ulong
*node
=
1058 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1059 struct ust_app
*app
=
1060 caa_container_of(node
, struct ust_app
, pid_n
);
1062 DBG3("Call RCU deleting app PID %d", app
->pid
);
1063 delete_ust_app(app
);
1067 * Delete the session from the application ht and delete the data structure by
1068 * freeing every object inside and releasing them.
1070 * The session list lock must be held by the caller.
1072 static void destroy_app_session(struct ust_app
*app
,
1073 struct ust_app_session
*ua_sess
)
1076 struct lttng_ht_iter iter
;
1081 iter
.iter
.node
= &ua_sess
->node
.node
;
1082 ret
= lttng_ht_del(app
->sessions
, &iter
);
1084 /* Already scheduled for teardown. */
1088 /* Once deleted, free the data structure. */
1089 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1096 * Alloc new UST app session.
1099 struct ust_app_session
*alloc_ust_app_session(void)
1101 struct ust_app_session
*ua_sess
;
1103 /* Init most of the default value by allocating and zeroing */
1104 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1105 if (ua_sess
== NULL
) {
1110 ua_sess
->handle
= -1;
1111 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1112 ua_sess
->metadata_attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1113 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1122 * Alloc new UST app channel.
1125 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1126 struct ust_app_session
*ua_sess
,
1127 struct lttng_ust_abi_channel_attr
*attr
)
1129 struct ust_app_channel
*ua_chan
;
1131 /* Init most of the default value by allocating and zeroing */
1132 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1133 if (ua_chan
== NULL
) {
1138 /* Setup channel name */
1139 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1140 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1142 ua_chan
->enabled
= 1;
1143 ua_chan
->handle
= -1;
1144 ua_chan
->session
= ua_sess
;
1145 ua_chan
->key
= get_next_channel_key();
1146 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1147 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1148 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1150 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1151 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1153 /* Copy attributes */
1155 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1156 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1157 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1158 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1159 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1160 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1161 ua_chan
->attr
.output
= attr
->output
;
1162 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1164 /* By default, the channel is a per cpu channel. */
1165 ua_chan
->attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1167 DBG3("UST app channel %s allocated", ua_chan
->name
);
1176 * Allocate and initialize a UST app stream.
1178 * Return newly allocated stream pointer or NULL on error.
1180 struct ust_app_stream
*ust_app_alloc_stream(void)
1182 struct ust_app_stream
*stream
= NULL
;
1184 stream
= zmalloc(sizeof(*stream
));
1185 if (stream
== NULL
) {
1186 PERROR("zmalloc ust app stream");
1190 /* Zero could be a valid value for a handle so flag it to -1. */
1191 stream
->handle
= -1;
1198 * Alloc new UST app event.
1201 struct ust_app_event
*alloc_ust_app_event(char *name
,
1202 struct lttng_ust_abi_event
*attr
)
1204 struct ust_app_event
*ua_event
;
1206 /* Init most of the default value by allocating and zeroing */
1207 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1208 if (ua_event
== NULL
) {
1209 PERROR("Failed to allocate ust_app_event structure");
1213 ua_event
->enabled
= 1;
1214 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1215 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1216 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1218 /* Copy attributes */
1220 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1223 DBG3("UST app event %s allocated", ua_event
->name
);
1232 * Allocate a new UST app event notifier rule.
1234 static struct ust_app_event_notifier_rule
*alloc_ust_app_event_notifier_rule(
1235 struct lttng_trigger
*trigger
)
1237 enum lttng_event_rule_generate_exclusions_status
1238 generate_exclusion_status
;
1239 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1240 struct lttng_condition
*condition
= NULL
;
1241 const struct lttng_event_rule
*event_rule
= NULL
;
1243 ua_event_notifier_rule
= zmalloc(sizeof(struct ust_app_event_notifier_rule
));
1244 if (ua_event_notifier_rule
== NULL
) {
1245 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1249 ua_event_notifier_rule
->enabled
= 1;
1250 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1251 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
,
1252 ua_event_notifier_rule
->token
);
1254 condition
= lttng_trigger_get_condition(trigger
);
1256 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
);
1258 assert(LTTNG_CONDITION_STATUS_OK
== lttng_condition_event_rule_get_rule(condition
, &event_rule
));
1261 /* Acquire the event notifier's reference to the trigger. */
1262 lttng_trigger_get(trigger
);
1264 ua_event_notifier_rule
->trigger
= trigger
;
1265 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1266 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1267 event_rule
, &ua_event_notifier_rule
->exclusion
);
1268 switch (generate_exclusion_status
) {
1269 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1270 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1273 /* Error occured. */
1274 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1275 goto error_put_trigger
;
1278 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1279 ua_event_notifier_rule
->token
);
1281 return ua_event_notifier_rule
;
1284 lttng_trigger_put(trigger
);
1286 free(ua_event_notifier_rule
);
1291 * Alloc new UST app context.
1294 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1296 struct ust_app_ctx
*ua_ctx
;
1298 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1299 if (ua_ctx
== NULL
) {
1303 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1306 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1307 if (uctx
->ctx
== LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
) {
1308 char *provider_name
= NULL
, *ctx_name
= NULL
;
1310 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1311 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1312 if (!provider_name
|| !ctx_name
) {
1313 free(provider_name
);
1318 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1319 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1323 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1331 * Create a liblttng-ust filter bytecode from given bytecode.
1333 * Return allocated filter or NULL on error.
1335 static struct lttng_ust_abi_filter_bytecode
*create_ust_filter_bytecode_from_bytecode(
1336 const struct lttng_bytecode
*orig_f
)
1338 struct lttng_ust_abi_filter_bytecode
*filter
= NULL
;
1340 /* Copy filter bytecode. */
1341 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1343 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1347 assert(sizeof(struct lttng_bytecode
) ==
1348 sizeof(struct lttng_ust_abi_filter_bytecode
));
1349 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1355 * Create a liblttng-ust capture bytecode from given bytecode.
1357 * Return allocated filter or NULL on error.
1359 static struct lttng_ust_abi_capture_bytecode
*
1360 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1362 struct lttng_ust_abi_capture_bytecode
*capture
= NULL
;
1364 /* Copy capture bytecode. */
1365 capture
= zmalloc(sizeof(*capture
) + orig_f
->len
);
1367 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1371 assert(sizeof(struct lttng_bytecode
) ==
1372 sizeof(struct lttng_ust_abi_capture_bytecode
));
1373 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1379 * Find an ust_app using the sock and return it. RCU read side lock must be
1380 * held before calling this helper function.
1382 struct ust_app
*ust_app_find_by_sock(int sock
)
1384 struct lttng_ht_node_ulong
*node
;
1385 struct lttng_ht_iter iter
;
1387 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1388 node
= lttng_ht_iter_get_node_ulong(&iter
);
1390 DBG2("UST app find by sock %d not found", sock
);
1394 return caa_container_of(node
, struct ust_app
, sock_n
);
1401 * Find an ust_app using the notify sock and return it. RCU read side lock must
1402 * be held before calling this helper function.
1404 static struct ust_app
*find_app_by_notify_sock(int sock
)
1406 struct lttng_ht_node_ulong
*node
;
1407 struct lttng_ht_iter iter
;
1409 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1411 node
= lttng_ht_iter_get_node_ulong(&iter
);
1413 DBG2("UST app find by notify sock %d not found", sock
);
1417 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1424 * Lookup for an ust app event based on event name, filter bytecode and the
1427 * Return an ust_app_event object or NULL on error.
1429 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1430 const char *name
, const struct lttng_bytecode
*filter
,
1432 const struct lttng_event_exclusion
*exclusion
)
1434 struct lttng_ht_iter iter
;
1435 struct lttng_ht_node_str
*node
;
1436 struct ust_app_event
*event
= NULL
;
1437 struct ust_app_ht_key key
;
1442 /* Setup key for event lookup. */
1444 key
.filter
= filter
;
1445 key
.loglevel_type
= loglevel_value
;
1446 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1447 key
.exclusion
= exclusion
;
1449 /* Lookup using the event name as hash and a custom match fct. */
1450 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1451 ht_match_ust_app_event
, &key
, &iter
.iter
);
1452 node
= lttng_ht_iter_get_node_str(&iter
);
1457 event
= caa_container_of(node
, struct ust_app_event
, node
);
1464 * Look-up an event notifier rule based on its token id.
1466 * Must be called with the RCU read lock held.
1467 * Return an ust_app_event_notifier_rule object or NULL on error.
1469 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(
1470 struct lttng_ht
*ht
, uint64_t token
)
1472 struct lttng_ht_iter iter
;
1473 struct lttng_ht_node_u64
*node
;
1474 struct ust_app_event_notifier_rule
*event_notifier_rule
= NULL
;
1478 lttng_ht_lookup(ht
, &token
, &iter
);
1479 node
= lttng_ht_iter_get_node_u64(&iter
);
1481 DBG2("UST app event notifier rule token not found: token = %" PRIu64
,
1486 event_notifier_rule
= caa_container_of(
1487 node
, struct ust_app_event_notifier_rule
, node
);
1489 return event_notifier_rule
;
1493 * Create the channel context on the tracer.
1495 * Called with UST app session lock held.
1498 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1499 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1503 health_code_update();
1505 pthread_mutex_lock(&app
->sock_lock
);
1506 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1507 ua_chan
->obj
, &ua_ctx
->obj
);
1508 pthread_mutex_unlock(&app
->sock_lock
);
1510 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1511 ERR("UST app create channel context failed for app (pid: %d) "
1512 "with ret %d", app
->pid
, ret
);
1515 * This is normal behavior, an application can die during the
1516 * creation process. Don't report an error so the execution can
1517 * continue normally.
1520 DBG3("UST app add context failed. Application is dead.");
1525 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1527 DBG2("UST app context handle %d created successfully for channel %s",
1528 ua_ctx
->handle
, ua_chan
->name
);
1531 health_code_update();
1536 * Set the filter on the tracer.
1538 static int set_ust_object_filter(struct ust_app
*app
,
1539 const struct lttng_bytecode
*bytecode
,
1540 struct lttng_ust_abi_object_data
*ust_object
)
1543 struct lttng_ust_abi_filter_bytecode
*ust_bytecode
= NULL
;
1545 health_code_update();
1547 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1548 if (!ust_bytecode
) {
1549 ret
= -LTTNG_ERR_NOMEM
;
1552 pthread_mutex_lock(&app
->sock_lock
);
1553 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1555 pthread_mutex_unlock(&app
->sock_lock
);
1557 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1558 ERR("UST app set object filter failed: object = %p of app pid = %d, ret = %d",
1559 ust_object
, app
->pid
, ret
);
1562 * This is normal behavior, an application can die during the
1563 * creation process. Don't report an error so the execution can
1564 * continue normally.
1567 DBG3("Failed to set UST app object filter. Application is dead.");
1572 DBG2("UST filter successfully set: object = %p", ust_object
);
1575 health_code_update();
1581 * Set a capture bytecode for the passed object.
1582 * The sequence number enforces the ordering at runtime and on reception of
1583 * the captured payloads.
1585 static int set_ust_capture(struct ust_app
*app
,
1586 const struct lttng_bytecode
*bytecode
,
1587 unsigned int capture_seqnum
,
1588 struct lttng_ust_object_data
*ust_object
)
1591 struct lttng_ust_abi_capture_bytecode
*ust_bytecode
= NULL
;
1593 health_code_update();
1595 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1596 if (!ust_bytecode
) {
1597 ret
= -LTTNG_ERR_NOMEM
;
1602 * Set the sequence number to ensure the capture of fields is ordered.
1604 ust_bytecode
->seqnum
= capture_seqnum
;
1606 pthread_mutex_lock(&app
->sock_lock
);
1607 ret
= ustctl_set_capture(app
->sock
, ust_bytecode
,
1609 pthread_mutex_unlock(&app
->sock_lock
);
1611 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1612 ERR("UST app set object capture failed: object = %p of app pid = %d, ret = %d",
1613 ust_object
, app
->pid
, ret
);
1616 * This is normal behavior, an application can die during the
1617 * creation process. Don't report an error so the execution can
1618 * continue normally.
1621 DBG3("Failed to set UST app object capture. Application is dead.");
1627 DBG2("UST capture successfully set: object = %p", ust_object
);
1630 health_code_update();
1636 struct lttng_ust_abi_event_exclusion
*create_ust_exclusion_from_exclusion(
1637 const struct lttng_event_exclusion
*exclusion
)
1639 struct lttng_ust_abi_event_exclusion
*ust_exclusion
= NULL
;
1640 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_abi_event_exclusion
) +
1641 LTTNG_UST_ABI_SYM_NAME_LEN
* exclusion
->count
;
1643 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1644 if (!ust_exclusion
) {
1649 assert(sizeof(struct lttng_event_exclusion
) ==
1650 sizeof(struct lttng_ust_abi_event_exclusion
));
1651 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1653 return ust_exclusion
;
1657 * Set event exclusions on the tracer.
1659 static int set_ust_object_exclusions(struct ust_app
*app
,
1660 const struct lttng_event_exclusion
*exclusions
,
1661 struct lttng_ust_abi_object_data
*ust_object
)
1664 struct lttng_ust_abi_event_exclusion
*ust_exclusions
= NULL
;
1666 assert(exclusions
&& exclusions
->count
> 0);
1668 health_code_update();
1670 ust_exclusions
= create_ust_exclusion_from_exclusion(
1672 if (!ust_exclusions
) {
1673 ret
= -LTTNG_ERR_NOMEM
;
1676 pthread_mutex_lock(&app
->sock_lock
);
1677 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1678 pthread_mutex_unlock(&app
->sock_lock
);
1680 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1681 ERR("Failed to set UST app exclusions for object %p of app (pid: %d) "
1682 "with ret %d", ust_object
, app
->pid
, ret
);
1685 * This is normal behavior, an application can die during the
1686 * creation process. Don't report an error so the execution can
1687 * continue normally.
1690 DBG3("Failed to set UST app object exclusions. Application is dead.");
1695 DBG2("UST exclusions set successfully for object %p", ust_object
);
1698 health_code_update();
1699 free(ust_exclusions
);
1704 * Disable the specified event on to UST tracer for the UST session.
1706 static int disable_ust_object(struct ust_app
*app
,
1707 struct lttng_ust_abi_object_data
*object
)
1711 health_code_update();
1713 pthread_mutex_lock(&app
->sock_lock
);
1714 ret
= ustctl_disable(app
->sock
, object
);
1715 pthread_mutex_unlock(&app
->sock_lock
);
1717 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1718 ERR("Failed to disable UST app object %p app (pid: %d) with ret %d",
1719 object
, app
->pid
, ret
);
1722 * This is normal behavior, an application can die during the
1723 * creation process. Don't report an error so the execution can
1724 * continue normally.
1727 DBG3("Failed to disable UST app object. Application is dead.");
1732 DBG2("UST app object %p disabled successfully for app (pid: %d)",
1736 health_code_update();
1741 * Disable the specified channel on to UST tracer for the UST session.
1743 static int disable_ust_channel(struct ust_app
*app
,
1744 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1748 health_code_update();
1750 pthread_mutex_lock(&app
->sock_lock
);
1751 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1752 pthread_mutex_unlock(&app
->sock_lock
);
1754 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1755 ERR("UST app channel %s disable failed for app (pid: %d) "
1756 "and session handle %d with ret %d",
1757 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1760 * This is normal behavior, an application can die during the
1761 * creation process. Don't report an error so the execution can
1762 * continue normally.
1765 DBG3("UST app disable channel failed. Application is dead.");
1770 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1771 ua_chan
->name
, app
->pid
);
1774 health_code_update();
1779 * Enable the specified channel on to UST tracer for the UST session.
1781 static int enable_ust_channel(struct ust_app
*app
,
1782 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1786 health_code_update();
1788 pthread_mutex_lock(&app
->sock_lock
);
1789 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1790 pthread_mutex_unlock(&app
->sock_lock
);
1792 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1793 ERR("UST app channel %s enable failed for app (pid: %d) "
1794 "and session handle %d with ret %d",
1795 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1798 * This is normal behavior, an application can die during the
1799 * creation process. Don't report an error so the execution can
1800 * continue normally.
1803 DBG3("UST app enable channel failed. Application is dead.");
1808 ua_chan
->enabled
= 1;
1810 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1811 ua_chan
->name
, app
->pid
);
1814 health_code_update();
1819 * Enable the specified event on to UST tracer for the UST session.
1821 static int enable_ust_object(
1822 struct ust_app
*app
, struct lttng_ust_abi_object_data
*ust_object
)
1826 health_code_update();
1828 pthread_mutex_lock(&app
->sock_lock
);
1829 ret
= ustctl_enable(app
->sock
, ust_object
);
1830 pthread_mutex_unlock(&app
->sock_lock
);
1832 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1833 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
1834 ust_object
, app
->pid
, ret
);
1837 * This is normal behavior, an application can die during the
1838 * creation process. Don't report an error so the execution can
1839 * continue normally.
1842 DBG3("Failed to enable UST app object. Application is dead.");
1847 DBG2("UST app object %p enabled successfully for app (pid: %d)",
1848 ust_object
, app
->pid
);
1851 health_code_update();
1856 * Send channel and stream buffer to application.
1858 * Return 0 on success. On error, a negative value is returned.
1860 static int send_channel_pid_to_ust(struct ust_app
*app
,
1861 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1864 struct ust_app_stream
*stream
, *stmp
;
1870 health_code_update();
1872 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1875 /* Send channel to the application. */
1876 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1877 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1878 ret
= -ENOTCONN
; /* Caused by app exiting. */
1880 } else if (ret
< 0) {
1884 health_code_update();
1886 /* Send all streams to application. */
1887 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1888 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1889 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1890 ret
= -ENOTCONN
; /* Caused by app exiting. */
1892 } else if (ret
< 0) {
1895 /* We don't need the stream anymore once sent to the tracer. */
1896 cds_list_del(&stream
->list
);
1897 delete_ust_app_stream(-1, stream
, app
);
1899 /* Flag the channel that it is sent to the application. */
1900 ua_chan
->is_sent
= 1;
1903 health_code_update();
1908 * Create the specified event onto the UST tracer for a UST session.
1910 * Should be called with session mutex held.
1913 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1914 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1918 health_code_update();
1920 /* Create UST event on tracer */
1921 pthread_mutex_lock(&app
->sock_lock
);
1922 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1924 pthread_mutex_unlock(&app
->sock_lock
);
1926 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1928 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1929 ua_event
->attr
.name
, app
->pid
, ret
);
1932 * This is normal behavior, an application can die during the
1933 * creation process. Don't report an error so the execution can
1934 * continue normally.
1937 DBG3("UST app create event failed. Application is dead.");
1942 ua_event
->handle
= ua_event
->obj
->handle
;
1944 DBG2("UST app event %s created successfully for pid:%d object: %p",
1945 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
1947 health_code_update();
1949 /* Set filter if one is present. */
1950 if (ua_event
->filter
) {
1951 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
1957 /* Set exclusions for the event */
1958 if (ua_event
->exclusion
) {
1959 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
1965 /* If event not enabled, disable it on the tracer */
1966 if (ua_event
->enabled
) {
1968 * We now need to explicitly enable the event, since it
1969 * is now disabled at creation.
1971 ret
= enable_ust_object(app
, ua_event
->obj
);
1974 * If we hit an EPERM, something is wrong with our enable call. If
1975 * we get an EEXIST, there is a problem on the tracer side since we
1979 case -LTTNG_UST_ERR_PERM
:
1980 /* Code flow problem */
1982 case -LTTNG_UST_ERR_EXIST
:
1983 /* It's OK for our use case. */
1994 health_code_update();
1998 static int init_ust_event_notifier_from_event_rule(
1999 const struct lttng_event_rule
*rule
,
2000 struct lttng_ust_abi_event_notifier
*event_notifier
)
2002 enum lttng_event_rule_status status
;
2003 enum lttng_loglevel_type loglevel_type
;
2004 enum lttng_ust_abi_loglevel_type ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2005 int loglevel
= -1, ret
= 0;
2006 const char *pattern
;
2008 /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported. */
2009 assert(lttng_event_rule_get_type(rule
) ==
2010 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
2012 memset(event_notifier
, 0, sizeof(*event_notifier
));
2014 if (lttng_event_rule_targets_agent_domain(rule
)) {
2016 * Special event for agents
2017 * The actual meat of the event is in the filter that will be
2018 * attached later on.
2019 * Set the default values for the agent event.
2021 pattern
= event_get_default_agent_ust_name(
2022 lttng_event_rule_get_domain_type(rule
));
2024 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2026 status
= lttng_event_rule_tracepoint_get_pattern(
2028 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2029 /* At this point, this is a fatal error. */
2033 status
= lttng_event_rule_tracepoint_get_log_level_type(
2034 rule
, &loglevel_type
);
2035 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2036 /* At this point, this is a fatal error. */
2040 switch (loglevel_type
) {
2041 case LTTNG_EVENT_LOGLEVEL_ALL
:
2042 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_ALL
;
2044 case LTTNG_EVENT_LOGLEVEL_RANGE
:
2045 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_RANGE
;
2047 case LTTNG_EVENT_LOGLEVEL_SINGLE
:
2048 ust_loglevel_type
= LTTNG_UST_ABI_LOGLEVEL_SINGLE
;
2051 /* Unknown log level specification type. */
2055 if (loglevel_type
!= LTTNG_EVENT_LOGLEVEL_ALL
) {
2056 status
= lttng_event_rule_tracepoint_get_log_level(
2058 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
2062 event_notifier
->event
.instrumentation
= LTTNG_UST_ABI_TRACEPOINT
;
2063 ret
= lttng_strncpy(event_notifier
->event
.name
, pattern
,
2064 LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
2066 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
2071 event_notifier
->event
.loglevel_type
= ust_loglevel_type
;
2072 event_notifier
->event
.loglevel
= loglevel
;
2078 * Create the specified event notifier against the user space tracer of a
2079 * given application.
2081 static int create_ust_event_notifier(struct ust_app
*app
,
2082 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2085 enum lttng_condition_status condition_status
;
2086 const struct lttng_condition
*condition
= NULL
;
2087 struct lttng_ust_abi_event_notifier event_notifier
;
2088 const struct lttng_event_rule
*event_rule
= NULL
;
2089 unsigned int capture_bytecode_count
= 0, i
;
2090 enum lttng_condition_status cond_status
;
2092 health_code_update();
2093 assert(app
->event_notifier_group
.object
);
2095 condition
= lttng_trigger_get_const_condition(
2096 ua_event_notifier_rule
->trigger
);
2098 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
);
2100 condition_status
= lttng_condition_event_rule_get_rule(condition
, &event_rule
);
2101 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2103 assert(lttng_event_rule_get_type(event_rule
) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
2105 init_ust_event_notifier_from_event_rule(event_rule
, &event_notifier
);
2106 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2108 /* Create UST event notifier against the tracer. */
2109 pthread_mutex_lock(&app
->sock_lock
);
2110 ret
= ustctl_create_event_notifier(app
->sock
, &event_notifier
,
2111 app
->event_notifier_group
.object
,
2112 &ua_event_notifier_rule
->obj
);
2113 pthread_mutex_unlock(&app
->sock_lock
);
2115 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2116 ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d",
2117 event_notifier
.event
.name
, app
->name
,
2121 * This is normal behavior, an application can die
2122 * during the creation process. Don't report an error so
2123 * the execution can continue normally.
2126 DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)",
2127 app
->name
, app
->ppid
);
2133 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2135 DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p",
2136 event_notifier
.event
.name
, app
->name
, app
->ppid
,
2137 ua_event_notifier_rule
->obj
);
2139 health_code_update();
2141 /* Set filter if one is present. */
2142 if (ua_event_notifier_rule
->filter
) {
2143 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2144 ua_event_notifier_rule
->obj
);
2150 /* Set exclusions for the event. */
2151 if (ua_event_notifier_rule
->exclusion
) {
2152 ret
= set_ust_object_exclusions(app
,
2153 ua_event_notifier_rule
->exclusion
,
2154 ua_event_notifier_rule
->obj
);
2160 /* Set the capture bytecodes. */
2161 cond_status
= lttng_condition_event_rule_get_capture_descriptor_count(
2162 condition
, &capture_bytecode_count
);
2163 assert(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2165 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2166 const struct lttng_bytecode
*capture_bytecode
=
2167 lttng_condition_event_rule_get_capture_bytecode_at_index(
2170 ret
= set_ust_capture(app
, capture_bytecode
, i
,
2171 ua_event_notifier_rule
->obj
);
2178 * We now need to explicitly enable the event, since it
2179 * is disabled at creation.
2181 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2184 * If we hit an EPERM, something is wrong with our enable call.
2185 * If we get an EEXIST, there is a problem on the tracer side
2186 * since we just created it.
2189 case -LTTNG_UST_ERR_PERM
:
2190 /* Code flow problem. */
2192 case -LTTNG_UST_ERR_EXIST
:
2193 /* It's OK for our use case. */
2203 ua_event_notifier_rule
->enabled
= true;
2206 health_code_update();
2211 * Copy data between an UST app event and a LTT event.
2213 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2214 struct ltt_ust_event
*uevent
)
2216 size_t exclusion_alloc_size
;
2218 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2219 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2221 ua_event
->enabled
= uevent
->enabled
;
2223 /* Copy event attributes */
2224 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2226 /* Copy filter bytecode */
2227 if (uevent
->filter
) {
2228 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2229 /* Filter might be NULL here in case of ENONEM. */
2232 /* Copy exclusion data */
2233 if (uevent
->exclusion
) {
2234 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2235 LTTNG_UST_ABI_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2236 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
2237 if (ua_event
->exclusion
== NULL
) {
2240 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2241 exclusion_alloc_size
);
2247 * Copy data between an UST app channel and a LTT channel.
2249 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2250 struct ltt_ust_channel
*uchan
)
2252 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2254 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2255 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2257 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2258 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2260 /* Copy event attributes since the layout is different. */
2261 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2262 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2263 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2264 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2265 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2266 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2267 ua_chan
->attr
.output
= uchan
->attr
.output
;
2268 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2271 * Note that the attribute channel type is not set since the channel on the
2272 * tracing registry side does not have this information.
2275 ua_chan
->enabled
= uchan
->enabled
;
2276 ua_chan
->tracing_channel_id
= uchan
->id
;
2278 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2282 * Copy data between a UST app session and a regular LTT session.
2284 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2285 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2287 struct tm
*timeinfo
;
2290 char tmp_shm_path
[PATH_MAX
];
2292 timeinfo
= localtime(&app
->registration_time
);
2293 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2295 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2297 ua_sess
->tracing_id
= usess
->id
;
2298 ua_sess
->id
= get_next_session_id();
2299 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2300 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2301 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2302 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2303 ua_sess
->buffer_type
= usess
->buffer_type
;
2304 ua_sess
->bits_per_long
= app
->bits_per_long
;
2306 /* There is only one consumer object per session possible. */
2307 consumer_output_get(usess
->consumer
);
2308 ua_sess
->consumer
= usess
->consumer
;
2310 ua_sess
->output_traces
= usess
->output_traces
;
2311 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2312 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2313 &usess
->metadata_attr
);
2315 switch (ua_sess
->buffer_type
) {
2316 case LTTNG_BUFFER_PER_PID
:
2317 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2318 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2321 case LTTNG_BUFFER_PER_UID
:
2322 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2323 DEFAULT_UST_TRACE_UID_PATH
,
2324 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2325 app
->bits_per_long
);
2332 PERROR("asprintf UST shadow copy session");
2337 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2338 sizeof(ua_sess
->root_shm_path
));
2339 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2340 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2341 sizeof(ua_sess
->shm_path
));
2342 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2343 if (ua_sess
->shm_path
[0]) {
2344 switch (ua_sess
->buffer_type
) {
2345 case LTTNG_BUFFER_PER_PID
:
2346 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2347 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2348 app
->name
, app
->pid
, datetime
);
2350 case LTTNG_BUFFER_PER_UID
:
2351 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2352 "/" DEFAULT_UST_TRACE_UID_PATH
,
2353 app
->uid
, app
->bits_per_long
);
2360 PERROR("sprintf UST shadow copy session");
2364 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2365 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2366 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2371 consumer_output_put(ua_sess
->consumer
);
2375 * Lookup sesison wrapper.
2378 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2379 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2381 /* Get right UST app session from app */
2382 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2386 * Return ust app session from the app session hashtable using the UST session
2389 static struct ust_app_session
*lookup_session_by_app(
2390 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2392 struct lttng_ht_iter iter
;
2393 struct lttng_ht_node_u64
*node
;
2395 __lookup_session_by_app(usess
, app
, &iter
);
2396 node
= lttng_ht_iter_get_node_u64(&iter
);
2401 return caa_container_of(node
, struct ust_app_session
, node
);
2408 * Setup buffer registry per PID for the given session and application. If none
2409 * is found, a new one is created, added to the global registry and
2410 * initialized. If regp is valid, it's set with the newly created object.
2412 * Return 0 on success or else a negative value.
2414 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2415 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2418 struct buffer_reg_pid
*reg_pid
;
2425 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2428 * This is the create channel path meaning that if there is NO
2429 * registry available, we have to create one for this session.
2431 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2432 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2440 /* Initialize registry. */
2441 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2442 app
->bits_per_long
, app
->uint8_t_alignment
,
2443 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2444 app
->uint64_t_alignment
, app
->long_alignment
,
2445 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
2446 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
2447 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
2448 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
2449 ua_sess
->tracing_id
,
2453 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2454 * destroy the buffer registry, because it is always expected
2455 * that if the buffer registry can be found, its ust registry is
2458 buffer_reg_pid_destroy(reg_pid
);
2462 buffer_reg_pid_add(reg_pid
);
2464 DBG3("UST app buffer registry per PID created successfully");
2476 * Setup buffer registry per UID for the given session and application. If none
2477 * is found, a new one is created, added to the global registry and
2478 * initialized. If regp is valid, it's set with the newly created object.
2480 * Return 0 on success or else a negative value.
2482 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2483 struct ust_app_session
*ua_sess
,
2484 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2487 struct buffer_reg_uid
*reg_uid
;
2494 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2497 * This is the create channel path meaning that if there is NO
2498 * registry available, we have to create one for this session.
2500 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2501 LTTNG_DOMAIN_UST
, ®_uid
,
2502 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2510 /* Initialize registry. */
2511 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2512 app
->bits_per_long
, app
->uint8_t_alignment
,
2513 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2514 app
->uint64_t_alignment
, app
->long_alignment
,
2515 app
->byte_order
, app
->version
.major
,
2516 app
->version
.minor
, reg_uid
->root_shm_path
,
2517 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
2518 ua_sess
->tracing_id
, app
->uid
);
2521 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2522 * destroy the buffer registry, because it is always expected
2523 * that if the buffer registry can be found, its ust registry is
2526 buffer_reg_uid_destroy(reg_uid
, NULL
);
2529 /* Add node to teardown list of the session. */
2530 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2532 buffer_reg_uid_add(reg_uid
);
2534 DBG3("UST app buffer registry per UID created successfully");
2545 * Create a session on the tracer side for the given app.
2547 * On success, ua_sess_ptr is populated with the session pointer or else left
2548 * untouched. If the session was created, is_created is set to 1. On error,
2549 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2552 * Returns 0 on success or else a negative code which is either -ENOMEM or
2553 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2555 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
2556 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2559 int ret
, created
= 0;
2560 struct ust_app_session
*ua_sess
;
2564 assert(ua_sess_ptr
);
2566 health_code_update();
2568 ua_sess
= lookup_session_by_app(usess
, app
);
2569 if (ua_sess
== NULL
) {
2570 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2571 app
->pid
, usess
->id
);
2572 ua_sess
= alloc_ust_app_session();
2573 if (ua_sess
== NULL
) {
2574 /* Only malloc can failed so something is really wrong */
2578 shadow_copy_session(ua_sess
, usess
, app
);
2582 switch (usess
->buffer_type
) {
2583 case LTTNG_BUFFER_PER_PID
:
2584 /* Init local registry. */
2585 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2587 delete_ust_app_session(-1, ua_sess
, app
);
2591 case LTTNG_BUFFER_PER_UID
:
2592 /* Look for a global registry. If none exists, create one. */
2593 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2595 delete_ust_app_session(-1, ua_sess
, app
);
2605 health_code_update();
2607 if (ua_sess
->handle
== -1) {
2608 pthread_mutex_lock(&app
->sock_lock
);
2609 ret
= ustctl_create_session(app
->sock
);
2610 pthread_mutex_unlock(&app
->sock_lock
);
2612 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2613 ERR("Creating session for app pid %d with ret %d",
2616 DBG("UST app creating session failed. Application is dead");
2618 * This is normal behavior, an application can die during the
2619 * creation process. Don't report an error so the execution can
2620 * continue normally. This will get flagged ENOTCONN and the
2621 * caller will handle it.
2625 delete_ust_app_session(-1, ua_sess
, app
);
2626 if (ret
!= -ENOMEM
) {
2628 * Tracer is probably gone or got an internal error so let's
2629 * behave like it will soon unregister or not usable.
2636 ua_sess
->handle
= ret
;
2638 /* Add ust app session to app's HT */
2639 lttng_ht_node_init_u64(&ua_sess
->node
,
2640 ua_sess
->tracing_id
);
2641 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2642 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2643 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2644 &ua_sess
->ust_objd_node
);
2646 DBG2("UST app session created successfully with handle %d", ret
);
2649 *ua_sess_ptr
= ua_sess
;
2651 *is_created
= created
;
2654 /* Everything went well. */
2658 health_code_update();
2663 * Match function for a hash table lookup of ust_app_ctx.
2665 * It matches an ust app context based on the context type and, in the case
2666 * of perf counters, their name.
2668 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2670 struct ust_app_ctx
*ctx
;
2671 const struct lttng_ust_context_attr
*key
;
2676 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2680 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2685 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER
:
2686 if (strncmp(key
->u
.perf_counter
.name
,
2687 ctx
->ctx
.u
.perf_counter
.name
,
2688 sizeof(key
->u
.perf_counter
.name
))) {
2692 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
2693 if (strcmp(key
->u
.app_ctx
.provider_name
,
2694 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2695 strcmp(key
->u
.app_ctx
.ctx_name
,
2696 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2712 * Lookup for an ust app context from an lttng_ust_context.
2714 * Must be called while holding RCU read side lock.
2715 * Return an ust_app_ctx object or NULL on error.
2718 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2719 struct lttng_ust_context_attr
*uctx
)
2721 struct lttng_ht_iter iter
;
2722 struct lttng_ht_node_ulong
*node
;
2723 struct ust_app_ctx
*app_ctx
= NULL
;
2728 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2729 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2730 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2731 node
= lttng_ht_iter_get_node_ulong(&iter
);
2736 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2743 * Create a context for the channel on the tracer.
2745 * Called with UST app session lock held and a RCU read side lock.
2748 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
2749 struct lttng_ust_context_attr
*uctx
,
2750 struct ust_app
*app
)
2753 struct ust_app_ctx
*ua_ctx
;
2755 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2757 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2763 ua_ctx
= alloc_ust_app_ctx(uctx
);
2764 if (ua_ctx
== NULL
) {
2770 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2771 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2772 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2774 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2784 * Enable on the tracer side a ust app event for the session and channel.
2786 * Called with UST app session lock held.
2789 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2790 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2794 ret
= enable_ust_object(app
, ua_event
->obj
);
2799 ua_event
->enabled
= 1;
2806 * Disable on the tracer side a ust app event for the session and channel.
2808 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2809 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2813 ret
= disable_ust_object(app
, ua_event
->obj
);
2818 ua_event
->enabled
= 0;
2825 * Lookup ust app channel for session and disable it on the tracer side.
2828 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2829 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2833 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2838 ua_chan
->enabled
= 0;
2845 * Lookup ust app channel for session and enable it on the tracer side. This
2846 * MUST be called with a RCU read side lock acquired.
2848 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2849 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2852 struct lttng_ht_iter iter
;
2853 struct lttng_ht_node_str
*ua_chan_node
;
2854 struct ust_app_channel
*ua_chan
;
2856 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2857 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2858 if (ua_chan_node
== NULL
) {
2859 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2860 uchan
->name
, ua_sess
->tracing_id
);
2864 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2866 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2876 * Ask the consumer to create a channel and get it if successful.
2878 * Called with UST app session lock held.
2880 * Return 0 on success or else a negative value.
2882 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2883 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2884 int bitness
, struct ust_registry_session
*registry
,
2885 uint64_t trace_archive_id
)
2888 unsigned int nb_fd
= 0;
2889 struct consumer_socket
*socket
;
2897 health_code_update();
2899 /* Get the right consumer socket for the application. */
2900 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2906 health_code_update();
2908 /* Need one fd for the channel. */
2909 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2911 ERR("Exhausted number of available FD upon create channel");
2916 * Ask consumer to create channel. The consumer will return the number of
2917 * stream we have to expect.
2919 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2920 registry
, usess
->current_trace_chunk
);
2926 * Compute the number of fd needed before receiving them. It must be 2 per
2927 * stream (2 being the default value here).
2929 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2931 /* Reserve the amount of file descriptor we need. */
2932 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2934 ERR("Exhausted number of available FD upon create channel");
2935 goto error_fd_get_stream
;
2938 health_code_update();
2941 * Now get the channel from the consumer. This call wil populate the stream
2942 * list of that channel and set the ust objects.
2944 if (usess
->consumer
->enabled
) {
2945 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2955 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2956 error_fd_get_stream
:
2958 * Initiate a destroy channel on the consumer since we had an error
2959 * handling it on our side. The return value is of no importance since we
2960 * already have a ret value set by the previous error that we need to
2963 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2965 lttng_fd_put(LTTNG_FD_APPS
, 1);
2967 health_code_update();
2973 * Duplicate the ust data object of the ust app stream and save it in the
2974 * buffer registry stream.
2976 * Return 0 on success or else a negative value.
2978 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2979 struct ust_app_stream
*stream
)
2986 /* Reserve the amount of file descriptor we need. */
2987 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2989 ERR("Exhausted number of available FD upon duplicate stream");
2993 /* Duplicate object for stream once the original is in the registry. */
2994 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2995 reg_stream
->obj
.ust
);
2997 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2998 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2999 lttng_fd_put(LTTNG_FD_APPS
, 2);
3002 stream
->handle
= stream
->obj
->handle
;
3009 * Duplicate the ust data object of the ust app. channel and save it in the
3010 * buffer registry channel.
3012 * Return 0 on success or else a negative value.
3014 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
3015 struct ust_app_channel
*ua_chan
)
3022 /* Need two fds for the channel. */
3023 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3025 ERR("Exhausted number of available FD upon duplicate channel");
3029 /* Duplicate object for stream once the original is in the registry. */
3030 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
3032 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3033 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
3036 ua_chan
->handle
= ua_chan
->obj
->handle
;
3041 lttng_fd_put(LTTNG_FD_APPS
, 1);
3047 * For a given channel buffer registry, setup all streams of the given ust
3048 * application channel.
3050 * Return 0 on success or else a negative value.
3052 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
3053 struct ust_app_channel
*ua_chan
,
3054 struct ust_app
*app
)
3057 struct ust_app_stream
*stream
, *stmp
;
3062 DBG2("UST app setup buffer registry stream");
3064 /* Send all streams to application. */
3065 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3066 struct buffer_reg_stream
*reg_stream
;
3068 ret
= buffer_reg_stream_create(®_stream
);
3074 * Keep original pointer and nullify it in the stream so the delete
3075 * stream call does not release the object.
3077 reg_stream
->obj
.ust
= stream
->obj
;
3079 buffer_reg_stream_add(reg_stream
, reg_chan
);
3081 /* We don't need the streams anymore. */
3082 cds_list_del(&stream
->list
);
3083 delete_ust_app_stream(-1, stream
, app
);
3091 * Create a buffer registry channel for the given session registry and
3092 * application channel object. If regp pointer is valid, it's set with the
3093 * created object. Important, the created object is NOT added to the session
3094 * registry hash table.
3096 * Return 0 on success else a negative value.
3098 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3099 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
3102 struct buffer_reg_channel
*reg_chan
= NULL
;
3107 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3109 /* Create buffer registry channel. */
3110 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
3115 reg_chan
->consumer_key
= ua_chan
->key
;
3116 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3117 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3119 /* Create and add a channel registry to session. */
3120 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
3121 ua_chan
->tracing_channel_id
);
3125 buffer_reg_channel_add(reg_sess
, reg_chan
);
3134 /* Safe because the registry channel object was not added to any HT. */
3135 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3141 * Setup buffer registry channel for the given session registry and application
3142 * channel object. If regp pointer is valid, it's set with the created object.
3144 * Return 0 on success else a negative value.
3146 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3147 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
3148 struct ust_app
*app
)
3155 assert(ua_chan
->obj
);
3157 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
3159 /* Setup all streams for the registry. */
3160 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
3165 reg_chan
->obj
.ust
= ua_chan
->obj
;
3166 ua_chan
->obj
= NULL
;
3171 buffer_reg_channel_remove(reg_sess
, reg_chan
);
3172 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3177 * Send buffer registry channel to the application.
3179 * Return 0 on success else a negative value.
3181 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
3182 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
3183 struct ust_app_channel
*ua_chan
)
3186 struct buffer_reg_stream
*reg_stream
;
3193 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
3195 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
3200 /* Send channel to the application. */
3201 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
3202 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3203 ret
= -ENOTCONN
; /* Caused by app exiting. */
3205 } else if (ret
< 0) {
3209 health_code_update();
3211 /* Send all streams to application. */
3212 pthread_mutex_lock(®_chan
->stream_list_lock
);
3213 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
3214 struct ust_app_stream stream
;
3216 ret
= duplicate_stream_object(reg_stream
, &stream
);
3218 goto error_stream_unlock
;
3221 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
3223 (void) release_ust_app_stream(-1, &stream
, app
);
3224 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
3225 ret
= -ENOTCONN
; /* Caused by app exiting. */
3227 goto error_stream_unlock
;
3231 * The return value is not important here. This function will output an
3234 (void) release_ust_app_stream(-1, &stream
, app
);
3236 ua_chan
->is_sent
= 1;
3238 error_stream_unlock
:
3239 pthread_mutex_unlock(®_chan
->stream_list_lock
);
3245 * Create and send to the application the created buffers with per UID buffers.
3247 * This MUST be called with a RCU read side lock acquired.
3248 * The session list lock and the session's lock must be acquired.
3250 * Return 0 on success else a negative value.
3252 static int create_channel_per_uid(struct ust_app
*app
,
3253 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3254 struct ust_app_channel
*ua_chan
)
3257 struct buffer_reg_uid
*reg_uid
;
3258 struct buffer_reg_channel
*reg_chan
;
3259 struct ltt_session
*session
= NULL
;
3260 enum lttng_error_code notification_ret
;
3261 struct ust_registry_channel
*chan_reg
;
3268 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
3270 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3272 * The session creation handles the creation of this global registry
3273 * object. If none can be find, there is a code flow problem or a
3278 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
3284 /* Create the buffer registry channel object. */
3285 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
3287 ERR("Error creating the UST channel \"%s\" registry instance",
3292 session
= session_find_by_id(ua_sess
->tracing_id
);
3294 assert(pthread_mutex_trylock(&session
->lock
));
3295 assert(session_trylock_list());
3298 * Create the buffers on the consumer side. This call populates the
3299 * ust app channel object with all streams and data object.
3301 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3302 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
3303 session
->most_recent_chunk_id
.value
);
3305 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3309 * Let's remove the previously created buffer registry channel so
3310 * it's not visible anymore in the session registry.
3312 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
3313 ua_chan
->tracing_channel_id
, false);
3314 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
3315 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
3320 * Setup the streams and add it to the session registry.
3322 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
3323 ua_chan
, reg_chan
, app
);
3325 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
3329 /* Notify the notification subsystem of the channel's creation. */
3330 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
3331 chan_reg
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
3332 ua_chan
->tracing_channel_id
);
3334 chan_reg
->consumer_key
= ua_chan
->key
;
3336 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
3338 notification_ret
= notification_thread_command_add_channel(
3339 notification_thread_handle
, session
->name
,
3340 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3341 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3343 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3344 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3345 if (notification_ret
!= LTTNG_OK
) {
3346 ret
= - (int) notification_ret
;
3347 ERR("Failed to add channel to notification thread");
3352 /* Send buffers to the application. */
3353 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
3355 if (ret
!= -ENOTCONN
) {
3356 ERR("Error sending channel to application");
3363 session_put(session
);
3369 * Create and send to the application the created buffers with per PID buffers.
3371 * Called with UST app session lock held.
3372 * The session list lock and the session's lock must be acquired.
3374 * Return 0 on success else a negative value.
3376 static int create_channel_per_pid(struct ust_app
*app
,
3377 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3378 struct ust_app_channel
*ua_chan
)
3381 struct ust_registry_session
*registry
;
3382 enum lttng_error_code cmd_ret
;
3383 struct ltt_session
*session
= NULL
;
3384 uint64_t chan_reg_key
;
3385 struct ust_registry_channel
*chan_reg
;
3392 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
3396 registry
= get_session_registry(ua_sess
);
3397 /* The UST app session lock is held, registry shall not be null. */
3400 /* Create and add a new channel registry to session. */
3401 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
3403 ERR("Error creating the UST channel \"%s\" registry instance",
3408 session
= session_find_by_id(ua_sess
->tracing_id
);
3411 assert(pthread_mutex_trylock(&session
->lock
));
3412 assert(session_trylock_list());
3414 /* Create and get channel on the consumer side. */
3415 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
3416 app
->bits_per_long
, registry
,
3417 session
->most_recent_chunk_id
.value
);
3419 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3421 goto error_remove_from_registry
;
3424 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
3426 if (ret
!= -ENOTCONN
) {
3427 ERR("Error sending channel to application");
3429 goto error_remove_from_registry
;
3432 chan_reg_key
= ua_chan
->key
;
3433 pthread_mutex_lock(®istry
->lock
);
3434 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
3436 chan_reg
->consumer_key
= ua_chan
->key
;
3437 pthread_mutex_unlock(®istry
->lock
);
3439 cmd_ret
= notification_thread_command_add_channel(
3440 notification_thread_handle
, session
->name
,
3441 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3442 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3444 ua_chan
->key
, LTTNG_DOMAIN_UST
,
3445 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
3446 if (cmd_ret
!= LTTNG_OK
) {
3447 ret
= - (int) cmd_ret
;
3448 ERR("Failed to add channel to notification thread");
3449 goto error_remove_from_registry
;
3452 error_remove_from_registry
:
3454 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
3459 session_put(session
);
3465 * From an already allocated ust app channel, create the channel buffers if
3466 * needed and send them to the application. This MUST be called with a RCU read
3467 * side lock acquired.
3469 * Called with UST app session lock held.
3471 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3472 * the application exited concurrently.
3474 static int ust_app_channel_send(struct ust_app
*app
,
3475 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
3476 struct ust_app_channel
*ua_chan
)
3482 assert(usess
->active
);
3486 /* Handle buffer type before sending the channel to the application. */
3487 switch (usess
->buffer_type
) {
3488 case LTTNG_BUFFER_PER_UID
:
3490 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3496 case LTTNG_BUFFER_PER_PID
:
3498 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3510 /* Initialize ust objd object using the received handle and add it. */
3511 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3512 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3514 /* If channel is not enabled, disable it on the tracer */
3515 if (!ua_chan
->enabled
) {
3516 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3527 * Create UST app channel and return it through ua_chanp if not NULL.
3529 * Called with UST app session lock and RCU read-side lock held.
3531 * Return 0 on success or else a negative value.
3533 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
3534 struct ltt_ust_channel
*uchan
,
3535 enum lttng_ust_abi_chan_type type
, struct ltt_ust_session
*usess
,
3536 struct ust_app_channel
**ua_chanp
)
3539 struct lttng_ht_iter iter
;
3540 struct lttng_ht_node_str
*ua_chan_node
;
3541 struct ust_app_channel
*ua_chan
;
3543 /* Lookup channel in the ust app session */
3544 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3545 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3546 if (ua_chan_node
!= NULL
) {
3547 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3551 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3552 if (ua_chan
== NULL
) {
3553 /* Only malloc can fail here */
3557 shadow_copy_channel(ua_chan
, uchan
);
3559 /* Set channel type. */
3560 ua_chan
->attr
.type
= type
;
3562 /* Only add the channel if successful on the tracer side. */
3563 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3566 *ua_chanp
= ua_chan
;
3569 /* Everything went well. */
3577 * Create UST app event and create it on the tracer side.
3579 * Must be called with the RCU read side lock held.
3580 * Called with ust app session mutex held.
3583 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3584 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3585 struct ust_app
*app
)
3588 struct ust_app_event
*ua_event
;
3590 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3591 if (ua_event
== NULL
) {
3592 /* Only failure mode of alloc_ust_app_event(). */
3596 shadow_copy_event(ua_event
, uevent
);
3598 /* Create it on the tracer side */
3599 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3602 * Not found previously means that it does not exist on the
3603 * tracer. If the application reports that the event existed,
3604 * it means there is a bug in the sessiond or lttng-ust
3605 * (or corruption, etc.)
3607 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3608 ERR("Tracer for application reported that an event being created already existed: "
3609 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3611 app
->pid
, app
->ppid
, app
->uid
,
3617 add_unique_ust_app_event(ua_chan
, ua_event
);
3619 DBG2("UST app create event completed: app = '%s' (ppid: %d)",
3620 app
->name
, app
->ppid
);
3626 /* Valid. Calling here is already in a read side lock */
3627 delete_ust_app_event(-1, ua_event
, app
);
3632 * Create UST app event notifier rule and create it on the tracer side.
3634 * Must be called with the RCU read side lock held.
3635 * Called with ust app session mutex held.
3638 int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
,
3639 struct ust_app
*app
)
3642 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
3644 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
3645 if (ua_event_notifier_rule
== NULL
) {
3650 /* Create it on the tracer side. */
3651 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
3654 * Not found previously means that it does not exist on the
3655 * tracer. If the application reports that the event existed,
3656 * it means there is a bug in the sessiond or lttng-ust
3657 * (or corruption, etc.)
3659 if (ret
== -LTTNG_UST_ERR_EXIST
) {
3660 ERR("Tracer for application reported that an event notifier being created already exists: "
3661 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
3662 lttng_trigger_get_tracer_token(trigger
),
3663 app
->pid
, app
->ppid
, app
->uid
,
3669 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
3670 &ua_event_notifier_rule
->node
);
3672 DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64
,
3673 app
->name
, app
->ppid
, lttng_trigger_get_tracer_token(trigger
));
3679 /* The RCU read side lock is already being held by the caller. */
3680 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
3685 * Create UST metadata and open it on the tracer side.
3687 * Called with UST app session lock held and RCU read side lock.
3689 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3690 struct ust_app
*app
, struct consumer_output
*consumer
)
3693 struct ust_app_channel
*metadata
;
3694 struct consumer_socket
*socket
;
3695 struct ust_registry_session
*registry
;
3696 struct ltt_session
*session
= NULL
;
3702 registry
= get_session_registry(ua_sess
);
3703 /* The UST app session is held registry shall not be null. */
3706 pthread_mutex_lock(®istry
->lock
);
3708 /* Metadata already exists for this registry or it was closed previously */
3709 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3714 /* Allocate UST metadata */
3715 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3717 /* malloc() failed */
3722 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3724 /* Need one fd for the channel. */
3725 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3727 ERR("Exhausted number of available FD upon create metadata");
3731 /* Get the right consumer socket for the application. */
3732 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3735 goto error_consumer
;