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/lttng-share.h>
32 #include <common/runas.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 lttng_ht_iter iter
;
61 struct ust_app_ctx
*ua_ctx
;
63 /* Destroy each context of event */
64 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter
.iter
, ua_ctx
,
66 ret
= lttng_ht_del(ua_event
->ctx
, &iter
);
68 delete_ust_app_ctx(sock
, ua_ctx
);
70 lttng_ht_destroy(ua_event
->ctx
);
72 if (ua_event
->obj
!= NULL
) {
73 ustctl_release_object(sock
, ua_event
->obj
);
80 * Delete ust app stream safely. RCU read lock must be held before calling
84 void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
87 ustctl_release_object(sock
, stream
->obj
);
94 * Delete ust app channel safely. RCU read lock must be held before calling
98 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
101 struct lttng_ht_iter iter
;
102 struct ust_app_event
*ua_event
;
103 struct ust_app_ctx
*ua_ctx
;
104 struct ltt_ust_stream
*stream
, *stmp
;
107 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
108 cds_list_del(&stream
->list
);
109 delete_ust_app_stream(sock
, stream
);
113 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
114 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
116 delete_ust_app_ctx(sock
, ua_ctx
);
118 lttng_ht_destroy(ua_chan
->ctx
);
121 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
123 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
125 delete_ust_app_event(sock
, ua_event
);
127 lttng_ht_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 lttng_ht_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
->ht
, &iter
.iter
, ua_chan
,
160 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
162 delete_ust_app_channel(sock
, ua_chan
);
164 lttng_ht_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 lttng_ht_iter iter
;
181 struct ust_app_session
*ua_sess
;
185 /* Delete ust app sessions info */
186 sock
= app
->key
.sock
;
190 cds_lfht_for_each_entry(app
->sessions
->ht
, &iter
.iter
, ua_sess
,
192 ret
= lttng_ht_del(app
->sessions
, &iter
);
194 delete_ust_app_session(app
->key
.sock
, ua_sess
);
196 lttng_ht_destroy(app
->sessions
);
199 * Wait until we have removed the key from the sock hash table before
200 * closing this socket, otherwise an application could re-use the socket ID
201 * and race with the teardown, using the same hash table entry.
205 DBG2("UST app pid %d deleted", app
->key
.pid
);
212 * URCU intermediate call to delete an UST app.
215 void delete_ust_app_rcu(struct rcu_head
*head
)
217 struct lttng_ht_node_ulong
*node
=
218 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
219 struct ust_app
*app
=
220 caa_container_of(node
, struct ust_app
, node
);
222 DBG3("Call RCU deleting app PID %d", app
->key
.pid
);
227 * Alloc new UST app session.
230 struct ust_app_session
*alloc_ust_app_session(void)
232 struct ust_app_session
*ua_sess
;
234 /* Init most of the default value by allocating and zeroing */
235 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
236 if (ua_sess
== NULL
) {
241 ua_sess
->handle
= -1;
242 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
251 * Alloc new UST app channel.
254 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
255 struct lttng_ust_channel
*attr
)
257 struct ust_app_channel
*ua_chan
;
259 /* Init most of the default value by allocating and zeroing */
260 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
261 if (ua_chan
== NULL
) {
266 /* Setup channel name */
267 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
268 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
270 ua_chan
->enabled
= 1;
271 ua_chan
->handle
= -1;
272 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
273 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
274 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
276 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
278 /* Copy attributes */
280 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
283 DBG3("UST app channel %s allocated", ua_chan
->name
);
292 * Alloc new UST app event.
295 struct ust_app_event
*alloc_ust_app_event(char *name
,
296 struct lttng_ust_event
*attr
)
298 struct ust_app_event
*ua_event
;
300 /* Init most of the default value by allocating and zeroing */
301 ua_event
= zmalloc(sizeof(struct ust_app_event
));
302 if (ua_event
== NULL
) {
307 ua_event
->enabled
= 1;
308 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
309 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
310 ua_event
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
311 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
313 /* Copy attributes */
315 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
318 DBG3("UST app event %s allocated", ua_event
->name
);
327 * Alloc new UST app context.
330 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
332 struct ust_app_ctx
*ua_ctx
;
334 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
335 if (ua_ctx
== NULL
) {
340 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
343 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
350 * Find an ust_app using the sock and return it. RCU read side lock must be
351 * held before calling this helper function.
354 struct ust_app
*find_app_by_sock(int sock
)
356 struct lttng_ht_node_ulong
*node
;
357 struct ust_app_key
*key
;
358 struct lttng_ht_iter iter
;
360 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
),
362 node
= lttng_ht_iter_get_node_ulong(&iter
);
364 DBG2("UST app find by sock %d key not found", sock
);
367 key
= caa_container_of(node
, struct ust_app_key
, node
);
369 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) key
->pid
), &iter
);
370 node
= lttng_ht_iter_get_node_ulong(&iter
);
372 DBG2("UST app find by sock %d not found", sock
);
375 return caa_container_of(node
, struct ust_app
, node
);
382 * Create the channel context on the tracer.
385 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
386 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
390 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
391 ua_chan
->obj
, &ua_ctx
->obj
);
396 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
398 DBG2("UST app context added to channel %s successfully", ua_chan
->name
);
405 * Create the event context on the tracer.
408 int create_ust_event_context(struct ust_app_event
*ua_event
,
409 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
413 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
414 ua_event
->obj
, &ua_ctx
->obj
);
419 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
421 DBG2("UST app context added to event %s successfully", ua_event
->name
);
428 * Disable the specified event on to UST tracer for the UST session.
430 static int disable_ust_event(struct ust_app
*app
,
431 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
435 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
437 ERR("UST app event %s disable failed for app (pid: %d) "
438 "and session handle %d with ret %d",
439 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
443 DBG2("UST app event %s disabled successfully for app (pid: %d)",
444 ua_event
->attr
.name
, app
->key
.pid
);
451 * Disable the specified channel on to UST tracer for the UST session.
453 static int disable_ust_channel(struct ust_app
*app
,
454 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
458 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
460 ERR("UST app channel %s disable failed for app (pid: %d) "
461 "and session handle %d with ret %d",
462 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
466 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
467 ua_chan
->name
, app
->key
.pid
);
474 * Enable the specified channel on to UST tracer for the UST session.
476 static int enable_ust_channel(struct ust_app
*app
,
477 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
481 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
483 ERR("UST app channel %s enable 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 ua_chan
->enabled
= 1;
491 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
492 ua_chan
->name
, app
->key
.pid
);
499 * Enable the specified event on to UST tracer for the UST session.
501 static int enable_ust_event(struct ust_app
*app
,
502 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
506 ret
= ustctl_enable(app
->key
.sock
, ua_event
->obj
);
508 ERR("UST app event %s enable failed for app (pid: %d) "
509 "and session handle %d with ret %d",
510 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
514 DBG2("UST app event %s enabled successfully for app (pid: %d)",
515 ua_event
->attr
.name
, app
->key
.pid
);
522 * Open metadata onto the UST tracer for a UST session.
524 static int open_ust_metadata(struct ust_app
*app
,
525 struct ust_app_session
*ua_sess
)
528 struct lttng_ust_channel_attr uattr
;
530 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
531 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
532 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
533 uattr
.switch_timer_interval
=
534 ua_sess
->metadata
->attr
.switch_timer_interval
;
535 uattr
.read_timer_interval
=
536 ua_sess
->metadata
->attr
.read_timer_interval
;
537 uattr
.output
= ua_sess
->metadata
->attr
.output
;
539 /* UST tracer metadata creation */
540 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
541 &ua_sess
->metadata
->obj
);
543 ERR("UST app open metadata failed for app pid:%d with ret %d",
548 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
555 * Create stream onto the UST tracer for a UST session.
557 static int create_ust_stream(struct ust_app
*app
,
558 struct ust_app_session
*ua_sess
)
562 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
563 &ua_sess
->metadata
->stream_obj
);
565 ERR("UST create metadata stream failed");
574 * Create the specified channel onto the UST tracer for a UST session.
576 static int create_ust_channel(struct ust_app
*app
,
577 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
581 /* TODO: remove cast and use lttng-ust-abi.h */
582 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
583 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
585 ERR("Creating channel %s for app (pid: %d, sock: %d) "
586 "and session handle %d with ret %d",
587 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
588 ua_sess
->handle
, ret
);
592 ua_chan
->handle
= ua_chan
->obj
->handle
;
594 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
595 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
597 /* If channel is not enabled, disable it on the tracer */
598 if (!ua_chan
->enabled
) {
599 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
610 * Create the specified event onto the UST tracer for a UST session.
613 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
614 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
618 /* Create UST event on tracer */
619 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
622 if (ret
== -EEXIST
) {
626 ERR("Error ustctl create event %s for app pid: %d with ret %d",
627 ua_event
->attr
.name
, app
->key
.pid
, ret
);
631 ua_event
->handle
= ua_event
->obj
->handle
;
633 DBG2("UST app event %s created successfully for pid:%d",
634 ua_event
->attr
.name
, app
->key
.pid
);
636 /* If event not enabled, disable it on the tracer */
637 if (ua_event
->enabled
== 0) {
638 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
641 * If we hit an EPERM, something is wrong with our disable call. If
642 * we get an EEXIST, there is a problem on the tracer side since we
647 /* Code flow problem */
650 /* It's OK for our use case. */
665 * Copy data between an UST app event and a LTT event.
667 static void shadow_copy_event(struct ust_app_event
*ua_event
,
668 struct ltt_ust_event
*uevent
)
670 struct lttng_ht_iter iter
;
671 struct ltt_ust_context
*uctx
;
672 struct ust_app_ctx
*ua_ctx
;
674 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
675 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
677 ua_event
->enabled
= uevent
->enabled
;
679 /* Copy event attributes */
680 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
682 cds_lfht_for_each_entry(uevent
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
683 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
684 if (ua_ctx
== NULL
) {
685 /* malloc() failed. We should simply stop */
689 lttng_ht_node_init_ulong(&ua_ctx
->node
,
690 (unsigned long) ua_ctx
->ctx
.ctx
);
691 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
696 * Copy data between an UST app channel and a LTT channel.
698 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
699 struct ltt_ust_channel
*uchan
)
701 struct lttng_ht_iter iter
;
702 struct lttng_ht_node_str
*ua_event_node
;
703 struct ltt_ust_event
*uevent
;
704 struct ltt_ust_context
*uctx
;
705 struct ust_app_event
*ua_event
;
706 struct ust_app_ctx
*ua_ctx
;
708 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
710 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
711 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
712 /* Copy event attributes */
713 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
715 ua_chan
->enabled
= uchan
->enabled
;
717 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
718 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
719 if (ua_ctx
== NULL
) {
722 lttng_ht_node_init_ulong(&ua_ctx
->node
,
723 (unsigned long) ua_ctx
->ctx
.ctx
);
724 lttng_ht_add_unique_ulong(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
->ht
, &iter
.iter
, uevent
, node
.node
) {
729 struct lttng_ht_iter uiter
;
731 lttng_ht_lookup(ua_chan
->events
, (void *) uevent
->attr
.name
, &uiter
);
732 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
733 if (ua_event_node
== NULL
) {
734 DBG2("UST event %s not found on shadow copy channel",
736 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
737 if (ua_event
== NULL
) {
740 shadow_copy_event(ua_event
, uevent
);
741 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
745 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
749 * Copy data between a UST app session and a regular LTT session.
751 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
752 struct ltt_ust_session
*usess
, struct ust_app
*app
)
754 struct lttng_ht_node_str
*ua_chan_node
;
755 struct lttng_ht_iter iter
;
756 struct ltt_ust_channel
*uchan
;
757 struct ust_app_channel
*ua_chan
;
763 /* Get date and time for unique app path */
765 timeinfo
= localtime(&rawtime
);
766 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
768 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
770 ua_sess
->id
= usess
->id
;
771 ua_sess
->uid
= usess
->uid
;
772 ua_sess
->gid
= usess
->gid
;
774 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s/%s-%d-%s", usess
->pathname
,
775 app
->name
, app
->key
.pid
, datetime
);
777 PERROR("asprintf UST shadow copy session");
778 /* TODO: We cannot return an error from here.. */
782 /* TODO: support all UST domain */
784 /* Iterate over all channels in global domain. */
785 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
787 struct lttng_ht_iter uiter
;
789 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
790 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
791 if (ua_chan_node
!= NULL
) {
792 /* Session exist. Contiuing. */
796 DBG2("Channel %s not found on shadow session copy, creating it",
798 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
799 if (ua_chan
== NULL
) {
800 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
804 shadow_copy_channel(ua_chan
, uchan
);
805 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
810 * Lookup sesison wrapper.
813 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
814 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
816 /* Get right UST app session from app */
817 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
821 * Return ust app session from the app session hashtable using the UST session
824 static struct ust_app_session
*lookup_session_by_app(
825 struct ltt_ust_session
*usess
, struct ust_app
*app
)
827 struct lttng_ht_iter iter
;
828 struct lttng_ht_node_ulong
*node
;
830 __lookup_session_by_app(usess
, app
, &iter
);
831 node
= lttng_ht_iter_get_node_ulong(&iter
);
836 return caa_container_of(node
, struct ust_app_session
, node
);
843 * Create a UST session onto the tracer of app and add it the session
846 * Return ust app session or NULL on error.
848 static struct ust_app_session
*create_ust_app_session(
849 struct ltt_ust_session
*usess
, struct ust_app
*app
)
852 struct ust_app_session
*ua_sess
;
854 ua_sess
= lookup_session_by_app(usess
, app
);
855 if (ua_sess
== NULL
) {
856 DBG2("UST app pid: %d session id %d not found, creating it",
857 app
->key
.pid
, usess
->id
);
858 ua_sess
= alloc_ust_app_session();
859 if (ua_sess
== NULL
) {
860 /* Only malloc can failed so something is really wrong */
863 shadow_copy_session(ua_sess
, usess
, app
);
866 if (ua_sess
->handle
== -1) {
867 ret
= ustctl_create_session(app
->key
.sock
);
869 ERR("Creating session for app pid %d", app
->key
.pid
);
873 ua_sess
->handle
= ret
;
875 /* Add ust app session to app's HT */
876 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
877 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
879 DBG2("UST app session created successfully with handle %d", ret
);
886 delete_ust_app_session(-1, ua_sess
);
891 * Create a context for the channel on the tracer.
894 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
895 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
899 struct lttng_ht_iter iter
;
900 struct lttng_ht_node_ulong
*node
;
901 struct ust_app_ctx
*ua_ctx
;
903 DBG2("UST app adding context to channel %s", ua_chan
->name
);
905 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
906 node
= lttng_ht_iter_get_node_ulong(&iter
);
912 ua_ctx
= alloc_ust_app_ctx(uctx
);
913 if (ua_ctx
== NULL
) {
919 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
920 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
922 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
932 * Create an UST context and enable it for the event on the tracer.
935 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
936 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
940 struct lttng_ht_iter iter
;
941 struct lttng_ht_node_ulong
*node
;
942 struct ust_app_ctx
*ua_ctx
;
944 DBG2("UST app adding context to event %s", ua_event
->name
);
946 lttng_ht_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
947 node
= lttng_ht_iter_get_node_ulong(&iter
);
953 ua_ctx
= alloc_ust_app_ctx(uctx
);
954 if (ua_ctx
== NULL
) {
960 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
961 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
963 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
973 * Enable on the tracer side a ust app event for the session and channel.
976 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
977 struct ust_app_event
*ua_event
, struct ust_app
*app
)
981 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
986 ua_event
->enabled
= 1;
993 * Disable on the tracer side a ust app event for the session and channel.
995 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
996 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1000 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1005 ua_event
->enabled
= 0;
1012 * Lookup ust app channel for session and disable it on the tracer side.
1015 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1016 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1020 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1025 ua_chan
->enabled
= 0;
1032 * Lookup ust app channel for session and enable it on the tracer side.
1034 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1035 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1038 struct lttng_ht_iter iter
;
1039 struct lttng_ht_node_str
*ua_chan_node
;
1040 struct ust_app_channel
*ua_chan
;
1042 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1043 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1044 if (ua_chan_node
== NULL
) {
1045 DBG2("Unable to find channel %s in ust session id %u",
1046 uchan
->name
, ua_sess
->id
);
1050 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1052 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1062 * Create UST app channel and create it on the tracer.
1064 static struct ust_app_channel
*create_ust_app_channel(
1065 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1066 struct ust_app
*app
)
1069 struct lttng_ht_iter iter
;
1070 struct lttng_ht_node_str
*ua_chan_node
;
1071 struct ust_app_channel
*ua_chan
;
1073 /* Lookup channel in the ust app session */
1074 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1075 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1076 if (ua_chan_node
!= NULL
) {
1077 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1081 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1082 if (ua_chan
== NULL
) {
1083 /* Only malloc can fail here */
1086 shadow_copy_channel(ua_chan
, uchan
);
1088 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1090 /* Not found previously means that it does not exist on the tracer */
1091 assert(ret
!= -EEXIST
);
1095 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1097 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1104 delete_ust_app_channel(-1, ua_chan
);
1109 * Create UST app event and create it on the tracer side.
1112 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1113 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1114 struct ust_app
*app
)
1117 struct lttng_ht_iter iter
;
1118 struct lttng_ht_node_str
*ua_event_node
;
1119 struct ust_app_event
*ua_event
;
1121 /* Get event node */
1122 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
1123 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
1124 if (ua_event_node
!= NULL
) {
1129 /* Does not exist so create one */
1130 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1131 if (ua_event
== NULL
) {
1132 /* Only malloc can failed so something is really wrong */
1136 shadow_copy_event(ua_event
, uevent
);
1138 /* Create it on the tracer side */
1139 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1141 /* Not found previously means that it does not exist on the tracer */
1142 assert(ret
!= -EEXIST
);
1146 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
1148 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1155 /* Valid. Calling here is already in a read side lock */
1156 delete_ust_app_event(-1, ua_event
);
1161 * Create UST metadata and open it on the tracer side.
1163 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1164 char *pathname
, struct ust_app
*app
)
1168 if (ua_sess
->metadata
== NULL
) {
1169 /* Allocate UST metadata */
1170 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1171 if (ua_sess
->metadata
== NULL
) {
1172 /* malloc() failed */
1176 ret
= open_ust_metadata(app
, ua_sess
);
1178 DBG3("Opening metadata failed. Cleaning up memory");
1180 /* Cleanup failed metadata struct */
1181 free(ua_sess
->metadata
);
1183 * This is very important because delete_ust_app_session check if
1184 * the pointer is null or not in order to delete the metadata.
1186 ua_sess
->metadata
= NULL
;
1190 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
1193 /* Open UST metadata stream */
1194 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1195 ret
= create_ust_stream(app
, ua_sess
);
1200 ret
= run_as_mkdir(ua_sess
->path
, S_IRWXU
| S_IRWXG
,
1201 ua_sess
->uid
, ua_sess
->gid
);
1203 PERROR("mkdir UST metadata");
1207 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1208 "%s/metadata", ua_sess
->path
);
1210 PERROR("asprintf UST create stream");
1214 DBG2("UST metadata stream object created for app pid %d",
1217 ERR("Attempting to create stream without metadata opened");
1228 * Return pointer to traceable apps list.
1230 struct lttng_ht
*ust_app_get_ht(void)
1236 * Return ust app pointer or NULL if not found.
1238 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1240 struct lttng_ht_node_ulong
*node
;
1241 struct lttng_ht_iter iter
;
1244 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1245 node
= lttng_ht_iter_get_node_ulong(&iter
);
1247 DBG2("UST app no found with pid %d", pid
);
1252 DBG2("Found UST app by pid %d", pid
);
1254 return caa_container_of(node
, struct ust_app
, node
);
1262 * Using pid and uid (of the app), allocate a new ust_app struct and
1263 * add it to the global traceable app list.
1265 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1266 * bitness is not supported.
1268 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1270 struct ust_app
*lta
;
1272 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1273 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1274 ERR("Registration failed: application \"%s\" (pid: %d) has "
1275 "%d-bit long, but no consumerd for this long size is available.\n",
1276 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1280 lta
= zmalloc(sizeof(struct ust_app
));
1286 lta
->ppid
= msg
->ppid
;
1287 lta
->uid
= msg
->uid
;
1288 lta
->gid
= msg
->gid
;
1289 lta
->bits_per_long
= msg
->bits_per_long
;
1290 lta
->v_major
= msg
->major
;
1291 lta
->v_minor
= msg
->minor
;
1292 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1293 lta
->name
[16] = '\0';
1294 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1297 lta
->key
.pid
= msg
->pid
;
1298 lttng_ht_node_init_ulong(<a
->node
, (unsigned long)lta
->key
.pid
);
1299 lta
->key
.sock
= sock
;
1300 lttng_ht_node_init_ulong(<a
->key
.node
, (unsigned long)lta
->key
.sock
);
1303 lttng_ht_add_unique_ulong(ust_app_sock_key_map
, <a
->key
.node
);
1304 lttng_ht_add_unique_ulong(ust_app_ht
, <a
->node
);
1307 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1308 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1309 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1315 * Unregister app by removing it from the global traceable app list and freeing
1318 * The socket is already closed at this point so no close to sock.
1320 void ust_app_unregister(int sock
)
1322 struct ust_app
*lta
;
1323 struct lttng_ht_node_ulong
*node
;
1324 struct lttng_ht_iter iter
;
1328 lta
= find_app_by_sock(sock
);
1330 ERR("Unregister app sock %d not found!", sock
);
1334 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1336 /* Remove application from socket hash table */
1337 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
), &iter
);
1338 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1341 /* Get the node reference for a call_rcu */
1342 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) lta
->key
.pid
), &iter
);
1343 node
= lttng_ht_iter_get_node_ulong(&iter
);
1345 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1349 /* Remove application from PID hash table */
1350 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1352 call_rcu(&node
->head
, delete_ust_app_rcu
);
1359 * Return traceable_app_count
1361 unsigned long ust_app_list_count(void)
1363 unsigned long count
;
1366 count
= lttng_ht_get_count(ust_app_ht
);
1373 * Fill events array with all events name of all registered apps.
1375 int ust_app_list_events(struct lttng_event
**events
)
1378 size_t nbmem
, count
= 0;
1379 struct lttng_ht_iter iter
;
1380 struct ust_app
*app
;
1381 struct lttng_event
*tmp
;
1383 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1384 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1386 PERROR("zmalloc ust app events");
1393 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1394 struct lttng_ust_tracepoint_iter uiter
;
1396 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1398 ERR("UST app list events getting handle failed for app pid %d",
1403 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1404 &uiter
)) != -ENOENT
) {
1405 if (count
>= nbmem
) {
1406 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1409 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1411 PERROR("realloc ust app events");
1416 memcpy(tmp
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1417 memcpy(tmp
[count
].loglevel
, uiter
.loglevel
, LTTNG_UST_SYM_NAME_LEN
);
1418 tmp
[count
].loglevel_value
= uiter
.loglevel_value
;
1419 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1420 tmp
[count
].pid
= app
->key
.pid
;
1421 tmp
[count
].enabled
= -1;
1429 DBG2("UST app list events done (%zu events)", count
);
1438 * Free and clean all traceable apps of the global list.
1440 void ust_app_clean_list(void)
1443 struct lttng_ht_iter iter
;
1444 struct lttng_ht_node_ulong
*node
;
1446 DBG2("UST app cleaning registered apps hash table");
1450 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, node
, node
) {
1451 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1453 call_rcu(&node
->head
, delete_ust_app_rcu
);
1455 /* Destroy is done only when the ht is empty */
1456 lttng_ht_destroy(ust_app_ht
);
1458 cds_lfht_for_each_entry(ust_app_sock_key_map
->ht
, &iter
.iter
, node
, node
) {
1459 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1462 /* Destroy is done only when the ht is empty */
1463 lttng_ht_destroy(ust_app_sock_key_map
);
1469 * Init UST app hash table.
1471 void ust_app_ht_alloc(void)
1473 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1474 ust_app_sock_key_map
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1478 * For a specific UST session, disable the channel for all registered apps.
1480 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1481 struct ltt_ust_channel
*uchan
)
1484 struct lttng_ht_iter iter
;
1485 struct lttng_ht_node_str
*ua_chan_node
;
1486 struct ust_app
*app
;
1487 struct ust_app_session
*ua_sess
;
1488 struct ust_app_channel
*ua_chan
;
1490 if (usess
== NULL
|| uchan
== NULL
) {
1491 ERR("Disabling UST global channel with NULL values");
1496 DBG2("UST app disabling channel %s from global domain for session id %d",
1497 uchan
->name
, usess
->id
);
1501 /* For every registered applications */
1502 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1503 struct lttng_ht_iter uiter
;
1504 ua_sess
= lookup_session_by_app(usess
, app
);
1505 if (ua_sess
== NULL
) {
1510 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1511 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1512 /* If the session if found for the app, the channel must be there */
1513 assert(ua_chan_node
);
1515 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1516 /* The channel must not be already disabled */
1517 assert(ua_chan
->enabled
== 1);
1519 /* Disable channel onto application */
1520 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1522 /* XXX: We might want to report this error at some point... */
1534 * For a specific UST session, enable the channel for all registered apps.
1536 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1537 struct ltt_ust_channel
*uchan
)
1540 struct lttng_ht_iter iter
;
1541 struct ust_app
*app
;
1542 struct ust_app_session
*ua_sess
;
1544 if (usess
== NULL
|| uchan
== NULL
) {
1545 ERR("Adding UST global channel to NULL values");
1550 DBG2("UST app enabling channel %s to global domain for session id %d",
1551 uchan
->name
, usess
->id
);
1555 /* For every registered applications */
1556 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1557 ua_sess
= lookup_session_by_app(usess
, app
);
1558 if (ua_sess
== NULL
) {
1562 /* Enable channel onto application */
1563 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1565 /* XXX: We might want to report this error at some point... */
1577 * Disable an event in a channel and for a specific session.
1579 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1580 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1583 struct lttng_ht_iter iter
, uiter
;
1584 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1585 struct ust_app
*app
;
1586 struct ust_app_session
*ua_sess
;
1587 struct ust_app_channel
*ua_chan
;
1588 struct ust_app_event
*ua_event
;
1590 DBG("UST app disabling event %s for all apps in channel "
1591 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
1595 /* For all registered applications */
1596 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1597 ua_sess
= lookup_session_by_app(usess
, app
);
1598 if (ua_sess
== NULL
) {
1603 /* Lookup channel in the ust app session */
1604 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1605 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1606 if (ua_chan_node
== NULL
) {
1607 DBG2("Channel %s not found in session id %d for app pid %d."
1608 "Skipping", uchan
->name
, usess
->id
, app
->key
.pid
);
1611 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1613 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
1614 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1615 if (ua_event_node
== NULL
) {
1616 DBG2("Event %s not found in channel %s for app pid %d."
1617 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1620 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1622 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1624 /* XXX: Report error someday... */
1635 * For a specific UST session and UST channel, the event for all
1638 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1639 struct ltt_ust_channel
*uchan
)
1642 struct lttng_ht_iter iter
, uiter
;
1643 struct lttng_ht_node_str
*ua_chan_node
;
1644 struct ust_app
*app
;
1645 struct ust_app_session
*ua_sess
;
1646 struct ust_app_channel
*ua_chan
;
1647 struct ust_app_event
*ua_event
;
1649 DBG("UST app disabling all event for all apps in channel "
1650 "%s for session id %d", uchan
->name
, usess
->id
);
1654 /* For all registered applications */
1655 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1656 ua_sess
= lookup_session_by_app(usess
, app
);
1657 /* If ua_sess is NULL, there is a code flow error */
1660 /* Lookup channel in the ust app session */
1661 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1662 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1663 /* If the channel is not found, there is a code flow error */
1664 assert(ua_chan_node
);
1666 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1668 /* Disable each events of channel */
1669 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
1671 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1673 /* XXX: Report error someday... */
1685 * For a specific UST session, create the channel for all registered apps.
1687 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1688 struct ltt_ust_channel
*uchan
)
1690 struct lttng_ht_iter iter
;
1691 struct ust_app
*app
;
1692 struct ust_app_session
*ua_sess
;
1693 struct ust_app_channel
*ua_chan
;
1695 /* Very wrong code flow */
1699 DBG2("UST app adding channel %s to global domain for session id %d",
1700 uchan
->name
, usess
->id
);
1704 /* For every registered applications */
1705 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1707 * Create session on the tracer side and add it to app session HT. Note
1708 * that if session exist, it will simply return a pointer to the ust
1711 ua_sess
= create_ust_app_session(usess
, app
);
1712 if (ua_sess
== NULL
) {
1713 /* Major problem here and it's maybe the tracer or malloc() */
1717 /* Create channel onto application */
1718 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1719 if (ua_chan
== NULL
) {
1720 /* Major problem here and it's maybe the tracer or malloc() */
1734 * Enable event for a specific session and channel on the tracer.
1736 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1737 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1740 struct lttng_ht_iter iter
, uiter
;
1741 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1742 struct ust_app
*app
;
1743 struct ust_app_session
*ua_sess
;
1744 struct ust_app_channel
*ua_chan
;
1745 struct ust_app_event
*ua_event
;
1747 DBG("UST app enabling event %s for all apps for session id %d",
1748 uevent
->attr
.name
, usess
->id
);
1751 * NOTE: At this point, this function is called only if the session and
1752 * channel passed are already created for all apps. and enabled on the
1758 /* For all registered applications */
1759 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1760 ua_sess
= lookup_session_by_app(usess
, app
);
1761 /* If ua_sess is NULL, there is a code flow error */
1764 /* Lookup channel in the ust app session */
1765 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1766 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1767 /* If the channel is not found, there is a code flow error */
1768 assert(ua_chan_node
);
1770 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1772 lttng_ht_lookup(ua_chan
->events
, (void*)uevent
->attr
.name
, &uiter
);
1773 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1774 if (ua_event_node
== NULL
) {
1775 DBG3("UST app enable event %s not found for app PID %d."
1776 "Skipping app", uevent
->attr
.name
, app
->key
.pid
);
1779 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1781 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1793 * For a specific existing UST session and UST channel, creates the event for
1794 * all registered apps.
1796 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1797 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1800 struct lttng_ht_iter iter
, uiter
;
1801 struct lttng_ht_node_str
*ua_chan_node
;
1802 struct ust_app
*app
;
1803 struct ust_app_session
*ua_sess
;
1804 struct ust_app_channel
*ua_chan
;
1806 DBG("UST app creating event %s for all apps for session id %d",
1807 uevent
->attr
.name
, usess
->id
);
1811 /* For all registered applications */
1812 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1813 ua_sess
= lookup_session_by_app(usess
, app
);
1814 /* If ua_sess is NULL, there is a code flow error */
1817 /* Lookup channel in the ust app session */
1818 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1819 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1820 /* If the channel is not found, there is a code flow error */
1821 assert(ua_chan_node
);
1823 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1825 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1827 if (ret
!= -EEXIST
) {
1828 /* Possible value at this point: -ENOMEM. If so, we stop! */
1831 DBG2("UST app event %s already exist on app PID %d",
1832 uevent
->attr
.name
, app
->key
.pid
);
1843 * Start tracing for a specific UST session and app.
1845 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1848 struct lttng_ht_iter iter
;
1849 struct ust_app_session
*ua_sess
;
1850 struct ust_app_channel
*ua_chan
;
1851 struct ltt_ust_stream
*ustream
;
1854 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1858 ua_sess
= lookup_session_by_app(usess
, app
);
1859 if (ua_sess
== NULL
) {
1860 goto error_rcu_unlock
;
1863 /* Upon restart, we skip the setup, already done */
1864 if (ua_sess
->started
) {
1868 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1870 goto error_rcu_unlock
;
1873 /* For each channel */
1874 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1876 /* Create all streams */
1878 /* Create UST stream */
1879 ustream
= zmalloc(sizeof(*ustream
));
1880 if (ustream
== NULL
) {
1881 PERROR("zmalloc ust stream");
1882 goto error_rcu_unlock
;
1885 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1888 /* Got all streams */
1891 ustream
->handle
= ustream
->obj
->handle
;
1893 /* Order is important */
1894 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1895 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1896 ua_sess
->path
, ua_chan
->name
,
1897 ua_chan
->streams
.count
++);
1899 PERROR("asprintf UST create stream");
1902 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1907 switch (app
->bits_per_long
) {
1909 consumerd_fd
= ust_consumerd64_fd
;
1912 consumerd_fd
= ust_consumerd32_fd
;
1916 goto error_rcu_unlock
;
1919 /* Setup UST consumer socket and send fds to it */
1920 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1922 goto error_rcu_unlock
;
1924 ua_sess
->started
= 1;
1927 /* This start the UST tracing */
1928 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1930 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1931 goto error_rcu_unlock
;
1936 /* Quiescent wait after starting trace */
1937 ustctl_wait_quiescent(app
->key
.sock
);
1947 * Stop tracing for a specific UST session and app.
1949 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1952 struct lttng_ht_iter iter
;
1953 struct ust_app_session
*ua_sess
;
1954 struct ust_app_channel
*ua_chan
;
1956 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1960 ua_sess
= lookup_session_by_app(usess
, app
);
1961 if (ua_sess
== NULL
) {
1962 /* Only malloc can failed so something is really wrong */
1963 goto error_rcu_unlock
;
1966 /* Not started, continuing. */
1967 if (ua_sess
->started
== 0) {
1971 /* This inhibits UST tracing */
1972 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1974 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1975 goto error_rcu_unlock
;
1978 /* Quiescent wait after stopping trace */
1979 ustctl_wait_quiescent(app
->key
.sock
);
1981 /* Flushing buffers */
1982 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1984 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_chan
->obj
);
1986 ERR("UST app PID %d channel %s flush failed with ret %d",
1987 app
->key
.pid
, ua_chan
->name
, ret
);
1988 /* Continuing flushing all buffers */
1993 /* Flush all buffers before stopping */
1994 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_sess
->metadata
->obj
);
1996 ERR("UST app PID %d metadata flush failed with ret %d", app
->key
.pid
,
2000 ua_sess
->started
= 0;
2012 * Destroy a specific UST session in apps.
2014 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2016 struct ust_app_session
*ua_sess
;
2017 struct lttng_ust_object_data obj
;
2018 struct lttng_ht_iter iter
;
2019 struct lttng_ht_node_ulong
*node
;
2022 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
2026 __lookup_session_by_app(usess
, app
, &iter
);
2027 node
= lttng_ht_iter_get_node_ulong(&iter
);
2029 /* Only malloc can failed so something is really wrong */
2030 goto error_rcu_unlock
;
2032 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2033 ret
= lttng_ht_del(app
->sessions
, &iter
);
2035 obj
.handle
= ua_sess
->handle
;
2038 obj
.memory_map_size
= 0;
2039 ustctl_release_object(app
->key
.sock
, &obj
);
2041 delete_ust_app_session(app
->key
.sock
, ua_sess
);
2045 /* Quiescent wait after stopping trace */
2046 ustctl_wait_quiescent(app
->key
.sock
);
2056 * Start tracing for the UST session.
2058 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2061 struct lttng_ht_iter iter
;
2062 struct ust_app
*app
;
2064 DBG("Starting all UST traces");
2068 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2069 ret
= ust_app_start_trace(usess
, app
);
2071 /* Continue to next apps even on error */
2082 * Start tracing for the UST session.
2084 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2087 struct lttng_ht_iter iter
;
2088 struct ust_app
*app
;
2090 DBG("Stopping all UST traces");
2094 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2095 ret
= ust_app_stop_trace(usess
, app
);
2097 /* Continue to next apps even on error */
2108 * Destroy app UST session.
2110 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2113 struct lttng_ht_iter iter
;
2114 struct ust_app
*app
;
2116 DBG("Destroy all UST traces");
2120 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2121 ret
= ust_app_destroy_trace(usess
, app
);
2123 /* Continue to next apps even on error */
2134 * Add channels/events from UST global domain to registered apps at sock.
2136 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2139 struct lttng_ht_iter iter
, uiter
;
2140 struct ust_app
*app
;
2141 struct ust_app_session
*ua_sess
;
2142 struct ust_app_channel
*ua_chan
;
2143 struct ust_app_event
*ua_event
;
2145 if (usess
== NULL
) {
2146 ERR("No UST session on global update. Returning");
2150 DBG2("UST app global update for app sock %d for session id %d", sock
,
2155 app
= find_app_by_sock(sock
);
2157 ERR("Failed to update app sock %d", sock
);
2161 ua_sess
= create_ust_app_session(usess
, app
);
2162 if (ua_sess
== NULL
) {
2167 * We can iterate safely here over all UST app session sicne the create ust
2168 * app session above made a shadow copy of the UST global domain from the
2171 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2173 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2175 /* FIXME: Should we quit here or continue... */
2179 /* For each events */
2180 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2182 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2184 /* FIXME: Should we quit here or continue... */
2190 if (usess
->start_trace
) {
2191 ret
= ust_app_start_trace(usess
, app
);
2196 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2205 * Add context to a specific channel for global UST domain.
2207 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2208 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2211 struct lttng_ht_node_str
*ua_chan_node
;
2212 struct lttng_ht_iter iter
, uiter
;
2213 struct ust_app_channel
*ua_chan
= NULL
;
2214 struct ust_app_session
*ua_sess
;
2215 struct ust_app
*app
;
2219 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2220 ua_sess
= lookup_session_by_app(usess
, app
);
2221 if (ua_sess
== NULL
) {
2225 /* Lookup channel in the ust app session */
2226 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2227 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2228 if (ua_chan_node
== NULL
) {
2231 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2234 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2245 * Add context to a specific event in a channel for global UST domain.
2247 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2248 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2249 struct ltt_ust_context
*uctx
)
2252 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2253 struct lttng_ht_iter iter
, uiter
;
2254 struct ust_app_session
*ua_sess
;
2255 struct ust_app_event
*ua_event
;
2256 struct ust_app_channel
*ua_chan
= NULL
;
2257 struct ust_app
*app
;
2261 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2262 ua_sess
= lookup_session_by_app(usess
, app
);
2263 if (ua_sess
== NULL
) {
2267 /* Lookup channel in the ust app session */
2268 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2269 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2270 if (ua_chan_node
== NULL
) {
2273 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2276 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2277 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2278 if (ua_event_node
== NULL
) {
2281 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2284 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2295 * Enable event for a channel from a UST session for a specific PID.
2297 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2298 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2301 struct lttng_ht_iter iter
;
2302 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2303 struct ust_app
*app
;
2304 struct ust_app_session
*ua_sess
;
2305 struct ust_app_channel
*ua_chan
;
2306 struct ust_app_event
*ua_event
;
2308 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2312 app
= ust_app_find_by_pid(pid
);
2314 ERR("UST app enable event per PID %d not found", pid
);
2319 ua_sess
= lookup_session_by_app(usess
, app
);
2320 /* If ua_sess is NULL, there is a code flow error */
2323 /* Lookup channel in the ust app session */
2324 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2325 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2326 /* If the channel is not found, there is a code flow error */
2327 assert(ua_chan_node
);
2329 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2331 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2332 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2333 if (ua_event_node
== NULL
) {
2334 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2339 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2341 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2353 * Disable event for a channel from a UST session for a specific PID.
2355 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2356 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2359 struct lttng_ht_iter iter
;
2360 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2361 struct ust_app
*app
;
2362 struct ust_app_session
*ua_sess
;
2363 struct ust_app_channel
*ua_chan
;
2364 struct ust_app_event
*ua_event
;
2366 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2370 app
= ust_app_find_by_pid(pid
);
2372 ERR("UST app disable event per PID %d not found", pid
);
2377 ua_sess
= lookup_session_by_app(usess
, app
);
2378 /* If ua_sess is NULL, there is a code flow error */
2381 /* Lookup channel in the ust app session */
2382 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2383 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2384 if (ua_chan_node
== NULL
) {
2385 /* Channel does not exist, skip disabling */
2388 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2390 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2391 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2392 if (ua_event_node
== NULL
) {
2393 /* Event does not exist, skip disabling */
2396 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2398 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);