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 ust_app_channel
*ua_chan
,
146 struct ust_app_event
*event
)
148 struct cds_lfht_node
*node_ptr
;
149 struct ust_app_ht_key key
;
153 assert(ua_chan
->events
);
156 ht
= ua_chan
->events
;
157 key
.name
= event
->attr
.name
;
158 key
.filter
= event
->filter
;
159 key
.loglevel
= event
->attr
.loglevel
;
161 node_ptr
= cds_lfht_add_unique(ht
->ht
,
162 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
163 ht_match_ust_app_event
, &key
, &event
->node
.node
);
164 assert(node_ptr
== &event
->node
.node
);
168 * Delete ust context safely. RCU read lock must be held before calling
172 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
179 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
180 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
181 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
182 sock
, ua_ctx
->obj
->handle
, ret
);
190 * Delete ust app event safely. RCU read lock must be held before calling
194 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
200 free(ua_event
->filter
);
202 if (ua_event
->obj
!= NULL
) {
203 ret
= ustctl_release_object(sock
, ua_event
->obj
);
204 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
205 ERR("UST app sock %d release event obj failed with ret %d",
214 * Delete ust app stream safely. RCU read lock must be held before calling
218 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
225 ret
= ustctl_release_object(sock
, stream
->obj
);
226 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
227 ERR("UST app sock %d release stream obj failed with ret %d",
230 lttng_fd_put(LTTNG_FD_APPS
, 2);
237 * Delete ust app channel safely. RCU read lock must be held before calling
241 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
245 struct lttng_ht_iter iter
;
246 struct ust_app_event
*ua_event
;
247 struct ust_app_ctx
*ua_ctx
;
248 struct ust_app_stream
*stream
, *stmp
;
252 DBG3("UST app deleting channel %s", ua_chan
->name
);
255 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
256 cds_list_del(&stream
->list
);
257 delete_ust_app_stream(sock
, stream
);
261 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
262 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
264 delete_ust_app_ctx(sock
, ua_ctx
);
266 lttng_ht_destroy(ua_chan
->ctx
);
269 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
271 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
273 delete_ust_app_event(sock
, ua_event
);
275 lttng_ht_destroy(ua_chan
->events
);
277 /* Wipe and free registry. */
278 ust_registry_channel_destroy(&ua_chan
->session
->registry
, &ua_chan
->registry
);
280 if (ua_chan
->obj
!= NULL
) {
281 /* Remove channel from application UST object descriptor. */
282 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
283 lttng_ht_del(app
->ust_objd
, &iter
);
284 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
285 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
286 ERR("UST app sock %d release channel obj failed with ret %d",
289 lttng_fd_put(LTTNG_FD_APPS
, 2);
296 * Delete ust app session safely. RCU read lock must be held before calling
300 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
304 struct lttng_ht_iter iter
;
305 struct ust_app_channel
*ua_chan
;
307 if (ua_sess
->metadata
) {
308 delete_ust_app_channel(sock
, ua_sess
->metadata
, app
);
311 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
313 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
315 delete_ust_app_channel(sock
, ua_chan
, app
);
317 lttng_ht_destroy(ua_sess
->channels
);
319 ust_registry_session_destroy(&ua_sess
->registry
);
321 if (ua_sess
->handle
!= -1) {
322 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
323 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
324 ERR("UST app sock %d release session handle failed with ret %d",
332 * Delete a traceable application structure from the global list. Never call
333 * this function outside of a call_rcu call.
336 void delete_ust_app(struct ust_app
*app
)
339 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
343 /* Delete ust app sessions info */
347 lttng_ht_destroy(app
->sessions
);
350 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
352 /* Free every object in the session and the session. */
353 delete_ust_app_session(sock
, ua_sess
, app
);
357 * Wait until we have deleted the application from the sock hash table
358 * before closing this socket, otherwise an application could re-use the
359 * socket ID and race with the teardown, using the same hash table entry.
361 * It's OK to leave the close in call_rcu. We want it to stay unique for
362 * all RCU readers that could run concurrently with unregister app,
363 * therefore we _need_ to only close that socket after a grace period. So
364 * it should stay in this RCU callback.
366 * This close() is a very important step of the synchronization model so
367 * every modification to this function must be carefully reviewed.
373 lttng_fd_put(LTTNG_FD_APPS
, 1);
375 DBG2("UST app pid %d deleted", app
->pid
);
382 * URCU intermediate call to delete an UST app.
385 void delete_ust_app_rcu(struct rcu_head
*head
)
387 struct lttng_ht_node_ulong
*node
=
388 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
389 struct ust_app
*app
=
390 caa_container_of(node
, struct ust_app
, pid_n
);
392 DBG3("Call RCU deleting app PID %d", app
->pid
);
397 * Delete the session from the application ht and delete the data structure by
398 * freeing every object inside and releasing them.
400 static void destroy_app_session(struct ust_app
*app
,
401 struct ust_app_session
*ua_sess
)
404 struct lttng_ht_iter iter
;
409 iter
.iter
.node
= &ua_sess
->node
.node
;
410 ret
= lttng_ht_del(app
->sessions
, &iter
);
412 /* Already scheduled for teardown. */
416 /* Once deleted, free the data structure. */
417 delete_ust_app_session(app
->sock
, ua_sess
, app
);
424 * Alloc new UST app session.
427 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
429 struct ust_app_session
*ua_sess
;
431 /* Init most of the default value by allocating and zeroing */
432 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
433 if (ua_sess
== NULL
) {
438 ua_sess
->handle
= -1;
439 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
440 pthread_mutex_init(&ua_sess
->lock
, NULL
);
441 if (ust_registry_session_init(&ua_sess
->registry
, app
,
443 app
->uint8_t_alignment
,
444 app
->uint16_t_alignment
,
445 app
->uint32_t_alignment
,
446 app
->uint64_t_alignment
,
461 * Alloc new UST app channel.
464 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
465 struct ust_app_session
*ua_sess
,
466 struct lttng_ust_channel_attr
*attr
)
468 struct ust_app_channel
*ua_chan
;
470 /* Init most of the default value by allocating and zeroing */
471 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
472 if (ua_chan
== NULL
) {
477 /* Setup channel name */
478 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
479 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
481 ua_chan
->enabled
= 1;
482 ua_chan
->handle
= -1;
483 ua_chan
->key
= get_next_channel_key();
484 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
485 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
486 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
488 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
490 /* Initialize UST registry. */
491 ust_registry_channel_init(&ua_sess
->registry
, &ua_chan
->registry
);
493 /* Copy attributes */
495 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
496 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
497 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
498 ua_chan
->attr
.overwrite
= attr
->overwrite
;
499 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
500 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
501 ua_chan
->attr
.output
= attr
->output
;
503 /* By default, the channel is a per cpu channel. */
504 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
506 DBG3("UST app channel %s allocated", ua_chan
->name
);
515 * Allocate and initialize a UST app stream.
517 * Return newly allocated stream pointer or NULL on error.
519 struct ust_app_stream
*ust_app_alloc_stream(void)
521 struct ust_app_stream
*stream
= NULL
;
523 stream
= zmalloc(sizeof(*stream
));
524 if (stream
== NULL
) {
525 PERROR("zmalloc ust app stream");
529 /* Zero could be a valid value for a handle so flag it to -1. */
537 * Alloc new UST app event.
540 struct ust_app_event
*alloc_ust_app_event(char *name
,
541 struct lttng_ust_event
*attr
)
543 struct ust_app_event
*ua_event
;
545 /* Init most of the default value by allocating and zeroing */
546 ua_event
= zmalloc(sizeof(struct ust_app_event
));
547 if (ua_event
== NULL
) {
552 ua_event
->enabled
= 1;
553 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
554 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
555 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
557 /* Copy attributes */
559 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
562 DBG3("UST app event %s allocated", ua_event
->name
);
571 * Alloc new UST app context.
574 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
576 struct ust_app_ctx
*ua_ctx
;
578 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
579 if (ua_ctx
== NULL
) {
584 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
587 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
594 * Allocate a filter and copy the given original filter.
596 * Return allocated filter or NULL on error.
598 static struct lttng_ust_filter_bytecode
*alloc_copy_ust_app_filter(
599 struct lttng_ust_filter_bytecode
*orig_f
)
601 struct lttng_ust_filter_bytecode
*filter
= NULL
;
603 /* Copy filter bytecode */
604 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
606 PERROR("zmalloc alloc ust app filter");
610 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
617 * Find an ust_app using the sock and return it. RCU read side lock must be
618 * held before calling this helper function.
621 struct ust_app
*find_app_by_sock(int sock
)
623 struct lttng_ht_node_ulong
*node
;
624 struct lttng_ht_iter iter
;
626 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
627 node
= lttng_ht_iter_get_node_ulong(&iter
);
629 DBG2("UST app find by sock %d not found", sock
);
633 return caa_container_of(node
, struct ust_app
, sock_n
);
640 * Find an ust_app using the notify sock and return it. RCU read side lock must
641 * be held before calling this helper function.
643 static struct ust_app
*find_app_by_notify_sock(int sock
)
645 struct lttng_ht_node_ulong
*node
;
646 struct lttng_ht_iter iter
;
648 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
650 node
= lttng_ht_iter_get_node_ulong(&iter
);
652 DBG2("UST app find by notify sock %d not found", sock
);
656 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
663 * Lookup for an ust app event based on event name, filter bytecode and the
666 * Return an ust_app_event object or NULL on error.
668 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
669 char *name
, struct lttng_ust_filter_bytecode
*filter
, int loglevel
)
671 struct lttng_ht_iter iter
;
672 struct lttng_ht_node_str
*node
;
673 struct ust_app_event
*event
= NULL
;
674 struct ust_app_ht_key key
;
679 /* Setup key for event lookup. */
682 key
.loglevel
= loglevel
;
684 /* Lookup using the event name as hash and a custom match fct. */
685 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
686 ht_match_ust_app_event
, &key
, &iter
.iter
);
687 node
= lttng_ht_iter_get_node_str(&iter
);
692 event
= caa_container_of(node
, struct ust_app_event
, node
);
699 * Create the channel context on the tracer.
701 * Called with UST app session lock held.
704 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
705 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
709 health_code_update();
711 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
712 ua_chan
->obj
, &ua_ctx
->obj
);
714 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
715 ERR("UST app create channel context failed for app (pid: %d) "
716 "with ret %d", app
->pid
, ret
);
718 DBG3("UST app disable event failed. Application is dead.");
723 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
725 DBG2("UST app context handle %d created successfully for channel %s",
726 ua_ctx
->handle
, ua_chan
->name
);
729 health_code_update();
734 * Set the filter on the tracer.
737 int set_ust_event_filter(struct ust_app_event
*ua_event
,
742 health_code_update();
744 if (!ua_event
->filter
) {
749 ret
= ustctl_set_filter(app
->sock
, ua_event
->filter
,
752 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
753 ERR("UST app event %s filter failed for app (pid: %d) "
754 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
756 DBG3("UST app filter event failed. Application is dead.");
761 DBG2("UST filter set successfully for event %s", ua_event
->name
);
764 health_code_update();
769 * Disable the specified event on to UST tracer for the UST session.
771 static int disable_ust_event(struct ust_app
*app
,
772 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
776 health_code_update();
778 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
780 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
781 ERR("UST app event %s disable failed for app (pid: %d) "
782 "and session handle %d with ret %d",
783 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
785 DBG3("UST app disable event failed. Application is dead.");
790 DBG2("UST app event %s disabled successfully for app (pid: %d)",
791 ua_event
->attr
.name
, app
->pid
);
794 health_code_update();
799 * Disable the specified channel on to UST tracer for the UST session.
801 static int disable_ust_channel(struct ust_app
*app
,
802 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
806 health_code_update();
808 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
810 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
811 ERR("UST app channel %s disable failed for app (pid: %d) "
812 "and session handle %d with ret %d",
813 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
815 DBG3("UST app disable channel failed. Application is dead.");
820 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
821 ua_chan
->name
, app
->pid
);
824 health_code_update();
829 * Enable the specified channel on to UST tracer for the UST session.
831 static int enable_ust_channel(struct ust_app
*app
,
832 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
836 health_code_update();
838 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
840 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
841 ERR("UST app channel %s enable failed for app (pid: %d) "
842 "and session handle %d with ret %d",
843 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
845 DBG3("UST app enable channel failed. Application is dead.");
850 ua_chan
->enabled
= 1;
852 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
853 ua_chan
->name
, app
->pid
);
856 health_code_update();
861 * Enable the specified event on to UST tracer for the UST session.
863 static int enable_ust_event(struct ust_app
*app
,
864 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
868 health_code_update();
870 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
872 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
873 ERR("UST app event %s enable failed for app (pid: %d) "
874 "and session handle %d with ret %d",
875 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
877 DBG3("UST app enable event failed. Application is dead.");
882 DBG2("UST app event %s enabled successfully for app (pid: %d)",
883 ua_event
->attr
.name
, app
->pid
);
886 health_code_update();
891 * Create the specified channel onto the UST tracer for a UST session.
892 * Called with UST app session lock held.
894 * Return 0 on success. On error, a negative value is returned.
896 static int create_ust_channel(struct ust_app
*app
,
897 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
898 struct consumer_output
*consumer
)
901 unsigned int nb_fd
= 0;
902 struct consumer_socket
*socket
;
903 struct ust_app_stream
*stream
, *stmp
;
910 health_code_update();
912 /* Get the right consumer socket for the application. */
913 socket
= find_consumer_socket_by_bitness(app
->bits_per_long
, consumer
);
919 health_code_update();
922 * Ask consumer to create channel. The consumer will return the number of
923 * stream we have to expect.
925 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, consumer
, socket
);
931 * Compute the number of fd needed before receiving them. It must be 2 per
934 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
936 /* Reserve the amount of file descriptor we need. */
937 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
939 ERR("Exhausted number of available FD upon create channel");
943 health_code_update();
946 * Now get the channel from the consumer. This call wil populate the stream
947 * list of that channel and set the ust object.
949 ret
= ust_consumer_get_channel(socket
, ua_chan
);
954 /* Send channel to the application. */
955 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
960 /* Send all streams to application. */
961 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
962 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
966 /* We don't need the stream anymore once sent to the tracer. */
967 cds_list_del(&stream
->list
);
968 delete_ust_app_stream(-1, stream
);
971 /* Flag the channel that it is sent to the application. */
972 ua_chan
->is_sent
= 1;
973 /* Assign session to channel. */
974 ua_chan
->session
= ua_sess
;
975 /* Initialize ust objd object using the received handle and add it. */
976 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
977 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
979 health_code_update();
981 /* If channel is not enabled, disable it on the tracer */
982 if (!ua_chan
->enabled
) {
983 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
992 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
995 * Initiate a destroy channel on the consumer since we had an error
996 * handling it on our side. The return value is of no importance since we
997 * already have a ret value set by the previous error that we need to
1000 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
1002 health_code_update();
1007 * Create the specified event onto the UST tracer for a UST session.
1009 * Should be called with session mutex held.
1012 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1013 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1017 health_code_update();
1019 /* Create UST event on tracer */
1020 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1023 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1024 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1025 ua_event
->attr
.name
, app
->pid
, ret
);
1027 DBG3("UST app create event failed. Application is dead.");
1032 ua_event
->handle
= ua_event
->obj
->handle
;
1034 DBG2("UST app event %s created successfully for pid:%d",
1035 ua_event
->attr
.name
, app
->pid
);
1037 health_code_update();
1039 /* Set filter if one is present. */
1040 if (ua_event
->filter
) {
1041 ret
= set_ust_event_filter(ua_event
, app
);
1047 /* If event not enabled, disable it on the tracer */
1048 if (ua_event
->enabled
== 0) {
1049 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1052 * If we hit an EPERM, something is wrong with our disable call. If
1053 * we get an EEXIST, there is a problem on the tracer side since we
1057 case -LTTNG_UST_ERR_PERM
:
1058 /* Code flow problem */
1060 case -LTTNG_UST_ERR_EXIST
:
1061 /* It's OK for our use case. */
1072 health_code_update();
1077 * Copy data between an UST app event and a LTT event.
1079 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1080 struct ltt_ust_event
*uevent
)
1082 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1083 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1085 ua_event
->enabled
= uevent
->enabled
;
1087 /* Copy event attributes */
1088 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1090 /* Copy filter bytecode */
1091 if (uevent
->filter
) {
1092 ua_event
->filter
= alloc_copy_ust_app_filter(uevent
->filter
);
1093 /* Filter might be NULL here in case of ENONEM. */
1098 * Copy data between an UST app channel and a LTT channel.
1100 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1101 struct ltt_ust_channel
*uchan
)
1103 struct lttng_ht_iter iter
;
1104 struct ltt_ust_event
*uevent
;
1105 struct ltt_ust_context
*uctx
;
1106 struct ust_app_event
*ua_event
;
1107 struct ust_app_ctx
*ua_ctx
;
1109 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1111 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1112 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1114 /* Copy event attributes since the layout is different. */
1115 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1116 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1117 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1118 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1119 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1120 ua_chan
->attr
.output
= uchan
->attr
.output
;
1122 * Note that the attribute channel type is not set since the channel on the
1123 * tracing registry side does not have this information.
1126 ua_chan
->enabled
= uchan
->enabled
;
1128 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
1129 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1130 if (ua_ctx
== NULL
) {
1133 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1134 (unsigned long) ua_ctx
->ctx
.ctx
);
1135 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1138 /* Copy all events from ltt ust channel to ust app channel */
1139 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1140 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1141 uevent
->filter
, uevent
->attr
.loglevel
);
1142 if (ua_event
== NULL
) {
1143 DBG2("UST event %s not found on shadow copy channel",
1145 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1146 if (ua_event
== NULL
) {
1149 shadow_copy_event(ua_event
, uevent
);
1150 add_unique_ust_app_event(ua_chan
, ua_event
);
1154 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1158 * Copy data between a UST app session and a regular LTT session.
1160 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1161 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1163 struct lttng_ht_node_str
*ua_chan_node
;
1164 struct lttng_ht_iter iter
;
1165 struct ltt_ust_channel
*uchan
;
1166 struct ust_app_channel
*ua_chan
;
1168 struct tm
*timeinfo
;
1172 /* Get date and time for unique app path */
1174 timeinfo
= localtime(&rawtime
);
1175 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1177 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1179 ua_sess
->id
= usess
->id
;
1180 ua_sess
->uid
= usess
->uid
;
1181 ua_sess
->gid
= usess
->gid
;
1183 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s-%d-%s/", app
->name
, app
->pid
,
1186 PERROR("asprintf UST shadow copy session");
1187 /* TODO: We cannot return an error from here.. */
1191 /* TODO: support all UST domain */
1193 /* Iterate over all channels in global domain. */
1194 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1196 struct lttng_ht_iter uiter
;
1198 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1199 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1200 if (ua_chan_node
!= NULL
) {
1201 /* Session exist. Contiuing. */
1205 DBG2("Channel %s not found on shadow session copy, creating it",
1207 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1208 if (ua_chan
== NULL
) {
1209 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1212 shadow_copy_channel(ua_chan
, uchan
);
1214 * The concept of metadata channel does not exist on the tracing
1215 * registry side of the session daemon so this can only be a per CPU
1216 * channel and not metadata.
1218 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1220 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1225 * Lookup sesison wrapper.
1228 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1229 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1231 /* Get right UST app session from app */
1232 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
1236 * Return ust app session from the app session hashtable using the UST session
1239 static struct ust_app_session
*lookup_session_by_app(
1240 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1242 struct lttng_ht_iter iter
;
1243 struct lttng_ht_node_ulong
*node
;
1245 __lookup_session_by_app(usess
, app
, &iter
);
1246 node
= lttng_ht_iter_get_node_ulong(&iter
);
1251 return caa_container_of(node
, struct ust_app_session
, node
);
1258 * Create a session on the tracer side for the given app.
1260 * On success, ua_sess_ptr is populated with the session pointer or else left
1261 * untouched. If the session was created, is_created is set to 1. On error,
1262 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1265 * Returns 0 on success or else a negative code which is either -ENOMEM or
1266 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1268 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1269 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1272 int ret
, created
= 0;
1273 struct ust_app_session
*ua_sess
;
1277 assert(ua_sess_ptr
);
1279 health_code_update();
1281 ua_sess
= lookup_session_by_app(usess
, app
);
1282 if (ua_sess
== NULL
) {
1283 DBG2("UST app pid: %d session id %d not found, creating it",
1284 app
->pid
, usess
->id
);
1285 ua_sess
= alloc_ust_app_session(app
);
1286 if (ua_sess
== NULL
) {
1287 /* Only malloc can failed so something is really wrong */
1291 shadow_copy_session(ua_sess
, usess
, app
);
1295 health_code_update();
1297 if (ua_sess
->handle
== -1) {
1298 ret
= ustctl_create_session(app
->sock
);
1300 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1301 ERR("Creating session for app pid %d with ret %d",
1304 DBG("UST app creating session failed. Application is dead");
1306 delete_ust_app_session(-1, ua_sess
, app
);
1307 if (ret
!= -ENOMEM
) {
1309 * Tracer is probably gone or got an internal error so let's
1310 * behave like it will soon unregister or not usable.
1317 ua_sess
->handle
= ret
;
1319 /* Add ust app session to app's HT */
1320 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
1321 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
1323 DBG2("UST app session created successfully with handle %d", ret
);
1326 *ua_sess_ptr
= ua_sess
;
1328 *is_created
= created
;
1330 /* Everything went well. */
1334 health_code_update();
1339 * Create a context for the channel on the tracer.
1341 * Called with UST app session lock held.
1344 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
1345 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
1346 struct ust_app
*app
)
1349 struct lttng_ht_iter iter
;
1350 struct lttng_ht_node_ulong
*node
;
1351 struct ust_app_ctx
*ua_ctx
;
1353 DBG2("UST app adding context to channel %s", ua_chan
->name
);
1355 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
1356 node
= lttng_ht_iter_get_node_ulong(&iter
);
1362 ua_ctx
= alloc_ust_app_ctx(uctx
);
1363 if (ua_ctx
== NULL
) {
1369 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
1370 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1372 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
1382 * Enable on the tracer side a ust app event for the session and channel.
1384 * Called with UST app session lock held.
1387 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
1388 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1392 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1397 ua_event
->enabled
= 1;
1404 * Disable on the tracer side a ust app event for the session and channel.
1406 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1407 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1411 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1416 ua_event
->enabled
= 0;
1423 * Lookup ust app channel for session and disable it on the tracer side.
1426 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1427 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1431 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1436 ua_chan
->enabled
= 0;
1443 * Lookup ust app channel for session and enable it on the tracer side.
1445 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1446 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1449 struct lttng_ht_iter iter
;
1450 struct lttng_ht_node_str
*ua_chan_node
;
1451 struct ust_app_channel
*ua_chan
;
1453 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1454 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1455 if (ua_chan_node
== NULL
) {
1456 DBG2("Unable to find channel %s in ust session id %u",
1457 uchan
->name
, ua_sess
->id
);
1461 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1463 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1473 * Create UST app channel and create it on the tracer. Set ua_chanp of the
1474 * newly created channel if not NULL.
1476 * Called with UST app session lock held.
1478 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
1479 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
1480 struct consumer_output
*consumer
, enum lttng_ust_chan_type type
,
1481 struct ust_app_channel
**ua_chanp
)
1484 struct lttng_ht_iter iter
;
1485 struct lttng_ht_node_str
*ua_chan_node
;
1486 struct ust_app_channel
*ua_chan
;
1488 /* Lookup channel in the ust app session */
1489 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1490 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1491 if (ua_chan_node
!= NULL
) {
1492 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1496 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1497 if (ua_chan
== NULL
) {
1498 /* Only malloc can fail here */
1502 shadow_copy_channel(ua_chan
, uchan
);
1504 /* Set channel type. */
1505 ua_chan
->attr
.type
= type
;
1507 ret
= create_ust_channel(app
, ua_sess
, ua_chan
, consumer
);
1512 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1515 /* Only add the channel if successful on the tracer side. */
1516 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1520 *ua_chanp
= ua_chan
;
1523 /* Everything went well. */
1527 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
1532 * Create UST app event and create it on the tracer side.
1534 * Called with ust app session mutex held.
1537 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1538 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1539 struct ust_app
*app
)
1542 struct ust_app_event
*ua_event
;
1544 /* Get event node */
1545 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1546 uevent
->filter
, uevent
->attr
.loglevel
);
1547 if (ua_event
!= NULL
) {
1552 /* Does not exist so create one */
1553 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1554 if (ua_event
== NULL
) {
1555 /* Only malloc can failed so something is really wrong */
1559 shadow_copy_event(ua_event
, uevent
);
1561 /* Create it on the tracer side */
1562 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1564 /* Not found previously means that it does not exist on the tracer */
1565 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
1569 add_unique_ust_app_event(ua_chan
, ua_event
);
1571 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1578 /* Valid. Calling here is already in a read side lock */
1579 delete_ust_app_event(-1, ua_event
);
1584 * Create UST metadata and open it on the tracer side.
1586 * Called with UST app session lock held.
1588 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1589 struct ust_app
*app
, struct consumer_output
*consumer
)
1592 struct ust_app_channel
*metadata
;
1597 if (ua_sess
->metadata
) {
1598 /* Already exist. Return success. */
1602 /* Allocate UST metadata */
1603 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
1605 /* malloc() failed */
1610 /* Set default attributes for metadata. */
1611 metadata
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1612 metadata
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
1613 metadata
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
1614 metadata
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1615 metadata
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1616 metadata
->attr
.output
= LTTNG_UST_MMAP
;
1617 metadata
->attr
.type
= LTTNG_UST_CHAN_METADATA
;
1619 ret
= create_ust_channel(app
, ua_sess
, metadata
, consumer
);
1624 ua_sess
->metadata
= metadata
;
1626 DBG2("UST metadata opened for app pid %d", app
->pid
);
1631 delete_ust_app_channel(metadata
->is_sent
? app
->sock
: -1, metadata
, app
);
1637 * Return pointer to traceable apps list.
1639 struct lttng_ht
*ust_app_get_ht(void)
1645 * Return ust app pointer or NULL if not found.
1647 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1649 struct lttng_ht_node_ulong
*node
;
1650 struct lttng_ht_iter iter
;
1652 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1653 node
= lttng_ht_iter_get_node_ulong(&iter
);
1655 DBG2("UST app no found with pid %d", pid
);
1659 DBG2("Found UST app by pid %d", pid
);
1661 return caa_container_of(node
, struct ust_app
, pid_n
);
1668 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
1670 struct ust_app
*lta
= NULL
;
1675 DBG3("UST app creating application for socket %d", sock
);
1677 if ((msg
->bits_per_long
== 64 &&
1678 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
1679 || (msg
->bits_per_long
== 32 &&
1680 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
1681 ERR("Registration failed: application \"%s\" (pid: %d) has "
1682 "%d-bit long, but no consumerd for this size is available.\n",
1683 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1687 lta
= zmalloc(sizeof(struct ust_app
));
1693 lta
->ppid
= msg
->ppid
;
1694 lta
->uid
= msg
->uid
;
1695 lta
->gid
= msg
->gid
;
1696 lta
->compatible
= 0; /* Not compatible until proven */
1698 lta
->bits_per_long
= msg
->bits_per_long
;
1699 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
1700 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
1701 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
1702 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
1703 lta
->long_alignment
= msg
->long_alignment
;
1704 lta
->byte_order
= msg
->byte_order
;
1706 lta
->v_major
= msg
->major
;
1707 lta
->v_minor
= msg
->minor
;
1708 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1709 lta
->name
[LTTNG_UST_ABI_PROCNAME_LEN
] = '\0';
1710 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1711 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1712 lta
->notify_sock
= -1;
1713 lta
->compatible
= 1;
1715 lta
->pid
= msg
->pid
;
1716 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
1718 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
1720 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
1726 void ust_app_add(struct ust_app
*app
)
1729 assert(app
->notify_sock
>= 0);
1734 * On a re-registration, we want to kick out the previous registration of
1737 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
1740 * The socket _should_ be unique until _we_ call close. So, a add_unique
1741 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1742 * already in the table.
1744 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
1746 /* Add application to the notify socket hash table. */
1747 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
1748 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
1750 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
1751 "(version %d.%d)", app
->pid
, app
->ppid
, app
->uid
, app
->gid
,
1752 app
->sock
, app
->name
, app
->v_major
, app
->v_minor
);
1757 int ust_app_version(struct ust_app
*app
)
1760 return ustctl_tracer_version(app
->sock
, &app
->version
);
1764 * Unregister app by removing it from the global traceable app list and freeing
1767 * The socket is already closed at this point so no close to sock.
1769 void ust_app_unregister(int sock
)
1771 struct ust_app
*lta
;
1772 struct lttng_ht_node_ulong
*node
;
1773 struct lttng_ht_iter iter
;
1774 struct ust_app_session
*ua_sess
;
1779 /* Get the node reference for a call_rcu */
1780 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1781 node
= lttng_ht_iter_get_node_ulong(&iter
);
1784 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
1785 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
1787 /* Remove application from PID hash table */
1788 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
1791 /* Remove application from notify hash table */
1792 iter
.iter
.node
= <a
->notify_sock_n
.node
;
1793 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
1796 * Ignore return value since the node might have been removed before by an
1797 * add replace during app registration because the PID can be reassigned by
1800 iter
.iter
.node
= <a
->pid_n
.node
;
1801 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1803 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
1807 /* Remove sessions so they are not visible during deletion.*/
1808 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
1810 ret
= lttng_ht_del(lta
->sessions
, &iter
);
1812 /* The session was already removed so scheduled for teardown. */
1817 * Add session to list for teardown. This is safe since at this point we
1818 * are the only one using this list.
1820 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
1824 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
1831 * Return traceable_app_count
1833 unsigned long ust_app_list_count(void)
1835 unsigned long count
;
1838 count
= lttng_ht_get_count(ust_app_ht
);
1845 * Fill events array with all events name of all registered apps.
1847 int ust_app_list_events(struct lttng_event
**events
)
1850 size_t nbmem
, count
= 0;
1851 struct lttng_ht_iter iter
;
1852 struct ust_app
*app
;
1853 struct lttng_event
*tmp_event
;
1855 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1856 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1857 if (tmp_event
== NULL
) {
1858 PERROR("zmalloc ust app events");
1865 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1866 struct lttng_ust_tracepoint_iter uiter
;
1868 health_code_update();
1870 if (!app
->compatible
) {
1872 * TODO: In time, we should notice the caller of this error by
1873 * telling him that this is a version error.
1877 handle
= ustctl_tracepoint_list(app
->sock
);
1879 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
1880 ERR("UST app list events getting handle failed for app pid %d",
1886 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
1887 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
1888 /* Handle ustctl error. */
1891 if (ret
!= -LTTNG_UST_ERR_EXITING
|| ret
!= -EPIPE
) {
1892 ERR("UST app tp list get failed for app %d with ret %d",
1895 DBG3("UST app tp list get failed. Application is dead");
1900 health_code_update();
1901 if (count
>= nbmem
) {
1902 /* In case the realloc fails, we free the memory */
1905 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1908 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event
));
1910 PERROR("realloc ust app events");
1917 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1918 tmp_event
[count
].loglevel
= uiter
.loglevel
;
1919 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
1920 tmp_event
[count
].pid
= app
->pid
;
1921 tmp_event
[count
].enabled
= -1;
1927 *events
= tmp_event
;
1929 DBG2("UST app list events done (%zu events)", count
);
1934 health_code_update();
1939 * Fill events array with all events name of all registered apps.
1941 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
1944 size_t nbmem
, count
= 0;
1945 struct lttng_ht_iter iter
;
1946 struct ust_app
*app
;
1947 struct lttng_event_field
*tmp_event
;
1949 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1950 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
1951 if (tmp_event
== NULL
) {
1952 PERROR("zmalloc ust app event fields");
1959 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1960 struct lttng_ust_field_iter uiter
;
1962 health_code_update();
1964 if (!app
->compatible
) {
1966 * TODO: In time, we should notice the caller of this error by
1967 * telling him that this is a version error.
1971 handle
= ustctl_tracepoint_field_list(app
->sock
);
1973 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
1974 ERR("UST app list field getting handle failed for app pid %d",
1980 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
1981 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
1982 /* Handle ustctl error. */
1985 if (ret
!= -LTTNG_UST_ERR_EXITING
|| ret
!= -EPIPE
) {
1986 ERR("UST app tp list field failed for app %d with ret %d",
1989 DBG3("UST app tp list field failed. Application is dead");
1994 health_code_update();
1995 if (count
>= nbmem
) {
1996 /* In case the realloc fails, we free the memory */
1999 DBG2("Reallocating event field list from %zu to %zu entries", nbmem
,
2002 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event_field
));
2004 PERROR("realloc ust app event fields");
2012 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
2013 tmp_event
[count
].type
= uiter
.type
;
2014 tmp_event
[count
].nowrite
= uiter
.nowrite
;
2016 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
2017 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
2018 tmp_event
[count
].event
.type
= LTTNG_UST_TRACEPOINT
;
2019 tmp_event
[count
].event
.pid
= app
->pid
;
2020 tmp_event
[count
].event
.enabled
= -1;
2026 *fields
= tmp_event
;
2028 DBG2("UST app list event fields done (%zu events)", count
);
2033 health_code_update();
2038 * Free and clean all traceable apps of the global list.
2040 void ust_app_clean_list(void)
2043 struct ust_app
*app
;
2044 struct lttng_ht_iter iter
;
2046 DBG2("UST app cleaning registered apps hash table");
2050 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2051 ret
= lttng_ht_del(ust_app_ht
, &iter
);
2053 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
2056 /* Cleanup socket hash table */
2057 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
2059 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
2063 /* Destroy is done only when the ht is empty */
2064 lttng_ht_destroy(ust_app_ht
);
2065 lttng_ht_destroy(ust_app_ht_by_sock
);
2071 * Init UST app hash table.
2073 void ust_app_ht_alloc(void)
2075 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2076 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2077 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2081 * For a specific UST session, disable the channel for all registered apps.
2083 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
2084 struct ltt_ust_channel
*uchan
)
2087 struct lttng_ht_iter iter
;
2088 struct lttng_ht_node_str
*ua_chan_node
;
2089 struct ust_app
*app
;
2090 struct ust_app_session
*ua_sess
;
2091 struct ust_app_channel
*ua_chan
;
2093 if (usess
== NULL
|| uchan
== NULL
) {
2094 ERR("Disabling UST global channel with NULL values");
2099 DBG2("UST app disabling channel %s from global domain for session id %d",
2100 uchan
->name
, usess
->id
);
2104 /* For every registered applications */
2105 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2106 struct lttng_ht_iter uiter
;
2107 if (!app
->compatible
) {
2109 * TODO: In time, we should notice the caller of this error by
2110 * telling him that this is a version error.
2114 ua_sess
= lookup_session_by_app(usess
, app
);
2115 if (ua_sess
== NULL
) {
2120 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2121 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2122 /* If the session if found for the app, the channel must be there */
2123 assert(ua_chan_node
);
2125 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2126 /* The channel must not be already disabled */
2127 assert(ua_chan
->enabled
== 1);
2129 /* Disable channel onto application */
2130 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
2132 /* XXX: We might want to report this error at some point... */
2144 * For a specific UST session, enable the channel for all registered apps.
2146 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
2147 struct ltt_ust_channel
*uchan
)
2150 struct lttng_ht_iter iter
;
2151 struct ust_app
*app
;
2152 struct ust_app_session
*ua_sess
;
2154 if (usess
== NULL
|| uchan
== NULL
) {
2155 ERR("Adding UST global channel to NULL values");
2160 DBG2("UST app enabling channel %s to global domain for session id %d",
2161 uchan
->name
, usess
->id
);
2165 /* For every registered applications */
2166 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2167 if (!app
->compatible
) {
2169 * TODO: In time, we should notice the caller of this error by
2170 * telling him that this is a version error.
2174 ua_sess
= lookup_session_by_app(usess
, app
);
2175 if (ua_sess
== NULL
) {
2179 /* Enable channel onto application */
2180 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
2182 /* XXX: We might want to report this error at some point... */
2194 * Disable an event in a channel and for a specific session.
2196 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
2197 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2200 struct lttng_ht_iter iter
, uiter
;
2201 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2202 struct ust_app
*app
;
2203 struct ust_app_session
*ua_sess
;
2204 struct ust_app_channel
*ua_chan
;
2205 struct ust_app_event
*ua_event
;
2207 DBG("UST app disabling event %s for all apps in channel "
2208 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
2212 /* For all registered applications */
2213 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2214 if (!app
->compatible
) {
2216 * TODO: In time, we should notice the caller of this error by
2217 * telling him that this is a version error.
2221 ua_sess
= lookup_session_by_app(usess
, app
);
2222 if (ua_sess
== NULL
) {
2227 /* Lookup channel in the ust app session */
2228 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2229 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2230 if (ua_chan_node
== NULL
) {
2231 DBG2("Channel %s not found in session id %d for app pid %d."
2232 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
2235 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2237 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2238 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2239 if (ua_event_node
== NULL
) {
2240 DBG2("Event %s not found in channel %s for app pid %d."
2241 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
2244 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2246 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2248 /* XXX: Report error someday... */
2259 * For a specific UST session and UST channel, the event for all
2262 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
2263 struct ltt_ust_channel
*uchan
)
2266 struct lttng_ht_iter iter
, uiter
;
2267 struct lttng_ht_node_str
*ua_chan_node
;
2268 struct ust_app
*app
;
2269 struct ust_app_session
*ua_sess
;
2270 struct ust_app_channel
*ua_chan
;
2271 struct ust_app_event
*ua_event
;
2273 DBG("UST app disabling all event for all apps in channel "
2274 "%s for session id %d", uchan
->name
, usess
->id
);
2278 /* For all registered applications */
2279 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2280 if (!app
->compatible
) {
2282 * TODO: In time, we should notice the caller of this error by
2283 * telling him that this is a version error.
2287 ua_sess
= lookup_session_by_app(usess
, app
);
2289 /* The application has problem or is probably dead. */
2293 /* Lookup channel in the ust app session */
2294 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2295 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2296 /* If the channel is not found, there is a code flow error */
2297 assert(ua_chan_node
);
2299 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2301 /* Disable each events of channel */
2302 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2304 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2306 /* XXX: Report error someday... */
2318 * For a specific UST session, create the channel for all registered apps.
2320 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
2321 struct ltt_ust_channel
*uchan
)
2323 int ret
= 0, created
;
2324 struct lttng_ht_iter iter
;
2325 struct ust_app
*app
;
2326 struct ust_app_session
*ua_sess
= NULL
;
2328 /* Very wrong code flow */
2332 DBG2("UST app adding channel %s to global domain for session id %d",
2333 uchan
->name
, usess
->id
);
2337 /* For every registered applications */
2338 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2339 if (!app
->compatible
) {
2341 * TODO: In time, we should notice the caller of this error by
2342 * telling him that this is a version error.
2347 * Create session on the tracer side and add it to app session HT. Note
2348 * that if session exist, it will simply return a pointer to the ust
2351 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
2356 * The application's socket is not valid. Either a bad socket
2357 * or a timeout on it. We can't inform the caller that for a
2358 * specific app, the session failed so lets continue here.
2363 goto error_rcu_unlock
;
2368 pthread_mutex_lock(&ua_sess
->lock
);
2369 /* Create channel onto application. We don't need the chan ref. */
2370 ret
= create_ust_app_channel(ua_sess
, uchan
, app
, usess
->consumer
,
2371 LTTNG_UST_CHAN_PER_CPU
, NULL
);
2372 pthread_mutex_unlock(&ua_sess
->lock
);
2374 if (ret
== -ENOMEM
) {
2375 /* No more memory is a fatal error. Stop right now. */
2376 goto error_rcu_unlock
;
2378 /* Cleanup the created session if it's the case. */
2380 destroy_app_session(app
, ua_sess
);
2391 * Enable event for a specific session and channel on the tracer.
2393 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
2394 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2397 struct lttng_ht_iter iter
, uiter
;
2398 struct lttng_ht_node_str
*ua_chan_node
;
2399 struct ust_app
*app
;
2400 struct ust_app_session
*ua_sess
;
2401 struct ust_app_channel
*ua_chan
;
2402 struct ust_app_event
*ua_event
;
2404 DBG("UST app enabling event %s for all apps for session id %d",
2405 uevent
->attr
.name
, usess
->id
);
2408 * NOTE: At this point, this function is called only if the session and
2409 * channel passed are already created for all apps. and enabled on the
2415 /* For all registered applications */
2416 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2417 if (!app
->compatible
) {
2419 * TODO: In time, we should notice the caller of this error by
2420 * telling him that this is a version error.
2424 ua_sess
= lookup_session_by_app(usess
, app
);
2426 /* The application has problem or is probably dead. */
2430 pthread_mutex_lock(&ua_sess
->lock
);
2432 /* Lookup channel in the ust app session */
2433 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2434 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2435 /* If the channel is not found, there is a code flow error */
2436 assert(ua_chan_node
);
2438 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2440 /* Get event node */
2441 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2442 uevent
->filter
, uevent
->attr
.loglevel
);
2443 if (ua_event
== NULL
) {
2444 DBG3("UST app enable event %s not found for app PID %d."
2445 "Skipping app", uevent
->attr
.name
, app
->pid
);
2449 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2451 pthread_mutex_unlock(&ua_sess
->lock
);
2455 pthread_mutex_unlock(&ua_sess
->lock
);
2464 * For a specific existing UST session and UST channel, creates the event for
2465 * all registered apps.
2467 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
2468 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2471 struct lttng_ht_iter iter
, uiter
;
2472 struct lttng_ht_node_str
*ua_chan_node
;
2473 struct ust_app
*app
;
2474 struct ust_app_session
*ua_sess
;
2475 struct ust_app_channel
*ua_chan
;
2477 DBG("UST app creating event %s for all apps for session id %d",
2478 uevent
->attr
.name
, usess
->id
);
2482 /* For all registered applications */
2483 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2484 if (!app
->compatible
) {
2486 * TODO: In time, we should notice the caller of this error by
2487 * telling him that this is a version error.
2491 ua_sess
= lookup_session_by_app(usess
, app
);
2493 /* The application has problem or is probably dead. */
2497 pthread_mutex_lock(&ua_sess
->lock
);
2498 /* Lookup channel in the ust app session */
2499 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2500 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2501 /* If the channel is not found, there is a code flow error */
2502 assert(ua_chan_node
);
2504 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2506 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2507 pthread_mutex_unlock(&ua_sess
->lock
);
2509 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
2510 /* Possible value at this point: -ENOMEM. If so, we stop! */
2513 DBG2("UST app event %s already exist on app PID %d",
2514 uevent
->attr
.name
, app
->pid
);
2525 * Start tracing for a specific UST session and app.
2527 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2530 struct ust_app_session
*ua_sess
;
2532 DBG("Starting tracing for ust app pid %d", app
->pid
);
2536 if (!app
->compatible
) {
2540 ua_sess
= lookup_session_by_app(usess
, app
);
2541 if (ua_sess
== NULL
) {
2542 /* The session is in teardown process. Ignore and continue. */
2546 pthread_mutex_lock(&ua_sess
->lock
);
2548 /* Upon restart, we skip the setup, already done */
2549 if (ua_sess
->started
) {
2553 /* Create directories if consumer is LOCAL and has a path defined. */
2554 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2555 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
2556 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
2557 S_IRWXU
| S_IRWXG
, usess
->uid
, usess
->gid
);
2559 if (ret
!= -EEXIST
) {
2560 ERR("Trace directory creation error");
2566 /* Create the metadata for the application. */
2567 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
2572 health_code_update();
2575 /* This start the UST tracing */
2576 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
2578 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2579 ERR("Error starting tracing for app pid: %d (ret: %d)",
2582 DBG("UST app start session failed. Application is dead.");
2587 /* Indicate that the session has been started once */
2588 ua_sess
->started
= 1;
2590 pthread_mutex_unlock(&ua_sess
->lock
);
2592 health_code_update();
2594 /* Quiescent wait after starting trace */
2595 ret
= ustctl_wait_quiescent(app
->sock
);
2596 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2597 ERR("UST app wait quiescent failed for app pid %d ret %d",
2603 health_code_update();
2607 pthread_mutex_unlock(&ua_sess
->lock
);
2609 health_code_update();
2614 * Stop tracing for a specific UST session and app.
2616 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2619 struct lttng_ht_iter iter
;
2620 struct ust_app_session
*ua_sess
;
2621 struct ust_app_channel
*ua_chan
;
2623 DBG("Stopping tracing for ust app pid %d", app
->pid
);
2627 if (!app
->compatible
) {
2631 ua_sess
= lookup_session_by_app(usess
, app
);
2632 if (ua_sess
== NULL
) {
2637 * If started = 0, it means that stop trace has been called for a session
2638 * that was never started. It's possible since we can have a fail start
2639 * from either the application manager thread or the command thread. Simply
2640 * indicate that this is a stop error.
2642 if (!ua_sess
->started
) {
2643 goto error_rcu_unlock
;
2646 health_code_update();
2648 /* This inhibits UST tracing */
2649 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
2651 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2652 ERR("Error stopping tracing for app pid: %d (ret: %d)",
2655 DBG("UST app stop session failed. Application is dead.");
2657 goto error_rcu_unlock
;
2660 health_code_update();
2662 /* Quiescent wait after stopping trace */
2663 ret
= ustctl_wait_quiescent(app
->sock
);
2664 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2665 ERR("UST app wait quiescent failed for app pid %d ret %d",
2669 health_code_update();
2671 /* Flushing buffers */
2672 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2674 health_code_update();
2675 assert(ua_chan
->is_sent
);
2676 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_chan
->obj
);
2678 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2679 ERR("UST app PID %d channel %s flush failed with ret %d",
2680 app
->pid
, ua_chan
->name
, ret
);
2682 DBG3("UST app failed to flush %s. Application is dead.",
2684 /* No need to continue. */
2687 /* Continuing flushing all buffers */
2692 health_code_update();
2694 assert(ua_sess
->metadata
->is_sent
);
2695 /* Flush all buffers before stopping */
2696 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_sess
->metadata
->obj
);
2698 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2699 ERR("UST app PID %d metadata flush failed with ret %d", app
->pid
,
2701 goto error_rcu_unlock
;
2703 DBG3("UST app failed to flush metadata. Application is dead.");
2709 health_code_update();
2714 health_code_update();
2719 * Destroy a specific UST session in apps.
2721 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2724 struct ust_app_session
*ua_sess
;
2725 struct lttng_ht_iter iter
;
2726 struct lttng_ht_node_ulong
*node
;
2728 DBG("Destroy tracing for ust app pid %d", app
->pid
);
2732 if (!app
->compatible
) {
2736 __lookup_session_by_app(usess
, app
, &iter
);
2737 node
= lttng_ht_iter_get_node_ulong(&iter
);
2739 /* Session is being or is deleted. */
2742 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2744 health_code_update();
2745 destroy_app_session(app
, ua_sess
);
2747 health_code_update();
2749 /* Quiescent wait after stopping trace */
2750 ret
= ustctl_wait_quiescent(app
->sock
);
2751 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2752 ERR("UST app wait quiescent failed for app pid %d ret %d",
2758 health_code_update();
2763 * Start tracing for the UST session.
2765 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2768 struct lttng_ht_iter iter
;
2769 struct ust_app
*app
;
2771 DBG("Starting all UST traces");
2775 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2776 ret
= ust_app_start_trace(usess
, app
);
2778 /* Continue to next apps even on error */
2789 * Start tracing for the UST session.
2791 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2794 struct lttng_ht_iter iter
;
2795 struct ust_app
*app
;
2797 DBG("Stopping all UST traces");
2801 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2802 ret
= ust_app_stop_trace(usess
, app
);
2804 /* Continue to next apps even on error */
2815 * Destroy app UST session.
2817 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2820 struct lttng_ht_iter iter
;
2821 struct ust_app
*app
;
2823 DBG("Destroy all UST traces");
2827 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2828 ret
= destroy_trace(usess
, app
);
2830 /* Continue to next apps even on error */
2841 * Add channels/events from UST global domain to registered apps at sock.
2843 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2846 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
2847 struct ust_app
*app
;
2848 struct ust_app_session
*ua_sess
= NULL
;
2849 struct ust_app_channel
*ua_chan
;
2850 struct ust_app_event
*ua_event
;
2851 struct ust_app_ctx
*ua_ctx
;
2856 DBG2("UST app global update for app sock %d for session id %d", sock
,
2861 app
= find_app_by_sock(sock
);
2863 ERR("Failed to find app sock %d", sock
);
2867 if (!app
->compatible
) {
2871 ret
= create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
2873 /* Tracer is probably gone or ENOMEM. */
2878 pthread_mutex_lock(&ua_sess
->lock
);
2881 * We can iterate safely here over all UST app session since the create ust
2882 * app session above made a shadow copy of the UST global domain from the
2885 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2887 ret
= create_ust_channel(app
, ua_sess
, ua_chan
, usess
->consumer
);
2890 * Stop everything. On error, the application failed, no more file
2891 * descriptor are available or ENOMEM so stopping here is the only
2892 * thing we can do for now.
2897 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
2899 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2906 /* For each events */
2907 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2909 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2916 pthread_mutex_unlock(&ua_sess
->lock
);
2918 if (usess
->start_trace
) {
2919 ret
= ust_app_start_trace(usess
, app
);
2924 DBG2("UST trace started for app pid %d", app
->pid
);
2927 /* Everything went well at this point. */
2932 pthread_mutex_unlock(&ua_sess
->lock
);
2935 destroy_app_session(app
, ua_sess
);
2942 * Add context to a specific channel for global UST domain.
2944 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2945 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2948 struct lttng_ht_node_str
*ua_chan_node
;
2949 struct lttng_ht_iter iter
, uiter
;
2950 struct ust_app_channel
*ua_chan
= NULL
;
2951 struct ust_app_session
*ua_sess
;
2952 struct ust_app
*app
;
2956 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2957 if (!app
->compatible
) {
2959 * TODO: In time, we should notice the caller of this error by
2960 * telling him that this is a version error.
2964 ua_sess
= lookup_session_by_app(usess
, app
);
2965 if (ua_sess
== NULL
) {
2969 pthread_mutex_lock(&ua_sess
->lock
);
2970 /* Lookup channel in the ust app session */
2971 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2972 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2973 if (ua_chan_node
== NULL
) {
2976 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2978 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2983 pthread_mutex_unlock(&ua_sess
->lock
);
2991 * Enable event for a channel from a UST session for a specific PID.
2993 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2994 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2997 struct lttng_ht_iter iter
;
2998 struct lttng_ht_node_str
*ua_chan_node
;
2999 struct ust_app
*app
;
3000 struct ust_app_session
*ua_sess
;
3001 struct ust_app_channel
*ua_chan
;
3002 struct ust_app_event
*ua_event
;
3004 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
3008 app
= ust_app_find_by_pid(pid
);
3010 ERR("UST app enable event per PID %d not found", pid
);
3015 if (!app
->compatible
) {
3020 ua_sess
= lookup_session_by_app(usess
, app
);
3022 /* The application has problem or is probably dead. */
3027 pthread_mutex_lock(&ua_sess
->lock
);
3028 /* Lookup channel in the ust app session */
3029 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3030 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3031 /* If the channel is not found, there is a code flow error */
3032 assert(ua_chan_node
);
3034 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3036 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3037 uevent
->filter
, uevent
->attr
.loglevel
);
3038 if (ua_event
== NULL
) {
3039 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
3044 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
3051 pthread_mutex_unlock(&ua_sess
->lock
);
3058 * Disable event for a channel from a UST session for a specific PID.
3060 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
3061 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
3064 struct lttng_ht_iter iter
;
3065 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3066 struct ust_app
*app
;
3067 struct ust_app_session
*ua_sess
;
3068 struct ust_app_channel
*ua_chan
;
3069 struct ust_app_event
*ua_event
;
3071 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
3075 app
= ust_app_find_by_pid(pid
);
3077 ERR("UST app disable event per PID %d not found", pid
);
3082 if (!app
->compatible
) {
3087 ua_sess
= lookup_session_by_app(usess
, app
);
3089 /* The application has problem or is probably dead. */
3093 /* Lookup channel in the ust app session */
3094 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3095 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3096 if (ua_chan_node
== NULL
) {
3097 /* Channel does not exist, skip disabling */
3100 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3102 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
3103 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
3104 if (ua_event_node
== NULL
) {
3105 /* Event does not exist, skip disabling */
3108 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3110 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3121 * Calibrate registered applications.
3123 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
3126 struct lttng_ht_iter iter
;
3127 struct ust_app
*app
;
3131 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3132 if (!app
->compatible
) {
3134 * TODO: In time, we should notice the caller of this error by
3135 * telling him that this is a version error.
3140 health_code_update();
3142 ret
= ustctl_calibrate(app
->sock
, calibrate
);
3146 /* Means that it's not implemented on the tracer side. */
3150 DBG2("Calibrate app PID %d returned with error %d",
3157 DBG("UST app global domain calibration finished");
3161 health_code_update();
3167 * Receive registration and populate the given msg structure.
3169 * On success return 0 else a negative value returned by the ustctl call.
3171 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
3174 uint32_t pid
, ppid
, uid
, gid
;
3178 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
3179 &pid
, &ppid
, &uid
, &gid
,
3180 &msg
->bits_per_long
,
3181 &msg
->uint8_t_alignment
,
3182 &msg
->uint16_t_alignment
,
3183 &msg
->uint32_t_alignment
,
3184 &msg
->uint64_t_alignment
,
3185 &msg
->long_alignment
,
3192 case LTTNG_UST_ERR_EXITING
:
3193 DBG3("UST app recv reg message failed. Application died");
3195 case LTTNG_UST_ERR_UNSUP_MAJOR
:
3196 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
3197 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
3198 LTTNG_UST_ABI_MINOR_VERSION
);
3201 ERR("UST app recv reg message failed with ret %d", ret
);
3206 msg
->pid
= (pid_t
) pid
;
3207 msg
->ppid
= (pid_t
) ppid
;
3208 msg
->uid
= (uid_t
) uid
;
3209 msg
->gid
= (gid_t
) gid
;
3215 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
3218 struct lttng_ht_node_ulong
*node
;
3219 struct lttng_ht_iter iter
;
3220 struct ust_app_channel
*ua_chan
= NULL
;
3224 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
3225 node
= lttng_ht_iter_get_node_ulong(&iter
);
3227 DBG2("UST app channel find by objd %d not found", objd
);
3231 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
3237 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
3238 size_t nr_fields
, struct ustctl_field
*fields
)
3240 int ret
, ret_code
= 0;
3241 uint32_t chan_id
, reg_count
;
3242 enum ustctl_channel_header type
;
3243 struct ust_app
*app
;
3244 struct ust_app_channel
*ua_chan
;
3245 struct ust_app_session
*ua_sess
;
3249 /* Lookup application. If not found, there is a code flow error. */
3250 app
= find_app_by_notify_sock(sock
);
3253 /* Lookup channel by UST object descriptor. Should always be found. */
3254 ua_chan
= find_channel_by_objd(app
, cobjd
);
3256 assert(ua_chan
->session
);
3257 ua_sess
= ua_chan
->session
;
3260 pthread_mutex_lock(&ua_sess
->registry
.lock
);
3262 if (ust_registry_is_max_id(ua_chan
->session
->registry
.used_channel_id
)) {
3269 /* Don't assign ID to metadata. */
3270 if (ua_chan
->attr
.type
== LTTNG_UST_CHAN_METADATA
) {
3273 chan_id
= ust_registry_get_next_chan_id(&ua_chan
->session
->registry
);
3276 reg_count
= ust_registry_get_event_count(&ua_chan
->registry
);
3277 if (reg_count
< 31) {
3278 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
3280 type
= USTCTL_CHANNEL_HEADER_LARGE
;
3283 ua_chan
->registry
.nr_ctx_fields
= nr_fields
;
3284 ua_chan
->registry
.ctx_fields
= fields
;
3285 ua_chan
->registry
.chan_id
= chan_id
;
3286 ua_chan
->registry
.header_type
= type
;
3288 /* Append to metadata */
3290 ret_code
= ust_metadata_channel_statedump(&ua_chan
->session
->registry
,
3291 &ua_chan
->registry
);
3293 ERR("Error appending channel metadata (errno = %d)", ret_code
);
3299 DBG3("UST app replying to register channel with id %u, type: %d, ret: %d",
3300 chan_id
, type
, ret_code
);
3302 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
3304 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3305 ERR("UST app reply channel failed with ret %d", ret
);
3307 DBG3("UST app reply channel failed. Application died");
3313 pthread_mutex_unlock(&ua_sess
->registry
.lock
);
3318 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
3319 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
3320 char *model_emf_uri
)
3323 uint32_t event_id
= 0;
3324 struct ust_app
*app
;
3325 struct ust_app_channel
*ua_chan
;
3326 struct ust_app_session
*ua_sess
;
3330 /* Lookup application. If not found, there is a code flow error. */
3331 app
= find_app_by_notify_sock(sock
);
3334 /* Lookup channel by UST object descriptor. Should always be found. */
3335 ua_chan
= find_channel_by_objd(app
, cobjd
);
3337 assert(ua_chan
->session
);
3338 ua_sess
= ua_chan
->session
;
3340 pthread_mutex_lock(&ua_sess
->registry
.lock
);
3342 ret_code
= ust_registry_create_event(&ua_sess
->registry
, &ua_chan
->registry
, sobjd
, cobjd
,
3343 name
, sig
, nr_fields
, fields
, loglevel
, model_emf_uri
, &event_id
);
3346 * The return value is returned to ustctl so in case of an error, the
3347 * application can be notified. In case of an error, it's important not to
3348 * return a negative error or else the application will get closed.
3350 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
3352 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3353 ERR("UST app reply event failed with ret %d", ret
);
3355 DBG3("UST app reply event failed. Application died");
3358 * No need to wipe the create event since the application socket will
3359 * get close on error hence cleaning up everything by itself.
3365 pthread_mutex_unlock(&ua_sess
->registry
.lock
);
3370 int ust_app_recv_notify(int sock
)
3373 enum ustctl_notify_cmd cmd
;
3375 DBG3("UST app receiving notify from sock %d", sock
);
3377 ret
= ustctl_recv_notify(sock
, &cmd
);
3379 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3380 ERR("UST app recv notify failed with ret %d", ret
);
3382 DBG3("UST app recv notify failed. Application died");
3388 case USTCTL_NOTIFY_CMD_EVENT
:
3390 int sobjd
, cobjd
, loglevel
;
3391 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
3393 struct ustctl_field
*fields
;
3395 DBG2("UST app ustctl register event received");
3397 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
3398 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
3400 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3401 ERR("UST app recv event failed with ret %d", ret
);
3403 DBG3("UST app recv event failed. Application died");
3408 /* Add event to the UST registry coming from the notify socket. */
3409 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
3410 fields
, loglevel
, model_emf_uri
);
3417 case USTCTL_NOTIFY_CMD_CHANNEL
:
3421 struct ustctl_field
*fields
;
3423 DBG2("UST app ustctl register channel received");
3425 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
3428 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3429 ERR("UST app recv channel failed with ret %d", ret
);
3431 DBG3("UST app recv channel failed. Application died");
3436 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
3445 /* Should NEVER happen. */