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.
26 #include <sys/types.h>
28 #include <urcu/compiler.h>
29 #include <lttng/ust-error.h>
31 #include <common/common.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
34 #include "buffer-registry.h"
38 #include "ust-consumer.h"
41 /* Next available channel key. */
42 static unsigned long next_channel_key
;
43 static unsigned long next_session_id
;
46 * Return the atomically incremented value of next_channel_key.
48 static inline unsigned long get_next_channel_key(void)
50 return uatomic_add_return(&next_channel_key
, 1);
54 * Return the atomically incremented value of next_session_id.
56 static inline unsigned long get_next_session_id(void)
58 return uatomic_add_return(&next_session_id
, 1);
61 static void copy_channel_attr_to_ustctl(
62 struct ustctl_consumer_channel_attr
*attr
,
63 struct lttng_ust_channel_attr
*uattr
)
65 /* Copy event attributes since the layout is different. */
66 attr
->subbuf_size
= uattr
->subbuf_size
;
67 attr
->num_subbuf
= uattr
->num_subbuf
;
68 attr
->overwrite
= uattr
->overwrite
;
69 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
70 attr
->read_timer_interval
= uattr
->read_timer_interval
;
71 attr
->output
= uattr
->output
;
75 * Match function for the hash table lookup.
77 * It matches an ust app event based on three attributes which are the event
78 * name, the filter bytecode and the loglevel.
80 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
82 struct ust_app_event
*event
;
83 const struct ust_app_ht_key
*key
;
88 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
91 /* Match the 3 elements of the key: name, filter and loglevel. */
94 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
99 if (event
->attr
.loglevel
!= key
->loglevel
) {
100 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
101 && key
->loglevel
== 0 && event
->attr
.loglevel
== -1) {
103 * Match is accepted. This is because on event creation, the
104 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
105 * -1 are accepted for this loglevel type since 0 is the one set by
106 * the API when receiving an enable event.
113 /* One of the filters is NULL, fail. */
114 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
118 if (key
->filter
&& event
->filter
) {
119 /* Both filters exists, check length followed by the bytecode. */
120 if (event
->filter
->len
!= key
->filter
->len
||
121 memcmp(event
->filter
->data
, key
->filter
->data
,
122 event
->filter
->len
) != 0) {
135 * Unique add of an ust app event in the given ht. This uses the custom
136 * ht_match_ust_app_event match function and the event name as hash.
138 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
139 struct ust_app_event
*event
)
141 struct cds_lfht_node
*node_ptr
;
142 struct ust_app_ht_key key
;
146 assert(ua_chan
->events
);
149 ht
= ua_chan
->events
;
150 key
.name
= event
->attr
.name
;
151 key
.filter
= event
->filter
;
152 key
.loglevel
= event
->attr
.loglevel
;
154 node_ptr
= cds_lfht_add_unique(ht
->ht
,
155 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
156 ht_match_ust_app_event
, &key
, &event
->node
.node
);
157 assert(node_ptr
== &event
->node
.node
);
161 * Close the notify socket from the given RCU head object. This MUST be called
162 * through a call_rcu().
164 static void close_notify_sock_rcu(struct rcu_head
*head
)
167 struct ust_app_notify_sock_obj
*obj
=
168 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
170 /* Must have a valid fd here. */
171 assert(obj
->fd
>= 0);
173 ret
= close(obj
->fd
);
175 ERR("close notify sock %d RCU", obj
->fd
);
177 lttng_fd_put(LTTNG_FD_APPS
, 1);
183 * Return the session registry according to the buffer type of the given
186 * A registry per UID object MUST exists before calling this function or else
187 * it assert() if not found. RCU read side lock must be acquired.
189 static struct ust_registry_session
*get_session_registry(
190 struct ust_app_session
*ua_sess
)
192 struct ust_registry_session
*registry
= NULL
;
196 switch (ua_sess
->buffer_type
) {
197 case LTTNG_BUFFER_PER_PID
:
199 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
203 registry
= reg_pid
->registry
->reg
.ust
;
206 case LTTNG_BUFFER_PER_UID
:
208 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
209 ua_sess
->tracing_id
, ua_sess
->bits_per_long
, ua_sess
->uid
);
213 registry
= reg_uid
->registry
->reg
.ust
;
225 * Delete ust context safely. RCU read lock must be held before calling
229 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
236 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
237 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
238 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
239 sock
, ua_ctx
->obj
->handle
, ret
);
247 * Delete ust app event safely. RCU read lock must be held before calling
251 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
257 free(ua_event
->filter
);
259 if (ua_event
->obj
!= NULL
) {
260 ret
= ustctl_release_object(sock
, ua_event
->obj
);
261 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
262 ERR("UST app sock %d release event obj failed with ret %d",
271 * Release ust data object of the given stream.
273 * Return 0 on success or else a negative value.
275 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
282 ret
= ustctl_release_object(sock
, stream
->obj
);
283 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
284 ERR("UST app sock %d release stream obj failed with ret %d",
287 lttng_fd_put(LTTNG_FD_APPS
, 2);
295 * Delete ust app stream safely. RCU read lock must be held before calling
299 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
303 (void) release_ust_app_stream(sock
, stream
);
308 * Delete ust app channel safely. RCU read lock must be held before calling
312 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
316 struct lttng_ht_iter iter
;
317 struct ust_app_event
*ua_event
;
318 struct ust_app_ctx
*ua_ctx
;
319 struct ust_app_stream
*stream
, *stmp
;
320 struct ust_registry_session
*registry
;
324 DBG3("UST app deleting channel %s", ua_chan
->name
);
327 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
328 cds_list_del(&stream
->list
);
329 delete_ust_app_stream(sock
, stream
);
333 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
334 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
336 delete_ust_app_ctx(sock
, ua_ctx
);
338 lttng_ht_destroy(ua_chan
->ctx
);
341 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
343 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
345 delete_ust_app_event(sock
, ua_event
);
347 lttng_ht_destroy(ua_chan
->events
);
349 /* Wipe and free registry from session registry. */
350 registry
= get_session_registry(ua_chan
->session
);
352 ust_registry_channel_del_free(registry
, ua_chan
->key
);
355 if (ua_chan
->obj
!= NULL
) {
356 /* Remove channel from application UST object descriptor. */
357 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
358 lttng_ht_del(app
->ust_objd
, &iter
);
359 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
360 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
361 ERR("UST app sock %d release channel obj failed with ret %d",
364 lttng_fd_put(LTTNG_FD_APPS
, 1);
371 * For a given application and session, push metadata to consumer. The session
372 * lock MUST be acquired here before calling this.
374 * Return 0 on success else a negative error.
376 static int push_metadata(struct ust_registry_session
*registry
,
377 struct consumer_output
*consumer
)
380 char *metadata_str
= NULL
;
382 struct consumer_socket
*socket
;
390 * Means that no metadata was assigned to the session. This can happens if
391 * no start has been done previously.
393 if (!registry
->metadata_key
) {
395 goto error_rcu_unlock
;
398 /* Get consumer socket to use to push the metadata.*/
399 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
403 goto error_rcu_unlock
;
407 * TODO: Currently, we hold the socket lock around sampling of the next
408 * metadata segment to ensure we send metadata over the consumer socket in
409 * the correct order. This makes the registry lock nest inside the socket
412 * Please note that this is a temporary measure: we should move this lock
413 * back into ust_consumer_push_metadata() when the consumer gets the
414 * ability to reorder the metadata it receives.
416 pthread_mutex_lock(socket
->lock
);
417 pthread_mutex_lock(®istry
->lock
);
419 offset
= registry
->metadata_len_sent
;
420 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
422 DBG3("No metadata to push for metadata key %" PRIu64
,
423 registry
->metadata_key
);
425 goto error_reg_unlock
;
429 /* Allocate only what we have to send. */
430 metadata_str
= zmalloc(len
);
432 PERROR("zmalloc ust app metadata string");
434 goto error_reg_unlock
;
436 /* Copy what we haven't send out. */
437 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
439 pthread_mutex_unlock(®istry
->lock
);
441 ret
= consumer_push_metadata(socket
, registry
->metadata_key
,
442 metadata_str
, len
, offset
);
444 pthread_mutex_unlock(socket
->lock
);
445 goto error_rcu_unlock
;
448 /* Update len sent of the registry. */
449 pthread_mutex_lock(®istry
->lock
);
450 registry
->metadata_len_sent
+= len
;
451 pthread_mutex_unlock(®istry
->lock
);
452 pthread_mutex_unlock(socket
->lock
);
459 pthread_mutex_unlock(®istry
->lock
);
460 pthread_mutex_unlock(socket
->lock
);
468 * Send to the consumer a close metadata command for the given session. Once
469 * done, the metadata channel is deleted and the session metadata pointer is
470 * nullified. The session lock MUST be acquired here unless the application is
471 * in the destroy path.
473 * Return 0 on success else a negative value.
475 static int close_metadata(struct ust_registry_session
*registry
,
476 struct consumer_output
*consumer
)
479 struct consumer_socket
*socket
;
486 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
491 /* Get consumer socket to use to push the metadata.*/
492 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
499 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
504 /* Metadata successfully closed. Flag the registry. */
505 registry
->metadata_closed
= 1;
513 * Delete ust app session safely. RCU read lock must be held before calling
517 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
521 struct lttng_ht_iter iter
;
522 struct ust_app_channel
*ua_chan
;
523 struct ust_registry_session
*registry
;
527 registry
= get_session_registry(ua_sess
);
529 /* Push metadata for application before freeing the application. */
530 (void) push_metadata(registry
, ua_sess
->consumer
);
533 * Don't ask to close metadata for global per UID buffers. Close
534 * metadata only on destroy trace session in this case.
536 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
537 /* And ask to close it for this session registry. */
538 (void) close_metadata(registry
, ua_sess
->consumer
);
542 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
544 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
546 delete_ust_app_channel(sock
, ua_chan
, app
);
548 lttng_ht_destroy(ua_sess
->channels
);
550 /* In case of per PID, the registry is kept in the session. */
551 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
552 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
554 buffer_reg_pid_remove(reg_pid
);
555 buffer_reg_pid_destroy(reg_pid
);
559 if (ua_sess
->handle
!= -1) {
560 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
561 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
562 ERR("UST app sock %d release session handle failed with ret %d",
570 * Delete a traceable application structure from the global list. Never call
571 * this function outside of a call_rcu call.
574 void delete_ust_app(struct ust_app
*app
)
577 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
581 /* Delete ust app sessions info */
585 lttng_ht_destroy(app
->sessions
);
588 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
590 /* Free every object in the session and the session. */
591 delete_ust_app_session(sock
, ua_sess
, app
);
593 lttng_ht_destroy(app
->ust_objd
);
596 * Wait until we have deleted the application from the sock hash table
597 * before closing this socket, otherwise an application could re-use the
598 * socket ID and race with the teardown, using the same hash table entry.
600 * It's OK to leave the close in call_rcu. We want it to stay unique for
601 * all RCU readers that could run concurrently with unregister app,
602 * therefore we _need_ to only close that socket after a grace period. So
603 * it should stay in this RCU callback.
605 * This close() is a very important step of the synchronization model so
606 * every modification to this function must be carefully reviewed.
612 lttng_fd_put(LTTNG_FD_APPS
, 1);
614 DBG2("UST app pid %d deleted", app
->pid
);
621 * URCU intermediate call to delete an UST app.
624 void delete_ust_app_rcu(struct rcu_head
*head
)
626 struct lttng_ht_node_ulong
*node
=
627 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
628 struct ust_app
*app
=
629 caa_container_of(node
, struct ust_app
, pid_n
);
631 DBG3("Call RCU deleting app PID %d", app
->pid
);
636 * Delete the session from the application ht and delete the data structure by
637 * freeing every object inside and releasing them.
639 static void destroy_app_session(struct ust_app
*app
,
640 struct ust_app_session
*ua_sess
)
643 struct lttng_ht_iter iter
;
648 iter
.iter
.node
= &ua_sess
->node
.node
;
649 ret
= lttng_ht_del(app
->sessions
, &iter
);
651 /* Already scheduled for teardown. */
655 /* Once deleted, free the data structure. */
656 delete_ust_app_session(app
->sock
, ua_sess
, app
);
663 * Alloc new UST app session.
666 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
668 struct ust_app_session
*ua_sess
;
670 /* Init most of the default value by allocating and zeroing */
671 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
672 if (ua_sess
== NULL
) {
677 ua_sess
->handle
= -1;
678 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
679 pthread_mutex_init(&ua_sess
->lock
, NULL
);
688 * Alloc new UST app channel.
691 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
692 struct ust_app_session
*ua_sess
,
693 struct lttng_ust_channel_attr
*attr
)
695 struct ust_app_channel
*ua_chan
;
697 /* Init most of the default value by allocating and zeroing */
698 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
699 if (ua_chan
== NULL
) {
704 /* Setup channel name */
705 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
706 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
708 ua_chan
->enabled
= 1;
709 ua_chan
->handle
= -1;
710 ua_chan
->session
= ua_sess
;
711 ua_chan
->key
= get_next_channel_key();
712 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
713 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
714 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
716 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
718 /* Copy attributes */
720 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
721 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
722 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
723 ua_chan
->attr
.overwrite
= attr
->overwrite
;
724 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
725 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
726 ua_chan
->attr
.output
= attr
->output
;
728 /* By default, the channel is a per cpu channel. */
729 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
731 DBG3("UST app channel %s allocated", ua_chan
->name
);
740 * Allocate and initialize a UST app stream.
742 * Return newly allocated stream pointer or NULL on error.
744 struct ust_app_stream
*ust_app_alloc_stream(void)
746 struct ust_app_stream
*stream
= NULL
;
748 stream
= zmalloc(sizeof(*stream
));
749 if (stream
== NULL
) {
750 PERROR("zmalloc ust app stream");
754 /* Zero could be a valid value for a handle so flag it to -1. */
762 * Alloc new UST app event.
765 struct ust_app_event
*alloc_ust_app_event(char *name
,
766 struct lttng_ust_event
*attr
)
768 struct ust_app_event
*ua_event
;
770 /* Init most of the default value by allocating and zeroing */
771 ua_event
= zmalloc(sizeof(struct ust_app_event
));
772 if (ua_event
== NULL
) {
777 ua_event
->enabled
= 1;
778 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
779 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
780 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
782 /* Copy attributes */
784 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
787 DBG3("UST app event %s allocated", ua_event
->name
);
796 * Alloc new UST app context.
799 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
801 struct ust_app_ctx
*ua_ctx
;
803 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
804 if (ua_ctx
== NULL
) {
809 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
812 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
819 * Allocate a filter and copy the given original filter.
821 * Return allocated filter or NULL on error.
823 static struct lttng_ust_filter_bytecode
*alloc_copy_ust_app_filter(
824 struct lttng_ust_filter_bytecode
*orig_f
)
826 struct lttng_ust_filter_bytecode
*filter
= NULL
;
828 /* Copy filter bytecode */
829 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
831 PERROR("zmalloc alloc ust app filter");
835 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
842 * Find an ust_app using the sock and return it. RCU read side lock must be
843 * held before calling this helper function.
846 struct ust_app
*find_app_by_sock(int sock
)
848 struct lttng_ht_node_ulong
*node
;
849 struct lttng_ht_iter iter
;
851 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
852 node
= lttng_ht_iter_get_node_ulong(&iter
);
854 DBG2("UST app find by sock %d not found", sock
);
858 return caa_container_of(node
, struct ust_app
, sock_n
);
865 * Find an ust_app using the notify sock and return it. RCU read side lock must
866 * be held before calling this helper function.
868 static struct ust_app
*find_app_by_notify_sock(int sock
)
870 struct lttng_ht_node_ulong
*node
;
871 struct lttng_ht_iter iter
;
873 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
875 node
= lttng_ht_iter_get_node_ulong(&iter
);
877 DBG2("UST app find by notify sock %d not found", sock
);
881 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
888 * Lookup for an ust app event based on event name, filter bytecode and the
891 * Return an ust_app_event object or NULL on error.
893 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
894 char *name
, struct lttng_ust_filter_bytecode
*filter
, int loglevel
)
896 struct lttng_ht_iter iter
;
897 struct lttng_ht_node_str
*node
;
898 struct ust_app_event
*event
= NULL
;
899 struct ust_app_ht_key key
;
904 /* Setup key for event lookup. */
907 key
.loglevel
= loglevel
;
909 /* Lookup using the event name as hash and a custom match fct. */
910 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
911 ht_match_ust_app_event
, &key
, &iter
.iter
);
912 node
= lttng_ht_iter_get_node_str(&iter
);
917 event
= caa_container_of(node
, struct ust_app_event
, node
);
924 * Create the channel context on the tracer.
926 * Called with UST app session lock held.
929 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
930 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
934 health_code_update();
936 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
937 ua_chan
->obj
, &ua_ctx
->obj
);
939 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
940 ERR("UST app create channel context failed for app (pid: %d) "
941 "with ret %d", app
->pid
, ret
);
943 DBG3("UST app disable event failed. Application is dead.");
948 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
950 DBG2("UST app context handle %d created successfully for channel %s",
951 ua_ctx
->handle
, ua_chan
->name
);
954 health_code_update();
959 * Set the filter on the tracer.
962 int set_ust_event_filter(struct ust_app_event
*ua_event
,
967 health_code_update();
969 if (!ua_event
->filter
) {
974 ret
= ustctl_set_filter(app
->sock
, ua_event
->filter
,
977 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
978 ERR("UST app event %s filter failed for app (pid: %d) "
979 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
981 DBG3("UST app filter event failed. Application is dead.");
986 DBG2("UST filter set successfully for event %s", ua_event
->name
);
989 health_code_update();
994 * Disable the specified event on to UST tracer for the UST session.
996 static int disable_ust_event(struct ust_app
*app
,
997 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1001 health_code_update();
1003 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1005 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1006 ERR("UST app event %s disable failed for app (pid: %d) "
1007 "and session handle %d with ret %d",
1008 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1010 DBG3("UST app disable event failed. Application is dead.");
1015 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1016 ua_event
->attr
.name
, app
->pid
);
1019 health_code_update();
1024 * Disable the specified channel on to UST tracer for the UST session.
1026 static int disable_ust_channel(struct ust_app
*app
,
1027 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1031 health_code_update();
1033 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1035 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1036 ERR("UST app channel %s disable failed for app (pid: %d) "
1037 "and session handle %d with ret %d",
1038 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1040 DBG3("UST app disable channel failed. Application is dead.");
1045 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1046 ua_chan
->name
, app
->pid
);
1049 health_code_update();
1054 * Enable the specified channel on to UST tracer for the UST session.
1056 static int enable_ust_channel(struct ust_app
*app
,
1057 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1061 health_code_update();
1063 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1065 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1066 ERR("UST app channel %s enable failed for app (pid: %d) "
1067 "and session handle %d with ret %d",
1068 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1070 DBG3("UST app enable channel failed. Application is dead.");
1075 ua_chan
->enabled
= 1;
1077 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1078 ua_chan
->name
, app
->pid
);
1081 health_code_update();
1086 * Enable the specified event on to UST tracer for the UST session.
1088 static int enable_ust_event(struct ust_app
*app
,
1089 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1093 health_code_update();
1095 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1097 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1098 ERR("UST app event %s enable failed for app (pid: %d) "
1099 "and session handle %d with ret %d",
1100 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1102 DBG3("UST app enable event failed. Application is dead.");
1107 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1108 ua_event
->attr
.name
, app
->pid
);
1111 health_code_update();
1116 * Send channel and stream buffer to application.
1118 * Return 0 on success. On error, a negative value is returned.
1120 static int send_channel_pid_to_ust(struct ust_app
*app
,
1121 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1124 struct ust_app_stream
*stream
, *stmp
;
1130 health_code_update();
1132 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1135 /* Send channel to the application. */
1136 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1141 health_code_update();
1143 /* Send all streams to application. */
1144 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1145 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1149 /* We don't need the stream anymore once sent to the tracer. */
1150 cds_list_del(&stream
->list
);
1151 delete_ust_app_stream(-1, stream
);
1153 /* Flag the channel that it is sent to the application. */
1154 ua_chan
->is_sent
= 1;
1157 health_code_update();
1162 * Create the specified event onto the UST tracer for a UST session.
1164 * Should be called with session mutex held.
1167 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1168 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1172 health_code_update();
1174 /* Create UST event on tracer */
1175 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1178 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1179 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1180 ua_event
->attr
.name
, app
->pid
, ret
);
1182 DBG3("UST app create event failed. Application is dead.");
1187 ua_event
->handle
= ua_event
->obj
->handle
;
1189 DBG2("UST app event %s created successfully for pid:%d",
1190 ua_event
->attr
.name
, app
->pid
);
1192 health_code_update();
1194 /* Set filter if one is present. */
1195 if (ua_event
->filter
) {
1196 ret
= set_ust_event_filter(ua_event
, app
);
1202 /* If event not enabled, disable it on the tracer */
1203 if (ua_event
->enabled
== 0) {
1204 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1207 * If we hit an EPERM, something is wrong with our disable call. If
1208 * we get an EEXIST, there is a problem on the tracer side since we
1212 case -LTTNG_UST_ERR_PERM
:
1213 /* Code flow problem */
1215 case -LTTNG_UST_ERR_EXIST
:
1216 /* It's OK for our use case. */
1227 health_code_update();
1232 * Copy data between an UST app event and a LTT event.
1234 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1235 struct ltt_ust_event
*uevent
)
1237 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1238 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1240 ua_event
->enabled
= uevent
->enabled
;
1242 /* Copy event attributes */
1243 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1245 /* Copy filter bytecode */
1246 if (uevent
->filter
) {
1247 ua_event
->filter
= alloc_copy_ust_app_filter(uevent
->filter
);
1248 /* Filter might be NULL here in case of ENONEM. */
1253 * Copy data between an UST app channel and a LTT channel.
1255 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1256 struct ltt_ust_channel
*uchan
)
1258 struct lttng_ht_iter iter
;
1259 struct ltt_ust_event
*uevent
;
1260 struct ltt_ust_context
*uctx
;
1261 struct ust_app_event
*ua_event
;
1262 struct ust_app_ctx
*ua_ctx
;
1264 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1266 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1267 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1269 /* Copy event attributes since the layout is different. */
1270 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1271 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1272 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1273 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1274 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1275 ua_chan
->attr
.output
= uchan
->attr
.output
;
1277 * Note that the attribute channel type is not set since the channel on the
1278 * tracing registry side does not have this information.
1281 ua_chan
->enabled
= uchan
->enabled
;
1282 ua_chan
->tracing_channel_id
= uchan
->id
;
1284 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
1285 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1286 if (ua_ctx
== NULL
) {
1289 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1290 (unsigned long) ua_ctx
->ctx
.ctx
);
1291 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1294 /* Copy all events from ltt ust channel to ust app channel */
1295 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1296 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1297 uevent
->filter
, uevent
->attr
.loglevel
);
1298 if (ua_event
== NULL
) {
1299 DBG2("UST event %s not found on shadow copy channel",
1301 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1302 if (ua_event
== NULL
) {
1305 shadow_copy_event(ua_event
, uevent
);
1306 add_unique_ust_app_event(ua_chan
, ua_event
);
1310 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1314 * Copy data between a UST app session and a regular LTT session.
1316 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1317 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1319 struct lttng_ht_node_str
*ua_chan_node
;
1320 struct lttng_ht_iter iter
;
1321 struct ltt_ust_channel
*uchan
;
1322 struct ust_app_channel
*ua_chan
;
1324 struct tm
*timeinfo
;
1328 /* Get date and time for unique app path */
1330 timeinfo
= localtime(&rawtime
);
1331 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1333 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1335 ua_sess
->tracing_id
= usess
->id
;
1336 ua_sess
->id
= get_next_session_id();
1337 ua_sess
->uid
= app
->uid
;
1338 ua_sess
->gid
= app
->gid
;
1339 ua_sess
->euid
= usess
->uid
;
1340 ua_sess
->egid
= usess
->gid
;
1341 ua_sess
->buffer_type
= usess
->buffer_type
;
1342 ua_sess
->bits_per_long
= app
->bits_per_long
;
1343 /* There is only one consumer object per session possible. */
1344 ua_sess
->consumer
= usess
->consumer
;
1346 switch (ua_sess
->buffer_type
) {
1347 case LTTNG_BUFFER_PER_PID
:
1348 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1349 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s/", app
->name
, app
->pid
,
1352 case LTTNG_BUFFER_PER_UID
:
1353 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1354 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1361 PERROR("asprintf UST shadow copy session");
1366 /* Iterate over all channels in global domain. */
1367 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1369 struct lttng_ht_iter uiter
;
1371 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1372 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1373 if (ua_chan_node
!= NULL
) {
1374 /* Session exist. Contiuing. */
1378 DBG2("Channel %s not found on shadow session copy, creating it",
1380 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1381 if (ua_chan
== NULL
) {
1382 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1385 shadow_copy_channel(ua_chan
, uchan
);
1387 * The concept of metadata channel does not exist on the tracing
1388 * registry side of the session daemon so this can only be a per CPU
1389 * channel and not metadata.
1391 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1393 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1401 * Lookup sesison wrapper.
1404 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1405 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1407 /* Get right UST app session from app */
1408 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
1412 * Return ust app session from the app session hashtable using the UST session
1415 static struct ust_app_session
*lookup_session_by_app(
1416 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1418 struct lttng_ht_iter iter
;
1419 struct lttng_ht_node_ulong
*node
;
1421 __lookup_session_by_app(usess
, app
, &iter
);
1422 node
= lttng_ht_iter_get_node_ulong(&iter
);
1427 return caa_container_of(node
, struct ust_app_session
, node
);
1434 * Setup buffer registry per PID for the given session and application. If none
1435 * is found, a new one is created, added to the global registry and
1436 * initialized. If regp is valid, it's set with the newly created object.
1438 * Return 0 on success or else a negative value.
1440 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1441 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1444 struct buffer_reg_pid
*reg_pid
;
1451 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1454 * This is the create channel path meaning that if there is NO
1455 * registry available, we have to create one for this session.
1457 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
);
1461 buffer_reg_pid_add(reg_pid
);
1466 /* Initialize registry. */
1467 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1468 app
->bits_per_long
, app
->uint8_t_alignment
,
1469 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1470 app
->uint64_t_alignment
, app
->long_alignment
, app
->byte_order
);
1475 DBG3("UST app buffer registry per PID created successfully");
1487 * Setup buffer registry per UID for the given session and application. If none
1488 * is found, a new one is created, added to the global registry and
1489 * initialized. If regp is valid, it's set with the newly created object.
1491 * Return 0 on success or else a negative value.
1493 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
1494 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
1497 struct buffer_reg_uid
*reg_uid
;
1504 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
1507 * This is the create channel path meaning that if there is NO
1508 * registry available, we have to create one for this session.
1510 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
1511 LTTNG_DOMAIN_UST
, ®_uid
);
1515 buffer_reg_uid_add(reg_uid
);
1520 /* Initialize registry. */
1521 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, app
,
1522 app
->bits_per_long
, app
->uint8_t_alignment
,
1523 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1524 app
->uint64_t_alignment
, app
->long_alignment
, app
->byte_order
);
1528 /* Add node to teardown list of the session. */
1529 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
1531 DBG3("UST app buffer registry per UID created successfully");
1543 * Create a session on the tracer side for the given app.
1545 * On success, ua_sess_ptr is populated with the session pointer or else left
1546 * untouched. If the session was created, is_created is set to 1. On error,
1547 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1550 * Returns 0 on success or else a negative code which is either -ENOMEM or
1551 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1553 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1554 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1557 int ret
, created
= 0;
1558 struct ust_app_session
*ua_sess
;
1562 assert(ua_sess_ptr
);
1564 health_code_update();
1566 ua_sess
= lookup_session_by_app(usess
, app
);
1567 if (ua_sess
== NULL
) {
1568 DBG2("UST app pid: %d session id %d not found, creating it",
1569 app
->pid
, usess
->id
);
1570 ua_sess
= alloc_ust_app_session(app
);
1571 if (ua_sess
== NULL
) {
1572 /* Only malloc can failed so something is really wrong */
1576 shadow_copy_session(ua_sess
, usess
, app
);
1580 switch (usess
->buffer_type
) {
1581 case LTTNG_BUFFER_PER_PID
:
1582 /* Init local registry. */
1583 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
1588 case LTTNG_BUFFER_PER_UID
:
1589 /* Look for a global registry. If none exists, create one. */
1590 ret
= setup_buffer_reg_uid(usess
, app
, NULL
);
1601 health_code_update();
1603 if (ua_sess
->handle
== -1) {
1604 ret
= ustctl_create_session(app
->sock
);
1606 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1607 ERR("Creating session for app pid %d with ret %d",
1610 DBG("UST app creating session failed. Application is dead");
1612 delete_ust_app_session(-1, ua_sess
, app
);
1613 if (ret
!= -ENOMEM
) {
1615 * Tracer is probably gone or got an internal error so let's
1616 * behave like it will soon unregister or not usable.
1623 ua_sess
->handle
= ret
;
1625 /* Add ust app session to app's HT */
1626 lttng_ht_node_init_ulong(&ua_sess
->node
,
1627 (unsigned long) ua_sess
->tracing_id
);
1628 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
1630 DBG2("UST app session created successfully with handle %d", ret
);
1633 *ua_sess_ptr
= ua_sess
;
1635 *is_created
= created
;
1638 /* Everything went well. */
1642 health_code_update();
1647 * Create a context for the channel on the tracer.
1649 * Called with UST app session lock held and a RCU read side lock.
1652 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
1653 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
1654 struct ust_app
*app
)
1657 struct lttng_ht_iter iter
;
1658 struct lttng_ht_node_ulong
*node
;
1659 struct ust_app_ctx
*ua_ctx
;
1661 DBG2("UST app adding context to channel %s", ua_chan
->name
);
1663 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
1664 node
= lttng_ht_iter_get_node_ulong(&iter
);
1670 ua_ctx
= alloc_ust_app_ctx(uctx
);
1671 if (ua_ctx
== NULL
) {
1677 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
1678 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1680 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
1690 * Enable on the tracer side a ust app event for the session and channel.
1692 * Called with UST app session lock held.
1695 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
1696 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1700 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1705 ua_event
->enabled
= 1;
1712 * Disable on the tracer side a ust app event for the session and channel.
1714 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1715 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1719 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1724 ua_event
->enabled
= 0;
1731 * Lookup ust app channel for session and disable it on the tracer side.
1734 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1735 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1739 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1744 ua_chan
->enabled
= 0;
1751 * Lookup ust app channel for session and enable it on the tracer side. This
1752 * MUST be called with a RCU read side lock acquired.
1754 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1755 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1758 struct lttng_ht_iter iter
;
1759 struct lttng_ht_node_str
*ua_chan_node
;
1760 struct ust_app_channel
*ua_chan
;
1762 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1763 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1764 if (ua_chan_node
== NULL
) {
1765 DBG2("Unable to find channel %s in ust session id %u",
1766 uchan
->name
, ua_sess
->tracing_id
);
1770 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1772 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1782 * Ask the consumer to create a channel and get it if successful.
1784 * Return 0 on success or else a negative value.
1786 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
1787 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
1788 int bitness
, struct ust_registry_session
*registry
)
1791 unsigned int nb_fd
= 0;
1792 struct consumer_socket
*socket
;
1800 health_code_update();
1802 /* Get the right consumer socket for the application. */
1803 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
1809 health_code_update();
1811 /* Need one fd for the channel. */
1812 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1814 ERR("Exhausted number of available FD upon create channel");
1819 * Ask consumer to create channel. The consumer will return the number of
1820 * stream we have to expect.
1822 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
1829 * Compute the number of fd needed before receiving them. It must be 2 per
1830 * stream (2 being the default value here).
1832 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
1834 /* Reserve the amount of file descriptor we need. */
1835 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
1837 ERR("Exhausted number of available FD upon create channel");
1838 goto error_fd_get_stream
;
1841 health_code_update();
1844 * Now get the channel from the consumer. This call wil populate the stream
1845 * list of that channel and set the ust objects.
1847 ret
= ust_consumer_get_channel(socket
, ua_chan
);
1856 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
1857 error_fd_get_stream
:
1859 * Initiate a destroy channel on the consumer since we had an error
1860 * handling it on our side. The return value is of no importance since we
1861 * already have a ret value set by the previous error that we need to
1864 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
1866 lttng_fd_put(LTTNG_FD_APPS
, 1);
1868 health_code_update();
1874 * Duplicate the ust data object of the ust app stream and save it in the
1875 * buffer registry stream.
1877 * Return 0 on success or else a negative value.
1879 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
1880 struct ust_app_stream
*stream
)
1887 /* Reserve the amount of file descriptor we need. */
1888 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
1890 ERR("Exhausted number of available FD upon duplicate stream");
1894 /* Duplicate object for stream once the original is in the registry. */
1895 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
1896 reg_stream
->obj
.ust
);
1898 ERR("Duplicate stream obj from %p to %p failed with ret %d",
1899 reg_stream
->obj
.ust
, stream
->obj
, ret
);
1900 lttng_fd_put(LTTNG_FD_APPS
, 2);
1903 stream
->handle
= stream
->obj
->handle
;
1910 * Duplicate the ust data object of the ust app. channel and save it in the
1911 * buffer registry channel.
1913 * Return 0 on success or else a negative value.
1915 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
1916 struct ust_app_channel
*ua_chan
)
1923 /* Need two fds for the channel. */
1924 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1926 ERR("Exhausted number of available FD upon duplicate channel");
1930 /* Duplicate object for stream once the original is in the registry. */
1931 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
1933 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
1934 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
1937 ua_chan
->handle
= ua_chan
->obj
->handle
;
1942 lttng_fd_put(LTTNG_FD_APPS
, 1);
1948 * For a given channel buffer registry, setup all streams of the given ust
1949 * application channel.
1951 * Return 0 on success or else a negative value.
1953 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
1954 struct ust_app_channel
*ua_chan
)
1957 struct ust_app_stream
*stream
, *stmp
;
1962 DBG2("UST app setup buffer registry stream");
1964 /* Send all streams to application. */
1965 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1966 struct buffer_reg_stream
*reg_stream
;
1968 ret
= buffer_reg_stream_create(®_stream
);
1974 * Keep original pointer and nullify it in the stream so the delete
1975 * stream call does not release the object.
1977 reg_stream
->obj
.ust
= stream
->obj
;
1979 buffer_reg_stream_add(reg_stream
, reg_chan
);
1981 /* We don't need the streams anymore. */
1982 cds_list_del(&stream
->list
);
1983 delete_ust_app_stream(-1, stream
);
1991 * Create a buffer registry channel for the given session registry and
1992 * application channel object. If regp pointer is valid, it's set with the
1993 * created object. Important, the created object is NOT added to the session
1994 * registry hash table.
1996 * Return 0 on success else a negative value.
1998 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
1999 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2002 struct buffer_reg_channel
*reg_chan
= NULL
;
2007 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2009 /* Create buffer registry channel. */
2010 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2015 reg_chan
->consumer_key
= ua_chan
->key
;
2017 /* Create and add a channel registry to session. */
2018 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2019 ua_chan
->tracing_channel_id
);
2023 buffer_reg_channel_add(reg_sess
, reg_chan
);
2032 /* Safe because the registry channel object was not added to any HT. */
2033 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2039 * Setup buffer registry channel for the given session registry and application
2040 * channel object. If regp pointer is valid, it's set with the created object.
2042 * Return 0 on success else a negative value.
2044 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2045 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
)
2052 assert(ua_chan
->obj
);
2054 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2056 /* Setup all streams for the registry. */
2057 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
);
2062 reg_chan
->obj
.ust
= ua_chan
->obj
;
2063 ua_chan
->obj
= NULL
;
2068 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2069 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2074 * Send buffer registry channel to the application.
2076 * Return 0 on success else a negative value.
2078 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2079 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2080 struct ust_app_channel
*ua_chan
)
2083 struct buffer_reg_stream
*reg_stream
;
2090 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2092 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2097 /* Send channel to the application. */
2098 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2103 health_code_update();
2105 /* Send all streams to application. */
2106 pthread_mutex_lock(®_chan
->stream_list_lock
);
2107 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2108 struct ust_app_stream stream
;
2110 ret
= duplicate_stream_object(reg_stream
, &stream
);
2112 goto error_stream_unlock
;
2115 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2117 goto error_stream_unlock
;
2121 * The return value is not important here. This function will output an
2124 (void) release_ust_app_stream(-1, &stream
);
2126 ua_chan
->is_sent
= 1;
2128 error_stream_unlock
:
2129 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2135 * Create and send to the application the created buffers with per UID buffers.
2137 * Return 0 on success else a negative value.
2139 static int create_channel_per_uid(struct ust_app
*app
,
2140 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2141 struct ust_app_channel
*ua_chan
)
2144 struct buffer_reg_uid
*reg_uid
;
2145 struct buffer_reg_channel
*reg_chan
;
2152 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2154 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2156 * The session creation handles the creation of this global registry
2157 * object. If none can be find, there is a code flow problem or a
2162 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2165 /* Create the buffer registry channel object. */
2166 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2173 * Create the buffers on the consumer side. This call populates the
2174 * ust app channel object with all streams and data object.
2176 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2177 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2183 * Setup the streams and add it to the session registry.
2185 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, reg_chan
);
2192 /* Send buffers to the application. */
2193 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2203 * Create and send to the application the created buffers with per PID buffers.
2205 * Return 0 on success else a negative value.
2207 static int create_channel_per_pid(struct ust_app
*app
,
2208 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2209 struct ust_app_channel
*ua_chan
)
2212 struct ust_registry_session
*registry
;
2219 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2223 registry
= get_session_registry(ua_sess
);
2226 /* Create and add a new channel registry to session. */
2227 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2232 /* Create and get channel on the consumer side. */
2233 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2234 app
->bits_per_long
, registry
);
2239 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2250 * From an already allocated ust app channel, create the channel buffers if
2251 * need and send it to the application. This MUST be called with a RCU read
2252 * side lock acquired.
2254 * Return 0 on success or else a negative value.
2256 static int do_create_channel(struct ust_app
*app
,
2257 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2258 struct ust_app_channel
*ua_chan
)
2267 /* Handle buffer type before sending the channel to the application. */
2268 switch (usess
->buffer_type
) {
2269 case LTTNG_BUFFER_PER_UID
:
2271 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
2277 case LTTNG_BUFFER_PER_PID
:
2279 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
2291 /* Initialize ust objd object using the received handle and add it. */
2292 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
2293 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
2295 /* If channel is not enabled, disable it on the tracer */
2296 if (!ua_chan
->enabled
) {
2297 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2308 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2309 * newly created channel if not NULL.
2311 * Called with UST app session lock held.
2313 * Return 0 on success or else a negative value.
2315 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
2316 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
2317 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
2318 struct ust_app_channel
**ua_chanp
)
2321 struct lttng_ht_iter iter
;
2322 struct lttng_ht_node_str
*ua_chan_node
;
2323 struct ust_app_channel
*ua_chan
;
2325 /* Lookup channel in the ust app session */
2326 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2327 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2328 if (ua_chan_node
!= NULL
) {
2329 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2333 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
2334 if (ua_chan
== NULL
) {
2335 /* Only malloc can fail here */
2339 shadow_copy_channel(ua_chan
, uchan
);
2341 /* Set channel type. */
2342 ua_chan
->attr
.type
= type
;
2344 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
2349 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
2352 /* Only add the channel if successful on the tracer side. */
2353 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
2357 *ua_chanp
= ua_chan
;
2360 /* Everything went well. */
2364 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
2369 * Create UST app event and create it on the tracer side.
2371 * Called with ust app session mutex held.
2374 int create_ust_app_event(struct ust_app_session
*ua_sess
,
2375 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
2376 struct ust_app
*app
)
2379 struct ust_app_event
*ua_event
;
2381 /* Get event node */
2382 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2383 uevent
->filter
, uevent
->attr
.loglevel
);
2384 if (ua_event
!= NULL
) {
2389 /* Does not exist so create one */
2390 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
2391 if (ua_event
== NULL
) {
2392 /* Only malloc can failed so something is really wrong */
2396 shadow_copy_event(ua_event
, uevent
);
2398 /* Create it on the tracer side */
2399 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2401 /* Not found previously means that it does not exist on the tracer */
2402 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
2406 add_unique_ust_app_event(ua_chan
, ua_event
);
2408 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
2415 /* Valid. Calling here is already in a read side lock */
2416 delete_ust_app_event(-1, ua_event
);
2421 * Create UST metadata and open it on the tracer side.
2423 * Called with UST app session lock held and RCU read side lock.
2425 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
2426 struct ust_app
*app
, struct consumer_output
*consumer
,
2427 struct ustctl_consumer_channel_attr
*attr
)
2430 struct ust_app_channel
*metadata
;
2431 struct consumer_socket
*socket
;
2432 struct ust_registry_session
*registry
;
2438 registry
= get_session_registry(ua_sess
);
2441 /* Metadata already exists for this registry. */
2442 if (registry
->metadata_key
) {
2447 /* Allocate UST metadata */
2448 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
2450 /* malloc() failed */
2456 /* Set default attributes for metadata. */
2457 metadata
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
2458 metadata
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
2459 metadata
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
2460 metadata
->attr
.switch_timer_interval
= DEFAULT_UST_CHANNEL_SWITCH_TIMER
;
2461 metadata
->attr
.read_timer_interval
= DEFAULT_UST_CHANNEL_READ_TIMER
;
2462 metadata
->attr
.output
= LTTNG_UST_MMAP
;
2463 metadata
->attr
.type
= LTTNG_UST_CHAN_METADATA
;
2465 memcpy(&metadata
->attr
, attr
, sizeof(metadata
->attr
));
2466 metadata
->attr
.output
= LTTNG_UST_MMAP
;
2467 metadata
->attr
.type
= LTTNG_UST_CHAN_METADATA
;
2470 /* Get the right consumer socket for the application. */
2471 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
2474 goto error_consumer
;
2477 /* Need one fd for the channel. */
2478 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2480 ERR("Exhausted number of available FD upon create metadata");
2485 * Ask the metadata channel creation to the consumer. The metadata object
2486 * will be created by the consumer and kept their. However, the stream is
2487 * never added or monitored until we do a first push metadata to the
2490 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
2494 * Safe because the metadata obj pointer is not set so the delete below
2495 * will not put a FD back again.
2497 lttng_fd_put(LTTNG_FD_APPS
, 1);
2498 goto error_consumer
;
2502 * The setup command will make the metadata stream be sent to the relayd,
2503 * if applicable, and the thread managing the metadatas. This is important
2504 * because after this point, if an error occurs, the only way the stream
2505 * can be deleted is to be monitored in the consumer.
2507 ret
= consumer_setup_metadata(socket
, metadata
->key
);
2510 * Safe because the metadata obj pointer is not set so the delete below
2511 * will not put a FD back again.
2513 lttng_fd_put(LTTNG_FD_APPS
, 1);
2514 goto error_consumer
;
2517 /* Keep metadata key so we can identify it on the consumer side. */
2518 registry
->metadata_key
= metadata
->key
;
2520 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
2521 metadata
->key
, app
->pid
);
2524 delete_ust_app_channel(-1, metadata
, app
);
2530 * Return pointer to traceable apps list.
2532 struct lttng_ht
*ust_app_get_ht(void)
2538 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
2539 * acquired before calling this function.
2541 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
2543 struct ust_app
*app
= NULL
;
2544 struct lttng_ht_node_ulong
*node
;
2545 struct lttng_ht_iter iter
;
2547 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
2548 node
= lttng_ht_iter_get_node_ulong(&iter
);
2550 DBG2("UST app no found with pid %d", pid
);
2554 DBG2("Found UST app by pid %d", pid
);
2556 app
= caa_container_of(node
, struct ust_app
, pid_n
);
2563 * Allocate and init an UST app object using the registration information and
2564 * the command socket. This is called when the command socket connects to the
2567 * The object is returned on success or else NULL.
2569 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
2571 struct ust_app
*lta
= NULL
;
2576 DBG3("UST app creating application for socket %d", sock
);
2578 if ((msg
->bits_per_long
== 64 &&
2579 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
2580 || (msg
->bits_per_long
== 32 &&
2581 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
2582 ERR("Registration failed: application \"%s\" (pid: %d) has "
2583 "%d-bit long, but no consumerd for this size is available.\n",
2584 msg
->name
, msg
->pid
, msg
->bits_per_long
);
2588 lta
= zmalloc(sizeof(struct ust_app
));
2594 lta
->ppid
= msg
->ppid
;
2595 lta
->uid
= msg
->uid
;
2596 lta
->gid
= msg
->gid
;
2598 lta
->bits_per_long
= msg
->bits_per_long
;
2599 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
2600 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
2601 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
2602 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
2603 lta
->long_alignment
= msg
->long_alignment
;
2604 lta
->byte_order
= msg
->byte_order
;
2606 lta
->v_major
= msg
->major
;
2607 lta
->v_minor
= msg
->minor
;
2608 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2609 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2610 lta
->notify_sock
= -1;
2612 /* Copy name and make sure it's NULL terminated. */
2613 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
2614 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
2617 * Before this can be called, when receiving the registration information,
2618 * the application compatibility is checked. So, at this point, the
2619 * application can work with this session daemon.
2621 lta
->compatible
= 1;
2623 lta
->pid
= msg
->pid
;
2624 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
2626 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
2628 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
2635 * For a given application object, add it to every hash table.
2637 void ust_app_add(struct ust_app
*app
)
2640 assert(app
->notify_sock
>= 0);
2645 * On a re-registration, we want to kick out the previous registration of
2648 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
2651 * The socket _should_ be unique until _we_ call close. So, a add_unique
2652 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
2653 * already in the table.
2655 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
2657 /* Add application to the notify socket hash table. */
2658 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
2659 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
2661 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
2662 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
2663 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
2670 * Set the application version into the object.
2672 * Return 0 on success else a negative value either an errno code or a
2673 * LTTng-UST error code.
2675 int ust_app_version(struct ust_app
*app
)
2681 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
2683 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
2684 ERR("UST app %d verson failed with ret %d", app
->sock
, ret
);
2686 DBG3("UST app %d verion failed. Application is dead", app
->sock
);
2694 * Unregister app by removing it from the global traceable app list and freeing
2697 * The socket is already closed at this point so no close to sock.
2699 void ust_app_unregister(int sock
)
2701 struct ust_app
*lta
;
2702 struct lttng_ht_node_ulong
*node
;
2703 struct lttng_ht_iter iter
;
2704 struct ust_app_session
*ua_sess
;
2709 /* Get the node reference for a call_rcu */
2710 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
2711 node
= lttng_ht_iter_get_node_ulong(&iter
);
2714 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
2715 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
2717 /* Remove application from PID hash table */
2718 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
2722 * Remove application from notify hash table. The thread handling the
2723 * notify socket could have deleted the node so ignore on error because
2724 * either way it's valid. The close of that socket is handled by the other
2727 iter
.iter
.node
= <a
->notify_sock_n
.node
;
2728 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
2731 * Ignore return value since the node might have been removed before by an
2732 * add replace during app registration because the PID can be reassigned by
2735 iter
.iter
.node
= <a
->pid_n
.node
;
2736 ret
= lttng_ht_del(ust_app_ht
, &iter
);
2738 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
2742 /* Remove sessions so they are not visible during deletion.*/
2743 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
2745 struct ust_registry_session
*registry
;
2747 ret
= lttng_ht_del(lta
->sessions
, &iter
);
2749 /* The session was already removed so scheduled for teardown. */
2754 * Add session to list for teardown. This is safe since at this point we
2755 * are the only one using this list.
2757 pthread_mutex_lock(&ua_sess
->lock
);
2760 * Normally, this is done in the delete session process which is
2761 * executed in the call rcu below. However, upon registration we can't
2762 * afford to wait for the grace period before pushing data or else the
2763 * data pending feature can race between the unregistration and stop
2764 * command where the data pending command is sent *before* the grace
2767 * The close metadata below nullifies the metadata pointer in the
2768 * session so the delete session will NOT push/close a second time.
2770 registry
= get_session_registry(ua_sess
);
2772 /* Push metadata for application before freeing the application. */
2773 (void) push_metadata(registry
, ua_sess
->consumer
);
2776 * Don't ask to close metadata for global per UID buffers. Close
2777 * metadata only on destroy trace session in this case.
2779 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
2780 /* And ask to close it for this session registry. */
2781 (void) close_metadata(registry
, ua_sess
->consumer
);
2785 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
2786 pthread_mutex_unlock(&ua_sess
->lock
);
2790 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
2797 * Return traceable_app_count
2799 unsigned long ust_app_list_count(void)
2801 unsigned long count
;
2804 count
= lttng_ht_get_count(ust_app_ht
);
2811 * Fill events array with all events name of all registered apps.
2813 int ust_app_list_events(struct lttng_event
**events
)
2816 size_t nbmem
, count
= 0;
2817 struct lttng_ht_iter iter
;
2818 struct ust_app
*app
;
2819 struct lttng_event
*tmp_event
;
2821 nbmem
= UST_APP_EVENT_LIST_SIZE
;
2822 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
2823 if (tmp_event
== NULL
) {
2824 PERROR("zmalloc ust app events");
2831 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2832 struct lttng_ust_tracepoint_iter uiter
;
2834 health_code_update();
2836 if (!app
->compatible
) {
2838 * TODO: In time, we should notice the caller of this error by
2839 * telling him that this is a version error.
2843 handle
= ustctl_tracepoint_list(app
->sock
);
2845 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
2846 ERR("UST app list events getting handle failed for app pid %d",
2852 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
2853 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
2854 /* Handle ustctl error. */
2857 if (ret
!= -LTTNG_UST_ERR_EXITING
|| ret
!= -EPIPE
) {
2858 ERR("UST app tp list get failed for app %d with ret %d",
2861 DBG3("UST app tp list get failed. Application is dead");
2866 health_code_update();
2867 if (count
>= nbmem
) {
2868 /* In case the realloc fails, we free the memory */
2871 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
2874 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event
));
2876 PERROR("realloc ust app events");
2883 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
2884 tmp_event
[count
].loglevel
= uiter
.loglevel
;
2885 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
2886 tmp_event
[count
].pid
= app
->pid
;
2887 tmp_event
[count
].enabled
= -1;
2893 *events
= tmp_event
;
2895 DBG2("UST app list events done (%zu events)", count
);
2900 health_code_update();
2905 * Fill events array with all events name of all registered apps.
2907 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
2910 size_t nbmem
, count
= 0;
2911 struct lttng_ht_iter iter
;
2912 struct ust_app
*app
;
2913 struct lttng_event_field
*tmp_event
;
2915 nbmem
= UST_APP_EVENT_LIST_SIZE
;
2916 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
2917 if (tmp_event
== NULL
) {
2918 PERROR("zmalloc ust app event fields");
2925 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2926 struct lttng_ust_field_iter uiter
;
2928 health_code_update();
2930 if (!app
->compatible
) {
2932 * TODO: In time, we should notice the caller of this error by
2933 * telling him that this is a version error.
2937 handle
= ustctl_tracepoint_field_list(app
->sock
);
2939 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
2940 ERR("UST app list field getting handle failed for app pid %d",
2946 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
2947 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
2948 /* Handle ustctl error. */
2951 if (ret
!= -LTTNG_UST_ERR_EXITING
|| ret
!= -EPIPE
) {
2952 ERR("UST app tp list field failed for app %d with ret %d",
2955 DBG3("UST app tp list field failed. Application is dead");
2960 health_code_update();
2961 if (count
>= nbmem
) {
2962 /* In case the realloc fails, we free the memory */
2965 DBG2("Reallocating event field list from %zu to %zu entries", nbmem
,
2968 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event_field
));
2970 PERROR("realloc ust app event fields");
2978 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
2979 tmp_event
[count
].type
= uiter
.type
;
2980 tmp_event
[count
].nowrite
= uiter
.nowrite
;
2982 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2983 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
2984 tmp_event
[count
].event
.type
= LTTNG_UST_TRACEPOINT
;
2985 tmp_event
[count
].event
.pid
= app
->pid
;
2986 tmp_event
[count
].event
.enabled
= -1;
2992 *fields
= tmp_event
;
2994 DBG2("UST app list event fields done (%zu events)", count
);
2999 health_code_update();
3004 * Free and clean all traceable apps of the global list.
3006 void ust_app_clean_list(void)
3009 struct ust_app
*app
;
3010 struct lttng_ht_iter iter
;
3012 DBG2("UST app cleaning registered apps hash table");
3016 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3017 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3019 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3022 /* Cleanup socket hash table */
3023 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3025 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3029 /* Cleanup notify socket hash table */
3030 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3031 notify_sock_n
.node
) {
3032 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3036 /* Destroy is done only when the ht is empty */
3037 lttng_ht_destroy(ust_app_ht
);
3038 lttng_ht_destroy(ust_app_ht_by_sock
);
3039 lttng_ht_destroy(ust_app_ht_by_notify_sock
);
3045 * Init UST app hash table.
3047 void ust_app_ht_alloc(void)
3049 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3050 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3051 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3055 * For a specific UST session, disable the channel for all registered apps.
3057 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3058 struct ltt_ust_channel
*uchan
)
3061 struct lttng_ht_iter iter
;
3062 struct lttng_ht_node_str
*ua_chan_node
;
3063 struct ust_app
*app
;
3064 struct ust_app_session
*ua_sess
;
3065 struct ust_app_channel
*ua_chan
;
3067 if (usess
== NULL
|| uchan
== NULL
) {
3068 ERR("Disabling UST global channel with NULL values");
3073 DBG2("UST app disabling channel %s from global domain for session id %d",
3074 uchan
->name
, usess
->id
);
3078 /* For every registered applications */
3079 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3080 struct lttng_ht_iter uiter
;
3081 if (!app
->compatible
) {
3083 * TODO: In time, we should notice the caller of this error by
3084 * telling him that this is a version error.
3088 ua_sess
= lookup_session_by_app(usess
, app
);
3089 if (ua_sess
== NULL
) {
3094 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3095 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3096 /* If the session if found for the app, the channel must be there */
3097 assert(ua_chan_node
);
3099 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3100 /* The channel must not be already disabled */
3101 assert(ua_chan
->enabled
== 1);
3103 /* Disable channel onto application */
3104 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3106 /* XXX: We might want to report this error at some point... */
3118 * For a specific UST session, enable the channel for all registered apps.
3120 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3121 struct ltt_ust_channel
*uchan
)
3124 struct lttng_ht_iter iter
;
3125 struct ust_app
*app
;
3126 struct ust_app_session
*ua_sess
;
3128 if (usess
== NULL
|| uchan
== NULL
) {
3129 ERR("Adding UST global channel to NULL values");
3134 DBG2("UST app enabling channel %s to global domain for session id %d",
3135 uchan
->name
, usess
->id
);
3139 /* For every registered applications */
3140 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3141 if (!app
->compatible
) {
3143 * TODO: In time, we should notice the caller of this error by
3144 * telling him that this is a version error.
3148 ua_sess
= lookup_session_by_app(usess
, app
);
3149 if (ua_sess
== NULL
) {
3153 /* Enable channel onto application */
3154 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3156 /* XXX: We might want to report this error at some point... */
3168 * Disable an event in a channel and for a specific session.
3170 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3171 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3174 struct lttng_ht_iter iter
, uiter
;
3175 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3176 struct ust_app
*app
;
3177 struct ust_app_session
*ua_sess
;
3178 struct ust_app_channel
*ua_chan
;
3179 struct ust_app_event
*ua_event
;
3181 DBG("UST app disabling event %s for all apps in channel "
3182 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
3186 /* For all registered applications */
3187 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3188 if (!app
->compatible
) {
3190 * TODO: In time, we should notice the caller of this error by
3191 * telling him that this is a version error.
3195 ua_sess
= lookup_session_by_app(usess
, app
);
3196 if (ua_sess
== NULL
) {
3201 /* Lookup channel in the ust app session */
3202 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3203 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3204 if (ua_chan_node
== NULL
) {
3205 DBG2("Channel %s not found in session id %d for app pid %d."
3206 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
3209 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3211 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
3212 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
3213 if (ua_event_node
== NULL
) {
3214 DBG2("Event %s not found in channel %s for app pid %d."
3215 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
3218 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3220 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3222 /* XXX: Report error someday... */
3233 * For a specific UST session and UST channel, the event for all
3236 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
3237 struct ltt_ust_channel
*uchan
)
3240 struct lttng_ht_iter iter
, uiter
;
3241 struct lttng_ht_node_str
*ua_chan_node
;
3242 struct ust_app
*app
;
3243 struct ust_app_session
*ua_sess
;
3244 struct ust_app_channel
*ua_chan
;
3245 struct ust_app_event
*ua_event
;
3247 DBG("UST app disabling all event for all apps in channel "
3248 "%s for session id %d", uchan
->name
, usess
->id
);
3252 /* For all registered applications */
3253 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3254 if (!app
->compatible
) {
3256 * TODO: In time, we should notice the caller of this error by
3257 * telling him that this is a version error.
3261 ua_sess
= lookup_session_by_app(usess
, app
);
3263 /* The application has problem or is probably dead. */
3267 /* Lookup channel in the ust app session */
3268 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3269 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3270 /* If the channel is not found, there is a code flow error */
3271 assert(ua_chan_node
);
3273 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3275 /* Disable each events of channel */
3276 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
3278 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3280 /* XXX: Report error someday... */
3292 * For a specific UST session, create the channel for all registered apps.
3294 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
3295 struct ltt_ust_channel
*uchan
)
3297 int ret
= 0, created
;
3298 struct lttng_ht_iter iter
;
3299 struct ust_app
*app
;
3300 struct ust_app_session
*ua_sess
= NULL
;
3302 /* Very wrong code flow */
3306 DBG2("UST app adding channel %s to UST domain for session id %d",
3307 uchan
->name
, usess
->id
);
3311 /* For every registered applications */
3312 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3313 if (!app
->compatible
) {
3315 * TODO: In time, we should notice the caller of this error by
3316 * telling him that this is a version error.
3321 * Create session on the tracer side and add it to app session HT. Note
3322 * that if session exist, it will simply return a pointer to the ust
3325 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
3330 * The application's socket is not valid. Either a bad socket
3331 * or a timeout on it. We can't inform the caller that for a
3332 * specific app, the session failed so lets continue here.
3337 goto error_rcu_unlock
;
3342 pthread_mutex_lock(&ua_sess
->lock
);
3343 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
3344 sizeof(uchan
->name
))) {
3345 struct ustctl_consumer_channel_attr attr
;
3346 copy_channel_attr_to_ustctl(&attr
, &uchan
->attr
);
3347 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
,
3350 /* Create channel onto application. We don't need the chan ref. */
3351 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
3352 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
3354 pthread_mutex_unlock(&ua_sess
->lock
);
3356 if (ret
== -ENOMEM
) {
3357 /* No more memory is a fatal error. Stop right now. */
3358 goto error_rcu_unlock
;
3360 /* Cleanup the created session if it's the case. */
3362 destroy_app_session(app
, ua_sess
);
3373 * Enable event for a specific session and channel on the tracer.
3375 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
3376 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3379 struct lttng_ht_iter iter
, uiter
;
3380 struct lttng_ht_node_str
*ua_chan_node
;
3381 struct ust_app
*app
;
3382 struct ust_app_session
*ua_sess
;
3383 struct ust_app_channel
*ua_chan
;
3384 struct ust_app_event
*ua_event
;
3386 DBG("UST app enabling event %s for all apps for session id %d",
3387 uevent
->attr
.name
, usess
->id
);
3390 * NOTE: At this point, this function is called only if the session and
3391 * channel passed are already created for all apps. and enabled on the
3397 /* For all registered applications */
3398 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3399 if (!app
->compatible
) {
3401 * TODO: In time, we should notice the caller of this error by
3402 * telling him that this is a version error.
3406 ua_sess
= lookup_session_by_app(usess
, app
);
3408 /* The application has problem or is probably dead. */
3412 pthread_mutex_lock(&ua_sess
->lock
);
3414 /* Lookup channel in the ust app session */
3415 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3416 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3417 /* If the channel is not found, there is a code flow error */
3418 assert(ua_chan_node
);
3420 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3422 /* Get event node */
3423 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3424 uevent
->filter
, uevent
->attr
.loglevel
);
3425 if (ua_event
== NULL
) {
3426 DBG3("UST app enable event %s not found for app PID %d."
3427 "Skipping app", uevent
->attr
.name
, app
->pid
);
3431 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
3433 pthread_mutex_unlock(&ua_sess
->lock
);
3437 pthread_mutex_unlock(&ua_sess
->lock
);
3446 * For a specific existing UST session and UST channel, creates the event for
3447 * all registered apps.
3449 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
3450 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3453 struct lttng_ht_iter iter
, uiter
;
3454 struct lttng_ht_node_str
*ua_chan_node
;
3455 struct ust_app
*app
;
3456 struct ust_app_session
*ua_sess
;
3457 struct ust_app_channel
*ua_chan
;
3459 DBG("UST app creating event %s for all apps for session id %d",
3460 uevent
->attr
.name
, usess
->id
);
3464 /* For all registered applications */
3465 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3466 if (!app
->compatible
) {
3468 * TODO: In time, we should notice the caller of this error by
3469 * telling him that this is a version error.
3473 ua_sess
= lookup_session_by_app(usess
, app
);
3475 /* The application has problem or is probably dead. */
3479 pthread_mutex_lock(&ua_sess
->lock
);
3480 /* Lookup channel in the ust app session */
3481 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3482 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3483 /* If the channel is not found, there is a code flow error */
3484 assert(ua_chan_node
);
3486 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3488 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
3489 pthread_mutex_unlock(&ua_sess
->lock
);
3491 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
3492 /* Possible value at this point: -ENOMEM. If so, we stop! */
3495 DBG2("UST app event %s already exist on app PID %d",
3496 uevent
->attr
.name
, app
->pid
);
3507 * Start tracing for a specific UST session and app.
3509 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
3512 struct ust_app_session
*ua_sess
;
3514 DBG("Starting tracing for ust app pid %d", app
->pid
);
3518 if (!app
->compatible
) {
3522 ua_sess
= lookup_session_by_app(usess
, app
);
3523 if (ua_sess
== NULL
) {
3524 /* The session is in teardown process. Ignore and continue. */
3528 pthread_mutex_lock(&ua_sess
->lock
);
3530 /* Upon restart, we skip the setup, already done */
3531 if (ua_sess
->started
) {
3535 /* Create directories if consumer is LOCAL and has a path defined. */
3536 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
3537 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
3538 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
3539 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
3541 if (ret
!= -EEXIST
) {
3542 ERR("Trace directory creation error");
3549 * Create the metadata for the application. This returns gracefully if a
3550 * metadata was already set for the session.
3552 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
, NULL
);
3557 health_code_update();
3560 /* This start the UST tracing */
3561 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
3563 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3564 ERR("Error starting tracing for app pid: %d (ret: %d)",
3567 DBG("UST app start session failed. Application is dead.");
3572 /* Indicate that the session has been started once */
3573 ua_sess
->started
= 1;
3575 pthread_mutex_unlock(&ua_sess
->lock
);
3577 health_code_update();
3579 /* Quiescent wait after starting trace */
3580 ret
= ustctl_wait_quiescent(app
->sock
);
3581 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3582 ERR("UST app wait quiescent failed for app pid %d ret %d",
3588 health_code_update();
3592 pthread_mutex_unlock(&ua_sess
->lock
);
3594 health_code_update();
3599 * Stop tracing for a specific UST session and app.
3601 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
3604 struct lttng_ht_iter iter
;
3605 struct ust_app_session
*ua_sess
;
3606 struct ust_app_channel
*ua_chan
;
3607 struct ust_registry_session
*registry
;
3609 DBG("Stopping tracing for ust app pid %d", app
->pid
);
3613 if (!app
->compatible
) {
3614 goto end_no_session
;
3617 ua_sess
= lookup_session_by_app(usess
, app
);
3618 if (ua_sess
== NULL
) {
3619 goto end_no_session
;
3622 pthread_mutex_lock(&ua_sess
->lock
);
3625 * If started = 0, it means that stop trace has been called for a session
3626 * that was never started. It's possible since we can have a fail start
3627 * from either the application manager thread or the command thread. Simply
3628 * indicate that this is a stop error.
3630 if (!ua_sess
->started
) {
3631 goto error_rcu_unlock
;
3634 health_code_update();
3636 /* This inhibits UST tracing */
3637 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
3639 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3640 ERR("Error stopping tracing for app pid: %d (ret: %d)",
3643 DBG("UST app stop session failed. Application is dead.");
3645 goto error_rcu_unlock
;
3648 health_code_update();
3650 /* Quiescent wait after stopping trace */
3651 ret
= ustctl_wait_quiescent(app
->sock
);
3652 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3653 ERR("UST app wait quiescent failed for app pid %d ret %d",
3657 health_code_update();
3659 /* Flushing buffers */
3660 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
3662 health_code_update();
3663 assert(ua_chan
->is_sent
);
3664 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_chan
->obj
);
3666 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3667 ERR("UST app PID %d channel %s flush failed with ret %d",
3668 app
->pid
, ua_chan
->name
, ret
);
3670 DBG3("UST app failed to flush %s. Application is dead.",
3672 /* No need to continue. */
3675 /* Continuing flushing all buffers */
3680 health_code_update();
3682 registry
= get_session_registry(ua_sess
);
3684 /* Push metadata for application before freeing the application. */
3685 (void) push_metadata(registry
, ua_sess
->consumer
);
3687 pthread_mutex_unlock(&ua_sess
->lock
);
3690 health_code_update();
3694 pthread_mutex_unlock(&ua_sess
->lock
);
3696 health_code_update();
3701 * Destroy a specific UST session in apps.
3703 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
3706 struct ust_app_session
*ua_sess
;
3707 struct lttng_ht_iter iter
;
3708 struct lttng_ht_node_ulong
*node
;
3710 DBG("Destroy tracing for ust app pid %d", app
->pid
);
3714 if (!app
->compatible
) {
3718 __lookup_session_by_app(usess
, app
, &iter
);
3719 node
= lttng_ht_iter_get_node_ulong(&iter
);
3721 /* Session is being or is deleted. */
3724 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
3726 health_code_update();
3727 destroy_app_session(app
, ua_sess
);
3729 health_code_update();
3731 /* Quiescent wait after stopping trace */
3732 ret
= ustctl_wait_quiescent(app
->sock
);
3733 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3734 ERR("UST app wait quiescent failed for app pid %d ret %d",
3739 health_code_update();
3744 * Start tracing for the UST session.
3746 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
3749 struct lttng_ht_iter iter
;
3750 struct ust_app
*app
;
3752 DBG("Starting all UST traces");
3756 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3757 ret
= ust_app_start_trace(usess
, app
);
3759 /* Continue to next apps even on error */
3770 * Start tracing for the UST session.
3772 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
3775 struct lttng_ht_iter iter
;
3776 struct ust_app
*app
;
3778 DBG("Stopping all UST traces");
3782 /* Flush all per UID buffers associated to that session. */
3783 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
3784 struct buffer_reg_uid
*reg
;
3785 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3786 struct buffer_reg_channel
*reg_chan
;
3787 struct consumer_socket
*socket
;
3789 /* Get consumer socket to use to push the metadata.*/
3790 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
3793 /* Ignore request if no consumer is found for the session. */
3797 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
3798 reg_chan
, node
.node
) {
3800 * The following call will print error values so the return
3801 * code is of little importance because whatever happens, we
3802 * have to try them all.
3804 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
3809 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3810 ret
= ust_app_stop_trace(usess
, app
);
3812 /* Continue to next apps even on error */
3823 * Destroy app UST session.
3825 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
3828 struct lttng_ht_iter iter
;
3829 struct ust_app
*app
;
3831 DBG("Destroy all UST traces");
3835 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3836 ret
= destroy_trace(usess
, app
);
3838 /* Continue to next apps even on error */
3849 * Add channels/events from UST global domain to registered apps at sock.
3851 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
3854 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
3855 struct ust_app
*app
;
3856 struct ust_app_session
*ua_sess
= NULL
;
3857 struct ust_app_channel
*ua_chan
;
3858 struct ust_app_event
*ua_event
;
3859 struct ust_app_ctx
*ua_ctx
;
3864 DBG2("UST app global update for app sock %d for session id %d", sock
,
3869 app
= find_app_by_sock(sock
);
3872 * Application can be unregistered before so this is possible hence
3873 * simply stopping the update.
3875 DBG3("UST app update failed to find app sock %d", sock
);
3879 if (!app
->compatible
) {
3883 ret
= create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
3885 /* Tracer is probably gone or ENOMEM. */
3890 pthread_mutex_lock(&ua_sess
->lock
);
3893 * We can iterate safely here over all UST app session since the create ust
3894 * app session above made a shadow copy of the UST global domain from the
3897 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
3900 * For a metadata channel, handle it differently.
3902 if (!strncmp(ua_chan
->name
, DEFAULT_METADATA_NAME
,
3903 sizeof(ua_chan
->name
))) {
3904 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
,
3906 /* Remove it from the hash table and continue!. */
3907 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
3909 delete_ust_app_channel(-1, ua_chan
, app
);
3912 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
3916 * Stop everything. On error, the application failed, no more
3917 * file descriptor are available or ENOMEM so stopping here is
3918 * the only thing we can do for now.
3923 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
3925 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
3932 /* For each events */
3933 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
3935 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3942 pthread_mutex_unlock(&ua_sess
->lock
);
3944 if (usess
->start_trace
) {
3945 ret
= ust_app_start_trace(usess
, app
);
3950 DBG2("UST trace started for app pid %d", app
->pid
);
3953 /* Everything went well at this point. */
3958 pthread_mutex_unlock(&ua_sess
->lock
);
3961 destroy_app_session(app
, ua_sess
);
3968 * Add context to a specific channel for global UST domain.
3970 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
3971 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
3974 struct lttng_ht_node_str
*ua_chan_node
;
3975 struct lttng_ht_iter iter
, uiter
;
3976 struct ust_app_channel
*ua_chan
= NULL
;
3977 struct ust_app_session
*ua_sess
;
3978 struct ust_app
*app
;
3982 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3983 if (!app
->compatible
) {
3985 * TODO: In time, we should notice the caller of this error by
3986 * telling him that this is a version error.
3990 ua_sess
= lookup_session_by_app(usess
, app
);
3991 if (ua_sess
== NULL
) {
3995 pthread_mutex_lock(&ua_sess
->lock
);
3996 /* Lookup channel in the ust app session */
3997 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3998 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3999 if (ua_chan_node
== NULL
) {
4002 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4004 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4009 pthread_mutex_unlock(&ua_sess
->lock
);
4017 * Enable event for a channel from a UST session for a specific PID.
4019 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4020 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4023 struct lttng_ht_iter iter
;
4024 struct lttng_ht_node_str
*ua_chan_node
;
4025 struct ust_app
*app
;
4026 struct ust_app_session
*ua_sess
;
4027 struct ust_app_channel
*ua_chan
;
4028 struct ust_app_event
*ua_event
;
4030 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4034 app
= ust_app_find_by_pid(pid
);
4036 ERR("UST app enable event per PID %d not found", pid
);
4041 if (!app
->compatible
) {
4046 ua_sess
= lookup_session_by_app(usess
, app
);
4048 /* The application has problem or is probably dead. */
4053 pthread_mutex_lock(&ua_sess
->lock
);
4054 /* Lookup channel in the ust app session */
4055 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4056 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4057 /* If the channel is not found, there is a code flow error */
4058 assert(ua_chan_node
);
4060 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4062 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4063 uevent
->filter
, uevent
->attr
.loglevel
);
4064 if (ua_event
== NULL
) {
4065 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4070 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4077 pthread_mutex_unlock(&ua_sess
->lock
);
4084 * Disable event for a channel from a UST session for a specific PID.
4086 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
4087 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4090 struct lttng_ht_iter iter
;
4091 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
4092 struct ust_app
*app
;
4093 struct ust_app_session
*ua_sess
;
4094 struct ust_app_channel
*ua_chan
;
4095 struct ust_app_event
*ua_event
;
4097 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
4101 app
= ust_app_find_by_pid(pid
);
4103 ERR("UST app disable event per PID %d not found", pid
);
4108 if (!app
->compatible
) {
4113 ua_sess
= lookup_session_by_app(usess
, app
);
4115 /* The application has problem or is probably dead. */
4119 /* Lookup channel in the ust app session */
4120 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4121 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4122 if (ua_chan_node
== NULL
) {
4123 /* Channel does not exist, skip disabling */
4126 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4128 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
4129 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
4130 if (ua_event_node
== NULL
) {
4131 /* Event does not exist, skip disabling */
4134 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
4136 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
4147 * Calibrate registered applications.
4149 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
4152 struct lttng_ht_iter iter
;
4153 struct ust_app
*app
;
4157 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4158 if (!app
->compatible
) {
4160 * TODO: In time, we should notice the caller of this error by
4161 * telling him that this is a version error.
4166 health_code_update();
4168 ret
= ustctl_calibrate(app
->sock
, calibrate
);
4172 /* Means that it's not implemented on the tracer side. */
4176 DBG2("Calibrate app PID %d returned with error %d",
4183 DBG("UST app global domain calibration finished");
4187 health_code_update();
4193 * Receive registration and populate the given msg structure.
4195 * On success return 0 else a negative value returned by the ustctl call.
4197 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
4200 uint32_t pid
, ppid
, uid
, gid
;
4204 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
4205 &pid
, &ppid
, &uid
, &gid
,
4206 &msg
->bits_per_long
,
4207 &msg
->uint8_t_alignment
,
4208 &msg
->uint16_t_alignment
,
4209 &msg
->uint32_t_alignment
,
4210 &msg
->uint64_t_alignment
,
4211 &msg
->long_alignment
,
4218 case LTTNG_UST_ERR_EXITING
:
4219 DBG3("UST app recv reg message failed. Application died");
4221 case LTTNG_UST_ERR_UNSUP_MAJOR
:
4222 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
4223 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
4224 LTTNG_UST_ABI_MINOR_VERSION
);
4227 ERR("UST app recv reg message failed with ret %d", ret
);
4232 msg
->pid
= (pid_t
) pid
;
4233 msg
->ppid
= (pid_t
) ppid
;
4234 msg
->uid
= (uid_t
) uid
;
4235 msg
->gid
= (gid_t
) gid
;
4242 * Return a ust app channel object using the application object and the channel
4243 * object descriptor has a key. If not found, NULL is returned. A RCU read side
4244 * lock MUST be acquired before calling this function.
4246 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
4249 struct lttng_ht_node_ulong
*node
;
4250 struct lttng_ht_iter iter
;
4251 struct ust_app_channel
*ua_chan
= NULL
;
4255 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
4256 node
= lttng_ht_iter_get_node_ulong(&iter
);
4258 DBG2("UST app channel find by objd %d not found", objd
);
4262 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
4269 * Reply to a register channel notification from an application on the notify
4270 * socket. The channel metadata is also created.
4272 * The session UST registry lock is acquired in this function.
4274 * On success 0 is returned else a negative value.
4276 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
4277 size_t nr_fields
, struct ustctl_field
*fields
)
4279 int ret
, ret_code
= 0;
4280 uint32_t chan_id
, reg_count
;
4281 uint64_t chan_reg_key
;
4282 enum ustctl_channel_header type
;
4283 struct ust_app
*app
;
4284 struct ust_app_channel
*ua_chan
;
4285 struct ust_app_session
*ua_sess
;
4286 struct ust_registry_session
*registry
;
4287 struct ust_registry_channel
*chan_reg
;
4291 /* Lookup application. If not found, there is a code flow error. */
4292 app
= find_app_by_notify_sock(sock
);
4294 DBG("Application socket %d is being teardown. Abort event notify",
4297 goto error_rcu_unlock
;
4300 /* Lookup channel by UST object descriptor. Should always be found. */
4301 ua_chan
= find_channel_by_objd(app
, cobjd
);
4303 assert(ua_chan
->session
);
4304 ua_sess
= ua_chan
->session
;
4306 /* Get right session registry depending on the session buffer type. */
4307 registry
= get_session_registry(ua_sess
);
4310 /* Depending on the buffer type, a different channel key is used. */
4311 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4312 chan_reg_key
= ua_chan
->tracing_channel_id
;
4314 chan_reg_key
= ua_chan
->key
;
4317 pthread_mutex_lock(®istry
->lock
);
4319 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
4322 if (!chan_reg
->register_done
) {
4323 reg_count
= ust_registry_get_event_count(chan_reg
);
4324 if (reg_count
< 31) {
4325 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
4327 type
= USTCTL_CHANNEL_HEADER_LARGE
;
4330 chan_reg
->nr_ctx_fields
= nr_fields
;
4331 chan_reg
->ctx_fields
= fields
;
4332 chan_reg
->header_type
= type
;
4334 /* Get current already assigned values. */
4335 type
= chan_reg
->header_type
;
4337 /* Channel id is set during the object creation. */
4338 chan_id
= chan_reg
->chan_id
;
4340 /* Append to metadata */
4341 if (!chan_reg
->metadata_dumped
) {
4342 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
4344 ERR("Error appending channel metadata (errno = %d)", ret_code
);
4350 DBG3("UST app replying to register channel key %" PRIu64
4351 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
4354 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
4356 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4357 ERR("UST app reply channel failed with ret %d", ret
);
4359 DBG3("UST app reply channel failed. Application died");
4364 /* This channel registry registration is completed. */
4365 chan_reg
->register_done
= 1;
4368 pthread_mutex_unlock(®istry
->lock
);
4375 * Add event to the UST channel registry. When the event is added to the
4376 * registry, the metadata is also created. Once done, this replies to the
4377 * application with the appropriate error code.
4379 * The session UST registry lock is acquired in the function.
4381 * On success 0 is returned else a negative value.
4383 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
4384 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
4385 char *model_emf_uri
)
4388 uint32_t event_id
= 0;
4389 uint64_t chan_reg_key
;
4390 struct ust_app
*app
;
4391 struct ust_app_channel
*ua_chan
;
4392 struct ust_app_session
*ua_sess
;
4393 struct ust_registry_session
*registry
;
4397 /* Lookup application. If not found, there is a code flow error. */
4398 app
= find_app_by_notify_sock(sock
);
4400 DBG("Application socket %d is being teardown. Abort event notify",
4403 goto error_rcu_unlock
;
4406 /* Lookup channel by UST object descriptor. Should always be found. */
4407 ua_chan
= find_channel_by_objd(app
, cobjd
);
4409 assert(ua_chan
->session
);
4410 ua_sess
= ua_chan
->session
;
4412 registry
= get_session_registry(ua_sess
);
4415 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4416 chan_reg_key
= ua_chan
->tracing_channel_id
;
4418 chan_reg_key
= ua_chan
->key
;
4421 pthread_mutex_lock(®istry
->lock
);
4423 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
4424 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
, loglevel
,
4425 model_emf_uri
, ua_sess
->buffer_type
, &event_id
);
4428 * The return value is returned to ustctl so in case of an error, the
4429 * application can be notified. In case of an error, it's important not to
4430 * return a negative error or else the application will get closed.
4432 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
4434 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4435 ERR("UST app reply event failed with ret %d", ret
);
4437 DBG3("UST app reply event failed. Application died");
4440 * No need to wipe the create event since the application socket will
4441 * get close on error hence cleaning up everything by itself.
4446 DBG3("UST registry event %s with id %" PRId32
" added successfully",
4450 pthread_mutex_unlock(®istry
->lock
);
4457 * Handle application notification through the given notify socket.
4459 * Return 0 on success or else a negative value.
4461 int ust_app_recv_notify(int sock
)
4464 enum ustctl_notify_cmd cmd
;
4466 DBG3("UST app receiving notify from sock %d", sock
);
4468 ret
= ustctl_recv_notify(sock
, &cmd
);
4470 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4471 ERR("UST app recv notify failed with ret %d", ret
);
4473 DBG3("UST app recv notify failed. Application died");
4479 case USTCTL_NOTIFY_CMD_EVENT
:
4481 int sobjd
, cobjd
, loglevel
;
4482 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
4484 struct ustctl_field
*fields
;
4486 DBG2("UST app ustctl register event received");
4488 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
4489 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
4491 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4492 ERR("UST app recv event failed with ret %d", ret
);
4494 DBG3("UST app recv event failed. Application died");
4499 /* Add event to the UST registry coming from the notify socket. */
4500 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
4501 fields
, loglevel
, model_emf_uri
);
4508 case USTCTL_NOTIFY_CMD_CHANNEL
:
4512 struct ustctl_field
*fields
;
4514 DBG2("UST app ustctl register channel received");
4516 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
4519 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4520 ERR("UST app recv channel failed with ret %d", ret
);
4522 DBG3("UST app recv channel failed. Application died");
4527 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
4536 /* Should NEVER happen. */
4545 * Once the notify socket hangs up, this is called. First, it tries to find the
4546 * corresponding application. On failure, the call_rcu to close the socket is
4547 * executed. If an application is found, it tries to delete it from the notify
4548 * socket hash table. Whathever the result, it proceeds to the call_rcu.
4550 * Note that an object needs to be allocated here so on ENOMEM failure, the
4551 * call RCU is not done but the rest of the cleanup is.
4553 void ust_app_notify_sock_unregister(int sock
)
4556 struct lttng_ht_iter iter
;
4557 struct ust_app
*app
;
4558 struct ust_app_notify_sock_obj
*obj
;
4564 obj
= zmalloc(sizeof(*obj
));
4567 * An ENOMEM is kind of uncool. If this strikes we continue the
4568 * procedure but the call_rcu will not be called. In this case, we
4569 * accept the fd leak rather than possibly creating an unsynchronized
4570 * state between threads.
4572 * TODO: The notify object should be created once the notify socket is
4573 * registered and stored independantely from the ust app object. The
4574 * tricky part is to synchronize the teardown of the application and
4575 * this notify object. Let's keep that in mind so we can avoid this
4576 * kind of shenanigans with ENOMEM in the teardown path.
4583 DBG("UST app notify socket unregister %d", sock
);
4586 * Lookup application by notify socket. If this fails, this means that the
4587 * hash table delete has already been done by the application
4588 * unregistration process so we can safely close the notify socket in a
4591 app
= find_app_by_notify_sock(sock
);
4596 iter
.iter
.node
= &app
->notify_sock_n
.node
;
4599 * Whatever happens here either we fail or succeed, in both cases we have
4600 * to close the socket after a grace period to continue to the call RCU
4601 * here. If the deletion is successful, the application is not visible
4602 * anymore by other threads and is it fails it means that it was already
4603 * deleted from the hash table so either way we just have to close the
4606 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
4612 * Close socket after a grace period to avoid for the socket to be reused
4613 * before the application object is freed creating potential race between
4614 * threads trying to add unique in the global hash table.
4617 call_rcu(&obj
->head
, close_notify_sock_rcu
);