2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
27 #include <urcu/compiler.h>
28 #include <lttng/ust-error.h>
30 #include <common/common.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
36 #include "ust-consumer.h"
39 /* Next available channel key. */
40 static unsigned long next_channel_key
;
43 * Return the atomically incremented value of next_channel_key.
45 static inline unsigned long get_next_channel_key(void)
47 return uatomic_add_return(&next_channel_key
, 1);
51 * Return the consumer socket from the given consumer output with the right
52 * bitness. On error, returns NULL.
54 * The caller MUST acquire a rcu read side lock and keep it until the socket
55 * object reference is not needed anymore.
57 static struct consumer_socket
*find_consumer_socket_by_bitness(int bits
,
58 struct consumer_output
*consumer
)
61 struct consumer_socket
*socket
= NULL
;
65 consumer_fd
= uatomic_read(&ust_consumerd64_fd
);
68 consumer_fd
= uatomic_read(&ust_consumerd32_fd
);
75 socket
= consumer_find_socket(consumer_fd
, consumer
);
77 ERR("Consumer socket fd %d not found in consumer obj %p",
78 consumer_fd
, consumer
);
86 * Match function for the hash table lookup.
88 * It matches an ust app event based on three attributes which are the event
89 * name, the filter bytecode and the loglevel.
91 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
93 struct ust_app_event
*event
;
94 const struct ust_app_ht_key
*key
;
99 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
102 /* Match the 3 elements of the key: name, filter and loglevel. */
105 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
109 /* Event loglevel. */
110 if (event
->attr
.loglevel
!= key
->loglevel
) {
111 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
112 && key
->loglevel
== 0 && event
->attr
.loglevel
== -1) {
114 * Match is accepted. This is because on event creation, the
115 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
116 * -1 are accepted for this loglevel type since 0 is the one set by
117 * the API when receiving an enable event.
124 /* One of the filters is NULL, fail. */
125 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
129 if (key
->filter
&& event
->filter
) {
130 /* Both filters exists, check length followed by the bytecode. */
131 if (event
->filter
->len
!= key
->filter
->len
||
132 memcmp(event
->filter
->data
, key
->filter
->data
,
133 event
->filter
->len
) != 0) {
146 * Unique add of an ust app event in the given ht. This uses the custom
147 * ht_match_ust_app_event match function and the event name as hash.
149 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
150 struct ust_app_event
*event
)
152 struct cds_lfht_node
*node_ptr
;
153 struct ust_app_ht_key key
;
157 assert(ua_chan
->events
);
160 ht
= ua_chan
->events
;
161 key
.name
= event
->attr
.name
;
162 key
.filter
= event
->filter
;
163 key
.loglevel
= event
->attr
.loglevel
;
165 node_ptr
= cds_lfht_add_unique(ht
->ht
,
166 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
167 ht_match_ust_app_event
, &key
, &event
->node
.node
);
168 assert(node_ptr
== &event
->node
.node
);
172 * Close the notify socket from the given RCU head object. This MUST be called
173 * through a call_rcu().
175 static void close_notify_sock_rcu(struct rcu_head
*head
)
178 struct ust_app_notify_sock_obj
*obj
=
179 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
181 /* Must have a valid fd here. */
182 assert(obj
->fd
>= 0);
184 ret
= close(obj
->fd
);
186 ERR("close notify sock %d RCU", obj
->fd
);
188 lttng_fd_put(LTTNG_FD_APPS
, 1);
194 * Delete ust context safely. RCU read lock must be held before calling
198 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
205 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
206 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
207 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
208 sock
, ua_ctx
->obj
->handle
, ret
);
216 * Delete ust app event safely. RCU read lock must be held before calling
220 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
226 free(ua_event
->filter
);
228 if (ua_event
->obj
!= NULL
) {
229 ret
= ustctl_release_object(sock
, ua_event
->obj
);
230 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
231 ERR("UST app sock %d release event obj failed with ret %d",
240 * Delete ust app stream safely. RCU read lock must be held before calling
244 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
251 ret
= ustctl_release_object(sock
, stream
->obj
);
252 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
253 ERR("UST app sock %d release stream obj failed with ret %d",
256 lttng_fd_put(LTTNG_FD_APPS
, 2);
263 * Delete ust app channel safely. RCU read lock must be held before calling
267 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
271 struct lttng_ht_iter iter
;
272 struct ust_app_event
*ua_event
;
273 struct ust_app_ctx
*ua_ctx
;
274 struct ust_app_stream
*stream
, *stmp
;
278 DBG3("UST app deleting channel %s", ua_chan
->name
);
281 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
282 cds_list_del(&stream
->list
);
283 delete_ust_app_stream(sock
, stream
);
287 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
288 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
290 delete_ust_app_ctx(sock
, ua_ctx
);
292 lttng_ht_destroy(ua_chan
->ctx
);
295 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
297 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
299 delete_ust_app_event(sock
, ua_event
);
301 lttng_ht_destroy(ua_chan
->events
);
303 /* Wipe and free registry from session registry. */
304 ust_registry_channel_del_free(ua_chan
->session
->registry
, ua_chan
->key
);
306 if (ua_chan
->obj
!= NULL
) {
307 /* Remove channel from application UST object descriptor. */
308 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
309 lttng_ht_del(app
->ust_objd
, &iter
);
310 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
311 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
312 ERR("UST app sock %d release channel obj failed with ret %d",
315 lttng_fd_put(LTTNG_FD_APPS
, 2);
322 * For a given application and session, push metadata to consumer. The session
323 * lock MUST be acquired here before calling this.
325 * Return 0 on success else a negative error.
327 static int push_metadata(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
330 char *metadata_str
= NULL
;
332 struct consumer_socket
*socket
;
337 if (!ua_sess
->consumer
|| !ua_sess
->metadata
) {
338 /* No consumer means no stream associated so just return gracefully. */
345 /* Get consumer socket to use to push the metadata.*/
346 socket
= find_consumer_socket_by_bitness(app
->bits_per_long
,
350 goto error_rcu_unlock
;
354 * TODO: Currently, we hold the socket lock around sampling of the next
355 * metadata segment to ensure we send metadata over the consumer socket in
356 * the correct order. This makes the registry lock nest inside the socket
359 * Please note that this is a temporary measure: we should move this lock
360 * back into ust_consumer_push_metadata() when the consumer gets the
361 * ability to reorder the metadata it receives.
363 pthread_mutex_lock(socket
->lock
);
364 pthread_mutex_lock(&ua_sess
->registry
->lock
);
366 offset
= ua_sess
->registry
->metadata_len_sent
;
367 len
= ua_sess
->registry
->metadata_len
-
368 ua_sess
->registry
->metadata_len_sent
;
370 DBG3("No metadata to push for session id %d", ua_sess
->id
);
372 goto error_reg_unlock
;
376 /* Allocate only what we have to send. */
377 metadata_str
= zmalloc(len
);
379 PERROR("zmalloc ust app metadata string");
381 goto error_reg_unlock
;
383 /* Copy what we haven't send out. */
384 memcpy(metadata_str
, ua_sess
->registry
->metadata
+ offset
, len
);
386 pthread_mutex_unlock(&ua_sess
->registry
->lock
);
388 ret
= ust_consumer_push_metadata(socket
, ua_sess
, metadata_str
, len
,
391 pthread_mutex_unlock(socket
->lock
);
392 goto error_rcu_unlock
;
395 /* Update len sent of the registry. */
396 pthread_mutex_lock(&ua_sess
->registry
->lock
);
397 ua_sess
->registry
->metadata_len_sent
+= len
;
398 pthread_mutex_unlock(&ua_sess
->registry
->lock
);
399 pthread_mutex_unlock(socket
->lock
);
406 pthread_mutex_unlock(&ua_sess
->registry
->lock
);
407 pthread_mutex_unlock(socket
->lock
);
416 * Send to the consumer a close metadata command for the given session. Once
417 * done, the metadata channel is deleted and the session metadata pointer is
418 * nullified. The session lock MUST be acquired here unless the application is
419 * in the destroy path.
421 * Return 0 on success else a negative value.
423 static int close_metadata(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
426 struct consumer_socket
*socket
;
431 /* Ignore if no metadata. Valid since it can be called on unregister. */
432 if (!ua_sess
->metadata
) {
439 /* Get consumer socket to use to push the metadata.*/
440 socket
= find_consumer_socket_by_bitness(app
->bits_per_long
,
444 goto error_rcu_unlock
;
447 ret
= ust_consumer_close_metadata(socket
, ua_sess
->metadata
);
449 goto error_rcu_unlock
;
453 /* Destroy metadata on our side since we must not use it anymore. */
454 delete_ust_app_channel(-1, ua_sess
->metadata
, app
);
455 ua_sess
->metadata
= NULL
;
463 * Delete ust app session safely. RCU read lock must be held before calling
467 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
471 struct lttng_ht_iter iter
;
472 struct ust_app_channel
*ua_chan
;
476 if (ua_sess
->metadata
) {
477 /* Push metadata for application before freeing the application. */
478 (void) push_metadata(app
, ua_sess
);
480 /* And ask to close it for this session. */
481 (void) close_metadata(app
, ua_sess
);
484 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
486 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
488 delete_ust_app_channel(sock
, ua_chan
, app
);
490 lttng_ht_destroy(ua_sess
->channels
);
492 ust_registry_session_destroy(ua_sess
->registry
);
493 free(ua_sess
->registry
);
495 if (ua_sess
->handle
!= -1) {
496 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
497 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
498 ERR("UST app sock %d release session handle failed with ret %d",
506 * Delete a traceable application structure from the global list. Never call
507 * this function outside of a call_rcu call.
510 void delete_ust_app(struct ust_app
*app
)
513 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
517 /* Delete ust app sessions info */
521 lttng_ht_destroy(app
->sessions
);
524 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
526 /* Free every object in the session and the session. */
527 delete_ust_app_session(sock
, ua_sess
, app
);
531 * Wait until we have deleted the application from the sock hash table
532 * before closing this socket, otherwise an application could re-use the
533 * socket ID and race with the teardown, using the same hash table entry.
535 * It's OK to leave the close in call_rcu. We want it to stay unique for
536 * all RCU readers that could run concurrently with unregister app,
537 * therefore we _need_ to only close that socket after a grace period. So
538 * it should stay in this RCU callback.
540 * This close() is a very important step of the synchronization model so
541 * every modification to this function must be carefully reviewed.
547 lttng_fd_put(LTTNG_FD_APPS
, 1);
549 DBG2("UST app pid %d deleted", app
->pid
);
556 * URCU intermediate call to delete an UST app.
559 void delete_ust_app_rcu(struct rcu_head
*head
)
561 struct lttng_ht_node_ulong
*node
=
562 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
563 struct ust_app
*app
=
564 caa_container_of(node
, struct ust_app
, pid_n
);
566 DBG3("Call RCU deleting app PID %d", app
->pid
);
571 * Delete the session from the application ht and delete the data structure by
572 * freeing every object inside and releasing them.
574 static void destroy_app_session(struct ust_app
*app
,
575 struct ust_app_session
*ua_sess
)
578 struct lttng_ht_iter iter
;
583 iter
.iter
.node
= &ua_sess
->node
.node
;
584 ret
= lttng_ht_del(app
->sessions
, &iter
);
586 /* Already scheduled for teardown. */
590 /* Once deleted, free the data structure. */
591 delete_ust_app_session(app
->sock
, ua_sess
, app
);
598 * Alloc new UST app session.
601 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
603 struct ust_app_session
*ua_sess
;
605 /* Init most of the default value by allocating and zeroing */
606 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
607 if (ua_sess
== NULL
) {
612 ua_sess
->handle
= -1;
613 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
614 pthread_mutex_init(&ua_sess
->lock
, NULL
);
615 if (ust_registry_session_init(&ua_sess
->registry
, app
,
617 app
->uint8_t_alignment
,
618 app
->uint16_t_alignment
,
619 app
->uint32_t_alignment
,
620 app
->uint64_t_alignment
,
635 * Alloc new UST app channel.
638 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
639 struct ust_app_session
*ua_sess
,
640 struct lttng_ust_channel_attr
*attr
)
642 struct ust_app_channel
*ua_chan
;
644 /* Init most of the default value by allocating and zeroing */
645 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
646 if (ua_chan
== NULL
) {
651 /* Setup channel name */
652 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
653 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
655 ua_chan
->enabled
= 1;
656 ua_chan
->handle
= -1;
657 ua_chan
->session
= ua_sess
;
658 ua_chan
->key
= get_next_channel_key();
659 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
660 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
661 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
663 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
665 /* Add a channel registry to session. */
666 if (ust_registry_channel_add(ua_sess
->registry
, ua_chan
->key
) < 0) {
670 /* Copy attributes */
672 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
673 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
674 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
675 ua_chan
->attr
.overwrite
= attr
->overwrite
;
676 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
677 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
678 ua_chan
->attr
.output
= attr
->output
;
680 /* By default, the channel is a per cpu channel. */
681 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
683 DBG3("UST app channel %s allocated", ua_chan
->name
);
692 * Allocate and initialize a UST app stream.
694 * Return newly allocated stream pointer or NULL on error.
696 struct ust_app_stream
*ust_app_alloc_stream(void)
698 struct ust_app_stream
*stream
= NULL
;
700 stream
= zmalloc(sizeof(*stream
));
701 if (stream
== NULL
) {
702 PERROR("zmalloc ust app stream");
706 /* Zero could be a valid value for a handle so flag it to -1. */
714 * Alloc new UST app event.
717 struct ust_app_event
*alloc_ust_app_event(char *name
,
718 struct lttng_ust_event
*attr
)
720 struct ust_app_event
*ua_event
;
722 /* Init most of the default value by allocating and zeroing */
723 ua_event
= zmalloc(sizeof(struct ust_app_event
));
724 if (ua_event
== NULL
) {
729 ua_event
->enabled
= 1;
730 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
731 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
732 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
734 /* Copy attributes */
736 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
739 DBG3("UST app event %s allocated", ua_event
->name
);
748 * Alloc new UST app context.
751 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
753 struct ust_app_ctx
*ua_ctx
;
755 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
756 if (ua_ctx
== NULL
) {
761 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
764 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
771 * Allocate a filter and copy the given original filter.
773 * Return allocated filter or NULL on error.
775 static struct lttng_ust_filter_bytecode
*alloc_copy_ust_app_filter(
776 struct lttng_ust_filter_bytecode
*orig_f
)
778 struct lttng_ust_filter_bytecode
*filter
= NULL
;
780 /* Copy filter bytecode */
781 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
783 PERROR("zmalloc alloc ust app filter");
787 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
794 * Find an ust_app using the sock and return it. RCU read side lock must be
795 * held before calling this helper function.
798 struct ust_app
*find_app_by_sock(int sock
)
800 struct lttng_ht_node_ulong
*node
;
801 struct lttng_ht_iter iter
;
803 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
804 node
= lttng_ht_iter_get_node_ulong(&iter
);
806 DBG2("UST app find by sock %d not found", sock
);
810 return caa_container_of(node
, struct ust_app
, sock_n
);
817 * Find an ust_app using the notify sock and return it. RCU read side lock must
818 * be held before calling this helper function.
820 static struct ust_app
*find_app_by_notify_sock(int sock
)
822 struct lttng_ht_node_ulong
*node
;
823 struct lttng_ht_iter iter
;
825 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
827 node
= lttng_ht_iter_get_node_ulong(&iter
);
829 DBG2("UST app find by notify sock %d not found", sock
);
833 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
840 * Lookup for an ust app event based on event name, filter bytecode and the
843 * Return an ust_app_event object or NULL on error.
845 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
846 char *name
, struct lttng_ust_filter_bytecode
*filter
, int loglevel
)
848 struct lttng_ht_iter iter
;
849 struct lttng_ht_node_str
*node
;
850 struct ust_app_event
*event
= NULL
;
851 struct ust_app_ht_key key
;
856 /* Setup key for event lookup. */
859 key
.loglevel
= loglevel
;
861 /* Lookup using the event name as hash and a custom match fct. */
862 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
863 ht_match_ust_app_event
, &key
, &iter
.iter
);
864 node
= lttng_ht_iter_get_node_str(&iter
);
869 event
= caa_container_of(node
, struct ust_app_event
, node
);
876 * Create the channel context on the tracer.
878 * Called with UST app session lock held.
881 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
882 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
886 health_code_update();
888 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
889 ua_chan
->obj
, &ua_ctx
->obj
);
891 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
892 ERR("UST app create channel context failed for app (pid: %d) "
893 "with ret %d", app
->pid
, ret
);
895 DBG3("UST app disable event failed. Application is dead.");
900 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
902 DBG2("UST app context handle %d created successfully for channel %s",
903 ua_ctx
->handle
, ua_chan
->name
);
906 health_code_update();
911 * Set the filter on the tracer.
914 int set_ust_event_filter(struct ust_app_event
*ua_event
,
919 health_code_update();
921 if (!ua_event
->filter
) {
926 ret
= ustctl_set_filter(app
->sock
, ua_event
->filter
,
929 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
930 ERR("UST app event %s filter failed for app (pid: %d) "
931 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
933 DBG3("UST app filter event failed. Application is dead.");
938 DBG2("UST filter set successfully for event %s", ua_event
->name
);
941 health_code_update();
946 * Disable the specified event on to UST tracer for the UST session.
948 static int disable_ust_event(struct ust_app
*app
,
949 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
953 health_code_update();
955 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
957 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
958 ERR("UST app event %s disable failed for app (pid: %d) "
959 "and session handle %d with ret %d",
960 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
962 DBG3("UST app disable event failed. Application is dead.");
967 DBG2("UST app event %s disabled successfully for app (pid: %d)",
968 ua_event
->attr
.name
, app
->pid
);
971 health_code_update();
976 * Disable the specified channel on to UST tracer for the UST session.
978 static int disable_ust_channel(struct ust_app
*app
,
979 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
983 health_code_update();
985 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
987 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
988 ERR("UST app channel %s disable failed for app (pid: %d) "
989 "and session handle %d with ret %d",
990 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
992 DBG3("UST app disable channel failed. Application is dead.");
997 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
998 ua_chan
->name
, app
->pid
);
1001 health_code_update();
1006 * Enable the specified channel on to UST tracer for the UST session.
1008 static int enable_ust_channel(struct ust_app
*app
,
1009 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1013 health_code_update();
1015 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1017 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1018 ERR("UST app channel %s enable failed for app (pid: %d) "
1019 "and session handle %d with ret %d",
1020 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1022 DBG3("UST app enable channel failed. Application is dead.");
1027 ua_chan
->enabled
= 1;
1029 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1030 ua_chan
->name
, app
->pid
);
1033 health_code_update();
1038 * Enable the specified event on to UST tracer for the UST session.
1040 static int enable_ust_event(struct ust_app
*app
,
1041 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1045 health_code_update();
1047 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1049 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1050 ERR("UST app event %s enable failed for app (pid: %d) "
1051 "and session handle %d with ret %d",
1052 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1054 DBG3("UST app enable event failed. Application is dead.");
1059 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1060 ua_event
->attr
.name
, app
->pid
);
1063 health_code_update();
1068 * Create the specified channel onto the UST tracer for a UST session. This
1069 * MUST be called with UST app session lock held.
1071 * Return 0 on success. On error, a negative value is returned.
1073 static int create_ust_channel(struct ust_app
*app
,
1074 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
1075 struct consumer_output
*consumer
)
1078 unsigned int nb_fd
= 0;
1079 struct consumer_socket
*socket
;
1080 struct ust_app_stream
*stream
, *stmp
;
1088 health_code_update();
1090 /* Get the right consumer socket for the application. */
1091 socket
= find_consumer_socket_by_bitness(app
->bits_per_long
, consumer
);
1097 health_code_update();
1100 * Ask consumer to create channel. The consumer will return the number of
1101 * stream we have to expect.
1103 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, consumer
, socket
);
1109 * Compute the number of fd needed before receiving them. It must be 2 per
1110 * stream (2 being the default value here).
1112 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
1114 /* Reserve the amount of file descriptor we need. */
1115 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
1117 ERR("Exhausted number of available FD upon create channel");
1121 health_code_update();
1124 * Now get the channel from the consumer. This call wil populate the stream
1125 * list of that channel and set the ust object.
1127 ret
= ust_consumer_get_channel(socket
, ua_chan
);
1132 /* Send channel to the application. */
1133 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1138 health_code_update();
1140 /* Send all streams to application. */
1141 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1142 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1146 /* We don't need the stream anymore once sent to the tracer. */
1147 cds_list_del(&stream
->list
);
1148 delete_ust_app_stream(-1, stream
);
1151 /* Flag the channel that it is sent to the application. */
1152 ua_chan
->is_sent
= 1;
1153 /* Initialize ust objd object using the received handle and add it. */
1154 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
1155 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
1157 health_code_update();
1159 /* If channel is not enabled, disable it on the tracer */
1160 if (!ua_chan
->enabled
) {
1161 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1171 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
1174 * Initiate a destroy channel on the consumer since we had an error
1175 * handling it on our side. The return value is of no importance since we
1176 * already have a ret value set by the previous error that we need to
1179 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
1181 health_code_update();
1187 * Create the specified event onto the UST tracer for a UST session.
1189 * Should be called with session mutex held.
1192 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1193 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1197 health_code_update();
1199 /* Create UST event on tracer */
1200 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1203 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1204 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1205 ua_event
->attr
.name
, app
->pid
, ret
);
1207 DBG3("UST app create event failed. Application is dead.");
1212 ua_event
->handle
= ua_event
->obj
->handle
;
1214 DBG2("UST app event %s created successfully for pid:%d",
1215 ua_event
->attr
.name
, app
->pid
);
1217 health_code_update();
1219 /* Set filter if one is present. */
1220 if (ua_event
->filter
) {
1221 ret
= set_ust_event_filter(ua_event
, app
);
1227 /* If event not enabled, disable it on the tracer */
1228 if (ua_event
->enabled
== 0) {
1229 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1232 * If we hit an EPERM, something is wrong with our disable call. If
1233 * we get an EEXIST, there is a problem on the tracer side since we
1237 case -LTTNG_UST_ERR_PERM
:
1238 /* Code flow problem */
1240 case -LTTNG_UST_ERR_EXIST
:
1241 /* It's OK for our use case. */
1252 health_code_update();
1257 * Copy data between an UST app event and a LTT event.
1259 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1260 struct ltt_ust_event
*uevent
)
1262 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1263 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1265 ua_event
->enabled
= uevent
->enabled
;
1267 /* Copy event attributes */
1268 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1270 /* Copy filter bytecode */
1271 if (uevent
->filter
) {
1272 ua_event
->filter
= alloc_copy_ust_app_filter(uevent
->filter
);
1273 /* Filter might be NULL here in case of ENONEM. */
1278 * Copy data between an UST app channel and a LTT channel.
1280 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1281 struct ltt_ust_channel
*uchan
)
1283 struct lttng_ht_iter iter
;
1284 struct ltt_ust_event
*uevent
;
1285 struct ltt_ust_context
*uctx
;
1286 struct ust_app_event
*ua_event
;
1287 struct ust_app_ctx
*ua_ctx
;
1289 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1291 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1292 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1294 /* Copy event attributes since the layout is different. */
1295 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1296 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1297 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1298 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1299 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1300 ua_chan
->attr
.output
= uchan
->attr
.output
;
1302 * Note that the attribute channel type is not set since the channel on the
1303 * tracing registry side does not have this information.
1306 ua_chan
->enabled
= uchan
->enabled
;
1308 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
1309 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1310 if (ua_ctx
== NULL
) {
1313 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1314 (unsigned long) ua_ctx
->ctx
.ctx
);
1315 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1318 /* Copy all events from ltt ust channel to ust app channel */
1319 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1320 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1321 uevent
->filter
, uevent
->attr
.loglevel
);
1322 if (ua_event
== NULL
) {
1323 DBG2("UST event %s not found on shadow copy channel",
1325 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1326 if (ua_event
== NULL
) {
1329 shadow_copy_event(ua_event
, uevent
);
1330 add_unique_ust_app_event(ua_chan
, ua_event
);
1334 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1338 * Copy data between a UST app session and a regular LTT session.
1340 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1341 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1343 struct lttng_ht_node_str
*ua_chan_node
;
1344 struct lttng_ht_iter iter
;
1345 struct ltt_ust_channel
*uchan
;
1346 struct ust_app_channel
*ua_chan
;
1348 struct tm
*timeinfo
;
1352 /* Get date and time for unique app path */
1354 timeinfo
= localtime(&rawtime
);
1355 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1357 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1359 ua_sess
->id
= usess
->id
;
1360 ua_sess
->uid
= usess
->uid
;
1361 ua_sess
->gid
= usess
->gid
;
1363 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s-%d-%s/", app
->name
, app
->pid
,
1366 PERROR("asprintf UST shadow copy session");
1367 /* TODO: We cannot return an error from here.. */
1371 /* TODO: support all UST domain */
1373 /* Iterate over all channels in global domain. */
1374 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1376 struct lttng_ht_iter uiter
;
1378 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1379 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1380 if (ua_chan_node
!= NULL
) {
1381 /* Session exist. Contiuing. */
1385 DBG2("Channel %s not found on shadow session copy, creating it",
1387 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1388 if (ua_chan
== NULL
) {
1389 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1392 shadow_copy_channel(ua_chan
, uchan
);
1394 * The concept of metadata channel does not exist on the tracing
1395 * registry side of the session daemon so this can only be a per CPU
1396 * channel and not metadata.
1398 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1400 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1405 * Lookup sesison wrapper.
1408 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1409 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1411 /* Get right UST app session from app */
1412 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
1416 * Return ust app session from the app session hashtable using the UST session
1419 static struct ust_app_session
*lookup_session_by_app(
1420 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1422 struct lttng_ht_iter iter
;
1423 struct lttng_ht_node_ulong
*node
;
1425 __lookup_session_by_app(usess
, app
, &iter
);
1426 node
= lttng_ht_iter_get_node_ulong(&iter
);
1431 return caa_container_of(node
, struct ust_app_session
, node
);
1438 * Create a session on the tracer side for the given app.
1440 * On success, ua_sess_ptr is populated with the session pointer or else left
1441 * untouched. If the session was created, is_created is set to 1. On error,
1442 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1445 * Returns 0 on success or else a negative code which is either -ENOMEM or
1446 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1448 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1449 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1452 int ret
, created
= 0;
1453 struct ust_app_session
*ua_sess
;
1457 assert(ua_sess_ptr
);
1459 health_code_update();
1461 ua_sess
= lookup_session_by_app(usess
, app
);
1462 if (ua_sess
== NULL
) {
1463 DBG2("UST app pid: %d session id %d not found, creating it",
1464 app
->pid
, usess
->id
);
1465 ua_sess
= alloc_ust_app_session(app
);
1466 if (ua_sess
== NULL
) {
1467 /* Only malloc can failed so something is really wrong */
1471 shadow_copy_session(ua_sess
, usess
, app
);
1475 health_code_update();
1477 if (ua_sess
->handle
== -1) {
1478 ret
= ustctl_create_session(app
->sock
);
1480 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1481 ERR("Creating session for app pid %d with ret %d",
1484 DBG("UST app creating session failed. Application is dead");
1486 delete_ust_app_session(-1, ua_sess
, app
);
1487 if (ret
!= -ENOMEM
) {
1489 * Tracer is probably gone or got an internal error so let's
1490 * behave like it will soon unregister or not usable.
1497 ua_sess
->handle
= ret
;
1499 /* Add ust app session to app's HT */
1500 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
1501 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
1503 DBG2("UST app session created successfully with handle %d", ret
);
1507 * Assign consumer if not already set. For one application, there is only
1508 * one possible consumer has of now.
1510 if (!ua_sess
->consumer
) {
1511 ua_sess
->consumer
= usess
->consumer
;
1514 *ua_sess_ptr
= ua_sess
;
1516 *is_created
= created
;
1519 /* Everything went well. */
1523 health_code_update();
1528 * Create a context for the channel on the tracer.
1530 * Called with UST app session lock held.
1533 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
1534 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
1535 struct ust_app
*app
)
1538 struct lttng_ht_iter iter
;
1539 struct lttng_ht_node_ulong
*node
;
1540 struct ust_app_ctx
*ua_ctx
;
1542 DBG2("UST app adding context to channel %s", ua_chan
->name
);
1544 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
1545 node
= lttng_ht_iter_get_node_ulong(&iter
);
1551 ua_ctx
= alloc_ust_app_ctx(uctx
);
1552 if (ua_ctx
== NULL
) {
1558 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
1559 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1561 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
1571 * Enable on the tracer side a ust app event for the session and channel.
1573 * Called with UST app session lock held.
1576 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
1577 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1581 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1586 ua_event
->enabled
= 1;
1593 * Disable on the tracer side a ust app event for the session and channel.
1595 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1596 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1600 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1605 ua_event
->enabled
= 0;
1612 * Lookup ust app channel for session and disable it on the tracer side.
1615 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1616 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1620 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1625 ua_chan
->enabled
= 0;
1632 * Lookup ust app channel for session and enable it on the tracer side.
1634 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1635 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1638 struct lttng_ht_iter iter
;
1639 struct lttng_ht_node_str
*ua_chan_node
;
1640 struct ust_app_channel
*ua_chan
;
1642 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1643 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1644 if (ua_chan_node
== NULL
) {
1645 DBG2("Unable to find channel %s in ust session id %u",
1646 uchan
->name
, ua_sess
->id
);
1650 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1652 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1662 * Create UST app channel and create it on the tracer. Set ua_chanp of the
1663 * newly created channel if not NULL.
1665 * Called with UST app session lock held.
1667 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
1668 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
1669 struct consumer_output
*consumer
, enum lttng_ust_chan_type type
,
1670 struct ust_app_channel
**ua_chanp
)
1673 struct lttng_ht_iter iter
;
1674 struct lttng_ht_node_str
*ua_chan_node
;
1675 struct ust_app_channel
*ua_chan
;
1677 /* Lookup channel in the ust app session */
1678 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1679 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1680 if (ua_chan_node
!= NULL
) {
1681 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1685 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1686 if (ua_chan
== NULL
) {
1687 /* Only malloc can fail here */
1691 shadow_copy_channel(ua_chan
, uchan
);
1693 /* Set channel type. */
1694 ua_chan
->attr
.type
= type
;
1696 ret
= create_ust_channel(app
, ua_sess
, ua_chan
, consumer
);
1701 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1704 /* Only add the channel if successful on the tracer side. */
1705 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1709 *ua_chanp
= ua_chan
;
1712 /* Everything went well. */
1716 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
1721 * Create UST app event and create it on the tracer side.
1723 * Called with ust app session mutex held.
1726 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1727 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1728 struct ust_app
*app
)
1731 struct ust_app_event
*ua_event
;
1733 /* Get event node */
1734 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1735 uevent
->filter
, uevent
->attr
.loglevel
);
1736 if (ua_event
!= NULL
) {
1741 /* Does not exist so create one */
1742 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1743 if (ua_event
== NULL
) {
1744 /* Only malloc can failed so something is really wrong */
1748 shadow_copy_event(ua_event
, uevent
);
1750 /* Create it on the tracer side */
1751 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1753 /* Not found previously means that it does not exist on the tracer */
1754 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
1758 add_unique_ust_app_event(ua_chan
, ua_event
);
1760 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1767 /* Valid. Calling here is already in a read side lock */
1768 delete_ust_app_event(-1, ua_event
);
1773 * Create UST metadata and open it on the tracer side.
1775 * Called with UST app session lock held.
1777 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1778 struct ust_app
*app
, struct consumer_output
*consumer
)
1781 struct ust_app_channel
*metadata
;
1782 struct consumer_socket
*socket
;
1788 if (ua_sess
->metadata
) {
1789 /* Already exist. Return success. */
1793 /* Allocate UST metadata */
1794 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
1796 /* malloc() failed */
1801 /* Set default attributes for metadata. */
1802 metadata
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1803 metadata
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
1804 metadata
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
1805 metadata
->attr
.switch_timer_interval
= DEFAULT_UST_CHANNEL_SWITCH_TIMER
;
1806 metadata
->attr
.read_timer_interval
= DEFAULT_UST_CHANNEL_READ_TIMER
;
1807 metadata
->attr
.output
= LTTNG_UST_MMAP
;
1808 metadata
->attr
.type
= LTTNG_UST_CHAN_METADATA
;
1810 /* Get the right consumer socket for the application. */
1811 socket
= find_consumer_socket_by_bitness(app
->bits_per_long
, consumer
);
1814 goto error_consumer
;
1818 * Ask the metadata channel creation to the consumer. The metadata object
1819 * will be created by the consumer and kept their. However, the stream is
1820 * never added or monitored until we do a first push metadata to the
1823 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
);
1825 goto error_consumer
;
1829 * The setup command will make the metadata stream be sent to the relayd,
1830 * if applicable, and the thread managing the metadatas. This is important
1831 * because after this point, if an error occurs, the only way the stream
1832 * can be deleted is to be monitored in the consumer.
1834 ret
= ust_consumer_setup_metadata(socket
, metadata
);
1836 goto error_consumer
;
1839 ua_sess
->metadata
= metadata
;
1841 DBG2("UST metadata created for app pid %d", app
->pid
);
1846 delete_ust_app_channel(-1, metadata
, app
);
1852 * Return pointer to traceable apps list.
1854 struct lttng_ht
*ust_app_get_ht(void)
1860 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
1861 * acquired before calling this function.
1863 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1865 struct ust_app
*app
= NULL
;
1866 struct lttng_ht_node_ulong
*node
;
1867 struct lttng_ht_iter iter
;
1869 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1870 node
= lttng_ht_iter_get_node_ulong(&iter
);
1872 DBG2("UST app no found with pid %d", pid
);
1876 DBG2("Found UST app by pid %d", pid
);
1878 app
= caa_container_of(node
, struct ust_app
, pid_n
);
1885 * Allocate and init an UST app object using the registration information and
1886 * the command socket. This is called when the command socket connects to the
1889 * The object is returned on success or else NULL.
1891 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
1893 struct ust_app
*lta
= NULL
;
1898 DBG3("UST app creating application for socket %d", sock
);
1900 if ((msg
->bits_per_long
== 64 &&
1901 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
1902 || (msg
->bits_per_long
== 32 &&
1903 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
1904 ERR("Registration failed: application \"%s\" (pid: %d) has "
1905 "%d-bit long, but no consumerd for this size is available.\n",
1906 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1910 lta
= zmalloc(sizeof(struct ust_app
));
1916 lta
->ppid
= msg
->ppid
;
1917 lta
->uid
= msg
->uid
;
1918 lta
->gid
= msg
->gid
;
1920 lta
->bits_per_long
= msg
->bits_per_long
;
1921 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
1922 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
1923 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
1924 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
1925 lta
->long_alignment
= msg
->long_alignment
;
1926 lta
->byte_order
= msg
->byte_order
;
1928 lta
->v_major
= msg
->major
;
1929 lta
->v_minor
= msg
->minor
;
1930 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1931 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1932 lta
->notify_sock
= -1;
1934 /* Copy name and make sure it's NULL terminated. */
1935 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1936 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
1939 * Before this can be called, when receiving the registration information,
1940 * the application compatibility is checked. So, at this point, the
1941 * application can work with this session daemon.
1943 lta
->compatible
= 1;
1945 lta
->pid
= msg
->pid
;
1946 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
1948 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
1950 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
1957 * For a given application object, add it to every hash table.
1959 void ust_app_add(struct ust_app
*app
)
1962 assert(app
->notify_sock
>= 0);
1967 * On a re-registration, we want to kick out the previous registration of
1970 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
1973 * The socket _should_ be unique until _we_ call close. So, a add_unique
1974 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1975 * already in the table.
1977 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
1979 /* Add application to the notify socket hash table. */
1980 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
1981 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
1983 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
1984 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
1985 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
1992 * Set the application version into the object.
1994 * Return 0 on success else a negative value either an errno code or a
1995 * LTTng-UST error code.
1997 int ust_app_version(struct ust_app
*app
)
2003 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
2005 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
2006 ERR("UST app %d verson failed with ret %d", app
->sock
, ret
);
2008 DBG3("UST app %d verion failed. Application is dead", app
->sock
);
2016 * Unregister app by removing it from the global traceable app list and freeing
2019 * The socket is already closed at this point so no close to sock.
2021 void ust_app_unregister(int sock
)
2023 struct ust_app
*lta
;
2024 struct lttng_ht_node_ulong
*node
;
2025 struct lttng_ht_iter iter
;
2026 struct ust_app_session
*ua_sess
;
2031 /* Get the node reference for a call_rcu */
2032 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
2033 node
= lttng_ht_iter_get_node_ulong(&iter
);
2036 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
2037 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
2039 /* Remove application from PID hash table */
2040 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
2044 * Remove application from notify hash table. The thread handling the
2045 * notify socket could have deleted the node so ignore on error because
2046 * either way it's valid. The close of that socket is handled by the other
2049 iter
.iter
.node
= <a
->notify_sock_n
.node
;
2050 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
2053 * Ignore return value since the node might have been removed before by an
2054 * add replace during app registration because the PID can be reassigned by
2057 iter
.iter
.node
= <a
->pid_n
.node
;
2058 ret
= lttng_ht_del(ust_app_ht
, &iter
);
2060 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
2064 /* Remove sessions so they are not visible during deletion.*/
2065 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
2067 ret
= lttng_ht_del(lta
->sessions
, &iter
);
2069 /* The session was already removed so scheduled for teardown. */
2074 * Add session to list for teardown. This is safe since at this point we
2075 * are the only one using this list.
2077 pthread_mutex_lock(&ua_sess
->lock
);
2080 * Normally, this is done in the delete session process which is
2081 * executed in the call rcu below. However, upon registration we can't
2082 * afford to wait for the grace period before pushing data or else the
2083 * data pending feature can race between the unregistration and stop
2084 * command where the data pending command is sent *before* the grace
2087 * The close metadata below nullifies the metadata pointer in the
2088 * session so the delete session will NOT push/close a second time.
2090 (void) push_metadata(lta
, ua_sess
);
2091 (void) close_metadata(lta
, ua_sess
);
2093 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
2094 pthread_mutex_unlock(&ua_sess
->lock
);
2098 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
2105 * Return traceable_app_count
2107 unsigned long ust_app_list_count(void)
2109 unsigned long count
;
2112 count
= lttng_ht_get_count(ust_app_ht
);
2119 * Fill events array with all events name of all registered apps.
2121 int ust_app_list_events(struct lttng_event
**events
)
2124 size_t nbmem
, count
= 0;
2125 struct lttng_ht_iter iter
;
2126 struct ust_app
*app
;
2127 struct lttng_event
*tmp_event
;
2129 nbmem
= UST_APP_EVENT_LIST_SIZE
;
2130 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
2131 if (tmp_event
== NULL
) {
2132 PERROR("zmalloc ust app events");
2139 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2140 struct lttng_ust_tracepoint_iter uiter
;
2142 health_code_update();
2144 if (!app
->compatible
) {
2146 * TODO: In time, we should notice the caller of this error by
2147 * telling him that this is a version error.
2151 handle
= ustctl_tracepoint_list(app
->sock
);
2153 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
2154 ERR("UST app list events getting handle failed for app pid %d",
2160 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
2161 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
2162 /* Handle ustctl error. */
2165 if (ret
!= -LTTNG_UST_ERR_EXITING
|| ret
!= -EPIPE
) {
2166 ERR("UST app tp list get failed for app %d with ret %d",
2169 DBG3("UST app tp list get failed. Application is dead");
2174 health_code_update();
2175 if (count
>= nbmem
) {
2176 /* In case the realloc fails, we free the memory */
2179 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
2182 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event
));
2184 PERROR("realloc ust app events");
2191 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
2192 tmp_event
[count
].loglevel
= uiter
.loglevel
;
2193 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
2194 tmp_event
[count
].pid
= app
->pid
;
2195 tmp_event
[count
].enabled
= -1;
2201 *events
= tmp_event
;
2203 DBG2("UST app list events done (%zu events)", count
);
2208 health_code_update();
2213 * Fill events array with all events name of all registered apps.
2215 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
2218 size_t nbmem
, count
= 0;
2219 struct lttng_ht_iter iter
;
2220 struct ust_app
*app
;
2221 struct lttng_event_field
*tmp_event
;
2223 nbmem
= UST_APP_EVENT_LIST_SIZE
;
2224 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
2225 if (tmp_event
== NULL
) {
2226 PERROR("zmalloc ust app event fields");
2233 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2234 struct lttng_ust_field_iter uiter
;
2236 health_code_update();
2238 if (!app
->compatible
) {
2240 * TODO: In time, we should notice the caller of this error by
2241 * telling him that this is a version error.
2245 handle
= ustctl_tracepoint_field_list(app
->sock
);
2247 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
2248 ERR("UST app list field getting handle failed for app pid %d",
2254 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
2255 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
2256 /* Handle ustctl error. */
2259 if (ret
!= -LTTNG_UST_ERR_EXITING
|| ret
!= -EPIPE
) {
2260 ERR("UST app tp list field failed for app %d with ret %d",
2263 DBG3("UST app tp list field failed. Application is dead");
2268 health_code_update();
2269 if (count
>= nbmem
) {
2270 /* In case the realloc fails, we free the memory */
2273 DBG2("Reallocating event field list from %zu to %zu entries", nbmem
,
2276 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event_field
));
2278 PERROR("realloc ust app event fields");
2286 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
2287 tmp_event
[count
].type
= uiter
.type
;
2288 tmp_event
[count
].nowrite
= uiter
.nowrite
;
2290 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2291 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
2292 tmp_event
[count
].event
.type
= LTTNG_UST_TRACEPOINT
;
2293 tmp_event
[count
].event
.pid
= app
->pid
;
2294 tmp_event
[count
].event
.enabled
= -1;
2300 *fields
= tmp_event
;
2302 DBG2("UST app list event fields done (%zu events)", count
);
2307 health_code_update();
2312 * Free and clean all traceable apps of the global list.
2314 void ust_app_clean_list(void)
2317 struct ust_app
*app
;
2318 struct lttng_ht_iter iter
;
2320 DBG2("UST app cleaning registered apps hash table");
2324 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2325 ret
= lttng_ht_del(ust_app_ht
, &iter
);
2327 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
2330 /* Cleanup socket hash table */
2331 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
2333 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
2337 /* Cleanup notify socket hash table */
2338 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
2339 notify_sock_n
.node
) {
2340 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
2344 /* Destroy is done only when the ht is empty */
2345 lttng_ht_destroy(ust_app_ht
);
2346 lttng_ht_destroy(ust_app_ht_by_sock
);
2347 lttng_ht_destroy(ust_app_ht_by_notify_sock
);
2353 * Init UST app hash table.
2355 void ust_app_ht_alloc(void)
2357 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2358 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2359 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2363 * For a specific UST session, disable the channel for all registered apps.
2365 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
2366 struct ltt_ust_channel
*uchan
)
2369 struct lttng_ht_iter iter
;
2370 struct lttng_ht_node_str
*ua_chan_node
;
2371 struct ust_app
*app
;
2372 struct ust_app_session
*ua_sess
;
2373 struct ust_app_channel
*ua_chan
;
2375 if (usess
== NULL
|| uchan
== NULL
) {
2376 ERR("Disabling UST global channel with NULL values");
2381 DBG2("UST app disabling channel %s from global domain for session id %d",
2382 uchan
->name
, usess
->id
);
2386 /* For every registered applications */
2387 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2388 struct lttng_ht_iter uiter
;
2389 if (!app
->compatible
) {
2391 * TODO: In time, we should notice the caller of this error by
2392 * telling him that this is a version error.
2396 ua_sess
= lookup_session_by_app(usess
, app
);
2397 if (ua_sess
== NULL
) {
2402 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2403 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2404 /* If the session if found for the app, the channel must be there */
2405 assert(ua_chan_node
);
2407 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2408 /* The channel must not be already disabled */
2409 assert(ua_chan
->enabled
== 1);
2411 /* Disable channel onto application */
2412 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
2414 /* XXX: We might want to report this error at some point... */
2426 * For a specific UST session, enable the channel for all registered apps.
2428 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
2429 struct ltt_ust_channel
*uchan
)
2432 struct lttng_ht_iter iter
;
2433 struct ust_app
*app
;
2434 struct ust_app_session
*ua_sess
;
2436 if (usess
== NULL
|| uchan
== NULL
) {
2437 ERR("Adding UST global channel to NULL values");
2442 DBG2("UST app enabling channel %s to global domain for session id %d",
2443 uchan
->name
, usess
->id
);
2447 /* For every registered applications */
2448 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2449 if (!app
->compatible
) {
2451 * TODO: In time, we should notice the caller of this error by
2452 * telling him that this is a version error.
2456 ua_sess
= lookup_session_by_app(usess
, app
);
2457 if (ua_sess
== NULL
) {
2461 /* Enable channel onto application */
2462 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
2464 /* XXX: We might want to report this error at some point... */
2476 * Disable an event in a channel and for a specific session.
2478 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
2479 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2482 struct lttng_ht_iter iter
, uiter
;
2483 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2484 struct ust_app
*app
;
2485 struct ust_app_session
*ua_sess
;
2486 struct ust_app_channel
*ua_chan
;
2487 struct ust_app_event
*ua_event
;
2489 DBG("UST app disabling event %s for all apps in channel "
2490 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
2494 /* For all registered applications */
2495 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2496 if (!app
->compatible
) {
2498 * TODO: In time, we should notice the caller of this error by
2499 * telling him that this is a version error.
2503 ua_sess
= lookup_session_by_app(usess
, app
);
2504 if (ua_sess
== NULL
) {
2509 /* Lookup channel in the ust app session */
2510 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2511 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2512 if (ua_chan_node
== NULL
) {
2513 DBG2("Channel %s not found in session id %d for app pid %d."
2514 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
2517 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2519 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2520 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2521 if (ua_event_node
== NULL
) {
2522 DBG2("Event %s not found in channel %s for app pid %d."
2523 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
2526 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2528 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2530 /* XXX: Report error someday... */
2541 * For a specific UST session and UST channel, the event for all
2544 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
2545 struct ltt_ust_channel
*uchan
)
2548 struct lttng_ht_iter iter
, uiter
;
2549 struct lttng_ht_node_str
*ua_chan_node
;
2550 struct ust_app
*app
;
2551 struct ust_app_session
*ua_sess
;
2552 struct ust_app_channel
*ua_chan
;
2553 struct ust_app_event
*ua_event
;
2555 DBG("UST app disabling all event for all apps in channel "
2556 "%s for session id %d", uchan
->name
, usess
->id
);
2560 /* For all registered applications */
2561 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2562 if (!app
->compatible
) {
2564 * TODO: In time, we should notice the caller of this error by
2565 * telling him that this is a version error.
2569 ua_sess
= lookup_session_by_app(usess
, app
);
2571 /* The application has problem or is probably dead. */
2575 /* Lookup channel in the ust app session */
2576 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2577 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2578 /* If the channel is not found, there is a code flow error */
2579 assert(ua_chan_node
);
2581 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2583 /* Disable each events of channel */
2584 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2586 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2588 /* XXX: Report error someday... */
2600 * For a specific UST session, create the channel for all registered apps.
2602 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
2603 struct ltt_ust_channel
*uchan
)
2605 int ret
= 0, created
;
2606 struct lttng_ht_iter iter
;
2607 struct ust_app
*app
;
2608 struct ust_app_session
*ua_sess
= NULL
;
2610 /* Very wrong code flow */
2614 DBG2("UST app adding channel %s to global domain for session id %d",
2615 uchan
->name
, usess
->id
);
2619 /* For every registered applications */
2620 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2621 if (!app
->compatible
) {
2623 * TODO: In time, we should notice the caller of this error by
2624 * telling him that this is a version error.
2629 * Create session on the tracer side and add it to app session HT. Note
2630 * that if session exist, it will simply return a pointer to the ust
2633 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
2638 * The application's socket is not valid. Either a bad socket
2639 * or a timeout on it. We can't inform the caller that for a
2640 * specific app, the session failed so lets continue here.
2645 goto error_rcu_unlock
;
2650 pthread_mutex_lock(&ua_sess
->lock
);
2651 /* Create channel onto application. We don't need the chan ref. */
2652 ret
= create_ust_app_channel(ua_sess
, uchan
, app
, usess
->consumer
,
2653 LTTNG_UST_CHAN_PER_CPU
, NULL
);
2654 pthread_mutex_unlock(&ua_sess
->lock
);
2656 if (ret
== -ENOMEM
) {
2657 /* No more memory is a fatal error. Stop right now. */
2658 goto error_rcu_unlock
;
2660 /* Cleanup the created session if it's the case. */
2662 destroy_app_session(app
, ua_sess
);
2673 * Enable event for a specific session and channel on the tracer.
2675 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
2676 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2679 struct lttng_ht_iter iter
, uiter
;
2680 struct lttng_ht_node_str
*ua_chan_node
;
2681 struct ust_app
*app
;
2682 struct ust_app_session
*ua_sess
;
2683 struct ust_app_channel
*ua_chan
;
2684 struct ust_app_event
*ua_event
;
2686 DBG("UST app enabling event %s for all apps for session id %d",
2687 uevent
->attr
.name
, usess
->id
);
2690 * NOTE: At this point, this function is called only if the session and
2691 * channel passed are already created for all apps. and enabled on the
2697 /* For all registered applications */
2698 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2699 if (!app
->compatible
) {
2701 * TODO: In time, we should notice the caller of this error by
2702 * telling him that this is a version error.
2706 ua_sess
= lookup_session_by_app(usess
, app
);
2708 /* The application has problem or is probably dead. */
2712 pthread_mutex_lock(&ua_sess
->lock
);
2714 /* Lookup channel in the ust app session */
2715 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2716 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2717 /* If the channel is not found, there is a code flow error */
2718 assert(ua_chan_node
);
2720 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2722 /* Get event node */
2723 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2724 uevent
->filter
, uevent
->attr
.loglevel
);
2725 if (ua_event
== NULL
) {
2726 DBG3("UST app enable event %s not found for app PID %d."
2727 "Skipping app", uevent
->attr
.name
, app
->pid
);
2731 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2733 pthread_mutex_unlock(&ua_sess
->lock
);
2737 pthread_mutex_unlock(&ua_sess
->lock
);
2746 * For a specific existing UST session and UST channel, creates the event for
2747 * all registered apps.
2749 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
2750 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2753 struct lttng_ht_iter iter
, uiter
;
2754 struct lttng_ht_node_str
*ua_chan_node
;
2755 struct ust_app
*app
;
2756 struct ust_app_session
*ua_sess
;
2757 struct ust_app_channel
*ua_chan
;
2759 DBG("UST app creating event %s for all apps for session id %d",
2760 uevent
->attr
.name
, usess
->id
);
2764 /* For all registered applications */
2765 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2766 if (!app
->compatible
) {
2768 * TODO: In time, we should notice the caller of this error by
2769 * telling him that this is a version error.
2773 ua_sess
= lookup_session_by_app(usess
, app
);
2775 /* The application has problem or is probably dead. */
2779 pthread_mutex_lock(&ua_sess
->lock
);
2780 /* Lookup channel in the ust app session */
2781 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2782 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2783 /* If the channel is not found, there is a code flow error */
2784 assert(ua_chan_node
);
2786 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2788 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2789 pthread_mutex_unlock(&ua_sess
->lock
);
2791 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
2792 /* Possible value at this point: -ENOMEM. If so, we stop! */
2795 DBG2("UST app event %s already exist on app PID %d",
2796 uevent
->attr
.name
, app
->pid
);
2807 * Start tracing for a specific UST session and app.
2809 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2812 struct ust_app_session
*ua_sess
;
2814 DBG("Starting tracing for ust app pid %d", app
->pid
);
2818 if (!app
->compatible
) {
2822 ua_sess
= lookup_session_by_app(usess
, app
);
2823 if (ua_sess
== NULL
) {
2824 /* The session is in teardown process. Ignore and continue. */
2828 pthread_mutex_lock(&ua_sess
->lock
);
2830 /* Upon restart, we skip the setup, already done */
2831 if (ua_sess
->started
) {
2835 /* Create directories if consumer is LOCAL and has a path defined. */
2836 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2837 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
2838 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
2839 S_IRWXU
| S_IRWXG
, usess
->uid
, usess
->gid
);
2841 if (ret
!= -EEXIST
) {
2842 ERR("Trace directory creation error");
2848 /* Create the metadata for the application. */
2849 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
2854 health_code_update();
2857 /* This start the UST tracing */
2858 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
2860 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2861 ERR("Error starting tracing for app pid: %d (ret: %d)",
2864 DBG("UST app start session failed. Application is dead.");
2869 /* Indicate that the session has been started once */
2870 ua_sess
->started
= 1;
2872 pthread_mutex_unlock(&ua_sess
->lock
);
2874 health_code_update();
2876 /* Quiescent wait after starting trace */
2877 ret
= ustctl_wait_quiescent(app
->sock
);
2878 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2879 ERR("UST app wait quiescent failed for app pid %d ret %d",
2885 health_code_update();
2889 pthread_mutex_unlock(&ua_sess
->lock
);
2891 health_code_update();
2896 * Stop tracing for a specific UST session and app.
2898 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2901 struct lttng_ht_iter iter
;
2902 struct ust_app_session
*ua_sess
;
2903 struct ust_app_channel
*ua_chan
;
2905 DBG("Stopping tracing for ust app pid %d", app
->pid
);
2909 if (!app
->compatible
) {
2910 goto end_no_session
;
2913 ua_sess
= lookup_session_by_app(usess
, app
);
2914 if (ua_sess
== NULL
) {
2915 goto end_no_session
;
2918 pthread_mutex_lock(&ua_sess
->lock
);
2921 * If started = 0, it means that stop trace has been called for a session
2922 * that was never started. It's possible since we can have a fail start
2923 * from either the application manager thread or the command thread. Simply
2924 * indicate that this is a stop error.
2926 if (!ua_sess
->started
) {
2927 goto error_rcu_unlock
;
2930 health_code_update();
2932 /* This inhibits UST tracing */
2933 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
2935 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2936 ERR("Error stopping tracing for app pid: %d (ret: %d)",
2939 DBG("UST app stop session failed. Application is dead.");
2941 goto error_rcu_unlock
;
2944 health_code_update();
2946 /* Quiescent wait after stopping trace */
2947 ret
= ustctl_wait_quiescent(app
->sock
);
2948 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2949 ERR("UST app wait quiescent failed for app pid %d ret %d",
2953 health_code_update();
2955 /* Flushing buffers */
2956 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2958 health_code_update();
2959 assert(ua_chan
->is_sent
);
2960 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_chan
->obj
);
2962 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2963 ERR("UST app PID %d channel %s flush failed with ret %d",
2964 app
->pid
, ua_chan
->name
, ret
);
2966 DBG3("UST app failed to flush %s. Application is dead.",
2968 /* No need to continue. */
2971 /* Continuing flushing all buffers */
2976 health_code_update();
2978 ret
= push_metadata(app
, ua_sess
);
2980 goto error_rcu_unlock
;
2983 pthread_mutex_unlock(&ua_sess
->lock
);
2986 health_code_update();
2990 pthread_mutex_unlock(&ua_sess
->lock
);
2992 health_code_update();
2997 * Destroy a specific UST session in apps.
2999 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
3002 struct ust_app_session
*ua_sess
;
3003 struct lttng_ht_iter iter
;
3004 struct lttng_ht_node_ulong
*node
;
3006 DBG("Destroy tracing for ust app pid %d", app
->pid
);
3010 if (!app
->compatible
) {
3014 __lookup_session_by_app(usess
, app
, &iter
);
3015 node
= lttng_ht_iter_get_node_ulong(&iter
);
3017 /* Session is being or is deleted. */
3020 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
3022 health_code_update();
3023 destroy_app_session(app
, ua_sess
);
3025 health_code_update();
3027 /* Quiescent wait after stopping trace */
3028 ret
= ustctl_wait_quiescent(app
->sock
);
3029 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3030 ERR("UST app wait quiescent failed for app pid %d ret %d",
3035 health_code_update();
3040 * Start tracing for the UST session.
3042 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
3045 struct lttng_ht_iter iter
;
3046 struct ust_app
*app
;
3048 DBG("Starting all UST traces");
3052 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3053 ret
= ust_app_start_trace(usess
, app
);
3055 /* Continue to next apps even on error */
3066 * Start tracing for the UST session.
3068 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
3071 struct lttng_ht_iter iter
;
3072 struct ust_app
*app
;
3074 DBG("Stopping all UST traces");
3078 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3079 ret
= ust_app_stop_trace(usess
, app
);
3081 ERR("UST app stop trace failed with ret %d", ret
);
3082 /* Continue to next apps even on error */
3093 * Destroy app UST session.
3095 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
3098 struct lttng_ht_iter iter
;
3099 struct ust_app
*app
;
3101 DBG("Destroy all UST traces");
3105 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3106 ret
= destroy_trace(usess
, app
);
3108 /* Continue to next apps even on error */
3119 * Add channels/events from UST global domain to registered apps at sock.
3121 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
3124 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
3125 struct ust_app
*app
;
3126 struct ust_app_session
*ua_sess
= NULL
;
3127 struct ust_app_channel
*ua_chan
;
3128 struct ust_app_event
*ua_event
;
3129 struct ust_app_ctx
*ua_ctx
;
3134 DBG2("UST app global update for app sock %d for session id %d", sock
,
3139 app
= find_app_by_sock(sock
);
3142 * Application can be unregistered before so this is possible hence
3143 * simply stopping the update.
3145 DBG3("UST app update failed to find app sock %d", sock
);
3149 if (!app
->compatible
) {
3153 ret
= create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
3155 /* Tracer is probably gone or ENOMEM. */
3160 pthread_mutex_lock(&ua_sess
->lock
);
3163 * We can iterate safely here over all UST app session since the create ust
3164 * app session above made a shadow copy of the UST global domain from the
3167 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
3169 ret
= create_ust_channel(app
, ua_sess
, ua_chan
, usess
->consumer
);
3172 * Stop everything. On error, the application failed, no more file
3173 * descriptor are available or ENOMEM so stopping here is the only
3174 * thing we can do for now.
3179 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
3181 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
3188 /* For each events */
3189 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
3191 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3198 pthread_mutex_unlock(&ua_sess
->lock
);
3200 if (usess
->start_trace
) {
3201 ret
= ust_app_start_trace(usess
, app
);
3206 DBG2("UST trace started for app pid %d", app
->pid
);
3209 /* Everything went well at this point. */
3214 pthread_mutex_unlock(&ua_sess
->lock
);
3217 destroy_app_session(app
, ua_sess
);
3224 * Add context to a specific channel for global UST domain.
3226 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
3227 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
3230 struct lttng_ht_node_str
*ua_chan_node
;
3231 struct lttng_ht_iter iter
, uiter
;
3232 struct ust_app_channel
*ua_chan
= NULL
;
3233 struct ust_app_session
*ua_sess
;
3234 struct ust_app
*app
;
3238 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3239 if (!app
->compatible
) {
3241 * TODO: In time, we should notice the caller of this error by
3242 * telling him that this is a version error.
3246 ua_sess
= lookup_session_by_app(usess
, app
);
3247 if (ua_sess
== NULL
) {
3251 pthread_mutex_lock(&ua_sess
->lock
);
3252 /* Lookup channel in the ust app session */
3253 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3254 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3255 if (ua_chan_node
== NULL
) {
3258 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
3260 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
3265 pthread_mutex_unlock(&ua_sess
->lock
);
3273 * Enable event for a channel from a UST session for a specific PID.
3275 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
3276 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
3279 struct lttng_ht_iter iter
;
3280 struct lttng_ht_node_str
*ua_chan_node
;
3281 struct ust_app
*app
;
3282 struct ust_app_session
*ua_sess
;
3283 struct ust_app_channel
*ua_chan
;
3284 struct ust_app_event
*ua_event
;
3286 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
3290 app
= ust_app_find_by_pid(pid
);
3292 ERR("UST app enable event per PID %d not found", pid
);
3297 if (!app
->compatible
) {
3302 ua_sess
= lookup_session_by_app(usess
, app
);
3304 /* The application has problem or is probably dead. */
3309 pthread_mutex_lock(&ua_sess
->lock
);
3310 /* Lookup channel in the ust app session */
3311 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3312 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3313 /* If the channel is not found, there is a code flow error */
3314 assert(ua_chan_node
);
3316 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3318 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3319 uevent
->filter
, uevent
->attr
.loglevel
);
3320 if (ua_event
== NULL
) {
3321 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
3326 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
3333 pthread_mutex_unlock(&ua_sess
->lock
);
3340 * Disable event for a channel from a UST session for a specific PID.
3342 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
3343 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
3346 struct lttng_ht_iter iter
;
3347 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3348 struct ust_app
*app
;
3349 struct ust_app_session
*ua_sess
;
3350 struct ust_app_channel
*ua_chan
;
3351 struct ust_app_event
*ua_event
;
3353 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
3357 app
= ust_app_find_by_pid(pid
);
3359 ERR("UST app disable event per PID %d not found", pid
);
3364 if (!app
->compatible
) {
3369 ua_sess
= lookup_session_by_app(usess
, app
);
3371 /* The application has problem or is probably dead. */
3375 /* Lookup channel in the ust app session */
3376 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3377 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3378 if (ua_chan_node
== NULL
) {
3379 /* Channel does not exist, skip disabling */
3382 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3384 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
3385 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
3386 if (ua_event_node
== NULL
) {
3387 /* Event does not exist, skip disabling */
3390 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3392 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3403 * Calibrate registered applications.
3405 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
3408 struct lttng_ht_iter iter
;
3409 struct ust_app
*app
;
3413 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3414 if (!app
->compatible
) {
3416 * TODO: In time, we should notice the caller of this error by
3417 * telling him that this is a version error.
3422 health_code_update();
3424 ret
= ustctl_calibrate(app
->sock
, calibrate
);
3428 /* Means that it's not implemented on the tracer side. */
3432 DBG2("Calibrate app PID %d returned with error %d",
3439 DBG("UST app global domain calibration finished");
3443 health_code_update();
3449 * Receive registration and populate the given msg structure.
3451 * On success return 0 else a negative value returned by the ustctl call.
3453 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
3456 uint32_t pid
, ppid
, uid
, gid
;
3460 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
3461 &pid
, &ppid
, &uid
, &gid
,
3462 &msg
->bits_per_long
,
3463 &msg
->uint8_t_alignment
,
3464 &msg
->uint16_t_alignment
,
3465 &msg
->uint32_t_alignment
,
3466 &msg
->uint64_t_alignment
,
3467 &msg
->long_alignment
,
3474 case LTTNG_UST_ERR_EXITING
:
3475 DBG3("UST app recv reg message failed. Application died");
3477 case LTTNG_UST_ERR_UNSUP_MAJOR
:
3478 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
3479 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
3480 LTTNG_UST_ABI_MINOR_VERSION
);
3483 ERR("UST app recv reg message failed with ret %d", ret
);
3488 msg
->pid
= (pid_t
) pid
;
3489 msg
->ppid
= (pid_t
) ppid
;
3490 msg
->uid
= (uid_t
) uid
;
3491 msg
->gid
= (gid_t
) gid
;
3498 * Return a ust app channel object using the application object and the channel
3499 * object descriptor has a key. If not found, NULL is returned. A RCU read side
3500 * lock MUST be acquired before calling this function.
3502 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
3505 struct lttng_ht_node_ulong
*node
;
3506 struct lttng_ht_iter iter
;
3507 struct ust_app_channel
*ua_chan
= NULL
;
3511 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
3512 node
= lttng_ht_iter_get_node_ulong(&iter
);
3514 DBG2("UST app channel find by objd %d not found", objd
);
3518 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
3525 * Reply to a register channel notification from an application on the notify
3526 * socket. The channel metadata is also created.
3528 * The session UST registry lock is acquired in this function.
3530 * On success 0 is returned else a negative value.
3532 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
3533 size_t nr_fields
, struct ustctl_field
*fields
)
3535 int ret
, ret_code
= 0;
3536 uint32_t chan_id
, reg_count
;
3537 enum ustctl_channel_header type
;
3538 struct ust_app
*app
;
3539 struct ust_app_channel
*ua_chan
;
3540 struct ust_app_session
*ua_sess
;
3541 struct ust_registry_channel
*chan_reg
;
3545 /* Lookup application. If not found, there is a code flow error. */
3546 app
= find_app_by_notify_sock(sock
);
3548 DBG("Application socket %d is being teardown. Abort event notify",
3551 goto error_rcu_unlock
;
3554 /* Lookup channel by UST object descriptor. Should always be found. */
3555 ua_chan
= find_channel_by_objd(app
, cobjd
);
3557 assert(ua_chan
->session
);
3558 ua_sess
= ua_chan
->session
;
3560 pthread_mutex_lock(&ua_sess
->registry
->lock
);
3562 chan_reg
= ust_registry_channel_find(ua_sess
->registry
, ua_chan
->key
);
3565 if (ust_registry_is_max_id(ua_sess
->registry
->used_channel_id
)) {
3572 /* Don't assign ID to metadata. */
3573 if (ua_chan
->attr
.type
== LTTNG_UST_CHAN_METADATA
) {
3576 chan_id
= ust_registry_get_next_chan_id(ua_sess
->registry
);
3579 reg_count
= ust_registry_get_event_count(chan_reg
);
3580 if (reg_count
< 31) {
3581 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
3583 type
= USTCTL_CHANNEL_HEADER_LARGE
;
3586 chan_reg
->nr_ctx_fields
= nr_fields
;
3587 chan_reg
->ctx_fields
= fields
;
3588 chan_reg
->chan_id
= chan_id
;
3589 chan_reg
->header_type
= type
;
3591 /* Append to metadata */
3593 ret_code
= ust_metadata_channel_statedump(ua_chan
->session
->registry
,
3596 ERR("Error appending channel metadata (errno = %d)", ret_code
);
3602 DBG3("UST app replying to register channel with id %u, type: %d, ret: %d",
3603 chan_id
, type
, ret_code
);
3605 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
3607 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3608 ERR("UST app reply channel failed with ret %d", ret
);
3610 DBG3("UST app reply channel failed. Application died");
3616 pthread_mutex_unlock(&ua_sess
->registry
->lock
);
3623 * Add event to the UST channel registry. When the event is added to the
3624 * registry, the metadata is also created. Once done, this replies to the
3625 * application with the appropriate error code.
3627 * The session UST registry lock is acquired in the function.
3629 * On success 0 is returned else a negative value.
3631 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
3632 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
3633 char *model_emf_uri
)
3636 uint32_t event_id
= 0;
3637 struct ust_app
*app
;
3638 struct ust_app_channel
*ua_chan
;
3639 struct ust_app_session
*ua_sess
;
3643 /* Lookup application. If not found, there is a code flow error. */
3644 app
= find_app_by_notify_sock(sock
);
3646 DBG("Application socket %d is being teardown. Abort event notify",
3649 goto error_rcu_unlock
;
3652 /* Lookup channel by UST object descriptor. Should always be found. */
3653 ua_chan
= find_channel_by_objd(app
, cobjd
);
3655 assert(ua_chan
->session
);
3656 ua_sess
= ua_chan
->session
;
3658 pthread_mutex_lock(&ua_sess
->registry
->lock
);
3660 ret_code
= ust_registry_create_event(ua_sess
->registry
, ua_chan
->key
,
3661 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
, loglevel
,
3662 model_emf_uri
, &event_id
);
3665 * The return value is returned to ustctl so in case of an error, the
3666 * application can be notified. In case of an error, it's important not to
3667 * return a negative error or else the application will get closed.
3669 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
3671 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3672 ERR("UST app reply event failed with ret %d", ret
);
3674 DBG3("UST app reply event failed. Application died");
3677 * No need to wipe the create event since the application socket will
3678 * get close on error hence cleaning up everything by itself.
3683 DBG3("UST registry event %s has been added successfully", name
);
3686 pthread_mutex_unlock(&ua_sess
->registry
->lock
);
3693 * Handle application notification through the given notify socket.
3695 * Return 0 on success or else a negative value.
3697 int ust_app_recv_notify(int sock
)
3700 enum ustctl_notify_cmd cmd
;
3702 DBG3("UST app receiving notify from sock %d", sock
);
3704 ret
= ustctl_recv_notify(sock
, &cmd
);
3706 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3707 ERR("UST app recv notify failed with ret %d", ret
);
3709 DBG3("UST app recv notify failed. Application died");
3715 case USTCTL_NOTIFY_CMD_EVENT
:
3717 int sobjd
, cobjd
, loglevel
;
3718 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
3720 struct ustctl_field
*fields
;
3722 DBG2("UST app ustctl register event received");
3724 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
3725 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
3727 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3728 ERR("UST app recv event failed with ret %d", ret
);
3730 DBG3("UST app recv event failed. Application died");
3735 /* Add event to the UST registry coming from the notify socket. */
3736 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
3737 fields
, loglevel
, model_emf_uri
);
3744 case USTCTL_NOTIFY_CMD_CHANNEL
:
3748 struct ustctl_field
*fields
;
3750 DBG2("UST app ustctl register channel received");
3752 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
3755 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3756 ERR("UST app recv channel failed with ret %d", ret
);
3758 DBG3("UST app recv channel failed. Application died");
3763 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
3772 /* Should NEVER happen. */
3781 * Once the notify socket hangs up, this is called. First, it tries to find the
3782 * corresponding application. On failure, the call_rcu to close the socket is
3783 * executed. If an application is found, it tries to delete it from the notify
3784 * socket hash table. Whathever the result, it proceeds to the call_rcu.
3786 * Note that an object needs to be allocated here so on ENOMEM failure, the
3787 * call RCU is not done but the rest of the cleanup is.
3789 void ust_app_notify_sock_unregister(int sock
)
3792 struct lttng_ht_iter iter
;
3793 struct ust_app
*app
;
3794 struct ust_app_notify_sock_obj
*obj
;
3800 obj
= zmalloc(sizeof(*obj
));
3803 * An ENOMEM is kind of uncool. If this strikes we continue the
3804 * procedure but the call_rcu will not be called. In this case, we
3805 * accept the fd leak rather than possibly creating an unsynchronized
3806 * state between threads.
3808 * TODO: The notify object should be created once the notify socket is
3809 * registered and stored independantely from the ust app object. The
3810 * tricky part is to synchronize the teardown of the application and
3811 * this notify object. Let's keep that in mind so we can avoid this
3812 * kind of shenanigans with ENOMEM in the teardown path.
3819 DBG("UST app notify socket unregister %d", sock
);
3822 * Lookup application by notify socket. If this fails, this means that the
3823 * hash table delete has already been done by the application
3824 * unregistration process so we can safely close the notify socket in a
3827 app
= find_app_by_notify_sock(sock
);
3832 iter
.iter
.node
= &app
->notify_sock_n
.node
;
3835 * Whatever happens here either we fail or succeed, in both cases we have
3836 * to close the socket after a grace period to continue to the call RCU
3837 * here. If the deletion is successful, the application is not visible
3838 * anymore by other threads and is it fails it means that it was already
3839 * deleted from the hash table so either way we just have to close the
3842 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3848 * Close socket after a grace period to avoid for the socket to be reused
3849 * before the application object is freed creating potential race between
3850 * threads trying to add unique in the global hash table.
3853 call_rcu(&obj
->head
, close_notify_sock_rcu
);