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 ERR("Error ustctl create event %s for app pid: %d with ret %d",
716 ua_event
->attr
.name
, app
->pid
, ret
);
720 ua_event
->handle
= ua_event
->obj
->handle
;
722 DBG2("UST app event %s created successfully for pid:%d",
723 ua_event
->attr
.name
, app
->pid
);
725 health_code_update(&health_thread_cmd
);
727 /* If event not enabled, disable it on the tracer */
728 if (ua_event
->enabled
== 0) {
729 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
732 * If we hit an EPERM, something is wrong with our disable call. If
733 * we get an EEXIST, there is a problem on the tracer side since we
737 case -LTTNG_UST_ERR_PERM
:
738 /* Code flow problem */
740 case -LTTNG_UST_ERR_EXIST
:
741 /* It's OK for our use case. */
752 health_code_update(&health_thread_cmd
);
757 * Copy data between an UST app event and a LTT event.
759 static void shadow_copy_event(struct ust_app_event
*ua_event
,
760 struct ltt_ust_event
*uevent
)
762 struct lttng_ht_iter iter
;
763 struct ltt_ust_context
*uctx
;
764 struct ust_app_ctx
*ua_ctx
;
766 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
767 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
769 ua_event
->enabled
= uevent
->enabled
;
771 /* Copy event attributes */
772 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
774 /* Copy filter bytecode */
775 if (uevent
->filter
) {
776 ua_event
->filter
= zmalloc(sizeof(*ua_event
->filter
) +
777 uevent
->filter
->len
);
778 if (!ua_event
->filter
) {
781 memcpy(ua_event
->filter
, uevent
->filter
,
782 sizeof(*ua_event
->filter
) + uevent
->filter
->len
);
784 cds_lfht_for_each_entry(uevent
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
785 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
786 if (ua_ctx
== NULL
) {
787 /* malloc() failed. We should simply stop */
791 lttng_ht_node_init_ulong(&ua_ctx
->node
,
792 (unsigned long) ua_ctx
->ctx
.ctx
);
793 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
798 * Copy data between an UST app channel and a LTT channel.
800 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
801 struct ltt_ust_channel
*uchan
)
803 struct lttng_ht_iter iter
;
804 struct lttng_ht_node_str
*ua_event_node
;
805 struct ltt_ust_event
*uevent
;
806 struct ltt_ust_context
*uctx
;
807 struct ust_app_event
*ua_event
;
808 struct ust_app_ctx
*ua_ctx
;
810 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
812 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
813 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
814 /* Copy event attributes */
815 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
817 ua_chan
->enabled
= uchan
->enabled
;
819 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
820 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
821 if (ua_ctx
== NULL
) {
824 lttng_ht_node_init_ulong(&ua_ctx
->node
,
825 (unsigned long) ua_ctx
->ctx
.ctx
);
826 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
829 /* Copy all events from ltt ust channel to ust app channel */
830 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
831 struct lttng_ht_iter uiter
;
833 lttng_ht_lookup(ua_chan
->events
, (void *) uevent
->attr
.name
, &uiter
);
834 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
835 if (ua_event_node
== NULL
) {
836 DBG2("UST event %s not found on shadow copy channel",
838 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
839 if (ua_event
== NULL
) {
842 shadow_copy_event(ua_event
, uevent
);
843 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
847 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
851 * Copy data between a UST app session and a regular LTT session.
853 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
854 struct ltt_ust_session
*usess
, struct ust_app
*app
)
856 struct lttng_ht_node_str
*ua_chan_node
;
857 struct lttng_ht_iter iter
;
858 struct ltt_ust_channel
*uchan
;
859 struct ust_app_channel
*ua_chan
;
865 /* Get date and time for unique app path */
867 timeinfo
= localtime(&rawtime
);
868 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
870 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
872 ua_sess
->id
= usess
->id
;
873 ua_sess
->uid
= usess
->uid
;
874 ua_sess
->gid
= usess
->gid
;
876 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s-%d-%s/", app
->name
, app
->pid
,
879 PERROR("asprintf UST shadow copy session");
880 /* TODO: We cannot return an error from here.. */
884 /* TODO: support all UST domain */
886 /* Iterate over all channels in global domain. */
887 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
889 struct lttng_ht_iter uiter
;
891 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
892 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
893 if (ua_chan_node
!= NULL
) {
894 /* Session exist. Contiuing. */
898 DBG2("Channel %s not found on shadow session copy, creating it",
900 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
901 if (ua_chan
== NULL
) {
902 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
906 shadow_copy_channel(ua_chan
, uchan
);
907 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
912 * Lookup sesison wrapper.
915 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
916 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
918 /* Get right UST app session from app */
919 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
923 * Return ust app session from the app session hashtable using the UST session
926 static struct ust_app_session
*lookup_session_by_app(
927 struct ltt_ust_session
*usess
, struct ust_app
*app
)
929 struct lttng_ht_iter iter
;
930 struct lttng_ht_node_ulong
*node
;
932 __lookup_session_by_app(usess
, app
, &iter
);
933 node
= lttng_ht_iter_get_node_ulong(&iter
);
938 return caa_container_of(node
, struct ust_app_session
, node
);
945 * Create a UST session onto the tracer of app and add it the session
948 * Return ust app session or NULL on error.
950 static struct ust_app_session
*create_ust_app_session(
951 struct ltt_ust_session
*usess
, struct ust_app
*app
)
954 struct ust_app_session
*ua_sess
;
956 health_code_update(&health_thread_cmd
);
958 ua_sess
= lookup_session_by_app(usess
, app
);
959 if (ua_sess
== NULL
) {
960 DBG2("UST app pid: %d session id %d not found, creating it",
961 app
->pid
, usess
->id
);
962 ua_sess
= alloc_ust_app_session();
963 if (ua_sess
== NULL
) {
964 /* Only malloc can failed so something is really wrong */
967 shadow_copy_session(ua_sess
, usess
, app
);
970 health_code_update(&health_thread_cmd
);
972 if (ua_sess
->handle
== -1) {
973 ret
= ustctl_create_session(app
->sock
);
975 ERR("Creating session for app pid %d", app
->pid
);
976 delete_ust_app_session(-1, ua_sess
);
977 /* This means that the tracer is gone... */
978 ua_sess
= (void*) -1UL;
982 ua_sess
->handle
= ret
;
984 /* Add ust app session to app's HT */
985 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
986 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
988 DBG2("UST app session created successfully with handle %d", ret
);
992 health_code_update(&health_thread_cmd
);
997 * Create a context for the channel on the tracer.
1000 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
1001 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
1002 struct ust_app
*app
)
1005 struct lttng_ht_iter iter
;
1006 struct lttng_ht_node_ulong
*node
;
1007 struct ust_app_ctx
*ua_ctx
;
1009 DBG2("UST app adding context to channel %s", ua_chan
->name
);
1011 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
1012 node
= lttng_ht_iter_get_node_ulong(&iter
);
1018 ua_ctx
= alloc_ust_app_ctx(uctx
);
1019 if (ua_ctx
== NULL
) {
1025 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
1026 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1028 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
1038 * Create an UST context and enable it for the event on the tracer.
1041 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
1042 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
1043 struct ust_app
*app
)
1046 struct lttng_ht_iter iter
;
1047 struct lttng_ht_node_ulong
*node
;
1048 struct ust_app_ctx
*ua_ctx
;
1050 DBG2("UST app adding context to event %s", ua_event
->name
);
1052 lttng_ht_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
1053 node
= lttng_ht_iter_get_node_ulong(&iter
);
1059 ua_ctx
= alloc_ust_app_ctx(uctx
);
1060 if (ua_ctx
== NULL
) {
1066 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
1067 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
1069 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
1079 * Set UST filter for the event on the tracer.
1082 int set_ust_app_event_filter(struct ust_app_session
*ua_sess
,
1083 struct ust_app_event
*ua_event
,
1084 struct lttng_filter_bytecode
*bytecode
,
1085 struct ust_app
*app
)
1089 DBG2("UST app adding context to event %s", ua_event
->name
);
1091 /* Copy filter bytecode */
1092 ua_event
->filter
= zmalloc(sizeof(*ua_event
->filter
) + bytecode
->len
);
1093 if (!ua_event
->filter
) {
1096 memcpy(ua_event
->filter
, bytecode
,
1097 sizeof(*ua_event
->filter
) + bytecode
->len
);
1098 ret
= set_ust_event_filter(ua_event
, app
);
1108 * Enable on the tracer side a ust app event for the session and channel.
1111 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
1112 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1116 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1121 ua_event
->enabled
= 1;
1128 * Disable on the tracer side a ust app event for the session and channel.
1130 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1131 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1135 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1140 ua_event
->enabled
= 0;
1147 * Lookup ust app channel for session and disable it on the tracer side.
1150 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1151 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1155 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1160 ua_chan
->enabled
= 0;
1167 * Lookup ust app channel for session and enable it on the tracer side.
1169 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1170 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1173 struct lttng_ht_iter iter
;
1174 struct lttng_ht_node_str
*ua_chan_node
;
1175 struct ust_app_channel
*ua_chan
;
1177 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1178 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1179 if (ua_chan_node
== NULL
) {
1180 DBG2("Unable to find channel %s in ust session id %u",
1181 uchan
->name
, ua_sess
->id
);
1185 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1187 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1197 * Create UST app channel and create it on the tracer.
1199 static struct ust_app_channel
*create_ust_app_channel(
1200 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1201 struct ust_app
*app
)
1204 struct lttng_ht_iter iter
;
1205 struct lttng_ht_node_str
*ua_chan_node
;
1206 struct ust_app_channel
*ua_chan
;
1208 /* Lookup channel in the ust app session */
1209 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1210 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1211 if (ua_chan_node
!= NULL
) {
1212 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1216 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1217 if (ua_chan
== NULL
) {
1218 /* Only malloc can fail here */
1221 shadow_copy_channel(ua_chan
, uchan
);
1223 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1225 /* Not found previously means that it does not exist on the tracer */
1226 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
1230 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1232 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1239 delete_ust_app_channel(-1, ua_chan
);
1244 * Create UST app event and create it on the tracer side.
1247 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1248 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1249 struct ust_app
*app
)
1252 struct lttng_ht_iter iter
;
1253 struct lttng_ht_node_str
*ua_event_node
;
1254 struct ust_app_event
*ua_event
;
1256 /* Get event node */
1257 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
1258 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
1259 if (ua_event_node
!= NULL
) {
1264 /* Does not exist so create one */
1265 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1266 if (ua_event
== NULL
) {
1267 /* Only malloc can failed so something is really wrong */
1271 shadow_copy_event(ua_event
, uevent
);
1273 /* Create it on the tracer side */
1274 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1276 /* Not found previously means that it does not exist on the tracer */
1277 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
1281 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
1283 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1290 /* Valid. Calling here is already in a read side lock */
1291 delete_ust_app_event(-1, ua_event
);
1296 * Create UST metadata and open it on the tracer side.
1298 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1299 char *pathname
, struct ust_app
*app
)
1303 if (ua_sess
->metadata
== NULL
) {
1304 /* Allocate UST metadata */
1305 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1306 if (ua_sess
->metadata
== NULL
) {
1307 /* malloc() failed */
1311 ret
= open_ust_metadata(app
, ua_sess
);
1313 DBG3("Opening metadata failed. Cleaning up memory");
1315 /* Cleanup failed metadata struct */
1316 free(ua_sess
->metadata
);
1318 * This is very important because delete_ust_app_session check if
1319 * the pointer is null or not in order to delete the metadata.
1321 ua_sess
->metadata
= NULL
;
1325 DBG2("UST metadata opened for app pid %d", app
->pid
);
1328 /* Open UST metadata stream */
1329 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1330 ret
= create_ust_stream(app
, ua_sess
);
1335 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1336 "%s/metadata", ua_sess
->path
);
1338 PERROR("asprintf UST create stream");
1342 DBG2("UST metadata stream object created for app pid %d",
1345 ERR("Attempting to create stream without metadata opened");
1356 * Return pointer to traceable apps list.
1358 struct lttng_ht
*ust_app_get_ht(void)
1364 * Return ust app pointer or NULL if not found.
1366 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1368 struct lttng_ht_node_ulong
*node
;
1369 struct lttng_ht_iter iter
;
1372 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1373 node
= lttng_ht_iter_get_node_ulong(&iter
);
1375 DBG2("UST app no found with pid %d", pid
);
1380 DBG2("Found UST app by pid %d", pid
);
1382 return caa_container_of(node
, struct ust_app
, pid_n
);
1390 * Using pid and uid (of the app), allocate a new ust_app struct and
1391 * add it to the global traceable app list.
1393 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1394 * bitness is not supported.
1396 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1398 struct ust_app
*lta
;
1401 if ((msg
->bits_per_long
== 64 &&
1402 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
1403 || (msg
->bits_per_long
== 32 &&
1404 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
1405 ERR("Registration failed: application \"%s\" (pid: %d) has "
1406 "%d-bit long, but no consumerd for this long size is available.\n",
1407 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1412 lttng_fd_put(LTTNG_FD_APPS
, 1);
1415 if (msg
->major
!= LTTNG_UST_COMM_MAJOR
) {
1416 ERR("Registration failed: application \"%s\" (pid: %d) has "
1417 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1418 msg
->name
, msg
->pid
, msg
->major
, msg
->minor
);
1423 lttng_fd_put(LTTNG_FD_APPS
, 1);
1426 lta
= zmalloc(sizeof(struct ust_app
));
1432 lta
->ppid
= msg
->ppid
;
1433 lta
->uid
= msg
->uid
;
1434 lta
->gid
= msg
->gid
;
1435 lta
->compatible
= 0; /* Not compatible until proven */
1436 lta
->bits_per_long
= msg
->bits_per_long
;
1437 lta
->v_major
= msg
->major
;
1438 lta
->v_minor
= msg
->minor
;
1439 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1440 lta
->name
[16] = '\0';
1441 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1443 lta
->pid
= msg
->pid
;
1444 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long)lta
->pid
);
1446 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long)lta
->sock
);
1451 * On a re-registration, we want to kick out the previous registration of
1454 lttng_ht_add_replace_ulong(ust_app_ht
, <a
->pid_n
);
1457 * The socket _should_ be unique until _we_ call close. So, a add_unique
1458 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1459 * already in the table.
1461 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, <a
->sock_n
);
1465 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1466 " (version %d.%d)", lta
->pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1467 lta
->sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1473 * Unregister app by removing it from the global traceable app list and freeing
1476 * The socket is already closed at this point so no close to sock.
1478 void ust_app_unregister(int sock
)
1480 struct ust_app
*lta
;
1481 struct lttng_ht_node_ulong
*node
;
1482 struct lttng_ht_iter iter
;
1487 /* Get the node reference for a call_rcu */
1488 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1489 node
= lttng_ht_iter_get_node_ulong(&iter
);
1491 ERR("Unable to find app by sock %d", sock
);
1495 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
1497 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
1499 /* Remove application from PID hash table */
1500 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
1503 /* Assign second node for deletion */
1504 iter
.iter
.node
= <a
->pid_n
.node
;
1507 * Ignore return value since the node might have been removed before by an
1508 * add replace during app registration because the PID can be reassigned by
1511 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1513 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
1518 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
1526 * Return traceable_app_count
1528 unsigned long ust_app_list_count(void)
1530 unsigned long count
;
1533 count
= lttng_ht_get_count(ust_app_ht
);
1540 * Fill events array with all events name of all registered apps.
1542 int ust_app_list_events(struct lttng_event
**events
)
1545 size_t nbmem
, count
= 0;
1546 struct lttng_ht_iter iter
;
1547 struct ust_app
*app
;
1548 struct lttng_event
*tmp
;
1550 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1551 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1553 PERROR("zmalloc ust app events");
1560 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1561 struct lttng_ust_tracepoint_iter uiter
;
1563 health_code_update(&health_thread_cmd
);
1565 if (!app
->compatible
) {
1567 * TODO: In time, we should notice the caller of this error by
1568 * telling him that this is a version error.
1572 handle
= ustctl_tracepoint_list(app
->sock
);
1574 ERR("UST app list events getting handle failed for app pid %d",
1579 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
1580 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
1581 health_code_update(&health_thread_cmd
);
1582 if (count
>= nbmem
) {
1583 /* In case the realloc fails, we free the memory */
1584 void *tmp_ptr
= (void *) tmp
;
1585 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1588 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1590 PERROR("realloc ust app events");
1596 memcpy(tmp
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1597 tmp
[count
].loglevel
= uiter
.loglevel
;
1598 tmp
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
1599 tmp
[count
].pid
= app
->pid
;
1600 tmp
[count
].enabled
= -1;
1608 DBG2("UST app list events done (%zu events)", count
);
1613 health_code_update(&health_thread_cmd
);
1618 * Fill events array with all events name of all registered apps.
1620 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
1623 size_t nbmem
, count
= 0;
1624 struct lttng_ht_iter iter
;
1625 struct ust_app
*app
;
1626 struct lttng_event_field
*tmp
;
1628 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1629 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
1631 PERROR("zmalloc ust app event fields");
1638 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1639 struct lttng_ust_field_iter uiter
;
1641 health_code_update(&health_thread_cmd
);
1643 if (!app
->compatible
) {
1645 * TODO: In time, we should notice the caller of this error by
1646 * telling him that this is a version error.
1650 handle
= ustctl_tracepoint_field_list(app
->sock
);
1652 ERR("UST app list event fields getting handle failed for app pid %d",
1657 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
1658 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
1659 health_code_update(&health_thread_cmd
);
1660 if (count
>= nbmem
) {
1661 /* In case the realloc fails, we free the memory */
1662 void *tmp_ptr
= (void *) tmp
;
1663 DBG2("Reallocating event field list from %zu to %zu entries", nbmem
,
1666 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event_field
));
1668 PERROR("realloc ust app event fields");
1675 memcpy(tmp
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
1676 tmp
[count
].type
= uiter
.type
;
1677 tmp
[count
].nowrite
= uiter
.nowrite
;
1679 memcpy(tmp
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
1680 tmp
[count
].event
.loglevel
= uiter
.loglevel
;
1681 tmp
[count
].event
.type
= LTTNG_UST_TRACEPOINT
;
1682 tmp
[count
].event
.pid
= app
->pid
;
1683 tmp
[count
].event
.enabled
= -1;
1691 DBG2("UST app list event fields done (%zu events)", count
);
1696 health_code_update(&health_thread_cmd
);
1701 * Free and clean all traceable apps of the global list.
1703 void ust_app_clean_list(void)
1706 struct ust_app
*app
;
1707 struct lttng_ht_iter iter
;
1709 DBG2("UST app cleaning registered apps hash table");
1713 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1714 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1716 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
1719 /* Cleanup socket hash table */
1720 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
1722 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
1726 /* Destroy is done only when the ht is empty */
1727 lttng_ht_destroy(ust_app_ht
);
1728 lttng_ht_destroy(ust_app_ht_by_sock
);
1734 * Init UST app hash table.
1736 void ust_app_ht_alloc(void)
1738 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1739 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1743 * For a specific UST session, disable the channel for all registered apps.
1745 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1746 struct ltt_ust_channel
*uchan
)
1749 struct lttng_ht_iter iter
;
1750 struct lttng_ht_node_str
*ua_chan_node
;
1751 struct ust_app
*app
;
1752 struct ust_app_session
*ua_sess
;
1753 struct ust_app_channel
*ua_chan
;
1755 if (usess
== NULL
|| uchan
== NULL
) {
1756 ERR("Disabling UST global channel with NULL values");
1761 DBG2("UST app disabling channel %s from global domain for session id %d",
1762 uchan
->name
, usess
->id
);
1766 /* For every registered applications */
1767 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1768 struct lttng_ht_iter uiter
;
1769 if (!app
->compatible
) {
1771 * TODO: In time, we should notice the caller of this error by
1772 * telling him that this is a version error.
1776 ua_sess
= lookup_session_by_app(usess
, app
);
1777 if (ua_sess
== NULL
) {
1782 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1783 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1784 /* If the session if found for the app, the channel must be there */
1785 assert(ua_chan_node
);
1787 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1788 /* The channel must not be already disabled */
1789 assert(ua_chan
->enabled
== 1);
1791 /* Disable channel onto application */
1792 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1794 /* XXX: We might want to report this error at some point... */
1806 * For a specific UST session, enable the channel for all registered apps.
1808 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1809 struct ltt_ust_channel
*uchan
)
1812 struct lttng_ht_iter iter
;
1813 struct ust_app
*app
;
1814 struct ust_app_session
*ua_sess
;
1816 if (usess
== NULL
|| uchan
== NULL
) {
1817 ERR("Adding UST global channel to NULL values");
1822 DBG2("UST app enabling channel %s to global domain for session id %d",
1823 uchan
->name
, usess
->id
);
1827 /* For every registered applications */
1828 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1829 if (!app
->compatible
) {
1831 * TODO: In time, we should notice the caller of this error by
1832 * telling him that this is a version error.
1836 ua_sess
= lookup_session_by_app(usess
, app
);
1837 if (ua_sess
== NULL
) {
1841 /* Enable channel onto application */
1842 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1844 /* XXX: We might want to report this error at some point... */
1856 * Disable an event in a channel and for a specific session.
1858 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1859 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1862 struct lttng_ht_iter iter
, uiter
;
1863 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1864 struct ust_app
*app
;
1865 struct ust_app_session
*ua_sess
;
1866 struct ust_app_channel
*ua_chan
;
1867 struct ust_app_event
*ua_event
;
1869 DBG("UST app disabling event %s for all apps in channel "
1870 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
1874 /* For all registered applications */
1875 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1876 if (!app
->compatible
) {
1878 * TODO: In time, we should notice the caller of this error by
1879 * telling him that this is a version error.
1883 ua_sess
= lookup_session_by_app(usess
, app
);
1884 if (ua_sess
== NULL
) {
1889 /* Lookup channel in the ust app session */
1890 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1891 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1892 if (ua_chan_node
== NULL
) {
1893 DBG2("Channel %s not found in session id %d for app pid %d."
1894 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
1897 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1899 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
1900 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1901 if (ua_event_node
== NULL
) {
1902 DBG2("Event %s not found in channel %s for app pid %d."
1903 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
1906 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1908 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1910 /* XXX: Report error someday... */
1921 * For a specific UST session and UST channel, the event for all
1924 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1925 struct ltt_ust_channel
*uchan
)
1928 struct lttng_ht_iter iter
, uiter
;
1929 struct lttng_ht_node_str
*ua_chan_node
;
1930 struct ust_app
*app
;
1931 struct ust_app_session
*ua_sess
;
1932 struct ust_app_channel
*ua_chan
;
1933 struct ust_app_event
*ua_event
;
1935 DBG("UST app disabling all event for all apps in channel "
1936 "%s for session id %d", uchan
->name
, usess
->id
);
1940 /* For all registered applications */
1941 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1942 if (!app
->compatible
) {
1944 * TODO: In time, we should notice the caller of this error by
1945 * telling him that this is a version error.
1949 ua_sess
= lookup_session_by_app(usess
, app
);
1950 /* If ua_sess is NULL, there is a code flow error */
1953 /* Lookup channel in the ust app session */
1954 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1955 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1956 /* If the channel is not found, there is a code flow error */
1957 assert(ua_chan_node
);
1959 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1961 /* Disable each events of channel */
1962 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
1964 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1966 /* XXX: Report error someday... */
1978 * For a specific UST session, create the channel for all registered apps.
1980 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1981 struct ltt_ust_channel
*uchan
)
1984 struct lttng_ht_iter iter
;
1985 struct ust_app
*app
;
1986 struct ust_app_session
*ua_sess
;
1987 struct ust_app_channel
*ua_chan
;
1989 /* Very wrong code flow */
1993 DBG2("UST app adding channel %s to global domain for session id %d",
1994 uchan
->name
, usess
->id
);
1998 /* For every registered applications */
1999 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2000 if (!app
->compatible
) {
2002 * TODO: In time, we should notice the caller of this error by
2003 * telling him that this is a version error.
2008 * Create session on the tracer side and add it to app session HT. Note
2009 * that if session exist, it will simply return a pointer to the ust
2012 ua_sess
= create_ust_app_session(usess
, app
);
2013 if (ua_sess
== NULL
) {
2014 /* The malloc() failed. */
2017 } else if (ua_sess
== (void *) -1UL) {
2018 /* The application's socket is not valid. Contiuing */
2023 /* Create channel onto application */
2024 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
2025 if (ua_chan
== NULL
) {
2026 /* Major problem here and it's maybe the tracer or malloc() */
2039 * Enable event for a specific session and channel on the tracer.
2041 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
2042 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2045 struct lttng_ht_iter iter
, uiter
;
2046 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2047 struct ust_app
*app
;
2048 struct ust_app_session
*ua_sess
;
2049 struct ust_app_channel
*ua_chan
;
2050 struct ust_app_event
*ua_event
;
2052 DBG("UST app enabling event %s for all apps for session id %d",
2053 uevent
->attr
.name
, usess
->id
);
2056 * NOTE: At this point, this function is called only if the session and
2057 * channel passed are already created for all apps. and enabled on the
2063 /* For all registered applications */
2064 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2065 if (!app
->compatible
) {
2067 * TODO: In time, we should notice the caller of this error by
2068 * telling him that this is a version error.
2072 ua_sess
= lookup_session_by_app(usess
, app
);
2073 /* If ua_sess is NULL, there is a code flow error */
2076 /* Lookup channel in the ust app session */
2077 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2078 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2079 /* If the channel is not found, there is a code flow error */
2080 assert(ua_chan_node
);
2082 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2084 lttng_ht_lookup(ua_chan
->events
, (void*)uevent
->attr
.name
, &uiter
);
2085 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2086 if (ua_event_node
== NULL
) {
2087 DBG3("UST app enable event %s not found for app PID %d."
2088 "Skipping app", uevent
->attr
.name
, app
->pid
);
2091 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2093 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2105 * For a specific existing UST session and UST channel, creates the event for
2106 * all registered apps.
2108 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
2109 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
2112 struct lttng_ht_iter iter
, uiter
;
2113 struct lttng_ht_node_str
*ua_chan_node
;
2114 struct ust_app
*app
;
2115 struct ust_app_session
*ua_sess
;
2116 struct ust_app_channel
*ua_chan
;
2118 DBG("UST app creating event %s for all apps for session id %d",
2119 uevent
->attr
.name
, usess
->id
);
2123 /* For all registered applications */
2124 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2125 if (!app
->compatible
) {
2127 * TODO: In time, we should notice the caller of this error by
2128 * telling him that this is a version error.
2132 ua_sess
= lookup_session_by_app(usess
, app
);
2133 /* If ua_sess is NULL, there is a code flow error */
2136 /* Lookup channel in the ust app session */
2137 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2138 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2139 /* If the channel is not found, there is a code flow error */
2140 assert(ua_chan_node
);
2142 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2144 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2146 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
2147 /* Possible value at this point: -ENOMEM. If so, we stop! */
2150 DBG2("UST app event %s already exist on app PID %d",
2151 uevent
->attr
.name
, app
->pid
);
2162 * Start tracing for a specific UST session and app.
2164 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2167 struct lttng_ht_iter iter
;
2168 struct ust_app_session
*ua_sess
;
2169 struct ust_app_channel
*ua_chan
;
2170 struct ltt_ust_stream
*ustream
;
2171 struct consumer_socket
*socket
;
2173 DBG("Starting tracing for ust app pid %d", app
->pid
);
2177 if (!app
->compatible
) {
2181 ua_sess
= lookup_session_by_app(usess
, app
);
2182 if (ua_sess
== NULL
) {
2183 goto error_rcu_unlock
;
2186 /* Upon restart, we skip the setup, already done */
2187 if (ua_sess
->started
) {
2191 /* Create directories if consumer is LOCAL and has a path defined. */
2192 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
2193 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
2194 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
2195 S_IRWXU
| S_IRWXG
, usess
->uid
, usess
->gid
);
2197 if (ret
!= -EEXIST
) {
2198 ERR("Trace directory creation error");
2200 goto error_rcu_unlock
;
2205 /* Indicate that the session has been started once */
2206 ua_sess
->started
= 1;
2208 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
2210 ret
= LTTNG_ERR_UST_META_FAIL
;
2211 goto error_rcu_unlock
;
2214 /* For each channel */
2215 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2217 /* Create all streams */
2219 /* Create UST stream */
2220 ustream
= zmalloc(sizeof(*ustream
));
2221 if (ustream
== NULL
) {
2222 PERROR("zmalloc ust stream");
2223 goto error_rcu_unlock
;
2226 /* We are going to receive 2 fds, we need to reserve them. */
2227 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2229 ERR("Exhausted number of available FD upon stream create");
2231 goto error_rcu_unlock
;
2234 health_code_update(&health_thread_cmd
);
2236 ret
= ustctl_create_stream(app
->sock
, ua_chan
->obj
,
2239 /* Got all streams */
2240 lttng_fd_put(LTTNG_FD_APPS
, 2);
2242 ret
= LTTNG_ERR_UST_STREAM_FAIL
;
2245 ustream
->handle
= ustream
->obj
->handle
;
2247 health_code_update(&health_thread_cmd
);
2249 /* Order is important */
2250 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
2251 ret
= snprintf(ustream
->name
, sizeof(ustream
->name
), "%s_%u",
2252 ua_chan
->name
, ua_chan
->streams
.count
);
2253 ua_chan
->streams
.count
++;
2255 PERROR("asprintf UST create stream");
2257 * XXX what should we do here with the
2262 DBG2("UST stream %d ready (handle: %d)", ua_chan
->streams
.count
,
2266 health_code_update(&health_thread_cmd
);
2269 switch (app
->bits_per_long
) {
2271 socket
= consumer_find_socket(uatomic_read(&ust_consumerd64_fd
),
2273 if (socket
== NULL
) {
2278 socket
= consumer_find_socket(uatomic_read(&ust_consumerd32_fd
),
2280 if (socket
== NULL
) {
2286 goto error_rcu_unlock
;
2289 /* Setup UST consumer socket and send fds to it */
2290 ret
= ust_consumer_send_session(ua_sess
, usess
->consumer
, socket
);
2292 goto error_rcu_unlock
;
2295 health_code_update(&health_thread_cmd
);
2298 /* This start the UST tracing */
2299 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
2301 ERR("Error starting tracing for app pid: %d", app
->pid
);
2302 goto error_rcu_unlock
;
2305 health_code_update(&health_thread_cmd
);
2307 /* Quiescent wait after starting trace */
2308 ustctl_wait_quiescent(app
->sock
);
2312 health_code_update(&health_thread_cmd
);
2317 health_code_update(&health_thread_cmd
);
2322 * Stop tracing for a specific UST session and app.
2324 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2327 struct lttng_ht_iter iter
;
2328 struct ust_app_session
*ua_sess
;
2329 struct ust_app_channel
*ua_chan
;
2331 DBG("Stopping tracing for ust app pid %d", app
->pid
);
2335 if (!app
->compatible
) {
2339 ua_sess
= lookup_session_by_app(usess
, app
);
2340 if (ua_sess
== NULL
) {
2341 /* Only malloc can failed so something is really wrong */
2342 goto error_rcu_unlock
;
2346 * If started = 0, it means that stop trace has been called for a session
2347 * that was never started. This is a code flow error and should never
2350 assert(ua_sess
->started
== 1);
2352 health_code_update(&health_thread_cmd
);
2354 /* This inhibits UST tracing */
2355 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
2357 ERR("Error stopping tracing for app pid: %d", app
->pid
);
2358 goto error_rcu_unlock
;
2361 health_code_update(&health_thread_cmd
);
2363 /* Quiescent wait after stopping trace */
2364 ustctl_wait_quiescent(app
->sock
);
2366 health_code_update(&health_thread_cmd
);
2368 /* Flushing buffers */
2369 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2371 health_code_update(&health_thread_cmd
);
2372 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_chan
->obj
);
2374 ERR("UST app PID %d channel %s flush failed with ret %d",
2375 app
->pid
, ua_chan
->name
, ret
);
2376 /* Continuing flushing all buffers */
2381 health_code_update(&health_thread_cmd
);
2383 /* Flush all buffers before stopping */
2384 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_sess
->metadata
->obj
);
2386 ERR("UST app PID %d metadata flush failed with ret %d", app
->pid
,
2392 health_code_update(&health_thread_cmd
);
2397 health_code_update(&health_thread_cmd
);
2402 * Destroy a specific UST session in apps.
2404 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2406 struct ust_app_session
*ua_sess
;
2407 struct lttng_ust_object_data obj
;
2408 struct lttng_ht_iter iter
;
2409 struct lttng_ht_node_ulong
*node
;
2412 DBG("Destroy tracing for ust app pid %d", app
->pid
);
2416 if (!app
->compatible
) {
2420 __lookup_session_by_app(usess
, app
, &iter
);
2421 node
= lttng_ht_iter_get_node_ulong(&iter
);
2423 /* Only malloc can failed so something is really wrong */
2424 goto error_rcu_unlock
;
2426 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2427 ret
= lttng_ht_del(app
->sessions
, &iter
);
2429 obj
.handle
= ua_sess
->handle
;
2432 obj
.memory_map_size
= 0;
2433 health_code_update(&health_thread_cmd
);
2434 ustctl_release_object(app
->sock
, &obj
);
2436 health_code_update(&health_thread_cmd
);
2437 delete_ust_app_session(app
->sock
, ua_sess
);
2439 /* Quiescent wait after stopping trace */
2440 ustctl_wait_quiescent(app
->sock
);
2444 health_code_update(&health_thread_cmd
);
2449 health_code_update(&health_thread_cmd
);
2454 * Start tracing for the UST session.
2456 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2459 struct lttng_ht_iter iter
;
2460 struct ust_app
*app
;
2462 DBG("Starting all UST traces");
2466 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2467 ret
= ust_app_start_trace(usess
, app
);
2469 /* Continue to next apps even on error */
2480 * Start tracing for the UST session.
2482 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2485 struct lttng_ht_iter iter
;
2486 struct ust_app
*app
;
2488 DBG("Stopping all UST traces");
2492 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2493 ret
= ust_app_stop_trace(usess
, app
);
2495 /* Continue to next apps even on error */
2506 * Destroy app UST session.
2508 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2511 struct lttng_ht_iter iter
;
2512 struct ust_app
*app
;
2514 DBG("Destroy all UST traces");
2518 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2519 ret
= ust_app_destroy_trace(usess
, app
);
2521 /* Continue to next apps even on error */
2532 * Add channels/events from UST global domain to registered apps at sock.
2534 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2537 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
2538 struct ust_app
*app
;
2539 struct ust_app_session
*ua_sess
;
2540 struct ust_app_channel
*ua_chan
;
2541 struct ust_app_event
*ua_event
;
2542 struct ust_app_ctx
*ua_ctx
;
2544 if (usess
== NULL
) {
2545 ERR("No UST session on global update. Returning");
2549 DBG2("UST app global update for app sock %d for session id %d", sock
,
2554 app
= find_app_by_sock(sock
);
2556 ERR("Failed to update app sock %d", sock
);
2560 if (!app
->compatible
) {
2564 ua_sess
= create_ust_app_session(usess
, app
);
2565 if (ua_sess
== NULL
|| ua_sess
== (void *) -1UL) {
2566 /* Tracer is gone for this session and has been freed */
2571 * We can iterate safely here over all UST app session sicne the create ust
2572 * app session above made a shadow copy of the UST global domain from the
2575 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2577 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2579 /* FIXME: Should we quit here or continue... */
2583 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
2585 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2587 /* FIXME: Should we quit here or continue... */
2593 /* For each events */
2594 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2596 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2598 /* FIXME: Should we quit here or continue... */
2602 /* Add context on events. */
2603 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter_ctx
.iter
,
2604 ua_ctx
, node
.node
) {
2605 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
2607 /* FIXME: Should we quit here or continue... */
2611 ret
= set_ust_event_filter(ua_event
, app
);
2613 /* FIXME: Should we quit here or continue... */
2619 if (usess
->start_trace
) {
2620 ret
= ust_app_start_trace(usess
, app
);
2625 DBG2("UST trace started for app pid %d", app
->pid
);
2634 * Add context to a specific channel for global UST domain.
2636 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2637 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2640 struct lttng_ht_node_str
*ua_chan_node
;
2641 struct lttng_ht_iter iter
, uiter
;
2642 struct ust_app_channel
*ua_chan
= NULL
;
2643 struct ust_app_session
*ua_sess
;
2644 struct ust_app
*app
;
2648 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2649 if (!app
->compatible
) {
2651 * TODO: In time, we should notice the caller of this error by
2652 * telling him that this is a version error.
2656 ua_sess
= lookup_session_by_app(usess
, app
);
2657 if (ua_sess
== NULL
) {
2661 /* Lookup channel in the ust app session */
2662 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2663 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2664 if (ua_chan_node
== NULL
) {
2667 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2670 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2681 * Add context to a specific event in a channel for global UST domain.
2683 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2684 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2685 struct ltt_ust_context
*uctx
)
2688 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2689 struct lttng_ht_iter iter
, uiter
;
2690 struct ust_app_session
*ua_sess
;
2691 struct ust_app_event
*ua_event
;
2692 struct ust_app_channel
*ua_chan
= NULL
;
2693 struct ust_app
*app
;
2697 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2698 if (!app
->compatible
) {
2700 * TODO: In time, we should notice the caller of this error by
2701 * telling him that this is a version error.
2705 ua_sess
= lookup_session_by_app(usess
, app
);
2706 if (ua_sess
== NULL
) {
2710 /* Lookup channel in the ust app session */
2711 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2712 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2713 if (ua_chan_node
== NULL
) {
2716 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2719 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2720 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2721 if (ua_event_node
== NULL
) {
2724 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2727 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2738 * Add context to a specific event in a channel for global UST domain.
2740 int ust_app_set_filter_event_glb(struct ltt_ust_session
*usess
,
2741 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2742 struct lttng_filter_bytecode
*bytecode
)
2745 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2746 struct lttng_ht_iter iter
, uiter
;
2747 struct ust_app_session
*ua_sess
;
2748 struct ust_app_event
*ua_event
;
2749 struct ust_app_channel
*ua_chan
= NULL
;
2750 struct ust_app
*app
;
2754 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2755 if (!app
->compatible
) {
2757 * TODO: In time, we should notice the caller of this error by
2758 * telling him that this is a version error.
2762 ua_sess
= lookup_session_by_app(usess
, app
);
2763 if (ua_sess
== NULL
) {
2767 /* Lookup channel in the ust app session */
2768 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2769 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2770 if (ua_chan_node
== NULL
) {
2773 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2776 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2777 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2778 if (ua_event_node
== NULL
) {
2781 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2784 ret
= set_ust_app_event_filter(ua_sess
, ua_event
, bytecode
, app
);
2795 * Enable event for a channel from a UST session for a specific PID.
2797 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2798 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2801 struct lttng_ht_iter iter
;
2802 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2803 struct ust_app
*app
;
2804 struct ust_app_session
*ua_sess
;
2805 struct ust_app_channel
*ua_chan
;
2806 struct ust_app_event
*ua_event
;
2808 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2812 app
= ust_app_find_by_pid(pid
);
2814 ERR("UST app enable event per PID %d not found", pid
);
2819 if (!app
->compatible
) {
2824 ua_sess
= lookup_session_by_app(usess
, app
);
2825 /* If ua_sess is NULL, there is a code flow error */
2828 /* Lookup channel in the ust app session */
2829 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2830 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2831 /* If the channel is not found, there is a code flow error */
2832 assert(ua_chan_node
);
2834 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2836 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2837 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2838 if (ua_event_node
== NULL
) {
2839 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2844 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2846 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2858 * Disable event for a channel from a UST session for a specific PID.
2860 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2861 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2864 struct lttng_ht_iter iter
;
2865 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2866 struct ust_app
*app
;
2867 struct ust_app_session
*ua_sess
;
2868 struct ust_app_channel
*ua_chan
;
2869 struct ust_app_event
*ua_event
;
2871 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2875 app
= ust_app_find_by_pid(pid
);
2877 ERR("UST app disable event per PID %d not found", pid
);
2882 if (!app
->compatible
) {
2887 ua_sess
= lookup_session_by_app(usess
, app
);
2888 /* If ua_sess is NULL, there is a code flow error */
2891 /* Lookup channel in the ust app session */
2892 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2893 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2894 if (ua_chan_node
== NULL
) {
2895 /* Channel does not exist, skip disabling */
2898 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2900 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2901 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2902 if (ua_event_node
== NULL
) {
2903 /* Event does not exist, skip disabling */
2906 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2908 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2919 * Validate version of UST apps and set the compatible bit.
2921 int ust_app_validate_version(int sock
)
2924 struct ust_app
*app
;
2928 app
= find_app_by_sock(sock
);
2931 health_code_update(&health_thread_cmd
);
2933 ret
= ustctl_tracer_version(sock
, &app
->version
);
2938 /* Validate version */
2939 if (app
->version
.major
!= UST_APP_MAJOR_VERSION
) {
2943 DBG2("UST app PID %d is compatible with internal major version %d "
2944 "(supporting == %d)", app
->pid
, app
->version
.major
,
2945 UST_APP_MAJOR_VERSION
);
2946 app
->compatible
= 1;
2948 health_code_update(&health_thread_cmd
);
2952 DBG2("UST app PID %d is not compatible with internal major version %d "
2953 "(supporting == %d)", app
->pid
, app
->version
.major
,
2954 UST_APP_MAJOR_VERSION
);
2955 app
->compatible
= 0;
2957 health_code_update(&health_thread_cmd
);
2962 * Calibrate registered applications.
2964 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
2967 struct lttng_ht_iter iter
;
2968 struct ust_app
*app
;
2972 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2973 if (!app
->compatible
) {
2975 * TODO: In time, we should notice the caller of this error by
2976 * telling him that this is a version error.
2981 health_code_update(&health_thread_cmd
);
2983 ret
= ustctl_calibrate(app
->sock
, calibrate
);
2987 /* Means that it's not implemented on the tracer side. */
2991 /* TODO: Report error to user */
2992 DBG2("Calibrate app PID %d returned with error %d",
2999 DBG("UST app global domain calibration finished");
3003 health_code_update(&health_thread_cmd
);