2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <sys/types.h>
30 #include <urcu/compiler.h>
32 #include <lttng-share.h>
35 #include "../common/hashtable.h"
37 #include "ust-consumer.h"
41 * Delete ust context safely. RCU read lock must be held before calling
45 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
48 ustctl_release_object(sock
, ua_ctx
->obj
);
55 * Delete ust app event safely. RCU read lock must be held before calling
59 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
62 struct cds_lfht_iter iter
;
63 struct ust_app_ctx
*ua_ctx
;
65 cds_lfht_for_each_entry(ua_event
->ctx
, &iter
, ua_ctx
, node
) {
66 ret
= hashtable_del(ua_event
->ctx
, &iter
);
68 delete_ust_app_ctx(sock
, ua_ctx
);
70 ret
= hashtable_destroy(ua_event
->ctx
);
73 if (ua_event
->obj
!= NULL
) {
74 ustctl_release_object(sock
, ua_event
->obj
);
81 * Delete ust app stream safely. RCU read lock must be held before calling
85 void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
88 ustctl_release_object(sock
, stream
->obj
);
95 * Delete ust app channel safely. RCU read lock must be held before calling
99 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
102 struct cds_lfht_iter iter
;
103 struct ust_app_event
*ua_event
;
104 struct ust_app_ctx
*ua_ctx
;
105 struct ltt_ust_stream
*stream
, *stmp
;
108 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
109 cds_list_del(&stream
->list
);
110 delete_ust_app_stream(sock
, stream
);
114 cds_lfht_for_each_entry(ua_chan
->ctx
, &iter
, ua_ctx
, node
) {
115 ret
= hashtable_del(ua_chan
->ctx
, &iter
);
117 delete_ust_app_ctx(sock
, ua_ctx
);
119 ret
= hashtable_destroy(ua_chan
->ctx
);
123 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
124 ret
= hashtable_del(ua_chan
->events
, &iter
);
126 delete_ust_app_event(sock
, ua_event
);
128 ret
= hashtable_destroy(ua_chan
->events
);
131 if (ua_chan
->obj
!= NULL
) {
132 ustctl_release_object(sock
, ua_chan
->obj
);
139 * Delete ust app session safely. RCU read lock must be held before calling
143 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
)
146 struct cds_lfht_iter iter
;
147 struct ust_app_channel
*ua_chan
;
149 if (ua_sess
->metadata
) {
150 if (ua_sess
->metadata
->stream_obj
) {
151 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
152 free(ua_sess
->metadata
->stream_obj
);
154 if (ua_sess
->metadata
->obj
) {
155 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
156 free(ua_sess
->metadata
->obj
);
160 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
161 ret
= hashtable_del(ua_sess
->channels
, &iter
);
163 delete_ust_app_channel(sock
, ua_chan
);
165 ret
= hashtable_destroy(ua_sess
->channels
);
168 if (ua_sess
->handle
!= -1) {
169 ustctl_release_handle(sock
, ua_sess
->handle
);
175 * Delete a traceable application structure from the global list. Never call
176 * this function outside of a call_rcu call.
179 void delete_ust_app(struct ust_app
*app
)
182 struct cds_lfht_node
*node
;
183 struct cds_lfht_iter iter
;
184 struct ust_app_session
*ua_sess
;
188 /* Remove from key hash table */
189 node
= hashtable_lookup(ust_app_sock_key_map
,
190 (void *) ((unsigned long) app
->key
.sock
), sizeof(void *), &iter
);
192 /* Not suppose to happen */
193 ERR("UST app key %d not found in key hash table", app
->key
.sock
);
197 ret
= hashtable_del(ust_app_sock_key_map
, &iter
);
199 ERR("UST app unable to delete app sock %d from key hash table",
202 DBG2("UST app pair sock %d key %d deleted",
203 app
->key
.sock
, app
->key
.pid
);
206 /* Socket is already closed at this point */
208 /* Delete ust app sessions info */
209 sock
= app
->key
.sock
;
213 cds_lfht_for_each_entry(app
->sessions
, &iter
, ua_sess
, node
) {
214 ret
= hashtable_del(app
->sessions
, &iter
);
216 delete_ust_app_session(app
->key
.sock
, ua_sess
);
218 ret
= hashtable_destroy(app
->sessions
);
222 * Wait until we have removed the key from the sock hash table
223 * before closing this socket, otherwise an application could
224 * re-use the socket ID and race with the teardown, using the
225 * same hash table entry.
229 DBG2("UST app pid %d deleted", app
->key
.pid
);
236 * URCU intermediate call to delete an UST app.
239 void delete_ust_app_rcu(struct rcu_head
*head
)
241 struct cds_lfht_node
*node
=
242 caa_container_of(head
, struct cds_lfht_node
, head
);
243 struct ust_app
*app
=
244 caa_container_of(node
, struct ust_app
, node
);
250 * Alloc new UST app session.
253 struct ust_app_session
*alloc_ust_app_session(void)
255 struct ust_app_session
*ua_sess
;
257 /* Init most of the default value by allocating and zeroing */
258 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
259 if (ua_sess
== NULL
) {
264 ua_sess
->handle
= -1;
265 ua_sess
->channels
= hashtable_new_str(0);
274 * Alloc new UST app channel.
277 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
278 struct lttng_ust_channel
*attr
)
280 struct ust_app_channel
*ua_chan
;
282 /* Init most of the default value by allocating and zeroing */
283 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
284 if (ua_chan
== NULL
) {
289 /* Setup channel name */
290 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
291 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
293 ua_chan
->enabled
= 1;
294 ua_chan
->handle
= -1;
295 ua_chan
->ctx
= hashtable_new(0);
296 ua_chan
->events
= hashtable_new_str(0);
297 hashtable_node_init(&ua_chan
->node
, (void *) ua_chan
->name
,
298 strlen(ua_chan
->name
));
300 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
302 /* Copy attributes */
304 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
307 DBG3("UST app channel %s allocated", ua_chan
->name
);
316 * Alloc new UST app event.
319 struct ust_app_event
*alloc_ust_app_event(char *name
,
320 struct lttng_ust_event
*attr
)
322 struct ust_app_event
*ua_event
;
324 /* Init most of the default value by allocating and zeroing */
325 ua_event
= zmalloc(sizeof(struct ust_app_event
));
326 if (ua_event
== NULL
) {
331 ua_event
->enabled
= 1;
332 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
333 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
334 ua_event
->ctx
= hashtable_new(0);
335 hashtable_node_init(&ua_event
->node
, (void *) ua_event
->name
,
336 strlen(ua_event
->name
));
338 /* Copy attributes */
340 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
343 DBG3("UST app event %s allocated", ua_event
->name
);
352 * Alloc new UST app context.
355 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
357 struct ust_app_ctx
*ua_ctx
;
359 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
360 if (ua_ctx
== NULL
) {
365 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
368 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
375 * Find an ust_app using the sock and return it. RCU read side lock must be
376 * held before calling this helper function.
379 struct ust_app
*find_app_by_sock(int sock
)
381 struct cds_lfht_node
*node
;
382 struct ust_app_key
*key
;
383 struct cds_lfht_iter iter
;
385 node
= hashtable_lookup(ust_app_sock_key_map
,
386 (void *)((unsigned long) sock
), sizeof(void *), &iter
);
388 DBG2("UST app find by sock %d key not found", sock
);
392 key
= caa_container_of(node
, struct ust_app_key
, node
);
394 node
= hashtable_lookup(ust_app_ht
,
395 (void *)((unsigned long) key
->pid
), sizeof(void *), &iter
);
397 DBG2("UST app find by sock %d not found", sock
);
400 return caa_container_of(node
, struct ust_app
, node
);
407 * Create the channel context on the tracer.
410 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
411 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
415 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
416 ua_chan
->obj
, &ua_ctx
->obj
);
421 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
423 DBG2("UST app context added to channel %s successfully", ua_chan
->name
);
430 * Create the event context on the tracer.
433 int create_ust_event_context(struct ust_app_event
*ua_event
,
434 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
438 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
439 ua_event
->obj
, &ua_ctx
->obj
);
444 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
446 DBG2("UST app context added to event %s successfully", ua_event
->name
);
453 * Disable the specified event on to UST tracer for the UST session.
455 static int disable_ust_event(struct ust_app
*app
,
456 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
460 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
462 ERR("UST app event %s disable failed for app (pid: %d) "
463 "and session handle %d with ret %d",
464 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
468 DBG2("UST app event %s disabled successfully for app (pid: %d)",
469 ua_event
->attr
.name
, app
->key
.pid
);
476 * Disable the specified channel on to UST tracer for the UST session.
478 static int disable_ust_channel(struct ust_app
*app
,
479 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
483 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
485 ERR("UST app channel %s disable failed for app (pid: %d) "
486 "and session handle %d with ret %d",
487 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
491 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
492 ua_chan
->name
, app
->key
.pid
);
499 * Enable the specified channel on to UST tracer for the UST session.
501 static int enable_ust_channel(struct ust_app
*app
,
502 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
506 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
508 ERR("UST app channel %s enable failed for app (pid: %d) "
509 "and session handle %d with ret %d",
510 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
514 ua_chan
->enabled
= 1;
516 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
517 ua_chan
->name
, app
->key
.pid
);
524 * Enable the specified event on to UST tracer for the UST session.
526 static int enable_ust_event(struct ust_app
*app
,
527 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
531 ret
= ustctl_enable(app
->key
.sock
, ua_event
->obj
);
533 ERR("UST app event %s enable failed for app (pid: %d) "
534 "and session handle %d with ret %d",
535 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
539 DBG2("UST app event %s enabled successfully for app (pid: %d)",
540 ua_event
->attr
.name
, app
->key
.pid
);
547 * Open metadata onto the UST tracer for a UST session.
549 static int open_ust_metadata(struct ust_app
*app
,
550 struct ust_app_session
*ua_sess
)
553 struct lttng_ust_channel_attr uattr
;
555 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
556 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
557 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
558 uattr
.switch_timer_interval
=
559 ua_sess
->metadata
->attr
.switch_timer_interval
;
560 uattr
.read_timer_interval
=
561 ua_sess
->metadata
->attr
.read_timer_interval
;
562 uattr
.output
= ua_sess
->metadata
->attr
.output
;
564 /* UST tracer metadata creation */
565 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
566 &ua_sess
->metadata
->obj
);
568 ERR("UST app open metadata failed for app pid:%d",
573 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
580 * Create stream onto the UST tracer for a UST session.
582 static int create_ust_stream(struct ust_app
*app
,
583 struct ust_app_session
*ua_sess
)
587 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
588 &ua_sess
->metadata
->stream_obj
);
590 ERR("UST create metadata stream failed");
599 * Create the specified channel onto the UST tracer for a UST session.
601 static int create_ust_channel(struct ust_app
*app
,
602 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
606 /* TODO: remove cast and use lttng-ust-abi.h */
607 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
608 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
610 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
611 "and session handle %d with ret %d",
612 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
613 ua_sess
->handle
, ret
);
617 ua_chan
->handle
= ua_chan
->obj
->handle
;
618 ua_chan
->attr
.shm_fd
= ua_chan
->obj
->shm_fd
;
619 ua_chan
->attr
.wait_fd
= ua_chan
->obj
->wait_fd
;
620 ua_chan
->attr
.memory_map_size
= ua_chan
->obj
->memory_map_size
;
622 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
623 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
625 /* If channel is not enabled, disable it on the tracer */
626 if (!ua_chan
->enabled
) {
627 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
638 * Create the specified event onto the UST tracer for a UST session.
641 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
642 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
646 /* Create UST event on tracer */
647 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
650 ERR("Error ustctl create event %s for app pid: %d with ret %d",
651 ua_event
->attr
.name
, app
->key
.pid
, ret
);
655 ua_event
->handle
= ua_event
->obj
->handle
;
657 DBG2("UST app event %s created successfully for pid:%d",
658 ua_event
->attr
.name
, app
->key
.pid
);
660 /* If event not enabled, disable it on the tracer */
661 if (!ua_event
->enabled
) {
662 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
673 * Copy data between an UST app event and a LTT event.
675 static void shadow_copy_event(struct ust_app_event
*ua_event
,
676 struct ltt_ust_event
*uevent
)
678 struct cds_lfht_iter iter
;
679 struct ltt_ust_context
*uctx
;
680 struct ust_app_ctx
*ua_ctx
;
682 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
683 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
685 /* Copy event attributes */
686 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
688 cds_lfht_for_each_entry(uevent
->ctx
, &iter
, uctx
, node
) {
689 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
690 if (ua_ctx
== NULL
) {
693 hashtable_node_init(&ua_ctx
->node
,
694 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
695 hashtable_add_unique(ua_event
->ctx
, &ua_ctx
->node
);
700 * Copy data between an UST app channel and a LTT channel.
702 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
703 struct ltt_ust_channel
*uchan
)
705 struct cds_lfht_iter iter
;
706 struct cds_lfht_node
*ua_event_node
;
707 struct ltt_ust_event
*uevent
;
708 struct ltt_ust_context
*uctx
;
709 struct ust_app_event
*ua_event
;
710 struct ust_app_ctx
*ua_ctx
;
712 DBG2("Shadow copy of UST app channel %s", ua_chan
->name
);
714 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
715 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
716 /* Copy event attributes */
717 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
719 cds_lfht_for_each_entry(uchan
->ctx
, &iter
, uctx
, node
) {
720 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
721 if (ua_ctx
== NULL
) {
724 hashtable_node_init(&ua_ctx
->node
,
725 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
726 hashtable_add_unique(ua_chan
->ctx
, &ua_ctx
->node
);
729 /* Copy all events from ltt ust channel to ust app channel */
730 cds_lfht_for_each_entry(uchan
->events
, &iter
, uevent
, node
) {
731 struct cds_lfht_iter uiter
;
733 ua_event_node
= hashtable_lookup(ua_chan
->events
,
734 (void *) uevent
->attr
.name
, strlen(uevent
->attr
.name
),
736 if (ua_event_node
== NULL
) {
737 DBG2("UST event %s not found on shadow copy channel",
739 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
740 if (ua_event
== NULL
) {
743 shadow_copy_event(ua_event
, uevent
);
744 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
748 DBG3("Shadow copy channel done");
752 * Copy data between a UST app session and a regular LTT session.
754 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
755 struct ltt_ust_session
*usess
,
758 struct cds_lfht_node
*ua_chan_node
;
759 struct cds_lfht_iter iter
;
760 struct ltt_ust_channel
*uchan
;
761 struct ust_app_channel
*ua_chan
;
767 /* Get date and time for unique app path */
769 timeinfo
= localtime(&rawtime
);
770 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
772 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
774 ua_sess
->id
= usess
->id
;
775 ua_sess
->uid
= usess
->uid
;
776 ua_sess
->gid
= usess
->gid
;
778 ret
= snprintf(ua_sess
->path
, PATH_MAX
,
780 usess
->pathname
, app
->name
, app
->key
.pid
,
783 PERROR("asprintf UST shadow copy session");
784 /* TODO: We cannot return an error from here.. */
788 /* TODO: support all UST domain */
790 /* Iterate over all channels in global domain. */
791 cds_lfht_for_each_entry(usess
->domain_global
.channels
, &iter
,
793 struct cds_lfht_iter uiter
;
795 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
796 (void *)uchan
->name
, strlen(uchan
->name
),
798 if (ua_chan_node
!= NULL
) {
802 DBG2("Channel %s not found on shadow session copy, creating it",
804 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
805 if (ua_chan
== NULL
) {
806 /* malloc failed... continuing */
810 shadow_copy_channel(ua_chan
, uchan
);
811 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
816 * Lookup sesison wrapper.
819 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
820 struct ust_app
*app
, struct cds_lfht_iter
*iter
)
822 /* Get right UST app session from app */
823 (void) hashtable_lookup(app
->sessions
,
824 (void *) ((unsigned long) usess
->id
), sizeof(void *),
829 * Return ust app session from the app session hashtable using the UST session
832 static struct ust_app_session
*lookup_session_by_app(
833 struct ltt_ust_session
*usess
, struct ust_app
*app
)
835 struct cds_lfht_iter iter
;
836 struct cds_lfht_node
*node
;
838 __lookup_session_by_app(usess
, app
, &iter
);
839 node
= hashtable_iter_get_node(&iter
);
844 return caa_container_of(node
, struct ust_app_session
, node
);
851 * Create a UST session onto the tracer of app and add it the session
854 * Return ust app session or NULL on error.
856 static struct ust_app_session
*create_ust_app_session(
857 struct ltt_ust_session
*usess
, struct ust_app
*app
)
860 struct ust_app_session
*ua_sess
;
862 ua_sess
= lookup_session_by_app(usess
, app
);
863 if (ua_sess
== NULL
) {
864 DBG2("UST app pid: %d session id %d not found, creating it",
865 app
->key
.pid
, usess
->id
);
866 ua_sess
= alloc_ust_app_session();
867 if (ua_sess
== NULL
) {
868 /* Only malloc can failed so something is really wrong */
871 shadow_copy_session(ua_sess
, usess
, app
);
874 if (ua_sess
->handle
== -1) {
875 ret
= ustctl_create_session(app
->key
.sock
);
877 ERR("Error creating session for app pid %d, sock %d",
878 app
->key
.pid
, app
->key
.sock
);
879 /* TODO: free() ua_sess */
883 DBG2("UST app ustctl create session handle %d", ret
);
884 ua_sess
->handle
= ret
;
886 /* Add ust app session to app's HT */
887 hashtable_node_init(&ua_sess
->node
,
888 (void *)((unsigned long) ua_sess
->id
), sizeof(void *));
889 hashtable_add_unique(app
->sessions
, &ua_sess
->node
);
891 DBG2("UST app session created successfully with handle %d", ret
);
901 * Create a context for the channel on the tracer.
904 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
905 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
909 struct cds_lfht_iter iter
;
910 struct cds_lfht_node
*node
;
911 struct ust_app_ctx
*ua_ctx
;
913 DBG2("UST app adding context to channel %s", ua_chan
->name
);
915 node
= hashtable_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
),
916 sizeof(void *), &iter
);
922 ua_ctx
= alloc_ust_app_ctx(uctx
);
923 if (ua_ctx
== NULL
) {
929 hashtable_node_init(&ua_ctx
->node
,
930 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
931 hashtable_add_unique(ua_chan
->ctx
, &ua_ctx
->node
);
933 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
943 * Create an UST context and enable it for the event on the tracer.
946 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
947 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
951 struct cds_lfht_iter iter
;
952 struct cds_lfht_node
*node
;
953 struct ust_app_ctx
*ua_ctx
;
955 DBG2("UST app adding context to event %s", ua_event
->name
);
957 node
= hashtable_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
),
958 sizeof(void *), &iter
);
964 ua_ctx
= alloc_ust_app_ctx(uctx
);
965 if (ua_ctx
== NULL
) {
971 hashtable_node_init(&ua_ctx
->node
,
972 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
973 hashtable_add_unique(ua_event
->ctx
, &ua_ctx
->node
);
975 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
985 * Enable on the tracer side a ust app event for the session and channel.
988 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
989 struct ust_app_event
*ua_event
, struct ust_app
*app
)
993 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
998 ua_event
->enabled
= 1;
1005 * Disable on the tracer side a ust app event for the session and channel.
1007 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1008 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1012 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1017 ua_event
->enabled
= 0;
1024 * Lookup ust app channel for session and disable it on the tracer side.
1027 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1028 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1032 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1037 ua_chan
->enabled
= 0;
1044 * Lookup ust app channel for session and enable it on the tracer side.
1046 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1047 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1050 struct cds_lfht_iter iter
;
1051 struct cds_lfht_node
*ua_chan_node
;
1052 struct ust_app_channel
*ua_chan
;
1054 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1055 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1056 if (ua_chan_node
== NULL
) {
1057 DBG2("Unable to find channel %s in ust session id %u",
1058 uchan
->name
, ua_sess
->id
);
1062 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1064 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1074 * Create UST app channel and create it on the tracer.
1076 static struct ust_app_channel
*create_ust_app_channel(
1077 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1078 struct ust_app
*app
)
1081 struct cds_lfht_iter iter
;
1082 struct cds_lfht_node
*ua_chan_node
;
1083 struct ust_app_channel
*ua_chan
;
1085 /* Lookup channel in the ust app session */
1086 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1087 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1088 if (ua_chan_node
== NULL
) {
1089 DBG2("Unable to find channel %s in ust session id %u",
1090 uchan
->name
, ua_sess
->id
);
1091 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1092 if (ua_chan
== NULL
) {
1095 shadow_copy_channel(ua_chan
, uchan
);
1097 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
1099 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1102 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1114 * Create UST app event and create it on the tracer side.
1117 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1118 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1119 struct ust_app
*app
)
1122 struct cds_lfht_iter iter
;
1123 struct cds_lfht_node
*ua_event_node
;
1124 struct ust_app_event
*ua_event
;
1126 /* Get event node */
1127 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1128 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
1129 if (ua_event_node
!= NULL
) {
1130 ERR("UST app event %s already exist. Stopping creation.",
1135 /* Does not exist so create one */
1136 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1137 if (ua_event
== NULL
) {
1138 /* Only malloc can failed so something is really wrong */
1142 shadow_copy_event(ua_event
, uevent
);
1144 /* Create it on the tracer side */
1145 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1148 delete_ust_app_event(app
->key
.sock
, ua_event
);
1153 ua_event
->enabled
= 1;
1155 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
1157 DBG2("UST app create event %s for PID %d completed",
1158 ua_event
->name
, app
->key
.pid
);
1166 * Create UST metadata and open it on the tracer side.
1168 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1169 char *pathname
, struct ust_app
*app
)
1173 if (ua_sess
->metadata
== NULL
) {
1174 /* Allocate UST metadata */
1175 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1176 if (ua_sess
->metadata
== NULL
) {
1177 ERR("UST app session %d creating metadata failed",
1182 ret
= open_ust_metadata(app
, ua_sess
);
1187 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
1190 /* Open UST metadata stream */
1191 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1192 ret
= create_ust_stream(app
, ua_sess
);
1197 ret
= mkdir_run_as(ua_sess
->path
, S_IRWXU
| S_IRWXG
,
1198 ua_sess
->uid
, ua_sess
->gid
);
1200 PERROR("mkdir UST metadata");
1204 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1205 "%s/metadata", ua_sess
->path
);
1207 PERROR("asprintf UST create stream");
1211 DBG2("UST metadata stream object created for app pid %d",
1214 ERR("Attempting to create stream without metadata opened");
1225 * Return pointer to traceable apps list.
1227 struct cds_lfht
*ust_app_get_ht(void)
1233 * Return ust app pointer or NULL if not found.
1235 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1237 struct cds_lfht_node
*node
;
1238 struct cds_lfht_iter iter
;
1241 node
= hashtable_lookup(ust_app_ht
,
1242 (void *)((unsigned long) pid
), sizeof(void *), &iter
);
1244 DBG2("UST app no found with pid %d", pid
);
1249 DBG2("Found UST app by pid %d", pid
);
1251 return caa_container_of(node
, struct ust_app
, node
);
1259 * Using pid and uid (of the app), allocate a new ust_app struct and
1260 * add it to the global traceable app list.
1262 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1263 * bitness is not supported.
1265 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1267 struct ust_app
*lta
;
1269 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1270 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1271 ERR("Registration failed: application \"%s\" (pid: %d) has "
1272 "%d-bit long, but no consumerd for this long size is available.\n",
1273 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1277 lta
= zmalloc(sizeof(struct ust_app
));
1283 lta
->ppid
= msg
->ppid
;
1284 lta
->uid
= msg
->uid
;
1285 lta
->gid
= msg
->gid
;
1286 lta
->bits_per_long
= msg
->bits_per_long
;
1287 lta
->v_major
= msg
->major
;
1288 lta
->v_minor
= msg
->minor
;
1289 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1290 lta
->name
[16] = '\0';
1291 lta
->sessions
= hashtable_new(0);
1294 lta
->key
.pid
= msg
->pid
;
1295 hashtable_node_init(<a
->node
, (void *)((unsigned long)lta
->key
.pid
),
1297 lta
->key
.sock
= sock
;
1298 hashtable_node_init(<a
->key
.node
, (void *)((unsigned long)lta
->key
.sock
),
1302 hashtable_add_unique(ust_app_sock_key_map
, <a
->key
.node
);
1303 hashtable_add_unique(ust_app_ht
, <a
->node
);
1306 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1307 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1308 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1314 * Unregister app by removing it from the global traceable app list and freeing
1317 * The socket is already closed at this point so no close to sock.
1319 void ust_app_unregister(int sock
)
1321 struct ust_app
*lta
;
1322 struct cds_lfht_node
*node
;
1323 struct cds_lfht_iter iter
;
1327 lta
= find_app_by_sock(sock
);
1329 ERR("Unregister app sock %d not found!", sock
);
1333 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1335 /* Get the node reference for a call_rcu */
1336 node
= hashtable_lookup(ust_app_ht
,
1337 (void *)((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
1339 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1343 ret
= hashtable_del(ust_app_ht
, &iter
);
1345 call_rcu(&node
->head
, delete_ust_app_rcu
);
1352 * Return traceable_app_count
1354 unsigned long ust_app_list_count(void)
1356 unsigned long count
;
1359 count
= hashtable_get_count(ust_app_ht
);
1366 * Fill events array with all events name of all registered apps.
1368 int ust_app_list_events(struct lttng_event
**events
)
1371 size_t nbmem
, count
= 0;
1372 struct cds_lfht_iter iter
;
1373 struct ust_app
*app
;
1374 struct lttng_event
*tmp
;
1376 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1377 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1379 PERROR("zmalloc ust app events");
1386 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1387 struct lttng_ust_tracepoint_iter uiter
;
1389 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1391 ERR("UST app list events getting handle failed for app pid %d",
1396 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1397 &uiter
)) != -ENOENT
) {
1398 if (count
>= nbmem
) {
1399 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1402 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1404 PERROR("realloc ust app events");
1409 memcpy(tmp
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1410 memcpy(tmp
[count
].loglevel
, uiter
.loglevel
, LTTNG_UST_SYM_NAME_LEN
);
1411 tmp
[count
].loglevel_value
= uiter
.loglevel_value
;
1412 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1413 tmp
[count
].pid
= app
->key
.pid
;
1414 tmp
[count
].enabled
= -1;
1422 DBG2("UST app list events done (%zu events)", count
);
1431 * Free and clean all traceable apps of the global list.
1433 void ust_app_clean_list(void)
1436 struct cds_lfht_iter iter
;
1437 struct ust_app
*app
;
1439 DBG2("UST app cleaning registered apps hash table");
1443 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1444 ret
= hashtable_del(ust_app_ht
, &iter
);
1446 call_rcu(&iter
.node
->head
, delete_ust_app_rcu
);
1449 hashtable_destroy(ust_app_ht
);
1450 hashtable_destroy(ust_app_sock_key_map
);
1456 * Init UST app hash table.
1458 void ust_app_ht_alloc(void)
1460 ust_app_ht
= hashtable_new(0);
1461 ust_app_sock_key_map
= hashtable_new(0);
1465 * For a specific UST session, disable the channel for all registered apps.
1467 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1468 struct ltt_ust_channel
*uchan
)
1471 struct cds_lfht_iter iter
;
1472 struct cds_lfht_node
*ua_chan_node
;
1473 struct ust_app
*app
;
1474 struct ust_app_session
*ua_sess
;
1475 struct ust_app_channel
*ua_chan
;
1477 if (usess
== NULL
|| uchan
== NULL
) {
1478 ERR("Disabling UST global channel with NULL values");
1483 DBG2("UST app disabling channel %s from global domain for session id %d",
1484 uchan
->name
, usess
->id
);
1488 /* For every registered applications */
1489 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1490 struct cds_lfht_iter uiter
;
1492 ua_sess
= lookup_session_by_app(usess
, app
);
1493 if (ua_sess
== NULL
) {
1498 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1499 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
1500 /* If the session if found for the app, the channel must be there */
1501 assert(ua_chan_node
);
1503 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1504 /* The channel must not be already disabled */
1505 assert(ua_chan
->enabled
== 1);
1507 /* Disable channel onto application */
1508 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1510 /* XXX: We might want to report this error at some point... */
1522 * For a specific UST session, enable the channel for all registered apps.
1524 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1525 struct ltt_ust_channel
*uchan
)
1528 struct cds_lfht_iter iter
;
1529 struct ust_app
*app
;
1530 struct ust_app_session
*ua_sess
;
1532 if (usess
== NULL
|| uchan
== NULL
) {
1533 ERR("Adding UST global channel to NULL values");
1538 DBG2("UST app enabling channel %s to global domain for session id %d",
1539 uchan
->name
, usess
->id
);
1543 /* For every registered applications */
1544 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1545 ua_sess
= lookup_session_by_app(usess
, app
);
1546 if (ua_sess
== NULL
) {
1550 /* Enable channel onto application */
1551 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1553 /* XXX: We might want to report this error at some point... */
1565 * Disable an event in a channel and for a specific session.
1567 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1568 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1571 struct cds_lfht_iter iter
;
1572 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
1573 struct ust_app
*app
;
1574 struct ust_app_session
*ua_sess
;
1575 struct ust_app_channel
*ua_chan
;
1576 struct ust_app_event
*ua_event
;
1578 DBG("UST app disabling event %s for all apps in channel "
1579 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
1583 /* For all registered applications */
1584 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1585 struct cds_lfht_iter uiter
;
1587 ua_sess
= lookup_session_by_app(usess
, app
);
1588 if (ua_sess
== NULL
) {
1593 /* Lookup channel in the ust app session */
1594 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1595 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
1596 if (ua_chan_node
== NULL
) {
1597 DBG2("Channel %s not found in session id %d for app pid %d."
1598 "Skipping", uchan
->name
, usess
->id
, app
->key
.pid
);
1601 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1603 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1604 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
1605 if (ua_event_node
== NULL
) {
1606 DBG2("Event %s not found in channel %s for app pid %d."
1607 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1610 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1612 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1614 /* XXX: Report error someday... */
1625 * For a specific UST session and UST channel, the event for all
1628 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1629 struct ltt_ust_channel
*uchan
)
1632 struct cds_lfht_iter iter
;
1633 struct cds_lfht_node
*ua_chan_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 all event for all apps in channel "
1640 "%s for session id %d", uchan
->name
, usess
->id
);
1644 /* For all registered applications */
1645 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1646 struct cds_lfht_iter uiter
;
1648 ua_sess
= lookup_session_by_app(usess
, app
);
1649 /* If ua_sess is NULL, there is a code flow error */
1652 /* Lookup channel in the ust app session */
1653 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1654 strlen(uchan
->name
), &uiter
);
1655 /* If the channel is not found, there is a code flow error */
1656 assert(ua_chan_node
);
1658 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1660 /* Disable each events of channel */
1661 cds_lfht_for_each_entry(ua_chan
->events
, &uiter
, ua_event
, node
) {
1662 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1664 /* XXX: Report error someday... */
1676 * For a specific UST session, create the channel for all registered apps.
1678 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1679 struct ltt_ust_channel
*uchan
)
1682 struct cds_lfht_iter iter
;
1683 struct ust_app
*app
;
1684 struct ust_app_session
*ua_sess
;
1685 struct ust_app_channel
*ua_chan
;
1687 if (usess
== NULL
|| uchan
== NULL
) {
1688 ERR("Adding UST global channel to NULL values");
1693 DBG2("UST app adding channel %s to global domain for session id %d",
1694 uchan
->name
, usess
->id
);
1698 /* For every registered applications */
1699 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1701 * Create session on the tracer side and add it to app session HT. Note
1702 * that if session exist, it will simply return a pointer to the ust
1705 ua_sess
= create_ust_app_session(usess
, app
);
1706 if (ua_sess
== NULL
) {
1710 /* Create channel onto application */
1711 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1712 if (ua_chan
== NULL
) {
1724 * Enable event for a specific session and channel on the tracer.
1726 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1727 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1730 struct cds_lfht_iter iter
;
1731 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
1732 struct ust_app
*app
;
1733 struct ust_app_session
*ua_sess
;
1734 struct ust_app_channel
*ua_chan
;
1735 struct ust_app_event
*ua_event
;
1737 DBG("UST app enabling event %s for all apps for session id %d",
1738 uevent
->attr
.name
, usess
->id
);
1741 * NOTE: At this point, this function is called only if the session and
1742 * channel passed are already created for all apps. and enabled on the
1748 /* For all registered applications */
1749 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1750 struct cds_lfht_iter uiter
;
1752 ua_sess
= lookup_session_by_app(usess
, app
);
1753 /* If ua_sess is NULL, there is a code flow error */
1756 /* Lookup channel in the ust app session */
1757 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1758 strlen(uchan
->name
), &uiter
);
1759 /* If the channel is not found, there is a code flow error */
1760 assert(ua_chan_node
);
1762 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1764 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1765 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
1766 if (ua_event_node
== NULL
) {
1767 DBG3("UST app enable event %s not found for app PID %d."
1768 "Skipping app", uevent
->attr
.name
, app
->key
.pid
);
1771 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1773 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1785 * For a specific existing UST session and UST channel, creates the event for
1786 * all registered apps.
1788 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1789 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1792 struct cds_lfht_iter iter
;
1793 struct cds_lfht_node
*ua_chan_node
;
1794 struct ust_app
*app
;
1795 struct ust_app_session
*ua_sess
;
1796 struct ust_app_channel
*ua_chan
;
1798 DBG("UST app creating event %s for all apps for session id %d",
1799 uevent
->attr
.name
, usess
->id
);
1802 * NOTE: At this point, this function is called only if the session and
1803 * channel passed are already created for all apps. and enabled on the
1809 /* For all registered applications */
1810 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1811 struct cds_lfht_iter uiter
;
1813 ua_sess
= lookup_session_by_app(usess
, app
);
1814 /* If ua_sess is NULL, there is a code flow error */
1817 /* Lookup channel in the ust app session */
1818 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1819 strlen(uchan
->name
), &uiter
);
1820 /* If the channel is not found, there is a code flow error */
1821 assert(ua_chan_node
);
1823 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1825 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1837 * Start tracing for a specific UST session and app.
1839 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1842 struct cds_lfht_iter iter
;
1843 struct ust_app_session
*ua_sess
;
1844 struct ust_app_channel
*ua_chan
;
1845 struct ltt_ust_stream
*ustream
;
1848 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1852 ua_sess
= lookup_session_by_app(usess
, app
);
1853 if (ua_sess
== NULL
) {
1854 goto error_rcu_unlock
;
1857 /* Upon restart, we skip the setup, already done */
1858 if (ua_sess
->started
) {
1862 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1864 goto error_rcu_unlock
;
1867 /* For each channel */
1868 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1869 /* Create all streams */
1871 /* Create UST stream */
1872 ustream
= zmalloc(sizeof(*ustream
));
1873 if (ustream
== NULL
) {
1874 PERROR("zmalloc ust stream");
1875 goto error_rcu_unlock
;
1878 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1881 /* Got all streams */
1884 ustream
->handle
= ustream
->obj
->handle
;
1886 /* Order is important */
1887 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1888 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1889 ua_sess
->path
, ua_chan
->name
,
1890 ua_chan
->streams
.count
++);
1892 PERROR("asprintf UST create stream");
1895 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1900 switch (app
->bits_per_long
) {
1902 consumerd_fd
= ust_consumerd64_fd
;
1905 consumerd_fd
= ust_consumerd32_fd
;
1909 goto error_rcu_unlock
;
1912 /* Setup UST consumer socket and send fds to it */
1913 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1915 goto error_rcu_unlock
;
1917 ua_sess
->started
= 1;
1920 /* This start the UST tracing */
1921 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1923 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1924 goto error_rcu_unlock
;
1929 /* Quiescent wait after starting trace */
1930 ustctl_wait_quiescent(app
->key
.sock
);
1940 * Stop tracing for a specific UST session and app.
1942 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1945 struct cds_lfht_iter iter
;
1946 struct ust_app_session
*ua_sess
;
1947 struct ust_app_channel
*ua_chan
;
1949 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1953 ua_sess
= lookup_session_by_app(usess
, app
);
1954 if (ua_sess
== NULL
) {
1955 /* Only malloc can failed so something is really wrong */
1956 goto error_rcu_unlock
;
1959 /* This inhibits UST tracing */
1960 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1962 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1963 goto error_rcu_unlock
;
1966 /* Quiescent wait after stopping trace */
1967 ustctl_wait_quiescent(app
->key
.sock
);
1969 /* Flushing buffers */
1970 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1971 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_chan
->obj
);
1973 ERR("UST app PID %d channel %s flush failed",
1974 app
->key
.pid
, ua_chan
->name
);
1975 ERR("Ended with ret %d", ret
);
1976 /* Continuing flushing all buffers */
1981 /* Flush all buffers before stopping */
1982 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_sess
->metadata
->obj
);
1984 ERR("UST app PID %d metadata flush failed", app
->key
.pid
);
1985 ERR("Ended with ret %d", ret
);
1998 * Destroy a specific UST session in apps.
2000 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2002 struct ust_app_session
*ua_sess
;
2003 struct lttng_ust_object_data obj
;
2004 struct cds_lfht_iter iter
;
2005 struct cds_lfht_node
*node
;
2008 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
2012 __lookup_session_by_app(usess
, app
, &iter
);
2013 node
= hashtable_iter_get_node(&iter
);
2015 /* Only malloc can failed so something is really wrong */
2016 goto error_rcu_unlock
;
2018 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2019 ret
= hashtable_del(app
->sessions
, &iter
);
2021 delete_ust_app_session(app
->key
.sock
, ua_sess
);
2022 obj
.handle
= ua_sess
->handle
;
2025 obj
.memory_map_size
= 0;
2026 ustctl_release_object(app
->key
.sock
, &obj
);
2030 /* Quiescent wait after stopping trace */
2031 ustctl_wait_quiescent(app
->key
.sock
);
2041 * Start tracing for the UST session.
2043 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2046 struct cds_lfht_iter iter
;
2047 struct ust_app
*app
;
2049 DBG("Starting all UST traces");
2053 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2054 ret
= ust_app_start_trace(usess
, app
);
2056 /* Continue to next apps even on error */
2067 * Start tracing for the UST session.
2069 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2072 struct cds_lfht_iter iter
;
2073 struct ust_app
*app
;
2075 DBG("Stopping all UST traces");
2079 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2080 ret
= ust_app_stop_trace(usess
, app
);
2082 /* Continue to next apps even on error */
2093 * Destroy app UST session.
2095 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2098 struct cds_lfht_iter iter
;
2099 struct ust_app
*app
;
2101 DBG("Destroy all UST traces");
2105 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2106 ret
= ust_app_destroy_trace(usess
, app
);
2108 /* Continue to next apps even on error */
2119 * Add channels/events from UST global domain to registered apps at sock.
2121 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2124 struct cds_lfht_iter iter
;
2125 struct ust_app
*app
;
2126 struct ust_app_session
*ua_sess
;
2127 struct ust_app_channel
*ua_chan
;
2128 struct ust_app_event
*ua_event
;
2130 if (usess
== NULL
) {
2131 ERR("No UST session on global update. Returning");
2135 DBG2("UST app global update for app sock %d for session id %d", sock
,
2140 app
= find_app_by_sock(sock
);
2142 ERR("Failed to update app sock %d", sock
);
2146 ua_sess
= create_ust_app_session(usess
, app
);
2147 if (ua_sess
== NULL
) {
2152 * We can iterate safely here over all UST app session sicne the create ust
2153 * app session above made a shadow copy of the UST global domain from the
2156 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
2157 struct cds_lfht_iter uiter
;
2159 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2161 /* FIXME: Should we quit here or continue... */
2165 /* For each events */
2166 cds_lfht_for_each_entry(ua_chan
->events
, &uiter
, ua_event
, node
) {
2167 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2169 /* FIXME: Should we quit here or continue... */
2175 if (usess
->start_trace
) {
2176 ret
= ust_app_start_trace(usess
, app
);
2181 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2190 * Add context to a specific channel for global UST domain.
2192 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2193 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2196 struct cds_lfht_node
*ua_chan_node
;
2197 struct cds_lfht_iter iter
;
2198 struct ust_app_channel
*ua_chan
= NULL
;
2199 struct ust_app_session
*ua_sess
;
2200 struct ust_app
*app
;
2204 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2205 struct cds_lfht_iter uiter
;
2207 ua_sess
= lookup_session_by_app(usess
, app
);
2208 if (ua_sess
== NULL
) {
2212 /* Lookup channel in the ust app session */
2213 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
2214 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
2215 if (ua_chan_node
== NULL
) {
2218 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2221 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2232 * Add context to a specific event in a channel for global UST domain.
2234 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2235 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2236 struct ltt_ust_context
*uctx
)
2239 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2240 struct cds_lfht_iter iter
;
2241 struct ust_app_session
*ua_sess
;
2242 struct ust_app_event
*ua_event
;
2243 struct ust_app_channel
*ua_chan
= NULL
;
2244 struct ust_app
*app
;
2248 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2249 struct cds_lfht_iter uiter
;
2251 ua_sess
= lookup_session_by_app(usess
, app
);
2252 if (ua_sess
== NULL
) {
2256 /* Lookup channel in the ust app session */
2257 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
2258 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
2259 if (ua_chan_node
== NULL
) {
2262 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2265 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2266 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
2267 if (ua_event_node
== NULL
) {
2270 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2273 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2284 * Enable event for a channel from a UST session for a specific PID.
2286 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2287 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2290 struct cds_lfht_iter iter
;
2291 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2292 struct ust_app
*app
;
2293 struct ust_app_session
*ua_sess
;
2294 struct ust_app_channel
*ua_chan
;
2295 struct ust_app_event
*ua_event
;
2297 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2301 app
= ust_app_find_by_pid(pid
);
2303 ERR("UST app enable event per PID %d not found", pid
);
2308 ua_sess
= lookup_session_by_app(usess
, app
);
2309 /* If ua_sess is NULL, there is a code flow error */
2312 /* Lookup channel in the ust app session */
2313 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
2314 strlen(uchan
->name
), &iter
);
2315 /* If the channel is not found, there is a code flow error */
2316 assert(ua_chan_node
);
2318 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2320 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2321 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
2322 if (ua_event_node
== NULL
) {
2323 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2328 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2330 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2342 * Disable event for a channel from a UST session for a specific PID.
2344 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2345 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2348 struct cds_lfht_iter iter
;
2349 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2350 struct ust_app
*app
;
2351 struct ust_app_session
*ua_sess
;
2352 struct ust_app_channel
*ua_chan
;
2353 struct ust_app_event
*ua_event
;
2355 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2359 app
= ust_app_find_by_pid(pid
);
2361 ERR("UST app disable event per PID %d not found", pid
);
2366 ua_sess
= lookup_session_by_app(usess
, app
);
2367 /* If ua_sess is NULL, there is a code flow error */
2370 /* Lookup channel in the ust app session */
2371 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
2372 strlen(uchan
->name
), &iter
);
2373 if (ua_chan_node
== NULL
) {
2374 /* Channel does not exist, skip disabling */
2377 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2379 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2380 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
2381 if (ua_event_node
== NULL
) {
2382 /* Event does not exist, skip disabling */
2385 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2387 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);