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"
40 * Match function for the hash table lookup.
42 * It matches an ust app event based on three attributes which are the event
43 * name, the filter bytecode and the loglevel.
45 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
47 struct ust_app_event
*event
;
48 const struct ust_app_ht_key
*key
;
53 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
56 /* Match the 3 elements of the key: name, filter and loglevel. */
59 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
64 if (event
->attr
.loglevel
!= key
->loglevel
) {
65 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
66 && key
->loglevel
== 0 && event
->attr
.loglevel
== -1) {
68 * Match is accepted. This is because on event creation, the
69 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
70 * -1 are accepted for this loglevel type since 0 is the one set by
71 * the API when receiving an enable event.
78 /* One of the filters is NULL, fail. */
79 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
83 if (key
->filter
&& event
->filter
) {
84 /* Both filters exists, check length followed by the bytecode. */
85 if (event
->filter
->len
!= key
->filter
->len
||
86 memcmp(event
->filter
->data
, key
->filter
->data
,
87 event
->filter
->len
) != 0) {
100 * Unique add of an ust app event in the given ht. This uses the custom
101 * ht_match_ust_app_event match function and the event name as hash.
103 static void add_unique_ust_app_event(struct lttng_ht
*ht
,
104 struct ust_app_event
*event
)
106 struct cds_lfht_node
*node_ptr
;
107 struct ust_app_ht_key key
;
113 key
.name
= event
->attr
.name
;
114 key
.filter
= event
->filter
;
115 key
.loglevel
= event
->attr
.loglevel
;
117 node_ptr
= cds_lfht_add_unique(ht
->ht
,
118 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
119 ht_match_ust_app_event
, &key
, &event
->node
.node
);
120 assert(node_ptr
== &event
->node
.node
);
124 * Delete ust context safely. RCU read lock must be held before calling
128 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
131 ustctl_release_object(sock
, ua_ctx
->obj
);
138 * Delete ust app event safely. RCU read lock must be held before calling
142 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
144 free(ua_event
->filter
);
146 if (ua_event
->obj
!= NULL
) {
147 ustctl_release_object(sock
, ua_event
->obj
);
154 * Delete ust app stream safely. RCU read lock must be held before calling
158 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
161 ustctl_release_object(sock
, stream
->obj
);
162 lttng_fd_put(LTTNG_FD_APPS
, 2);
169 * Delete ust app channel safely. RCU read lock must be held before calling
173 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
176 struct lttng_ht_iter iter
;
177 struct ust_app_event
*ua_event
;
178 struct ust_app_ctx
*ua_ctx
;
179 struct ust_app_stream
*stream
, *stmp
;
182 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
183 cds_list_del(&stream
->list
);
184 delete_ust_app_stream(sock
, stream
);
188 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
189 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
191 delete_ust_app_ctx(sock
, ua_ctx
);
193 lttng_ht_destroy(ua_chan
->ctx
);
196 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
198 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
200 delete_ust_app_event(sock
, ua_event
);
202 lttng_ht_destroy(ua_chan
->events
);
204 if (ua_chan
->obj
!= NULL
) {
205 ustctl_release_object(sock
, ua_chan
->obj
);
206 lttng_fd_put(LTTNG_FD_APPS
, 2);
213 * Delete ust app session safely. RCU read lock must be held before calling
217 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
)
220 struct lttng_ht_iter iter
;
221 struct ust_app_channel
*ua_chan
;
223 if (ua_sess
->metadata
) {
224 if (ua_sess
->metadata
->stream_obj
) {
225 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
226 lttng_fd_put(LTTNG_FD_APPS
, 2);
227 free(ua_sess
->metadata
->stream_obj
);
229 if (ua_sess
->metadata
->obj
) {
230 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
231 lttng_fd_put(LTTNG_FD_APPS
, 2);
232 free(ua_sess
->metadata
->obj
);
234 trace_ust_destroy_metadata(ua_sess
->metadata
);
237 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
239 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
241 delete_ust_app_channel(sock
, ua_chan
);
243 lttng_ht_destroy(ua_sess
->channels
);
245 if (ua_sess
->handle
!= -1) {
246 ustctl_release_handle(sock
, ua_sess
->handle
);
252 * Delete a traceable application structure from the global list. Never call
253 * this function outside of a call_rcu call.
256 void delete_ust_app(struct ust_app
*app
)
259 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
263 /* Delete ust app sessions info */
267 lttng_ht_destroy(app
->sessions
);
270 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
272 /* Free every object in the session and the session. */
273 delete_ust_app_session(sock
, ua_sess
);
277 * Wait until we have deleted the application from the sock hash table
278 * before closing this socket, otherwise an application could re-use the
279 * socket ID and race with the teardown, using the same hash table entry.
281 * It's OK to leave the close in call_rcu. We want it to stay unique for
282 * all RCU readers that could run concurrently with unregister app,
283 * therefore we _need_ to only close that socket after a grace period. So
284 * it should stay in this RCU callback.
286 * This close() is a very important step of the synchronization model so
287 * every modification to this function must be carefully reviewed.
293 lttng_fd_put(LTTNG_FD_APPS
, 1);
295 DBG2("UST app pid %d deleted", app
->pid
);
302 * URCU intermediate call to delete an UST app.
305 void delete_ust_app_rcu(struct rcu_head
*head
)
307 struct lttng_ht_node_ulong
*node
=
308 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
309 struct ust_app
*app
=
310 caa_container_of(node
, struct ust_app
, pid_n
);
312 DBG3("Call RCU deleting app PID %d", app
->pid
);
317 * Alloc new UST app session.
320 struct ust_app_session
*alloc_ust_app_session(void)
322 struct ust_app_session
*ua_sess
;
324 /* Init most of the default value by allocating and zeroing */
325 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
326 if (ua_sess
== NULL
) {
331 ua_sess
->handle
= -1;
332 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
341 * Alloc new UST app channel.
344 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
345 struct lttng_ust_channel
*attr
)
347 struct ust_app_channel
*ua_chan
;
349 /* Init most of the default value by allocating and zeroing */
350 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
351 if (ua_chan
== NULL
) {
356 /* Setup channel name */
357 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
358 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
360 ua_chan
->enabled
= 1;
361 ua_chan
->handle
= -1;
362 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
363 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
364 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
366 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
368 /* Copy attributes */
370 /* Translate from lttng_ust_channel to lttng_ust_channel_attr.*/
371 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
372 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
373 ua_chan
->attr
.overwrite
= attr
->overwrite
;
374 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
375 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
376 ua_chan
->attr
.output
= attr
->output
;
379 DBG3("UST app channel %s allocated", ua_chan
->name
);
388 * Allocate and initialize a UST app stream.
390 * Return newly allocated stream pointer or NULL on error.
392 static struct ust_app_stream
*alloc_ust_app_stream(void)
394 struct ust_app_stream
*stream
= NULL
;
396 stream
= zmalloc(sizeof(*stream
));
397 if (stream
== NULL
) {
398 PERROR("zmalloc ust app stream");
402 /* Zero could be a valid value for a handle so flag it to -1. */
410 * Alloc new UST app event.
413 struct ust_app_event
*alloc_ust_app_event(char *name
,
414 struct lttng_ust_event
*attr
)
416 struct ust_app_event
*ua_event
;
418 /* Init most of the default value by allocating and zeroing */
419 ua_event
= zmalloc(sizeof(struct ust_app_event
));
420 if (ua_event
== NULL
) {
425 ua_event
->enabled
= 1;
426 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
427 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
428 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
430 /* Copy attributes */
432 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
435 DBG3("UST app event %s allocated", ua_event
->name
);
444 * Alloc new UST app context.
447 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
449 struct ust_app_ctx
*ua_ctx
;
451 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
452 if (ua_ctx
== NULL
) {
457 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
460 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
467 * Allocate a filter and copy the given original filter.
469 * Return allocated filter or NULL on error.
471 static struct lttng_ust_filter_bytecode
*alloc_copy_ust_app_filter(
472 struct lttng_ust_filter_bytecode
*orig_f
)
474 struct lttng_ust_filter_bytecode
*filter
= NULL
;
476 /* Copy filter bytecode */
477 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
479 PERROR("zmalloc alloc ust app filter");
483 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
490 * Find an ust_app using the sock and return it. RCU read side lock must be
491 * held before calling this helper function.
494 struct ust_app
*find_app_by_sock(int sock
)
496 struct lttng_ht_node_ulong
*node
;
497 struct lttng_ht_iter iter
;
499 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
500 node
= lttng_ht_iter_get_node_ulong(&iter
);
502 DBG2("UST app find by sock %d not found", sock
);
506 return caa_container_of(node
, struct ust_app
, sock_n
);
513 * Lookup for an ust app event based on event name, filter bytecode and the
516 * Return an ust_app_event object or NULL on error.
518 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
519 char *name
, struct lttng_ust_filter_bytecode
*filter
, int loglevel
)
521 struct lttng_ht_iter iter
;
522 struct lttng_ht_node_str
*node
;
523 struct ust_app_event
*event
= NULL
;
524 struct ust_app_ht_key key
;
529 /* Setup key for event lookup. */
532 key
.loglevel
= loglevel
;
534 /* Lookup using the event name as hash and a custom match fct. */
535 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
536 ht_match_ust_app_event
, &key
, &iter
.iter
);
537 node
= lttng_ht_iter_get_node_str(&iter
);
542 event
= caa_container_of(node
, struct ust_app_event
, node
);
549 * Create the channel context on the tracer.
552 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
553 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
557 health_code_update(&health_thread_cmd
);
559 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
560 ua_chan
->obj
, &ua_ctx
->obj
);
565 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
567 DBG2("UST app context created successfully for channel %s", ua_chan
->name
);
570 health_code_update(&health_thread_cmd
);
575 * Set the filter on the tracer.
578 int set_ust_event_filter(struct ust_app_event
*ua_event
,
583 health_code_update(&health_thread_cmd
);
585 if (!ua_event
->filter
) {
590 ret
= ustctl_set_filter(app
->sock
, ua_event
->filter
,
596 DBG2("UST filter set successfully for event %s", ua_event
->name
);
599 health_code_update(&health_thread_cmd
);
604 * Disable the specified event on to UST tracer for the UST session.
606 static int disable_ust_event(struct ust_app
*app
,
607 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
611 health_code_update(&health_thread_cmd
);
613 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
615 ERR("UST app event %s disable failed for app (pid: %d) "
616 "and session handle %d with ret %d",
617 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
621 DBG2("UST app event %s disabled successfully for app (pid: %d)",
622 ua_event
->attr
.name
, app
->pid
);
625 health_code_update(&health_thread_cmd
);
630 * Disable the specified channel on to UST tracer for the UST session.
632 static int disable_ust_channel(struct ust_app
*app
,
633 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
637 health_code_update(&health_thread_cmd
);
639 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
641 ERR("UST app channel %s disable failed for app (pid: %d) "
642 "and session handle %d with ret %d",
643 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
647 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
648 ua_chan
->name
, app
->pid
);
651 health_code_update(&health_thread_cmd
);
656 * Enable the specified channel on to UST tracer for the UST session.
658 static int enable_ust_channel(struct ust_app
*app
,
659 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
663 health_code_update(&health_thread_cmd
);
665 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
667 ERR("UST app channel %s enable failed for app (pid: %d) "
668 "and session handle %d with ret %d",
669 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
673 ua_chan
->enabled
= 1;
675 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
676 ua_chan
->name
, app
->pid
);
679 health_code_update(&health_thread_cmd
);
684 * Enable the specified event on to UST tracer for the UST session.
686 static int enable_ust_event(struct ust_app
*app
,
687 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
691 health_code_update(&health_thread_cmd
);
693 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
695 ERR("UST app event %s enable failed for app (pid: %d) "
696 "and session handle %d with ret %d",
697 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
701 DBG2("UST app event %s enabled successfully for app (pid: %d)",
702 ua_event
->attr
.name
, app
->pid
);
705 health_code_update(&health_thread_cmd
);
710 * Open metadata onto the UST tracer for a UST session.
712 static int open_ust_metadata(struct ust_app
*app
,
713 struct ust_app_session
*ua_sess
)
716 struct lttng_ust_channel_attr uattr
;
718 health_code_update(&health_thread_cmd
);
720 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
721 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
722 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
723 uattr
.switch_timer_interval
=
724 ua_sess
->metadata
->attr
.switch_timer_interval
;
725 uattr
.read_timer_interval
=
726 ua_sess
->metadata
->attr
.read_timer_interval
;
727 uattr
.output
= ua_sess
->metadata
->attr
.output
;
729 /* We are going to receive 2 fds, we need to reserve them. */
730 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
732 ERR("Exhausted number of available FD upon metadata open");
735 /* UST tracer metadata creation */
736 ret
= ustctl_open_metadata(app
->sock
, ua_sess
->handle
, &uattr
,
737 &ua_sess
->metadata
->obj
);
739 ERR("UST app open metadata failed for app pid:%d with ret %d",
744 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
747 health_code_update(&health_thread_cmd
);
752 * Create metadata stream onto the UST tracer for a given session.
754 static int create_ust_metadata_stream(struct ust_app
*app
,
755 struct ust_app_session
*ua_sess
)
759 health_code_update(&health_thread_cmd
);
761 /* We are going to receive 2 fds, we need to reserve them. */
762 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
764 ERR("Exhausted number of available FD upon metadata stream create");
767 ret
= ustctl_create_stream(app
->sock
, ua_sess
->metadata
->obj
,
768 &ua_sess
->metadata
->stream_obj
);
770 lttng_fd_put(LTTNG_FD_APPS
, 2);
771 ERR("UST create metadata stream failed");
776 health_code_update(&health_thread_cmd
);
781 * Create stream onto the UST tracer for a given channel.
783 * Return -ENOENT if no more stream is available for this channel.
784 * On success, return 0.
785 * On error, return a negative value.
787 static int create_ust_stream(struct ust_app
*app
,
788 struct ust_app_channel
*ua_chan
, struct ust_app_stream
*stream
)
794 assert(ua_chan
->obj
);
797 health_code_update(&health_thread_cmd
);
799 /* We are going to receive 2 fds, we need to reserve them. */
800 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
802 ERR("Exhausted number of available FD on stream creation");
803 /* Just to make sure we never return -ENOENT. */
809 * Set the stream name before creating it. On error, we don't have to
810 * delete it on the tracer side.
812 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%u",
813 ua_chan
->name
, ua_chan
->streams
.count
);
815 /* Without the stream name we can't continue using it. */
816 PERROR("snprintf UST create stream");
817 /* Just to make sure we never return -ENOENT. */
822 ret
= ustctl_create_stream(app
->sock
, ua_chan
->obj
, &stream
->obj
);
824 lttng_fd_put(LTTNG_FD_APPS
, 2);
825 /* Indicates that there is no more stream for that channel. */
826 if (ret
!= -LTTNG_UST_ERR_NOENT
) {
827 ERR("UST create metadata stream failed (ret: %d)", ret
);
832 /* Set stream handle with the returned value. */
833 stream
->handle
= stream
->obj
->handle
;
836 health_code_update(&health_thread_cmd
);
841 * Create the specified channel onto the UST tracer for a UST session.
843 static int create_ust_channel(struct ust_app
*app
,
844 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
848 health_code_update(&health_thread_cmd
);
850 /* We are going to receive 2 fds, we need to reserve them. */
851 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
853 ERR("Exhausted number of available FD upon create channel");
857 health_code_update(&health_thread_cmd
);
859 ret
= ustctl_create_channel(app
->sock
, ua_sess
->handle
, &ua_chan
->attr
,
862 ERR("Creating channel %s for app (pid: %d, sock: %d) "
863 "and session handle %d with ret %d",
864 ua_chan
->name
, app
->pid
, app
->sock
,
865 ua_sess
->handle
, ret
);
866 lttng_fd_put(LTTNG_FD_APPS
, 2);
870 ua_chan
->handle
= ua_chan
->obj
->handle
;
872 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
873 ua_chan
->name
, app
->pid
, app
->sock
);
875 health_code_update(&health_thread_cmd
);
877 /* If channel is not enabled, disable it on the tracer */
878 if (!ua_chan
->enabled
) {
879 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
886 health_code_update(&health_thread_cmd
);
891 * Create the specified event onto the UST tracer for a UST session.
894 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
895 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
899 health_code_update(&health_thread_cmd
);
901 /* Create UST event on tracer */
902 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
905 ERR("Error ustctl create event %s for app pid: %d with ret %d",
906 ua_event
->attr
.name
, app
->pid
, ret
);
910 ua_event
->handle
= ua_event
->obj
->handle
;
912 DBG2("UST app event %s created successfully for pid:%d",
913 ua_event
->attr
.name
, app
->pid
);
915 health_code_update(&health_thread_cmd
);
917 /* Set filter if one is present. */
918 if (ua_event
->filter
) {
919 ret
= set_ust_event_filter(ua_event
, app
);
925 /* If event not enabled, disable it on the tracer */
926 if (ua_event
->enabled
== 0) {
927 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
930 * If we hit an EPERM, something is wrong with our disable call. If
931 * we get an EEXIST, there is a problem on the tracer side since we
935 case -LTTNG_UST_ERR_PERM
:
936 /* Code flow problem */
938 case -LTTNG_UST_ERR_EXIST
:
939 /* It's OK for our use case. */
950 health_code_update(&health_thread_cmd
);
955 * Copy data between an UST app event and a LTT event.
957 static void shadow_copy_event(struct ust_app_event
*ua_event
,
958 struct ltt_ust_event
*uevent
)
960 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
961 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
963 ua_event
->enabled
= uevent
->enabled
;
965 /* Copy event attributes */
966 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
968 /* Copy filter bytecode */
969 if (uevent
->filter
) {
970 ua_event
->filter
= alloc_copy_ust_app_filter(uevent
->filter
);
971 /* Filter might be NULL here in case of ENONEM. */
976 * Copy data between an UST app channel and a LTT channel.
978 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
979 struct ltt_ust_channel
*uchan
)
981 struct lttng_ht_iter iter
;
982 struct ltt_ust_event
*uevent
;
983 struct ltt_ust_context
*uctx
;
984 struct ust_app_event
*ua_event
;
985 struct ust_app_ctx
*ua_ctx
;
987 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
989 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
990 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
991 /* Copy event attributes */
992 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
994 ua_chan
->enabled
= uchan
->enabled
;
996 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
997 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
998 if (ua_ctx
== NULL
) {
1001 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1002 (unsigned long) ua_ctx
->ctx
.ctx
);
1003 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1006 /* Copy all events from ltt ust channel to ust app channel */
1007 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1008 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1009 uevent
->filter
, uevent
->attr
.loglevel
);
1010 if (ua_event
== NULL
) {
1011 DBG2("UST event %s not found on shadow copy channel",
1013 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1014 if (ua_event
== NULL
) {
1017 shadow_copy_event(ua_event
, uevent
);
1018 add_unique_ust_app_event(ua_chan
->events
, ua_event
);
1022 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1026 * Copy data between a UST app session and a regular LTT session.
1028 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1029 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1031 struct lttng_ht_node_str
*ua_chan_node
;
1032 struct lttng_ht_iter iter
;
1033 struct ltt_ust_channel
*uchan
;
1034 struct ust_app_channel
*ua_chan
;
1036 struct tm
*timeinfo
;
1040 /* Get date and time for unique app path */
1042 timeinfo
= localtime(&rawtime
);
1043 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1045 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1047 ua_sess
->id
= usess
->id
;
1048 ua_sess
->uid
= usess
->uid
;
1049 ua_sess
->gid
= usess
->gid
;
1051 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s-%d-%s/", app
->name
, app
->pid
,
1054 PERROR("asprintf UST shadow copy session");
1055 /* TODO: We cannot return an error from here.. */
1059 /* TODO: support all UST domain */
1061 /* Iterate over all channels in global domain. */
1062 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1064 struct lttng_ht_iter uiter
;
1066 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1067 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1068 if (ua_chan_node
!= NULL
) {
1069 /* Session exist. Contiuing. */
1073 DBG2("Channel %s not found on shadow session copy, creating it",
1075 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1076 if (ua_chan
== NULL
) {
1077 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1081 shadow_copy_channel(ua_chan
, uchan
);
1082 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1087 * Lookup sesison wrapper.
1090 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1091 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1093 /* Get right UST app session from app */
1094 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
1098 * Return ust app session from the app session hashtable using the UST session
1101 static struct ust_app_session
*lookup_session_by_app(
1102 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1104 struct lttng_ht_iter iter
;
1105 struct lttng_ht_node_ulong
*node
;
1107 __lookup_session_by_app(usess
, app
, &iter
);
1108 node
= lttng_ht_iter_get_node_ulong(&iter
);
1113 return caa_container_of(node
, struct ust_app_session
, node
);
1120 * Create a session on the tracer side for the given app.
1122 * On success, ua_sess_ptr is populated with the session pointer or else left
1123 * untouched. If the session was created, is_created is set to 1. On error,
1124 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1127 * Returns 0 on success or else a negative code which is either -ENOMEM or
1128 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1130 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1131 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1134 int ret
, created
= 0;
1135 struct ust_app_session
*ua_sess
;
1139 assert(ua_sess_ptr
);
1141 health_code_update(&health_thread_cmd
);
1143 ua_sess
= lookup_session_by_app(usess
, app
);
1144 if (ua_sess
== NULL
) {
1145 DBG2("UST app pid: %d session id %d not found, creating it",
1146 app
->pid
, usess
->id
);
1147 ua_sess
= alloc_ust_app_session();
1148 if (ua_sess
== NULL
) {
1149 /* Only malloc can failed so something is really wrong */
1153 shadow_copy_session(ua_sess
, usess
, app
);
1157 health_code_update(&health_thread_cmd
);
1159 if (ua_sess
->handle
== -1) {
1160 ret
= ustctl_create_session(app
->sock
);
1162 ERR("Creating session for app pid %d", app
->pid
);
1163 delete_ust_app_session(-1, ua_sess
);
1164 if (ret
!= -ENOMEM
) {
1166 * Tracer is probably gone or got an internal error so let's
1167 * behave like it will soon unregister or not usable.
1174 ua_sess
->handle
= ret
;
1176 /* Add ust app session to app's HT */
1177 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
1178 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
1180 DBG2("UST app session created successfully with handle %d", ret
);
1183 *ua_sess_ptr
= ua_sess
;
1185 *is_created
= created
;
1187 /* Everything went well. */
1191 health_code_update(&health_thread_cmd
);
1196 * Create a context for the channel on the tracer.
1199 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
1200 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
1201 struct ust_app
*app
)
1204 struct lttng_ht_iter iter
;
1205 struct lttng_ht_node_ulong
*node
;
1206 struct ust_app_ctx
*ua_ctx
;
1208 DBG2("UST app adding context to channel %s", ua_chan
->name
);
1210 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
1211 node
= lttng_ht_iter_get_node_ulong(&iter
);
1217 ua_ctx
= alloc_ust_app_ctx(uctx
);
1218 if (ua_ctx
== NULL
) {
1224 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
1225 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1227 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
1237 * Enable on the tracer side a ust app event for the session and channel.
1240 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
1241 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1245 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1250 ua_event
->enabled
= 1;
1257 * Disable on the tracer side a ust app event for the session and channel.
1259 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1260 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1264 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1269 ua_event
->enabled
= 0;
1276 * Lookup ust app channel for session and disable it on the tracer side.
1279 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1280 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1284 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1289 ua_chan
->enabled
= 0;
1296 * Lookup ust app channel for session and enable it on the tracer side.
1298 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1299 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1302 struct lttng_ht_iter iter
;
1303 struct lttng_ht_node_str
*ua_chan_node
;
1304 struct ust_app_channel
*ua_chan
;
1306 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1307 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1308 if (ua_chan_node
== NULL
) {
1309 DBG2("Unable to find channel %s in ust session id %u",
1310 uchan
->name
, ua_sess
->id
);
1314 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1316 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1326 * Create UST app channel and create it on the tracer. Set ua_chanp of the
1327 * newly created channel if not NULL.
1329 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
1330 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
1331 struct ust_app_channel
**ua_chanp
)
1334 struct lttng_ht_iter iter
;
1335 struct lttng_ht_node_str
*ua_chan_node
;
1336 struct ust_app_channel
*ua_chan
;
1338 /* Lookup channel in the ust app session */
1339 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1340 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1341 if (ua_chan_node
!= NULL
) {
1342 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1346 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1347 if (ua_chan
== NULL
) {
1348 /* Only malloc can fail here */
1352 shadow_copy_channel(ua_chan
, uchan
);
1354 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1356 /* Not found previously means that it does not exist on the tracer */
1357 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
1361 /* Only add the channel if successful on the tracer side. */
1362 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1364 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1369 *ua_chanp
= ua_chan
;
1372 /* Everything went well. */
1376 delete_ust_app_channel(-1, ua_chan
);
1381 * Create UST app event and create it on the tracer side.
1384 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1385 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1386 struct ust_app
*app
)
1389 struct ust_app_event
*ua_event
;
1391 /* Get event node */
1392 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1393 uevent
->filter
, uevent
->attr
.loglevel
);
1394 if (ua_event
!= NULL
) {
1399 /* Does not exist so create one */
1400 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1401 if (ua_event
== NULL
) {
1402 /* Only malloc can failed so something is really wrong */
1406 shadow_copy_event(ua_event
, uevent
);
1408 /* Create it on the tracer side */
1409 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1411 /* Not found previously means that it does not exist on the tracer */
1412 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
1416 add_unique_ust_app_event(ua_chan
->events
, ua_event
);
1418 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1425 /* Valid. Calling here is already in a read side lock */
1426 delete_ust_app_event(-1, ua_event
);
1431 * Create UST metadata and open it on the tracer side.
1433 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1434 char *pathname
, struct ust_app
*app
)
1438 if (ua_sess
->metadata
== NULL
) {
1439 /* Allocate UST metadata */
1440 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1441 if (ua_sess
->metadata
== NULL
) {
1442 /* malloc() failed */
1446 ret
= open_ust_metadata(app
, ua_sess
);
1448 DBG3("Opening metadata failed. Cleaning up memory");
1450 /* Cleanup failed metadata struct */
1451 free(ua_sess
->metadata
);
1453 * This is very important because delete_ust_app_session check if
1454 * the pointer is null or not in order to delete the metadata.
1456 ua_sess
->metadata
= NULL
;
1460 DBG2("UST metadata opened for app pid %d", app
->pid
);
1463 /* Open UST metadata stream */
1464 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1465 ret
= create_ust_metadata_stream(app
, ua_sess
);
1470 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1471 "%s/" DEFAULT_METADATA_NAME
, ua_sess
->path
);
1473 PERROR("asprintf UST create stream");
1477 DBG2("UST metadata stream object created for app pid %d",
1480 ERR("Attempting to create stream without metadata opened");
1491 * Return pointer to traceable apps list.
1493 struct lttng_ht
*ust_app_get_ht(void)
1499 * Return ust app pointer or NULL if not found.
1501 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1503 struct lttng_ht_node_ulong
*node
;
1504 struct lttng_ht_iter iter
;
1507 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1508 node
= lttng_ht_iter_get_node_ulong(&iter
);
1510 DBG2("UST app no found with pid %d", pid
);
1515 DBG2("Found UST app by pid %d", pid
);
1517 return caa_container_of(node
, struct ust_app
, pid_n
);
1525 * Using pid and uid (of the app), allocate a new ust_app struct and
1526 * add it to the global traceable app list.
1528 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1529 * bitness is not supported.
1531 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1533 struct ust_app
*lta
;
1536 if ((msg
->bits_per_long
== 64 &&
1537 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
1538 || (msg
->bits_per_long
== 32 &&
1539 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
1540 ERR("Registration failed: application \"%s\" (pid: %d) has "
1541 "%d-bit long, but no consumerd for this long size is available.\n",
1542 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1547 lttng_fd_put(LTTNG_FD_APPS
, 1);
1550 if (msg
->major
!= LTTNG_UST_COMM_MAJOR
) {
1551 ERR("Registration failed: application \"%s\" (pid: %d) has "
1552 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1553 msg
->name
, msg
->pid
, msg
->major
, msg
->minor
);
1558 lttng_fd_put(LTTNG_FD_APPS
, 1);
1561 lta
= zmalloc(sizeof(struct ust_app
));
1567 lta
->ppid
= msg
->ppid
;
1568 lta
->uid
= msg
->uid
;
1569 lta
->gid
= msg
->gid
;
1570 lta
->compatible
= 0; /* Not compatible until proven */
1571 lta
->bits_per_long
= msg
->bits_per_long
;
1572 lta
->v_major
= msg
->major
;
1573 lta
->v_minor
= msg
->minor
;
1574 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1575 lta
->name
[16] = '\0';
1576 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1578 lta
->pid
= msg
->pid
;
1579 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long)lta
->pid
);
1581 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long)lta
->sock
);
1583 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
1588 * On a re-registration, we want to kick out the previous registration of
1591 lttng_ht_add_replace_ulong(ust_app_ht
, <a
->pid_n
);
1594 * The socket _should_ be unique until _we_ call close. So, a add_unique
1595 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1596 * already in the table.
1598 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, <a
->sock_n
);
1602 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1603 " (version %d.%d)", lta
->pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1604 lta
->sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1610 * Unregister app by removing it from the global traceable app list and freeing
1613 * The socket is already closed at this point so no close to sock.
1615 void ust_app_unregister(int sock
)
1617 struct ust_app
*lta
;
1618 struct lttng_ht_node_ulong
*node
;
1619 struct lttng_ht_iter iter
;
1620 struct ust_app_session
*ua_sess
;
1625 /* Get the node reference for a call_rcu */
1626 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1627 node
= lttng_ht_iter_get_node_ulong(&iter
);
1629 ERR("Unable to find app by sock %d", sock
);
1633 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
1635 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
1637 /* Remove application from PID hash table */
1638 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
1641 /* Assign second node for deletion */
1642 iter
.iter
.node
= <a
->pid_n
.node
;
1645 * Ignore return value since the node might have been removed before by an
1646 * add replace during app registration because the PID can be reassigned by
1649 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1651 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
1655 /* Remove sessions so they are not visible during deletion.*/
1656 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
1658 ret
= lttng_ht_del(lta
->sessions
, &iter
);
1660 /* The session was already removed so scheduled for teardown. */
1665 * Add session to list for teardown. This is safe since at this point we
1666 * are the only one using this list.
1668 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
1672 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
1680 * Return traceable_app_count
1682 unsigned long ust_app_list_count(void)
1684 unsigned long count
;
1687 count
= lttng_ht_get_count(ust_app_ht
);
1694 * Fill events array with all events name of all registered apps.
1696 int ust_app_list_events(struct lttng_event
**events
)
1699 size_t nbmem
, count
= 0;
1700 struct lttng_ht_iter iter
;
1701 struct ust_app
*app
;
1702 struct lttng_event
*tmp_event
;
1704 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1705 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1706 if (tmp_event
== NULL
) {
1707 PERROR("zmalloc ust app events");
1714 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1715 struct lttng_ust_tracepoint_iter uiter
;
1717 health_code_update(&health_thread_cmd
);
1719 if (!app
->compatible
) {
1721 * TODO: In time, we should notice the caller of this error by
1722 * telling him that this is a version error.
1726 handle
= ustctl_tracepoint_list(app
->sock
);
1728 ERR("UST app list events getting handle failed for app pid %d",
1733 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
1734 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
1735 health_code_update(&health_thread_cmd
);
1736 if (count
>= nbmem
) {
1737 /* In case the realloc fails, we free the memory */
1740 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1743 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event
));
1745 PERROR("realloc ust app events");
1752 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1753 tmp_event
[count
].loglevel
= uiter
.loglevel
;
1754 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
1755 tmp_event
[count
].pid
= app
->pid
;
1756 tmp_event
[count
].enabled
= -1;
1762 *events
= tmp_event
;
1764 DBG2("UST app list events done (%zu events)", count
);
1769 health_code_update(&health_thread_cmd
);
1774 * Fill events array with all events name of all registered apps.
1776 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
1779 size_t nbmem
, count
= 0;
1780 struct lttng_ht_iter iter
;
1781 struct ust_app
*app
;
1782 struct lttng_event_field
*tmp_event
;
1784 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1785 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
1786 if (tmp_event
== NULL
) {
1787 PERROR("zmalloc ust app event fields");
1794 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1795 struct lttng_ust_field_iter uiter
;
1797 health_code_update(&health_thread_cmd
);
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_field_list(app
->sock
);
1808 ERR("UST app list event fields getting handle failed for app pid %d",
1813 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
1814 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
1815 health_code_update(&health_thread_cmd
);
1816 if (count
>= nbmem
) {
1817 /* In case the realloc fails, we free the memory */
1820 DBG2("Reallocating event field list from %zu to %zu entries", nbmem
,
1823 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event_field
));
1825 PERROR("realloc ust app event fields");
1833 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
1834 tmp_event
[count
].type
= uiter
.type
;
1835 tmp_event
[count
].nowrite
= uiter
.nowrite
;
1837 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1838 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
1839 tmp_event
[count
].event
.type
= LTTNG_UST_TRACEPOINT
;
1840 tmp_event
[count
].event
.pid
= app
->pid
;
1841 tmp_event
[count
].event
.enabled
= -1;
1847 *fields
= tmp_event
;
1849 DBG2("UST app list event fields done (%zu events)", count
);
1854 health_code_update(&health_thread_cmd
);
1859 * Free and clean all traceable apps of the global list.
1861 void ust_app_clean_list(void)
1864 struct ust_app
*app
;
1865 struct lttng_ht_iter iter
;
1867 DBG2("UST app cleaning registered apps hash table");
1871 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1872 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1874 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
1877 /* Cleanup socket hash table */
1878 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
1880 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
1884 /* Destroy is done only when the ht is empty */
1885 lttng_ht_destroy(ust_app_ht
);
1886 lttng_ht_destroy(ust_app_ht_by_sock
);
1892 * Init UST app hash table.
1894 void ust_app_ht_alloc(void)
1896 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1897 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1901 * For a specific UST session, disable the channel for all registered apps.
1903 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1904 struct ltt_ust_channel
*uchan
)
1907 struct lttng_ht_iter iter
;
1908 struct lttng_ht_node_str
*ua_chan_node
;
1909 struct ust_app
*app
;
1910 struct ust_app_session
*ua_sess
;
1911 struct ust_app_channel
*ua_chan
;
1913 if (usess
== NULL
|| uchan
== NULL
) {
1914 ERR("Disabling UST global channel with NULL values");
1919 DBG2("UST app disabling channel %s from global domain for session id %d",
1920 uchan
->name
, usess
->id
);
1924 /* For every registered applications */
1925 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1926 struct lttng_ht_iter uiter
;
1927 if (!app
->compatible
) {
1929 * TODO: In time, we should notice the caller of this error by
1930 * telling him that this is a version error.
1934 ua_sess
= lookup_session_by_app(usess
, app
);
1935 if (ua_sess
== NULL
) {
1940 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1941 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1942 /* If the session if found for the app, the channel must be there */
1943 assert(ua_chan_node
);
1945 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1946 /* The channel must not be already disabled */
1947 assert(ua_chan
->enabled
== 1);
1949 /* Disable channel onto application */
1950 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1952 /* XXX: We might want to report this error at some point... */
1964 * For a specific UST session, enable the channel for all registered apps.
1966 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1967 struct ltt_ust_channel
*uchan
)
1970 struct lttng_ht_iter iter
;
1971 struct ust_app
*app
;
1972 struct ust_app_session
*ua_sess
;
1974 if (usess
== NULL
|| uchan
== NULL
) {
1975 ERR("Adding UST global channel to NULL values");
1980 DBG2("UST app enabling channel %s to global domain for session id %d",
1981 uchan
->name
, usess
->id
);
1985 /* For every registered applications */
1986 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1987 if (!app
->compatible
) {
1989 * TODO: In time, we should notice the caller of this error by
1990 * telling him that this is a version error.
1994 ua_sess
= lookup_session_by_app(usess
, app
);
1995 if (ua_sess
== NULL
) {
1999 /* Enable channel onto application */
2000 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
2002 /* XXX: We might want to report this error at some point... */
2014 * Disable an event in a channel and for a specific session.
2016 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
2017 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2020 struct lttng_ht_iter iter
, uiter
;
2021 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2022 struct ust_app
*app
;
2023 struct ust_app_session
*ua_sess
;
2024 struct ust_app_channel
*ua_chan
;
2025 struct ust_app_event
*ua_event
;
2027 DBG("UST app disabling event %s for all apps in channel "
2028 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
2032 /* For all registered applications */
2033 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2034 if (!app
->compatible
) {
2036 * TODO: In time, we should notice the caller of this error by
2037 * telling him that this is a version error.
2041 ua_sess
= lookup_session_by_app(usess
, app
);
2042 if (ua_sess
== NULL
) {
2047 /* Lookup channel in the ust app session */
2048 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2049 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2050 if (ua_chan_node
== NULL
) {
2051 DBG2("Channel %s not found in session id %d for app pid %d."
2052 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
2055 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2057 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2058 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2059 if (ua_event_node
== NULL
) {
2060 DBG2("Event %s not found in channel %s for app pid %d."
2061 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
2064 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2066 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2068 /* XXX: Report error someday... */
2079 * For a specific UST session and UST channel, the event for all
2082 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
2083 struct ltt_ust_channel
*uchan
)
2086 struct lttng_ht_iter iter
, uiter
;
2087 struct lttng_ht_node_str
*ua_chan_node
;
2088 struct ust_app
*app
;
2089 struct ust_app_session
*ua_sess
;
2090 struct ust_app_channel
*ua_chan
;
2091 struct ust_app_event
*ua_event
;
2093 DBG("UST app disabling all event for all apps in channel "
2094 "%s for session id %d", uchan
->name
, usess
->id
);
2098 /* For all registered applications */
2099 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2100 if (!app
->compatible
) {
2102 * TODO: In time, we should notice the caller of this error by
2103 * telling him that this is a version error.
2107 ua_sess
= lookup_session_by_app(usess
, app
);
2109 /* The application has problem or is probably dead. */
2113 /* Lookup channel in the ust app session */
2114 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2115 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2116 /* If the channel is not found, there is a code flow error */
2117 assert(ua_chan_node
);
2119 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2121 /* Disable each events of channel */
2122 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2124 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2126 /* XXX: Report error someday... */
2138 * For a specific UST session, create the channel for all registered apps.
2140 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
2141 struct ltt_ust_channel
*uchan
)
2143 int ret
= 0, created
;
2144 struct lttng_ht_iter iter
;
2145 struct ust_app
*app
;
2146 struct ust_app_session
*ua_sess
= NULL
;
2148 /* Very wrong code flow */
2152 DBG2("UST app adding channel %s to global domain for session id %d",
2153 uchan
->name
, usess
->id
);
2157 /* For every registered applications */
2158 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2159 if (!app
->compatible
) {
2161 * TODO: In time, we should notice the caller of this error by
2162 * telling him that this is a version error.
2167 * Create session on the tracer side and add it to app session HT. Note
2168 * that if session exist, it will simply return a pointer to the ust
2171 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
2176 * The application's socket is not valid. Either a bad socket
2177 * or a timeout on it. We can't inform the caller that for a
2178 * specific app, the session failed so lets continue here.
2183 goto error_rcu_unlock
;
2188 /* Create channel onto application. We don't need the chan ref. */
2189 ret
= create_ust_app_channel(ua_sess
, uchan
, app
, NULL
);
2191 if (ret
== -ENOMEM
) {
2192 /* No more memory is a fatal error. Stop right now. */
2193 goto error_rcu_unlock
;
2195 /* Cleanup the created session if it's the case. */
2197 delete_ust_app_session(app
->sock
, ua_sess
);
2208 * Enable event for a specific session and channel on the tracer.
2210 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
2211 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2214 struct lttng_ht_iter iter
, uiter
;
2215 struct lttng_ht_node_str
*ua_chan_node
;
2216 struct ust_app
*app
;
2217 struct ust_app_session
*ua_sess
;
2218 struct ust_app_channel
*ua_chan
;
2219 struct ust_app_event
*ua_event
;
2221 DBG("UST app enabling event %s for all apps for session id %d",
2222 uevent
->attr
.name
, usess
->id
);
2225 * NOTE: At this point, this function is called only if the session and
2226 * channel passed are already created for all apps. and enabled on the
2232 /* For all registered applications */
2233 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2234 if (!app
->compatible
) {
2236 * TODO: In time, we should notice the caller of this error by
2237 * telling him that this is a version error.
2241 ua_sess
= lookup_session_by_app(usess
, app
);
2243 /* The application has problem or is probably dead. */
2247 /* Lookup channel in the ust app session */
2248 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2249 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2250 /* If the channel is not found, there is a code flow error */
2251 assert(ua_chan_node
);
2253 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2255 /* Get event node */
2256 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2257 uevent
->filter
, uevent
->attr
.loglevel
);
2258 if (ua_event
== NULL
) {
2259 DBG3("UST app enable event %s not found for app PID %d."
2260 "Skipping app", uevent
->attr
.name
, app
->pid
);
2264 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2276 * For a specific existing UST session and UST channel, creates the event for
2277 * all registered apps.
2279 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
2280 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2283 struct lttng_ht_iter iter
, uiter
;
2284 struct lttng_ht_node_str
*ua_chan_node
;
2285 struct ust_app
*app
;
2286 struct ust_app_session
*ua_sess
;
2287 struct ust_app_channel
*ua_chan
;
2289 DBG("UST app creating event %s for all apps for session id %d",
2290 uevent
->attr
.name
, usess
->id
);
2294 /* For all registered applications */
2295 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2296 if (!app
->compatible
) {
2298 * TODO: In time, we should notice the caller of this error by
2299 * telling him that this is a version error.
2303 ua_sess
= lookup_session_by_app(usess
, app
);
2305 /* The application has problem or is probably dead. */
2309 /* Lookup channel in the ust app session */
2310 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2311 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2312 /* If the channel is not found, there is a code flow error */
2313 assert(ua_chan_node
);
2315 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2317 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2319 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
2320 /* Possible value at this point: -ENOMEM. If so, we stop! */
2323 DBG2("UST app event %s already exist on app PID %d",
2324 uevent
->attr
.name
, app
->pid
);
2335 * Start tracing for a specific UST session and app.
2337 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2340 struct lttng_ht_iter iter
;
2341 struct ust_app_session
*ua_sess
;
2342 struct ust_app_channel
*ua_chan
;
2343 struct ust_app_stream
*ustream
;
2344 struct consumer_socket
*socket
;
2346 DBG("Starting tracing for ust app pid %d", app
->pid
);
2350 if (!app
->compatible
) {
2354 ua_sess
= lookup_session_by_app(usess
, app
);
2355 if (ua_sess
== NULL
) {
2356 /* The session is in teardown process. Ignore and continue. */
2360 /* Upon restart, we skip the setup, already done */
2361 if (ua_sess
->started
) {
2365 /* Create directories if consumer is LOCAL and has a path defined. */
2366 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2367 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
2368 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
2369 S_IRWXU
| S_IRWXG
, usess
->uid
, usess
->gid
);
2371 if (ret
!= -EEXIST
) {
2372 ERR("Trace directory creation error");
2374 goto error_rcu_unlock
;
2379 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
2381 ret
= LTTNG_ERR_UST_META_FAIL
;
2382 goto error_rcu_unlock
;
2385 /* For each channel */
2386 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2388 /* Create all streams */
2390 /* Create UST stream */
2391 ustream
= alloc_ust_app_stream();
2392 if (ustream
== NULL
) {
2393 goto error_rcu_unlock
;
2396 health_code_update(&health_thread_cmd
);
2398 ret
= create_ust_stream(app
, ua_chan
, ustream
);
2400 /* Free unused memory after this point. */
2402 if (ret
== -LTTNG_UST_ERR_NOENT
) {
2403 /* Got all streams. Continue normal execution. */
2406 /* Error at this point. Stop everything. */
2407 ret
= LTTNG_ERR_UST_STREAM_FAIL
;
2408 goto error_rcu_unlock
;
2411 health_code_update(&health_thread_cmd
);
2413 /* Order is important this is why a list is used. */
2414 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
2415 ua_chan
->streams
.count
++;
2417 DBG2("UST stream %d ready (handle: %d)", ua_chan
->streams
.count
,
2421 health_code_update(&health_thread_cmd
);
2424 switch (app
->bits_per_long
) {
2426 socket
= consumer_find_socket(uatomic_read(&ust_consumerd64_fd
),
2428 if (socket
== NULL
) {
2433 socket
= consumer_find_socket(uatomic_read(&ust_consumerd32_fd
),
2435 if (socket
== NULL
) {
2441 goto error_rcu_unlock
;
2444 /* Setup UST consumer socket and send fds to it */
2445 ret
= ust_consumer_send_session(ua_sess
, usess
->consumer
, socket
);
2447 goto error_rcu_unlock
;
2450 health_code_update(&health_thread_cmd
);
2453 /* This start the UST tracing */
2454 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
2456 ERR("Error starting tracing for app pid: %d (ret: %d)", app
->pid
, ret
);
2457 goto error_rcu_unlock
;
2460 /* Indicate that the session has been started once */
2461 ua_sess
->started
= 1;
2463 health_code_update(&health_thread_cmd
);
2465 /* Quiescent wait after starting trace */
2466 ustctl_wait_quiescent(app
->sock
);
2470 health_code_update(&health_thread_cmd
);
2475 health_code_update(&health_thread_cmd
);
2480 * Stop tracing for a specific UST session and app.
2482 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2485 struct lttng_ht_iter iter
;
2486 struct ust_app_session
*ua_sess
;
2487 struct ust_app_channel
*ua_chan
;
2489 DBG("Stopping tracing for ust app pid %d", app
->pid
);
2493 if (!app
->compatible
) {
2497 ua_sess
= lookup_session_by_app(usess
, app
);
2498 if (ua_sess
== NULL
) {
2503 * If started = 0, it means that stop trace has been called for a session
2504 * that was never started. It's possible since we can have a fail start
2505 * from either the application manager thread or the command thread. Simply
2506 * indicate that this is a stop error.
2508 if (!ua_sess
->started
) {
2509 goto error_rcu_unlock
;
2512 health_code_update(&health_thread_cmd
);
2514 /* This inhibits UST tracing */
2515 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
2517 ERR("Error stopping tracing for app pid: %d (ret: %d)", app
->pid
, ret
);
2518 goto error_rcu_unlock
;
2521 health_code_update(&health_thread_cmd
);
2523 /* Quiescent wait after stopping trace */
2524 ustctl_wait_quiescent(app
->sock
);
2526 health_code_update(&health_thread_cmd
);
2528 /* Flushing buffers */
2529 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2531 health_code_update(&health_thread_cmd
);
2532 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_chan
->obj
);
2534 ERR("UST app PID %d channel %s flush failed with ret %d",
2535 app
->pid
, ua_chan
->name
, ret
);
2536 /* Continuing flushing all buffers */
2541 health_code_update(&health_thread_cmd
);
2543 /* Flush all buffers before stopping */
2544 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_sess
->metadata
->obj
);
2546 ERR("UST app PID %d metadata flush failed with ret %d", app
->pid
,
2552 health_code_update(&health_thread_cmd
);
2557 health_code_update(&health_thread_cmd
);
2562 * Destroy a specific UST session in apps.
2564 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2566 struct ust_app_session
*ua_sess
;
2567 struct lttng_ust_object_data obj
;
2568 struct lttng_ht_iter iter
;
2569 struct lttng_ht_node_ulong
*node
;
2572 DBG("Destroy tracing for ust app pid %d", app
->pid
);
2576 if (!app
->compatible
) {
2580 __lookup_session_by_app(usess
, app
, &iter
);
2581 node
= lttng_ht_iter_get_node_ulong(&iter
);
2583 /* Session is being or is deleted. */
2586 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2587 ret
= lttng_ht_del(app
->sessions
, &iter
);
2589 /* Already scheduled for teardown. */
2593 obj
.handle
= ua_sess
->handle
;
2596 obj
.memory_map_size
= 0;
2597 health_code_update(&health_thread_cmd
);
2598 ustctl_release_object(app
->sock
, &obj
);
2600 health_code_update(&health_thread_cmd
);
2601 delete_ust_app_session(app
->sock
, ua_sess
);
2603 /* Quiescent wait after stopping trace */
2604 ustctl_wait_quiescent(app
->sock
);
2608 health_code_update(&health_thread_cmd
);
2613 * Start tracing for the UST session.
2615 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2618 struct lttng_ht_iter iter
;
2619 struct ust_app
*app
;
2621 DBG("Starting all UST traces");
2625 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2626 ret
= ust_app_start_trace(usess
, app
);
2628 /* Continue to next apps even on error */
2639 * Start tracing for the UST session.
2641 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2644 struct lttng_ht_iter iter
;
2645 struct ust_app
*app
;
2647 DBG("Stopping all UST traces");
2651 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2652 ret
= ust_app_stop_trace(usess
, app
);
2654 /* Continue to next apps even on error */
2665 * Destroy app UST session.
2667 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2670 struct lttng_ht_iter iter
;
2671 struct ust_app
*app
;
2673 DBG("Destroy all UST traces");
2677 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2678 ret
= destroy_trace(usess
, app
);
2680 /* Continue to next apps even on error */
2691 * Add channels/events from UST global domain to registered apps at sock.
2693 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2696 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
2697 struct ust_app
*app
;
2698 struct ust_app_session
*ua_sess
= NULL
;
2699 struct ust_app_channel
*ua_chan
;
2700 struct ust_app_event
*ua_event
;
2701 struct ust_app_ctx
*ua_ctx
;
2705 DBG2("UST app global update for app sock %d for session id %d", sock
,
2710 app
= find_app_by_sock(sock
);
2712 ERR("Failed to update app sock %d", sock
);
2716 if (!app
->compatible
) {
2720 ret
= create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
2722 /* Tracer is probably gone or ENOMEM. */
2728 * We can iterate safely here over all UST app session sicne the create ust
2729 * app session above made a shadow copy of the UST global domain from the
2732 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2734 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2736 /* FIXME: Should we quit here or continue... */
2740 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
2742 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2744 /* FIXME: Should we quit here or continue... */
2750 /* For each events */
2751 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2753 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2755 /* FIXME: Should we quit here or continue... */
2761 if (usess
->start_trace
) {
2762 ret
= ust_app_start_trace(usess
, app
);
2767 DBG2("UST trace started for app pid %d", app
->pid
);
2776 * Add context to a specific channel for global UST domain.
2778 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2779 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2782 struct lttng_ht_node_str
*ua_chan_node
;
2783 struct lttng_ht_iter iter
, uiter
;
2784 struct ust_app_channel
*ua_chan
= NULL
;
2785 struct ust_app_session
*ua_sess
;
2786 struct ust_app
*app
;
2790 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2791 if (!app
->compatible
) {
2793 * TODO: In time, we should notice the caller of this error by
2794 * telling him that this is a version error.
2798 ua_sess
= lookup_session_by_app(usess
, app
);
2799 if (ua_sess
== NULL
) {
2803 /* Lookup channel in the ust app session */
2804 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2805 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2806 if (ua_chan_node
== NULL
) {
2809 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2812 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2823 * Enable event for a channel from a UST session for a specific PID.
2825 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2826 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2829 struct lttng_ht_iter iter
;
2830 struct lttng_ht_node_str
*ua_chan_node
;
2831 struct ust_app
*app
;
2832 struct ust_app_session
*ua_sess
;
2833 struct ust_app_channel
*ua_chan
;
2834 struct ust_app_event
*ua_event
;
2836 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2840 app
= ust_app_find_by_pid(pid
);
2842 ERR("UST app enable event per PID %d not found", pid
);
2847 if (!app
->compatible
) {
2852 ua_sess
= lookup_session_by_app(usess
, app
);
2854 /* The application has problem or is probably dead. */
2858 /* Lookup channel in the ust app session */
2859 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2860 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2861 /* If the channel is not found, there is a code flow error */
2862 assert(ua_chan_node
);
2864 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2866 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2867 uevent
->filter
, uevent
->attr
.loglevel
);
2868 if (ua_event
== NULL
) {
2869 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2874 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2886 * Disable event for a channel from a UST session for a specific PID.
2888 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2889 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2892 struct lttng_ht_iter iter
;
2893 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2894 struct ust_app
*app
;
2895 struct ust_app_session
*ua_sess
;
2896 struct ust_app_channel
*ua_chan
;
2897 struct ust_app_event
*ua_event
;
2899 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2903 app
= ust_app_find_by_pid(pid
);
2905 ERR("UST app disable event per PID %d not found", pid
);
2910 if (!app
->compatible
) {
2915 ua_sess
= lookup_session_by_app(usess
, app
);
2917 /* The application has problem or is probably dead. */
2921 /* Lookup channel in the ust app session */
2922 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2923 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2924 if (ua_chan_node
== NULL
) {
2925 /* Channel does not exist, skip disabling */
2928 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2930 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2931 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2932 if (ua_event_node
== NULL
) {
2933 /* Event does not exist, skip disabling */
2936 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2938 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2949 * Validate version of UST apps and set the compatible bit.
2951 int ust_app_validate_version(int sock
)
2954 struct ust_app
*app
;
2958 app
= find_app_by_sock(sock
);
2961 health_code_update(&health_thread_cmd
);
2963 ret
= ustctl_tracer_version(sock
, &app
->version
);
2968 /* Validate version */
2969 if (app
->version
.major
!= UST_APP_MAJOR_VERSION
) {
2973 DBG2("UST app PID %d is compatible with internal major version %d "
2974 "(supporting == %d)", app
->pid
, app
->version
.major
,
2975 UST_APP_MAJOR_VERSION
);
2976 app
->compatible
= 1;
2978 health_code_update(&health_thread_cmd
);
2982 DBG2("UST app PID %d is not compatible with internal major version %d "
2983 "(supporting == %d)", app
->pid
, app
->version
.major
,
2984 UST_APP_MAJOR_VERSION
);
2985 app
->compatible
= 0;
2987 health_code_update(&health_thread_cmd
);
2992 * Calibrate registered applications.
2994 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
2997 struct lttng_ht_iter iter
;
2998 struct ust_app
*app
;
3002 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3003 if (!app
->compatible
) {
3005 * TODO: In time, we should notice the caller of this error by
3006 * telling him that this is a version error.
3011 health_code_update(&health_thread_cmd
);
3013 ret
= ustctl_calibrate(app
->sock
, calibrate
);
3017 /* Means that it's not implemented on the tracer side. */
3021 /* TODO: Report error to user */
3022 DBG2("Calibrate app PID %d returned with error %d",
3029 DBG("UST app global domain calibration finished");
3033 health_code_update(&health_thread_cmd
);