2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <sys/types.h>
30 #include <urcu/compiler.h>
34 #include <lttng-share.h>
38 #include "ust-consumer.h"
42 * Delete ust context safely. RCU read lock must be held before calling
46 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
49 ustctl_release_object(sock
, ua_ctx
->obj
);
56 * Delete ust app event safely. RCU read lock must be held before calling
60 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
63 struct lttng_ht_iter iter
;
64 struct ust_app_ctx
*ua_ctx
;
66 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter
.iter
, ua_ctx
,
68 ret
= lttng_ht_del(ua_event
->ctx
, &iter
);
70 delete_ust_app_ctx(sock
, ua_ctx
);
72 lttng_ht_destroy(ua_event
->ctx
);
74 if (ua_event
->obj
!= NULL
) {
75 ustctl_release_object(sock
, ua_event
->obj
);
82 * Delete ust app stream safely. RCU read lock must be held before calling
86 void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
89 ustctl_release_object(sock
, stream
->obj
);
96 * Delete ust app channel safely. RCU read lock must be held before calling
100 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
103 struct lttng_ht_iter iter
;
104 struct ust_app_event
*ua_event
;
105 struct ust_app_ctx
*ua_ctx
;
106 struct ltt_ust_stream
*stream
, *stmp
;
109 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
110 cds_list_del(&stream
->list
);
111 delete_ust_app_stream(sock
, stream
);
115 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
116 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
118 delete_ust_app_ctx(sock
, ua_ctx
);
120 lttng_ht_destroy(ua_chan
->ctx
);
123 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
125 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
127 delete_ust_app_event(sock
, ua_event
);
129 lttng_ht_destroy(ua_chan
->events
);
131 if (ua_chan
->obj
!= NULL
) {
132 ustctl_release_object(sock
, ua_chan
->obj
);
139 * Delete ust app session safely. RCU read lock must be held before calling
143 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
)
146 struct lttng_ht_iter iter
;
147 struct ust_app_channel
*ua_chan
;
149 if (ua_sess
->metadata
) {
150 if (ua_sess
->metadata
->stream_obj
) {
151 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
152 free(ua_sess
->metadata
->stream_obj
);
154 if (ua_sess
->metadata
->obj
) {
155 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
156 free(ua_sess
->metadata
->obj
);
160 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
162 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
164 delete_ust_app_channel(sock
, ua_chan
);
166 lttng_ht_destroy(ua_sess
->channels
);
168 if (ua_sess
->handle
!= -1) {
169 ustctl_release_handle(sock
, ua_sess
->handle
);
175 * Delete a traceable application structure from the global list. Never call
176 * this function outside of a call_rcu call.
179 void delete_ust_app(struct ust_app
*app
)
182 struct lttng_ht_iter iter
;
183 struct ust_app_session
*ua_sess
;
187 /* Delete ust app sessions info */
188 sock
= app
->key
.sock
;
192 cds_lfht_for_each_entry(app
->sessions
->ht
, &iter
.iter
, ua_sess
,
194 ret
= lttng_ht_del(app
->sessions
, &iter
);
196 delete_ust_app_session(app
->key
.sock
, ua_sess
);
198 lttng_ht_destroy(app
->sessions
);
201 * Wait until we have removed the key from the sock hash table before
202 * closing this socket, otherwise an application could re-use the socket ID
203 * and race with the teardown, using the same hash table entry.
207 DBG2("UST app pid %d deleted", app
->key
.pid
);
214 * URCU intermediate call to delete an UST app.
217 void delete_ust_app_rcu(struct rcu_head
*head
)
219 struct lttng_ht_node_ulong
*node
=
220 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
221 struct ust_app
*app
=
222 caa_container_of(node
, struct ust_app
, node
);
224 DBG3("Call RCU deleting app PID %d", app
->key
.pid
);
229 * Alloc new UST app session.
232 struct ust_app_session
*alloc_ust_app_session(void)
234 struct ust_app_session
*ua_sess
;
236 /* Init most of the default value by allocating and zeroing */
237 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
238 if (ua_sess
== NULL
) {
243 ua_sess
->handle
= -1;
244 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
253 * Alloc new UST app channel.
256 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
257 struct lttng_ust_channel
*attr
)
259 struct ust_app_channel
*ua_chan
;
261 /* Init most of the default value by allocating and zeroing */
262 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
263 if (ua_chan
== NULL
) {
268 /* Setup channel name */
269 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
270 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
272 ua_chan
->enabled
= 1;
273 ua_chan
->handle
= -1;
274 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
275 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
276 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
278 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
280 /* Copy attributes */
282 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
285 DBG3("UST app channel %s allocated", ua_chan
->name
);
294 * Alloc new UST app event.
297 struct ust_app_event
*alloc_ust_app_event(char *name
,
298 struct lttng_ust_event
*attr
)
300 struct ust_app_event
*ua_event
;
302 /* Init most of the default value by allocating and zeroing */
303 ua_event
= zmalloc(sizeof(struct ust_app_event
));
304 if (ua_event
== NULL
) {
309 ua_event
->enabled
= 1;
310 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
311 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
312 ua_event
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
313 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
315 /* Copy attributes */
317 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
320 DBG3("UST app event %s allocated", ua_event
->name
);
329 * Alloc new UST app context.
332 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
334 struct ust_app_ctx
*ua_ctx
;
336 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
337 if (ua_ctx
== NULL
) {
342 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
345 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
352 * Find an ust_app using the sock and return it. RCU read side lock must be
353 * held before calling this helper function.
356 struct ust_app
*find_app_by_sock(int sock
)
358 struct lttng_ht_node_ulong
*node
;
359 struct ust_app_key
*key
;
360 struct lttng_ht_iter iter
;
362 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
),
364 node
= lttng_ht_iter_get_node_ulong(&iter
);
366 DBG2("UST app find by sock %d key not found", sock
);
369 key
= caa_container_of(node
, struct ust_app_key
, node
);
371 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) key
->pid
), &iter
);
372 node
= lttng_ht_iter_get_node_ulong(&iter
);
374 DBG2("UST app find by sock %d not found", sock
);
377 return caa_container_of(node
, struct ust_app
, node
);
384 * Create the channel context on the tracer.
387 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
388 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
392 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
393 ua_chan
->obj
, &ua_ctx
->obj
);
398 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
400 DBG2("UST app context added to channel %s successfully", ua_chan
->name
);
407 * Create the event context on the tracer.
410 int create_ust_event_context(struct ust_app_event
*ua_event
,
411 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
415 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
416 ua_event
->obj
, &ua_ctx
->obj
);
421 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
423 DBG2("UST app context added to event %s successfully", ua_event
->name
);
430 * Disable the specified event on to UST tracer for the UST session.
432 static int disable_ust_event(struct ust_app
*app
,
433 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
437 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
439 ERR("UST app event %s disable failed for app (pid: %d) "
440 "and session handle %d with ret %d",
441 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
445 DBG2("UST app event %s disabled successfully for app (pid: %d)",
446 ua_event
->attr
.name
, app
->key
.pid
);
453 * Disable the specified channel on to UST tracer for the UST session.
455 static int disable_ust_channel(struct ust_app
*app
,
456 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
460 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
462 ERR("UST app channel %s disable failed for app (pid: %d) "
463 "and session handle %d with ret %d",
464 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
468 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
469 ua_chan
->name
, app
->key
.pid
);
476 * Enable the specified channel on to UST tracer for the UST session.
478 static int enable_ust_channel(struct ust_app
*app
,
479 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
483 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
485 ERR("UST app channel %s enable failed for app (pid: %d) "
486 "and session handle %d with ret %d",
487 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
491 ua_chan
->enabled
= 1;
493 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
494 ua_chan
->name
, app
->key
.pid
);
501 * Enable the specified event on to UST tracer for the UST session.
503 static int enable_ust_event(struct ust_app
*app
,
504 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
508 ret
= ustctl_enable(app
->key
.sock
, ua_event
->obj
);
510 ERR("UST app event %s enable failed for app (pid: %d) "
511 "and session handle %d with ret %d",
512 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
516 DBG2("UST app event %s enabled successfully for app (pid: %d)",
517 ua_event
->attr
.name
, app
->key
.pid
);
524 * Open metadata onto the UST tracer for a UST session.
526 static int open_ust_metadata(struct ust_app
*app
,
527 struct ust_app_session
*ua_sess
)
530 struct lttng_ust_channel_attr uattr
;
532 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
533 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
534 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
535 uattr
.switch_timer_interval
=
536 ua_sess
->metadata
->attr
.switch_timer_interval
;
537 uattr
.read_timer_interval
=
538 ua_sess
->metadata
->attr
.read_timer_interval
;
539 uattr
.output
= ua_sess
->metadata
->attr
.output
;
541 /* UST tracer metadata creation */
542 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
543 &ua_sess
->metadata
->obj
);
545 ERR("UST app open metadata failed for app pid:%d",
550 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
557 * Create stream onto the UST tracer for a UST session.
559 static int create_ust_stream(struct ust_app
*app
,
560 struct ust_app_session
*ua_sess
)
564 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
565 &ua_sess
->metadata
->stream_obj
);
567 ERR("UST create metadata stream failed");
576 * Create the specified channel onto the UST tracer for a UST session.
578 static int create_ust_channel(struct ust_app
*app
,
579 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
583 /* TODO: remove cast and use lttng-ust-abi.h */
584 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
585 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
587 ERR("Creating channel %s for app (pid: %d, sock: %d) "
588 "and session handle %d with ret %d",
589 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
590 ua_sess
->handle
, ret
);
594 ua_chan
->handle
= ua_chan
->obj
->handle
;
596 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
597 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
599 /* If channel is not enabled, disable it on the tracer */
600 if (!ua_chan
->enabled
) {
601 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
612 * Create the specified event onto the UST tracer for a UST session.
615 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
616 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
620 /* Create UST event on tracer */
621 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
624 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
) {
636 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
647 * Copy data between an UST app event and a LTT event.
649 static void shadow_copy_event(struct ust_app_event
*ua_event
,
650 struct ltt_ust_event
*uevent
)
652 struct lttng_ht_iter iter
;
653 struct ltt_ust_context
*uctx
;
654 struct ust_app_ctx
*ua_ctx
;
656 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
657 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
659 /* Copy event attributes */
660 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
662 cds_lfht_for_each_entry(uevent
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
663 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
664 if (ua_ctx
== NULL
) {
667 lttng_ht_node_init_ulong(&ua_ctx
->node
,
668 (unsigned long) ua_ctx
->ctx
.ctx
);
669 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
674 * Copy data between an UST app channel and a LTT channel.
676 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
677 struct ltt_ust_channel
*uchan
)
679 struct lttng_ht_iter iter
;
680 struct lttng_ht_node_str
*ua_event_node
;
681 struct ltt_ust_event
*uevent
;
682 struct ltt_ust_context
*uctx
;
683 struct ust_app_event
*ua_event
;
684 struct ust_app_ctx
*ua_ctx
;
686 DBG2("Shadow copy of UST app channel %s", ua_chan
->name
);
688 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
689 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
690 /* Copy event attributes */
691 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
693 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
694 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
695 if (ua_ctx
== NULL
) {
698 lttng_ht_node_init_ulong(&ua_ctx
->node
,
699 (unsigned long) ua_ctx
->ctx
.ctx
);
700 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
703 /* Copy all events from ltt ust channel to ust app channel */
704 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
705 struct lttng_ht_iter uiter
;
707 lttng_ht_lookup(ua_chan
->events
, (void *) uevent
->attr
.name
, &uiter
);
708 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
709 if (ua_event_node
== NULL
) {
710 DBG2("UST event %s not found on shadow copy channel",
712 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
713 if (ua_event
== NULL
) {
716 shadow_copy_event(ua_event
, uevent
);
717 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
721 DBG3("Shadow copy channel done");
725 * Copy data between a UST app session and a regular LTT session.
727 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
728 struct ltt_ust_session
*usess
, struct ust_app
*app
)
730 struct lttng_ht_node_str
*ua_chan_node
;
731 struct lttng_ht_iter iter
;
732 struct ltt_ust_channel
*uchan
;
733 struct ust_app_channel
*ua_chan
;
739 /* Get date and time for unique app path */
741 timeinfo
= localtime(&rawtime
);
742 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
744 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
746 ua_sess
->id
= usess
->id
;
747 ua_sess
->uid
= usess
->uid
;
748 ua_sess
->gid
= usess
->gid
;
750 ret
= snprintf(ua_sess
->path
, PATH_MAX
,
752 usess
->pathname
, app
->name
, app
->key
.pid
,
755 PERROR("asprintf UST shadow copy session");
756 /* TODO: We cannot return an error from here.. */
760 /* TODO: support all UST domain */
762 /* Iterate over all channels in global domain. */
763 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
765 struct lttng_ht_iter uiter
;
767 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
768 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
769 if (ua_chan_node
!= NULL
) {
773 DBG2("Channel %s not found on shadow session copy, creating it",
775 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
776 if (ua_chan
== NULL
) {
777 /* malloc failed... continuing */
781 shadow_copy_channel(ua_chan
, uchan
);
782 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
787 * Lookup sesison wrapper.
790 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
791 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
793 /* Get right UST app session from app */
794 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
798 * Return ust app session from the app session hashtable using the UST session
801 static struct ust_app_session
*lookup_session_by_app(
802 struct ltt_ust_session
*usess
, struct ust_app
*app
)
804 struct lttng_ht_iter iter
;
805 struct lttng_ht_node_ulong
*node
;
807 __lookup_session_by_app(usess
, app
, &iter
);
808 node
= lttng_ht_iter_get_node_ulong(&iter
);
813 return caa_container_of(node
, struct ust_app_session
, node
);
820 * Create a UST session onto the tracer of app and add it the session
823 * Return ust app session or NULL on error.
825 static struct ust_app_session
*create_ust_app_session(
826 struct ltt_ust_session
*usess
, struct ust_app
*app
)
829 struct ust_app_session
*ua_sess
;
831 ua_sess
= lookup_session_by_app(usess
, app
);
832 if (ua_sess
== NULL
) {
833 DBG2("UST app pid: %d session id %d not found, creating it",
834 app
->key
.pid
, usess
->id
);
835 ua_sess
= alloc_ust_app_session();
836 if (ua_sess
== NULL
) {
837 /* Only malloc can failed so something is really wrong */
840 shadow_copy_session(ua_sess
, usess
, app
);
843 if (ua_sess
->handle
== -1) {
844 ret
= ustctl_create_session(app
->key
.sock
);
846 ERR("Error creating session for app pid %d, sock %d",
847 app
->key
.pid
, app
->key
.sock
);
848 /* TODO: free() ua_sess */
852 DBG2("UST app ustctl create session handle %d", ret
);
853 ua_sess
->handle
= ret
;
855 /* Add ust app session to app's HT */
856 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
857 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
859 DBG2("UST app session created successfully with handle %d", ret
);
869 * Create a context for the channel on the tracer.
872 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
873 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
877 struct lttng_ht_iter iter
;
878 struct lttng_ht_node_ulong
*node
;
879 struct ust_app_ctx
*ua_ctx
;
881 DBG2("UST app adding context to channel %s", ua_chan
->name
);
883 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
884 node
= lttng_ht_iter_get_node_ulong(&iter
);
890 ua_ctx
= alloc_ust_app_ctx(uctx
);
891 if (ua_ctx
== NULL
) {
897 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
898 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
900 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
910 * Create an UST context and enable it for the event on the tracer.
913 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
914 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
918 struct lttng_ht_iter iter
;
919 struct lttng_ht_node_ulong
*node
;
920 struct ust_app_ctx
*ua_ctx
;
922 DBG2("UST app adding context to event %s", ua_event
->name
);
924 lttng_ht_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
925 node
= lttng_ht_iter_get_node_ulong(&iter
);
931 ua_ctx
= alloc_ust_app_ctx(uctx
);
932 if (ua_ctx
== NULL
) {
938 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
939 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
941 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
951 * Enable on the tracer side a ust app event for the session and channel.
954 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
955 struct ust_app_event
*ua_event
, struct ust_app
*app
)
959 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
964 ua_event
->enabled
= 1;
971 * Disable on the tracer side a ust app event for the session and channel.
973 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
974 struct ust_app_event
*ua_event
, struct ust_app
*app
)
978 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
983 ua_event
->enabled
= 0;
990 * Lookup ust app channel for session and disable it on the tracer side.
993 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
994 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
998 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1003 ua_chan
->enabled
= 0;
1010 * Lookup ust app channel for session and enable it on the tracer side.
1012 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1013 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1016 struct lttng_ht_iter iter
;
1017 struct lttng_ht_node_str
*ua_chan_node
;
1018 struct ust_app_channel
*ua_chan
;
1020 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1021 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1022 if (ua_chan_node
== NULL
) {
1023 DBG2("Unable to find channel %s in ust session id %u",
1024 uchan
->name
, ua_sess
->id
);
1028 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1030 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1040 * Create UST app channel and create it on the tracer.
1042 static struct ust_app_channel
*create_ust_app_channel(
1043 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1044 struct ust_app
*app
)
1047 struct lttng_ht_iter iter
;
1048 struct lttng_ht_node_str
*ua_chan_node
;
1049 struct ust_app_channel
*ua_chan
;
1051 /* Lookup channel in the ust app session */
1052 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1053 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1054 if (ua_chan_node
== NULL
) {
1055 DBG2("Unable to find channel %s in ust session id %u",
1056 uchan
->name
, ua_sess
->id
);
1057 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1058 if (ua_chan
== NULL
) {
1061 shadow_copy_channel(ua_chan
, uchan
);
1063 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1066 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1071 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1080 * Create UST app event and create it on the tracer side.
1083 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1084 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1085 struct ust_app
*app
)
1088 struct lttng_ht_iter iter
;
1089 struct lttng_ht_node_str
*ua_event_node
;
1090 struct ust_app_event
*ua_event
;
1092 /* Get event node */
1093 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
1094 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
1095 if (ua_event_node
!= NULL
) {
1096 ERR("UST app event %s already exist. Stopping creation.",
1101 /* Does not exist so create one */
1102 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1103 if (ua_event
== NULL
) {
1104 /* Only malloc can failed so something is really wrong */
1108 shadow_copy_event(ua_event
, uevent
);
1110 /* Create it on the tracer side */
1111 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1114 delete_ust_app_event(app
->key
.sock
, ua_event
);
1119 ua_event
->enabled
= 1;
1121 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
1123 DBG2("UST app create event %s for PID %d completed",
1124 ua_event
->name
, app
->key
.pid
);
1132 * Create UST metadata and open it on the tracer side.
1134 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1135 char *pathname
, struct ust_app
*app
)
1139 if (ua_sess
->metadata
== NULL
) {
1140 /* Allocate UST metadata */
1141 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1142 if (ua_sess
->metadata
== NULL
) {
1143 ERR("UST app session %d creating metadata failed",
1148 ret
= open_ust_metadata(app
, ua_sess
);
1153 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
1156 /* Open UST metadata stream */
1157 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1158 ret
= create_ust_stream(app
, ua_sess
);
1163 ret
= mkdir_run_as(ua_sess
->path
, S_IRWXU
| S_IRWXG
,
1164 ua_sess
->uid
, ua_sess
->gid
);
1166 PERROR("mkdir UST metadata");
1170 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1171 "%s/metadata", ua_sess
->path
);
1173 PERROR("asprintf UST create stream");
1177 DBG2("UST metadata stream object created for app pid %d",
1180 ERR("Attempting to create stream without metadata opened");
1191 * Return pointer to traceable apps list.
1193 struct lttng_ht
*ust_app_get_ht(void)
1199 * Return ust app pointer or NULL if not found.
1201 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1203 struct lttng_ht_node_ulong
*node
;
1204 struct lttng_ht_iter iter
;
1207 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1208 node
= lttng_ht_iter_get_node_ulong(&iter
);
1210 DBG2("UST app no found with pid %d", pid
);
1215 DBG2("Found UST app by pid %d", pid
);
1217 return caa_container_of(node
, struct ust_app
, node
);
1225 * Using pid and uid (of the app), allocate a new ust_app struct and
1226 * add it to the global traceable app list.
1228 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1229 * bitness is not supported.
1231 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1233 struct ust_app
*lta
;
1235 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1236 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1237 ERR("Registration failed: application \"%s\" (pid: %d) has "
1238 "%d-bit long, but no consumerd for this long size is available.\n",
1239 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1243 lta
= zmalloc(sizeof(struct ust_app
));
1249 lta
->ppid
= msg
->ppid
;
1250 lta
->uid
= msg
->uid
;
1251 lta
->gid
= msg
->gid
;
1252 lta
->bits_per_long
= msg
->bits_per_long
;
1253 lta
->v_major
= msg
->major
;
1254 lta
->v_minor
= msg
->minor
;
1255 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1256 lta
->name
[16] = '\0';
1257 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1260 lta
->key
.pid
= msg
->pid
;
1261 lttng_ht_node_init_ulong(<a
->node
, (unsigned long)lta
->key
.pid
);
1262 lta
->key
.sock
= sock
;
1263 lttng_ht_node_init_ulong(<a
->key
.node
, (unsigned long)lta
->key
.sock
);
1266 lttng_ht_add_unique_ulong(ust_app_sock_key_map
, <a
->key
.node
);
1267 lttng_ht_add_unique_ulong(ust_app_ht
, <a
->node
);
1270 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1271 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1272 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1278 * Unregister app by removing it from the global traceable app list and freeing
1281 * The socket is already closed at this point so no close to sock.
1283 void ust_app_unregister(int sock
)
1285 struct ust_app
*lta
;
1286 struct lttng_ht_node_ulong
*node
;
1287 struct lttng_ht_iter iter
;
1291 lta
= find_app_by_sock(sock
);
1293 ERR("Unregister app sock %d not found!", sock
);
1297 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1299 /* Remove application from socket hash table */
1300 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
), &iter
);
1301 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1304 /* Get the node reference for a call_rcu */
1305 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) lta
->key
.pid
), &iter
);
1306 node
= lttng_ht_iter_get_node_ulong(&iter
);
1308 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1312 /* Remove application from PID hash table */
1313 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1315 call_rcu(&node
->head
, delete_ust_app_rcu
);
1322 * Return traceable_app_count
1324 unsigned long ust_app_list_count(void)
1326 unsigned long count
;
1329 count
= lttng_ht_get_count(ust_app_ht
);
1336 * Fill events array with all events name of all registered apps.
1338 int ust_app_list_events(struct lttng_event
**events
)
1341 size_t nbmem
, count
= 0;
1342 struct lttng_ht_iter iter
;
1343 struct ust_app
*app
;
1344 struct lttng_event
*tmp
;
1346 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1347 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1349 PERROR("zmalloc ust app events");
1356 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1357 struct lttng_ust_tracepoint_iter uiter
;
1359 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1361 ERR("UST app list events getting handle failed for app pid %d",
1366 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1367 &uiter
)) != -ENOENT
) {
1368 if (count
>= nbmem
) {
1369 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1372 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1374 PERROR("realloc ust app events");
1379 memcpy(tmp
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1380 memcpy(tmp
[count
].loglevel
, uiter
.loglevel
, LTTNG_UST_SYM_NAME_LEN
);
1381 tmp
[count
].loglevel_value
= uiter
.loglevel_value
;
1382 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1383 tmp
[count
].pid
= app
->key
.pid
;
1384 tmp
[count
].enabled
= -1;
1392 DBG2("UST app list events done (%zu events)", count
);
1401 * Free and clean all traceable apps of the global list.
1403 void ust_app_clean_list(void)
1406 struct lttng_ht_iter iter
;
1407 struct lttng_ht_node_ulong
*node
;
1409 DBG2("UST app cleaning registered apps hash table");
1413 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, node
, node
) {
1414 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1416 call_rcu(&node
->head
, delete_ust_app_rcu
);
1418 /* Destroy is done only when the ht is empty */
1419 lttng_ht_destroy(ust_app_ht
);
1421 cds_lfht_for_each_entry(ust_app_sock_key_map
->ht
, &iter
.iter
, node
, node
) {
1422 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1425 /* Destroy is done only when the ht is empty */
1426 lttng_ht_destroy(ust_app_sock_key_map
);
1432 * Init UST app hash table.
1434 void ust_app_ht_alloc(void)
1436 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1437 ust_app_sock_key_map
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1441 * For a specific UST session, disable the channel for all registered apps.
1443 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1444 struct ltt_ust_channel
*uchan
)
1447 struct lttng_ht_iter iter
;
1448 struct lttng_ht_node_str
*ua_chan_node
;
1449 struct ust_app
*app
;
1450 struct ust_app_session
*ua_sess
;
1451 struct ust_app_channel
*ua_chan
;
1453 if (usess
== NULL
|| uchan
== NULL
) {
1454 ERR("Disabling UST global channel with NULL values");
1459 DBG2("UST app disabling channel %s from global domain for session id %d",
1460 uchan
->name
, usess
->id
);
1464 /* For every registered applications */
1465 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1466 struct lttng_ht_iter uiter
;
1467 ua_sess
= lookup_session_by_app(usess
, app
);
1468 if (ua_sess
== NULL
) {
1473 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1474 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1475 /* If the session if found for the app, the channel must be there */
1476 assert(ua_chan_node
);
1478 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1479 /* The channel must not be already disabled */
1480 assert(ua_chan
->enabled
== 1);
1482 /* Disable channel onto application */
1483 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1485 /* XXX: We might want to report this error at some point... */
1497 * For a specific UST session, enable the channel for all registered apps.
1499 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1500 struct ltt_ust_channel
*uchan
)
1503 struct lttng_ht_iter iter
;
1504 struct ust_app
*app
;
1505 struct ust_app_session
*ua_sess
;
1507 if (usess
== NULL
|| uchan
== NULL
) {
1508 ERR("Adding UST global channel to NULL values");
1513 DBG2("UST app enabling channel %s to global domain for session id %d",
1514 uchan
->name
, usess
->id
);
1518 /* For every registered applications */
1519 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1520 ua_sess
= lookup_session_by_app(usess
, app
);
1521 if (ua_sess
== NULL
) {
1525 /* Enable channel onto application */
1526 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1528 /* XXX: We might want to report this error at some point... */
1540 * Disable an event in a channel and for a specific session.
1542 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1543 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1546 struct lttng_ht_iter iter
, uiter
;
1547 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1548 struct ust_app
*app
;
1549 struct ust_app_session
*ua_sess
;
1550 struct ust_app_channel
*ua_chan
;
1551 struct ust_app_event
*ua_event
;
1553 DBG("UST app disabling event %s for all apps in channel "
1554 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
1558 /* For all registered applications */
1559 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1560 ua_sess
= lookup_session_by_app(usess
, app
);
1561 if (ua_sess
== NULL
) {
1566 /* Lookup channel in the ust app session */
1567 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1568 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1569 if (ua_chan_node
== NULL
) {
1570 DBG2("Channel %s not found in session id %d for app pid %d."
1571 "Skipping", uchan
->name
, usess
->id
, app
->key
.pid
);
1574 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1576 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
1577 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1578 if (ua_event_node
== NULL
) {
1579 DBG2("Event %s not found in channel %s for app pid %d."
1580 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1583 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1585 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1587 /* XXX: Report error someday... */
1598 * For a specific UST session and UST channel, the event for all
1601 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1602 struct ltt_ust_channel
*uchan
)
1605 struct lttng_ht_iter iter
, uiter
;
1606 struct lttng_ht_node_str
*ua_chan_node
;
1607 struct ust_app
*app
;
1608 struct ust_app_session
*ua_sess
;
1609 struct ust_app_channel
*ua_chan
;
1610 struct ust_app_event
*ua_event
;
1612 DBG("UST app disabling all event for all apps in channel "
1613 "%s for session id %d", uchan
->name
, usess
->id
);
1617 /* For all registered applications */
1618 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1619 ua_sess
= lookup_session_by_app(usess
, app
);
1620 /* If ua_sess is NULL, there is a code flow error */
1623 /* Lookup channel in the ust app session */
1624 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1625 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1626 /* If the channel is not found, there is a code flow error */
1627 assert(ua_chan_node
);
1629 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1631 /* Disable each events of channel */
1632 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
1634 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1636 /* XXX: Report error someday... */
1648 * For a specific UST session, create the channel for all registered apps.
1650 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1651 struct ltt_ust_channel
*uchan
)
1654 struct lttng_ht_iter iter
;
1655 struct ust_app
*app
;
1656 struct ust_app_session
*ua_sess
;
1657 struct ust_app_channel
*ua_chan
;
1659 if (usess
== NULL
|| uchan
== NULL
) {
1660 ERR("Adding UST global channel to NULL values");
1665 DBG2("UST app adding channel %s to global domain for session id %d",
1666 uchan
->name
, usess
->id
);
1670 /* For every registered applications */
1671 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1673 * Create session on the tracer side and add it to app session HT. Note
1674 * that if session exist, it will simply return a pointer to the ust
1677 ua_sess
= create_ust_app_session(usess
, app
);
1678 if (ua_sess
== NULL
) {
1682 /* Create channel onto application */
1683 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1684 if (ua_chan
== NULL
) {
1696 * Enable event for a specific session and channel on the tracer.
1698 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1699 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1702 struct lttng_ht_iter iter
, uiter
;
1703 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1704 struct ust_app
*app
;
1705 struct ust_app_session
*ua_sess
;
1706 struct ust_app_channel
*ua_chan
;
1707 struct ust_app_event
*ua_event
;
1709 DBG("UST app enabling event %s for all apps for session id %d",
1710 uevent
->attr
.name
, usess
->id
);
1713 * NOTE: At this point, this function is called only if the session and
1714 * channel passed are already created for all apps. and enabled on the
1720 /* For all registered applications */
1721 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1722 ua_sess
= lookup_session_by_app(usess
, app
);
1723 /* If ua_sess is NULL, there is a code flow error */
1726 /* Lookup channel in the ust app session */
1727 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1728 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1729 /* If the channel is not found, there is a code flow error */
1730 assert(ua_chan_node
);
1732 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1734 lttng_ht_lookup(ua_chan
->events
, (void*)uevent
->attr
.name
, &uiter
);
1735 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1736 if (ua_event_node
== NULL
) {
1737 DBG3("UST app enable event %s not found for app PID %d."
1738 "Skipping app", uevent
->attr
.name
, app
->key
.pid
);
1741 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1743 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1755 * For a specific existing UST session and UST channel, creates the event for
1756 * all registered apps.
1758 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1759 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1762 struct lttng_ht_iter iter
, uiter
;
1763 struct lttng_ht_node_str
*ua_chan_node
;
1764 struct ust_app
*app
;
1765 struct ust_app_session
*ua_sess
;
1766 struct ust_app_channel
*ua_chan
;
1768 DBG("UST app creating event %s for all apps for session id %d",
1769 uevent
->attr
.name
, usess
->id
);
1772 * NOTE: At this point, this function is called only if the session and
1773 * channel passed are already created for all apps. and enabled on the
1779 /* For all registered applications */
1780 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1781 ua_sess
= lookup_session_by_app(usess
, app
);
1782 /* If ua_sess is NULL, there is a code flow error */
1785 /* Lookup channel in the ust app session */
1786 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1787 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1788 /* If the channel is not found, there is a code flow error */
1789 assert(ua_chan_node
);
1791 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1793 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1805 * Start tracing for a specific UST session and app.
1807 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1810 struct lttng_ht_iter iter
;
1811 struct ust_app_session
*ua_sess
;
1812 struct ust_app_channel
*ua_chan
;
1813 struct ltt_ust_stream
*ustream
;
1816 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1820 ua_sess
= lookup_session_by_app(usess
, app
);
1821 if (ua_sess
== NULL
) {
1822 goto error_rcu_unlock
;
1825 /* Upon restart, we skip the setup, already done */
1826 if (ua_sess
->started
) {
1830 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1832 goto error_rcu_unlock
;
1835 /* For each channel */
1836 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1838 /* Create all streams */
1840 /* Create UST stream */
1841 ustream
= zmalloc(sizeof(*ustream
));
1842 if (ustream
== NULL
) {
1843 PERROR("zmalloc ust stream");
1844 goto error_rcu_unlock
;
1847 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1850 /* Got all streams */
1853 ustream
->handle
= ustream
->obj
->handle
;
1855 /* Order is important */
1856 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1857 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1858 ua_sess
->path
, ua_chan
->name
,
1859 ua_chan
->streams
.count
++);
1861 PERROR("asprintf UST create stream");
1864 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1869 switch (app
->bits_per_long
) {
1871 consumerd_fd
= ust_consumerd64_fd
;
1874 consumerd_fd
= ust_consumerd32_fd
;
1878 goto error_rcu_unlock
;
1881 /* Setup UST consumer socket and send fds to it */
1882 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1884 goto error_rcu_unlock
;
1886 ua_sess
->started
= 1;
1889 /* This start the UST tracing */
1890 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1892 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1893 goto error_rcu_unlock
;
1898 /* Quiescent wait after starting trace */
1899 ustctl_wait_quiescent(app
->key
.sock
);
1909 * Stop tracing for a specific UST session and app.
1911 int ust_app_stop_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
;
1918 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1922 ua_sess
= lookup_session_by_app(usess
, app
);
1923 if (ua_sess
== NULL
) {
1924 /* Only malloc can failed so something is really wrong */
1925 goto error_rcu_unlock
;
1928 /* This inhibits UST tracing */
1929 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1931 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1932 goto error_rcu_unlock
;
1935 /* Quiescent wait after stopping trace */
1936 ustctl_wait_quiescent(app
->key
.sock
);
1938 /* Flushing buffers */
1939 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1941 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_chan
->obj
);
1943 ERR("UST app PID %d channel %s flush failed",
1944 app
->key
.pid
, ua_chan
->name
);
1945 ERR("Ended with ret %d", ret
);
1946 /* Continuing flushing all buffers */
1951 /* Flush all buffers before stopping */
1952 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_sess
->metadata
->obj
);
1954 ERR("UST app PID %d metadata flush failed", app
->key
.pid
);
1955 ERR("Ended with ret %d", ret
);
1968 * Destroy a specific UST session in apps.
1970 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1972 struct ust_app_session
*ua_sess
;
1973 struct lttng_ust_object_data obj
;
1974 struct lttng_ht_iter iter
;
1975 struct lttng_ht_node_ulong
*node
;
1978 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
1982 __lookup_session_by_app(usess
, app
, &iter
);
1983 node
= lttng_ht_iter_get_node_ulong(&iter
);
1985 /* Only malloc can failed so something is really wrong */
1986 goto error_rcu_unlock
;
1988 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
1989 ret
= lttng_ht_del(app
->sessions
, &iter
);
1991 delete_ust_app_session(app
->key
.sock
, ua_sess
);
1992 obj
.handle
= ua_sess
->handle
;
1995 obj
.memory_map_size
= 0;
1996 ustctl_release_object(app
->key
.sock
, &obj
);
2000 /* Quiescent wait after stopping trace */
2001 ustctl_wait_quiescent(app
->key
.sock
);
2011 * Start tracing for the UST session.
2013 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2016 struct lttng_ht_iter iter
;
2017 struct ust_app
*app
;
2019 DBG("Starting all UST traces");
2023 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2024 ret
= ust_app_start_trace(usess
, app
);
2026 /* Continue to next apps even on error */
2037 * Start tracing for the UST session.
2039 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2042 struct lttng_ht_iter iter
;
2043 struct ust_app
*app
;
2045 DBG("Stopping all UST traces");
2049 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2050 ret
= ust_app_stop_trace(usess
, app
);
2052 /* Continue to next apps even on error */
2063 * Destroy app UST session.
2065 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2068 struct lttng_ht_iter iter
;
2069 struct ust_app
*app
;
2071 DBG("Destroy all UST traces");
2075 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2076 ret
= ust_app_destroy_trace(usess
, app
);
2078 /* Continue to next apps even on error */
2089 * Add channels/events from UST global domain to registered apps at sock.
2091 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2094 struct lttng_ht_iter iter
, uiter
;
2095 struct ust_app
*app
;
2096 struct ust_app_session
*ua_sess
;
2097 struct ust_app_channel
*ua_chan
;
2098 struct ust_app_event
*ua_event
;
2100 if (usess
== NULL
) {
2101 ERR("No UST session on global update. Returning");
2105 DBG2("UST app global update for app sock %d for session id %d", sock
,
2110 app
= find_app_by_sock(sock
);
2112 ERR("Failed to update app sock %d", sock
);
2116 ua_sess
= create_ust_app_session(usess
, app
);
2117 if (ua_sess
== NULL
) {
2122 * We can iterate safely here over all UST app session sicne the create ust
2123 * app session above made a shadow copy of the UST global domain from the
2126 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2128 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2130 /* FIXME: Should we quit here or continue... */
2134 /* For each events */
2135 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2137 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2139 /* FIXME: Should we quit here or continue... */
2145 if (usess
->start_trace
) {
2146 ret
= ust_app_start_trace(usess
, app
);
2151 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2160 * Add context to a specific channel for global UST domain.
2162 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2163 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2166 struct lttng_ht_node_str
*ua_chan_node
;
2167 struct lttng_ht_iter iter
, uiter
;
2168 struct ust_app_channel
*ua_chan
= NULL
;
2169 struct ust_app_session
*ua_sess
;
2170 struct ust_app
*app
;
2174 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2175 ua_sess
= lookup_session_by_app(usess
, app
);
2176 if (ua_sess
== NULL
) {
2180 /* Lookup channel in the ust app session */
2181 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2182 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2183 if (ua_chan_node
== NULL
) {
2186 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2189 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2200 * Add context to a specific event in a channel for global UST domain.
2202 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2203 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2204 struct ltt_ust_context
*uctx
)
2207 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2208 struct lttng_ht_iter iter
, uiter
;
2209 struct ust_app_session
*ua_sess
;
2210 struct ust_app_event
*ua_event
;
2211 struct ust_app_channel
*ua_chan
= NULL
;
2212 struct ust_app
*app
;
2216 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2217 ua_sess
= lookup_session_by_app(usess
, app
);
2218 if (ua_sess
== NULL
) {
2222 /* Lookup channel in the ust app session */
2223 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2224 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2225 if (ua_chan_node
== NULL
) {
2228 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2231 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2232 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2233 if (ua_event_node
== NULL
) {
2236 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2239 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2250 * Enable event for a channel from a UST session for a specific PID.
2252 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2253 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2256 struct lttng_ht_iter iter
;
2257 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2258 struct ust_app
*app
;
2259 struct ust_app_session
*ua_sess
;
2260 struct ust_app_channel
*ua_chan
;
2261 struct ust_app_event
*ua_event
;
2263 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2267 app
= ust_app_find_by_pid(pid
);
2269 ERR("UST app enable event per PID %d not found", pid
);
2274 ua_sess
= lookup_session_by_app(usess
, app
);
2275 /* If ua_sess is NULL, there is a code flow error */
2278 /* Lookup channel in the ust app session */
2279 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2280 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2281 /* If the channel is not found, there is a code flow error */
2282 assert(ua_chan_node
);
2284 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2286 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2287 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2288 if (ua_event_node
== NULL
) {
2289 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2294 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2296 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2308 * Disable event for a channel from a UST session for a specific PID.
2310 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2311 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2314 struct lttng_ht_iter iter
;
2315 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2316 struct ust_app
*app
;
2317 struct ust_app_session
*ua_sess
;
2318 struct ust_app_channel
*ua_chan
;
2319 struct ust_app_event
*ua_event
;
2321 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2325 app
= ust_app_find_by_pid(pid
);
2327 ERR("UST app disable event per PID %d not found", pid
);
2332 ua_sess
= lookup_session_by_app(usess
, app
);
2333 /* If ua_sess is NULL, there is a code flow error */
2336 /* Lookup channel in the ust app session */
2337 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2338 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2339 if (ua_chan_node
== NULL
) {
2340 /* Channel does not exist, skip disabling */
2343 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2345 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2346 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2347 if (ua_event_node
== NULL
) {
2348 /* Event does not exist, skip disabling */
2351 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2353 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);