2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
29 #include <urcu/compiler.h>
30 #include <lttng/ust-error.h>
33 #include <common/common.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
36 #include "buffer-registry.h"
38 #include "health-sessiond.h"
40 #include "ust-consumer.h"
46 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
48 /* Next available channel key. Access under next_channel_key_lock. */
49 static uint64_t _next_channel_key
;
50 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
52 /* Next available session ID. Access under next_session_id_lock. */
53 static uint64_t _next_session_id
;
54 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
57 * Return the incremented value of next_channel_key.
59 static uint64_t get_next_channel_key(void)
63 pthread_mutex_lock(&next_channel_key_lock
);
64 ret
= ++_next_channel_key
;
65 pthread_mutex_unlock(&next_channel_key_lock
);
70 * Return the atomically incremented value of next_session_id.
72 static uint64_t get_next_session_id(void)
76 pthread_mutex_lock(&next_session_id_lock
);
77 ret
= ++_next_session_id
;
78 pthread_mutex_unlock(&next_session_id_lock
);
82 static void copy_channel_attr_to_ustctl(
83 struct ustctl_consumer_channel_attr
*attr
,
84 struct lttng_ust_channel_attr
*uattr
)
86 /* Copy event attributes since the layout is different. */
87 attr
->subbuf_size
= uattr
->subbuf_size
;
88 attr
->num_subbuf
= uattr
->num_subbuf
;
89 attr
->overwrite
= uattr
->overwrite
;
90 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
91 attr
->read_timer_interval
= uattr
->read_timer_interval
;
92 attr
->output
= uattr
->output
;
96 * Match function for the hash table lookup.
98 * It matches an ust app event based on three attributes which are the event
99 * name, the filter bytecode and the loglevel.
101 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
103 struct ust_app_event
*event
;
104 const struct ust_app_ht_key
*key
;
105 int ev_loglevel_value
;
110 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
112 ev_loglevel_value
= event
->attr
.loglevel
;
114 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
117 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
121 /* Event loglevel. */
122 if (ev_loglevel_value
!= key
->loglevel_type
) {
123 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
124 && key
->loglevel_type
== 0 &&
125 ev_loglevel_value
== -1) {
127 * Match is accepted. This is because on event creation, the
128 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
129 * -1 are accepted for this loglevel type since 0 is the one set by
130 * the API when receiving an enable event.
137 /* One of the filters is NULL, fail. */
138 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
142 if (key
->filter
&& event
->filter
) {
143 /* Both filters exists, check length followed by the bytecode. */
144 if (event
->filter
->len
!= key
->filter
->len
||
145 memcmp(event
->filter
->data
, key
->filter
->data
,
146 event
->filter
->len
) != 0) {
151 /* One of the exclusions is NULL, fail. */
152 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
156 if (key
->exclusion
&& event
->exclusion
) {
157 /* Both exclusions exists, check count followed by the names. */
158 if (event
->exclusion
->count
!= key
->exclusion
->count
||
159 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
160 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
174 * Unique add of an ust app event in the given ht. This uses the custom
175 * ht_match_ust_app_event match function and the event name as hash.
177 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
178 struct ust_app_event
*event
)
180 struct cds_lfht_node
*node_ptr
;
181 struct ust_app_ht_key key
;
185 assert(ua_chan
->events
);
188 ht
= ua_chan
->events
;
189 key
.name
= event
->attr
.name
;
190 key
.filter
= event
->filter
;
191 key
.loglevel_type
= event
->attr
.loglevel
;
192 key
.exclusion
= event
->exclusion
;
194 node_ptr
= cds_lfht_add_unique(ht
->ht
,
195 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
196 ht_match_ust_app_event
, &key
, &event
->node
.node
);
197 assert(node_ptr
== &event
->node
.node
);
201 * Close the notify socket from the given RCU head object. This MUST be called
202 * through a call_rcu().
204 static void close_notify_sock_rcu(struct rcu_head
*head
)
207 struct ust_app_notify_sock_obj
*obj
=
208 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
210 /* Must have a valid fd here. */
211 assert(obj
->fd
>= 0);
213 ret
= close(obj
->fd
);
215 ERR("close notify sock %d RCU", obj
->fd
);
217 lttng_fd_put(LTTNG_FD_APPS
, 1);
223 * Return the session registry according to the buffer type of the given
226 * A registry per UID object MUST exists before calling this function or else
227 * it assert() if not found. RCU read side lock must be acquired.
229 static struct ust_registry_session
*get_session_registry(
230 struct ust_app_session
*ua_sess
)
232 struct ust_registry_session
*registry
= NULL
;
236 switch (ua_sess
->buffer_type
) {
237 case LTTNG_BUFFER_PER_PID
:
239 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
243 registry
= reg_pid
->registry
->reg
.ust
;
246 case LTTNG_BUFFER_PER_UID
:
248 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
249 ua_sess
->tracing_id
, ua_sess
->bits_per_long
, ua_sess
->uid
);
253 registry
= reg_uid
->registry
->reg
.ust
;
265 * Delete ust context safely. RCU read lock must be held before calling
269 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
277 pthread_mutex_lock(&app
->sock_lock
);
278 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
279 pthread_mutex_unlock(&app
->sock_lock
);
280 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
281 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
282 sock
, ua_ctx
->obj
->handle
, ret
);
290 * Delete ust app event safely. RCU read lock must be held before calling
294 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
301 free(ua_event
->filter
);
302 if (ua_event
->exclusion
!= NULL
)
303 free(ua_event
->exclusion
);
304 if (ua_event
->obj
!= NULL
) {
305 pthread_mutex_lock(&app
->sock_lock
);
306 ret
= ustctl_release_object(sock
, ua_event
->obj
);
307 pthread_mutex_unlock(&app
->sock_lock
);
308 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
309 ERR("UST app sock %d release event obj failed with ret %d",
318 * Release ust data object of the given stream.
320 * Return 0 on success or else a negative value.
322 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
330 pthread_mutex_lock(&app
->sock_lock
);
331 ret
= ustctl_release_object(sock
, stream
->obj
);
332 pthread_mutex_unlock(&app
->sock_lock
);
333 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
334 ERR("UST app sock %d release stream obj failed with ret %d",
337 lttng_fd_put(LTTNG_FD_APPS
, 2);
345 * Delete ust app stream safely. RCU read lock must be held before calling
349 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
354 (void) release_ust_app_stream(sock
, stream
, app
);
359 * We need to execute ht_destroy outside of RCU read-side critical
360 * section and outside of call_rcu thread, so we postpone its execution
361 * using ht_cleanup_push. It is simpler than to change the semantic of
362 * the many callers of delete_ust_app_session().
365 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
367 struct ust_app_channel
*ua_chan
=
368 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
370 ht_cleanup_push(ua_chan
->ctx
);
371 ht_cleanup_push(ua_chan
->events
);
376 * Extract the lost packet or discarded events counter when the channel is
377 * being deleted and store the value in the parent channel so we can
378 * access it from lttng list and at stop/destroy.
381 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
383 uint64_t discarded
= 0, lost
= 0;
384 struct ltt_session
*session
;
385 struct ltt_ust_channel
*uchan
;
387 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
392 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
394 ERR("Missing LTT session to get discarded events");
397 if (!session
->ust_session
) {
398 ERR("Missing UST session to get discarded events");
402 if (ua_chan
->attr
.overwrite
) {
403 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
404 ua_chan
->key
, session
->ust_session
->consumer
,
407 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
408 ua_chan
->key
, session
->ust_session
->consumer
,
411 uchan
= trace_ust_find_channel_by_name(
412 session
->ust_session
->domain_global
.channels
,
415 ERR("Missing UST channel to store discarded counters");
419 uchan
->per_pid_closed_app_discarded
+= discarded
;
420 uchan
->per_pid_closed_app_lost
+= lost
;
427 * Delete ust app channel safely. RCU read lock must be held before calling
431 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
435 struct lttng_ht_iter iter
;
436 struct ust_app_event
*ua_event
;
437 struct ust_app_ctx
*ua_ctx
;
438 struct ust_app_stream
*stream
, *stmp
;
439 struct ust_registry_session
*registry
;
443 DBG3("UST app deleting channel %s", ua_chan
->name
);
446 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
447 cds_list_del(&stream
->list
);
448 delete_ust_app_stream(sock
, stream
, app
);
452 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
453 cds_list_del(&ua_ctx
->list
);
454 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
456 delete_ust_app_ctx(sock
, ua_ctx
, app
);
460 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
462 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
464 delete_ust_app_event(sock
, ua_event
, app
);
467 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
468 /* Wipe and free registry from session registry. */
469 registry
= get_session_registry(ua_chan
->session
);
471 ust_registry_channel_del_free(registry
, ua_chan
->key
);
473 save_per_pid_lost_discarded_counters(ua_chan
);
476 if (ua_chan
->obj
!= NULL
) {
477 /* Remove channel from application UST object descriptor. */
478 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
479 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
481 pthread_mutex_lock(&app
->sock_lock
);
482 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
483 pthread_mutex_unlock(&app
->sock_lock
);
484 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
485 ERR("UST app sock %d release channel obj failed with ret %d",
488 lttng_fd_put(LTTNG_FD_APPS
, 1);
491 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
494 int ust_app_register_done(struct ust_app
*app
)
498 pthread_mutex_lock(&app
->sock_lock
);
499 ret
= ustctl_register_done(app
->sock
);
500 pthread_mutex_unlock(&app
->sock_lock
);
504 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
509 pthread_mutex_lock(&app
->sock_lock
);
514 ret
= ustctl_release_object(sock
, data
);
516 pthread_mutex_unlock(&app
->sock_lock
);
522 * Push metadata to consumer socket.
524 * RCU read-side lock must be held to guarantee existance of socket.
525 * Must be called with the ust app session lock held.
526 * Must be called with the registry lock held.
528 * On success, return the len of metadata pushed or else a negative value.
529 * Returning a -EPIPE return value means we could not send the metadata,
530 * but it can be caused by recoverable errors (e.g. the application has
531 * terminated concurrently).
533 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
534 struct consumer_socket
*socket
, int send_zero_data
)
537 char *metadata_str
= NULL
;
538 size_t len
, offset
, new_metadata_len_sent
;
540 uint64_t metadata_key
;
545 metadata_key
= registry
->metadata_key
;
548 * Means that no metadata was assigned to the session. This can
549 * happens if no start has been done previously.
556 * On a push metadata error either the consumer is dead or the
557 * metadata channel has been destroyed because its endpoint
558 * might have died (e.g: relayd), or because the application has
559 * exited. If so, the metadata closed flag is set to 1 so we
560 * deny pushing metadata again which is not valid anymore on the
563 if (registry
->metadata_closed
) {
567 offset
= registry
->metadata_len_sent
;
568 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
569 new_metadata_len_sent
= registry
->metadata_len
;
571 DBG3("No metadata to push for metadata key %" PRIu64
,
572 registry
->metadata_key
);
574 if (send_zero_data
) {
575 DBG("No metadata to push");
581 /* Allocate only what we have to send. */
582 metadata_str
= zmalloc(len
);
584 PERROR("zmalloc ust app metadata string");
588 /* Copy what we haven't sent out. */
589 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
592 pthread_mutex_unlock(®istry
->lock
);
594 * We need to unlock the registry while we push metadata to
595 * break a circular dependency between the consumerd metadata
596 * lock and the sessiond registry lock. Indeed, pushing metadata
597 * to the consumerd awaits that it gets pushed all the way to
598 * relayd, but doing so requires grabbing the metadata lock. If
599 * a concurrent metadata request is being performed by
600 * consumerd, this can try to grab the registry lock on the
601 * sessiond while holding the metadata lock on the consumer
602 * daemon. Those push and pull schemes are performed on two
603 * different bidirectionnal communication sockets.
605 ret
= consumer_push_metadata(socket
, metadata_key
,
606 metadata_str
, len
, offset
);
607 pthread_mutex_lock(®istry
->lock
);
610 * There is an acceptable race here between the registry
611 * metadata key assignment and the creation on the
612 * consumer. The session daemon can concurrently push
613 * metadata for this registry while being created on the
614 * consumer since the metadata key of the registry is
615 * assigned *before* it is setup to avoid the consumer
616 * to ask for metadata that could possibly be not found
617 * in the session daemon.
619 * The metadata will get pushed either by the session
620 * being stopped or the consumer requesting metadata if
621 * that race is triggered.
623 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
626 ERR("Error pushing metadata to consumer");
632 * Metadata may have been concurrently pushed, since
633 * we're not holding the registry lock while pushing to
634 * consumer. This is handled by the fact that we send
635 * the metadata content, size, and the offset at which
636 * that metadata belongs. This may arrive out of order
637 * on the consumer side, and the consumer is able to
638 * deal with overlapping fragments. The consumer
639 * supports overlapping fragments, which must be
640 * contiguous starting from offset 0. We keep the
641 * largest metadata_len_sent value of the concurrent
644 registry
->metadata_len_sent
=
645 max_t(size_t, registry
->metadata_len_sent
,
646 new_metadata_len_sent
);
655 * On error, flag the registry that the metadata is
656 * closed. We were unable to push anything and this
657 * means that either the consumer is not responding or
658 * the metadata cache has been destroyed on the
661 registry
->metadata_closed
= 1;
669 * For a given application and session, push metadata to consumer.
670 * Either sock or consumer is required : if sock is NULL, the default
671 * socket to send the metadata is retrieved from consumer, if sock
672 * is not NULL we use it to send the metadata.
673 * RCU read-side lock must be held while calling this function,
674 * therefore ensuring existance of registry. It also ensures existance
675 * of socket throughout this function.
677 * Return 0 on success else a negative error.
678 * Returning a -EPIPE return value means we could not send the metadata,
679 * but it can be caused by recoverable errors (e.g. the application has
680 * terminated concurrently).
682 static int push_metadata(struct ust_registry_session
*registry
,
683 struct consumer_output
*consumer
)
687 struct consumer_socket
*socket
;
692 pthread_mutex_lock(®istry
->lock
);
693 if (registry
->metadata_closed
) {
698 /* Get consumer socket to use to push the metadata.*/
699 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
706 ret
= ust_app_push_metadata(registry
, socket
, 0);
711 pthread_mutex_unlock(®istry
->lock
);
715 pthread_mutex_unlock(®istry
->lock
);
720 * Send to the consumer a close metadata command for the given session. Once
721 * done, the metadata channel is deleted and the session metadata pointer is
722 * nullified. The session lock MUST be held unless the application is
723 * in the destroy path.
725 * Return 0 on success else a negative value.
727 static int close_metadata(struct ust_registry_session
*registry
,
728 struct consumer_output
*consumer
)
731 struct consumer_socket
*socket
;
738 pthread_mutex_lock(®istry
->lock
);
740 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
745 /* Get consumer socket to use to push the metadata.*/
746 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
753 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
760 * Metadata closed. Even on error this means that the consumer is not
761 * responding or not found so either way a second close should NOT be emit
764 registry
->metadata_closed
= 1;
766 pthread_mutex_unlock(®istry
->lock
);
772 * We need to execute ht_destroy outside of RCU read-side critical
773 * section and outside of call_rcu thread, so we postpone its execution
774 * using ht_cleanup_push. It is simpler than to change the semantic of
775 * the many callers of delete_ust_app_session().
778 void delete_ust_app_session_rcu(struct rcu_head
*head
)
780 struct ust_app_session
*ua_sess
=
781 caa_container_of(head
, struct ust_app_session
, rcu_head
);
783 ht_cleanup_push(ua_sess
->channels
);
788 * Delete ust app session safely. RCU read lock must be held before calling
792 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
796 struct lttng_ht_iter iter
;
797 struct ust_app_channel
*ua_chan
;
798 struct ust_registry_session
*registry
;
802 pthread_mutex_lock(&ua_sess
->lock
);
804 assert(!ua_sess
->deleted
);
805 ua_sess
->deleted
= true;
807 registry
= get_session_registry(ua_sess
);
809 /* Push metadata for application before freeing the application. */
810 (void) push_metadata(registry
, ua_sess
->consumer
);
813 * Don't ask to close metadata for global per UID buffers. Close
814 * metadata only on destroy trace session in this case. Also, the
815 * previous push metadata could have flag the metadata registry to
816 * close so don't send a close command if closed.
818 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
819 /* And ask to close it for this session registry. */
820 (void) close_metadata(registry
, ua_sess
->consumer
);
824 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
826 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
828 delete_ust_app_channel(sock
, ua_chan
, app
);
831 /* In case of per PID, the registry is kept in the session. */
832 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
833 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
835 buffer_reg_pid_remove(reg_pid
);
836 buffer_reg_pid_destroy(reg_pid
);
840 if (ua_sess
->handle
!= -1) {
841 pthread_mutex_lock(&app
->sock_lock
);
842 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
843 pthread_mutex_unlock(&app
->sock_lock
);
844 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
845 ERR("UST app sock %d release session handle failed with ret %d",
848 /* Remove session from application UST object descriptor. */
849 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
850 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
854 pthread_mutex_unlock(&ua_sess
->lock
);
856 consumer_output_put(ua_sess
->consumer
);
858 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
862 * Delete a traceable application structure from the global list. Never call
863 * this function outside of a call_rcu call.
865 * RCU read side lock should _NOT_ be held when calling this function.
868 void delete_ust_app(struct ust_app
*app
)
871 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
873 /* Delete ust app sessions info */
878 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
880 /* Free every object in the session and the session. */
882 delete_ust_app_session(sock
, ua_sess
, app
);
886 ht_cleanup_push(app
->sessions
);
887 ht_cleanup_push(app
->ust_sessions_objd
);
888 ht_cleanup_push(app
->ust_objd
);
891 * Wait until we have deleted the application from the sock hash table
892 * before closing this socket, otherwise an application could re-use the
893 * socket ID and race with the teardown, using the same hash table entry.
895 * It's OK to leave the close in call_rcu. We want it to stay unique for
896 * all RCU readers that could run concurrently with unregister app,
897 * therefore we _need_ to only close that socket after a grace period. So
898 * it should stay in this RCU callback.
900 * This close() is a very important step of the synchronization model so
901 * every modification to this function must be carefully reviewed.
907 lttng_fd_put(LTTNG_FD_APPS
, 1);
909 DBG2("UST app pid %d deleted", app
->pid
);
914 * URCU intermediate call to delete an UST app.
917 void delete_ust_app_rcu(struct rcu_head
*head
)
919 struct lttng_ht_node_ulong
*node
=
920 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
921 struct ust_app
*app
=
922 caa_container_of(node
, struct ust_app
, pid_n
);
924 DBG3("Call RCU deleting app PID %d", app
->pid
);
929 * Delete the session from the application ht and delete the data structure by
930 * freeing every object inside and releasing them.
932 static void destroy_app_session(struct ust_app
*app
,
933 struct ust_app_session
*ua_sess
)
936 struct lttng_ht_iter iter
;
941 iter
.iter
.node
= &ua_sess
->node
.node
;
942 ret
= lttng_ht_del(app
->sessions
, &iter
);
944 /* Already scheduled for teardown. */
948 /* Once deleted, free the data structure. */
949 delete_ust_app_session(app
->sock
, ua_sess
, app
);
956 * Alloc new UST app session.
959 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
961 struct ust_app_session
*ua_sess
;
963 /* Init most of the default value by allocating and zeroing */
964 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
965 if (ua_sess
== NULL
) {
970 ua_sess
->handle
= -1;
971 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
972 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
973 pthread_mutex_init(&ua_sess
->lock
, NULL
);
982 * Alloc new UST app channel.
985 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
986 struct ust_app_session
*ua_sess
,
987 struct lttng_ust_channel_attr
*attr
)
989 struct ust_app_channel
*ua_chan
;
991 /* Init most of the default value by allocating and zeroing */
992 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
993 if (ua_chan
== NULL
) {
998 /* Setup channel name */
999 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1000 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1002 ua_chan
->enabled
= 1;
1003 ua_chan
->handle
= -1;
1004 ua_chan
->session
= ua_sess
;
1005 ua_chan
->key
= get_next_channel_key();
1006 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1007 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1008 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1010 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1011 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1013 /* Copy attributes */
1015 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1016 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1017 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1018 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1019 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1020 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1021 ua_chan
->attr
.output
= attr
->output
;
1023 /* By default, the channel is a per cpu channel. */
1024 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1026 DBG3("UST app channel %s allocated", ua_chan
->name
);
1035 * Allocate and initialize a UST app stream.
1037 * Return newly allocated stream pointer or NULL on error.
1039 struct ust_app_stream
*ust_app_alloc_stream(void)
1041 struct ust_app_stream
*stream
= NULL
;
1043 stream
= zmalloc(sizeof(*stream
));
1044 if (stream
== NULL
) {
1045 PERROR("zmalloc ust app stream");
1049 /* Zero could be a valid value for a handle so flag it to -1. */
1050 stream
->handle
= -1;
1057 * Alloc new UST app event.
1060 struct ust_app_event
*alloc_ust_app_event(char *name
,
1061 struct lttng_ust_event
*attr
)
1063 struct ust_app_event
*ua_event
;
1065 /* Init most of the default value by allocating and zeroing */
1066 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1067 if (ua_event
== NULL
) {
1072 ua_event
->enabled
= 1;
1073 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1074 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1075 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1077 /* Copy attributes */
1079 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1082 DBG3("UST app event %s allocated", ua_event
->name
);
1091 * Alloc new UST app context.
1094 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1096 struct ust_app_ctx
*ua_ctx
;
1098 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1099 if (ua_ctx
== NULL
) {
1103 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1106 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1107 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1108 char *provider_name
= NULL
, *ctx_name
= NULL
;
1110 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1111 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1112 if (!provider_name
|| !ctx_name
) {
1113 free(provider_name
);
1118 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1119 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1123 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1131 * Allocate a filter and copy the given original filter.
1133 * Return allocated filter or NULL on error.
1135 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1136 struct lttng_filter_bytecode
*orig_f
)
1138 struct lttng_filter_bytecode
*filter
= NULL
;
1140 /* Copy filter bytecode */
1141 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1143 PERROR("zmalloc alloc filter bytecode");
1147 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1154 * Create a liblttng-ust filter bytecode from given bytecode.
1156 * Return allocated filter or NULL on error.
1158 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1159 struct lttng_filter_bytecode
*orig_f
)
1161 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1163 /* Copy filter bytecode */
1164 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1166 PERROR("zmalloc alloc ust filter bytecode");
1170 assert(sizeof(struct lttng_filter_bytecode
) ==
1171 sizeof(struct lttng_ust_filter_bytecode
));
1172 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1178 * Find an ust_app using the sock and return it. RCU read side lock must be
1179 * held before calling this helper function.
1181 struct ust_app
*ust_app_find_by_sock(int sock
)
1183 struct lttng_ht_node_ulong
*node
;
1184 struct lttng_ht_iter iter
;
1186 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1187 node
= lttng_ht_iter_get_node_ulong(&iter
);
1189 DBG2("UST app find by sock %d not found", sock
);
1193 return caa_container_of(node
, struct ust_app
, sock_n
);
1200 * Find an ust_app using the notify sock and return it. RCU read side lock must
1201 * be held before calling this helper function.
1203 static struct ust_app
*find_app_by_notify_sock(int sock
)
1205 struct lttng_ht_node_ulong
*node
;
1206 struct lttng_ht_iter iter
;
1208 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1210 node
= lttng_ht_iter_get_node_ulong(&iter
);
1212 DBG2("UST app find by notify sock %d not found", sock
);
1216 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1223 * Lookup for an ust app event based on event name, filter bytecode and the
1226 * Return an ust_app_event object or NULL on error.
1228 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1229 char *name
, struct lttng_filter_bytecode
*filter
,
1231 const struct lttng_event_exclusion
*exclusion
)
1233 struct lttng_ht_iter iter
;
1234 struct lttng_ht_node_str
*node
;
1235 struct ust_app_event
*event
= NULL
;
1236 struct ust_app_ht_key key
;
1241 /* Setup key for event lookup. */
1243 key
.filter
= filter
;
1244 key
.loglevel_type
= loglevel_value
;
1245 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1246 key
.exclusion
= exclusion
;
1248 /* Lookup using the event name as hash and a custom match fct. */
1249 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1250 ht_match_ust_app_event
, &key
, &iter
.iter
);
1251 node
= lttng_ht_iter_get_node_str(&iter
);
1256 event
= caa_container_of(node
, struct ust_app_event
, node
);
1263 * Create the channel context on the tracer.
1265 * Called with UST app session lock held.
1268 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1269 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1273 health_code_update();
1275 pthread_mutex_lock(&app
->sock_lock
);
1276 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1277 ua_chan
->obj
, &ua_ctx
->obj
);
1278 pthread_mutex_unlock(&app
->sock_lock
);
1280 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1281 ERR("UST app create channel context failed for app (pid: %d) "
1282 "with ret %d", app
->pid
, ret
);
1285 * This is normal behavior, an application can die during the
1286 * creation process. Don't report an error so the execution can
1287 * continue normally.
1290 DBG3("UST app disable event failed. Application is dead.");
1295 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1297 DBG2("UST app context handle %d created successfully for channel %s",
1298 ua_ctx
->handle
, ua_chan
->name
);
1301 health_code_update();
1306 * Set the filter on the tracer.
1309 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1310 struct ust_app
*app
)
1313 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1315 health_code_update();
1317 if (!ua_event
->filter
) {
1322 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1323 if (!ust_bytecode
) {
1324 ret
= -LTTNG_ERR_NOMEM
;
1327 pthread_mutex_lock(&app
->sock_lock
);
1328 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1330 pthread_mutex_unlock(&app
->sock_lock
);
1332 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1333 ERR("UST app event %s filter failed for app (pid: %d) "
1334 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1337 * This is normal behavior, an application can die during the
1338 * creation process. Don't report an error so the execution can
1339 * continue normally.
1342 DBG3("UST app filter event failed. Application is dead.");
1347 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1350 health_code_update();
1356 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1357 struct lttng_event_exclusion
*exclusion
)
1359 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1360 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1361 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1363 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1364 if (!ust_exclusion
) {
1369 assert(sizeof(struct lttng_event_exclusion
) ==
1370 sizeof(struct lttng_ust_event_exclusion
));
1371 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1373 return ust_exclusion
;
1377 * Set event exclusions on the tracer.
1380 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1381 struct ust_app
*app
)
1384 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1386 health_code_update();
1388 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1393 ust_exclusion
= create_ust_exclusion_from_exclusion(
1394 ua_event
->exclusion
);
1395 if (!ust_exclusion
) {
1396 ret
= -LTTNG_ERR_NOMEM
;
1399 pthread_mutex_lock(&app
->sock_lock
);
1400 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1401 pthread_mutex_unlock(&app
->sock_lock
);
1403 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1404 ERR("UST app event %s exclusions failed for app (pid: %d) "
1405 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1408 * This is normal behavior, an application can die during the
1409 * creation process. Don't report an error so the execution can
1410 * continue normally.
1413 DBG3("UST app event exclusion failed. Application is dead.");
1418 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1421 health_code_update();
1422 free(ust_exclusion
);
1427 * Disable the specified event on to UST tracer for the UST session.
1429 static int disable_ust_event(struct ust_app
*app
,
1430 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1434 health_code_update();
1436 pthread_mutex_lock(&app
->sock_lock
);
1437 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1438 pthread_mutex_unlock(&app
->sock_lock
);
1440 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1441 ERR("UST app event %s disable failed for app (pid: %d) "
1442 "and session handle %d with ret %d",
1443 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1446 * This is normal behavior, an application can die during the
1447 * creation process. Don't report an error so the execution can
1448 * continue normally.
1451 DBG3("UST app disable event failed. Application is dead.");
1456 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1457 ua_event
->attr
.name
, app
->pid
);
1460 health_code_update();
1465 * Disable the specified channel on to UST tracer for the UST session.
1467 static int disable_ust_channel(struct ust_app
*app
,
1468 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1472 health_code_update();
1474 pthread_mutex_lock(&app
->sock_lock
);
1475 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1476 pthread_mutex_unlock(&app
->sock_lock
);
1478 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1479 ERR("UST app channel %s disable failed for app (pid: %d) "
1480 "and session handle %d with ret %d",
1481 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1484 * This is normal behavior, an application can die during the
1485 * creation process. Don't report an error so the execution can
1486 * continue normally.
1489 DBG3("UST app disable channel failed. Application is dead.");
1494 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1495 ua_chan
->name
, app
->pid
);
1498 health_code_update();
1503 * Enable the specified channel on to UST tracer for the UST session.
1505 static int enable_ust_channel(struct ust_app
*app
,
1506 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1510 health_code_update();
1512 pthread_mutex_lock(&app
->sock_lock
);
1513 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1514 pthread_mutex_unlock(&app
->sock_lock
);
1516 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1517 ERR("UST app channel %s enable failed for app (pid: %d) "
1518 "and session handle %d with ret %d",
1519 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1522 * This is normal behavior, an application can die during the
1523 * creation process. Don't report an error so the execution can
1524 * continue normally.
1527 DBG3("UST app enable channel failed. Application is dead.");
1532 ua_chan
->enabled
= 1;
1534 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1535 ua_chan
->name
, app
->pid
);
1538 health_code_update();
1543 * Enable the specified event on to UST tracer for the UST session.
1545 static int enable_ust_event(struct ust_app
*app
,
1546 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1550 health_code_update();
1552 pthread_mutex_lock(&app
->sock_lock
);
1553 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1554 pthread_mutex_unlock(&app
->sock_lock
);
1556 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1557 ERR("UST app event %s enable failed for app (pid: %d) "
1558 "and session handle %d with ret %d",
1559 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, 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("UST app enable event failed. Application is dead.");
1572 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1573 ua_event
->attr
.name
, app
->pid
);
1576 health_code_update();
1581 * Send channel and stream buffer to application.
1583 * Return 0 on success. On error, a negative value is returned.
1585 static int send_channel_pid_to_ust(struct ust_app
*app
,
1586 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1589 struct ust_app_stream
*stream
, *stmp
;
1595 health_code_update();
1597 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1600 /* Send channel to the application. */
1601 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1602 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1603 ret
= -ENOTCONN
; /* Caused by app exiting. */
1605 } else if (ret
< 0) {
1609 health_code_update();
1611 /* Send all streams to application. */
1612 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1613 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1614 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1615 ret
= -ENOTCONN
; /* Caused by app exiting. */
1617 } else if (ret
< 0) {
1620 /* We don't need the stream anymore once sent to the tracer. */
1621 cds_list_del(&stream
->list
);
1622 delete_ust_app_stream(-1, stream
, app
);
1624 /* Flag the channel that it is sent to the application. */
1625 ua_chan
->is_sent
= 1;
1628 health_code_update();
1633 * Create the specified event onto the UST tracer for a UST session.
1635 * Should be called with session mutex held.
1638 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1639 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1643 health_code_update();
1645 /* Create UST event on tracer */
1646 pthread_mutex_lock(&app
->sock_lock
);
1647 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1649 pthread_mutex_unlock(&app
->sock_lock
);
1651 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1652 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1653 ua_event
->attr
.name
, app
->pid
, ret
);
1656 * This is normal behavior, an application can die during the
1657 * creation process. Don't report an error so the execution can
1658 * continue normally.
1661 DBG3("UST app create event failed. Application is dead.");
1666 ua_event
->handle
= ua_event
->obj
->handle
;
1668 DBG2("UST app event %s created successfully for pid:%d",
1669 ua_event
->attr
.name
, app
->pid
);
1671 health_code_update();
1673 /* Set filter if one is present. */
1674 if (ua_event
->filter
) {
1675 ret
= set_ust_event_filter(ua_event
, app
);
1681 /* Set exclusions for the event */
1682 if (ua_event
->exclusion
) {
1683 ret
= set_ust_event_exclusion(ua_event
, app
);
1689 /* If event not enabled, disable it on the tracer */
1690 if (ua_event
->enabled
) {
1692 * We now need to explicitly enable the event, since it
1693 * is now disabled at creation.
1695 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1698 * If we hit an EPERM, something is wrong with our enable call. If
1699 * we get an EEXIST, there is a problem on the tracer side since we
1703 case -LTTNG_UST_ERR_PERM
:
1704 /* Code flow problem */
1706 case -LTTNG_UST_ERR_EXIST
:
1707 /* It's OK for our use case. */
1718 health_code_update();
1723 * Copy data between an UST app event and a LTT event.
1725 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1726 struct ltt_ust_event
*uevent
)
1728 size_t exclusion_alloc_size
;
1730 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1731 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1733 ua_event
->enabled
= uevent
->enabled
;
1735 /* Copy event attributes */
1736 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1738 /* Copy filter bytecode */
1739 if (uevent
->filter
) {
1740 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1741 /* Filter might be NULL here in case of ENONEM. */
1744 /* Copy exclusion data */
1745 if (uevent
->exclusion
) {
1746 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1747 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1748 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1749 if (ua_event
->exclusion
== NULL
) {
1752 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1753 exclusion_alloc_size
);
1759 * Copy data between an UST app channel and a LTT channel.
1761 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1762 struct ltt_ust_channel
*uchan
)
1764 struct lttng_ht_iter iter
;
1765 struct ltt_ust_event
*uevent
;
1766 struct ltt_ust_context
*uctx
;
1767 struct ust_app_event
*ua_event
;
1769 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1771 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1772 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1774 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1775 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1777 /* Copy event attributes since the layout is different. */
1778 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1779 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1780 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1781 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1782 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1783 ua_chan
->attr
.output
= uchan
->attr
.output
;
1785 * Note that the attribute channel type is not set since the channel on the
1786 * tracing registry side does not have this information.
1789 ua_chan
->enabled
= uchan
->enabled
;
1790 ua_chan
->tracing_channel_id
= uchan
->id
;
1792 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1793 struct ust_app_ctx
*ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1795 if (ua_ctx
== NULL
) {
1798 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1799 (unsigned long) ua_ctx
->ctx
.ctx
);
1800 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1801 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1804 /* Copy all events from ltt ust channel to ust app channel */
1805 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1806 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1807 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1808 if (ua_event
== NULL
) {
1809 DBG2("UST event %s not found on shadow copy channel",
1811 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1812 if (ua_event
== NULL
) {
1815 shadow_copy_event(ua_event
, uevent
);
1816 add_unique_ust_app_event(ua_chan
, ua_event
);
1820 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1824 * Copy data between a UST app session and a regular LTT session.
1826 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1827 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1829 struct lttng_ht_node_str
*ua_chan_node
;
1830 struct lttng_ht_iter iter
;
1831 struct ltt_ust_channel
*uchan
;
1832 struct ust_app_channel
*ua_chan
;
1834 struct tm
*timeinfo
;
1837 char tmp_shm_path
[PATH_MAX
];
1839 /* Get date and time for unique app path */
1841 timeinfo
= localtime(&rawtime
);
1842 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1844 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1846 ua_sess
->tracing_id
= usess
->id
;
1847 ua_sess
->id
= get_next_session_id();
1848 ua_sess
->uid
= app
->uid
;
1849 ua_sess
->gid
= app
->gid
;
1850 ua_sess
->euid
= usess
->uid
;
1851 ua_sess
->egid
= usess
->gid
;
1852 ua_sess
->buffer_type
= usess
->buffer_type
;
1853 ua_sess
->bits_per_long
= app
->bits_per_long
;
1855 /* There is only one consumer object per session possible. */
1856 consumer_output_get(usess
->consumer
);
1857 ua_sess
->consumer
= usess
->consumer
;
1859 ua_sess
->output_traces
= usess
->output_traces
;
1860 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1861 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1862 &usess
->metadata_attr
);
1864 switch (ua_sess
->buffer_type
) {
1865 case LTTNG_BUFFER_PER_PID
:
1866 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1867 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1870 case LTTNG_BUFFER_PER_UID
:
1871 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1872 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1879 PERROR("asprintf UST shadow copy session");
1884 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1885 sizeof(ua_sess
->root_shm_path
));
1886 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1887 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1888 sizeof(ua_sess
->shm_path
));
1889 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1890 if (ua_sess
->shm_path
[0]) {
1891 switch (ua_sess
->buffer_type
) {
1892 case LTTNG_BUFFER_PER_PID
:
1893 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1894 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1895 app
->name
, app
->pid
, datetime
);
1897 case LTTNG_BUFFER_PER_UID
:
1898 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1899 DEFAULT_UST_TRACE_UID_PATH
,
1900 app
->uid
, app
->bits_per_long
);
1907 PERROR("sprintf UST shadow copy session");
1911 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1912 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1913 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1916 /* Iterate over all channels in global domain. */
1917 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1919 struct lttng_ht_iter uiter
;
1921 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1922 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1923 if (ua_chan_node
!= NULL
) {
1924 /* Session exist. Contiuing. */
1928 DBG2("Channel %s not found on shadow session copy, creating it",
1930 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
,
1932 if (ua_chan
== NULL
) {
1933 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1936 shadow_copy_channel(ua_chan
, uchan
);
1938 * The concept of metadata channel does not exist on the tracing
1939 * registry side of the session daemon so this can only be a per CPU
1940 * channel and not metadata.
1942 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1944 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1949 consumer_output_put(ua_sess
->consumer
);
1953 * Lookup sesison wrapper.
1956 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1957 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1959 /* Get right UST app session from app */
1960 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1964 * Return ust app session from the app session hashtable using the UST session
1967 static struct ust_app_session
*lookup_session_by_app(
1968 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1970 struct lttng_ht_iter iter
;
1971 struct lttng_ht_node_u64
*node
;
1973 __lookup_session_by_app(usess
, app
, &iter
);
1974 node
= lttng_ht_iter_get_node_u64(&iter
);
1979 return caa_container_of(node
, struct ust_app_session
, node
);
1986 * Setup buffer registry per PID for the given session and application. If none
1987 * is found, a new one is created, added to the global registry and
1988 * initialized. If regp is valid, it's set with the newly created object.
1990 * Return 0 on success or else a negative value.
1992 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1993 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1996 struct buffer_reg_pid
*reg_pid
;
2003 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2006 * This is the create channel path meaning that if there is NO
2007 * registry available, we have to create one for this session.
2009 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2010 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2018 /* Initialize registry. */
2019 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2020 app
->bits_per_long
, app
->uint8_t_alignment
,
2021 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2022 app
->uint64_t_alignment
, app
->long_alignment
,
2023 app
->byte_order
, app
->version
.major
,
2024 app
->version
.minor
, reg_pid
->root_shm_path
,
2026 ua_sess
->euid
, ua_sess
->egid
);
2029 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2030 * destroy the buffer registry, because it is always expected
2031 * that if the buffer registry can be found, its ust registry is
2034 buffer_reg_pid_destroy(reg_pid
);
2038 buffer_reg_pid_add(reg_pid
);
2040 DBG3("UST app buffer registry per PID created successfully");
2052 * Setup buffer registry per UID for the given session and application. If none
2053 * is found, a new one is created, added to the global registry and
2054 * initialized. If regp is valid, it's set with the newly created object.
2056 * Return 0 on success or else a negative value.
2058 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2059 struct ust_app_session
*ua_sess
,
2060 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2063 struct buffer_reg_uid
*reg_uid
;
2070 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2073 * This is the create channel path meaning that if there is NO
2074 * registry available, we have to create one for this session.
2076 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2077 LTTNG_DOMAIN_UST
, ®_uid
,
2078 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2086 /* Initialize registry. */
2087 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2088 app
->bits_per_long
, app
->uint8_t_alignment
,
2089 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2090 app
->uint64_t_alignment
, app
->long_alignment
,
2091 app
->byte_order
, app
->version
.major
,
2092 app
->version
.minor
, reg_uid
->root_shm_path
,
2093 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
2096 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2097 * destroy the buffer registry, because it is always expected
2098 * that if the buffer registry can be found, its ust registry is
2101 buffer_reg_uid_destroy(reg_uid
, NULL
);
2104 /* Add node to teardown list of the session. */
2105 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2107 buffer_reg_uid_add(reg_uid
);
2109 DBG3("UST app buffer registry per UID created successfully");
2120 * Create a session on the tracer side for the given app.
2122 * On success, ua_sess_ptr is populated with the session pointer or else left
2123 * untouched. If the session was created, is_created is set to 1. On error,
2124 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2127 * Returns 0 on success or else a negative code which is either -ENOMEM or
2128 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2130 static int create_ust_app_session(struct ltt_ust_session
*usess
,
2131 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2134 int ret
, created
= 0;
2135 struct ust_app_session
*ua_sess
;
2139 assert(ua_sess_ptr
);
2141 health_code_update();
2143 ua_sess
= lookup_session_by_app(usess
, app
);
2144 if (ua_sess
== NULL
) {
2145 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2146 app
->pid
, usess
->id
);
2147 ua_sess
= alloc_ust_app_session(app
);
2148 if (ua_sess
== NULL
) {
2149 /* Only malloc can failed so something is really wrong */
2153 shadow_copy_session(ua_sess
, usess
, app
);
2157 switch (usess
->buffer_type
) {
2158 case LTTNG_BUFFER_PER_PID
:
2159 /* Init local registry. */
2160 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2162 delete_ust_app_session(-1, ua_sess
, app
);
2166 case LTTNG_BUFFER_PER_UID
:
2167 /* Look for a global registry. If none exists, create one. */
2168 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2170 delete_ust_app_session(-1, ua_sess
, app
);
2180 health_code_update();
2182 if (ua_sess
->handle
== -1) {
2183 pthread_mutex_lock(&app
->sock_lock
);
2184 ret
= ustctl_create_session(app
->sock
);
2185 pthread_mutex_unlock(&app
->sock_lock
);
2187 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2188 ERR("Creating session for app pid %d with ret %d",
2191 DBG("UST app creating session failed. Application is dead");
2193 * This is normal behavior, an application can die during the
2194 * creation process. Don't report an error so the execution can
2195 * continue normally. This will get flagged ENOTCONN and the
2196 * caller will handle it.
2200 delete_ust_app_session(-1, ua_sess
, app
);
2201 if (ret
!= -ENOMEM
) {
2203 * Tracer is probably gone or got an internal error so let's
2204 * behave like it will soon unregister or not usable.
2211 ua_sess
->handle
= ret
;
2213 /* Add ust app session to app's HT */
2214 lttng_ht_node_init_u64(&ua_sess
->node
,
2215 ua_sess
->tracing_id
);
2216 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2217 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2218 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2219 &ua_sess
->ust_objd_node
);
2221 DBG2("UST app session created successfully with handle %d", ret
);
2224 *ua_sess_ptr
= ua_sess
;
2226 *is_created
= created
;
2229 /* Everything went well. */
2233 health_code_update();
2238 * Match function for a hash table lookup of ust_app_ctx.
2240 * It matches an ust app context based on the context type and, in the case
2241 * of perf counters, their name.
2243 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2245 struct ust_app_ctx
*ctx
;
2246 const struct lttng_ust_context_attr
*key
;
2251 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2255 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2260 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2261 if (strncmp(key
->u
.perf_counter
.name
,
2262 ctx
->ctx
.u
.perf_counter
.name
,
2263 sizeof(key
->u
.perf_counter
.name
))) {
2267 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2268 if (strcmp(key
->u
.app_ctx
.provider_name
,
2269 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2270 strcmp(key
->u
.app_ctx
.ctx_name
,
2271 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2287 * Lookup for an ust app context from an lttng_ust_context.
2289 * Must be called while holding RCU read side lock.
2290 * Return an ust_app_ctx object or NULL on error.
2293 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2294 struct lttng_ust_context_attr
*uctx
)
2296 struct lttng_ht_iter iter
;
2297 struct lttng_ht_node_ulong
*node
;
2298 struct ust_app_ctx
*app_ctx
= NULL
;
2303 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2304 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2305 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2306 node
= lttng_ht_iter_get_node_ulong(&iter
);
2311 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2318 * Create a context for the channel on the tracer.
2320 * Called with UST app session lock held and a RCU read side lock.
2323 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2324 struct ust_app_channel
*ua_chan
,
2325 struct lttng_ust_context_attr
*uctx
,
2326 struct ust_app
*app
)
2329 struct ust_app_ctx
*ua_ctx
;
2331 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2333 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2339 ua_ctx
= alloc_ust_app_ctx(uctx
);
2340 if (ua_ctx
== NULL
) {
2346 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2347 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2348 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2350 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2360 * Enable on the tracer side a ust app event for the session and channel.
2362 * Called with UST app session lock held.
2365 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2366 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2370 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2375 ua_event
->enabled
= 1;
2382 * Disable on the tracer side a ust app event for the session and channel.
2384 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2385 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2389 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2394 ua_event
->enabled
= 0;
2401 * Lookup ust app channel for session and disable it on the tracer side.
2404 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2405 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2409 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2414 ua_chan
->enabled
= 0;
2421 * Lookup ust app channel for session and enable it on the tracer side. This
2422 * MUST be called with a RCU read side lock acquired.
2424 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2425 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2428 struct lttng_ht_iter iter
;
2429 struct lttng_ht_node_str
*ua_chan_node
;
2430 struct ust_app_channel
*ua_chan
;
2432 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2433 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2434 if (ua_chan_node
== NULL
) {
2435 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2436 uchan
->name
, ua_sess
->tracing_id
);
2440 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2442 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2452 * Ask the consumer to create a channel and get it if successful.
2454 * Return 0 on success or else a negative value.
2456 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2457 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2458 int bitness
, struct ust_registry_session
*registry
)
2461 unsigned int nb_fd
= 0;
2462 struct consumer_socket
*socket
;
2470 health_code_update();
2472 /* Get the right consumer socket for the application. */
2473 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2479 health_code_update();
2481 /* Need one fd for the channel. */
2482 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2484 ERR("Exhausted number of available FD upon create channel");
2489 * Ask consumer to create channel. The consumer will return the number of
2490 * stream we have to expect.
2492 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2499 * Compute the number of fd needed before receiving them. It must be 2 per
2500 * stream (2 being the default value here).
2502 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2504 /* Reserve the amount of file descriptor we need. */
2505 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2507 ERR("Exhausted number of available FD upon create channel");
2508 goto error_fd_get_stream
;
2511 health_code_update();
2514 * Now get the channel from the consumer. This call wil populate the stream
2515 * list of that channel and set the ust objects.
2517 if (usess
->consumer
->enabled
) {
2518 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2528 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2529 error_fd_get_stream
:
2531 * Initiate a destroy channel on the consumer since we had an error
2532 * handling it on our side. The return value is of no importance since we
2533 * already have a ret value set by the previous error that we need to
2536 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2538 lttng_fd_put(LTTNG_FD_APPS
, 1);
2540 health_code_update();
2546 * Duplicate the ust data object of the ust app stream and save it in the
2547 * buffer registry stream.
2549 * Return 0 on success or else a negative value.
2551 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2552 struct ust_app_stream
*stream
)
2559 /* Reserve the amount of file descriptor we need. */
2560 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2562 ERR("Exhausted number of available FD upon duplicate stream");
2566 /* Duplicate object for stream once the original is in the registry. */
2567 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2568 reg_stream
->obj
.ust
);
2570 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2571 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2572 lttng_fd_put(LTTNG_FD_APPS
, 2);
2575 stream
->handle
= stream
->obj
->handle
;
2582 * Duplicate the ust data object of the ust app. channel and save it in the
2583 * buffer registry channel.
2585 * Return 0 on success or else a negative value.
2587 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2588 struct ust_app_channel
*ua_chan
)
2595 /* Need two fds for the channel. */
2596 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2598 ERR("Exhausted number of available FD upon duplicate channel");
2602 /* Duplicate object for stream once the original is in the registry. */
2603 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2605 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2606 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2609 ua_chan
->handle
= ua_chan
->obj
->handle
;
2614 lttng_fd_put(LTTNG_FD_APPS
, 1);
2620 * For a given channel buffer registry, setup all streams of the given ust
2621 * application channel.
2623 * Return 0 on success or else a negative value.
2625 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2626 struct ust_app_channel
*ua_chan
,
2627 struct ust_app
*app
)
2630 struct ust_app_stream
*stream
, *stmp
;
2635 DBG2("UST app setup buffer registry stream");
2637 /* Send all streams to application. */
2638 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2639 struct buffer_reg_stream
*reg_stream
;
2641 ret
= buffer_reg_stream_create(®_stream
);
2647 * Keep original pointer and nullify it in the stream so the delete
2648 * stream call does not release the object.
2650 reg_stream
->obj
.ust
= stream
->obj
;
2652 buffer_reg_stream_add(reg_stream
, reg_chan
);
2654 /* We don't need the streams anymore. */
2655 cds_list_del(&stream
->list
);
2656 delete_ust_app_stream(-1, stream
, app
);
2664 * Create a buffer registry channel for the given session registry and
2665 * application channel object. If regp pointer is valid, it's set with the
2666 * created object. Important, the created object is NOT added to the session
2667 * registry hash table.
2669 * Return 0 on success else a negative value.
2671 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2672 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2675 struct buffer_reg_channel
*reg_chan
= NULL
;
2680 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2682 /* Create buffer registry channel. */
2683 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2688 reg_chan
->consumer_key
= ua_chan
->key
;
2689 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2690 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2692 /* Create and add a channel registry to session. */
2693 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2694 ua_chan
->tracing_channel_id
);
2698 buffer_reg_channel_add(reg_sess
, reg_chan
);
2707 /* Safe because the registry channel object was not added to any HT. */
2708 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2714 * Setup buffer registry channel for the given session registry and application
2715 * channel object. If regp pointer is valid, it's set with the created object.
2717 * Return 0 on success else a negative value.
2719 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2720 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2721 struct ust_app
*app
)
2728 assert(ua_chan
->obj
);
2730 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2732 /* Setup all streams for the registry. */
2733 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2738 reg_chan
->obj
.ust
= ua_chan
->obj
;
2739 ua_chan
->obj
= NULL
;
2744 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2745 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2750 * Send buffer registry channel to the application.
2752 * Return 0 on success else a negative value.
2754 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2755 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2756 struct ust_app_channel
*ua_chan
)
2759 struct buffer_reg_stream
*reg_stream
;
2766 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2768 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2773 /* Send channel to the application. */
2774 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2775 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2776 ret
= -ENOTCONN
; /* Caused by app exiting. */
2778 } else if (ret
< 0) {
2782 health_code_update();
2784 /* Send all streams to application. */
2785 pthread_mutex_lock(®_chan
->stream_list_lock
);
2786 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2787 struct ust_app_stream stream
;
2789 ret
= duplicate_stream_object(reg_stream
, &stream
);
2791 goto error_stream_unlock
;
2794 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2796 (void) release_ust_app_stream(-1, &stream
, app
);
2797 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2798 ret
= -ENOTCONN
; /* Caused by app exiting. */
2799 goto error_stream_unlock
;
2800 } else if (ret
< 0) {
2801 goto error_stream_unlock
;
2803 goto error_stream_unlock
;
2807 * The return value is not important here. This function will output an
2810 (void) release_ust_app_stream(-1, &stream
, app
);
2812 ua_chan
->is_sent
= 1;
2814 error_stream_unlock
:
2815 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2821 * Create and send to the application the created buffers with per UID buffers.
2823 * Return 0 on success else a negative value.
2825 static int create_channel_per_uid(struct ust_app
*app
,
2826 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2827 struct ust_app_channel
*ua_chan
)
2830 struct buffer_reg_uid
*reg_uid
;
2831 struct buffer_reg_channel
*reg_chan
;
2838 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2840 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2842 * The session creation handles the creation of this global registry
2843 * object. If none can be find, there is a code flow problem or a
2848 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2851 /* Create the buffer registry channel object. */
2852 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2854 ERR("Error creating the UST channel \"%s\" registry instance",
2861 * Create the buffers on the consumer side. This call populates the
2862 * ust app channel object with all streams and data object.
2864 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2865 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2867 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2871 * Let's remove the previously created buffer registry channel so
2872 * it's not visible anymore in the session registry.
2874 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2875 ua_chan
->tracing_channel_id
);
2876 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2877 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2882 * Setup the streams and add it to the session registry.
2884 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2885 ua_chan
, reg_chan
, app
);
2887 ERR("Error setting up UST channel \"%s\"",
2894 /* Send buffers to the application. */
2895 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2897 if (ret
!= -ENOTCONN
) {
2898 ERR("Error sending channel to application");
2908 * Create and send to the application the created buffers with per PID buffers.
2910 * Return 0 on success else a negative value.
2912 static int create_channel_per_pid(struct ust_app
*app
,
2913 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2914 struct ust_app_channel
*ua_chan
)
2917 struct ust_registry_session
*registry
;
2924 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2928 registry
= get_session_registry(ua_sess
);
2931 /* Create and add a new channel registry to session. */
2932 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2934 ERR("Error creating the UST channel \"%s\" registry instance",
2939 /* Create and get channel on the consumer side. */
2940 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2941 app
->bits_per_long
, registry
);
2943 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2948 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2950 if (ret
!= -ENOTCONN
) {
2951 ERR("Error sending channel to application");
2962 * From an already allocated ust app channel, create the channel buffers if
2963 * need and send it to the application. This MUST be called with a RCU read
2964 * side lock acquired.
2966 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2967 * the application exited concurrently.
2969 static int do_create_channel(struct ust_app
*app
,
2970 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2971 struct ust_app_channel
*ua_chan
)
2980 /* Handle buffer type before sending the channel to the application. */
2981 switch (usess
->buffer_type
) {
2982 case LTTNG_BUFFER_PER_UID
:
2984 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
2990 case LTTNG_BUFFER_PER_PID
:
2992 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3004 /* Initialize ust objd object using the received handle and add it. */
3005 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3006 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3008 /* If channel is not enabled, disable it on the tracer */
3009 if (!ua_chan
->enabled
) {
3010 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3021 * Create UST app channel and create it on the tracer. Set ua_chanp of the
3022 * newly created channel if not NULL.
3024 * Called with UST app session lock and RCU read-side lock held.
3026 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3027 * the application exited concurrently.
3029 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
3030 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
3031 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3032 struct ust_app_channel
**ua_chanp
)
3035 struct lttng_ht_iter iter
;
3036 struct lttng_ht_node_str
*ua_chan_node
;
3037 struct ust_app_channel
*ua_chan
;
3039 /* Lookup channel in the ust app session */
3040 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3041 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3042 if (ua_chan_node
!= NULL
) {
3043 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3047 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3048 if (ua_chan
== NULL
) {
3049 /* Only malloc can fail here */
3053 shadow_copy_channel(ua_chan
, uchan
);
3055 /* Set channel type. */
3056 ua_chan
->attr
.type
= type
;
3058 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
3063 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
3066 /* Only add the channel if successful on the tracer side. */
3067 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3071 *ua_chanp
= ua_chan
;
3074 /* Everything went well. */
3078 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
3084 * Create UST app event and create it on the tracer side.
3086 * Called with ust app session mutex held.
3089 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3090 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3091 struct ust_app
*app
)
3094 struct ust_app_event
*ua_event
;
3096 /* Get event node */
3097 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3098 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3099 if (ua_event
!= NULL
) {
3104 /* Does not exist so create one */
3105 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3106 if (ua_event
== NULL
) {
3107 /* Only malloc can failed so something is really wrong */
3111 shadow_copy_event(ua_event
, uevent
);
3113 /* Create it on the tracer side */
3114 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3116 /* Not found previously means that it does not exist on the tracer */
3117 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
3121 add_unique_ust_app_event(ua_chan
, ua_event
);
3123 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3130 /* Valid. Calling here is already in a read side lock */
3131 delete_ust_app_event(-1, ua_event
, app
);
3136 * Create UST metadata and open it on the tracer side.
3138 * Called with UST app session lock held and RCU read side lock.
3140 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3141 struct ust_app
*app
, struct consumer_output
*consumer
)
3144 struct ust_app_channel
*metadata
;
3145 struct consumer_socket
*socket
;
3146 struct ust_registry_session
*registry
;
3152 registry
= get_session_registry(ua_sess
);
3155 pthread_mutex_lock(®istry
->lock
);
3157 /* Metadata already exists for this registry or it was closed previously */
3158 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3163 /* Allocate UST metadata */
3164 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3166 /* malloc() failed */
3171 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3173 /* Need one fd for the channel. */
3174 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3176 ERR("Exhausted number of available FD upon create metadata");
3180 /* Get the right consumer socket for the application. */
3181 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3184 goto error_consumer
;
3188 * Keep metadata key so we can identify it on the consumer side. Assign it
3189 * to the registry *before* we ask the consumer so we avoid the race of the
3190 * consumer requesting the metadata and the ask_channel call on our side
3191 * did not returned yet.
3193 registry
->metadata_key
= metadata
->key
;
3196 * Ask the metadata channel creation to the consumer. The metadata object
3197 * will be created by the consumer and kept their. However, the stream is
3198 * never added or monitored until we do a first push metadata to the
3201 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3204 /* Nullify the metadata key so we don't try to close it later on. */
3205 registry
->metadata_key
= 0;
3206 goto error_consumer
;
3210 * The setup command will make the metadata stream be sent to the relayd,
3211 * if applicable, and the thread managing the metadatas. This is important
3212 * because after this point, if an error occurs, the only way the stream
3213 * can be deleted is to be monitored in the consumer.
3215 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3217 /* Nullify the metadata key so we don't try to close it later on. */
3218 registry
->metadata_key
= 0;
3219 goto error_consumer
;
3222 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3223 metadata
->key
, app
->pid
);
3226 lttng_fd_put(LTTNG_FD_APPS
, 1);
3227 delete_ust_app_channel(-1, metadata
, app
);
3229 pthread_mutex_unlock(®istry
->lock
);
3234 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3235 * acquired before calling this function.
3237 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3239 struct ust_app
*app
= NULL
;
3240 struct lttng_ht_node_ulong
*node
;
3241 struct lttng_ht_iter iter
;
3243 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3244 node
= lttng_ht_iter_get_node_ulong(&iter
);
3246 DBG2("UST app no found with pid %d", pid
);
3250 DBG2("Found UST app by pid %d", pid
);
3252 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3259 * Allocate and init an UST app object using the registration information and
3260 * the command socket. This is called when the command socket connects to the
3263 * The object is returned on success or else NULL.
3265 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3267 struct ust_app
*lta
= NULL
;
3272 DBG3("UST app creating application for socket %d", sock
);
3274 if ((msg
->bits_per_long
== 64 &&
3275 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3276 || (msg
->bits_per_long
== 32 &&
3277 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3278 ERR("Registration failed: application \"%s\" (pid: %d) has "
3279 "%d-bit long, but no consumerd for this size is available.\n",
3280 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3284 lta
= zmalloc(sizeof(struct ust_app
));
3290 lta
->ppid
= msg
->ppid
;
3291 lta
->uid
= msg
->uid
;
3292 lta
->gid
= msg
->gid
;
3294 lta
->bits_per_long
= msg
->bits_per_long
;
3295 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3296 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3297 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3298 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3299 lta
->long_alignment
= msg
->long_alignment
;
3300 lta
->byte_order
= msg
->byte_order
;
3302 lta
->v_major
= msg
->major
;
3303 lta
->v_minor
= msg
->minor
;
3304 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3305 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3306 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3307 lta
->notify_sock
= -1;
3309 /* Copy name and make sure it's NULL terminated. */
3310 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3311 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3314 * Before this can be called, when receiving the registration information,
3315 * the application compatibility is checked. So, at this point, the
3316 * application can work with this session daemon.
3318 lta
->compatible
= 1;
3320 lta
->pid
= msg
->pid
;
3321 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3323 pthread_mutex_init(<a
->sock_lock
, NULL
);
3324 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3326 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3332 * For a given application object, add it to every hash table.
3334 void ust_app_add(struct ust_app
*app
)
3337 assert(app
->notify_sock
>= 0);
3342 * On a re-registration, we want to kick out the previous registration of
3345 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3348 * The socket _should_ be unique until _we_ call close. So, a add_unique
3349 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3350 * already in the table.
3352 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3354 /* Add application to the notify socket hash table. */
3355 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3356 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3358 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3359 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3360 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3367 * Set the application version into the object.
3369 * Return 0 on success else a negative value either an errno code or a
3370 * LTTng-UST error code.
3372 int ust_app_version(struct ust_app
*app
)
3378 pthread_mutex_lock(&app
->sock_lock
);
3379 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3380 pthread_mutex_unlock(&app
->sock_lock
);
3382 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3383 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3385 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3393 * Unregister app by removing it from the global traceable app list and freeing
3396 * The socket is already closed at this point so no close to sock.
3398 void ust_app_unregister(int sock
)
3400 struct ust_app
*lta
;
3401 struct lttng_ht_node_ulong
*node
;
3402 struct lttng_ht_iter ust_app_sock_iter
;
3403 struct lttng_ht_iter iter
;
3404 struct ust_app_session
*ua_sess
;
3409 /* Get the node reference for a call_rcu */
3410 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3411 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3414 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3415 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3418 * For per-PID buffers, perform "push metadata" and flush all
3419 * application streams before removing app from hash tables,
3420 * ensuring proper behavior of data_pending check.
3421 * Remove sessions so they are not visible during deletion.
3423 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3425 struct ust_registry_session
*registry
;
3427 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3429 /* The session was already removed so scheduled for teardown. */
3433 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3434 (void) ust_app_flush_app_session(lta
, ua_sess
);
3438 * Add session to list for teardown. This is safe since at this point we
3439 * are the only one using this list.
3441 pthread_mutex_lock(&ua_sess
->lock
);
3443 if (ua_sess
->deleted
) {
3444 pthread_mutex_unlock(&ua_sess
->lock
);
3449 * Normally, this is done in the delete session process which is
3450 * executed in the call rcu below. However, upon registration we can't
3451 * afford to wait for the grace period before pushing data or else the
3452 * data pending feature can race between the unregistration and stop
3453 * command where the data pending command is sent *before* the grace
3456 * The close metadata below nullifies the metadata pointer in the
3457 * session so the delete session will NOT push/close a second time.
3459 registry
= get_session_registry(ua_sess
);
3461 /* Push metadata for application before freeing the application. */
3462 (void) push_metadata(registry
, ua_sess
->consumer
);
3465 * Don't ask to close metadata for global per UID buffers. Close
3466 * metadata only on destroy trace session in this case. Also, the
3467 * previous push metadata could have flag the metadata registry to
3468 * close so don't send a close command if closed.
3470 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3471 /* And ask to close it for this session registry. */
3472 (void) close_metadata(registry
, ua_sess
->consumer
);
3475 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3477 pthread_mutex_unlock(&ua_sess
->lock
);
3480 /* Remove application from PID hash table */
3481 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3485 * Remove application from notify hash table. The thread handling the
3486 * notify socket could have deleted the node so ignore on error because
3487 * either way it's valid. The close of that socket is handled by the other
3490 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3491 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3494 * Ignore return value since the node might have been removed before by an
3495 * add replace during app registration because the PID can be reassigned by
3498 iter
.iter
.node
= <a
->pid_n
.node
;
3499 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3501 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3506 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3513 * Fill events array with all events name of all registered apps.
3515 int ust_app_list_events(struct lttng_event
**events
)
3518 size_t nbmem
, count
= 0;
3519 struct lttng_ht_iter iter
;
3520 struct ust_app
*app
;
3521 struct lttng_event
*tmp_event
;
3523 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3524 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3525 if (tmp_event
== NULL
) {
3526 PERROR("zmalloc ust app events");
3533 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3534 struct lttng_ust_tracepoint_iter uiter
;
3536 health_code_update();
3538 if (!app
->compatible
) {
3540 * TODO: In time, we should notice the caller of this error by
3541 * telling him that this is a version error.
3545 pthread_mutex_lock(&app
->sock_lock
);
3546 handle
= ustctl_tracepoint_list(app
->sock
);
3548 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3549 ERR("UST app list events getting handle failed for app pid %d",
3552 pthread_mutex_unlock(&app
->sock_lock
);
3556 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3557 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3558 /* Handle ustctl error. */
3562 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3563 ERR("UST app tp list get failed for app %d with ret %d",
3566 DBG3("UST app tp list get failed. Application is dead");
3568 * This is normal behavior, an application can die during the
3569 * creation process. Don't report an error so the execution can
3570 * continue normally. Continue normal execution.
3575 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3576 if (release_ret
< 0 &&
3577 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3578 release_ret
!= -EPIPE
) {
3579 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3581 pthread_mutex_unlock(&app
->sock_lock
);
3585 health_code_update();
3586 if (count
>= nbmem
) {
3587 /* In case the realloc fails, we free the memory */
3588 struct lttng_event
*new_tmp_event
;
3591 new_nbmem
= nbmem
<< 1;
3592 DBG2("Reallocating event list from %zu to %zu entries",
3594 new_tmp_event
= realloc(tmp_event
,
3595 new_nbmem
* sizeof(struct lttng_event
));
3596 if (new_tmp_event
== NULL
) {
3599 PERROR("realloc ust app events");
3602 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3603 if (release_ret
< 0 &&
3604 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3605 release_ret
!= -EPIPE
) {
3606 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3608 pthread_mutex_unlock(&app
->sock_lock
);
3611 /* Zero the new memory */
3612 memset(new_tmp_event
+ nbmem
, 0,
3613 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3615 tmp_event
= new_tmp_event
;
3617 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3618 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3619 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3620 tmp_event
[count
].pid
= app
->pid
;
3621 tmp_event
[count
].enabled
= -1;
3624 ret
= ustctl_release_handle(app
->sock
, handle
);
3625 pthread_mutex_unlock(&app
->sock_lock
);
3626 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3627 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3632 *events
= tmp_event
;
3634 DBG2("UST app list events done (%zu events)", count
);
3639 health_code_update();
3644 * Fill events array with all events name of all registered apps.
3646 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3649 size_t nbmem
, count
= 0;
3650 struct lttng_ht_iter iter
;
3651 struct ust_app
*app
;
3652 struct lttng_event_field
*tmp_event
;
3654 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3655 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3656 if (tmp_event
== NULL
) {
3657 PERROR("zmalloc ust app event fields");
3664 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3665 struct lttng_ust_field_iter uiter
;
3667 health_code_update();
3669 if (!app
->compatible
) {
3671 * TODO: In time, we should notice the caller of this error by
3672 * telling him that this is a version error.
3676 pthread_mutex_lock(&app
->sock_lock
);
3677 handle
= ustctl_tracepoint_field_list(app
->sock
);
3679 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3680 ERR("UST app list field getting handle failed for app pid %d",
3683 pthread_mutex_unlock(&app
->sock_lock
);
3687 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3688 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3689 /* Handle ustctl error. */
3693 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3694 ERR("UST app tp list field failed for app %d with ret %d",
3697 DBG3("UST app tp list field failed. Application is dead");
3699 * This is normal behavior, an application can die during the
3700 * creation process. Don't report an error so the execution can
3701 * continue normally. Reset list and count for next app.
3706 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3707 pthread_mutex_unlock(&app
->sock_lock
);
3708 if (release_ret
< 0 &&
3709 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3710 release_ret
!= -EPIPE
) {
3711 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3716 health_code_update();
3717 if (count
>= nbmem
) {
3718 /* In case the realloc fails, we free the memory */
3719 struct lttng_event_field
*new_tmp_event
;
3722 new_nbmem
= nbmem
<< 1;
3723 DBG2("Reallocating event field list from %zu to %zu entries",
3725 new_tmp_event
= realloc(tmp_event
,
3726 new_nbmem
* sizeof(struct lttng_event_field
));
3727 if (new_tmp_event
== NULL
) {
3730 PERROR("realloc ust app event fields");
3733 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3734 pthread_mutex_unlock(&app
->sock_lock
);
3736 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3737 release_ret
!= -EPIPE
) {
3738 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3742 /* Zero the new memory */
3743 memset(new_tmp_event
+ nbmem
, 0,
3744 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3746 tmp_event
= new_tmp_event
;
3749 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3750 /* Mapping between these enums matches 1 to 1. */
3751 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3752 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3754 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3755 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3756 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3757 tmp_event
[count
].event
.pid
= app
->pid
;
3758 tmp_event
[count
].event
.enabled
= -1;
3761 ret
= ustctl_release_handle(app
->sock
, handle
);
3762 pthread_mutex_unlock(&app
->sock_lock
);
3764 ret
!= -LTTNG_UST_ERR_EXITING
&&
3766 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3771 *fields
= tmp_event
;
3773 DBG2("UST app list event fields done (%zu events)", count
);
3778 health_code_update();
3783 * Free and clean all traceable apps of the global list.
3785 * Should _NOT_ be called with RCU read-side lock held.
3787 void ust_app_clean_list(void)
3790 struct ust_app
*app
;
3791 struct lttng_ht_iter iter
;
3793 DBG2("UST app cleaning registered apps hash table");
3798 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3799 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3801 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3805 /* Cleanup socket hash table */
3806 if (ust_app_ht_by_sock
) {
3807 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3809 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3814 /* Cleanup notify socket hash table */
3815 if (ust_app_ht_by_notify_sock
) {
3816 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3817 notify_sock_n
.node
) {
3818 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3824 /* Destroy is done only when the ht is empty */
3826 ht_cleanup_push(ust_app_ht
);
3828 if (ust_app_ht_by_sock
) {
3829 ht_cleanup_push(ust_app_ht_by_sock
);
3831 if (ust_app_ht_by_notify_sock
) {
3832 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3837 * Init UST app hash table.
3839 int ust_app_ht_alloc(void)
3841 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3845 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3846 if (!ust_app_ht_by_sock
) {
3849 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3850 if (!ust_app_ht_by_notify_sock
) {
3857 * For a specific UST session, disable the channel for all registered apps.
3859 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3860 struct ltt_ust_channel
*uchan
)
3863 struct lttng_ht_iter iter
;
3864 struct lttng_ht_node_str
*ua_chan_node
;
3865 struct ust_app
*app
;
3866 struct ust_app_session
*ua_sess
;
3867 struct ust_app_channel
*ua_chan
;
3869 if (usess
== NULL
|| uchan
== NULL
) {
3870 ERR("Disabling UST global channel with NULL values");
3875 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3876 uchan
->name
, usess
->id
);
3880 /* For every registered applications */
3881 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3882 struct lttng_ht_iter uiter
;
3883 if (!app
->compatible
) {
3885 * TODO: In time, we should notice the caller of this error by
3886 * telling him that this is a version error.
3890 ua_sess
= lookup_session_by_app(usess
, app
);
3891 if (ua_sess
== NULL
) {
3896 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3897 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3898 /* If the session if found for the app, the channel must be there */
3899 assert(ua_chan_node
);
3901 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3902 /* The channel must not be already disabled */
3903 assert(ua_chan
->enabled
== 1);
3905 /* Disable channel onto application */
3906 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3908 /* XXX: We might want to report this error at some point... */
3920 * For a specific UST session, enable the channel for all registered apps.
3922 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3923 struct ltt_ust_channel
*uchan
)
3926 struct lttng_ht_iter iter
;
3927 struct ust_app
*app
;
3928 struct ust_app_session
*ua_sess
;
3930 if (usess
== NULL
|| uchan
== NULL
) {
3931 ERR("Adding UST global channel to NULL values");
3936 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3937 uchan
->name
, usess
->id
);
3941 /* For every registered applications */
3942 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3943 if (!app
->compatible
) {
3945 * TODO: In time, we should notice the caller of this error by
3946 * telling him that this is a version error.
3950 ua_sess
= lookup_session_by_app(usess
, app
);
3951 if (ua_sess
== NULL
) {
3955 /* Enable channel onto application */
3956 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3958 /* XXX: We might want to report this error at some point... */
3970 * Disable an event in a channel and for a specific session.
3972 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3973 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3976 struct lttng_ht_iter iter
, uiter
;
3977 struct lttng_ht_node_str
*ua_chan_node
;
3978 struct ust_app
*app
;
3979 struct ust_app_session
*ua_sess
;
3980 struct ust_app_channel
*ua_chan
;
3981 struct ust_app_event
*ua_event
;
3983 DBG("UST app disabling event %s for all apps in channel "
3984 "%s for session id %" PRIu64
,
3985 uevent
->attr
.name
, uchan
->name
, usess
->id
);
3989 /* For all registered applications */
3990 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3991 if (!app
->compatible
) {
3993 * TODO: In time, we should notice the caller of this error by
3994 * telling him that this is a version error.
3998 ua_sess
= lookup_session_by_app(usess
, app
);
3999 if (ua_sess
== NULL
) {
4004 /* Lookup channel in the ust app session */
4005 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4006 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4007 if (ua_chan_node
== NULL
) {
4008 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4009 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4012 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4014 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4015 uevent
->filter
, uevent
->attr
.loglevel
,
4017 if (ua_event
== NULL
) {
4018 DBG2("Event %s not found in channel %s for app pid %d."
4019 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4023 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4025 /* XXX: Report error someday... */
4036 * For a specific UST session, create the channel for all registered apps.
4038 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
4039 struct ltt_ust_channel
*uchan
)
4041 int ret
= 0, created
;
4042 struct lttng_ht_iter iter
;
4043 struct ust_app
*app
;
4044 struct ust_app_session
*ua_sess
= NULL
;
4046 /* Very wrong code flow */
4050 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
4051 uchan
->name
, usess
->id
);
4055 /* For every registered applications */
4056 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4057 if (!app
->compatible
) {
4059 * TODO: In time, we should notice the caller of this error by
4060 * telling him that this is a version error.
4064 if (!trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4070 * Create session on the tracer side and add it to app session HT. Note
4071 * that if session exist, it will simply return a pointer to the ust
4074 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
4079 * The application's socket is not valid. Either a bad socket
4080 * or a timeout on it. We can't inform the caller that for a
4081 * specific app, the session failed so lets continue here.
4083 ret
= 0; /* Not an error. */
4087 goto error_rcu_unlock
;
4092 pthread_mutex_lock(&ua_sess
->lock
);
4094 if (ua_sess
->deleted
) {
4095 pthread_mutex_unlock(&ua_sess
->lock
);
4099 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4100 sizeof(uchan
->name
))) {
4101 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
4104 /* Create channel onto application. We don't need the chan ref. */
4105 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
4106 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
4108 pthread_mutex_unlock(&ua_sess
->lock
);
4110 /* Cleanup the created session if it's the case. */
4112 destroy_app_session(app
, ua_sess
);
4117 * The application's socket is not valid. Either a bad socket
4118 * or a timeout on it. We can't inform the caller that for a
4119 * specific app, the session failed so lets continue here.
4121 ret
= 0; /* Not an error. */
4125 goto error_rcu_unlock
;
4136 * Enable event for a specific session and channel on the tracer.
4138 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4139 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4142 struct lttng_ht_iter iter
, uiter
;
4143 struct lttng_ht_node_str
*ua_chan_node
;
4144 struct ust_app
*app
;
4145 struct ust_app_session
*ua_sess
;
4146 struct ust_app_channel
*ua_chan
;
4147 struct ust_app_event
*ua_event
;
4149 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4150 uevent
->attr
.name
, usess
->id
);
4153 * NOTE: At this point, this function is called only if the session and
4154 * channel passed are already created for all apps. and enabled on the
4160 /* For all registered applications */
4161 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4162 if (!app
->compatible
) {
4164 * TODO: In time, we should notice the caller of this error by
4165 * telling him that this is a version error.
4169 ua_sess
= lookup_session_by_app(usess
, app
);
4171 /* The application has problem or is probably dead. */
4175 pthread_mutex_lock(&ua_sess
->lock
);
4177 if (ua_sess
->deleted
) {
4178 pthread_mutex_unlock(&ua_sess
->lock
);
4182 /* Lookup channel in the ust app session */
4183 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4184 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4186 * It is possible that the channel cannot be found is
4187 * the channel/event creation occurs concurrently with
4188 * an application exit.
4190 if (!ua_chan_node
) {
4191 pthread_mutex_unlock(&ua_sess
->lock
);
4195 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4197 /* Get event node */
4198 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4199 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4200 if (ua_event
== NULL
) {
4201 DBG3("UST app enable event %s not found for app PID %d."
4202 "Skipping app", uevent
->attr
.name
, app
->pid
);
4206 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4208 pthread_mutex_unlock(&ua_sess
->lock
);
4212 pthread_mutex_unlock(&ua_sess
->lock
);
4221 * For a specific existing UST session and UST channel, creates the event for
4222 * all registered apps.
4224 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4225 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4228 struct lttng_ht_iter iter
, uiter
;
4229 struct lttng_ht_node_str
*ua_chan_node
;
4230 struct ust_app
*app
;
4231 struct ust_app_session
*ua_sess
;
4232 struct ust_app_channel
*ua_chan
;
4234 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4235 uevent
->attr
.name
, usess
->id
);
4239 /* For all registered applications */
4240 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4241 if (!app
->compatible
) {
4243 * TODO: In time, we should notice the caller of this error by
4244 * telling him that this is a version error.
4248 ua_sess
= lookup_session_by_app(usess
, app
);
4250 /* The application has problem or is probably dead. */
4254 pthread_mutex_lock(&ua_sess
->lock
);
4256 if (ua_sess
->deleted
) {
4257 pthread_mutex_unlock(&ua_sess
->lock
);
4261 /* Lookup channel in the ust app session */
4262 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4263 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4264 /* If the channel is not found, there is a code flow error */
4265 assert(ua_chan_node
);
4267 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4269 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4270 pthread_mutex_unlock(&ua_sess
->lock
);
4272 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4273 /* Possible value at this point: -ENOMEM. If so, we stop! */
4276 DBG2("UST app event %s already exist on app PID %d",
4277 uevent
->attr
.name
, app
->pid
);
4288 * Start tracing for a specific UST session and app.
4291 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4294 struct ust_app_session
*ua_sess
;
4296 DBG("Starting tracing for ust app pid %d", app
->pid
);
4300 if (!app
->compatible
) {
4304 ua_sess
= lookup_session_by_app(usess
, app
);
4305 if (ua_sess
== NULL
) {
4306 /* The session is in teardown process. Ignore and continue. */
4310 pthread_mutex_lock(&ua_sess
->lock
);
4312 if (ua_sess
->deleted
) {
4313 pthread_mutex_unlock(&ua_sess
->lock
);
4317 /* Upon restart, we skip the setup, already done */
4318 if (ua_sess
->started
) {
4322 /* Create directories if consumer is LOCAL and has a path defined. */
4323 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
4324 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
4325 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
4326 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
4328 if (errno
!= EEXIST
) {
4329 ERR("Trace directory creation error");
4336 * Create the metadata for the application. This returns gracefully if a
4337 * metadata was already set for the session.
4339 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4344 health_code_update();
4347 /* This start the UST tracing */
4348 pthread_mutex_lock(&app
->sock_lock
);
4349 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4350 pthread_mutex_unlock(&app
->sock_lock
);
4352 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4353 ERR("Error starting tracing for app pid: %d (ret: %d)",
4356 DBG("UST app start session failed. Application is dead.");
4358 * This is normal behavior, an application can die during the
4359 * creation process. Don't report an error so the execution can
4360 * continue normally.
4362 pthread_mutex_unlock(&ua_sess
->lock
);
4368 /* Indicate that the session has been started once */
4369 ua_sess
->started
= 1;
4371 pthread_mutex_unlock(&ua_sess
->lock
);
4373 health_code_update();
4375 /* Quiescent wait after starting trace */
4376 pthread_mutex_lock(&app
->sock_lock
);
4377 ret
= ustctl_wait_quiescent(app
->sock
);
4378 pthread_mutex_unlock(&app
->sock_lock
);
4379 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4380 ERR("UST app wait quiescent failed for app pid %d ret %d",
4386 health_code_update();
4390 pthread_mutex_unlock(&ua_sess
->lock
);
4392 health_code_update();
4397 * Stop tracing for a specific UST session and app.
4400 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4403 struct ust_app_session
*ua_sess
;
4404 struct ust_registry_session
*registry
;
4406 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4410 if (!app
->compatible
) {
4411 goto end_no_session
;
4414 ua_sess
= lookup_session_by_app(usess
, app
);
4415 if (ua_sess
== NULL
) {
4416 goto end_no_session
;
4419 pthread_mutex_lock(&ua_sess
->lock
);
4421 if (ua_sess
->deleted
) {
4422 pthread_mutex_unlock(&ua_sess
->lock
);
4423 goto end_no_session
;
4427 * If started = 0, it means that stop trace has been called for a session
4428 * that was never started. It's possible since we can have a fail start
4429 * from either the application manager thread or the command thread. Simply
4430 * indicate that this is a stop error.
4432 if (!ua_sess
->started
) {
4433 goto error_rcu_unlock
;
4436 health_code_update();
4438 /* This inhibits UST tracing */
4439 pthread_mutex_lock(&app
->sock_lock
);
4440 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4441 pthread_mutex_unlock(&app
->sock_lock
);
4443 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4444 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4447 DBG("UST app stop session failed. Application is dead.");
4449 * This is normal behavior, an application can die during the
4450 * creation process. Don't report an error so the execution can
4451 * continue normally.
4455 goto error_rcu_unlock
;
4458 health_code_update();
4460 /* Quiescent wait after stopping trace */
4461 pthread_mutex_lock(&app
->sock_lock
);
4462 ret
= ustctl_wait_quiescent(app
->sock
);
4463 pthread_mutex_unlock(&app
->sock_lock
);
4464 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4465 ERR("UST app wait quiescent failed for app pid %d ret %d",
4469 health_code_update();
4471 registry
= get_session_registry(ua_sess
);
4474 /* Push metadata for application before freeing the application. */
4475 (void) push_metadata(registry
, ua_sess
->consumer
);
4478 pthread_mutex_unlock(&ua_sess
->lock
);
4481 health_code_update();
4485 pthread_mutex_unlock(&ua_sess
->lock
);
4487 health_code_update();
4492 int ust_app_flush_app_session(struct ust_app
*app
,
4493 struct ust_app_session
*ua_sess
)
4495 int ret
, retval
= 0;
4496 struct lttng_ht_iter iter
;
4497 struct ust_app_channel
*ua_chan
;
4498 struct consumer_socket
*socket
;
4500 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4504 if (!app
->compatible
) {
4505 goto end_not_compatible
;
4508 pthread_mutex_lock(&ua_sess
->lock
);
4510 if (ua_sess
->deleted
) {
4514 health_code_update();
4516 /* Flushing buffers */
4517 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4520 /* Flush buffers and push metadata. */
4521 switch (ua_sess
->buffer_type
) {
4522 case LTTNG_BUFFER_PER_PID
:
4523 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4525 health_code_update();
4526 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4528 ERR("Error flushing consumer channel");
4534 case LTTNG_BUFFER_PER_UID
:
4540 health_code_update();
4543 pthread_mutex_unlock(&ua_sess
->lock
);
4547 health_code_update();
4552 * Flush buffers for all applications for a specific UST session.
4553 * Called with UST session lock held.
4556 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4561 DBG("Flushing session buffers for all ust apps");
4565 /* Flush buffers and push metadata. */
4566 switch (usess
->buffer_type
) {
4567 case LTTNG_BUFFER_PER_UID
:
4569 struct buffer_reg_uid
*reg
;
4570 struct lttng_ht_iter iter
;
4572 /* Flush all per UID buffers associated to that session. */
4573 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4574 struct ust_registry_session
*ust_session_reg
;
4575 struct buffer_reg_channel
*reg_chan
;
4576 struct consumer_socket
*socket
;
4578 /* Get consumer socket to use to push the metadata.*/
4579 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4582 /* Ignore request if no consumer is found for the session. */
4586 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4587 reg_chan
, node
.node
) {
4589 * The following call will print error values so the return
4590 * code is of little importance because whatever happens, we
4591 * have to try them all.
4593 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4596 ust_session_reg
= reg
->registry
->reg
.ust
;
4597 /* Push metadata. */
4598 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4602 case LTTNG_BUFFER_PER_PID
:
4604 struct ust_app_session
*ua_sess
;
4605 struct lttng_ht_iter iter
;
4606 struct ust_app
*app
;
4608 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4609 ua_sess
= lookup_session_by_app(usess
, app
);
4610 if (ua_sess
== NULL
) {
4613 (void) ust_app_flush_app_session(app
, ua_sess
);
4624 health_code_update();
4629 * Destroy a specific UST session in apps.
4631 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4634 struct ust_app_session
*ua_sess
;
4635 struct lttng_ht_iter iter
;
4636 struct lttng_ht_node_u64
*node
;
4638 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4642 if (!app
->compatible
) {
4646 __lookup_session_by_app(usess
, app
, &iter
);
4647 node
= lttng_ht_iter_get_node_u64(&iter
);
4649 /* Session is being or is deleted. */
4652 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4654 health_code_update();
4655 destroy_app_session(app
, ua_sess
);
4657 health_code_update();
4659 /* Quiescent wait after stopping trace */
4660 pthread_mutex_lock(&app
->sock_lock
);
4661 ret
= ustctl_wait_quiescent(app
->sock
);
4662 pthread_mutex_unlock(&app
->sock_lock
);
4663 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4664 ERR("UST app wait quiescent failed for app pid %d ret %d",
4669 health_code_update();
4674 * Start tracing for the UST session.
4676 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4679 struct lttng_ht_iter iter
;
4680 struct ust_app
*app
;
4682 DBG("Starting all UST traces");
4686 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4687 ret
= ust_app_start_trace(usess
, app
);
4689 /* Continue to next apps even on error */
4700 * Start tracing for the UST session.
4701 * Called with UST session lock held.
4703 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4706 struct lttng_ht_iter iter
;
4707 struct ust_app
*app
;
4709 DBG("Stopping all UST traces");
4713 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4714 ret
= ust_app_stop_trace(usess
, app
);
4716 /* Continue to next apps even on error */
4721 (void) ust_app_flush_session(usess
);
4729 * Destroy app UST session.
4731 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4734 struct lttng_ht_iter iter
;
4735 struct ust_app
*app
;
4737 DBG("Destroy all UST traces");
4741 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4742 ret
= destroy_trace(usess
, app
);
4744 /* Continue to next apps even on error */
4755 void ust_app_global_create(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4758 struct lttng_ht_iter iter
, uiter
;
4759 struct ust_app_session
*ua_sess
= NULL
;
4760 struct ust_app_channel
*ua_chan
;
4761 struct ust_app_event
*ua_event
;
4762 struct ust_app_ctx
*ua_ctx
;
4765 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &is_created
);
4767 /* Tracer is probably gone or ENOMEM. */
4771 /* App session already created. */
4776 pthread_mutex_lock(&ua_sess
->lock
);
4778 if (ua_sess
->deleted
) {
4779 pthread_mutex_unlock(&ua_sess
->lock
);
4784 * We can iterate safely here over all UST app session since the create ust
4785 * app session above made a shadow copy of the UST global domain from the
4788 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4790 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4791 if (ret
< 0 && ret
!= -ENOTCONN
) {
4793 * Stop everything. On error, the application
4794 * failed, no more file descriptor are available
4795 * or ENOMEM so stopping here is the only thing
4796 * we can do for now. The only exception is
4797 * -ENOTCONN, which indicates that the application
4804 * Add context using the list so they are enabled in the same order the
4807 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
4808 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
4815 /* For each events */
4816 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
4818 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
4825 pthread_mutex_unlock(&ua_sess
->lock
);
4827 if (usess
->active
) {
4828 ret
= ust_app_start_trace(usess
, app
);
4833 DBG2("UST trace started for app pid %d", app
->pid
);
4836 /* Everything went well at this point. */
4840 pthread_mutex_unlock(&ua_sess
->lock
);
4843 destroy_app_session(app
, ua_sess
);
4849 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4851 struct ust_app_session
*ua_sess
;
4853 ua_sess
= lookup_session_by_app(usess
, app
);
4854 if (ua_sess
== NULL
) {
4857 destroy_app_session(app
, ua_sess
);
4861 * Add channels/events from UST global domain to registered apps at sock.
4863 * Called with session lock held.
4864 * Called with RCU read-side lock held.
4866 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4870 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
4871 app
->sock
, usess
->id
);
4873 if (!app
->compatible
) {
4877 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4878 ust_app_global_create(usess
, app
);
4880 ust_app_global_destroy(usess
, app
);
4885 * Called with session lock held.
4887 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
4889 struct lttng_ht_iter iter
;
4890 struct ust_app
*app
;
4893 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4894 ust_app_global_update(usess
, app
);
4900 * Add context to a specific channel for global UST domain.
4902 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
4903 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
4906 struct lttng_ht_node_str
*ua_chan_node
;
4907 struct lttng_ht_iter iter
, uiter
;
4908 struct ust_app_channel
*ua_chan
= NULL
;
4909 struct ust_app_session
*ua_sess
;
4910 struct ust_app
*app
;
4914 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4915 if (!app
->compatible
) {
4917 * TODO: In time, we should notice the caller of this error by
4918 * telling him that this is a version error.
4922 ua_sess
= lookup_session_by_app(usess
, app
);
4923 if (ua_sess
== NULL
) {
4927 pthread_mutex_lock(&ua_sess
->lock
);
4929 if (ua_sess
->deleted
) {
4930 pthread_mutex_unlock(&ua_sess
->lock
);
4934 /* Lookup channel in the ust app session */
4935 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4936 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4937 if (ua_chan_node
== NULL
) {
4940 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4942 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4947 pthread_mutex_unlock(&ua_sess
->lock
);
4955 * Enable event for a channel from a UST session for a specific PID.
4957 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4958 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4961 struct lttng_ht_iter iter
;
4962 struct lttng_ht_node_str
*ua_chan_node
;
4963 struct ust_app
*app
;
4964 struct ust_app_session
*ua_sess
;
4965 struct ust_app_channel
*ua_chan
;
4966 struct ust_app_event
*ua_event
;
4968 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4972 app
= ust_app_find_by_pid(pid
);
4974 ERR("UST app enable event per PID %d not found", pid
);
4979 if (!app
->compatible
) {
4984 ua_sess
= lookup_session_by_app(usess
, app
);
4986 /* The application has problem or is probably dead. */
4991 pthread_mutex_lock(&ua_sess
->lock
);
4993 if (ua_sess
->deleted
) {
4998 /* Lookup channel in the ust app session */
4999 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
5000 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5001 /* If the channel is not found, there is a code flow error */
5002 assert(ua_chan_node
);
5004 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5006 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5007 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5008 if (ua_event
== NULL
) {
5009 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5014 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
5021 pthread_mutex_unlock(&ua_sess
->lock
);
5028 * Calibrate registered applications.
5030 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
5033 struct lttng_ht_iter iter
;
5034 struct ust_app
*app
;
5038 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5039 if (!app
->compatible
) {
5041 * TODO: In time, we should notice the caller of this error by
5042 * telling him that this is a version error.
5047 health_code_update();
5049 pthread_mutex_lock(&app
->sock_lock
);
5050 ret
= ustctl_calibrate(app
->sock
, calibrate
);
5051 pthread_mutex_unlock(&app
->sock_lock
);
5055 /* Means that it's not implemented on the tracer side. */
5059 DBG2("Calibrate app PID %d returned with error %d",
5066 DBG("UST app global domain calibration finished");
5070 health_code_update();
5076 * Receive registration and populate the given msg structure.
5078 * On success return 0 else a negative value returned by the ustctl call.
5080 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5083 uint32_t pid
, ppid
, uid
, gid
;
5087 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5088 &pid
, &ppid
, &uid
, &gid
,
5089 &msg
->bits_per_long
,
5090 &msg
->uint8_t_alignment
,
5091 &msg
->uint16_t_alignment
,
5092 &msg
->uint32_t_alignment
,
5093 &msg
->uint64_t_alignment
,
5094 &msg
->long_alignment
,
5101 case LTTNG_UST_ERR_EXITING
:
5102 DBG3("UST app recv reg message failed. Application died");
5104 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5105 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5106 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5107 LTTNG_UST_ABI_MINOR_VERSION
);
5110 ERR("UST app recv reg message failed with ret %d", ret
);
5115 msg
->pid
= (pid_t
) pid
;
5116 msg
->ppid
= (pid_t
) ppid
;
5117 msg
->uid
= (uid_t
) uid
;
5118 msg
->gid
= (gid_t
) gid
;
5125 * Return a ust app session object using the application object and the
5126 * session object descriptor has a key. If not found, NULL is returned.
5127 * A RCU read side lock MUST be acquired when calling this function.
5129 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5132 struct lttng_ht_node_ulong
*node
;
5133 struct lttng_ht_iter iter
;
5134 struct ust_app_session
*ua_sess
= NULL
;
5138 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5139 node
= lttng_ht_iter_get_node_ulong(&iter
);
5141 DBG2("UST app session find by objd %d not found", objd
);
5145 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5152 * Return a ust app channel object using the application object and the channel
5153 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5154 * lock MUST be acquired before calling this function.
5156 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5159 struct lttng_ht_node_ulong
*node
;
5160 struct lttng_ht_iter iter
;
5161 struct ust_app_channel
*ua_chan
= NULL
;
5165 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5166 node
= lttng_ht_iter_get_node_ulong(&iter
);
5168 DBG2("UST app channel find by objd %d not found", objd
);
5172 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5179 * Reply to a register channel notification from an application on the notify
5180 * socket. The channel metadata is also created.
5182 * The session UST registry lock is acquired in this function.
5184 * On success 0 is returned else a negative value.
5186 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
5187 size_t nr_fields
, struct ustctl_field
*fields
)
5189 int ret
, ret_code
= 0;
5190 uint32_t chan_id
, reg_count
;
5191 uint64_t chan_reg_key
;
5192 enum ustctl_channel_header type
;
5193 struct ust_app
*app
;
5194 struct ust_app_channel
*ua_chan
;
5195 struct ust_app_session
*ua_sess
;
5196 struct ust_registry_session
*registry
;
5197 struct ust_registry_channel
*chan_reg
;
5201 /* Lookup application. If not found, there is a code flow error. */
5202 app
= find_app_by_notify_sock(sock
);
5204 DBG("Application socket %d is being teardown. Abort event notify",
5208 goto error_rcu_unlock
;
5211 /* Lookup channel by UST object descriptor. */
5212 ua_chan
= find_channel_by_objd(app
, cobjd
);
5214 DBG("Application channel is being teardown. Abort event notify");
5217 goto error_rcu_unlock
;
5220 assert(ua_chan
->session
);
5221 ua_sess
= ua_chan
->session
;
5223 /* Get right session registry depending on the session buffer type. */
5224 registry
= get_session_registry(ua_sess
);
5227 /* Depending on the buffer type, a different channel key is used. */
5228 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5229 chan_reg_key
= ua_chan
->tracing_channel_id
;
5231 chan_reg_key
= ua_chan
->key
;
5234 pthread_mutex_lock(®istry
->lock
);
5236 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5239 if (!chan_reg
->register_done
) {
5240 reg_count
= ust_registry_get_event_count(chan_reg
);
5241 if (reg_count
< 31) {
5242 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
5244 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5247 chan_reg
->nr_ctx_fields
= nr_fields
;
5248 chan_reg
->ctx_fields
= fields
;
5249 chan_reg
->header_type
= type
;
5251 /* Get current already assigned values. */
5252 type
= chan_reg
->header_type
;
5254 /* Set to NULL so the error path does not do a double free. */
5257 /* Channel id is set during the object creation. */
5258 chan_id
= chan_reg
->chan_id
;
5260 /* Append to metadata */
5261 if (!chan_reg
->metadata_dumped
) {
5262 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5264 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5270 DBG3("UST app replying to register channel key %" PRIu64
5271 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5274 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5276 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5277 ERR("UST app reply channel failed with ret %d", ret
);
5279 DBG3("UST app reply channel failed. Application died");
5284 /* This channel registry registration is completed. */
5285 chan_reg
->register_done
= 1;
5288 pthread_mutex_unlock(®istry
->lock
);
5298 * Add event to the UST channel registry. When the event is added to the
5299 * registry, the metadata is also created. Once done, this replies to the
5300 * application with the appropriate error code.
5302 * The session UST registry lock is acquired in the function.
5304 * On success 0 is returned else a negative value.
5306 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5307 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5308 int loglevel_value
, char *model_emf_uri
)
5311 uint32_t event_id
= 0;
5312 uint64_t chan_reg_key
;
5313 struct ust_app
*app
;
5314 struct ust_app_channel
*ua_chan
;
5315 struct ust_app_session
*ua_sess
;
5316 struct ust_registry_session
*registry
;
5320 /* Lookup application. If not found, there is a code flow error. */
5321 app
= find_app_by_notify_sock(sock
);
5323 DBG("Application socket %d is being teardown. Abort event notify",
5328 free(model_emf_uri
);
5329 goto error_rcu_unlock
;
5332 /* Lookup channel by UST object descriptor. */
5333 ua_chan
= find_channel_by_objd(app
, cobjd
);
5335 DBG("Application channel is being teardown. Abort event notify");
5339 free(model_emf_uri
);
5340 goto error_rcu_unlock
;
5343 assert(ua_chan
->session
);
5344 ua_sess
= ua_chan
->session
;
5346 registry
= get_session_registry(ua_sess
);
5349 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5350 chan_reg_key
= ua_chan
->tracing_channel_id
;
5352 chan_reg_key
= ua_chan
->key
;
5355 pthread_mutex_lock(®istry
->lock
);
5358 * From this point on, this call acquires the ownership of the sig, fields
5359 * and model_emf_uri meaning any free are done inside it if needed. These
5360 * three variables MUST NOT be read/write after this.
5362 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5363 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5364 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5368 * The return value is returned to ustctl so in case of an error, the
5369 * application can be notified. In case of an error, it's important not to
5370 * return a negative error or else the application will get closed.
5372 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5374 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5375 ERR("UST app reply event failed with ret %d", ret
);
5377 DBG3("UST app reply event failed. Application died");
5380 * No need to wipe the create event since the application socket will
5381 * get close on error hence cleaning up everything by itself.
5386 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5390 pthread_mutex_unlock(®istry
->lock
);
5397 * Add enum to the UST session registry. Once done, this replies to the
5398 * application with the appropriate error code.
5400 * The session UST registry lock is acquired within this function.
5402 * On success 0 is returned else a negative value.
5404 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
5405 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
5407 int ret
= 0, ret_code
;
5408 struct ust_app
*app
;
5409 struct ust_app_session
*ua_sess
;
5410 struct ust_registry_session
*registry
;
5411 uint64_t enum_id
= -1ULL;
5415 /* Lookup application. If not found, there is a code flow error. */
5416 app
= find_app_by_notify_sock(sock
);
5418 /* Return an error since this is not an error */
5419 DBG("Application socket %d is being torn down. Aborting enum registration",
5422 goto error_rcu_unlock
;
5425 /* Lookup session by UST object descriptor. */
5426 ua_sess
= find_session_by_objd(app
, sobjd
);
5428 /* Return an error since this is not an error */
5429 DBG("Application session is being torn down. Aborting enum registration.");
5431 goto error_rcu_unlock
;
5434 registry
= get_session_registry(ua_sess
);
5437 pthread_mutex_lock(®istry
->lock
);
5440 * From this point on, the callee acquires the ownership of
5441 * entries. The variable entries MUST NOT be read/written after
5444 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
5445 entries
, nr_entries
, &enum_id
);
5449 * The return value is returned to ustctl so in case of an error, the
5450 * application can be notified. In case of an error, it's important not to
5451 * return a negative error or else the application will get closed.
5453 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
5455 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5456 ERR("UST app reply enum failed with ret %d", ret
);
5458 DBG3("UST app reply enum failed. Application died");
5461 * No need to wipe the create enum since the application socket will
5462 * get close on error hence cleaning up everything by itself.
5467 DBG3("UST registry enum %s added successfully or already found", name
);
5470 pthread_mutex_unlock(®istry
->lock
);
5477 * Handle application notification through the given notify socket.
5479 * Return 0 on success or else a negative value.
5481 int ust_app_recv_notify(int sock
)
5484 enum ustctl_notify_cmd cmd
;
5486 DBG3("UST app receiving notify from sock %d", sock
);
5488 ret
= ustctl_recv_notify(sock
, &cmd
);
5490 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5491 ERR("UST app recv notify failed with ret %d", ret
);
5493 DBG3("UST app recv notify failed. Application died");
5499 case USTCTL_NOTIFY_CMD_EVENT
:
5501 int sobjd
, cobjd
, loglevel_value
;
5502 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5504 struct ustctl_field
*fields
;
5506 DBG2("UST app ustctl register event received");
5508 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5509 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5512 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5513 ERR("UST app recv event failed with ret %d", ret
);
5515 DBG3("UST app recv event failed. Application died");
5521 * Add event to the UST registry coming from the notify socket. This
5522 * call will free if needed the sig, fields and model_emf_uri. This
5523 * code path loses the ownsership of these variables and transfer them
5524 * to the this function.
5526 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5527 fields
, loglevel_value
, model_emf_uri
);
5534 case USTCTL_NOTIFY_CMD_CHANNEL
:
5538 struct ustctl_field
*fields
;
5540 DBG2("UST app ustctl register channel received");
5542 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5545 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5546 ERR("UST app recv channel failed with ret %d", ret
);
5548 DBG3("UST app recv channel failed. Application died");
5554 * The fields ownership are transfered to this function call meaning
5555 * that if needed it will be freed. After this, it's invalid to access
5556 * fields or clean it up.
5558 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
5566 case USTCTL_NOTIFY_CMD_ENUM
:
5569 char name
[LTTNG_UST_SYM_NAME_LEN
];
5571 struct ustctl_enum_entry
*entries
;
5573 DBG2("UST app ustctl register enum received");
5575 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
5576 &entries
, &nr_entries
);
5578 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5579 ERR("UST app recv enum failed with ret %d", ret
);
5581 DBG3("UST app recv enum failed. Application died");
5586 /* Callee assumes ownership of entries */
5587 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
5588 entries
, nr_entries
);
5596 /* Should NEVER happen. */
5605 * Once the notify socket hangs up, this is called. First, it tries to find the
5606 * corresponding application. On failure, the call_rcu to close the socket is
5607 * executed. If an application is found, it tries to delete it from the notify
5608 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5610 * Note that an object needs to be allocated here so on ENOMEM failure, the
5611 * call RCU is not done but the rest of the cleanup is.
5613 void ust_app_notify_sock_unregister(int sock
)
5616 struct lttng_ht_iter iter
;
5617 struct ust_app
*app
;
5618 struct ust_app_notify_sock_obj
*obj
;
5624 obj
= zmalloc(sizeof(*obj
));
5627 * An ENOMEM is kind of uncool. If this strikes we continue the
5628 * procedure but the call_rcu will not be called. In this case, we
5629 * accept the fd leak rather than possibly creating an unsynchronized
5630 * state between threads.
5632 * TODO: The notify object should be created once the notify socket is
5633 * registered and stored independantely from the ust app object. The
5634 * tricky part is to synchronize the teardown of the application and
5635 * this notify object. Let's keep that in mind so we can avoid this
5636 * kind of shenanigans with ENOMEM in the teardown path.
5643 DBG("UST app notify socket unregister %d", sock
);
5646 * Lookup application by notify socket. If this fails, this means that the
5647 * hash table delete has already been done by the application
5648 * unregistration process so we can safely close the notify socket in a
5651 app
= find_app_by_notify_sock(sock
);
5656 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5659 * Whatever happens here either we fail or succeed, in both cases we have
5660 * to close the socket after a grace period to continue to the call RCU
5661 * here. If the deletion is successful, the application is not visible
5662 * anymore by other threads and is it fails it means that it was already
5663 * deleted from the hash table so either way we just have to close the
5666 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5672 * Close socket after a grace period to avoid for the socket to be reused
5673 * before the application object is freed creating potential race between
5674 * threads trying to add unique in the global hash table.
5677 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5682 * Destroy a ust app data structure and free its memory.
5684 void ust_app_destroy(struct ust_app
*app
)
5690 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5694 * Take a snapshot for a given UST session. The snapshot is sent to the given
5697 * Return 0 on success or else a negative value.
5699 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5700 struct snapshot_output
*output
, int wait
,
5701 uint64_t nb_packets_per_stream
)
5704 unsigned int snapshot_done
= 0;
5705 struct lttng_ht_iter iter
;
5706 struct ust_app
*app
;
5707 char pathname
[PATH_MAX
];
5714 switch (usess
->buffer_type
) {
5715 case LTTNG_BUFFER_PER_UID
:
5717 struct buffer_reg_uid
*reg
;
5719 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5720 struct buffer_reg_channel
*reg_chan
;
5721 struct consumer_socket
*socket
;
5723 /* Get consumer socket to use to push the metadata.*/
5724 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5731 memset(pathname
, 0, sizeof(pathname
));
5732 ret
= snprintf(pathname
, sizeof(pathname
),
5733 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5734 reg
->uid
, reg
->bits_per_long
);
5736 PERROR("snprintf snapshot path");
5740 /* Add the UST default trace dir to path. */
5741 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5742 reg_chan
, node
.node
) {
5743 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5744 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5745 nb_packets_per_stream
);
5750 ret
= consumer_snapshot_channel(socket
,
5751 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5752 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
5760 case LTTNG_BUFFER_PER_PID
:
5762 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5763 struct consumer_socket
*socket
;
5764 struct lttng_ht_iter chan_iter
;
5765 struct ust_app_channel
*ua_chan
;
5766 struct ust_app_session
*ua_sess
;
5767 struct ust_registry_session
*registry
;
5769 ua_sess
= lookup_session_by_app(usess
, app
);
5771 /* Session not associated with this app. */
5775 /* Get the right consumer socket for the application. */
5776 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5783 /* Add the UST default trace dir to path. */
5784 memset(pathname
, 0, sizeof(pathname
));
5785 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5788 PERROR("snprintf snapshot path");
5792 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5793 ua_chan
, node
.node
) {
5794 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
5795 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
5796 nb_packets_per_stream
);
5802 registry
= get_session_registry(ua_sess
);
5804 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
5805 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
5818 if (!snapshot_done
) {
5820 * If no snapshot was made and we are not in the error path, this means
5821 * that there are no buffers thus no (prior) application to snapshot
5822 * data from so we have simply NO data.
5833 * Return the size taken by one more packet per stream.
5835 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
5836 uint64_t cur_nr_packets
)
5838 uint64_t tot_size
= 0;
5839 struct ust_app
*app
;
5840 struct lttng_ht_iter iter
;
5844 switch (usess
->buffer_type
) {
5845 case LTTNG_BUFFER_PER_UID
:
5847 struct buffer_reg_uid
*reg
;
5849 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5850 struct buffer_reg_channel
*reg_chan
;
5853 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5854 reg_chan
, node
.node
) {
5855 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
5857 * Don't take channel into account if we
5858 * already grab all its packets.
5862 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
5868 case LTTNG_BUFFER_PER_PID
:
5871 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5872 struct ust_app_channel
*ua_chan
;
5873 struct ust_app_session
*ua_sess
;
5874 struct lttng_ht_iter chan_iter
;
5876 ua_sess
= lookup_session_by_app(usess
, app
);
5878 /* Session not associated with this app. */
5882 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5883 ua_chan
, node
.node
) {
5884 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
5886 * Don't take channel into account if we
5887 * already grab all its packets.
5891 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
5905 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
5906 struct cds_list_head
*buffer_reg_uid_list
,
5907 struct consumer_output
*consumer
, uint64_t uchan_id
,
5908 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
5911 uint64_t consumer_chan_key
;
5913 ret
= buffer_reg_uid_consumer_channel_key(
5914 buffer_reg_uid_list
, ust_session_id
,
5915 uchan_id
, &consumer_chan_key
);
5921 ret
= consumer_get_lost_packets(ust_session_id
,
5922 consumer_chan_key
, consumer
, lost
);
5924 ret
= consumer_get_discarded_events(ust_session_id
,
5925 consumer_chan_key
, consumer
, discarded
);
5932 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
5933 struct ltt_ust_channel
*uchan
,
5934 struct consumer_output
*consumer
, int overwrite
,
5935 uint64_t *discarded
, uint64_t *lost
)
5938 struct lttng_ht_iter iter
;
5939 struct lttng_ht_node_str
*ua_chan_node
;
5940 struct ust_app
*app
;
5941 struct ust_app_session
*ua_sess
;
5942 struct ust_app_channel
*ua_chan
;
5946 * Iterate over every registered applications, return when we
5947 * found one in the right session and channel.
5949 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5950 struct lttng_ht_iter uiter
;
5952 ua_sess
= lookup_session_by_app(usess
, app
);
5953 if (ua_sess
== NULL
) {
5958 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
5959 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5960 /* If the session is found for the app, the channel must be there */
5961 assert(ua_chan_node
);
5963 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5966 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
5970 ret
= consumer_get_discarded_events(usess
->id
,
5971 ua_chan
->key
, consumer
, discarded
);