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
);
166 if (ua_sess
->handle
!= -1) {
167 ustctl_release_handle(sock
, ua_sess
->handle
);
173 * Delete a traceable application structure from the global list. Never call
174 * this function outside of a call_rcu call.
177 void delete_ust_app(struct ust_app
*app
)
180 struct cds_lfht_node
*node
;
181 struct cds_lfht_iter iter
;
182 struct ust_app_session
*ua_sess
;
186 /* Remove from key hash table */
187 node
= hashtable_lookup(ust_app_sock_key_map
,
188 (void *) ((unsigned long) app
->key
.sock
), sizeof(void *), &iter
);
190 /* Not suppose to happen */
191 ERR("UST app key %d not found in key hash table", app
->key
.sock
);
195 ret
= hashtable_del(ust_app_sock_key_map
, &iter
);
197 ERR("UST app unable to delete app sock %d from key hash table",
200 DBG2("UST app pair sock %d key %d deleted",
201 app
->key
.sock
, app
->key
.pid
);
204 /* Socket is already closed at this point */
206 /* Delete ust app sessions info */
207 sock
= app
->key
.sock
;
211 cds_lfht_for_each_entry(app
->sessions
, &iter
, ua_sess
, node
) {
212 ret
= hashtable_del(app
->sessions
, &iter
);
214 delete_ust_app_session(app
->key
.sock
, ua_sess
);
216 ret
= hashtable_destroy(app
->sessions
);
220 * Wait until we have removed the key from the sock hash table
221 * before closing this socket, otherwise an application could
222 * re-use the socket ID and race with the teardown, using the
223 * same hash table entry.
227 DBG2("UST app pid %d deleted", app
->key
.pid
);
234 * URCU intermediate call to delete an UST app.
237 void delete_ust_app_rcu(struct rcu_head
*head
)
239 struct cds_lfht_node
*node
=
240 caa_container_of(head
, struct cds_lfht_node
, head
);
241 struct ust_app
*app
=
242 caa_container_of(node
, struct ust_app
, node
);
248 * Alloc new UST app session.
251 struct ust_app_session
*alloc_ust_app_session(void)
253 struct ust_app_session
*ua_sess
;
255 /* Init most of the default value by allocating and zeroing */
256 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
257 if (ua_sess
== NULL
) {
262 ua_sess
->handle
= -1;
263 ua_sess
->channels
= hashtable_new_str(0);
272 * Alloc new UST app channel.
275 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
276 struct lttng_ust_channel
*attr
)
278 struct ust_app_channel
*ua_chan
;
280 /* Init most of the default value by allocating and zeroing */
281 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
282 if (ua_chan
== NULL
) {
287 /* Setup channel name */
288 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
289 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
291 ua_chan
->enabled
= 1;
292 ua_chan
->handle
= -1;
293 ua_chan
->ctx
= hashtable_new(0);
294 ua_chan
->events
= hashtable_new_str(0);
295 hashtable_node_init(&ua_chan
->node
, (void *) ua_chan
->name
,
296 strlen(ua_chan
->name
));
298 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
300 /* Copy attributes */
302 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
305 DBG3("UST app channel %s allocated", ua_chan
->name
);
314 * Alloc new UST app event.
317 struct ust_app_event
*alloc_ust_app_event(char *name
,
318 struct lttng_ust_event
*attr
)
320 struct ust_app_event
*ua_event
;
322 /* Init most of the default value by allocating and zeroing */
323 ua_event
= zmalloc(sizeof(struct ust_app_event
));
324 if (ua_event
== NULL
) {
329 ua_event
->enabled
= 1;
330 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
331 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
332 ua_event
->ctx
= hashtable_new(0);
333 hashtable_node_init(&ua_event
->node
, (void *) ua_event
->name
,
334 strlen(ua_event
->name
));
336 /* Copy attributes */
338 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
341 DBG3("UST app event %s allocated", ua_event
->name
);
350 * Alloc new UST app context.
353 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
355 struct ust_app_ctx
*ua_ctx
;
357 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
358 if (ua_ctx
== NULL
) {
363 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
366 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
373 * Find an ust_app using the sock and return it. RCU read side lock must be
374 * held before calling this helper function.
377 struct ust_app
*find_app_by_sock(int sock
)
379 struct cds_lfht_node
*node
;
380 struct ust_app_key
*key
;
381 struct cds_lfht_iter iter
;
383 node
= hashtable_lookup(ust_app_sock_key_map
,
384 (void *)((unsigned long) sock
), sizeof(void *), &iter
);
386 DBG2("UST app find by sock %d key not found", sock
);
390 key
= caa_container_of(node
, struct ust_app_key
, node
);
392 node
= hashtable_lookup(ust_app_ht
,
393 (void *)((unsigned long) key
->pid
), sizeof(void *), &iter
);
395 DBG2("UST app find by sock %d not found", sock
);
398 return caa_container_of(node
, struct ust_app
, node
);
405 * Create the channel context on the tracer.
408 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
409 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
413 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
414 ua_chan
->obj
, &ua_ctx
->obj
);
419 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
421 DBG2("UST app context added to channel %s successfully", ua_chan
->name
);
428 * Create the event context on the tracer.
431 int create_ust_event_context(struct ust_app_event
*ua_event
,
432 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
436 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
437 ua_event
->obj
, &ua_ctx
->obj
);
442 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
444 DBG2("UST app context added to event %s successfully", ua_event
->name
);
451 * Disable the specified event on to UST tracer for the UST session.
453 static int disable_ust_event(struct ust_app
*app
,
454 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
458 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
460 ERR("UST app event %s disable failed for app (pid: %d) "
461 "and session handle %d with ret %d",
462 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
466 DBG2("UST app event %s disabled successfully for app (pid: %d)",
467 ua_event
->attr
.name
, app
->key
.pid
);
474 * Disable the specified channel on to UST tracer for the UST session.
476 static int disable_ust_channel(struct ust_app
*app
,
477 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
481 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
483 ERR("UST app channel %s disable failed for app (pid: %d) "
484 "and session handle %d with ret %d",
485 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
489 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
490 ua_chan
->name
, app
->key
.pid
);
497 * Enable the specified channel on to UST tracer for the UST session.
499 static int enable_ust_channel(struct ust_app
*app
,
500 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
504 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
506 ERR("UST app channel %s enable failed for app (pid: %d) "
507 "and session handle %d with ret %d",
508 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
512 ua_chan
->enabled
= 1;
514 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
515 ua_chan
->name
, app
->key
.pid
);
522 * Enable the specified event on to UST tracer for the UST session.
524 static int enable_ust_event(struct ust_app
*app
,
525 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
529 ret
= ustctl_enable(app
->key
.sock
, ua_event
->obj
);
531 ERR("UST app event %s enable failed for app (pid: %d) "
532 "and session handle %d with ret %d",
533 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
537 DBG2("UST app event %s enabled successfully for app (pid: %d)",
538 ua_event
->attr
.name
, app
->key
.pid
);
545 * Open metadata onto the UST tracer for a UST session.
547 static int open_ust_metadata(struct ust_app
*app
,
548 struct ust_app_session
*ua_sess
)
551 struct lttng_ust_channel_attr uattr
;
553 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
554 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
555 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
556 uattr
.switch_timer_interval
=
557 ua_sess
->metadata
->attr
.switch_timer_interval
;
558 uattr
.read_timer_interval
=
559 ua_sess
->metadata
->attr
.read_timer_interval
;
560 uattr
.output
= ua_sess
->metadata
->attr
.output
;
562 /* UST tracer metadata creation */
563 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
564 &ua_sess
->metadata
->obj
);
566 ERR("UST app open metadata failed for app pid:%d",
571 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
578 * Create stream onto the UST tracer for a UST session.
580 static int create_ust_stream(struct ust_app
*app
,
581 struct ust_app_session
*ua_sess
)
585 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
586 &ua_sess
->metadata
->stream_obj
);
588 ERR("UST create metadata stream failed");
597 * Create the specified channel onto the UST tracer for a UST session.
599 static int create_ust_channel(struct ust_app
*app
,
600 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
604 /* TODO: remove cast and use lttng-ust-abi.h */
605 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
606 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
608 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
609 "and session handle %d with ret %d",
610 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
611 ua_sess
->handle
, ret
);
615 ua_chan
->handle
= ua_chan
->obj
->handle
;
616 ua_chan
->attr
.shm_fd
= ua_chan
->obj
->shm_fd
;
617 ua_chan
->attr
.wait_fd
= ua_chan
->obj
->wait_fd
;
618 ua_chan
->attr
.memory_map_size
= ua_chan
->obj
->memory_map_size
;
620 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
621 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
623 /* If channel is not enabled, disable it on the tracer */
624 if (!ua_chan
->enabled
) {
625 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
636 * Create the specified event onto the UST tracer for a UST session.
639 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
640 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
644 /* Create UST event on tracer */
645 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
648 ERR("Error ustctl create event %s for app pid: %d with ret %d",
649 ua_event
->attr
.name
, app
->key
.pid
, ret
);
653 ua_event
->handle
= ua_event
->obj
->handle
;
655 DBG2("UST app event %s created successfully for pid:%d",
656 ua_event
->attr
.name
, app
->key
.pid
);
658 /* If event not enabled, disable it on the tracer */
659 if (!ua_event
->enabled
) {
660 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
671 * Copy data between an UST app event and a LTT event.
673 static void shadow_copy_event(struct ust_app_event
*ua_event
,
674 struct ltt_ust_event
*uevent
)
676 struct cds_lfht_iter iter
;
677 struct ltt_ust_context
*uctx
;
678 struct ust_app_ctx
*ua_ctx
;
680 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
681 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
683 /* Copy event attributes */
684 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
686 cds_lfht_for_each_entry(uevent
->ctx
, &iter
, uctx
, node
) {
687 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
688 if (ua_ctx
== NULL
) {
691 hashtable_node_init(&ua_ctx
->node
,
692 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
693 hashtable_add_unique(ua_event
->ctx
, &ua_ctx
->node
);
698 * Copy data between an UST app channel and a LTT channel.
700 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
701 struct ltt_ust_channel
*uchan
)
703 struct cds_lfht_iter iter
;
704 struct cds_lfht_node
*ua_event_node
;
705 struct ltt_ust_event
*uevent
;
706 struct ltt_ust_context
*uctx
;
707 struct ust_app_event
*ua_event
;
708 struct ust_app_ctx
*ua_ctx
;
710 DBG2("Shadow copy of UST app channel %s", ua_chan
->name
);
712 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
713 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
714 /* Copy event attributes */
715 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
717 cds_lfht_for_each_entry(uchan
->ctx
, &iter
, uctx
, node
) {
718 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
719 if (ua_ctx
== NULL
) {
722 hashtable_node_init(&ua_ctx
->node
,
723 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
724 hashtable_add_unique(ua_chan
->ctx
, &ua_ctx
->node
);
727 /* Copy all events from ltt ust channel to ust app channel */
728 cds_lfht_for_each_entry(uchan
->events
, &iter
, uevent
, node
) {
729 struct cds_lfht_iter uiter
;
731 ua_event_node
= hashtable_lookup(ua_chan
->events
,
732 (void *) uevent
->attr
.name
, strlen(uevent
->attr
.name
),
734 if (ua_event_node
== NULL
) {
735 DBG2("UST event %s not found on shadow copy channel",
737 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
738 if (ua_event
== NULL
) {
741 shadow_copy_event(ua_event
, uevent
);
742 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
746 DBG3("Shadow copy channel done");
750 * Copy data between a UST app session and a regular LTT session.
752 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
753 struct ltt_ust_session
*usess
,
756 struct cds_lfht_node
*ua_chan_node
;
757 struct cds_lfht_iter iter
;
758 struct ltt_ust_channel
*uchan
;
759 struct ust_app_channel
*ua_chan
;
765 /* Get date and time for unique app path */
767 timeinfo
= localtime(&rawtime
);
768 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
770 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
772 ua_sess
->uid
= usess
->uid
;
774 ret
= snprintf(ua_sess
->path
, PATH_MAX
,
776 usess
->pathname
, app
->name
, app
->key
.pid
,
779 PERROR("asprintf UST shadow copy session");
780 /* TODO: We cannot return an error from here.. */
784 /* TODO: support all UST domain */
786 /* Iterate over all channels in global domain. */
787 cds_lfht_for_each_entry(usess
->domain_global
.channels
, &iter
,
789 struct cds_lfht_iter uiter
;
791 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
792 (void *)uchan
->name
, strlen(uchan
->name
),
794 if (ua_chan_node
!= NULL
) {
798 DBG2("Channel %s not found on shadow session copy, creating it",
800 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
801 if (ua_chan
== NULL
) {
802 /* malloc failed... continuing */
806 shadow_copy_channel(ua_chan
, uchan
);
807 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
812 * Lookup sesison wrapper.
815 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
816 struct ust_app
*app
, struct cds_lfht_iter
*iter
)
818 /* Get right UST app session from app */
819 (void) hashtable_lookup(app
->sessions
,
820 (void *) ((unsigned long) usess
->uid
), sizeof(void *),
825 * Return ust app session from the app session hashtable using the UST session
828 static struct ust_app_session
*lookup_session_by_app(
829 struct ltt_ust_session
*usess
, struct ust_app
*app
)
831 struct cds_lfht_iter iter
;
832 struct cds_lfht_node
*node
;
834 __lookup_session_by_app(usess
, app
, &iter
);
835 node
= hashtable_iter_get_node(&iter
);
840 return caa_container_of(node
, struct ust_app_session
, node
);
847 * Create a UST session onto the tracer of app and add it the session
850 * Return ust app session or NULL on error.
852 static struct ust_app_session
*create_ust_app_session(
853 struct ltt_ust_session
*usess
, struct ust_app
*app
)
856 struct ust_app_session
*ua_sess
;
858 ua_sess
= lookup_session_by_app(usess
, app
);
859 if (ua_sess
== NULL
) {
860 DBG2("UST app pid: %d session uid %d not found, creating it",
861 app
->key
.pid
, usess
->uid
);
862 ua_sess
= alloc_ust_app_session();
863 if (ua_sess
== NULL
) {
864 /* Only malloc can failed so something is really wrong */
867 shadow_copy_session(ua_sess
, usess
, app
);
870 if (ua_sess
->handle
== -1) {
871 ret
= ustctl_create_session(app
->key
.sock
);
873 ERR("Error creating session for app pid %d, sock %d",
874 app
->key
.pid
, app
->key
.sock
);
875 /* TODO: free() ua_sess */
879 DBG2("UST app ustctl create session handle %d", ret
);
880 ua_sess
->handle
= ret
;
882 /* Add ust app session to app's HT */
883 hashtable_node_init(&ua_sess
->node
,
884 (void *)((unsigned long) ua_sess
->uid
), sizeof(void *));
885 hashtable_add_unique(app
->sessions
, &ua_sess
->node
);
887 DBG2("UST app session created successfully with handle %d", ret
);
897 * Create a context for the channel on the tracer.
900 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
901 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
905 struct cds_lfht_iter iter
;
906 struct cds_lfht_node
*node
;
907 struct ust_app_ctx
*ua_ctx
;
909 DBG2("UST app adding context to channel %s", ua_chan
->name
);
911 node
= hashtable_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
),
912 sizeof(void *), &iter
);
918 ua_ctx
= alloc_ust_app_ctx(uctx
);
919 if (ua_ctx
== NULL
) {
925 hashtable_node_init(&ua_ctx
->node
,
926 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
927 hashtable_add_unique(ua_chan
->ctx
, &ua_ctx
->node
);
929 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
939 * Create an UST context and enable it for the event on the tracer.
942 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
943 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
947 struct cds_lfht_iter iter
;
948 struct cds_lfht_node
*node
;
949 struct ust_app_ctx
*ua_ctx
;
951 DBG2("UST app adding context to event %s", ua_event
->name
);
953 node
= hashtable_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
),
954 sizeof(void *), &iter
);
960 ua_ctx
= alloc_ust_app_ctx(uctx
);
961 if (ua_ctx
== NULL
) {
967 hashtable_node_init(&ua_ctx
->node
,
968 (void *)((unsigned long) ua_ctx
->ctx
.ctx
), sizeof(void *));
969 hashtable_add_unique(ua_event
->ctx
, &ua_ctx
->node
);
971 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
981 * Enable on the tracer side a ust app event for the session and channel.
984 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
985 struct ust_app_event
*ua_event
, struct ust_app
*app
)
989 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
994 ua_event
->enabled
= 1;
1001 * Disable on the tracer side a ust app event for the session and channel.
1003 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
1004 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1008 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1013 ua_event
->enabled
= 0;
1020 * Lookup ust app channel for session and disable it on the tracer side.
1023 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1024 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1028 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1033 ua_chan
->enabled
= 0;
1040 * Lookup ust app channel for session and enable it on the tracer side.
1042 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1043 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1046 struct cds_lfht_iter iter
;
1047 struct cds_lfht_node
*ua_chan_node
;
1048 struct ust_app_channel
*ua_chan
;
1050 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1051 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1052 if (ua_chan_node
== NULL
) {
1053 DBG2("Unable to find channel %s in ust session uid %u",
1054 uchan
->name
, ua_sess
->uid
);
1058 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1060 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1070 * Create UST app channel and create it on the tracer.
1072 static struct ust_app_channel
*create_ust_app_channel(
1073 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1074 struct ust_app
*app
)
1077 struct cds_lfht_iter iter
;
1078 struct cds_lfht_node
*ua_chan_node
;
1079 struct ust_app_channel
*ua_chan
;
1081 /* Lookup channel in the ust app session */
1082 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1083 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1084 if (ua_chan_node
== NULL
) {
1085 DBG2("Unable to find channel %s in ust session uid %u",
1086 uchan
->name
, ua_sess
->uid
);
1087 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1088 if (ua_chan
== NULL
) {
1091 shadow_copy_channel(ua_chan
, uchan
);
1093 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
1095 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1098 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1110 * Create UST app event and create it on the tracer side.
1113 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1114 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1115 struct ust_app
*app
)
1118 struct cds_lfht_iter iter
;
1119 struct cds_lfht_node
*ua_event_node
;
1120 struct ust_app_event
*ua_event
;
1122 /* Get event node */
1123 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1124 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
1125 if (ua_event_node
!= NULL
) {
1126 ERR("UST app event %s already exist. Stopping creation.",
1131 /* Does not exist so create one */
1132 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1133 if (ua_event
== NULL
) {
1134 /* Only malloc can failed so something is really wrong */
1138 shadow_copy_event(ua_event
, uevent
);
1140 /* Create it on the tracer side */
1141 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1144 delete_ust_app_event(app
->key
.sock
, ua_event
);
1149 ua_event
->enabled
= 1;
1151 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
1153 DBG2("UST app create event %s for PID %d completed",
1154 ua_event
->name
, app
->key
.pid
);
1162 * Create UST metadata and open it on the tracer side.
1164 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1165 char *pathname
, struct ust_app
*app
)
1170 if (ua_sess
->metadata
== NULL
) {
1171 /* Allocate UST metadata */
1172 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1173 if (ua_sess
->metadata
== NULL
) {
1174 ERR("UST app session %d creating metadata failed",
1179 ret
= open_ust_metadata(app
, ua_sess
);
1184 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
1187 /* Open UST metadata stream */
1188 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1189 ret
= create_ust_stream(app
, ua_sess
);
1194 old_umask
= umask(0);
1195 ret
= mkdir(ua_sess
->path
, S_IRWXU
| S_IRWXG
);
1197 PERROR("mkdir UST metadata");
1202 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1203 "%s/metadata", ua_sess
->path
);
1205 PERROR("asprintf UST create stream");
1209 DBG2("UST metadata stream object created for app pid %d",
1212 ERR("Attempting to create stream without metadata opened");
1223 * Return pointer to traceable apps list.
1225 struct cds_lfht
*ust_app_get_ht(void)
1231 * Return ust app pointer or NULL if not found.
1233 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1235 struct cds_lfht_node
*node
;
1236 struct cds_lfht_iter iter
;
1239 node
= hashtable_lookup(ust_app_ht
,
1240 (void *)((unsigned long) pid
), sizeof(void *), &iter
);
1242 DBG2("UST app no found with pid %d", pid
);
1247 DBG2("Found UST app by pid %d", pid
);
1249 return caa_container_of(node
, struct ust_app
, node
);
1257 * Using pid and uid (of the app), allocate a new ust_app struct and
1258 * add it to the global traceable app list.
1260 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1261 * bitness is not supported.
1263 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1265 struct ust_app
*lta
;
1267 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1268 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1269 ERR("Registration failed: application \"%s\" (pid: %d) has "
1270 "%d-bit long, but no consumerd for this long size is available.\n",
1271 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1275 lta
= zmalloc(sizeof(struct ust_app
));
1281 lta
->ppid
= msg
->ppid
;
1282 lta
->uid
= msg
->uid
;
1283 lta
->gid
= msg
->gid
;
1284 lta
->bits_per_long
= msg
->bits_per_long
;
1285 lta
->v_major
= msg
->major
;
1286 lta
->v_minor
= msg
->minor
;
1287 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1288 lta
->name
[16] = '\0';
1289 lta
->sessions
= hashtable_new(0);
1292 lta
->key
.pid
= msg
->pid
;
1293 hashtable_node_init(<a
->node
, (void *)((unsigned long)lta
->key
.pid
),
1295 lta
->key
.sock
= sock
;
1296 hashtable_node_init(<a
->key
.node
, (void *)((unsigned long)lta
->key
.sock
),
1300 hashtable_add_unique(ust_app_sock_key_map
, <a
->key
.node
);
1301 hashtable_add_unique(ust_app_ht
, <a
->node
);
1304 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1305 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1306 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1312 * Unregister app by removing it from the global traceable app list and freeing
1315 * The socket is already closed at this point so no close to sock.
1317 void ust_app_unregister(int sock
)
1319 struct ust_app
*lta
;
1320 struct cds_lfht_node
*node
;
1321 struct cds_lfht_iter iter
;
1325 lta
= find_app_by_sock(sock
);
1327 ERR("Unregister app sock %d not found!", sock
);
1331 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1333 /* Get the node reference for a call_rcu */
1334 node
= hashtable_lookup(ust_app_ht
,
1335 (void *)((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
1337 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1341 ret
= hashtable_del(ust_app_ht
, &iter
);
1343 call_rcu(&node
->head
, delete_ust_app_rcu
);
1350 * Return traceable_app_count
1352 unsigned long ust_app_list_count(void)
1354 unsigned long count
;
1357 count
= hashtable_get_count(ust_app_ht
);
1364 * Fill events array with all events name of all registered apps.
1366 int ust_app_list_events(struct lttng_event
**events
)
1369 size_t nbmem
, count
= 0;
1370 struct cds_lfht_iter iter
;
1371 struct ust_app
*app
;
1372 struct lttng_event
*tmp
;
1374 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1375 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1377 PERROR("zmalloc ust app events");
1384 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1385 struct lttng_ust_tracepoint_iter iter
;
1387 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1389 ERR("UST app list events getting handle failed for app pid %d",
1394 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1395 &iter
)) != -ENOENT
) {
1396 if (count
>= nbmem
) {
1397 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1400 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1402 PERROR("realloc ust app events");
1407 memcpy(tmp
[count
].name
, iter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1408 memcpy(tmp
[count
].loglevel
, iter
.loglevel
, LTTNG_UST_SYM_NAME_LEN
);
1409 tmp
[count
].loglevel_value
= iter
.loglevel_value
;
1410 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1411 tmp
[count
].pid
= app
->key
.pid
;
1412 tmp
[count
].enabled
= -1;
1420 DBG2("UST app list events done (%zu events)", count
);
1429 * Free and clean all traceable apps of the global list.
1431 void ust_app_clean_list(void)
1434 struct cds_lfht_iter iter
;
1435 struct ust_app
*app
;
1437 DBG2("UST app cleaning registered apps hash table");
1441 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1442 ret
= hashtable_del(ust_app_ht
, &iter
);
1444 call_rcu(&iter
.node
->head
, delete_ust_app_rcu
);
1447 hashtable_destroy(ust_app_ht
);
1448 hashtable_destroy(ust_app_sock_key_map
);
1454 * Init UST app hash table.
1456 void ust_app_ht_alloc(void)
1458 ust_app_ht
= hashtable_new(0);
1459 ust_app_sock_key_map
= hashtable_new(0);
1463 * For a specific UST session, disable the channel for all registered apps.
1465 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1466 struct ltt_ust_channel
*uchan
)
1469 struct cds_lfht_iter iter
;
1470 struct cds_lfht_node
*ua_chan_node
;
1471 struct ust_app
*app
;
1472 struct ust_app_session
*ua_sess
;
1473 struct ust_app_channel
*ua_chan
;
1475 if (usess
== NULL
|| uchan
== NULL
) {
1476 ERR("Disabling UST global channel with NULL values");
1481 DBG2("UST app disabling channel %s from global domain for session uid %d",
1482 uchan
->name
, usess
->uid
);
1486 /* For every registered applications */
1487 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1488 ua_sess
= lookup_session_by_app(usess
, app
);
1489 if (ua_sess
== NULL
) {
1494 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1495 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1496 /* If the session if found for the app, the channel must be there */
1497 assert(ua_chan_node
);
1499 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1500 /* The channel must not be already disabled */
1501 assert(ua_chan
->enabled
== 1);
1503 /* Disable channel onto application */
1504 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1506 /* XXX: We might want to report this error at some point... */
1518 * For a specific UST session, enable the channel for all registered apps.
1520 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1521 struct ltt_ust_channel
*uchan
)
1524 struct cds_lfht_iter iter
;
1525 struct ust_app
*app
;
1526 struct ust_app_session
*ua_sess
;
1528 if (usess
== NULL
|| uchan
== NULL
) {
1529 ERR("Adding UST global channel to NULL values");
1534 DBG2("UST app enabling channel %s to global domain for session uid %d",
1535 uchan
->name
, usess
->uid
);
1539 /* For every registered applications */
1540 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1541 ua_sess
= lookup_session_by_app(usess
, app
);
1542 if (ua_sess
== NULL
) {
1546 /* Enable channel onto application */
1547 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1549 /* XXX: We might want to report this error at some point... */
1561 * Disable an event in a channel and for a specific session.
1563 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1564 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1567 struct cds_lfht_iter iter
, uiter
;
1568 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
1569 struct ust_app
*app
;
1570 struct ust_app_session
*ua_sess
;
1571 struct ust_app_channel
*ua_chan
;
1572 struct ust_app_event
*ua_event
;
1574 DBG("UST app disabling event %s for all apps in channel "
1575 "%s for session uid %d", uevent
->attr
.name
, uchan
->name
, usess
->uid
);
1579 /* For all registered applications */
1580 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1581 ua_sess
= lookup_session_by_app(usess
, app
);
1582 if (ua_sess
== NULL
) {
1587 /* Lookup channel in the ust app session */
1588 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1589 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
1590 if (ua_chan_node
== NULL
) {
1591 DBG2("Channel %s not found in session uid %d for app pid %d."
1592 "Skipping", uchan
->name
, usess
->uid
, app
->key
.pid
);
1595 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1597 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1598 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
1599 if (ua_event_node
== NULL
) {
1600 DBG2("Event %s not found in channel %s for app pid %d."
1601 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1604 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1606 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1608 /* XXX: Report error someday... */
1619 * For a specific UST session and UST channel, the event for all
1622 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1623 struct ltt_ust_channel
*uchan
)
1626 struct cds_lfht_iter iter
, uiter
;
1627 struct cds_lfht_node
*ua_chan_node
;
1628 struct ust_app
*app
;
1629 struct ust_app_session
*ua_sess
;
1630 struct ust_app_channel
*ua_chan
;
1631 struct ust_app_event
*ua_event
;
1633 DBG("UST app disabling all event for all apps in channel "
1634 "%s for session uid %d", uchan
->name
, usess
->uid
);
1638 /* For all registered applications */
1639 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1640 ua_sess
= lookup_session_by_app(usess
, app
);
1641 /* If ua_sess is NULL, there is a code flow error */
1644 /* Lookup channel in the ust app session */
1645 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1646 strlen(uchan
->name
), &uiter
);
1647 /* If the channel is not found, there is a code flow error */
1648 assert(ua_chan_node
);
1650 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1652 /* Disable each events of channel */
1653 cds_lfht_for_each_entry(ua_chan
->events
, &uiter
, ua_event
, node
) {
1654 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1656 /* XXX: Report error someday... */
1668 * For a specific UST session, create the channel for all registered apps.
1670 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1671 struct ltt_ust_channel
*uchan
)
1674 struct cds_lfht_iter iter
;
1675 struct ust_app
*app
;
1676 struct ust_app_session
*ua_sess
;
1677 struct ust_app_channel
*ua_chan
;
1679 if (usess
== NULL
|| uchan
== NULL
) {
1680 ERR("Adding UST global channel to NULL values");
1685 DBG2("UST app adding channel %s to global domain for session uid %d",
1686 uchan
->name
, usess
->uid
);
1690 /* For every registered applications */
1691 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1693 * Create session on the tracer side and add it to app session HT. Note
1694 * that if session exist, it will simply return a pointer to the ust
1697 ua_sess
= create_ust_app_session(usess
, app
);
1698 if (ua_sess
== NULL
) {
1702 /* Create channel onto application */
1703 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1704 if (ua_chan
== NULL
) {
1716 * Enable event for a specific session and channel on the tracer.
1718 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1719 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1722 struct cds_lfht_iter iter
, uiter
;
1723 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
1724 struct ust_app
*app
;
1725 struct ust_app_session
*ua_sess
;
1726 struct ust_app_channel
*ua_chan
;
1727 struct ust_app_event
*ua_event
;
1729 DBG("UST app enabling event %s for all apps for session uid %d",
1730 uevent
->attr
.name
, usess
->uid
);
1733 * NOTE: At this point, this function is called only if the session and
1734 * channel passed are already created for all apps. and enabled on the
1740 /* For all registered applications */
1741 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1742 ua_sess
= lookup_session_by_app(usess
, app
);
1743 /* If ua_sess is NULL, there is a code flow error */
1746 /* Lookup channel in the ust app session */
1747 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1748 strlen(uchan
->name
), &uiter
);
1749 /* If the channel is not found, there is a code flow error */
1750 assert(ua_chan_node
);
1752 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1754 ua_event_node
= hashtable_lookup(ua_chan
->events
,
1755 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
1756 if (ua_event_node
== NULL
) {
1757 DBG3("UST app enable event %s not found for app PID %d."
1758 "Skipping app", uevent
->attr
.name
, app
->key
.pid
);
1761 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1763 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1775 * For a specific existing UST session and UST channel, creates the event for
1776 * all registered apps.
1778 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1779 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1782 struct cds_lfht_iter iter
, uiter
;
1783 struct cds_lfht_node
*ua_chan_node
;
1784 struct ust_app
*app
;
1785 struct ust_app_session
*ua_sess
;
1786 struct ust_app_channel
*ua_chan
;
1788 DBG("UST app creating event %s for all apps for session uid %d",
1789 uevent
->attr
.name
, usess
->uid
);
1792 * NOTE: At this point, this function is called only if the session and
1793 * channel passed are already created for all apps. and enabled on the
1799 /* For all registered applications */
1800 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1801 ua_sess
= lookup_session_by_app(usess
, app
);
1802 /* If ua_sess is NULL, there is a code flow error */
1805 /* Lookup channel in the ust app session */
1806 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
1807 strlen(uchan
->name
), &uiter
);
1808 /* If the channel is not found, there is a code flow error */
1809 assert(ua_chan_node
);
1811 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1813 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1825 * Start tracing for a specific UST session and app.
1827 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1830 struct cds_lfht_iter iter
;
1831 struct ust_app_session
*ua_sess
;
1832 struct ust_app_channel
*ua_chan
;
1833 struct ltt_ust_stream
*ustream
;
1836 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1840 ua_sess
= lookup_session_by_app(usess
, app
);
1841 if (ua_sess
== NULL
) {
1842 goto error_rcu_unlock
;
1845 /* Upon restart, we skip the setup, already done */
1846 if (ua_sess
->started
) {
1850 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1852 goto error_rcu_unlock
;
1855 /* For each channel */
1856 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1857 /* Create all streams */
1859 /* Create UST stream */
1860 ustream
= zmalloc(sizeof(*ustream
));
1861 if (ustream
== NULL
) {
1862 PERROR("zmalloc ust stream");
1863 goto error_rcu_unlock
;
1866 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1869 /* Got all streams */
1872 ustream
->handle
= ustream
->obj
->handle
;
1874 /* Order is important */
1875 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1876 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1877 ua_sess
->path
, ua_chan
->name
,
1878 ua_chan
->streams
.count
++);
1880 PERROR("asprintf UST create stream");
1883 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1888 switch (app
->bits_per_long
) {
1890 consumerd_fd
= ust_consumerd64_fd
;
1893 consumerd_fd
= ust_consumerd32_fd
;
1897 goto error_rcu_unlock
;
1900 /* Setup UST consumer socket and send fds to it */
1901 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1903 goto error_rcu_unlock
;
1905 ua_sess
->started
= 1;
1908 /* This start the UST tracing */
1909 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1911 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1912 goto error_rcu_unlock
;
1917 /* Quiescent wait after starting trace */
1918 ustctl_wait_quiescent(app
->key
.sock
);
1928 * Stop tracing for a specific UST session and app.
1930 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1933 struct cds_lfht_iter iter
;
1934 struct ust_app_session
*ua_sess
;
1935 struct ust_app_channel
*ua_chan
;
1937 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1941 ua_sess
= lookup_session_by_app(usess
, app
);
1942 if (ua_sess
== NULL
) {
1943 /* Only malloc can failed so something is really wrong */
1944 goto error_rcu_unlock
;
1947 /* Flush all buffers before stopping */
1948 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_sess
->metadata
->obj
);
1950 ERR("UST app PID %d metadata flush failed", app
->key
.pid
);
1951 ERR("Ended with ret %d", ret
);
1954 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1955 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_chan
->obj
);
1957 ERR("UST app PID %d channel %s flush failed",
1958 app
->key
.pid
, ua_chan
->name
);
1959 ERR("Ended with ret %d", ret
);
1960 /* Continuing flushing all buffers */
1965 /* This inhibits UST tracing */
1966 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1968 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1969 goto error_rcu_unlock
;
1974 /* Quiescent wait after stopping trace */
1975 ustctl_wait_quiescent(app
->key
.sock
);
1985 * Destroy a specific UST session in apps.
1987 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1989 struct ust_app_session
*ua_sess
;
1990 struct lttng_ust_object_data obj
;
1991 struct cds_lfht_iter iter
;
1992 struct cds_lfht_node
*node
;
1995 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
1999 __lookup_session_by_app(usess
, app
, &iter
);
2000 node
= hashtable_iter_get_node(&iter
);
2002 /* Only malloc can failed so something is really wrong */
2003 goto error_rcu_unlock
;
2005 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2006 ret
= hashtable_del(app
->sessions
, &iter
);
2008 delete_ust_app_session(app
->key
.sock
, ua_sess
);
2009 obj
.handle
= ua_sess
->handle
;
2012 obj
.memory_map_size
= 0;
2013 ustctl_release_object(app
->key
.sock
, &obj
);
2017 /* Quiescent wait after stopping trace */
2018 ustctl_wait_quiescent(app
->key
.sock
);
2028 * Start tracing for the UST session.
2030 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2033 struct cds_lfht_iter iter
;
2034 struct ust_app
*app
;
2036 DBG("Starting all UST traces");
2040 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2041 ret
= ust_app_start_trace(usess
, app
);
2043 /* Continue to next apps even on error */
2054 * Start tracing for the UST session.
2056 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2059 struct cds_lfht_iter iter
;
2060 struct ust_app
*app
;
2062 DBG("Stopping all UST traces");
2066 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2067 ret
= ust_app_stop_trace(usess
, app
);
2069 /* Continue to next apps even on error */
2080 * Destroy app UST session.
2082 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2085 struct cds_lfht_iter iter
;
2086 struct ust_app
*app
;
2088 DBG("Destroy all UST traces");
2092 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2093 ret
= ust_app_destroy_trace(usess
, app
);
2095 /* Continue to next apps even on error */
2106 * Add channels/events from UST global domain to registered apps at sock.
2108 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2111 struct cds_lfht_iter iter
;
2112 struct ust_app
*app
;
2113 struct ust_app_session
*ua_sess
;
2114 struct ust_app_channel
*ua_chan
;
2115 struct ust_app_event
*ua_event
;
2117 if (usess
== NULL
) {
2118 ERR("No UST session on global update. Returning");
2122 DBG2("UST app global update for app sock %d for session uid %d", sock
,
2127 app
= find_app_by_sock(sock
);
2129 ERR("Failed to update app sock %d", sock
);
2133 ua_sess
= create_ust_app_session(usess
, app
);
2134 if (ua_sess
== NULL
) {
2139 * We can iterate safely here over all UST app session sicne the create ust
2140 * app session above made a shadow copy of the UST global domain from the
2143 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
2144 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2146 /* FIXME: Should we quit here or continue... */
2150 /* For each events */
2151 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
2152 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2154 /* FIXME: Should we quit here or continue... */
2160 if (usess
->start_trace
) {
2161 ret
= ust_app_start_trace(usess
, app
);
2166 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2175 * Add context to a specific channel for global UST domain.
2177 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2178 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2181 struct cds_lfht_node
*ua_chan_node
;
2182 struct cds_lfht_iter iter
, uiter
;
2183 struct ust_app_channel
*ua_chan
= NULL
;
2184 struct ust_app_session
*ua_sess
;
2185 struct ust_app
*app
;
2189 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2190 ua_sess
= lookup_session_by_app(usess
, app
);
2191 if (ua_sess
== NULL
) {
2195 /* Lookup channel in the ust app session */
2196 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
2197 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
2198 if (ua_chan_node
== NULL
) {
2201 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2204 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2215 * Add context to a specific event in a channel for global UST domain.
2217 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2218 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2219 struct ltt_ust_context
*uctx
)
2222 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2223 struct cds_lfht_iter iter
, uiter
;
2224 struct ust_app_session
*ua_sess
;
2225 struct ust_app_event
*ua_event
;
2226 struct ust_app_channel
*ua_chan
= NULL
;
2227 struct ust_app
*app
;
2231 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
2232 ua_sess
= lookup_session_by_app(usess
, app
);
2233 if (ua_sess
== NULL
) {
2237 /* Lookup channel in the ust app session */
2238 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
2239 (void *)uchan
->name
, strlen(uchan
->name
), &uiter
);
2240 if (ua_chan_node
== NULL
) {
2243 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2246 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2247 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &uiter
);
2248 if (ua_event_node
== NULL
) {
2251 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2254 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2265 * Enable event for a channel from a UST session for a specific PID.
2267 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2268 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2271 struct cds_lfht_iter iter
;
2272 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2273 struct ust_app
*app
;
2274 struct ust_app_session
*ua_sess
;
2275 struct ust_app_channel
*ua_chan
;
2276 struct ust_app_event
*ua_event
;
2278 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2282 app
= ust_app_find_by_pid(pid
);
2284 ERR("UST app enable event per PID %d not found", pid
);
2289 ua_sess
= lookup_session_by_app(usess
, app
);
2290 /* If ua_sess is NULL, there is a code flow error */
2293 /* Lookup channel in the ust app session */
2294 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
2295 strlen(uchan
->name
), &iter
);
2296 /* If the channel is not found, there is a code flow error */
2297 assert(ua_chan_node
);
2299 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2301 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2302 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
2303 if (ua_event_node
== NULL
) {
2304 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2309 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2311 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2323 * Disable event for a channel from a UST session for a specific PID.
2325 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2326 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2329 struct cds_lfht_iter iter
;
2330 struct cds_lfht_node
*ua_chan_node
, *ua_event_node
;
2331 struct ust_app
*app
;
2332 struct ust_app_session
*ua_sess
;
2333 struct ust_app_channel
*ua_chan
;
2334 struct ust_app_event
*ua_event
;
2336 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2340 app
= ust_app_find_by_pid(pid
);
2342 ERR("UST app disable event per PID %d not found", pid
);
2347 ua_sess
= lookup_session_by_app(usess
, app
);
2348 /* If ua_sess is NULL, there is a code flow error */
2351 /* Lookup channel in the ust app session */
2352 ua_chan_node
= hashtable_lookup(ua_sess
->channels
, (void *)uchan
->name
,
2353 strlen(uchan
->name
), &iter
);
2354 if (ua_chan_node
== NULL
) {
2355 /* Channel does not exist, skip disabling */
2358 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2360 ua_event_node
= hashtable_lookup(ua_chan
->events
,
2361 (void*)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
2362 if (ua_event_node
== NULL
) {
2363 /* Event does not exist, skip disabling */
2366 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2368 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);