2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
27 #include <urcu/compiler.h>
28 #include <lttng/ust-error.h>
30 #include <common/common.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
36 #include "ust-consumer.h"
39 /* Next available channel key. */
40 static unsigned long next_channel_key
;
43 * Return the atomically incremented value of next_channel_key.
45 static inline unsigned long get_next_channel_key(void)
47 return uatomic_add_return(&next_channel_key
, 1);
51 * Return the consumer socket from the given consumer output with the right
52 * bitness. On error, returns NULL.
54 * The caller MUST acquire a rcu read side lock and keep it until the socket
55 * object reference is not needed anymore.
57 static struct consumer_socket
*find_consumer_socket_by_bitness(int bits
,
58 struct consumer_output
*consumer
)
61 struct consumer_socket
*socket
= NULL
;
65 consumer_fd
= uatomic_read(&ust_consumerd64_fd
);
68 consumer_fd
= uatomic_read(&ust_consumerd32_fd
);
75 socket
= consumer_find_socket(consumer_fd
, consumer
);
77 ERR("Consumer socket fd %d not found in consumer obj %p",
78 consumer_fd
, consumer
);
86 * Match function for the hash table lookup.
88 * It matches an ust app event based on three attributes which are the event
89 * name, the filter bytecode and the loglevel.
91 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
93 struct ust_app_event
*event
;
94 const struct ust_app_ht_key
*key
;
99 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
102 /* Match the 3 elements of the key: name, filter and loglevel. */
105 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
109 /* Event loglevel. */
110 if (event
->attr
.loglevel
!= key
->loglevel
) {
111 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
112 && key
->loglevel
== 0 && event
->attr
.loglevel
== -1) {
114 * Match is accepted. This is because on event creation, the
115 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
116 * -1 are accepted for this loglevel type since 0 is the one set by
117 * the API when receiving an enable event.
124 /* One of the filters is NULL, fail. */
125 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
129 if (key
->filter
&& event
->filter
) {
130 /* Both filters exists, check length followed by the bytecode. */
131 if (event
->filter
->len
!= key
->filter
->len
||
132 memcmp(event
->filter
->data
, key
->filter
->data
,
133 event
->filter
->len
) != 0) {
146 * Unique add of an ust app event in the given ht. This uses the custom
147 * ht_match_ust_app_event match function and the event name as hash.
149 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
150 struct ust_app_event
*event
)
152 struct cds_lfht_node
*node_ptr
;
153 struct ust_app_ht_key key
;
157 assert(ua_chan
->events
);
160 ht
= ua_chan
->events
;
161 key
.name
= event
->attr
.name
;
162 key
.filter
= event
->filter
;
163 key
.loglevel
= event
->attr
.loglevel
;
165 node_ptr
= cds_lfht_add_unique(ht
->ht
,
166 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
167 ht_match_ust_app_event
, &key
, &event
->node
.node
);
168 assert(node_ptr
== &event
->node
.node
);
172 * Close the notify socket from the given RCU head object. This MUST be called
173 * through a call_rcu().
175 static void close_notify_sock_rcu(struct rcu_head
*head
)
178 struct ust_app_notify_sock_obj
*obj
=
179 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
181 /* Must have a valid fd here. */
182 assert(obj
->fd
>= 0);
184 ret
= close(obj
->fd
);
186 ERR("close notify sock %d RCU", obj
->fd
);
188 lttng_fd_put(LTTNG_FD_APPS
, 1);
194 * Delete ust context safely. RCU read lock must be held before calling
198 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
205 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
206 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
207 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
208 sock
, ua_ctx
->obj
->handle
, ret
);
216 * Delete ust app event safely. RCU read lock must be held before calling
220 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
226 free(ua_event
->filter
);
228 if (ua_event
->obj
!= NULL
) {
229 ret
= ustctl_release_object(sock
, ua_event
->obj
);
230 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
231 ERR("UST app sock %d release event obj failed with ret %d",
240 * Delete ust app stream safely. RCU read lock must be held before calling
244 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
251 ret
= ustctl_release_object(sock
, stream
->obj
);
252 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
253 ERR("UST app sock %d release stream obj failed with ret %d",
256 lttng_fd_put(LTTNG_FD_APPS
, 2);
263 * Delete ust app channel safely. RCU read lock must be held before calling
267 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
271 struct lttng_ht_iter iter
;
272 struct ust_app_event
*ua_event
;
273 struct ust_app_ctx
*ua_ctx
;
274 struct ust_app_stream
*stream
, *stmp
;
278 DBG3("UST app deleting channel %s", ua_chan
->name
);
281 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
282 cds_list_del(&stream
->list
);
283 delete_ust_app_stream(sock
, stream
);
287 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
288 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
290 delete_ust_app_ctx(sock
, ua_ctx
);
292 lttng_ht_destroy(ua_chan
->ctx
);
295 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
297 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
299 delete_ust_app_event(sock
, ua_event
);
301 lttng_ht_destroy(ua_chan
->events
);
303 /* Wipe and free registry. */
304 ust_registry_channel_destroy(&ua_chan
->session
->registry
, &ua_chan
->registry
);
306 if (ua_chan
->obj
!= NULL
) {
307 /* Remove channel from application UST object descriptor. */
308 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
309 lttng_ht_del(app
->ust_objd
, &iter
);
310 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
311 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
312 ERR("UST app sock %d release channel obj failed with ret %d",
315 lttng_fd_put(LTTNG_FD_APPS
, 2);
322 * For a given application and session, push metadata to consumer. The session
323 * lock MUST be acquired here before calling this.
325 * Return 0 on success else a negative error.
327 static int push_metadata(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
330 char *metadata_str
= NULL
;
332 struct consumer_socket
*socket
;
337 if (!ua_sess
->consumer
|| !ua_sess
->metadata
) {
338 /* No consumer means no stream associated so just return gracefully. */
345 /* Get consumer socket to use to push the metadata.*/
346 socket
= find_consumer_socket_by_bitness(app
->bits_per_long
,
350 goto error_rcu_unlock
;
354 * TODO: Currently, we hold the socket lock around sampling of the next
355 * metadata segment to ensure we send metadata over the consumer socket in
356 * the correct order. This makes the registry lock nest inside the socket
359 * Please note that this is a temporary measure: we should move this lock
360 * back into ust_consumer_push_metadata() when the consumer gets the
361 * ability to reorder the metadata it receives.
363 pthread_mutex_lock(socket
->lock
);
364 pthread_mutex_lock(&ua_sess
->registry
.lock
);
366 offset
= ua_sess
->registry
.metadata_len_sent
;
367 len
= ua_sess
->registry
.metadata_len
- ua_sess
->registry
.metadata_len_sent
;
369 DBG3("No metadata to push for session id %d", ua_sess
->id
);
371 goto error_reg_unlock
;
375 /* Allocate only what we have to send. */
376 metadata_str
= zmalloc(len
);
378 PERROR("zmalloc ust app metadata string");
380 goto error_reg_unlock
;
382 /* Copy what we haven't send out. */
383 memcpy(metadata_str
, ua_sess
->registry
.metadata
+ offset
, len
);
385 pthread_mutex_unlock(&ua_sess
->registry
.lock
);
387 ret
= ust_consumer_push_metadata(socket
, ua_sess
, metadata_str
, len
,
390 pthread_mutex_unlock(socket
->lock
);
391 goto error_rcu_unlock
;
394 /* Update len sent of the registry. */
395 pthread_mutex_lock(&ua_sess
->registry
.lock
);
396 ua_sess
->registry
.metadata_len_sent
+= len
;
397 pthread_mutex_unlock(&ua_sess
->registry
.lock
);
398 pthread_mutex_unlock(socket
->lock
);
405 pthread_mutex_unlock(&ua_sess
->registry
.lock
);
406 pthread_mutex_unlock(socket
->lock
);
415 * Send to the consumer a close metadata command for the given session. Once
416 * done, the metadata channel is deleted and the session metadata pointer is
417 * nullified. The session lock MUST be acquired here unless the application is
418 * in the destroy path.
420 * Return 0 on success else a negative value.
422 static int close_metadata(struct ust_app
*app
, struct ust_app_session
*ua_sess
)
425 struct consumer_socket
*socket
;
430 /* Ignore if no metadata. Valid since it can be called on unregister. */
431 if (!ua_sess
->metadata
) {
438 /* Get consumer socket to use to push the metadata.*/
439 socket
= find_consumer_socket_by_bitness(app
->bits_per_long
,
443 goto error_rcu_unlock
;
446 ret
= ust_consumer_close_metadata(socket
, ua_sess
->metadata
);
448 goto error_rcu_unlock
;
452 /* Destroy metadata on our side since we must not use it anymore. */
453 delete_ust_app_channel(-1, ua_sess
->metadata
, app
);
454 ua_sess
->metadata
= NULL
;
462 * Delete ust app session safely. RCU read lock must be held before calling
466 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
470 struct lttng_ht_iter iter
;
471 struct ust_app_channel
*ua_chan
;
475 if (ua_sess
->metadata
) {
476 /* Push metadata for application before freeing the application. */
477 (void) push_metadata(app
, ua_sess
);
479 /* And ask to close it for this session. */
480 (void) close_metadata(app
, ua_sess
);
483 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
485 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
487 delete_ust_app_channel(sock
, ua_chan
, app
);
489 lttng_ht_destroy(ua_sess
->channels
);
491 ust_registry_session_destroy(&ua_sess
->registry
);
493 if (ua_sess
->handle
!= -1) {
494 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
495 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
496 ERR("UST app sock %d release session handle failed with ret %d",
504 * Delete a traceable application structure from the global list. Never call
505 * this function outside of a call_rcu call.
508 void delete_ust_app(struct ust_app
*app
)
511 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
515 /* Delete ust app sessions info */
519 lttng_ht_destroy(app
->sessions
);
522 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
524 /* Free every object in the session and the session. */
525 delete_ust_app_session(sock
, ua_sess
, app
);
529 * Wait until we have deleted the application from the sock hash table
530 * before closing this socket, otherwise an application could re-use the
531 * socket ID and race with the teardown, using the same hash table entry.
533 * It's OK to leave the close in call_rcu. We want it to stay unique for
534 * all RCU readers that could run concurrently with unregister app,
535 * therefore we _need_ to only close that socket after a grace period. So
536 * it should stay in this RCU callback.
538 * This close() is a very important step of the synchronization model so
539 * every modification to this function must be carefully reviewed.
545 lttng_fd_put(LTTNG_FD_APPS
, 1);
547 DBG2("UST app pid %d deleted", app
->pid
);
554 * URCU intermediate call to delete an UST app.
557 void delete_ust_app_rcu(struct rcu_head
*head
)
559 struct lttng_ht_node_ulong
*node
=
560 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
561 struct ust_app
*app
=
562 caa_container_of(node
, struct ust_app
, pid_n
);
564 DBG3("Call RCU deleting app PID %d", app
->pid
);
569 * Delete the session from the application ht and delete the data structure by
570 * freeing every object inside and releasing them.
572 static void destroy_app_session(struct ust_app
*app
,
573 struct ust_app_session
*ua_sess
)
576 struct lttng_ht_iter iter
;
581 iter
.iter
.node
= &ua_sess
->node
.node
;
582 ret
= lttng_ht_del(app
->sessions
, &iter
);
584 /* Already scheduled for teardown. */
588 /* Once deleted, free the data structure. */
589 delete_ust_app_session(app
->sock
, ua_sess
, app
);
596 * Alloc new UST app session.
599 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
601 struct ust_app_session
*ua_sess
;
603 /* Init most of the default value by allocating and zeroing */
604 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
605 if (ua_sess
== NULL
) {
610 ua_sess
->handle
= -1;
611 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
612 pthread_mutex_init(&ua_sess
->lock
, NULL
);
613 if (ust_registry_session_init(&ua_sess
->registry
, app
,
615 app
->uint8_t_alignment
,
616 app
->uint16_t_alignment
,
617 app
->uint32_t_alignment
,
618 app
->uint64_t_alignment
,
633 * Alloc new UST app channel.
636 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
637 struct ust_app_session
*ua_sess
,
638 struct lttng_ust_channel_attr
*attr
)
640 struct ust_app_channel
*ua_chan
;
642 /* Init most of the default value by allocating and zeroing */
643 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
644 if (ua_chan
== NULL
) {
649 /* Setup channel name */
650 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
651 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
653 ua_chan
->enabled
= 1;
654 ua_chan
->handle
= -1;
655 ua_chan
->key
= get_next_channel_key();
656 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
657 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
658 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
660 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
662 /* Initialize UST registry. */
663 ust_registry_channel_init(&ua_sess
->registry
, &ua_chan
->registry
);
665 /* Copy attributes */
667 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
668 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
669 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
670 ua_chan
->attr
.overwrite
= attr
->overwrite
;
671 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
672 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
673 ua_chan
->attr
.output
= attr
->output
;
675 /* By default, the channel is a per cpu channel. */
676 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
678 DBG3("UST app channel %s allocated", ua_chan
->name
);
687 * Allocate and initialize a UST app stream.
689 * Return newly allocated stream pointer or NULL on error.
691 struct ust_app_stream
*ust_app_alloc_stream(void)
693 struct ust_app_stream
*stream
= NULL
;
695 stream
= zmalloc(sizeof(*stream
));
696 if (stream
== NULL
) {
697 PERROR("zmalloc ust app stream");
701 /* Zero could be a valid value for a handle so flag it to -1. */
709 * Alloc new UST app event.
712 struct ust_app_event
*alloc_ust_app_event(char *name
,
713 struct lttng_ust_event
*attr
)
715 struct ust_app_event
*ua_event
;
717 /* Init most of the default value by allocating and zeroing */
718 ua_event
= zmalloc(sizeof(struct ust_app_event
));
719 if (ua_event
== NULL
) {
724 ua_event
->enabled
= 1;
725 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
726 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
727 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
729 /* Copy attributes */
731 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
734 DBG3("UST app event %s allocated", ua_event
->name
);
743 * Alloc new UST app context.
746 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
748 struct ust_app_ctx
*ua_ctx
;
750 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
751 if (ua_ctx
== NULL
) {
756 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
759 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
766 * Allocate a filter and copy the given original filter.
768 * Return allocated filter or NULL on error.
770 static struct lttng_ust_filter_bytecode
*alloc_copy_ust_app_filter(
771 struct lttng_ust_filter_bytecode
*orig_f
)
773 struct lttng_ust_filter_bytecode
*filter
= NULL
;
775 /* Copy filter bytecode */
776 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
778 PERROR("zmalloc alloc ust app filter");
782 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
789 * Find an ust_app using the sock and return it. RCU read side lock must be
790 * held before calling this helper function.
793 struct ust_app
*find_app_by_sock(int sock
)
795 struct lttng_ht_node_ulong
*node
;
796 struct lttng_ht_iter iter
;
798 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
799 node
= lttng_ht_iter_get_node_ulong(&iter
);
801 DBG2("UST app find by sock %d not found", sock
);
805 return caa_container_of(node
, struct ust_app
, sock_n
);
812 * Find an ust_app using the notify sock and return it. RCU read side lock must
813 * be held before calling this helper function.
815 static struct ust_app
*find_app_by_notify_sock(int sock
)
817 struct lttng_ht_node_ulong
*node
;
818 struct lttng_ht_iter iter
;
820 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
822 node
= lttng_ht_iter_get_node_ulong(&iter
);
824 DBG2("UST app find by notify sock %d not found", sock
);
828 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
835 * Lookup for an ust app event based on event name, filter bytecode and the
838 * Return an ust_app_event object or NULL on error.
840 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
841 char *name
, struct lttng_ust_filter_bytecode
*filter
, int loglevel
)
843 struct lttng_ht_iter iter
;
844 struct lttng_ht_node_str
*node
;
845 struct ust_app_event
*event
= NULL
;
846 struct ust_app_ht_key key
;
851 /* Setup key for event lookup. */
854 key
.loglevel
= loglevel
;
856 /* Lookup using the event name as hash and a custom match fct. */
857 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
858 ht_match_ust_app_event
, &key
, &iter
.iter
);
859 node
= lttng_ht_iter_get_node_str(&iter
);
864 event
= caa_container_of(node
, struct ust_app_event
, node
);
871 * Create the channel context on the tracer.
873 * Called with UST app session lock held.
876 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
877 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
881 health_code_update();
883 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
884 ua_chan
->obj
, &ua_ctx
->obj
);
886 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
887 ERR("UST app create channel context failed for app (pid: %d) "
888 "with ret %d", app
->pid
, ret
);
890 DBG3("UST app disable event failed. Application is dead.");
895 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
897 DBG2("UST app context handle %d created successfully for channel %s",
898 ua_ctx
->handle
, ua_chan
->name
);
901 health_code_update();
906 * Set the filter on the tracer.
909 int set_ust_event_filter(struct ust_app_event
*ua_event
,
914 health_code_update();
916 if (!ua_event
->filter
) {
921 ret
= ustctl_set_filter(app
->sock
, ua_event
->filter
,
924 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
925 ERR("UST app event %s filter failed for app (pid: %d) "
926 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
928 DBG3("UST app filter event failed. Application is dead.");
933 DBG2("UST filter set successfully for event %s", ua_event
->name
);
936 health_code_update();
941 * Disable the specified event on to UST tracer for the UST session.
943 static int disable_ust_event(struct ust_app
*app
,
944 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
948 health_code_update();
950 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
952 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
953 ERR("UST app event %s disable failed for app (pid: %d) "
954 "and session handle %d with ret %d",
955 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
957 DBG3("UST app disable event failed. Application is dead.");
962 DBG2("UST app event %s disabled successfully for app (pid: %d)",
963 ua_event
->attr
.name
, app
->pid
);
966 health_code_update();
971 * Disable the specified channel on to UST tracer for the UST session.
973 static int disable_ust_channel(struct ust_app
*app
,
974 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
978 health_code_update();
980 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
982 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
983 ERR("UST app channel %s disable failed for app (pid: %d) "
984 "and session handle %d with ret %d",
985 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
987 DBG3("UST app disable channel failed. Application is dead.");
992 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
993 ua_chan
->name
, app
->pid
);
996 health_code_update();
1001 * Enable the specified channel on to UST tracer for the UST session.
1003 static int enable_ust_channel(struct ust_app
*app
,
1004 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1008 health_code_update();
1010 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1012 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1013 ERR("UST app channel %s enable failed for app (pid: %d) "
1014 "and session handle %d with ret %d",
1015 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1017 DBG3("UST app enable channel failed. Application is dead.");
1022 ua_chan
->enabled
= 1;
1024 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1025 ua_chan
->name
, app
->pid
);
1028 health_code_update();
1033 * Enable the specified event on to UST tracer for the UST session.
1035 static int enable_ust_event(struct ust_app
*app
,
1036 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1040 health_code_update();
1042 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1044 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1045 ERR("UST app event %s enable failed for app (pid: %d) "
1046 "and session handle %d with ret %d",
1047 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1049 DBG3("UST app enable event failed. Application is dead.");
1054 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1055 ua_event
->attr
.name
, app
->pid
);
1058 health_code_update();
1063 * Create the specified channel onto the UST tracer for a UST session. This
1064 * MUST be called with UST app session lock held.
1066 * Return 0 on success. On error, a negative value is returned.
1068 static int create_ust_channel(struct ust_app
*app
,
1069 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
1070 struct consumer_output
*consumer
)
1073 unsigned int nb_fd
= 0;
1074 struct consumer_socket
*socket
;
1075 struct ust_app_stream
*stream
, *stmp
;
1083 health_code_update();
1085 /* Get the right consumer socket for the application. */
1086 socket
= find_consumer_socket_by_bitness(app
->bits_per_long
, consumer
);
1092 health_code_update();
1095 * Ask consumer to create channel. The consumer will return the number of
1096 * stream we have to expect.
1098 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, consumer
, socket
);
1104 * Compute the number of fd needed before receiving them. It must be 2 per
1105 * stream (2 being the default value here).
1107 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
1109 /* Reserve the amount of file descriptor we need. */
1110 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
1112 ERR("Exhausted number of available FD upon create channel");
1116 health_code_update();
1119 * Now get the channel from the consumer. This call wil populate the stream
1120 * list of that channel and set the ust object.
1122 ret
= ust_consumer_get_channel(socket
, ua_chan
);
1127 /* Send channel to the application. */
1128 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1133 health_code_update();
1135 /* Send all streams to application. */
1136 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1137 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1141 /* We don't need the stream anymore once sent to the tracer. */
1142 cds_list_del(&stream
->list
);
1143 delete_ust_app_stream(-1, stream
);
1146 /* Flag the channel that it is sent to the application. */
1147 ua_chan
->is_sent
= 1;
1148 /* Assign session to channel. */
1149 ua_chan
->session
= ua_sess
;
1150 /* Initialize ust objd object using the received handle and add it. */
1151 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
1152 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
1154 health_code_update();
1156 /* If channel is not enabled, disable it on the tracer */
1157 if (!ua_chan
->enabled
) {
1158 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1168 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
1171 * Initiate a destroy channel on the consumer since we had an error
1172 * handling it on our side. The return value is of no importance since we
1173 * already have a ret value set by the previous error that we need to
1176 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
1178 health_code_update();
1184 * Create the specified event onto the UST tracer for a UST session.
1186 * Should be called with session mutex held.
1189 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1190 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1194 health_code_update();
1196 /* Create UST event on tracer */
1197 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1200 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1201 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1202 ua_event
->attr
.name
, app
->pid
, ret
);
1204 DBG3("UST app create event failed. Application is dead.");
1209 ua_event
->handle
= ua_event
->obj
->handle
;
1211 DBG2("UST app event %s created successfully for pid:%d",
1212 ua_event
->attr
.name
, app
->pid
);
1214 health_code_update();
1216 /* Set filter if one is present. */
1217 if (ua_event
->filter
) {
1218 ret
= set_ust_event_filter(ua_event
, app
);
1224 /* If event not enabled, disable it on the tracer */
1225 if (ua_event
->enabled
== 0) {
1226 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1229 * If we hit an EPERM, something is wrong with our disable call. If
1230 * we get an EEXIST, there is a problem on the tracer side since we
1234 case -LTTNG_UST_ERR_PERM
:
1235 /* Code flow problem */
1237 case -LTTNG_UST_ERR_EXIST
:
1238 /* It's OK for our use case. */
1249 health_code_update();
1254 * Copy data between an UST app event and a LTT event.
1256 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1257 struct ltt_ust_event
*uevent
)
1259 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1260 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1262 ua_event
->enabled
= uevent
->enabled
;
1264 /* Copy event attributes */
1265 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1267 /* Copy filter bytecode */
1268 if (uevent
->filter
) {
1269 ua_event
->filter
= alloc_copy_ust_app_filter(uevent
->filter
);
1270 /* Filter might be NULL here in case of ENONEM. */
1275 * Copy data between an UST app channel and a LTT channel.
1277 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1278 struct ltt_ust_channel
*uchan
)
1280 struct lttng_ht_iter iter
;
1281 struct ltt_ust_event
*uevent
;
1282 struct ltt_ust_context
*uctx
;
1283 struct ust_app_event
*ua_event
;
1284 struct ust_app_ctx
*ua_ctx
;
1286 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1288 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1289 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1291 /* Copy event attributes since the layout is different. */
1292 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1293 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1294 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1295 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1296 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1297 ua_chan
->attr
.output
= uchan
->attr
.output
;
1299 * Note that the attribute channel type is not set since the channel on the
1300 * tracing registry side does not have this information.
1303 ua_chan
->enabled
= uchan
->enabled
;
1305 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
1306 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1307 if (ua_ctx
== NULL
) {
1310 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1311 (unsigned long) ua_ctx
->ctx
.ctx
);
1312 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1315 /* Copy all events from ltt ust channel to ust app channel */
1316 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1317 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1318 uevent
->filter
, uevent
->attr
.loglevel
);
1319 if (ua_event
== NULL
) {
1320 DBG2("UST event %s not found on shadow copy channel",
1322 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1323 if (ua_event
== NULL
) {
1326 shadow_copy_event(ua_event
, uevent
);
1327 add_unique_ust_app_event(ua_chan
, ua_event
);
1331 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1335 * Copy data between a UST app session and a regular LTT session.
1337 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1338 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1340 struct lttng_ht_node_str
*ua_chan_node
;
1341 struct lttng_ht_iter iter
;
1342 struct ltt_ust_channel
*uchan
;
1343 struct ust_app_channel
*ua_chan
;
1345 struct tm
*timeinfo
;
1349 /* Get date and time for unique app path */
1351 timeinfo
= localtime(&rawtime
);
1352 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1354 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1356 ua_sess
->id
= usess
->id
;
1357 ua_sess
->uid
= usess
->uid
;
1358 ua_sess
->gid
= usess
->gid
;
1360 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s-%d-%s/", app
->name
, app
->pid
,
1363 PERROR("asprintf UST shadow copy session");
1364 /* TODO: We cannot return an error from here.. */
1368 /* TODO: support all UST domain */
1370 /* Iterate over all channels in global domain. */
1371 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1373 struct lttng_ht_iter uiter
;
1375 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1376 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1377 if (ua_chan_node
!= NULL
) {
1378 /* Session exist. Contiuing. */
1382 DBG2("Channel %s not found on shadow session copy, creating it",
1384 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1385 if (ua_chan
== NULL
) {
1386 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1389 shadow_copy_channel(ua_chan
, uchan
);
1391 * The concept of metadata channel does not exist on the tracing
1392 * registry side of the session daemon so this can only be a per CPU
1393 * channel and not metadata.
1395 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1397 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1402 * Lookup sesison wrapper.
1405 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1406 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1408 /* Get right UST app session from app */
1409 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
1413 * Return ust app session from the app session hashtable using the UST session
1416 static struct ust_app_session
*lookup_session_by_app(
1417 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1419 struct lttng_ht_iter iter
;
1420 struct lttng_ht_node_ulong
*node
;
1422 __lookup_session_by_app(usess
, app
, &iter
);
1423 node
= lttng_ht_iter_get_node_ulong(&iter
);
1428 return caa_container_of(node
, struct ust_app_session
, node
);
1435 * Create a session on the tracer side for the given app.
1437 * On success, ua_sess_ptr is populated with the session pointer or else left
1438 * untouched. If the session was created, is_created is set to 1. On error,
1439 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1442 * Returns 0 on success or else a negative code which is either -ENOMEM or
1443 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1445 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1446 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1449 int ret
, created
= 0;
1450 struct ust_app_session
*ua_sess
;
1454 assert(ua_sess_ptr
);
1456 health_code_update();
1458 ua_sess
= lookup_session_by_app(usess
, app
);
1459 if (ua_sess
== NULL
) {
1460 DBG2("UST app pid: %d session id %d not found, creating it",
1461 app
->pid
, usess
->id
);
1462 ua_sess
= alloc_ust_app_session(app
);
1463 if (ua_sess
== NULL
) {
1464 /* Only malloc can failed so something is really wrong */
1468 shadow_copy_session(ua_sess
, usess
, app
);
1472 health_code_update();
1474 if (ua_sess
->handle
== -1) {
1475 ret
= ustctl_create_session(app
->sock
);
1477 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1478 ERR("Creating session for app pid %d with ret %d",
1481 DBG("UST app creating session failed. Application is dead");
1483 delete_ust_app_session(-1, ua_sess
, app
);
1484 if (ret
!= -ENOMEM
) {
1486 * Tracer is probably gone or got an internal error so let's
1487 * behave like it will soon unregister or not usable.
1494 ua_sess
->handle
= ret
;
1496 /* Add ust app session to app's HT */
1497 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
1498 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
1500 DBG2("UST app session created successfully with handle %d", ret
);
1504 * Assign consumer if not already set. For one application, there is only
1505 * one possible consumer has of now.
1507 if (!ua_sess
->consumer
) {
1508 ua_sess
->consumer
= usess
->consumer
;
1511 *ua_sess_ptr
= ua_sess
;
1513 *is_created
= created
;
1516 /* Everything went well. */
1520 health_code_update();
1525 * Create a context for the channel on the tracer.
1527 * Called with UST app session lock held.
1530 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
1531 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
1532 struct ust_app
*app
)
1535 struct lttng_ht_iter iter
;
1536 struct lttng_ht_node_ulong
*node
;
1537 struct ust_app_ctx
*ua_ctx
;
1539 DBG2("UST app adding context to channel %s", ua_chan
->name
);
1541 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
1542 node
= lttng_ht_iter_get_node_ulong(&iter
);
1548 ua_ctx
= alloc_ust_app_ctx(uctx
);
1549 if (ua_ctx
== NULL
) {
1555 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
1556 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1558 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
1568 * Enable on the tracer side a ust app event for the session and channel.
1570 * Called with UST app session lock held.
1573 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
1574 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1578 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1583 ua_event
->enabled
= 1;
1590 * Disable on the tracer side a ust app event for the session and channel.
1592 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1593 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1597 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1602 ua_event
->enabled
= 0;
1609 * Lookup ust app channel for session and disable it on the tracer side.
1612 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1613 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1617 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1622 ua_chan
->enabled
= 0;
1629 * Lookup ust app channel for session and enable it on the tracer side.
1631 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1632 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1635 struct lttng_ht_iter iter
;
1636 struct lttng_ht_node_str
*ua_chan_node
;
1637 struct ust_app_channel
*ua_chan
;
1639 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1640 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1641 if (ua_chan_node
== NULL
) {
1642 DBG2("Unable to find channel %s in ust session id %u",
1643 uchan
->name
, ua_sess
->id
);
1647 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1649 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1659 * Create UST app channel and create it on the tracer. Set ua_chanp of the
1660 * newly created channel if not NULL.
1662 * Called with UST app session lock held.
1664 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
1665 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
1666 struct consumer_output
*consumer
, enum lttng_ust_chan_type type
,
1667 struct ust_app_channel
**ua_chanp
)
1670 struct lttng_ht_iter iter
;
1671 struct lttng_ht_node_str
*ua_chan_node
;
1672 struct ust_app_channel
*ua_chan
;
1674 /* Lookup channel in the ust app session */
1675 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1676 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1677 if (ua_chan_node
!= NULL
) {
1678 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1682 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1683 if (ua_chan
== NULL
) {
1684 /* Only malloc can fail here */
1688 shadow_copy_channel(ua_chan
, uchan
);
1690 /* Set channel type. */
1691 ua_chan
->attr
.type
= type
;
1693 ret
= create_ust_channel(app
, ua_sess
, ua_chan
, consumer
);
1698 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1701 /* Only add the channel if successful on the tracer side. */
1702 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1706 *ua_chanp
= ua_chan
;
1709 /* Everything went well. */
1713 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
1718 * Create UST app event and create it on the tracer side.
1720 * Called with ust app session mutex held.
1723 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1724 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1725 struct ust_app
*app
)
1728 struct ust_app_event
*ua_event
;
1730 /* Get event node */
1731 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1732 uevent
->filter
, uevent
->attr
.loglevel
);
1733 if (ua_event
!= NULL
) {
1738 /* Does not exist so create one */
1739 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1740 if (ua_event
== NULL
) {
1741 /* Only malloc can failed so something is really wrong */
1745 shadow_copy_event(ua_event
, uevent
);
1747 /* Create it on the tracer side */
1748 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1750 /* Not found previously means that it does not exist on the tracer */
1751 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
1755 add_unique_ust_app_event(ua_chan
, ua_event
);
1757 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1764 /* Valid. Calling here is already in a read side lock */
1765 delete_ust_app_event(-1, ua_event
);
1770 * Create UST metadata and open it on the tracer side.
1772 * Called with UST app session lock held.
1774 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1775 struct ust_app
*app
, struct consumer_output
*consumer
)
1778 struct ust_app_channel
*metadata
;
1779 struct consumer_socket
*socket
;
1785 if (ua_sess
->metadata
) {
1786 /* Already exist. Return success. */
1790 /* Allocate UST metadata */
1791 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
1793 /* malloc() failed */
1798 /* Set default attributes for metadata. */
1799 metadata
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1800 metadata
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
1801 metadata
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
1802 metadata
->attr
.switch_timer_interval
= DEFAULT_UST_CHANNEL_SWITCH_TIMER
;
1803 metadata
->attr
.read_timer_interval
= DEFAULT_UST_CHANNEL_READ_TIMER
;
1804 metadata
->attr
.output
= LTTNG_UST_MMAP
;
1805 metadata
->attr
.type
= LTTNG_UST_CHAN_METADATA
;
1807 /* Get the right consumer socket for the application. */
1808 socket
= find_consumer_socket_by_bitness(app
->bits_per_long
, consumer
);
1811 goto error_consumer
;
1815 * Ask the metadata channel creation to the consumer. The metadata object
1816 * will be created by the consumer and kept their. However, the stream is
1817 * never added or monitored until we do a first push metadata to the
1820 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
);
1822 goto error_consumer
;
1826 * The setup command will make the metadata stream be sent to the relayd,
1827 * if applicable, and the thread managing the metadatas. This is important
1828 * because after this point, if an error occurs, the only way the stream
1829 * can be deleted is to be monitored in the consumer.
1831 ret
= ust_consumer_setup_metadata(socket
, metadata
);
1833 goto error_consumer
;
1836 ua_sess
->metadata
= metadata
;
1838 DBG2("UST metadata created for app pid %d", app
->pid
);
1843 delete_ust_app_channel(-1, metadata
, app
);
1849 * Return pointer to traceable apps list.
1851 struct lttng_ht
*ust_app_get_ht(void)
1857 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
1858 * acquired before calling this function.
1860 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1862 struct ust_app
*app
= NULL
;
1863 struct lttng_ht_node_ulong
*node
;
1864 struct lttng_ht_iter iter
;
1866 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1867 node
= lttng_ht_iter_get_node_ulong(&iter
);
1869 DBG2("UST app no found with pid %d", pid
);
1873 DBG2("Found UST app by pid %d", pid
);
1875 app
= caa_container_of(node
, struct ust_app
, pid_n
);
1882 * Allocate and init an UST app object using the registration information and
1883 * the command socket. This is called when the command socket connects to the
1886 * The object is returned on success or else NULL.
1888 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
1890 struct ust_app
*lta
= NULL
;
1895 DBG3("UST app creating application for socket %d", sock
);
1897 if ((msg
->bits_per_long
== 64 &&
1898 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
1899 || (msg
->bits_per_long
== 32 &&
1900 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
1901 ERR("Registration failed: application \"%s\" (pid: %d) has "
1902 "%d-bit long, but no consumerd for this size is available.\n",
1903 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1907 lta
= zmalloc(sizeof(struct ust_app
));
1913 lta
->ppid
= msg
->ppid
;
1914 lta
->uid
= msg
->uid
;
1915 lta
->gid
= msg
->gid
;
1917 lta
->bits_per_long
= msg
->bits_per_long
;
1918 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
1919 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
1920 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
1921 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
1922 lta
->long_alignment
= msg
->long_alignment
;
1923 lta
->byte_order
= msg
->byte_order
;
1925 lta
->v_major
= msg
->major
;
1926 lta
->v_minor
= msg
->minor
;
1927 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1928 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1929 lta
->notify_sock
= -1;
1931 /* Copy name and make sure it's NULL terminated. */
1932 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1933 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
1936 * Before this can be called, when receiving the registration information,
1937 * the application compatibility is checked. So, at this point, the
1938 * application can work with this session daemon.
1940 lta
->compatible
= 1;
1942 lta
->pid
= msg
->pid
;
1943 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
1945 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
1947 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
1954 * For a given application object, add it to every hash table.
1956 void ust_app_add(struct ust_app
*app
)
1959 assert(app
->notify_sock
>= 0);
1964 * On a re-registration, we want to kick out the previous registration of
1967 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
1970 * The socket _should_ be unique until _we_ call close. So, a add_unique
1971 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1972 * already in the table.
1974 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
1976 /* Add application to the notify socket hash table. */
1977 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
1978 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
1980 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
1981 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
1982 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
1989 * Set the application version into the object.
1991 * Return 0 on success else a negative value either an errno code or a
1992 * LTTng-UST error code.
1994 int ust_app_version(struct ust_app
*app
)
2000 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
2002 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
2003 ERR("UST app %d verson failed with ret %d", app
->sock
, ret
);
2005 DBG3("UST app %d verion failed. Application is dead", app
->sock
);
2013 * Unregister app by removing it from the global traceable app list and freeing
2016 * The socket is already closed at this point so no close to sock.
2018 void ust_app_unregister(int sock
)
2020 struct ust_app
*lta
;
2021 struct lttng_ht_node_ulong
*node
;
2022 struct lttng_ht_iter iter
;
2023 struct ust_app_session
*ua_sess
;
2028 /* Get the node reference for a call_rcu */
2029 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
2030 node
= lttng_ht_iter_get_node_ulong(&iter
);
2033 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
2034 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
2036 /* Remove application from PID hash table */
2037 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
2041 * Remove application from notify hash table. The thread handling the
2042 * notify socket could have deleted the node so ignore on error because
2043 * either way it's valid. The close of that socket is handled by the other
2046 iter
.iter
.node
= <a
->notify_sock_n
.node
;
2047 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
2050 * Ignore return value since the node might have been removed before by an
2051 * add replace during app registration because the PID can be reassigned by
2054 iter
.iter
.node
= <a
->pid_n
.node
;
2055 ret
= lttng_ht_del(ust_app_ht
, &iter
);
2057 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
2061 /* Remove sessions so they are not visible during deletion.*/
2062 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
2064 ret
= lttng_ht_del(lta
->sessions
, &iter
);
2066 /* The session was already removed so scheduled for teardown. */
2071 * Add session to list for teardown. This is safe since at this point we
2072 * are the only one using this list.
2074 pthread_mutex_lock(&ua_sess
->lock
);
2077 * Normally, this is done in the delete session process which is
2078 * executed in the call rcu below. However, upon registration we can't
2079 * afford to wait for the grace period before pushing data or else the
2080 * data pending feature can race between the unregistration and stop
2081 * command where the data pending command is sent *before* the grace
2084 * The close metadata below nullifies the metadata pointer in the
2085 * session so the delete session will NOT push/close a second time.
2087 (void) push_metadata(lta
, ua_sess
);
2088 (void) close_metadata(lta
, ua_sess
);
2090 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
2091 pthread_mutex_unlock(&ua_sess
->lock
);
2095 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
2102 * Return traceable_app_count
2104 unsigned long ust_app_list_count(void)
2106 unsigned long count
;
2109 count
= lttng_ht_get_count(ust_app_ht
);
2116 * Fill events array with all events name of all registered apps.
2118 int ust_app_list_events(struct lttng_event
**events
)
2121 size_t nbmem
, count
= 0;
2122 struct lttng_ht_iter iter
;
2123 struct ust_app
*app
;
2124 struct lttng_event
*tmp_event
;
2126 nbmem
= UST_APP_EVENT_LIST_SIZE
;
2127 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
2128 if (tmp_event
== NULL
) {
2129 PERROR("zmalloc ust app events");
2136 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2137 struct lttng_ust_tracepoint_iter uiter
;
2139 health_code_update();
2141 if (!app
->compatible
) {
2143 * TODO: In time, we should notice the caller of this error by
2144 * telling him that this is a version error.
2148 handle
= ustctl_tracepoint_list(app
->sock
);
2150 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
2151 ERR("UST app list events getting handle failed for app pid %d",
2157 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
2158 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
2159 /* Handle ustctl error. */
2162 if (ret
!= -LTTNG_UST_ERR_EXITING
|| ret
!= -EPIPE
) {
2163 ERR("UST app tp list get failed for app %d with ret %d",
2166 DBG3("UST app tp list get failed. Application is dead");
2171 health_code_update();
2172 if (count
>= nbmem
) {
2173 /* In case the realloc fails, we free the memory */
2176 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
2179 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event
));
2181 PERROR("realloc ust app events");
2188 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
2189 tmp_event
[count
].loglevel
= uiter
.loglevel
;
2190 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
2191 tmp_event
[count
].pid
= app
->pid
;
2192 tmp_event
[count
].enabled
= -1;
2198 *events
= tmp_event
;
2200 DBG2("UST app list events done (%zu events)", count
);
2205 health_code_update();
2210 * Fill events array with all events name of all registered apps.
2212 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
2215 size_t nbmem
, count
= 0;
2216 struct lttng_ht_iter iter
;
2217 struct ust_app
*app
;
2218 struct lttng_event_field
*tmp_event
;
2220 nbmem
= UST_APP_EVENT_LIST_SIZE
;
2221 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
2222 if (tmp_event
== NULL
) {
2223 PERROR("zmalloc ust app event fields");
2230 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2231 struct lttng_ust_field_iter uiter
;
2233 health_code_update();
2235 if (!app
->compatible
) {
2237 * TODO: In time, we should notice the caller of this error by
2238 * telling him that this is a version error.
2242 handle
= ustctl_tracepoint_field_list(app
->sock
);
2244 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
2245 ERR("UST app list field getting handle failed for app pid %d",
2251 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
2252 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
2253 /* Handle ustctl error. */
2256 if (ret
!= -LTTNG_UST_ERR_EXITING
|| ret
!= -EPIPE
) {
2257 ERR("UST app tp list field failed for app %d with ret %d",
2260 DBG3("UST app tp list field failed. Application is dead");
2265 health_code_update();
2266 if (count
>= nbmem
) {
2267 /* In case the realloc fails, we free the memory */
2270 DBG2("Reallocating event field list from %zu to %zu entries", nbmem
,
2273 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event_field
));
2275 PERROR("realloc ust app event fields");
2283 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
2284 tmp_event
[count
].type
= uiter
.type
;
2285 tmp_event
[count
].nowrite
= uiter
.nowrite
;
2287 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2288 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
2289 tmp_event
[count
].event
.type
= LTTNG_UST_TRACEPOINT
;
2290 tmp_event
[count
].event
.pid
= app
->pid
;
2291 tmp_event
[count
].event
.enabled
= -1;
2297 *fields
= tmp_event
;
2299 DBG2("UST app list event fields done (%zu events)", count
);
2304 health_code_update();
2309 * Free and clean all traceable apps of the global list.
2311 void ust_app_clean_list(void)
2314 struct ust_app
*app
;
2315 struct lttng_ht_iter iter
;
2317 DBG2("UST app cleaning registered apps hash table");
2321 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2322 ret
= lttng_ht_del(ust_app_ht
, &iter
);
2324 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
2327 /* Cleanup socket hash table */
2328 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
2330 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
2334 /* Cleanup notify socket hash table */
2335 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
2336 notify_sock_n
.node
) {
2337 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
2341 /* Destroy is done only when the ht is empty */
2342 lttng_ht_destroy(ust_app_ht
);
2343 lttng_ht_destroy(ust_app_ht_by_sock
);
2344 lttng_ht_destroy(ust_app_ht_by_notify_sock
);
2350 * Init UST app hash table.
2352 void ust_app_ht_alloc(void)
2354 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2355 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2356 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2360 * For a specific UST session, disable the channel for all registered apps.
2362 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
2363 struct ltt_ust_channel
*uchan
)
2366 struct lttng_ht_iter iter
;
2367 struct lttng_ht_node_str
*ua_chan_node
;
2368 struct ust_app
*app
;
2369 struct ust_app_session
*ua_sess
;
2370 struct ust_app_channel
*ua_chan
;
2372 if (usess
== NULL
|| uchan
== NULL
) {
2373 ERR("Disabling UST global channel with NULL values");
2378 DBG2("UST app disabling channel %s from global domain for session id %d",
2379 uchan
->name
, usess
->id
);
2383 /* For every registered applications */
2384 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2385 struct lttng_ht_iter uiter
;
2386 if (!app
->compatible
) {
2388 * TODO: In time, we should notice the caller of this error by
2389 * telling him that this is a version error.
2393 ua_sess
= lookup_session_by_app(usess
, app
);
2394 if (ua_sess
== NULL
) {
2399 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2400 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2401 /* If the session if found for the app, the channel must be there */
2402 assert(ua_chan_node
);
2404 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2405 /* The channel must not be already disabled */
2406 assert(ua_chan
->enabled
== 1);
2408 /* Disable channel onto application */
2409 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
2411 /* XXX: We might want to report this error at some point... */
2423 * For a specific UST session, enable the channel for all registered apps.
2425 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
2426 struct ltt_ust_channel
*uchan
)
2429 struct lttng_ht_iter iter
;
2430 struct ust_app
*app
;
2431 struct ust_app_session
*ua_sess
;
2433 if (usess
== NULL
|| uchan
== NULL
) {
2434 ERR("Adding UST global channel to NULL values");
2439 DBG2("UST app enabling channel %s to global domain for session id %d",
2440 uchan
->name
, usess
->id
);
2444 /* For every registered applications */
2445 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2446 if (!app
->compatible
) {
2448 * TODO: In time, we should notice the caller of this error by
2449 * telling him that this is a version error.
2453 ua_sess
= lookup_session_by_app(usess
, app
);
2454 if (ua_sess
== NULL
) {
2458 /* Enable channel onto application */
2459 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
2461 /* XXX: We might want to report this error at some point... */
2473 * Disable an event in a channel and for a specific session.
2475 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
2476 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2479 struct lttng_ht_iter iter
, uiter
;
2480 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2481 struct ust_app
*app
;
2482 struct ust_app_session
*ua_sess
;
2483 struct ust_app_channel
*ua_chan
;
2484 struct ust_app_event
*ua_event
;
2486 DBG("UST app disabling event %s for all apps in channel "
2487 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
2491 /* For all registered applications */
2492 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2493 if (!app
->compatible
) {
2495 * TODO: In time, we should notice the caller of this error by
2496 * telling him that this is a version error.
2500 ua_sess
= lookup_session_by_app(usess
, app
);
2501 if (ua_sess
== NULL
) {
2506 /* Lookup channel in the ust app session */
2507 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2508 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2509 if (ua_chan_node
== NULL
) {
2510 DBG2("Channel %s not found in session id %d for app pid %d."
2511 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
2514 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2516 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2517 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2518 if (ua_event_node
== NULL
) {
2519 DBG2("Event %s not found in channel %s for app pid %d."
2520 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
2523 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2525 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2527 /* XXX: Report error someday... */
2538 * For a specific UST session and UST channel, the event for all
2541 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
2542 struct ltt_ust_channel
*uchan
)
2545 struct lttng_ht_iter iter
, uiter
;
2546 struct lttng_ht_node_str
*ua_chan_node
;
2547 struct ust_app
*app
;
2548 struct ust_app_session
*ua_sess
;
2549 struct ust_app_channel
*ua_chan
;
2550 struct ust_app_event
*ua_event
;
2552 DBG("UST app disabling all event for all apps in channel "
2553 "%s for session id %d", uchan
->name
, usess
->id
);
2557 /* For all registered applications */
2558 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2559 if (!app
->compatible
) {
2561 * TODO: In time, we should notice the caller of this error by
2562 * telling him that this is a version error.
2566 ua_sess
= lookup_session_by_app(usess
, app
);
2568 /* The application has problem or is probably dead. */
2572 /* Lookup channel in the ust app session */
2573 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2574 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2575 /* If the channel is not found, there is a code flow error */
2576 assert(ua_chan_node
);
2578 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2580 /* Disable each events of channel */
2581 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2583 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2585 /* XXX: Report error someday... */
2597 * For a specific UST session, create the channel for all registered apps.
2599 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
2600 struct ltt_ust_channel
*uchan
)
2602 int ret
= 0, created
;
2603 struct lttng_ht_iter iter
;
2604 struct ust_app
*app
;
2605 struct ust_app_session
*ua_sess
= NULL
;
2607 /* Very wrong code flow */
2611 DBG2("UST app adding channel %s to global domain for session id %d",
2612 uchan
->name
, usess
->id
);
2616 /* For every registered applications */
2617 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2618 if (!app
->compatible
) {
2620 * TODO: In time, we should notice the caller of this error by
2621 * telling him that this is a version error.
2626 * Create session on the tracer side and add it to app session HT. Note
2627 * that if session exist, it will simply return a pointer to the ust
2630 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
2635 * The application's socket is not valid. Either a bad socket
2636 * or a timeout on it. We can't inform the caller that for a
2637 * specific app, the session failed so lets continue here.
2642 goto error_rcu_unlock
;
2647 pthread_mutex_lock(&ua_sess
->lock
);
2648 /* Create channel onto application. We don't need the chan ref. */
2649 ret
= create_ust_app_channel(ua_sess
, uchan
, app
, usess
->consumer
,
2650 LTTNG_UST_CHAN_PER_CPU
, NULL
);
2651 pthread_mutex_unlock(&ua_sess
->lock
);
2653 if (ret
== -ENOMEM
) {
2654 /* No more memory is a fatal error. Stop right now. */
2655 goto error_rcu_unlock
;
2657 /* Cleanup the created session if it's the case. */
2659 destroy_app_session(app
, ua_sess
);
2670 * Enable event for a specific session and channel on the tracer.
2672 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
2673 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2676 struct lttng_ht_iter iter
, uiter
;
2677 struct lttng_ht_node_str
*ua_chan_node
;
2678 struct ust_app
*app
;
2679 struct ust_app_session
*ua_sess
;
2680 struct ust_app_channel
*ua_chan
;
2681 struct ust_app_event
*ua_event
;
2683 DBG("UST app enabling event %s for all apps for session id %d",
2684 uevent
->attr
.name
, usess
->id
);
2687 * NOTE: At this point, this function is called only if the session and
2688 * channel passed are already created for all apps. and enabled on the
2694 /* For all registered applications */
2695 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2696 if (!app
->compatible
) {
2698 * TODO: In time, we should notice the caller of this error by
2699 * telling him that this is a version error.
2703 ua_sess
= lookup_session_by_app(usess
, app
);
2705 /* The application has problem or is probably dead. */
2709 pthread_mutex_lock(&ua_sess
->lock
);
2711 /* Lookup channel in the ust app session */
2712 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2713 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2714 /* If the channel is not found, there is a code flow error */
2715 assert(ua_chan_node
);
2717 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2719 /* Get event node */
2720 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2721 uevent
->filter
, uevent
->attr
.loglevel
);
2722 if (ua_event
== NULL
) {
2723 DBG3("UST app enable event %s not found for app PID %d."
2724 "Skipping app", uevent
->attr
.name
, app
->pid
);
2728 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2730 pthread_mutex_unlock(&ua_sess
->lock
);
2734 pthread_mutex_unlock(&ua_sess
->lock
);
2743 * For a specific existing UST session and UST channel, creates the event for
2744 * all registered apps.
2746 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
2747 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2750 struct lttng_ht_iter iter
, uiter
;
2751 struct lttng_ht_node_str
*ua_chan_node
;
2752 struct ust_app
*app
;
2753 struct ust_app_session
*ua_sess
;
2754 struct ust_app_channel
*ua_chan
;
2756 DBG("UST app creating event %s for all apps for session id %d",
2757 uevent
->attr
.name
, usess
->id
);
2761 /* For all registered applications */
2762 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2763 if (!app
->compatible
) {
2765 * TODO: In time, we should notice the caller of this error by
2766 * telling him that this is a version error.
2770 ua_sess
= lookup_session_by_app(usess
, app
);
2772 /* The application has problem or is probably dead. */
2776 pthread_mutex_lock(&ua_sess
->lock
);
2777 /* Lookup channel in the ust app session */
2778 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2779 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2780 /* If the channel is not found, there is a code flow error */
2781 assert(ua_chan_node
);
2783 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2785 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2786 pthread_mutex_unlock(&ua_sess
->lock
);
2788 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
2789 /* Possible value at this point: -ENOMEM. If so, we stop! */
2792 DBG2("UST app event %s already exist on app PID %d",
2793 uevent
->attr
.name
, app
->pid
);
2804 * Start tracing for a specific UST session and app.
2806 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2809 struct ust_app_session
*ua_sess
;
2811 DBG("Starting tracing for ust app pid %d", app
->pid
);
2815 if (!app
->compatible
) {
2819 ua_sess
= lookup_session_by_app(usess
, app
);
2820 if (ua_sess
== NULL
) {
2821 /* The session is in teardown process. Ignore and continue. */
2825 pthread_mutex_lock(&ua_sess
->lock
);
2827 /* Upon restart, we skip the setup, already done */
2828 if (ua_sess
->started
) {
2832 /* Create directories if consumer is LOCAL and has a path defined. */
2833 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2834 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
2835 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
2836 S_IRWXU
| S_IRWXG
, usess
->uid
, usess
->gid
);
2838 if (ret
!= -EEXIST
) {
2839 ERR("Trace directory creation error");
2845 /* Create the metadata for the application. */
2846 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
2851 health_code_update();
2854 /* This start the UST tracing */
2855 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
2857 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2858 ERR("Error starting tracing for app pid: %d (ret: %d)",
2861 DBG("UST app start session failed. Application is dead.");
2866 /* Indicate that the session has been started once */
2867 ua_sess
->started
= 1;
2869 pthread_mutex_unlock(&ua_sess
->lock
);
2871 health_code_update();
2873 /* Quiescent wait after starting trace */
2874 ret
= ustctl_wait_quiescent(app
->sock
);
2875 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2876 ERR("UST app wait quiescent failed for app pid %d ret %d",
2882 health_code_update();
2886 pthread_mutex_unlock(&ua_sess
->lock
);
2888 health_code_update();
2893 * Stop tracing for a specific UST session and app.
2895 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2898 struct lttng_ht_iter iter
;
2899 struct ust_app_session
*ua_sess
;
2900 struct ust_app_channel
*ua_chan
;
2902 DBG("Stopping tracing for ust app pid %d", app
->pid
);
2906 if (!app
->compatible
) {
2907 goto end_no_session
;
2910 ua_sess
= lookup_session_by_app(usess
, app
);
2911 if (ua_sess
== NULL
) {
2912 goto end_no_session
;
2915 pthread_mutex_lock(&ua_sess
->lock
);
2918 * If started = 0, it means that stop trace has been called for a session
2919 * that was never started. It's possible since we can have a fail start
2920 * from either the application manager thread or the command thread. Simply
2921 * indicate that this is a stop error.
2923 if (!ua_sess
->started
) {
2924 goto error_rcu_unlock
;
2927 health_code_update();
2929 /* This inhibits UST tracing */
2930 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
2932 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2933 ERR("Error stopping tracing for app pid: %d (ret: %d)",
2936 DBG("UST app stop session failed. Application is dead.");
2938 goto error_rcu_unlock
;
2941 health_code_update();
2943 /* Quiescent wait after stopping trace */
2944 ret
= ustctl_wait_quiescent(app
->sock
);
2945 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2946 ERR("UST app wait quiescent failed for app pid %d ret %d",
2950 health_code_update();
2952 /* Flushing buffers */
2953 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2955 health_code_update();
2956 assert(ua_chan
->is_sent
);
2957 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_chan
->obj
);
2959 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2960 ERR("UST app PID %d channel %s flush failed with ret %d",
2961 app
->pid
, ua_chan
->name
, ret
);
2963 DBG3("UST app failed to flush %s. Application is dead.",
2965 /* No need to continue. */
2968 /* Continuing flushing all buffers */
2973 health_code_update();
2975 ret
= push_metadata(app
, ua_sess
);
2977 goto error_rcu_unlock
;
2980 pthread_mutex_unlock(&ua_sess
->lock
);
2983 health_code_update();
2987 pthread_mutex_unlock(&ua_sess
->lock
);
2989 health_code_update();
2994 * Destroy a specific UST session in apps.
2996 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2999 struct ust_app_session
*ua_sess
;
3000 struct lttng_ht_iter iter
;
3001 struct lttng_ht_node_ulong
*node
;
3003 DBG("Destroy tracing for ust app pid %d", app
->pid
);
3007 if (!app
->compatible
) {
3011 __lookup_session_by_app(usess
, app
, &iter
);
3012 node
= lttng_ht_iter_get_node_ulong(&iter
);
3014 /* Session is being or is deleted. */
3017 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
3019 health_code_update();
3020 destroy_app_session(app
, ua_sess
);
3022 health_code_update();
3024 /* Quiescent wait after stopping trace */
3025 ret
= ustctl_wait_quiescent(app
->sock
);
3026 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3027 ERR("UST app wait quiescent failed for app pid %d ret %d",
3032 health_code_update();
3037 * Start tracing for the UST session.
3039 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
3042 struct lttng_ht_iter iter
;
3043 struct ust_app
*app
;
3045 DBG("Starting all UST traces");
3049 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3050 ret
= ust_app_start_trace(usess
, app
);
3052 /* Continue to next apps even on error */
3063 * Start tracing for the UST session.
3065 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
3068 struct lttng_ht_iter iter
;
3069 struct ust_app
*app
;
3071 DBG("Stopping all UST traces");
3075 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3076 ret
= ust_app_stop_trace(usess
, app
);
3078 ERR("UST app stop trace failed with ret %d", ret
);
3079 /* Continue to next apps even on error */
3090 * Destroy app UST session.
3092 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
3095 struct lttng_ht_iter iter
;
3096 struct ust_app
*app
;
3098 DBG("Destroy all UST traces");
3102 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3103 ret
= destroy_trace(usess
, app
);
3105 /* Continue to next apps even on error */
3116 * Add channels/events from UST global domain to registered apps at sock.
3118 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
3121 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
3122 struct ust_app
*app
;
3123 struct ust_app_session
*ua_sess
= NULL
;
3124 struct ust_app_channel
*ua_chan
;
3125 struct ust_app_event
*ua_event
;
3126 struct ust_app_ctx
*ua_ctx
;
3131 DBG2("UST app global update for app sock %d for session id %d", sock
,
3136 app
= find_app_by_sock(sock
);
3139 * Application can be unregistered before so this is possible hence
3140 * simply stopping the update.
3142 DBG3("UST app update failed to find app sock %d", sock
);
3146 if (!app
->compatible
) {
3150 ret
= create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
3152 /* Tracer is probably gone or ENOMEM. */
3157 pthread_mutex_lock(&ua_sess
->lock
);
3160 * We can iterate safely here over all UST app session since the create ust
3161 * app session above made a shadow copy of the UST global domain from the
3164 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
3166 ret
= create_ust_channel(app
, ua_sess
, ua_chan
, usess
->consumer
);
3169 * Stop everything. On error, the application failed, no more file
3170 * descriptor are available or ENOMEM so stopping here is the only
3171 * thing we can do for now.
3176 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
3178 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
3185 /* For each events */
3186 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
3188 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
3195 pthread_mutex_unlock(&ua_sess
->lock
);
3197 if (usess
->start_trace
) {
3198 ret
= ust_app_start_trace(usess
, app
);
3203 DBG2("UST trace started for app pid %d", app
->pid
);
3206 /* Everything went well at this point. */
3211 pthread_mutex_unlock(&ua_sess
->lock
);
3214 destroy_app_session(app
, ua_sess
);
3221 * Add context to a specific channel for global UST domain.
3223 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
3224 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
3227 struct lttng_ht_node_str
*ua_chan_node
;
3228 struct lttng_ht_iter iter
, uiter
;
3229 struct ust_app_channel
*ua_chan
= NULL
;
3230 struct ust_app_session
*ua_sess
;
3231 struct ust_app
*app
;
3235 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3236 if (!app
->compatible
) {
3238 * TODO: In time, we should notice the caller of this error by
3239 * telling him that this is a version error.
3243 ua_sess
= lookup_session_by_app(usess
, app
);
3244 if (ua_sess
== NULL
) {
3248 pthread_mutex_lock(&ua_sess
->lock
);
3249 /* Lookup channel in the ust app session */
3250 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3251 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3252 if (ua_chan_node
== NULL
) {
3255 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
3257 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
3262 pthread_mutex_unlock(&ua_sess
->lock
);
3270 * Enable event for a channel from a UST session for a specific PID.
3272 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
3273 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
3276 struct lttng_ht_iter iter
;
3277 struct lttng_ht_node_str
*ua_chan_node
;
3278 struct ust_app
*app
;
3279 struct ust_app_session
*ua_sess
;
3280 struct ust_app_channel
*ua_chan
;
3281 struct ust_app_event
*ua_event
;
3283 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
3287 app
= ust_app_find_by_pid(pid
);
3289 ERR("UST app enable event per PID %d not found", pid
);
3294 if (!app
->compatible
) {
3299 ua_sess
= lookup_session_by_app(usess
, app
);
3301 /* The application has problem or is probably dead. */
3306 pthread_mutex_lock(&ua_sess
->lock
);
3307 /* Lookup channel in the ust app session */
3308 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3309 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3310 /* If the channel is not found, there is a code flow error */
3311 assert(ua_chan_node
);
3313 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3315 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3316 uevent
->filter
, uevent
->attr
.loglevel
);
3317 if (ua_event
== NULL
) {
3318 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
3323 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
3330 pthread_mutex_unlock(&ua_sess
->lock
);
3337 * Disable event for a channel from a UST session for a specific PID.
3339 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
3340 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
3343 struct lttng_ht_iter iter
;
3344 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3345 struct ust_app
*app
;
3346 struct ust_app_session
*ua_sess
;
3347 struct ust_app_channel
*ua_chan
;
3348 struct ust_app_event
*ua_event
;
3350 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
3354 app
= ust_app_find_by_pid(pid
);
3356 ERR("UST app disable event per PID %d not found", pid
);
3361 if (!app
->compatible
) {
3366 ua_sess
= lookup_session_by_app(usess
, app
);
3368 /* The application has problem or is probably dead. */
3372 /* Lookup channel in the ust app session */
3373 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3374 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3375 if (ua_chan_node
== NULL
) {
3376 /* Channel does not exist, skip disabling */
3379 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3381 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
3382 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
3383 if (ua_event_node
== NULL
) {
3384 /* Event does not exist, skip disabling */
3387 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3389 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3400 * Calibrate registered applications.
3402 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
3405 struct lttng_ht_iter iter
;
3406 struct ust_app
*app
;
3410 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3411 if (!app
->compatible
) {
3413 * TODO: In time, we should notice the caller of this error by
3414 * telling him that this is a version error.
3419 health_code_update();
3421 ret
= ustctl_calibrate(app
->sock
, calibrate
);
3425 /* Means that it's not implemented on the tracer side. */
3429 DBG2("Calibrate app PID %d returned with error %d",
3436 DBG("UST app global domain calibration finished");
3440 health_code_update();
3446 * Receive registration and populate the given msg structure.
3448 * On success return 0 else a negative value returned by the ustctl call.
3450 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
3453 uint32_t pid
, ppid
, uid
, gid
;
3457 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
3458 &pid
, &ppid
, &uid
, &gid
,
3459 &msg
->bits_per_long
,
3460 &msg
->uint8_t_alignment
,
3461 &msg
->uint16_t_alignment
,
3462 &msg
->uint32_t_alignment
,
3463 &msg
->uint64_t_alignment
,
3464 &msg
->long_alignment
,
3471 case LTTNG_UST_ERR_EXITING
:
3472 DBG3("UST app recv reg message failed. Application died");
3474 case LTTNG_UST_ERR_UNSUP_MAJOR
:
3475 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
3476 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
3477 LTTNG_UST_ABI_MINOR_VERSION
);
3480 ERR("UST app recv reg message failed with ret %d", ret
);
3485 msg
->pid
= (pid_t
) pid
;
3486 msg
->ppid
= (pid_t
) ppid
;
3487 msg
->uid
= (uid_t
) uid
;
3488 msg
->gid
= (gid_t
) gid
;
3495 * Return a ust app channel object using the application object and the channel
3496 * object descriptor has a key. If not found, NULL is returned. A RCU read side
3497 * lock MUST be acquired before calling this function.
3499 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
3502 struct lttng_ht_node_ulong
*node
;
3503 struct lttng_ht_iter iter
;
3504 struct ust_app_channel
*ua_chan
= NULL
;
3508 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
3509 node
= lttng_ht_iter_get_node_ulong(&iter
);
3511 DBG2("UST app channel find by objd %d not found", objd
);
3515 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
3522 * Reply to a register channel notification from an application on the notify
3523 * socket. The channel metadata is also created.
3525 * The session UST registry lock is acquired in this function.
3527 * On success 0 is returned else a negative value.
3529 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
3530 size_t nr_fields
, struct ustctl_field
*fields
)
3532 int ret
, ret_code
= 0;
3533 uint32_t chan_id
, reg_count
;
3534 enum ustctl_channel_header type
;
3535 struct ust_app
*app
;
3536 struct ust_app_channel
*ua_chan
;
3537 struct ust_app_session
*ua_sess
;
3541 /* Lookup application. If not found, there is a code flow error. */
3542 app
= find_app_by_notify_sock(sock
);
3544 DBG("Application socket %d is being teardown. Abort event notify",
3547 goto error_rcu_unlock
;
3550 /* Lookup channel by UST object descriptor. Should always be found. */
3551 ua_chan
= find_channel_by_objd(app
, cobjd
);
3553 assert(ua_chan
->session
);
3554 ua_sess
= ua_chan
->session
;
3557 pthread_mutex_lock(&ua_sess
->registry
.lock
);
3559 if (ust_registry_is_max_id(ua_chan
->session
->registry
.used_channel_id
)) {
3566 /* Don't assign ID to metadata. */
3567 if (ua_chan
->attr
.type
== LTTNG_UST_CHAN_METADATA
) {
3570 chan_id
= ust_registry_get_next_chan_id(&ua_chan
->session
->registry
);
3573 reg_count
= ust_registry_get_event_count(&ua_chan
->registry
);
3574 if (reg_count
< 31) {
3575 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
3577 type
= USTCTL_CHANNEL_HEADER_LARGE
;
3580 ua_chan
->registry
.nr_ctx_fields
= nr_fields
;
3581 ua_chan
->registry
.ctx_fields
= fields
;
3582 ua_chan
->registry
.chan_id
= chan_id
;
3583 ua_chan
->registry
.header_type
= type
;
3585 /* Append to metadata */
3587 ret_code
= ust_metadata_channel_statedump(&ua_chan
->session
->registry
,
3588 &ua_chan
->registry
);
3590 ERR("Error appending channel metadata (errno = %d)", ret_code
);
3596 DBG3("UST app replying to register channel with id %u, type: %d, ret: %d",
3597 chan_id
, type
, ret_code
);
3599 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
3601 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3602 ERR("UST app reply channel failed with ret %d", ret
);
3604 DBG3("UST app reply channel failed. Application died");
3610 pthread_mutex_unlock(&ua_sess
->registry
.lock
);
3617 * Add event to the UST channel registry. When the event is added to the
3618 * registry, the metadata is also created. Once done, this replies to the
3619 * application with the appropriate error code.
3621 * The session UST registry lock is acquired in the function.
3623 * On success 0 is returned else a negative value.
3625 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
3626 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
3627 char *model_emf_uri
)
3630 uint32_t event_id
= 0;
3631 struct ust_app
*app
;
3632 struct ust_app_channel
*ua_chan
;
3633 struct ust_app_session
*ua_sess
;
3637 /* Lookup application. If not found, there is a code flow error. */
3638 app
= find_app_by_notify_sock(sock
);
3640 DBG("Application socket %d is being teardown. Abort event notify",
3643 goto error_rcu_unlock
;
3646 /* Lookup channel by UST object descriptor. Should always be found. */
3647 ua_chan
= find_channel_by_objd(app
, cobjd
);
3649 assert(ua_chan
->session
);
3650 ua_sess
= ua_chan
->session
;
3652 pthread_mutex_lock(&ua_sess
->registry
.lock
);
3654 ret_code
= ust_registry_create_event(&ua_sess
->registry
,
3655 &ua_chan
->registry
, sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
3656 loglevel
, model_emf_uri
, &event_id
);
3659 * The return value is returned to ustctl so in case of an error, the
3660 * application can be notified. In case of an error, it's important not to
3661 * return a negative error or else the application will get closed.
3663 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
3665 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3666 ERR("UST app reply event failed with ret %d", ret
);
3668 DBG3("UST app reply event failed. Application died");
3671 * No need to wipe the create event since the application socket will
3672 * get close on error hence cleaning up everything by itself.
3677 DBG3("UST registry event %s has been added successfully", name
);
3680 pthread_mutex_unlock(&ua_sess
->registry
.lock
);
3687 * Handle application notification through the given notify socket.
3689 * Return 0 on success or else a negative value.
3691 int ust_app_recv_notify(int sock
)
3694 enum ustctl_notify_cmd cmd
;
3696 DBG3("UST app receiving notify from sock %d", sock
);
3698 ret
= ustctl_recv_notify(sock
, &cmd
);
3700 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3701 ERR("UST app recv notify failed with ret %d", ret
);
3703 DBG3("UST app recv notify failed. Application died");
3709 case USTCTL_NOTIFY_CMD_EVENT
:
3711 int sobjd
, cobjd
, loglevel
;
3712 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
3714 struct ustctl_field
*fields
;
3716 DBG2("UST app ustctl register event received");
3718 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
3719 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
3721 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3722 ERR("UST app recv event failed with ret %d", ret
);
3724 DBG3("UST app recv event failed. Application died");
3729 /* Add event to the UST registry coming from the notify socket. */
3730 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
3731 fields
, loglevel
, model_emf_uri
);
3738 case USTCTL_NOTIFY_CMD_CHANNEL
:
3742 struct ustctl_field
*fields
;
3744 DBG2("UST app ustctl register channel received");
3746 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
3749 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3750 ERR("UST app recv channel failed with ret %d", ret
);
3752 DBG3("UST app recv channel failed. Application died");
3757 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
3766 /* Should NEVER happen. */
3775 * Once the notify socket hangs up, this is called. First, it tries to find the
3776 * corresponding application. On failure, the call_rcu to close the socket is
3777 * executed. If an application is found, it tries to delete it from the notify
3778 * socket hash table. Whathever the result, it proceeds to the call_rcu.
3780 * Note that an object needs to be allocated here so on ENOMEM failure, the
3781 * call RCU is not done but the rest of the cleanup is.
3783 void ust_app_notify_sock_unregister(int sock
)
3786 struct lttng_ht_iter iter
;
3787 struct ust_app
*app
;
3788 struct ust_app_notify_sock_obj
*obj
;
3794 obj
= zmalloc(sizeof(*obj
));
3797 * An ENOMEM is kind of uncool. If this strikes we continue the
3798 * procedure but the call_rcu will not be called. In this case, we
3799 * accept the fd leak rather than possibly creating an unsynchronized
3800 * state between threads.
3802 * TODO: The notify object should be created once the notify socket is
3803 * registered and stored independantely from the ust app object. The
3804 * tricky part is to synchronize the teardown of the application and
3805 * this notify object. Let's keep that in mind so we can avoid this
3806 * kind of shenanigans with ENOMEM in the teardown path.
3813 DBG("UST app notify socket unregister %d", sock
);
3816 * Lookup application by notify socket. If this fails, this means that the
3817 * hash table delete has already been done by the application
3818 * unregistration process so we can safely close the notify socket in a
3821 app
= find_app_by_notify_sock(sock
);
3826 iter
.iter
.node
= &app
->notify_sock_n
.node
;
3829 * Whatever happens here either we fail or succeed, in both cases we have
3830 * to close the socket after a grace period to continue to the call RCU
3831 * here. If the deletion is successful, the application is not visible
3832 * anymore by other threads and is it fails it means that it was already
3833 * deleted from the hash table so either way we just have to close the
3836 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3842 * Close socket after a grace period to avoid for the socket to be reused
3843 * before the application object is freed creating potential race between
3844 * threads trying to add unique in the global hash table.
3847 call_rcu(&obj
->head
, close_notify_sock_rcu
);