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>
28 #include <urcu/compiler.h>
30 #include <common/lttngerr.h>
31 #include <common/common.h>
34 #include "ust-consumer.h"
38 * Delete ust context safely. RCU read lock must be held before calling
42 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
45 ustctl_release_object(sock
, ua_ctx
->obj
);
52 * Delete ust app event safely. RCU read lock must be held before calling
56 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
59 struct lttng_ht_iter iter
;
60 struct ust_app_ctx
*ua_ctx
;
62 /* Destroy each context of event */
63 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter
.iter
, ua_ctx
,
65 ret
= lttng_ht_del(ua_event
->ctx
, &iter
);
67 delete_ust_app_ctx(sock
, ua_ctx
);
69 lttng_ht_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 lttng_ht_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
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
113 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
115 delete_ust_app_ctx(sock
, ua_ctx
);
117 lttng_ht_destroy(ua_chan
->ctx
);
120 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
122 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
124 delete_ust_app_event(sock
, ua_event
);
126 lttng_ht_destroy(ua_chan
->events
);
128 if (ua_chan
->obj
!= NULL
) {
129 ustctl_release_object(sock
, ua_chan
->obj
);
136 * Delete ust app session safely. RCU read lock must be held before calling
140 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
)
143 struct lttng_ht_iter iter
;
144 struct ust_app_channel
*ua_chan
;
146 if (ua_sess
->metadata
) {
147 if (ua_sess
->metadata
->stream_obj
) {
148 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
149 free(ua_sess
->metadata
->stream_obj
);
151 if (ua_sess
->metadata
->obj
) {
152 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
153 free(ua_sess
->metadata
->obj
);
157 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
159 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
161 delete_ust_app_channel(sock
, ua_chan
);
163 lttng_ht_destroy(ua_sess
->channels
);
165 if (ua_sess
->handle
!= -1) {
166 ustctl_release_handle(sock
, ua_sess
->handle
);
172 * Delete a traceable application structure from the global list. Never call
173 * this function outside of a call_rcu call.
176 void delete_ust_app(struct ust_app
*app
)
179 struct lttng_ht_iter iter
;
180 struct ust_app_session
*ua_sess
;
184 /* Delete ust app sessions info */
185 sock
= app
->key
.sock
;
189 cds_lfht_for_each_entry(app
->sessions
->ht
, &iter
.iter
, ua_sess
,
191 ret
= lttng_ht_del(app
->sessions
, &iter
);
193 delete_ust_app_session(app
->key
.sock
, ua_sess
);
195 lttng_ht_destroy(app
->sessions
);
198 * Wait until we have removed the key from the sock hash table before
199 * closing this socket, otherwise an application could re-use the socket ID
200 * and race with the teardown, using the same hash table entry.
204 DBG2("UST app pid %d deleted", app
->key
.pid
);
211 * URCU intermediate call to delete an UST app.
214 void delete_ust_app_rcu(struct rcu_head
*head
)
216 struct lttng_ht_node_ulong
*node
=
217 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
218 struct ust_app
*app
=
219 caa_container_of(node
, struct ust_app
, node
);
221 DBG3("Call RCU deleting app PID %d", app
->key
.pid
);
226 * Alloc new UST app session.
229 struct ust_app_session
*alloc_ust_app_session(void)
231 struct ust_app_session
*ua_sess
;
233 /* Init most of the default value by allocating and zeroing */
234 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
235 if (ua_sess
== NULL
) {
240 ua_sess
->handle
= -1;
241 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
250 * Alloc new UST app channel.
253 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
254 struct lttng_ust_channel
*attr
)
256 struct ust_app_channel
*ua_chan
;
258 /* Init most of the default value by allocating and zeroing */
259 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
260 if (ua_chan
== NULL
) {
265 /* Setup channel name */
266 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
267 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
269 ua_chan
->enabled
= 1;
270 ua_chan
->handle
= -1;
271 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
272 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
273 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
275 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
277 /* Copy attributes */
279 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
282 DBG3("UST app channel %s allocated", ua_chan
->name
);
291 * Alloc new UST app event.
294 struct ust_app_event
*alloc_ust_app_event(char *name
,
295 struct lttng_ust_event
*attr
)
297 struct ust_app_event
*ua_event
;
299 /* Init most of the default value by allocating and zeroing */
300 ua_event
= zmalloc(sizeof(struct ust_app_event
));
301 if (ua_event
== NULL
) {
306 ua_event
->enabled
= 1;
307 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
308 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
309 ua_event
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
310 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
312 /* Copy attributes */
314 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
317 DBG3("UST app event %s allocated", ua_event
->name
);
326 * Alloc new UST app context.
329 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
331 struct ust_app_ctx
*ua_ctx
;
333 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
334 if (ua_ctx
== NULL
) {
339 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
342 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
349 * Find an ust_app using the sock and return it. RCU read side lock must be
350 * held before calling this helper function.
353 struct ust_app
*find_app_by_sock(int sock
)
355 struct lttng_ht_node_ulong
*node
;
356 struct ust_app_key
*key
;
357 struct lttng_ht_iter iter
;
359 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
),
361 node
= lttng_ht_iter_get_node_ulong(&iter
);
363 DBG2("UST app find by sock %d key not found", sock
);
366 key
= caa_container_of(node
, struct ust_app_key
, node
);
368 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) key
->pid
), &iter
);
369 node
= lttng_ht_iter_get_node_ulong(&iter
);
371 DBG2("UST app find by sock %d not found", sock
);
374 return caa_container_of(node
, struct ust_app
, node
);
381 * Create the channel context on the tracer.
384 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
385 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
389 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
390 ua_chan
->obj
, &ua_ctx
->obj
);
395 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
397 DBG2("UST app context added to channel %s successfully", ua_chan
->name
);
404 * Create the event context on the tracer.
407 int create_ust_event_context(struct ust_app_event
*ua_event
,
408 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
412 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
413 ua_event
->obj
, &ua_ctx
->obj
);
418 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
420 DBG2("UST app context added to event %s successfully", ua_event
->name
);
427 * Disable the specified event on to UST tracer for the UST session.
429 static int disable_ust_event(struct ust_app
*app
,
430 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
434 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
436 ERR("UST app event %s disable failed for app (pid: %d) "
437 "and session handle %d with ret %d",
438 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
442 DBG2("UST app event %s disabled successfully for app (pid: %d)",
443 ua_event
->attr
.name
, app
->key
.pid
);
450 * Disable the specified channel on to UST tracer for the UST session.
452 static int disable_ust_channel(struct ust_app
*app
,
453 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
457 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
459 ERR("UST app channel %s disable failed for app (pid: %d) "
460 "and session handle %d with ret %d",
461 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
465 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
466 ua_chan
->name
, app
->key
.pid
);
473 * Enable the specified channel on to UST tracer for the UST session.
475 static int enable_ust_channel(struct ust_app
*app
,
476 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
480 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
482 ERR("UST app channel %s enable failed for app (pid: %d) "
483 "and session handle %d with ret %d",
484 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
488 ua_chan
->enabled
= 1;
490 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
491 ua_chan
->name
, app
->key
.pid
);
498 * Enable the specified event on to UST tracer for the UST session.
500 static int enable_ust_event(struct ust_app
*app
,
501 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
505 ret
= ustctl_enable(app
->key
.sock
, ua_event
->obj
);
507 ERR("UST app event %s enable failed for app (pid: %d) "
508 "and session handle %d with ret %d",
509 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
513 DBG2("UST app event %s enabled successfully for app (pid: %d)",
514 ua_event
->attr
.name
, app
->key
.pid
);
521 * Open metadata onto the UST tracer for a UST session.
523 static int open_ust_metadata(struct ust_app
*app
,
524 struct ust_app_session
*ua_sess
)
527 struct lttng_ust_channel_attr uattr
;
529 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
530 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
531 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
532 uattr
.switch_timer_interval
=
533 ua_sess
->metadata
->attr
.switch_timer_interval
;
534 uattr
.read_timer_interval
=
535 ua_sess
->metadata
->attr
.read_timer_interval
;
536 uattr
.output
= ua_sess
->metadata
->attr
.output
;
538 /* UST tracer metadata creation */
539 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
540 &ua_sess
->metadata
->obj
);
542 ERR("UST app open metadata failed for app pid:%d with ret %d",
547 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
554 * Create stream onto the UST tracer for a UST session.
556 static int create_ust_stream(struct ust_app
*app
,
557 struct ust_app_session
*ua_sess
)
561 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
562 &ua_sess
->metadata
->stream_obj
);
564 ERR("UST create metadata stream failed");
573 * Create the specified channel onto the UST tracer for a UST session.
575 static int create_ust_channel(struct ust_app
*app
,
576 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
580 /* TODO: remove cast and use lttng-ust-abi.h */
581 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
582 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
584 ERR("Creating channel %s for app (pid: %d, sock: %d) "
585 "and session handle %d with ret %d",
586 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
587 ua_sess
->handle
, ret
);
591 ua_chan
->handle
= ua_chan
->obj
->handle
;
593 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
594 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
596 /* If channel is not enabled, disable it on the tracer */
597 if (!ua_chan
->enabled
) {
598 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
609 * Create the specified event onto the UST tracer for a UST session.
612 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
613 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
617 /* Create UST event on tracer */
618 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
621 if (ret
== -EEXIST
) {
625 ERR("Error ustctl create event %s for app pid: %d with ret %d",
626 ua_event
->attr
.name
, app
->key
.pid
, ret
);
630 ua_event
->handle
= ua_event
->obj
->handle
;
632 DBG2("UST app event %s created successfully for pid:%d",
633 ua_event
->attr
.name
, app
->key
.pid
);
635 /* If event not enabled, disable it on the tracer */
636 if (ua_event
->enabled
== 0) {
637 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
640 * If we hit an EPERM, something is wrong with our disable call. If
641 * we get an EEXIST, there is a problem on the tracer side since we
646 /* Code flow problem */
649 /* It's OK for our use case. */
664 * Copy data between an UST app event and a LTT event.
666 static void shadow_copy_event(struct ust_app_event
*ua_event
,
667 struct ltt_ust_event
*uevent
)
669 struct lttng_ht_iter iter
;
670 struct ltt_ust_context
*uctx
;
671 struct ust_app_ctx
*ua_ctx
;
673 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
674 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
676 ua_event
->enabled
= uevent
->enabled
;
678 /* Copy event attributes */
679 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
681 cds_lfht_for_each_entry(uevent
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
682 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
683 if (ua_ctx
== NULL
) {
684 /* malloc() failed. We should simply stop */
688 lttng_ht_node_init_ulong(&ua_ctx
->node
,
689 (unsigned long) ua_ctx
->ctx
.ctx
);
690 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
695 * Copy data between an UST app channel and a LTT channel.
697 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
698 struct ltt_ust_channel
*uchan
)
700 struct lttng_ht_iter iter
;
701 struct lttng_ht_node_str
*ua_event_node
;
702 struct ltt_ust_event
*uevent
;
703 struct ltt_ust_context
*uctx
;
704 struct ust_app_event
*ua_event
;
705 struct ust_app_ctx
*ua_ctx
;
707 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
709 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
710 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
711 /* Copy event attributes */
712 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
714 ua_chan
->enabled
= uchan
->enabled
;
716 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
717 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
718 if (ua_ctx
== NULL
) {
721 lttng_ht_node_init_ulong(&ua_ctx
->node
,
722 (unsigned long) ua_ctx
->ctx
.ctx
);
723 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
726 /* Copy all events from ltt ust channel to ust app channel */
727 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
728 struct lttng_ht_iter uiter
;
730 lttng_ht_lookup(ua_chan
->events
, (void *) uevent
->attr
.name
, &uiter
);
731 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
732 if (ua_event_node
== NULL
) {
733 DBG2("UST event %s not found on shadow copy channel",
735 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
736 if (ua_event
== NULL
) {
739 shadow_copy_event(ua_event
, uevent
);
740 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
744 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
748 * Copy data between a UST app session and a regular LTT session.
750 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
751 struct ltt_ust_session
*usess
, struct ust_app
*app
)
753 struct lttng_ht_node_str
*ua_chan_node
;
754 struct lttng_ht_iter iter
;
755 struct ltt_ust_channel
*uchan
;
756 struct ust_app_channel
*ua_chan
;
762 /* Get date and time for unique app path */
764 timeinfo
= localtime(&rawtime
);
765 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
767 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
769 ua_sess
->id
= usess
->id
;
770 ua_sess
->uid
= usess
->uid
;
771 ua_sess
->gid
= usess
->gid
;
773 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s/%s-%d-%s", usess
->pathname
,
774 app
->name
, app
->key
.pid
, datetime
);
776 PERROR("asprintf UST shadow copy session");
777 /* TODO: We cannot return an error from here.. */
781 /* TODO: support all UST domain */
783 /* Iterate over all channels in global domain. */
784 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
786 struct lttng_ht_iter uiter
;
788 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
789 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
790 if (ua_chan_node
!= NULL
) {
791 /* Session exist. Contiuing. */
795 DBG2("Channel %s not found on shadow session copy, creating it",
797 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
798 if (ua_chan
== NULL
) {
799 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
803 shadow_copy_channel(ua_chan
, uchan
);
804 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
809 * Lookup sesison wrapper.
812 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
813 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
815 /* Get right UST app session from app */
816 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
820 * Return ust app session from the app session hashtable using the UST session
823 static struct ust_app_session
*lookup_session_by_app(
824 struct ltt_ust_session
*usess
, struct ust_app
*app
)
826 struct lttng_ht_iter iter
;
827 struct lttng_ht_node_ulong
*node
;
829 __lookup_session_by_app(usess
, app
, &iter
);
830 node
= lttng_ht_iter_get_node_ulong(&iter
);
835 return caa_container_of(node
, struct ust_app_session
, node
);
842 * Create a UST session onto the tracer of app and add it the session
845 * Return ust app session or NULL on error.
847 static struct ust_app_session
*create_ust_app_session(
848 struct ltt_ust_session
*usess
, struct ust_app
*app
)
851 struct ust_app_session
*ua_sess
;
853 ua_sess
= lookup_session_by_app(usess
, app
);
854 if (ua_sess
== NULL
) {
855 DBG2("UST app pid: %d session id %d not found, creating it",
856 app
->key
.pid
, usess
->id
);
857 ua_sess
= alloc_ust_app_session();
858 if (ua_sess
== NULL
) {
859 /* Only malloc can failed so something is really wrong */
862 shadow_copy_session(ua_sess
, usess
, app
);
865 if (ua_sess
->handle
== -1) {
866 ret
= ustctl_create_session(app
->key
.sock
);
868 ERR("Creating session for app pid %d", app
->key
.pid
);
872 ua_sess
->handle
= ret
;
874 /* Add ust app session to app's HT */
875 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
876 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
878 DBG2("UST app session created successfully with handle %d", ret
);
885 delete_ust_app_session(-1, ua_sess
);
890 * Create a context for the channel on the tracer.
893 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
894 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
898 struct lttng_ht_iter iter
;
899 struct lttng_ht_node_ulong
*node
;
900 struct ust_app_ctx
*ua_ctx
;
902 DBG2("UST app adding context to channel %s", ua_chan
->name
);
904 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
905 node
= lttng_ht_iter_get_node_ulong(&iter
);
911 ua_ctx
= alloc_ust_app_ctx(uctx
);
912 if (ua_ctx
== NULL
) {
918 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
919 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
921 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
931 * Create an UST context and enable it for the event on the tracer.
934 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
935 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
939 struct lttng_ht_iter iter
;
940 struct lttng_ht_node_ulong
*node
;
941 struct ust_app_ctx
*ua_ctx
;
943 DBG2("UST app adding context to event %s", ua_event
->name
);
945 lttng_ht_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
946 node
= lttng_ht_iter_get_node_ulong(&iter
);
952 ua_ctx
= alloc_ust_app_ctx(uctx
);
953 if (ua_ctx
== NULL
) {
959 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
960 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
962 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
972 * Enable on the tracer side a ust app event for the session and channel.
975 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
976 struct ust_app_event
*ua_event
, struct ust_app
*app
)
980 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
985 ua_event
->enabled
= 1;
992 * Disable on the tracer side a ust app event for the session and channel.
994 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
995 struct ust_app_event
*ua_event
, struct ust_app
*app
)
999 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1004 ua_event
->enabled
= 0;
1011 * Lookup ust app channel for session and disable it on the tracer side.
1014 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1015 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1019 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1024 ua_chan
->enabled
= 0;
1031 * Lookup ust app channel for session and enable it on the tracer side.
1033 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1034 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1037 struct lttng_ht_iter iter
;
1038 struct lttng_ht_node_str
*ua_chan_node
;
1039 struct ust_app_channel
*ua_chan
;
1041 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1042 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1043 if (ua_chan_node
== NULL
) {
1044 DBG2("Unable to find channel %s in ust session id %u",
1045 uchan
->name
, ua_sess
->id
);
1049 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1051 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1061 * Create UST app channel and create it on the tracer.
1063 static struct ust_app_channel
*create_ust_app_channel(
1064 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1065 struct ust_app
*app
)
1068 struct lttng_ht_iter iter
;
1069 struct lttng_ht_node_str
*ua_chan_node
;
1070 struct ust_app_channel
*ua_chan
;
1072 /* Lookup channel in the ust app session */
1073 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1074 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1075 if (ua_chan_node
!= NULL
) {
1076 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1080 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1081 if (ua_chan
== NULL
) {
1082 /* Only malloc can fail here */
1085 shadow_copy_channel(ua_chan
, uchan
);
1087 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1089 /* Not found previously means that it does not exist on the tracer */
1090 assert(ret
!= -EEXIST
);
1094 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1096 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1103 delete_ust_app_channel(-1, ua_chan
);
1108 * Create UST app event and create it on the tracer side.
1111 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1112 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1113 struct ust_app
*app
)
1116 struct lttng_ht_iter iter
;
1117 struct lttng_ht_node_str
*ua_event_node
;
1118 struct ust_app_event
*ua_event
;
1120 /* Get event node */
1121 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
1122 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
1123 if (ua_event_node
!= NULL
) {
1128 /* Does not exist so create one */
1129 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1130 if (ua_event
== NULL
) {
1131 /* Only malloc can failed so something is really wrong */
1135 shadow_copy_event(ua_event
, uevent
);
1137 /* Create it on the tracer side */
1138 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1140 /* Not found previously means that it does not exist on the tracer */
1141 assert(ret
!= -EEXIST
);
1145 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
1147 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1154 /* Valid. Calling here is already in a read side lock */
1155 delete_ust_app_event(-1, ua_event
);
1160 * Create UST metadata and open it on the tracer side.
1162 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1163 char *pathname
, struct ust_app
*app
)
1167 if (ua_sess
->metadata
== NULL
) {
1168 /* Allocate UST metadata */
1169 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1170 if (ua_sess
->metadata
== NULL
) {
1171 /* malloc() failed */
1175 ret
= open_ust_metadata(app
, ua_sess
);
1177 DBG3("Opening metadata failed. Cleaning up memory");
1179 /* Cleanup failed metadata struct */
1180 free(ua_sess
->metadata
);
1182 * This is very important because delete_ust_app_session check if
1183 * the pointer is null or not in order to delete the metadata.
1185 ua_sess
->metadata
= NULL
;
1189 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
1192 /* Open UST metadata stream */
1193 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1194 ret
= create_ust_stream(app
, ua_sess
);
1199 ret
= run_as_mkdir(ua_sess
->path
, S_IRWXU
| S_IRWXG
,
1200 ua_sess
->uid
, ua_sess
->gid
);
1202 PERROR("mkdir UST metadata");
1206 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1207 "%s/metadata", ua_sess
->path
);
1209 PERROR("asprintf UST create stream");
1213 DBG2("UST metadata stream object created for app pid %d",
1216 ERR("Attempting to create stream without metadata opened");
1227 * Return pointer to traceable apps list.
1229 struct lttng_ht
*ust_app_get_ht(void)
1235 * Return ust app pointer or NULL if not found.
1237 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1239 struct lttng_ht_node_ulong
*node
;
1240 struct lttng_ht_iter iter
;
1243 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1244 node
= lttng_ht_iter_get_node_ulong(&iter
);
1246 DBG2("UST app no found with pid %d", pid
);
1251 DBG2("Found UST app by pid %d", pid
);
1253 return caa_container_of(node
, struct ust_app
, node
);
1261 * Using pid and uid (of the app), allocate a new ust_app struct and
1262 * add it to the global traceable app list.
1264 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1265 * bitness is not supported.
1267 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1269 struct ust_app
*lta
;
1271 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1272 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1273 ERR("Registration failed: application \"%s\" (pid: %d) has "
1274 "%d-bit long, but no consumerd for this long size is available.\n",
1275 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1279 lta
= zmalloc(sizeof(struct ust_app
));
1285 lta
->ppid
= msg
->ppid
;
1286 lta
->uid
= msg
->uid
;
1287 lta
->gid
= msg
->gid
;
1288 lta
->bits_per_long
= msg
->bits_per_long
;
1289 lta
->v_major
= msg
->major
;
1290 lta
->v_minor
= msg
->minor
;
1291 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1292 lta
->name
[16] = '\0';
1293 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1296 lta
->key
.pid
= msg
->pid
;
1297 lttng_ht_node_init_ulong(<a
->node
, (unsigned long)lta
->key
.pid
);
1298 lta
->key
.sock
= sock
;
1299 lttng_ht_node_init_ulong(<a
->key
.node
, (unsigned long)lta
->key
.sock
);
1302 lttng_ht_add_unique_ulong(ust_app_sock_key_map
, <a
->key
.node
);
1303 lttng_ht_add_unique_ulong(ust_app_ht
, <a
->node
);
1306 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1307 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1308 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1314 * Unregister app by removing it from the global traceable app list and freeing
1317 * The socket is already closed at this point so no close to sock.
1319 void ust_app_unregister(int sock
)
1321 struct ust_app
*lta
;
1322 struct lttng_ht_node_ulong
*node
;
1323 struct lttng_ht_iter iter
;
1327 lta
= find_app_by_sock(sock
);
1329 ERR("Unregister app sock %d not found!", sock
);
1333 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1335 /* Remove application from socket hash table */
1336 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
), &iter
);
1337 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1340 /* Get the node reference for a call_rcu */
1341 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) lta
->key
.pid
), &iter
);
1342 node
= lttng_ht_iter_get_node_ulong(&iter
);
1344 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1348 /* Remove application from PID hash table */
1349 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1351 call_rcu(&node
->head
, delete_ust_app_rcu
);
1358 * Return traceable_app_count
1360 unsigned long ust_app_list_count(void)
1362 unsigned long count
;
1365 count
= lttng_ht_get_count(ust_app_ht
);
1372 * Fill events array with all events name of all registered apps.
1374 int ust_app_list_events(struct lttng_event
**events
)
1377 size_t nbmem
, count
= 0;
1378 struct lttng_ht_iter iter
;
1379 struct ust_app
*app
;
1380 struct lttng_event
*tmp
;
1382 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1383 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1385 PERROR("zmalloc ust app events");
1392 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1393 struct lttng_ust_tracepoint_iter uiter
;
1395 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1397 ERR("UST app list events getting handle failed for app pid %d",
1402 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1403 &uiter
)) != -ENOENT
) {
1404 if (count
>= nbmem
) {
1405 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1408 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1410 PERROR("realloc ust app events");
1415 memcpy(tmp
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1416 memcpy(tmp
[count
].loglevel
, uiter
.loglevel
, LTTNG_UST_SYM_NAME_LEN
);
1417 tmp
[count
].loglevel_value
= uiter
.loglevel_value
;
1418 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1419 tmp
[count
].pid
= app
->key
.pid
;
1420 tmp
[count
].enabled
= -1;
1428 DBG2("UST app list events done (%zu events)", count
);
1437 * Free and clean all traceable apps of the global list.
1439 void ust_app_clean_list(void)
1442 struct lttng_ht_iter iter
;
1443 struct lttng_ht_node_ulong
*node
;
1445 DBG2("UST app cleaning registered apps hash table");
1449 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, node
, node
) {
1450 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1452 call_rcu(&node
->head
, delete_ust_app_rcu
);
1454 /* Destroy is done only when the ht is empty */
1455 lttng_ht_destroy(ust_app_ht
);
1457 cds_lfht_for_each_entry(ust_app_sock_key_map
->ht
, &iter
.iter
, node
, node
) {
1458 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1461 /* Destroy is done only when the ht is empty */
1462 lttng_ht_destroy(ust_app_sock_key_map
);
1468 * Init UST app hash table.
1470 void ust_app_ht_alloc(void)
1472 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1473 ust_app_sock_key_map
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1477 * For a specific UST session, disable the channel for all registered apps.
1479 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1480 struct ltt_ust_channel
*uchan
)
1483 struct lttng_ht_iter iter
;
1484 struct lttng_ht_node_str
*ua_chan_node
;
1485 struct ust_app
*app
;
1486 struct ust_app_session
*ua_sess
;
1487 struct ust_app_channel
*ua_chan
;
1489 if (usess
== NULL
|| uchan
== NULL
) {
1490 ERR("Disabling UST global channel with NULL values");
1495 DBG2("UST app disabling channel %s from global domain for session id %d",
1496 uchan
->name
, usess
->id
);
1500 /* For every registered applications */
1501 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1502 struct lttng_ht_iter uiter
;
1503 ua_sess
= lookup_session_by_app(usess
, app
);
1504 if (ua_sess
== NULL
) {
1509 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1510 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1511 /* If the session if found for the app, the channel must be there */
1512 assert(ua_chan_node
);
1514 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1515 /* The channel must not be already disabled */
1516 assert(ua_chan
->enabled
== 1);
1518 /* Disable channel onto application */
1519 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1521 /* XXX: We might want to report this error at some point... */
1533 * For a specific UST session, enable the channel for all registered apps.
1535 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1536 struct ltt_ust_channel
*uchan
)
1539 struct lttng_ht_iter iter
;
1540 struct ust_app
*app
;
1541 struct ust_app_session
*ua_sess
;
1543 if (usess
== NULL
|| uchan
== NULL
) {
1544 ERR("Adding UST global channel to NULL values");
1549 DBG2("UST app enabling channel %s to global domain for session id %d",
1550 uchan
->name
, usess
->id
);
1554 /* For every registered applications */
1555 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1556 ua_sess
= lookup_session_by_app(usess
, app
);
1557 if (ua_sess
== NULL
) {
1561 /* Enable channel onto application */
1562 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1564 /* XXX: We might want to report this error at some point... */
1576 * Disable an event in a channel and for a specific session.
1578 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1579 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1582 struct lttng_ht_iter iter
, uiter
;
1583 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1584 struct ust_app
*app
;
1585 struct ust_app_session
*ua_sess
;
1586 struct ust_app_channel
*ua_chan
;
1587 struct ust_app_event
*ua_event
;
1589 DBG("UST app disabling event %s for all apps in channel "
1590 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
1594 /* For all registered applications */
1595 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1596 ua_sess
= lookup_session_by_app(usess
, app
);
1597 if (ua_sess
== NULL
) {
1602 /* Lookup channel in the ust app session */
1603 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1604 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1605 if (ua_chan_node
== NULL
) {
1606 DBG2("Channel %s not found in session id %d for app pid %d."
1607 "Skipping", uchan
->name
, usess
->id
, app
->key
.pid
);
1610 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1612 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
1613 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1614 if (ua_event_node
== NULL
) {
1615 DBG2("Event %s not found in channel %s for app pid %d."
1616 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1619 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1621 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1623 /* XXX: Report error someday... */
1634 * For a specific UST session and UST channel, the event for all
1637 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1638 struct ltt_ust_channel
*uchan
)
1641 struct lttng_ht_iter iter
, uiter
;
1642 struct lttng_ht_node_str
*ua_chan_node
;
1643 struct ust_app
*app
;
1644 struct ust_app_session
*ua_sess
;
1645 struct ust_app_channel
*ua_chan
;
1646 struct ust_app_event
*ua_event
;
1648 DBG("UST app disabling all event for all apps in channel "
1649 "%s for session id %d", uchan
->name
, usess
->id
);
1653 /* For all registered applications */
1654 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1655 ua_sess
= lookup_session_by_app(usess
, app
);
1656 /* If ua_sess is NULL, there is a code flow error */
1659 /* Lookup channel in the ust app session */
1660 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1661 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1662 /* If the channel is not found, there is a code flow error */
1663 assert(ua_chan_node
);
1665 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1667 /* Disable each events of channel */
1668 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
1670 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1672 /* XXX: Report error someday... */
1684 * For a specific UST session, create the channel for all registered apps.
1686 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1687 struct ltt_ust_channel
*uchan
)
1689 struct lttng_ht_iter iter
;
1690 struct ust_app
*app
;
1691 struct ust_app_session
*ua_sess
;
1692 struct ust_app_channel
*ua_chan
;
1694 /* Very wrong code flow */
1698 DBG2("UST app adding channel %s to global domain for session id %d",
1699 uchan
->name
, usess
->id
);
1703 /* For every registered applications */
1704 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1706 * Create session on the tracer side and add it to app session HT. Note
1707 * that if session exist, it will simply return a pointer to the ust
1710 ua_sess
= create_ust_app_session(usess
, app
);
1711 if (ua_sess
== NULL
) {
1712 /* Major problem here and it's maybe the tracer or malloc() */
1716 /* Create channel onto application */
1717 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1718 if (ua_chan
== NULL
) {
1719 /* Major problem here and it's maybe the tracer or malloc() */
1733 * Enable event for a specific session and channel on the tracer.
1735 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1736 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1739 struct lttng_ht_iter iter
, uiter
;
1740 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1741 struct ust_app
*app
;
1742 struct ust_app_session
*ua_sess
;
1743 struct ust_app_channel
*ua_chan
;
1744 struct ust_app_event
*ua_event
;
1746 DBG("UST app enabling event %s for all apps for session id %d",
1747 uevent
->attr
.name
, usess
->id
);
1750 * NOTE: At this point, this function is called only if the session and
1751 * channel passed are already created for all apps. and enabled on the
1757 /* For all registered applications */
1758 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1759 ua_sess
= lookup_session_by_app(usess
, app
);
1760 /* If ua_sess is NULL, there is a code flow error */
1763 /* Lookup channel in the ust app session */
1764 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1765 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1766 /* If the channel is not found, there is a code flow error */
1767 assert(ua_chan_node
);
1769 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1771 lttng_ht_lookup(ua_chan
->events
, (void*)uevent
->attr
.name
, &uiter
);
1772 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1773 if (ua_event_node
== NULL
) {
1774 DBG3("UST app enable event %s not found for app PID %d."
1775 "Skipping app", uevent
->attr
.name
, app
->key
.pid
);
1778 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1780 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1792 * For a specific existing UST session and UST channel, creates the event for
1793 * all registered apps.
1795 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1796 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1799 struct lttng_ht_iter iter
, uiter
;
1800 struct lttng_ht_node_str
*ua_chan_node
;
1801 struct ust_app
*app
;
1802 struct ust_app_session
*ua_sess
;
1803 struct ust_app_channel
*ua_chan
;
1805 DBG("UST app creating event %s for all apps for session id %d",
1806 uevent
->attr
.name
, usess
->id
);
1810 /* For all registered applications */
1811 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1812 ua_sess
= lookup_session_by_app(usess
, app
);
1813 /* If ua_sess is NULL, there is a code flow error */
1816 /* Lookup channel in the ust app session */
1817 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1818 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1819 /* If the channel is not found, there is a code flow error */
1820 assert(ua_chan_node
);
1822 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1824 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1826 if (ret
!= -EEXIST
) {
1827 /* Possible value at this point: -ENOMEM. If so, we stop! */
1830 DBG2("UST app event %s already exist on app PID %d",
1831 uevent
->attr
.name
, app
->key
.pid
);
1842 * Start tracing for a specific UST session and app.
1844 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1847 struct lttng_ht_iter iter
;
1848 struct ust_app_session
*ua_sess
;
1849 struct ust_app_channel
*ua_chan
;
1850 struct ltt_ust_stream
*ustream
;
1853 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1857 ua_sess
= lookup_session_by_app(usess
, app
);
1858 if (ua_sess
== NULL
) {
1859 goto error_rcu_unlock
;
1862 /* Upon restart, we skip the setup, already done */
1863 if (ua_sess
->started
) {
1867 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1869 goto error_rcu_unlock
;
1872 /* For each channel */
1873 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1875 /* Create all streams */
1877 /* Create UST stream */
1878 ustream
= zmalloc(sizeof(*ustream
));
1879 if (ustream
== NULL
) {
1880 PERROR("zmalloc ust stream");
1881 goto error_rcu_unlock
;
1884 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1887 /* Got all streams */
1890 ustream
->handle
= ustream
->obj
->handle
;
1892 /* Order is important */
1893 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1894 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1895 ua_sess
->path
, ua_chan
->name
,
1896 ua_chan
->streams
.count
++);
1898 PERROR("asprintf UST create stream");
1901 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1906 switch (app
->bits_per_long
) {
1908 consumerd_fd
= ust_consumerd64_fd
;
1911 consumerd_fd
= ust_consumerd32_fd
;
1915 goto error_rcu_unlock
;
1918 /* Setup UST consumer socket and send fds to it */
1919 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1921 goto error_rcu_unlock
;
1923 ua_sess
->started
= 1;
1926 /* This start the UST tracing */
1927 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1929 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1930 goto error_rcu_unlock
;
1935 /* Quiescent wait after starting trace */
1936 ustctl_wait_quiescent(app
->key
.sock
);
1946 * Stop tracing for a specific UST session and app.
1948 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1951 struct lttng_ht_iter iter
;
1952 struct ust_app_session
*ua_sess
;
1953 struct ust_app_channel
*ua_chan
;
1955 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1959 ua_sess
= lookup_session_by_app(usess
, app
);
1960 if (ua_sess
== NULL
) {
1961 /* Only malloc can failed so something is really wrong */
1962 goto error_rcu_unlock
;
1965 /* Not started, continuing. */
1966 if (ua_sess
->started
== 0) {
1970 /* This inhibits UST tracing */
1971 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1973 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1974 goto error_rcu_unlock
;
1977 /* Quiescent wait after stopping trace */
1978 ustctl_wait_quiescent(app
->key
.sock
);
1980 /* Flushing buffers */
1981 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1983 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_chan
->obj
);
1985 ERR("UST app PID %d channel %s flush failed with ret %d",
1986 app
->key
.pid
, ua_chan
->name
, ret
);
1987 /* Continuing flushing all buffers */
1992 /* Flush all buffers before stopping */
1993 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_sess
->metadata
->obj
);
1995 ERR("UST app PID %d metadata flush failed with ret %d", app
->key
.pid
,
1999 ua_sess
->started
= 0;
2011 * Destroy a specific UST session in apps.
2013 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2015 struct ust_app_session
*ua_sess
;
2016 struct lttng_ust_object_data obj
;
2017 struct lttng_ht_iter iter
;
2018 struct lttng_ht_node_ulong
*node
;
2021 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
2025 __lookup_session_by_app(usess
, app
, &iter
);
2026 node
= lttng_ht_iter_get_node_ulong(&iter
);
2028 /* Only malloc can failed so something is really wrong */
2029 goto error_rcu_unlock
;
2031 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2032 ret
= lttng_ht_del(app
->sessions
, &iter
);
2034 obj
.handle
= ua_sess
->handle
;
2037 obj
.memory_map_size
= 0;
2038 ustctl_release_object(app
->key
.sock
, &obj
);
2040 delete_ust_app_session(app
->key
.sock
, ua_sess
);
2044 /* Quiescent wait after stopping trace */
2045 ustctl_wait_quiescent(app
->key
.sock
);
2055 * Start tracing for the UST session.
2057 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2060 struct lttng_ht_iter iter
;
2061 struct ust_app
*app
;
2063 DBG("Starting all UST traces");
2067 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2068 ret
= ust_app_start_trace(usess
, app
);
2070 /* Continue to next apps even on error */
2081 * Start tracing for the UST session.
2083 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2086 struct lttng_ht_iter iter
;
2087 struct ust_app
*app
;
2089 DBG("Stopping all UST traces");
2093 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2094 ret
= ust_app_stop_trace(usess
, app
);
2096 /* Continue to next apps even on error */
2107 * Destroy app UST session.
2109 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2112 struct lttng_ht_iter iter
;
2113 struct ust_app
*app
;
2115 DBG("Destroy all UST traces");
2119 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2120 ret
= ust_app_destroy_trace(usess
, app
);
2122 /* Continue to next apps even on error */
2133 * Add channels/events from UST global domain to registered apps at sock.
2135 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2138 struct lttng_ht_iter iter
, uiter
;
2139 struct ust_app
*app
;
2140 struct ust_app_session
*ua_sess
;
2141 struct ust_app_channel
*ua_chan
;
2142 struct ust_app_event
*ua_event
;
2144 if (usess
== NULL
) {
2145 ERR("No UST session on global update. Returning");
2149 DBG2("UST app global update for app sock %d for session id %d", sock
,
2154 app
= find_app_by_sock(sock
);
2156 ERR("Failed to update app sock %d", sock
);
2160 ua_sess
= create_ust_app_session(usess
, app
);
2161 if (ua_sess
== NULL
) {
2166 * We can iterate safely here over all UST app session sicne the create ust
2167 * app session above made a shadow copy of the UST global domain from the
2170 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2172 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2174 /* FIXME: Should we quit here or continue... */
2178 /* For each events */
2179 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2181 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2183 /* FIXME: Should we quit here or continue... */
2189 if (usess
->start_trace
) {
2190 ret
= ust_app_start_trace(usess
, app
);
2195 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2204 * Add context to a specific channel for global UST domain.
2206 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2207 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2210 struct lttng_ht_node_str
*ua_chan_node
;
2211 struct lttng_ht_iter iter
, uiter
;
2212 struct ust_app_channel
*ua_chan
= NULL
;
2213 struct ust_app_session
*ua_sess
;
2214 struct ust_app
*app
;
2218 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2219 ua_sess
= lookup_session_by_app(usess
, app
);
2220 if (ua_sess
== NULL
) {
2224 /* Lookup channel in the ust app session */
2225 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2226 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2227 if (ua_chan_node
== NULL
) {
2230 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2233 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2244 * Add context to a specific event in a channel for global UST domain.
2246 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2247 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2248 struct ltt_ust_context
*uctx
)
2251 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2252 struct lttng_ht_iter iter
, uiter
;
2253 struct ust_app_session
*ua_sess
;
2254 struct ust_app_event
*ua_event
;
2255 struct ust_app_channel
*ua_chan
= NULL
;
2256 struct ust_app
*app
;
2260 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2261 ua_sess
= lookup_session_by_app(usess
, app
);
2262 if (ua_sess
== NULL
) {
2266 /* Lookup channel in the ust app session */
2267 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2268 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2269 if (ua_chan_node
== NULL
) {
2272 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2275 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2276 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2277 if (ua_event_node
== NULL
) {
2280 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2283 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2294 * Enable event for a channel from a UST session for a specific PID.
2296 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2297 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2300 struct lttng_ht_iter iter
;
2301 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2302 struct ust_app
*app
;
2303 struct ust_app_session
*ua_sess
;
2304 struct ust_app_channel
*ua_chan
;
2305 struct ust_app_event
*ua_event
;
2307 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2311 app
= ust_app_find_by_pid(pid
);
2313 ERR("UST app enable event per PID %d not found", pid
);
2318 ua_sess
= lookup_session_by_app(usess
, app
);
2319 /* If ua_sess is NULL, there is a code flow error */
2322 /* Lookup channel in the ust app session */
2323 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2324 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2325 /* If the channel is not found, there is a code flow error */
2326 assert(ua_chan_node
);
2328 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2330 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2331 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2332 if (ua_event_node
== NULL
) {
2333 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2338 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2340 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2352 * Disable event for a channel from a UST session for a specific PID.
2354 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2355 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2358 struct lttng_ht_iter iter
;
2359 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2360 struct ust_app
*app
;
2361 struct ust_app_session
*ua_sess
;
2362 struct ust_app_channel
*ua_chan
;
2363 struct ust_app_event
*ua_event
;
2365 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2369 app
= ust_app_find_by_pid(pid
);
2371 ERR("UST app disable event per PID %d not found", pid
);
2376 ua_sess
= lookup_session_by_app(usess
, app
);
2377 /* If ua_sess is NULL, there is a code flow error */
2380 /* Lookup channel in the ust app session */
2381 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2382 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2383 if (ua_chan_node
== NULL
) {
2384 /* Channel does not exist, skip disabling */
2387 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2389 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2390 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2391 if (ua_event_node
== NULL
) {
2392 /* Event does not exist, skip disabling */
2395 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2397 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);