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.
380 * The session list lock must be held by the caller.
383 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
385 uint64_t discarded
= 0, lost
= 0;
386 struct ltt_session
*session
;
387 struct ltt_ust_channel
*uchan
;
389 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
394 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
396 ERR("Missing LTT session to get discarded events");
399 if (!session
->ust_session
) {
400 ERR("Missing UST session to get discarded events");
404 if (ua_chan
->attr
.overwrite
) {
405 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
406 ua_chan
->key
, session
->ust_session
->consumer
,
409 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
410 ua_chan
->key
, session
->ust_session
->consumer
,
413 uchan
= trace_ust_find_channel_by_name(
414 session
->ust_session
->domain_global
.channels
,
417 ERR("Missing UST channel to store discarded counters");
421 uchan
->per_pid_closed_app_discarded
+= discarded
;
422 uchan
->per_pid_closed_app_lost
+= lost
;
429 * Delete ust app channel safely. RCU read lock must be held before calling
432 * The session list lock must be held by the caller.
435 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
439 struct lttng_ht_iter iter
;
440 struct ust_app_event
*ua_event
;
441 struct ust_app_ctx
*ua_ctx
;
442 struct ust_app_stream
*stream
, *stmp
;
443 struct ust_registry_session
*registry
;
447 DBG3("UST app deleting channel %s", ua_chan
->name
);
450 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
451 cds_list_del(&stream
->list
);
452 delete_ust_app_stream(sock
, stream
, app
);
456 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
457 cds_list_del(&ua_ctx
->list
);
458 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
460 delete_ust_app_ctx(sock
, ua_ctx
, app
);
464 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
466 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
468 delete_ust_app_event(sock
, ua_event
, app
);
471 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
472 /* Wipe and free registry from session registry. */
473 registry
= get_session_registry(ua_chan
->session
);
475 ust_registry_channel_del_free(registry
, ua_chan
->key
);
477 save_per_pid_lost_discarded_counters(ua_chan
);
480 if (ua_chan
->obj
!= NULL
) {
481 /* Remove channel from application UST object descriptor. */
482 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
483 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
485 pthread_mutex_lock(&app
->sock_lock
);
486 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
487 pthread_mutex_unlock(&app
->sock_lock
);
488 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
489 ERR("UST app sock %d release channel obj failed with ret %d",
492 lttng_fd_put(LTTNG_FD_APPS
, 1);
495 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
498 int ust_app_register_done(struct ust_app
*app
)
502 pthread_mutex_lock(&app
->sock_lock
);
503 ret
= ustctl_register_done(app
->sock
);
504 pthread_mutex_unlock(&app
->sock_lock
);
508 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
513 pthread_mutex_lock(&app
->sock_lock
);
518 ret
= ustctl_release_object(sock
, data
);
520 pthread_mutex_unlock(&app
->sock_lock
);
526 * Push metadata to consumer socket.
528 * RCU read-side lock must be held to guarantee existance of socket.
529 * Must be called with the ust app session lock held.
530 * Must be called with the registry lock held.
532 * On success, return the len of metadata pushed or else a negative value.
533 * Returning a -EPIPE return value means we could not send the metadata,
534 * but it can be caused by recoverable errors (e.g. the application has
535 * terminated concurrently).
537 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
538 struct consumer_socket
*socket
, int send_zero_data
)
541 char *metadata_str
= NULL
;
542 size_t len
, offset
, new_metadata_len_sent
;
544 uint64_t metadata_key
;
549 metadata_key
= registry
->metadata_key
;
552 * Means that no metadata was assigned to the session. This can
553 * happens if no start has been done previously.
560 * On a push metadata error either the consumer is dead or the
561 * metadata channel has been destroyed because its endpoint
562 * might have died (e.g: relayd), or because the application has
563 * exited. If so, the metadata closed flag is set to 1 so we
564 * deny pushing metadata again which is not valid anymore on the
567 if (registry
->metadata_closed
) {
571 offset
= registry
->metadata_len_sent
;
572 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
573 new_metadata_len_sent
= registry
->metadata_len
;
575 DBG3("No metadata to push for metadata key %" PRIu64
,
576 registry
->metadata_key
);
578 if (send_zero_data
) {
579 DBG("No metadata to push");
585 /* Allocate only what we have to send. */
586 metadata_str
= zmalloc(len
);
588 PERROR("zmalloc ust app metadata string");
592 /* Copy what we haven't sent out. */
593 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
596 pthread_mutex_unlock(®istry
->lock
);
598 * We need to unlock the registry while we push metadata to
599 * break a circular dependency between the consumerd metadata
600 * lock and the sessiond registry lock. Indeed, pushing metadata
601 * to the consumerd awaits that it gets pushed all the way to
602 * relayd, but doing so requires grabbing the metadata lock. If
603 * a concurrent metadata request is being performed by
604 * consumerd, this can try to grab the registry lock on the
605 * sessiond while holding the metadata lock on the consumer
606 * daemon. Those push and pull schemes are performed on two
607 * different bidirectionnal communication sockets.
609 ret
= consumer_push_metadata(socket
, metadata_key
,
610 metadata_str
, len
, offset
);
611 pthread_mutex_lock(®istry
->lock
);
614 * There is an acceptable race here between the registry
615 * metadata key assignment and the creation on the
616 * consumer. The session daemon can concurrently push
617 * metadata for this registry while being created on the
618 * consumer since the metadata key of the registry is
619 * assigned *before* it is setup to avoid the consumer
620 * to ask for metadata that could possibly be not found
621 * in the session daemon.
623 * The metadata will get pushed either by the session
624 * being stopped or the consumer requesting metadata if
625 * that race is triggered.
627 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
630 ERR("Error pushing metadata to consumer");
636 * Metadata may have been concurrently pushed, since
637 * we're not holding the registry lock while pushing to
638 * consumer. This is handled by the fact that we send
639 * the metadata content, size, and the offset at which
640 * that metadata belongs. This may arrive out of order
641 * on the consumer side, and the consumer is able to
642 * deal with overlapping fragments. The consumer
643 * supports overlapping fragments, which must be
644 * contiguous starting from offset 0. We keep the
645 * largest metadata_len_sent value of the concurrent
648 registry
->metadata_len_sent
=
649 max_t(size_t, registry
->metadata_len_sent
,
650 new_metadata_len_sent
);
659 * On error, flag the registry that the metadata is
660 * closed. We were unable to push anything and this
661 * means that either the consumer is not responding or
662 * the metadata cache has been destroyed on the
665 registry
->metadata_closed
= 1;
673 * For a given application and session, push metadata to consumer.
674 * Either sock or consumer is required : if sock is NULL, the default
675 * socket to send the metadata is retrieved from consumer, if sock
676 * is not NULL we use it to send the metadata.
677 * RCU read-side lock must be held while calling this function,
678 * therefore ensuring existance of registry. It also ensures existance
679 * of socket throughout this function.
681 * Return 0 on success else a negative error.
682 * Returning a -EPIPE return value means we could not send the metadata,
683 * but it can be caused by recoverable errors (e.g. the application has
684 * terminated concurrently).
686 static int push_metadata(struct ust_registry_session
*registry
,
687 struct consumer_output
*consumer
)
691 struct consumer_socket
*socket
;
696 pthread_mutex_lock(®istry
->lock
);
697 if (registry
->metadata_closed
) {
702 /* Get consumer socket to use to push the metadata.*/
703 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
710 ret
= ust_app_push_metadata(registry
, socket
, 0);
715 pthread_mutex_unlock(®istry
->lock
);
719 pthread_mutex_unlock(®istry
->lock
);
724 * Send to the consumer a close metadata command for the given session. Once
725 * done, the metadata channel is deleted and the session metadata pointer is
726 * nullified. The session lock MUST be held unless the application is
727 * in the destroy path.
729 * Return 0 on success else a negative value.
731 static int close_metadata(struct ust_registry_session
*registry
,
732 struct consumer_output
*consumer
)
735 struct consumer_socket
*socket
;
742 pthread_mutex_lock(®istry
->lock
);
744 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
749 /* Get consumer socket to use to push the metadata.*/
750 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
757 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
764 * Metadata closed. Even on error this means that the consumer is not
765 * responding or not found so either way a second close should NOT be emit
768 registry
->metadata_closed
= 1;
770 pthread_mutex_unlock(®istry
->lock
);
776 * We need to execute ht_destroy outside of RCU read-side critical
777 * section and outside of call_rcu thread, so we postpone its execution
778 * using ht_cleanup_push. It is simpler than to change the semantic of
779 * the many callers of delete_ust_app_session().
782 void delete_ust_app_session_rcu(struct rcu_head
*head
)
784 struct ust_app_session
*ua_sess
=
785 caa_container_of(head
, struct ust_app_session
, rcu_head
);
787 ht_cleanup_push(ua_sess
->channels
);
792 * Delete ust app session safely. RCU read lock must be held before calling
795 * The session list lock must be held by the caller.
798 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
802 struct lttng_ht_iter iter
;
803 struct ust_app_channel
*ua_chan
;
804 struct ust_registry_session
*registry
;
808 pthread_mutex_lock(&ua_sess
->lock
);
810 assert(!ua_sess
->deleted
);
811 ua_sess
->deleted
= true;
813 registry
= get_session_registry(ua_sess
);
815 /* Push metadata for application before freeing the application. */
816 (void) push_metadata(registry
, ua_sess
->consumer
);
819 * Don't ask to close metadata for global per UID buffers. Close
820 * metadata only on destroy trace session in this case. Also, the
821 * previous push metadata could have flag the metadata registry to
822 * close so don't send a close command if closed.
824 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
825 /* And ask to close it for this session registry. */
826 (void) close_metadata(registry
, ua_sess
->consumer
);
830 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
832 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
834 delete_ust_app_channel(sock
, ua_chan
, app
);
837 /* In case of per PID, the registry is kept in the session. */
838 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
839 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
841 buffer_reg_pid_remove(reg_pid
);
842 buffer_reg_pid_destroy(reg_pid
);
846 if (ua_sess
->handle
!= -1) {
847 pthread_mutex_lock(&app
->sock_lock
);
848 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
849 pthread_mutex_unlock(&app
->sock_lock
);
850 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
851 ERR("UST app sock %d release session handle failed with ret %d",
854 /* Remove session from application UST object descriptor. */
855 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
856 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
860 pthread_mutex_unlock(&ua_sess
->lock
);
862 consumer_output_put(ua_sess
->consumer
);
864 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
868 * Delete a traceable application structure from the global list. Never call
869 * this function outside of a call_rcu call.
871 * RCU read side lock should _NOT_ be held when calling this function.
874 void delete_ust_app(struct ust_app
*app
)
877 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
880 * The session list lock must be held during this function to guarantee
881 * the existence of ua_sess.
884 /* Delete ust app sessions info */
889 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
891 /* Free every object in the session and the session. */
893 delete_ust_app_session(sock
, ua_sess
, app
);
897 ht_cleanup_push(app
->sessions
);
898 ht_cleanup_push(app
->ust_sessions_objd
);
899 ht_cleanup_push(app
->ust_objd
);
902 * Wait until we have deleted the application from the sock hash table
903 * before closing this socket, otherwise an application could re-use the
904 * socket ID and race with the teardown, using the same hash table entry.
906 * It's OK to leave the close in call_rcu. We want it to stay unique for
907 * all RCU readers that could run concurrently with unregister app,
908 * therefore we _need_ to only close that socket after a grace period. So
909 * it should stay in this RCU callback.
911 * This close() is a very important step of the synchronization model so
912 * every modification to this function must be carefully reviewed.
918 lttng_fd_put(LTTNG_FD_APPS
, 1);
920 DBG2("UST app pid %d deleted", app
->pid
);
922 session_unlock_list();
926 * URCU intermediate call to delete an UST app.
929 void delete_ust_app_rcu(struct rcu_head
*head
)
931 struct lttng_ht_node_ulong
*node
=
932 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
933 struct ust_app
*app
=
934 caa_container_of(node
, struct ust_app
, pid_n
);
936 DBG3("Call RCU deleting app PID %d", app
->pid
);
941 * Delete the session from the application ht and delete the data structure by
942 * freeing every object inside and releasing them.
944 * The session list lock must be held by the caller.
946 static void destroy_app_session(struct ust_app
*app
,
947 struct ust_app_session
*ua_sess
)
950 struct lttng_ht_iter iter
;
955 iter
.iter
.node
= &ua_sess
->node
.node
;
956 ret
= lttng_ht_del(app
->sessions
, &iter
);
958 /* Already scheduled for teardown. */
962 /* Once deleted, free the data structure. */
963 delete_ust_app_session(app
->sock
, ua_sess
, app
);
970 * Alloc new UST app session.
973 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
975 struct ust_app_session
*ua_sess
;
977 /* Init most of the default value by allocating and zeroing */
978 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
979 if (ua_sess
== NULL
) {
984 ua_sess
->handle
= -1;
985 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
986 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
987 pthread_mutex_init(&ua_sess
->lock
, NULL
);
996 * Alloc new UST app channel.
999 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
1000 struct ust_app_session
*ua_sess
,
1001 struct lttng_ust_channel_attr
*attr
)
1003 struct ust_app_channel
*ua_chan
;
1005 /* Init most of the default value by allocating and zeroing */
1006 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1007 if (ua_chan
== NULL
) {
1012 /* Setup channel name */
1013 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1014 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1016 ua_chan
->enabled
= 1;
1017 ua_chan
->handle
= -1;
1018 ua_chan
->session
= ua_sess
;
1019 ua_chan
->key
= get_next_channel_key();
1020 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1021 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1022 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1024 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1025 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1027 /* Copy attributes */
1029 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1030 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1031 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1032 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1033 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1034 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1035 ua_chan
->attr
.output
= attr
->output
;
1037 /* By default, the channel is a per cpu channel. */
1038 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1040 DBG3("UST app channel %s allocated", ua_chan
->name
);
1049 * Allocate and initialize a UST app stream.
1051 * Return newly allocated stream pointer or NULL on error.
1053 struct ust_app_stream
*ust_app_alloc_stream(void)
1055 struct ust_app_stream
*stream
= NULL
;
1057 stream
= zmalloc(sizeof(*stream
));
1058 if (stream
== NULL
) {
1059 PERROR("zmalloc ust app stream");
1063 /* Zero could be a valid value for a handle so flag it to -1. */
1064 stream
->handle
= -1;
1071 * Alloc new UST app event.
1074 struct ust_app_event
*alloc_ust_app_event(char *name
,
1075 struct lttng_ust_event
*attr
)
1077 struct ust_app_event
*ua_event
;
1079 /* Init most of the default value by allocating and zeroing */
1080 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1081 if (ua_event
== NULL
) {
1086 ua_event
->enabled
= 1;
1087 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1088 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1089 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1091 /* Copy attributes */
1093 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1096 DBG3("UST app event %s allocated", ua_event
->name
);
1105 * Alloc new UST app context.
1108 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1110 struct ust_app_ctx
*ua_ctx
;
1112 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1113 if (ua_ctx
== NULL
) {
1117 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1120 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1121 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1122 char *provider_name
= NULL
, *ctx_name
= NULL
;
1124 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1125 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1126 if (!provider_name
|| !ctx_name
) {
1127 free(provider_name
);
1132 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1133 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1137 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1145 * Allocate a filter and copy the given original filter.
1147 * Return allocated filter or NULL on error.
1149 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1150 struct lttng_filter_bytecode
*orig_f
)
1152 struct lttng_filter_bytecode
*filter
= NULL
;
1154 /* Copy filter bytecode */
1155 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1157 PERROR("zmalloc alloc filter bytecode");
1161 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1168 * Create a liblttng-ust filter bytecode from given bytecode.
1170 * Return allocated filter or NULL on error.
1172 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1173 struct lttng_filter_bytecode
*orig_f
)
1175 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1177 /* Copy filter bytecode */
1178 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1180 PERROR("zmalloc alloc ust filter bytecode");
1184 assert(sizeof(struct lttng_filter_bytecode
) ==
1185 sizeof(struct lttng_ust_filter_bytecode
));
1186 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1192 * Find an ust_app using the sock and return it. RCU read side lock must be
1193 * held before calling this helper function.
1195 struct ust_app
*ust_app_find_by_sock(int sock
)
1197 struct lttng_ht_node_ulong
*node
;
1198 struct lttng_ht_iter iter
;
1200 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1201 node
= lttng_ht_iter_get_node_ulong(&iter
);
1203 DBG2("UST app find by sock %d not found", sock
);
1207 return caa_container_of(node
, struct ust_app
, sock_n
);
1214 * Find an ust_app using the notify sock and return it. RCU read side lock must
1215 * be held before calling this helper function.
1217 static struct ust_app
*find_app_by_notify_sock(int sock
)
1219 struct lttng_ht_node_ulong
*node
;
1220 struct lttng_ht_iter iter
;
1222 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1224 node
= lttng_ht_iter_get_node_ulong(&iter
);
1226 DBG2("UST app find by notify sock %d not found", sock
);
1230 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1237 * Lookup for an ust app event based on event name, filter bytecode and the
1240 * Return an ust_app_event object or NULL on error.
1242 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1243 char *name
, struct lttng_filter_bytecode
*filter
,
1245 const struct lttng_event_exclusion
*exclusion
)
1247 struct lttng_ht_iter iter
;
1248 struct lttng_ht_node_str
*node
;
1249 struct ust_app_event
*event
= NULL
;
1250 struct ust_app_ht_key key
;
1255 /* Setup key for event lookup. */
1257 key
.filter
= filter
;
1258 key
.loglevel_type
= loglevel_value
;
1259 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1260 key
.exclusion
= exclusion
;
1262 /* Lookup using the event name as hash and a custom match fct. */
1263 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1264 ht_match_ust_app_event
, &key
, &iter
.iter
);
1265 node
= lttng_ht_iter_get_node_str(&iter
);
1270 event
= caa_container_of(node
, struct ust_app_event
, node
);
1277 * Create the channel context on the tracer.
1279 * Called with UST app session lock held.
1282 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1283 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1287 health_code_update();
1289 pthread_mutex_lock(&app
->sock_lock
);
1290 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1291 ua_chan
->obj
, &ua_ctx
->obj
);
1292 pthread_mutex_unlock(&app
->sock_lock
);
1294 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1295 ERR("UST app create channel context failed for app (pid: %d) "
1296 "with ret %d", app
->pid
, ret
);
1299 * This is normal behavior, an application can die during the
1300 * creation process. Don't report an error so the execution can
1301 * continue normally.
1304 DBG3("UST app disable event failed. Application is dead.");
1309 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1311 DBG2("UST app context handle %d created successfully for channel %s",
1312 ua_ctx
->handle
, ua_chan
->name
);
1315 health_code_update();
1320 * Set the filter on the tracer.
1323 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1324 struct ust_app
*app
)
1327 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1329 health_code_update();
1331 if (!ua_event
->filter
) {
1336 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1337 if (!ust_bytecode
) {
1338 ret
= -LTTNG_ERR_NOMEM
;
1341 pthread_mutex_lock(&app
->sock_lock
);
1342 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1344 pthread_mutex_unlock(&app
->sock_lock
);
1346 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1347 ERR("UST app event %s filter failed for app (pid: %d) "
1348 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1351 * This is normal behavior, an application can die during the
1352 * creation process. Don't report an error so the execution can
1353 * continue normally.
1356 DBG3("UST app filter event failed. Application is dead.");
1361 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1364 health_code_update();
1370 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1371 struct lttng_event_exclusion
*exclusion
)
1373 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1374 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1375 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1377 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1378 if (!ust_exclusion
) {
1383 assert(sizeof(struct lttng_event_exclusion
) ==
1384 sizeof(struct lttng_ust_event_exclusion
));
1385 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1387 return ust_exclusion
;
1391 * Set event exclusions on the tracer.
1394 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1395 struct ust_app
*app
)
1398 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1400 health_code_update();
1402 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1407 ust_exclusion
= create_ust_exclusion_from_exclusion(
1408 ua_event
->exclusion
);
1409 if (!ust_exclusion
) {
1410 ret
= -LTTNG_ERR_NOMEM
;
1413 pthread_mutex_lock(&app
->sock_lock
);
1414 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1415 pthread_mutex_unlock(&app
->sock_lock
);
1417 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1418 ERR("UST app event %s exclusions failed for app (pid: %d) "
1419 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1422 * This is normal behavior, an application can die during the
1423 * creation process. Don't report an error so the execution can
1424 * continue normally.
1427 DBG3("UST app event exclusion failed. Application is dead.");
1432 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1435 health_code_update();
1436 free(ust_exclusion
);
1441 * Disable the specified event on to UST tracer for the UST session.
1443 static int disable_ust_event(struct ust_app
*app
,
1444 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1448 health_code_update();
1450 pthread_mutex_lock(&app
->sock_lock
);
1451 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1452 pthread_mutex_unlock(&app
->sock_lock
);
1454 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1455 ERR("UST app event %s disable failed for app (pid: %d) "
1456 "and session handle %d with ret %d",
1457 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1460 * This is normal behavior, an application can die during the
1461 * creation process. Don't report an error so the execution can
1462 * continue normally.
1465 DBG3("UST app disable event failed. Application is dead.");
1470 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1471 ua_event
->attr
.name
, app
->pid
);
1474 health_code_update();
1479 * Disable the specified channel on to UST tracer for the UST session.
1481 static int disable_ust_channel(struct ust_app
*app
,
1482 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1486 health_code_update();
1488 pthread_mutex_lock(&app
->sock_lock
);
1489 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1490 pthread_mutex_unlock(&app
->sock_lock
);
1492 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1493 ERR("UST app channel %s disable failed for app (pid: %d) "
1494 "and session handle %d with ret %d",
1495 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1498 * This is normal behavior, an application can die during the
1499 * creation process. Don't report an error so the execution can
1500 * continue normally.
1503 DBG3("UST app disable channel failed. Application is dead.");
1508 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1509 ua_chan
->name
, app
->pid
);
1512 health_code_update();
1517 * Enable the specified channel on to UST tracer for the UST session.
1519 static int enable_ust_channel(struct ust_app
*app
,
1520 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1524 health_code_update();
1526 pthread_mutex_lock(&app
->sock_lock
);
1527 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1528 pthread_mutex_unlock(&app
->sock_lock
);
1530 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1531 ERR("UST app channel %s enable failed for app (pid: %d) "
1532 "and session handle %d with ret %d",
1533 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1536 * This is normal behavior, an application can die during the
1537 * creation process. Don't report an error so the execution can
1538 * continue normally.
1541 DBG3("UST app enable channel failed. Application is dead.");
1546 ua_chan
->enabled
= 1;
1548 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1549 ua_chan
->name
, app
->pid
);
1552 health_code_update();
1557 * Enable the specified event on to UST tracer for the UST session.
1559 static int enable_ust_event(struct ust_app
*app
,
1560 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1564 health_code_update();
1566 pthread_mutex_lock(&app
->sock_lock
);
1567 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1568 pthread_mutex_unlock(&app
->sock_lock
);
1570 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1571 ERR("UST app event %s enable failed for app (pid: %d) "
1572 "and session handle %d with ret %d",
1573 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1576 * This is normal behavior, an application can die during the
1577 * creation process. Don't report an error so the execution can
1578 * continue normally.
1581 DBG3("UST app enable event failed. Application is dead.");
1586 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1587 ua_event
->attr
.name
, app
->pid
);
1590 health_code_update();
1595 * Send channel and stream buffer to application.
1597 * Return 0 on success. On error, a negative value is returned.
1599 static int send_channel_pid_to_ust(struct ust_app
*app
,
1600 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1603 struct ust_app_stream
*stream
, *stmp
;
1609 health_code_update();
1611 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1614 /* Send channel to the application. */
1615 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1616 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1617 ret
= -ENOTCONN
; /* Caused by app exiting. */
1619 } else if (ret
< 0) {
1623 health_code_update();
1625 /* Send all streams to application. */
1626 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1627 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1628 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1629 ret
= -ENOTCONN
; /* Caused by app exiting. */
1631 } else if (ret
< 0) {
1634 /* We don't need the stream anymore once sent to the tracer. */
1635 cds_list_del(&stream
->list
);
1636 delete_ust_app_stream(-1, stream
, app
);
1638 /* Flag the channel that it is sent to the application. */
1639 ua_chan
->is_sent
= 1;
1642 health_code_update();
1647 * Create the specified event onto the UST tracer for a UST session.
1649 * Should be called with session mutex held.
1652 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1653 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1657 health_code_update();
1659 /* Create UST event on tracer */
1660 pthread_mutex_lock(&app
->sock_lock
);
1661 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1663 pthread_mutex_unlock(&app
->sock_lock
);
1665 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1666 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1667 ua_event
->attr
.name
, app
->pid
, ret
);
1670 * This is normal behavior, an application can die during the
1671 * creation process. Don't report an error so the execution can
1672 * continue normally.
1675 DBG3("UST app create event failed. Application is dead.");
1680 ua_event
->handle
= ua_event
->obj
->handle
;
1682 DBG2("UST app event %s created successfully for pid:%d",
1683 ua_event
->attr
.name
, app
->pid
);
1685 health_code_update();
1687 /* Set filter if one is present. */
1688 if (ua_event
->filter
) {
1689 ret
= set_ust_event_filter(ua_event
, app
);
1695 /* Set exclusions for the event */
1696 if (ua_event
->exclusion
) {
1697 ret
= set_ust_event_exclusion(ua_event
, app
);
1703 /* If event not enabled, disable it on the tracer */
1704 if (ua_event
->enabled
) {
1706 * We now need to explicitly enable the event, since it
1707 * is now disabled at creation.
1709 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1712 * If we hit an EPERM, something is wrong with our enable call. If
1713 * we get an EEXIST, there is a problem on the tracer side since we
1717 case -LTTNG_UST_ERR_PERM
:
1718 /* Code flow problem */
1720 case -LTTNG_UST_ERR_EXIST
:
1721 /* It's OK for our use case. */
1732 health_code_update();
1737 * Copy data between an UST app event and a LTT event.
1739 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1740 struct ltt_ust_event
*uevent
)
1742 size_t exclusion_alloc_size
;
1744 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1745 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1747 ua_event
->enabled
= uevent
->enabled
;
1749 /* Copy event attributes */
1750 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1752 /* Copy filter bytecode */
1753 if (uevent
->filter
) {
1754 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1755 /* Filter might be NULL here in case of ENONEM. */
1758 /* Copy exclusion data */
1759 if (uevent
->exclusion
) {
1760 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1761 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1762 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1763 if (ua_event
->exclusion
== NULL
) {
1766 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1767 exclusion_alloc_size
);
1773 * Copy data between an UST app channel and a LTT channel.
1775 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1776 struct ltt_ust_channel
*uchan
)
1778 struct lttng_ht_iter iter
;
1779 struct ltt_ust_event
*uevent
;
1780 struct ltt_ust_context
*uctx
;
1781 struct ust_app_event
*ua_event
;
1783 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1785 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1786 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1788 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1789 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1791 /* Copy event attributes since the layout is different. */
1792 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1793 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1794 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1795 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1796 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1797 ua_chan
->attr
.output
= uchan
->attr
.output
;
1799 * Note that the attribute channel type is not set since the channel on the
1800 * tracing registry side does not have this information.
1803 ua_chan
->enabled
= uchan
->enabled
;
1804 ua_chan
->tracing_channel_id
= uchan
->id
;
1806 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1807 struct ust_app_ctx
*ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1809 if (ua_ctx
== NULL
) {
1812 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1813 (unsigned long) ua_ctx
->ctx
.ctx
);
1814 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1815 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1818 /* Copy all events from ltt ust channel to ust app channel */
1819 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1820 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1821 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1822 if (ua_event
== NULL
) {
1823 DBG2("UST event %s not found on shadow copy channel",
1825 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1826 if (ua_event
== NULL
) {
1829 shadow_copy_event(ua_event
, uevent
);
1830 add_unique_ust_app_event(ua_chan
, ua_event
);
1834 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1838 * Copy data between a UST app session and a regular LTT session.
1840 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1841 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1843 struct lttng_ht_node_str
*ua_chan_node
;
1844 struct lttng_ht_iter iter
;
1845 struct ltt_ust_channel
*uchan
;
1846 struct ust_app_channel
*ua_chan
;
1848 struct tm
*timeinfo
;
1851 char tmp_shm_path
[PATH_MAX
];
1853 /* Get date and time for unique app path */
1855 timeinfo
= localtime(&rawtime
);
1856 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1858 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1860 ua_sess
->tracing_id
= usess
->id
;
1861 ua_sess
->id
= get_next_session_id();
1862 ua_sess
->uid
= app
->uid
;
1863 ua_sess
->gid
= app
->gid
;
1864 ua_sess
->euid
= usess
->uid
;
1865 ua_sess
->egid
= usess
->gid
;
1866 ua_sess
->buffer_type
= usess
->buffer_type
;
1867 ua_sess
->bits_per_long
= app
->bits_per_long
;
1869 /* There is only one consumer object per session possible. */
1870 consumer_output_get(usess
->consumer
);
1871 ua_sess
->consumer
= usess
->consumer
;
1873 ua_sess
->output_traces
= usess
->output_traces
;
1874 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1875 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1876 &usess
->metadata_attr
);
1878 switch (ua_sess
->buffer_type
) {
1879 case LTTNG_BUFFER_PER_PID
:
1880 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1881 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1884 case LTTNG_BUFFER_PER_UID
:
1885 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1886 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1893 PERROR("asprintf UST shadow copy session");
1898 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1899 sizeof(ua_sess
->root_shm_path
));
1900 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1901 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1902 sizeof(ua_sess
->shm_path
));
1903 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1904 if (ua_sess
->shm_path
[0]) {
1905 switch (ua_sess
->buffer_type
) {
1906 case LTTNG_BUFFER_PER_PID
:
1907 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1908 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1909 app
->name
, app
->pid
, datetime
);
1911 case LTTNG_BUFFER_PER_UID
:
1912 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1913 DEFAULT_UST_TRACE_UID_PATH
,
1914 app
->uid
, app
->bits_per_long
);
1921 PERROR("sprintf UST shadow copy session");
1925 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1926 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1927 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1930 /* Iterate over all channels in global domain. */
1931 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1933 struct lttng_ht_iter uiter
;
1935 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1936 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1937 if (ua_chan_node
!= NULL
) {
1938 /* Session exist. Contiuing. */
1942 DBG2("Channel %s not found on shadow session copy, creating it",
1944 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
,
1946 if (ua_chan
== NULL
) {
1947 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1950 shadow_copy_channel(ua_chan
, uchan
);
1952 * The concept of metadata channel does not exist on the tracing
1953 * registry side of the session daemon so this can only be a per CPU
1954 * channel and not metadata.
1956 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1958 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1963 consumer_output_put(ua_sess
->consumer
);
1967 * Lookup sesison wrapper.
1970 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1971 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1973 /* Get right UST app session from app */
1974 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1978 * Return ust app session from the app session hashtable using the UST session
1981 static struct ust_app_session
*lookup_session_by_app(
1982 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1984 struct lttng_ht_iter iter
;
1985 struct lttng_ht_node_u64
*node
;
1987 __lookup_session_by_app(usess
, app
, &iter
);
1988 node
= lttng_ht_iter_get_node_u64(&iter
);
1993 return caa_container_of(node
, struct ust_app_session
, node
);
2000 * Setup buffer registry per PID for the given session and application. If none
2001 * is found, a new one is created, added to the global registry and
2002 * initialized. If regp is valid, it's set with the newly created object.
2004 * Return 0 on success or else a negative value.
2006 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2007 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2010 struct buffer_reg_pid
*reg_pid
;
2017 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
2020 * This is the create channel path meaning that if there is NO
2021 * registry available, we have to create one for this session.
2023 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
2024 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2032 /* Initialize registry. */
2033 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
2034 app
->bits_per_long
, app
->uint8_t_alignment
,
2035 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2036 app
->uint64_t_alignment
, app
->long_alignment
,
2037 app
->byte_order
, app
->version
.major
,
2038 app
->version
.minor
, reg_pid
->root_shm_path
,
2040 ua_sess
->euid
, ua_sess
->egid
);
2043 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2044 * destroy the buffer registry, because it is always expected
2045 * that if the buffer registry can be found, its ust registry is
2048 buffer_reg_pid_destroy(reg_pid
);
2052 buffer_reg_pid_add(reg_pid
);
2054 DBG3("UST app buffer registry per PID created successfully");
2066 * Setup buffer registry per UID for the given session and application. If none
2067 * is found, a new one is created, added to the global registry and
2068 * initialized. If regp is valid, it's set with the newly created object.
2070 * Return 0 on success or else a negative value.
2072 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
2073 struct ust_app_session
*ua_sess
,
2074 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
2077 struct buffer_reg_uid
*reg_uid
;
2084 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2087 * This is the create channel path meaning that if there is NO
2088 * registry available, we have to create one for this session.
2090 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
2091 LTTNG_DOMAIN_UST
, ®_uid
,
2092 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
2100 /* Initialize registry. */
2101 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
2102 app
->bits_per_long
, app
->uint8_t_alignment
,
2103 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
2104 app
->uint64_t_alignment
, app
->long_alignment
,
2105 app
->byte_order
, app
->version
.major
,
2106 app
->version
.minor
, reg_uid
->root_shm_path
,
2107 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
2110 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2111 * destroy the buffer registry, because it is always expected
2112 * that if the buffer registry can be found, its ust registry is
2115 buffer_reg_uid_destroy(reg_uid
, NULL
);
2118 /* Add node to teardown list of the session. */
2119 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
2121 buffer_reg_uid_add(reg_uid
);
2123 DBG3("UST app buffer registry per UID created successfully");
2134 * Create a session on the tracer side for the given app.
2136 * On success, ua_sess_ptr is populated with the session pointer or else left
2137 * untouched. If the session was created, is_created is set to 1. On error,
2138 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2141 * Returns 0 on success or else a negative code which is either -ENOMEM or
2142 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2144 static int create_ust_app_session(struct ltt_ust_session
*usess
,
2145 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
2148 int ret
, created
= 0;
2149 struct ust_app_session
*ua_sess
;
2153 assert(ua_sess_ptr
);
2155 health_code_update();
2157 ua_sess
= lookup_session_by_app(usess
, app
);
2158 if (ua_sess
== NULL
) {
2159 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2160 app
->pid
, usess
->id
);
2161 ua_sess
= alloc_ust_app_session(app
);
2162 if (ua_sess
== NULL
) {
2163 /* Only malloc can failed so something is really wrong */
2167 shadow_copy_session(ua_sess
, usess
, app
);
2171 switch (usess
->buffer_type
) {
2172 case LTTNG_BUFFER_PER_PID
:
2173 /* Init local registry. */
2174 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2176 delete_ust_app_session(-1, ua_sess
, app
);
2180 case LTTNG_BUFFER_PER_UID
:
2181 /* Look for a global registry. If none exists, create one. */
2182 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2184 delete_ust_app_session(-1, ua_sess
, app
);
2194 health_code_update();
2196 if (ua_sess
->handle
== -1) {
2197 pthread_mutex_lock(&app
->sock_lock
);
2198 ret
= ustctl_create_session(app
->sock
);
2199 pthread_mutex_unlock(&app
->sock_lock
);
2201 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2202 ERR("Creating session for app pid %d with ret %d",
2205 DBG("UST app creating session failed. Application is dead");
2207 * This is normal behavior, an application can die during the
2208 * creation process. Don't report an error so the execution can
2209 * continue normally. This will get flagged ENOTCONN and the
2210 * caller will handle it.
2214 delete_ust_app_session(-1, ua_sess
, app
);
2215 if (ret
!= -ENOMEM
) {
2217 * Tracer is probably gone or got an internal error so let's
2218 * behave like it will soon unregister or not usable.
2225 ua_sess
->handle
= ret
;
2227 /* Add ust app session to app's HT */
2228 lttng_ht_node_init_u64(&ua_sess
->node
,
2229 ua_sess
->tracing_id
);
2230 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2231 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
2232 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
2233 &ua_sess
->ust_objd_node
);
2235 DBG2("UST app session created successfully with handle %d", ret
);
2238 *ua_sess_ptr
= ua_sess
;
2240 *is_created
= created
;
2243 /* Everything went well. */
2247 health_code_update();
2252 * Match function for a hash table lookup of ust_app_ctx.
2254 * It matches an ust app context based on the context type and, in the case
2255 * of perf counters, their name.
2257 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2259 struct ust_app_ctx
*ctx
;
2260 const struct lttng_ust_context_attr
*key
;
2265 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2269 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2274 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
2275 if (strncmp(key
->u
.perf_counter
.name
,
2276 ctx
->ctx
.u
.perf_counter
.name
,
2277 sizeof(key
->u
.perf_counter
.name
))) {
2281 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
2282 if (strcmp(key
->u
.app_ctx
.provider_name
,
2283 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
2284 strcmp(key
->u
.app_ctx
.ctx_name
,
2285 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
2301 * Lookup for an ust app context from an lttng_ust_context.
2303 * Must be called while holding RCU read side lock.
2304 * Return an ust_app_ctx object or NULL on error.
2307 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2308 struct lttng_ust_context_attr
*uctx
)
2310 struct lttng_ht_iter iter
;
2311 struct lttng_ht_node_ulong
*node
;
2312 struct ust_app_ctx
*app_ctx
= NULL
;
2317 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2318 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2319 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2320 node
= lttng_ht_iter_get_node_ulong(&iter
);
2325 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2332 * Create a context for the channel on the tracer.
2334 * Called with UST app session lock held and a RCU read side lock.
2337 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2338 struct ust_app_channel
*ua_chan
,
2339 struct lttng_ust_context_attr
*uctx
,
2340 struct ust_app
*app
)
2343 struct ust_app_ctx
*ua_ctx
;
2345 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2347 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2353 ua_ctx
= alloc_ust_app_ctx(uctx
);
2354 if (ua_ctx
== NULL
) {
2360 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2361 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2362 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2364 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2374 * Enable on the tracer side a ust app event for the session and channel.
2376 * Called with UST app session lock held.
2379 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2380 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2384 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2389 ua_event
->enabled
= 1;
2396 * Disable on the tracer side a ust app event for the session and channel.
2398 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2399 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2403 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2408 ua_event
->enabled
= 0;
2415 * Lookup ust app channel for session and disable it on the tracer side.
2418 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2419 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2423 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2428 ua_chan
->enabled
= 0;
2435 * Lookup ust app channel for session and enable it on the tracer side. This
2436 * MUST be called with a RCU read side lock acquired.
2438 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2439 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2442 struct lttng_ht_iter iter
;
2443 struct lttng_ht_node_str
*ua_chan_node
;
2444 struct ust_app_channel
*ua_chan
;
2446 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2447 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2448 if (ua_chan_node
== NULL
) {
2449 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2450 uchan
->name
, ua_sess
->tracing_id
);
2454 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2456 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2466 * Ask the consumer to create a channel and get it if successful.
2468 * Return 0 on success or else a negative value.
2470 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2471 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2472 int bitness
, struct ust_registry_session
*registry
)
2475 unsigned int nb_fd
= 0;
2476 struct consumer_socket
*socket
;
2484 health_code_update();
2486 /* Get the right consumer socket for the application. */
2487 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2493 health_code_update();
2495 /* Need one fd for the channel. */
2496 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2498 ERR("Exhausted number of available FD upon create channel");
2503 * Ask consumer to create channel. The consumer will return the number of
2504 * stream we have to expect.
2506 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2513 * Compute the number of fd needed before receiving them. It must be 2 per
2514 * stream (2 being the default value here).
2516 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2518 /* Reserve the amount of file descriptor we need. */
2519 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2521 ERR("Exhausted number of available FD upon create channel");
2522 goto error_fd_get_stream
;
2525 health_code_update();
2528 * Now get the channel from the consumer. This call wil populate the stream
2529 * list of that channel and set the ust objects.
2531 if (usess
->consumer
->enabled
) {
2532 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2542 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2543 error_fd_get_stream
:
2545 * Initiate a destroy channel on the consumer since we had an error
2546 * handling it on our side. The return value is of no importance since we
2547 * already have a ret value set by the previous error that we need to
2550 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2552 lttng_fd_put(LTTNG_FD_APPS
, 1);
2554 health_code_update();
2560 * Duplicate the ust data object of the ust app stream and save it in the
2561 * buffer registry stream.
2563 * Return 0 on success or else a negative value.
2565 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2566 struct ust_app_stream
*stream
)
2573 /* Reserve the amount of file descriptor we need. */
2574 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2576 ERR("Exhausted number of available FD upon duplicate stream");
2580 /* Duplicate object for stream once the original is in the registry. */
2581 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2582 reg_stream
->obj
.ust
);
2584 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2585 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2586 lttng_fd_put(LTTNG_FD_APPS
, 2);
2589 stream
->handle
= stream
->obj
->handle
;
2596 * Duplicate the ust data object of the ust app. channel and save it in the
2597 * buffer registry channel.
2599 * Return 0 on success or else a negative value.
2601 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2602 struct ust_app_channel
*ua_chan
)
2609 /* Need two fds for the channel. */
2610 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2612 ERR("Exhausted number of available FD upon duplicate channel");
2616 /* Duplicate object for stream once the original is in the registry. */
2617 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2619 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2620 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2623 ua_chan
->handle
= ua_chan
->obj
->handle
;
2628 lttng_fd_put(LTTNG_FD_APPS
, 1);
2634 * For a given channel buffer registry, setup all streams of the given ust
2635 * application channel.
2637 * Return 0 on success or else a negative value.
2639 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2640 struct ust_app_channel
*ua_chan
,
2641 struct ust_app
*app
)
2644 struct ust_app_stream
*stream
, *stmp
;
2649 DBG2("UST app setup buffer registry stream");
2651 /* Send all streams to application. */
2652 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2653 struct buffer_reg_stream
*reg_stream
;
2655 ret
= buffer_reg_stream_create(®_stream
);
2661 * Keep original pointer and nullify it in the stream so the delete
2662 * stream call does not release the object.
2664 reg_stream
->obj
.ust
= stream
->obj
;
2666 buffer_reg_stream_add(reg_stream
, reg_chan
);
2668 /* We don't need the streams anymore. */
2669 cds_list_del(&stream
->list
);
2670 delete_ust_app_stream(-1, stream
, app
);
2678 * Create a buffer registry channel for the given session registry and
2679 * application channel object. If regp pointer is valid, it's set with the
2680 * created object. Important, the created object is NOT added to the session
2681 * registry hash table.
2683 * Return 0 on success else a negative value.
2685 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2686 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2689 struct buffer_reg_channel
*reg_chan
= NULL
;
2694 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2696 /* Create buffer registry channel. */
2697 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2702 reg_chan
->consumer_key
= ua_chan
->key
;
2703 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2704 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2706 /* Create and add a channel registry to session. */
2707 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2708 ua_chan
->tracing_channel_id
);
2712 buffer_reg_channel_add(reg_sess
, reg_chan
);
2721 /* Safe because the registry channel object was not added to any HT. */
2722 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2728 * Setup buffer registry channel for the given session registry and application
2729 * channel object. If regp pointer is valid, it's set with the created object.
2731 * Return 0 on success else a negative value.
2733 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2734 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
,
2735 struct ust_app
*app
)
2742 assert(ua_chan
->obj
);
2744 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2746 /* Setup all streams for the registry. */
2747 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
, app
);
2752 reg_chan
->obj
.ust
= ua_chan
->obj
;
2753 ua_chan
->obj
= NULL
;
2758 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2759 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2764 * Send buffer registry channel to the application.
2766 * Return 0 on success else a negative value.
2768 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2769 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2770 struct ust_app_channel
*ua_chan
)
2773 struct buffer_reg_stream
*reg_stream
;
2780 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2782 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2787 /* Send channel to the application. */
2788 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2789 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2790 ret
= -ENOTCONN
; /* Caused by app exiting. */
2792 } else if (ret
< 0) {
2796 health_code_update();
2798 /* Send all streams to application. */
2799 pthread_mutex_lock(®_chan
->stream_list_lock
);
2800 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2801 struct ust_app_stream stream
;
2803 ret
= duplicate_stream_object(reg_stream
, &stream
);
2805 goto error_stream_unlock
;
2808 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2810 (void) release_ust_app_stream(-1, &stream
, app
);
2811 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2812 ret
= -ENOTCONN
; /* Caused by app exiting. */
2813 goto error_stream_unlock
;
2814 } else if (ret
< 0) {
2815 goto error_stream_unlock
;
2817 goto error_stream_unlock
;
2821 * The return value is not important here. This function will output an
2824 (void) release_ust_app_stream(-1, &stream
, app
);
2826 ua_chan
->is_sent
= 1;
2828 error_stream_unlock
:
2829 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2835 * Create and send to the application the created buffers with per UID buffers.
2837 * Return 0 on success else a negative value.
2839 static int create_channel_per_uid(struct ust_app
*app
,
2840 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2841 struct ust_app_channel
*ua_chan
)
2844 struct buffer_reg_uid
*reg_uid
;
2845 struct buffer_reg_channel
*reg_chan
;
2852 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2854 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2856 * The session creation handles the creation of this global registry
2857 * object. If none can be find, there is a code flow problem or a
2862 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2865 /* Create the buffer registry channel object. */
2866 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2868 ERR("Error creating the UST channel \"%s\" registry instance",
2875 * Create the buffers on the consumer side. This call populates the
2876 * ust app channel object with all streams and data object.
2878 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2879 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2881 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2885 * Let's remove the previously created buffer registry channel so
2886 * it's not visible anymore in the session registry.
2888 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2889 ua_chan
->tracing_channel_id
);
2890 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2891 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2896 * Setup the streams and add it to the session registry.
2898 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
2899 ua_chan
, reg_chan
, app
);
2901 ERR("Error setting up UST channel \"%s\"",
2908 /* Send buffers to the application. */
2909 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2911 if (ret
!= -ENOTCONN
) {
2912 ERR("Error sending channel to application");
2922 * Create and send to the application the created buffers with per PID buffers.
2924 * Return 0 on success else a negative value.
2926 static int create_channel_per_pid(struct ust_app
*app
,
2927 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2928 struct ust_app_channel
*ua_chan
)
2931 struct ust_registry_session
*registry
;
2938 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2942 registry
= get_session_registry(ua_sess
);
2945 /* Create and add a new channel registry to session. */
2946 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2948 ERR("Error creating the UST channel \"%s\" registry instance",
2953 /* Create and get channel on the consumer side. */
2954 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2955 app
->bits_per_long
, registry
);
2957 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2962 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2964 if (ret
!= -ENOTCONN
) {
2965 ERR("Error sending channel to application");
2976 * From an already allocated ust app channel, create the channel buffers if
2977 * need and send it to the application. This MUST be called with a RCU read
2978 * side lock acquired.
2980 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2981 * the application exited concurrently.
2983 static int do_create_channel(struct ust_app
*app
,
2984 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2985 struct ust_app_channel
*ua_chan
)
2994 /* Handle buffer type before sending the channel to the application. */
2995 switch (usess
->buffer_type
) {
2996 case LTTNG_BUFFER_PER_UID
:
2998 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
3004 case LTTNG_BUFFER_PER_PID
:
3006 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
3018 /* Initialize ust objd object using the received handle and add it. */
3019 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
3020 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
3022 /* If channel is not enabled, disable it on the tracer */
3023 if (!ua_chan
->enabled
) {
3024 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3035 * Create UST app channel and create it on the tracer. Set ua_chanp of the
3036 * newly created channel if not NULL.
3038 * Called with UST app session lock and RCU read-side lock held.
3040 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3041 * the application exited concurrently.
3043 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
3044 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
3045 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
3046 struct ust_app_channel
**ua_chanp
)
3049 struct lttng_ht_iter iter
;
3050 struct lttng_ht_node_str
*ua_chan_node
;
3051 struct ust_app_channel
*ua_chan
;
3053 /* Lookup channel in the ust app session */
3054 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3055 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3056 if (ua_chan_node
!= NULL
) {
3057 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3061 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
3062 if (ua_chan
== NULL
) {
3063 /* Only malloc can fail here */
3067 shadow_copy_channel(ua_chan
, uchan
);
3069 /* Set channel type. */
3070 ua_chan
->attr
.type
= type
;
3072 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
3077 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
3080 /* Only add the channel if successful on the tracer side. */
3081 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
3085 *ua_chanp
= ua_chan
;
3088 /* Everything went well. */
3092 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
3098 * Create UST app event and create it on the tracer side.
3100 * Called with ust app session mutex held.
3103 int create_ust_app_event(struct ust_app_session
*ua_sess
,
3104 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
3105 struct ust_app
*app
)
3108 struct ust_app_event
*ua_event
;
3110 /* Get event node */
3111 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3112 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3113 if (ua_event
!= NULL
) {
3118 /* Does not exist so create one */
3119 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
3120 if (ua_event
== NULL
) {
3121 /* Only malloc can failed so something is really wrong */
3125 shadow_copy_event(ua_event
, uevent
);
3127 /* Create it on the tracer side */
3128 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3130 /* Not found previously means that it does not exist on the tracer */
3131 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
3135 add_unique_ust_app_event(ua_chan
, ua_event
);
3137 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
3144 /* Valid. Calling here is already in a read side lock */
3145 delete_ust_app_event(-1, ua_event
, app
);
3150 * Create UST metadata and open it on the tracer side.
3152 * Called with UST app session lock held and RCU read side lock.
3154 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
3155 struct ust_app
*app
, struct consumer_output
*consumer
)
3158 struct ust_app_channel
*metadata
;
3159 struct consumer_socket
*socket
;
3160 struct ust_registry_session
*registry
;
3166 registry
= get_session_registry(ua_sess
);
3169 pthread_mutex_lock(®istry
->lock
);
3171 /* Metadata already exists for this registry or it was closed previously */
3172 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3177 /* Allocate UST metadata */
3178 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3180 /* malloc() failed */
3185 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3187 /* Need one fd for the channel. */
3188 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3190 ERR("Exhausted number of available FD upon create metadata");
3194 /* Get the right consumer socket for the application. */
3195 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3198 goto error_consumer
;
3202 * Keep metadata key so we can identify it on the consumer side. Assign it
3203 * to the registry *before* we ask the consumer so we avoid the race of the
3204 * consumer requesting the metadata and the ask_channel call on our side
3205 * did not returned yet.
3207 registry
->metadata_key
= metadata
->key
;
3210 * Ask the metadata channel creation to the consumer. The metadata object
3211 * will be created by the consumer and kept their. However, the stream is
3212 * never added or monitored until we do a first push metadata to the
3215 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3218 /* Nullify the metadata key so we don't try to close it later on. */
3219 registry
->metadata_key
= 0;
3220 goto error_consumer
;
3224 * The setup command will make the metadata stream be sent to the relayd,
3225 * if applicable, and the thread managing the metadatas. This is important
3226 * because after this point, if an error occurs, the only way the stream
3227 * can be deleted is to be monitored in the consumer.
3229 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3231 /* Nullify the metadata key so we don't try to close it later on. */
3232 registry
->metadata_key
= 0;
3233 goto error_consumer
;
3236 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3237 metadata
->key
, app
->pid
);
3240 lttng_fd_put(LTTNG_FD_APPS
, 1);
3241 delete_ust_app_channel(-1, metadata
, app
);
3243 pthread_mutex_unlock(®istry
->lock
);
3248 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3249 * acquired before calling this function.
3251 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3253 struct ust_app
*app
= NULL
;
3254 struct lttng_ht_node_ulong
*node
;
3255 struct lttng_ht_iter iter
;
3257 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3258 node
= lttng_ht_iter_get_node_ulong(&iter
);
3260 DBG2("UST app no found with pid %d", pid
);
3264 DBG2("Found UST app by pid %d", pid
);
3266 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3273 * Allocate and init an UST app object using the registration information and
3274 * the command socket. This is called when the command socket connects to the
3277 * The object is returned on success or else NULL.
3279 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3281 struct ust_app
*lta
= NULL
;
3286 DBG3("UST app creating application for socket %d", sock
);
3288 if ((msg
->bits_per_long
== 64 &&
3289 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3290 || (msg
->bits_per_long
== 32 &&
3291 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3292 ERR("Registration failed: application \"%s\" (pid: %d) has "
3293 "%d-bit long, but no consumerd for this size is available.\n",
3294 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3298 lta
= zmalloc(sizeof(struct ust_app
));
3304 lta
->ppid
= msg
->ppid
;
3305 lta
->uid
= msg
->uid
;
3306 lta
->gid
= msg
->gid
;
3308 lta
->bits_per_long
= msg
->bits_per_long
;
3309 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3310 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3311 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3312 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3313 lta
->long_alignment
= msg
->long_alignment
;
3314 lta
->byte_order
= msg
->byte_order
;
3316 lta
->v_major
= msg
->major
;
3317 lta
->v_minor
= msg
->minor
;
3318 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3319 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3320 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3321 lta
->notify_sock
= -1;
3323 /* Copy name and make sure it's NULL terminated. */
3324 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3325 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3328 * Before this can be called, when receiving the registration information,
3329 * the application compatibility is checked. So, at this point, the
3330 * application can work with this session daemon.
3332 lta
->compatible
= 1;
3334 lta
->pid
= msg
->pid
;
3335 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3337 pthread_mutex_init(<a
->sock_lock
, NULL
);
3338 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3340 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3346 * For a given application object, add it to every hash table.
3348 void ust_app_add(struct ust_app
*app
)
3351 assert(app
->notify_sock
>= 0);
3356 * On a re-registration, we want to kick out the previous registration of
3359 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3362 * The socket _should_ be unique until _we_ call close. So, a add_unique
3363 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3364 * already in the table.
3366 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3368 /* Add application to the notify socket hash table. */
3369 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3370 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3372 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3373 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3374 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3381 * Set the application version into the object.
3383 * Return 0 on success else a negative value either an errno code or a
3384 * LTTng-UST error code.
3386 int ust_app_version(struct ust_app
*app
)
3392 pthread_mutex_lock(&app
->sock_lock
);
3393 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3394 pthread_mutex_unlock(&app
->sock_lock
);
3396 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3397 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3399 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3407 * Unregister app by removing it from the global traceable app list and freeing
3410 * The socket is already closed at this point so no close to sock.
3412 void ust_app_unregister(int sock
)
3414 struct ust_app
*lta
;
3415 struct lttng_ht_node_ulong
*node
;
3416 struct lttng_ht_iter ust_app_sock_iter
;
3417 struct lttng_ht_iter iter
;
3418 struct ust_app_session
*ua_sess
;
3423 /* Get the node reference for a call_rcu */
3424 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3425 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3428 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3429 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3432 * For per-PID buffers, perform "push metadata" and flush all
3433 * application streams before removing app from hash tables,
3434 * ensuring proper behavior of data_pending check.
3435 * Remove sessions so they are not visible during deletion.
3437 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3439 struct ust_registry_session
*registry
;
3441 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3443 /* The session was already removed so scheduled for teardown. */
3447 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3448 (void) ust_app_flush_app_session(lta
, ua_sess
);
3452 * Add session to list for teardown. This is safe since at this point we
3453 * are the only one using this list.
3455 pthread_mutex_lock(&ua_sess
->lock
);
3457 if (ua_sess
->deleted
) {
3458 pthread_mutex_unlock(&ua_sess
->lock
);
3463 * Normally, this is done in the delete session process which is
3464 * executed in the call rcu below. However, upon registration we can't
3465 * afford to wait for the grace period before pushing data or else the
3466 * data pending feature can race between the unregistration and stop
3467 * command where the data pending command is sent *before* the grace
3470 * The close metadata below nullifies the metadata pointer in the
3471 * session so the delete session will NOT push/close a second time.
3473 registry
= get_session_registry(ua_sess
);
3475 /* Push metadata for application before freeing the application. */
3476 (void) push_metadata(registry
, ua_sess
->consumer
);
3479 * Don't ask to close metadata for global per UID buffers. Close
3480 * metadata only on destroy trace session in this case. Also, the
3481 * previous push metadata could have flag the metadata registry to
3482 * close so don't send a close command if closed.
3484 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3485 /* And ask to close it for this session registry. */
3486 (void) close_metadata(registry
, ua_sess
->consumer
);
3489 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3491 pthread_mutex_unlock(&ua_sess
->lock
);
3494 /* Remove application from PID hash table */
3495 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3499 * Remove application from notify hash table. The thread handling the
3500 * notify socket could have deleted the node so ignore on error because
3501 * either way it's valid. The close of that socket is handled by the other
3504 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3505 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3508 * Ignore return value since the node might have been removed before by an
3509 * add replace during app registration because the PID can be reassigned by
3512 iter
.iter
.node
= <a
->pid_n
.node
;
3513 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3515 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3520 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3527 * Fill events array with all events name of all registered apps.
3529 int ust_app_list_events(struct lttng_event
**events
)
3532 size_t nbmem
, count
= 0;
3533 struct lttng_ht_iter iter
;
3534 struct ust_app
*app
;
3535 struct lttng_event
*tmp_event
;
3537 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3538 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3539 if (tmp_event
== NULL
) {
3540 PERROR("zmalloc ust app events");
3547 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3548 struct lttng_ust_tracepoint_iter uiter
;
3550 health_code_update();
3552 if (!app
->compatible
) {
3554 * TODO: In time, we should notice the caller of this error by
3555 * telling him that this is a version error.
3559 pthread_mutex_lock(&app
->sock_lock
);
3560 handle
= ustctl_tracepoint_list(app
->sock
);
3562 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3563 ERR("UST app list events getting handle failed for app pid %d",
3566 pthread_mutex_unlock(&app
->sock_lock
);
3570 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3571 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3572 /* Handle ustctl error. */
3576 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3577 ERR("UST app tp list get failed for app %d with ret %d",
3580 DBG3("UST app tp list get failed. Application is dead");
3582 * This is normal behavior, an application can die during the
3583 * creation process. Don't report an error so the execution can
3584 * continue normally. Continue normal execution.
3589 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3590 if (release_ret
< 0 &&
3591 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3592 release_ret
!= -EPIPE
) {
3593 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3595 pthread_mutex_unlock(&app
->sock_lock
);
3599 health_code_update();
3600 if (count
>= nbmem
) {
3601 /* In case the realloc fails, we free the memory */
3602 struct lttng_event
*new_tmp_event
;
3605 new_nbmem
= nbmem
<< 1;
3606 DBG2("Reallocating event list from %zu to %zu entries",
3608 new_tmp_event
= realloc(tmp_event
,
3609 new_nbmem
* sizeof(struct lttng_event
));
3610 if (new_tmp_event
== NULL
) {
3613 PERROR("realloc ust app events");
3616 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3617 if (release_ret
< 0 &&
3618 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3619 release_ret
!= -EPIPE
) {
3620 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3622 pthread_mutex_unlock(&app
->sock_lock
);
3625 /* Zero the new memory */
3626 memset(new_tmp_event
+ nbmem
, 0,
3627 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3629 tmp_event
= new_tmp_event
;
3631 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3632 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3633 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3634 tmp_event
[count
].pid
= app
->pid
;
3635 tmp_event
[count
].enabled
= -1;
3638 ret
= ustctl_release_handle(app
->sock
, handle
);
3639 pthread_mutex_unlock(&app
->sock_lock
);
3640 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3641 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3646 *events
= tmp_event
;
3648 DBG2("UST app list events done (%zu events)", count
);
3653 health_code_update();
3658 * Fill events array with all events name of all registered apps.
3660 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3663 size_t nbmem
, count
= 0;
3664 struct lttng_ht_iter iter
;
3665 struct ust_app
*app
;
3666 struct lttng_event_field
*tmp_event
;
3668 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3669 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3670 if (tmp_event
== NULL
) {
3671 PERROR("zmalloc ust app event fields");
3678 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3679 struct lttng_ust_field_iter uiter
;
3681 health_code_update();
3683 if (!app
->compatible
) {
3685 * TODO: In time, we should notice the caller of this error by
3686 * telling him that this is a version error.
3690 pthread_mutex_lock(&app
->sock_lock
);
3691 handle
= ustctl_tracepoint_field_list(app
->sock
);
3693 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3694 ERR("UST app list field getting handle failed for app pid %d",
3697 pthread_mutex_unlock(&app
->sock_lock
);
3701 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3702 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3703 /* Handle ustctl error. */
3707 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3708 ERR("UST app tp list field failed for app %d with ret %d",
3711 DBG3("UST app tp list field failed. Application is dead");
3713 * This is normal behavior, an application can die during the
3714 * creation process. Don't report an error so the execution can
3715 * continue normally. Reset list and count for next app.
3720 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3721 pthread_mutex_unlock(&app
->sock_lock
);
3722 if (release_ret
< 0 &&
3723 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3724 release_ret
!= -EPIPE
) {
3725 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3730 health_code_update();
3731 if (count
>= nbmem
) {
3732 /* In case the realloc fails, we free the memory */
3733 struct lttng_event_field
*new_tmp_event
;
3736 new_nbmem
= nbmem
<< 1;
3737 DBG2("Reallocating event field list from %zu to %zu entries",
3739 new_tmp_event
= realloc(tmp_event
,
3740 new_nbmem
* sizeof(struct lttng_event_field
));
3741 if (new_tmp_event
== NULL
) {
3744 PERROR("realloc ust app event fields");
3747 release_ret
= ustctl_release_handle(app
->sock
, handle
);
3748 pthread_mutex_unlock(&app
->sock_lock
);
3750 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
3751 release_ret
!= -EPIPE
) {
3752 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
3756 /* Zero the new memory */
3757 memset(new_tmp_event
+ nbmem
, 0,
3758 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3760 tmp_event
= new_tmp_event
;
3763 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3764 /* Mapping between these enums matches 1 to 1. */
3765 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3766 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3768 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3769 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3770 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3771 tmp_event
[count
].event
.pid
= app
->pid
;
3772 tmp_event
[count
].event
.enabled
= -1;
3775 ret
= ustctl_release_handle(app
->sock
, handle
);
3776 pthread_mutex_unlock(&app
->sock_lock
);
3778 ret
!= -LTTNG_UST_ERR_EXITING
&&
3780 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
3785 *fields
= tmp_event
;
3787 DBG2("UST app list event fields done (%zu events)", count
);
3792 health_code_update();
3797 * Free and clean all traceable apps of the global list.
3799 * Should _NOT_ be called with RCU read-side lock held.
3801 void ust_app_clean_list(void)
3804 struct ust_app
*app
;
3805 struct lttng_ht_iter iter
;
3807 DBG2("UST app cleaning registered apps hash table");
3812 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3813 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3815 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3819 /* Cleanup socket hash table */
3820 if (ust_app_ht_by_sock
) {
3821 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3823 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3828 /* Cleanup notify socket hash table */
3829 if (ust_app_ht_by_notify_sock
) {
3830 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3831 notify_sock_n
.node
) {
3832 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3838 /* Destroy is done only when the ht is empty */
3840 ht_cleanup_push(ust_app_ht
);
3842 if (ust_app_ht_by_sock
) {
3843 ht_cleanup_push(ust_app_ht_by_sock
);
3845 if (ust_app_ht_by_notify_sock
) {
3846 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3851 * Init UST app hash table.
3853 int ust_app_ht_alloc(void)
3855 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3859 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3860 if (!ust_app_ht_by_sock
) {
3863 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3864 if (!ust_app_ht_by_notify_sock
) {
3871 * For a specific UST session, disable the channel for all registered apps.
3873 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3874 struct ltt_ust_channel
*uchan
)
3877 struct lttng_ht_iter iter
;
3878 struct lttng_ht_node_str
*ua_chan_node
;
3879 struct ust_app
*app
;
3880 struct ust_app_session
*ua_sess
;
3881 struct ust_app_channel
*ua_chan
;
3883 if (usess
== NULL
|| uchan
== NULL
) {
3884 ERR("Disabling UST global channel with NULL values");
3889 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3890 uchan
->name
, usess
->id
);
3894 /* For every registered applications */
3895 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3896 struct lttng_ht_iter uiter
;
3897 if (!app
->compatible
) {
3899 * TODO: In time, we should notice the caller of this error by
3900 * telling him that this is a version error.
3904 ua_sess
= lookup_session_by_app(usess
, app
);
3905 if (ua_sess
== NULL
) {
3910 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3911 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3912 /* If the session if found for the app, the channel must be there */
3913 assert(ua_chan_node
);
3915 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3916 /* The channel must not be already disabled */
3917 assert(ua_chan
->enabled
== 1);
3919 /* Disable channel onto application */
3920 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3922 /* XXX: We might want to report this error at some point... */
3934 * For a specific UST session, enable the channel for all registered apps.
3936 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3937 struct ltt_ust_channel
*uchan
)
3940 struct lttng_ht_iter iter
;
3941 struct ust_app
*app
;
3942 struct ust_app_session
*ua_sess
;
3944 if (usess
== NULL
|| uchan
== NULL
) {
3945 ERR("Adding UST global channel to NULL values");
3950 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3951 uchan
->name
, usess
->id
);
3955 /* For every registered applications */
3956 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3957 if (!app
->compatible
) {
3959 * TODO: In time, we should notice the caller of this error by
3960 * telling him that this is a version error.
3964 ua_sess
= lookup_session_by_app(usess
, app
);
3965 if (ua_sess
== NULL
) {
3969 /* Enable channel onto application */
3970 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3972 /* XXX: We might want to report this error at some point... */
3984 * Disable an event in a channel and for a specific session.
3986 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3987 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3990 struct lttng_ht_iter iter
, uiter
;
3991 struct lttng_ht_node_str
*ua_chan_node
;
3992 struct ust_app
*app
;
3993 struct ust_app_session
*ua_sess
;
3994 struct ust_app_channel
*ua_chan
;
3995 struct ust_app_event
*ua_event
;
3997 DBG("UST app disabling event %s for all apps in channel "
3998 "%s for session id %" PRIu64
,
3999 uevent
->attr
.name
, uchan
->name
, usess
->id
);
4003 /* For all registered applications */
4004 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4005 if (!app
->compatible
) {
4007 * TODO: In time, we should notice the caller of this error by
4008 * telling him that this is a version error.
4012 ua_sess
= lookup_session_by_app(usess
, app
);
4013 if (ua_sess
== NULL
) {
4018 /* Lookup channel in the ust app session */
4019 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4020 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4021 if (ua_chan_node
== NULL
) {
4022 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
4023 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
4026 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4028 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4029 uevent
->filter
, uevent
->attr
.loglevel
,
4031 if (ua_event
== NULL
) {
4032 DBG2("Event %s not found in channel %s for app pid %d."
4033 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
4037 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4039 /* XXX: Report error someday... */
4050 * For a specific UST session, create the channel for all registered apps.
4052 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
4053 struct ltt_ust_channel
*uchan
)
4055 int ret
= 0, created
;
4056 struct lttng_ht_iter iter
;
4057 struct ust_app
*app
;
4058 struct ust_app_session
*ua_sess
= NULL
;
4060 /* Very wrong code flow */
4064 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
4065 uchan
->name
, usess
->id
);
4069 /* For every registered applications */
4070 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4071 if (!app
->compatible
) {
4073 * TODO: In time, we should notice the caller of this error by
4074 * telling him that this is a version error.
4078 if (!trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4084 * Create session on the tracer side and add it to app session HT. Note
4085 * that if session exist, it will simply return a pointer to the ust
4088 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
4093 * The application's socket is not valid. Either a bad socket
4094 * or a timeout on it. We can't inform the caller that for a
4095 * specific app, the session failed so lets continue here.
4097 ret
= 0; /* Not an error. */
4101 goto error_rcu_unlock
;
4106 pthread_mutex_lock(&ua_sess
->lock
);
4108 if (ua_sess
->deleted
) {
4109 pthread_mutex_unlock(&ua_sess
->lock
);
4113 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
4114 sizeof(uchan
->name
))) {
4115 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
4118 /* Create channel onto application. We don't need the chan ref. */
4119 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
4120 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
4122 pthread_mutex_unlock(&ua_sess
->lock
);
4124 /* Cleanup the created session if it's the case. */
4126 destroy_app_session(app
, ua_sess
);
4131 * The application's socket is not valid. Either a bad socket
4132 * or a timeout on it. We can't inform the caller that for a
4133 * specific app, the session failed so lets continue here.
4135 ret
= 0; /* Not an error. */
4139 goto error_rcu_unlock
;
4150 * Enable event for a specific session and channel on the tracer.
4152 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
4153 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4156 struct lttng_ht_iter iter
, uiter
;
4157 struct lttng_ht_node_str
*ua_chan_node
;
4158 struct ust_app
*app
;
4159 struct ust_app_session
*ua_sess
;
4160 struct ust_app_channel
*ua_chan
;
4161 struct ust_app_event
*ua_event
;
4163 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
4164 uevent
->attr
.name
, usess
->id
);
4167 * NOTE: At this point, this function is called only if the session and
4168 * channel passed are already created for all apps. and enabled on the
4174 /* For all registered applications */
4175 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4176 if (!app
->compatible
) {
4178 * TODO: In time, we should notice the caller of this error by
4179 * telling him that this is a version error.
4183 ua_sess
= lookup_session_by_app(usess
, app
);
4185 /* The application has problem or is probably dead. */
4189 pthread_mutex_lock(&ua_sess
->lock
);
4191 if (ua_sess
->deleted
) {
4192 pthread_mutex_unlock(&ua_sess
->lock
);
4196 /* Lookup channel in the ust app session */
4197 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4198 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4200 * It is possible that the channel cannot be found is
4201 * the channel/event creation occurs concurrently with
4202 * an application exit.
4204 if (!ua_chan_node
) {
4205 pthread_mutex_unlock(&ua_sess
->lock
);
4209 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4211 /* Get event node */
4212 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4213 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4214 if (ua_event
== NULL
) {
4215 DBG3("UST app enable event %s not found for app PID %d."
4216 "Skipping app", uevent
->attr
.name
, app
->pid
);
4220 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4222 pthread_mutex_unlock(&ua_sess
->lock
);
4226 pthread_mutex_unlock(&ua_sess
->lock
);
4235 * For a specific existing UST session and UST channel, creates the event for
4236 * all registered apps.
4238 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4239 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4242 struct lttng_ht_iter iter
, uiter
;
4243 struct lttng_ht_node_str
*ua_chan_node
;
4244 struct ust_app
*app
;
4245 struct ust_app_session
*ua_sess
;
4246 struct ust_app_channel
*ua_chan
;
4248 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4249 uevent
->attr
.name
, usess
->id
);
4253 /* For all registered applications */
4254 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4255 if (!app
->compatible
) {
4257 * TODO: In time, we should notice the caller of this error by
4258 * telling him that this is a version error.
4262 ua_sess
= lookup_session_by_app(usess
, app
);
4264 /* The application has problem or is probably dead. */
4268 pthread_mutex_lock(&ua_sess
->lock
);
4270 if (ua_sess
->deleted
) {
4271 pthread_mutex_unlock(&ua_sess
->lock
);
4275 /* Lookup channel in the ust app session */
4276 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4277 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4278 /* If the channel is not found, there is a code flow error */
4279 assert(ua_chan_node
);
4281 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4283 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4284 pthread_mutex_unlock(&ua_sess
->lock
);
4286 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4287 /* Possible value at this point: -ENOMEM. If so, we stop! */
4290 DBG2("UST app event %s already exist on app PID %d",
4291 uevent
->attr
.name
, app
->pid
);
4302 * Start tracing for a specific UST session and app.
4305 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4308 struct ust_app_session
*ua_sess
;
4310 DBG("Starting tracing for ust app pid %d", app
->pid
);
4314 if (!app
->compatible
) {
4318 ua_sess
= lookup_session_by_app(usess
, app
);
4319 if (ua_sess
== NULL
) {
4320 /* The session is in teardown process. Ignore and continue. */
4324 pthread_mutex_lock(&ua_sess
->lock
);
4326 if (ua_sess
->deleted
) {
4327 pthread_mutex_unlock(&ua_sess
->lock
);
4331 /* Upon restart, we skip the setup, already done */
4332 if (ua_sess
->started
) {
4336 /* Create directories if consumer is LOCAL and has a path defined. */
4337 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
4338 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
4339 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
4340 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
4342 if (errno
!= EEXIST
) {
4343 ERR("Trace directory creation error");
4350 * Create the metadata for the application. This returns gracefully if a
4351 * metadata was already set for the session.
4353 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4358 health_code_update();
4361 /* This start the UST tracing */
4362 pthread_mutex_lock(&app
->sock_lock
);
4363 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4364 pthread_mutex_unlock(&app
->sock_lock
);
4366 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4367 ERR("Error starting tracing for app pid: %d (ret: %d)",
4370 DBG("UST app start session failed. Application is dead.");
4372 * This is normal behavior, an application can die during the
4373 * creation process. Don't report an error so the execution can
4374 * continue normally.
4376 pthread_mutex_unlock(&ua_sess
->lock
);
4382 /* Indicate that the session has been started once */
4383 ua_sess
->started
= 1;
4385 pthread_mutex_unlock(&ua_sess
->lock
);
4387 health_code_update();
4389 /* Quiescent wait after starting trace */
4390 pthread_mutex_lock(&app
->sock_lock
);
4391 ret
= ustctl_wait_quiescent(app
->sock
);
4392 pthread_mutex_unlock(&app
->sock_lock
);
4393 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4394 ERR("UST app wait quiescent failed for app pid %d ret %d",
4400 health_code_update();
4404 pthread_mutex_unlock(&ua_sess
->lock
);
4406 health_code_update();
4411 * Stop tracing for a specific UST session and app.
4414 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4417 struct ust_app_session
*ua_sess
;
4418 struct ust_registry_session
*registry
;
4420 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4424 if (!app
->compatible
) {
4425 goto end_no_session
;
4428 ua_sess
= lookup_session_by_app(usess
, app
);
4429 if (ua_sess
== NULL
) {
4430 goto end_no_session
;
4433 pthread_mutex_lock(&ua_sess
->lock
);
4435 if (ua_sess
->deleted
) {
4436 pthread_mutex_unlock(&ua_sess
->lock
);
4437 goto end_no_session
;
4441 * If started = 0, it means that stop trace has been called for a session
4442 * that was never started. It's possible since we can have a fail start
4443 * from either the application manager thread or the command thread. Simply
4444 * indicate that this is a stop error.
4446 if (!ua_sess
->started
) {
4447 goto error_rcu_unlock
;
4450 health_code_update();
4452 /* This inhibits UST tracing */
4453 pthread_mutex_lock(&app
->sock_lock
);
4454 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4455 pthread_mutex_unlock(&app
->sock_lock
);
4457 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4458 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4461 DBG("UST app stop session failed. Application is dead.");
4463 * This is normal behavior, an application can die during the
4464 * creation process. Don't report an error so the execution can
4465 * continue normally.
4469 goto error_rcu_unlock
;
4472 health_code_update();
4474 /* Quiescent wait after stopping trace */
4475 pthread_mutex_lock(&app
->sock_lock
);
4476 ret
= ustctl_wait_quiescent(app
->sock
);
4477 pthread_mutex_unlock(&app
->sock_lock
);
4478 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4479 ERR("UST app wait quiescent failed for app pid %d ret %d",
4483 health_code_update();
4485 registry
= get_session_registry(ua_sess
);
4488 /* Push metadata for application before freeing the application. */
4489 (void) push_metadata(registry
, ua_sess
->consumer
);
4492 pthread_mutex_unlock(&ua_sess
->lock
);
4495 health_code_update();
4499 pthread_mutex_unlock(&ua_sess
->lock
);
4501 health_code_update();
4506 int ust_app_flush_app_session(struct ust_app
*app
,
4507 struct ust_app_session
*ua_sess
)
4509 int ret
, retval
= 0;
4510 struct lttng_ht_iter iter
;
4511 struct ust_app_channel
*ua_chan
;
4512 struct consumer_socket
*socket
;
4514 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4518 if (!app
->compatible
) {
4519 goto end_not_compatible
;
4522 pthread_mutex_lock(&ua_sess
->lock
);
4524 if (ua_sess
->deleted
) {
4528 health_code_update();
4530 /* Flushing buffers */
4531 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4534 /* Flush buffers and push metadata. */
4535 switch (ua_sess
->buffer_type
) {
4536 case LTTNG_BUFFER_PER_PID
:
4537 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4539 health_code_update();
4540 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4542 ERR("Error flushing consumer channel");
4548 case LTTNG_BUFFER_PER_UID
:
4554 health_code_update();
4557 pthread_mutex_unlock(&ua_sess
->lock
);
4561 health_code_update();
4566 * Flush buffers for all applications for a specific UST session.
4567 * Called with UST session lock held.
4570 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4575 DBG("Flushing session buffers for all ust apps");
4579 /* Flush buffers and push metadata. */
4580 switch (usess
->buffer_type
) {
4581 case LTTNG_BUFFER_PER_UID
:
4583 struct buffer_reg_uid
*reg
;
4584 struct lttng_ht_iter iter
;
4586 /* Flush all per UID buffers associated to that session. */
4587 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4588 struct ust_registry_session
*ust_session_reg
;
4589 struct buffer_reg_channel
*reg_chan
;
4590 struct consumer_socket
*socket
;
4592 /* Get consumer socket to use to push the metadata.*/
4593 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4596 /* Ignore request if no consumer is found for the session. */
4600 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4601 reg_chan
, node
.node
) {
4603 * The following call will print error values so the return
4604 * code is of little importance because whatever happens, we
4605 * have to try them all.
4607 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4610 ust_session_reg
= reg
->registry
->reg
.ust
;
4611 /* Push metadata. */
4612 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4616 case LTTNG_BUFFER_PER_PID
:
4618 struct ust_app_session
*ua_sess
;
4619 struct lttng_ht_iter iter
;
4620 struct ust_app
*app
;
4622 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4623 ua_sess
= lookup_session_by_app(usess
, app
);
4624 if (ua_sess
== NULL
) {
4627 (void) ust_app_flush_app_session(app
, ua_sess
);
4638 health_code_update();
4643 * Destroy a specific UST session in apps.
4645 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4648 struct ust_app_session
*ua_sess
;
4649 struct lttng_ht_iter iter
;
4650 struct lttng_ht_node_u64
*node
;
4652 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4656 if (!app
->compatible
) {
4660 __lookup_session_by_app(usess
, app
, &iter
);
4661 node
= lttng_ht_iter_get_node_u64(&iter
);
4663 /* Session is being or is deleted. */
4666 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4668 health_code_update();
4669 destroy_app_session(app
, ua_sess
);
4671 health_code_update();
4673 /* Quiescent wait after stopping trace */
4674 pthread_mutex_lock(&app
->sock_lock
);
4675 ret
= ustctl_wait_quiescent(app
->sock
);
4676 pthread_mutex_unlock(&app
->sock_lock
);
4677 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4678 ERR("UST app wait quiescent failed for app pid %d ret %d",
4683 health_code_update();
4688 * Start tracing for the UST session.
4690 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4693 struct lttng_ht_iter iter
;
4694 struct ust_app
*app
;
4696 DBG("Starting all UST traces");
4700 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4701 ret
= ust_app_start_trace(usess
, app
);
4703 /* Continue to next apps even on error */
4714 * Start tracing for the UST session.
4715 * Called with UST session lock held.
4717 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4720 struct lttng_ht_iter iter
;
4721 struct ust_app
*app
;
4723 DBG("Stopping all UST traces");
4727 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4728 ret
= ust_app_stop_trace(usess
, app
);
4730 /* Continue to next apps even on error */
4735 (void) ust_app_flush_session(usess
);
4743 * Destroy app UST session.
4745 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4748 struct lttng_ht_iter iter
;
4749 struct ust_app
*app
;
4751 DBG("Destroy all UST traces");
4755 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4756 ret
= destroy_trace(usess
, app
);
4758 /* Continue to next apps even on error */
4769 void ust_app_global_create(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4772 struct lttng_ht_iter iter
, uiter
;
4773 struct ust_app_session
*ua_sess
= NULL
;
4774 struct ust_app_channel
*ua_chan
;
4775 struct ust_app_event
*ua_event
;
4776 struct ust_app_ctx
*ua_ctx
;
4779 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &is_created
);
4781 /* Tracer is probably gone or ENOMEM. */
4785 /* App session already created. */
4790 pthread_mutex_lock(&ua_sess
->lock
);
4792 if (ua_sess
->deleted
) {
4793 pthread_mutex_unlock(&ua_sess
->lock
);
4798 * We can iterate safely here over all UST app session since the create ust
4799 * app session above made a shadow copy of the UST global domain from the
4802 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4804 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4805 if (ret
< 0 && ret
!= -ENOTCONN
) {
4807 * Stop everything. On error, the application
4808 * failed, no more file descriptor are available
4809 * or ENOMEM so stopping here is the only thing
4810 * we can do for now. The only exception is
4811 * -ENOTCONN, which indicates that the application
4818 * Add context using the list so they are enabled in the same order the
4821 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
4822 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
4829 /* For each events */
4830 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
4832 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
4839 pthread_mutex_unlock(&ua_sess
->lock
);
4841 if (usess
->active
) {
4842 ret
= ust_app_start_trace(usess
, app
);
4847 DBG2("UST trace started for app pid %d", app
->pid
);
4850 /* Everything went well at this point. */
4854 pthread_mutex_unlock(&ua_sess
->lock
);
4857 destroy_app_session(app
, ua_sess
);
4863 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4865 struct ust_app_session
*ua_sess
;
4867 ua_sess
= lookup_session_by_app(usess
, app
);
4868 if (ua_sess
== NULL
) {
4871 destroy_app_session(app
, ua_sess
);
4875 * Add channels/events from UST global domain to registered apps at sock.
4877 * Called with session lock held.
4878 * Called with RCU read-side lock held.
4880 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4884 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
4885 app
->sock
, usess
->id
);
4887 if (!app
->compatible
) {
4891 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4892 ust_app_global_create(usess
, app
);
4894 ust_app_global_destroy(usess
, app
);
4899 * Called with session lock held.
4901 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
4903 struct lttng_ht_iter iter
;
4904 struct ust_app
*app
;
4907 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4908 ust_app_global_update(usess
, app
);
4914 * Add context to a specific channel for global UST domain.
4916 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
4917 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
4920 struct lttng_ht_node_str
*ua_chan_node
;
4921 struct lttng_ht_iter iter
, uiter
;
4922 struct ust_app_channel
*ua_chan
= NULL
;
4923 struct ust_app_session
*ua_sess
;
4924 struct ust_app
*app
;
4928 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4929 if (!app
->compatible
) {
4931 * TODO: In time, we should notice the caller of this error by
4932 * telling him that this is a version error.
4936 ua_sess
= lookup_session_by_app(usess
, app
);
4937 if (ua_sess
== NULL
) {
4941 pthread_mutex_lock(&ua_sess
->lock
);
4943 if (ua_sess
->deleted
) {
4944 pthread_mutex_unlock(&ua_sess
->lock
);
4948 /* Lookup channel in the ust app session */
4949 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4950 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4951 if (ua_chan_node
== NULL
) {
4954 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4956 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4961 pthread_mutex_unlock(&ua_sess
->lock
);
4969 * Enable event for a channel from a UST session for a specific PID.
4971 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4972 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4975 struct lttng_ht_iter iter
;
4976 struct lttng_ht_node_str
*ua_chan_node
;
4977 struct ust_app
*app
;
4978 struct ust_app_session
*ua_sess
;
4979 struct ust_app_channel
*ua_chan
;
4980 struct ust_app_event
*ua_event
;
4982 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4986 app
= ust_app_find_by_pid(pid
);
4988 ERR("UST app enable event per PID %d not found", pid
);
4993 if (!app
->compatible
) {
4998 ua_sess
= lookup_session_by_app(usess
, app
);
5000 /* The application has problem or is probably dead. */
5005 pthread_mutex_lock(&ua_sess
->lock
);
5007 if (ua_sess
->deleted
) {
5012 /* Lookup channel in the ust app session */
5013 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
5014 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
5015 /* If the channel is not found, there is a code flow error */
5016 assert(ua_chan_node
);
5018 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5020 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
5021 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
5022 if (ua_event
== NULL
) {
5023 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
5028 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
5035 pthread_mutex_unlock(&ua_sess
->lock
);
5042 * Calibrate registered applications.
5044 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
5047 struct lttng_ht_iter iter
;
5048 struct ust_app
*app
;
5052 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5053 if (!app
->compatible
) {
5055 * TODO: In time, we should notice the caller of this error by
5056 * telling him that this is a version error.
5061 health_code_update();
5063 pthread_mutex_lock(&app
->sock_lock
);
5064 ret
= ustctl_calibrate(app
->sock
, calibrate
);
5065 pthread_mutex_unlock(&app
->sock_lock
);
5069 /* Means that it's not implemented on the tracer side. */
5073 DBG2("Calibrate app PID %d returned with error %d",
5080 DBG("UST app global domain calibration finished");
5084 health_code_update();
5090 * Receive registration and populate the given msg structure.
5092 * On success return 0 else a negative value returned by the ustctl call.
5094 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
5097 uint32_t pid
, ppid
, uid
, gid
;
5101 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
5102 &pid
, &ppid
, &uid
, &gid
,
5103 &msg
->bits_per_long
,
5104 &msg
->uint8_t_alignment
,
5105 &msg
->uint16_t_alignment
,
5106 &msg
->uint32_t_alignment
,
5107 &msg
->uint64_t_alignment
,
5108 &msg
->long_alignment
,
5115 case LTTNG_UST_ERR_EXITING
:
5116 DBG3("UST app recv reg message failed. Application died");
5118 case LTTNG_UST_ERR_UNSUP_MAJOR
:
5119 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5120 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
5121 LTTNG_UST_ABI_MINOR_VERSION
);
5124 ERR("UST app recv reg message failed with ret %d", ret
);
5129 msg
->pid
= (pid_t
) pid
;
5130 msg
->ppid
= (pid_t
) ppid
;
5131 msg
->uid
= (uid_t
) uid
;
5132 msg
->gid
= (gid_t
) gid
;
5139 * Return a ust app session object using the application object and the
5140 * session object descriptor has a key. If not found, NULL is returned.
5141 * A RCU read side lock MUST be acquired when calling this function.
5143 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
5146 struct lttng_ht_node_ulong
*node
;
5147 struct lttng_ht_iter iter
;
5148 struct ust_app_session
*ua_sess
= NULL
;
5152 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
5153 node
= lttng_ht_iter_get_node_ulong(&iter
);
5155 DBG2("UST app session find by objd %d not found", objd
);
5159 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
5166 * Return a ust app channel object using the application object and the channel
5167 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5168 * lock MUST be acquired before calling this function.
5170 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
5173 struct lttng_ht_node_ulong
*node
;
5174 struct lttng_ht_iter iter
;
5175 struct ust_app_channel
*ua_chan
= NULL
;
5179 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
5180 node
= lttng_ht_iter_get_node_ulong(&iter
);
5182 DBG2("UST app channel find by objd %d not found", objd
);
5186 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
5193 * Reply to a register channel notification from an application on the notify
5194 * socket. The channel metadata is also created.
5196 * The session UST registry lock is acquired in this function.
5198 * On success 0 is returned else a negative value.
5200 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
5201 size_t nr_fields
, struct ustctl_field
*fields
)
5203 int ret
, ret_code
= 0;
5204 uint32_t chan_id
, reg_count
;
5205 uint64_t chan_reg_key
;
5206 enum ustctl_channel_header type
;
5207 struct ust_app
*app
;
5208 struct ust_app_channel
*ua_chan
;
5209 struct ust_app_session
*ua_sess
;
5210 struct ust_registry_session
*registry
;
5211 struct ust_registry_channel
*chan_reg
;
5215 /* Lookup application. If not found, there is a code flow error. */
5216 app
= find_app_by_notify_sock(sock
);
5218 DBG("Application socket %d is being teardown. Abort event notify",
5222 goto error_rcu_unlock
;
5225 /* Lookup channel by UST object descriptor. */
5226 ua_chan
= find_channel_by_objd(app
, cobjd
);
5228 DBG("Application channel is being teardown. Abort event notify");
5231 goto error_rcu_unlock
;
5234 assert(ua_chan
->session
);
5235 ua_sess
= ua_chan
->session
;
5237 /* Get right session registry depending on the session buffer type. */
5238 registry
= get_session_registry(ua_sess
);
5241 /* Depending on the buffer type, a different channel key is used. */
5242 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5243 chan_reg_key
= ua_chan
->tracing_channel_id
;
5245 chan_reg_key
= ua_chan
->key
;
5248 pthread_mutex_lock(®istry
->lock
);
5250 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
5253 if (!chan_reg
->register_done
) {
5254 reg_count
= ust_registry_get_event_count(chan_reg
);
5255 if (reg_count
< 31) {
5256 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
5258 type
= USTCTL_CHANNEL_HEADER_LARGE
;
5261 chan_reg
->nr_ctx_fields
= nr_fields
;
5262 chan_reg
->ctx_fields
= fields
;
5263 chan_reg
->header_type
= type
;
5265 /* Get current already assigned values. */
5266 type
= chan_reg
->header_type
;
5268 /* Set to NULL so the error path does not do a double free. */
5271 /* Channel id is set during the object creation. */
5272 chan_id
= chan_reg
->chan_id
;
5274 /* Append to metadata */
5275 if (!chan_reg
->metadata_dumped
) {
5276 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5278 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5284 DBG3("UST app replying to register channel key %" PRIu64
5285 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5288 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5290 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5291 ERR("UST app reply channel failed with ret %d", ret
);
5293 DBG3("UST app reply channel failed. Application died");
5298 /* This channel registry registration is completed. */
5299 chan_reg
->register_done
= 1;
5302 pthread_mutex_unlock(®istry
->lock
);
5312 * Add event to the UST channel registry. When the event is added to the
5313 * registry, the metadata is also created. Once done, this replies to the
5314 * application with the appropriate error code.
5316 * The session UST registry lock is acquired in the function.
5318 * On success 0 is returned else a negative value.
5320 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5321 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
5322 int loglevel_value
, char *model_emf_uri
)
5325 uint32_t event_id
= 0;
5326 uint64_t chan_reg_key
;
5327 struct ust_app
*app
;
5328 struct ust_app_channel
*ua_chan
;
5329 struct ust_app_session
*ua_sess
;
5330 struct ust_registry_session
*registry
;
5334 /* Lookup application. If not found, there is a code flow error. */
5335 app
= find_app_by_notify_sock(sock
);
5337 DBG("Application socket %d is being teardown. Abort event notify",
5342 free(model_emf_uri
);
5343 goto error_rcu_unlock
;
5346 /* Lookup channel by UST object descriptor. */
5347 ua_chan
= find_channel_by_objd(app
, cobjd
);
5349 DBG("Application channel is being teardown. Abort event notify");
5353 free(model_emf_uri
);
5354 goto error_rcu_unlock
;
5357 assert(ua_chan
->session
);
5358 ua_sess
= ua_chan
->session
;
5360 registry
= get_session_registry(ua_sess
);
5363 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5364 chan_reg_key
= ua_chan
->tracing_channel_id
;
5366 chan_reg_key
= ua_chan
->key
;
5369 pthread_mutex_lock(®istry
->lock
);
5372 * From this point on, this call acquires the ownership of the sig, fields
5373 * and model_emf_uri meaning any free are done inside it if needed. These
5374 * three variables MUST NOT be read/write after this.
5376 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5377 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
5378 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
5382 * The return value is returned to ustctl so in case of an error, the
5383 * application can be notified. In case of an error, it's important not to
5384 * return a negative error or else the application will get closed.
5386 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5388 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5389 ERR("UST app reply event failed with ret %d", ret
);
5391 DBG3("UST app reply event failed. Application died");
5394 * No need to wipe the create event since the application socket will
5395 * get close on error hence cleaning up everything by itself.
5400 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5404 pthread_mutex_unlock(®istry
->lock
);
5411 * Add enum to the UST session registry. Once done, this replies to the
5412 * application with the appropriate error code.
5414 * The session UST registry lock is acquired within this function.
5416 * On success 0 is returned else a negative value.
5418 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
5419 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
5421 int ret
= 0, ret_code
;
5422 struct ust_app
*app
;
5423 struct ust_app_session
*ua_sess
;
5424 struct ust_registry_session
*registry
;
5425 uint64_t enum_id
= -1ULL;
5429 /* Lookup application. If not found, there is a code flow error. */
5430 app
= find_app_by_notify_sock(sock
);
5432 /* Return an error since this is not an error */
5433 DBG("Application socket %d is being torn down. Aborting enum registration",
5436 goto error_rcu_unlock
;
5439 /* Lookup session by UST object descriptor. */
5440 ua_sess
= find_session_by_objd(app
, sobjd
);
5442 /* Return an error since this is not an error */
5443 DBG("Application session is being torn down. Aborting enum registration.");
5445 goto error_rcu_unlock
;
5448 registry
= get_session_registry(ua_sess
);
5451 pthread_mutex_lock(®istry
->lock
);
5454 * From this point on, the callee acquires the ownership of
5455 * entries. The variable entries MUST NOT be read/written after
5458 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
5459 entries
, nr_entries
, &enum_id
);
5463 * The return value is returned to ustctl so in case of an error, the
5464 * application can be notified. In case of an error, it's important not to
5465 * return a negative error or else the application will get closed.
5467 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
5469 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5470 ERR("UST app reply enum failed with ret %d", ret
);
5472 DBG3("UST app reply enum failed. Application died");
5475 * No need to wipe the create enum since the application socket will
5476 * get close on error hence cleaning up everything by itself.
5481 DBG3("UST registry enum %s added successfully or already found", name
);
5484 pthread_mutex_unlock(®istry
->lock
);
5491 * Handle application notification through the given notify socket.
5493 * Return 0 on success or else a negative value.
5495 int ust_app_recv_notify(int sock
)
5498 enum ustctl_notify_cmd cmd
;
5500 DBG3("UST app receiving notify from sock %d", sock
);
5502 ret
= ustctl_recv_notify(sock
, &cmd
);
5504 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5505 ERR("UST app recv notify failed with ret %d", ret
);
5507 DBG3("UST app recv notify failed. Application died");
5513 case USTCTL_NOTIFY_CMD_EVENT
:
5515 int sobjd
, cobjd
, loglevel_value
;
5516 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5518 struct ustctl_field
*fields
;
5520 DBG2("UST app ustctl register event received");
5522 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
5523 &loglevel_value
, &sig
, &nr_fields
, &fields
,
5526 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5527 ERR("UST app recv event failed with ret %d", ret
);
5529 DBG3("UST app recv event failed. Application died");
5535 * Add event to the UST registry coming from the notify socket. This
5536 * call will free if needed the sig, fields and model_emf_uri. This
5537 * code path loses the ownsership of these variables and transfer them
5538 * to the this function.
5540 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5541 fields
, loglevel_value
, model_emf_uri
);
5548 case USTCTL_NOTIFY_CMD_CHANNEL
:
5552 struct ustctl_field
*fields
;
5554 DBG2("UST app ustctl register channel received");
5556 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5559 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5560 ERR("UST app recv channel failed with ret %d", ret
);
5562 DBG3("UST app recv channel failed. Application died");
5568 * The fields ownership are transfered to this function call meaning
5569 * that if needed it will be freed. After this, it's invalid to access
5570 * fields or clean it up.
5572 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
5580 case USTCTL_NOTIFY_CMD_ENUM
:
5583 char name
[LTTNG_UST_SYM_NAME_LEN
];
5585 struct ustctl_enum_entry
*entries
;
5587 DBG2("UST app ustctl register enum received");
5589 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
5590 &entries
, &nr_entries
);
5592 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5593 ERR("UST app recv enum failed with ret %d", ret
);
5595 DBG3("UST app recv enum failed. Application died");
5600 /* Callee assumes ownership of entries */
5601 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
5602 entries
, nr_entries
);
5610 /* Should NEVER happen. */
5619 * Once the notify socket hangs up, this is called. First, it tries to find the
5620 * corresponding application. On failure, the call_rcu to close the socket is
5621 * executed. If an application is found, it tries to delete it from the notify
5622 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5624 * Note that an object needs to be allocated here so on ENOMEM failure, the
5625 * call RCU is not done but the rest of the cleanup is.
5627 void ust_app_notify_sock_unregister(int sock
)
5630 struct lttng_ht_iter iter
;
5631 struct ust_app
*app
;
5632 struct ust_app_notify_sock_obj
*obj
;
5638 obj
= zmalloc(sizeof(*obj
));
5641 * An ENOMEM is kind of uncool. If this strikes we continue the
5642 * procedure but the call_rcu will not be called. In this case, we
5643 * accept the fd leak rather than possibly creating an unsynchronized
5644 * state between threads.
5646 * TODO: The notify object should be created once the notify socket is
5647 * registered and stored independantely from the ust app object. The
5648 * tricky part is to synchronize the teardown of the application and
5649 * this notify object. Let's keep that in mind so we can avoid this
5650 * kind of shenanigans with ENOMEM in the teardown path.
5657 DBG("UST app notify socket unregister %d", sock
);
5660 * Lookup application by notify socket. If this fails, this means that the
5661 * hash table delete has already been done by the application
5662 * unregistration process so we can safely close the notify socket in a
5665 app
= find_app_by_notify_sock(sock
);
5670 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5673 * Whatever happens here either we fail or succeed, in both cases we have
5674 * to close the socket after a grace period to continue to the call RCU
5675 * here. If the deletion is successful, the application is not visible
5676 * anymore by other threads and is it fails it means that it was already
5677 * deleted from the hash table so either way we just have to close the
5680 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5686 * Close socket after a grace period to avoid for the socket to be reused
5687 * before the application object is freed creating potential race between
5688 * threads trying to add unique in the global hash table.
5691 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5696 * Destroy a ust app data structure and free its memory.
5698 void ust_app_destroy(struct ust_app
*app
)
5704 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5708 * Take a snapshot for a given UST session. The snapshot is sent to the given
5711 * Return 0 on success or else a negative value.
5713 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5714 struct snapshot_output
*output
, int wait
,
5715 uint64_t nb_packets_per_stream
)
5718 unsigned int snapshot_done
= 0;
5719 struct lttng_ht_iter iter
;
5720 struct ust_app
*app
;
5721 char pathname
[PATH_MAX
];
5728 switch (usess
->buffer_type
) {
5729 case LTTNG_BUFFER_PER_UID
:
5731 struct buffer_reg_uid
*reg
;
5733 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5734 struct buffer_reg_channel
*reg_chan
;
5735 struct consumer_socket
*socket
;
5737 /* Get consumer socket to use to push the metadata.*/
5738 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5745 memset(pathname
, 0, sizeof(pathname
));
5746 ret
= snprintf(pathname
, sizeof(pathname
),
5747 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5748 reg
->uid
, reg
->bits_per_long
);
5750 PERROR("snprintf snapshot path");
5754 /* Add the UST default trace dir to path. */
5755 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5756 reg_chan
, node
.node
) {
5757 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5758 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5759 nb_packets_per_stream
);
5764 ret
= consumer_snapshot_channel(socket
,
5765 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5766 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
5774 case LTTNG_BUFFER_PER_PID
:
5776 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5777 struct consumer_socket
*socket
;
5778 struct lttng_ht_iter chan_iter
;
5779 struct ust_app_channel
*ua_chan
;
5780 struct ust_app_session
*ua_sess
;
5781 struct ust_registry_session
*registry
;
5783 ua_sess
= lookup_session_by_app(usess
, app
);
5785 /* Session not associated with this app. */
5789 /* Get the right consumer socket for the application. */
5790 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5797 /* Add the UST default trace dir to path. */
5798 memset(pathname
, 0, sizeof(pathname
));
5799 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5802 PERROR("snprintf snapshot path");
5806 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5807 ua_chan
, node
.node
) {
5808 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
5809 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
5810 nb_packets_per_stream
);
5816 registry
= get_session_registry(ua_sess
);
5818 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
5819 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
5832 if (!snapshot_done
) {
5834 * If no snapshot was made and we are not in the error path, this means
5835 * that there are no buffers thus no (prior) application to snapshot
5836 * data from so we have simply NO data.
5847 * Return the size taken by one more packet per stream.
5849 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
5850 uint64_t cur_nr_packets
)
5852 uint64_t tot_size
= 0;
5853 struct ust_app
*app
;
5854 struct lttng_ht_iter iter
;
5858 switch (usess
->buffer_type
) {
5859 case LTTNG_BUFFER_PER_UID
:
5861 struct buffer_reg_uid
*reg
;
5863 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5864 struct buffer_reg_channel
*reg_chan
;
5867 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5868 reg_chan
, node
.node
) {
5869 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
5871 * Don't take channel into account if we
5872 * already grab all its packets.
5876 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
5882 case LTTNG_BUFFER_PER_PID
:
5885 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5886 struct ust_app_channel
*ua_chan
;
5887 struct ust_app_session
*ua_sess
;
5888 struct lttng_ht_iter chan_iter
;
5890 ua_sess
= lookup_session_by_app(usess
, app
);
5892 /* Session not associated with this app. */
5896 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5897 ua_chan
, node
.node
) {
5898 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
5900 * Don't take channel into account if we
5901 * already grab all its packets.
5905 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
5919 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
5920 struct cds_list_head
*buffer_reg_uid_list
,
5921 struct consumer_output
*consumer
, uint64_t uchan_id
,
5922 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
5925 uint64_t consumer_chan_key
;
5927 ret
= buffer_reg_uid_consumer_channel_key(
5928 buffer_reg_uid_list
, ust_session_id
,
5929 uchan_id
, &consumer_chan_key
);
5935 ret
= consumer_get_lost_packets(ust_session_id
,
5936 consumer_chan_key
, consumer
, lost
);
5938 ret
= consumer_get_discarded_events(ust_session_id
,
5939 consumer_chan_key
, consumer
, discarded
);
5946 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
5947 struct ltt_ust_channel
*uchan
,
5948 struct consumer_output
*consumer
, int overwrite
,
5949 uint64_t *discarded
, uint64_t *lost
)
5952 struct lttng_ht_iter iter
;
5953 struct lttng_ht_node_str
*ua_chan_node
;
5954 struct ust_app
*app
;
5955 struct ust_app_session
*ua_sess
;
5956 struct ust_app_channel
*ua_chan
;
5960 * Iterate over every registered applications, return when we
5961 * found one in the right session and channel.
5963 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5964 struct lttng_ht_iter uiter
;
5966 ua_sess
= lookup_session_by_app(usess
, app
);
5967 if (ua_sess
== NULL
) {
5972 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
5973 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
5974 /* If the session is found for the app, the channel must be there */
5975 assert(ua_chan_node
);
5977 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
5980 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
5984 ret
= consumer_get_discarded_events(usess
->id
,
5985 ua_chan
->key
, consumer
, discarded
);