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
);
82 * Match function for the hash table lookup.
84 * It matches an ust app event based on three attributes which are the event
85 * name, the filter bytecode and the loglevel.
87 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
89 struct ust_app_event
*event
;
90 const struct ust_app_ht_key
*key
;
95 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
98 /* Match the 3 elements of the key: name, filter and loglevel. */
101 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
105 /* Event loglevel. */
106 if (event
->attr
.loglevel
!= key
->loglevel
) {
107 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
108 && key
->loglevel
== 0 && event
->attr
.loglevel
== -1) {
110 * Match is accepted. This is because on event creation, the
111 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
112 * -1 are accepted for this loglevel type since 0 is the one set by
113 * the API when receiving an enable event.
120 /* One of the filters is NULL, fail. */
121 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
125 if (key
->filter
&& event
->filter
) {
126 /* Both filters exists, check length followed by the bytecode. */
127 if (event
->filter
->len
!= key
->filter
->len
||
128 memcmp(event
->filter
->data
, key
->filter
->data
,
129 event
->filter
->len
) != 0) {
142 * Unique add of an ust app event in the given ht. This uses the custom
143 * ht_match_ust_app_event match function and the event name as hash.
145 static void add_unique_ust_app_event(struct lttng_ht
*ht
,
146 struct ust_app_event
*event
)
148 struct cds_lfht_node
*node_ptr
;
149 struct ust_app_ht_key key
;
155 key
.name
= event
->attr
.name
;
156 key
.filter
= event
->filter
;
157 key
.loglevel
= event
->attr
.loglevel
;
159 node_ptr
= cds_lfht_add_unique(ht
->ht
,
160 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
161 ht_match_ust_app_event
, &key
, &event
->node
.node
);
162 assert(node_ptr
== &event
->node
.node
);
166 * Delete ust context safely. RCU read lock must be held before calling
170 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
177 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
178 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
179 ERR("UST app sock %d release context obj failed with ret %d",
188 * Delete ust app event safely. RCU read lock must be held before calling
192 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
198 free(ua_event
->filter
);
200 if (ua_event
->obj
!= NULL
) {
201 ret
= ustctl_release_object(sock
, ua_event
->obj
);
202 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
203 ERR("UST app sock %d release event obj failed with ret %d",
212 * Delete ust app stream safely. RCU read lock must be held before calling
216 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
223 ret
= ustctl_release_object(sock
, stream
->obj
);
224 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
225 ERR("UST app sock %d release stream obj failed with ret %d",
228 lttng_fd_put(LTTNG_FD_APPS
, 2);
235 * Delete ust app channel safely. RCU read lock must be held before calling
239 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
242 struct lttng_ht_iter iter
;
243 struct ust_app_event
*ua_event
;
244 struct ust_app_ctx
*ua_ctx
;
245 struct ust_app_stream
*stream
, *stmp
;
249 DBG3("UST app deleting channel %s", ua_chan
->name
);
252 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
253 cds_list_del(&stream
->list
);
254 delete_ust_app_stream(sock
, stream
);
258 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
259 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
261 delete_ust_app_ctx(sock
, ua_ctx
);
263 lttng_ht_destroy(ua_chan
->ctx
);
266 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
268 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
270 delete_ust_app_event(sock
, ua_event
);
272 lttng_ht_destroy(ua_chan
->events
);
274 if (ua_chan
->obj
!= NULL
) {
275 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
276 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
277 ERR("UST app sock %d release channel obj failed with ret %d",
280 lttng_fd_put(LTTNG_FD_APPS
, 2);
287 * Delete ust app session safely. RCU read lock must be held before calling
291 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
)
294 struct lttng_ht_iter iter
;
295 struct ust_app_channel
*ua_chan
;
297 if (ua_sess
->metadata
) {
298 delete_ust_app_channel(sock
, ua_sess
->metadata
);
301 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
303 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
305 delete_ust_app_channel(sock
, ua_chan
);
307 lttng_ht_destroy(ua_sess
->channels
);
309 if (ua_sess
->handle
!= -1) {
310 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
311 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
312 ERR("UST app sock %d release session handle failed with ret %d",
320 * Delete a traceable application structure from the global list. Never call
321 * this function outside of a call_rcu call.
324 void delete_ust_app(struct ust_app
*app
)
327 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
331 /* Delete ust app sessions info */
335 lttng_ht_destroy(app
->sessions
);
338 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
340 /* Free every object in the session and the session. */
341 delete_ust_app_session(sock
, ua_sess
);
345 * Wait until we have deleted the application from the sock hash table
346 * before closing this socket, otherwise an application could re-use the
347 * socket ID and race with the teardown, using the same hash table entry.
349 * It's OK to leave the close in call_rcu. We want it to stay unique for
350 * all RCU readers that could run concurrently with unregister app,
351 * therefore we _need_ to only close that socket after a grace period. So
352 * it should stay in this RCU callback.
354 * This close() is a very important step of the synchronization model so
355 * every modification to this function must be carefully reviewed.
361 lttng_fd_put(LTTNG_FD_APPS
, 1);
363 DBG2("UST app pid %d deleted", app
->pid
);
370 * URCU intermediate call to delete an UST app.
373 void delete_ust_app_rcu(struct rcu_head
*head
)
375 struct lttng_ht_node_ulong
*node
=
376 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
377 struct ust_app
*app
=
378 caa_container_of(node
, struct ust_app
, pid_n
);
380 DBG3("Call RCU deleting app PID %d", app
->pid
);
385 * Delete the session from the application ht and delete the data structure by
386 * freeing every object inside and releasing them.
388 static void destroy_session(struct ust_app
*app
,
389 struct ust_app_session
*ua_sess
)
392 struct lttng_ht_iter iter
;
397 iter
.iter
.node
= &ua_sess
->node
.node
;
398 ret
= lttng_ht_del(app
->sessions
, &iter
);
400 /* Already scheduled for teardown. */
404 /* Once deleted, free the data structure. */
405 delete_ust_app_session(app
->sock
, ua_sess
);
412 * Alloc new UST app session.
415 struct ust_app_session
*alloc_ust_app_session(void)
417 struct ust_app_session
*ua_sess
;
419 /* Init most of the default value by allocating and zeroing */
420 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
421 if (ua_sess
== NULL
) {
426 ua_sess
->handle
= -1;
427 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
429 if ((lttng_uuid_generate(ua_sess
->uuid
))) {
430 ERR("Failed to generate UST uuid");
443 * Alloc new UST app channel.
446 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
447 struct lttng_ust_channel_attr
*attr
)
449 struct ust_app_channel
*ua_chan
;
451 /* Init most of the default value by allocating and zeroing */
452 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
453 if (ua_chan
== NULL
) {
458 /* Setup channel name */
459 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
460 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
462 ua_chan
->enabled
= 1;
463 ua_chan
->handle
= -1;
464 ua_chan
->key
= get_next_channel_key();
465 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
466 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
467 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
469 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
471 /* Copy attributes */
473 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
474 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
475 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
476 ua_chan
->attr
.overwrite
= attr
->overwrite
;
477 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
478 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
479 ua_chan
->attr
.output
= attr
->output
;
481 /* By default, the channel is a per cpu channel. */
482 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
484 DBG3("UST app channel %s allocated", ua_chan
->name
);
493 * Allocate and initialize a UST app stream.
495 * Return newly allocated stream pointer or NULL on error.
497 struct ust_app_stream
*ust_app_alloc_stream(void)
499 struct ust_app_stream
*stream
= NULL
;
501 stream
= zmalloc(sizeof(*stream
));
502 if (stream
== NULL
) {
503 PERROR("zmalloc ust app stream");
507 /* Zero could be a valid value for a handle so flag it to -1. */
515 * Alloc new UST app event.
518 struct ust_app_event
*alloc_ust_app_event(char *name
,
519 struct lttng_ust_event
*attr
)
521 struct ust_app_event
*ua_event
;
523 /* Init most of the default value by allocating and zeroing */
524 ua_event
= zmalloc(sizeof(struct ust_app_event
));
525 if (ua_event
== NULL
) {
530 ua_event
->enabled
= 1;
531 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
532 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
533 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
535 /* Copy attributes */
537 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
540 DBG3("UST app event %s allocated", ua_event
->name
);
549 * Alloc new UST app context.
552 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
554 struct ust_app_ctx
*ua_ctx
;
556 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
557 if (ua_ctx
== NULL
) {
562 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
565 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
572 * Allocate a filter and copy the given original filter.
574 * Return allocated filter or NULL on error.
576 static struct lttng_ust_filter_bytecode
*alloc_copy_ust_app_filter(
577 struct lttng_ust_filter_bytecode
*orig_f
)
579 struct lttng_ust_filter_bytecode
*filter
= NULL
;
581 /* Copy filter bytecode */
582 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
584 PERROR("zmalloc alloc ust app filter");
588 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
595 * Find an ust_app using the sock and return it. RCU read side lock must be
596 * held before calling this helper function.
599 struct ust_app
*find_app_by_sock(int sock
)
601 struct lttng_ht_node_ulong
*node
;
602 struct lttng_ht_iter iter
;
604 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
605 node
= lttng_ht_iter_get_node_ulong(&iter
);
607 DBG2("UST app find by sock %d not found", sock
);
611 return caa_container_of(node
, struct ust_app
, sock_n
);
618 * Lookup for an ust app event based on event name, filter bytecode and the
621 * Return an ust_app_event object or NULL on error.
623 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
624 char *name
, struct lttng_ust_filter_bytecode
*filter
, int loglevel
)
626 struct lttng_ht_iter iter
;
627 struct lttng_ht_node_str
*node
;
628 struct ust_app_event
*event
= NULL
;
629 struct ust_app_ht_key key
;
634 /* Setup key for event lookup. */
637 key
.loglevel
= loglevel
;
639 /* Lookup using the event name as hash and a custom match fct. */
640 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
641 ht_match_ust_app_event
, &key
, &iter
.iter
);
642 node
= lttng_ht_iter_get_node_str(&iter
);
647 event
= caa_container_of(node
, struct ust_app_event
, node
);
654 * Create the channel context on the tracer.
657 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
658 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
662 health_code_update();
664 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
665 ua_chan
->obj
, &ua_ctx
->obj
);
667 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
668 ERR("UST app create channel context failed for app (pid: %d) "
669 "with ret %d", app
->pid
, ret
);
671 DBG3("UST app disable event failed. Application is dead.");
676 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
678 DBG2("UST app context created successfully for channel %s", ua_chan
->name
);
681 health_code_update();
686 * Set the filter on the tracer.
689 int set_ust_event_filter(struct ust_app_event
*ua_event
,
694 health_code_update();
696 if (!ua_event
->filter
) {
701 ret
= ustctl_set_filter(app
->sock
, ua_event
->filter
,
704 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
705 ERR("UST app event %s filter failed for app (pid: %d) "
706 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
708 DBG3("UST app filter event failed. Application is dead.");
713 DBG2("UST filter set successfully for event %s", ua_event
->name
);
716 health_code_update();
721 * Disable the specified event on to UST tracer for the UST session.
723 static int disable_ust_event(struct ust_app
*app
,
724 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
728 health_code_update();
730 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
732 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
733 ERR("UST app event %s disable failed for app (pid: %d) "
734 "and session handle %d with ret %d",
735 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
737 DBG3("UST app disable event failed. Application is dead.");
742 DBG2("UST app event %s disabled successfully for app (pid: %d)",
743 ua_event
->attr
.name
, app
->pid
);
746 health_code_update();
751 * Disable the specified channel on to UST tracer for the UST session.
753 static int disable_ust_channel(struct ust_app
*app
,
754 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
758 health_code_update();
760 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
762 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
763 ERR("UST app channel %s disable failed for app (pid: %d) "
764 "and session handle %d with ret %d",
765 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
767 DBG3("UST app disable channel failed. Application is dead.");
772 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
773 ua_chan
->name
, app
->pid
);
776 health_code_update();
781 * Enable the specified channel on to UST tracer for the UST session.
783 static int enable_ust_channel(struct ust_app
*app
,
784 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
788 health_code_update();
790 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
792 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
793 ERR("UST app channel %s enable failed for app (pid: %d) "
794 "and session handle %d with ret %d",
795 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
797 DBG3("UST app enable channel failed. Application is dead.");
802 ua_chan
->enabled
= 1;
804 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
805 ua_chan
->name
, app
->pid
);
808 health_code_update();
813 * Enable the specified event on to UST tracer for the UST session.
815 static int enable_ust_event(struct ust_app
*app
,
816 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
820 health_code_update();
822 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
824 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
825 ERR("UST app event %s enable failed for app (pid: %d) "
826 "and session handle %d with ret %d",
827 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
829 DBG3("UST app enable event failed. Application is dead.");
834 DBG2("UST app event %s enabled successfully for app (pid: %d)",
835 ua_event
->attr
.name
, app
->pid
);
838 health_code_update();
843 * Create the specified channel onto the UST tracer for a UST session.
845 * Return 0 on success. On error, a negative value is returned.
847 static int create_ust_channel(struct ust_app
*app
,
848 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
849 struct consumer_output
*consumer
)
852 unsigned int nb_fd
= 0;
853 struct consumer_socket
*socket
;
854 struct ust_app_stream
*stream
, *stmp
;
861 health_code_update();
863 /* Get the right consumer socket for the application. */
864 socket
= find_consumer_socket_by_bitness(app
->bits_per_long
, consumer
);
870 health_code_update();
873 * Ask consumer to create channel. The consumer will return the number of
874 * stream we have to expect.
876 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, consumer
, socket
);
882 * Compute the number of fd needed before receiving them. It must be 2 per
885 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
887 /* Reserve the amount of file descriptor we need. */
888 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
890 ERR("Exhausted number of available FD upon create channel");
894 health_code_update();
897 * Now get the channel from the consumer. This call wil populate the stream
898 * list of that channel and set the ust object.
900 ret
= ust_consumer_get_channel(socket
, ua_chan
);
905 /* Send channel to the application. */
906 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
911 /* Send all streams to application. */
912 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
913 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
917 /* We don't need the stream anymore once sent to the tracer. */
918 cds_list_del(&stream
->list
);
919 delete_ust_app_stream(-1, stream
);
922 /* Flag the channel that it is sent to the application. */
923 ua_chan
->is_sent
= 1;
925 health_code_update();
927 /* If channel is not enabled, disable it on the tracer */
928 if (!ua_chan
->enabled
) {
929 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
938 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
941 * Initiate a destroy channel on the consumer since we had an error
942 * handling it on our side. The return value is of no importance since we
943 * already have a ret value set by the previous error that we need to
946 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
948 health_code_update();
953 * Create the specified event onto the UST tracer for a UST session.
956 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
957 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
961 health_code_update();
963 /* Create UST event on tracer */
964 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
967 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
968 ERR("Error ustctl create event %s for app pid: %d with ret %d",
969 ua_event
->attr
.name
, app
->pid
, ret
);
971 DBG3("UST app create event failed. Application is dead.");
976 ua_event
->handle
= ua_event
->obj
->handle
;
978 DBG2("UST app event %s created successfully for pid:%d",
979 ua_event
->attr
.name
, app
->pid
);
981 health_code_update();
983 /* Set filter if one is present. */
984 if (ua_event
->filter
) {
985 ret
= set_ust_event_filter(ua_event
, app
);
991 /* If event not enabled, disable it on the tracer */
992 if (ua_event
->enabled
== 0) {
993 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
996 * If we hit an EPERM, something is wrong with our disable call. If
997 * we get an EEXIST, there is a problem on the tracer side since we
1001 case -LTTNG_UST_ERR_PERM
:
1002 /* Code flow problem */
1004 case -LTTNG_UST_ERR_EXIST
:
1005 /* It's OK for our use case. */
1016 health_code_update();
1021 * Copy data between an UST app event and a LTT event.
1023 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1024 struct ltt_ust_event
*uevent
)
1026 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1027 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1029 ua_event
->enabled
= uevent
->enabled
;
1031 /* Copy event attributes */
1032 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1034 /* Copy filter bytecode */
1035 if (uevent
->filter
) {
1036 ua_event
->filter
= alloc_copy_ust_app_filter(uevent
->filter
);
1037 /* Filter might be NULL here in case of ENONEM. */
1042 * Copy data between an UST app channel and a LTT channel.
1044 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1045 struct ltt_ust_channel
*uchan
)
1047 struct lttng_ht_iter iter
;
1048 struct ltt_ust_event
*uevent
;
1049 struct ltt_ust_context
*uctx
;
1050 struct ust_app_event
*ua_event
;
1051 struct ust_app_ctx
*ua_ctx
;
1053 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1055 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1056 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1058 /* Copy event attributes since the layout is different. */
1059 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1060 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1061 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1062 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1063 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1064 ua_chan
->attr
.output
= uchan
->attr
.output
;
1066 * Note that the attribute channel type is not set since the channel on the
1067 * tracing registry side does not have this information.
1070 ua_chan
->enabled
= uchan
->enabled
;
1072 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
1073 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1074 if (ua_ctx
== NULL
) {
1077 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1078 (unsigned long) ua_ctx
->ctx
.ctx
);
1079 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1082 /* Copy all events from ltt ust channel to ust app channel */
1083 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1084 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1085 uevent
->filter
, uevent
->attr
.loglevel
);
1086 if (ua_event
== NULL
) {
1087 DBG2("UST event %s not found on shadow copy channel",
1089 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1090 if (ua_event
== NULL
) {
1093 shadow_copy_event(ua_event
, uevent
);
1094 add_unique_ust_app_event(ua_chan
->events
, ua_event
);
1098 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1102 * Copy data between a UST app session and a regular LTT session.
1104 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1105 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1107 struct lttng_ht_node_str
*ua_chan_node
;
1108 struct lttng_ht_iter iter
;
1109 struct ltt_ust_channel
*uchan
;
1110 struct ust_app_channel
*ua_chan
;
1112 struct tm
*timeinfo
;
1116 /* Get date and time for unique app path */
1118 timeinfo
= localtime(&rawtime
);
1119 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1121 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1123 ua_sess
->id
= usess
->id
;
1124 ua_sess
->uid
= usess
->uid
;
1125 ua_sess
->gid
= usess
->gid
;
1127 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s-%d-%s/", app
->name
, app
->pid
,
1130 PERROR("asprintf UST shadow copy session");
1131 /* TODO: We cannot return an error from here.. */
1135 /* TODO: support all UST domain */
1137 /* Iterate over all channels in global domain. */
1138 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1140 struct lttng_ht_iter uiter
;
1142 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1143 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1144 if (ua_chan_node
!= NULL
) {
1145 /* Session exist. Contiuing. */
1149 DBG2("Channel %s not found on shadow session copy, creating it",
1151 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1152 if (ua_chan
== NULL
) {
1153 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1156 shadow_copy_channel(ua_chan
, uchan
);
1158 * The concept of metadata channel does not exist on the tracing
1159 * registry side of the session daemon so this can only be a per CPU
1160 * channel and not metadata.
1162 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1164 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1169 * Lookup sesison wrapper.
1172 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1173 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1175 /* Get right UST app session from app */
1176 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
1180 * Return ust app session from the app session hashtable using the UST session
1183 static struct ust_app_session
*lookup_session_by_app(
1184 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1186 struct lttng_ht_iter iter
;
1187 struct lttng_ht_node_ulong
*node
;
1189 __lookup_session_by_app(usess
, app
, &iter
);
1190 node
= lttng_ht_iter_get_node_ulong(&iter
);
1195 return caa_container_of(node
, struct ust_app_session
, node
);
1202 * Create a session on the tracer side for the given app.
1204 * On success, ua_sess_ptr is populated with the session pointer or else left
1205 * untouched. If the session was created, is_created is set to 1. On error,
1206 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1209 * Returns 0 on success or else a negative code which is either -ENOMEM or
1210 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1212 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1213 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1216 int ret
, created
= 0;
1217 struct ust_app_session
*ua_sess
;
1221 assert(ua_sess_ptr
);
1223 health_code_update();
1225 ua_sess
= lookup_session_by_app(usess
, app
);
1226 if (ua_sess
== NULL
) {
1227 DBG2("UST app pid: %d session id %d not found, creating it",
1228 app
->pid
, usess
->id
);
1229 ua_sess
= alloc_ust_app_session();
1230 if (ua_sess
== NULL
) {
1231 /* Only malloc can failed so something is really wrong */
1235 shadow_copy_session(ua_sess
, usess
, app
);
1239 health_code_update();
1241 if (ua_sess
->handle
== -1) {
1242 ret
= ustctl_create_session(app
->sock
);
1244 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1245 ERR("Creating session for app pid %d with ret %d",
1248 DBG("UST app creating session failed. Application is dead");
1250 delete_ust_app_session(-1, ua_sess
);
1251 if (ret
!= -ENOMEM
) {
1253 * Tracer is probably gone or got an internal error so let's
1254 * behave like it will soon unregister or not usable.
1261 ua_sess
->handle
= ret
;
1263 /* Add ust app session to app's HT */
1264 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
1265 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
1267 DBG2("UST app session created successfully with handle %d", ret
);
1270 *ua_sess_ptr
= ua_sess
;
1272 *is_created
= created
;
1274 /* Everything went well. */
1278 health_code_update();
1283 * Create a context for the channel on the tracer.
1286 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
1287 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
1288 struct ust_app
*app
)
1291 struct lttng_ht_iter iter
;
1292 struct lttng_ht_node_ulong
*node
;
1293 struct ust_app_ctx
*ua_ctx
;
1295 DBG2("UST app adding context to channel %s", ua_chan
->name
);
1297 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
1298 node
= lttng_ht_iter_get_node_ulong(&iter
);
1304 ua_ctx
= alloc_ust_app_ctx(uctx
);
1305 if (ua_ctx
== NULL
) {
1311 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
1312 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1314 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
1324 * Enable on the tracer side a ust app event for the session and channel.
1327 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
1328 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1332 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1337 ua_event
->enabled
= 1;
1344 * Disable on the tracer side a ust app event for the session and channel.
1346 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1347 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1351 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1356 ua_event
->enabled
= 0;
1363 * Lookup ust app channel for session and disable it on the tracer side.
1366 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1367 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1371 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1376 ua_chan
->enabled
= 0;
1383 * Lookup ust app channel for session and enable it on the tracer side.
1385 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1386 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1389 struct lttng_ht_iter iter
;
1390 struct lttng_ht_node_str
*ua_chan_node
;
1391 struct ust_app_channel
*ua_chan
;
1393 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1394 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1395 if (ua_chan_node
== NULL
) {
1396 DBG2("Unable to find channel %s in ust session id %u",
1397 uchan
->name
, ua_sess
->id
);
1401 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1403 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1413 * Create UST app channel and create it on the tracer. Set ua_chanp of the
1414 * newly created channel if not NULL.
1416 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
1417 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
1418 struct consumer_output
*consumer
, enum lttng_ust_chan_type type
,
1419 struct ust_app_channel
**ua_chanp
)
1422 struct lttng_ht_iter iter
;
1423 struct lttng_ht_node_str
*ua_chan_node
;
1424 struct ust_app_channel
*ua_chan
;
1426 /* Lookup channel in the ust app session */
1427 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1428 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1429 if (ua_chan_node
!= NULL
) {
1430 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1434 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1435 if (ua_chan
== NULL
) {
1436 /* Only malloc can fail here */
1440 shadow_copy_channel(ua_chan
, uchan
);
1442 /* Set channel type. */
1443 ua_chan
->attr
.type
= type
;
1445 ret
= create_ust_channel(app
, ua_sess
, ua_chan
, consumer
);
1450 /* Only add the channel if successful on the tracer side. */
1451 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1453 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1458 *ua_chanp
= ua_chan
;
1461 /* Everything went well. */
1465 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
);
1470 * Create UST app event and create it on the tracer side.
1473 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1474 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1475 struct ust_app
*app
)
1478 struct ust_app_event
*ua_event
;
1480 /* Get event node */
1481 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1482 uevent
->filter
, uevent
->attr
.loglevel
);
1483 if (ua_event
!= NULL
) {
1488 /* Does not exist so create one */
1489 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1490 if (ua_event
== NULL
) {
1491 /* Only malloc can failed so something is really wrong */
1495 shadow_copy_event(ua_event
, uevent
);
1497 /* Create it on the tracer side */
1498 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1500 /* Not found previously means that it does not exist on the tracer */
1501 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
1505 add_unique_ust_app_event(ua_chan
->events
, ua_event
);
1507 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1514 /* Valid. Calling here is already in a read side lock */
1515 delete_ust_app_event(-1, ua_event
);
1520 * Create UST metadata and open it on the tracer side.
1522 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1523 struct ust_app
*app
, struct consumer_output
*consumer
)
1526 struct ust_app_channel
*metadata
;
1531 if (ua_sess
->metadata
) {
1532 /* Already exist. Return success. */
1536 /* Allocate UST metadata */
1537 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, NULL
);
1539 /* malloc() failed */
1544 /* Set default attributes for metadata. */
1545 metadata
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1546 metadata
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
1547 metadata
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
1548 metadata
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1549 metadata
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1550 metadata
->attr
.output
= LTTNG_UST_MMAP
;
1551 metadata
->attr
.type
= LTTNG_UST_CHAN_METADATA
;
1553 ret
= create_ust_channel(app
, ua_sess
, metadata
, consumer
);
1558 ua_sess
->metadata
= metadata
;
1560 DBG2("UST metadata opened for app pid %d", app
->pid
);
1565 delete_ust_app_channel(metadata
->is_sent
? app
->sock
: -1, metadata
);
1571 * Return pointer to traceable apps list.
1573 struct lttng_ht
*ust_app_get_ht(void)
1579 * Return ust app pointer or NULL if not found.
1581 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1583 struct lttng_ht_node_ulong
*node
;
1584 struct lttng_ht_iter iter
;
1587 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1588 node
= lttng_ht_iter_get_node_ulong(&iter
);
1590 DBG2("UST app no found with pid %d", pid
);
1595 DBG2("Found UST app by pid %d", pid
);
1597 return caa_container_of(node
, struct ust_app
, pid_n
);
1605 * Using pid and uid (of the app), allocate a new ust_app struct and
1606 * add it to the global traceable app list.
1608 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1609 * bitness is not supported.
1611 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1613 struct ust_app
*lta
;
1616 if ((msg
->bits_per_long
== 64 &&
1617 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
1618 || (msg
->bits_per_long
== 32 &&
1619 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
1620 ERR("Registration failed: application \"%s\" (pid: %d) has "
1621 "%d-bit long, but no consumerd for this long size is available.\n",
1622 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1627 lttng_fd_put(LTTNG_FD_APPS
, 1);
1630 if (msg
->major
!= LTTNG_UST_COMM_MAJOR
) {
1631 ERR("Registration failed: application \"%s\" (pid: %d) has "
1632 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1633 msg
->name
, msg
->pid
, msg
->major
, msg
->minor
);
1638 lttng_fd_put(LTTNG_FD_APPS
, 1);
1641 lta
= zmalloc(sizeof(struct ust_app
));
1647 lta
->ppid
= msg
->ppid
;
1648 lta
->uid
= msg
->uid
;
1649 lta
->gid
= msg
->gid
;
1650 lta
->compatible
= 0; /* Not compatible until proven */
1651 lta
->bits_per_long
= msg
->bits_per_long
;
1652 lta
->v_major
= msg
->major
;
1653 lta
->v_minor
= msg
->minor
;
1654 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1655 lta
->name
[16] = '\0';
1656 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1658 lta
->pid
= msg
->pid
;
1659 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long)lta
->pid
);
1661 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long)lta
->sock
);
1663 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
1668 * On a re-registration, we want to kick out the previous registration of
1671 lttng_ht_add_replace_ulong(ust_app_ht
, <a
->pid_n
);
1674 * The socket _should_ be unique until _we_ call close. So, a add_unique
1675 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1676 * already in the table.
1678 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, <a
->sock_n
);
1682 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1683 " (version %d.%d)", lta
->pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1684 lta
->sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1690 * Unregister app by removing it from the global traceable app list and freeing
1693 * The socket is already closed at this point so no close to sock.
1695 void ust_app_unregister(int sock
)
1697 struct ust_app
*lta
;
1698 struct lttng_ht_node_ulong
*node
;
1699 struct lttng_ht_iter iter
;
1700 struct ust_app_session
*ua_sess
;
1705 /* Get the node reference for a call_rcu */
1706 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1707 node
= lttng_ht_iter_get_node_ulong(&iter
);
1709 ERR("Unable to find app by sock %d", sock
);
1713 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
1715 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
1717 /* Remove application from PID hash table */
1718 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
1721 /* Assign second node for deletion */
1722 iter
.iter
.node
= <a
->pid_n
.node
;
1725 * Ignore return value since the node might have been removed before by an
1726 * add replace during app registration because the PID can be reassigned by
1729 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1731 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
1735 /* Remove sessions so they are not visible during deletion.*/
1736 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
1738 ret
= lttng_ht_del(lta
->sessions
, &iter
);
1740 /* The session was already removed so scheduled for teardown. */
1745 * Add session to list for teardown. This is safe since at this point we
1746 * are the only one using this list.
1748 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
1752 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
1760 * Return traceable_app_count
1762 unsigned long ust_app_list_count(void)
1764 unsigned long count
;
1767 count
= lttng_ht_get_count(ust_app_ht
);
1774 * Fill events array with all events name of all registered apps.
1776 int ust_app_list_events(struct lttng_event
**events
)
1779 size_t nbmem
, count
= 0;
1780 struct lttng_ht_iter iter
;
1781 struct ust_app
*app
;
1782 struct lttng_event
*tmp_event
;
1784 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1785 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1786 if (tmp_event
== NULL
) {
1787 PERROR("zmalloc ust app events");
1794 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1795 struct lttng_ust_tracepoint_iter uiter
;
1797 health_code_update();
1799 if (!app
->compatible
) {
1801 * TODO: In time, we should notice the caller of this error by
1802 * telling him that this is a version error.
1806 handle
= ustctl_tracepoint_list(app
->sock
);
1808 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
1809 ERR("UST app list events getting handle failed for app pid %d",
1815 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
1816 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
1817 /* Handle ustctl error. */
1820 if (ret
!= -LTTNG_UST_ERR_EXITING
|| ret
!= -EPIPE
) {
1821 ERR("UST app tp list get failed for app %d with ret %d",
1824 DBG3("UST app tp list get failed. Application is dead");
1829 health_code_update();
1830 if (count
>= nbmem
) {
1831 /* In case the realloc fails, we free the memory */
1834 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1837 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event
));
1839 PERROR("realloc ust app events");
1846 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1847 tmp_event
[count
].loglevel
= uiter
.loglevel
;
1848 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
1849 tmp_event
[count
].pid
= app
->pid
;
1850 tmp_event
[count
].enabled
= -1;
1856 *events
= tmp_event
;
1858 DBG2("UST app list events done (%zu events)", count
);
1863 health_code_update();
1868 * Fill events array with all events name of all registered apps.
1870 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
1873 size_t nbmem
, count
= 0;
1874 struct lttng_ht_iter iter
;
1875 struct ust_app
*app
;
1876 struct lttng_event_field
*tmp_event
;
1878 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1879 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
1880 if (tmp_event
== NULL
) {
1881 PERROR("zmalloc ust app event fields");
1888 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1889 struct lttng_ust_field_iter uiter
;
1891 health_code_update();
1893 if (!app
->compatible
) {
1895 * TODO: In time, we should notice the caller of this error by
1896 * telling him that this is a version error.
1900 handle
= ustctl_tracepoint_field_list(app
->sock
);
1902 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
1903 ERR("UST app list field getting handle failed for app pid %d",
1909 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
1910 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
1911 /* Handle ustctl error. */
1914 if (ret
!= -LTTNG_UST_ERR_EXITING
|| ret
!= -EPIPE
) {
1915 ERR("UST app tp list field failed for app %d with ret %d",
1918 DBG3("UST app tp list field failed. Application is dead");
1923 health_code_update();
1924 if (count
>= nbmem
) {
1925 /* In case the realloc fails, we free the memory */
1928 DBG2("Reallocating event field list from %zu to %zu entries", nbmem
,
1931 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event_field
));
1933 PERROR("realloc ust app event fields");
1941 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
1942 tmp_event
[count
].type
= uiter
.type
;
1943 tmp_event
[count
].nowrite
= uiter
.nowrite
;
1945 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1946 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
1947 tmp_event
[count
].event
.type
= LTTNG_UST_TRACEPOINT
;
1948 tmp_event
[count
].event
.pid
= app
->pid
;
1949 tmp_event
[count
].event
.enabled
= -1;
1955 *fields
= tmp_event
;
1957 DBG2("UST app list event fields done (%zu events)", count
);
1962 health_code_update();
1967 * Free and clean all traceable apps of the global list.
1969 void ust_app_clean_list(void)
1972 struct ust_app
*app
;
1973 struct lttng_ht_iter iter
;
1975 DBG2("UST app cleaning registered apps hash table");
1979 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1980 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1982 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
1985 /* Cleanup socket hash table */
1986 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
1988 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
1992 /* Destroy is done only when the ht is empty */
1993 lttng_ht_destroy(ust_app_ht
);
1994 lttng_ht_destroy(ust_app_ht_by_sock
);
2000 * Init UST app hash table.
2002 void ust_app_ht_alloc(void)
2004 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2005 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2009 * For a specific UST session, disable the channel for all registered apps.
2011 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
2012 struct ltt_ust_channel
*uchan
)
2015 struct lttng_ht_iter iter
;
2016 struct lttng_ht_node_str
*ua_chan_node
;
2017 struct ust_app
*app
;
2018 struct ust_app_session
*ua_sess
;
2019 struct ust_app_channel
*ua_chan
;
2021 if (usess
== NULL
|| uchan
== NULL
) {
2022 ERR("Disabling UST global channel with NULL values");
2027 DBG2("UST app disabling channel %s from global domain for session id %d",
2028 uchan
->name
, usess
->id
);
2032 /* For every registered applications */
2033 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2034 struct lttng_ht_iter uiter
;
2035 if (!app
->compatible
) {
2037 * TODO: In time, we should notice the caller of this error by
2038 * telling him that this is a version error.
2042 ua_sess
= lookup_session_by_app(usess
, app
);
2043 if (ua_sess
== NULL
) {
2048 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2049 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2050 /* If the session if found for the app, the channel must be there */
2051 assert(ua_chan_node
);
2053 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2054 /* The channel must not be already disabled */
2055 assert(ua_chan
->enabled
== 1);
2057 /* Disable channel onto application */
2058 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
2060 /* XXX: We might want to report this error at some point... */
2072 * For a specific UST session, enable the channel for all registered apps.
2074 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
2075 struct ltt_ust_channel
*uchan
)
2078 struct lttng_ht_iter iter
;
2079 struct ust_app
*app
;
2080 struct ust_app_session
*ua_sess
;
2082 if (usess
== NULL
|| uchan
== NULL
) {
2083 ERR("Adding UST global channel to NULL values");
2088 DBG2("UST app enabling channel %s to global domain for session id %d",
2089 uchan
->name
, usess
->id
);
2093 /* For every registered applications */
2094 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2095 if (!app
->compatible
) {
2097 * TODO: In time, we should notice the caller of this error by
2098 * telling him that this is a version error.
2102 ua_sess
= lookup_session_by_app(usess
, app
);
2103 if (ua_sess
== NULL
) {
2107 /* Enable channel onto application */
2108 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
2110 /* XXX: We might want to report this error at some point... */
2122 * Disable an event in a channel and for a specific session.
2124 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
2125 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2128 struct lttng_ht_iter iter
, uiter
;
2129 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2130 struct ust_app
*app
;
2131 struct ust_app_session
*ua_sess
;
2132 struct ust_app_channel
*ua_chan
;
2133 struct ust_app_event
*ua_event
;
2135 DBG("UST app disabling event %s for all apps in channel "
2136 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
2140 /* For all registered applications */
2141 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2142 if (!app
->compatible
) {
2144 * TODO: In time, we should notice the caller of this error by
2145 * telling him that this is a version error.
2149 ua_sess
= lookup_session_by_app(usess
, app
);
2150 if (ua_sess
== NULL
) {
2155 /* Lookup channel in the ust app session */
2156 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2157 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2158 if (ua_chan_node
== NULL
) {
2159 DBG2("Channel %s not found in session id %d for app pid %d."
2160 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
2163 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2165 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2166 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2167 if (ua_event_node
== NULL
) {
2168 DBG2("Event %s not found in channel %s for app pid %d."
2169 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
2172 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2174 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2176 /* XXX: Report error someday... */
2187 * For a specific UST session and UST channel, the event for all
2190 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
2191 struct ltt_ust_channel
*uchan
)
2194 struct lttng_ht_iter iter
, uiter
;
2195 struct lttng_ht_node_str
*ua_chan_node
;
2196 struct ust_app
*app
;
2197 struct ust_app_session
*ua_sess
;
2198 struct ust_app_channel
*ua_chan
;
2199 struct ust_app_event
*ua_event
;
2201 DBG("UST app disabling all event for all apps in channel "
2202 "%s for session id %d", uchan
->name
, usess
->id
);
2206 /* For all registered applications */
2207 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2208 if (!app
->compatible
) {
2210 * TODO: In time, we should notice the caller of this error by
2211 * telling him that this is a version error.
2215 ua_sess
= lookup_session_by_app(usess
, app
);
2217 /* The application has problem or is probably dead. */
2221 /* Lookup channel in the ust app session */
2222 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2223 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2224 /* If the channel is not found, there is a code flow error */
2225 assert(ua_chan_node
);
2227 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2229 /* Disable each events of channel */
2230 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2232 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2234 /* XXX: Report error someday... */
2246 * For a specific UST session, create the channel for all registered apps.
2248 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
2249 struct ltt_ust_channel
*uchan
)
2251 int ret
= 0, created
;
2252 struct lttng_ht_iter iter
;
2253 struct ust_app
*app
;
2254 struct ust_app_session
*ua_sess
= NULL
;
2256 /* Very wrong code flow */
2260 DBG2("UST app adding channel %s to global domain for session id %d",
2261 uchan
->name
, usess
->id
);
2265 /* For every registered applications */
2266 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2267 if (!app
->compatible
) {
2269 * TODO: In time, we should notice the caller of this error by
2270 * telling him that this is a version error.
2275 * Create session on the tracer side and add it to app session HT. Note
2276 * that if session exist, it will simply return a pointer to the ust
2279 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
2284 * The application's socket is not valid. Either a bad socket
2285 * or a timeout on it. We can't inform the caller that for a
2286 * specific app, the session failed so lets continue here.
2291 goto error_rcu_unlock
;
2296 /* Create channel onto application. We don't need the chan ref. */
2297 ret
= create_ust_app_channel(ua_sess
, uchan
, app
, usess
->consumer
,
2298 LTTNG_UST_CHAN_PER_CPU
, NULL
);
2300 if (ret
== -ENOMEM
) {
2301 /* No more memory is a fatal error. Stop right now. */
2302 goto error_rcu_unlock
;
2304 /* Cleanup the created session if it's the case. */
2306 destroy_session(app
, ua_sess
);
2317 * Enable event for a specific session and channel on the tracer.
2319 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
2320 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2323 struct lttng_ht_iter iter
, uiter
;
2324 struct lttng_ht_node_str
*ua_chan_node
;
2325 struct ust_app
*app
;
2326 struct ust_app_session
*ua_sess
;
2327 struct ust_app_channel
*ua_chan
;
2328 struct ust_app_event
*ua_event
;
2330 DBG("UST app enabling event %s for all apps for session id %d",
2331 uevent
->attr
.name
, usess
->id
);
2334 * NOTE: At this point, this function is called only if the session and
2335 * channel passed are already created for all apps. and enabled on the
2341 /* For all registered applications */
2342 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2343 if (!app
->compatible
) {
2345 * TODO: In time, we should notice the caller of this error by
2346 * telling him that this is a version error.
2350 ua_sess
= lookup_session_by_app(usess
, app
);
2352 /* The application has problem or is probably dead. */
2356 /* Lookup channel in the ust app session */
2357 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2358 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2359 /* If the channel is not found, there is a code flow error */
2360 assert(ua_chan_node
);
2362 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2364 /* Get event node */
2365 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2366 uevent
->filter
, uevent
->attr
.loglevel
);
2367 if (ua_event
== NULL
) {
2368 DBG3("UST app enable event %s not found for app PID %d."
2369 "Skipping app", uevent
->attr
.name
, app
->pid
);
2373 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2385 * For a specific existing UST session and UST channel, creates the event for
2386 * all registered apps.
2388 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
2389 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2392 struct lttng_ht_iter iter
, uiter
;
2393 struct lttng_ht_node_str
*ua_chan_node
;
2394 struct ust_app
*app
;
2395 struct ust_app_session
*ua_sess
;
2396 struct ust_app_channel
*ua_chan
;
2398 DBG("UST app creating event %s for all apps for session id %d",
2399 uevent
->attr
.name
, usess
->id
);
2403 /* For all registered applications */
2404 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2405 if (!app
->compatible
) {
2407 * TODO: In time, we should notice the caller of this error by
2408 * telling him that this is a version error.
2412 ua_sess
= lookup_session_by_app(usess
, app
);
2414 /* The application has problem or is probably dead. */
2418 /* Lookup channel in the ust app session */
2419 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2420 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2421 /* If the channel is not found, there is a code flow error */
2422 assert(ua_chan_node
);
2424 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2426 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2428 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
2429 /* Possible value at this point: -ENOMEM. If so, we stop! */
2432 DBG2("UST app event %s already exist on app PID %d",
2433 uevent
->attr
.name
, app
->pid
);
2444 * Start tracing for a specific UST session and app.
2446 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2449 struct ust_app_session
*ua_sess
;
2451 DBG("Starting tracing for ust app pid %d", app
->pid
);
2455 if (!app
->compatible
) {
2459 ua_sess
= lookup_session_by_app(usess
, app
);
2460 if (ua_sess
== NULL
) {
2461 /* The session is in teardown process. Ignore and continue. */
2465 /* Upon restart, we skip the setup, already done */
2466 if (ua_sess
->started
) {
2470 /* Create directories if consumer is LOCAL and has a path defined. */
2471 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2472 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
2473 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
2474 S_IRWXU
| S_IRWXG
, usess
->uid
, usess
->gid
);
2476 if (ret
!= -EEXIST
) {
2477 ERR("Trace directory creation error");
2478 goto error_rcu_unlock
;
2483 /* Create the metadata for the application. */
2484 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
2486 goto error_rcu_unlock
;
2489 health_code_update();
2492 /* This start the UST tracing */
2493 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
2495 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2496 ERR("Error starting tracing for app pid: %d (ret: %d)",
2499 DBG("UST app start session failed. Application is dead.");
2501 goto error_rcu_unlock
;
2504 /* Indicate that the session has been started once */
2505 ua_sess
->started
= 1;
2507 health_code_update();
2509 /* Quiescent wait after starting trace */
2510 ret
= ustctl_wait_quiescent(app
->sock
);
2511 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2512 ERR("UST app wait quiescent failed for app pid %d ret %d",
2518 health_code_update();
2523 health_code_update();
2528 * Stop tracing for a specific UST session and app.
2530 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2533 struct lttng_ht_iter iter
;
2534 struct ust_app_session
*ua_sess
;
2535 struct ust_app_channel
*ua_chan
;
2537 DBG("Stopping tracing for ust app pid %d", app
->pid
);
2541 if (!app
->compatible
) {
2545 ua_sess
= lookup_session_by_app(usess
, app
);
2546 if (ua_sess
== NULL
) {
2551 * If started = 0, it means that stop trace has been called for a session
2552 * that was never started. It's possible since we can have a fail start
2553 * from either the application manager thread or the command thread. Simply
2554 * indicate that this is a stop error.
2556 if (!ua_sess
->started
) {
2557 goto error_rcu_unlock
;
2560 health_code_update();
2562 /* This inhibits UST tracing */
2563 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
2565 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2566 ERR("Error stopping tracing for app pid: %d (ret: %d)",
2569 DBG("UST app stop session failed. Application is dead.");
2571 goto error_rcu_unlock
;
2574 health_code_update();
2576 /* Quiescent wait after stopping trace */
2577 ret
= ustctl_wait_quiescent(app
->sock
);
2578 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2579 ERR("UST app wait quiescent failed for app pid %d ret %d",
2583 health_code_update();
2585 /* Flushing buffers */
2586 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2588 health_code_update();
2589 assert(ua_chan
->is_sent
);
2590 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_chan
->obj
);
2592 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2593 ERR("UST app PID %d channel %s flush failed with ret %d",
2594 app
->pid
, ua_chan
->name
, ret
);
2596 DBG3("UST app failed to flush %s. Application is dead.",
2598 /* No need to continue. */
2601 /* Continuing flushing all buffers */
2606 health_code_update();
2608 assert(ua_sess
->metadata
->is_sent
);
2609 /* Flush all buffers before stopping */
2610 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_sess
->metadata
->obj
);
2612 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2613 ERR("UST app PID %d metadata flush failed with ret %d", app
->pid
,
2615 goto error_rcu_unlock
;
2617 DBG3("UST app failed to flush metadata. Application is dead.");
2623 health_code_update();
2628 health_code_update();
2633 * Destroy a specific UST session in apps.
2635 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2638 struct ust_app_session
*ua_sess
;
2639 struct lttng_ht_iter iter
;
2640 struct lttng_ht_node_ulong
*node
;
2642 DBG("Destroy tracing for ust app pid %d", app
->pid
);
2646 if (!app
->compatible
) {
2650 __lookup_session_by_app(usess
, app
, &iter
);
2651 node
= lttng_ht_iter_get_node_ulong(&iter
);
2653 /* Session is being or is deleted. */
2656 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2658 health_code_update();
2659 destroy_session(app
, ua_sess
);
2661 health_code_update();
2663 /* Quiescent wait after stopping trace */
2664 ret
= ustctl_wait_quiescent(app
->sock
);
2665 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2666 ERR("UST app wait quiescent failed for app pid %d ret %d",
2672 health_code_update();
2677 * Start tracing for the UST session.
2679 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2682 struct lttng_ht_iter iter
;
2683 struct ust_app
*app
;
2685 DBG("Starting all UST traces");
2689 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2690 ret
= ust_app_start_trace(usess
, app
);
2692 /* Continue to next apps even on error */
2703 * Start tracing for the UST session.
2705 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2708 struct lttng_ht_iter iter
;
2709 struct ust_app
*app
;
2711 DBG("Stopping all UST traces");
2715 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2716 ret
= ust_app_stop_trace(usess
, app
);
2718 /* Continue to next apps even on error */
2729 * Destroy app UST session.
2731 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2734 struct lttng_ht_iter iter
;
2735 struct ust_app
*app
;
2737 DBG("Destroy all UST traces");
2741 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2742 ret
= destroy_trace(usess
, app
);
2744 /* Continue to next apps even on error */
2755 * Add channels/events from UST global domain to registered apps at sock.
2757 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2760 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
2761 struct ust_app
*app
;
2762 struct ust_app_session
*ua_sess
= NULL
;
2763 struct ust_app_channel
*ua_chan
;
2764 struct ust_app_event
*ua_event
;
2765 struct ust_app_ctx
*ua_ctx
;
2770 DBG2("UST app global update for app sock %d for session id %d", sock
,
2775 app
= find_app_by_sock(sock
);
2777 ERR("Failed to find app sock %d", sock
);
2781 if (!app
->compatible
) {
2785 ret
= create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
2787 /* Tracer is probably gone or ENOMEM. */
2793 * We can iterate safely here over all UST app session sicne the create ust
2794 * app session above made a shadow copy of the UST global domain from the
2797 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2799 ret
= create_ust_channel(app
, ua_sess
, ua_chan
, usess
->consumer
);
2802 * Stop everything. On error, the application failed, no more file
2803 * descriptor are available or ENOMEM so stopping here is the only
2804 * thing we can do for now.
2809 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
2811 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2818 /* For each events */
2819 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2821 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2828 if (usess
->start_trace
) {
2829 ret
= ust_app_start_trace(usess
, app
);
2834 DBG2("UST trace started for app pid %d", app
->pid
);
2837 /* Everything went well at this point. */
2843 destroy_session(app
, ua_sess
);
2850 * Add context to a specific channel for global UST domain.
2852 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2853 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2856 struct lttng_ht_node_str
*ua_chan_node
;
2857 struct lttng_ht_iter iter
, uiter
;
2858 struct ust_app_channel
*ua_chan
= NULL
;
2859 struct ust_app_session
*ua_sess
;
2860 struct ust_app
*app
;
2864 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2865 if (!app
->compatible
) {
2867 * TODO: In time, we should notice the caller of this error by
2868 * telling him that this is a version error.
2872 ua_sess
= lookup_session_by_app(usess
, app
);
2873 if (ua_sess
== NULL
) {
2877 /* Lookup channel in the ust app session */
2878 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2879 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2880 if (ua_chan_node
== NULL
) {
2883 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2886 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2897 * Enable event for a channel from a UST session for a specific PID.
2899 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2900 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2903 struct lttng_ht_iter iter
;
2904 struct lttng_ht_node_str
*ua_chan_node
;
2905 struct ust_app
*app
;
2906 struct ust_app_session
*ua_sess
;
2907 struct ust_app_channel
*ua_chan
;
2908 struct ust_app_event
*ua_event
;
2910 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2914 app
= ust_app_find_by_pid(pid
);
2916 ERR("UST app enable event per PID %d not found", pid
);
2921 if (!app
->compatible
) {
2926 ua_sess
= lookup_session_by_app(usess
, app
);
2928 /* The application has problem or is probably dead. */
2932 /* Lookup channel in the ust app session */
2933 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2934 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2935 /* If the channel is not found, there is a code flow error */
2936 assert(ua_chan_node
);
2938 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2940 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2941 uevent
->filter
, uevent
->attr
.loglevel
);
2942 if (ua_event
== NULL
) {
2943 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2948 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2960 * Disable event for a channel from a UST session for a specific PID.
2962 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2963 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2966 struct lttng_ht_iter iter
;
2967 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2968 struct ust_app
*app
;
2969 struct ust_app_session
*ua_sess
;
2970 struct ust_app_channel
*ua_chan
;
2971 struct ust_app_event
*ua_event
;
2973 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2977 app
= ust_app_find_by_pid(pid
);
2979 ERR("UST app disable event per PID %d not found", pid
);
2984 if (!app
->compatible
) {
2989 ua_sess
= lookup_session_by_app(usess
, app
);
2991 /* The application has problem or is probably dead. */
2995 /* Lookup channel in the ust app session */
2996 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2997 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2998 if (ua_chan_node
== NULL
) {
2999 /* Channel does not exist, skip disabling */
3002 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3004 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
3005 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
3006 if (ua_event_node
== NULL
) {
3007 /* Event does not exist, skip disabling */
3010 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3012 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3023 * Validate version of UST apps and set the compatible bit.
3025 int ust_app_validate_version(int sock
)
3028 struct ust_app
*app
;
3032 app
= find_app_by_sock(sock
);
3035 health_code_update();
3037 ret
= ustctl_tracer_version(sock
, &app
->version
);
3039 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3040 ERR("UST app tracer version failed for app pid %d", app
->pid
);
3045 /* Validate version */
3046 if (app
->version
.major
!= UST_APP_MAJOR_VERSION
) {
3050 DBG2("UST app PID %d is compatible with internal major version %d "
3051 "(supporting == %d)", app
->pid
, app
->version
.major
,
3052 UST_APP_MAJOR_VERSION
);
3053 app
->compatible
= 1;
3055 health_code_update();
3059 DBG2("UST app PID %d is not compatible with internal major version %d "
3060 "(supporting == %d)", app
->pid
, app
->version
.major
,
3061 UST_APP_MAJOR_VERSION
);
3062 app
->compatible
= 0;
3064 health_code_update();
3069 * Calibrate registered applications.
3071 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
3074 struct lttng_ht_iter iter
;
3075 struct ust_app
*app
;
3079 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3080 if (!app
->compatible
) {
3082 * TODO: In time, we should notice the caller of this error by
3083 * telling him that this is a version error.
3088 health_code_update();
3090 ret
= ustctl_calibrate(app
->sock
, calibrate
);
3094 /* Means that it's not implemented on the tracer side. */
3098 DBG2("Calibrate app PID %d returned with error %d",
3105 DBG("UST app global domain calibration finished");
3109 health_code_update();