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>
29 #include <urcu/compiler.h>
31 #include <lttng-share.h>
33 #include "hashtable.h"
35 #include "ust-consumer.h"
40 * Delete ust context safely. RCU read lock must be held before calling
44 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
47 ustctl_release_object(sock
, ua_ctx
->obj
);
54 * Delete ust app event safely. RCU read lock must be held before calling
58 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
61 struct cds_lfht_iter iter
;
62 struct ust_app_ctx
*ua_ctx
;
64 cds_lfht_for_each_entry(ua_event
->ctx
, &iter
, ua_ctx
, node
) {
65 ret
= hashtable_del(ua_event
->ctx
, &iter
);
67 delete_ust_app_ctx(sock
, ua_ctx
);
69 ret
= hashtable_destroy(ua_event
->ctx
);
72 if (ua_event
->obj
!= NULL
) {
73 ustctl_release_object(sock
, ua_event
->obj
);
80 * Delete ust app stream safely. RCU read lock must be held before calling
84 void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
87 ustctl_release_object(sock
, stream
->obj
);
94 * Delete ust app channel safely. RCU read lock must be held before calling
98 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
101 struct cds_lfht_iter iter
;
102 struct ust_app_event
*ua_event
;
103 struct ust_app_ctx
*ua_ctx
;
104 struct ltt_ust_stream
*stream
, *stmp
;
107 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
108 cds_list_del(&stream
->list
);
109 delete_ust_app_stream(sock
, stream
);
113 cds_lfht_for_each_entry(ua_chan
->ctx
, &iter
, ua_ctx
, node
) {
114 ret
= hashtable_del(ua_chan
->ctx
, &iter
);
116 delete_ust_app_ctx(sock
, ua_ctx
);
118 ret
= hashtable_destroy(ua_chan
->ctx
);
122 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
123 ret
= hashtable_del(ua_chan
->events
, &iter
);
125 delete_ust_app_event(sock
, ua_event
);
127 ret
= hashtable_destroy(ua_chan
->events
);
130 if (ua_chan
->obj
!= NULL
) {
131 ustctl_release_object(sock
, ua_chan
->obj
);
138 * Delete ust app session safely. RCU read lock must be held before calling
142 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
)
145 struct cds_lfht_iter iter
;
146 struct ust_app_channel
*ua_chan
;
148 if (ua_sess
->metadata
) {
149 if (ua_sess
->metadata
->stream_obj
) {
150 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
151 free(ua_sess
->metadata
->stream_obj
);
153 if (ua_sess
->metadata
->obj
) {
154 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
155 free(ua_sess
->metadata
->obj
);
159 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
160 ret
= hashtable_del(ua_sess
->channels
, &iter
);
162 delete_ust_app_channel(sock
, ua_chan
);
164 ret
= hashtable_destroy(ua_sess
->channels
);
167 if (ua_sess
->handle
!= -1) {
168 ustctl_release_handle(sock
, ua_sess
->handle
);
174 * Delete a traceable application structure from the global list. Never call
175 * this function outside of a call_rcu call.
178 void delete_ust_app(struct ust_app
*app
)
181 struct cds_lfht_node
*node
;
182 struct cds_lfht_iter iter
;
183 struct ust_app_session
*ua_sess
;
187 /* Remove from key hash table */
188 node
= hashtable_lookup(ust_app_sock_key_map
,
189 (void *) ((unsigned long) app
->key
.sock
), sizeof(void *), &iter
);
191 /* Not suppose to happen */
192 ERR("UST app key %d not found in key hash table", app
->key
.sock
);
196 ret
= hashtable_del(ust_app_sock_key_map
, &iter
);
198 ERR("UST app unable to delete app sock %d from key hash table",
201 DBG2("UST app pair sock %d key %d deleted",
202 app
->key
.sock
, app
->key
.pid
);
205 /* Socket is already closed at this point */
207 /* Delete ust app sessions info */
208 sock
= app
->key
.sock
;
212 cds_lfht_for_each_entry(app
->sessions
, &iter
, ua_sess
, node
) {
213 ret
= hashtable_del(app
->sessions
, &iter
);
215 delete_ust_app_session(app
->key
.sock
, ua_sess
);
217 ret
= hashtable_destroy(app
->sessions
);
221 * Wait until we have removed the key from the sock hash table
222 * before closing this socket, otherwise an application could
223 * re-use the socket ID and race with the teardown, using the
224 * same hash table entry.
228 DBG2("UST app pid %d deleted", app
->key
.pid
);
235 * URCU intermediate call to delete an UST app.
238 void delete_ust_app_rcu(struct rcu_head
*head
)
240 struct cds_lfht_node
*node
=
241 caa_container_of(head
, struct cds_lfht_node
, head
);
242 struct ust_app
*app
=
243 caa_container_of(node
, struct ust_app
, node
);
249 * Alloc new UST app session.
252 struct ust_app_session
*alloc_ust_app_session(void)
254 struct ust_app_session
*ua_sess
;
256 /* Init most of the default value by allocating and zeroing */
257 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
258 if (ua_sess
== NULL
) {
263 ua_sess
->handle
= -1;
264 ua_sess
->channels
= hashtable_new_str(0);
273 * Alloc new UST app channel.
276 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
277 struct lttng_ust_channel
*attr
)
279 struct ust_app_channel
*ua_chan
;
281 /* Init most of the default value by allocating and zeroing */
282 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
283 if (ua_chan
== NULL
) {
288 /* Setup channel name */
289 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
290 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
292 ua_chan
->enabled
= 1;
293 ua_chan
->handle
= -1;
294 ua_chan
->ctx
= hashtable_new(0);
295 ua_chan
->events
= hashtable_new_str(0);
296 hashtable_node_init(&ua_chan
->node
, (void *) ua_chan
->name
,
297 strlen(ua_chan
->name
));
299 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
301 /* Copy attributes */
303 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
306 DBG3("UST app channel %s allocated", ua_chan
->name
);
315 * Alloc new UST app event.
318 struct ust_app_event
*alloc_ust_app_event(char *name
,
319 struct lttng_ust_event
*attr
)
321 struct ust_app_event
*ua_event
;
323 /* Init most of the default value by allocating and zeroing */
324 ua_event
= zmalloc(sizeof(struct ust_app_event
));
325 if (ua_event
== NULL
) {
330 ua_event
->enabled
= 1;
331 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
332 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
333 ua_event
->ctx
= hashtable_new(0);
334 hashtable_node_init(&ua_event
->node
, (void *) ua_event
->name
,
335 strlen(ua_event
->name
));
337 /* Copy attributes */
339 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
342 DBG3("UST app event %s allocated", ua_event
->name
);
351 * Alloc new UST app context.
354 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
356 struct ust_app_ctx
*ua_ctx
;
358 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
359 if (ua_ctx
== NULL
) {
364 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
367 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
374 * Find an ust_app using the sock and return it. RCU read side lock must be
375 * held before calling this helper function.
378 struct ust_app
*find_app_by_sock(int sock
)
380 struct cds_lfht_node
*node
;
381 struct ust_app_key
*key
;
382 struct cds_lfht_iter iter
;
384 node
= hashtable_lookup(ust_app_sock_key_map
,
385 (void *)((unsigned long) sock
), sizeof(void *), &iter
);
387 DBG2("UST app find by sock %d key not found", sock
);
391 key
= caa_container_of(node
, struct ust_app_key
, node
);
393 node
= hashtable_lookup(ust_app_ht
,
394 (void *)((unsigned long) key
->pid
), sizeof(void *), &iter
);
396 DBG2("UST app find by sock %d not found", sock
);
399 return caa_container_of(node
, struct ust_app
, node
);
406 * Create the channel context on the tracer.
409 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
410 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
414 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
415 ua_chan
->obj
, &ua_ctx
->obj
);
420 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
422 DBG2("UST app context added to channel %s successfully", ua_chan
->name
);
429 * Create the event context on the tracer.
432 int create_ust_event_context(struct ust_app_event
*ua_event
,
433 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
437 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
438 ua_event
->obj
, &ua_ctx
->obj
);
443 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
445 DBG2("UST app context added to event %s successfully", ua_event
->name
);
452 * Disable the specified event on to UST tracer for the UST session.
454 static int disable_ust_event(struct ust_app
*app
,
455 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
459 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
461 ERR("UST app event %s disable failed for app (pid: %d) "
462 "and session handle %d with ret %d",
463 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
467 DBG2("UST app event %s disabled successfully for app (pid: %d)",
468 ua_event
->attr
.name
, app
->key
.pid
);
475 * Disable the specified channel on to UST tracer for the UST session.
477 static int disable_ust_channel(struct ust_app
*app
,
478 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
482 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
484 ERR("UST app channel %s disable failed for app (pid: %d) "
485 "and session handle %d with ret %d",
486 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
490 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
491 ua_chan
->name
, app
->key
.pid
);
498 * Enable the specified channel on to UST tracer for the UST session.
500 static int enable_ust_channel(struct ust_app
*app
,
501 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
505 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
507 ERR("UST app channel %s enable failed for app (pid: %d) "
508 "and session handle %d with ret %d",
509 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
513 ua_chan
->enabled
= 1;
515 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
516 ua_chan
->name
, app
->key
.pid
);
523 * Enable the specified event on to UST tracer for the UST session.
525 static int enable_ust_event(struct ust_app
*app
,
526 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
530 ret
= ustctl_enable(app
->key
.sock
, ua_event
->obj
);
532 ERR("UST app event %s enable failed for app (pid: %d) "
533 "and session handle %d with ret %d",
534 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
538 DBG2("UST app event %s enabled successfully for app (pid: %d)",
539 ua_event
->attr
.name
, app
->key
.pid
);
546 * Open metadata onto the UST tracer for a UST session.
548 static int open_ust_metadata(struct ust_app
*app
,
549 struct ust_app_session
*ua_sess
)
552 struct lttng_ust_channel_attr uattr
;
554 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
555 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
556 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
557 uattr
.switch_timer_interval
=
558 ua_sess
->metadata
->attr
.switch_timer_interval
;
559 uattr
.read_timer_interval
=
560 ua_sess
->metadata
->attr
.read_timer_interval
;
561 uattr
.output
= ua_sess
->metadata
->attr
.output
;
563 /* UST tracer metadata creation */
564 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
565 &ua_sess
->metadata
->obj
);
567 ERR("UST app open metadata failed for app pid:%d",
572 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
579 * Create stream onto the UST tracer for a UST session.
581 static int create_ust_stream(struct ust_app
*app
,
582 struct ust_app_session
*ua_sess
)
586 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
587 &ua_sess
->metadata
->stream_obj
);
589 ERR("UST create metadata stream failed");
598 * Create the specified channel onto the UST tracer for a UST session.
600 static int create_ust_channel(struct ust_app
*app
,
601 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
605 /* TODO: remove cast and use lttng-ust-abi.h */
606 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
607 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
609 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
610 "and session handle %d with ret %d",
611 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
612 ua_sess
->handle
, ret
);
616 ua_chan
->handle
= ua_chan
->obj
->handle
;
617 ua_chan
->attr
.shm_fd
= ua_chan
->obj
->shm_fd
;
618 ua_chan
->attr
.wait_fd
= ua_chan
->obj
->wait_fd
;
619 ua_chan
->attr
.memory_map_size
= ua_chan
->obj
->memory_map_size
;
621 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
622 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
624 /* If channel is not enabled, disable it on the tracer */
625 if (!ua_chan
->enabled
) {
626 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
637 * Create the specified event onto the UST tracer for a UST session.
640 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
641 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
645 /* Create UST event on tracer */
646 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
649 ERR("Error ustctl create event %s for app pid: %d with ret %d",
650 ua_event
->attr
.name
, app
->key
.pid
, ret
);
654 ua_event
->handle
= ua_event
->obj
->handle
;
656 DBG2("UST app event %s created successfully for pid:%d",
657 ua_event
->attr
.name
, app
->key
.pid
);
659 /* If event not enabled, disable it on the tracer */
660 if (!ua_event
->enabled
) {
661 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
672 * Copy data between an UST app event and a LTT event.
674 static void shadow_copy_event(struct ust_app_event
*ua_event
,
675 struct ltt_ust_event
*uevent
)
677 struct cds_lfht_iter iter
;
678 struct ltt_ust_context
*uctx
;
679 struct ust_app_ctx
*ua_ctx
;
681 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
682 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
684 /* Copy event attributes */
685 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
687 cds_lfht_for_each_entry(uevent
->ctx
, &iter
, uctx
, node
) {
688 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
689 if (ua_ctx
== NULL
) {
692 hashtable_node_init(&ua_ctx
->node
,
693 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
694 hashtable_add_unique(ua_event
->ctx
, &ua_ctx
->node
);
699 * Copy data between an UST app channel and a LTT channel.
701 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
702 struct ltt_ust_channel
*uchan
)
704 struct cds_lfht_iter iter
;
705 struct cds_lfht_node
*ua_event_node
;
706 struct ltt_ust_event
*uevent
;
707 struct ltt_ust_context
*uctx
;
708 struct ust_app_event
*ua_event
;
709 struct ust_app_ctx
*ua_ctx
;
711 DBG2("Shadow copy of UST app channel %s", ua_chan
->name
);
713 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
714 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
715 /* Copy event attributes */
716 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
718 cds_lfht_for_each_entry(uchan
->ctx
, &iter
, uctx
, node
) {
719 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
720 if (ua_ctx
== NULL
) {
723 hashtable_node_init(&ua_ctx
->node
,
724 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
725 hashtable_add_unique(ua_chan
->ctx
, &ua_ctx
->node
);
728 /* Copy all events from ltt ust channel to ust app channel */
729 cds_lfht_for_each_entry(uchan
->events
, &iter
, uevent
, node
) {
730 struct cds_lfht_iter uiter
;
732 ua_event_node
= hashtable_lookup(ua_chan
->events
,
733 (void *) uevent
->attr
.name
, strlen(uevent
->attr
.name
),
735 if (ua_event_node
== NULL
) {
736 DBG2("UST event %s not found on shadow copy channel",
738 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
739 if (ua_event
== NULL
) {
742 shadow_copy_event(ua_event
, uevent
);
743 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
747 DBG3("Shadow copy channel done");
751 * Copy data between a UST app session and a regular LTT session.
753 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
754 struct ltt_ust_session
*usess
,
757 struct cds_lfht_node
*ua_chan_node
;
758 struct cds_lfht_iter iter
;
759 struct ltt_ust_channel
*uchan
;
760 struct ust_app_channel
*ua_chan
;
766 /* Get date and time for unique app path */
768 timeinfo
= localtime(&rawtime
);
769 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
771 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
773 ua_sess
->id
= usess
->id
;
774 ua_sess
->uid
= usess
->uid
;
775 ua_sess
->gid
= usess
->gid
;
777 ret
= snprintf(ua_sess
->path
, PATH_MAX
,
779 usess
->pathname
, app
->name
, app
->key
.pid
,
782 PERROR("asprintf UST shadow copy session");
783 /* TODO: We cannot return an error from here.. */
787 /* TODO: support all UST domain */
789 /* Iterate over all channels in global domain. */
790 cds_lfht_for_each_entry(usess
->domain_global
.channels
, &iter
,
792 struct cds_lfht_iter uiter
;
794 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
795 (void *)uchan
->name
, strlen(uchan
->name
),
797 if (ua_chan_node
!= NULL
) {
801 DBG2("Channel %s not found on shadow session copy, creating it",
803 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
804 if (ua_chan
== NULL
) {
805 /* malloc failed... continuing */
809 shadow_copy_channel(ua_chan
, uchan
);
810 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
815 * Lookup sesison wrapper.
818 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
819 struct ust_app
*app
, struct cds_lfht_iter
*iter
)
821 /* Get right UST app session from app */
822 (void) hashtable_lookup(app
->sessions
,
823 (void *) ((unsigned long) usess
->id
), sizeof(void *),
828 * Return ust app session from the app session hashtable using the UST session
831 static struct ust_app_session
*lookup_session_by_app(
832 struct ltt_ust_session
*usess
, struct ust_app
*app
)
834 struct cds_lfht_iter iter
;
835 struct cds_lfht_node
*node
;
837 __lookup_session_by_app(usess
, app
, &iter
);
838 node
= hashtable_iter_get_node(&iter
);
843 return caa_container_of(node
, struct ust_app_session
, node
);
850 * Create a UST session onto the tracer of app and add it the session
853 * Return ust app session or NULL on error.
855 static struct ust_app_session
*create_ust_app_session(
856 struct ltt_ust_session
*usess
, struct ust_app
*app
)
859 struct ust_app_session
*ua_sess
;
861 ua_sess
= lookup_session_by_app(usess
, app
);
862 if (ua_sess
== NULL
) {
863 DBG2("UST app pid: %d session id %d not found, creating it",
864 app
->key
.pid
, usess
->id
);
865 ua_sess
= alloc_ust_app_session();
866 if (ua_sess
== NULL
) {
867 /* Only malloc can failed so something is really wrong */
870 shadow_copy_session(ua_sess
, usess
, app
);
873 if (ua_sess
->handle
== -1) {
874 ret
= ustctl_create_session(app
->key
.sock
);
876 ERR("Error creating session for app pid %d, sock %d",
877 app
->key
.pid
, app
->key
.sock
);
878 /* TODO: free() ua_sess */
882 DBG2("UST app ustctl create session handle %d", ret
);
883 ua_sess
->handle
= ret
;
885 /* Add ust app session to app's HT */
886 hashtable_node_init(&ua_sess
->node
,
887 (void *)((unsigned long) ua_sess
->id
), sizeof(void *));
888 hashtable_add_unique(app
->sessions
, &ua_sess
->node
);
890 DBG2("UST app session created successfully with handle %d", ret
);
900 * Create a context for the channel on the tracer.
903 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
904 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
908 struct cds_lfht_iter iter
;
909 struct cds_lfht_node
*node
;
910 struct ust_app_ctx
*ua_ctx
;
912 DBG2("UST app adding context to channel %s", ua_chan
->name
);
914 node
= hashtable_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
),
915 sizeof(void *), &iter
);
921 ua_ctx
= alloc_ust_app_ctx(uctx
);
922 if (ua_ctx
== NULL
) {
928 hashtable_node_init(&ua_ctx
->node
,
929 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
930 hashtable_add_unique(ua_chan
->ctx
, &ua_ctx
->node
);
932 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
942 * Create an UST context and enable it for the event on the tracer.
945 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
946 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
950 struct cds_lfht_iter iter
;
951 struct cds_lfht_node
*node
;
952 struct ust_app_ctx
*ua_ctx
;
954 DBG2("UST app adding context to event %s", ua_event
->name
);
956 node
= hashtable_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
),
957 sizeof(void *), &iter
);
963 ua_ctx
= alloc_ust_app_ctx(uctx
);
964 if (ua_ctx
== NULL
) {
970 hashtable_node_init(&ua_ctx
->node
,
971 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
972 hashtable_add_unique(ua_event
->ctx
, &ua_ctx
->node
);
974 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
984 * Enable on the tracer side a ust app event for the session and channel.
987 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
988 struct ust_app_event
*ua_event
, struct ust_app
*app
)
992 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
997 ua_event
->enabled
= 1;
1004 * Disable on the tracer side a ust app event for the session and channel.
1006 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1007 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1011 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1016 ua_event
->enabled
= 0;
1023 * Lookup ust app channel for session and disable it on the tracer side.
1026 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1027 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1031 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1036 ua_chan
->enabled
= 0;
1043 * Lookup ust app channel for session and enable it on the tracer side.
1045 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1046 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1049 struct cds_lfht_iter iter
;
1050 struct cds_lfht_node
*ua_chan_node
;
1051 struct ust_app_channel
*ua_chan
;
1053 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1054 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1055 if (ua_chan_node
== NULL
) {
1056 DBG2("Unable to find channel %s in ust session id %u",
1057 uchan
->name
, ua_sess
->id
);
1061 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1063 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1073 * Create UST app channel and create it on the tracer.
1075 static struct ust_app_channel
*create_ust_app_channel(
1076 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1077 struct ust_app
*app
)
1080 struct cds_lfht_iter iter
;
1081 struct cds_lfht_node
*ua_chan_node
;
1082 struct ust_app_channel
*ua_chan
;
1084 /* Lookup channel in the ust app session */
1085 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1086 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1087 if (ua_chan_node
== NULL
) {
1088 DBG2("Unable to find channel %s in ust session id %u",
1089 uchan
->name
, ua_sess
->id
);
1090 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1091 if (ua_chan
== NULL
) {
1094 shadow_copy_channel(ua_chan
, uchan
);
1096 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
1098 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1101 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1113 * Create UST app event and create it on the tracer side.
1116 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1117 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1118 struct ust_app
*app
)
1121 struct cds_lfht_iter iter
;
1122 struct cds_lfht_node
*ua_event_node
;
1123 struct ust_app_event
*ua_event
;
1125 /* Get event node */
1126 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1127 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
1128 if (ua_event_node
!= NULL
) {
1129 ERR("UST app event %s already exist. Stopping creation.",
1134 /* Does not exist so create one */
1135 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1136 if (ua_event
== NULL
) {
1137 /* Only malloc can failed so something is really wrong */
1141 shadow_copy_event(ua_event
, uevent
);
1143 /* Create it on the tracer side */
1144 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1147 delete_ust_app_event(app
->key
.sock
, ua_event
);
1152 ua_event
->enabled
= 1;
1154 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
1156 DBG2("UST app create event %s for PID %d completed",
1157 ua_event
->name
, app
->key
.pid
);
1165 * Create UST metadata and open it on the tracer side.
1167 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1168 char *pathname
, struct ust_app
*app
)
1172 if (ua_sess
->metadata
== NULL
) {
1173 /* Allocate UST metadata */
1174 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1175 if (ua_sess
->metadata
== NULL
) {
1176 ERR("UST app session %d creating metadata failed",
1181 ret
= open_ust_metadata(app
, ua_sess
);
1186 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
1189 /* Open UST metadata stream */
1190 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1191 ret
= create_ust_stream(app
, ua_sess
);
1196 ret
= mkdir_run_as(ua_sess
->path
, S_IRWXU
| S_IRWXG
,
1197 ua_sess
->uid
, ua_sess
->gid
);
1199 PERROR("mkdir UST metadata");
1203 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1204 "%s/metadata", ua_sess
->path
);
1206 PERROR("asprintf UST create stream");
1210 DBG2("UST metadata stream object created for app pid %d",
1213 ERR("Attempting to create stream without metadata opened");
1224 * Return pointer to traceable apps list.
1226 struct cds_lfht
*ust_app_get_ht(void)
1232 * Return ust app pointer or NULL if not found.
1234 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1236 struct cds_lfht_node
*node
;
1237 struct cds_lfht_iter iter
;
1240 node
= hashtable_lookup(ust_app_ht
,
1241 (void *)((unsigned long) pid
), sizeof(void *), &iter
);
1243 DBG2("UST app no found with pid %d", pid
);
1248 DBG2("Found UST app by pid %d", pid
);
1250 return caa_container_of(node
, struct ust_app
, node
);
1258 * Using pid and uid (of the app), allocate a new ust_app struct and
1259 * add it to the global traceable app list.
1261 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1262 * bitness is not supported.
1264 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1266 struct ust_app
*lta
;
1268 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1269 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1270 ERR("Registration failed: application \"%s\" (pid: %d) has "
1271 "%d-bit long, but no consumerd for this long size is available.\n",
1272 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1276 lta
= zmalloc(sizeof(struct ust_app
));
1282 lta
->ppid
= msg
->ppid
;
1283 lta
->uid
= msg
->uid
;
1284 lta
->gid
= msg
->gid
;
1285 lta
->bits_per_long
= msg
->bits_per_long
;
1286 lta
->v_major
= msg
->major
;
1287 lta
->v_minor
= msg
->minor
;
1288 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1289 lta
->name
[16] = '\0';
1290 lta
->sessions
= hashtable_new(0);
1293 lta
->key
.pid
= msg
->pid
;
1294 hashtable_node_init(<a
->node
, (void *)((unsigned long)lta
->key
.pid
),
1296 lta
->key
.sock
= sock
;
1297 hashtable_node_init(<a
->key
.node
, (void *)((unsigned long)lta
->key
.sock
),
1301 hashtable_add_unique(ust_app_sock_key_map
, <a
->key
.node
);
1302 hashtable_add_unique(ust_app_ht
, <a
->node
);
1305 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1306 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1307 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1313 * Unregister app by removing it from the global traceable app list and freeing
1316 * The socket is already closed at this point so no close to sock.
1318 void ust_app_unregister(int sock
)
1320 struct ust_app
*lta
;
1321 struct cds_lfht_node
*node
;
1322 struct cds_lfht_iter iter
;
1326 lta
= find_app_by_sock(sock
);
1328 ERR("Unregister app sock %d not found!", sock
);
1332 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1334 /* Get the node reference for a call_rcu */
1335 node
= hashtable_lookup(ust_app_ht
,
1336 (void *)((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
1338 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1342 ret
= hashtable_del(ust_app_ht
, &iter
);
1344 call_rcu(&node
->head
, delete_ust_app_rcu
);
1351 * Return traceable_app_count
1353 unsigned long ust_app_list_count(void)
1355 unsigned long count
;
1358 count
= hashtable_get_count(ust_app_ht
);
1365 * Fill events array with all events name of all registered apps.
1367 int ust_app_list_events(struct lttng_event
**events
)
1370 size_t nbmem
, count
= 0;
1371 struct cds_lfht_iter iter
;
1372 struct ust_app
*app
;
1373 struct lttng_event
*tmp
;
1375 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1376 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1378 PERROR("zmalloc ust app events");
1385 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1386 struct lttng_ust_tracepoint_iter uiter
;
1388 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1390 ERR("UST app list events getting handle failed for app pid %d",
1395 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1396 &uiter
)) != -ENOENT
) {
1397 if (count
>= nbmem
) {
1398 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1401 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1403 PERROR("realloc ust app events");
1408 memcpy(tmp
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1409 memcpy(tmp
[count
].loglevel
, uiter
.loglevel
, LTTNG_UST_SYM_NAME_LEN
);
1410 tmp
[count
].loglevel_value
= uiter
.loglevel_value
;
1411 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1412 tmp
[count
].pid
= app
->key
.pid
;
1413 tmp
[count
].enabled
= -1;
1421 DBG2("UST app list events done (%zu events)", count
);
1430 * Free and clean all traceable apps of the global list.
1432 void ust_app_clean_list(void)
1435 struct cds_lfht_iter iter
;
1436 struct ust_app
*app
;
1438 DBG2("UST app cleaning registered apps hash table");
1442 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1443 ret
= hashtable_del(ust_app_ht
, &iter
);
1445 call_rcu(&iter
.node
->head
, delete_ust_app_rcu
);
1448 hashtable_destroy(ust_app_ht
);
1449 hashtable_destroy(ust_app_sock_key_map
);
1455 * Init UST app hash table.
1457 void ust_app_ht_alloc(void)
1459 ust_app_ht
= hashtable_new(0);
1460 ust_app_sock_key_map
= hashtable_new(0);
1464 * For a specific UST session, disable the channel for all registered apps.
1466 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1467 struct ltt_ust_channel
*uchan
)
1470 struct cds_lfht_iter iter
;
1471 struct cds_lfht_node
*ua_chan_node
;
1472 struct ust_app
*app
;
1473 struct ust_app_session
*ua_sess
;
1474 struct ust_app_channel
*ua_chan
;
1476 if (usess
== NULL
|| uchan
== NULL
) {
1477 ERR("Disabling UST global channel with NULL values");
1482 DBG2("UST app disabling channel %s from global domain for session id %d",
1483 uchan
->name
, usess
->id
);
1487 /* For every registered applications */
1488 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1489 struct cds_lfht_iter uiter
;
1491 ua_sess
= lookup_session_by_app(usess
, app
);
1492 if (ua_sess
== NULL
) {
1497 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1498 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
1499 /* If the session if found for the app, the channel must be there */
1500 assert(ua_chan_node
);
1502 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1503 /* The channel must not be already disabled */
1504 assert(ua_chan
->enabled
== 1);
1506 /* Disable channel onto application */
1507 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1509 /* XXX: We might want to report this error at some point... */
1521 * For a specific UST session, enable the channel for all registered apps.
1523 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1524 struct ltt_ust_channel
*uchan
)
1527 struct cds_lfht_iter iter
;
1528 struct ust_app
*app
;
1529 struct ust_app_session
*ua_sess
;
1531 if (usess
== NULL
|| uchan
== NULL
) {
1532 ERR("Adding UST global channel to NULL values");
1537 DBG2("UST app enabling channel %s to global domain for session id %d",
1538 uchan
->name
, usess
->id
);
1542 /* For every registered applications */
1543 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1544 ua_sess
= lookup_session_by_app(usess
, app
);
1545 if (ua_sess
== NULL
) {
1549 /* Enable channel onto application */
1550 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1552 /* XXX: We might want to report this error at some point... */
1564 * Disable an event in a channel and for a specific session.
1566 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1567 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1570 struct cds_lfht_iter iter
;
1571 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
1572 struct ust_app
*app
;
1573 struct ust_app_session
*ua_sess
;
1574 struct ust_app_channel
*ua_chan
;
1575 struct ust_app_event
*ua_event
;
1577 DBG("UST app disabling event %s for all apps in channel "
1578 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
1582 /* For all registered applications */
1583 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1584 struct cds_lfht_iter uiter
;
1586 ua_sess
= lookup_session_by_app(usess
, app
);
1587 if (ua_sess
== NULL
) {
1592 /* Lookup channel in the ust app session */
1593 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1594 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
1595 if (ua_chan_node
== NULL
) {
1596 DBG2("Channel %s not found in session id %d for app pid %d."
1597 "Skipping", uchan
->name
, usess
->id
, app
->key
.pid
);
1600 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1602 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1603 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
1604 if (ua_event_node
== NULL
) {
1605 DBG2("Event %s not found in channel %s for app pid %d."
1606 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1609 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1611 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1613 /* XXX: Report error someday... */
1624 * For a specific UST session and UST channel, the event for all
1627 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1628 struct ltt_ust_channel
*uchan
)
1631 struct cds_lfht_iter iter
;
1632 struct cds_lfht_node
*ua_chan_node
;
1633 struct ust_app
*app
;
1634 struct ust_app_session
*ua_sess
;
1635 struct ust_app_channel
*ua_chan
;
1636 struct ust_app_event
*ua_event
;
1638 DBG("UST app disabling all event for all apps in channel "
1639 "%s for session id %d", uchan
->name
, usess
->id
);
1643 /* For all registered applications */
1644 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1645 struct cds_lfht_iter uiter
;
1647 ua_sess
= lookup_session_by_app(usess
, app
);
1648 /* If ua_sess is NULL, there is a code flow error */
1651 /* Lookup channel in the ust app session */
1652 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1653 strlen(uchan
->name
), &uiter
);
1654 /* If the channel is not found, there is a code flow error */
1655 assert(ua_chan_node
);
1657 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1659 /* Disable each events of channel */
1660 cds_lfht_for_each_entry(ua_chan
->events
, &uiter
, ua_event
, node
) {
1661 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1663 /* XXX: Report error someday... */
1675 * For a specific UST session, create the channel for all registered apps.
1677 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1678 struct ltt_ust_channel
*uchan
)
1681 struct cds_lfht_iter iter
;
1682 struct ust_app
*app
;
1683 struct ust_app_session
*ua_sess
;
1684 struct ust_app_channel
*ua_chan
;
1686 if (usess
== NULL
|| uchan
== NULL
) {
1687 ERR("Adding UST global channel to NULL values");
1692 DBG2("UST app adding channel %s to global domain for session id %d",
1693 uchan
->name
, usess
->id
);
1697 /* For every registered applications */
1698 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1700 * Create session on the tracer side and add it to app session HT. Note
1701 * that if session exist, it will simply return a pointer to the ust
1704 ua_sess
= create_ust_app_session(usess
, app
);
1705 if (ua_sess
== NULL
) {
1709 /* Create channel onto application */
1710 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1711 if (ua_chan
== NULL
) {
1723 * Enable event for a specific session and channel on the tracer.
1725 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1726 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1729 struct cds_lfht_iter iter
;
1730 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
1731 struct ust_app
*app
;
1732 struct ust_app_session
*ua_sess
;
1733 struct ust_app_channel
*ua_chan
;
1734 struct ust_app_event
*ua_event
;
1736 DBG("UST app enabling event %s for all apps for session id %d",
1737 uevent
->attr
.name
, usess
->id
);
1740 * NOTE: At this point, this function is called only if the session and
1741 * channel passed are already created for all apps. and enabled on the
1747 /* For all registered applications */
1748 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1749 struct cds_lfht_iter uiter
;
1751 ua_sess
= lookup_session_by_app(usess
, app
);
1752 /* If ua_sess is NULL, there is a code flow error */
1755 /* Lookup channel in the ust app session */
1756 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1757 strlen(uchan
->name
), &uiter
);
1758 /* If the channel is not found, there is a code flow error */
1759 assert(ua_chan_node
);
1761 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1763 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1764 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
1765 if (ua_event_node
== NULL
) {
1766 DBG3("UST app enable event %s not found for app PID %d."
1767 "Skipping app", uevent
->attr
.name
, app
->key
.pid
);
1770 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1772 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1784 * For a specific existing UST session and UST channel, creates the event for
1785 * all registered apps.
1787 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1788 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1791 struct cds_lfht_iter iter
;
1792 struct cds_lfht_node
*ua_chan_node
;
1793 struct ust_app
*app
;
1794 struct ust_app_session
*ua_sess
;
1795 struct ust_app_channel
*ua_chan
;
1797 DBG("UST app creating event %s for all apps for session id %d",
1798 uevent
->attr
.name
, usess
->id
);
1801 * NOTE: At this point, this function is called only if the session and
1802 * channel passed are already created for all apps. and enabled on the
1808 /* For all registered applications */
1809 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1810 struct cds_lfht_iter uiter
;
1812 ua_sess
= lookup_session_by_app(usess
, app
);
1813 /* If ua_sess is NULL, there is a code flow error */
1816 /* Lookup channel in the ust app session */
1817 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1818 strlen(uchan
->name
), &uiter
);
1819 /* If the channel is not found, there is a code flow error */
1820 assert(ua_chan_node
);
1822 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1824 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1836 * Start tracing for a specific UST session and app.
1838 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1841 struct cds_lfht_iter iter
;
1842 struct ust_app_session
*ua_sess
;
1843 struct ust_app_channel
*ua_chan
;
1844 struct ltt_ust_stream
*ustream
;
1847 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1851 ua_sess
= lookup_session_by_app(usess
, app
);
1852 if (ua_sess
== NULL
) {
1853 goto error_rcu_unlock
;
1856 /* Upon restart, we skip the setup, already done */
1857 if (ua_sess
->started
) {
1861 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1863 goto error_rcu_unlock
;
1866 /* For each channel */
1867 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1868 /* Create all streams */
1870 /* Create UST stream */
1871 ustream
= zmalloc(sizeof(*ustream
));
1872 if (ustream
== NULL
) {
1873 PERROR("zmalloc ust stream");
1874 goto error_rcu_unlock
;
1877 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1880 /* Got all streams */
1883 ustream
->handle
= ustream
->obj
->handle
;
1885 /* Order is important */
1886 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1887 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1888 ua_sess
->path
, ua_chan
->name
,
1889 ua_chan
->streams
.count
++);
1891 PERROR("asprintf UST create stream");
1894 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1899 switch (app
->bits_per_long
) {
1901 consumerd_fd
= ust_consumerd64_fd
;
1904 consumerd_fd
= ust_consumerd32_fd
;
1908 goto error_rcu_unlock
;
1911 /* Setup UST consumer socket and send fds to it */
1912 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1914 goto error_rcu_unlock
;
1916 ua_sess
->started
= 1;
1919 /* This start the UST tracing */
1920 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1922 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1923 goto error_rcu_unlock
;
1928 /* Quiescent wait after starting trace */
1929 ustctl_wait_quiescent(app
->key
.sock
);
1939 * Stop tracing for a specific UST session and app.
1941 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1944 struct cds_lfht_iter iter
;
1945 struct ust_app_session
*ua_sess
;
1946 struct ust_app_channel
*ua_chan
;
1948 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1952 ua_sess
= lookup_session_by_app(usess
, app
);
1953 if (ua_sess
== NULL
) {
1954 /* Only malloc can failed so something is really wrong */
1955 goto error_rcu_unlock
;
1958 /* This inhibits UST tracing */
1959 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1961 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1962 goto error_rcu_unlock
;
1965 /* Quiescent wait after stopping trace */
1966 ustctl_wait_quiescent(app
->key
.sock
);
1968 /* Flushing buffers */
1969 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1970 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_chan
->obj
);
1972 ERR("UST app PID %d channel %s flush failed",
1973 app
->key
.pid
, ua_chan
->name
);
1974 ERR("Ended with ret %d", ret
);
1975 /* Continuing flushing all buffers */
1980 /* Flush all buffers before stopping */
1981 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_sess
->metadata
->obj
);
1983 ERR("UST app PID %d metadata flush failed", app
->key
.pid
);
1984 ERR("Ended with ret %d", ret
);
1997 * Destroy a specific UST session in apps.
1999 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2001 struct ust_app_session
*ua_sess
;
2002 struct lttng_ust_object_data obj
;
2003 struct cds_lfht_iter iter
;
2004 struct cds_lfht_node
*node
;
2007 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
2011 __lookup_session_by_app(usess
, app
, &iter
);
2012 node
= hashtable_iter_get_node(&iter
);
2014 /* Only malloc can failed so something is really wrong */
2015 goto error_rcu_unlock
;
2017 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2018 ret
= hashtable_del(app
->sessions
, &iter
);
2020 delete_ust_app_session(app
->key
.sock
, ua_sess
);
2021 obj
.handle
= ua_sess
->handle
;
2024 obj
.memory_map_size
= 0;
2025 ustctl_release_object(app
->key
.sock
, &obj
);
2029 /* Quiescent wait after stopping trace */
2030 ustctl_wait_quiescent(app
->key
.sock
);
2040 * Start tracing for the UST session.
2042 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2045 struct cds_lfht_iter iter
;
2046 struct ust_app
*app
;
2048 DBG("Starting all UST traces");
2052 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2053 ret
= ust_app_start_trace(usess
, app
);
2055 /* Continue to next apps even on error */
2066 * Start tracing for the UST session.
2068 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2071 struct cds_lfht_iter iter
;
2072 struct ust_app
*app
;
2074 DBG("Stopping all UST traces");
2078 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2079 ret
= ust_app_stop_trace(usess
, app
);
2081 /* Continue to next apps even on error */
2092 * Destroy app UST session.
2094 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2097 struct cds_lfht_iter iter
;
2098 struct ust_app
*app
;
2100 DBG("Destroy all UST traces");
2104 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2105 ret
= ust_app_destroy_trace(usess
, app
);
2107 /* Continue to next apps even on error */
2118 * Add channels/events from UST global domain to registered apps at sock.
2120 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2123 struct cds_lfht_iter iter
;
2124 struct ust_app
*app
;
2125 struct ust_app_session
*ua_sess
;
2126 struct ust_app_channel
*ua_chan
;
2127 struct ust_app_event
*ua_event
;
2129 if (usess
== NULL
) {
2130 ERR("No UST session on global update. Returning");
2134 DBG2("UST app global update for app sock %d for session id %d", sock
,
2139 app
= find_app_by_sock(sock
);
2141 ERR("Failed to update app sock %d", sock
);
2145 ua_sess
= create_ust_app_session(usess
, app
);
2146 if (ua_sess
== NULL
) {
2151 * We can iterate safely here over all UST app session sicne the create ust
2152 * app session above made a shadow copy of the UST global domain from the
2155 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
2156 struct cds_lfht_iter uiter
;
2158 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2160 /* FIXME: Should we quit here or continue... */
2164 /* For each events */
2165 cds_lfht_for_each_entry(ua_chan
->events
, &uiter
, ua_event
, node
) {
2166 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2168 /* FIXME: Should we quit here or continue... */
2174 if (usess
->start_trace
) {
2175 ret
= ust_app_start_trace(usess
, app
);
2180 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2189 * Add context to a specific channel for global UST domain.
2191 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2192 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2195 struct cds_lfht_node
*ua_chan_node
;
2196 struct cds_lfht_iter iter
;
2197 struct ust_app_channel
*ua_chan
= NULL
;
2198 struct ust_app_session
*ua_sess
;
2199 struct ust_app
*app
;
2203 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2204 struct cds_lfht_iter uiter
;
2206 ua_sess
= lookup_session_by_app(usess
, app
);
2207 if (ua_sess
== NULL
) {
2211 /* Lookup channel in the ust app session */
2212 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
2213 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
2214 if (ua_chan_node
== NULL
) {
2217 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2220 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2231 * Add context to a specific event in a channel for global UST domain.
2233 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2234 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2235 struct ltt_ust_context
*uctx
)
2238 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2239 struct cds_lfht_iter iter
;
2240 struct ust_app_session
*ua_sess
;
2241 struct ust_app_event
*ua_event
;
2242 struct ust_app_channel
*ua_chan
= NULL
;
2243 struct ust_app
*app
;
2247 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2248 struct cds_lfht_iter uiter
;
2250 ua_sess
= lookup_session_by_app(usess
, app
);
2251 if (ua_sess
== NULL
) {
2255 /* Lookup channel in the ust app session */
2256 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
2257 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
2258 if (ua_chan_node
== NULL
) {
2261 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2264 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2265 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
2266 if (ua_event_node
== NULL
) {
2269 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2272 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2283 * Enable event for a channel from a UST session for a specific PID.
2285 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2286 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2289 struct cds_lfht_iter iter
;
2290 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2291 struct ust_app
*app
;
2292 struct ust_app_session
*ua_sess
;
2293 struct ust_app_channel
*ua_chan
;
2294 struct ust_app_event
*ua_event
;
2296 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2300 app
= ust_app_find_by_pid(pid
);
2302 ERR("UST app enable event per PID %d not found", pid
);
2307 ua_sess
= lookup_session_by_app(usess
, app
);
2308 /* If ua_sess is NULL, there is a code flow error */
2311 /* Lookup channel in the ust app session */
2312 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
2313 strlen(uchan
->name
), &iter
);
2314 /* If the channel is not found, there is a code flow error */
2315 assert(ua_chan_node
);
2317 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2319 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2320 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
2321 if (ua_event_node
== NULL
) {
2322 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2327 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2329 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2341 * Disable event for a channel from a UST session for a specific PID.
2343 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2344 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2347 struct cds_lfht_iter iter
;
2348 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2349 struct ust_app
*app
;
2350 struct ust_app_session
*ua_sess
;
2351 struct ust_app_channel
*ua_chan
;
2352 struct ust_app_event
*ua_event
;
2354 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2358 app
= ust_app_find_by_pid(pid
);
2360 ERR("UST app disable event per PID %d not found", pid
);
2365 ua_sess
= lookup_session_by_app(usess
, app
);
2366 /* If ua_sess is NULL, there is a code flow error */
2369 /* Lookup channel in the ust app session */
2370 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
2371 strlen(uchan
->name
), &iter
);
2372 if (ua_chan_node
== NULL
) {
2373 /* Channel does not exist, skip disabling */
2376 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2378 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2379 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
2380 if (ua_event_node
== NULL
) {
2381 /* Event does not exist, skip disabling */
2384 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2386 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);