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.
206 DBG2("UST app pid %d deleted", app
->key
.pid
);
213 * URCU intermediate call to delete an UST app.
216 void delete_ust_app_rcu(struct rcu_head
*head
)
218 struct lttng_ht_node_ulong
*node
=
219 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
220 struct ust_app
*app
=
221 caa_container_of(node
, struct ust_app
, node
);
223 DBG3("Call RCU deleting app PID %d", app
->key
.pid
);
228 * Alloc new UST app session.
231 struct ust_app_session
*alloc_ust_app_session(void)
233 struct ust_app_session
*ua_sess
;
235 /* Init most of the default value by allocating and zeroing */
236 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
237 if (ua_sess
== NULL
) {
242 ua_sess
->handle
= -1;
243 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
252 * Alloc new UST app channel.
255 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
256 struct lttng_ust_channel
*attr
)
258 struct ust_app_channel
*ua_chan
;
260 /* Init most of the default value by allocating and zeroing */
261 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
262 if (ua_chan
== NULL
) {
267 /* Setup channel name */
268 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
269 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
271 ua_chan
->enabled
= 1;
272 ua_chan
->handle
= -1;
273 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
274 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
275 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
277 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
279 /* Copy attributes */
281 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
284 DBG3("UST app channel %s allocated", ua_chan
->name
);
293 * Alloc new UST app event.
296 struct ust_app_event
*alloc_ust_app_event(char *name
,
297 struct lttng_ust_event
*attr
)
299 struct ust_app_event
*ua_event
;
301 /* Init most of the default value by allocating and zeroing */
302 ua_event
= zmalloc(sizeof(struct ust_app_event
));
303 if (ua_event
== NULL
) {
308 ua_event
->enabled
= 1;
309 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
310 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
311 ua_event
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
312 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
314 /* Copy attributes */
316 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
319 DBG3("UST app event %s allocated", ua_event
->name
);
328 * Alloc new UST app context.
331 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
333 struct ust_app_ctx
*ua_ctx
;
335 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
336 if (ua_ctx
== NULL
) {
341 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
344 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
351 * Find an ust_app using the sock and return it. RCU read side lock must be
352 * held before calling this helper function.
355 struct ust_app
*find_app_by_sock(int sock
)
357 struct lttng_ht_node_ulong
*node
;
358 struct ust_app_key
*key
;
359 struct lttng_ht_iter iter
;
361 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
),
363 node
= lttng_ht_iter_get_node_ulong(&iter
);
365 DBG2("UST app find by sock %d key not found", sock
);
368 key
= caa_container_of(node
, struct ust_app_key
, node
);
370 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) key
->pid
), &iter
);
371 node
= lttng_ht_iter_get_node_ulong(&iter
);
373 DBG2("UST app find by sock %d not found", sock
);
376 return caa_container_of(node
, struct ust_app
, node
);
383 * Create the channel context on the tracer.
386 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
387 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
391 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
392 ua_chan
->obj
, &ua_ctx
->obj
);
397 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
399 DBG2("UST app context created successfully for channel %s", ua_chan
->name
);
406 * Create the event context on the tracer.
409 int create_ust_event_context(struct ust_app_event
*ua_event
,
410 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
414 ret
= ustctl_add_context(app
->key
.sock
, &ua_ctx
->ctx
,
415 ua_event
->obj
, &ua_ctx
->obj
);
420 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
422 DBG2("UST app context created successfully for event %s", ua_event
->name
);
429 * Disable the specified event on to UST tracer for the UST session.
431 static int disable_ust_event(struct ust_app
*app
,
432 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
436 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
438 ERR("UST app event %s disable failed for app (pid: %d) "
439 "and session handle %d with ret %d",
440 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
444 DBG2("UST app event %s disabled successfully for app (pid: %d)",
445 ua_event
->attr
.name
, app
->key
.pid
);
452 * Disable the specified channel on to UST tracer for the UST session.
454 static int disable_ust_channel(struct ust_app
*app
,
455 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
459 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
461 ERR("UST app channel %s disable failed for app (pid: %d) "
462 "and session handle %d with ret %d",
463 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
467 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
468 ua_chan
->name
, app
->key
.pid
);
475 * Enable the specified channel on to UST tracer for the UST session.
477 static int enable_ust_channel(struct ust_app
*app
,
478 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
482 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
484 ERR("UST app channel %s enable failed for app (pid: %d) "
485 "and session handle %d with ret %d",
486 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
490 ua_chan
->enabled
= 1;
492 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
493 ua_chan
->name
, app
->key
.pid
);
500 * Enable the specified event on to UST tracer for the UST session.
502 static int enable_ust_event(struct ust_app
*app
,
503 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
507 ret
= ustctl_enable(app
->key
.sock
, ua_event
->obj
);
509 ERR("UST app event %s enable failed for app (pid: %d) "
510 "and session handle %d with ret %d",
511 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
515 DBG2("UST app event %s enabled successfully for app (pid: %d)",
516 ua_event
->attr
.name
, app
->key
.pid
);
523 * Open metadata onto the UST tracer for a UST session.
525 static int open_ust_metadata(struct ust_app
*app
,
526 struct ust_app_session
*ua_sess
)
529 struct lttng_ust_channel_attr uattr
;
531 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
532 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
533 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
534 uattr
.switch_timer_interval
=
535 ua_sess
->metadata
->attr
.switch_timer_interval
;
536 uattr
.read_timer_interval
=
537 ua_sess
->metadata
->attr
.read_timer_interval
;
538 uattr
.output
= ua_sess
->metadata
->attr
.output
;
540 /* UST tracer metadata creation */
541 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
542 &ua_sess
->metadata
->obj
);
544 ERR("UST app open metadata failed for app pid:%d with ret %d",
549 ua_sess
->metadata
->handle
= ua_sess
->metadata
->obj
->handle
;
556 * Create stream onto the UST tracer for a UST session.
558 static int create_ust_stream(struct ust_app
*app
,
559 struct ust_app_session
*ua_sess
)
563 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
564 &ua_sess
->metadata
->stream_obj
);
566 ERR("UST create metadata stream failed");
575 * Create the specified channel onto the UST tracer for a UST session.
577 static int create_ust_channel(struct ust_app
*app
,
578 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
582 /* TODO: remove cast and use lttng-ust-abi.h */
583 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
584 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
586 ERR("Creating channel %s for app (pid: %d, sock: %d) "
587 "and session handle %d with ret %d",
588 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
589 ua_sess
->handle
, ret
);
593 ua_chan
->handle
= ua_chan
->obj
->handle
;
595 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
596 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
598 /* If channel is not enabled, disable it on the tracer */
599 if (!ua_chan
->enabled
) {
600 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
611 * Create the specified event onto the UST tracer for a UST session.
614 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
615 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
619 /* Create UST event on tracer */
620 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
623 if (ret
== -EEXIST
) {
627 ERR("Error ustctl create event %s for app pid: %d with ret %d",
628 ua_event
->attr
.name
, app
->key
.pid
, ret
);
632 ua_event
->handle
= ua_event
->obj
->handle
;
634 DBG2("UST app event %s created successfully for pid:%d",
635 ua_event
->attr
.name
, app
->key
.pid
);
637 /* If event not enabled, disable it on the tracer */
638 if (ua_event
->enabled
== 0) {
639 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
642 * If we hit an EPERM, something is wrong with our disable call. If
643 * we get an EEXIST, there is a problem on the tracer side since we
648 /* Code flow problem */
651 /* It's OK for our use case. */
666 * Copy data between an UST app event and a LTT event.
668 static void shadow_copy_event(struct ust_app_event
*ua_event
,
669 struct ltt_ust_event
*uevent
)
671 struct lttng_ht_iter iter
;
672 struct ltt_ust_context
*uctx
;
673 struct ust_app_ctx
*ua_ctx
;
675 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
676 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
678 ua_event
->enabled
= uevent
->enabled
;
680 /* Copy event attributes */
681 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
683 cds_lfht_for_each_entry(uevent
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
684 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
685 if (ua_ctx
== NULL
) {
686 /* malloc() failed. We should simply stop */
690 lttng_ht_node_init_ulong(&ua_ctx
->node
,
691 (unsigned long) ua_ctx
->ctx
.ctx
);
692 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
697 * Copy data between an UST app channel and a LTT channel.
699 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
700 struct ltt_ust_channel
*uchan
)
702 struct lttng_ht_iter iter
;
703 struct lttng_ht_node_str
*ua_event_node
;
704 struct ltt_ust_event
*uevent
;
705 struct ltt_ust_context
*uctx
;
706 struct ust_app_event
*ua_event
;
707 struct ust_app_ctx
*ua_ctx
;
709 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
711 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
712 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
713 /* Copy event attributes */
714 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
716 ua_chan
->enabled
= uchan
->enabled
;
718 cds_lfht_for_each_entry(uchan
->ctx
->ht
, &iter
.iter
, uctx
, node
.node
) {
719 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
720 if (ua_ctx
== NULL
) {
723 lttng_ht_node_init_ulong(&ua_ctx
->node
,
724 (unsigned long) ua_ctx
->ctx
.ctx
);
725 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
728 /* Copy all events from ltt ust channel to ust app channel */
729 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
730 struct lttng_ht_iter uiter
;
732 lttng_ht_lookup(ua_chan
->events
, (void *) uevent
->attr
.name
, &uiter
);
733 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
734 if (ua_event_node
== NULL
) {
735 DBG2("UST event %s not found on shadow copy channel",
737 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
738 if (ua_event
== NULL
) {
741 shadow_copy_event(ua_event
, uevent
);
742 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
746 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
750 * Copy data between a UST app session and a regular LTT session.
752 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
753 struct ltt_ust_session
*usess
, struct ust_app
*app
)
755 struct lttng_ht_node_str
*ua_chan_node
;
756 struct lttng_ht_iter iter
;
757 struct ltt_ust_channel
*uchan
;
758 struct ust_app_channel
*ua_chan
;
764 /* Get date and time for unique app path */
766 timeinfo
= localtime(&rawtime
);
767 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
769 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
771 ua_sess
->id
= usess
->id
;
772 ua_sess
->uid
= usess
->uid
;
773 ua_sess
->gid
= usess
->gid
;
775 ret
= snprintf(ua_sess
->path
, PATH_MAX
, "%s/%s-%d-%s", usess
->pathname
,
776 app
->name
, app
->key
.pid
, datetime
);
778 PERROR("asprintf UST shadow copy session");
779 /* TODO: We cannot return an error from here.. */
783 /* TODO: support all UST domain */
785 /* Iterate over all channels in global domain. */
786 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
788 struct lttng_ht_iter uiter
;
790 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
791 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
792 if (ua_chan_node
!= NULL
) {
793 /* Session exist. Contiuing. */
797 DBG2("Channel %s not found on shadow session copy, creating it",
799 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
800 if (ua_chan
== NULL
) {
801 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
805 shadow_copy_channel(ua_chan
, uchan
);
806 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
811 * Lookup sesison wrapper.
814 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
815 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
817 /* Get right UST app session from app */
818 lttng_ht_lookup(app
->sessions
, (void *)((unsigned long) usess
->id
), iter
);
822 * Return ust app session from the app session hashtable using the UST session
825 static struct ust_app_session
*lookup_session_by_app(
826 struct ltt_ust_session
*usess
, struct ust_app
*app
)
828 struct lttng_ht_iter iter
;
829 struct lttng_ht_node_ulong
*node
;
831 __lookup_session_by_app(usess
, app
, &iter
);
832 node
= lttng_ht_iter_get_node_ulong(&iter
);
837 return caa_container_of(node
, struct ust_app_session
, node
);
844 * Create a UST session onto the tracer of app and add it the session
847 * Return ust app session or NULL on error.
849 static struct ust_app_session
*create_ust_app_session(
850 struct ltt_ust_session
*usess
, struct ust_app
*app
)
853 struct ust_app_session
*ua_sess
;
855 ua_sess
= lookup_session_by_app(usess
, app
);
856 if (ua_sess
== NULL
) {
857 DBG2("UST app pid: %d session id %d not found, creating it",
858 app
->key
.pid
, usess
->id
);
859 ua_sess
= alloc_ust_app_session();
860 if (ua_sess
== NULL
) {
861 /* Only malloc can failed so something is really wrong */
864 shadow_copy_session(ua_sess
, usess
, app
);
867 if (ua_sess
->handle
== -1) {
868 ret
= ustctl_create_session(app
->key
.sock
);
870 ERR("Creating session for app pid %d", app
->key
.pid
);
871 /* This means that the tracer is gone... */
872 ua_sess
= (void*) -1UL;
876 ua_sess
->handle
= ret
;
878 /* Add ust app session to app's HT */
879 lttng_ht_node_init_ulong(&ua_sess
->node
, (unsigned long) ua_sess
->id
);
880 lttng_ht_add_unique_ulong(app
->sessions
, &ua_sess
->node
);
882 DBG2("UST app session created successfully with handle %d", ret
);
889 delete_ust_app_session(-1, ua_sess
);
894 * Create a context for the channel on the tracer.
897 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
898 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
902 struct lttng_ht_iter iter
;
903 struct lttng_ht_node_ulong
*node
;
904 struct ust_app_ctx
*ua_ctx
;
906 DBG2("UST app adding context to channel %s", ua_chan
->name
);
908 lttng_ht_lookup(ua_chan
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
909 node
= lttng_ht_iter_get_node_ulong(&iter
);
915 ua_ctx
= alloc_ust_app_ctx(uctx
);
916 if (ua_ctx
== NULL
) {
922 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
923 lttng_ht_add_unique_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
925 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
935 * Create an UST context and enable it for the event on the tracer.
938 int create_ust_app_event_context(struct ust_app_session
*ua_sess
,
939 struct ust_app_event
*ua_event
, struct lttng_ust_context
*uctx
,
943 struct lttng_ht_iter iter
;
944 struct lttng_ht_node_ulong
*node
;
945 struct ust_app_ctx
*ua_ctx
;
947 DBG2("UST app adding context to event %s", ua_event
->name
);
949 lttng_ht_lookup(ua_event
->ctx
, (void *)((unsigned long)uctx
->ctx
), &iter
);
950 node
= lttng_ht_iter_get_node_ulong(&iter
);
956 ua_ctx
= alloc_ust_app_ctx(uctx
);
957 if (ua_ctx
== NULL
) {
963 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
964 lttng_ht_add_unique_ulong(ua_event
->ctx
, &ua_ctx
->node
);
966 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
976 * Enable on the tracer side a ust app event for the session and channel.
979 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
980 struct ust_app_event
*ua_event
, struct ust_app
*app
)
984 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
989 ua_event
->enabled
= 1;
996 * Disable on the tracer side a ust app event for the session and channel.
998 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
999 struct ust_app_event
*ua_event
, struct ust_app
*app
)
1003 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
1008 ua_event
->enabled
= 0;
1015 * Lookup ust app channel for session and disable it on the tracer side.
1018 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
1019 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
1023 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
1028 ua_chan
->enabled
= 0;
1035 * Lookup ust app channel for session and enable it on the tracer side.
1037 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
1038 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
1041 struct lttng_ht_iter iter
;
1042 struct lttng_ht_node_str
*ua_chan_node
;
1043 struct ust_app_channel
*ua_chan
;
1045 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1046 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1047 if (ua_chan_node
== NULL
) {
1048 DBG2("Unable to find channel %s in ust session id %u",
1049 uchan
->name
, ua_sess
->id
);
1053 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1055 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
1065 * Create UST app channel and create it on the tracer.
1067 static struct ust_app_channel
*create_ust_app_channel(
1068 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
1069 struct ust_app
*app
)
1072 struct lttng_ht_iter iter
;
1073 struct lttng_ht_node_str
*ua_chan_node
;
1074 struct ust_app_channel
*ua_chan
;
1076 /* Lookup channel in the ust app session */
1077 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
1078 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
1079 if (ua_chan_node
!= NULL
) {
1080 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1084 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
1085 if (ua_chan
== NULL
) {
1086 /* Only malloc can fail here */
1089 shadow_copy_channel(ua_chan
, uchan
);
1091 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1093 /* Not found previously means that it does not exist on the tracer */
1094 assert(ret
!= -EEXIST
);
1098 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1100 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
1107 delete_ust_app_channel(-1, ua_chan
);
1112 * Create UST app event and create it on the tracer side.
1115 int create_ust_app_event(struct ust_app_session
*ua_sess
,
1116 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
1117 struct ust_app
*app
)
1120 struct lttng_ht_iter iter
;
1121 struct lttng_ht_node_str
*ua_event_node
;
1122 struct ust_app_event
*ua_event
;
1124 /* Get event node */
1125 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
1126 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
1127 if (ua_event_node
!= NULL
) {
1132 /* Does not exist so create one */
1133 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1134 if (ua_event
== NULL
) {
1135 /* Only malloc can failed so something is really wrong */
1139 shadow_copy_event(ua_event
, uevent
);
1141 /* Create it on the tracer side */
1142 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1144 /* Not found previously means that it does not exist on the tracer */
1145 assert(ret
!= -EEXIST
);
1149 lttng_ht_add_unique_str(ua_chan
->events
, &ua_event
->node
);
1151 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
1158 /* Valid. Calling here is already in a read side lock */
1159 delete_ust_app_event(-1, ua_event
);
1164 * Create UST metadata and open it on the tracer side.
1166 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
1167 char *pathname
, struct ust_app
*app
)
1171 if (ua_sess
->metadata
== NULL
) {
1172 /* Allocate UST metadata */
1173 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
1174 if (ua_sess
->metadata
== NULL
) {
1175 /* malloc() failed */
1179 ret
= open_ust_metadata(app
, ua_sess
);
1181 DBG3("Opening metadata failed. Cleaning up memory");
1183 /* Cleanup failed metadata struct */
1184 free(ua_sess
->metadata
);
1186 * This is very important because delete_ust_app_session check if
1187 * the pointer is null or not in order to delete the metadata.
1189 ua_sess
->metadata
= NULL
;
1193 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
1196 /* Open UST metadata stream */
1197 if (ua_sess
->metadata
->stream_obj
== NULL
) {
1198 ret
= create_ust_stream(app
, ua_sess
);
1203 ret
= run_as_mkdir(ua_sess
->path
, S_IRWXU
| S_IRWXG
,
1204 ua_sess
->uid
, ua_sess
->gid
);
1206 PERROR("mkdir UST metadata");
1210 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
1211 "%s/metadata", ua_sess
->path
);
1213 PERROR("asprintf UST create stream");
1217 DBG2("UST metadata stream object created for app pid %d",
1220 ERR("Attempting to create stream without metadata opened");
1231 * Return pointer to traceable apps list.
1233 struct lttng_ht
*ust_app_get_ht(void)
1239 * Return ust app pointer or NULL if not found.
1241 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
1243 struct lttng_ht_node_ulong
*node
;
1244 struct lttng_ht_iter iter
;
1247 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
1248 node
= lttng_ht_iter_get_node_ulong(&iter
);
1250 DBG2("UST app no found with pid %d", pid
);
1255 DBG2("Found UST app by pid %d", pid
);
1257 return caa_container_of(node
, struct ust_app
, node
);
1265 * Using pid and uid (of the app), allocate a new ust_app struct and
1266 * add it to the global traceable app list.
1268 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1269 * bitness is not supported.
1271 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
1273 struct ust_app
*lta
;
1276 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
1277 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
1278 ERR("Registration failed: application \"%s\" (pid: %d) has "
1279 "%d-bit long, but no consumerd for this long size is available.\n",
1280 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1287 if (msg
->major
!= LTTNG_UST_COMM_MAJOR
) {
1288 ERR("Registration failed: application \"%s\" (pid: %d) has "
1289 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1290 msg
->name
, msg
->pid
, msg
->major
, msg
->minor
);
1297 lta
= zmalloc(sizeof(struct ust_app
));
1303 lta
->ppid
= msg
->ppid
;
1304 lta
->uid
= msg
->uid
;
1305 lta
->gid
= msg
->gid
;
1306 lta
->compatible
= 0; /* Not compatible until proven */
1307 lta
->bits_per_long
= msg
->bits_per_long
;
1308 lta
->v_major
= msg
->major
;
1309 lta
->v_minor
= msg
->minor
;
1310 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1311 lta
->name
[16] = '\0';
1312 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1315 lta
->key
.pid
= msg
->pid
;
1316 lttng_ht_node_init_ulong(<a
->node
, (unsigned long)lta
->key
.pid
);
1317 lta
->key
.sock
= sock
;
1318 lttng_ht_node_init_ulong(<a
->key
.node
, (unsigned long)lta
->key
.sock
);
1321 lttng_ht_add_unique_ulong(ust_app_sock_key_map
, <a
->key
.node
);
1322 lttng_ht_add_unique_ulong(ust_app_ht
, <a
->node
);
1325 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1326 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1327 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1333 * Unregister app by removing it from the global traceable app list and freeing
1336 * The socket is already closed at this point so no close to sock.
1338 void ust_app_unregister(int sock
)
1340 struct ust_app
*lta
;
1341 struct lttng_ht_node_ulong
*node
;
1342 struct lttng_ht_iter iter
;
1346 lta
= find_app_by_sock(sock
);
1348 ERR("Unregister app sock %d not found!", sock
);
1352 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1354 /* Remove application from socket hash table */
1355 lttng_ht_lookup(ust_app_sock_key_map
, (void *)((unsigned long) sock
), &iter
);
1356 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1359 /* Get the node reference for a call_rcu */
1360 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) lta
->key
.pid
), &iter
);
1361 node
= lttng_ht_iter_get_node_ulong(&iter
);
1363 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1367 /* Remove application from PID hash table */
1368 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1370 call_rcu(&node
->head
, delete_ust_app_rcu
);
1377 * Return traceable_app_count
1379 unsigned long ust_app_list_count(void)
1381 unsigned long count
;
1384 count
= lttng_ht_get_count(ust_app_ht
);
1391 * Fill events array with all events name of all registered apps.
1393 int ust_app_list_events(struct lttng_event
**events
)
1396 size_t nbmem
, count
= 0;
1397 struct lttng_ht_iter iter
;
1398 struct ust_app
*app
;
1399 struct lttng_event
*tmp
;
1401 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1402 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1404 PERROR("zmalloc ust app events");
1411 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1412 struct lttng_ust_tracepoint_iter uiter
;
1414 if (!app
->compatible
) {
1416 * TODO: In time, we should notice the caller of this error by
1417 * telling him that this is a version error.
1421 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1423 ERR("UST app list events getting handle failed for app pid %d",
1428 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1429 &uiter
)) != -ENOENT
) {
1430 if (count
>= nbmem
) {
1431 DBG2("Reallocating event list from %zu to %zu entries", nbmem
,
1434 tmp
= realloc(tmp
, nbmem
* sizeof(struct lttng_event
));
1436 PERROR("realloc ust app events");
1441 memcpy(tmp
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
1442 tmp
[count
].loglevel
= uiter
.loglevel
;
1443 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1444 tmp
[count
].pid
= app
->key
.pid
;
1445 tmp
[count
].enabled
= -1;
1453 DBG2("UST app list events done (%zu events)", count
);
1462 * Free and clean all traceable apps of the global list.
1464 void ust_app_clean_list(void)
1467 struct lttng_ht_iter iter
;
1468 struct lttng_ht_node_ulong
*node
;
1470 DBG2("UST app cleaning registered apps hash table");
1474 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, node
, node
) {
1475 ret
= lttng_ht_del(ust_app_ht
, &iter
);
1477 call_rcu(&node
->head
, delete_ust_app_rcu
);
1479 /* Destroy is done only when the ht is empty */
1480 lttng_ht_destroy(ust_app_ht
);
1482 cds_lfht_for_each_entry(ust_app_sock_key_map
->ht
, &iter
.iter
, node
, node
) {
1483 ret
= lttng_ht_del(ust_app_sock_key_map
, &iter
);
1486 /* Destroy is done only when the ht is empty */
1487 lttng_ht_destroy(ust_app_sock_key_map
);
1493 * Init UST app hash table.
1495 void ust_app_ht_alloc(void)
1497 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1498 ust_app_sock_key_map
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1502 * For a specific UST session, disable the channel for all registered apps.
1504 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
1505 struct ltt_ust_channel
*uchan
)
1508 struct lttng_ht_iter iter
;
1509 struct lttng_ht_node_str
*ua_chan_node
;
1510 struct ust_app
*app
;
1511 struct ust_app_session
*ua_sess
;
1512 struct ust_app_channel
*ua_chan
;
1514 if (usess
== NULL
|| uchan
== NULL
) {
1515 ERR("Disabling UST global channel with NULL values");
1520 DBG2("UST app disabling channel %s from global domain for session id %d",
1521 uchan
->name
, usess
->id
);
1525 /* For every registered applications */
1526 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1527 struct lttng_ht_iter uiter
;
1528 if (!app
->compatible
) {
1530 * TODO: In time, we should notice the caller of this error by
1531 * telling him that this is a version error.
1535 ua_sess
= lookup_session_by_app(usess
, app
);
1536 if (ua_sess
== NULL
) {
1541 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1542 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1543 /* If the session if found for the app, the channel must be there */
1544 assert(ua_chan_node
);
1546 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1547 /* The channel must not be already disabled */
1548 assert(ua_chan
->enabled
== 1);
1550 /* Disable channel onto application */
1551 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
1553 /* XXX: We might want to report this error at some point... */
1565 * For a specific UST session, enable the channel for all registered apps.
1567 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
1568 struct ltt_ust_channel
*uchan
)
1571 struct lttng_ht_iter iter
;
1572 struct ust_app
*app
;
1573 struct ust_app_session
*ua_sess
;
1575 if (usess
== NULL
|| uchan
== NULL
) {
1576 ERR("Adding UST global channel to NULL values");
1581 DBG2("UST app enabling channel %s to global domain for session id %d",
1582 uchan
->name
, usess
->id
);
1586 /* For every registered applications */
1587 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1588 if (!app
->compatible
) {
1590 * TODO: In time, we should notice the caller of this error by
1591 * telling him that this is a version error.
1595 ua_sess
= lookup_session_by_app(usess
, app
);
1596 if (ua_sess
== NULL
) {
1600 /* Enable channel onto application */
1601 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1603 /* XXX: We might want to report this error at some point... */
1615 * Disable an event in a channel and for a specific session.
1617 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
1618 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1621 struct lttng_ht_iter iter
, uiter
;
1622 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1623 struct ust_app
*app
;
1624 struct ust_app_session
*ua_sess
;
1625 struct ust_app_channel
*ua_chan
;
1626 struct ust_app_event
*ua_event
;
1628 DBG("UST app disabling event %s for all apps in channel "
1629 "%s for session id %d", uevent
->attr
.name
, uchan
->name
, usess
->id
);
1633 /* For all registered applications */
1634 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1635 if (!app
->compatible
) {
1637 * TODO: In time, we should notice the caller of this error by
1638 * telling him that this is a version error.
1642 ua_sess
= lookup_session_by_app(usess
, app
);
1643 if (ua_sess
== NULL
) {
1648 /* Lookup channel in the ust app session */
1649 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1650 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1651 if (ua_chan_node
== NULL
) {
1652 DBG2("Channel %s not found in session id %d for app pid %d."
1653 "Skipping", uchan
->name
, usess
->id
, app
->key
.pid
);
1656 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1658 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
1659 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1660 if (ua_event_node
== NULL
) {
1661 DBG2("Event %s not found in channel %s for app pid %d."
1662 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->key
.pid
);
1665 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1667 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1669 /* XXX: Report error someday... */
1680 * For a specific UST session and UST channel, the event for all
1683 int ust_app_disable_all_event_glb(struct ltt_ust_session
*usess
,
1684 struct ltt_ust_channel
*uchan
)
1687 struct lttng_ht_iter iter
, uiter
;
1688 struct lttng_ht_node_str
*ua_chan_node
;
1689 struct ust_app
*app
;
1690 struct ust_app_session
*ua_sess
;
1691 struct ust_app_channel
*ua_chan
;
1692 struct ust_app_event
*ua_event
;
1694 DBG("UST app disabling all event for all apps in channel "
1695 "%s for session id %d", uchan
->name
, usess
->id
);
1699 /* For all registered applications */
1700 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1701 if (!app
->compatible
) {
1703 * TODO: In time, we should notice the caller of this error by
1704 * telling him that this is a version error.
1708 ua_sess
= lookup_session_by_app(usess
, app
);
1709 /* If ua_sess is NULL, there is a code flow error */
1712 /* Lookup channel in the ust app session */
1713 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1714 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1715 /* If the channel is not found, there is a code flow error */
1716 assert(ua_chan_node
);
1718 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1720 /* Disable each events of channel */
1721 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
1723 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
1725 /* XXX: Report error someday... */
1737 * For a specific UST session, create the channel for all registered apps.
1739 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
1740 struct ltt_ust_channel
*uchan
)
1742 struct lttng_ht_iter iter
;
1743 struct ust_app
*app
;
1744 struct ust_app_session
*ua_sess
;
1745 struct ust_app_channel
*ua_chan
;
1747 /* Very wrong code flow */
1751 DBG2("UST app adding channel %s to global domain for session id %d",
1752 uchan
->name
, usess
->id
);
1756 /* For every registered applications */
1757 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1758 if (!app
->compatible
) {
1760 * TODO: In time, we should notice the caller of this error by
1761 * telling him that this is a version error.
1766 * Create session on the tracer side and add it to app session HT. Note
1767 * that if session exist, it will simply return a pointer to the ust
1770 ua_sess
= create_ust_app_session(usess
, app
);
1771 if (ua_sess
== NULL
) {
1772 /* The malloc() failed. */
1774 } else if (ua_sess
== (void *) -1UL) {
1775 /* The application's socket is not valid. Contiuing */
1779 /* Create channel onto application */
1780 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1781 if (ua_chan
== NULL
) {
1782 /* Major problem here and it's maybe the tracer or malloc() */
1796 * Enable event for a specific session and channel on the tracer.
1798 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
1799 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1802 struct lttng_ht_iter iter
, uiter
;
1803 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
1804 struct ust_app
*app
;
1805 struct ust_app_session
*ua_sess
;
1806 struct ust_app_channel
*ua_chan
;
1807 struct ust_app_event
*ua_event
;
1809 DBG("UST app enabling event %s for all apps for session id %d",
1810 uevent
->attr
.name
, usess
->id
);
1813 * NOTE: At this point, this function is called only if the session and
1814 * channel passed are already created for all apps. and enabled on the
1820 /* For all registered applications */
1821 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1822 if (!app
->compatible
) {
1824 * TODO: In time, we should notice the caller of this error by
1825 * telling him that this is a version error.
1829 ua_sess
= lookup_session_by_app(usess
, app
);
1830 /* If ua_sess is NULL, there is a code flow error */
1833 /* Lookup channel in the ust app session */
1834 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1835 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1836 /* If the channel is not found, there is a code flow error */
1837 assert(ua_chan_node
);
1839 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1841 lttng_ht_lookup(ua_chan
->events
, (void*)uevent
->attr
.name
, &uiter
);
1842 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
1843 if (ua_event_node
== NULL
) {
1844 DBG3("UST app enable event %s not found for app PID %d."
1845 "Skipping app", uevent
->attr
.name
, app
->key
.pid
);
1848 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
1850 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
1862 * For a specific existing UST session and UST channel, creates the event for
1863 * all registered apps.
1865 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
1866 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1869 struct lttng_ht_iter iter
, uiter
;
1870 struct lttng_ht_node_str
*ua_chan_node
;
1871 struct ust_app
*app
;
1872 struct ust_app_session
*ua_sess
;
1873 struct ust_app_channel
*ua_chan
;
1875 DBG("UST app creating event %s for all apps for session id %d",
1876 uevent
->attr
.name
, usess
->id
);
1880 /* For all registered applications */
1881 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
1882 if (!app
->compatible
) {
1884 * TODO: In time, we should notice the caller of this error by
1885 * telling him that this is a version error.
1889 ua_sess
= lookup_session_by_app(usess
, app
);
1890 /* If ua_sess is NULL, there is a code flow error */
1893 /* Lookup channel in the ust app session */
1894 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1895 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1896 /* If the channel is not found, there is a code flow error */
1897 assert(ua_chan_node
);
1899 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1901 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1903 if (ret
!= -EEXIST
) {
1904 /* Possible value at this point: -ENOMEM. If so, we stop! */
1907 DBG2("UST app event %s already exist on app PID %d",
1908 uevent
->attr
.name
, app
->key
.pid
);
1919 * Start tracing for a specific UST session and app.
1921 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1924 struct lttng_ht_iter iter
;
1925 struct ust_app_session
*ua_sess
;
1926 struct ust_app_channel
*ua_chan
;
1927 struct ltt_ust_stream
*ustream
;
1930 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1934 if (!app
->compatible
) {
1938 ua_sess
= lookup_session_by_app(usess
, app
);
1939 if (ua_sess
== NULL
) {
1940 goto error_rcu_unlock
;
1943 /* Upon restart, we skip the setup, already done */
1944 if (ua_sess
->started
) {
1948 /* Indicate that the session has been started once */
1949 ua_sess
->started
= 1;
1951 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1953 goto error_rcu_unlock
;
1956 /* For each channel */
1957 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1959 /* Create all streams */
1961 /* Create UST stream */
1962 ustream
= zmalloc(sizeof(*ustream
));
1963 if (ustream
== NULL
) {
1964 PERROR("zmalloc ust stream");
1965 goto error_rcu_unlock
;
1968 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1971 /* Got all streams */
1974 ustream
->handle
= ustream
->obj
->handle
;
1976 /* Order is important */
1977 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1978 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1979 ua_sess
->path
, ua_chan
->name
,
1980 ua_chan
->streams
.count
++);
1982 PERROR("asprintf UST create stream");
1985 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1990 switch (app
->bits_per_long
) {
1992 consumerd_fd
= ust_consumerd64_fd
;
1995 consumerd_fd
= ust_consumerd32_fd
;
1999 goto error_rcu_unlock
;
2002 /* Setup UST consumer socket and send fds to it */
2003 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
2005 goto error_rcu_unlock
;
2009 /* This start the UST tracing */
2010 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
2012 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
2013 goto error_rcu_unlock
;
2016 /* Quiescent wait after starting trace */
2017 ustctl_wait_quiescent(app
->key
.sock
);
2029 * Stop tracing for a specific UST session and app.
2031 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2034 struct lttng_ht_iter iter
;
2035 struct ust_app_session
*ua_sess
;
2036 struct ust_app_channel
*ua_chan
;
2038 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
2042 if (!app
->compatible
) {
2046 ua_sess
= lookup_session_by_app(usess
, app
);
2047 if (ua_sess
== NULL
) {
2048 /* Only malloc can failed so something is really wrong */
2049 goto error_rcu_unlock
;
2053 * If started = 0, it means that stop trace has been called for a session
2054 * that was never started. This is a code flow error and should never
2057 assert(ua_sess
->started
== 1);
2059 /* This inhibits UST tracing */
2060 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
2062 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
2063 goto error_rcu_unlock
;
2066 /* Quiescent wait after stopping trace */
2067 ustctl_wait_quiescent(app
->key
.sock
);
2069 /* Flushing buffers */
2070 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2072 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_chan
->obj
);
2074 ERR("UST app PID %d channel %s flush failed with ret %d",
2075 app
->key
.pid
, ua_chan
->name
, ret
);
2076 /* Continuing flushing all buffers */
2081 /* Flush all buffers before stopping */
2082 ret
= ustctl_sock_flush_buffer(app
->key
.sock
, ua_sess
->metadata
->obj
);
2084 ERR("UST app PID %d metadata flush failed with ret %d", app
->key
.pid
,
2098 * Destroy a specific UST session in apps.
2100 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
2102 struct ust_app_session
*ua_sess
;
2103 struct lttng_ust_object_data obj
;
2104 struct lttng_ht_iter iter
;
2105 struct lttng_ht_node_ulong
*node
;
2108 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
2112 if (!app
->compatible
) {
2116 __lookup_session_by_app(usess
, app
, &iter
);
2117 node
= lttng_ht_iter_get_node_ulong(&iter
);
2119 /* Only malloc can failed so something is really wrong */
2120 goto error_rcu_unlock
;
2122 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
2123 ret
= lttng_ht_del(app
->sessions
, &iter
);
2125 obj
.handle
= ua_sess
->handle
;
2128 obj
.memory_map_size
= 0;
2129 ustctl_release_object(app
->key
.sock
, &obj
);
2131 delete_ust_app_session(app
->key
.sock
, ua_sess
);
2133 /* Quiescent wait after stopping trace */
2134 ustctl_wait_quiescent(app
->key
.sock
);
2146 * Start tracing for the UST session.
2148 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
2151 struct lttng_ht_iter iter
;
2152 struct ust_app
*app
;
2154 DBG("Starting all UST traces");
2158 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2159 ret
= ust_app_start_trace(usess
, app
);
2161 /* Continue to next apps even on error */
2172 * Start tracing for the UST session.
2174 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
2177 struct lttng_ht_iter iter
;
2178 struct ust_app
*app
;
2180 DBG("Stopping all UST traces");
2184 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2185 ret
= ust_app_stop_trace(usess
, app
);
2187 /* Continue to next apps even on error */
2198 * Destroy app UST session.
2200 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
2203 struct lttng_ht_iter iter
;
2204 struct ust_app
*app
;
2206 DBG("Destroy all UST traces");
2210 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2211 ret
= ust_app_destroy_trace(usess
, app
);
2213 /* Continue to next apps even on error */
2224 * Add channels/events from UST global domain to registered apps at sock.
2226 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
2229 struct lttng_ht_iter iter
, uiter
, iter_ctx
;
2230 struct ust_app
*app
;
2231 struct ust_app_session
*ua_sess
;
2232 struct ust_app_channel
*ua_chan
;
2233 struct ust_app_event
*ua_event
;
2234 struct ust_app_ctx
*ua_ctx
;
2236 if (usess
== NULL
) {
2237 ERR("No UST session on global update. Returning");
2241 DBG2("UST app global update for app sock %d for session id %d", sock
,
2246 app
= find_app_by_sock(sock
);
2248 ERR("Failed to update app sock %d", sock
);
2252 if (!app
->compatible
) {
2256 ua_sess
= create_ust_app_session(usess
, app
);
2257 if (ua_sess
== NULL
) {
2262 * We can iterate safely here over all UST app session sicne the create ust
2263 * app session above made a shadow copy of the UST global domain from the
2266 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
2268 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
2270 /* FIXME: Should we quit here or continue... */
2274 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter_ctx
.iter
, ua_ctx
,
2276 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2278 /* FIXME: Should we quit here or continue... */
2284 /* For each events */
2285 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
2287 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2289 /* FIXME: Should we quit here or continue... */
2293 /* Add context on events. */
2294 cds_lfht_for_each_entry(ua_event
->ctx
->ht
, &iter_ctx
.iter
,
2295 ua_ctx
, node
.node
) {
2296 ret
= create_ust_event_context(ua_event
, ua_ctx
, app
);
2298 /* FIXME: Should we quit here or continue... */
2305 if (usess
->start_trace
) {
2306 ret
= ust_app_start_trace(usess
, app
);
2311 DBG2("UST trace started for app pid %d", app
->key
.pid
);
2320 * Add context to a specific channel for global UST domain.
2322 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
2323 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
2326 struct lttng_ht_node_str
*ua_chan_node
;
2327 struct lttng_ht_iter iter
, uiter
;
2328 struct ust_app_channel
*ua_chan
= NULL
;
2329 struct ust_app_session
*ua_sess
;
2330 struct ust_app
*app
;
2334 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2335 if (!app
->compatible
) {
2337 * TODO: In time, we should notice the caller of this error by
2338 * telling him that this is a version error.
2342 ua_sess
= lookup_session_by_app(usess
, app
);
2343 if (ua_sess
== NULL
) {
2347 /* Lookup channel in the ust app session */
2348 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2349 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2350 if (ua_chan_node
== NULL
) {
2353 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2356 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
2367 * Add context to a specific event in a channel for global UST domain.
2369 int ust_app_add_ctx_event_glb(struct ltt_ust_session
*usess
,
2370 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
,
2371 struct ltt_ust_context
*uctx
)
2374 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2375 struct lttng_ht_iter iter
, uiter
;
2376 struct ust_app_session
*ua_sess
;
2377 struct ust_app_event
*ua_event
;
2378 struct ust_app_channel
*ua_chan
= NULL
;
2379 struct ust_app
*app
;
2383 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2384 if (!app
->compatible
) {
2386 * TODO: In time, we should notice the caller of this error by
2387 * telling him that this is a version error.
2391 ua_sess
= lookup_session_by_app(usess
, app
);
2392 if (ua_sess
== NULL
) {
2396 /* Lookup channel in the ust app session */
2397 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
2398 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
2399 if (ua_chan_node
== NULL
) {
2402 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
2405 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
2406 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
2407 if (ua_event_node
== NULL
) {
2410 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
,
2413 ret
= create_ust_app_event_context(ua_sess
, ua_event
, &uctx
->ctx
, app
);
2424 * Enable event for a channel from a UST session for a specific PID.
2426 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
2427 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2430 struct lttng_ht_iter iter
;
2431 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2432 struct ust_app
*app
;
2433 struct ust_app_session
*ua_sess
;
2434 struct ust_app_channel
*ua_chan
;
2435 struct ust_app_event
*ua_event
;
2437 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
2441 app
= ust_app_find_by_pid(pid
);
2443 ERR("UST app enable event per PID %d not found", pid
);
2448 if (!app
->compatible
) {
2453 ua_sess
= lookup_session_by_app(usess
, app
);
2454 /* If ua_sess is NULL, there is a code flow error */
2457 /* Lookup channel in the ust app session */
2458 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2459 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2460 /* If the channel is not found, there is a code flow error */
2461 assert(ua_chan_node
);
2463 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2465 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2466 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2467 if (ua_event_node
== NULL
) {
2468 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
2473 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2475 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
2487 * Disable event for a channel from a UST session for a specific PID.
2489 int ust_app_disable_event_pid(struct ltt_ust_session
*usess
,
2490 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
2493 struct lttng_ht_iter iter
;
2494 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
2495 struct ust_app
*app
;
2496 struct ust_app_session
*ua_sess
;
2497 struct ust_app_channel
*ua_chan
;
2498 struct ust_app_event
*ua_event
;
2500 DBG("UST app disabling event %s for PID %d", uevent
->attr
.name
, pid
);
2504 app
= ust_app_find_by_pid(pid
);
2506 ERR("UST app disable event per PID %d not found", pid
);
2511 if (!app
->compatible
) {
2516 ua_sess
= lookup_session_by_app(usess
, app
);
2517 /* If ua_sess is NULL, there is a code flow error */
2520 /* Lookup channel in the ust app session */
2521 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2522 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2523 if (ua_chan_node
== NULL
) {
2524 /* Channel does not exist, skip disabling */
2527 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2529 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &iter
);
2530 ua_event_node
= lttng_ht_iter_get_node_str(&iter
);
2531 if (ua_event_node
== NULL
) {
2532 /* Event does not exist, skip disabling */
2535 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
2537 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
2548 * Validate version of UST apps and set the compatible bit.
2550 int ust_app_validate_version(int sock
)
2553 struct ust_app
*app
;
2557 app
= find_app_by_sock(sock
);
2560 ret
= ustctl_tracer_version(sock
, &app
->version
);
2565 /* Validate version */
2566 if (app
->version
.major
> UST_APP_MAJOR_VERSION
) {
2570 DBG2("UST app PID %d is compatible with major version %d "
2571 "(supporting <= %d)", app
->key
.pid
, app
->version
.major
,
2572 UST_APP_MAJOR_VERSION
);
2573 app
->compatible
= 1;
2578 DBG2("UST app PID %d is not compatible with major version %d "
2579 "(supporting <= %d)", app
->key
.pid
, app
->version
.major
,
2580 UST_APP_MAJOR_VERSION
);
2581 app
->compatible
= 0;
2587 * Calibrate registered applications.
2589 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
2592 struct lttng_ht_iter iter
;
2593 struct ust_app
*app
;
2597 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, node
.node
) {
2598 if (!app
->compatible
) {
2600 * TODO: In time, we should notice the caller of this error by
2601 * telling him that this is a version error.
2606 ret
= ustctl_calibrate(app
->key
.sock
, calibrate
);
2610 /* Means that it's not implemented on the tracer side. */
2614 /* TODO: Report error to user */
2615 DBG2("Calibrate app PID %d returned with error %d",
2622 DBG("UST app global domain calibration finished");