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
56 static void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
59 struct cds_lfht_iter iter
;
60 struct ust_app_ctx
*ua_ctx
;
62 cds_lfht_for_each_entry(ua_event
->ctx
, &iter
, ua_ctx
, node
) {
63 ret
= hashtable_del(ua_event
->ctx
, &iter
);
65 delete_ust_app_ctx(sock
, ua_ctx
);
67 ret
= hashtable_destroy(ua_event
->ctx
);
70 if (ua_event
->obj
!= NULL
) {
71 ustctl_release_object(sock
, ua_event
->obj
);
78 * Delete ust app stream safely. RCU read lock must be held before calling
81 static void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
83 ustctl_release_object(sock
, stream
->obj
);
89 * Delete ust app channel safely. RCU read lock must be held before calling
92 static void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
95 struct cds_lfht_iter iter
;
96 struct ust_app_event
*ua_event
;
97 struct ust_app_ctx
*ua_ctx
;
98 struct ltt_ust_stream
*stream
, *stmp
;
101 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
102 cds_list_del(&stream
->list
);
103 delete_ust_app_stream(sock
, stream
);
107 cds_lfht_for_each_entry(ua_chan
->ctx
, &iter
, ua_ctx
, node
) {
108 ret
= hashtable_del(ua_chan
->ctx
, &iter
);
110 delete_ust_app_ctx(sock
, ua_ctx
);
112 ret
= hashtable_destroy(ua_chan
->ctx
);
116 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
117 ret
= hashtable_del(ua_chan
->events
, &iter
);
119 delete_ust_app_event(sock
, ua_event
);
121 ret
= hashtable_destroy(ua_chan
->events
);
124 if (ua_chan
->obj
!= NULL
) {
125 ustctl_release_object(sock
, ua_chan
->obj
);
132 * Delete ust app session safely. RCU read lock must be held before calling
135 static void delete_ust_app_session(int sock
,
136 struct ust_app_session
*ua_sess
)
139 struct cds_lfht_iter iter
;
140 struct ust_app_channel
*ua_chan
;
142 if (ua_sess
->metadata
) {
143 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
144 free(ua_sess
->metadata
->stream_obj
);
145 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
146 free(ua_sess
->metadata
->obj
);
149 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
150 ret
= hashtable_del(ua_sess
->channels
, &iter
);
152 delete_ust_app_channel(sock
, ua_chan
);
155 ret
= hashtable_destroy(ua_sess
->channels
);
157 ERR("UST app destroy session hashtable failed");
166 * Delete a traceable application structure from the global list. Never call
167 * this function outside of a call_rcu call.
169 static void delete_ust_app(struct ust_app
*app
)
172 struct cds_lfht_node
*node
;
173 struct cds_lfht_iter iter
;
174 struct ust_app_session
*ua_sess
;
179 /* Remove from key hash table */
180 node
= hashtable_lookup(ust_app_sock_key_map
,
181 (void *) ((unsigned long) app
->key
.sock
), sizeof(void *), &iter
);
183 /* Not suppose to happen */
184 ERR("UST app key %d not found in key hash table", app
->key
.sock
);
188 ret
= hashtable_del(ust_app_sock_key_map
, &iter
);
190 ERR("UST app unable to delete app sock %d from key hash table",
193 DBG2("UST app pair sock %d key %d deleted",
194 app
->key
.sock
, app
->key
.pid
);
197 /* Socket is already closed at this point */
199 /* Delete ust app sessions info */
200 sock
= app
->key
.sock
;
203 cds_lfht_for_each_entry(app
->sessions
, &iter
, ua_sess
, node
) {
204 ret
= hashtable_del(app
->sessions
, &iter
);
206 delete_ust_app_session(app
->key
.sock
, ua_sess
);
209 ret
= hashtable_destroy(app
->sessions
);
211 ERR("UST app destroy session hashtable failed");
216 * Wait until we have removed the key from the sock hash table
217 * before closing this socket, otherwise an application could
218 * re-use the socket ID and race with the teardown, using the
219 * same hash table entry.
223 DBG2("UST app pid %d deleted", app
->key
.pid
);
230 * URCU intermediate call to delete an UST app.
232 static void delete_ust_app_rcu(struct rcu_head
*head
)
234 struct cds_lfht_node
*node
=
235 caa_container_of(head
, struct cds_lfht_node
, head
);
236 struct ust_app
*app
=
237 caa_container_of(node
, struct ust_app
, node
);
243 * Find an ust_app using the sock and return it. RCU read side lock must be
244 * held before calling this helper function.
246 static struct ust_app
*find_app_by_sock(int sock
)
248 struct cds_lfht_node
*node
;
249 struct ust_app_key
*key
;
250 struct cds_lfht_iter iter
;
252 node
= hashtable_lookup(ust_app_sock_key_map
,
253 (void *)((unsigned long) sock
), sizeof(void *), &iter
);
255 DBG2("UST app find by sock %d key not found", sock
);
259 key
= caa_container_of(node
, struct ust_app_key
, node
);
261 node
= hashtable_lookup(ust_app_ht
,
262 (void *)((unsigned long) key
->pid
), sizeof(void *), &iter
);
264 DBG2("UST app find by sock %d not found", sock
);
267 return caa_container_of(node
, struct ust_app
, node
);
274 * Create the channel context on the tracer.
277 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
278 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
282 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
283 ua_chan
->obj
, &ua_ctx
->obj
);
288 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
290 DBG2("UST app context added to channel %s successfully", ua_chan
->name
);
297 * Create the event context on the tracer.
300 int create_ust_event_context(struct ust_app_event
*ua_event
,
301 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
305 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
306 ua_event
->obj
, &ua_ctx
->obj
);
311 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
313 DBG2("UST app context added to event %s successfully", ua_event
->name
);
320 * Disable the specified event on to UST tracer for the UST session.
322 static int disable_ust_event(struct ust_app
*app
,
323 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
327 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
329 ERR("UST app event %s disable failed for app (pid: %d) "
330 "and session handle %d with ret %d",
331 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
335 DBG2("UST app event %s disabled successfully for app (pid: %d)",
336 ua_event
->attr
.name
, app
->key
.pid
);
343 * Disable the specified channel on to UST tracer for the UST session.
345 static int disable_ust_channel(struct ust_app
*app
,
346 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
350 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
352 ERR("UST app channel %s disable failed for app (pid: %d) "
353 "and session handle %d with ret %d",
354 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
358 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
359 ua_chan
->name
, app
->key
.pid
);
366 * Enable the specified channel on to UST tracer for the UST session.
368 static int enable_ust_channel(struct ust_app
*app
,
369 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
373 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
375 ERR("UST app channel %s enable failed for app (pid: %d) "
376 "and session handle %d with ret %d",
377 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
381 ua_chan
->enabled
= 1;
383 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
384 ua_chan
->name
, app
->key
.pid
);
391 * Enable the specified event on to UST tracer for the UST session.
393 static int enable_ust_event(struct ust_app
*app
,
394 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
398 ret
= ustctl_enable(app
->key
.sock
, ua_event
->obj
);
400 ERR("UST app event %s enable failed for app (pid: %d) "
401 "and session handle %d with ret %d",
402 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
406 DBG2("UST app event %s enabled successfully for app (pid: %d)",
407 ua_event
->attr
.name
, app
->key
.pid
);
414 * Open metadata onto the UST tracer for a UST session.
416 static int open_ust_metadata(struct ust_app
*app
,
417 struct ust_app_session
*ua_sess
)
420 struct lttng_ust_channel_attr uattr
;
422 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
423 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
424 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
425 uattr
.switch_timer_interval
=
426 ua_sess
->metadata
->attr
.switch_timer_interval
;
427 uattr
.read_timer_interval
=
428 ua_sess
->metadata
->attr
.read_timer_interval
;
429 uattr
.output
= ua_sess
->metadata
->attr
.output
;
431 /* UST tracer metadata creation */
432 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
433 &ua_sess
->metadata
->obj
);
435 ERR("UST app open metadata failed for app pid:%d",
445 * Create stream onto the UST tracer for a UST session.
447 static int create_ust_stream(struct ust_app
*app
,
448 struct ust_app_session
*ua_sess
)
452 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
453 &ua_sess
->metadata
->stream_obj
);
455 ERR("UST create metadata stream failed");
464 * Create the specified channel onto the UST tracer for a UST session.
466 static int create_ust_channel(struct ust_app
*app
,
467 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
471 /* TODO: remove cast and use lttng-ust-abi.h */
472 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
473 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
475 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
476 "and session handle %d with ret %d",
477 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
478 ua_sess
->handle
, ret
);
482 ua_chan
->handle
= ua_chan
->obj
->handle
;
483 ua_chan
->attr
.shm_fd
= ua_chan
->obj
->shm_fd
;
484 ua_chan
->attr
.wait_fd
= ua_chan
->obj
->wait_fd
;
485 ua_chan
->attr
.memory_map_size
= ua_chan
->obj
->memory_map_size
;
487 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
488 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
490 /* If channel is not enabled, disable it on the tracer */
491 if (!ua_chan
->enabled
) {
492 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
503 * Create the specified event onto the UST tracer for a UST session.
506 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
507 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
511 /* Create UST event on tracer */
512 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
515 ERR("Error ustctl create event %s for app pid: %d with ret %d",
516 ua_event
->attr
.name
, app
->key
.pid
, ret
);
520 ua_event
->handle
= ua_event
->obj
->handle
;
522 DBG2("UST app event %s created successfully for pid:%d",
523 ua_event
->attr
.name
, app
->key
.pid
);
525 /* If event not enabled, disable it on the tracer */
526 if (!ua_event
->enabled
) {
527 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
538 * Alloc new UST app session.
540 static struct ust_app_session
*alloc_ust_app_session(void)
542 struct ust_app_session
*ua_sess
;
544 /* Init most of the default value by allocating and zeroing */
545 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
546 if (ua_sess
== NULL
) {
551 ua_sess
->handle
= -1;
552 ua_sess
->channels
= hashtable_new_str(0);
561 * Alloc new UST app channel.
563 static struct ust_app_channel
*alloc_ust_app_channel(char *name
,
564 struct lttng_ust_channel
*attr
)
566 struct ust_app_channel
*ua_chan
;
568 /* Init most of the default value by allocating and zeroing */
569 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
570 if (ua_chan
== NULL
) {
575 /* Setup channel name */
576 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
577 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
579 ua_chan
->enabled
= 1;
580 ua_chan
->handle
= -1;
581 ua_chan
->ctx
= hashtable_new(0);
582 ua_chan
->events
= hashtable_new_str(0);
583 hashtable_node_init(&ua_chan
->node
, (void *) ua_chan
->name
,
584 strlen(ua_chan
->name
));
586 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
588 /* Copy attributes */
590 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
593 DBG3("UST app channel %s allocated", ua_chan
->name
);
602 * Alloc new UST app event.
604 static struct ust_app_event
*alloc_ust_app_event(char *name
,
605 struct lttng_ust_event
*attr
)
607 struct ust_app_event
*ua_event
;
609 /* Init most of the default value by allocating and zeroing */
610 ua_event
= zmalloc(sizeof(struct ust_app_event
));
611 if (ua_event
== NULL
) {
616 ua_event
->enabled
= 1;
617 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
618 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
619 ua_event
->ctx
= hashtable_new(0);
620 hashtable_node_init(&ua_event
->node
, (void *) ua_event
->name
,
621 strlen(ua_event
->name
));
623 /* Copy attributes */
625 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
628 DBG3("UST app event %s allocated", ua_event
->name
);
637 * Alloc new UST app context.
640 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
642 struct ust_app_ctx
*ua_ctx
;
644 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
645 if (ua_ctx
== NULL
) {
650 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
653 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
660 * Copy data between an UST app event and a LTT event.
662 static void shadow_copy_event(struct ust_app_event
*ua_event
,
663 struct ltt_ust_event
*uevent
)
665 struct cds_lfht_iter iter
;
666 struct ltt_ust_context
*uctx
;
667 struct ust_app_ctx
*ua_ctx
;
669 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
670 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
672 /* Copy event attributes */
673 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
675 cds_lfht_for_each_entry(uevent
->ctx
, &iter
, uctx
, node
) {
676 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
677 if (ua_ctx
== NULL
) {
680 hashtable_node_init(&ua_ctx
->node
,
681 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
682 hashtable_add_unique(ua_event
->ctx
, &ua_ctx
->node
);
687 * Copy data between an UST app channel and a LTT channel.
689 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
690 struct ltt_ust_channel
*uchan
)
692 struct cds_lfht_iter iter
;
693 struct cds_lfht_node
*ua_event_node
;
694 struct ltt_ust_event
*uevent
;
695 struct ltt_ust_context
*uctx
;
696 struct ust_app_event
*ua_event
;
697 struct ust_app_ctx
*ua_ctx
;
699 DBG2("Shadow copy of UST app channel %s", ua_chan
->name
);
701 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
702 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
703 /* Copy event attributes */
704 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
706 cds_lfht_for_each_entry(uchan
->ctx
, &iter
, uctx
, node
) {
707 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
708 if (ua_ctx
== NULL
) {
711 hashtable_node_init(&ua_ctx
->node
,
712 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
713 hashtable_add_unique(ua_chan
->ctx
, &ua_ctx
->node
);
716 /* Copy all events from ltt ust channel to ust app channel */
717 cds_lfht_for_each_entry(uchan
->events
, &iter
, uevent
, node
) {
718 struct cds_lfht_iter uiter
;
720 ua_event_node
= hashtable_lookup(ua_chan
->events
,
721 (void *) uevent
->attr
.name
, strlen(uevent
->attr
.name
),
723 if (ua_event_node
== NULL
) {
724 DBG2("UST event %s not found on shadow copy channel",
726 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
727 if (ua_event
== NULL
) {
730 shadow_copy_event(ua_event
, uevent
);
731 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
735 DBG3("Shadow copy channel done");
739 * Copy data between a UST app session and a regular LTT session.
741 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
742 struct ltt_ust_session
*usess
,
745 struct cds_lfht_node
*ua_chan_node
;
746 struct cds_lfht_iter iter
;
747 struct ltt_ust_channel
*uchan
;
748 struct ust_app_channel
*ua_chan
;
754 /* Get date and time for unique app path */
756 timeinfo
= localtime(&rawtime
);
757 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
759 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
761 ua_sess
->uid
= usess
->uid
;
763 ret
= snprintf(ua_sess
->path
, PATH_MAX
,
765 usess
->pathname
, app
->name
, app
->key
.pid
,
768 PERROR("asprintf UST shadow copy session");
769 /* TODO: We cannot return an error from here.. */
773 /* TODO: support all UST domain */
775 /* Iterate over all channels in global domain. */
776 cds_lfht_for_each_entry(usess
->domain_global
.channels
, &iter
,
778 struct cds_lfht_iter uiter
;
780 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
781 (void *)uchan
->name
, strlen(uchan
->name
),
783 if (ua_chan_node
!= NULL
) {
787 DBG2("Channel %s not found on shadow session copy, creating it",
789 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
790 if (ua_chan
== NULL
) {
791 /* malloc failed... continuing */
795 shadow_copy_channel(ua_chan
, uchan
);
796 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
801 * Lookup sesison wrapper.
804 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
805 struct ust_app
*app
, struct cds_lfht_iter
*iter
)
807 /* Get right UST app session from app */
808 (void) hashtable_lookup(app
->sessions
,
809 (void *) ((unsigned long) usess
->uid
), sizeof(void *),
814 * Return ust app session from the app session hashtable using the UST session
817 static struct ust_app_session
*lookup_session_by_app(
818 struct ltt_ust_session
*usess
, struct ust_app
*app
)
820 struct cds_lfht_iter iter
;
821 struct cds_lfht_node
*node
;
823 __lookup_session_by_app(usess
, app
, &iter
);
824 node
= hashtable_iter_get_node(&iter
);
829 return caa_container_of(node
, struct ust_app_session
, node
);
836 * Create a UST session onto the tracer of app and add it the session
839 * Return ust app session or NULL on error.
841 static struct ust_app_session
*create_ust_app_session(
842 struct ltt_ust_session
*usess
, struct ust_app
*app
)
845 struct ust_app_session
*ua_sess
;
847 ua_sess
= lookup_session_by_app(usess
, app
);
848 if (ua_sess
== NULL
) {
849 DBG2("UST app pid: %d session uid %d not found, creating it",
850 app
->key
.pid
, usess
->uid
);
851 ua_sess
= alloc_ust_app_session();
852 if (ua_sess
== NULL
) {
853 /* Only malloc can failed so something is really wrong */
856 shadow_copy_session(ua_sess
, usess
, app
);
859 if (ua_sess
->handle
== -1) {
860 ret
= ustctl_create_session(app
->key
.sock
);
862 ERR("Error creating session for app pid %d, sock %d",
863 app
->key
.pid
, app
->key
.sock
);
864 /* TODO: free() ua_sess */
868 DBG2("UST app ustctl create session handle %d", ret
);
869 ua_sess
->handle
= ret
;
871 /* Add ust app session to app's HT */
872 hashtable_node_init(&ua_sess
->node
,
873 (void *)((unsigned long) ua_sess
->uid
), sizeof(void *));
874 hashtable_add_unique(app
->sessions
, &ua_sess
->node
);
876 DBG2("UST app session created successfully with handle %d", ret
);
886 * Create a context for the channel on the tracer.
889 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
890 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
894 struct cds_lfht_iter iter
;
895 struct cds_lfht_node
*node
;
896 struct ust_app_ctx
*ua_ctx
;
898 DBG2("UST app adding context to channel %s", ua_chan
->name
);
900 node
= hashtable_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
),
901 sizeof(void *), &iter
);
907 ua_ctx
= alloc_ust_app_ctx(uctx
);
908 if (ua_ctx
== NULL
) {
914 hashtable_node_init(&ua_ctx
->node
,
915 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
916 hashtable_add_unique(ua_chan
->ctx
, &ua_ctx
->node
);
918 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
928 * Create an UST context and enable it for the event on the tracer.
931 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
932 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
936 struct cds_lfht_iter iter
;
937 struct cds_lfht_node
*node
;
938 struct ust_app_ctx
*ua_ctx
;
940 DBG2("UST app adding context to event %s", ua_event
->name
);
942 node
= hashtable_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
),
943 sizeof(void *), &iter
);
949 ua_ctx
= alloc_ust_app_ctx(uctx
);
950 if (ua_ctx
== NULL
) {
956 hashtable_node_init(&ua_ctx
->node
,
957 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
958 hashtable_add_unique(ua_event
->ctx
, &ua_ctx
->node
);
960 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
970 * Enable on the tracer side a ust app event for the session and channel.
973 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
974 struct ust_app_event
*ua_event
, struct ust_app
*app
)
978 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
983 ua_event
->enabled
= 1;
990 * Disable on the tracer side a ust app event for the session and channel.
992 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
993 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
,
998 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1003 ua_event
->enabled
= 0;
1010 * Lookup ust app channel for session and disable it on the tracer side.
1013 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1014 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1018 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1023 ua_chan
->enabled
= 0;
1030 * Lookup ust app channel for session and enable it on the tracer side.
1032 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1033 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1036 struct cds_lfht_iter iter
;
1037 struct cds_lfht_node
*ua_chan_node
;
1038 struct ust_app_channel
*ua_chan
;
1040 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1041 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1042 if (ua_chan_node
== NULL
) {
1043 DBG2("Unable to find channel %s in ust session uid %u",
1044 uchan
->name
, ua_sess
->uid
);
1048 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1050 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1060 * Create UST app channel and create it on the tracer.
1062 static struct ust_app_channel
*create_ust_app_channel(
1063 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1064 struct ust_app
*app
)
1067 struct cds_lfht_iter iter
;
1068 struct cds_lfht_node
*ua_chan_node
;
1069 struct ust_app_channel
*ua_chan
;
1071 /* Lookup channel in the ust app session */
1072 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1073 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1074 if (ua_chan_node
== NULL
) {
1075 DBG2("Unable to find channel %s in ust session uid %u",
1076 uchan
->name
, ua_sess
->uid
);
1077 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1078 if (ua_chan
== NULL
) {
1081 shadow_copy_channel(ua_chan
, uchan
);
1083 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
1085 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1088 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1100 * Create UST app event and create it on the tracer side.
1103 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1104 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1105 struct ust_app
*app
)
1108 struct cds_lfht_iter iter
;
1109 struct cds_lfht_node
*ua_event_node
;
1110 struct ust_app_event
*ua_event
;
1112 /* Get event node */
1113 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1114 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
1115 if (ua_event_node
!= NULL
) {
1116 ERR("UST app event %s already exist. Stopping creation.",
1121 /* Does not exist so create one */
1122 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1123 if (ua_event
== NULL
) {
1124 /* Only malloc can failed so something is really wrong */
1128 shadow_copy_event(ua_event
, uevent
);
1130 /* Create it on the tracer side */
1131 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1133 delete_ust_app_event(app
->key
.sock
, ua_event
);
1137 ua_event
->enabled
= 1;
1139 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
1147 * Create UST metadata and open it on the tracer side.
1149 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1150 char *pathname
, struct ust_app
*app
)
1154 if (ua_sess
->metadata
== NULL
) {
1155 /* Allocate UST metadata */
1156 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1157 if (ua_sess
->metadata
== NULL
) {
1158 ERR("UST app session %d creating metadata failed",
1163 ret
= open_ust_metadata(app
, ua_sess
);
1168 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
1171 /* Open UST metadata stream */
1172 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1173 ret
= create_ust_stream(app
, ua_sess
);
1178 ret
= mkdir(ua_sess
->path
, S_IRWXU
| S_IRWXG
);
1180 PERROR("mkdir UST metadata");
1184 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1185 "%s/metadata", ua_sess
->path
);
1187 PERROR("asprintf UST create stream");
1191 DBG2("UST metadata stream object created for app pid %d",
1194 ERR("Attempting to create stream without metadata opened");
1205 * Return pointer to traceable apps list.
1207 struct cds_lfht
*ust_app_get_ht(void)
1213 * Return ust app pointer or NULL if not found.
1215 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1217 struct cds_lfht_node
*node
;
1218 struct cds_lfht_iter iter
;
1221 node
= hashtable_lookup(ust_app_ht
,
1222 (void *)((unsigned long) pid
), sizeof(void *), &iter
);
1224 DBG2("UST app no found with pid %d", pid
);
1229 DBG2("Found UST app by pid %d", pid
);
1231 return caa_container_of(node
, struct ust_app
, node
);
1239 * Using pid and uid (of the app), allocate a new ust_app struct and
1240 * add it to the global traceable app list.
1242 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1243 * bitness is not supported.
1245 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1247 struct ust_app
*lta
;
1249 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1250 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1251 ERR("Registration failed: application \"%s\" (pid: %d) has "
1252 "%d-bit long, but no consumerd for this long size is available.\n",
1253 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1257 lta
= zmalloc(sizeof(struct ust_app
));
1263 lta
->ppid
= msg
->ppid
;
1264 lta
->uid
= msg
->uid
;
1265 lta
->gid
= msg
->gid
;
1266 lta
->bits_per_long
= msg
->bits_per_long
;
1267 lta
->v_major
= msg
->major
;
1268 lta
->v_minor
= msg
->minor
;
1269 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1270 lta
->name
[16] = '\0';
1271 lta
->sessions
= hashtable_new(0);
1274 lta
->key
.pid
= msg
->pid
;
1275 hashtable_node_init(<a
->node
, (void *)((unsigned long)lta
->key
.pid
),
1277 lta
->key
.sock
= sock
;
1278 hashtable_node_init(<a
->key
.node
, (void *)((unsigned long)lta
->key
.sock
),
1282 hashtable_add_unique(ust_app_sock_key_map
, <a
->key
.node
);
1283 hashtable_add_unique(ust_app_ht
, <a
->node
);
1286 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1287 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1288 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1294 * Unregister app by removing it from the global traceable app list and freeing
1297 * The socket is already closed at this point so no close to sock.
1299 void ust_app_unregister(int sock
)
1301 struct ust_app
*lta
;
1302 struct cds_lfht_node
*node
;
1303 struct cds_lfht_iter iter
;
1307 lta
= find_app_by_sock(sock
);
1309 ERR("Unregister app sock %d not found!", sock
);
1313 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1315 /* Get the node reference for a call_rcu */
1316 node
= hashtable_lookup(ust_app_ht
,
1317 (void *)((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
1319 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1323 ret
= hashtable_del(ust_app_ht
, &iter
);
1325 call_rcu(&node
->head
, delete_ust_app_rcu
);
1332 * Return traceable_app_count
1334 unsigned long ust_app_list_count(void)
1336 unsigned long count
;
1339 count
= hashtable_get_count(ust_app_ht
);
1346 * Fill events array with all events name of all registered apps.
1348 int ust_app_list_events(struct lttng_event
**events
)
1351 size_t nbmem
, count
= 0;
1352 struct cds_lfht_iter iter
;
1353 struct ust_app
*app
;
1354 struct lttng_event
*tmp
;
1356 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1357 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1359 PERROR("zmalloc ust app events");
1366 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1367 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1369 ERR("UST app list events getting handle failed for app pid %d",
1374 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1375 tmp
[count
].name
)) != -ENOENT
) {
1376 if (count
> nbmem
) {
1377 DBG2("Reallocating event list from %zu to %zu bytes", nbmem
,
1378 nbmem
+ UST_APP_EVENT_LIST_SIZE
);
1379 nbmem
+= UST_APP_EVENT_LIST_SIZE
;
1380 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1382 PERROR("realloc ust app events");
1388 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1389 tmp
[count
].pid
= app
->key
.pid
;
1390 tmp
[count
].enabled
= -1;
1398 DBG2("UST app list events done (%zu events)", count
);
1407 * Free and clean all traceable apps of the global list.
1409 void ust_app_clean_list(void)
1412 struct cds_lfht_iter iter
;
1413 struct ust_app
*app
;
1415 DBG2("UST app cleaning registered apps hash table");
1419 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1420 ret
= hashtable_del(ust_app_ht
, &iter
);
1422 call_rcu(&iter
.node
->head
, delete_ust_app_rcu
);
1425 hashtable_destroy(ust_app_ht
);
1426 hashtable_destroy(ust_app_sock_key_map
);
1432 * Init UST app hash table.
1434 void ust_app_ht_alloc(void)
1436 ust_app_ht
= hashtable_new(0);
1437 ust_app_sock_key_map
= hashtable_new(0);
1441 * For a specific UST session, disable the channel for all registered apps.
1443 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1444 struct ltt_ust_channel
*uchan
)
1447 struct cds_lfht_iter iter
;
1448 struct cds_lfht_node
*ua_chan_node
;
1449 struct ust_app
*app
;
1450 struct ust_app_session
*ua_sess
;
1451 struct ust_app_channel
*ua_chan
;
1453 if (usess
== NULL
|| uchan
== NULL
) {
1454 ERR("Disabling UST global channel with NULL values");
1459 DBG2("UST app disabling channel %s from global domain for session uid %d",
1460 uchan
->name
, usess
->uid
);
1464 /* For every registered applications */
1465 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1466 ua_sess
= lookup_session_by_app(usess
, app
);
1467 if (ua_sess
== NULL
) {
1472 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1473 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1474 /* If the session if found for the app, the channel must be there */
1475 assert(ua_chan_node
);
1477 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1478 /* The channel must not be already disabled */
1479 assert(ua_chan
->enabled
== 1);
1481 /* Disable channel onto application */
1482 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1484 /* XXX: We might want to report this error at some point... */
1496 * For a specific UST session, enable the channel for all registered apps.
1498 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1499 struct ltt_ust_channel
*uchan
)
1502 struct cds_lfht_iter iter
;
1503 struct ust_app
*app
;
1504 struct ust_app_session
*ua_sess
;
1506 if (usess
== NULL
|| uchan
== NULL
) {
1507 ERR("Adding UST global channel to NULL values");
1512 DBG2("UST app enabling channel %s to global domain for session uid %d",
1513 uchan
->name
, usess
->uid
);
1517 /* For every registered applications */
1518 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1519 ua_sess
= lookup_session_by_app(usess
, app
);
1520 if (ua_sess
== NULL
) {
1524 /* Enable channel onto application */
1525 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1527 /* XXX: We might want to report this error at some point... */
1539 * Disable an event in a channel and for a specific session.
1541 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1542 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1545 struct cds_lfht_iter iter
, uiter
;
1546 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
1547 struct ust_app
*app
;
1548 struct ust_app_session
*ua_sess
;
1549 struct ust_app_channel
*ua_chan
;
1550 struct ust_app_event
*ua_event
;
1552 DBG("UST app disabling event %s for all apps in channel "
1553 "%s for session uid %d", uevent
->attr
.name
, uchan
->name
, usess
->uid
);
1557 /* For all registered applications */
1558 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1559 ua_sess
= lookup_session_by_app(usess
, app
);
1560 if (ua_sess
== NULL
) {
1565 /* Lookup channel in the ust app session */
1566 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1567 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
1568 if (ua_chan_node
== NULL
) {
1569 DBG2("Channel %s not found in session uid %d for app pid %d."
1570 "Skipping", uchan
->name
, usess
->uid
, app
->key
.pid
);
1573 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1575 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1576 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
1577 if (ua_event_node
== NULL
) {
1578 DBG2("Event %s not found in channel %s for app pid %d."
1579 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1582 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1584 ret
= disable_ust_app_event(ua_sess
, ua_chan
, ua_event
, app
);
1586 /* XXX: Report error someday... */
1597 * For a specific UST session and UST channel, the event for all
1600 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1601 struct ltt_ust_channel
*uchan
)
1604 struct cds_lfht_iter iter
, uiter
;
1605 struct cds_lfht_node
*ua_chan_node
;
1606 struct ust_app
*app
;
1607 struct ust_app_session
*ua_sess
;
1608 struct ust_app_channel
*ua_chan
;
1609 struct ust_app_event
*ua_event
;
1611 DBG("UST app disabling all event for all apps in channel "
1612 "%s for session uid %d", uchan
->name
, usess
->uid
);
1616 /* For all registered applications */
1617 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1618 ua_sess
= lookup_session_by_app(usess
, app
);
1619 /* If ua_sess is NULL, there is a code flow error */
1622 /* Lookup channel in the ust app session */
1623 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1624 strlen(uchan
->name
), &uiter
);
1625 /* If the channel is not found, there is a code flow error */
1626 assert(ua_chan_node
);
1628 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1630 /* Disable each events of channel */
1631 cds_lfht_for_each_entry(ua_chan
->events
, &uiter
, ua_event
, node
) {
1632 ret
= disable_ust_app_event(ua_sess
, ua_chan
, ua_event
, app
);
1634 /* XXX: Report error someday... */
1646 * For a specific UST session, create the channel for all registered apps.
1648 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1649 struct ltt_ust_channel
*uchan
)
1652 struct cds_lfht_iter iter
;
1653 struct ust_app
*app
;
1654 struct ust_app_session
*ua_sess
;
1655 struct ust_app_channel
*ua_chan
;
1657 if (usess
== NULL
|| uchan
== NULL
) {
1658 ERR("Adding UST global channel to NULL values");
1663 DBG2("UST app adding channel %s to global domain for session uid %d",
1664 uchan
->name
, usess
->uid
);
1668 /* For every registered applications */
1669 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1671 * Create session on the tracer side and add it to app session HT. Note
1672 * that if session exist, it will simply return a pointer to the ust
1675 ua_sess
= create_ust_app_session(usess
, app
);
1676 if (ua_sess
== NULL
) {
1680 /* Create channel onto application */
1681 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1682 if (ua_chan
== NULL
) {
1694 * Enable event for a specific session and channel on the tracer.
1696 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1697 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1700 struct cds_lfht_iter iter
, uiter
;
1701 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
1702 struct ust_app
*app
;
1703 struct ust_app_session
*ua_sess
;
1704 struct ust_app_channel
*ua_chan
;
1705 struct ust_app_event
*ua_event
;
1707 DBG("UST app enabling event %s for all apps for session uid %d",
1708 uevent
->attr
.name
, usess
->uid
);
1711 * NOTE: At this point, this function is called only if the session and
1712 * channel passed are already created for all apps. and enabled on the
1718 /* For all registered applications */
1719 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1720 ua_sess
= lookup_session_by_app(usess
, app
);
1721 /* If ua_sess is NULL, there is a code flow error */
1724 /* Lookup channel in the ust app session */
1725 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1726 strlen(uchan
->name
), &uiter
);
1727 /* If the channel is not found, there is a code flow error */
1728 assert(ua_chan_node
);
1730 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1732 ua_event_node
= hashtable_lookup(ua_sess
->channels
,
1733 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
1734 if (ua_event_node
== NULL
) {
1735 DBG3("UST app enable event %s not found. Skipping app",
1739 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1741 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1743 /* XXX: Report error someday... */
1754 * For a specific existing UST session and UST channel, creates the event for
1755 * all registered apps.
1757 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1758 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1761 struct cds_lfht_iter iter
, uiter
;
1762 struct cds_lfht_node
*ua_chan_node
;
1763 struct ust_app
*app
;
1764 struct ust_app_session
*ua_sess
;
1765 struct ust_app_channel
*ua_chan
;
1767 DBG("UST app creating event %s for all apps for session uid %d",
1768 uevent
->attr
.name
, usess
->uid
);
1771 * NOTE: At this point, this function is called only if the session and
1772 * channel passed are already created for all apps. and enabled on the
1778 /* For all registered applications */
1779 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1780 ua_sess
= lookup_session_by_app(usess
, app
);
1781 /* If ua_sess is NULL, there is a code flow error */
1784 /* Lookup channel in the ust app session */
1785 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1786 strlen(uchan
->name
), &uiter
);
1787 /* If the channel is not found, there is a code flow error */
1788 assert(ua_chan_node
);
1790 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1792 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1804 * Start tracing for a specific UST session and app.
1806 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1809 struct cds_lfht_iter iter
;
1810 struct ust_app_session
*ua_sess
;
1811 struct ust_app_channel
*ua_chan
;
1812 struct ltt_ust_stream
*ustream
;
1815 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1819 ua_sess
= lookup_session_by_app(usess
, app
);
1820 if (ua_sess
== NULL
) {
1821 goto error_rcu_unlock
;
1824 /* Upon restart, we skip the setup, already done */
1825 if (ua_sess
->started
) {
1829 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1831 goto error_rcu_unlock
;
1834 /* For each channel */
1835 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1836 /* Create all streams */
1838 /* Create UST stream */
1839 ustream
= zmalloc(sizeof(*ustream
));
1840 if (ustream
== NULL
) {
1841 PERROR("zmalloc ust stream");
1842 goto error_rcu_unlock
;
1845 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1848 /* Got all streams */
1851 ustream
->handle
= ustream
->obj
->handle
;
1853 /* Order is important */
1854 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1855 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1856 ua_sess
->path
, ua_chan
->name
,
1857 ua_chan
->streams
.count
++);
1859 PERROR("asprintf UST create stream");
1862 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1867 switch (app
->bits_per_long
) {
1869 consumerd_fd
= ust_consumerd64_fd
;
1872 consumerd_fd
= ust_consumerd32_fd
;
1876 goto error_rcu_unlock
;
1879 /* Setup UST consumer socket and send fds to it */
1880 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1882 goto error_rcu_unlock
;
1884 ua_sess
->started
= 1;
1887 /* This start the UST tracing */
1888 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1890 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1891 goto error_rcu_unlock
;
1896 /* Quiescent wait after starting trace */
1897 ustctl_wait_quiescent(app
->key
.sock
);
1907 * Stop tracing for a specific UST session and app.
1909 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1912 struct ust_app_session
*ua_sess
;
1914 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1918 ua_sess
= lookup_session_by_app(usess
, app
);
1919 if (ua_sess
== NULL
) {
1920 /* Only malloc can failed so something is really wrong */
1921 goto error_rcu_unlock
;
1924 #if 0 /* only useful when periodical flush will be supported */
1925 /* need to keep a handle on shm in session for this. */
1926 /* Flush all buffers before stopping */
1927 ret
= ustctl_flush_buffer(usess
->sock
, usess
->metadata
->obj
);
1929 ERR("UST metadata flush failed");
1932 cds_list_for_each_entry(ustchan
, &usess
->channels
.head
, list
) {
1933 ret
= ustctl_flush_buffer(usess
->sock
, ustchan
->obj
);
1935 ERR("UST flush buffer error");
1940 /* This inhibits UST tracing */
1941 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1943 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1944 goto error_rcu_unlock
;
1949 /* Quiescent wait after stopping trace */
1950 ustctl_wait_quiescent(app
->key
.sock
);
1960 * Destroy a specific UST session in apps.
1962 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1964 struct ust_app_session
*ua_sess
;
1965 struct lttng_ust_object_data obj
;
1966 struct cds_lfht_iter iter
;
1967 struct cds_lfht_node
*node
;
1970 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
1974 __lookup_session_by_app(usess
, app
, &iter
);
1975 node
= hashtable_iter_get_node(&iter
);
1977 /* Only malloc can failed so something is really wrong */
1978 goto error_rcu_unlock
;
1980 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
1981 ret
= hashtable_del(app
->sessions
, &iter
);
1983 delete_ust_app_session(app
->key
.sock
, ua_sess
);
1984 obj
.handle
= ua_sess
->handle
;
1987 obj
.memory_map_size
= 0;
1988 ustctl_release_object(app
->key
.sock
, &obj
);
1992 /* Quiescent wait after stopping trace */
1993 ustctl_wait_quiescent(app
->key
.sock
);
2003 * Start tracing for the UST session.
2005 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2008 struct cds_lfht_iter iter
;
2009 struct ust_app
*app
;
2011 DBG("Starting all UST traces");
2015 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2016 ret
= ust_app_start_trace(usess
, app
);
2018 /* Continue to next apps even on error */
2029 * Start tracing for the UST session.
2031 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2034 struct cds_lfht_iter iter
;
2035 struct ust_app
*app
;
2037 DBG("Stopping all UST traces");
2041 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2042 ret
= ust_app_stop_trace(usess
, app
);
2044 /* Continue to next apps even on error */
2055 * Destroy app UST session.
2057 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2060 struct cds_lfht_iter iter
;
2061 struct ust_app
*app
;
2063 DBG("Destroy all UST traces");
2067 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2068 ret
= ust_app_destroy_trace(usess
, app
);
2070 /* Continue to next apps even on error */
2081 * Add channels/events from UST global domain to registered apps at sock.
2083 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2086 struct cds_lfht_iter iter
;
2087 struct ust_app
*app
;
2088 struct ust_app_session
*ua_sess
;
2089 struct ust_app_channel
*ua_chan
;
2090 struct ust_app_event
*ua_event
;
2092 if (usess
== NULL
) {
2093 ERR("No UST session on global update. Returning");
2097 DBG2("UST app global update for app sock %d for session uid %d", sock
,
2102 app
= find_app_by_sock(sock
);
2104 ERR("Failed to update app sock %d", sock
);
2108 ua_sess
= create_ust_app_session(usess
, app
);
2109 if (ua_sess
== NULL
) {
2114 * We can iterate safely here over all UST app session sicne the create ust
2115 * app session above made a shadow copy of the UST global domain from the
2118 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
2119 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2121 /* FIXME: Should we quit here or continue... */
2125 /* For each events */
2126 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
2127 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2129 /* FIXME: Should we quit here or continue... */
2135 if (usess
->start_trace
) {
2136 ret
= ust_app_start_trace(usess
, app
);
2141 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2150 * Add context to a specific channel for global UST domain.
2152 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2153 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2156 struct cds_lfht_node
*ua_chan_node
;
2157 struct cds_lfht_iter iter
, uiter
;
2158 struct ust_app_channel
*ua_chan
= NULL
;
2159 struct ust_app_session
*ua_sess
;
2160 struct ust_app
*app
;
2164 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2165 ua_sess
= lookup_session_by_app(usess
, app
);
2166 if (ua_sess
== NULL
) {
2170 /* Lookup channel in the ust app session */
2171 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
2172 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
2173 if (ua_chan_node
== NULL
) {
2176 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2179 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2185 /* Add ltt UST context node to ltt UST channel */
2186 hashtable_add_unique(uchan
->ctx
, &uctx
->node
);
2193 * Add context to a specific event in a channel for global UST domain.
2195 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2196 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2197 struct ltt_ust_context
*uctx
)
2200 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2201 struct cds_lfht_iter iter
, uiter
;
2202 struct ust_app_session
*ua_sess
;
2203 struct ust_app_event
*ua_event
;
2204 struct ust_app_channel
*ua_chan
= NULL
;
2205 struct ust_app
*app
;
2209 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2210 ua_sess
= lookup_session_by_app(usess
, app
);
2211 if (ua_sess
== NULL
) {
2215 /* Lookup channel in the ust app session */
2216 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
2217 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
2218 if (ua_chan_node
== NULL
) {
2221 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2224 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2225 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
2226 if (ua_event_node
== NULL
) {
2229 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2232 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2238 /* Add ltt UST context node to ltt UST event */
2239 hashtable_add_unique(uevent
->ctx
, &uctx
->node
);