2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <sys/types.h>
28 #include <urcu/compiler.h>
30 #include <common/common.h>
33 #include "ust-consumer.h"
37 * Delete ust context safely. RCU read lock must be held before calling
41 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
44 ustctl_release_object(sock
, ua_ctx
->obj
);
51 * Delete ust app event safely. RCU read lock must be held before calling
55 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
58 struct lttng_ht_iter iter
;
59 struct ust_app_ctx
*ua_ctx
;
61 /* Destroy each context of event */
62 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter
.iter
, ua_ctx
,
64 ret
= lttng_ht_del(ua_event
->ctx
, &iter
);
66 delete_ust_app_ctx(sock
, ua_ctx
);
68 lttng_ht_destroy(ua_event
->ctx
);
70 if (ua_event
->obj
!= NULL
) {
71 ustctl_release_object(sock
, ua_event
->obj
);
78 * Delete ust app stream safely. RCU read lock must be held before calling
82 void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
85 ustctl_release_object(sock
, stream
->obj
);
92 * Delete ust app channel safely. RCU read lock must be held before calling
96 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
99 struct lttng_ht_iter iter
;
100 struct ust_app_event
*ua_event
;
101 struct ust_app_ctx
*ua_ctx
;
102 struct ltt_ust_stream
*stream
, *stmp
;
105 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
106 cds_list_del(&stream
->list
);
107 delete_ust_app_stream(sock
, stream
);
111 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
112 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
114 delete_ust_app_ctx(sock
, ua_ctx
);
116 lttng_ht_destroy(ua_chan
->ctx
);
119 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
121 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
123 delete_ust_app_event(sock
, ua_event
);
125 lttng_ht_destroy(ua_chan
->events
);
127 if (ua_chan
->obj
!= NULL
) {
128 ustctl_release_object(sock
, ua_chan
->obj
);
135 * Delete ust app session safely. RCU read lock must be held before calling
139 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
)
142 struct lttng_ht_iter iter
;
143 struct ust_app_channel
*ua_chan
;
145 if (ua_sess
->metadata
) {
146 if (ua_sess
->metadata
->stream_obj
) {
147 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
148 free(ua_sess
->metadata
->stream_obj
);
150 if (ua_sess
->metadata
->obj
) {
151 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
152 free(ua_sess
->metadata
->obj
);
156 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
158 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
160 delete_ust_app_channel(sock
, ua_chan
);
162 lttng_ht_destroy(ua_sess
->channels
);
164 if (ua_sess
->handle
!= -1) {
165 ustctl_release_handle(sock
, ua_sess
->handle
);
171 * Delete a traceable application structure from the global list. Never call
172 * this function outside of a call_rcu call.
175 void delete_ust_app(struct ust_app
*app
)
178 struct lttng_ht_iter iter
;
179 struct ust_app_session
*ua_sess
;
183 /* Delete ust app sessions info */
184 sock
= app
->key
.sock
;
188 cds_lfht_for_each_entry(app
->sessions
->ht
, &iter
.iter
, ua_sess
,
190 ret
= lttng_ht_del(app
->sessions
, &iter
);
192 delete_ust_app_session(app
->key
.sock
, ua_sess
);
194 lttng_ht_destroy(app
->sessions
);
197 * Wait until we have removed the key from the sock hash table before
198 * closing this socket, otherwise an application could re-use the socket ID
199 * and race with the teardown, using the same hash table entry.
203 DBG2("UST app pid %d deleted", app
->key
.pid
);
210 * URCU intermediate call to delete an UST app.
213 void delete_ust_app_rcu(struct rcu_head
*head
)
215 struct lttng_ht_node_ulong
*node
=
216 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
217 struct ust_app
*app
=
218 caa_container_of(node
, struct ust_app
, node
);
220 DBG3("Call RCU deleting app PID %d", app
->key
.pid
);
225 * Alloc new UST app session.
228 struct ust_app_session
*alloc_ust_app_session(void)
230 struct ust_app_session
*ua_sess
;
232 /* Init most of the default value by allocating and zeroing */
233 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
234 if (ua_sess
== NULL
) {
239 ua_sess
->handle
= -1;
240 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
249 * Alloc new UST app channel.
252 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
253 struct lttng_ust_channel
*attr
)
255 struct ust_app_channel
*ua_chan
;
257 /* Init most of the default value by allocating and zeroing */
258 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
259 if (ua_chan
== NULL
) {
264 /* Setup channel name */
265 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
266 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
268 ua_chan
->enabled
= 1;
269 ua_chan
->handle
= -1;
270 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
271 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
272 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
274 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
276 /* Copy attributes */
278 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
281 DBG3("UST app channel %s allocated", ua_chan
->name
);
290 * Alloc new UST app event.
293 struct ust_app_event
*alloc_ust_app_event(char *name
,
294 struct lttng_ust_event
*attr
)
296 struct ust_app_event
*ua_event
;
298 /* Init most of the default value by allocating and zeroing */
299 ua_event
= zmalloc(sizeof(struct ust_app_event
));
300 if (ua_event
== NULL
) {
305 ua_event
->enabled
= 1;
306 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
307 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
308 ua_event
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
309 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
311 /* Copy attributes */
313 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
316 DBG3("UST app event %s allocated", ua_event
->name
);
325 * Alloc new UST app context.
328 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
330 struct ust_app_ctx
*ua_ctx
;
332 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
333 if (ua_ctx
== NULL
) {
338 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
341 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
348 * Find an ust_app using the sock and return it. RCU read side lock must be
349 * held before calling this helper function.
352 struct ust_app
*find_app_by_sock(int sock
)
354 struct lttng_ht_node_ulong
*node
;
355 struct ust_app_key
*key
;
356 struct lttng_ht_iter iter
;
358 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
),
360 node
= lttng_ht_iter_get_node_ulong(&iter
);
362 DBG2("UST app find by sock %d key not found", sock
);
365 key
= caa_container_of(node
, struct ust_app_key
, node
);
367 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) key
->pid
), &iter
);
368 node
= lttng_ht_iter_get_node_ulong(&iter
);
370 DBG2("UST app find by sock %d not found", sock
);
373 return caa_container_of(node
, struct ust_app
, node
);
380 * Create the channel context on the tracer.
383 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
384 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
388 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
389 ua_chan
->obj
, &ua_ctx
->obj
);
394 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
396 DBG2("UST app context created successfully for channel %s", ua_chan
->name
);
403 * Create the event context on the tracer.
406 int create_ust_event_context(struct ust_app_event
*ua_event
,
407 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
411 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
412 ua_event
->obj
, &ua_ctx
->obj
);
417 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
419 DBG2("UST app context created successfully for event %s", ua_event
->name
);
426 * Disable the specified event on to UST tracer for the UST session.
428 static int disable_ust_event(struct ust_app
*app
,
429 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
433 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
435 ERR("UST app event %s disable failed for app (pid: %d) "
436 "and session handle %d with ret %d",
437 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
441 DBG2("UST app event %s disabled successfully for app (pid: %d)",
442 ua_event
->attr
.name
, app
->key
.pid
);
449 * Disable the specified channel on to UST tracer for the UST session.
451 static int disable_ust_channel(struct ust_app
*app
,
452 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
456 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
458 ERR("UST app channel %s disable failed for app (pid: %d) "
459 "and session handle %d with ret %d",
460 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
464 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
465 ua_chan
->name
, app
->key
.pid
);
472 * Enable the specified channel on to UST tracer for the UST session.
474 static int enable_ust_channel(struct ust_app
*app
,
475 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
479 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
481 ERR("UST app channel %s enable failed for app (pid: %d) "
482 "and session handle %d with ret %d",
483 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
487 ua_chan
->enabled
= 1;
489 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
490 ua_chan
->name
, app
->key
.pid
);
497 * Enable the specified event on to UST tracer for the UST session.
499 static int enable_ust_event(struct ust_app
*app
,
500 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
504 ret
= ustctl_enable(app
->key
.sock
, ua_event
->obj
);
506 ERR("UST app event %s enable failed for app (pid: %d) "
507 "and session handle %d with ret %d",
508 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
512 DBG2("UST app event %s enabled successfully for app (pid: %d)",
513 ua_event
->attr
.name
, app
->key
.pid
);
520 * Open metadata onto the UST tracer for a UST session.
522 static int open_ust_metadata(struct ust_app
*app
,
523 struct ust_app_session
*ua_sess
)
526 struct lttng_ust_channel_attr uattr
;
528 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
529 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
530 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
531 uattr
.switch_timer_interval
=
532 ua_sess
->metadata
->attr
.switch_timer_interval
;
533 uattr
.read_timer_interval
=
534 ua_sess
->metadata
->attr
.read_timer_interval
;
535 uattr
.output
= ua_sess
->metadata
->attr
.output
;
537 /* UST tracer metadata creation */
538 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
539 &ua_sess
->metadata
->obj
);
541 ERR("UST app open metadata failed for app pid:%d with ret %d",
546 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
553 * Create stream onto the UST tracer for a UST session.
555 static int create_ust_stream(struct ust_app
*app
,
556 struct ust_app_session
*ua_sess
)
560 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
561 &ua_sess
->metadata
->stream_obj
);
563 ERR("UST create metadata stream failed");
572 * Create the specified channel onto the UST tracer for a UST session.
574 static int create_ust_channel(struct ust_app
*app
,
575 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
579 /* TODO: remove cast and use lttng-ust-abi.h */
580 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
581 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
583 ERR("Creating channel %s for app (pid: %d, sock: %d) "
584 "and session handle %d with ret %d",
585 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
586 ua_sess
->handle
, ret
);
590 ua_chan
->handle
= ua_chan
->obj
->handle
;
592 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
593 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
595 /* If channel is not enabled, disable it on the tracer */
596 if (!ua_chan
->enabled
) {
597 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
608 * Create the specified event onto the UST tracer for a UST session.
611 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
612 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
616 /* Create UST event on tracer */
617 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
620 if (ret
== -EEXIST
) {
624 ERR("Error ustctl create event %s for app pid: %d with ret %d",
625 ua_event
->attr
.name
, app
->key
.pid
, ret
);
629 ua_event
->handle
= ua_event
->obj
->handle
;
631 DBG2("UST app event %s created successfully for pid:%d",
632 ua_event
->attr
.name
, app
->key
.pid
);
634 /* If event not enabled, disable it on the tracer */
635 if (ua_event
->enabled
== 0) {
636 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
639 * If we hit an EPERM, something is wrong with our disable call. If
640 * we get an EEXIST, there is a problem on the tracer side since we
645 /* Code flow problem */
648 /* It's OK for our use case. */
663 * Copy data between an UST app event and a LTT event.
665 static void shadow_copy_event(struct ust_app_event
*ua_event
,
666 struct ltt_ust_event
*uevent
)
668 struct lttng_ht_iter iter
;
669 struct ltt_ust_context
*uctx
;
670 struct ust_app_ctx
*ua_ctx
;
672 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
673 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
675 ua_event
->enabled
= uevent
->enabled
;
677 /* Copy event attributes */
678 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
680 cds_lfht_for_each_entry(uevent
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
681 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
682 if (ua_ctx
== NULL
) {
683 /* malloc() failed. We should simply stop */
687 lttng_ht_node_init_ulong(&ua_ctx
->node
,
688 (unsigned long) ua_ctx
->ctx
.ctx
);
689 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
694 * Copy data between an UST app channel and a LTT channel.
696 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
697 struct ltt_ust_channel
*uchan
)
699 struct lttng_ht_iter iter
;
700 struct lttng_ht_node_str
*ua_event_node
;
701 struct ltt_ust_event
*uevent
;
702 struct ltt_ust_context
*uctx
;
703 struct ust_app_event
*ua_event
;
704 struct ust_app_ctx
*ua_ctx
;
706 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
708 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
709 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
710 /* Copy event attributes */
711 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
713 ua_chan
->enabled
= uchan
->enabled
;
715 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
716 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
717 if (ua_ctx
== NULL
) {
720 lttng_ht_node_init_ulong(&ua_ctx
->node
,
721 (unsigned long) ua_ctx
->ctx
.ctx
);
722 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
725 /* Copy all events from ltt ust channel to ust app channel */
726 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
727 struct lttng_ht_iter uiter
;
729 lttng_ht_lookup(ua_chan
->events
, (void *) uevent
->attr
.name
, &uiter
);
730 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
731 if (ua_event_node
== NULL
) {
732 DBG2("UST event %s not found on shadow copy channel",
734 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
735 if (ua_event
== NULL
) {
738 shadow_copy_event(ua_event
, uevent
);
739 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
743 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
747 * Copy data between a UST app session and a regular LTT session.
749 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
750 struct ltt_ust_session
*usess
, struct ust_app
*app
)
752 struct lttng_ht_node_str
*ua_chan_node
;
753 struct lttng_ht_iter iter
;
754 struct ltt_ust_channel
*uchan
;
755 struct ust_app_channel
*ua_chan
;
761 /* Get date and time for unique app path */
763 timeinfo
= localtime(&rawtime
);
764 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
766 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
768 ua_sess
->id
= usess
->id
;
769 ua_sess
->uid
= usess
->uid
;
770 ua_sess
->gid
= usess
->gid
;
772 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s/%s-%d-%s", usess
->pathname
,
773 app
->name
, app
->key
.pid
, datetime
);
775 PERROR("asprintf UST shadow copy session");
776 /* TODO: We cannot return an error from here.. */
780 /* TODO: support all UST domain */
782 /* Iterate over all channels in global domain. */
783 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
785 struct lttng_ht_iter uiter
;
787 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
788 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
789 if (ua_chan_node
!= NULL
) {
790 /* Session exist. Contiuing. */
794 DBG2("Channel %s not found on shadow session copy, creating it",
796 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
797 if (ua_chan
== NULL
) {
798 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
802 shadow_copy_channel(ua_chan
, uchan
);
803 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
808 * Lookup sesison wrapper.
811 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
812 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
814 /* Get right UST app session from app */
815 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
819 * Return ust app session from the app session hashtable using the UST session
822 static struct ust_app_session
*lookup_session_by_app(
823 struct ltt_ust_session
*usess
, struct ust_app
*app
)
825 struct lttng_ht_iter iter
;
826 struct lttng_ht_node_ulong
*node
;
828 __lookup_session_by_app(usess
, app
, &iter
);
829 node
= lttng_ht_iter_get_node_ulong(&iter
);
834 return caa_container_of(node
, struct ust_app_session
, node
);
841 * Create a UST session onto the tracer of app and add it the session
844 * Return ust app session or NULL on error.
846 static struct ust_app_session
*create_ust_app_session(
847 struct ltt_ust_session
*usess
, struct ust_app
*app
)
850 struct ust_app_session
*ua_sess
;
852 ua_sess
= lookup_session_by_app(usess
, app
);
853 if (ua_sess
== NULL
) {
854 DBG2("UST app pid: %d session id %d not found, creating it",
855 app
->key
.pid
, usess
->id
);
856 ua_sess
= alloc_ust_app_session();
857 if (ua_sess
== NULL
) {
858 /* Only malloc can failed so something is really wrong */
861 shadow_copy_session(ua_sess
, usess
, app
);
864 if (ua_sess
->handle
== -1) {
865 ret
= ustctl_create_session(app
->key
.sock
);
867 ERR("Creating session for app pid %d", app
->key
.pid
);
868 /* This means that the tracer is gone... */
869 ua_sess
= (void*) -1UL;
873 ua_sess
->handle
= ret
;
875 /* Add ust app session to app's HT */
876 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
877 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
879 DBG2("UST app session created successfully with handle %d", ret
);
886 delete_ust_app_session(-1, ua_sess
);
891 * Create a context for the channel on the tracer.
894 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
895 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
899 struct lttng_ht_iter iter
;
900 struct lttng_ht_node_ulong
*node
;
901 struct ust_app_ctx
*ua_ctx
;
903 DBG2("UST app adding context to channel %s", ua_chan
->name
);
905 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
906 node
= lttng_ht_iter_get_node_ulong(&iter
);
912 ua_ctx
= alloc_ust_app_ctx(uctx
);
913 if (ua_ctx
== NULL
) {
919 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
920 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
922 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
932 * Create an UST context and enable it for the event on the tracer.
935 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
936 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
940 struct lttng_ht_iter iter
;
941 struct lttng_ht_node_ulong
*node
;
942 struct ust_app_ctx
*ua_ctx
;
944 DBG2("UST app adding context to event %s", ua_event
->name
);
946 lttng_ht_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
947 node
= lttng_ht_iter_get_node_ulong(&iter
);
953 ua_ctx
= alloc_ust_app_ctx(uctx
);
954 if (ua_ctx
== NULL
) {
960 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
961 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
963 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
973 * Enable on the tracer side a ust app event for the session and channel.
976 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
977 struct ust_app_event
*ua_event
, struct ust_app
*app
)
981 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
986 ua_event
->enabled
= 1;
993 * Disable on the tracer side a ust app event for the session and channel.
995 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
996 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1000 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1005 ua_event
->enabled
= 0;
1012 * Lookup ust app channel for session and disable it on the tracer side.
1015 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1016 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1020 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1025 ua_chan
->enabled
= 0;
1032 * Lookup ust app channel for session and enable it on the tracer side.
1034 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1035 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1038 struct lttng_ht_iter iter
;
1039 struct lttng_ht_node_str
*ua_chan_node
;
1040 struct ust_app_channel
*ua_chan
;
1042 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1043 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1044 if (ua_chan_node
== NULL
) {
1045 DBG2("Unable to find channel %s in ust session id %u",
1046 uchan
->name
, ua_sess
->id
);
1050 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1052 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1062 * Create UST app channel and create it on the tracer.
1064 static struct ust_app_channel
*create_ust_app_channel(
1065 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1066 struct ust_app
*app
)
1069 struct lttng_ht_iter iter
;
1070 struct lttng_ht_node_str
*ua_chan_node
;
1071 struct ust_app_channel
*ua_chan
;
1073 /* Lookup channel in the ust app session */
1074 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1075 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1076 if (ua_chan_node
!= NULL
) {
1077 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1081 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1082 if (ua_chan
== NULL
) {
1083 /* Only malloc can fail here */
1086 shadow_copy_channel(ua_chan
, uchan
);
1088 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1090 /* Not found previously means that it does not exist on the tracer */
1091 assert(ret
!= -EEXIST
);
1095 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1097 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1104 delete_ust_app_channel(-1, ua_chan
);
1109 * Create UST app event and create it on the tracer side.
1112 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1113 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1114 struct ust_app
*app
)
1117 struct lttng_ht_iter iter
;
1118 struct lttng_ht_node_str
*ua_event_node
;
1119 struct ust_app_event
*ua_event
;
1121 /* Get event node */
1122 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
1123 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
1124 if (ua_event_node
!= NULL
) {
1129 /* Does not exist so create one */
1130 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1131 if (ua_event
== NULL
) {
1132 /* Only malloc can failed so something is really wrong */
1136 shadow_copy_event(ua_event
, uevent
);
1138 /* Create it on the tracer side */
1139 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1141 /* Not found previously means that it does not exist on the tracer */
1142 assert(ret
!= -EEXIST
);
1146 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
1148 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1155 /* Valid. Calling here is already in a read side lock */
1156 delete_ust_app_event(-1, ua_event
);
1161 * Create UST metadata and open it on the tracer side.
1163 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1164 char *pathname
, struct ust_app
*app
)
1168 if (ua_sess
->metadata
== NULL
) {
1169 /* Allocate UST metadata */
1170 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1171 if (ua_sess
->metadata
== NULL
) {
1172 /* malloc() failed */
1176 ret
= open_ust_metadata(app
, ua_sess
);
1178 DBG3("Opening metadata failed. Cleaning up memory");
1180 /* Cleanup failed metadata struct */
1181 free(ua_sess
->metadata
);
1183 * This is very important because delete_ust_app_session check if
1184 * the pointer is null or not in order to delete the metadata.
1186 ua_sess
->metadata
= NULL
;
1190 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
1193 /* Open UST metadata stream */
1194 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1195 ret
= create_ust_stream(app
, ua_sess
);
1200 ret
= run_as_mkdir(ua_sess
->path
, S_IRWXU
| S_IRWXG
,
1201 ua_sess
->uid
, ua_sess
->gid
);
1203 PERROR("mkdir UST metadata");
1207 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1208 "%s/metadata", ua_sess
->path
);
1210 PERROR("asprintf UST create stream");
1214 DBG2("UST metadata stream object created for app pid %d",
1217 ERR("Attempting to create stream without metadata opened");
1228 * Return pointer to traceable apps list.
1230 struct lttng_ht
*ust_app_get_ht(void)
1236 * Return ust app pointer or NULL if not found.
1238 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1240 struct lttng_ht_node_ulong
*node
;
1241 struct lttng_ht_iter iter
;
1244 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1245 node
= lttng_ht_iter_get_node_ulong(&iter
);
1247 DBG2("UST app no found with pid %d", pid
);
1252 DBG2("Found UST app by pid %d", pid
);
1254 return caa_container_of(node
, struct ust_app
, node
);
1262 * Using pid and uid (of the app), allocate a new ust_app struct and
1263 * add it to the global traceable app list.
1265 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1266 * bitness is not supported.
1268 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1270 struct ust_app
*lta
;
1272 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1273 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1274 ERR("Registration failed: application \"%s\" (pid: %d) has "
1275 "%d-bit long, but no consumerd for this long size is available.\n",
1276 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1280 if (msg
->major
!= LTTNG_UST_COMM_MAJOR
) {
1281 ERR("Registration failed: application \"%s\" (pid: %d) has "
1282 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1283 msg
->name
, msg
->pid
, msg
->major
, msg
->minor
);
1287 lta
= zmalloc(sizeof(struct ust_app
));
1293 lta
->ppid
= msg
->ppid
;
1294 lta
->uid
= msg
->uid
;
1295 lta
->gid
= msg
->gid
;
1296 lta
->compatible
= 0; /* Not compatible until proven */
1297 lta
->bits_per_long
= msg
->bits_per_long
;
1298 lta
->v_major
= msg
->major
;
1299 lta
->v_minor
= msg
->minor
;
1300 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1301 lta
->name
[16] = '\0';
1302 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1305 lta
->key
.pid
= msg
->pid
;
1306 lttng_ht_node_init_ulong(<a
->node
, (unsigned long)lta
->key
.pid
);
1307 lta
->key
.sock
= sock
;
1308 lttng_ht_node_init_ulong(<a
->key
.node
, (unsigned long)lta
->key
.sock
);
1311 lttng_ht_add_unique_ulong(ust_app_sock_key_map
, <a
->key
.node
);
1312 lttng_ht_add_unique_ulong(ust_app_ht
, <a
->node
);
1315 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1316 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1317 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1323 * Unregister app by removing it from the global traceable app list and freeing
1326 * The socket is already closed at this point so no close to sock.
1328 void ust_app_unregister(int sock
)
1330 struct ust_app
*lta
;
1331 struct lttng_ht_node_ulong
*node
;
1332 struct lttng_ht_iter iter
;
1336 lta
= find_app_by_sock(sock
);
1338 ERR("Unregister app sock %d not found!", sock
);
1342 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1344 /* Remove application from socket hash table */
1345 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
), &iter
);
1346 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1349 /* Get the node reference for a call_rcu */
1350 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) lta
->key
.pid
), &iter
);
1351 node
= lttng_ht_iter_get_node_ulong(&iter
);
1353 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1357 /* Remove application from PID hash table */
1358 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1360 call_rcu(&node
->head
, delete_ust_app_rcu
);
1367 * Return traceable_app_count
1369 unsigned long ust_app_list_count(void)
1371 unsigned long count
;
1374 count
= lttng_ht_get_count(ust_app_ht
);
1381 * Fill events array with all events name of all registered apps.
1383 int ust_app_list_events(struct lttng_event
**events
)
1386 size_t nbmem
, count
= 0;
1387 struct lttng_ht_iter iter
;
1388 struct ust_app
*app
;
1389 struct lttng_event
*tmp
;
1391 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1392 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1394 PERROR("zmalloc ust app events");
1401 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1402 struct lttng_ust_tracepoint_iter uiter
;
1404 if (!app
->compatible
) {
1406 * TODO: In time, we should notice the caller of this error by
1407 * telling him that this is a version error.
1411 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1413 ERR("UST app list events getting handle failed for app pid %d",
1418 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1419 &uiter
)) != -ENOENT
) {
1420 if (count
>= nbmem
) {
1421 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1424 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1426 PERROR("realloc ust app events");
1431 memcpy(tmp
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1432 tmp
[count
].loglevel
= uiter
.loglevel
;
1433 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1434 tmp
[count
].pid
= app
->key
.pid
;
1435 tmp
[count
].enabled
= -1;
1443 DBG2("UST app list events done (%zu events)", count
);
1452 * Free and clean all traceable apps of the global list.
1454 void ust_app_clean_list(void)
1457 struct lttng_ht_iter iter
;
1458 struct lttng_ht_node_ulong
*node
;
1460 DBG2("UST app cleaning registered apps hash table");
1464 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, node
, node
) {
1465 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1467 call_rcu(&node
->head
, delete_ust_app_rcu
);
1469 /* Destroy is done only when the ht is empty */
1470 lttng_ht_destroy(ust_app_ht
);
1472 cds_lfht_for_each_entry(ust_app_sock_key_map
->ht
, &iter
.iter
, node
, node
) {
1473 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1476 /* Destroy is done only when the ht is empty */
1477 lttng_ht_destroy(ust_app_sock_key_map
);
1483 * Init UST app hash table.
1485 void ust_app_ht_alloc(void)
1487 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1488 ust_app_sock_key_map
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1492 * For a specific UST session, disable the channel for all registered apps.
1494 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1495 struct ltt_ust_channel
*uchan
)
1498 struct lttng_ht_iter iter
;
1499 struct lttng_ht_node_str
*ua_chan_node
;
1500 struct ust_app
*app
;
1501 struct ust_app_session
*ua_sess
;
1502 struct ust_app_channel
*ua_chan
;
1504 if (usess
== NULL
|| uchan
== NULL
) {
1505 ERR("Disabling UST global channel with NULL values");
1510 DBG2("UST app disabling channel %s from global domain for session id %d",
1511 uchan
->name
, usess
->id
);
1515 /* For every registered applications */
1516 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1517 struct lttng_ht_iter uiter
;
1518 if (!app
->compatible
) {
1520 * TODO: In time, we should notice the caller of this error by
1521 * telling him that this is a version error.
1525 ua_sess
= lookup_session_by_app(usess
, app
);
1526 if (ua_sess
== NULL
) {
1531 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1532 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1533 /* If the session if found for the app, the channel must be there */
1534 assert(ua_chan_node
);
1536 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1537 /* The channel must not be already disabled */
1538 assert(ua_chan
->enabled
== 1);
1540 /* Disable channel onto application */
1541 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1543 /* XXX: We might want to report this error at some point... */
1555 * For a specific UST session, enable the channel for all registered apps.
1557 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1558 struct ltt_ust_channel
*uchan
)
1561 struct lttng_ht_iter iter
;
1562 struct ust_app
*app
;
1563 struct ust_app_session
*ua_sess
;
1565 if (usess
== NULL
|| uchan
== NULL
) {
1566 ERR("Adding UST global channel to NULL values");
1571 DBG2("UST app enabling channel %s to global domain for session id %d",
1572 uchan
->name
, usess
->id
);
1576 /* For every registered applications */
1577 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1578 if (!app
->compatible
) {
1580 * TODO: In time, we should notice the caller of this error by
1581 * telling him that this is a version error.
1585 ua_sess
= lookup_session_by_app(usess
, app
);
1586 if (ua_sess
== NULL
) {
1590 /* Enable channel onto application */
1591 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1593 /* XXX: We might want to report this error at some point... */
1605 * Disable an event in a channel and for a specific session.
1607 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1608 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1611 struct lttng_ht_iter iter
, uiter
;
1612 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1613 struct ust_app
*app
;
1614 struct ust_app_session
*ua_sess
;
1615 struct ust_app_channel
*ua_chan
;
1616 struct ust_app_event
*ua_event
;
1618 DBG("UST app disabling event %s for all apps in channel "
1619 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
1623 /* For all registered applications */
1624 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1625 if (!app
->compatible
) {
1627 * TODO: In time, we should notice the caller of this error by
1628 * telling him that this is a version error.
1632 ua_sess
= lookup_session_by_app(usess
, app
);
1633 if (ua_sess
== NULL
) {
1638 /* Lookup channel in the ust app session */
1639 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1640 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1641 if (ua_chan_node
== NULL
) {
1642 DBG2("Channel %s not found in session id %d for app pid %d."
1643 "Skipping", uchan
->name
, usess
->id
, app
->key
.pid
);
1646 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1648 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
1649 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1650 if (ua_event_node
== NULL
) {
1651 DBG2("Event %s not found in channel %s for app pid %d."
1652 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1655 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1657 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1659 /* XXX: Report error someday... */
1670 * For a specific UST session and UST channel, the event for all
1673 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1674 struct ltt_ust_channel
*uchan
)
1677 struct lttng_ht_iter iter
, uiter
;
1678 struct lttng_ht_node_str
*ua_chan_node
;
1679 struct ust_app
*app
;
1680 struct ust_app_session
*ua_sess
;
1681 struct ust_app_channel
*ua_chan
;
1682 struct ust_app_event
*ua_event
;
1684 DBG("UST app disabling all event for all apps in channel "
1685 "%s for session id %d", uchan
->name
, usess
->id
);
1689 /* For all registered applications */
1690 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1691 if (!app
->compatible
) {
1693 * TODO: In time, we should notice the caller of this error by
1694 * telling him that this is a version error.
1698 ua_sess
= lookup_session_by_app(usess
, app
);
1699 /* If ua_sess is NULL, there is a code flow error */
1702 /* Lookup channel in the ust app session */
1703 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1704 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1705 /* If the channel is not found, there is a code flow error */
1706 assert(ua_chan_node
);
1708 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1710 /* Disable each events of channel */
1711 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
1713 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1715 /* XXX: Report error someday... */
1727 * For a specific UST session, create the channel for all registered apps.
1729 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1730 struct ltt_ust_channel
*uchan
)
1732 struct lttng_ht_iter iter
;
1733 struct ust_app
*app
;
1734 struct ust_app_session
*ua_sess
;
1735 struct ust_app_channel
*ua_chan
;
1737 /* Very wrong code flow */
1741 DBG2("UST app adding channel %s to global domain for session id %d",
1742 uchan
->name
, usess
->id
);
1746 /* For every registered applications */
1747 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1748 if (!app
->compatible
) {
1750 * TODO: In time, we should notice the caller of this error by
1751 * telling him that this is a version error.
1756 * Create session on the tracer side and add it to app session HT. Note
1757 * that if session exist, it will simply return a pointer to the ust
1760 ua_sess
= create_ust_app_session(usess
, app
);
1761 if (ua_sess
== NULL
) {
1762 /* The malloc() failed. */
1764 } else if (ua_sess
== (void *) -1UL) {
1765 /* The application's socket is not valid. Contiuing */
1769 /* Create channel onto application */
1770 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1771 if (ua_chan
== NULL
) {
1772 /* Major problem here and it's maybe the tracer or malloc() */
1786 * Enable event for a specific session and channel on the tracer.
1788 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1789 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1792 struct lttng_ht_iter iter
, uiter
;
1793 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1794 struct ust_app
*app
;
1795 struct ust_app_session
*ua_sess
;
1796 struct ust_app_channel
*ua_chan
;
1797 struct ust_app_event
*ua_event
;
1799 DBG("UST app enabling event %s for all apps for session id %d",
1800 uevent
->attr
.name
, usess
->id
);
1803 * NOTE: At this point, this function is called only if the session and
1804 * channel passed are already created for all apps. and enabled on the
1810 /* For all registered applications */
1811 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1812 if (!app
->compatible
) {
1814 * TODO: In time, we should notice the caller of this error by
1815 * telling him that this is a version error.
1819 ua_sess
= lookup_session_by_app(usess
, app
);
1820 /* If ua_sess is NULL, there is a code flow error */
1823 /* Lookup channel in the ust app session */
1824 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1825 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1826 /* If the channel is not found, there is a code flow error */
1827 assert(ua_chan_node
);
1829 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1831 lttng_ht_lookup(ua_chan
->events
, (void*)uevent
->attr
.name
, &uiter
);
1832 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1833 if (ua_event_node
== NULL
) {
1834 DBG3("UST app enable event %s not found for app PID %d."
1835 "Skipping app", uevent
->attr
.name
, app
->key
.pid
);
1838 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1840 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1852 * For a specific existing UST session and UST channel, creates the event for
1853 * all registered apps.
1855 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1856 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1859 struct lttng_ht_iter iter
, uiter
;
1860 struct lttng_ht_node_str
*ua_chan_node
;
1861 struct ust_app
*app
;
1862 struct ust_app_session
*ua_sess
;
1863 struct ust_app_channel
*ua_chan
;
1865 DBG("UST app creating event %s for all apps for session id %d",
1866 uevent
->attr
.name
, usess
->id
);
1870 /* For all registered applications */
1871 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1872 if (!app
->compatible
) {
1874 * TODO: In time, we should notice the caller of this error by
1875 * telling him that this is a version error.
1879 ua_sess
= lookup_session_by_app(usess
, app
);
1880 /* If ua_sess is NULL, there is a code flow error */
1883 /* Lookup channel in the ust app session */
1884 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1885 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1886 /* If the channel is not found, there is a code flow error */
1887 assert(ua_chan_node
);
1889 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1891 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1893 if (ret
!= -EEXIST
) {
1894 /* Possible value at this point: -ENOMEM. If so, we stop! */
1897 DBG2("UST app event %s already exist on app PID %d",
1898 uevent
->attr
.name
, app
->key
.pid
);
1909 * Start tracing for a specific UST session and app.
1911 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1914 struct lttng_ht_iter iter
;
1915 struct ust_app_session
*ua_sess
;
1916 struct ust_app_channel
*ua_chan
;
1917 struct ltt_ust_stream
*ustream
;
1920 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1924 if (!app
->compatible
) {
1928 ua_sess
= lookup_session_by_app(usess
, app
);
1929 if (ua_sess
== NULL
) {
1930 goto error_rcu_unlock
;
1933 /* Upon restart, we skip the setup, already done */
1934 if (ua_sess
->started
) {
1938 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1940 goto error_rcu_unlock
;
1943 /* For each channel */
1944 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1946 /* Create all streams */
1948 /* Create UST stream */
1949 ustream
= zmalloc(sizeof(*ustream
));
1950 if (ustream
== NULL
) {
1951 PERROR("zmalloc ust stream");
1952 goto error_rcu_unlock
;
1955 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1958 /* Got all streams */
1961 ustream
->handle
= ustream
->obj
->handle
;
1963 /* Order is important */
1964 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1965 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1966 ua_sess
->path
, ua_chan
->name
,
1967 ua_chan
->streams
.count
++);
1969 PERROR("asprintf UST create stream");
1972 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1977 switch (app
->bits_per_long
) {
1979 consumerd_fd
= ust_consumerd64_fd
;
1982 consumerd_fd
= ust_consumerd32_fd
;
1986 goto error_rcu_unlock
;
1989 /* Setup UST consumer socket and send fds to it */
1990 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1992 goto error_rcu_unlock
;
1994 ua_sess
->started
= 1;
1997 /* This start the UST tracing */
1998 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
2000 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
2001 goto error_rcu_unlock
;
2004 /* Quiescent wait after starting trace */
2005 ustctl_wait_quiescent(app
->key
.sock
);
2017 * Stop tracing for a specific UST session and app.
2019 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2022 struct lttng_ht_iter iter
;
2023 struct ust_app_session
*ua_sess
;
2024 struct ust_app_channel
*ua_chan
;
2026 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
2030 if (!app
->compatible
) {
2034 ua_sess
= lookup_session_by_app(usess
, app
);
2035 if (ua_sess
== NULL
) {
2036 /* Only malloc can failed so something is really wrong */
2037 goto error_rcu_unlock
;
2040 /* Not started, continuing. */
2041 if (ua_sess
->started
== 0) {
2045 /* This inhibits UST tracing */
2046 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
2048 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
2049 goto error_rcu_unlock
;
2052 /* Quiescent wait after stopping trace */
2053 ustctl_wait_quiescent(app
->key
.sock
);
2055 /* Flushing buffers */
2056 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2058 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_chan
->obj
);
2060 ERR("UST app PID %d channel %s flush failed with ret %d",
2061 app
->key
.pid
, ua_chan
->name
, ret
);
2062 /* Continuing flushing all buffers */
2067 /* Flush all buffers before stopping */
2068 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_sess
->metadata
->obj
);
2070 ERR("UST app PID %d metadata flush failed with ret %d", app
->key
.pid
,
2074 ua_sess
->started
= 0;
2086 * Destroy a specific UST session in apps.
2088 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2090 struct ust_app_session
*ua_sess
;
2091 struct lttng_ust_object_data obj
;
2092 struct lttng_ht_iter iter
;
2093 struct lttng_ht_node_ulong
*node
;
2096 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
2100 if (!app
->compatible
) {
2104 __lookup_session_by_app(usess
, app
, &iter
);
2105 node
= lttng_ht_iter_get_node_ulong(&iter
);
2107 /* Only malloc can failed so something is really wrong */
2108 goto error_rcu_unlock
;
2110 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2111 ret
= lttng_ht_del(app
->sessions
, &iter
);
2113 obj
.handle
= ua_sess
->handle
;
2116 obj
.memory_map_size
= 0;
2117 ustctl_release_object(app
->key
.sock
, &obj
);
2119 delete_ust_app_session(app
->key
.sock
, ua_sess
);
2121 /* Quiescent wait after stopping trace */
2122 ustctl_wait_quiescent(app
->key
.sock
);
2134 * Start tracing for the UST session.
2136 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2139 struct lttng_ht_iter iter
;
2140 struct ust_app
*app
;
2142 DBG("Starting all UST traces");
2146 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2147 ret
= ust_app_start_trace(usess
, app
);
2149 /* Continue to next apps even on error */
2160 * Start tracing for the UST session.
2162 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2165 struct lttng_ht_iter iter
;
2166 struct ust_app
*app
;
2168 DBG("Stopping all UST traces");
2172 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2173 ret
= ust_app_stop_trace(usess
, app
);
2175 /* Continue to next apps even on error */
2186 * Destroy app UST session.
2188 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2191 struct lttng_ht_iter iter
;
2192 struct ust_app
*app
;
2194 DBG("Destroy all UST traces");
2198 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2199 ret
= ust_app_destroy_trace(usess
, app
);
2201 /* Continue to next apps even on error */
2212 * Add channels/events from UST global domain to registered apps at sock.
2214 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2217 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
2218 struct ust_app
*app
;
2219 struct ust_app_session
*ua_sess
;
2220 struct ust_app_channel
*ua_chan
;
2221 struct ust_app_event
*ua_event
;
2222 struct ust_app_ctx
*ua_ctx
;
2224 if (usess
== NULL
) {
2225 ERR("No UST session on global update. Returning");
2229 DBG2("UST app global update for app sock %d for session id %d", sock
,
2234 app
= find_app_by_sock(sock
);
2236 ERR("Failed to update app sock %d", sock
);
2240 if (!app
->compatible
) {
2244 ua_sess
= create_ust_app_session(usess
, app
);
2245 if (ua_sess
== NULL
) {
2250 * We can iterate safely here over all UST app session sicne the create ust
2251 * app session above made a shadow copy of the UST global domain from the
2254 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2256 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2258 /* FIXME: Should we quit here or continue... */
2262 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
2264 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2266 /* FIXME: Should we quit here or continue... */
2272 /* For each events */
2273 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2275 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2277 /* FIXME: Should we quit here or continue... */
2281 /* Add context on events. */
2282 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter_ctx
.iter
,
2283 ua_ctx
, node
.node
) {
2284 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
2286 /* FIXME: Should we quit here or continue... */
2293 if (usess
->start_trace
) {
2294 ret
= ust_app_start_trace(usess
, app
);
2299 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2308 * Add context to a specific channel for global UST domain.
2310 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2311 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2314 struct lttng_ht_node_str
*ua_chan_node
;
2315 struct lttng_ht_iter iter
, uiter
;
2316 struct ust_app_channel
*ua_chan
= NULL
;
2317 struct ust_app_session
*ua_sess
;
2318 struct ust_app
*app
;
2322 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2323 if (!app
->compatible
) {
2325 * TODO: In time, we should notice the caller of this error by
2326 * telling him that this is a version error.
2330 ua_sess
= lookup_session_by_app(usess
, app
);
2331 if (ua_sess
== NULL
) {
2335 /* Lookup channel in the ust app session */
2336 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2337 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2338 if (ua_chan_node
== NULL
) {
2341 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2344 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2355 * Add context to a specific event in a channel for global UST domain.
2357 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2358 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2359 struct ltt_ust_context
*uctx
)
2362 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2363 struct lttng_ht_iter iter
, uiter
;
2364 struct ust_app_session
*ua_sess
;
2365 struct ust_app_event
*ua_event
;
2366 struct ust_app_channel
*ua_chan
= NULL
;
2367 struct ust_app
*app
;
2371 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2372 if (!app
->compatible
) {
2374 * TODO: In time, we should notice the caller of this error by
2375 * telling him that this is a version error.
2379 ua_sess
= lookup_session_by_app(usess
, app
);
2380 if (ua_sess
== NULL
) {
2384 /* Lookup channel in the ust app session */
2385 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2386 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2387 if (ua_chan_node
== NULL
) {
2390 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2393 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2394 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2395 if (ua_event_node
== NULL
) {
2398 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2401 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2412 * Enable event for a channel from a UST session for a specific PID.
2414 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2415 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2418 struct lttng_ht_iter iter
;
2419 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2420 struct ust_app
*app
;
2421 struct ust_app_session
*ua_sess
;
2422 struct ust_app_channel
*ua_chan
;
2423 struct ust_app_event
*ua_event
;
2425 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2429 app
= ust_app_find_by_pid(pid
);
2431 ERR("UST app enable event per PID %d not found", pid
);
2436 if (!app
->compatible
) {
2441 ua_sess
= lookup_session_by_app(usess
, app
);
2442 /* If ua_sess is NULL, there is a code flow error */
2445 /* Lookup channel in the ust app session */
2446 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2447 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2448 /* If the channel is not found, there is a code flow error */
2449 assert(ua_chan_node
);
2451 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2453 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2454 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2455 if (ua_event_node
== NULL
) {
2456 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2461 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2463 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2475 * Disable event for a channel from a UST session for a specific PID.
2477 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2478 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2481 struct lttng_ht_iter iter
;
2482 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2483 struct ust_app
*app
;
2484 struct ust_app_session
*ua_sess
;
2485 struct ust_app_channel
*ua_chan
;
2486 struct ust_app_event
*ua_event
;
2488 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2492 app
= ust_app_find_by_pid(pid
);
2494 ERR("UST app disable event per PID %d not found", pid
);
2499 if (!app
->compatible
) {
2504 ua_sess
= lookup_session_by_app(usess
, app
);
2505 /* If ua_sess is NULL, there is a code flow error */
2508 /* Lookup channel in the ust app session */
2509 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2510 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2511 if (ua_chan_node
== NULL
) {
2512 /* Channel does not exist, skip disabling */
2515 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2517 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2518 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2519 if (ua_event_node
== NULL
) {
2520 /* Event does not exist, skip disabling */
2523 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2525 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2536 * Validate version of UST apps and set the compatible bit.
2538 int ust_app_validate_version(int sock
)
2541 struct ust_app
*app
;
2545 app
= find_app_by_sock(sock
);
2548 ret
= ustctl_tracer_version(sock
, &app
->version
);
2553 /* Validate version */
2554 if (app
->version
.major
> UST_APP_MAJOR_VERSION
) {
2558 DBG2("UST app PID %d is compatible with major version %d "
2559 "(supporting <= %d)", app
->key
.pid
, app
->version
.major
,
2560 UST_APP_MAJOR_VERSION
);
2561 app
->compatible
= 1;
2566 DBG2("UST app PID %d is not compatible with major version %d "
2567 "(supporting <= %d)", app
->key
.pid
, app
->version
.major
,
2568 UST_APP_MAJOR_VERSION
);
2569 app
->compatible
= 0;
2575 * Calibrate registered applications.
2577 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
2580 struct lttng_ht_iter iter
;
2581 struct ust_app
*app
;
2585 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2586 if (!app
->compatible
) {
2588 * TODO: In time, we should notice the caller of this error by
2589 * telling him that this is a version error.
2594 ret
= ustctl_calibrate(app
->key
.sock
, calibrate
);
2598 /* Means that it's not implemented on the tracer side. */
2602 /* TODO: Report error to user */
2603 DBG2("Calibrate app PID %d returned with error %d",
2610 DBG("UST app global domain calibration finished");