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>
29 #include <urcu/compiler.h>
31 #include <lttng-share.h>
33 #include "hashtable.h"
35 #include "ust-consumer.h"
39 * Delete ust app event safely. RCU read lock must be held before calling
42 static void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
44 /* TODO : remove context */
45 //struct ust_app_ctx *ltctx;
46 //cds_lfht_for_each_entry(lte->ctx, &iter, ltctx, node) {
47 // delete_ust_app_ctx(sock, ltctx);
50 ustctl_release_object(sock
, ua_event
->obj
);
56 * Delete ust app stream safely. RCU read lock must be held before calling
59 static void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
61 ustctl_release_object(sock
, stream
->obj
);
67 * Delete ust app channel safely. RCU read lock must be held before calling
70 static void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
)
73 struct cds_lfht_iter iter
;
74 struct ust_app_event
*ua_event
;
75 struct ltt_ust_stream
*stream
, *stmp
;
77 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
78 cds_list_del(&stream
->list
);
79 delete_ust_app_stream(sock
, stream
);
82 /* TODO : remove channel context */
83 //cds_lfht_for_each_entry(ltc->ctx, &iter, ltctx, node) {
84 // hashtable_del(ltc->ctx, &iter);
85 // delete_ust_app_ctx(sock, ltctx);
87 //ret = hashtable_destroy(ltc->ctx);
89 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
90 hashtable_del(ua_chan
->events
, &iter
);
91 delete_ust_app_event(sock
, ua_event
);
94 ret
= hashtable_destroy(ua_chan
->events
);
96 ERR("UST app destroy session hashtable failed");
99 ustctl_release_object(sock
, ua_chan
->obj
);
108 * Delete ust app session safely. RCU read lock must be held before calling
111 static void delete_ust_app_session(int sock
,
112 struct ust_app_session
*ua_sess
)
115 struct cds_lfht_iter iter
;
116 struct ust_app_channel
*ua_chan
;
118 if (ua_sess
->metadata
) {
119 ustctl_release_object(sock
, ua_sess
->metadata
->stream_obj
);
120 free(ua_sess
->metadata
->stream_obj
);
121 ustctl_release_object(sock
, ua_sess
->metadata
->obj
);
122 free(ua_sess
->metadata
->obj
);
125 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
126 hashtable_del(ua_sess
->channels
, &iter
);
127 delete_ust_app_channel(sock
, ua_chan
);
130 ret
= hashtable_destroy(ua_sess
->channels
);
132 ERR("UST app destroy session hashtable failed");
141 * Delete a traceable application structure from the global list. Never call
142 * this function outside of a call_rcu call.
144 static void delete_ust_app(struct ust_app
*app
)
147 struct cds_lfht_node
*node
;
148 struct cds_lfht_iter iter
;
149 struct ust_app_session
*ua_sess
;
154 /* Remove from key hash table */
155 node
= hashtable_lookup(ust_app_sock_key_map
,
156 (void *) ((unsigned long) app
->key
.sock
), sizeof(void *), &iter
);
158 /* Not suppose to happen */
159 ERR("UST app key %d not found in key hash table", app
->key
.sock
);
163 ret
= hashtable_del(ust_app_sock_key_map
, &iter
);
165 ERR("UST app unable to delete app sock %d from key hash table",
168 DBG2("UST app pair sock %d key %d deleted",
169 app
->key
.sock
, app
->key
.pid
);
172 /* Socket is already closed at this point */
174 /* Delete ust app sessions info */
175 sock
= app
->key
.sock
;
178 cds_lfht_for_each_entry(app
->sessions
, &iter
, ua_sess
, node
) {
179 hashtable_del(app
->sessions
, &iter
);
180 delete_ust_app_session(app
->key
.sock
, ua_sess
);
183 ret
= hashtable_destroy(app
->sessions
);
185 ERR("UST app destroy session hashtable failed");
190 * Wait until we have removed the key from the sock hash table
191 * before closing this socket, otherwise an application could
192 * re-use the socket ID and race with the teardown, using the
193 * same hash table entry.
197 DBG2("UST app pid %d deleted", app
->key
.pid
);
204 * URCU intermediate call to delete an UST app.
206 static void delete_ust_app_rcu(struct rcu_head
*head
)
208 struct cds_lfht_node
*node
=
209 caa_container_of(head
, struct cds_lfht_node
, head
);
210 struct ust_app
*app
=
211 caa_container_of(node
, struct ust_app
, node
);
217 * Find an ust_app using the sock and return it. RCU read side lock must be
218 * held before calling this helper function.
220 static struct ust_app
*find_app_by_sock(int sock
)
222 struct cds_lfht_node
*node
;
223 struct ust_app_key
*key
;
224 struct cds_lfht_iter iter
;
226 node
= hashtable_lookup(ust_app_sock_key_map
,
227 (void *)((unsigned long) sock
), sizeof(void *), &iter
);
229 DBG2("UST app find by sock %d key not found", sock
);
233 key
= caa_container_of(node
, struct ust_app_key
, node
);
235 node
= hashtable_lookup(ust_app_ht
,
236 (void *)((unsigned long) key
->pid
), sizeof(void *), &iter
);
238 DBG2("UST app find by sock %d not found", sock
);
241 return caa_container_of(node
, struct ust_app
, node
);
248 * Disable the specified event on to UST tracer for the UST session.
250 static int disable_ust_event(struct ust_app
*app
,
251 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
255 ret
= ustctl_disable(app
->key
.sock
, ua_event
->obj
);
257 ERR("UST app event %s disable failed for app (pid: %d) "
258 "and session handle %d with ret %d",
259 ua_event
->attr
.name
, app
->key
.pid
, ua_sess
->handle
, ret
);
263 DBG2("UST app event %s disabled successfully for app (pid: %d)",
264 ua_event
->attr
.name
, app
->key
.pid
);
271 * Disable the specified channel on to UST tracer for the UST session.
273 static int disable_ust_channel(struct ust_app
*app
,
274 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
278 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
280 ERR("UST app channel %s disable failed for app (pid: %d) "
281 "and session handle %d with ret %d",
282 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
286 ua_chan
->enabled
= 0;
288 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
289 ua_chan
->name
, app
->key
.pid
);
296 * Enable the specified channel on to UST tracer for the UST session.
298 static int enable_ust_channel(struct ust_app
*app
,
299 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
303 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
305 ERR("UST app channel %s enable failed for app (pid: %d) "
306 "and session handle %d with ret %d",
307 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
311 ua_chan
->enabled
= 1;
313 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
314 ua_chan
->name
, app
->key
.pid
);
321 * Open metadata onto the UST tracer for a UST session.
323 static int open_ust_metadata(struct ust_app
*app
,
324 struct ust_app_session
*ua_sess
)
327 struct lttng_ust_channel_attr uattr
;
329 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
330 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
331 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
332 uattr
.switch_timer_interval
=
333 ua_sess
->metadata
->attr
.switch_timer_interval
;
334 uattr
.read_timer_interval
=
335 ua_sess
->metadata
->attr
.read_timer_interval
;
336 uattr
.output
= ua_sess
->metadata
->attr
.output
;
338 /* UST tracer metadata creation */
339 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
340 &ua_sess
->metadata
->obj
);
342 ERR("UST app open metadata failed for app pid:%d",
352 * Create stream onto the UST tracer for a UST session.
354 static int create_ust_stream(struct ust_app
*app
,
355 struct ust_app_session
*ua_sess
)
359 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
360 &ua_sess
->metadata
->stream_obj
);
362 ERR("UST create metadata stream failed");
371 * Create the specified channel onto the UST tracer for a UST session.
373 static int create_ust_channel(struct ust_app
*app
,
374 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
378 /* TODO: remove cast and use lttng-ust-abi.h */
379 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
380 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
382 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
383 "and session handle %d with ret %d",
384 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
385 ua_sess
->handle
, ret
);
389 ua_chan
->handle
= ua_chan
->obj
->handle
;
390 ua_chan
->attr
.shm_fd
= ua_chan
->obj
->shm_fd
;
391 ua_chan
->attr
.wait_fd
= ua_chan
->obj
->wait_fd
;
392 ua_chan
->attr
.memory_map_size
= ua_chan
->obj
->memory_map_size
;
394 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
395 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
402 * Create the specified event onto the UST tracer for a UST session.
404 static int create_ust_event(struct ust_app
*app
,
405 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
406 struct ust_app_event
*ua_event
)
410 /* Create UST event on tracer */
411 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
414 ERR("Error ustctl create event %s for app pid: %d with ret %d",
415 ua_event
->attr
.name
, app
->key
.pid
, ret
);
419 ua_event
->handle
= ua_event
->obj
->handle
;
420 ua_event
->enabled
= 1;
422 DBG2("UST app event %s created successfully for pid:%d",
423 ua_event
->attr
.name
, app
->key
.pid
);
430 * Alloc new UST app session.
432 static struct ust_app_session
*alloc_ust_app_session(void)
434 struct ust_app_session
*ua_sess
;
436 /* Init most of the default value by allocating and zeroing */
437 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
438 if (ua_sess
== NULL
) {
443 ua_sess
->handle
= -1;
444 ua_sess
->channels
= hashtable_new_str(0);
453 * Alloc new UST app channel.
455 static struct ust_app_channel
*alloc_ust_app_channel(char *name
,
456 struct lttng_ust_channel
*attr
)
458 struct ust_app_channel
*ua_chan
;
460 /* Init most of the default value by allocating and zeroing */
461 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
462 if (ua_chan
== NULL
) {
467 /* Setup channel name */
468 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
469 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
471 ua_chan
->handle
= -1;
472 ua_chan
->ctx
= hashtable_new(0);
473 ua_chan
->events
= hashtable_new_str(0);
474 hashtable_node_init(&ua_chan
->node
, (void *) ua_chan
->name
,
475 strlen(ua_chan
->name
));
477 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
479 /* Copy attributes */
481 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
484 DBG3("UST app channel %s allocated", ua_chan
->name
);
493 * Alloc new UST app event.
495 static struct ust_app_event
*alloc_ust_app_event(char *name
,
496 struct lttng_ust_event
*attr
)
498 struct ust_app_event
*ua_event
;
500 /* Init most of the default value by allocating and zeroing */
501 ua_event
= zmalloc(sizeof(struct ust_app_event
));
502 if (ua_event
== NULL
) {
507 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
508 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
509 ua_event
->ctx
= hashtable_new(0);
510 hashtable_node_init(&ua_event
->node
, (void *) ua_event
->name
,
511 strlen(ua_event
->name
));
513 /* Copy attributes */
515 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
518 DBG3("UST app event %s allocated", ua_event
->name
);
527 * Copy data between an UST app event and a LTT event.
529 static void shadow_copy_event(struct ust_app_event
*ua_event
,
530 struct ltt_ust_event
*uevent
)
532 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
533 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
535 /* Copy event attributes */
536 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
538 /* TODO: support copy context */
542 * Copy data between an UST app channel and a LTT channel.
544 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
545 struct ltt_ust_channel
*uchan
)
547 struct cds_lfht_iter iter
;
548 struct cds_lfht_node
*ua_event_node
;
549 struct ltt_ust_event
*uevent
;
550 struct ust_app_event
*ua_event
;
552 DBG2("Shadow copy of UST app channel %s", ua_chan
->name
);
554 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
555 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
556 /* Copy event attributes */
557 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
559 /* TODO: support copy context */
561 /* Copy all events from ltt ust channel to ust app channel */
562 cds_lfht_for_each_entry(uchan
->events
, &iter
, uevent
, node
) {
563 struct cds_lfht_iter uiter
;
565 ua_event_node
= hashtable_lookup(ua_chan
->events
,
566 (void *) uevent
->attr
.name
, strlen(uevent
->attr
.name
),
568 if (ua_event_node
== NULL
) {
569 DBG2("UST event %s not found on shadow copy channel",
571 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
572 if (ua_event
== NULL
) {
575 shadow_copy_event(ua_event
, uevent
);
576 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
580 DBG3("Shadow copy channel done");
584 * Copy data between a UST app session and a regular LTT session.
586 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
587 struct ltt_ust_session
*usess
,
590 struct cds_lfht_node
*ua_chan_node
;
591 struct cds_lfht_iter iter
;
592 struct ltt_ust_channel
*uchan
;
593 struct ust_app_channel
*ua_chan
;
599 /* Get date and time for unique app path */
601 timeinfo
= localtime(&rawtime
);
602 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
604 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
606 ua_sess
->uid
= usess
->uid
;
608 ret
= snprintf(ua_sess
->path
, PATH_MAX
,
610 usess
->pathname
, app
->name
, app
->key
.pid
,
613 PERROR("asprintf UST shadow copy session");
614 /* TODO: We cannot return an error from here.. */
618 /* TODO: support all UST domain */
620 /* Iterate over all channels in global domain. */
621 cds_lfht_for_each_entry(usess
->domain_global
.channels
, &iter
,
623 struct cds_lfht_iter uiter
;
625 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
626 (void *)uchan
->name
, strlen(uchan
->name
),
628 if (ua_chan_node
!= NULL
) {
632 DBG2("Channel %s not found on shadow session copy, creating it",
634 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
635 if (ua_chan
== NULL
) {
636 /* malloc failed... continuing */
640 shadow_copy_channel(ua_chan
, uchan
);
641 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
646 * Lookup sesison wrapper.
649 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
650 struct ust_app
*app
, struct cds_lfht_iter
*iter
)
652 /* Get right UST app session from app */
653 (void) hashtable_lookup(app
->sessions
,
654 (void *) ((unsigned long) usess
->uid
), sizeof(void *),
659 * Return ust app session from the app session hashtable using the UST session
662 static struct ust_app_session
*lookup_session_by_app(
663 struct ltt_ust_session
*usess
, struct ust_app
*app
)
665 struct cds_lfht_iter iter
;
666 struct cds_lfht_node
*node
;
668 __lookup_session_by_app(usess
, app
, &iter
);
669 node
= hashtable_iter_get_node(&iter
);
674 return caa_container_of(node
, struct ust_app_session
, node
);
681 * Create a UST session onto the tracer of app and add it the session
684 * Return ust app session or NULL on error.
686 static struct ust_app_session
*create_ust_app_session(
687 struct ltt_ust_session
*usess
, struct ust_app
*app
)
690 struct ust_app_session
*ua_sess
;
692 ua_sess
= lookup_session_by_app(usess
, app
);
693 if (ua_sess
== NULL
) {
694 DBG2("UST app pid: %d session uid %d not found, creating it",
695 app
->key
.pid
, usess
->uid
);
696 ua_sess
= alloc_ust_app_session();
697 if (ua_sess
== NULL
) {
698 /* Only malloc can failed so something is really wrong */
701 shadow_copy_session(ua_sess
, usess
, app
);
704 if (ua_sess
->handle
== -1) {
705 ret
= ustctl_create_session(app
->key
.sock
);
707 ERR("Error creating session for app pid %d, sock %d",
708 app
->key
.pid
, app
->key
.sock
);
709 /* TODO: free() ua_sess */
713 DBG2("UST app ustctl create session handle %d", ret
);
714 ua_sess
->handle
= ret
;
716 /* Add ust app session to app's HT */
717 hashtable_node_init(&ua_sess
->node
,
718 (void *)((unsigned long) ua_sess
->uid
), sizeof(void *));
719 hashtable_add_unique(app
->sessions
, &ua_sess
->node
);
721 DBG2("UST app session created successfully with handle %d", ret
);
731 * Disable on the tracer side a ust app event for the session and channel.
733 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
734 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
,
739 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
744 ua_event
->enabled
= 0;
751 * Lookup ust app channel for session and disable it on the tracer side.
753 static int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
754 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
757 struct cds_lfht_iter iter
;
758 struct cds_lfht_node
*ua_chan_node
;
759 struct ust_app_channel
*ua_chan
;
761 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
762 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
763 if (ua_chan_node
== NULL
) {
764 DBG2("Unable to find channel %s in ust session uid %u",
765 uchan
->name
, ua_sess
->uid
);
769 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
771 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
781 * Lookup ust app channel for session and enable it on the tracer side.
783 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
784 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
787 struct cds_lfht_iter iter
;
788 struct cds_lfht_node
*ua_chan_node
;
789 struct ust_app_channel
*ua_chan
;
791 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
792 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
793 if (ua_chan_node
== NULL
) {
794 DBG2("Unable to find channel %s in ust session uid %u",
795 uchan
->name
, ua_sess
->uid
);
799 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
801 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
811 * Create UST app channel and create it on the tracer.
813 static struct ust_app_channel
*create_ust_app_channel(
814 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
818 struct cds_lfht_iter iter
;
819 struct cds_lfht_node
*ua_chan_node
;
820 struct ust_app_channel
*ua_chan
;
822 /* Lookup channel in the ust app session */
823 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
824 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
825 if (ua_chan_node
== NULL
) {
826 DBG2("Unable to find channel %s in ust session uid %u",
827 uchan
->name
, ua_sess
->uid
);
828 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
829 if (ua_chan
== NULL
) {
832 shadow_copy_channel(ua_chan
, uchan
);
834 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
836 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
839 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
851 * Create UST app event and create it on the tracer side.
853 static struct ust_app_event
*create_ust_app_event(
854 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
855 struct ltt_ust_event
*uevent
, struct ust_app
*app
)
858 struct cds_lfht_iter iter
;
859 struct cds_lfht_node
*ua_event_node
;
860 struct ust_app_event
*ua_event
;
863 ua_event_node
= hashtable_lookup(ua_chan
->events
,
864 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
865 if (ua_event_node
== NULL
) {
866 DBG2("UST app event %s not found, creating it", uevent
->attr
.name
);
867 /* Does not exist so create one */
868 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
869 if (ua_event
== NULL
) {
870 /* Only malloc can failed so something is really wrong */
873 shadow_copy_event(ua_event
, uevent
);
875 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
877 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
880 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
892 * Create UST metadata and open it on the tracer side.
894 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
895 char *pathname
, struct ust_app
*app
)
899 if (ua_sess
->metadata
== NULL
) {
900 /* Allocate UST metadata */
901 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
902 if (ua_sess
->metadata
== NULL
) {
903 ERR("UST app session %d creating metadata failed",
908 ret
= open_ust_metadata(app
, ua_sess
);
913 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
916 /* Open UST metadata stream */
917 if (ua_sess
->metadata
->stream_obj
== NULL
) {
918 ret
= create_ust_stream(app
, ua_sess
);
923 ret
= mkdir(ua_sess
->path
, S_IRWXU
| S_IRWXG
);
925 PERROR("mkdir UST metadata");
929 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
930 "%s/metadata", ua_sess
->path
);
932 PERROR("asprintf UST create stream");
936 DBG2("UST metadata stream object created for app pid %d",
939 ERR("Attempting to create stream without metadata opened");
950 * Return pointer to traceable apps list.
952 struct cds_lfht
*ust_app_get_ht(void)
958 * Return ust app pointer or NULL if not found.
960 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
962 struct cds_lfht_node
*node
;
963 struct cds_lfht_iter iter
;
966 node
= hashtable_lookup(ust_app_ht
,
967 (void *)((unsigned long) pid
), sizeof(void *), &iter
);
969 DBG2("UST app no found with pid %d", pid
);
974 DBG2("Found UST app by pid %d", pid
);
976 return caa_container_of(node
, struct ust_app
, node
);
984 * Using pid and uid (of the app), allocate a new ust_app struct and
985 * add it to the global traceable app list.
987 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
988 * bitness is not supported.
990 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
994 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
995 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
996 ERR("Registration failed: application \"%s\" (pid: %d) has "
997 "%d-bit long, but no consumerd for this long size is available.\n",
998 msg
->name
, msg
->pid
, msg
->bits_per_long
);
1002 lta
= zmalloc(sizeof(struct ust_app
));
1008 lta
->ppid
= msg
->ppid
;
1009 lta
->uid
= msg
->uid
;
1010 lta
->gid
= msg
->gid
;
1011 lta
->bits_per_long
= msg
->bits_per_long
;
1012 lta
->v_major
= msg
->major
;
1013 lta
->v_minor
= msg
->minor
;
1014 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
1015 lta
->name
[16] = '\0';
1016 lta
->sessions
= hashtable_new(0);
1019 lta
->key
.pid
= msg
->pid
;
1020 hashtable_node_init(<a
->node
, (void *)((unsigned long)lta
->key
.pid
),
1022 lta
->key
.sock
= sock
;
1023 hashtable_node_init(<a
->key
.node
, (void *)((unsigned long)lta
->key
.sock
),
1027 hashtable_add_unique(ust_app_sock_key_map
, <a
->key
.node
);
1028 hashtable_add_unique(ust_app_ht
, <a
->node
);
1031 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1032 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
1033 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
1039 * Unregister app by removing it from the global traceable app list and freeing
1042 * The socket is already closed at this point so no close to sock.
1044 void ust_app_unregister(int sock
)
1046 struct ust_app
*lta
;
1047 struct cds_lfht_node
*node
;
1048 struct cds_lfht_iter iter
;
1051 lta
= find_app_by_sock(sock
);
1053 ERR("Unregister app sock %d not found!", sock
);
1057 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1059 /* Get the node reference for a call_rcu */
1060 node
= hashtable_lookup(ust_app_ht
,
1061 (void *)((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
1063 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1067 hashtable_del(ust_app_ht
, &iter
);
1068 call_rcu(&node
->head
, delete_ust_app_rcu
);
1075 * Return traceable_app_count
1077 unsigned long ust_app_list_count(void)
1079 unsigned long count
;
1082 count
= hashtable_get_count(ust_app_ht
);
1089 * Fill events array with all events name of all registered apps.
1091 int ust_app_list_events(struct lttng_event
**events
)
1094 size_t nbmem
, count
= 0;
1095 struct cds_lfht_iter iter
;
1096 struct ust_app
*app
;
1097 struct lttng_event
*tmp
;
1099 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1100 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1102 PERROR("zmalloc ust app events");
1109 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1110 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1112 ERR("UST app list events getting handle failed for app pid %d",
1117 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1118 tmp
[count
].name
)) != -ENOENT
) {
1119 if (count
> nbmem
) {
1120 DBG2("Reallocating event list from %zu to %zu bytes", nbmem
,
1121 nbmem
+ UST_APP_EVENT_LIST_SIZE
);
1122 nbmem
+= UST_APP_EVENT_LIST_SIZE
;
1123 tmp
= realloc(tmp
, nbmem
);
1125 PERROR("realloc ust app events");
1131 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1132 tmp
[count
].pid
= app
->key
.pid
;
1133 tmp
[count
].enabled
= -1;
1141 DBG2("UST app list events done (%zu events)", count
);
1150 * Free and clean all traceable apps of the global list.
1152 void ust_app_clean_list(void)
1155 struct cds_lfht_node
*node
;
1156 struct cds_lfht_iter iter
;
1157 struct ust_app
*app
;
1159 DBG2("UST app cleaning registered apps hash table");
1163 cds_lfht_for_each(ust_app_ht
, &iter
, node
) {
1164 app
= caa_container_of(node
, struct ust_app
, node
);
1166 ret
= hashtable_del(ust_app_ht
, &iter
);
1168 call_rcu(&node
->head
, delete_ust_app_rcu
);
1172 hashtable_destroy(ust_app_ht
);
1173 hashtable_destroy(ust_app_sock_key_map
);
1179 * Init UST app hash table.
1181 void ust_app_ht_alloc(void)
1183 ust_app_ht
= hashtable_new(0);
1184 ust_app_sock_key_map
= hashtable_new(0);
1188 * For a specific UST session, disable the channel for all registered apps.
1190 int ust_app_disable_channel_all(struct ltt_ust_session
*usess
,
1191 struct ltt_ust_channel
*uchan
)
1194 struct cds_lfht_iter iter
;
1195 struct ust_app
*app
;
1196 struct ust_app_session
*ua_sess
;
1198 if (usess
== NULL
|| uchan
== NULL
) {
1199 ERR("Disabling UST global channel with NULL values");
1204 DBG2("UST app disablling channel %s from global domain for session uid %d",
1205 uchan
->name
, usess
->uid
);
1209 /* For every registered applications */
1210 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1211 ua_sess
= lookup_session_by_app(usess
, app
);
1212 if (ua_sess
== NULL
) {
1216 /* Create channel onto application */
1217 ret
= disable_ust_app_channel(ua_sess
, uchan
, app
);
1219 /* XXX: We might want to report this error at some point... */
1231 * For a specific UST session, enable the channel for all registered apps.
1233 int ust_app_enable_channel_all(struct ltt_ust_session
*usess
,
1234 struct ltt_ust_channel
*uchan
)
1237 struct cds_lfht_iter iter
;
1238 struct ust_app
*app
;
1239 struct ust_app_session
*ua_sess
;
1241 if (usess
== NULL
|| uchan
== NULL
) {
1242 ERR("Adding UST global channel to NULL values");
1247 DBG2("UST app enabling channel %s to global domain for session uid %d",
1248 uchan
->name
, usess
->uid
);
1252 /* For every registered applications */
1253 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1254 ua_sess
= lookup_session_by_app(usess
, app
);
1255 if (ua_sess
== NULL
) {
1259 /* Enable channel onto application */
1260 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
1262 /* XXX: We might want to report this error at some point... */
1274 * For a specific UST session and UST channel, create the event for all
1277 int ust_app_disable_event_all(struct ltt_ust_session
*usess
,
1278 struct ltt_ust_channel
*uchan
)
1281 struct cds_lfht_iter iter
, uiter
;
1282 struct cds_lfht_node
*ua_chan_node
;
1283 struct ust_app
*app
;
1284 struct ust_app_session
*ua_sess
;
1285 struct ust_app_channel
*ua_chan
;
1286 struct ust_app_event
*ua_event
;
1288 DBG("UST app disabling all event for all apps in channel "
1289 "%s for session uid %d", uchan
->name
, usess
->uid
);
1293 /* For all registered applications */
1294 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1295 ua_sess
= lookup_session_by_app(usess
, app
);
1296 if (ua_sess
== NULL
) {
1301 /* Lookup channel in the ust app session */
1302 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1303 (void *)uchan
->name
, strlen(uchan
->name
),
1305 if (ua_chan_node
== NULL
) {
1306 DBG2("Channel %s not found in session uid %d for app pid %d."
1307 "Skipping", uchan
->name
, app
->key
.pid
, usess
->uid
);
1310 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1312 /* Disable each events of channel */
1313 cds_lfht_for_each_entry(ua_chan
->events
, &uiter
, ua_event
, node
) {
1314 ret
= disable_ust_app_event(ua_sess
, ua_chan
, ua_event
, app
);
1316 /* XXX: Report error someday... */
1328 * For a specific UST session, create the channel for all registered apps.
1330 int ust_app_create_channel_all(struct ltt_ust_session
*usess
,
1331 struct ltt_ust_channel
*uchan
)
1334 struct cds_lfht_iter iter
;
1335 struct ust_app
*app
;
1336 struct ust_app_session
*ua_sess
;
1337 struct ust_app_channel
*ua_chan
;
1339 if (usess
== NULL
|| uchan
== NULL
) {
1340 ERR("Adding UST global channel to NULL values");
1345 DBG2("UST app adding channel %s to global domain for session uid %d",
1346 uchan
->name
, usess
->uid
);
1350 /* For every registered applications */
1351 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1352 /* Create session on the tracer side and add it to app session HT */
1353 ua_sess
= create_ust_app_session(usess
, app
);
1354 if (ua_sess
== NULL
) {
1358 /* Create channel onto application */
1359 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1360 if (ua_chan
== NULL
) {
1372 * For a specific UST session and UST channel, create the event for all
1375 int ust_app_create_event_all(struct ltt_ust_session
*usess
,
1376 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1379 struct cds_lfht_iter iter
;
1380 struct cds_lfht_node
*ua_chan_node
;
1381 struct ust_app
*app
;
1382 struct ust_app_session
*ua_sess
;
1383 struct ust_app_channel
*ua_chan
;
1384 struct ust_app_event
*ua_event
;
1386 DBG("UST app creating event %s for all apps for session uid %d",
1387 uevent
->attr
.name
, usess
->uid
);
1391 /* For all registered applications */
1392 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1393 struct cds_lfht_iter uiter
;
1395 /* Create session on the tracer side and add it to app session HT */
1396 ua_sess
= create_ust_app_session(usess
, app
);
1397 if (ua_sess
== NULL
) {
1401 /* Lookup channel in the ust app session */
1402 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1403 (void *)uchan
->name
, strlen(uchan
->name
),
1405 if (ua_chan_node
== NULL
) {
1406 ERR("Channel %s not found in session uid %d. Skipping",
1407 uchan
->name
, usess
->uid
);
1410 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1412 ua_event
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1413 if (ua_event
== NULL
) {
1424 * Start tracing for a specific UST session and app.
1426 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1429 struct cds_lfht_iter iter
;
1430 struct ust_app_session
*ua_sess
;
1431 struct ust_app_channel
*ua_chan
;
1432 struct ltt_ust_stream
*ustream
;
1435 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1439 ua_sess
= lookup_session_by_app(usess
, app
);
1440 if (ua_sess
== NULL
) {
1441 /* Only malloc can failed so something is really wrong */
1442 goto error_rcu_unlock
;
1445 /* Upon restart, we skip the setup, already done */
1446 if (ua_sess
->started
) {
1450 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1452 goto error_rcu_unlock
;
1455 /* For each channel */
1456 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1457 /* Create all streams */
1459 /* Create UST stream */
1460 ustream
= zmalloc(sizeof(*ustream
));
1461 if (ustream
== NULL
) {
1462 PERROR("zmalloc ust stream");
1463 goto error_rcu_unlock
;
1466 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1469 /* Got all streams */
1472 ustream
->handle
= ustream
->obj
->handle
;
1474 /* Order is important */
1475 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1476 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1477 ua_sess
->path
, ua_chan
->name
,
1478 ua_chan
->streams
.count
++);
1480 PERROR("asprintf UST create stream");
1483 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1488 switch (app
->bits_per_long
) {
1490 consumerd_fd
= ust_consumerd64_fd
;
1493 consumerd_fd
= ust_consumerd32_fd
;
1497 goto error_rcu_unlock
;
1500 /* Setup UST consumer socket and send fds to it */
1501 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1503 goto error_rcu_unlock
;
1505 ua_sess
->started
= 1;
1508 /* This start the UST tracing */
1509 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1511 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1512 goto error_rcu_unlock
;
1517 /* Quiescent wait after starting trace */
1518 ustctl_wait_quiescent(app
->key
.sock
);
1528 * Stop tracing for a specific UST session and app.
1530 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1533 struct ust_app_session
*ua_sess
;
1535 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1539 ua_sess
= lookup_session_by_app(usess
, app
);
1540 if (ua_sess
== NULL
) {
1541 /* Only malloc can failed so something is really wrong */
1542 goto error_rcu_unlock
;
1545 #if 0 /* only useful when periodical flush will be supported */
1546 /* need to keep a handle on shm in session for this. */
1547 /* Flush all buffers before stopping */
1548 ret
= ustctl_flush_buffer(usess
->sock
, usess
->metadata
->obj
);
1550 ERR("UST metadata flush failed");
1553 cds_list_for_each_entry(ustchan
, &usess
->channels
.head
, list
) {
1554 ret
= ustctl_flush_buffer(usess
->sock
, ustchan
->obj
);
1556 ERR("UST flush buffer error");
1561 /* This inhibits UST tracing */
1562 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1564 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1565 goto error_rcu_unlock
;
1570 /* Quiescent wait after stopping trace */
1571 ustctl_wait_quiescent(app
->key
.sock
);
1581 * Destroy a specific UST session in apps.
1583 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1585 struct ust_app_session
*ua_sess
;
1586 struct lttng_ust_object_data obj
;
1587 struct cds_lfht_iter iter
;
1588 struct cds_lfht_node
*node
;
1590 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
1594 __lookup_session_by_app(usess
, app
, &iter
);
1595 node
= hashtable_iter_get_node(&iter
);
1597 /* Only malloc can failed so something is really wrong */
1598 goto error_rcu_unlock
;
1600 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
1601 hashtable_del(app
->sessions
, &iter
);
1602 delete_ust_app_session(app
->key
.sock
, ua_sess
);
1603 obj
.handle
= ua_sess
->handle
;
1606 obj
.memory_map_size
= 0;
1607 ustctl_release_object(app
->key
.sock
, &obj
);
1611 /* Quiescent wait after stopping trace */
1612 ustctl_wait_quiescent(app
->key
.sock
);
1622 * Start tracing for the UST session.
1624 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
1627 struct cds_lfht_iter iter
;
1628 struct ust_app
*app
;
1630 DBG("Starting all UST traces");
1634 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1635 ret
= ust_app_start_trace(usess
, app
);
1637 /* Continue to next apps even on error */
1648 * Start tracing for the UST session.
1650 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
1653 struct cds_lfht_iter iter
;
1654 struct ust_app
*app
;
1656 DBG("Stopping all UST traces");
1660 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1661 ret
= ust_app_stop_trace(usess
, app
);
1663 /* Continue to next apps even on error */
1674 * Destroy app UST session.
1676 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
1679 struct cds_lfht_iter iter
;
1680 struct ust_app
*app
;
1682 DBG("Destroy all UST traces");
1686 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1687 ret
= ust_app_destroy_trace(usess
, app
);
1689 /* Continue to next apps even on error */
1700 * Add channels/events from UST global domain to registered apps at sock.
1702 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
1705 struct cds_lfht_iter iter
;
1706 struct ust_app
*app
;
1707 struct ust_app_session
*ua_sess
;
1708 struct ust_app_channel
*ua_chan
;
1709 struct ust_app_event
*ua_event
;
1711 if (usess
== NULL
) {
1712 ERR("No UST session on global update. Returning");
1716 DBG2("UST app global update for app sock %d for session uid %d", sock
,
1721 app
= find_app_by_sock(sock
);
1723 ERR("Failed to update app sock %d", sock
);
1727 ua_sess
= create_ust_app_session(usess
, app
);
1728 if (ua_sess
== NULL
) {
1733 * We can iterate safely here over all UST app session sicne the create ust
1734 * app session above made a shadow copy of the UST global domain from the
1737 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1738 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1740 /* FIXME: Should we quit here or continue... */
1744 /* For each events */
1745 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
1746 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1748 /* FIXME: Should we quit here or continue... */
1754 if (usess
->start_trace
) {
1755 ret
= ust_app_start_trace(usess
, app
);
1760 DBG2("UST trace started for app pid %d", app
->key
.pid
);