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 * Delete ust context safely. RCU read lock must be held before calling
44 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
47 ustctl_release_object(sock
, ua_ctx
->obj
);
54 * Delete ust app event safely. RCU read lock must be held before calling
58 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
61 struct lttng_ht_iter iter
;
62 struct ust_app_ctx
*ua_ctx
;
64 /* Destroy each context of event */
65 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter
.iter
, ua_ctx
,
67 ret
= lttng_ht_del(ua_event
->ctx
, &iter
);
69 delete_ust_app_ctx(sock
, ua_ctx
);
71 free(ua_event
->filter
);
72 lttng_ht_destroy(ua_event
->ctx
);
74 if (ua_event
->obj
!= NULL
) {
75 ustctl_release_object(sock
, ua_event
->obj
);
82 * Delete ust app stream safely. RCU read lock must be held before calling
86 void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
89 ustctl_release_object(sock
, stream
->obj
);
90 lttng_fd_put(LTTNG_FD_APPS
, 2);
97 * Delete ust app channel safely. RCU read lock must be held before calling
101 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
104 struct lttng_ht_iter iter
;
105 struct ust_app_event
*ua_event
;
106 struct ust_app_ctx
*ua_ctx
;
107 struct ltt_ust_stream
*stream
, *stmp
;
110 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
111 cds_list_del(&stream
->list
);
112 delete_ust_app_stream(sock
, stream
);
116 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
117 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
119 delete_ust_app_ctx(sock
, ua_ctx
);
121 lttng_ht_destroy(ua_chan
->ctx
);
124 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
126 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
128 delete_ust_app_event(sock
, ua_event
);
130 lttng_ht_destroy(ua_chan
->events
);
132 if (ua_chan
->obj
!= NULL
) {
133 ustctl_release_object(sock
, ua_chan
->obj
);
134 lttng_fd_put(LTTNG_FD_APPS
, 2);
141 * Delete ust app session safely. RCU read lock must be held before calling
145 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
)
148 struct lttng_ht_iter iter
;
149 struct ust_app_channel
*ua_chan
;
151 if (ua_sess
->metadata
) {
152 if (ua_sess
->metadata
->stream_obj
) {
153 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
154 lttng_fd_put(LTTNG_FD_APPS
, 2);
155 free(ua_sess
->metadata
->stream_obj
);
157 if (ua_sess
->metadata
->obj
) {
158 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
159 lttng_fd_put(LTTNG_FD_APPS
, 2);
160 free(ua_sess
->metadata
->obj
);
162 trace_ust_destroy_metadata(ua_sess
->metadata
);
165 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
167 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
169 delete_ust_app_channel(sock
, ua_chan
);
171 lttng_ht_destroy(ua_sess
->channels
);
173 if (ua_sess
->handle
!= -1) {
174 ustctl_release_handle(sock
, ua_sess
->handle
);
180 * Delete a traceable application structure from the global list. Never call
181 * this function outside of a call_rcu call.
184 void delete_ust_app(struct ust_app
*app
)
187 struct lttng_ht_iter iter
;
188 struct ust_app_session
*ua_sess
;
192 /* Delete ust app sessions info */
197 cds_lfht_for_each_entry(app
->sessions
->ht
, &iter
.iter
, ua_sess
,
199 ret
= lttng_ht_del(app
->sessions
, &iter
);
201 delete_ust_app_session(app
->sock
, ua_sess
);
203 lttng_ht_destroy(app
->sessions
);
206 * Wait until we have deleted the application from the sock hash table
207 * before closing this socket, otherwise an application could re-use the
208 * socket ID and race with the teardown, using the same hash table entry.
210 * It's OK to leave the close in call_rcu. We want it to stay unique for
211 * all RCU readers that could run concurrently with unregister app,
212 * therefore we _need_ to only close that socket after a grace period. So
213 * it should stay in this RCU callback.
215 * This close() is a very important step of the synchronization model so
216 * every modification to this function must be carefully reviewed.
222 lttng_fd_put(LTTNG_FD_APPS
, 1);
224 DBG2("UST app pid %d deleted", app
->pid
);
231 * URCU intermediate call to delete an UST app.
234 void delete_ust_app_rcu(struct rcu_head
*head
)
236 struct lttng_ht_node_ulong
*node
=
237 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
238 struct ust_app
*app
=
239 caa_container_of(node
, struct ust_app
, pid_n
);
241 DBG3("Call RCU deleting app PID %d", app
->pid
);
246 * Alloc new UST app session.
249 struct ust_app_session
*alloc_ust_app_session(void)
251 struct ust_app_session
*ua_sess
;
253 /* Init most of the default value by allocating and zeroing */
254 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
255 if (ua_sess
== NULL
) {
260 ua_sess
->handle
= -1;
261 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
270 * Alloc new UST app channel.
273 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
274 struct lttng_ust_channel
*attr
)
276 struct ust_app_channel
*ua_chan
;
278 /* Init most of the default value by allocating and zeroing */
279 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
280 if (ua_chan
== NULL
) {
285 /* Setup channel name */
286 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
287 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
289 ua_chan
->enabled
= 1;
290 ua_chan
->handle
= -1;
291 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
292 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
293 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
295 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
297 /* Copy attributes */
299 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
302 DBG3("UST app channel %s allocated", ua_chan
->name
);
311 * Alloc new UST app event.
314 struct ust_app_event
*alloc_ust_app_event(char *name
,
315 struct lttng_ust_event
*attr
)
317 struct ust_app_event
*ua_event
;
319 /* Init most of the default value by allocating and zeroing */
320 ua_event
= zmalloc(sizeof(struct ust_app_event
));
321 if (ua_event
== NULL
) {
326 ua_event
->enabled
= 1;
327 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
328 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
329 ua_event
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
330 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
332 /* Copy attributes */
334 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
337 DBG3("UST app event %s allocated", ua_event
->name
);
346 * Alloc new UST app context.
349 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
351 struct ust_app_ctx
*ua_ctx
;
353 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
354 if (ua_ctx
== NULL
) {
359 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
362 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
369 * Find an ust_app using the sock and return it. RCU read side lock must be
370 * held before calling this helper function.
373 struct ust_app
*find_app_by_sock(int sock
)
375 struct lttng_ht_node_ulong
*node
;
376 struct lttng_ht_iter iter
;
378 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
379 node
= lttng_ht_iter_get_node_ulong(&iter
);
381 DBG2("UST app find by sock %d not found", sock
);
385 return caa_container_of(node
, struct ust_app
, sock_n
);
392 * Create the channel context on the tracer.
395 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
396 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
400 health_code_update(&health_thread_cmd
);
402 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
403 ua_chan
->obj
, &ua_ctx
->obj
);
408 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
410 DBG2("UST app context created successfully for channel %s", ua_chan
->name
);
413 health_code_update(&health_thread_cmd
);
418 * Create the event context on the tracer.
421 int create_ust_event_context(struct ust_app_event
*ua_event
,
422 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
426 health_code_update(&health_thread_cmd
);
428 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
429 ua_event
->obj
, &ua_ctx
->obj
);
434 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
436 DBG2("UST app context created successfully for event %s", ua_event
->name
);
439 health_code_update(&health_thread_cmd
);
444 * Set the filter on the tracer.
447 int set_ust_event_filter(struct ust_app_event
*ua_event
,
452 health_code_update(&health_thread_cmd
);
454 if (!ua_event
->filter
) {
459 ret
= ustctl_set_filter(app
->sock
, ua_event
->filter
,
465 DBG2("UST filter set successfully for event %s", ua_event
->name
);
468 health_code_update(&health_thread_cmd
);
473 * Disable the specified event on to UST tracer for the UST session.
475 static int disable_ust_event(struct ust_app
*app
,
476 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
480 health_code_update(&health_thread_cmd
);
482 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
484 ERR("UST app event %s disable failed for app (pid: %d) "
485 "and session handle %d with ret %d",
486 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
490 DBG2("UST app event %s disabled successfully for app (pid: %d)",
491 ua_event
->attr
.name
, app
->pid
);
494 health_code_update(&health_thread_cmd
);
499 * Disable the specified channel on to UST tracer for the UST session.
501 static int disable_ust_channel(struct ust_app
*app
,
502 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
506 health_code_update(&health_thread_cmd
);
508 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
510 ERR("UST app channel %s disable failed for app (pid: %d) "
511 "and session handle %d with ret %d",
512 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
516 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
517 ua_chan
->name
, app
->pid
);
520 health_code_update(&health_thread_cmd
);
525 * Enable the specified channel on to UST tracer for the UST session.
527 static int enable_ust_channel(struct ust_app
*app
,
528 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
532 health_code_update(&health_thread_cmd
);
534 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
536 ERR("UST app channel %s enable failed for app (pid: %d) "
537 "and session handle %d with ret %d",
538 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
542 ua_chan
->enabled
= 1;
544 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
545 ua_chan
->name
, app
->pid
);
548 health_code_update(&health_thread_cmd
);
553 * Enable the specified event on to UST tracer for the UST session.
555 static int enable_ust_event(struct ust_app
*app
,
556 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
560 health_code_update(&health_thread_cmd
);
562 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
564 ERR("UST app event %s enable failed for app (pid: %d) "
565 "and session handle %d with ret %d",
566 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
570 DBG2("UST app event %s enabled successfully for app (pid: %d)",
571 ua_event
->attr
.name
, app
->pid
);
574 health_code_update(&health_thread_cmd
);
579 * Open metadata onto the UST tracer for a UST session.
581 static int open_ust_metadata(struct ust_app
*app
,
582 struct ust_app_session
*ua_sess
)
585 struct lttng_ust_channel_attr uattr
;
587 health_code_update(&health_thread_cmd
);
589 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
590 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
591 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
592 uattr
.switch_timer_interval
=
593 ua_sess
->metadata
->attr
.switch_timer_interval
;
594 uattr
.read_timer_interval
=
595 ua_sess
->metadata
->attr
.read_timer_interval
;
596 uattr
.output
= ua_sess
->metadata
->attr
.output
;
598 /* We are going to receive 2 fds, we need to reserve them. */
599 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
601 ERR("Exhausted number of available FD upon metadata open");
604 /* UST tracer metadata creation */
605 ret
= ustctl_open_metadata(app
->sock
, ua_sess
->handle
, &uattr
,
606 &ua_sess
->metadata
->obj
);
608 ERR("UST app open metadata failed for app pid:%d with ret %d",
613 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
616 health_code_update(&health_thread_cmd
);
621 * Create stream onto the UST tracer for a UST session.
623 static int create_ust_stream(struct ust_app
*app
,
624 struct ust_app_session
*ua_sess
)
628 health_code_update(&health_thread_cmd
);
630 /* We are going to receive 2 fds, we need to reserve them. */
631 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
633 ERR("Exhausted number of available FD upon metadata stream create");
636 ret
= ustctl_create_stream(app
->sock
, ua_sess
->metadata
->obj
,
637 &ua_sess
->metadata
->stream_obj
);
639 ERR("UST create metadata stream failed");
644 health_code_update(&health_thread_cmd
);
649 * Create the specified channel onto the UST tracer for a UST session.
651 static int create_ust_channel(struct ust_app
*app
,
652 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
656 health_code_update(&health_thread_cmd
);
658 /* TODO: remove cast and use lttng-ust-abi.h */
660 /* We are going to receive 2 fds, we need to reserve them. */
661 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
663 ERR("Exhausted number of available FD upon create channel");
667 health_code_update(&health_thread_cmd
);
669 ret
= ustctl_create_channel(app
->sock
, ua_sess
->handle
,
670 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
672 ERR("Creating channel %s for app (pid: %d, sock: %d) "
673 "and session handle %d with ret %d",
674 ua_chan
->name
, app
->pid
, app
->sock
,
675 ua_sess
->handle
, ret
);
676 lttng_fd_put(LTTNG_FD_APPS
, 2);
680 ua_chan
->handle
= ua_chan
->obj
->handle
;
682 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
683 ua_chan
->name
, app
->pid
, app
->sock
);
685 health_code_update(&health_thread_cmd
);
687 /* If channel is not enabled, disable it on the tracer */
688 if (!ua_chan
->enabled
) {
689 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
696 health_code_update(&health_thread_cmd
);
701 * Create the specified event onto the UST tracer for a UST session.
704 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
705 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
709 health_code_update(&health_thread_cmd
);
711 /* Create UST event on tracer */
712 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
715 if (ret
== -EEXIST
|| ret
== -EPERM
) {
719 ERR("Error ustctl create event %s for app pid: %d with ret %d",
720 ua_event
->attr
.name
, app
->pid
, ret
);
724 ua_event
->handle
= ua_event
->obj
->handle
;
726 DBG2("UST app event %s created successfully for pid:%d",
727 ua_event
->attr
.name
, app
->pid
);
729 health_code_update(&health_thread_cmd
);
731 /* If event not enabled, disable it on the tracer */
732 if (ua_event
->enabled
== 0) {
733 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
736 * If we hit an EPERM, something is wrong with our disable call. If
737 * we get an EEXIST, there is a problem on the tracer side since we
742 /* Code flow problem */
745 /* It's OK for our use case. */
756 health_code_update(&health_thread_cmd
);
761 * Copy data between an UST app event and a LTT event.
763 static void shadow_copy_event(struct ust_app_event
*ua_event
,
764 struct ltt_ust_event
*uevent
)
766 struct lttng_ht_iter iter
;
767 struct ltt_ust_context
*uctx
;
768 struct ust_app_ctx
*ua_ctx
;
770 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
771 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
773 ua_event
->enabled
= uevent
->enabled
;
775 /* Copy event attributes */
776 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
778 /* Copy filter bytecode */
779 if (uevent
->filter
) {
780 ua_event
->filter
= zmalloc(sizeof(*ua_event
->filter
) +
781 uevent
->filter
->len
);
782 if (!ua_event
->filter
) {
785 memcpy(ua_event
->filter
, uevent
->filter
,
786 sizeof(*ua_event
->filter
) + uevent
->filter
->len
);
788 cds_lfht_for_each_entry(uevent
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
789 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
790 if (ua_ctx
== NULL
) {
791 /* malloc() failed. We should simply stop */
795 lttng_ht_node_init_ulong(&ua_ctx
->node
,
796 (unsigned long) ua_ctx
->ctx
.ctx
);
797 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
802 * Copy data between an UST app channel and a LTT channel.
804 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
805 struct ltt_ust_channel
*uchan
)
807 struct lttng_ht_iter iter
;
808 struct lttng_ht_node_str
*ua_event_node
;
809 struct ltt_ust_event
*uevent
;
810 struct ltt_ust_context
*uctx
;
811 struct ust_app_event
*ua_event
;
812 struct ust_app_ctx
*ua_ctx
;
814 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
816 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
817 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
818 /* Copy event attributes */
819 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
821 ua_chan
->enabled
= uchan
->enabled
;
823 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
824 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
825 if (ua_ctx
== NULL
) {
828 lttng_ht_node_init_ulong(&ua_ctx
->node
,
829 (unsigned long) ua_ctx
->ctx
.ctx
);
830 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
833 /* Copy all events from ltt ust channel to ust app channel */
834 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
835 struct lttng_ht_iter uiter
;
837 lttng_ht_lookup(ua_chan
->events
, (void *) uevent
->attr
.name
, &uiter
);
838 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
839 if (ua_event_node
== NULL
) {
840 DBG2("UST event %s not found on shadow copy channel",
842 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
843 if (ua_event
== NULL
) {
846 shadow_copy_event(ua_event
, uevent
);
847 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
851 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
855 * Copy data between a UST app session and a regular LTT session.
857 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
858 struct ltt_ust_session
*usess
, struct ust_app
*app
)
860 struct lttng_ht_node_str
*ua_chan_node
;
861 struct lttng_ht_iter iter
;
862 struct ltt_ust_channel
*uchan
;
863 struct ust_app_channel
*ua_chan
;
869 /* Get date and time for unique app path */
871 timeinfo
= localtime(&rawtime
);
872 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
874 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
876 ua_sess
->id
= usess
->id
;
877 ua_sess
->uid
= usess
->uid
;
878 ua_sess
->gid
= usess
->gid
;
880 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s-%d-%s/", app
->name
, app
->pid
,
883 PERROR("asprintf UST shadow copy session");
884 /* TODO: We cannot return an error from here.. */
888 /* TODO: support all UST domain */
890 /* Iterate over all channels in global domain. */
891 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
893 struct lttng_ht_iter uiter
;
895 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
896 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
897 if (ua_chan_node
!= NULL
) {
898 /* Session exist. Contiuing. */
902 DBG2("Channel %s not found on shadow session copy, creating it",
904 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
905 if (ua_chan
== NULL
) {
906 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
910 shadow_copy_channel(ua_chan
, uchan
);
911 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
916 * Lookup sesison wrapper.
919 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
920 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
922 /* Get right UST app session from app */
923 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
927 * Return ust app session from the app session hashtable using the UST session
930 static struct ust_app_session
*lookup_session_by_app(
931 struct ltt_ust_session
*usess
, struct ust_app
*app
)
933 struct lttng_ht_iter iter
;
934 struct lttng_ht_node_ulong
*node
;
936 __lookup_session_by_app(usess
, app
, &iter
);
937 node
= lttng_ht_iter_get_node_ulong(&iter
);
942 return caa_container_of(node
, struct ust_app_session
, node
);
949 * Create a UST session onto the tracer of app and add it the session
952 * Return ust app session or NULL on error.
954 static struct ust_app_session
*create_ust_app_session(
955 struct ltt_ust_session
*usess
, struct ust_app
*app
)
958 struct ust_app_session
*ua_sess
;
960 health_code_update(&health_thread_cmd
);
962 ua_sess
= lookup_session_by_app(usess
, app
);
963 if (ua_sess
== NULL
) {
964 DBG2("UST app pid: %d session id %d not found, creating it",
965 app
->pid
, usess
->id
);
966 ua_sess
= alloc_ust_app_session();
967 if (ua_sess
== NULL
) {
968 /* Only malloc can failed so something is really wrong */
971 shadow_copy_session(ua_sess
, usess
, app
);
974 health_code_update(&health_thread_cmd
);
976 if (ua_sess
->handle
== -1) {
977 ret
= ustctl_create_session(app
->sock
);
979 ERR("Creating session for app pid %d", app
->pid
);
980 delete_ust_app_session(-1, ua_sess
);
981 /* This means that the tracer is gone... */
982 ua_sess
= (void*) -1UL;
986 ua_sess
->handle
= ret
;
988 /* Add ust app session to app's HT */
989 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
990 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
992 DBG2("UST app session created successfully with handle %d", ret
);
996 health_code_update(&health_thread_cmd
);
1001 * Create a context for the channel on the tracer.
1004 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
1005 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
1006 struct ust_app
*app
)
1009 struct lttng_ht_iter iter
;
1010 struct lttng_ht_node_ulong
*node
;
1011 struct ust_app_ctx
*ua_ctx
;
1013 DBG2("UST app adding context to channel %s", ua_chan
->name
);
1015 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
1016 node
= lttng_ht_iter_get_node_ulong(&iter
);
1022 ua_ctx
= alloc_ust_app_ctx(uctx
);
1023 if (ua_ctx
== NULL
) {
1029 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
1030 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1032 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
1042 * Create an UST context and enable it for the event on the tracer.
1045 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
1046 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
1047 struct ust_app
*app
)
1050 struct lttng_ht_iter iter
;
1051 struct lttng_ht_node_ulong
*node
;
1052 struct ust_app_ctx
*ua_ctx
;
1054 DBG2("UST app adding context to event %s", ua_event
->name
);
1056 lttng_ht_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
1057 node
= lttng_ht_iter_get_node_ulong(&iter
);
1063 ua_ctx
= alloc_ust_app_ctx(uctx
);
1064 if (ua_ctx
== NULL
) {
1070 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
1071 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
1073 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
1083 * Set UST filter for the event on the tracer.
1086 int set_ust_app_event_filter(struct ust_app_session
*ua_sess
,
1087 struct ust_app_event
*ua_event
,
1088 struct lttng_filter_bytecode
*bytecode
,
1089 struct ust_app
*app
)
1093 DBG2("UST app adding context to event %s", ua_event
->name
);
1095 /* Copy filter bytecode */
1096 ua_event
->filter
= zmalloc(sizeof(*ua_event
->filter
) + bytecode
->len
);
1097 if (!ua_event
->filter
) {
1100 memcpy(ua_event
->filter
, bytecode
,
1101 sizeof(*ua_event
->filter
) + bytecode
->len
);
1102 ret
= set_ust_event_filter(ua_event
, app
);
1112 * Enable on the tracer side a ust app event for the session and channel.
1115 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
1116 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1120 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1125 ua_event
->enabled
= 1;
1132 * Disable on the tracer side a ust app event for the session and channel.
1134 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1135 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1139 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1144 ua_event
->enabled
= 0;
1151 * Lookup ust app channel for session and disable it on the tracer side.
1154 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1155 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1159 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1164 ua_chan
->enabled
= 0;
1171 * Lookup ust app channel for session and enable it on the tracer side.
1173 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1174 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1177 struct lttng_ht_iter iter
;
1178 struct lttng_ht_node_str
*ua_chan_node
;
1179 struct ust_app_channel
*ua_chan
;
1181 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1182 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1183 if (ua_chan_node
== NULL
) {
1184 DBG2("Unable to find channel %s in ust session id %u",
1185 uchan
->name
, ua_sess
->id
);
1189 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1191 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1201 * Create UST app channel and create it on the tracer.
1203 static struct ust_app_channel
*create_ust_app_channel(
1204 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1205 struct ust_app
*app
)
1208 struct lttng_ht_iter iter
;
1209 struct lttng_ht_node_str
*ua_chan_node
;
1210 struct ust_app_channel
*ua_chan
;
1212 /* Lookup channel in the ust app session */
1213 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1214 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1215 if (ua_chan_node
!= NULL
) {
1216 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1220 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1221 if (ua_chan
== NULL
) {
1222 /* Only malloc can fail here */
1225 shadow_copy_channel(ua_chan
, uchan
);
1227 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1229 /* Not found previously means that it does not exist on the tracer */
1230 assert(ret
!= -EEXIST
);
1234 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1236 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1243 delete_ust_app_channel(-1, ua_chan
);
1248 * Create UST app event and create it on the tracer side.
1251 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1252 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1253 struct ust_app
*app
)
1256 struct lttng_ht_iter iter
;
1257 struct lttng_ht_node_str
*ua_event_node
;
1258 struct ust_app_event
*ua_event
;
1260 /* Get event node */
1261 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
1262 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
1263 if (ua_event_node
!= NULL
) {
1268 /* Does not exist so create one */
1269 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1270 if (ua_event
== NULL
) {
1271 /* Only malloc can failed so something is really wrong */
1275 shadow_copy_event(ua_event
, uevent
);
1277 /* Create it on the tracer side */
1278 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1280 /* Not found previously means that it does not exist on the tracer */
1281 assert(ret
!= -EEXIST
);
1285 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
1287 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1294 /* Valid. Calling here is already in a read side lock */
1295 delete_ust_app_event(-1, ua_event
);
1300 * Create UST metadata and open it on the tracer side.
1302 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1303 char *pathname
, struct ust_app
*app
)
1307 if (ua_sess
->metadata
== NULL
) {
1308 /* Allocate UST metadata */
1309 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1310 if (ua_sess
->metadata
== NULL
) {
1311 /* malloc() failed */
1315 ret
= open_ust_metadata(app
, ua_sess
);
1317 DBG3("Opening metadata failed. Cleaning up memory");
1319 /* Cleanup failed metadata struct */
1320 free(ua_sess
->metadata
);
1322 * This is very important because delete_ust_app_session check if
1323 * the pointer is null or not in order to delete the metadata.
1325 ua_sess
->metadata
= NULL
;
1329 DBG2("UST metadata opened for app pid %d", app
->pid
);
1332 /* Open UST metadata stream */
1333 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1334 ret
= create_ust_stream(app
, ua_sess
);
1339 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1340 "%s/metadata", ua_sess
->path
);
1342 PERROR("asprintf UST create stream");
1346 DBG2("UST metadata stream object created for app pid %d",
1349 ERR("Attempting to create stream without metadata opened");
1360 * Return pointer to traceable apps list.
1362 struct lttng_ht
*ust_app_get_ht(void)
1368 * Return ust app pointer or NULL if not found.
1370 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1372 struct lttng_ht_node_ulong
*node
;
1373 struct lttng_ht_iter iter
;
1376 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1377 node
= lttng_ht_iter_get_node_ulong(&iter
);
1379 DBG2("UST app no found with pid %d", pid
);
1384 DBG2("Found UST app by pid %d", pid
);
1386 return caa_container_of(node
, struct ust_app
, pid_n
);
1394 * Using pid and uid (of the app), allocate a new ust_app struct and
1395 * add it to the global traceable app list.
1397 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1398 * bitness is not supported.
1400 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1402 struct ust_app
*lta
;
1405 if ((msg
->bits_per_long
== 64 &&
1406 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
1407 || (msg
->bits_per_long
== 32 &&
1408 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
1409 ERR("Registration failed: application \"%s\" (pid: %d) has "
1410 "%d-bit long, but no consumerd for this long size is available.\n",
1411 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1416 lttng_fd_put(LTTNG_FD_APPS
, 1);
1419 if (msg
->major
!= LTTNG_UST_COMM_MAJOR
) {
1420 ERR("Registration failed: application \"%s\" (pid: %d) has "
1421 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1422 msg
->name
, msg
->pid
, msg
->major
, msg
->minor
);
1427 lttng_fd_put(LTTNG_FD_APPS
, 1);
1430 lta
= zmalloc(sizeof(struct ust_app
));
1436 lta
->ppid
= msg
->ppid
;
1437 lta
->uid
= msg
->uid
;
1438 lta
->gid
= msg
->gid
;
1439 lta
->compatible
= 0; /* Not compatible until proven */
1440 lta
->bits_per_long
= msg
->bits_per_long
;
1441 lta
->v_major
= msg
->major
;
1442 lta
->v_minor
= msg
->minor
;
1443 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1444 lta
->name
[16] = '\0';
1445 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1447 lta
->pid
= msg
->pid
;
1448 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long)lta
->pid
);
1450 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long)lta
->sock
);
1455 * On a re-registration, we want to kick out the previous registration of
1458 lttng_ht_add_replace_ulong(ust_app_ht
, <a
->pid_n
);
1461 * The socket _should_ be unique until _we_ call close. So, a add_unique
1462 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1463 * already in the table.
1465 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, <a
->sock_n
);
1469 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1470 " (version %d.%d)", lta
->pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1471 lta
->sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1477 * Unregister app by removing it from the global traceable app list and freeing
1480 * The socket is already closed at this point so no close to sock.
1482 void ust_app_unregister(int sock
)
1484 struct ust_app
*lta
;
1485 struct lttng_ht_node_ulong
*node
;
1486 struct lttng_ht_iter iter
;
1491 /* Get the node reference for a call_rcu */
1492 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1493 node
= lttng_ht_iter_get_node_ulong(&iter
);
1495 ERR("Unable to find app by sock %d", sock
);
1499 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
1501 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
1503 /* Remove application from PID hash table */
1504 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
1507 /* Assign second node for deletion */
1508 iter
.iter
.node
= <a
->pid_n
.node
;
1511 * Ignore return value since the node might have been removed before by an
1512 * add replace during app registration because the PID can be reassigned by
1515 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1517 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
1522 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
1530 * Return traceable_app_count
1532 unsigned long ust_app_list_count(void)
1534 unsigned long count
;
1537 count
= lttng_ht_get_count(ust_app_ht
);
1544 * Fill events array with all events name of all registered apps.
1546 int ust_app_list_events(struct lttng_event
**events
)
1549 size_t nbmem
, count
= 0;
1550 struct lttng_ht_iter iter
;
1551 struct ust_app
*app
;
1552 struct lttng_event
*tmp
;
1554 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1555 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1557 PERROR("zmalloc ust app events");
1564 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1565 struct lttng_ust_tracepoint_iter uiter
;
1567 health_code_update(&health_thread_cmd
);
1569 if (!app
->compatible
) {
1571 * TODO: In time, we should notice the caller of this error by
1572 * telling him that this is a version error.
1576 handle
= ustctl_tracepoint_list(app
->sock
);
1578 ERR("UST app list events getting handle failed for app pid %d",
1583 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
1584 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
1585 health_code_update(&health_thread_cmd
);
1586 if (count
>= nbmem
) {
1587 /* In case the realloc fails, we free the memory */
1588 void *tmp_ptr
= (void *) tmp
;
1589 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1592 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1594 PERROR("realloc ust app events");
1600 memcpy(tmp
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1601 tmp
[count
].loglevel
= uiter
.loglevel
;
1602 tmp
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
1603 tmp
[count
].pid
= app
->pid
;
1604 tmp
[count
].enabled
= -1;
1612 DBG2("UST app list events done (%zu events)", count
);
1617 health_code_update(&health_thread_cmd
);
1622 * Fill events array with all events name of all registered apps.
1624 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
1627 size_t nbmem
, count
= 0;
1628 struct lttng_ht_iter iter
;
1629 struct ust_app
*app
;
1630 struct lttng_event_field
*tmp
;
1632 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1633 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
1635 PERROR("zmalloc ust app event fields");
1642 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1643 struct lttng_ust_field_iter uiter
;
1645 health_code_update(&health_thread_cmd
);
1647 if (!app
->compatible
) {
1649 * TODO: In time, we should notice the caller of this error by
1650 * telling him that this is a version error.
1654 handle
= ustctl_tracepoint_field_list(app
->sock
);
1656 ERR("UST app list event fields getting handle failed for app pid %d",
1661 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
1662 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
1663 health_code_update(&health_thread_cmd
);
1664 if (count
>= nbmem
) {
1665 /* In case the realloc fails, we free the memory */
1666 void *tmp_ptr
= (void *) tmp
;
1667 DBG2("Reallocating event field list from %zu to %zu entries", nbmem
,
1670 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event_field
));
1672 PERROR("realloc ust app event fields");
1679 memcpy(tmp
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
1680 tmp
[count
].type
= uiter
.type
;
1681 tmp
[count
].nowrite
= uiter
.nowrite
;
1683 memcpy(tmp
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1684 tmp
[count
].event
.loglevel
= uiter
.loglevel
;
1685 tmp
[count
].event
.type
= LTTNG_UST_TRACEPOINT
;
1686 tmp
[count
].event
.pid
= app
->pid
;
1687 tmp
[count
].event
.enabled
= -1;
1695 DBG2("UST app list event fields done (%zu events)", count
);
1700 health_code_update(&health_thread_cmd
);
1705 * Free and clean all traceable apps of the global list.
1707 void ust_app_clean_list(void)
1710 struct ust_app
*app
;
1711 struct lttng_ht_iter iter
;
1713 DBG2("UST app cleaning registered apps hash table");
1717 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1718 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1720 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
1723 /* Cleanup socket hash table */
1724 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
1726 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
1730 /* Destroy is done only when the ht is empty */
1731 lttng_ht_destroy(ust_app_ht
);
1732 lttng_ht_destroy(ust_app_ht_by_sock
);
1738 * Init UST app hash table.
1740 void ust_app_ht_alloc(void)
1742 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1743 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1747 * For a specific UST session, disable the channel for all registered apps.
1749 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1750 struct ltt_ust_channel
*uchan
)
1753 struct lttng_ht_iter iter
;
1754 struct lttng_ht_node_str
*ua_chan_node
;
1755 struct ust_app
*app
;
1756 struct ust_app_session
*ua_sess
;
1757 struct ust_app_channel
*ua_chan
;
1759 if (usess
== NULL
|| uchan
== NULL
) {
1760 ERR("Disabling UST global channel with NULL values");
1765 DBG2("UST app disabling channel %s from global domain for session id %d",
1766 uchan
->name
, usess
->id
);
1770 /* For every registered applications */
1771 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1772 struct lttng_ht_iter uiter
;
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 ua_sess
= lookup_session_by_app(usess
, app
);
1781 if (ua_sess
== NULL
) {
1786 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1787 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1788 /* If the session if found for the app, the channel must be there */
1789 assert(ua_chan_node
);
1791 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1792 /* The channel must not be already disabled */
1793 assert(ua_chan
->enabled
== 1);
1795 /* Disable channel onto application */
1796 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1798 /* XXX: We might want to report this error at some point... */
1810 * For a specific UST session, enable the channel for all registered apps.
1812 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1813 struct ltt_ust_channel
*uchan
)
1816 struct lttng_ht_iter iter
;
1817 struct ust_app
*app
;
1818 struct ust_app_session
*ua_sess
;
1820 if (usess
== NULL
|| uchan
== NULL
) {
1821 ERR("Adding UST global channel to NULL values");
1826 DBG2("UST app enabling channel %s to global domain for session id %d",
1827 uchan
->name
, usess
->id
);
1831 /* For every registered applications */
1832 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1833 if (!app
->compatible
) {
1835 * TODO: In time, we should notice the caller of this error by
1836 * telling him that this is a version error.
1840 ua_sess
= lookup_session_by_app(usess
, app
);
1841 if (ua_sess
== NULL
) {
1845 /* Enable channel onto application */
1846 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1848 /* XXX: We might want to report this error at some point... */
1860 * Disable an event in a channel and for a specific session.
1862 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1863 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1866 struct lttng_ht_iter iter
, uiter
;
1867 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1868 struct ust_app
*app
;
1869 struct ust_app_session
*ua_sess
;
1870 struct ust_app_channel
*ua_chan
;
1871 struct ust_app_event
*ua_event
;
1873 DBG("UST app disabling event %s for all apps in channel "
1874 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
1878 /* For all registered applications */
1879 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1880 if (!app
->compatible
) {
1882 * TODO: In time, we should notice the caller of this error by
1883 * telling him that this is a version error.
1887 ua_sess
= lookup_session_by_app(usess
, app
);
1888 if (ua_sess
== NULL
) {
1893 /* Lookup channel in the ust app session */
1894 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1895 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1896 if (ua_chan_node
== NULL
) {
1897 DBG2("Channel %s not found in session id %d for app pid %d."
1898 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
1901 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1903 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
1904 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1905 if (ua_event_node
== NULL
) {
1906 DBG2("Event %s not found in channel %s for app pid %d."
1907 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
1910 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1912 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1914 /* XXX: Report error someday... */
1925 * For a specific UST session and UST channel, the event for all
1928 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1929 struct ltt_ust_channel
*uchan
)
1932 struct lttng_ht_iter iter
, uiter
;
1933 struct lttng_ht_node_str
*ua_chan_node
;
1934 struct ust_app
*app
;
1935 struct ust_app_session
*ua_sess
;
1936 struct ust_app_channel
*ua_chan
;
1937 struct ust_app_event
*ua_event
;
1939 DBG("UST app disabling all event for all apps in channel "
1940 "%s for session id %d", uchan
->name
, usess
->id
);
1944 /* For all registered applications */
1945 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1946 if (!app
->compatible
) {
1948 * TODO: In time, we should notice the caller of this error by
1949 * telling him that this is a version error.
1953 ua_sess
= lookup_session_by_app(usess
, app
);
1954 /* If ua_sess is NULL, there is a code flow error */
1957 /* Lookup channel in the ust app session */
1958 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1959 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1960 /* If the channel is not found, there is a code flow error */
1961 assert(ua_chan_node
);
1963 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1965 /* Disable each events of channel */
1966 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
1968 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1970 /* XXX: Report error someday... */
1982 * For a specific UST session, create the channel for all registered apps.
1984 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1985 struct ltt_ust_channel
*uchan
)
1988 struct lttng_ht_iter iter
;
1989 struct ust_app
*app
;
1990 struct ust_app_session
*ua_sess
;
1991 struct ust_app_channel
*ua_chan
;
1993 /* Very wrong code flow */
1997 DBG2("UST app adding channel %s to global domain for session id %d",
1998 uchan
->name
, usess
->id
);
2002 /* For every registered applications */
2003 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2004 if (!app
->compatible
) {
2006 * TODO: In time, we should notice the caller of this error by
2007 * telling him that this is a version error.
2012 * Create session on the tracer side and add it to app session HT. Note
2013 * that if session exist, it will simply return a pointer to the ust
2016 ua_sess
= create_ust_app_session(usess
, app
);
2017 if (ua_sess
== NULL
) {
2018 /* The malloc() failed. */
2021 } else if (ua_sess
== (void *) -1UL) {
2022 /* The application's socket is not valid. Contiuing */
2027 /* Create channel onto application */
2028 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
2029 if (ua_chan
== NULL
) {
2030 /* Major problem here and it's maybe the tracer or malloc() */
2043 * Enable event for a specific session and channel on the tracer.
2045 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
2046 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2049 struct lttng_ht_iter iter
, uiter
;
2050 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2051 struct ust_app
*app
;
2052 struct ust_app_session
*ua_sess
;
2053 struct ust_app_channel
*ua_chan
;
2054 struct ust_app_event
*ua_event
;
2056 DBG("UST app enabling event %s for all apps for session id %d",
2057 uevent
->attr
.name
, usess
->id
);
2060 * NOTE: At this point, this function is called only if the session and
2061 * channel passed are already created for all apps. and enabled on the
2067 /* For all registered applications */
2068 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2069 if (!app
->compatible
) {
2071 * TODO: In time, we should notice the caller of this error by
2072 * telling him that this is a version error.
2076 ua_sess
= lookup_session_by_app(usess
, app
);
2077 /* If ua_sess is NULL, there is a code flow error */
2080 /* Lookup channel in the ust app session */
2081 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2082 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2083 /* If the channel is not found, there is a code flow error */
2084 assert(ua_chan_node
);
2086 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2088 lttng_ht_lookup(ua_chan
->events
, (void*)uevent
->attr
.name
, &uiter
);
2089 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2090 if (ua_event_node
== NULL
) {
2091 DBG3("UST app enable event %s not found for app PID %d."
2092 "Skipping app", uevent
->attr
.name
, app
->pid
);
2095 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2097 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2109 * For a specific existing UST session and UST channel, creates the event for
2110 * all registered apps.
2112 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
2113 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2116 struct lttng_ht_iter iter
, uiter
;
2117 struct lttng_ht_node_str
*ua_chan_node
;
2118 struct ust_app
*app
;
2119 struct ust_app_session
*ua_sess
;
2120 struct ust_app_channel
*ua_chan
;
2122 DBG("UST app creating event %s for all apps for session id %d",
2123 uevent
->attr
.name
, usess
->id
);
2127 /* For all registered applications */
2128 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2129 if (!app
->compatible
) {
2131 * TODO: In time, we should notice the caller of this error by
2132 * telling him that this is a version error.
2136 ua_sess
= lookup_session_by_app(usess
, app
);
2137 /* If ua_sess is NULL, there is a code flow error */
2140 /* Lookup channel in the ust app session */
2141 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2142 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2143 /* If the channel is not found, there is a code flow error */
2144 assert(ua_chan_node
);
2146 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2148 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2150 if (ret
!= -EEXIST
) {
2151 /* Possible value at this point: -ENOMEM. If so, we stop! */
2154 DBG2("UST app event %s already exist on app PID %d",
2155 uevent
->attr
.name
, app
->pid
);
2166 * Start tracing for a specific UST session and app.
2168 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2171 struct lttng_ht_iter iter
;
2172 struct ust_app_session
*ua_sess
;
2173 struct ust_app_channel
*ua_chan
;
2174 struct ltt_ust_stream
*ustream
;
2175 struct consumer_socket
*socket
;
2177 DBG("Starting tracing for ust app pid %d", app
->pid
);
2181 if (!app
->compatible
) {
2185 ua_sess
= lookup_session_by_app(usess
, app
);
2186 if (ua_sess
== NULL
) {
2187 goto error_rcu_unlock
;
2190 /* Upon restart, we skip the setup, already done */
2191 if (ua_sess
->started
) {
2195 /* Create directories if consumer is LOCAL and has a path defined. */
2196 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2197 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
2198 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
2199 S_IRWXU
| S_IRWXG
, usess
->uid
, usess
->gid
);
2201 if (ret
!= -EEXIST
) {
2202 ERR("Trace directory creation error");
2204 goto error_rcu_unlock
;
2209 /* Indicate that the session has been started once */
2210 ua_sess
->started
= 1;
2212 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
2214 ret
= LTTNG_ERR_UST_META_FAIL
;
2215 goto error_rcu_unlock
;
2218 /* For each channel */
2219 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2221 /* Create all streams */
2223 /* Create UST stream */
2224 ustream
= zmalloc(sizeof(*ustream
));
2225 if (ustream
== NULL
) {
2226 PERROR("zmalloc ust stream");
2227 goto error_rcu_unlock
;
2230 /* We are going to receive 2 fds, we need to reserve them. */
2231 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2233 ERR("Exhausted number of available FD upon stream create");
2235 goto error_rcu_unlock
;
2238 health_code_update(&health_thread_cmd
);
2240 ret
= ustctl_create_stream(app
->sock
, ua_chan
->obj
,
2243 /* Got all streams */
2244 lttng_fd_put(LTTNG_FD_APPS
, 2);
2246 ret
= LTTNG_ERR_UST_STREAM_FAIL
;
2249 ustream
->handle
= ustream
->obj
->handle
;
2251 health_code_update(&health_thread_cmd
);
2253 /* Order is important */
2254 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
2255 ret
= snprintf(ustream
->name
, sizeof(ustream
->name
), "%s_%u",
2256 ua_chan
->name
, ua_chan
->streams
.count
);
2257 ua_chan
->streams
.count
++;
2259 PERROR("asprintf UST create stream");
2261 * XXX what should we do here with the
2266 DBG2("UST stream %d ready (handle: %d)", ua_chan
->streams
.count
,
2270 health_code_update(&health_thread_cmd
);
2273 switch (app
->bits_per_long
) {
2275 socket
= consumer_find_socket(uatomic_read(&ust_consumerd64_fd
),
2277 if (socket
== NULL
) {
2282 socket
= consumer_find_socket(uatomic_read(&ust_consumerd32_fd
),
2284 if (socket
== NULL
) {
2290 goto error_rcu_unlock
;
2293 /* Setup UST consumer socket and send fds to it */
2294 ret
= ust_consumer_send_session(ua_sess
, usess
->consumer
, socket
);
2296 goto error_rcu_unlock
;
2299 health_code_update(&health_thread_cmd
);
2302 /* This start the UST tracing */
2303 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
2305 ERR("Error starting tracing for app pid: %d", app
->pid
);
2306 goto error_rcu_unlock
;
2309 health_code_update(&health_thread_cmd
);
2311 /* Quiescent wait after starting trace */
2312 ustctl_wait_quiescent(app
->sock
);
2316 health_code_update(&health_thread_cmd
);
2321 health_code_update(&health_thread_cmd
);
2326 * Stop tracing for a specific UST session and app.
2328 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2331 struct lttng_ht_iter iter
;
2332 struct ust_app_session
*ua_sess
;
2333 struct ust_app_channel
*ua_chan
;
2335 DBG("Stopping tracing for ust app pid %d", app
->pid
);
2339 if (!app
->compatible
) {
2343 ua_sess
= lookup_session_by_app(usess
, app
);
2344 if (ua_sess
== NULL
) {
2345 /* Only malloc can failed so something is really wrong */
2346 goto error_rcu_unlock
;
2350 * If started = 0, it means that stop trace has been called for a session
2351 * that was never started. This is a code flow error and should never
2354 assert(ua_sess
->started
== 1);
2356 health_code_update(&health_thread_cmd
);
2358 /* This inhibits UST tracing */
2359 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
2361 ERR("Error stopping tracing for app pid: %d", app
->pid
);
2362 goto error_rcu_unlock
;
2365 health_code_update(&health_thread_cmd
);
2367 /* Quiescent wait after stopping trace */
2368 ustctl_wait_quiescent(app
->sock
);
2370 health_code_update(&health_thread_cmd
);
2372 /* Flushing buffers */
2373 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2375 health_code_update(&health_thread_cmd
);
2376 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_chan
->obj
);
2378 ERR("UST app PID %d channel %s flush failed with ret %d",
2379 app
->pid
, ua_chan
->name
, ret
);
2380 /* Continuing flushing all buffers */
2385 health_code_update(&health_thread_cmd
);
2387 /* Flush all buffers before stopping */
2388 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_sess
->metadata
->obj
);
2390 ERR("UST app PID %d metadata flush failed with ret %d", app
->pid
,
2396 health_code_update(&health_thread_cmd
);
2401 health_code_update(&health_thread_cmd
);
2406 * Destroy a specific UST session in apps.
2408 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2410 struct ust_app_session
*ua_sess
;
2411 struct lttng_ust_object_data obj
;
2412 struct lttng_ht_iter iter
;
2413 struct lttng_ht_node_ulong
*node
;
2416 DBG("Destroy tracing for ust app pid %d", app
->pid
);
2420 if (!app
->compatible
) {
2424 __lookup_session_by_app(usess
, app
, &iter
);
2425 node
= lttng_ht_iter_get_node_ulong(&iter
);
2427 /* Only malloc can failed so something is really wrong */
2428 goto error_rcu_unlock
;
2430 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2431 ret
= lttng_ht_del(app
->sessions
, &iter
);
2433 obj
.handle
= ua_sess
->handle
;
2436 obj
.memory_map_size
= 0;
2437 health_code_update(&health_thread_cmd
);
2438 ustctl_release_object(app
->sock
, &obj
);
2440 health_code_update(&health_thread_cmd
);
2441 delete_ust_app_session(app
->sock
, ua_sess
);
2443 /* Quiescent wait after stopping trace */
2444 ustctl_wait_quiescent(app
->sock
);
2448 health_code_update(&health_thread_cmd
);
2453 health_code_update(&health_thread_cmd
);
2458 * Start tracing for the UST session.
2460 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2463 struct lttng_ht_iter iter
;
2464 struct ust_app
*app
;
2466 DBG("Starting all UST traces");
2470 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2471 ret
= ust_app_start_trace(usess
, app
);
2473 /* Continue to next apps even on error */
2484 * Start tracing for the UST session.
2486 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2489 struct lttng_ht_iter iter
;
2490 struct ust_app
*app
;
2492 DBG("Stopping all UST traces");
2496 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2497 ret
= ust_app_stop_trace(usess
, app
);
2499 /* Continue to next apps even on error */
2510 * Destroy app UST session.
2512 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2515 struct lttng_ht_iter iter
;
2516 struct ust_app
*app
;
2518 DBG("Destroy all UST traces");
2522 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2523 ret
= ust_app_destroy_trace(usess
, app
);
2525 /* Continue to next apps even on error */
2536 * Add channels/events from UST global domain to registered apps at sock.
2538 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2541 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
2542 struct ust_app
*app
;
2543 struct ust_app_session
*ua_sess
;
2544 struct ust_app_channel
*ua_chan
;
2545 struct ust_app_event
*ua_event
;
2546 struct ust_app_ctx
*ua_ctx
;
2548 if (usess
== NULL
) {
2549 ERR("No UST session on global update. Returning");
2553 DBG2("UST app global update for app sock %d for session id %d", sock
,
2558 app
= find_app_by_sock(sock
);
2560 ERR("Failed to update app sock %d", sock
);
2564 if (!app
->compatible
) {
2568 ua_sess
= create_ust_app_session(usess
, app
);
2569 if (ua_sess
== NULL
|| ua_sess
== (void *) -1UL) {
2570 /* Tracer is gone for this session and has been freed */
2575 * We can iterate safely here over all UST app session sicne the create ust
2576 * app session above made a shadow copy of the UST global domain from the
2579 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2581 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2583 /* FIXME: Should we quit here or continue... */
2587 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
2589 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2591 /* FIXME: Should we quit here or continue... */
2597 /* For each events */
2598 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2600 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2602 /* FIXME: Should we quit here or continue... */
2606 /* Add context on events. */
2607 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter_ctx
.iter
,
2608 ua_ctx
, node
.node
) {
2609 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
2611 /* FIXME: Should we quit here or continue... */
2615 ret
= set_ust_event_filter(ua_event
, app
);
2617 /* FIXME: Should we quit here or continue... */
2623 if (usess
->start_trace
) {
2624 ret
= ust_app_start_trace(usess
, app
);
2629 DBG2("UST trace started for app pid %d", app
->pid
);
2638 * Add context to a specific channel for global UST domain.
2640 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2641 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2644 struct lttng_ht_node_str
*ua_chan_node
;
2645 struct lttng_ht_iter iter
, uiter
;
2646 struct ust_app_channel
*ua_chan
= NULL
;
2647 struct ust_app_session
*ua_sess
;
2648 struct ust_app
*app
;
2652 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2653 if (!app
->compatible
) {
2655 * TODO: In time, we should notice the caller of this error by
2656 * telling him that this is a version error.
2660 ua_sess
= lookup_session_by_app(usess
, app
);
2661 if (ua_sess
== NULL
) {
2665 /* Lookup channel in the ust app session */
2666 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2667 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2668 if (ua_chan_node
== NULL
) {
2671 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2674 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2685 * Add context to a specific event in a channel for global UST domain.
2687 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2688 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2689 struct ltt_ust_context
*uctx
)
2692 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2693 struct lttng_ht_iter iter
, uiter
;
2694 struct ust_app_session
*ua_sess
;
2695 struct ust_app_event
*ua_event
;
2696 struct ust_app_channel
*ua_chan
= NULL
;
2697 struct ust_app
*app
;
2701 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2702 if (!app
->compatible
) {
2704 * TODO: In time, we should notice the caller of this error by
2705 * telling him that this is a version error.
2709 ua_sess
= lookup_session_by_app(usess
, app
);
2710 if (ua_sess
== NULL
) {
2714 /* Lookup channel in the ust app session */
2715 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2716 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2717 if (ua_chan_node
== NULL
) {
2720 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2723 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2724 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2725 if (ua_event_node
== NULL
) {
2728 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2731 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2742 * Add context to a specific event in a channel for global UST domain.
2744 int ust_app_set_filter_event_glb(struct ltt_ust_session
*usess
,
2745 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2746 struct lttng_filter_bytecode
*bytecode
)
2749 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2750 struct lttng_ht_iter iter
, uiter
;
2751 struct ust_app_session
*ua_sess
;
2752 struct ust_app_event
*ua_event
;
2753 struct ust_app_channel
*ua_chan
= NULL
;
2754 struct ust_app
*app
;
2758 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2759 if (!app
->compatible
) {
2761 * TODO: In time, we should notice the caller of this error by
2762 * telling him that this is a version error.
2766 ua_sess
= lookup_session_by_app(usess
, app
);
2767 if (ua_sess
== NULL
) {
2771 /* Lookup channel in the ust app session */
2772 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2773 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2774 if (ua_chan_node
== NULL
) {
2777 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2780 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2781 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2782 if (ua_event_node
== NULL
) {
2785 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2788 ret
= set_ust_app_event_filter(ua_sess
, ua_event
, bytecode
, app
);
2799 * Enable event for a channel from a UST session for a specific PID.
2801 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2802 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2805 struct lttng_ht_iter iter
;
2806 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2807 struct ust_app
*app
;
2808 struct ust_app_session
*ua_sess
;
2809 struct ust_app_channel
*ua_chan
;
2810 struct ust_app_event
*ua_event
;
2812 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2816 app
= ust_app_find_by_pid(pid
);
2818 ERR("UST app enable event per PID %d not found", pid
);
2823 if (!app
->compatible
) {
2828 ua_sess
= lookup_session_by_app(usess
, app
);
2829 /* If ua_sess is NULL, there is a code flow error */
2832 /* Lookup channel in the ust app session */
2833 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2834 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2835 /* If the channel is not found, there is a code flow error */
2836 assert(ua_chan_node
);
2838 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2840 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2841 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2842 if (ua_event_node
== NULL
) {
2843 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2848 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2850 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2862 * Disable event for a channel from a UST session for a specific PID.
2864 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2865 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2868 struct lttng_ht_iter iter
;
2869 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2870 struct ust_app
*app
;
2871 struct ust_app_session
*ua_sess
;
2872 struct ust_app_channel
*ua_chan
;
2873 struct ust_app_event
*ua_event
;
2875 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2879 app
= ust_app_find_by_pid(pid
);
2881 ERR("UST app disable event per PID %d not found", pid
);
2886 if (!app
->compatible
) {
2891 ua_sess
= lookup_session_by_app(usess
, app
);
2892 /* If ua_sess is NULL, there is a code flow error */
2895 /* Lookup channel in the ust app session */
2896 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2897 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2898 if (ua_chan_node
== NULL
) {
2899 /* Channel does not exist, skip disabling */
2902 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2904 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2905 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2906 if (ua_event_node
== NULL
) {
2907 /* Event does not exist, skip disabling */
2910 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2912 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2923 * Validate version of UST apps and set the compatible bit.
2925 int ust_app_validate_version(int sock
)
2928 struct ust_app
*app
;
2932 app
= find_app_by_sock(sock
);
2935 health_code_update(&health_thread_cmd
);
2937 ret
= ustctl_tracer_version(sock
, &app
->version
);
2942 /* Validate version */
2943 if (app
->version
.major
!= UST_APP_MAJOR_VERSION
) {
2947 DBG2("UST app PID %d is compatible with internal major version %d "
2948 "(supporting == %d)", app
->pid
, app
->version
.major
,
2949 UST_APP_MAJOR_VERSION
);
2950 app
->compatible
= 1;
2952 health_code_update(&health_thread_cmd
);
2956 DBG2("UST app PID %d is not compatible with internal major version %d "
2957 "(supporting == %d)", app
->pid
, app
->version
.major
,
2958 UST_APP_MAJOR_VERSION
);
2959 app
->compatible
= 0;
2961 health_code_update(&health_thread_cmd
);
2966 * Calibrate registered applications.
2968 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
2971 struct lttng_ht_iter iter
;
2972 struct ust_app
*app
;
2976 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2977 if (!app
->compatible
) {
2979 * TODO: In time, we should notice the caller of this error by
2980 * telling him that this is a version error.
2985 health_code_update(&health_thread_cmd
);
2987 ret
= ustctl_calibrate(app
->sock
, calibrate
);
2991 /* Means that it's not implemented on the tracer side. */
2995 /* TODO: Report error to user */
2996 DBG2("Calibrate app PID %d returned with error %d",
3003 DBG("UST app global domain calibration finished");
3007 health_code_update(&health_thread_cmd
);