2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
27 #include <urcu/compiler.h>
29 #include <common/common.h>
32 #include "ust-consumer.h"
36 * Delete ust context safely. RCU read lock must be held before calling
40 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
43 ustctl_release_object(sock
, ua_ctx
->obj
);
50 * Delete ust app event safely. RCU read lock must be held before calling
54 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
57 struct lttng_ht_iter iter
;
58 struct ust_app_ctx
*ua_ctx
;
60 /* Destroy each context of event */
61 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter
.iter
, ua_ctx
,
63 ret
= lttng_ht_del(ua_event
->ctx
, &iter
);
65 delete_ust_app_ctx(sock
, ua_ctx
);
67 lttng_ht_destroy(ua_event
->ctx
);
69 if (ua_event
->obj
!= NULL
) {
70 ustctl_release_object(sock
, ua_event
->obj
);
77 * Delete ust app stream safely. RCU read lock must be held before calling
81 void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
84 ustctl_release_object(sock
, stream
->obj
);
91 * Delete ust app channel safely. RCU read lock must be held before calling
95 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
98 struct lttng_ht_iter iter
;
99 struct ust_app_event
*ua_event
;
100 struct ust_app_ctx
*ua_ctx
;
101 struct ltt_ust_stream
*stream
, *stmp
;
104 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
105 cds_list_del(&stream
->list
);
106 delete_ust_app_stream(sock
, stream
);
110 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
111 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
113 delete_ust_app_ctx(sock
, ua_ctx
);
115 lttng_ht_destroy(ua_chan
->ctx
);
118 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
120 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
122 delete_ust_app_event(sock
, ua_event
);
124 lttng_ht_destroy(ua_chan
->events
);
126 if (ua_chan
->obj
!= NULL
) {
127 ustctl_release_object(sock
, ua_chan
->obj
);
134 * Delete ust app session safely. RCU read lock must be held before calling
138 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
)
141 struct lttng_ht_iter iter
;
142 struct ust_app_channel
*ua_chan
;
144 if (ua_sess
->metadata
) {
145 if (ua_sess
->metadata
->stream_obj
) {
146 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
147 free(ua_sess
->metadata
->stream_obj
);
149 if (ua_sess
->metadata
->obj
) {
150 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
151 free(ua_sess
->metadata
->obj
);
155 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
157 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
159 delete_ust_app_channel(sock
, ua_chan
);
161 lttng_ht_destroy(ua_sess
->channels
);
163 if (ua_sess
->handle
!= -1) {
164 ustctl_release_handle(sock
, ua_sess
->handle
);
170 * Delete a traceable application structure from the global list. Never call
171 * this function outside of a call_rcu call.
174 void delete_ust_app(struct ust_app
*app
)
177 struct lttng_ht_iter iter
;
178 struct ust_app_session
*ua_sess
;
182 /* Delete ust app sessions info */
183 sock
= app
->key
.sock
;
187 cds_lfht_for_each_entry(app
->sessions
->ht
, &iter
.iter
, ua_sess
,
189 ret
= lttng_ht_del(app
->sessions
, &iter
);
191 delete_ust_app_session(app
->key
.sock
, ua_sess
);
193 lttng_ht_destroy(app
->sessions
);
196 * Wait until we have removed the key from the sock hash table before
197 * closing this socket, otherwise an application could re-use the socket ID
198 * and race with the teardown, using the same hash table entry.
205 DBG2("UST app pid %d deleted", app
->key
.pid
);
212 * URCU intermediate call to delete an UST app.
215 void delete_ust_app_rcu(struct rcu_head
*head
)
217 struct lttng_ht_node_ulong
*node
=
218 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
219 struct ust_app
*app
=
220 caa_container_of(node
, struct ust_app
, node
);
222 DBG3("Call RCU deleting app PID %d", app
->key
.pid
);
227 * Alloc new UST app session.
230 struct ust_app_session
*alloc_ust_app_session(void)
232 struct ust_app_session
*ua_sess
;
234 /* Init most of the default value by allocating and zeroing */
235 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
236 if (ua_sess
== NULL
) {
241 ua_sess
->handle
= -1;
242 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
251 * Alloc new UST app channel.
254 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
255 struct lttng_ust_channel
*attr
)
257 struct ust_app_channel
*ua_chan
;
259 /* Init most of the default value by allocating and zeroing */
260 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
261 if (ua_chan
== NULL
) {
266 /* Setup channel name */
267 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
268 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
270 ua_chan
->enabled
= 1;
271 ua_chan
->handle
= -1;
272 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
273 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
274 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
276 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
278 /* Copy attributes */
280 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
283 DBG3("UST app channel %s allocated", ua_chan
->name
);
292 * Alloc new UST app event.
295 struct ust_app_event
*alloc_ust_app_event(char *name
,
296 struct lttng_ust_event
*attr
)
298 struct ust_app_event
*ua_event
;
300 /* Init most of the default value by allocating and zeroing */
301 ua_event
= zmalloc(sizeof(struct ust_app_event
));
302 if (ua_event
== NULL
) {
307 ua_event
->enabled
= 1;
308 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
309 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
310 ua_event
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
311 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
313 /* Copy attributes */
315 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
318 DBG3("UST app event %s allocated", ua_event
->name
);
327 * Alloc new UST app context.
330 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
332 struct ust_app_ctx
*ua_ctx
;
334 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
335 if (ua_ctx
== NULL
) {
340 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
343 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
350 * Find an ust_app using the sock and return it. RCU read side lock must be
351 * held before calling this helper function.
354 struct ust_app
*find_app_by_sock(int sock
)
356 struct lttng_ht_node_ulong
*node
;
357 struct ust_app_key
*key
;
358 struct lttng_ht_iter iter
;
360 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
),
362 node
= lttng_ht_iter_get_node_ulong(&iter
);
364 DBG2("UST app find by sock %d key not found", sock
);
367 key
= caa_container_of(node
, struct ust_app_key
, node
);
369 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) key
->pid
), &iter
);
370 node
= lttng_ht_iter_get_node_ulong(&iter
);
372 DBG2("UST app find by sock %d not found", sock
);
375 return caa_container_of(node
, struct ust_app
, node
);
382 * Create the channel context on the tracer.
385 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
386 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
390 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
391 ua_chan
->obj
, &ua_ctx
->obj
);
396 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
398 DBG2("UST app context created successfully for channel %s", ua_chan
->name
);
405 * Create the event context on the tracer.
408 int create_ust_event_context(struct ust_app_event
*ua_event
,
409 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
413 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
414 ua_event
->obj
, &ua_ctx
->obj
);
419 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
421 DBG2("UST app context created successfully for event %s", ua_event
->name
);
428 * Disable the specified event on to UST tracer for the UST session.
430 static int disable_ust_event(struct ust_app
*app
,
431 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
435 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
437 ERR("UST app event %s disable failed for app (pid: %d) "
438 "and session handle %d with ret %d",
439 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
443 DBG2("UST app event %s disabled successfully for app (pid: %d)",
444 ua_event
->attr
.name
, app
->key
.pid
);
451 * Disable the specified channel on to UST tracer for the UST session.
453 static int disable_ust_channel(struct ust_app
*app
,
454 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
458 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
460 ERR("UST app channel %s disable failed for app (pid: %d) "
461 "and session handle %d with ret %d",
462 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
466 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
467 ua_chan
->name
, app
->key
.pid
);
474 * Enable the specified channel on to UST tracer for the UST session.
476 static int enable_ust_channel(struct ust_app
*app
,
477 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
481 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
483 ERR("UST app channel %s enable failed for app (pid: %d) "
484 "and session handle %d with ret %d",
485 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
489 ua_chan
->enabled
= 1;
491 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
492 ua_chan
->name
, app
->key
.pid
);
499 * Enable the specified event on to UST tracer for the UST session.
501 static int enable_ust_event(struct ust_app
*app
,
502 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
506 ret
= ustctl_enable(app
->key
.sock
, ua_event
->obj
);
508 ERR("UST app event %s enable failed for app (pid: %d) "
509 "and session handle %d with ret %d",
510 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
514 DBG2("UST app event %s enabled successfully for app (pid: %d)",
515 ua_event
->attr
.name
, app
->key
.pid
);
522 * Open metadata onto the UST tracer for a UST session.
524 static int open_ust_metadata(struct ust_app
*app
,
525 struct ust_app_session
*ua_sess
)
528 struct lttng_ust_channel_attr uattr
;
530 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
531 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
532 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
533 uattr
.switch_timer_interval
=
534 ua_sess
->metadata
->attr
.switch_timer_interval
;
535 uattr
.read_timer_interval
=
536 ua_sess
->metadata
->attr
.read_timer_interval
;
537 uattr
.output
= ua_sess
->metadata
->attr
.output
;
539 /* UST tracer metadata creation */
540 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
541 &ua_sess
->metadata
->obj
);
543 ERR("UST app open metadata failed for app pid:%d with ret %d",
548 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
555 * Create stream onto the UST tracer for a UST session.
557 static int create_ust_stream(struct ust_app
*app
,
558 struct ust_app_session
*ua_sess
)
562 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
563 &ua_sess
->metadata
->stream_obj
);
565 ERR("UST create metadata stream failed");
574 * Create the specified channel onto the UST tracer for a UST session.
576 static int create_ust_channel(struct ust_app
*app
,
577 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
581 /* TODO: remove cast and use lttng-ust-abi.h */
582 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
583 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
585 ERR("Creating channel %s for app (pid: %d, sock: %d) "
586 "and session handle %d with ret %d",
587 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
588 ua_sess
->handle
, ret
);
592 ua_chan
->handle
= ua_chan
->obj
->handle
;
594 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
595 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
597 /* If channel is not enabled, disable it on the tracer */
598 if (!ua_chan
->enabled
) {
599 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
610 * Create the specified event onto the UST tracer for a UST session.
613 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
614 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
618 /* Create UST event on tracer */
619 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
622 if (ret
== -EEXIST
) {
626 ERR("Error ustctl create event %s for app pid: %d with ret %d",
627 ua_event
->attr
.name
, app
->key
.pid
, ret
);
631 ua_event
->handle
= ua_event
->obj
->handle
;
633 DBG2("UST app event %s created successfully for pid:%d",
634 ua_event
->attr
.name
, app
->key
.pid
);
636 /* If event not enabled, disable it on the tracer */
637 if (ua_event
->enabled
== 0) {
638 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
641 * If we hit an EPERM, something is wrong with our disable call. If
642 * we get an EEXIST, there is a problem on the tracer side since we
647 /* Code flow problem */
650 /* It's OK for our use case. */
665 * Copy data between an UST app event and a LTT event.
667 static void shadow_copy_event(struct ust_app_event
*ua_event
,
668 struct ltt_ust_event
*uevent
)
670 struct lttng_ht_iter iter
;
671 struct ltt_ust_context
*uctx
;
672 struct ust_app_ctx
*ua_ctx
;
674 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
675 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
677 ua_event
->enabled
= uevent
->enabled
;
679 /* Copy event attributes */
680 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
682 cds_lfht_for_each_entry(uevent
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
683 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
684 if (ua_ctx
== NULL
) {
685 /* malloc() failed. We should simply stop */
689 lttng_ht_node_init_ulong(&ua_ctx
->node
,
690 (unsigned long) ua_ctx
->ctx
.ctx
);
691 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
696 * Copy data between an UST app channel and a LTT channel.
698 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
699 struct ltt_ust_channel
*uchan
)
701 struct lttng_ht_iter iter
;
702 struct lttng_ht_node_str
*ua_event_node
;
703 struct ltt_ust_event
*uevent
;
704 struct ltt_ust_context
*uctx
;
705 struct ust_app_event
*ua_event
;
706 struct ust_app_ctx
*ua_ctx
;
708 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
710 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
711 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
712 /* Copy event attributes */
713 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
715 ua_chan
->enabled
= uchan
->enabled
;
717 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
718 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
719 if (ua_ctx
== NULL
) {
722 lttng_ht_node_init_ulong(&ua_ctx
->node
,
723 (unsigned long) ua_ctx
->ctx
.ctx
);
724 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
727 /* Copy all events from ltt ust channel to ust app channel */
728 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
729 struct lttng_ht_iter uiter
;
731 lttng_ht_lookup(ua_chan
->events
, (void *) uevent
->attr
.name
, &uiter
);
732 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
733 if (ua_event_node
== NULL
) {
734 DBG2("UST event %s not found on shadow copy channel",
736 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
737 if (ua_event
== NULL
) {
740 shadow_copy_event(ua_event
, uevent
);
741 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
745 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
749 * Copy data between a UST app session and a regular LTT session.
751 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
752 struct ltt_ust_session
*usess
, struct ust_app
*app
)
754 struct lttng_ht_node_str
*ua_chan_node
;
755 struct lttng_ht_iter iter
;
756 struct ltt_ust_channel
*uchan
;
757 struct ust_app_channel
*ua_chan
;
763 /* Get date and time for unique app path */
765 timeinfo
= localtime(&rawtime
);
766 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
768 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
770 ua_sess
->id
= usess
->id
;
771 ua_sess
->uid
= usess
->uid
;
772 ua_sess
->gid
= usess
->gid
;
774 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s/%s-%d-%s", usess
->pathname
,
775 app
->name
, app
->key
.pid
, datetime
);
777 PERROR("asprintf UST shadow copy session");
778 /* TODO: We cannot return an error from here.. */
782 /* TODO: support all UST domain */
784 /* Iterate over all channels in global domain. */
785 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
787 struct lttng_ht_iter uiter
;
789 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
790 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
791 if (ua_chan_node
!= NULL
) {
792 /* Session exist. Contiuing. */
796 DBG2("Channel %s not found on shadow session copy, creating it",
798 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
799 if (ua_chan
== NULL
) {
800 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
804 shadow_copy_channel(ua_chan
, uchan
);
805 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
810 * Lookup sesison wrapper.
813 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
814 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
816 /* Get right UST app session from app */
817 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
821 * Return ust app session from the app session hashtable using the UST session
824 static struct ust_app_session
*lookup_session_by_app(
825 struct ltt_ust_session
*usess
, struct ust_app
*app
)
827 struct lttng_ht_iter iter
;
828 struct lttng_ht_node_ulong
*node
;
830 __lookup_session_by_app(usess
, app
, &iter
);
831 node
= lttng_ht_iter_get_node_ulong(&iter
);
836 return caa_container_of(node
, struct ust_app_session
, node
);
843 * Create a UST session onto the tracer of app and add it the session
846 * Return ust app session or NULL on error.
848 static struct ust_app_session
*create_ust_app_session(
849 struct ltt_ust_session
*usess
, struct ust_app
*app
)
852 struct ust_app_session
*ua_sess
;
854 ua_sess
= lookup_session_by_app(usess
, app
);
855 if (ua_sess
== NULL
) {
856 DBG2("UST app pid: %d session id %d not found, creating it",
857 app
->key
.pid
, usess
->id
);
858 ua_sess
= alloc_ust_app_session();
859 if (ua_sess
== NULL
) {
860 /* Only malloc can failed so something is really wrong */
863 shadow_copy_session(ua_sess
, usess
, app
);
866 if (ua_sess
->handle
== -1) {
867 ret
= ustctl_create_session(app
->key
.sock
);
869 ERR("Creating session for app pid %d", app
->key
.pid
);
870 /* This means that the tracer is gone... */
871 ua_sess
= (void*) -1UL;
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
;
1275 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1276 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1277 ERR("Registration failed: application \"%s\" (pid: %d) has "
1278 "%d-bit long, but no consumerd for this long size is available.\n",
1279 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1286 if (msg
->major
!= LTTNG_UST_COMM_MAJOR
) {
1287 ERR("Registration failed: application \"%s\" (pid: %d) has "
1288 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1289 msg
->name
, msg
->pid
, msg
->major
, msg
->minor
);
1296 lta
= zmalloc(sizeof(struct ust_app
));
1302 lta
->ppid
= msg
->ppid
;
1303 lta
->uid
= msg
->uid
;
1304 lta
->gid
= msg
->gid
;
1305 lta
->compatible
= 0; /* Not compatible until proven */
1306 lta
->bits_per_long
= msg
->bits_per_long
;
1307 lta
->v_major
= msg
->major
;
1308 lta
->v_minor
= msg
->minor
;
1309 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1310 lta
->name
[16] = '\0';
1311 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1314 lta
->key
.pid
= msg
->pid
;
1315 lttng_ht_node_init_ulong(<a
->node
, (unsigned long)lta
->key
.pid
);
1316 lta
->key
.sock
= sock
;
1317 lttng_ht_node_init_ulong(<a
->key
.node
, (unsigned long)lta
->key
.sock
);
1320 lttng_ht_add_unique_ulong(ust_app_sock_key_map
, <a
->key
.node
);
1321 lttng_ht_add_unique_ulong(ust_app_ht
, <a
->node
);
1324 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1325 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1326 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1332 * Unregister app by removing it from the global traceable app list and freeing
1335 * The socket is already closed at this point so no close to sock.
1337 void ust_app_unregister(int sock
)
1339 struct ust_app
*lta
;
1340 struct lttng_ht_node_ulong
*node
;
1341 struct lttng_ht_iter iter
;
1345 lta
= find_app_by_sock(sock
);
1347 ERR("Unregister app sock %d not found!", sock
);
1351 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1353 /* Remove application from socket hash table */
1354 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
), &iter
);
1355 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1358 /* Get the node reference for a call_rcu */
1359 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) lta
->key
.pid
), &iter
);
1360 node
= lttng_ht_iter_get_node_ulong(&iter
);
1362 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1366 /* Remove application from PID hash table */
1367 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1369 call_rcu(&node
->head
, delete_ust_app_rcu
);
1376 * Return traceable_app_count
1378 unsigned long ust_app_list_count(void)
1380 unsigned long count
;
1383 count
= lttng_ht_get_count(ust_app_ht
);
1390 * Fill events array with all events name of all registered apps.
1392 int ust_app_list_events(struct lttng_event
**events
)
1395 size_t nbmem
, count
= 0;
1396 struct lttng_ht_iter iter
;
1397 struct ust_app
*app
;
1398 struct lttng_event
*tmp
;
1400 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1401 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1403 PERROR("zmalloc ust app events");
1410 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1411 struct lttng_ust_tracepoint_iter uiter
;
1413 if (!app
->compatible
) {
1415 * TODO: In time, we should notice the caller of this error by
1416 * telling him that this is a version error.
1420 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1422 ERR("UST app list events getting handle failed for app pid %d",
1427 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1428 &uiter
)) != -ENOENT
) {
1429 if (count
>= nbmem
) {
1430 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1433 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1435 PERROR("realloc ust app events");
1440 memcpy(tmp
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1441 tmp
[count
].loglevel
= uiter
.loglevel
;
1442 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1443 tmp
[count
].pid
= app
->key
.pid
;
1444 tmp
[count
].enabled
= -1;
1452 DBG2("UST app list events done (%zu events)", count
);
1461 * Free and clean all traceable apps of the global list.
1463 void ust_app_clean_list(void)
1466 struct lttng_ht_iter iter
;
1467 struct lttng_ht_node_ulong
*node
;
1469 DBG2("UST app cleaning registered apps hash table");
1473 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, node
, node
) {
1474 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1476 call_rcu(&node
->head
, delete_ust_app_rcu
);
1478 /* Destroy is done only when the ht is empty */
1479 lttng_ht_destroy(ust_app_ht
);
1481 cds_lfht_for_each_entry(ust_app_sock_key_map
->ht
, &iter
.iter
, node
, node
) {
1482 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1485 /* Destroy is done only when the ht is empty */
1486 lttng_ht_destroy(ust_app_sock_key_map
);
1492 * Init UST app hash table.
1494 void ust_app_ht_alloc(void)
1496 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1497 ust_app_sock_key_map
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1501 * For a specific UST session, disable the channel for all registered apps.
1503 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1504 struct ltt_ust_channel
*uchan
)
1507 struct lttng_ht_iter iter
;
1508 struct lttng_ht_node_str
*ua_chan_node
;
1509 struct ust_app
*app
;
1510 struct ust_app_session
*ua_sess
;
1511 struct ust_app_channel
*ua_chan
;
1513 if (usess
== NULL
|| uchan
== NULL
) {
1514 ERR("Disabling UST global channel with NULL values");
1519 DBG2("UST app disabling channel %s from global domain for session id %d",
1520 uchan
->name
, usess
->id
);
1524 /* For every registered applications */
1525 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1526 struct lttng_ht_iter uiter
;
1527 if (!app
->compatible
) {
1529 * TODO: In time, we should notice the caller of this error by
1530 * telling him that this is a version error.
1534 ua_sess
= lookup_session_by_app(usess
, app
);
1535 if (ua_sess
== NULL
) {
1540 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1541 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1542 /* If the session if found for the app, the channel must be there */
1543 assert(ua_chan_node
);
1545 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1546 /* The channel must not be already disabled */
1547 assert(ua_chan
->enabled
== 1);
1549 /* Disable channel onto application */
1550 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1552 /* XXX: We might want to report this error at some point... */
1564 * For a specific UST session, enable the channel for all registered apps.
1566 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1567 struct ltt_ust_channel
*uchan
)
1570 struct lttng_ht_iter iter
;
1571 struct ust_app
*app
;
1572 struct ust_app_session
*ua_sess
;
1574 if (usess
== NULL
|| uchan
== NULL
) {
1575 ERR("Adding UST global channel to NULL values");
1580 DBG2("UST app enabling channel %s to global domain for session id %d",
1581 uchan
->name
, usess
->id
);
1585 /* For every registered applications */
1586 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1587 if (!app
->compatible
) {
1589 * TODO: In time, we should notice the caller of this error by
1590 * telling him that this is a version error.
1594 ua_sess
= lookup_session_by_app(usess
, app
);
1595 if (ua_sess
== NULL
) {
1599 /* Enable channel onto application */
1600 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1602 /* XXX: We might want to report this error at some point... */
1614 * Disable an event in a channel and for a specific session.
1616 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1617 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1620 struct lttng_ht_iter iter
, uiter
;
1621 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1622 struct ust_app
*app
;
1623 struct ust_app_session
*ua_sess
;
1624 struct ust_app_channel
*ua_chan
;
1625 struct ust_app_event
*ua_event
;
1627 DBG("UST app disabling event %s for all apps in channel "
1628 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
1632 /* For all registered applications */
1633 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1634 if (!app
->compatible
) {
1636 * TODO: In time, we should notice the caller of this error by
1637 * telling him that this is a version error.
1641 ua_sess
= lookup_session_by_app(usess
, app
);
1642 if (ua_sess
== NULL
) {
1647 /* Lookup channel in the ust app session */
1648 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1649 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1650 if (ua_chan_node
== NULL
) {
1651 DBG2("Channel %s not found in session id %d for app pid %d."
1652 "Skipping", uchan
->name
, usess
->id
, app
->key
.pid
);
1655 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1657 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
1658 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1659 if (ua_event_node
== NULL
) {
1660 DBG2("Event %s not found in channel %s for app pid %d."
1661 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1664 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1666 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1668 /* XXX: Report error someday... */
1679 * For a specific UST session and UST channel, the event for all
1682 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1683 struct ltt_ust_channel
*uchan
)
1686 struct lttng_ht_iter iter
, uiter
;
1687 struct lttng_ht_node_str
*ua_chan_node
;
1688 struct ust_app
*app
;
1689 struct ust_app_session
*ua_sess
;
1690 struct ust_app_channel
*ua_chan
;
1691 struct ust_app_event
*ua_event
;
1693 DBG("UST app disabling all event for all apps in channel "
1694 "%s for session id %d", uchan
->name
, usess
->id
);
1698 /* For all registered applications */
1699 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1700 if (!app
->compatible
) {
1702 * TODO: In time, we should notice the caller of this error by
1703 * telling him that this is a version error.
1707 ua_sess
= lookup_session_by_app(usess
, app
);
1708 /* If ua_sess is NULL, there is a code flow error */
1711 /* Lookup channel in the ust app session */
1712 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1713 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1714 /* If the channel is not found, there is a code flow error */
1715 assert(ua_chan_node
);
1717 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1719 /* Disable each events of channel */
1720 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
1722 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1724 /* XXX: Report error someday... */
1736 * For a specific UST session, create the channel for all registered apps.
1738 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1739 struct ltt_ust_channel
*uchan
)
1741 struct lttng_ht_iter iter
;
1742 struct ust_app
*app
;
1743 struct ust_app_session
*ua_sess
;
1744 struct ust_app_channel
*ua_chan
;
1746 /* Very wrong code flow */
1750 DBG2("UST app adding channel %s to global domain for session id %d",
1751 uchan
->name
, usess
->id
);
1755 /* For every registered applications */
1756 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1757 if (!app
->compatible
) {
1759 * TODO: In time, we should notice the caller of this error by
1760 * telling him that this is a version error.
1765 * Create session on the tracer side and add it to app session HT. Note
1766 * that if session exist, it will simply return a pointer to the ust
1769 ua_sess
= create_ust_app_session(usess
, app
);
1770 if (ua_sess
== NULL
) {
1771 /* The malloc() failed. */
1773 } else if (ua_sess
== (void *) -1UL) {
1774 /* The application's socket is not valid. Contiuing */
1778 /* Create channel onto application */
1779 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1780 if (ua_chan
== NULL
) {
1781 /* Major problem here and it's maybe the tracer or malloc() */
1795 * Enable event for a specific session and channel on the tracer.
1797 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1798 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1801 struct lttng_ht_iter iter
, uiter
;
1802 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1803 struct ust_app
*app
;
1804 struct ust_app_session
*ua_sess
;
1805 struct ust_app_channel
*ua_chan
;
1806 struct ust_app_event
*ua_event
;
1808 DBG("UST app enabling event %s for all apps for session id %d",
1809 uevent
->attr
.name
, usess
->id
);
1812 * NOTE: At this point, this function is called only if the session and
1813 * channel passed are already created for all apps. and enabled on the
1819 /* For all registered applications */
1820 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1821 if (!app
->compatible
) {
1823 * TODO: In time, we should notice the caller of this error by
1824 * telling him that this is a version error.
1828 ua_sess
= lookup_session_by_app(usess
, app
);
1829 /* If ua_sess is NULL, there is a code flow error */
1832 /* Lookup channel in the ust app session */
1833 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1834 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1835 /* If the channel is not found, there is a code flow error */
1836 assert(ua_chan_node
);
1838 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1840 lttng_ht_lookup(ua_chan
->events
, (void*)uevent
->attr
.name
, &uiter
);
1841 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1842 if (ua_event_node
== NULL
) {
1843 DBG3("UST app enable event %s not found for app PID %d."
1844 "Skipping app", uevent
->attr
.name
, app
->key
.pid
);
1847 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1849 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1861 * For a specific existing UST session and UST channel, creates the event for
1862 * all registered apps.
1864 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1865 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1868 struct lttng_ht_iter iter
, uiter
;
1869 struct lttng_ht_node_str
*ua_chan_node
;
1870 struct ust_app
*app
;
1871 struct ust_app_session
*ua_sess
;
1872 struct ust_app_channel
*ua_chan
;
1874 DBG("UST app creating event %s for all apps for session id %d",
1875 uevent
->attr
.name
, usess
->id
);
1879 /* For all registered applications */
1880 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1881 if (!app
->compatible
) {
1883 * TODO: In time, we should notice the caller of this error by
1884 * telling him that this is a version error.
1888 ua_sess
= lookup_session_by_app(usess
, app
);
1889 /* If ua_sess is NULL, there is a code flow error */
1892 /* Lookup channel in the ust app session */
1893 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1894 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1895 /* If the channel is not found, there is a code flow error */
1896 assert(ua_chan_node
);
1898 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1900 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1902 if (ret
!= -EEXIST
) {
1903 /* Possible value at this point: -ENOMEM. If so, we stop! */
1906 DBG2("UST app event %s already exist on app PID %d",
1907 uevent
->attr
.name
, app
->key
.pid
);
1918 * Start tracing for a specific UST session and app.
1920 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1923 struct lttng_ht_iter iter
;
1924 struct ust_app_session
*ua_sess
;
1925 struct ust_app_channel
*ua_chan
;
1926 struct ltt_ust_stream
*ustream
;
1929 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1933 if (!app
->compatible
) {
1937 ua_sess
= lookup_session_by_app(usess
, app
);
1938 if (ua_sess
== NULL
) {
1939 goto error_rcu_unlock
;
1942 /* Upon restart, we skip the setup, already done */
1943 if (ua_sess
->started
) {
1947 /* Indicate that the session has been started once */
1948 ua_sess
->started
= 1;
1950 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1952 goto error_rcu_unlock
;
1955 /* For each channel */
1956 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1958 /* Create all streams */
1960 /* Create UST stream */
1961 ustream
= zmalloc(sizeof(*ustream
));
1962 if (ustream
== NULL
) {
1963 PERROR("zmalloc ust stream");
1964 goto error_rcu_unlock
;
1967 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1970 /* Got all streams */
1973 ustream
->handle
= ustream
->obj
->handle
;
1975 /* Order is important */
1976 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1977 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1978 ua_sess
->path
, ua_chan
->name
,
1979 ua_chan
->streams
.count
++);
1981 PERROR("asprintf UST create stream");
1984 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1989 switch (app
->bits_per_long
) {
1991 consumerd_fd
= ust_consumerd64_fd
;
1994 consumerd_fd
= ust_consumerd32_fd
;
1998 goto error_rcu_unlock
;
2001 /* Setup UST consumer socket and send fds to it */
2002 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
2004 goto error_rcu_unlock
;
2008 /* This start the UST tracing */
2009 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
2011 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
2012 goto error_rcu_unlock
;
2015 /* Quiescent wait after starting trace */
2016 ustctl_wait_quiescent(app
->key
.sock
);
2028 * Stop tracing for a specific UST session and app.
2030 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2033 struct lttng_ht_iter iter
;
2034 struct ust_app_session
*ua_sess
;
2035 struct ust_app_channel
*ua_chan
;
2037 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
2041 if (!app
->compatible
) {
2045 ua_sess
= lookup_session_by_app(usess
, app
);
2046 if (ua_sess
== NULL
) {
2047 /* Only malloc can failed so something is really wrong */
2048 goto error_rcu_unlock
;
2052 * If started = 0, it means that stop trace has been called for a session
2053 * that was never started. This is a code flow error and should never
2056 assert(ua_sess
->started
== 1);
2058 /* This inhibits UST tracing */
2059 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
2061 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
2062 goto error_rcu_unlock
;
2065 /* Quiescent wait after stopping trace */
2066 ustctl_wait_quiescent(app
->key
.sock
);
2068 /* Flushing buffers */
2069 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2071 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_chan
->obj
);
2073 ERR("UST app PID %d channel %s flush failed with ret %d",
2074 app
->key
.pid
, ua_chan
->name
, ret
);
2075 /* Continuing flushing all buffers */
2080 /* Flush all buffers before stopping */
2081 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_sess
->metadata
->obj
);
2083 ERR("UST app PID %d metadata flush failed with ret %d", app
->key
.pid
,
2097 * Destroy a specific UST session in apps.
2099 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2101 struct ust_app_session
*ua_sess
;
2102 struct lttng_ust_object_data obj
;
2103 struct lttng_ht_iter iter
;
2104 struct lttng_ht_node_ulong
*node
;
2107 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
2111 if (!app
->compatible
) {
2115 __lookup_session_by_app(usess
, app
, &iter
);
2116 node
= lttng_ht_iter_get_node_ulong(&iter
);
2118 /* Only malloc can failed so something is really wrong */
2119 goto error_rcu_unlock
;
2121 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2122 ret
= lttng_ht_del(app
->sessions
, &iter
);
2124 obj
.handle
= ua_sess
->handle
;
2127 obj
.memory_map_size
= 0;
2128 ustctl_release_object(app
->key
.sock
, &obj
);
2130 delete_ust_app_session(app
->key
.sock
, ua_sess
);
2132 /* Quiescent wait after stopping trace */
2133 ustctl_wait_quiescent(app
->key
.sock
);
2145 * Start tracing for the UST session.
2147 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2150 struct lttng_ht_iter iter
;
2151 struct ust_app
*app
;
2153 DBG("Starting all UST traces");
2157 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2158 ret
= ust_app_start_trace(usess
, app
);
2160 /* Continue to next apps even on error */
2171 * Start tracing for the UST session.
2173 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2176 struct lttng_ht_iter iter
;
2177 struct ust_app
*app
;
2179 DBG("Stopping all UST traces");
2183 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2184 ret
= ust_app_stop_trace(usess
, app
);
2186 /* Continue to next apps even on error */
2197 * Destroy app UST session.
2199 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2202 struct lttng_ht_iter iter
;
2203 struct ust_app
*app
;
2205 DBG("Destroy all UST traces");
2209 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2210 ret
= ust_app_destroy_trace(usess
, app
);
2212 /* Continue to next apps even on error */
2223 * Add channels/events from UST global domain to registered apps at sock.
2225 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2228 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
2229 struct ust_app
*app
;
2230 struct ust_app_session
*ua_sess
;
2231 struct ust_app_channel
*ua_chan
;
2232 struct ust_app_event
*ua_event
;
2233 struct ust_app_ctx
*ua_ctx
;
2235 if (usess
== NULL
) {
2236 ERR("No UST session on global update. Returning");
2240 DBG2("UST app global update for app sock %d for session id %d", sock
,
2245 app
= find_app_by_sock(sock
);
2247 ERR("Failed to update app sock %d", sock
);
2251 if (!app
->compatible
) {
2255 ua_sess
= create_ust_app_session(usess
, app
);
2256 if (ua_sess
== NULL
) {
2261 * We can iterate safely here over all UST app session sicne the create ust
2262 * app session above made a shadow copy of the UST global domain from the
2265 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2267 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2269 /* FIXME: Should we quit here or continue... */
2273 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
2275 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2277 /* FIXME: Should we quit here or continue... */
2283 /* For each events */
2284 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2286 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2288 /* FIXME: Should we quit here or continue... */
2292 /* Add context on events. */
2293 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter_ctx
.iter
,
2294 ua_ctx
, node
.node
) {
2295 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
2297 /* FIXME: Should we quit here or continue... */
2304 if (usess
->start_trace
) {
2305 ret
= ust_app_start_trace(usess
, app
);
2310 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2319 * Add context to a specific channel for global UST domain.
2321 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2322 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2325 struct lttng_ht_node_str
*ua_chan_node
;
2326 struct lttng_ht_iter iter
, uiter
;
2327 struct ust_app_channel
*ua_chan
= NULL
;
2328 struct ust_app_session
*ua_sess
;
2329 struct ust_app
*app
;
2333 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2334 if (!app
->compatible
) {
2336 * TODO: In time, we should notice the caller of this error by
2337 * telling him that this is a version error.
2341 ua_sess
= lookup_session_by_app(usess
, app
);
2342 if (ua_sess
== NULL
) {
2346 /* Lookup channel in the ust app session */
2347 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2348 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2349 if (ua_chan_node
== NULL
) {
2352 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2355 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2366 * Add context to a specific event in a channel for global UST domain.
2368 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2369 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2370 struct ltt_ust_context
*uctx
)
2373 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2374 struct lttng_ht_iter iter
, uiter
;
2375 struct ust_app_session
*ua_sess
;
2376 struct ust_app_event
*ua_event
;
2377 struct ust_app_channel
*ua_chan
= NULL
;
2378 struct ust_app
*app
;
2382 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2383 if (!app
->compatible
) {
2385 * TODO: In time, we should notice the caller of this error by
2386 * telling him that this is a version error.
2390 ua_sess
= lookup_session_by_app(usess
, app
);
2391 if (ua_sess
== NULL
) {
2395 /* Lookup channel in the ust app session */
2396 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2397 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2398 if (ua_chan_node
== NULL
) {
2401 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2404 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2405 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2406 if (ua_event_node
== NULL
) {
2409 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2412 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2423 * Enable event for a channel from a UST session for a specific PID.
2425 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2426 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2429 struct lttng_ht_iter iter
;
2430 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2431 struct ust_app
*app
;
2432 struct ust_app_session
*ua_sess
;
2433 struct ust_app_channel
*ua_chan
;
2434 struct ust_app_event
*ua_event
;
2436 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2440 app
= ust_app_find_by_pid(pid
);
2442 ERR("UST app enable event per PID %d not found", pid
);
2447 if (!app
->compatible
) {
2452 ua_sess
= lookup_session_by_app(usess
, app
);
2453 /* If ua_sess is NULL, there is a code flow error */
2456 /* Lookup channel in the ust app session */
2457 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2458 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2459 /* If the channel is not found, there is a code flow error */
2460 assert(ua_chan_node
);
2462 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2464 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2465 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2466 if (ua_event_node
== NULL
) {
2467 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2472 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2474 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2486 * Disable event for a channel from a UST session for a specific PID.
2488 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2489 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2492 struct lttng_ht_iter iter
;
2493 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2494 struct ust_app
*app
;
2495 struct ust_app_session
*ua_sess
;
2496 struct ust_app_channel
*ua_chan
;
2497 struct ust_app_event
*ua_event
;
2499 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2503 app
= ust_app_find_by_pid(pid
);
2505 ERR("UST app disable event per PID %d not found", pid
);
2510 if (!app
->compatible
) {
2515 ua_sess
= lookup_session_by_app(usess
, app
);
2516 /* If ua_sess is NULL, there is a code flow error */
2519 /* Lookup channel in the ust app session */
2520 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2521 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2522 if (ua_chan_node
== NULL
) {
2523 /* Channel does not exist, skip disabling */
2526 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2528 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2529 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2530 if (ua_event_node
== NULL
) {
2531 /* Event does not exist, skip disabling */
2534 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2536 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2547 * Validate version of UST apps and set the compatible bit.
2549 int ust_app_validate_version(int sock
)
2552 struct ust_app
*app
;
2556 app
= find_app_by_sock(sock
);
2559 ret
= ustctl_tracer_version(sock
, &app
->version
);
2564 /* Validate version */
2565 if (app
->version
.major
> UST_APP_MAJOR_VERSION
) {
2569 DBG2("UST app PID %d is compatible with major version %d "
2570 "(supporting <= %d)", app
->key
.pid
, app
->version
.major
,
2571 UST_APP_MAJOR_VERSION
);
2572 app
->compatible
= 1;
2577 DBG2("UST app PID %d is not compatible with major version %d "
2578 "(supporting <= %d)", app
->key
.pid
, app
->version
.major
,
2579 UST_APP_MAJOR_VERSION
);
2580 app
->compatible
= 0;
2586 * Calibrate registered applications.
2588 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
2591 struct lttng_ht_iter iter
;
2592 struct ust_app
*app
;
2596 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2597 if (!app
->compatible
) {
2599 * TODO: In time, we should notice the caller of this error by
2600 * telling him that this is a version error.
2605 ret
= ustctl_calibrate(app
->key
.sock
, calibrate
);
2609 /* Means that it's not implemented on the tracer side. */
2613 /* TODO: Report error to user */
2614 DBG2("Calibrate app PID %d returned with error %d",
2621 DBG("UST app global domain calibration finished");