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",
568 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
575 * Create stream onto the UST tracer for a UST session.
577 static int create_ust_stream(struct ust_app
*app
,
578 struct ust_app_session
*ua_sess
)
582 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
583 &ua_sess
->metadata
->stream_obj
);
585 ERR("UST create metadata stream failed");
594 * Create the specified channel onto the UST tracer for a UST session.
596 static int create_ust_channel(struct ust_app
*app
,
597 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
601 /* TODO: remove cast and use lttng-ust-abi.h */
602 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
603 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
605 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
606 "and session handle %d with ret %d",
607 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
608 ua_sess
->handle
, ret
);
612 ua_chan
->handle
= ua_chan
->obj
->handle
;
613 ua_chan
->attr
.shm_fd
= ua_chan
->obj
->shm_fd
;
614 ua_chan
->attr
.wait_fd
= ua_chan
->obj
->wait_fd
;
615 ua_chan
->attr
.memory_map_size
= ua_chan
->obj
->memory_map_size
;
617 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
618 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
620 /* If channel is not enabled, disable it on the tracer */
621 if (!ua_chan
->enabled
) {
622 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
633 * Create the specified event onto the UST tracer for a UST session.
636 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
637 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
641 /* Create UST event on tracer */
642 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
645 ERR("Error ustctl create event %s for app pid: %d with ret %d",
646 ua_event
->attr
.name
, app
->key
.pid
, ret
);
650 ua_event
->handle
= ua_event
->obj
->handle
;
652 DBG2("UST app event %s created successfully for pid:%d",
653 ua_event
->attr
.name
, app
->key
.pid
);
655 /* If event not enabled, disable it on the tracer */
656 if (!ua_event
->enabled
) {
657 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
668 * Copy data between an UST app event and a LTT event.
670 static void shadow_copy_event(struct ust_app_event
*ua_event
,
671 struct ltt_ust_event
*uevent
)
673 struct cds_lfht_iter iter
;
674 struct ltt_ust_context
*uctx
;
675 struct ust_app_ctx
*ua_ctx
;
677 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
678 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
680 /* Copy event attributes */
681 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
683 cds_lfht_for_each_entry(uevent
->ctx
, &iter
, uctx
, node
) {
684 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
685 if (ua_ctx
== NULL
) {
688 hashtable_node_init(&ua_ctx
->node
,
689 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
690 hashtable_add_unique(ua_event
->ctx
, &ua_ctx
->node
);
695 * Copy data between an UST app channel and a LTT channel.
697 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
698 struct ltt_ust_channel
*uchan
)
700 struct cds_lfht_iter iter
;
701 struct cds_lfht_node
*ua_event_node
;
702 struct ltt_ust_event
*uevent
;
703 struct ltt_ust_context
*uctx
;
704 struct ust_app_event
*ua_event
;
705 struct ust_app_ctx
*ua_ctx
;
707 DBG2("Shadow copy of UST app channel %s", ua_chan
->name
);
709 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
710 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
711 /* Copy event attributes */
712 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
714 cds_lfht_for_each_entry(uchan
->ctx
, &iter
, uctx
, node
) {
715 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
716 if (ua_ctx
== NULL
) {
719 hashtable_node_init(&ua_ctx
->node
,
720 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
721 hashtable_add_unique(ua_chan
->ctx
, &ua_ctx
->node
);
724 /* Copy all events from ltt ust channel to ust app channel */
725 cds_lfht_for_each_entry(uchan
->events
, &iter
, uevent
, node
) {
726 struct cds_lfht_iter uiter
;
728 ua_event_node
= hashtable_lookup(ua_chan
->events
,
729 (void *) uevent
->attr
.name
, strlen(uevent
->attr
.name
),
731 if (ua_event_node
== NULL
) {
732 DBG2("UST event %s not found on shadow copy channel",
734 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
735 if (ua_event
== NULL
) {
738 shadow_copy_event(ua_event
, uevent
);
739 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
743 DBG3("Shadow copy channel done");
747 * Copy data between a UST app session and a regular LTT session.
749 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
750 struct ltt_ust_session
*usess
,
753 struct cds_lfht_node
*ua_chan_node
;
754 struct cds_lfht_iter iter
;
755 struct ltt_ust_channel
*uchan
;
756 struct ust_app_channel
*ua_chan
;
762 /* Get date and time for unique app path */
764 timeinfo
= localtime(&rawtime
);
765 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
767 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
769 ua_sess
->uid
= usess
->uid
;
771 ret
= snprintf(ua_sess
->path
, PATH_MAX
,
773 usess
->pathname
, app
->name
, app
->key
.pid
,
776 PERROR("asprintf UST shadow copy session");
777 /* TODO: We cannot return an error from here.. */
781 /* TODO: support all UST domain */
783 /* Iterate over all channels in global domain. */
784 cds_lfht_for_each_entry(usess
->domain_global
.channels
, &iter
,
786 struct cds_lfht_iter uiter
;
788 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
789 (void *)uchan
->name
, strlen(uchan
->name
),
791 if (ua_chan_node
!= NULL
) {
795 DBG2("Channel %s not found on shadow session copy, creating it",
797 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
798 if (ua_chan
== NULL
) {
799 /* malloc failed... continuing */
803 shadow_copy_channel(ua_chan
, uchan
);
804 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
809 * Lookup sesison wrapper.
812 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
813 struct ust_app
*app
, struct cds_lfht_iter
*iter
)
815 /* Get right UST app session from app */
816 (void) hashtable_lookup(app
->sessions
,
817 (void *) ((unsigned long) usess
->uid
), sizeof(void *),
822 * Return ust app session from the app session hashtable using the UST session
825 static struct ust_app_session
*lookup_session_by_app(
826 struct ltt_ust_session
*usess
, struct ust_app
*app
)
828 struct cds_lfht_iter iter
;
829 struct cds_lfht_node
*node
;
831 __lookup_session_by_app(usess
, app
, &iter
);
832 node
= hashtable_iter_get_node(&iter
);
837 return caa_container_of(node
, struct ust_app_session
, node
);
844 * Create a UST session onto the tracer of app and add it the session
847 * Return ust app session or NULL on error.
849 static struct ust_app_session
*create_ust_app_session(
850 struct ltt_ust_session
*usess
, struct ust_app
*app
)
853 struct ust_app_session
*ua_sess
;
855 ua_sess
= lookup_session_by_app(usess
, app
);
856 if (ua_sess
== NULL
) {
857 DBG2("UST app pid: %d session uid %d not found, creating it",
858 app
->key
.pid
, usess
->uid
);
859 ua_sess
= alloc_ust_app_session();
860 if (ua_sess
== NULL
) {
861 /* Only malloc can failed so something is really wrong */
864 shadow_copy_session(ua_sess
, usess
, app
);
867 if (ua_sess
->handle
== -1) {
868 ret
= ustctl_create_session(app
->key
.sock
);
870 ERR("Error creating session for app pid %d, sock %d",
871 app
->key
.pid
, app
->key
.sock
);
872 /* TODO: free() ua_sess */
876 DBG2("UST app ustctl create session handle %d", ret
);
877 ua_sess
->handle
= ret
;
879 /* Add ust app session to app's HT */
880 hashtable_node_init(&ua_sess
->node
,
881 (void *)((unsigned long) ua_sess
->uid
), sizeof(void *));
882 hashtable_add_unique(app
->sessions
, &ua_sess
->node
);
884 DBG2("UST app session created successfully with handle %d", ret
);
894 * Create a context for the channel on the tracer.
897 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
898 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
902 struct cds_lfht_iter iter
;
903 struct cds_lfht_node
*node
;
904 struct ust_app_ctx
*ua_ctx
;
906 DBG2("UST app adding context to channel %s", ua_chan
->name
);
908 node
= hashtable_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
),
909 sizeof(void *), &iter
);
915 ua_ctx
= alloc_ust_app_ctx(uctx
);
916 if (ua_ctx
== NULL
) {
922 hashtable_node_init(&ua_ctx
->node
,
923 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
924 hashtable_add_unique(ua_chan
->ctx
, &ua_ctx
->node
);
926 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
936 * Create an UST context and enable it for the event on the tracer.
939 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
940 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
944 struct cds_lfht_iter iter
;
945 struct cds_lfht_node
*node
;
946 struct ust_app_ctx
*ua_ctx
;
948 DBG2("UST app adding context to event %s", ua_event
->name
);
950 node
= hashtable_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
),
951 sizeof(void *), &iter
);
957 ua_ctx
= alloc_ust_app_ctx(uctx
);
958 if (ua_ctx
== NULL
) {
964 hashtable_node_init(&ua_ctx
->node
,
965 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
966 hashtable_add_unique(ua_event
->ctx
, &ua_ctx
->node
);
968 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
978 * Enable on the tracer side a ust app event for the session and channel.
981 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
982 struct ust_app_event
*ua_event
, struct ust_app
*app
)
986 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
991 ua_event
->enabled
= 1;
998 * Disable on the tracer side a ust app event for the session and channel.
1000 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1001 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1005 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1010 ua_event
->enabled
= 0;
1017 * Lookup ust app channel for session and disable it on the tracer side.
1020 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1021 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1025 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1030 ua_chan
->enabled
= 0;
1037 * Lookup ust app channel for session and enable it on the tracer side.
1039 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1040 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1043 struct cds_lfht_iter iter
;
1044 struct cds_lfht_node
*ua_chan_node
;
1045 struct ust_app_channel
*ua_chan
;
1047 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1048 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1049 if (ua_chan_node
== NULL
) {
1050 DBG2("Unable to find channel %s in ust session uid %u",
1051 uchan
->name
, ua_sess
->uid
);
1055 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1057 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1067 * Create UST app channel and create it on the tracer.
1069 static struct ust_app_channel
*create_ust_app_channel(
1070 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1071 struct ust_app
*app
)
1074 struct cds_lfht_iter iter
;
1075 struct cds_lfht_node
*ua_chan_node
;
1076 struct ust_app_channel
*ua_chan
;
1078 /* Lookup channel in the ust app session */
1079 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1080 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1081 if (ua_chan_node
== NULL
) {
1082 DBG2("Unable to find channel %s in ust session uid %u",
1083 uchan
->name
, ua_sess
->uid
);
1084 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1085 if (ua_chan
== NULL
) {
1088 shadow_copy_channel(ua_chan
, uchan
);
1090 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
1092 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1095 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1107 * Create UST app event and create it on the tracer side.
1110 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1111 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1112 struct ust_app
*app
)
1115 struct cds_lfht_iter iter
;
1116 struct cds_lfht_node
*ua_event_node
;
1117 struct ust_app_event
*ua_event
;
1119 /* Get event node */
1120 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1121 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
1122 if (ua_event_node
!= NULL
) {
1123 ERR("UST app event %s already exist. Stopping creation.",
1128 /* Does not exist so create one */
1129 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1130 if (ua_event
== NULL
) {
1131 /* Only malloc can failed so something is really wrong */
1135 shadow_copy_event(ua_event
, uevent
);
1137 /* Create it on the tracer side */
1138 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1141 delete_ust_app_event(app
->key
.sock
, ua_event
);
1146 ua_event
->enabled
= 1;
1148 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
1150 DBG2("UST app create event %s for PID %d completed",
1151 ua_event
->name
, app
->key
.pid
);
1159 * Create UST metadata and open it on the tracer side.
1161 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1162 char *pathname
, struct ust_app
*app
)
1167 if (ua_sess
->metadata
== NULL
) {
1168 /* Allocate UST metadata */
1169 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1170 if (ua_sess
->metadata
== NULL
) {
1171 ERR("UST app session %d creating metadata failed",
1176 ret
= open_ust_metadata(app
, ua_sess
);
1181 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
1184 /* Open UST metadata stream */
1185 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1186 ret
= create_ust_stream(app
, ua_sess
);
1191 old_umask
= umask(0);
1192 ret
= mkdir(ua_sess
->path
, S_IRWXU
| S_IRWXG
);
1194 PERROR("mkdir UST metadata");
1199 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1200 "%s/metadata", ua_sess
->path
);
1202 PERROR("asprintf UST create stream");
1206 DBG2("UST metadata stream object created for app pid %d",
1209 ERR("Attempting to create stream without metadata opened");
1220 * Return pointer to traceable apps list.
1222 struct cds_lfht
*ust_app_get_ht(void)
1228 * Return ust app pointer or NULL if not found.
1230 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1232 struct cds_lfht_node
*node
;
1233 struct cds_lfht_iter iter
;
1236 node
= hashtable_lookup(ust_app_ht
,
1237 (void *)((unsigned long) pid
), sizeof(void *), &iter
);
1239 DBG2("UST app no found with pid %d", pid
);
1244 DBG2("Found UST app by pid %d", pid
);
1246 return caa_container_of(node
, struct ust_app
, node
);
1254 * Using pid and uid (of the app), allocate a new ust_app struct and
1255 * add it to the global traceable app list.
1257 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1258 * bitness is not supported.
1260 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1262 struct ust_app
*lta
;
1264 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1265 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1266 ERR("Registration failed: application \"%s\" (pid: %d) has "
1267 "%d-bit long, but no consumerd for this long size is available.\n",
1268 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1272 lta
= zmalloc(sizeof(struct ust_app
));
1278 lta
->ppid
= msg
->ppid
;
1279 lta
->uid
= msg
->uid
;
1280 lta
->gid
= msg
->gid
;
1281 lta
->bits_per_long
= msg
->bits_per_long
;
1282 lta
->v_major
= msg
->major
;
1283 lta
->v_minor
= msg
->minor
;
1284 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1285 lta
->name
[16] = '\0';
1286 lta
->sessions
= hashtable_new(0);
1289 lta
->key
.pid
= msg
->pid
;
1290 hashtable_node_init(<a
->node
, (void *)((unsigned long)lta
->key
.pid
),
1292 lta
->key
.sock
= sock
;
1293 hashtable_node_init(<a
->key
.node
, (void *)((unsigned long)lta
->key
.sock
),
1297 hashtable_add_unique(ust_app_sock_key_map
, <a
->key
.node
);
1298 hashtable_add_unique(ust_app_ht
, <a
->node
);
1301 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1302 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1303 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1309 * Unregister app by removing it from the global traceable app list and freeing
1312 * The socket is already closed at this point so no close to sock.
1314 void ust_app_unregister(int sock
)
1316 struct ust_app
*lta
;
1317 struct cds_lfht_node
*node
;
1318 struct cds_lfht_iter iter
;
1322 lta
= find_app_by_sock(sock
);
1324 ERR("Unregister app sock %d not found!", sock
);
1328 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1330 /* Get the node reference for a call_rcu */
1331 node
= hashtable_lookup(ust_app_ht
,
1332 (void *)((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
1334 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1338 ret
= hashtable_del(ust_app_ht
, &iter
);
1340 call_rcu(&node
->head
, delete_ust_app_rcu
);
1347 * Return traceable_app_count
1349 unsigned long ust_app_list_count(void)
1351 unsigned long count
;
1354 count
= hashtable_get_count(ust_app_ht
);
1361 * Fill events array with all events name of all registered apps.
1363 int ust_app_list_events(struct lttng_event
**events
)
1366 size_t nbmem
, count
= 0;
1367 struct cds_lfht_iter iter
;
1368 struct ust_app
*app
;
1369 struct lttng_event
*tmp
;
1371 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1372 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1374 PERROR("zmalloc ust app events");
1381 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1382 struct lttng_ust_tracepoint_iter iter
;
1384 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1386 ERR("UST app list events getting handle failed for app pid %d",
1391 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1392 &iter
)) != -ENOENT
) {
1393 if (count
>= nbmem
) {
1394 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1397 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1399 PERROR("realloc ust app events");
1404 memcpy(tmp
[count
].name
, iter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1405 memcpy(tmp
[count
].loglevel
, iter
.loglevel
, LTTNG_UST_SYM_NAME_LEN
);
1406 tmp
[count
].loglevel_value
= iter
.loglevel_value
;
1407 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1408 tmp
[count
].pid
= app
->key
.pid
;
1409 tmp
[count
].enabled
= -1;
1417 DBG2("UST app list events done (%zu events)", count
);
1426 * Free and clean all traceable apps of the global list.
1428 void ust_app_clean_list(void)
1431 struct cds_lfht_iter iter
;
1432 struct ust_app
*app
;
1434 DBG2("UST app cleaning registered apps hash table");
1438 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1439 ret
= hashtable_del(ust_app_ht
, &iter
);
1441 call_rcu(&iter
.node
->head
, delete_ust_app_rcu
);
1444 hashtable_destroy(ust_app_ht
);
1445 hashtable_destroy(ust_app_sock_key_map
);
1451 * Init UST app hash table.
1453 void ust_app_ht_alloc(void)
1455 ust_app_ht
= hashtable_new(0);
1456 ust_app_sock_key_map
= hashtable_new(0);
1460 * For a specific UST session, disable the channel for all registered apps.
1462 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1463 struct ltt_ust_channel
*uchan
)
1466 struct cds_lfht_iter iter
;
1467 struct cds_lfht_node
*ua_chan_node
;
1468 struct ust_app
*app
;
1469 struct ust_app_session
*ua_sess
;
1470 struct ust_app_channel
*ua_chan
;
1472 if (usess
== NULL
|| uchan
== NULL
) {
1473 ERR("Disabling UST global channel with NULL values");
1478 DBG2("UST app disabling channel %s from global domain for session uid %d",
1479 uchan
->name
, usess
->uid
);
1483 /* For every registered applications */
1484 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1485 ua_sess
= lookup_session_by_app(usess
, app
);
1486 if (ua_sess
== NULL
) {
1491 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1492 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1493 /* If the session if found for the app, the channel must be there */
1494 assert(ua_chan_node
);
1496 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1497 /* The channel must not be already disabled */
1498 assert(ua_chan
->enabled
== 1);
1500 /* Disable channel onto application */
1501 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1503 /* XXX: We might want to report this error at some point... */
1515 * For a specific UST session, enable the channel for all registered apps.
1517 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1518 struct ltt_ust_channel
*uchan
)
1521 struct cds_lfht_iter iter
;
1522 struct ust_app
*app
;
1523 struct ust_app_session
*ua_sess
;
1525 if (usess
== NULL
|| uchan
== NULL
) {
1526 ERR("Adding UST global channel to NULL values");
1531 DBG2("UST app enabling channel %s to global domain for session uid %d",
1532 uchan
->name
, usess
->uid
);
1536 /* For every registered applications */
1537 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1538 ua_sess
= lookup_session_by_app(usess
, app
);
1539 if (ua_sess
== NULL
) {
1543 /* Enable channel onto application */
1544 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1546 /* XXX: We might want to report this error at some point... */
1558 * Disable an event in a channel and for a specific session.
1560 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1561 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1564 struct cds_lfht_iter iter
, uiter
;
1565 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
1566 struct ust_app
*app
;
1567 struct ust_app_session
*ua_sess
;
1568 struct ust_app_channel
*ua_chan
;
1569 struct ust_app_event
*ua_event
;
1571 DBG("UST app disabling event %s for all apps in channel "
1572 "%s for session uid %d", uevent
->attr
.name
, uchan
->name
, usess
->uid
);
1576 /* For all registered applications */
1577 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1578 ua_sess
= lookup_session_by_app(usess
, app
);
1579 if (ua_sess
== NULL
) {
1584 /* Lookup channel in the ust app session */
1585 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1586 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
1587 if (ua_chan_node
== NULL
) {
1588 DBG2("Channel %s not found in session uid %d for app pid %d."
1589 "Skipping", uchan
->name
, usess
->uid
, app
->key
.pid
);
1592 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1594 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1595 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
1596 if (ua_event_node
== NULL
) {
1597 DBG2("Event %s not found in channel %s for app pid %d."
1598 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1601 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1603 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1605 /* XXX: Report error someday... */
1616 * For a specific UST session and UST channel, the event for all
1619 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1620 struct ltt_ust_channel
*uchan
)
1623 struct cds_lfht_iter iter
, uiter
;
1624 struct cds_lfht_node
*ua_chan_node
;
1625 struct ust_app
*app
;
1626 struct ust_app_session
*ua_sess
;
1627 struct ust_app_channel
*ua_chan
;
1628 struct ust_app_event
*ua_event
;
1630 DBG("UST app disabling all event for all apps in channel "
1631 "%s for session uid %d", uchan
->name
, usess
->uid
);
1635 /* For all registered applications */
1636 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1637 ua_sess
= lookup_session_by_app(usess
, app
);
1638 /* If ua_sess is NULL, there is a code flow error */
1641 /* Lookup channel in the ust app session */
1642 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1643 strlen(uchan
->name
), &uiter
);
1644 /* If the channel is not found, there is a code flow error */
1645 assert(ua_chan_node
);
1647 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1649 /* Disable each events of channel */
1650 cds_lfht_for_each_entry(ua_chan
->events
, &uiter
, ua_event
, node
) {
1651 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1653 /* XXX: Report error someday... */
1665 * For a specific UST session, create the channel for all registered apps.
1667 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1668 struct ltt_ust_channel
*uchan
)
1671 struct cds_lfht_iter iter
;
1672 struct ust_app
*app
;
1673 struct ust_app_session
*ua_sess
;
1674 struct ust_app_channel
*ua_chan
;
1676 if (usess
== NULL
|| uchan
== NULL
) {
1677 ERR("Adding UST global channel to NULL values");
1682 DBG2("UST app adding channel %s to global domain for session uid %d",
1683 uchan
->name
, usess
->uid
);
1687 /* For every registered applications */
1688 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1690 * Create session on the tracer side and add it to app session HT. Note
1691 * that if session exist, it will simply return a pointer to the ust
1694 ua_sess
= create_ust_app_session(usess
, app
);
1695 if (ua_sess
== NULL
) {
1699 /* Create channel onto application */
1700 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1701 if (ua_chan
== NULL
) {
1713 * Enable event for a specific session and channel on the tracer.
1715 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1716 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1719 struct cds_lfht_iter iter
, uiter
;
1720 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
1721 struct ust_app
*app
;
1722 struct ust_app_session
*ua_sess
;
1723 struct ust_app_channel
*ua_chan
;
1724 struct ust_app_event
*ua_event
;
1726 DBG("UST app enabling event %s for all apps for session uid %d",
1727 uevent
->attr
.name
, usess
->uid
);
1730 * NOTE: At this point, this function is called only if the session and
1731 * channel passed are already created for all apps. and enabled on the
1737 /* For all registered applications */
1738 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1739 ua_sess
= lookup_session_by_app(usess
, app
);
1740 /* If ua_sess is NULL, there is a code flow error */
1743 /* Lookup channel in the ust app session */
1744 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1745 strlen(uchan
->name
), &uiter
);
1746 /* If the channel is not found, there is a code flow error */
1747 assert(ua_chan_node
);
1749 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1751 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1752 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
1753 if (ua_event_node
== NULL
) {
1754 DBG3("UST app enable event %s not found for app PID %d."
1755 "Skipping app", uevent
->attr
.name
, app
->key
.pid
);
1758 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1760 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1772 * For a specific existing UST session and UST channel, creates the event for
1773 * all registered apps.
1775 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1776 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1779 struct cds_lfht_iter iter
, uiter
;
1780 struct cds_lfht_node
*ua_chan_node
;
1781 struct ust_app
*app
;
1782 struct ust_app_session
*ua_sess
;
1783 struct ust_app_channel
*ua_chan
;
1785 DBG("UST app creating event %s for all apps for session uid %d",
1786 uevent
->attr
.name
, usess
->uid
);
1789 * NOTE: At this point, this function is called only if the session and
1790 * channel passed are already created for all apps. and enabled on the
1796 /* For all registered applications */
1797 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1798 ua_sess
= lookup_session_by_app(usess
, app
);
1799 /* If ua_sess is NULL, there is a code flow error */
1802 /* Lookup channel in the ust app session */
1803 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1804 strlen(uchan
->name
), &uiter
);
1805 /* If the channel is not found, there is a code flow error */
1806 assert(ua_chan_node
);
1808 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1810 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1822 * Start tracing for a specific UST session and app.
1824 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1827 struct cds_lfht_iter iter
;
1828 struct ust_app_session
*ua_sess
;
1829 struct ust_app_channel
*ua_chan
;
1830 struct ltt_ust_stream
*ustream
;
1833 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1837 ua_sess
= lookup_session_by_app(usess
, app
);
1838 if (ua_sess
== NULL
) {
1839 goto error_rcu_unlock
;
1842 /* Upon restart, we skip the setup, already done */
1843 if (ua_sess
->started
) {
1847 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1849 goto error_rcu_unlock
;
1852 /* For each channel */
1853 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1854 /* Create all streams */
1856 /* Create UST stream */
1857 ustream
= zmalloc(sizeof(*ustream
));
1858 if (ustream
== NULL
) {
1859 PERROR("zmalloc ust stream");
1860 goto error_rcu_unlock
;
1863 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1866 /* Got all streams */
1869 ustream
->handle
= ustream
->obj
->handle
;
1871 /* Order is important */
1872 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1873 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1874 ua_sess
->path
, ua_chan
->name
,
1875 ua_chan
->streams
.count
++);
1877 PERROR("asprintf UST create stream");
1880 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1885 switch (app
->bits_per_long
) {
1887 consumerd_fd
= ust_consumerd64_fd
;
1890 consumerd_fd
= ust_consumerd32_fd
;
1894 goto error_rcu_unlock
;
1897 /* Setup UST consumer socket and send fds to it */
1898 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1900 goto error_rcu_unlock
;
1902 ua_sess
->started
= 1;
1905 /* This start the UST tracing */
1906 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1908 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1909 goto error_rcu_unlock
;
1914 /* Quiescent wait after starting trace */
1915 ustctl_wait_quiescent(app
->key
.sock
);
1925 * Stop tracing for a specific UST session and app.
1927 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1930 struct cds_lfht_iter iter
;
1931 struct ust_app_session
*ua_sess
;
1932 struct ust_app_channel
*ua_chan
;
1934 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1938 ua_sess
= lookup_session_by_app(usess
, app
);
1939 if (ua_sess
== NULL
) {
1940 /* Only malloc can failed so something is really wrong */
1941 goto error_rcu_unlock
;
1944 /* Flush all buffers before stopping */
1945 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_sess
->metadata
->obj
);
1947 ERR("UST app PID %d metadata flush failed", app
->key
.pid
);
1948 ERR("Ended with ret %d", ret
);
1951 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1952 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_chan
->obj
);
1954 ERR("UST app PID %d channel %s flush failed",
1955 app
->key
.pid
, ua_chan
->name
);
1956 ERR("Ended with ret %d", ret
);
1957 /* Continuing flushing all buffers */
1962 /* This inhibits UST tracing */
1963 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1965 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1966 goto error_rcu_unlock
;
1971 /* Quiescent wait after stopping trace */
1972 ustctl_wait_quiescent(app
->key
.sock
);
1982 * Destroy a specific UST session in apps.
1984 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1986 struct ust_app_session
*ua_sess
;
1987 struct lttng_ust_object_data obj
;
1988 struct cds_lfht_iter iter
;
1989 struct cds_lfht_node
*node
;
1992 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
1996 __lookup_session_by_app(usess
, app
, &iter
);
1997 node
= hashtable_iter_get_node(&iter
);
1999 /* Only malloc can failed so something is really wrong */
2000 goto error_rcu_unlock
;
2002 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2003 ret
= hashtable_del(app
->sessions
, &iter
);
2005 delete_ust_app_session(app
->key
.sock
, ua_sess
);
2006 obj
.handle
= ua_sess
->handle
;
2009 obj
.memory_map_size
= 0;
2010 ustctl_release_object(app
->key
.sock
, &obj
);
2014 /* Quiescent wait after stopping trace */
2015 ustctl_wait_quiescent(app
->key
.sock
);
2025 * Start tracing for the UST session.
2027 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2030 struct cds_lfht_iter iter
;
2031 struct ust_app
*app
;
2033 DBG("Starting all UST traces");
2037 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2038 ret
= ust_app_start_trace(usess
, app
);
2040 /* Continue to next apps even on error */
2051 * Start tracing for the UST session.
2053 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2056 struct cds_lfht_iter iter
;
2057 struct ust_app
*app
;
2059 DBG("Stopping all UST traces");
2063 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2064 ret
= ust_app_stop_trace(usess
, app
);
2066 /* Continue to next apps even on error */
2077 * Destroy app UST session.
2079 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2082 struct cds_lfht_iter iter
;
2083 struct ust_app
*app
;
2085 DBG("Destroy all UST traces");
2089 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2090 ret
= ust_app_destroy_trace(usess
, app
);
2092 /* Continue to next apps even on error */
2103 * Add channels/events from UST global domain to registered apps at sock.
2105 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2108 struct cds_lfht_iter iter
;
2109 struct ust_app
*app
;
2110 struct ust_app_session
*ua_sess
;
2111 struct ust_app_channel
*ua_chan
;
2112 struct ust_app_event
*ua_event
;
2114 if (usess
== NULL
) {
2115 ERR("No UST session on global update. Returning");
2119 DBG2("UST app global update for app sock %d for session uid %d", sock
,
2124 app
= find_app_by_sock(sock
);
2126 ERR("Failed to update app sock %d", sock
);
2130 ua_sess
= create_ust_app_session(usess
, app
);
2131 if (ua_sess
== NULL
) {
2136 * We can iterate safely here over all UST app session sicne the create ust
2137 * app session above made a shadow copy of the UST global domain from the
2140 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
2141 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2143 /* FIXME: Should we quit here or continue... */
2147 /* For each events */
2148 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
2149 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2151 /* FIXME: Should we quit here or continue... */
2157 if (usess
->start_trace
) {
2158 ret
= ust_app_start_trace(usess
, app
);
2163 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2172 * Add context to a specific channel for global UST domain.
2174 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2175 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2178 struct cds_lfht_node
*ua_chan_node
;
2179 struct cds_lfht_iter iter
, uiter
;
2180 struct ust_app_channel
*ua_chan
= NULL
;
2181 struct ust_app_session
*ua_sess
;
2182 struct ust_app
*app
;
2186 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2187 ua_sess
= lookup_session_by_app(usess
, app
);
2188 if (ua_sess
== NULL
) {
2192 /* Lookup channel in the ust app session */
2193 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
2194 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
2195 if (ua_chan_node
== NULL
) {
2198 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2201 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2212 * Add context to a specific event in a channel for global UST domain.
2214 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2215 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2216 struct ltt_ust_context
*uctx
)
2219 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2220 struct cds_lfht_iter iter
, uiter
;
2221 struct ust_app_session
*ua_sess
;
2222 struct ust_app_event
*ua_event
;
2223 struct ust_app_channel
*ua_chan
= NULL
;
2224 struct ust_app
*app
;
2228 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2229 ua_sess
= lookup_session_by_app(usess
, app
);
2230 if (ua_sess
== NULL
) {
2234 /* Lookup channel in the ust app session */
2235 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
2236 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
2237 if (ua_chan_node
== NULL
) {
2240 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2243 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2244 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
2245 if (ua_event_node
== NULL
) {
2248 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2251 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2262 * Enable event for a channel from a UST session for a specific PID.
2264 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2265 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2268 struct cds_lfht_iter iter
;
2269 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2270 struct ust_app
*app
;
2271 struct ust_app_session
*ua_sess
;
2272 struct ust_app_channel
*ua_chan
;
2273 struct ust_app_event
*ua_event
;
2275 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2279 app
= ust_app_find_by_pid(pid
);
2281 ERR("UST app enable event per PID %d not found", pid
);
2286 ua_sess
= lookup_session_by_app(usess
, app
);
2287 /* If ua_sess is NULL, there is a code flow error */
2290 /* Lookup channel in the ust app session */
2291 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
2292 strlen(uchan
->name
), &iter
);
2293 /* If the channel is not found, there is a code flow error */
2294 assert(ua_chan_node
);
2296 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2298 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2299 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
2300 if (ua_event_node
== NULL
) {
2301 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2306 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2308 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2320 * Disable event for a channel from a UST session for a specific PID.
2322 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2323 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2326 struct cds_lfht_iter iter
;
2327 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2328 struct ust_app
*app
;
2329 struct ust_app_session
*ua_sess
;
2330 struct ust_app_channel
*ua_chan
;
2331 struct ust_app_event
*ua_event
;
2333 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2337 app
= ust_app_find_by_pid(pid
);
2339 ERR("UST app disable event per PID %d not found", pid
);
2344 ua_sess
= lookup_session_by_app(usess
, app
);
2345 /* If ua_sess is NULL, there is a code flow error */
2348 /* Lookup channel in the ust app session */
2349 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
2350 strlen(uchan
->name
), &iter
);
2351 if (ua_chan_node
== NULL
) {
2352 /* Channel does not exist, skip disabling */
2355 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2357 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2358 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
2359 if (ua_event_node
== NULL
) {
2360 /* Event does not exist, skip disabling */
2363 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2365 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);