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"
39 * Delete ust context safely. RCU read lock must be held before calling
43 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
46 ustctl_release_object(sock
, ua_ctx
->obj
);
53 * Delete ust app event safely. RCU read lock must be held before calling
57 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
60 struct cds_lfht_iter iter
;
61 struct ust_app_ctx
*ua_ctx
;
63 cds_lfht_for_each_entry(ua_event
->ctx
, &iter
, ua_ctx
, node
) {
64 ret
= hashtable_del(ua_event
->ctx
, &iter
);
66 delete_ust_app_ctx(sock
, ua_ctx
);
68 ret
= hashtable_destroy(ua_event
->ctx
);
71 if (ua_event
->obj
!= NULL
) {
72 ustctl_release_object(sock
, ua_event
->obj
);
79 * Delete ust app stream safely. RCU read lock must be held before calling
83 void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
86 ustctl_release_object(sock
, stream
->obj
);
93 * Delete ust app channel safely. RCU read lock must be held before calling
97 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
100 struct cds_lfht_iter iter
;
101 struct ust_app_event
*ua_event
;
102 struct ust_app_ctx
*ua_ctx
;
103 struct ltt_ust_stream
*stream
, *stmp
;
106 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
107 cds_list_del(&stream
->list
);
108 delete_ust_app_stream(sock
, stream
);
112 cds_lfht_for_each_entry(ua_chan
->ctx
, &iter
, ua_ctx
, node
) {
113 ret
= hashtable_del(ua_chan
->ctx
, &iter
);
115 delete_ust_app_ctx(sock
, ua_ctx
);
117 ret
= hashtable_destroy(ua_chan
->ctx
);
121 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
122 ret
= hashtable_del(ua_chan
->events
, &iter
);
124 delete_ust_app_event(sock
, ua_event
);
126 ret
= hashtable_destroy(ua_chan
->events
);
129 if (ua_chan
->obj
!= NULL
) {
130 ustctl_release_object(sock
, ua_chan
->obj
);
137 * Delete ust app session safely. RCU read lock must be held before calling
141 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
)
144 struct cds_lfht_iter iter
;
145 struct ust_app_channel
*ua_chan
;
147 if (ua_sess
->metadata
) {
148 if (ua_sess
->metadata
->stream_obj
) {
149 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
150 free(ua_sess
->metadata
->stream_obj
);
152 if (ua_sess
->metadata
->obj
) {
153 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
154 free(ua_sess
->metadata
->obj
);
158 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
159 ret
= hashtable_del(ua_sess
->channels
, &iter
);
161 delete_ust_app_channel(sock
, ua_chan
);
163 ret
= hashtable_destroy(ua_sess
->channels
);
170 * Delete a traceable application structure from the global list. Never call
171 * this function outside of a call_rcu call.
174 void delete_ust_app(struct ust_app
*app
)
177 struct cds_lfht_node
*node
;
178 struct cds_lfht_iter iter
;
179 struct ust_app_session
*ua_sess
;
183 /* Remove from key hash table */
184 node
= hashtable_lookup(ust_app_sock_key_map
,
185 (void *) ((unsigned long) app
->key
.sock
), sizeof(void *), &iter
);
187 /* Not suppose to happen */
188 ERR("UST app key %d not found in key hash table", app
->key
.sock
);
192 ret
= hashtable_del(ust_app_sock_key_map
, &iter
);
194 ERR("UST app unable to delete app sock %d from key hash table",
197 DBG2("UST app pair sock %d key %d deleted",
198 app
->key
.sock
, app
->key
.pid
);
201 /* Socket is already closed at this point */
203 /* Delete ust app sessions info */
204 sock
= app
->key
.sock
;
208 cds_lfht_for_each_entry(app
->sessions
, &iter
, ua_sess
, node
) {
209 ret
= hashtable_del(app
->sessions
, &iter
);
211 delete_ust_app_session(app
->key
.sock
, ua_sess
);
213 ret
= hashtable_destroy(app
->sessions
);
217 * Wait until we have removed the key from the sock hash table
218 * before closing this socket, otherwise an application could
219 * re-use the socket ID and race with the teardown, using the
220 * same hash table entry.
224 DBG2("UST app pid %d deleted", app
->key
.pid
);
231 * URCU intermediate call to delete an UST app.
234 void delete_ust_app_rcu(struct rcu_head
*head
)
236 struct cds_lfht_node
*node
=
237 caa_container_of(head
, struct cds_lfht_node
, head
);
238 struct ust_app
*app
=
239 caa_container_of(node
, struct ust_app
, node
);
245 * Alloc new UST app session.
248 struct ust_app_session
*alloc_ust_app_session(void)
250 struct ust_app_session
*ua_sess
;
252 /* Init most of the default value by allocating and zeroing */
253 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
254 if (ua_sess
== NULL
) {
259 ua_sess
->handle
= -1;
260 ua_sess
->channels
= hashtable_new_str(0);
269 * Alloc new UST app channel.
272 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
273 struct lttng_ust_channel
*attr
)
275 struct ust_app_channel
*ua_chan
;
277 /* Init most of the default value by allocating and zeroing */
278 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
279 if (ua_chan
== NULL
) {
284 /* Setup channel name */
285 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
286 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
288 ua_chan
->enabled
= 1;
289 ua_chan
->handle
= -1;
290 ua_chan
->ctx
= hashtable_new(0);
291 ua_chan
->events
= hashtable_new_str(0);
292 hashtable_node_init(&ua_chan
->node
, (void *) ua_chan
->name
,
293 strlen(ua_chan
->name
));
295 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
297 /* Copy attributes */
299 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
302 DBG3("UST app channel %s allocated", ua_chan
->name
);
311 * Alloc new UST app event.
314 struct ust_app_event
*alloc_ust_app_event(char *name
,
315 struct lttng_ust_event
*attr
)
317 struct ust_app_event
*ua_event
;
319 /* Init most of the default value by allocating and zeroing */
320 ua_event
= zmalloc(sizeof(struct ust_app_event
));
321 if (ua_event
== NULL
) {
326 ua_event
->enabled
= 1;
327 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
328 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
329 ua_event
->ctx
= hashtable_new(0);
330 hashtable_node_init(&ua_event
->node
, (void *) ua_event
->name
,
331 strlen(ua_event
->name
));
333 /* Copy attributes */
335 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
338 DBG3("UST app event %s allocated", ua_event
->name
);
347 * Alloc new UST app context.
350 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
352 struct ust_app_ctx
*ua_ctx
;
354 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
355 if (ua_ctx
== NULL
) {
360 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
363 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
370 * Find an ust_app using the sock and return it. RCU read side lock must be
371 * held before calling this helper function.
374 struct ust_app
*find_app_by_sock(int sock
)
376 struct cds_lfht_node
*node
;
377 struct ust_app_key
*key
;
378 struct cds_lfht_iter iter
;
380 node
= hashtable_lookup(ust_app_sock_key_map
,
381 (void *)((unsigned long) sock
), sizeof(void *), &iter
);
383 DBG2("UST app find by sock %d key not found", sock
);
387 key
= caa_container_of(node
, struct ust_app_key
, node
);
389 node
= hashtable_lookup(ust_app_ht
,
390 (void *)((unsigned long) key
->pid
), sizeof(void *), &iter
);
392 DBG2("UST app find by sock %d not found", sock
);
395 return caa_container_of(node
, struct ust_app
, node
);
402 * Create the channel context on the tracer.
405 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
406 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
410 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
411 ua_chan
->obj
, &ua_ctx
->obj
);
416 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
418 DBG2("UST app context added to channel %s successfully", ua_chan
->name
);
425 * Create the event context on the tracer.
428 int create_ust_event_context(struct ust_app_event
*ua_event
,
429 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
433 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
434 ua_event
->obj
, &ua_ctx
->obj
);
439 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
441 DBG2("UST app context added to event %s successfully", ua_event
->name
);
448 * Disable the specified event on to UST tracer for the UST session.
450 static int disable_ust_event(struct ust_app
*app
,
451 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
455 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
457 ERR("UST app event %s disable failed for app (pid: %d) "
458 "and session handle %d with ret %d",
459 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
463 DBG2("UST app event %s disabled successfully for app (pid: %d)",
464 ua_event
->attr
.name
, app
->key
.pid
);
471 * Disable the specified channel on to UST tracer for the UST session.
473 static int disable_ust_channel(struct ust_app
*app
,
474 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
478 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
480 ERR("UST app channel %s disable failed for app (pid: %d) "
481 "and session handle %d with ret %d",
482 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
486 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
487 ua_chan
->name
, app
->key
.pid
);
494 * Enable the specified channel on to UST tracer for the UST session.
496 static int enable_ust_channel(struct ust_app
*app
,
497 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
501 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
503 ERR("UST app channel %s enable failed for app (pid: %d) "
504 "and session handle %d with ret %d",
505 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
509 ua_chan
->enabled
= 1;
511 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
512 ua_chan
->name
, app
->key
.pid
);
519 * Enable the specified event on to UST tracer for the UST session.
521 static int enable_ust_event(struct ust_app
*app
,
522 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
526 ret
= ustctl_enable(app
->key
.sock
, ua_event
->obj
);
528 ERR("UST app event %s enable failed for app (pid: %d) "
529 "and session handle %d with ret %d",
530 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
534 DBG2("UST app event %s enabled successfully for app (pid: %d)",
535 ua_event
->attr
.name
, app
->key
.pid
);
542 * Open metadata onto the UST tracer for a UST session.
544 static int open_ust_metadata(struct ust_app
*app
,
545 struct ust_app_session
*ua_sess
)
548 struct lttng_ust_channel_attr uattr
;
550 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
551 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
552 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
553 uattr
.switch_timer_interval
=
554 ua_sess
->metadata
->attr
.switch_timer_interval
;
555 uattr
.read_timer_interval
=
556 ua_sess
->metadata
->attr
.read_timer_interval
;
557 uattr
.output
= ua_sess
->metadata
->attr
.output
;
559 /* UST tracer metadata creation */
560 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
561 &ua_sess
->metadata
->obj
);
563 ERR("UST app open metadata failed for app pid:%d",
573 * Create stream onto the UST tracer for a UST session.
575 static int create_ust_stream(struct ust_app
*app
,
576 struct ust_app_session
*ua_sess
)
580 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
581 &ua_sess
->metadata
->stream_obj
);
583 ERR("UST create metadata stream failed");
592 * Create the specified channel onto the UST tracer for a UST session.
594 static int create_ust_channel(struct ust_app
*app
,
595 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
599 /* TODO: remove cast and use lttng-ust-abi.h */
600 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
601 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
603 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
604 "and session handle %d with ret %d",
605 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
606 ua_sess
->handle
, ret
);
610 ua_chan
->handle
= ua_chan
->obj
->handle
;
611 ua_chan
->attr
.shm_fd
= ua_chan
->obj
->shm_fd
;
612 ua_chan
->attr
.wait_fd
= ua_chan
->obj
->wait_fd
;
613 ua_chan
->attr
.memory_map_size
= ua_chan
->obj
->memory_map_size
;
615 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
616 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
618 /* If channel is not enabled, disable it on the tracer */
619 if (!ua_chan
->enabled
) {
620 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
631 * Create the specified event onto the UST tracer for a UST session.
634 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
635 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
639 /* Create UST event on tracer */
640 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
643 ERR("Error ustctl create event %s for app pid: %d with ret %d",
644 ua_event
->attr
.name
, app
->key
.pid
, ret
);
648 ua_event
->handle
= ua_event
->obj
->handle
;
650 DBG2("UST app event %s created successfully for pid:%d",
651 ua_event
->attr
.name
, app
->key
.pid
);
653 /* If event not enabled, disable it on the tracer */
654 if (!ua_event
->enabled
) {
655 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
666 * Copy data between an UST app event and a LTT event.
668 static void shadow_copy_event(struct ust_app_event
*ua_event
,
669 struct ltt_ust_event
*uevent
)
671 struct cds_lfht_iter iter
;
672 struct ltt_ust_context
*uctx
;
673 struct ust_app_ctx
*ua_ctx
;
675 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
676 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
678 /* Copy event attributes */
679 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
681 cds_lfht_for_each_entry(uevent
->ctx
, &iter
, uctx
, node
) {
682 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
683 if (ua_ctx
== NULL
) {
686 hashtable_node_init(&ua_ctx
->node
,
687 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
688 hashtable_add_unique(ua_event
->ctx
, &ua_ctx
->node
);
693 * Copy data between an UST app channel and a LTT channel.
695 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
696 struct ltt_ust_channel
*uchan
)
698 struct cds_lfht_iter iter
;
699 struct cds_lfht_node
*ua_event_node
;
700 struct ltt_ust_event
*uevent
;
701 struct ltt_ust_context
*uctx
;
702 struct ust_app_event
*ua_event
;
703 struct ust_app_ctx
*ua_ctx
;
705 DBG2("Shadow copy of UST app channel %s", ua_chan
->name
);
707 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
708 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
709 /* Copy event attributes */
710 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
712 cds_lfht_for_each_entry(uchan
->ctx
, &iter
, uctx
, node
) {
713 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
714 if (ua_ctx
== NULL
) {
717 hashtable_node_init(&ua_ctx
->node
,
718 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
719 hashtable_add_unique(ua_chan
->ctx
, &ua_ctx
->node
);
722 /* Copy all events from ltt ust channel to ust app channel */
723 cds_lfht_for_each_entry(uchan
->events
, &iter
, uevent
, node
) {
724 struct cds_lfht_iter uiter
;
726 ua_event_node
= hashtable_lookup(ua_chan
->events
,
727 (void *) uevent
->attr
.name
, strlen(uevent
->attr
.name
),
729 if (ua_event_node
== NULL
) {
730 DBG2("UST event %s not found on shadow copy channel",
732 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
733 if (ua_event
== NULL
) {
736 shadow_copy_event(ua_event
, uevent
);
737 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
741 DBG3("Shadow copy channel done");
745 * Copy data between a UST app session and a regular LTT session.
747 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
748 struct ltt_ust_session
*usess
,
751 struct cds_lfht_node
*ua_chan_node
;
752 struct cds_lfht_iter iter
;
753 struct ltt_ust_channel
*uchan
;
754 struct ust_app_channel
*ua_chan
;
760 /* Get date and time for unique app path */
762 timeinfo
= localtime(&rawtime
);
763 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
765 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
767 ua_sess
->uid
= usess
->uid
;
769 ret
= snprintf(ua_sess
->path
, PATH_MAX
,
771 usess
->pathname
, app
->name
, app
->key
.pid
,
774 PERROR("asprintf UST shadow copy session");
775 /* TODO: We cannot return an error from here.. */
779 /* TODO: support all UST domain */
781 /* Iterate over all channels in global domain. */
782 cds_lfht_for_each_entry(usess
->domain_global
.channels
, &iter
,
784 struct cds_lfht_iter uiter
;
786 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
787 (void *)uchan
->name
, strlen(uchan
->name
),
789 if (ua_chan_node
!= NULL
) {
793 DBG2("Channel %s not found on shadow session copy, creating it",
795 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
796 if (ua_chan
== NULL
) {
797 /* malloc failed... continuing */
801 shadow_copy_channel(ua_chan
, uchan
);
802 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
807 * Lookup sesison wrapper.
810 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
811 struct ust_app
*app
, struct cds_lfht_iter
*iter
)
813 /* Get right UST app session from app */
814 (void) hashtable_lookup(app
->sessions
,
815 (void *) ((unsigned long) usess
->uid
), sizeof(void *),
820 * Return ust app session from the app session hashtable using the UST session
823 static struct ust_app_session
*lookup_session_by_app(
824 struct ltt_ust_session
*usess
, struct ust_app
*app
)
826 struct cds_lfht_iter iter
;
827 struct cds_lfht_node
*node
;
829 __lookup_session_by_app(usess
, app
, &iter
);
830 node
= hashtable_iter_get_node(&iter
);
835 return caa_container_of(node
, struct ust_app_session
, node
);
842 * Create a UST session onto the tracer of app and add it the session
845 * Return ust app session or NULL on error.
847 static struct ust_app_session
*create_ust_app_session(
848 struct ltt_ust_session
*usess
, struct ust_app
*app
)
851 struct ust_app_session
*ua_sess
;
853 ua_sess
= lookup_session_by_app(usess
, app
);
854 if (ua_sess
== NULL
) {
855 DBG2("UST app pid: %d session uid %d not found, creating it",
856 app
->key
.pid
, usess
->uid
);
857 ua_sess
= alloc_ust_app_session();
858 if (ua_sess
== NULL
) {
859 /* Only malloc can failed so something is really wrong */
862 shadow_copy_session(ua_sess
, usess
, app
);
865 if (ua_sess
->handle
== -1) {
866 ret
= ustctl_create_session(app
->key
.sock
);
868 ERR("Error creating session for app pid %d, sock %d",
869 app
->key
.pid
, app
->key
.sock
);
870 /* TODO: free() ua_sess */
874 DBG2("UST app ustctl create session handle %d", ret
);
875 ua_sess
->handle
= ret
;
877 /* Add ust app session to app's HT */
878 hashtable_node_init(&ua_sess
->node
,
879 (void *)((unsigned long) ua_sess
->uid
), sizeof(void *));
880 hashtable_add_unique(app
->sessions
, &ua_sess
->node
);
882 DBG2("UST app session created successfully with handle %d", ret
);
892 * Create a context for the channel on the tracer.
895 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
896 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
900 struct cds_lfht_iter iter
;
901 struct cds_lfht_node
*node
;
902 struct ust_app_ctx
*ua_ctx
;
904 DBG2("UST app adding context to channel %s", ua_chan
->name
);
906 node
= hashtable_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
),
907 sizeof(void *), &iter
);
913 ua_ctx
= alloc_ust_app_ctx(uctx
);
914 if (ua_ctx
== NULL
) {
920 hashtable_node_init(&ua_ctx
->node
,
921 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
922 hashtable_add_unique(ua_chan
->ctx
, &ua_ctx
->node
);
924 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
934 * Create an UST context and enable it for the event on the tracer.
937 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
938 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
942 struct cds_lfht_iter iter
;
943 struct cds_lfht_node
*node
;
944 struct ust_app_ctx
*ua_ctx
;
946 DBG2("UST app adding context to event %s", ua_event
->name
);
948 node
= hashtable_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
),
949 sizeof(void *), &iter
);
955 ua_ctx
= alloc_ust_app_ctx(uctx
);
956 if (ua_ctx
== NULL
) {
962 hashtable_node_init(&ua_ctx
->node
,
963 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
964 hashtable_add_unique(ua_event
->ctx
, &ua_ctx
->node
);
966 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
976 * Enable on the tracer side a ust app event for the session and channel.
979 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
980 struct ust_app_event
*ua_event
, struct ust_app
*app
)
984 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
989 ua_event
->enabled
= 1;
996 * Disable on the tracer side a ust app event for the session and channel.
998 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
999 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
,
1000 struct ust_app
*app
)
1004 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1009 ua_event
->enabled
= 0;
1016 * Lookup ust app channel for session and disable it on the tracer side.
1019 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1020 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1024 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1029 ua_chan
->enabled
= 0;
1036 * Lookup ust app channel for session and enable it on the tracer side.
1038 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1039 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1042 struct cds_lfht_iter iter
;
1043 struct cds_lfht_node
*ua_chan_node
;
1044 struct ust_app_channel
*ua_chan
;
1046 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1047 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1048 if (ua_chan_node
== NULL
) {
1049 DBG2("Unable to find channel %s in ust session uid %u",
1050 uchan
->name
, ua_sess
->uid
);
1054 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1056 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1066 * Create UST app channel and create it on the tracer.
1068 static struct ust_app_channel
*create_ust_app_channel(
1069 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1070 struct ust_app
*app
)
1073 struct cds_lfht_iter iter
;
1074 struct cds_lfht_node
*ua_chan_node
;
1075 struct ust_app_channel
*ua_chan
;
1077 /* Lookup channel in the ust app session */
1078 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1079 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1080 if (ua_chan_node
== NULL
) {
1081 DBG2("Unable to find channel %s in ust session uid %u",
1082 uchan
->name
, ua_sess
->uid
);
1083 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1084 if (ua_chan
== NULL
) {
1087 shadow_copy_channel(ua_chan
, uchan
);
1089 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
1091 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1094 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1106 * Create UST app event and create it on the tracer side.
1109 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1110 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1111 struct ust_app
*app
)
1114 struct cds_lfht_iter iter
;
1115 struct cds_lfht_node
*ua_event_node
;
1116 struct ust_app_event
*ua_event
;
1118 /* Get event node */
1119 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1120 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
1121 if (ua_event_node
!= NULL
) {
1122 ERR("UST app event %s already exist. Stopping creation.",
1127 /* Does not exist so create one */
1128 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1129 if (ua_event
== NULL
) {
1130 /* Only malloc can failed so something is really wrong */
1134 shadow_copy_event(ua_event
, uevent
);
1136 /* Create it on the tracer side */
1137 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1140 delete_ust_app_event(app
->key
.sock
, ua_event
);
1145 ua_event
->enabled
= 1;
1147 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
1155 * Create UST metadata and open it on the tracer side.
1157 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1158 char *pathname
, struct ust_app
*app
)
1162 if (ua_sess
->metadata
== NULL
) {
1163 /* Allocate UST metadata */
1164 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1165 if (ua_sess
->metadata
== NULL
) {
1166 ERR("UST app session %d creating metadata failed",
1171 ret
= open_ust_metadata(app
, ua_sess
);
1176 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
1179 /* Open UST metadata stream */
1180 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1181 ret
= create_ust_stream(app
, ua_sess
);
1186 ret
= mkdir(ua_sess
->path
, S_IRWXU
| S_IRWXG
);
1188 PERROR("mkdir UST metadata");
1192 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1193 "%s/metadata", ua_sess
->path
);
1195 PERROR("asprintf UST create stream");
1199 DBG2("UST metadata stream object created for app pid %d",
1202 ERR("Attempting to create stream without metadata opened");
1213 * Return pointer to traceable apps list.
1215 struct cds_lfht
*ust_app_get_ht(void)
1221 * Return ust app pointer or NULL if not found.
1223 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1225 struct cds_lfht_node
*node
;
1226 struct cds_lfht_iter iter
;
1229 node
= hashtable_lookup(ust_app_ht
,
1230 (void *)((unsigned long) pid
), sizeof(void *), &iter
);
1232 DBG2("UST app no found with pid %d", pid
);
1237 DBG2("Found UST app by pid %d", pid
);
1239 return caa_container_of(node
, struct ust_app
, node
);
1247 * Using pid and uid (of the app), allocate a new ust_app struct and
1248 * add it to the global traceable app list.
1250 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1251 * bitness is not supported.
1253 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1255 struct ust_app
*lta
;
1257 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1258 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1259 ERR("Registration failed: application \"%s\" (pid: %d) has "
1260 "%d-bit long, but no consumerd for this long size is available.\n",
1261 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1265 lta
= zmalloc(sizeof(struct ust_app
));
1271 lta
->ppid
= msg
->ppid
;
1272 lta
->uid
= msg
->uid
;
1273 lta
->gid
= msg
->gid
;
1274 lta
->bits_per_long
= msg
->bits_per_long
;
1275 lta
->v_major
= msg
->major
;
1276 lta
->v_minor
= msg
->minor
;
1277 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1278 lta
->name
[16] = '\0';
1279 lta
->sessions
= hashtable_new(0);
1282 lta
->key
.pid
= msg
->pid
;
1283 hashtable_node_init(<a
->node
, (void *)((unsigned long)lta
->key
.pid
),
1285 lta
->key
.sock
= sock
;
1286 hashtable_node_init(<a
->key
.node
, (void *)((unsigned long)lta
->key
.sock
),
1290 hashtable_add_unique(ust_app_sock_key_map
, <a
->key
.node
);
1291 hashtable_add_unique(ust_app_ht
, <a
->node
);
1294 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1295 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1296 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1302 * Unregister app by removing it from the global traceable app list and freeing
1305 * The socket is already closed at this point so no close to sock.
1307 void ust_app_unregister(int sock
)
1309 struct ust_app
*lta
;
1310 struct cds_lfht_node
*node
;
1311 struct cds_lfht_iter iter
;
1315 lta
= find_app_by_sock(sock
);
1317 ERR("Unregister app sock %d not found!", sock
);
1321 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1323 /* Get the node reference for a call_rcu */
1324 node
= hashtable_lookup(ust_app_ht
,
1325 (void *)((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
1327 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1331 ret
= hashtable_del(ust_app_ht
, &iter
);
1333 call_rcu(&node
->head
, delete_ust_app_rcu
);
1340 * Return traceable_app_count
1342 unsigned long ust_app_list_count(void)
1344 unsigned long count
;
1347 count
= hashtable_get_count(ust_app_ht
);
1354 * Fill events array with all events name of all registered apps.
1356 int ust_app_list_events(struct lttng_event
**events
)
1359 size_t nbmem
, count
= 0;
1360 struct cds_lfht_iter iter
;
1361 struct ust_app
*app
;
1362 struct lttng_event
*tmp
;
1364 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1365 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1367 PERROR("zmalloc ust app events");
1374 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1375 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1377 ERR("UST app list events getting handle failed for app pid %d",
1382 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1383 tmp
[count
].name
)) != -ENOENT
) {
1384 if (count
> nbmem
) {
1385 DBG2("Reallocating event list from %zu to %zu bytes", nbmem
,
1386 nbmem
+ UST_APP_EVENT_LIST_SIZE
);
1387 nbmem
+= UST_APP_EVENT_LIST_SIZE
;
1388 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1390 PERROR("realloc ust app events");
1396 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1397 tmp
[count
].pid
= app
->key
.pid
;
1398 tmp
[count
].enabled
= -1;
1406 DBG2("UST app list events done (%zu events)", count
);
1415 * Free and clean all traceable apps of the global list.
1417 void ust_app_clean_list(void)
1420 struct cds_lfht_iter iter
;
1421 struct ust_app
*app
;
1423 DBG2("UST app cleaning registered apps hash table");
1427 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1428 ret
= hashtable_del(ust_app_ht
, &iter
);
1430 call_rcu(&iter
.node
->head
, delete_ust_app_rcu
);
1433 hashtable_destroy(ust_app_ht
);
1434 hashtable_destroy(ust_app_sock_key_map
);
1440 * Init UST app hash table.
1442 void ust_app_ht_alloc(void)
1444 ust_app_ht
= hashtable_new(0);
1445 ust_app_sock_key_map
= hashtable_new(0);
1449 * For a specific UST session, disable the channel for all registered apps.
1451 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1452 struct ltt_ust_channel
*uchan
)
1455 struct cds_lfht_iter iter
;
1456 struct cds_lfht_node
*ua_chan_node
;
1457 struct ust_app
*app
;
1458 struct ust_app_session
*ua_sess
;
1459 struct ust_app_channel
*ua_chan
;
1461 if (usess
== NULL
|| uchan
== NULL
) {
1462 ERR("Disabling UST global channel with NULL values");
1467 DBG2("UST app disabling channel %s from global domain for session uid %d",
1468 uchan
->name
, usess
->uid
);
1472 /* For every registered applications */
1473 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1474 ua_sess
= lookup_session_by_app(usess
, app
);
1475 if (ua_sess
== NULL
) {
1480 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1481 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1482 /* If the session if found for the app, the channel must be there */
1483 assert(ua_chan_node
);
1485 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1486 /* The channel must not be already disabled */
1487 assert(ua_chan
->enabled
== 1);
1489 /* Disable channel onto application */
1490 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1492 /* XXX: We might want to report this error at some point... */
1504 * For a specific UST session, enable the channel for all registered apps.
1506 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1507 struct ltt_ust_channel
*uchan
)
1510 struct cds_lfht_iter iter
;
1511 struct ust_app
*app
;
1512 struct ust_app_session
*ua_sess
;
1514 if (usess
== NULL
|| uchan
== NULL
) {
1515 ERR("Adding UST global channel to NULL values");
1520 DBG2("UST app enabling channel %s to global domain for session uid %d",
1521 uchan
->name
, usess
->uid
);
1525 /* For every registered applications */
1526 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1527 ua_sess
= lookup_session_by_app(usess
, app
);
1528 if (ua_sess
== NULL
) {
1532 /* Enable channel onto application */
1533 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1535 /* XXX: We might want to report this error at some point... */
1547 * Disable an event in a channel and for a specific session.
1549 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1550 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1553 struct cds_lfht_iter iter
, uiter
;
1554 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
1555 struct ust_app
*app
;
1556 struct ust_app_session
*ua_sess
;
1557 struct ust_app_channel
*ua_chan
;
1558 struct ust_app_event
*ua_event
;
1560 DBG("UST app disabling event %s for all apps in channel "
1561 "%s for session uid %d", uevent
->attr
.name
, uchan
->name
, usess
->uid
);
1565 /* For all registered applications */
1566 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1567 ua_sess
= lookup_session_by_app(usess
, app
);
1568 if (ua_sess
== NULL
) {
1573 /* Lookup channel in the ust app session */
1574 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1575 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
1576 if (ua_chan_node
== NULL
) {
1577 DBG2("Channel %s not found in session uid %d for app pid %d."
1578 "Skipping", uchan
->name
, usess
->uid
, app
->key
.pid
);
1581 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1583 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1584 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
1585 if (ua_event_node
== NULL
) {
1586 DBG2("Event %s not found in channel %s for app pid %d."
1587 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1590 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1592 ret
= disable_ust_app_event(ua_sess
, ua_chan
, ua_event
, app
);
1594 /* XXX: Report error someday... */
1605 * For a specific UST session and UST channel, the event for all
1608 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1609 struct ltt_ust_channel
*uchan
)
1612 struct cds_lfht_iter iter
, uiter
;
1613 struct cds_lfht_node
*ua_chan_node
;
1614 struct ust_app
*app
;
1615 struct ust_app_session
*ua_sess
;
1616 struct ust_app_channel
*ua_chan
;
1617 struct ust_app_event
*ua_event
;
1619 DBG("UST app disabling all event for all apps in channel "
1620 "%s for session uid %d", uchan
->name
, usess
->uid
);
1624 /* For all registered applications */
1625 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1626 ua_sess
= lookup_session_by_app(usess
, app
);
1627 /* If ua_sess is NULL, there is a code flow error */
1630 /* Lookup channel in the ust app session */
1631 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1632 strlen(uchan
->name
), &uiter
);
1633 /* If the channel is not found, there is a code flow error */
1634 assert(ua_chan_node
);
1636 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1638 /* Disable each events of channel */
1639 cds_lfht_for_each_entry(ua_chan
->events
, &uiter
, ua_event
, node
) {
1640 ret
= disable_ust_app_event(ua_sess
, ua_chan
, ua_event
, app
);
1642 /* XXX: Report error someday... */
1654 * For a specific UST session, create the channel for all registered apps.
1656 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1657 struct ltt_ust_channel
*uchan
)
1660 struct cds_lfht_iter iter
;
1661 struct ust_app
*app
;
1662 struct ust_app_session
*ua_sess
;
1663 struct ust_app_channel
*ua_chan
;
1665 if (usess
== NULL
|| uchan
== NULL
) {
1666 ERR("Adding UST global channel to NULL values");
1671 DBG2("UST app adding channel %s to global domain for session uid %d",
1672 uchan
->name
, usess
->uid
);
1676 /* For every registered applications */
1677 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1679 * Create session on the tracer side and add it to app session HT. Note
1680 * that if session exist, it will simply return a pointer to the ust
1683 ua_sess
= create_ust_app_session(usess
, app
);
1684 if (ua_sess
== NULL
) {
1688 /* Create channel onto application */
1689 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1690 if (ua_chan
== NULL
) {
1702 * Enable event for a specific session and channel on the tracer.
1704 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1705 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1708 struct cds_lfht_iter iter
, uiter
;
1709 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
1710 struct ust_app
*app
;
1711 struct ust_app_session
*ua_sess
;
1712 struct ust_app_channel
*ua_chan
;
1713 struct ust_app_event
*ua_event
;
1715 DBG("UST app enabling event %s for all apps for session uid %d",
1716 uevent
->attr
.name
, usess
->uid
);
1719 * NOTE: At this point, this function is called only if the session and
1720 * channel passed are already created for all apps. and enabled on the
1726 /* For all registered applications */
1727 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1728 ua_sess
= lookup_session_by_app(usess
, app
);
1729 /* If ua_sess is NULL, there is a code flow error */
1732 /* Lookup channel in the ust app session */
1733 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1734 strlen(uchan
->name
), &uiter
);
1735 /* If the channel is not found, there is a code flow error */
1736 assert(ua_chan_node
);
1738 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1740 ua_event_node
= hashtable_lookup(ua_sess
->channels
,
1741 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
1742 if (ua_event_node
== NULL
) {
1743 DBG3("UST app enable event %s not found. Skipping app",
1747 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1749 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1751 /* XXX: Report error someday... */
1762 * For a specific existing UST session and UST channel, creates the event for
1763 * all registered apps.
1765 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1766 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1769 struct cds_lfht_iter iter
, uiter
;
1770 struct cds_lfht_node
*ua_chan_node
;
1771 struct ust_app
*app
;
1772 struct ust_app_session
*ua_sess
;
1773 struct ust_app_channel
*ua_chan
;
1775 DBG("UST app creating event %s for all apps for session uid %d",
1776 uevent
->attr
.name
, usess
->uid
);
1779 * NOTE: At this point, this function is called only if the session and
1780 * channel passed are already created for all apps. and enabled on the
1786 /* For all registered applications */
1787 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1788 ua_sess
= lookup_session_by_app(usess
, app
);
1789 /* If ua_sess is NULL, there is a code flow error */
1792 /* Lookup channel in the ust app session */
1793 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1794 strlen(uchan
->name
), &uiter
);
1795 /* If the channel is not found, there is a code flow error */
1796 assert(ua_chan_node
);
1798 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1800 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1812 * Start tracing for a specific UST session and app.
1814 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1817 struct cds_lfht_iter iter
;
1818 struct ust_app_session
*ua_sess
;
1819 struct ust_app_channel
*ua_chan
;
1820 struct ltt_ust_stream
*ustream
;
1823 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1827 ua_sess
= lookup_session_by_app(usess
, app
);
1828 if (ua_sess
== NULL
) {
1829 goto error_rcu_unlock
;
1832 /* Upon restart, we skip the setup, already done */
1833 if (ua_sess
->started
) {
1837 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1839 goto error_rcu_unlock
;
1842 /* For each channel */
1843 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1844 /* Create all streams */
1846 /* Create UST stream */
1847 ustream
= zmalloc(sizeof(*ustream
));
1848 if (ustream
== NULL
) {
1849 PERROR("zmalloc ust stream");
1850 goto error_rcu_unlock
;
1853 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1856 /* Got all streams */
1859 ustream
->handle
= ustream
->obj
->handle
;
1861 /* Order is important */
1862 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1863 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1864 ua_sess
->path
, ua_chan
->name
,
1865 ua_chan
->streams
.count
++);
1867 PERROR("asprintf UST create stream");
1870 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1875 switch (app
->bits_per_long
) {
1877 consumerd_fd
= ust_consumerd64_fd
;
1880 consumerd_fd
= ust_consumerd32_fd
;
1884 goto error_rcu_unlock
;
1887 /* Setup UST consumer socket and send fds to it */
1888 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1890 goto error_rcu_unlock
;
1892 ua_sess
->started
= 1;
1895 /* This start the UST tracing */
1896 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1898 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1899 goto error_rcu_unlock
;
1904 /* Quiescent wait after starting trace */
1905 ustctl_wait_quiescent(app
->key
.sock
);
1915 * Stop tracing for a specific UST session and app.
1917 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1920 struct ust_app_session
*ua_sess
;
1922 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1926 ua_sess
= lookup_session_by_app(usess
, app
);
1927 if (ua_sess
== NULL
) {
1928 /* Only malloc can failed so something is really wrong */
1929 goto error_rcu_unlock
;
1932 #if 0 /* only useful when periodical flush will be supported */
1933 /* need to keep a handle on shm in session for this. */
1934 /* Flush all buffers before stopping */
1935 ret
= ustctl_flush_buffer(usess
->sock
, usess
->metadata
->obj
);
1937 ERR("UST metadata flush failed");
1940 cds_list_for_each_entry(ustchan
, &usess
->channels
.head
, list
) {
1941 ret
= ustctl_flush_buffer(usess
->sock
, ustchan
->obj
);
1943 ERR("UST flush buffer error");
1948 /* This inhibits UST tracing */
1949 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1951 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1952 goto error_rcu_unlock
;
1957 /* Quiescent wait after stopping trace */
1958 ustctl_wait_quiescent(app
->key
.sock
);
1968 * Destroy a specific UST session in apps.
1970 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1972 struct ust_app_session
*ua_sess
;
1973 struct lttng_ust_object_data obj
;
1974 struct cds_lfht_iter iter
;
1975 struct cds_lfht_node
*node
;
1978 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
1982 __lookup_session_by_app(usess
, app
, &iter
);
1983 node
= hashtable_iter_get_node(&iter
);
1985 /* Only malloc can failed so something is really wrong */
1986 goto error_rcu_unlock
;
1988 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
1989 ret
= hashtable_del(app
->sessions
, &iter
);
1991 delete_ust_app_session(app
->key
.sock
, ua_sess
);
1992 obj
.handle
= ua_sess
->handle
;
1995 obj
.memory_map_size
= 0;
1996 ustctl_release_object(app
->key
.sock
, &obj
);
2000 /* Quiescent wait after stopping trace */
2001 ustctl_wait_quiescent(app
->key
.sock
);
2011 * Start tracing for the UST session.
2013 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2016 struct cds_lfht_iter iter
;
2017 struct ust_app
*app
;
2019 DBG("Starting all UST traces");
2023 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2024 ret
= ust_app_start_trace(usess
, app
);
2026 /* Continue to next apps even on error */
2037 * Start tracing for the UST session.
2039 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2042 struct cds_lfht_iter iter
;
2043 struct ust_app
*app
;
2045 DBG("Stopping all UST traces");
2049 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2050 ret
= ust_app_stop_trace(usess
, app
);
2052 /* Continue to next apps even on error */
2063 * Destroy app UST session.
2065 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2068 struct cds_lfht_iter iter
;
2069 struct ust_app
*app
;
2071 DBG("Destroy all UST traces");
2075 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2076 ret
= ust_app_destroy_trace(usess
, app
);
2078 /* Continue to next apps even on error */
2089 * Add channels/events from UST global domain to registered apps at sock.
2091 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2094 struct cds_lfht_iter iter
;
2095 struct ust_app
*app
;
2096 struct ust_app_session
*ua_sess
;
2097 struct ust_app_channel
*ua_chan
;
2098 struct ust_app_event
*ua_event
;
2100 if (usess
== NULL
) {
2101 ERR("No UST session on global update. Returning");
2105 DBG2("UST app global update for app sock %d for session uid %d", sock
,
2110 app
= find_app_by_sock(sock
);
2112 ERR("Failed to update app sock %d", sock
);
2116 ua_sess
= create_ust_app_session(usess
, app
);
2117 if (ua_sess
== NULL
) {
2122 * We can iterate safely here over all UST app session sicne the create ust
2123 * app session above made a shadow copy of the UST global domain from the
2126 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
2127 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2129 /* FIXME: Should we quit here or continue... */
2133 /* For each events */
2134 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
2135 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2137 /* FIXME: Should we quit here or continue... */
2143 if (usess
->start_trace
) {
2144 ret
= ust_app_start_trace(usess
, app
);
2149 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2158 * Add context to a specific channel for global UST domain.
2160 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2161 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2164 struct cds_lfht_node
*ua_chan_node
;
2165 struct cds_lfht_iter iter
, uiter
;
2166 struct ust_app_channel
*ua_chan
= NULL
;
2167 struct ust_app_session
*ua_sess
;
2168 struct ust_app
*app
;
2172 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2173 ua_sess
= lookup_session_by_app(usess
, app
);
2174 if (ua_sess
== NULL
) {
2178 /* Lookup channel in the ust app session */
2179 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
2180 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
2181 if (ua_chan_node
== NULL
) {
2184 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2187 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2193 /* Add ltt UST context node to ltt UST channel */
2194 hashtable_add_unique(uchan
->ctx
, &uctx
->node
);
2201 * Add context to a specific event in a channel for global UST domain.
2203 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2204 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2205 struct ltt_ust_context
*uctx
)
2208 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2209 struct cds_lfht_iter iter
, uiter
;
2210 struct ust_app_session
*ua_sess
;
2211 struct ust_app_event
*ua_event
;
2212 struct ust_app_channel
*ua_chan
= NULL
;
2213 struct ust_app
*app
;
2217 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2218 ua_sess
= lookup_session_by_app(usess
, app
);
2219 if (ua_sess
== NULL
) {
2223 /* Lookup channel in the ust app session */
2224 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
2225 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
2226 if (ua_chan_node
== NULL
) {
2229 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2232 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2233 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
2234 if (ua_event_node
== NULL
) {
2237 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2240 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2246 /* Add ltt UST context node to ltt UST event */
2247 hashtable_add_unique(uevent
->ctx
, &uctx
->node
);