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 ltt_ust_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 ltt_ust_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 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
373 DBG3("UST app channel %s allocated", ua_chan
->name
);
382 * Alloc new UST app event.
385 struct ust_app_event
*alloc_ust_app_event(char *name
,
386 struct lttng_ust_event
*attr
)
388 struct ust_app_event
*ua_event
;
390 /* Init most of the default value by allocating and zeroing */
391 ua_event
= zmalloc(sizeof(struct ust_app_event
));
392 if (ua_event
== NULL
) {
397 ua_event
->enabled
= 1;
398 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
399 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
400 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
402 /* Copy attributes */
404 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
407 DBG3("UST app event %s allocated", ua_event
->name
);
416 * Alloc new UST app context.
419 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
421 struct ust_app_ctx
*ua_ctx
;
423 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
424 if (ua_ctx
== NULL
) {
429 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
432 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
439 * Allocate a filter and copy the given original filter.
441 * Return allocated filter or NULL on error.
443 static struct lttng_ust_filter_bytecode
*alloc_copy_ust_app_filter(
444 struct lttng_ust_filter_bytecode
*orig_f
)
446 struct lttng_ust_filter_bytecode
*filter
= NULL
;
448 /* Copy filter bytecode */
449 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
451 PERROR("zmalloc alloc ust app filter");
455 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
462 * Find an ust_app using the sock and return it. RCU read side lock must be
463 * held before calling this helper function.
466 struct ust_app
*find_app_by_sock(int sock
)
468 struct lttng_ht_node_ulong
*node
;
469 struct lttng_ht_iter iter
;
471 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
472 node
= lttng_ht_iter_get_node_ulong(&iter
);
474 DBG2("UST app find by sock %d not found", sock
);
478 return caa_container_of(node
, struct ust_app
, sock_n
);
485 * Lookup for an ust app event based on event name, filter bytecode and the
488 * Return an ust_app_event object or NULL on error.
490 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
491 char *name
, struct lttng_ust_filter_bytecode
*filter
, int loglevel
)
493 struct lttng_ht_iter iter
;
494 struct lttng_ht_node_str
*node
;
495 struct ust_app_event
*event
= NULL
;
496 struct ust_app_ht_key key
;
501 /* Setup key for event lookup. */
504 key
.loglevel
= loglevel
;
506 /* Lookup using the event name as hash and a custom match fct. */
507 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
508 ht_match_ust_app_event
, &key
, &iter
.iter
);
509 node
= lttng_ht_iter_get_node_str(&iter
);
514 event
= caa_container_of(node
, struct ust_app_event
, node
);
521 * Create the channel context on the tracer.
524 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
525 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
529 health_code_update(&health_thread_cmd
);
531 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
532 ua_chan
->obj
, &ua_ctx
->obj
);
537 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
539 DBG2("UST app context created successfully for channel %s", ua_chan
->name
);
542 health_code_update(&health_thread_cmd
);
547 * Set the filter on the tracer.
550 int set_ust_event_filter(struct ust_app_event
*ua_event
,
555 health_code_update(&health_thread_cmd
);
557 if (!ua_event
->filter
) {
562 ret
= ustctl_set_filter(app
->sock
, ua_event
->filter
,
568 DBG2("UST filter set successfully for event %s", ua_event
->name
);
571 health_code_update(&health_thread_cmd
);
576 * Disable the specified event on to UST tracer for the UST session.
578 static int disable_ust_event(struct ust_app
*app
,
579 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
583 health_code_update(&health_thread_cmd
);
585 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
587 ERR("UST app event %s disable failed for app (pid: %d) "
588 "and session handle %d with ret %d",
589 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
593 DBG2("UST app event %s disabled successfully for app (pid: %d)",
594 ua_event
->attr
.name
, app
->pid
);
597 health_code_update(&health_thread_cmd
);
602 * Disable the specified channel on to UST tracer for the UST session.
604 static int disable_ust_channel(struct ust_app
*app
,
605 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
609 health_code_update(&health_thread_cmd
);
611 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
613 ERR("UST app channel %s disable failed for app (pid: %d) "
614 "and session handle %d with ret %d",
615 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
619 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
620 ua_chan
->name
, app
->pid
);
623 health_code_update(&health_thread_cmd
);
628 * Enable the specified channel on to UST tracer for the UST session.
630 static int enable_ust_channel(struct ust_app
*app
,
631 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
635 health_code_update(&health_thread_cmd
);
637 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
639 ERR("UST app channel %s enable failed for app (pid: %d) "
640 "and session handle %d with ret %d",
641 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
645 ua_chan
->enabled
= 1;
647 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
648 ua_chan
->name
, app
->pid
);
651 health_code_update(&health_thread_cmd
);
656 * Enable the specified event on to UST tracer for the UST session.
658 static int enable_ust_event(struct ust_app
*app
,
659 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
663 health_code_update(&health_thread_cmd
);
665 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
667 ERR("UST app event %s enable failed for app (pid: %d) "
668 "and session handle %d with ret %d",
669 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
673 DBG2("UST app event %s enabled successfully for app (pid: %d)",
674 ua_event
->attr
.name
, app
->pid
);
677 health_code_update(&health_thread_cmd
);
682 * Open metadata onto the UST tracer for a UST session.
684 static int open_ust_metadata(struct ust_app
*app
,
685 struct ust_app_session
*ua_sess
)
688 struct lttng_ust_channel_attr uattr
;
690 health_code_update(&health_thread_cmd
);
692 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
693 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
694 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
695 uattr
.switch_timer_interval
=
696 ua_sess
->metadata
->attr
.switch_timer_interval
;
697 uattr
.read_timer_interval
=
698 ua_sess
->metadata
->attr
.read_timer_interval
;
699 uattr
.output
= ua_sess
->metadata
->attr
.output
;
701 /* We are going to receive 2 fds, we need to reserve them. */
702 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
704 ERR("Exhausted number of available FD upon metadata open");
707 /* UST tracer metadata creation */
708 ret
= ustctl_open_metadata(app
->sock
, ua_sess
->handle
, &uattr
,
709 &ua_sess
->metadata
->obj
);
711 ERR("UST app open metadata failed for app pid:%d with ret %d",
716 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
719 health_code_update(&health_thread_cmd
);
724 * Create metadata stream onto the UST tracer for a given session.
726 static int create_ust_metadata_stream(struct ust_app
*app
,
727 struct ust_app_session
*ua_sess
)
731 health_code_update(&health_thread_cmd
);
733 /* We are going to receive 2 fds, we need to reserve them. */
734 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
736 ERR("Exhausted number of available FD upon metadata stream create");
739 ret
= ustctl_create_stream(app
->sock
, ua_sess
->metadata
->obj
,
740 &ua_sess
->metadata
->stream_obj
);
742 lttng_fd_put(LTTNG_FD_APPS
, 2);
743 ERR("UST create metadata stream failed");
748 health_code_update(&health_thread_cmd
);
753 * Create stream onto the UST tracer for a given channel.
755 * Return -ENOENT if no more stream is available for this channel.
756 * On success, return 0.
757 * On error, return a negative value.
759 static int create_ust_stream(struct ust_app
*app
,
760 struct ust_app_channel
*ua_chan
, struct ltt_ust_stream
*stream
)
766 assert(ua_chan
->obj
);
769 health_code_update(&health_thread_cmd
);
771 /* We are going to receive 2 fds, we need to reserve them. */
772 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
774 ERR("Exhausted number of available FD on stream creation");
775 /* Just to make sure we never return -ENOENT. */
781 * Set the stream name before creating it. On error, we don't have to
782 * delete it on the tracer side.
784 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%u",
785 ua_chan
->name
, ua_chan
->streams
.count
);
787 /* Without the stream name we can't continue using it. */
788 PERROR("snprintf UST create stream");
789 /* Just to make sure we never return -ENOENT. */
794 ret
= ustctl_create_stream(app
->sock
, ua_chan
->obj
, &stream
->obj
);
796 lttng_fd_put(LTTNG_FD_APPS
, 2);
797 /* Indicates that there is no more stream for that channel. */
798 if (ret
!= -LTTNG_UST_ERR_NOENT
) {
799 ERR("UST create metadata stream failed (ret: %d)", ret
);
804 /* Set stream handle with the returned value. */
805 stream
->handle
= stream
->obj
->handle
;
808 health_code_update(&health_thread_cmd
);
813 * Create the specified channel onto the UST tracer for a UST session.
815 static int create_ust_channel(struct ust_app
*app
,
816 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
820 health_code_update(&health_thread_cmd
);
822 /* TODO: remove cast and use lttng-ust-abi.h */
824 /* We are going to receive 2 fds, we need to reserve them. */
825 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
827 ERR("Exhausted number of available FD upon create channel");
831 health_code_update(&health_thread_cmd
);
833 ret
= ustctl_create_channel(app
->sock
, ua_sess
->handle
,
834 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
836 ERR("Creating channel %s for app (pid: %d, sock: %d) "
837 "and session handle %d with ret %d",
838 ua_chan
->name
, app
->pid
, app
->sock
,
839 ua_sess
->handle
, ret
);
840 lttng_fd_put(LTTNG_FD_APPS
, 2);
844 ua_chan
->handle
= ua_chan
->obj
->handle
;
846 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
847 ua_chan
->name
, app
->pid
, app
->sock
);
849 health_code_update(&health_thread_cmd
);
851 /* If channel is not enabled, disable it on the tracer */
852 if (!ua_chan
->enabled
) {
853 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
860 health_code_update(&health_thread_cmd
);
865 * Create the specified event onto the UST tracer for a UST session.
868 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
869 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
873 health_code_update(&health_thread_cmd
);
875 /* Create UST event on tracer */
876 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
879 ERR("Error ustctl create event %s for app pid: %d with ret %d",
880 ua_event
->attr
.name
, app
->pid
, ret
);
884 ua_event
->handle
= ua_event
->obj
->handle
;
886 DBG2("UST app event %s created successfully for pid:%d",
887 ua_event
->attr
.name
, app
->pid
);
889 health_code_update(&health_thread_cmd
);
891 /* Set filter if one is present. */
892 if (ua_event
->filter
) {
893 ret
= set_ust_event_filter(ua_event
, app
);
899 /* If event not enabled, disable it on the tracer */
900 if (ua_event
->enabled
== 0) {
901 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
904 * If we hit an EPERM, something is wrong with our disable call. If
905 * we get an EEXIST, there is a problem on the tracer side since we
909 case -LTTNG_UST_ERR_PERM
:
910 /* Code flow problem */
912 case -LTTNG_UST_ERR_EXIST
:
913 /* It's OK for our use case. */
924 health_code_update(&health_thread_cmd
);
929 * Copy data between an UST app event and a LTT event.
931 static void shadow_copy_event(struct ust_app_event
*ua_event
,
932 struct ltt_ust_event
*uevent
)
934 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
935 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
937 ua_event
->enabled
= uevent
->enabled
;
939 /* Copy event attributes */
940 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
942 /* Copy filter bytecode */
943 if (uevent
->filter
) {
944 ua_event
->filter
= alloc_copy_ust_app_filter(uevent
->filter
);
945 /* Filter might be NULL here in case of ENONEM. */
950 * Copy data between an UST app channel and a LTT channel.
952 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
953 struct ltt_ust_channel
*uchan
)
955 struct lttng_ht_iter iter
;
956 struct ltt_ust_event
*uevent
;
957 struct ltt_ust_context
*uctx
;
958 struct ust_app_event
*ua_event
;
959 struct ust_app_ctx
*ua_ctx
;
961 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
963 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
964 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
965 /* Copy event attributes */
966 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
968 ua_chan
->enabled
= uchan
->enabled
;
970 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
971 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
972 if (ua_ctx
== NULL
) {
975 lttng_ht_node_init_ulong(&ua_ctx
->node
,
976 (unsigned long) ua_ctx
->ctx
.ctx
);
977 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
980 /* Copy all events from ltt ust channel to ust app channel */
981 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
982 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
983 uevent
->filter
, uevent
->attr
.loglevel
);
984 if (ua_event
== NULL
) {
985 DBG2("UST event %s not found on shadow copy channel",
987 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
988 if (ua_event
== NULL
) {
991 shadow_copy_event(ua_event
, uevent
);
992 add_unique_ust_app_event(ua_chan
->events
, ua_event
);
996 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1000 * Copy data between a UST app session and a regular LTT session.
1002 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1003 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1005 struct lttng_ht_node_str
*ua_chan_node
;
1006 struct lttng_ht_iter iter
;
1007 struct ltt_ust_channel
*uchan
;
1008 struct ust_app_channel
*ua_chan
;
1010 struct tm
*timeinfo
;
1014 /* Get date and time for unique app path */
1016 timeinfo
= localtime(&rawtime
);
1017 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1019 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1021 ua_sess
->id
= usess
->id
;
1022 ua_sess
->uid
= usess
->uid
;
1023 ua_sess
->gid
= usess
->gid
;
1025 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s-%d-%s/", app
->name
, app
->pid
,
1028 PERROR("asprintf UST shadow copy session");
1029 /* TODO: We cannot return an error from here.. */
1033 /* TODO: support all UST domain */
1035 /* Iterate over all channels in global domain. */
1036 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1038 struct lttng_ht_iter uiter
;
1040 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1041 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1042 if (ua_chan_node
!= NULL
) {
1043 /* Session exist. Contiuing. */
1047 DBG2("Channel %s not found on shadow session copy, creating it",
1049 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1050 if (ua_chan
== NULL
) {
1051 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1055 shadow_copy_channel(ua_chan
, uchan
);
1056 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1061 * Lookup sesison wrapper.
1064 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1065 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1067 /* Get right UST app session from app */
1068 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
1072 * Return ust app session from the app session hashtable using the UST session
1075 static struct ust_app_session
*lookup_session_by_app(
1076 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1078 struct lttng_ht_iter iter
;
1079 struct lttng_ht_node_ulong
*node
;
1081 __lookup_session_by_app(usess
, app
, &iter
);
1082 node
= lttng_ht_iter_get_node_ulong(&iter
);
1087 return caa_container_of(node
, struct ust_app_session
, node
);
1094 * Create a session on the tracer side for the given app.
1096 * On success, ua_sess_ptr is populated with the session pointer or else left
1097 * untouched. If the session was created, is_created is set to 1. On error,
1098 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1101 * Returns 0 on success or else a negative code which is either -ENOMEM or
1102 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1104 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1105 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1108 int ret
, created
= 0;
1109 struct ust_app_session
*ua_sess
;
1113 assert(ua_sess_ptr
);
1115 health_code_update(&health_thread_cmd
);
1117 ua_sess
= lookup_session_by_app(usess
, app
);
1118 if (ua_sess
== NULL
) {
1119 DBG2("UST app pid: %d session id %d not found, creating it",
1120 app
->pid
, usess
->id
);
1121 ua_sess
= alloc_ust_app_session();
1122 if (ua_sess
== NULL
) {
1123 /* Only malloc can failed so something is really wrong */
1127 shadow_copy_session(ua_sess
, usess
, app
);
1131 health_code_update(&health_thread_cmd
);
1133 if (ua_sess
->handle
== -1) {
1134 ret
= ustctl_create_session(app
->sock
);
1136 ERR("Creating session for app pid %d", app
->pid
);
1137 delete_ust_app_session(-1, ua_sess
);
1138 if (ret
!= -ENOMEM
) {
1140 * Tracer is probably gone or got an internal error so let's
1141 * behave like it will soon unregister or not usable.
1148 ua_sess
->handle
= ret
;
1150 /* Add ust app session to app's HT */
1151 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
1152 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
1154 DBG2("UST app session created successfully with handle %d", ret
);
1157 *ua_sess_ptr
= ua_sess
;
1159 *is_created
= created
;
1161 /* Everything went well. */
1165 health_code_update(&health_thread_cmd
);
1170 * Create a context for the channel on the tracer.
1173 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
1174 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
1175 struct ust_app
*app
)
1178 struct lttng_ht_iter iter
;
1179 struct lttng_ht_node_ulong
*node
;
1180 struct ust_app_ctx
*ua_ctx
;
1182 DBG2("UST app adding context to channel %s", ua_chan
->name
);
1184 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
1185 node
= lttng_ht_iter_get_node_ulong(&iter
);
1191 ua_ctx
= alloc_ust_app_ctx(uctx
);
1192 if (ua_ctx
== NULL
) {
1198 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
1199 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1201 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
1211 * Enable on the tracer side a ust app event for the session and channel.
1214 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
1215 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1219 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1224 ua_event
->enabled
= 1;
1231 * Disable on the tracer side a ust app event for the session and channel.
1233 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1234 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1238 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1243 ua_event
->enabled
= 0;
1250 * Lookup ust app channel for session and disable it on the tracer side.
1253 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1254 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1258 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1263 ua_chan
->enabled
= 0;
1270 * Lookup ust app channel for session and enable it on the tracer side.
1272 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1273 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1276 struct lttng_ht_iter iter
;
1277 struct lttng_ht_node_str
*ua_chan_node
;
1278 struct ust_app_channel
*ua_chan
;
1280 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1281 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1282 if (ua_chan_node
== NULL
) {
1283 DBG2("Unable to find channel %s in ust session id %u",
1284 uchan
->name
, ua_sess
->id
);
1288 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1290 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1300 * Create UST app channel and create it on the tracer. Set ua_chanp of the
1301 * newly created channel if not NULL.
1303 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
1304 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
1305 struct ust_app_channel
**ua_chanp
)
1308 struct lttng_ht_iter iter
;
1309 struct lttng_ht_node_str
*ua_chan_node
;
1310 struct ust_app_channel
*ua_chan
;
1312 /* Lookup channel in the ust app session */
1313 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1314 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1315 if (ua_chan_node
!= NULL
) {
1316 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1320 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1321 if (ua_chan
== NULL
) {
1322 /* Only malloc can fail here */
1326 shadow_copy_channel(ua_chan
, uchan
);
1328 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1330 /* Not found previously means that it does not exist on the tracer */
1331 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
1335 /* Only add the channel if successful on the tracer side. */
1336 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1338 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1343 *ua_chanp
= ua_chan
;
1346 /* Everything went well. */
1350 delete_ust_app_channel(-1, ua_chan
);
1355 * Create UST app event and create it on the tracer side.
1358 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1359 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1360 struct ust_app
*app
)
1363 struct ust_app_event
*ua_event
;
1365 /* Get event node */
1366 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1367 uevent
->filter
, uevent
->attr
.loglevel
);
1368 if (ua_event
!= NULL
) {
1373 /* Does not exist so create one */
1374 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1375 if (ua_event
== NULL
) {
1376 /* Only malloc can failed so something is really wrong */
1380 shadow_copy_event(ua_event
, uevent
);
1382 /* Create it on the tracer side */
1383 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1385 /* Not found previously means that it does not exist on the tracer */
1386 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
1390 add_unique_ust_app_event(ua_chan
->events
, ua_event
);
1392 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1399 /* Valid. Calling here is already in a read side lock */
1400 delete_ust_app_event(-1, ua_event
);
1405 * Create UST metadata and open it on the tracer side.
1407 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1408 char *pathname
, struct ust_app
*app
)
1412 if (ua_sess
->metadata
== NULL
) {
1413 /* Allocate UST metadata */
1414 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1415 if (ua_sess
->metadata
== NULL
) {
1416 /* malloc() failed */
1420 ret
= open_ust_metadata(app
, ua_sess
);
1422 DBG3("Opening metadata failed. Cleaning up memory");
1424 /* Cleanup failed metadata struct */
1425 free(ua_sess
->metadata
);
1427 * This is very important because delete_ust_app_session check if
1428 * the pointer is null or not in order to delete the metadata.
1430 ua_sess
->metadata
= NULL
;
1434 DBG2("UST metadata opened for app pid %d", app
->pid
);
1437 /* Open UST metadata stream */
1438 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1439 ret
= create_ust_metadata_stream(app
, ua_sess
);
1444 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1445 "%s/metadata", ua_sess
->path
);
1447 PERROR("asprintf UST create stream");
1451 DBG2("UST metadata stream object created for app pid %d",
1454 ERR("Attempting to create stream without metadata opened");
1465 * Return pointer to traceable apps list.
1467 struct lttng_ht
*ust_app_get_ht(void)
1473 * Return ust app pointer or NULL if not found.
1475 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1477 struct lttng_ht_node_ulong
*node
;
1478 struct lttng_ht_iter iter
;
1481 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1482 node
= lttng_ht_iter_get_node_ulong(&iter
);
1484 DBG2("UST app no found with pid %d", pid
);
1489 DBG2("Found UST app by pid %d", pid
);
1491 return caa_container_of(node
, struct ust_app
, pid_n
);
1499 * Using pid and uid (of the app), allocate a new ust_app struct and
1500 * add it to the global traceable app list.
1502 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1503 * bitness is not supported.
1505 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1507 struct ust_app
*lta
;
1510 if ((msg
->bits_per_long
== 64 &&
1511 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
1512 || (msg
->bits_per_long
== 32 &&
1513 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
1514 ERR("Registration failed: application \"%s\" (pid: %d) has "
1515 "%d-bit long, but no consumerd for this long size is available.\n",
1516 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1521 lttng_fd_put(LTTNG_FD_APPS
, 1);
1524 if (msg
->major
!= LTTNG_UST_COMM_MAJOR
) {
1525 ERR("Registration failed: application \"%s\" (pid: %d) has "
1526 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1527 msg
->name
, msg
->pid
, msg
->major
, msg
->minor
);
1532 lttng_fd_put(LTTNG_FD_APPS
, 1);
1535 lta
= zmalloc(sizeof(struct ust_app
));
1541 lta
->ppid
= msg
->ppid
;
1542 lta
->uid
= msg
->uid
;
1543 lta
->gid
= msg
->gid
;
1544 lta
->compatible
= 0; /* Not compatible until proven */
1545 lta
->bits_per_long
= msg
->bits_per_long
;
1546 lta
->v_major
= msg
->major
;
1547 lta
->v_minor
= msg
->minor
;
1548 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1549 lta
->name
[16] = '\0';
1550 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1552 lta
->pid
= msg
->pid
;
1553 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long)lta
->pid
);
1555 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long)lta
->sock
);
1557 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
1562 * On a re-registration, we want to kick out the previous registration of
1565 lttng_ht_add_replace_ulong(ust_app_ht
, <a
->pid_n
);
1568 * The socket _should_ be unique until _we_ call close. So, a add_unique
1569 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1570 * already in the table.
1572 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, <a
->sock_n
);
1576 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1577 " (version %d.%d)", lta
->pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1578 lta
->sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1584 * Unregister app by removing it from the global traceable app list and freeing
1587 * The socket is already closed at this point so no close to sock.
1589 void ust_app_unregister(int sock
)
1591 struct ust_app
*lta
;
1592 struct lttng_ht_node_ulong
*node
;
1593 struct lttng_ht_iter iter
;
1594 struct ust_app_session
*ua_sess
;
1599 /* Get the node reference for a call_rcu */
1600 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1601 node
= lttng_ht_iter_get_node_ulong(&iter
);
1603 ERR("Unable to find app by sock %d", sock
);
1607 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
1609 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
1611 /* Remove application from PID hash table */
1612 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
1615 /* Assign second node for deletion */
1616 iter
.iter
.node
= <a
->pid_n
.node
;
1619 * Ignore return value since the node might have been removed before by an
1620 * add replace during app registration because the PID can be reassigned by
1623 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1625 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
1629 /* Remove sessions so they are not visible during deletion.*/
1630 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
1632 ret
= lttng_ht_del(lta
->sessions
, &iter
);
1634 /* The session was already removed so scheduled for teardown. */
1639 * Add session to list for teardown. This is safe since at this point we
1640 * are the only one using this list.
1642 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
1646 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
1654 * Return traceable_app_count
1656 unsigned long ust_app_list_count(void)
1658 unsigned long count
;
1661 count
= lttng_ht_get_count(ust_app_ht
);
1668 * Fill events array with all events name of all registered apps.
1670 int ust_app_list_events(struct lttng_event
**events
)
1673 size_t nbmem
, count
= 0;
1674 struct lttng_ht_iter iter
;
1675 struct ust_app
*app
;
1676 struct lttng_event
*tmp_event
;
1678 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1679 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1680 if (tmp_event
== NULL
) {
1681 PERROR("zmalloc ust app events");
1688 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1689 struct lttng_ust_tracepoint_iter uiter
;
1691 health_code_update(&health_thread_cmd
);
1693 if (!app
->compatible
) {
1695 * TODO: In time, we should notice the caller of this error by
1696 * telling him that this is a version error.
1700 handle
= ustctl_tracepoint_list(app
->sock
);
1702 ERR("UST app list events getting handle failed for app pid %d",
1707 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
1708 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
1709 health_code_update(&health_thread_cmd
);
1710 if (count
>= nbmem
) {
1711 /* In case the realloc fails, we free the memory */
1714 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1717 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event
));
1719 PERROR("realloc ust app events");
1726 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1727 tmp_event
[count
].loglevel
= uiter
.loglevel
;
1728 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
1729 tmp_event
[count
].pid
= app
->pid
;
1730 tmp_event
[count
].enabled
= -1;
1736 *events
= tmp_event
;
1738 DBG2("UST app list events done (%zu events)", count
);
1743 health_code_update(&health_thread_cmd
);
1748 * Fill events array with all events name of all registered apps.
1750 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
1753 size_t nbmem
, count
= 0;
1754 struct lttng_ht_iter iter
;
1755 struct ust_app
*app
;
1756 struct lttng_event_field
*tmp_event
;
1758 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1759 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
1760 if (tmp_event
== NULL
) {
1761 PERROR("zmalloc ust app event fields");
1768 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1769 struct lttng_ust_field_iter uiter
;
1771 health_code_update(&health_thread_cmd
);
1773 if (!app
->compatible
) {
1775 * TODO: In time, we should notice the caller of this error by
1776 * telling him that this is a version error.
1780 handle
= ustctl_tracepoint_field_list(app
->sock
);
1782 ERR("UST app list event fields getting handle failed for app pid %d",
1787 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
1788 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
1789 health_code_update(&health_thread_cmd
);
1790 if (count
>= nbmem
) {
1791 /* In case the realloc fails, we free the memory */
1794 DBG2("Reallocating event field list from %zu to %zu entries", nbmem
,
1797 ptr
= realloc(tmp_event
, nbmem
* sizeof(struct lttng_event_field
));
1799 PERROR("realloc ust app event fields");
1807 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
1808 tmp_event
[count
].type
= uiter
.type
;
1809 tmp_event
[count
].nowrite
= uiter
.nowrite
;
1811 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1812 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
1813 tmp_event
[count
].event
.type
= LTTNG_UST_TRACEPOINT
;
1814 tmp_event
[count
].event
.pid
= app
->pid
;
1815 tmp_event
[count
].event
.enabled
= -1;
1821 *fields
= tmp_event
;
1823 DBG2("UST app list event fields done (%zu events)", count
);
1828 health_code_update(&health_thread_cmd
);
1833 * Free and clean all traceable apps of the global list.
1835 void ust_app_clean_list(void)
1838 struct ust_app
*app
;
1839 struct lttng_ht_iter iter
;
1841 DBG2("UST app cleaning registered apps hash table");
1845 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1846 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1848 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
1851 /* Cleanup socket hash table */
1852 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
1854 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
1858 /* Destroy is done only when the ht is empty */
1859 lttng_ht_destroy(ust_app_ht
);
1860 lttng_ht_destroy(ust_app_ht_by_sock
);
1866 * Init UST app hash table.
1868 void ust_app_ht_alloc(void)
1870 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1871 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1875 * For a specific UST session, disable the channel for all registered apps.
1877 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1878 struct ltt_ust_channel
*uchan
)
1881 struct lttng_ht_iter iter
;
1882 struct lttng_ht_node_str
*ua_chan_node
;
1883 struct ust_app
*app
;
1884 struct ust_app_session
*ua_sess
;
1885 struct ust_app_channel
*ua_chan
;
1887 if (usess
== NULL
|| uchan
== NULL
) {
1888 ERR("Disabling UST global channel with NULL values");
1893 DBG2("UST app disabling channel %s from global domain for session id %d",
1894 uchan
->name
, usess
->id
);
1898 /* For every registered applications */
1899 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1900 struct lttng_ht_iter uiter
;
1901 if (!app
->compatible
) {
1903 * TODO: In time, we should notice the caller of this error by
1904 * telling him that this is a version error.
1908 ua_sess
= lookup_session_by_app(usess
, app
);
1909 if (ua_sess
== NULL
) {
1914 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1915 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1916 /* If the session if found for the app, the channel must be there */
1917 assert(ua_chan_node
);
1919 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1920 /* The channel must not be already disabled */
1921 assert(ua_chan
->enabled
== 1);
1923 /* Disable channel onto application */
1924 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1926 /* XXX: We might want to report this error at some point... */
1938 * For a specific UST session, enable the channel for all registered apps.
1940 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1941 struct ltt_ust_channel
*uchan
)
1944 struct lttng_ht_iter iter
;
1945 struct ust_app
*app
;
1946 struct ust_app_session
*ua_sess
;
1948 if (usess
== NULL
|| uchan
== NULL
) {
1949 ERR("Adding UST global channel to NULL values");
1954 DBG2("UST app enabling channel %s to global domain for session id %d",
1955 uchan
->name
, usess
->id
);
1959 /* For every registered applications */
1960 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1961 if (!app
->compatible
) {
1963 * TODO: In time, we should notice the caller of this error by
1964 * telling him that this is a version error.
1968 ua_sess
= lookup_session_by_app(usess
, app
);
1969 if (ua_sess
== NULL
) {
1973 /* Enable channel onto application */
1974 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1976 /* XXX: We might want to report this error at some point... */
1988 * Disable an event in a channel and for a specific session.
1990 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1991 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1994 struct lttng_ht_iter iter
, uiter
;
1995 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1996 struct ust_app
*app
;
1997 struct ust_app_session
*ua_sess
;
1998 struct ust_app_channel
*ua_chan
;
1999 struct ust_app_event
*ua_event
;
2001 DBG("UST app disabling event %s for all apps in channel "
2002 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
2006 /* For all registered applications */
2007 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2008 if (!app
->compatible
) {
2010 * TODO: In time, we should notice the caller of this error by
2011 * telling him that this is a version error.
2015 ua_sess
= lookup_session_by_app(usess
, app
);
2016 if (ua_sess
== NULL
) {
2021 /* Lookup channel in the ust app session */
2022 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2023 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2024 if (ua_chan_node
== NULL
) {
2025 DBG2("Channel %s not found in session id %d for app pid %d."
2026 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
2029 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2031 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2032 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2033 if (ua_event_node
== NULL
) {
2034 DBG2("Event %s not found in channel %s for app pid %d."
2035 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
2038 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2040 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2042 /* XXX: Report error someday... */
2053 * For a specific UST session and UST channel, the event for all
2056 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
2057 struct ltt_ust_channel
*uchan
)
2060 struct lttng_ht_iter iter
, uiter
;
2061 struct lttng_ht_node_str
*ua_chan_node
;
2062 struct ust_app
*app
;
2063 struct ust_app_session
*ua_sess
;
2064 struct ust_app_channel
*ua_chan
;
2065 struct ust_app_event
*ua_event
;
2067 DBG("UST app disabling all event for all apps in channel "
2068 "%s for session id %d", uchan
->name
, usess
->id
);
2072 /* For all registered applications */
2073 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2074 if (!app
->compatible
) {
2076 * TODO: In time, we should notice the caller of this error by
2077 * telling him that this is a version error.
2081 ua_sess
= lookup_session_by_app(usess
, app
);
2083 /* The application has problem or is probably dead. */
2087 /* Lookup channel in the ust app session */
2088 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2089 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2090 /* If the channel is not found, there is a code flow error */
2091 assert(ua_chan_node
);
2093 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2095 /* Disable each events of channel */
2096 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2098 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2100 /* XXX: Report error someday... */
2112 * For a specific UST session, create the channel for all registered apps.
2114 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
2115 struct ltt_ust_channel
*uchan
)
2117 int ret
= 0, created
;
2118 struct lttng_ht_iter iter
;
2119 struct ust_app
*app
;
2120 struct ust_app_session
*ua_sess
= NULL
;
2122 /* Very wrong code flow */
2126 DBG2("UST app adding channel %s to global domain for session id %d",
2127 uchan
->name
, usess
->id
);
2131 /* For every registered applications */
2132 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2133 if (!app
->compatible
) {
2135 * TODO: In time, we should notice the caller of this error by
2136 * telling him that this is a version error.
2141 * Create session on the tracer side and add it to app session HT. Note
2142 * that if session exist, it will simply return a pointer to the ust
2145 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
2150 * The application's socket is not valid. Either a bad socket
2151 * or a timeout on it. We can't inform the caller that for a
2152 * specific app, the session failed so lets continue here.
2157 goto error_rcu_unlock
;
2162 /* Create channel onto application. We don't need the chan ref. */
2163 ret
= create_ust_app_channel(ua_sess
, uchan
, app
, NULL
);
2165 if (ret
== -ENOMEM
) {
2166 /* No more memory is a fatal error. Stop right now. */
2167 goto error_rcu_unlock
;
2169 /* Cleanup the created session if it's the case. */
2171 delete_ust_app_session(app
->sock
, ua_sess
);
2182 * Enable event for a specific session and channel on the tracer.
2184 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
2185 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2188 struct lttng_ht_iter iter
, uiter
;
2189 struct lttng_ht_node_str
*ua_chan_node
;
2190 struct ust_app
*app
;
2191 struct ust_app_session
*ua_sess
;
2192 struct ust_app_channel
*ua_chan
;
2193 struct ust_app_event
*ua_event
;
2195 DBG("UST app enabling event %s for all apps for session id %d",
2196 uevent
->attr
.name
, usess
->id
);
2199 * NOTE: At this point, this function is called only if the session and
2200 * channel passed are already created for all apps. and enabled on the
2206 /* For all registered applications */
2207 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2208 if (!app
->compatible
) {
2210 * TODO: In time, we should notice the caller of this error by
2211 * telling him that this is a version error.
2215 ua_sess
= lookup_session_by_app(usess
, app
);
2217 /* The application has problem or is probably dead. */
2221 /* Lookup channel in the ust app session */
2222 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2223 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2224 /* If the channel is not found, there is a code flow error */
2225 assert(ua_chan_node
);
2227 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2229 /* Get event node */
2230 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2231 uevent
->filter
, uevent
->attr
.loglevel
);
2232 if (ua_event
== NULL
) {
2233 DBG3("UST app enable event %s not found for app PID %d."
2234 "Skipping app", uevent
->attr
.name
, app
->pid
);
2238 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2250 * For a specific existing UST session and UST channel, creates the event for
2251 * all registered apps.
2253 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
2254 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2257 struct lttng_ht_iter iter
, uiter
;
2258 struct lttng_ht_node_str
*ua_chan_node
;
2259 struct ust_app
*app
;
2260 struct ust_app_session
*ua_sess
;
2261 struct ust_app_channel
*ua_chan
;
2263 DBG("UST app creating event %s for all apps for session id %d",
2264 uevent
->attr
.name
, usess
->id
);
2268 /* For all registered applications */
2269 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2270 if (!app
->compatible
) {
2272 * TODO: In time, we should notice the caller of this error by
2273 * telling him that this is a version error.
2277 ua_sess
= lookup_session_by_app(usess
, app
);
2279 /* The application has problem or is probably dead. */
2283 /* Lookup channel in the ust app session */
2284 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2285 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2286 /* If the channel is not found, there is a code flow error */
2287 assert(ua_chan_node
);
2289 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2291 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2293 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
2294 /* Possible value at this point: -ENOMEM. If so, we stop! */
2297 DBG2("UST app event %s already exist on app PID %d",
2298 uevent
->attr
.name
, app
->pid
);
2309 * Start tracing for a specific UST session and app.
2311 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2314 struct lttng_ht_iter iter
;
2315 struct ust_app_session
*ua_sess
;
2316 struct ust_app_channel
*ua_chan
;
2317 struct ltt_ust_stream
*ustream
;
2318 struct consumer_socket
*socket
;
2320 DBG("Starting tracing for ust app pid %d", app
->pid
);
2324 if (!app
->compatible
) {
2328 ua_sess
= lookup_session_by_app(usess
, app
);
2329 if (ua_sess
== NULL
) {
2330 /* The session is in teardown process. Ignore and continue. */
2334 /* Upon restart, we skip the setup, already done */
2335 if (ua_sess
->started
) {
2339 /* Create directories if consumer is LOCAL and has a path defined. */
2340 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2341 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
2342 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
2343 S_IRWXU
| S_IRWXG
, usess
->uid
, usess
->gid
);
2345 if (ret
!= -EEXIST
) {
2346 ERR("Trace directory creation error");
2348 goto error_rcu_unlock
;
2353 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
2355 ret
= LTTNG_ERR_UST_META_FAIL
;
2356 goto error_rcu_unlock
;
2359 /* For each channel */
2360 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2362 /* Create all streams */
2364 /* Create UST stream */
2365 ustream
= zmalloc(sizeof(*ustream
));
2366 if (ustream
== NULL
) {
2367 PERROR("zmalloc ust stream");
2368 goto error_rcu_unlock
;
2371 health_code_update(&health_thread_cmd
);
2373 ret
= create_ust_stream(app
, ua_chan
, ustream
);
2375 /* Free unused memory after this point. */
2377 if (ret
== -LTTNG_UST_ERR_NOENT
) {
2378 /* Got all streams. Continue normal execution. */
2381 /* Error at this point. Stop everything. */
2382 ret
= LTTNG_ERR_UST_STREAM_FAIL
;
2383 goto error_rcu_unlock
;
2386 health_code_update(&health_thread_cmd
);
2388 /* Order is important this is why a list is used. */
2389 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
2390 ua_chan
->streams
.count
++;
2392 DBG2("UST stream %d ready (handle: %d)", ua_chan
->streams
.count
,
2396 health_code_update(&health_thread_cmd
);
2399 switch (app
->bits_per_long
) {
2401 socket
= consumer_find_socket(uatomic_read(&ust_consumerd64_fd
),
2403 if (socket
== NULL
) {
2408 socket
= consumer_find_socket(uatomic_read(&ust_consumerd32_fd
),
2410 if (socket
== NULL
) {
2416 goto error_rcu_unlock
;
2419 /* Setup UST consumer socket and send fds to it */
2420 ret
= ust_consumer_send_session(ua_sess
, usess
->consumer
, socket
);
2422 goto error_rcu_unlock
;
2425 health_code_update(&health_thread_cmd
);
2428 /* This start the UST tracing */
2429 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
2431 ERR("Error starting tracing for app pid: %d (ret: %d)", app
->pid
, ret
);
2432 goto error_rcu_unlock
;
2435 /* Indicate that the session has been started once */
2436 ua_sess
->started
= 1;
2438 health_code_update(&health_thread_cmd
);
2440 /* Quiescent wait after starting trace */
2441 ustctl_wait_quiescent(app
->sock
);
2445 health_code_update(&health_thread_cmd
);
2450 health_code_update(&health_thread_cmd
);
2455 * Stop tracing for a specific UST session and app.
2457 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2460 struct lttng_ht_iter iter
;
2461 struct ust_app_session
*ua_sess
;
2462 struct ust_app_channel
*ua_chan
;
2464 DBG("Stopping tracing for ust app pid %d", app
->pid
);
2468 if (!app
->compatible
) {
2472 ua_sess
= lookup_session_by_app(usess
, app
);
2473 if (ua_sess
== NULL
) {
2478 * If started = 0, it means that stop trace has been called for a session
2479 * that was never started. It's possible since we can have a fail start
2480 * from either the application manager thread or the command thread. Simply
2481 * indicate that this is a stop error.
2483 if (!ua_sess
->started
) {
2484 goto error_rcu_unlock
;
2487 health_code_update(&health_thread_cmd
);
2489 /* This inhibits UST tracing */
2490 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
2492 ERR("Error stopping tracing for app pid: %d (ret: %d)", app
->pid
, ret
);
2493 goto error_rcu_unlock
;
2496 health_code_update(&health_thread_cmd
);
2498 /* Quiescent wait after stopping trace */
2499 ustctl_wait_quiescent(app
->sock
);
2501 health_code_update(&health_thread_cmd
);
2503 /* Flushing buffers */
2504 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2506 health_code_update(&health_thread_cmd
);
2507 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_chan
->obj
);
2509 ERR("UST app PID %d channel %s flush failed with ret %d",
2510 app
->pid
, ua_chan
->name
, ret
);
2511 /* Continuing flushing all buffers */
2516 health_code_update(&health_thread_cmd
);
2518 /* Flush all buffers before stopping */
2519 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_sess
->metadata
->obj
);
2521 ERR("UST app PID %d metadata flush failed with ret %d", app
->pid
,
2527 health_code_update(&health_thread_cmd
);
2532 health_code_update(&health_thread_cmd
);
2537 * Destroy a specific UST session in apps.
2539 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2541 struct ust_app_session
*ua_sess
;
2542 struct lttng_ust_object_data obj
;
2543 struct lttng_ht_iter iter
;
2544 struct lttng_ht_node_ulong
*node
;
2547 DBG("Destroy tracing for ust app pid %d", app
->pid
);
2551 if (!app
->compatible
) {
2555 __lookup_session_by_app(usess
, app
, &iter
);
2556 node
= lttng_ht_iter_get_node_ulong(&iter
);
2558 /* Session is being or is deleted. */
2561 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2562 ret
= lttng_ht_del(app
->sessions
, &iter
);
2564 /* Already scheduled for teardown. */
2568 obj
.handle
= ua_sess
->handle
;
2571 obj
.memory_map_size
= 0;
2572 health_code_update(&health_thread_cmd
);
2573 ustctl_release_object(app
->sock
, &obj
);
2575 health_code_update(&health_thread_cmd
);
2576 delete_ust_app_session(app
->sock
, ua_sess
);
2578 /* Quiescent wait after stopping trace */
2579 ustctl_wait_quiescent(app
->sock
);
2583 health_code_update(&health_thread_cmd
);
2588 * Start tracing for the UST session.
2590 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2593 struct lttng_ht_iter iter
;
2594 struct ust_app
*app
;
2596 DBG("Starting all UST traces");
2600 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2601 ret
= ust_app_start_trace(usess
, app
);
2603 /* Continue to next apps even on error */
2614 * Start tracing for the UST session.
2616 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2619 struct lttng_ht_iter iter
;
2620 struct ust_app
*app
;
2622 DBG("Stopping all UST traces");
2626 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2627 ret
= ust_app_stop_trace(usess
, app
);
2629 /* Continue to next apps even on error */
2640 * Destroy app UST session.
2642 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2645 struct lttng_ht_iter iter
;
2646 struct ust_app
*app
;
2648 DBG("Destroy all UST traces");
2652 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2653 ret
= destroy_trace(usess
, app
);
2655 /* Continue to next apps even on error */
2666 * Add channels/events from UST global domain to registered apps at sock.
2668 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2671 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
2672 struct ust_app
*app
;
2673 struct ust_app_session
*ua_sess
= NULL
;
2674 struct ust_app_channel
*ua_chan
;
2675 struct ust_app_event
*ua_event
;
2676 struct ust_app_ctx
*ua_ctx
;
2680 DBG2("UST app global update for app sock %d for session id %d", sock
,
2685 app
= find_app_by_sock(sock
);
2687 ERR("Failed to update app sock %d", sock
);
2691 if (!app
->compatible
) {
2695 ret
= create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
2697 /* Tracer is probably gone or ENOMEM. */
2703 * We can iterate safely here over all UST app session sicne the create ust
2704 * app session above made a shadow copy of the UST global domain from the
2707 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2709 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2711 /* FIXME: Should we quit here or continue... */
2715 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
2717 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2719 /* FIXME: Should we quit here or continue... */
2725 /* For each events */
2726 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2728 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2730 /* FIXME: Should we quit here or continue... */
2736 if (usess
->start_trace
) {
2737 ret
= ust_app_start_trace(usess
, app
);
2742 DBG2("UST trace started for app pid %d", app
->pid
);
2751 * Add context to a specific channel for global UST domain.
2753 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2754 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2757 struct lttng_ht_node_str
*ua_chan_node
;
2758 struct lttng_ht_iter iter
, uiter
;
2759 struct ust_app_channel
*ua_chan
= NULL
;
2760 struct ust_app_session
*ua_sess
;
2761 struct ust_app
*app
;
2765 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2766 if (!app
->compatible
) {
2768 * TODO: In time, we should notice the caller of this error by
2769 * telling him that this is a version error.
2773 ua_sess
= lookup_session_by_app(usess
, app
);
2774 if (ua_sess
== NULL
) {
2778 /* Lookup channel in the ust app session */
2779 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2780 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2781 if (ua_chan_node
== NULL
) {
2784 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2787 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2798 * Enable event for a channel from a UST session for a specific PID.
2800 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2801 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2804 struct lttng_ht_iter iter
;
2805 struct lttng_ht_node_str
*ua_chan_node
;
2806 struct ust_app
*app
;
2807 struct ust_app_session
*ua_sess
;
2808 struct ust_app_channel
*ua_chan
;
2809 struct ust_app_event
*ua_event
;
2811 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2815 app
= ust_app_find_by_pid(pid
);
2817 ERR("UST app enable event per PID %d not found", pid
);
2822 if (!app
->compatible
) {
2827 ua_sess
= lookup_session_by_app(usess
, app
);
2829 /* The application has problem or is probably dead. */
2833 /* Lookup channel in the ust app session */
2834 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2835 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2836 /* If the channel is not found, there is a code flow error */
2837 assert(ua_chan_node
);
2839 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2841 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2842 uevent
->filter
, uevent
->attr
.loglevel
);
2843 if (ua_event
== NULL
) {
2844 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2849 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2861 * Disable event for a channel from a UST session for a specific PID.
2863 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2864 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2867 struct lttng_ht_iter iter
;
2868 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2869 struct ust_app
*app
;
2870 struct ust_app_session
*ua_sess
;
2871 struct ust_app_channel
*ua_chan
;
2872 struct ust_app_event
*ua_event
;
2874 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2878 app
= ust_app_find_by_pid(pid
);
2880 ERR("UST app disable event per PID %d not found", pid
);
2885 if (!app
->compatible
) {
2890 ua_sess
= lookup_session_by_app(usess
, app
);
2892 /* The application has problem or is probably dead. */
2896 /* Lookup channel in the ust app session */
2897 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2898 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2899 if (ua_chan_node
== NULL
) {
2900 /* Channel does not exist, skip disabling */
2903 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2905 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2906 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2907 if (ua_event_node
== NULL
) {
2908 /* Event does not exist, skip disabling */
2911 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2913 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2924 * Validate version of UST apps and set the compatible bit.
2926 int ust_app_validate_version(int sock
)
2929 struct ust_app
*app
;
2933 app
= find_app_by_sock(sock
);
2936 health_code_update(&health_thread_cmd
);
2938 ret
= ustctl_tracer_version(sock
, &app
->version
);
2943 /* Validate version */
2944 if (app
->version
.major
!= UST_APP_MAJOR_VERSION
) {
2948 DBG2("UST app PID %d is compatible with internal major version %d "
2949 "(supporting == %d)", app
->pid
, app
->version
.major
,
2950 UST_APP_MAJOR_VERSION
);
2951 app
->compatible
= 1;
2953 health_code_update(&health_thread_cmd
);
2957 DBG2("UST app PID %d is not compatible with internal major version %d "
2958 "(supporting == %d)", app
->pid
, app
->version
.major
,
2959 UST_APP_MAJOR_VERSION
);
2960 app
->compatible
= 0;
2962 health_code_update(&health_thread_cmd
);
2967 * Calibrate registered applications.
2969 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
2972 struct lttng_ht_iter iter
;
2973 struct ust_app
*app
;
2977 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2978 if (!app
->compatible
) {
2980 * TODO: In time, we should notice the caller of this error by
2981 * telling him that this is a version error.
2986 health_code_update(&health_thread_cmd
);
2988 ret
= ustctl_calibrate(app
->sock
, calibrate
);
2992 /* Means that it's not implemented on the tracer side. */
2996 /* TODO: Report error to user */
2997 DBG2("Calibrate app PID %d returned with error %d",
3004 DBG("UST app global domain calibration finished");
3008 health_code_update(&health_thread_cmd
);