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>
29 #include <common/common.h>
32 #include "ust-consumer.h"
36 * Delete ust context safely. RCU read lock must be held before calling
40 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
43 ustctl_release_object(sock
, ua_ctx
->obj
);
50 * Delete ust app event safely. RCU read lock must be held before calling
54 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
57 struct lttng_ht_iter iter
;
58 struct ust_app_ctx
*ua_ctx
;
60 /* Destroy each context of event */
61 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter
.iter
, ua_ctx
,
63 ret
= lttng_ht_del(ua_event
->ctx
, &iter
);
65 delete_ust_app_ctx(sock
, ua_ctx
);
67 lttng_ht_destroy(ua_event
->ctx
);
69 if (ua_event
->obj
!= NULL
) {
70 ustctl_release_object(sock
, ua_event
->obj
);
77 * Delete ust app stream safely. RCU read lock must be held before calling
81 void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
84 ustctl_release_object(sock
, stream
->obj
);
91 * Delete ust app channel safely. RCU read lock must be held before calling
95 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
98 struct lttng_ht_iter iter
;
99 struct ust_app_event
*ua_event
;
100 struct ust_app_ctx
*ua_ctx
;
101 struct ltt_ust_stream
*stream
, *stmp
;
104 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
105 cds_list_del(&stream
->list
);
106 delete_ust_app_stream(sock
, stream
);
110 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
111 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
113 delete_ust_app_ctx(sock
, ua_ctx
);
115 lttng_ht_destroy(ua_chan
->ctx
);
118 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
120 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
122 delete_ust_app_event(sock
, ua_event
);
124 lttng_ht_destroy(ua_chan
->events
);
126 if (ua_chan
->obj
!= NULL
) {
127 ustctl_release_object(sock
, ua_chan
->obj
);
134 * Delete ust app session safely. RCU read lock must be held before calling
138 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
)
141 struct lttng_ht_iter iter
;
142 struct ust_app_channel
*ua_chan
;
144 if (ua_sess
->metadata
) {
145 if (ua_sess
->metadata
->stream_obj
) {
146 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
147 free(ua_sess
->metadata
->stream_obj
);
149 if (ua_sess
->metadata
->obj
) {
150 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
151 free(ua_sess
->metadata
->obj
);
155 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
157 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
159 delete_ust_app_channel(sock
, ua_chan
);
161 lttng_ht_destroy(ua_sess
->channels
);
163 if (ua_sess
->handle
!= -1) {
164 ustctl_release_handle(sock
, ua_sess
->handle
);
170 * Delete a traceable application structure from the global list. Never call
171 * this function outside of a call_rcu call.
174 void delete_ust_app(struct ust_app
*app
)
177 struct lttng_ht_iter iter
;
178 struct ust_app_session
*ua_sess
;
182 /* Delete ust app sessions info */
187 cds_lfht_for_each_entry(app
->sessions
->ht
, &iter
.iter
, ua_sess
,
189 ret
= lttng_ht_del(app
->sessions
, &iter
);
191 delete_ust_app_session(app
->sock
, ua_sess
);
193 lttng_ht_destroy(app
->sessions
);
196 * Wait until we have deleted the application from the sock hash table
197 * before closing this socket, otherwise an application could re-use the
198 * socket ID and race with the teardown, using the same hash table entry.
200 * It's OK to leave the close in call_rcu. We want it to stay unique for
201 * all RCU readers that could run concurrently with unregister app,
202 * therefore we _need_ to only close that socket after a grace period. So
203 * it should stay in this RCU callback.
205 * This close() is a very important step of the synchronization model so
206 * every modification to this function must be carefully reviewed.
213 DBG2("UST app pid %d deleted", app
->pid
);
220 * URCU intermediate call to delete an UST app.
223 void delete_ust_app_rcu(struct rcu_head
*head
)
225 struct lttng_ht_node_ulong
*node
=
226 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
227 struct ust_app
*app
=
228 caa_container_of(node
, struct ust_app
, pid_n
);
230 DBG3("Call RCU deleting app PID %d", app
->pid
);
235 * Alloc new UST app session.
238 struct ust_app_session
*alloc_ust_app_session(void)
240 struct ust_app_session
*ua_sess
;
242 /* Init most of the default value by allocating and zeroing */
243 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
244 if (ua_sess
== NULL
) {
249 ua_sess
->handle
= -1;
250 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
259 * Alloc new UST app channel.
262 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
263 struct lttng_ust_channel
*attr
)
265 struct ust_app_channel
*ua_chan
;
267 /* Init most of the default value by allocating and zeroing */
268 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
269 if (ua_chan
== NULL
) {
274 /* Setup channel name */
275 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
276 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
278 ua_chan
->enabled
= 1;
279 ua_chan
->handle
= -1;
280 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
281 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
282 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
284 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
286 /* Copy attributes */
288 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
291 DBG3("UST app channel %s allocated", ua_chan
->name
);
300 * Alloc new UST app event.
303 struct ust_app_event
*alloc_ust_app_event(char *name
,
304 struct lttng_ust_event
*attr
)
306 struct ust_app_event
*ua_event
;
308 /* Init most of the default value by allocating and zeroing */
309 ua_event
= zmalloc(sizeof(struct ust_app_event
));
310 if (ua_event
== NULL
) {
315 ua_event
->enabled
= 1;
316 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
317 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
318 ua_event
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
319 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
321 /* Copy attributes */
323 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
326 DBG3("UST app event %s allocated", ua_event
->name
);
335 * Alloc new UST app context.
338 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
340 struct ust_app_ctx
*ua_ctx
;
342 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
343 if (ua_ctx
== NULL
) {
348 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
351 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
358 * Find an ust_app using the sock and return it. RCU read side lock must be
359 * held before calling this helper function.
362 struct ust_app
*find_app_by_sock(int sock
)
364 struct lttng_ht_node_ulong
*node
;
365 struct lttng_ht_iter iter
;
367 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
368 node
= lttng_ht_iter_get_node_ulong(&iter
);
370 DBG2("UST app find by sock %d not found", sock
);
374 return caa_container_of(node
, struct ust_app
, sock_n
);
381 * Create the channel context on the tracer.
384 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
385 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
389 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
390 ua_chan
->obj
, &ua_ctx
->obj
);
395 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
397 DBG2("UST app context created successfully for channel %s", ua_chan
->name
);
404 * Create the event context on the tracer.
407 int create_ust_event_context(struct ust_app_event
*ua_event
,
408 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
412 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
413 ua_event
->obj
, &ua_ctx
->obj
);
418 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
420 DBG2("UST app context created successfully for event %s", ua_event
->name
);
427 * Disable the specified event on to UST tracer for the UST session.
429 static int disable_ust_event(struct ust_app
*app
,
430 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
434 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
436 ERR("UST app event %s disable failed for app (pid: %d) "
437 "and session handle %d with ret %d",
438 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
442 DBG2("UST app event %s disabled successfully for app (pid: %d)",
443 ua_event
->attr
.name
, app
->pid
);
450 * Disable the specified channel on to UST tracer for the UST session.
452 static int disable_ust_channel(struct ust_app
*app
,
453 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
457 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
459 ERR("UST app channel %s disable failed for app (pid: %d) "
460 "and session handle %d with ret %d",
461 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
465 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
466 ua_chan
->name
, app
->pid
);
473 * Enable the specified channel on to UST tracer for the UST session.
475 static int enable_ust_channel(struct ust_app
*app
,
476 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
480 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
482 ERR("UST app channel %s enable failed for app (pid: %d) "
483 "and session handle %d with ret %d",
484 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
488 ua_chan
->enabled
= 1;
490 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
491 ua_chan
->name
, app
->pid
);
498 * Enable the specified event on to UST tracer for the UST session.
500 static int enable_ust_event(struct ust_app
*app
,
501 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
505 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
507 ERR("UST app event %s enable failed for app (pid: %d) "
508 "and session handle %d with ret %d",
509 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
513 DBG2("UST app event %s enabled successfully for app (pid: %d)",
514 ua_event
->attr
.name
, app
->pid
);
521 * Open metadata onto the UST tracer for a UST session.
523 static int open_ust_metadata(struct ust_app
*app
,
524 struct ust_app_session
*ua_sess
)
527 struct lttng_ust_channel_attr uattr
;
529 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
530 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
531 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
532 uattr
.switch_timer_interval
=
533 ua_sess
->metadata
->attr
.switch_timer_interval
;
534 uattr
.read_timer_interval
=
535 ua_sess
->metadata
->attr
.read_timer_interval
;
536 uattr
.output
= ua_sess
->metadata
->attr
.output
;
538 /* UST tracer metadata creation */
539 ret
= ustctl_open_metadata(app
->sock
, ua_sess
->handle
, &uattr
,
540 &ua_sess
->metadata
->obj
);
542 ERR("UST app open metadata failed for app pid:%d with ret %d",
547 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
554 * Create stream onto the UST tracer for a UST session.
556 static int create_ust_stream(struct ust_app
*app
,
557 struct ust_app_session
*ua_sess
)
561 ret
= ustctl_create_stream(app
->sock
, ua_sess
->metadata
->obj
,
562 &ua_sess
->metadata
->stream_obj
);
564 ERR("UST create metadata stream failed");
573 * Create the specified channel onto the UST tracer for a UST session.
575 static int create_ust_channel(struct ust_app
*app
,
576 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
580 /* TODO: remove cast and use lttng-ust-abi.h */
581 ret
= ustctl_create_channel(app
->sock
, ua_sess
->handle
,
582 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
584 ERR("Creating channel %s for app (pid: %d, sock: %d) "
585 "and session handle %d with ret %d",
586 ua_chan
->name
, app
->pid
, app
->sock
,
587 ua_sess
->handle
, ret
);
591 ua_chan
->handle
= ua_chan
->obj
->handle
;
593 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
594 ua_chan
->name
, app
->pid
, app
->sock
);
596 /* If channel is not enabled, disable it on the tracer */
597 if (!ua_chan
->enabled
) {
598 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
609 * Create the specified event onto the UST tracer for a UST session.
612 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
613 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
617 /* Create UST event on tracer */
618 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
621 if (ret
== -EEXIST
) {
625 ERR("Error ustctl create event %s for app pid: %d with ret %d",
626 ua_event
->attr
.name
, app
->pid
, ret
);
630 ua_event
->handle
= ua_event
->obj
->handle
;
632 DBG2("UST app event %s created successfully for pid:%d",
633 ua_event
->attr
.name
, app
->pid
);
635 /* If event not enabled, disable it on the tracer */
636 if (ua_event
->enabled
== 0) {
637 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
640 * If we hit an EPERM, something is wrong with our disable call. If
641 * we get an EEXIST, there is a problem on the tracer side since we
646 /* Code flow problem */
649 /* It's OK for our use case. */
664 * Copy data between an UST app event and a LTT event.
666 static void shadow_copy_event(struct ust_app_event
*ua_event
,
667 struct ltt_ust_event
*uevent
)
669 struct lttng_ht_iter iter
;
670 struct ltt_ust_context
*uctx
;
671 struct ust_app_ctx
*ua_ctx
;
673 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
674 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
676 ua_event
->enabled
= uevent
->enabled
;
678 /* Copy event attributes */
679 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
681 cds_lfht_for_each_entry(uevent
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
682 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
683 if (ua_ctx
== NULL
) {
684 /* malloc() failed. We should simply stop */
688 lttng_ht_node_init_ulong(&ua_ctx
->node
,
689 (unsigned long) ua_ctx
->ctx
.ctx
);
690 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
695 * Copy data between an UST app channel and a LTT channel.
697 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
698 struct ltt_ust_channel
*uchan
)
700 struct lttng_ht_iter iter
;
701 struct lttng_ht_node_str
*ua_event_node
;
702 struct ltt_ust_event
*uevent
;
703 struct ltt_ust_context
*uctx
;
704 struct ust_app_event
*ua_event
;
705 struct ust_app_ctx
*ua_ctx
;
707 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
709 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
710 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
711 /* Copy event attributes */
712 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
714 ua_chan
->enabled
= uchan
->enabled
;
716 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
717 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
718 if (ua_ctx
== NULL
) {
721 lttng_ht_node_init_ulong(&ua_ctx
->node
,
722 (unsigned long) ua_ctx
->ctx
.ctx
);
723 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
726 /* Copy all events from ltt ust channel to ust app channel */
727 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
728 struct lttng_ht_iter uiter
;
730 lttng_ht_lookup(ua_chan
->events
, (void *) uevent
->attr
.name
, &uiter
);
731 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
732 if (ua_event_node
== NULL
) {
733 DBG2("UST event %s not found on shadow copy channel",
735 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
736 if (ua_event
== NULL
) {
739 shadow_copy_event(ua_event
, uevent
);
740 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
744 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
748 * Copy data between a UST app session and a regular LTT session.
750 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
751 struct ltt_ust_session
*usess
, struct ust_app
*app
)
753 struct lttng_ht_node_str
*ua_chan_node
;
754 struct lttng_ht_iter iter
;
755 struct ltt_ust_channel
*uchan
;
756 struct ust_app_channel
*ua_chan
;
762 /* Get date and time for unique app path */
764 timeinfo
= localtime(&rawtime
);
765 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
767 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
769 ua_sess
->id
= usess
->id
;
770 ua_sess
->uid
= usess
->uid
;
771 ua_sess
->gid
= usess
->gid
;
773 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s/%s-%d-%s", usess
->pathname
,
774 app
->name
, app
->pid
, datetime
);
776 PERROR("asprintf UST shadow copy session");
777 /* TODO: We cannot return an error from here.. */
781 /* TODO: support all UST domain */
783 /* Iterate over all channels in global domain. */
784 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
786 struct lttng_ht_iter uiter
;
788 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
789 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
790 if (ua_chan_node
!= NULL
) {
791 /* Session exist. Contiuing. */
795 DBG2("Channel %s not found on shadow session copy, creating it",
797 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
798 if (ua_chan
== NULL
) {
799 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
803 shadow_copy_channel(ua_chan
, uchan
);
804 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
809 * Lookup sesison wrapper.
812 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
813 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
815 /* Get right UST app session from app */
816 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
820 * Return ust app session from the app session hashtable using the UST session
823 static struct ust_app_session
*lookup_session_by_app(
824 struct ltt_ust_session
*usess
, struct ust_app
*app
)
826 struct lttng_ht_iter iter
;
827 struct lttng_ht_node_ulong
*node
;
829 __lookup_session_by_app(usess
, app
, &iter
);
830 node
= lttng_ht_iter_get_node_ulong(&iter
);
835 return caa_container_of(node
, struct ust_app_session
, node
);
842 * Create a UST session onto the tracer of app and add it the session
845 * Return ust app session or NULL on error.
847 static struct ust_app_session
*create_ust_app_session(
848 struct ltt_ust_session
*usess
, struct ust_app
*app
)
851 struct ust_app_session
*ua_sess
;
853 ua_sess
= lookup_session_by_app(usess
, app
);
854 if (ua_sess
== NULL
) {
855 DBG2("UST app pid: %d session id %d not found, creating it",
856 app
->pid
, usess
->id
);
857 ua_sess
= alloc_ust_app_session();
858 if (ua_sess
== NULL
) {
859 /* Only malloc can failed so something is really wrong */
862 shadow_copy_session(ua_sess
, usess
, app
);
865 if (ua_sess
->handle
== -1) {
866 ret
= ustctl_create_session(app
->sock
);
868 ERR("Creating session for app pid %d", app
->pid
);
869 /* This means that the tracer is gone... */
870 ua_sess
= (void*) -1UL;
874 ua_sess
->handle
= ret
;
876 /* Add ust app session to app's HT */
877 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
878 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
880 DBG2("UST app session created successfully with handle %d", ret
);
887 delete_ust_app_session(-1, ua_sess
);
892 * Create a context for the channel on the tracer.
895 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
896 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
900 struct lttng_ht_iter iter
;
901 struct lttng_ht_node_ulong
*node
;
902 struct ust_app_ctx
*ua_ctx
;
904 DBG2("UST app adding context to channel %s", ua_chan
->name
);
906 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
907 node
= lttng_ht_iter_get_node_ulong(&iter
);
913 ua_ctx
= alloc_ust_app_ctx(uctx
);
914 if (ua_ctx
== NULL
) {
920 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
921 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
923 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
933 * Create an UST context and enable it for the event on the tracer.
936 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
937 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
941 struct lttng_ht_iter iter
;
942 struct lttng_ht_node_ulong
*node
;
943 struct ust_app_ctx
*ua_ctx
;
945 DBG2("UST app adding context to event %s", ua_event
->name
);
947 lttng_ht_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
948 node
= lttng_ht_iter_get_node_ulong(&iter
);
954 ua_ctx
= alloc_ust_app_ctx(uctx
);
955 if (ua_ctx
== NULL
) {
961 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
962 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
964 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
974 * Enable on the tracer side a ust app event for the session and channel.
977 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
978 struct ust_app_event
*ua_event
, struct ust_app
*app
)
982 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
987 ua_event
->enabled
= 1;
994 * Disable on the tracer side a ust app event for the session and channel.
996 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
997 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1001 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1006 ua_event
->enabled
= 0;
1013 * Lookup ust app channel for session and disable it on the tracer side.
1016 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1017 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1021 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1026 ua_chan
->enabled
= 0;
1033 * Lookup ust app channel for session and enable it on the tracer side.
1035 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1036 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1039 struct lttng_ht_iter iter
;
1040 struct lttng_ht_node_str
*ua_chan_node
;
1041 struct ust_app_channel
*ua_chan
;
1043 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1044 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1045 if (ua_chan_node
== NULL
) {
1046 DBG2("Unable to find channel %s in ust session id %u",
1047 uchan
->name
, ua_sess
->id
);
1051 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1053 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1063 * Create UST app channel and create it on the tracer.
1065 static struct ust_app_channel
*create_ust_app_channel(
1066 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1067 struct ust_app
*app
)
1070 struct lttng_ht_iter iter
;
1071 struct lttng_ht_node_str
*ua_chan_node
;
1072 struct ust_app_channel
*ua_chan
;
1074 /* Lookup channel in the ust app session */
1075 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1076 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1077 if (ua_chan_node
!= NULL
) {
1078 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1082 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1083 if (ua_chan
== NULL
) {
1084 /* Only malloc can fail here */
1087 shadow_copy_channel(ua_chan
, uchan
);
1089 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1091 /* Not found previously means that it does not exist on the tracer */
1092 assert(ret
!= -EEXIST
);
1096 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1098 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1105 delete_ust_app_channel(-1, ua_chan
);
1110 * Create UST app event and create it on the tracer side.
1113 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1114 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1115 struct ust_app
*app
)
1118 struct lttng_ht_iter iter
;
1119 struct lttng_ht_node_str
*ua_event_node
;
1120 struct ust_app_event
*ua_event
;
1122 /* Get event node */
1123 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
1124 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
1125 if (ua_event_node
!= NULL
) {
1130 /* Does not exist so create one */
1131 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1132 if (ua_event
== NULL
) {
1133 /* Only malloc can failed so something is really wrong */
1137 shadow_copy_event(ua_event
, uevent
);
1139 /* Create it on the tracer side */
1140 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1142 /* Not found previously means that it does not exist on the tracer */
1143 assert(ret
!= -EEXIST
);
1147 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
1149 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1156 /* Valid. Calling here is already in a read side lock */
1157 delete_ust_app_event(-1, ua_event
);
1162 * Create UST metadata and open it on the tracer side.
1164 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1165 char *pathname
, struct ust_app
*app
)
1169 if (ua_sess
->metadata
== NULL
) {
1170 /* Allocate UST metadata */
1171 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1172 if (ua_sess
->metadata
== NULL
) {
1173 /* malloc() failed */
1177 ret
= open_ust_metadata(app
, ua_sess
);
1179 DBG3("Opening metadata failed. Cleaning up memory");
1181 /* Cleanup failed metadata struct */
1182 free(ua_sess
->metadata
);
1184 * This is very important because delete_ust_app_session check if
1185 * the pointer is null or not in order to delete the metadata.
1187 ua_sess
->metadata
= NULL
;
1191 DBG2("UST metadata opened for app pid %d", app
->pid
);
1194 /* Open UST metadata stream */
1195 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1196 ret
= create_ust_stream(app
, ua_sess
);
1201 ret
= run_as_mkdir(ua_sess
->path
, S_IRWXU
| S_IRWXG
,
1202 ua_sess
->uid
, ua_sess
->gid
);
1204 PERROR("mkdir UST metadata");
1208 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1209 "%s/metadata", ua_sess
->path
);
1211 PERROR("asprintf UST create stream");
1215 DBG2("UST metadata stream object created for app pid %d",
1218 ERR("Attempting to create stream without metadata opened");
1229 * Return pointer to traceable apps list.
1231 struct lttng_ht
*ust_app_get_ht(void)
1237 * Return ust app pointer or NULL if not found.
1239 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1241 struct lttng_ht_node_ulong
*node
;
1242 struct lttng_ht_iter iter
;
1245 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1246 node
= lttng_ht_iter_get_node_ulong(&iter
);
1248 DBG2("UST app no found with pid %d", pid
);
1253 DBG2("Found UST app by pid %d", pid
);
1255 return caa_container_of(node
, struct ust_app
, pid_n
);
1263 * Using pid and uid (of the app), allocate a new ust_app struct and
1264 * add it to the global traceable app list.
1266 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1267 * bitness is not supported.
1269 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1271 struct ust_app
*lta
;
1274 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1275 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1276 ERR("Registration failed: application \"%s\" (pid: %d) has "
1277 "%d-bit long, but no consumerd for this long size is available.\n",
1278 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1285 if (msg
->major
!= LTTNG_UST_COMM_MAJOR
) {
1286 ERR("Registration failed: application \"%s\" (pid: %d) has "
1287 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1288 msg
->name
, msg
->pid
, msg
->major
, msg
->minor
);
1295 lta
= zmalloc(sizeof(struct ust_app
));
1301 lta
->ppid
= msg
->ppid
;
1302 lta
->uid
= msg
->uid
;
1303 lta
->gid
= msg
->gid
;
1304 lta
->compatible
= 0; /* Not compatible until proven */
1305 lta
->bits_per_long
= msg
->bits_per_long
;
1306 lta
->v_major
= msg
->major
;
1307 lta
->v_minor
= msg
->minor
;
1308 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1309 lta
->name
[16] = '\0';
1310 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1312 lta
->pid
= msg
->pid
;
1313 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long)lta
->pid
);
1315 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long)lta
->sock
);
1320 * On a re-registration, we want to kick out the previous registration of
1323 lttng_ht_add_replace_ulong(ust_app_ht
, <a
->pid_n
);
1326 * The socket _should_ be unique until _we_ call close. So, a add_unique
1327 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1328 * already in the table.
1330 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, <a
->sock_n
);
1334 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1335 " (version %d.%d)", lta
->pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1336 lta
->sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1342 * Unregister app by removing it from the global traceable app list and freeing
1345 * The socket is already closed at this point so no close to sock.
1347 void ust_app_unregister(int sock
)
1349 struct ust_app
*lta
;
1350 struct lttng_ht_node_ulong
*node
;
1351 struct lttng_ht_iter iter
;
1356 /* Get the node reference for a call_rcu */
1357 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1358 node
= lttng_ht_iter_get_node_ulong(&iter
);
1360 ERR("Unable to find app by sock %d", sock
);
1364 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
1366 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
1368 /* Remove application from PID hash table */
1369 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
1372 /* Assign second node for deletion */
1373 iter
.iter
.node
= <a
->pid_n
.node
;
1375 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1379 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
1387 * Return traceable_app_count
1389 unsigned long ust_app_list_count(void)
1391 unsigned long count
;
1394 count
= lttng_ht_get_count(ust_app_ht
);
1401 * Fill events array with all events name of all registered apps.
1403 int ust_app_list_events(struct lttng_event
**events
)
1406 size_t nbmem
, count
= 0;
1407 struct lttng_ht_iter iter
;
1408 struct ust_app
*app
;
1409 struct lttng_event
*tmp
;
1411 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1412 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1414 PERROR("zmalloc ust app events");
1421 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1422 struct lttng_ust_tracepoint_iter uiter
;
1424 if (!app
->compatible
) {
1426 * TODO: In time, we should notice the caller of this error by
1427 * telling him that this is a version error.
1431 handle
= ustctl_tracepoint_list(app
->sock
);
1433 ERR("UST app list events getting handle failed for app pid %d",
1438 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
1439 &uiter
)) != -ENOENT
) {
1440 if (count
>= nbmem
) {
1441 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1444 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1446 PERROR("realloc ust app events");
1451 memcpy(tmp
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1452 tmp
[count
].loglevel
= uiter
.loglevel
;
1453 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1454 tmp
[count
].pid
= app
->pid
;
1455 tmp
[count
].enabled
= -1;
1463 DBG2("UST app list events done (%zu events)", count
);
1472 * Free and clean all traceable apps of the global list.
1474 void ust_app_clean_list(void)
1477 struct lttng_ht_iter iter
;
1478 struct lttng_ht_node_ulong
*node
;
1480 DBG2("UST app cleaning registered apps hash table");
1484 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, node
, node
) {
1485 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1487 call_rcu(&node
->head
, delete_ust_app_rcu
);
1490 /* Cleanup socket hash table */
1491 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
1492 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
1496 /* Destroy is done only when the ht is empty */
1497 lttng_ht_destroy(ust_app_ht
);
1498 lttng_ht_destroy(ust_app_ht_by_sock
);
1504 * Init UST app hash table.
1506 void ust_app_ht_alloc(void)
1508 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1509 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1513 * For a specific UST session, disable the channel for all registered apps.
1515 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1516 struct ltt_ust_channel
*uchan
)
1519 struct lttng_ht_iter iter
;
1520 struct lttng_ht_node_str
*ua_chan_node
;
1521 struct ust_app
*app
;
1522 struct ust_app_session
*ua_sess
;
1523 struct ust_app_channel
*ua_chan
;
1525 if (usess
== NULL
|| uchan
== NULL
) {
1526 ERR("Disabling UST global channel with NULL values");
1531 DBG2("UST app disabling channel %s from global domain for session id %d",
1532 uchan
->name
, usess
->id
);
1536 /* For every registered applications */
1537 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1538 struct lttng_ht_iter uiter
;
1539 if (!app
->compatible
) {
1541 * TODO: In time, we should notice the caller of this error by
1542 * telling him that this is a version error.
1546 ua_sess
= lookup_session_by_app(usess
, app
);
1547 if (ua_sess
== NULL
) {
1552 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1553 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1554 /* If the session if found for the app, the channel must be there */
1555 assert(ua_chan_node
);
1557 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1558 /* The channel must not be already disabled */
1559 assert(ua_chan
->enabled
== 1);
1561 /* Disable channel onto application */
1562 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1564 /* XXX: We might want to report this error at some point... */
1576 * For a specific UST session, enable the channel for all registered apps.
1578 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1579 struct ltt_ust_channel
*uchan
)
1582 struct lttng_ht_iter iter
;
1583 struct ust_app
*app
;
1584 struct ust_app_session
*ua_sess
;
1586 if (usess
== NULL
|| uchan
== NULL
) {
1587 ERR("Adding UST global channel to NULL values");
1592 DBG2("UST app enabling channel %s to global domain for session id %d",
1593 uchan
->name
, usess
->id
);
1597 /* For every registered applications */
1598 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1599 if (!app
->compatible
) {
1601 * TODO: In time, we should notice the caller of this error by
1602 * telling him that this is a version error.
1606 ua_sess
= lookup_session_by_app(usess
, app
);
1607 if (ua_sess
== NULL
) {
1611 /* Enable channel onto application */
1612 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1614 /* XXX: We might want to report this error at some point... */
1626 * Disable an event in a channel and for a specific session.
1628 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1629 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1632 struct lttng_ht_iter iter
, uiter
;
1633 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1634 struct ust_app
*app
;
1635 struct ust_app_session
*ua_sess
;
1636 struct ust_app_channel
*ua_chan
;
1637 struct ust_app_event
*ua_event
;
1639 DBG("UST app disabling event %s for all apps in channel "
1640 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
1644 /* For all registered applications */
1645 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1646 if (!app
->compatible
) {
1648 * TODO: In time, we should notice the caller of this error by
1649 * telling him that this is a version error.
1653 ua_sess
= lookup_session_by_app(usess
, app
);
1654 if (ua_sess
== NULL
) {
1659 /* Lookup channel in the ust app session */
1660 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1661 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1662 if (ua_chan_node
== NULL
) {
1663 DBG2("Channel %s not found in session id %d for app pid %d."
1664 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
1667 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1669 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
1670 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1671 if (ua_event_node
== NULL
) {
1672 DBG2("Event %s not found in channel %s for app pid %d."
1673 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
1676 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1678 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1680 /* XXX: Report error someday... */
1691 * For a specific UST session and UST channel, the event for all
1694 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1695 struct ltt_ust_channel
*uchan
)
1698 struct lttng_ht_iter iter
, uiter
;
1699 struct lttng_ht_node_str
*ua_chan_node
;
1700 struct ust_app
*app
;
1701 struct ust_app_session
*ua_sess
;
1702 struct ust_app_channel
*ua_chan
;
1703 struct ust_app_event
*ua_event
;
1705 DBG("UST app disabling all event for all apps in channel "
1706 "%s for session id %d", uchan
->name
, usess
->id
);
1710 /* For all registered applications */
1711 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1712 if (!app
->compatible
) {
1714 * TODO: In time, we should notice the caller of this error by
1715 * telling him that this is a version error.
1719 ua_sess
= lookup_session_by_app(usess
, app
);
1720 /* If ua_sess is NULL, there is a code flow error */
1723 /* Lookup channel in the ust app session */
1724 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1725 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1726 /* If the channel is not found, there is a code flow error */
1727 assert(ua_chan_node
);
1729 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1731 /* Disable each events of channel */
1732 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
1734 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1736 /* XXX: Report error someday... */
1748 * For a specific UST session, create the channel for all registered apps.
1750 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1751 struct ltt_ust_channel
*uchan
)
1753 struct lttng_ht_iter iter
;
1754 struct ust_app
*app
;
1755 struct ust_app_session
*ua_sess
;
1756 struct ust_app_channel
*ua_chan
;
1758 /* Very wrong code flow */
1762 DBG2("UST app adding channel %s to global domain for session id %d",
1763 uchan
->name
, usess
->id
);
1767 /* For every registered applications */
1768 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
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.
1777 * Create session on the tracer side and add it to app session HT. Note
1778 * that if session exist, it will simply return a pointer to the ust
1781 ua_sess
= create_ust_app_session(usess
, app
);
1782 if (ua_sess
== NULL
) {
1783 /* The malloc() failed. */
1785 } else if (ua_sess
== (void *) -1UL) {
1786 /* The application's socket is not valid. Contiuing */
1790 /* Create channel onto application */
1791 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1792 if (ua_chan
== NULL
) {
1793 /* Major problem here and it's maybe the tracer or malloc() */
1807 * Enable event for a specific session and channel on the tracer.
1809 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1810 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1813 struct lttng_ht_iter iter
, uiter
;
1814 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1815 struct ust_app
*app
;
1816 struct ust_app_session
*ua_sess
;
1817 struct ust_app_channel
*ua_chan
;
1818 struct ust_app_event
*ua_event
;
1820 DBG("UST app enabling event %s for all apps for session id %d",
1821 uevent
->attr
.name
, usess
->id
);
1824 * NOTE: At this point, this function is called only if the session and
1825 * channel passed are already created for all apps. and enabled on the
1831 /* For all 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 is NULL, there is a code flow error */
1844 /* Lookup channel in the ust app session */
1845 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1846 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1847 /* If the channel is not found, there is a code flow error */
1848 assert(ua_chan_node
);
1850 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1852 lttng_ht_lookup(ua_chan
->events
, (void*)uevent
->attr
.name
, &uiter
);
1853 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1854 if (ua_event_node
== NULL
) {
1855 DBG3("UST app enable event %s not found for app PID %d."
1856 "Skipping app", uevent
->attr
.name
, app
->pid
);
1859 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1861 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1873 * For a specific existing UST session and UST channel, creates the event for
1874 * all registered apps.
1876 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1877 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1880 struct lttng_ht_iter iter
, uiter
;
1881 struct lttng_ht_node_str
*ua_chan_node
;
1882 struct ust_app
*app
;
1883 struct ust_app_session
*ua_sess
;
1884 struct ust_app_channel
*ua_chan
;
1886 DBG("UST app creating event %s for all apps for session id %d",
1887 uevent
->attr
.name
, usess
->id
);
1891 /* For all registered applications */
1892 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
1893 if (!app
->compatible
) {
1895 * TODO: In time, we should notice the caller of this error by
1896 * telling him that this is a version error.
1900 ua_sess
= lookup_session_by_app(usess
, app
);
1901 /* If ua_sess is NULL, there is a code flow error */
1904 /* Lookup channel in the ust app session */
1905 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1906 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1907 /* If the channel is not found, there is a code flow error */
1908 assert(ua_chan_node
);
1910 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1912 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1914 if (ret
!= -EEXIST
) {
1915 /* Possible value at this point: -ENOMEM. If so, we stop! */
1918 DBG2("UST app event %s already exist on app PID %d",
1919 uevent
->attr
.name
, app
->pid
);
1930 * Start tracing for a specific UST session and app.
1932 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1935 struct lttng_ht_iter iter
;
1936 struct ust_app_session
*ua_sess
;
1937 struct ust_app_channel
*ua_chan
;
1938 struct ltt_ust_stream
*ustream
;
1941 DBG("Starting tracing for ust app pid %d", app
->pid
);
1945 if (!app
->compatible
) {
1949 ua_sess
= lookup_session_by_app(usess
, app
);
1950 if (ua_sess
== NULL
) {
1951 goto error_rcu_unlock
;
1954 /* Upon restart, we skip the setup, already done */
1955 if (ua_sess
->started
) {
1959 /* Indicate that the session has been started once */
1960 ua_sess
->started
= 1;
1962 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1964 goto error_rcu_unlock
;
1967 /* For each channel */
1968 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1970 /* Create all streams */
1972 /* Create UST stream */
1973 ustream
= zmalloc(sizeof(*ustream
));
1974 if (ustream
== NULL
) {
1975 PERROR("zmalloc ust stream");
1976 goto error_rcu_unlock
;
1979 ret
= ustctl_create_stream(app
->sock
, ua_chan
->obj
,
1982 /* Got all streams */
1985 ustream
->handle
= ustream
->obj
->handle
;
1987 /* Order is important */
1988 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1989 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1990 ua_sess
->path
, ua_chan
->name
,
1991 ua_chan
->streams
.count
++);
1993 PERROR("asprintf UST create stream");
1996 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
2001 switch (app
->bits_per_long
) {
2003 consumerd_fd
= ust_consumerd64_fd
;
2006 consumerd_fd
= ust_consumerd32_fd
;
2010 goto error_rcu_unlock
;
2013 /* Setup UST consumer socket and send fds to it */
2014 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
2016 goto error_rcu_unlock
;
2020 /* This start the UST tracing */
2021 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
2023 ERR("Error starting tracing for app pid: %d", app
->pid
);
2024 goto error_rcu_unlock
;
2027 /* Quiescent wait after starting trace */
2028 ustctl_wait_quiescent(app
->sock
);
2040 * Stop tracing for a specific UST session and app.
2042 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2045 struct lttng_ht_iter iter
;
2046 struct ust_app_session
*ua_sess
;
2047 struct ust_app_channel
*ua_chan
;
2049 DBG("Stopping tracing for ust app pid %d", app
->pid
);
2053 if (!app
->compatible
) {
2057 ua_sess
= lookup_session_by_app(usess
, app
);
2058 if (ua_sess
== NULL
) {
2059 /* Only malloc can failed so something is really wrong */
2060 goto error_rcu_unlock
;
2064 * If started = 0, it means that stop trace has been called for a session
2065 * that was never started. This is a code flow error and should never
2068 assert(ua_sess
->started
== 1);
2070 /* This inhibits UST tracing */
2071 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
2073 ERR("Error stopping tracing for app pid: %d", app
->pid
);
2074 goto error_rcu_unlock
;
2077 /* Quiescent wait after stopping trace */
2078 ustctl_wait_quiescent(app
->sock
);
2080 /* Flushing buffers */
2081 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2083 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_chan
->obj
);
2085 ERR("UST app PID %d channel %s flush failed with ret %d",
2086 app
->pid
, ua_chan
->name
, ret
);
2087 /* Continuing flushing all buffers */
2092 /* Flush all buffers before stopping */
2093 ret
= ustctl_sock_flush_buffer(app
->sock
, ua_sess
->metadata
->obj
);
2095 ERR("UST app PID %d metadata flush failed with ret %d", app
->pid
,
2109 * Destroy a specific UST session in apps.
2111 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2113 struct ust_app_session
*ua_sess
;
2114 struct lttng_ust_object_data obj
;
2115 struct lttng_ht_iter iter
;
2116 struct lttng_ht_node_ulong
*node
;
2119 DBG("Destroy tracing for ust app pid %d", app
->pid
);
2123 if (!app
->compatible
) {
2127 __lookup_session_by_app(usess
, app
, &iter
);
2128 node
= lttng_ht_iter_get_node_ulong(&iter
);
2130 /* Only malloc can failed so something is really wrong */
2131 goto error_rcu_unlock
;
2133 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2134 ret
= lttng_ht_del(app
->sessions
, &iter
);
2136 obj
.handle
= ua_sess
->handle
;
2139 obj
.memory_map_size
= 0;
2140 ustctl_release_object(app
->sock
, &obj
);
2142 delete_ust_app_session(app
->sock
, ua_sess
);
2144 /* Quiescent wait after stopping trace */
2145 ustctl_wait_quiescent(app
->sock
);
2157 * Start tracing for the UST session.
2159 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2162 struct lttng_ht_iter iter
;
2163 struct ust_app
*app
;
2165 DBG("Starting all UST traces");
2169 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2170 ret
= ust_app_start_trace(usess
, app
);
2172 /* Continue to next apps even on error */
2183 * Start tracing for the UST session.
2185 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2188 struct lttng_ht_iter iter
;
2189 struct ust_app
*app
;
2191 DBG("Stopping all UST traces");
2195 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2196 ret
= ust_app_stop_trace(usess
, app
);
2198 /* Continue to next apps even on error */
2209 * Destroy app UST session.
2211 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2214 struct lttng_ht_iter iter
;
2215 struct ust_app
*app
;
2217 DBG("Destroy all UST traces");
2221 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2222 ret
= ust_app_destroy_trace(usess
, app
);
2224 /* Continue to next apps even on error */
2235 * Add channels/events from UST global domain to registered apps at sock.
2237 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2240 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
2241 struct ust_app
*app
;
2242 struct ust_app_session
*ua_sess
;
2243 struct ust_app_channel
*ua_chan
;
2244 struct ust_app_event
*ua_event
;
2245 struct ust_app_ctx
*ua_ctx
;
2247 if (usess
== NULL
) {
2248 ERR("No UST session on global update. Returning");
2252 DBG2("UST app global update for app sock %d for session id %d", sock
,
2257 app
= find_app_by_sock(sock
);
2259 ERR("Failed to update app sock %d", sock
);
2263 if (!app
->compatible
) {
2267 ua_sess
= create_ust_app_session(usess
, app
);
2268 if (ua_sess
== NULL
) {
2273 * We can iterate safely here over all UST app session sicne the create ust
2274 * app session above made a shadow copy of the UST global domain from the
2277 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2279 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2281 /* FIXME: Should we quit here or continue... */
2285 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
2287 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2289 /* FIXME: Should we quit here or continue... */
2295 /* For each events */
2296 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2298 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2300 /* FIXME: Should we quit here or continue... */
2304 /* Add context on events. */
2305 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter_ctx
.iter
,
2306 ua_ctx
, node
.node
) {
2307 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
2309 /* FIXME: Should we quit here or continue... */
2316 if (usess
->start_trace
) {
2317 ret
= ust_app_start_trace(usess
, app
);
2322 DBG2("UST trace started for app pid %d", app
->pid
);
2331 * Add context to a specific channel for global UST domain.
2333 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2334 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2337 struct lttng_ht_node_str
*ua_chan_node
;
2338 struct lttng_ht_iter iter
, uiter
;
2339 struct ust_app_channel
*ua_chan
= NULL
;
2340 struct ust_app_session
*ua_sess
;
2341 struct ust_app
*app
;
2345 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2346 if (!app
->compatible
) {
2348 * TODO: In time, we should notice the caller of this error by
2349 * telling him that this is a version error.
2353 ua_sess
= lookup_session_by_app(usess
, app
);
2354 if (ua_sess
== NULL
) {
2358 /* Lookup channel in the ust app session */
2359 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2360 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2361 if (ua_chan_node
== NULL
) {
2364 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2367 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2378 * Add context to a specific event in a channel for global UST domain.
2380 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2381 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2382 struct ltt_ust_context
*uctx
)
2385 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2386 struct lttng_ht_iter iter
, uiter
;
2387 struct ust_app_session
*ua_sess
;
2388 struct ust_app_event
*ua_event
;
2389 struct ust_app_channel
*ua_chan
= NULL
;
2390 struct ust_app
*app
;
2394 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2395 if (!app
->compatible
) {
2397 * TODO: In time, we should notice the caller of this error by
2398 * telling him that this is a version error.
2402 ua_sess
= lookup_session_by_app(usess
, app
);
2403 if (ua_sess
== NULL
) {
2407 /* Lookup channel in the ust app session */
2408 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2409 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2410 if (ua_chan_node
== NULL
) {
2413 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2416 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2417 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2418 if (ua_event_node
== NULL
) {
2421 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2424 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2435 * Enable event for a channel from a UST session for a specific PID.
2437 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2438 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2441 struct lttng_ht_iter iter
;
2442 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2443 struct ust_app
*app
;
2444 struct ust_app_session
*ua_sess
;
2445 struct ust_app_channel
*ua_chan
;
2446 struct ust_app_event
*ua_event
;
2448 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2452 app
= ust_app_find_by_pid(pid
);
2454 ERR("UST app enable event per PID %d not found", pid
);
2459 if (!app
->compatible
) {
2464 ua_sess
= lookup_session_by_app(usess
, app
);
2465 /* If ua_sess is NULL, there is a code flow error */
2468 /* Lookup channel in the ust app session */
2469 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2470 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2471 /* If the channel is not found, there is a code flow error */
2472 assert(ua_chan_node
);
2474 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2476 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2477 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2478 if (ua_event_node
== NULL
) {
2479 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2484 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2486 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2498 * Disable event for a channel from a UST session for a specific PID.
2500 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2501 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2504 struct lttng_ht_iter iter
;
2505 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2506 struct ust_app
*app
;
2507 struct ust_app_session
*ua_sess
;
2508 struct ust_app_channel
*ua_chan
;
2509 struct ust_app_event
*ua_event
;
2511 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2515 app
= ust_app_find_by_pid(pid
);
2517 ERR("UST app disable event per PID %d not found", pid
);
2522 if (!app
->compatible
) {
2527 ua_sess
= lookup_session_by_app(usess
, app
);
2528 /* If ua_sess is NULL, there is a code flow error */
2531 /* Lookup channel in the ust app session */
2532 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2533 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2534 if (ua_chan_node
== NULL
) {
2535 /* Channel does not exist, skip disabling */
2538 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2540 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2541 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2542 if (ua_event_node
== NULL
) {
2543 /* Event does not exist, skip disabling */
2546 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2548 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2559 * Validate version of UST apps and set the compatible bit.
2561 int ust_app_validate_version(int sock
)
2564 struct ust_app
*app
;
2568 app
= find_app_by_sock(sock
);
2571 ret
= ustctl_tracer_version(sock
, &app
->version
);
2576 /* Validate version */
2577 if (app
->version
.major
> UST_APP_MAJOR_VERSION
) {
2581 DBG2("UST app PID %d is compatible with major version %d "
2582 "(supporting <= %d)", app
->pid
, app
->version
.major
,
2583 UST_APP_MAJOR_VERSION
);
2584 app
->compatible
= 1;
2589 DBG2("UST app PID %d is not compatible with major version %d "
2590 "(supporting <= %d)", app
->pid
, app
->version
.major
,
2591 UST_APP_MAJOR_VERSION
);
2592 app
->compatible
= 0;
2598 * Calibrate registered applications.
2600 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
2603 struct lttng_ht_iter iter
;
2604 struct ust_app
*app
;
2608 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
2609 if (!app
->compatible
) {
2611 * TODO: In time, we should notice the caller of this error by
2612 * telling him that this is a version error.
2617 ret
= ustctl_calibrate(app
->sock
, calibrate
);
2621 /* Means that it's not implemented on the tracer side. */
2625 /* TODO: Report error to user */
2626 DBG2("Calibrate app PID %d returned with error %d",
2633 DBG("UST app global domain calibration finished");