2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <sys/types.h>
29 #include <urcu/compiler.h>
33 #include <lttng-share.h>
35 #include "common/runas.h"
37 #include "ust-consumer.h"
41 * Delete ust context safely. RCU read lock must be held before calling
45 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
48 ustctl_release_object(sock
, ua_ctx
->obj
);
55 * Delete ust app event safely. RCU read lock must be held before calling
59 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
62 struct lttng_ht_iter iter
;
63 struct ust_app_ctx
*ua_ctx
;
65 /* Destroy each context of event */
66 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter
.iter
, ua_ctx
,
68 ret
= lttng_ht_del(ua_event
->ctx
, &iter
);
70 delete_ust_app_ctx(sock
, ua_ctx
);
72 lttng_ht_destroy(ua_event
->ctx
);
74 if (ua_event
->obj
!= NULL
) {
75 ustctl_release_object(sock
, ua_event
->obj
);
82 * Delete ust app stream safely. RCU read lock must be held before calling
86 void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
89 ustctl_release_object(sock
, stream
->obj
);
96 * Delete ust app channel safely. RCU read lock must be held before calling
100 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
103 struct lttng_ht_iter iter
;
104 struct ust_app_event
*ua_event
;
105 struct ust_app_ctx
*ua_ctx
;
106 struct ltt_ust_stream
*stream
, *stmp
;
109 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
110 cds_list_del(&stream
->list
);
111 delete_ust_app_stream(sock
, stream
);
115 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
116 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
118 delete_ust_app_ctx(sock
, ua_ctx
);
120 lttng_ht_destroy(ua_chan
->ctx
);
123 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
125 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
127 delete_ust_app_event(sock
, ua_event
);
129 lttng_ht_destroy(ua_chan
->events
);
131 if (ua_chan
->obj
!= NULL
) {
132 ustctl_release_object(sock
, ua_chan
->obj
);
139 * Delete ust app session safely. RCU read lock must be held before calling
143 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
)
146 struct lttng_ht_iter iter
;
147 struct ust_app_channel
*ua_chan
;
149 if (ua_sess
->metadata
) {
150 if (ua_sess
->metadata
->stream_obj
) {
151 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
152 free(ua_sess
->metadata
->stream_obj
);
154 if (ua_sess
->metadata
->obj
) {
155 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
156 free(ua_sess
->metadata
->obj
);
160 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
162 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
164 delete_ust_app_channel(sock
, ua_chan
);
166 lttng_ht_destroy(ua_sess
->channels
);
168 if (ua_sess
->handle
!= -1) {
169 ustctl_release_handle(sock
, ua_sess
->handle
);
175 * Delete a traceable application structure from the global list. Never call
176 * this function outside of a call_rcu call.
179 void delete_ust_app(struct ust_app
*app
)
182 struct lttng_ht_iter iter
;
183 struct ust_app_session
*ua_sess
;
187 /* Delete ust app sessions info */
188 sock
= app
->key
.sock
;
192 cds_lfht_for_each_entry(app
->sessions
->ht
, &iter
.iter
, ua_sess
,
194 ret
= lttng_ht_del(app
->sessions
, &iter
);
196 delete_ust_app_session(app
->key
.sock
, ua_sess
);
198 lttng_ht_destroy(app
->sessions
);
201 * Wait until we have removed the key from the sock hash table before
202 * closing this socket, otherwise an application could re-use the socket ID
203 * and race with the teardown, using the same hash table entry.
207 DBG2("UST app pid %d deleted", app
->key
.pid
);
214 * URCU intermediate call to delete an UST app.
217 void delete_ust_app_rcu(struct rcu_head
*head
)
219 struct lttng_ht_node_ulong
*node
=
220 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
221 struct ust_app
*app
=
222 caa_container_of(node
, struct ust_app
, node
);
224 DBG3("Call RCU deleting app PID %d", app
->key
.pid
);
229 * Alloc new UST app session.
232 struct ust_app_session
*alloc_ust_app_session(void)
234 struct ust_app_session
*ua_sess
;
236 /* Init most of the default value by allocating and zeroing */
237 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
238 if (ua_sess
== NULL
) {
243 ua_sess
->handle
= -1;
244 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
253 * Alloc new UST app channel.
256 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
257 struct lttng_ust_channel
*attr
)
259 struct ust_app_channel
*ua_chan
;
261 /* Init most of the default value by allocating and zeroing */
262 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
263 if (ua_chan
== NULL
) {
268 /* Setup channel name */
269 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
270 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
272 ua_chan
->enabled
= 1;
273 ua_chan
->handle
= -1;
274 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
275 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
276 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
278 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
280 /* Copy attributes */
282 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
285 DBG3("UST app channel %s allocated", ua_chan
->name
);
294 * Alloc new UST app event.
297 struct ust_app_event
*alloc_ust_app_event(char *name
,
298 struct lttng_ust_event
*attr
)
300 struct ust_app_event
*ua_event
;
302 /* Init most of the default value by allocating and zeroing */
303 ua_event
= zmalloc(sizeof(struct ust_app_event
));
304 if (ua_event
== NULL
) {
309 ua_event
->enabled
= 1;
310 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
311 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
312 ua_event
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
313 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
315 /* Copy attributes */
317 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
320 DBG3("UST app event %s allocated", ua_event
->name
);
329 * Alloc new UST app context.
332 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
334 struct ust_app_ctx
*ua_ctx
;
336 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
337 if (ua_ctx
== NULL
) {
342 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
345 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
352 * Find an ust_app using the sock and return it. RCU read side lock must be
353 * held before calling this helper function.
356 struct ust_app
*find_app_by_sock(int sock
)
358 struct lttng_ht_node_ulong
*node
;
359 struct ust_app_key
*key
;
360 struct lttng_ht_iter iter
;
362 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
),
364 node
= lttng_ht_iter_get_node_ulong(&iter
);
366 DBG2("UST app find by sock %d key not found", sock
);
369 key
= caa_container_of(node
, struct ust_app_key
, node
);
371 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) key
->pid
), &iter
);
372 node
= lttng_ht_iter_get_node_ulong(&iter
);
374 DBG2("UST app find by sock %d not found", sock
);
377 return caa_container_of(node
, struct ust_app
, node
);
384 * Create the channel context on the tracer.
387 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
388 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
392 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
393 ua_chan
->obj
, &ua_ctx
->obj
);
398 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
400 DBG2("UST app context added to channel %s successfully", ua_chan
->name
);
407 * Create the event context on the tracer.
410 int create_ust_event_context(struct ust_app_event
*ua_event
,
411 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
415 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
416 ua_event
->obj
, &ua_ctx
->obj
);
421 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
423 DBG2("UST app context added to event %s successfully", ua_event
->name
);
430 * Disable the specified event on to UST tracer for the UST session.
432 static int disable_ust_event(struct ust_app
*app
,
433 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
437 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
439 ERR("UST app event %s disable failed for app (pid: %d) "
440 "and session handle %d with ret %d",
441 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
445 DBG2("UST app event %s disabled successfully for app (pid: %d)",
446 ua_event
->attr
.name
, app
->key
.pid
);
453 * Disable the specified channel on to UST tracer for the UST session.
455 static int disable_ust_channel(struct ust_app
*app
,
456 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
460 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
462 ERR("UST app channel %s disable failed for app (pid: %d) "
463 "and session handle %d with ret %d",
464 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
468 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
469 ua_chan
->name
, app
->key
.pid
);
476 * Enable the specified channel on to UST tracer for the UST session.
478 static int enable_ust_channel(struct ust_app
*app
,
479 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
483 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
485 ERR("UST app channel %s enable failed for app (pid: %d) "
486 "and session handle %d with ret %d",
487 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
491 ua_chan
->enabled
= 1;
493 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
494 ua_chan
->name
, app
->key
.pid
);
501 * Enable the specified event on to UST tracer for the UST session.
503 static int enable_ust_event(struct ust_app
*app
,
504 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
508 ret
= ustctl_enable(app
->key
.sock
, ua_event
->obj
);
510 ERR("UST app event %s enable failed for app (pid: %d) "
511 "and session handle %d with ret %d",
512 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
516 DBG2("UST app event %s enabled successfully for app (pid: %d)",
517 ua_event
->attr
.name
, app
->key
.pid
);
524 * Open metadata onto the UST tracer for a UST session.
526 static int open_ust_metadata(struct ust_app
*app
,
527 struct ust_app_session
*ua_sess
)
530 struct lttng_ust_channel_attr uattr
;
532 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
533 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
534 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
535 uattr
.switch_timer_interval
=
536 ua_sess
->metadata
->attr
.switch_timer_interval
;
537 uattr
.read_timer_interval
=
538 ua_sess
->metadata
->attr
.read_timer_interval
;
539 uattr
.output
= ua_sess
->metadata
->attr
.output
;
541 /* UST tracer metadata creation */
542 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
543 &ua_sess
->metadata
->obj
);
545 ERR("UST app open metadata failed for app pid:%d with ret %d",
550 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
557 * Create stream onto the UST tracer for a UST session.
559 static int create_ust_stream(struct ust_app
*app
,
560 struct ust_app_session
*ua_sess
)
564 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
565 &ua_sess
->metadata
->stream_obj
);
567 ERR("UST create metadata stream failed");
576 * Create the specified channel onto the UST tracer for a UST session.
578 static int create_ust_channel(struct ust_app
*app
,
579 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
583 /* TODO: remove cast and use lttng-ust-abi.h */
584 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
585 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
587 ERR("Creating channel %s for app (pid: %d, sock: %d) "
588 "and session handle %d with ret %d",
589 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
590 ua_sess
->handle
, ret
);
594 ua_chan
->handle
= ua_chan
->obj
->handle
;
596 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
597 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
599 /* If channel is not enabled, disable it on the tracer */
600 if (!ua_chan
->enabled
) {
601 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
612 * Create the specified event onto the UST tracer for a UST session.
615 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
616 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
620 /* Create UST event on tracer */
621 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
624 if (ret
== -EEXIST
) {
628 ERR("Error ustctl create event %s for app pid: %d with ret %d",
629 ua_event
->attr
.name
, app
->key
.pid
, ret
);
633 ua_event
->handle
= ua_event
->obj
->handle
;
635 DBG2("UST app event %s created successfully for pid:%d",
636 ua_event
->attr
.name
, app
->key
.pid
);
638 /* If event not enabled, disable it on the tracer */
639 if (ua_event
->enabled
== 0) {
640 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
643 * If we hit an EPERM, something is wrong with our disable call. If
644 * we get an EEXIST, there is a problem on the tracer side since we
649 /* Code flow problem */
652 /* It's OK for our use case. */
667 * Copy data between an UST app event and a LTT event.
669 static void shadow_copy_event(struct ust_app_event
*ua_event
,
670 struct ltt_ust_event
*uevent
)
672 struct lttng_ht_iter iter
;
673 struct ltt_ust_context
*uctx
;
674 struct ust_app_ctx
*ua_ctx
;
676 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
677 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
679 ua_event
->enabled
= uevent
->enabled
;
681 /* Copy event attributes */
682 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
684 cds_lfht_for_each_entry(uevent
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
685 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
686 if (ua_ctx
== NULL
) {
687 /* malloc() failed. We should simply stop */
691 lttng_ht_node_init_ulong(&ua_ctx
->node
,
692 (unsigned long) ua_ctx
->ctx
.ctx
);
693 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
698 * Copy data between an UST app channel and a LTT channel.
700 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
701 struct ltt_ust_channel
*uchan
)
703 struct lttng_ht_iter iter
;
704 struct lttng_ht_node_str
*ua_event_node
;
705 struct ltt_ust_event
*uevent
;
706 struct ltt_ust_context
*uctx
;
707 struct ust_app_event
*ua_event
;
708 struct ust_app_ctx
*ua_ctx
;
710 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
712 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
713 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
714 /* Copy event attributes */
715 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
717 ua_chan
->enabled
= uchan
->enabled
;
719 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
720 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
721 if (ua_ctx
== NULL
) {
724 lttng_ht_node_init_ulong(&ua_ctx
->node
,
725 (unsigned long) ua_ctx
->ctx
.ctx
);
726 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
729 /* Copy all events from ltt ust channel to ust app channel */
730 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
731 struct lttng_ht_iter uiter
;
733 lttng_ht_lookup(ua_chan
->events
, (void *) uevent
->attr
.name
, &uiter
);
734 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
735 if (ua_event_node
== NULL
) {
736 DBG2("UST event %s not found on shadow copy channel",
738 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
739 if (ua_event
== NULL
) {
742 shadow_copy_event(ua_event
, uevent
);
743 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
747 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
751 * Copy data between a UST app session and a regular LTT session.
753 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
754 struct ltt_ust_session
*usess
, struct ust_app
*app
)
756 struct lttng_ht_node_str
*ua_chan_node
;
757 struct lttng_ht_iter iter
;
758 struct ltt_ust_channel
*uchan
;
759 struct ust_app_channel
*ua_chan
;
765 /* Get date and time for unique app path */
767 timeinfo
= localtime(&rawtime
);
768 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
770 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
772 ua_sess
->id
= usess
->id
;
773 ua_sess
->uid
= usess
->uid
;
774 ua_sess
->gid
= usess
->gid
;
776 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s/%s-%d-%s", usess
->pathname
,
777 app
->name
, app
->key
.pid
, datetime
);
779 PERROR("asprintf UST shadow copy session");
780 /* TODO: We cannot return an error from here.. */
784 /* TODO: support all UST domain */
786 /* Iterate over all channels in global domain. */
787 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
789 struct lttng_ht_iter uiter
;
791 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
792 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
793 if (ua_chan_node
!= NULL
) {
794 /* Session exist. Contiuing. */
798 DBG2("Channel %s not found on shadow session copy, creating it",
800 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
801 if (ua_chan
== NULL
) {
802 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
806 shadow_copy_channel(ua_chan
, uchan
);
807 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
812 * Lookup sesison wrapper.
815 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
816 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
818 /* Get right UST app session from app */
819 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
823 * Return ust app session from the app session hashtable using the UST session
826 static struct ust_app_session
*lookup_session_by_app(
827 struct ltt_ust_session
*usess
, struct ust_app
*app
)
829 struct lttng_ht_iter iter
;
830 struct lttng_ht_node_ulong
*node
;
832 __lookup_session_by_app(usess
, app
, &iter
);
833 node
= lttng_ht_iter_get_node_ulong(&iter
);
838 return caa_container_of(node
, struct ust_app_session
, node
);
845 * Create a UST session onto the tracer of app and add it the session
848 * Return ust app session or NULL on error.
850 static struct ust_app_session
*create_ust_app_session(
851 struct ltt_ust_session
*usess
, struct ust_app
*app
)
854 struct ust_app_session
*ua_sess
;
856 ua_sess
= lookup_session_by_app(usess
, app
);
857 if (ua_sess
== NULL
) {
858 DBG2("UST app pid: %d session id %d not found, creating it",
859 app
->key
.pid
, usess
->id
);
860 ua_sess
= alloc_ust_app_session();
861 if (ua_sess
== NULL
) {
862 /* Only malloc can failed so something is really wrong */
865 shadow_copy_session(ua_sess
, usess
, app
);
868 if (ua_sess
->handle
== -1) {
869 ret
= ustctl_create_session(app
->key
.sock
);
871 ERR("Creating session for app pid %d", app
->key
.pid
);
875 ua_sess
->handle
= ret
;
877 /* Add ust app session to app's HT */
878 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
879 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
881 DBG2("UST app session created successfully with handle %d", ret
);
888 delete_ust_app_session(-1, ua_sess
);
893 * Create a context for the channel on the tracer.
896 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
897 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
901 struct lttng_ht_iter iter
;
902 struct lttng_ht_node_ulong
*node
;
903 struct ust_app_ctx
*ua_ctx
;
905 DBG2("UST app adding context to channel %s", ua_chan
->name
);
907 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
908 node
= lttng_ht_iter_get_node_ulong(&iter
);
914 ua_ctx
= alloc_ust_app_ctx(uctx
);
915 if (ua_ctx
== NULL
) {
921 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
922 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
924 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
934 * Create an UST context and enable it for the event on the tracer.
937 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
938 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
942 struct lttng_ht_iter iter
;
943 struct lttng_ht_node_ulong
*node
;
944 struct ust_app_ctx
*ua_ctx
;
946 DBG2("UST app adding context to event %s", ua_event
->name
);
948 lttng_ht_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
949 node
= lttng_ht_iter_get_node_ulong(&iter
);
955 ua_ctx
= alloc_ust_app_ctx(uctx
);
956 if (ua_ctx
== NULL
) {
962 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
963 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
965 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
975 * Enable on the tracer side a ust app event for the session and channel.
978 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
979 struct ust_app_event
*ua_event
, struct ust_app
*app
)
983 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
988 ua_event
->enabled
= 1;
995 * Disable on the tracer side a ust app event for the session and channel.
997 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
998 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1002 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1007 ua_event
->enabled
= 0;
1014 * Lookup ust app channel for session and disable it on the tracer side.
1017 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1018 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1022 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1027 ua_chan
->enabled
= 0;
1034 * Lookup ust app channel for session and enable it on the tracer side.
1036 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1037 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1040 struct lttng_ht_iter iter
;
1041 struct lttng_ht_node_str
*ua_chan_node
;
1042 struct ust_app_channel
*ua_chan
;
1044 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1045 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1046 if (ua_chan_node
== NULL
) {
1047 DBG2("Unable to find channel %s in ust session id %u",
1048 uchan
->name
, ua_sess
->id
);
1052 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1054 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1064 * Create UST app channel and create it on the tracer.
1066 static struct ust_app_channel
*create_ust_app_channel(
1067 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1068 struct ust_app
*app
)
1071 struct lttng_ht_iter iter
;
1072 struct lttng_ht_node_str
*ua_chan_node
;
1073 struct ust_app_channel
*ua_chan
;
1075 /* Lookup channel in the ust app session */
1076 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1077 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1078 if (ua_chan_node
!= NULL
) {
1079 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1083 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1084 if (ua_chan
== NULL
) {
1085 /* Only malloc can fail here */
1088 shadow_copy_channel(ua_chan
, uchan
);
1090 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1092 /* Not found previously means that it does not exist on the tracer */
1093 assert(ret
!= -EEXIST
);
1097 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1099 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1106 delete_ust_app_channel(-1, ua_chan
);
1111 * Create UST app event and create it on the tracer side.
1114 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1115 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1116 struct ust_app
*app
)
1119 struct lttng_ht_iter iter
;
1120 struct lttng_ht_node_str
*ua_event_node
;
1121 struct ust_app_event
*ua_event
;
1123 /* Get event node */
1124 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
1125 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
1126 if (ua_event_node
!= NULL
) {
1131 /* Does not exist so create one */
1132 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1133 if (ua_event
== NULL
) {
1134 /* Only malloc can failed so something is really wrong */
1138 shadow_copy_event(ua_event
, uevent
);
1140 /* Create it on the tracer side */
1141 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1143 /* Not found previously means that it does not exist on the tracer */
1144 assert(ret
!= -EEXIST
);
1148 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
1150 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1157 /* Valid. Calling here is already in a read side lock */
1158 delete_ust_app_event(-1, ua_event
);
1163 * Create UST metadata and open it on the tracer side.
1165 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1166 char *pathname
, struct ust_app
*app
)
1170 if (ua_sess
->metadata
== NULL
) {
1171 /* Allocate UST metadata */
1172 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1173 if (ua_sess
->metadata
== NULL
) {
1174 /* malloc() failed */
1178 ret
= open_ust_metadata(app
, ua_sess
);
1180 DBG3("Opening metadata failed. Cleaning up memory");
1182 /* Cleanup failed metadata struct */
1183 free(ua_sess
->metadata
);
1185 * This is very important because delete_ust_app_session check if
1186 * the pointer is null or not in order to delete the metadata.
1188 ua_sess
->metadata
= NULL
;
1192 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
1195 /* Open UST metadata stream */
1196 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1197 ret
= create_ust_stream(app
, ua_sess
);
1202 ret
= run_as_mkdir(ua_sess
->path
, S_IRWXU
| S_IRWXG
,
1203 ua_sess
->uid
, ua_sess
->gid
);
1205 PERROR("mkdir UST metadata");
1209 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1210 "%s/metadata", ua_sess
->path
);
1212 PERROR("asprintf UST create stream");
1216 DBG2("UST metadata stream object created for app pid %d",
1219 ERR("Attempting to create stream without metadata opened");
1230 * Return pointer to traceable apps list.
1232 struct lttng_ht
*ust_app_get_ht(void)
1238 * Return ust app pointer or NULL if not found.
1240 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1242 struct lttng_ht_node_ulong
*node
;
1243 struct lttng_ht_iter iter
;
1246 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1247 node
= lttng_ht_iter_get_node_ulong(&iter
);
1249 DBG2("UST app no found with pid %d", pid
);
1254 DBG2("Found UST app by pid %d", pid
);
1256 return caa_container_of(node
, struct ust_app
, node
);
1264 * Using pid and uid (of the app), allocate a new ust_app struct and
1265 * add it to the global traceable app list.
1267 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1268 * bitness is not supported.
1270 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1272 struct ust_app
*lta
;
1274 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1275 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1276 ERR("Registration failed: application \"%s\" (pid: %d) has "
1277 "%d-bit long, but no consumerd for this long size is available.\n",
1278 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1282 lta
= zmalloc(sizeof(struct ust_app
));
1288 lta
->ppid
= msg
->ppid
;
1289 lta
->uid
= msg
->uid
;
1290 lta
->gid
= msg
->gid
;
1291 lta
->bits_per_long
= msg
->bits_per_long
;
1292 lta
->v_major
= msg
->major
;
1293 lta
->v_minor
= msg
->minor
;
1294 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1295 lta
->name
[16] = '\0';
1296 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1299 lta
->key
.pid
= msg
->pid
;
1300 lttng_ht_node_init_ulong(<a
->node
, (unsigned long)lta
->key
.pid
);
1301 lta
->key
.sock
= sock
;
1302 lttng_ht_node_init_ulong(<a
->key
.node
, (unsigned long)lta
->key
.sock
);
1305 lttng_ht_add_unique_ulong(ust_app_sock_key_map
, <a
->key
.node
);
1306 lttng_ht_add_unique_ulong(ust_app_ht
, <a
->node
);
1309 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1310 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1311 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1317 * Unregister app by removing it from the global traceable app list and freeing
1320 * The socket is already closed at this point so no close to sock.
1322 void ust_app_unregister(int sock
)
1324 struct ust_app
*lta
;
1325 struct lttng_ht_node_ulong
*node
;
1326 struct lttng_ht_iter iter
;
1330 lta
= find_app_by_sock(sock
);
1332 ERR("Unregister app sock %d not found!", sock
);
1336 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1338 /* Remove application from socket hash table */
1339 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
), &iter
);
1340 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1343 /* Get the node reference for a call_rcu */
1344 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) lta
->key
.pid
), &iter
);
1345 node
= lttng_ht_iter_get_node_ulong(&iter
);
1347 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1351 /* Remove application from PID hash table */
1352 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1354 call_rcu(&node
->head
, delete_ust_app_rcu
);
1361 * Return traceable_app_count
1363 unsigned long ust_app_list_count(void)
1365 unsigned long count
;
1368 count
= lttng_ht_get_count(ust_app_ht
);
1375 * Fill events array with all events name of all registered apps.
1377 int ust_app_list_events(struct lttng_event
**events
)
1380 size_t nbmem
, count
= 0;
1381 struct lttng_ht_iter iter
;
1382 struct ust_app
*app
;
1383 struct lttng_event
*tmp
;
1385 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1386 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1388 PERROR("zmalloc ust app events");
1395 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1396 struct lttng_ust_tracepoint_iter uiter
;
1398 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1400 ERR("UST app list events getting handle failed for app pid %d",
1405 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1406 &uiter
)) != -ENOENT
) {
1407 if (count
>= nbmem
) {
1408 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1411 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1413 PERROR("realloc ust app events");
1418 memcpy(tmp
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1419 memcpy(tmp
[count
].loglevel
, uiter
.loglevel
, LTTNG_UST_SYM_NAME_LEN
);
1420 tmp
[count
].loglevel_value
= uiter
.loglevel_value
;
1421 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1422 tmp
[count
].pid
= app
->key
.pid
;
1423 tmp
[count
].enabled
= -1;
1431 DBG2("UST app list events done (%zu events)", count
);
1440 * Free and clean all traceable apps of the global list.
1442 void ust_app_clean_list(void)
1445 struct lttng_ht_iter iter
;
1446 struct lttng_ht_node_ulong
*node
;
1448 DBG2("UST app cleaning registered apps hash table");
1452 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, node
, node
) {
1453 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1455 call_rcu(&node
->head
, delete_ust_app_rcu
);
1457 /* Destroy is done only when the ht is empty */
1458 lttng_ht_destroy(ust_app_ht
);
1460 cds_lfht_for_each_entry(ust_app_sock_key_map
->ht
, &iter
.iter
, node
, node
) {
1461 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1464 /* Destroy is done only when the ht is empty */
1465 lttng_ht_destroy(ust_app_sock_key_map
);
1471 * Init UST app hash table.
1473 void ust_app_ht_alloc(void)
1475 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1476 ust_app_sock_key_map
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1480 * For a specific UST session, disable the channel for all registered apps.
1482 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1483 struct ltt_ust_channel
*uchan
)
1486 struct lttng_ht_iter iter
;
1487 struct lttng_ht_node_str
*ua_chan_node
;
1488 struct ust_app
*app
;
1489 struct ust_app_session
*ua_sess
;
1490 struct ust_app_channel
*ua_chan
;
1492 if (usess
== NULL
|| uchan
== NULL
) {
1493 ERR("Disabling UST global channel with NULL values");
1498 DBG2("UST app disabling channel %s from global domain for session id %d",
1499 uchan
->name
, usess
->id
);
1503 /* For every registered applications */
1504 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1505 struct lttng_ht_iter uiter
;
1506 ua_sess
= lookup_session_by_app(usess
, app
);
1507 if (ua_sess
== NULL
) {
1512 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1513 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1514 /* If the session if found for the app, the channel must be there */
1515 assert(ua_chan_node
);
1517 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1518 /* The channel must not be already disabled */
1519 assert(ua_chan
->enabled
== 1);
1521 /* Disable channel onto application */
1522 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1524 /* XXX: We might want to report this error at some point... */
1536 * For a specific UST session, enable the channel for all registered apps.
1538 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1539 struct ltt_ust_channel
*uchan
)
1542 struct lttng_ht_iter iter
;
1543 struct ust_app
*app
;
1544 struct ust_app_session
*ua_sess
;
1546 if (usess
== NULL
|| uchan
== NULL
) {
1547 ERR("Adding UST global channel to NULL values");
1552 DBG2("UST app enabling channel %s to global domain for session id %d",
1553 uchan
->name
, usess
->id
);
1557 /* For every registered applications */
1558 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1559 ua_sess
= lookup_session_by_app(usess
, app
);
1560 if (ua_sess
== NULL
) {
1564 /* Enable channel onto application */
1565 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1567 /* XXX: We might want to report this error at some point... */
1579 * Disable an event in a channel and for a specific session.
1581 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1582 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1585 struct lttng_ht_iter iter
, uiter
;
1586 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1587 struct ust_app
*app
;
1588 struct ust_app_session
*ua_sess
;
1589 struct ust_app_channel
*ua_chan
;
1590 struct ust_app_event
*ua_event
;
1592 DBG("UST app disabling event %s for all apps in channel "
1593 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
1597 /* For all registered applications */
1598 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1599 ua_sess
= lookup_session_by_app(usess
, app
);
1600 if (ua_sess
== NULL
) {
1605 /* Lookup channel in the ust app session */
1606 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1607 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1608 if (ua_chan_node
== NULL
) {
1609 DBG2("Channel %s not found in session id %d for app pid %d."
1610 "Skipping", uchan
->name
, usess
->id
, app
->key
.pid
);
1613 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1615 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
1616 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1617 if (ua_event_node
== NULL
) {
1618 DBG2("Event %s not found in channel %s for app pid %d."
1619 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1622 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1624 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1626 /* XXX: Report error someday... */
1637 * For a specific UST session and UST channel, the event for all
1640 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1641 struct ltt_ust_channel
*uchan
)
1644 struct lttng_ht_iter iter
, uiter
;
1645 struct lttng_ht_node_str
*ua_chan_node
;
1646 struct ust_app
*app
;
1647 struct ust_app_session
*ua_sess
;
1648 struct ust_app_channel
*ua_chan
;
1649 struct ust_app_event
*ua_event
;
1651 DBG("UST app disabling all event for all apps in channel "
1652 "%s for session id %d", uchan
->name
, usess
->id
);
1656 /* For all registered applications */
1657 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1658 ua_sess
= lookup_session_by_app(usess
, app
);
1659 /* If ua_sess is NULL, there is a code flow error */
1662 /* Lookup channel in the ust app session */
1663 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1664 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1665 /* If the channel is not found, there is a code flow error */
1666 assert(ua_chan_node
);
1668 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1670 /* Disable each events of channel */
1671 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
1673 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1675 /* XXX: Report error someday... */
1687 * For a specific UST session, create the channel for all registered apps.
1689 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1690 struct ltt_ust_channel
*uchan
)
1692 struct lttng_ht_iter iter
;
1693 struct ust_app
*app
;
1694 struct ust_app_session
*ua_sess
;
1695 struct ust_app_channel
*ua_chan
;
1697 /* Very wrong code flow */
1701 DBG2("UST app adding channel %s to global domain for session id %d",
1702 uchan
->name
, usess
->id
);
1706 /* For every registered applications */
1707 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1709 * Create session on the tracer side and add it to app session HT. Note
1710 * that if session exist, it will simply return a pointer to the ust
1713 ua_sess
= create_ust_app_session(usess
, app
);
1714 if (ua_sess
== NULL
) {
1715 /* Major problem here and it's maybe the tracer or malloc() */
1719 /* Create channel onto application */
1720 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1721 if (ua_chan
== NULL
) {
1722 /* Major problem here and it's maybe the tracer or malloc() */
1736 * Enable event for a specific session and channel on the tracer.
1738 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1739 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1742 struct lttng_ht_iter iter
, uiter
;
1743 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1744 struct ust_app
*app
;
1745 struct ust_app_session
*ua_sess
;
1746 struct ust_app_channel
*ua_chan
;
1747 struct ust_app_event
*ua_event
;
1749 DBG("UST app enabling event %s for all apps for session id %d",
1750 uevent
->attr
.name
, usess
->id
);
1753 * NOTE: At this point, this function is called only if the session and
1754 * channel passed are already created for all apps. and enabled on the
1760 /* For all registered applications */
1761 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1762 ua_sess
= lookup_session_by_app(usess
, app
);
1763 /* If ua_sess is NULL, there is a code flow error */
1766 /* Lookup channel in the ust app session */
1767 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1768 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1769 /* If the channel is not found, there is a code flow error */
1770 assert(ua_chan_node
);
1772 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1774 lttng_ht_lookup(ua_chan
->events
, (void*)uevent
->attr
.name
, &uiter
);
1775 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1776 if (ua_event_node
== NULL
) {
1777 DBG3("UST app enable event %s not found for app PID %d."
1778 "Skipping app", uevent
->attr
.name
, app
->key
.pid
);
1781 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1783 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1795 * For a specific existing UST session and UST channel, creates the event for
1796 * all registered apps.
1798 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1799 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1802 struct lttng_ht_iter iter
, uiter
;
1803 struct lttng_ht_node_str
*ua_chan_node
;
1804 struct ust_app
*app
;
1805 struct ust_app_session
*ua_sess
;
1806 struct ust_app_channel
*ua_chan
;
1808 DBG("UST app creating event %s for all apps for session id %d",
1809 uevent
->attr
.name
, usess
->id
);
1813 /* For all registered applications */
1814 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1815 ua_sess
= lookup_session_by_app(usess
, app
);
1816 /* If ua_sess is NULL, there is a code flow error */
1819 /* Lookup channel in the ust app session */
1820 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1821 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1822 /* If the channel is not found, there is a code flow error */
1823 assert(ua_chan_node
);
1825 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1827 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1829 if (ret
!= -EEXIST
) {
1830 /* Possible value at this point: -ENOMEM. If so, we stop! */
1833 DBG2("UST app event %s already exist on app PID %d",
1834 uevent
->attr
.name
, app
->key
.pid
);
1845 * Start tracing for a specific UST session and app.
1847 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1850 struct lttng_ht_iter iter
;
1851 struct ust_app_session
*ua_sess
;
1852 struct ust_app_channel
*ua_chan
;
1853 struct ltt_ust_stream
*ustream
;
1856 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1860 ua_sess
= lookup_session_by_app(usess
, app
);
1861 if (ua_sess
== NULL
) {
1862 goto error_rcu_unlock
;
1865 /* Upon restart, we skip the setup, already done */
1866 if (ua_sess
->started
) {
1870 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1872 goto error_rcu_unlock
;
1875 /* For each channel */
1876 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1878 /* Create all streams */
1880 /* Create UST stream */
1881 ustream
= zmalloc(sizeof(*ustream
));
1882 if (ustream
== NULL
) {
1883 PERROR("zmalloc ust stream");
1884 goto error_rcu_unlock
;
1887 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1890 /* Got all streams */
1893 ustream
->handle
= ustream
->obj
->handle
;
1895 /* Order is important */
1896 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1897 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1898 ua_sess
->path
, ua_chan
->name
,
1899 ua_chan
->streams
.count
++);
1901 PERROR("asprintf UST create stream");
1904 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1909 switch (app
->bits_per_long
) {
1911 consumerd_fd
= ust_consumerd64_fd
;
1914 consumerd_fd
= ust_consumerd32_fd
;
1918 goto error_rcu_unlock
;
1921 /* Setup UST consumer socket and send fds to it */
1922 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1924 goto error_rcu_unlock
;
1926 ua_sess
->started
= 1;
1929 /* This start the UST tracing */
1930 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1932 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1933 goto error_rcu_unlock
;
1938 /* Quiescent wait after starting trace */
1939 ustctl_wait_quiescent(app
->key
.sock
);
1949 * Stop tracing for a specific UST session and app.
1951 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1954 struct lttng_ht_iter iter
;
1955 struct ust_app_session
*ua_sess
;
1956 struct ust_app_channel
*ua_chan
;
1958 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1962 ua_sess
= lookup_session_by_app(usess
, app
);
1963 if (ua_sess
== NULL
) {
1964 /* Only malloc can failed so something is really wrong */
1965 goto error_rcu_unlock
;
1968 /* Not started, continuing. */
1969 if (ua_sess
->started
== 0) {
1973 /* This inhibits UST tracing */
1974 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1976 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1977 goto error_rcu_unlock
;
1980 /* Quiescent wait after stopping trace */
1981 ustctl_wait_quiescent(app
->key
.sock
);
1983 /* Flushing buffers */
1984 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1986 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_chan
->obj
);
1988 ERR("UST app PID %d channel %s flush failed with ret %d",
1989 app
->key
.pid
, ua_chan
->name
, ret
);
1990 /* Continuing flushing all buffers */
1995 /* Flush all buffers before stopping */
1996 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_sess
->metadata
->obj
);
1998 ERR("UST app PID %d metadata flush failed with ret %d", app
->key
.pid
,
2002 ua_sess
->started
= 0;
2014 * Destroy a specific UST session in apps.
2016 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2018 struct ust_app_session
*ua_sess
;
2019 struct lttng_ust_object_data obj
;
2020 struct lttng_ht_iter iter
;
2021 struct lttng_ht_node_ulong
*node
;
2024 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
2028 __lookup_session_by_app(usess
, app
, &iter
);
2029 node
= lttng_ht_iter_get_node_ulong(&iter
);
2031 /* Only malloc can failed so something is really wrong */
2032 goto error_rcu_unlock
;
2034 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2035 ret
= lttng_ht_del(app
->sessions
, &iter
);
2037 obj
.handle
= ua_sess
->handle
;
2040 obj
.memory_map_size
= 0;
2041 ustctl_release_object(app
->key
.sock
, &obj
);
2043 delete_ust_app_session(app
->key
.sock
, ua_sess
);
2047 /* Quiescent wait after stopping trace */
2048 ustctl_wait_quiescent(app
->key
.sock
);
2058 * Start tracing for the UST session.
2060 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2063 struct lttng_ht_iter iter
;
2064 struct ust_app
*app
;
2066 DBG("Starting all UST traces");
2070 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2071 ret
= ust_app_start_trace(usess
, app
);
2073 /* Continue to next apps even on error */
2084 * Start tracing for the UST session.
2086 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2089 struct lttng_ht_iter iter
;
2090 struct ust_app
*app
;
2092 DBG("Stopping all UST traces");
2096 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2097 ret
= ust_app_stop_trace(usess
, app
);
2099 /* Continue to next apps even on error */
2110 * Destroy app UST session.
2112 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2115 struct lttng_ht_iter iter
;
2116 struct ust_app
*app
;
2118 DBG("Destroy all UST traces");
2122 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2123 ret
= ust_app_destroy_trace(usess
, app
);
2125 /* Continue to next apps even on error */
2136 * Add channels/events from UST global domain to registered apps at sock.
2138 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2141 struct lttng_ht_iter iter
, uiter
;
2142 struct ust_app
*app
;
2143 struct ust_app_session
*ua_sess
;
2144 struct ust_app_channel
*ua_chan
;
2145 struct ust_app_event
*ua_event
;
2147 if (usess
== NULL
) {
2148 ERR("No UST session on global update. Returning");
2152 DBG2("UST app global update for app sock %d for session id %d", sock
,
2157 app
= find_app_by_sock(sock
);
2159 ERR("Failed to update app sock %d", sock
);
2163 ua_sess
= create_ust_app_session(usess
, app
);
2164 if (ua_sess
== NULL
) {
2169 * We can iterate safely here over all UST app session sicne the create ust
2170 * app session above made a shadow copy of the UST global domain from the
2173 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2175 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2177 /* FIXME: Should we quit here or continue... */
2181 /* For each events */
2182 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2184 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2186 /* FIXME: Should we quit here or continue... */
2192 if (usess
->start_trace
) {
2193 ret
= ust_app_start_trace(usess
, app
);
2198 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2207 * Add context to a specific channel for global UST domain.
2209 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2210 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2213 struct lttng_ht_node_str
*ua_chan_node
;
2214 struct lttng_ht_iter iter
, uiter
;
2215 struct ust_app_channel
*ua_chan
= NULL
;
2216 struct ust_app_session
*ua_sess
;
2217 struct ust_app
*app
;
2221 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2222 ua_sess
= lookup_session_by_app(usess
, app
);
2223 if (ua_sess
== NULL
) {
2227 /* Lookup channel in the ust app session */
2228 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2229 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2230 if (ua_chan_node
== NULL
) {
2233 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2236 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2247 * Add context to a specific event in a channel for global UST domain.
2249 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2250 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2251 struct ltt_ust_context
*uctx
)
2254 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2255 struct lttng_ht_iter iter
, uiter
;
2256 struct ust_app_session
*ua_sess
;
2257 struct ust_app_event
*ua_event
;
2258 struct ust_app_channel
*ua_chan
= NULL
;
2259 struct ust_app
*app
;
2263 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2264 ua_sess
= lookup_session_by_app(usess
, app
);
2265 if (ua_sess
== NULL
) {
2269 /* Lookup channel in the ust app session */
2270 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2271 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2272 if (ua_chan_node
== NULL
) {
2275 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2278 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2279 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2280 if (ua_event_node
== NULL
) {
2283 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2286 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2297 * Enable event for a channel from a UST session for a specific PID.
2299 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2300 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2303 struct lttng_ht_iter iter
;
2304 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2305 struct ust_app
*app
;
2306 struct ust_app_session
*ua_sess
;
2307 struct ust_app_channel
*ua_chan
;
2308 struct ust_app_event
*ua_event
;
2310 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2314 app
= ust_app_find_by_pid(pid
);
2316 ERR("UST app enable event per PID %d not found", pid
);
2321 ua_sess
= lookup_session_by_app(usess
, app
);
2322 /* If ua_sess is NULL, there is a code flow error */
2325 /* Lookup channel in the ust app session */
2326 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2327 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2328 /* If the channel is not found, there is a code flow error */
2329 assert(ua_chan_node
);
2331 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2333 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2334 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2335 if (ua_event_node
== NULL
) {
2336 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2341 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2343 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2355 * Disable event for a channel from a UST session for a specific PID.
2357 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2358 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2361 struct lttng_ht_iter iter
;
2362 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2363 struct ust_app
*app
;
2364 struct ust_app_session
*ua_sess
;
2365 struct ust_app_channel
*ua_chan
;
2366 struct ust_app_event
*ua_event
;
2368 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2372 app
= ust_app_find_by_pid(pid
);
2374 ERR("UST app disable event per PID %d not found", pid
);
2379 ua_sess
= lookup_session_by_app(usess
, app
);
2380 /* If ua_sess is NULL, there is a code flow error */
2383 /* Lookup channel in the ust app session */
2384 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2385 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2386 if (ua_chan_node
== NULL
) {
2387 /* Channel does not exist, skip disabling */
2390 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2392 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2393 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2394 if (ua_event_node
== NULL
) {
2395 /* Event does not exist, skip disabling */
2398 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2400 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);