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>
30 #include <urcu/compiler.h>
34 #include <lttng-share.h>
38 #include "ust-consumer.h"
42 * Delete ust context safely. RCU read lock must be held before calling
46 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
49 ustctl_release_object(sock
, ua_ctx
->obj
);
56 * Delete ust app event safely. RCU read lock must be held before calling
60 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
63 struct lttng_ht_iter iter
;
64 struct ust_app_ctx
*ua_ctx
;
66 /* Destroy each context of event */
67 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter
.iter
, ua_ctx
,
69 ret
= lttng_ht_del(ua_event
->ctx
, &iter
);
71 delete_ust_app_ctx(sock
, ua_ctx
);
73 lttng_ht_destroy(ua_event
->ctx
);
75 if (ua_event
->obj
!= NULL
) {
76 ustctl_release_object(sock
, ua_event
->obj
);
83 * Delete ust app stream safely. RCU read lock must be held before calling
87 void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
90 ustctl_release_object(sock
, stream
->obj
);
97 * Delete ust app channel safely. RCU read lock must be held before calling
101 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
104 struct lttng_ht_iter iter
;
105 struct ust_app_event
*ua_event
;
106 struct ust_app_ctx
*ua_ctx
;
107 struct ltt_ust_stream
*stream
, *stmp
;
110 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
111 cds_list_del(&stream
->list
);
112 delete_ust_app_stream(sock
, stream
);
116 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
117 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
119 delete_ust_app_ctx(sock
, ua_ctx
);
121 lttng_ht_destroy(ua_chan
->ctx
);
124 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
126 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
128 delete_ust_app_event(sock
, ua_event
);
130 lttng_ht_destroy(ua_chan
->events
);
132 if (ua_chan
->obj
!= NULL
) {
133 ustctl_release_object(sock
, ua_chan
->obj
);
140 * Delete ust app session safely. RCU read lock must be held before calling
144 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
)
147 struct lttng_ht_iter iter
;
148 struct ust_app_channel
*ua_chan
;
150 if (ua_sess
->metadata
) {
151 if (ua_sess
->metadata
->stream_obj
) {
152 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
153 free(ua_sess
->metadata
->stream_obj
);
155 if (ua_sess
->metadata
->obj
) {
156 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
157 free(ua_sess
->metadata
->obj
);
161 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
163 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
165 delete_ust_app_channel(sock
, ua_chan
);
167 lttng_ht_destroy(ua_sess
->channels
);
169 if (ua_sess
->handle
!= -1) {
170 ustctl_release_handle(sock
, ua_sess
->handle
);
176 * Delete a traceable application structure from the global list. Never call
177 * this function outside of a call_rcu call.
180 void delete_ust_app(struct ust_app
*app
)
183 struct lttng_ht_iter iter
;
184 struct ust_app_session
*ua_sess
;
188 /* Delete ust app sessions info */
189 sock
= app
->key
.sock
;
193 cds_lfht_for_each_entry(app
->sessions
->ht
, &iter
.iter
, ua_sess
,
195 ret
= lttng_ht_del(app
->sessions
, &iter
);
197 delete_ust_app_session(app
->key
.sock
, ua_sess
);
199 lttng_ht_destroy(app
->sessions
);
202 * Wait until we have removed the key from the sock hash table before
203 * closing this socket, otherwise an application could re-use the socket ID
204 * and race with the teardown, using the same hash table entry.
208 DBG2("UST app pid %d deleted", app
->key
.pid
);
215 * URCU intermediate call to delete an UST app.
218 void delete_ust_app_rcu(struct rcu_head
*head
)
220 struct lttng_ht_node_ulong
*node
=
221 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
222 struct ust_app
*app
=
223 caa_container_of(node
, struct ust_app
, node
);
225 DBG3("Call RCU deleting app PID %d", app
->key
.pid
);
230 * Alloc new UST app session.
233 struct ust_app_session
*alloc_ust_app_session(void)
235 struct ust_app_session
*ua_sess
;
237 /* Init most of the default value by allocating and zeroing */
238 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
239 if (ua_sess
== NULL
) {
244 ua_sess
->handle
= -1;
245 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
254 * Alloc new UST app channel.
257 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
258 struct lttng_ust_channel
*attr
)
260 struct ust_app_channel
*ua_chan
;
262 /* Init most of the default value by allocating and zeroing */
263 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
264 if (ua_chan
== NULL
) {
269 /* Setup channel name */
270 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
271 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
273 ua_chan
->enabled
= 1;
274 ua_chan
->handle
= -1;
275 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
276 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
277 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
279 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
281 /* Copy attributes */
283 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
286 DBG3("UST app channel %s allocated", ua_chan
->name
);
295 * Alloc new UST app event.
298 struct ust_app_event
*alloc_ust_app_event(char *name
,
299 struct lttng_ust_event
*attr
)
301 struct ust_app_event
*ua_event
;
303 /* Init most of the default value by allocating and zeroing */
304 ua_event
= zmalloc(sizeof(struct ust_app_event
));
305 if (ua_event
== NULL
) {
310 ua_event
->enabled
= 1;
311 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
312 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
313 ua_event
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
314 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
316 /* Copy attributes */
318 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
321 DBG3("UST app event %s allocated", ua_event
->name
);
330 * Alloc new UST app context.
333 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
335 struct ust_app_ctx
*ua_ctx
;
337 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
338 if (ua_ctx
== NULL
) {
343 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
346 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
353 * Find an ust_app using the sock and return it. RCU read side lock must be
354 * held before calling this helper function.
357 struct ust_app
*find_app_by_sock(int sock
)
359 struct lttng_ht_node_ulong
*node
;
360 struct ust_app_key
*key
;
361 struct lttng_ht_iter iter
;
363 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
),
365 node
= lttng_ht_iter_get_node_ulong(&iter
);
367 DBG2("UST app find by sock %d key not found", sock
);
370 key
= caa_container_of(node
, struct ust_app_key
, node
);
372 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) key
->pid
), &iter
);
373 node
= lttng_ht_iter_get_node_ulong(&iter
);
375 DBG2("UST app find by sock %d not found", sock
);
378 return caa_container_of(node
, struct ust_app
, node
);
385 * Create the channel context on the tracer.
388 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
389 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
393 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
394 ua_chan
->obj
, &ua_ctx
->obj
);
399 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
401 DBG2("UST app context added to channel %s successfully", ua_chan
->name
);
408 * Create the event context on the tracer.
411 int create_ust_event_context(struct ust_app_event
*ua_event
,
412 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
416 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
417 ua_event
->obj
, &ua_ctx
->obj
);
422 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
424 DBG2("UST app context added to event %s successfully", ua_event
->name
);
431 * Disable the specified event on to UST tracer for the UST session.
433 static int disable_ust_event(struct ust_app
*app
,
434 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
438 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
440 ERR("UST app event %s disable failed for app (pid: %d) "
441 "and session handle %d with ret %d",
442 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
446 DBG2("UST app event %s disabled successfully for app (pid: %d)",
447 ua_event
->attr
.name
, app
->key
.pid
);
454 * Disable the specified channel on to UST tracer for the UST session.
456 static int disable_ust_channel(struct ust_app
*app
,
457 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
461 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
463 ERR("UST app channel %s disable failed for app (pid: %d) "
464 "and session handle %d with ret %d",
465 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
469 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
470 ua_chan
->name
, app
->key
.pid
);
477 * Enable the specified channel on to UST tracer for the UST session.
479 static int enable_ust_channel(struct ust_app
*app
,
480 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
484 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
486 ERR("UST app channel %s enable failed for app (pid: %d) "
487 "and session handle %d with ret %d",
488 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
492 ua_chan
->enabled
= 1;
494 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
495 ua_chan
->name
, app
->key
.pid
);
502 * Enable the specified event on to UST tracer for the UST session.
504 static int enable_ust_event(struct ust_app
*app
,
505 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
509 ret
= ustctl_enable(app
->key
.sock
, ua_event
->obj
);
511 ERR("UST app event %s enable failed for app (pid: %d) "
512 "and session handle %d with ret %d",
513 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
517 DBG2("UST app event %s enabled successfully for app (pid: %d)",
518 ua_event
->attr
.name
, app
->key
.pid
);
525 * Open metadata onto the UST tracer for a UST session.
527 static int open_ust_metadata(struct ust_app
*app
,
528 struct ust_app_session
*ua_sess
)
531 struct lttng_ust_channel_attr uattr
;
533 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
534 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
535 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
536 uattr
.switch_timer_interval
=
537 ua_sess
->metadata
->attr
.switch_timer_interval
;
538 uattr
.read_timer_interval
=
539 ua_sess
->metadata
->attr
.read_timer_interval
;
540 uattr
.output
= ua_sess
->metadata
->attr
.output
;
542 /* UST tracer metadata creation */
543 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
544 &ua_sess
->metadata
->obj
);
546 ERR("UST app open metadata failed for app pid:%d with ret %d",
551 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
558 * Create stream onto the UST tracer for a UST session.
560 static int create_ust_stream(struct ust_app
*app
,
561 struct ust_app_session
*ua_sess
)
565 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
566 &ua_sess
->metadata
->stream_obj
);
568 ERR("UST create metadata stream failed");
577 * Create the specified channel onto the UST tracer for a UST session.
579 static int create_ust_channel(struct ust_app
*app
,
580 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
584 /* TODO: remove cast and use lttng-ust-abi.h */
585 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
586 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
588 ERR("Creating channel %s for app (pid: %d, sock: %d) "
589 "and session handle %d with ret %d",
590 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
591 ua_sess
->handle
, ret
);
595 ua_chan
->handle
= ua_chan
->obj
->handle
;
597 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
598 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
600 /* If channel is not enabled, disable it on the tracer */
601 if (!ua_chan
->enabled
) {
602 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
613 * Create the specified event onto the UST tracer for a UST session.
616 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
617 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
621 /* Create UST event on tracer */
622 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
625 if (ret
== -EEXIST
) {
629 ERR("Error ustctl create event %s for app pid: %d with ret %d",
630 ua_event
->attr
.name
, app
->key
.pid
, ret
);
634 ua_event
->handle
= ua_event
->obj
->handle
;
636 DBG2("UST app event %s created successfully for pid:%d",
637 ua_event
->attr
.name
, app
->key
.pid
);
639 /* If event not enabled, disable it on the tracer */
640 if (ua_event
->enabled
== 0) {
641 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
644 * If we hit an EPERM, something is wrong with our disable call. If
645 * we get an EEXIST, there is a problem on the tracer side since we
650 /* Code flow problem */
653 /* It's OK for our use case. */
668 * Copy data between an UST app event and a LTT event.
670 static void shadow_copy_event(struct ust_app_event
*ua_event
,
671 struct ltt_ust_event
*uevent
)
673 struct lttng_ht_iter iter
;
674 struct ltt_ust_context
*uctx
;
675 struct ust_app_ctx
*ua_ctx
;
677 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
678 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
680 ua_event
->enabled
= uevent
->enabled
;
682 /* Copy event attributes */
683 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
685 cds_lfht_for_each_entry(uevent
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
686 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
687 if (ua_ctx
== NULL
) {
688 /* malloc() failed. We should simply stop */
692 lttng_ht_node_init_ulong(&ua_ctx
->node
,
693 (unsigned long) ua_ctx
->ctx
.ctx
);
694 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
699 * Copy data between an UST app channel and a LTT channel.
701 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
702 struct ltt_ust_channel
*uchan
)
704 struct lttng_ht_iter iter
;
705 struct lttng_ht_node_str
*ua_event_node
;
706 struct ltt_ust_event
*uevent
;
707 struct ltt_ust_context
*uctx
;
708 struct ust_app_event
*ua_event
;
709 struct ust_app_ctx
*ua_ctx
;
711 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
713 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
714 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
715 /* Copy event attributes */
716 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
718 ua_chan
->enabled
= uchan
->enabled
;
720 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
721 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
722 if (ua_ctx
== NULL
) {
725 lttng_ht_node_init_ulong(&ua_ctx
->node
,
726 (unsigned long) ua_ctx
->ctx
.ctx
);
727 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
730 /* Copy all events from ltt ust channel to ust app channel */
731 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
732 struct lttng_ht_iter uiter
;
734 lttng_ht_lookup(ua_chan
->events
, (void *) uevent
->attr
.name
, &uiter
);
735 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
736 if (ua_event_node
== NULL
) {
737 DBG2("UST event %s not found on shadow copy channel",
739 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
740 if (ua_event
== NULL
) {
743 shadow_copy_event(ua_event
, uevent
);
744 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
748 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
752 * Copy data between a UST app session and a regular LTT session.
754 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
755 struct ltt_ust_session
*usess
, struct ust_app
*app
)
757 struct lttng_ht_node_str
*ua_chan_node
;
758 struct lttng_ht_iter iter
;
759 struct ltt_ust_channel
*uchan
;
760 struct ust_app_channel
*ua_chan
;
766 /* Get date and time for unique app path */
768 timeinfo
= localtime(&rawtime
);
769 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
771 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
773 ua_sess
->id
= usess
->id
;
774 ua_sess
->uid
= usess
->uid
;
775 ua_sess
->gid
= usess
->gid
;
777 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s/%s-%d-%s", usess
->pathname
,
778 app
->name
, app
->key
.pid
, datetime
);
780 PERROR("asprintf UST shadow copy session");
781 /* TODO: We cannot return an error from here.. */
785 /* TODO: support all UST domain */
787 /* Iterate over all channels in global domain. */
788 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
790 struct lttng_ht_iter uiter
;
792 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
793 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
794 if (ua_chan_node
!= NULL
) {
795 /* Session exist. Contiuing. */
799 DBG2("Channel %s not found on shadow session copy, creating it",
801 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
802 if (ua_chan
== NULL
) {
803 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
807 shadow_copy_channel(ua_chan
, uchan
);
808 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
813 * Lookup sesison wrapper.
816 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
817 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
819 /* Get right UST app session from app */
820 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
824 * Return ust app session from the app session hashtable using the UST session
827 static struct ust_app_session
*lookup_session_by_app(
828 struct ltt_ust_session
*usess
, struct ust_app
*app
)
830 struct lttng_ht_iter iter
;
831 struct lttng_ht_node_ulong
*node
;
833 __lookup_session_by_app(usess
, app
, &iter
);
834 node
= lttng_ht_iter_get_node_ulong(&iter
);
839 return caa_container_of(node
, struct ust_app_session
, node
);
846 * Create a UST session onto the tracer of app and add it the session
849 * Return ust app session or NULL on error.
851 static struct ust_app_session
*create_ust_app_session(
852 struct ltt_ust_session
*usess
, struct ust_app
*app
)
855 struct ust_app_session
*ua_sess
;
857 ua_sess
= lookup_session_by_app(usess
, app
);
858 if (ua_sess
== NULL
) {
859 DBG2("UST app pid: %d session id %d not found, creating it",
860 app
->key
.pid
, usess
->id
);
861 ua_sess
= alloc_ust_app_session();
862 if (ua_sess
== NULL
) {
863 /* Only malloc can failed so something is really wrong */
866 shadow_copy_session(ua_sess
, usess
, app
);
869 if (ua_sess
->handle
== -1) {
870 ret
= ustctl_create_session(app
->key
.sock
);
872 ERR("Creating session for app pid %d", app
->key
.pid
);
876 ua_sess
->handle
= ret
;
878 /* Add ust app session to app's HT */
879 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
880 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
882 DBG2("UST app session created successfully with handle %d", ret
);
889 delete_ust_app_session(-1, ua_sess
);
894 * Create a context for the channel on the tracer.
897 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
898 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
902 struct lttng_ht_iter iter
;
903 struct lttng_ht_node_ulong
*node
;
904 struct ust_app_ctx
*ua_ctx
;
906 DBG2("UST app adding context to channel %s", ua_chan
->name
);
908 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
909 node
= lttng_ht_iter_get_node_ulong(&iter
);
915 ua_ctx
= alloc_ust_app_ctx(uctx
);
916 if (ua_ctx
== NULL
) {
922 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
923 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
925 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
935 * Create an UST context and enable it for the event on the tracer.
938 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
939 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
943 struct lttng_ht_iter iter
;
944 struct lttng_ht_node_ulong
*node
;
945 struct ust_app_ctx
*ua_ctx
;
947 DBG2("UST app adding context to event %s", ua_event
->name
);
949 lttng_ht_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
950 node
= lttng_ht_iter_get_node_ulong(&iter
);
956 ua_ctx
= alloc_ust_app_ctx(uctx
);
957 if (ua_ctx
== NULL
) {
963 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
964 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
966 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
976 * Enable on the tracer side a ust app event for the session and channel.
979 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
980 struct ust_app_event
*ua_event
, struct ust_app
*app
)
984 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
989 ua_event
->enabled
= 1;
996 * Disable on the tracer side a ust app event for the session and channel.
998 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
999 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1003 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1008 ua_event
->enabled
= 0;
1015 * Lookup ust app channel for session and disable it on the tracer side.
1018 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1019 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1023 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1028 ua_chan
->enabled
= 0;
1035 * Lookup ust app channel for session and enable it on the tracer side.
1037 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1038 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1041 struct lttng_ht_iter iter
;
1042 struct lttng_ht_node_str
*ua_chan_node
;
1043 struct ust_app_channel
*ua_chan
;
1045 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1046 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1047 if (ua_chan_node
== NULL
) {
1048 DBG2("Unable to find channel %s in ust session id %u",
1049 uchan
->name
, ua_sess
->id
);
1053 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1055 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1065 * Create UST app channel and create it on the tracer.
1067 static struct ust_app_channel
*create_ust_app_channel(
1068 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1069 struct ust_app
*app
)
1072 struct lttng_ht_iter iter
;
1073 struct lttng_ht_node_str
*ua_chan_node
;
1074 struct ust_app_channel
*ua_chan
;
1076 /* Lookup channel in the ust app session */
1077 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1078 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1079 if (ua_chan_node
!= NULL
) {
1080 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1084 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1085 if (ua_chan
== NULL
) {
1086 /* Only malloc can fail here */
1089 shadow_copy_channel(ua_chan
, uchan
);
1091 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1093 /* Not found previously means that it does not exist on the tracer */
1094 assert(ret
!= -EEXIST
);
1098 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1100 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1107 delete_ust_app_channel(-1, ua_chan
);
1112 * Create UST app event and create it on the tracer side.
1115 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1116 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1117 struct ust_app
*app
)
1120 struct lttng_ht_iter iter
;
1121 struct lttng_ht_node_str
*ua_event_node
;
1122 struct ust_app_event
*ua_event
;
1124 /* Get event node */
1125 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
1126 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
1127 if (ua_event_node
!= NULL
) {
1132 /* Does not exist so create one */
1133 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1134 if (ua_event
== NULL
) {
1135 /* Only malloc can failed so something is really wrong */
1139 shadow_copy_event(ua_event
, uevent
);
1141 /* Create it on the tracer side */
1142 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1144 /* Not found previously means that it does not exist on the tracer */
1145 assert(ret
!= -EEXIST
);
1149 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
1151 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1158 /* Valid. Calling here is already in a read side lock */
1159 delete_ust_app_event(-1, ua_event
);
1164 * Create UST metadata and open it on the tracer side.
1166 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1167 char *pathname
, struct ust_app
*app
)
1171 if (ua_sess
->metadata
== NULL
) {
1172 /* Allocate UST metadata */
1173 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1174 if (ua_sess
->metadata
== NULL
) {
1175 /* malloc() failed */
1179 ret
= open_ust_metadata(app
, ua_sess
);
1181 /* Cleanup failed metadata struct */
1182 free(ua_sess
->metadata
);
1186 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
1189 /* Open UST metadata stream */
1190 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1191 ret
= create_ust_stream(app
, ua_sess
);
1196 ret
= mkdir_run_as(ua_sess
->path
, S_IRWXU
| S_IRWXG
,
1197 ua_sess
->uid
, ua_sess
->gid
);
1199 PERROR("mkdir UST metadata");
1203 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1204 "%s/metadata", ua_sess
->path
);
1206 PERROR("asprintf UST create stream");
1210 DBG2("UST metadata stream object created for app pid %d",
1213 ERR("Attempting to create stream without metadata opened");
1224 * Return pointer to traceable apps list.
1226 struct lttng_ht
*ust_app_get_ht(void)
1232 * Return ust app pointer or NULL if not found.
1234 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1236 struct lttng_ht_node_ulong
*node
;
1237 struct lttng_ht_iter iter
;
1240 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1241 node
= lttng_ht_iter_get_node_ulong(&iter
);
1243 DBG2("UST app no found with pid %d", pid
);
1248 DBG2("Found UST app by pid %d", pid
);
1250 return caa_container_of(node
, struct ust_app
, node
);
1258 * Using pid and uid (of the app), allocate a new ust_app struct and
1259 * add it to the global traceable app list.
1261 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1262 * bitness is not supported.
1264 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1266 struct ust_app
*lta
;
1268 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1269 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1270 ERR("Registration failed: application \"%s\" (pid: %d) has "
1271 "%d-bit long, but no consumerd for this long size is available.\n",
1272 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1276 lta
= zmalloc(sizeof(struct ust_app
));
1282 lta
->ppid
= msg
->ppid
;
1283 lta
->uid
= msg
->uid
;
1284 lta
->gid
= msg
->gid
;
1285 lta
->bits_per_long
= msg
->bits_per_long
;
1286 lta
->v_major
= msg
->major
;
1287 lta
->v_minor
= msg
->minor
;
1288 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1289 lta
->name
[16] = '\0';
1290 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1293 lta
->key
.pid
= msg
->pid
;
1294 lttng_ht_node_init_ulong(<a
->node
, (unsigned long)lta
->key
.pid
);
1295 lta
->key
.sock
= sock
;
1296 lttng_ht_node_init_ulong(<a
->key
.node
, (unsigned long)lta
->key
.sock
);
1299 lttng_ht_add_unique_ulong(ust_app_sock_key_map
, <a
->key
.node
);
1300 lttng_ht_add_unique_ulong(ust_app_ht
, <a
->node
);
1303 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1304 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1305 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1311 * Unregister app by removing it from the global traceable app list and freeing
1314 * The socket is already closed at this point so no close to sock.
1316 void ust_app_unregister(int sock
)
1318 struct ust_app
*lta
;
1319 struct lttng_ht_node_ulong
*node
;
1320 struct lttng_ht_iter iter
;
1324 lta
= find_app_by_sock(sock
);
1326 ERR("Unregister app sock %d not found!", sock
);
1330 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1332 /* Remove application from socket hash table */
1333 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
), &iter
);
1334 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1337 /* Get the node reference for a call_rcu */
1338 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) lta
->key
.pid
), &iter
);
1339 node
= lttng_ht_iter_get_node_ulong(&iter
);
1341 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1345 /* Remove application from PID hash table */
1346 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1348 call_rcu(&node
->head
, delete_ust_app_rcu
);
1355 * Return traceable_app_count
1357 unsigned long ust_app_list_count(void)
1359 unsigned long count
;
1362 count
= lttng_ht_get_count(ust_app_ht
);
1369 * Fill events array with all events name of all registered apps.
1371 int ust_app_list_events(struct lttng_event
**events
)
1374 size_t nbmem
, count
= 0;
1375 struct lttng_ht_iter iter
;
1376 struct ust_app
*app
;
1377 struct lttng_event
*tmp
;
1379 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1380 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1382 PERROR("zmalloc ust app events");
1389 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1390 struct lttng_ust_tracepoint_iter uiter
;
1392 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1394 ERR("UST app list events getting handle failed for app pid %d",
1399 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1400 &uiter
)) != -ENOENT
) {
1401 if (count
>= nbmem
) {
1402 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1405 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1407 PERROR("realloc ust app events");
1412 memcpy(tmp
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1413 memcpy(tmp
[count
].loglevel
, uiter
.loglevel
, LTTNG_UST_SYM_NAME_LEN
);
1414 tmp
[count
].loglevel_value
= uiter
.loglevel_value
;
1415 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1416 tmp
[count
].pid
= app
->key
.pid
;
1417 tmp
[count
].enabled
= -1;
1425 DBG2("UST app list events done (%zu events)", count
);
1434 * Free and clean all traceable apps of the global list.
1436 void ust_app_clean_list(void)
1439 struct lttng_ht_iter iter
;
1440 struct lttng_ht_node_ulong
*node
;
1442 DBG2("UST app cleaning registered apps hash table");
1446 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, node
, node
) {
1447 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1449 call_rcu(&node
->head
, delete_ust_app_rcu
);
1451 /* Destroy is done only when the ht is empty */
1452 lttng_ht_destroy(ust_app_ht
);
1454 cds_lfht_for_each_entry(ust_app_sock_key_map
->ht
, &iter
.iter
, node
, node
) {
1455 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1458 /* Destroy is done only when the ht is empty */
1459 lttng_ht_destroy(ust_app_sock_key_map
);
1465 * Init UST app hash table.
1467 void ust_app_ht_alloc(void)
1469 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1470 ust_app_sock_key_map
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1474 * For a specific UST session, disable the channel for all registered apps.
1476 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1477 struct ltt_ust_channel
*uchan
)
1480 struct lttng_ht_iter iter
;
1481 struct lttng_ht_node_str
*ua_chan_node
;
1482 struct ust_app
*app
;
1483 struct ust_app_session
*ua_sess
;
1484 struct ust_app_channel
*ua_chan
;
1486 if (usess
== NULL
|| uchan
== NULL
) {
1487 ERR("Disabling UST global channel with NULL values");
1492 DBG2("UST app disabling channel %s from global domain for session id %d",
1493 uchan
->name
, usess
->id
);
1497 /* For every registered applications */
1498 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1499 struct lttng_ht_iter uiter
;
1500 ua_sess
= lookup_session_by_app(usess
, app
);
1501 if (ua_sess
== NULL
) {
1506 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1507 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1508 /* If the session if found for the app, the channel must be there */
1509 assert(ua_chan_node
);
1511 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1512 /* The channel must not be already disabled */
1513 assert(ua_chan
->enabled
== 1);
1515 /* Disable channel onto application */
1516 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1518 /* XXX: We might want to report this error at some point... */
1530 * For a specific UST session, enable the channel for all registered apps.
1532 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1533 struct ltt_ust_channel
*uchan
)
1536 struct lttng_ht_iter iter
;
1537 struct ust_app
*app
;
1538 struct ust_app_session
*ua_sess
;
1540 if (usess
== NULL
|| uchan
== NULL
) {
1541 ERR("Adding UST global channel to NULL values");
1546 DBG2("UST app enabling channel %s to global domain for session id %d",
1547 uchan
->name
, usess
->id
);
1551 /* For every registered applications */
1552 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1553 ua_sess
= lookup_session_by_app(usess
, app
);
1554 if (ua_sess
== NULL
) {
1558 /* Enable channel onto application */
1559 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1561 /* XXX: We might want to report this error at some point... */
1573 * Disable an event in a channel and for a specific session.
1575 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1576 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1579 struct lttng_ht_iter iter
, uiter
;
1580 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1581 struct ust_app
*app
;
1582 struct ust_app_session
*ua_sess
;
1583 struct ust_app_channel
*ua_chan
;
1584 struct ust_app_event
*ua_event
;
1586 DBG("UST app disabling event %s for all apps in channel "
1587 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
1591 /* For all registered applications */
1592 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1593 ua_sess
= lookup_session_by_app(usess
, app
);
1594 if (ua_sess
== NULL
) {
1599 /* Lookup channel in the ust app session */
1600 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1601 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1602 if (ua_chan_node
== NULL
) {
1603 DBG2("Channel %s not found in session id %d for app pid %d."
1604 "Skipping", uchan
->name
, usess
->id
, app
->key
.pid
);
1607 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1609 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
1610 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1611 if (ua_event_node
== NULL
) {
1612 DBG2("Event %s not found in channel %s for app pid %d."
1613 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1616 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1618 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1620 /* XXX: Report error someday... */
1631 * For a specific UST session and UST channel, the event for all
1634 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1635 struct ltt_ust_channel
*uchan
)
1638 struct lttng_ht_iter iter
, uiter
;
1639 struct lttng_ht_node_str
*ua_chan_node
;
1640 struct ust_app
*app
;
1641 struct ust_app_session
*ua_sess
;
1642 struct ust_app_channel
*ua_chan
;
1643 struct ust_app_event
*ua_event
;
1645 DBG("UST app disabling all event for all apps in channel "
1646 "%s for session id %d", uchan
->name
, usess
->id
);
1650 /* For all registered applications */
1651 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1652 ua_sess
= lookup_session_by_app(usess
, app
);
1653 /* If ua_sess is NULL, there is a code flow error */
1656 /* Lookup channel in the ust app session */
1657 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1658 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1659 /* If the channel is not found, there is a code flow error */
1660 assert(ua_chan_node
);
1662 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1664 /* Disable each events of channel */
1665 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
1667 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1669 /* XXX: Report error someday... */
1681 * For a specific UST session, create the channel for all registered apps.
1683 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1684 struct ltt_ust_channel
*uchan
)
1686 struct lttng_ht_iter iter
;
1687 struct ust_app
*app
;
1688 struct ust_app_session
*ua_sess
;
1689 struct ust_app_channel
*ua_chan
;
1691 /* Very wrong code flow */
1695 DBG2("UST app adding channel %s to global domain for session id %d",
1696 uchan
->name
, usess
->id
);
1700 /* For every registered applications */
1701 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1703 * Create session on the tracer side and add it to app session HT. Note
1704 * that if session exist, it will simply return a pointer to the ust
1707 ua_sess
= create_ust_app_session(usess
, app
);
1708 if (ua_sess
== NULL
) {
1709 /* Major problem here and it's maybe the tracer or malloc() */
1713 /* Create channel onto application */
1714 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1715 if (ua_chan
== NULL
) {
1716 /* Major problem here and it's maybe the tracer or malloc() */
1730 * Enable event for a specific session and channel on the tracer.
1732 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1733 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1736 struct lttng_ht_iter iter
, uiter
;
1737 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1738 struct ust_app
*app
;
1739 struct ust_app_session
*ua_sess
;
1740 struct ust_app_channel
*ua_chan
;
1741 struct ust_app_event
*ua_event
;
1743 DBG("UST app enabling event %s for all apps for session id %d",
1744 uevent
->attr
.name
, usess
->id
);
1747 * NOTE: At this point, this function is called only if the session and
1748 * channel passed are already created for all apps. and enabled on the
1754 /* For all registered applications */
1755 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1756 ua_sess
= lookup_session_by_app(usess
, app
);
1757 /* If ua_sess is NULL, there is a code flow error */
1760 /* Lookup channel in the ust app session */
1761 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1762 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1763 /* If the channel is not found, there is a code flow error */
1764 assert(ua_chan_node
);
1766 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1768 lttng_ht_lookup(ua_chan
->events
, (void*)uevent
->attr
.name
, &uiter
);
1769 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1770 if (ua_event_node
== NULL
) {
1771 DBG3("UST app enable event %s not found for app PID %d."
1772 "Skipping app", uevent
->attr
.name
, app
->key
.pid
);
1775 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1777 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1789 * For a specific existing UST session and UST channel, creates the event for
1790 * all registered apps.
1792 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1793 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1796 struct lttng_ht_iter iter
, uiter
;
1797 struct lttng_ht_node_str
*ua_chan_node
;
1798 struct ust_app
*app
;
1799 struct ust_app_session
*ua_sess
;
1800 struct ust_app_channel
*ua_chan
;
1802 DBG("UST app creating event %s for all apps for session id %d",
1803 uevent
->attr
.name
, usess
->id
);
1807 /* For all registered applications */
1808 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1809 ua_sess
= lookup_session_by_app(usess
, app
);
1810 /* If ua_sess is NULL, there is a code flow error */
1813 /* Lookup channel in the ust app session */
1814 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1815 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1816 /* If the channel is not found, there is a code flow error */
1817 assert(ua_chan_node
);
1819 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1821 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1823 if (ret
!= -EEXIST
) {
1824 /* Possible value at this point: -ENOMEM. If so, we stop! */
1827 DBG2("UST app event %s already exist on app PID %d",
1828 uevent
->attr
.name
, app
->key
.pid
);
1839 * Start tracing for a specific UST session and app.
1841 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1844 struct lttng_ht_iter iter
;
1845 struct ust_app_session
*ua_sess
;
1846 struct ust_app_channel
*ua_chan
;
1847 struct ltt_ust_stream
*ustream
;
1850 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1854 ua_sess
= lookup_session_by_app(usess
, app
);
1855 if (ua_sess
== NULL
) {
1856 goto error_rcu_unlock
;
1859 /* Upon restart, we skip the setup, already done */
1860 if (ua_sess
->started
) {
1864 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1866 goto error_rcu_unlock
;
1869 /* For each channel */
1870 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1872 /* Create all streams */
1874 /* Create UST stream */
1875 ustream
= zmalloc(sizeof(*ustream
));
1876 if (ustream
== NULL
) {
1877 PERROR("zmalloc ust stream");
1878 goto error_rcu_unlock
;
1881 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1884 /* Got all streams */
1887 ustream
->handle
= ustream
->obj
->handle
;
1889 /* Order is important */
1890 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1891 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1892 ua_sess
->path
, ua_chan
->name
,
1893 ua_chan
->streams
.count
++);
1895 PERROR("asprintf UST create stream");
1898 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1903 switch (app
->bits_per_long
) {
1905 consumerd_fd
= ust_consumerd64_fd
;
1908 consumerd_fd
= ust_consumerd32_fd
;
1912 goto error_rcu_unlock
;
1915 /* Setup UST consumer socket and send fds to it */
1916 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1918 goto error_rcu_unlock
;
1920 ua_sess
->started
= 1;
1923 /* This start the UST tracing */
1924 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1926 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1927 goto error_rcu_unlock
;
1932 /* Quiescent wait after starting trace */
1933 ustctl_wait_quiescent(app
->key
.sock
);
1943 * Stop tracing for a specific UST session and app.
1945 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1948 struct lttng_ht_iter iter
;
1949 struct ust_app_session
*ua_sess
;
1950 struct ust_app_channel
*ua_chan
;
1952 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1956 ua_sess
= lookup_session_by_app(usess
, app
);
1957 if (ua_sess
== NULL
) {
1958 /* Only malloc can failed so something is really wrong */
1959 goto error_rcu_unlock
;
1962 /* This inhibits UST tracing */
1963 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1965 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1966 goto error_rcu_unlock
;
1969 /* Quiescent wait after stopping trace */
1970 ustctl_wait_quiescent(app
->key
.sock
);
1972 /* Flushing buffers */
1973 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1975 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_chan
->obj
);
1977 ERR("UST app PID %d channel %s flush failed",
1978 app
->key
.pid
, ua_chan
->name
);
1979 ERR("Ended with ret %d", ret
);
1980 /* Continuing flushing all buffers */
1985 /* Flush all buffers before stopping */
1986 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_sess
->metadata
->obj
);
1988 ERR("UST app PID %d metadata flush failed", app
->key
.pid
);
1989 ERR("Ended with ret %d", ret
);
2002 * Destroy a specific UST session in apps.
2004 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2006 struct ust_app_session
*ua_sess
;
2007 struct lttng_ust_object_data obj
;
2008 struct lttng_ht_iter iter
;
2009 struct lttng_ht_node_ulong
*node
;
2012 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
2016 __lookup_session_by_app(usess
, app
, &iter
);
2017 node
= lttng_ht_iter_get_node_ulong(&iter
);
2019 /* Only malloc can failed so something is really wrong */
2020 goto error_rcu_unlock
;
2022 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2023 ret
= lttng_ht_del(app
->sessions
, &iter
);
2025 delete_ust_app_session(app
->key
.sock
, ua_sess
);
2026 obj
.handle
= ua_sess
->handle
;
2029 obj
.memory_map_size
= 0;
2030 ustctl_release_object(app
->key
.sock
, &obj
);
2034 /* Quiescent wait after stopping trace */
2035 ustctl_wait_quiescent(app
->key
.sock
);
2045 * Start tracing for the UST session.
2047 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2050 struct lttng_ht_iter iter
;
2051 struct ust_app
*app
;
2053 DBG("Starting all UST traces");
2057 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2058 ret
= ust_app_start_trace(usess
, app
);
2060 /* Continue to next apps even on error */
2071 * Start tracing for the UST session.
2073 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2076 struct lttng_ht_iter iter
;
2077 struct ust_app
*app
;
2079 DBG("Stopping all UST traces");
2083 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2084 ret
= ust_app_stop_trace(usess
, app
);
2086 /* Continue to next apps even on error */
2097 * Destroy app UST session.
2099 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2102 struct lttng_ht_iter iter
;
2103 struct ust_app
*app
;
2105 DBG("Destroy all UST traces");
2109 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2110 ret
= ust_app_destroy_trace(usess
, app
);
2112 /* Continue to next apps even on error */
2123 * Add channels/events from UST global domain to registered apps at sock.
2125 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2128 struct lttng_ht_iter iter
, uiter
;
2129 struct ust_app
*app
;
2130 struct ust_app_session
*ua_sess
;
2131 struct ust_app_channel
*ua_chan
;
2132 struct ust_app_event
*ua_event
;
2134 if (usess
== NULL
) {
2135 ERR("No UST session on global update. Returning");
2139 DBG2("UST app global update for app sock %d for session id %d", sock
,
2144 app
= find_app_by_sock(sock
);
2146 ERR("Failed to update app sock %d", sock
);
2150 ua_sess
= create_ust_app_session(usess
, app
);
2151 if (ua_sess
== NULL
) {
2156 * We can iterate safely here over all UST app session sicne the create ust
2157 * app session above made a shadow copy of the UST global domain from the
2160 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2162 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2164 /* FIXME: Should we quit here or continue... */
2168 /* For each events */
2169 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2171 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2173 /* FIXME: Should we quit here or continue... */
2179 if (usess
->start_trace
) {
2180 ret
= ust_app_start_trace(usess
, app
);
2185 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2194 * Add context to a specific channel for global UST domain.
2196 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2197 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2200 struct lttng_ht_node_str
*ua_chan_node
;
2201 struct lttng_ht_iter iter
, uiter
;
2202 struct ust_app_channel
*ua_chan
= NULL
;
2203 struct ust_app_session
*ua_sess
;
2204 struct ust_app
*app
;
2208 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2209 ua_sess
= lookup_session_by_app(usess
, app
);
2210 if (ua_sess
== NULL
) {
2214 /* Lookup channel in the ust app session */
2215 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2216 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2217 if (ua_chan_node
== NULL
) {
2220 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2223 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2234 * Add context to a specific event in a channel for global UST domain.
2236 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2237 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2238 struct ltt_ust_context
*uctx
)
2241 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2242 struct lttng_ht_iter iter
, uiter
;
2243 struct ust_app_session
*ua_sess
;
2244 struct ust_app_event
*ua_event
;
2245 struct ust_app_channel
*ua_chan
= NULL
;
2246 struct ust_app
*app
;
2250 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2251 ua_sess
= lookup_session_by_app(usess
, app
);
2252 if (ua_sess
== NULL
) {
2256 /* Lookup channel in the ust app session */
2257 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2258 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2259 if (ua_chan_node
== NULL
) {
2262 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2265 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2266 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2267 if (ua_event_node
== NULL
) {
2270 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2273 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2284 * Enable event for a channel from a UST session for a specific PID.
2286 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2287 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2290 struct lttng_ht_iter iter
;
2291 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2292 struct ust_app
*app
;
2293 struct ust_app_session
*ua_sess
;
2294 struct ust_app_channel
*ua_chan
;
2295 struct ust_app_event
*ua_event
;
2297 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2301 app
= ust_app_find_by_pid(pid
);
2303 ERR("UST app enable event per PID %d not found", pid
);
2308 ua_sess
= lookup_session_by_app(usess
, app
);
2309 /* If ua_sess is NULL, there is a code flow error */
2312 /* Lookup channel in the ust app session */
2313 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2314 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2315 /* If the channel is not found, there is a code flow error */
2316 assert(ua_chan_node
);
2318 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2320 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2321 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2322 if (ua_event_node
== NULL
) {
2323 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2328 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2330 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2342 * Disable event for a channel from a UST session for a specific PID.
2344 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2345 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2348 struct lttng_ht_iter iter
;
2349 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2350 struct ust_app
*app
;
2351 struct ust_app_session
*ua_sess
;
2352 struct ust_app_channel
*ua_chan
;
2353 struct ust_app_event
*ua_event
;
2355 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2359 app
= ust_app_find_by_pid(pid
);
2361 ERR("UST app disable event per PID %d not found", pid
);
2366 ua_sess
= lookup_session_by_app(usess
, app
);
2367 /* If ua_sess is NULL, there is a code flow error */
2370 /* Lookup channel in the ust app session */
2371 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2372 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2373 if (ua_chan_node
== NULL
) {
2374 /* Channel does not exist, skip disabling */
2377 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2379 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2380 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2381 if (ua_event_node
== NULL
) {
2382 /* Event does not exist, skip disabling */
2385 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2387 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);