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 channel on to UST tracer for the UST session.
250 static int disable_ust_channel(struct ust_app
*app
,
251 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
255 ret
= ustctl_disable(app
->key
.sock
, ua_chan
->obj
);
257 ERR("UST app channel %s disable failed for app (pid: %d) "
258 "and session handle %d with ret %d",
259 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
263 ua_chan
->enabled
= 0;
265 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
266 ua_chan
->name
, app
->key
.pid
);
273 * Enable the specified channel on to UST tracer for the UST session.
275 static int enable_ust_channel(struct ust_app
*app
,
276 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
280 ret
= ustctl_enable(app
->key
.sock
, ua_chan
->obj
);
282 ERR("UST app channel %s enable failed for app (pid: %d) "
283 "and session handle %d with ret %d",
284 ua_chan
->name
, app
->key
.pid
, ua_sess
->handle
, ret
);
288 ua_chan
->enabled
= 1;
290 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
291 ua_chan
->name
, app
->key
.pid
);
298 * Open metadata onto the UST tracer for a UST session.
300 static int open_ust_metadata(struct ust_app
*app
,
301 struct ust_app_session
*ua_sess
)
304 struct lttng_ust_channel_attr uattr
;
306 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
307 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
308 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
309 uattr
.switch_timer_interval
=
310 ua_sess
->metadata
->attr
.switch_timer_interval
;
311 uattr
.read_timer_interval
=
312 ua_sess
->metadata
->attr
.read_timer_interval
;
313 uattr
.output
= ua_sess
->metadata
->attr
.output
;
315 /* UST tracer metadata creation */
316 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
317 &ua_sess
->metadata
->obj
);
319 ERR("UST app open metadata failed for app pid:%d",
329 * Create stream onto the UST tracer for a UST session.
331 static int create_ust_stream(struct ust_app
*app
,
332 struct ust_app_session
*ua_sess
)
336 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
337 &ua_sess
->metadata
->stream_obj
);
339 ERR("UST create metadata stream failed");
348 * Create the specified channel onto the UST tracer for a UST session.
350 static int create_ust_channel(struct ust_app
*app
,
351 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
355 /* TODO: remove cast and use lttng-ust-abi.h */
356 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
357 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
359 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
360 "and session handle %d with ret %d",
361 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
362 ua_sess
->handle
, ret
);
366 ua_chan
->handle
= ua_chan
->obj
->handle
;
367 ua_chan
->attr
.shm_fd
= ua_chan
->obj
->shm_fd
;
368 ua_chan
->attr
.wait_fd
= ua_chan
->obj
->wait_fd
;
369 ua_chan
->attr
.memory_map_size
= ua_chan
->obj
->memory_map_size
;
371 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
372 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
379 * Create the specified event onto the UST tracer for a UST session.
381 static int create_ust_event(struct ust_app
*app
,
382 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
383 struct ust_app_event
*ua_event
)
387 /* Create UST event on tracer */
388 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
391 ERR("Error ustctl create event %s for app pid: %d with ret %d",
392 ua_event
->attr
.name
, app
->key
.pid
, ret
);
396 ua_event
->handle
= ua_event
->obj
->handle
;
397 ua_event
->enabled
= 1;
399 DBG2("UST app event %s created successfully for pid:%d",
400 ua_event
->attr
.name
, app
->key
.pid
);
407 * Alloc new UST app session.
409 static struct ust_app_session
*alloc_ust_app_session(void)
411 struct ust_app_session
*ua_sess
;
413 /* Init most of the default value by allocating and zeroing */
414 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
415 if (ua_sess
== NULL
) {
420 ua_sess
->handle
= -1;
421 ua_sess
->channels
= hashtable_new_str(0);
430 * Alloc new UST app channel.
432 static struct ust_app_channel
*alloc_ust_app_channel(char *name
,
433 struct lttng_ust_channel
*attr
)
435 struct ust_app_channel
*ua_chan
;
437 /* Init most of the default value by allocating and zeroing */
438 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
439 if (ua_chan
== NULL
) {
444 /* Setup channel name */
445 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
446 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
448 ua_chan
->handle
= -1;
449 ua_chan
->ctx
= hashtable_new(0);
450 ua_chan
->events
= hashtable_new_str(0);
451 hashtable_node_init(&ua_chan
->node
, (void *) ua_chan
->name
,
452 strlen(ua_chan
->name
));
454 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
456 /* Copy attributes */
458 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
461 DBG3("UST app channel %s allocated", ua_chan
->name
);
470 * Alloc new UST app event.
472 static struct ust_app_event
*alloc_ust_app_event(char *name
,
473 struct lttng_ust_event
*attr
)
475 struct ust_app_event
*ua_event
;
477 /* Init most of the default value by allocating and zeroing */
478 ua_event
= zmalloc(sizeof(struct ust_app_event
));
479 if (ua_event
== NULL
) {
484 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
485 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
486 ua_event
->ctx
= hashtable_new(0);
487 hashtable_node_init(&ua_event
->node
, (void *) ua_event
->name
,
488 strlen(ua_event
->name
));
490 /* Copy attributes */
492 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
495 DBG3("UST app event %s allocated", ua_event
->name
);
504 * Copy data between an UST app event and a LTT event.
506 static void shadow_copy_event(struct ust_app_event
*ua_event
,
507 struct ltt_ust_event
*uevent
)
509 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
510 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
512 /* Copy event attributes */
513 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
515 /* TODO: support copy context */
519 * Copy data between an UST app channel and a LTT channel.
521 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
522 struct ltt_ust_channel
*uchan
)
524 struct cds_lfht_iter iter
;
525 struct cds_lfht_node
*ua_event_node
;
526 struct ltt_ust_event
*uevent
;
527 struct ust_app_event
*ua_event
;
529 DBG2("Shadow copy of UST app channel %s", ua_chan
->name
);
531 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
532 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
533 /* Copy event attributes */
534 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
536 /* TODO: support copy context */
538 /* Copy all events from ltt ust channel to ust app channel */
539 cds_lfht_for_each_entry(uchan
->events
, &iter
, uevent
, node
) {
540 struct cds_lfht_iter uiter
;
542 ua_event_node
= hashtable_lookup(ua_chan
->events
,
543 (void *) uevent
->attr
.name
, strlen(uevent
->attr
.name
),
545 if (ua_event_node
== NULL
) {
546 DBG2("UST event %s not found on shadow copy channel",
548 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
549 if (ua_event
== NULL
) {
552 shadow_copy_event(ua_event
, uevent
);
553 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
557 DBG3("Shadow copy channel done");
561 * Copy data between a UST app session and a regular LTT session.
563 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
564 struct ltt_ust_session
*usess
,
567 struct cds_lfht_node
*ua_chan_node
;
568 struct cds_lfht_iter iter
;
569 struct ltt_ust_channel
*uchan
;
570 struct ust_app_channel
*ua_chan
;
576 /* Get date and time for unique app path */
578 timeinfo
= localtime(&rawtime
);
579 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
581 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
583 ua_sess
->uid
= usess
->uid
;
585 ret
= snprintf(ua_sess
->path
, PATH_MAX
,
587 usess
->pathname
, app
->name
, app
->key
.pid
,
590 PERROR("asprintf UST shadow copy session");
591 /* TODO: We cannot return an error from here.. */
595 /* TODO: support all UST domain */
597 /* Iterate over all channels in global domain. */
598 cds_lfht_for_each_entry(usess
->domain_global
.channels
, &iter
,
600 struct cds_lfht_iter uiter
;
602 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
603 (void *)uchan
->name
, strlen(uchan
->name
),
605 if (ua_chan_node
!= NULL
) {
609 DBG2("Channel %s not found on shadow session copy, creating it",
611 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
612 if (ua_chan
== NULL
) {
613 /* malloc failed... continuing */
617 shadow_copy_channel(ua_chan
, uchan
);
618 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
623 * Lookup sesison wrapper.
626 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
627 struct ust_app
*app
, struct cds_lfht_iter
*iter
)
629 /* Get right UST app session from app */
630 (void) hashtable_lookup(app
->sessions
,
631 (void *) ((unsigned long) usess
->uid
), sizeof(void *),
636 * Return ust app session from the app session hashtable using the UST session
639 static struct ust_app_session
*lookup_session_by_app(
640 struct ltt_ust_session
*usess
, struct ust_app
*app
)
642 struct cds_lfht_iter iter
;
643 struct cds_lfht_node
*node
;
645 __lookup_session_by_app(usess
, app
, &iter
);
646 node
= hashtable_iter_get_node(&iter
);
651 return caa_container_of(node
, struct ust_app_session
, node
);
658 * Create a UST session onto the tracer of app and add it the session
661 * Return ust app session or NULL on error.
663 static struct ust_app_session
*create_ust_app_session(
664 struct ltt_ust_session
*usess
, struct ust_app
*app
)
667 struct ust_app_session
*ua_sess
;
669 ua_sess
= lookup_session_by_app(usess
, app
);
670 if (ua_sess
== NULL
) {
671 DBG2("UST app pid: %d session uid %d not found, creating it",
672 app
->key
.pid
, usess
->uid
);
673 ua_sess
= alloc_ust_app_session();
674 if (ua_sess
== NULL
) {
675 /* Only malloc can failed so something is really wrong */
678 shadow_copy_session(ua_sess
, usess
, app
);
681 if (ua_sess
->handle
== -1) {
682 ret
= ustctl_create_session(app
->key
.sock
);
684 ERR("Error creating session for app pid %d, sock %d",
685 app
->key
.pid
, app
->key
.sock
);
686 /* TODO: free() ua_sess */
690 DBG2("UST app ustctl create session handle %d", ret
);
691 ua_sess
->handle
= ret
;
693 /* Add ust app session to app's HT */
694 hashtable_node_init(&ua_sess
->node
,
695 (void *)((unsigned long) ua_sess
->uid
), sizeof(void *));
696 hashtable_add_unique(app
->sessions
, &ua_sess
->node
);
698 DBG2("UST app session created successfully with handle %d", ret
);
708 * Lookup ust app channel for session and disable it on the tracer side.
710 static int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
711 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
714 struct cds_lfht_iter iter
;
715 struct cds_lfht_node
*ua_chan_node
;
716 struct ust_app_channel
*ua_chan
;
718 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
719 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
720 if (ua_chan_node
== NULL
) {
721 DBG2("Unable to find channel %s in ust session uid %u",
722 uchan
->name
, ua_sess
->uid
);
726 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
728 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
738 * Lookup ust app channel for session and enable it on the tracer side.
740 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
741 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
744 struct cds_lfht_iter iter
;
745 struct cds_lfht_node
*ua_chan_node
;
746 struct ust_app_channel
*ua_chan
;
748 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
749 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
750 if (ua_chan_node
== NULL
) {
751 DBG2("Unable to find channel %s in ust session uid %u",
752 uchan
->name
, ua_sess
->uid
);
756 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
758 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
768 * Create UST app channel and create it on the tracer.
770 static struct ust_app_channel
*create_ust_app_channel(
771 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
775 struct cds_lfht_iter iter
;
776 struct cds_lfht_node
*ua_chan_node
;
777 struct ust_app_channel
*ua_chan
;
779 /* Lookup channel in the ust app session */
780 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
781 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
782 if (ua_chan_node
== NULL
) {
783 DBG2("Unable to find channel %s in ust session uid %u",
784 uchan
->name
, ua_sess
->uid
);
785 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
786 if (ua_chan
== NULL
) {
789 shadow_copy_channel(ua_chan
, uchan
);
791 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
793 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
796 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
808 * Create UST app event and create it on the tracer side.
810 static struct ust_app_event
*create_ust_app_event(
811 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
812 struct ltt_ust_event
*uevent
, struct ust_app
*app
)
815 struct cds_lfht_iter iter
;
816 struct cds_lfht_node
*ua_event_node
;
817 struct ust_app_event
*ua_event
;
820 ua_event_node
= hashtable_lookup(ua_chan
->events
,
821 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
822 if (ua_event_node
== NULL
) {
823 DBG2("UST app event %s not found, creating it", uevent
->attr
.name
);
824 /* Does not exist so create one */
825 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
826 if (ua_event
== NULL
) {
827 /* Only malloc can failed so something is really wrong */
830 shadow_copy_event(ua_event
, uevent
);
832 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
834 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
837 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
849 * Create UST metadata and open it on the tracer side.
851 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
852 char *pathname
, struct ust_app
*app
)
856 if (ua_sess
->metadata
== NULL
) {
857 /* Allocate UST metadata */
858 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
859 if (ua_sess
->metadata
== NULL
) {
860 ERR("UST app session %d creating metadata failed",
865 ret
= open_ust_metadata(app
, ua_sess
);
870 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
873 /* Open UST metadata stream */
874 if (ua_sess
->metadata
->stream_obj
== NULL
) {
875 ret
= create_ust_stream(app
, ua_sess
);
880 ret
= mkdir(ua_sess
->path
, S_IRWXU
| S_IRWXG
);
882 PERROR("mkdir UST metadata");
886 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
887 "%s/metadata", ua_sess
->path
);
889 PERROR("asprintf UST create stream");
893 DBG2("UST metadata stream object created for app pid %d",
896 ERR("Attempting to create stream without metadata opened");
907 * Return pointer to traceable apps list.
909 struct cds_lfht
*ust_app_get_ht(void)
915 * Return ust app pointer or NULL if not found.
917 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
919 struct cds_lfht_node
*node
;
920 struct cds_lfht_iter iter
;
923 node
= hashtable_lookup(ust_app_ht
,
924 (void *)((unsigned long) pid
), sizeof(void *), &iter
);
926 DBG2("UST app no found with pid %d", pid
);
931 DBG2("Found UST app by pid %d", pid
);
933 return caa_container_of(node
, struct ust_app
, node
);
941 * Using pid and uid (of the app), allocate a new ust_app struct and
942 * add it to the global traceable app list.
944 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
945 * bitness is not supported.
947 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
951 if ((msg
->bits_per_long
== 64 && ust_consumerd64_fd
== -EINVAL
)
952 || (msg
->bits_per_long
== 32 && ust_consumerd32_fd
== -EINVAL
)) {
953 ERR("Registration failed: application \"%s\" (pid: %d) has "
954 "%d-bit long, but no consumerd for this long size is available.\n",
955 msg
->name
, msg
->pid
, msg
->bits_per_long
);
959 lta
= zmalloc(sizeof(struct ust_app
));
965 lta
->ppid
= msg
->ppid
;
968 lta
->bits_per_long
= msg
->bits_per_long
;
969 lta
->v_major
= msg
->major
;
970 lta
->v_minor
= msg
->minor
;
971 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
972 lta
->name
[16] = '\0';
973 lta
->sessions
= hashtable_new(0);
976 lta
->key
.pid
= msg
->pid
;
977 hashtable_node_init(<a
->node
, (void *)((unsigned long)lta
->key
.pid
),
979 lta
->key
.sock
= sock
;
980 hashtable_node_init(<a
->key
.node
, (void *)((unsigned long)lta
->key
.sock
),
984 hashtable_add_unique(ust_app_sock_key_map
, <a
->key
.node
);
985 hashtable_add_unique(ust_app_ht
, <a
->node
);
988 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
989 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
990 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
996 * Unregister app by removing it from the global traceable app list and freeing
999 * The socket is already closed at this point so no close to sock.
1001 void ust_app_unregister(int sock
)
1003 struct ust_app
*lta
;
1004 struct cds_lfht_node
*node
;
1005 struct cds_lfht_iter iter
;
1008 lta
= find_app_by_sock(sock
);
1010 ERR("Unregister app sock %d not found!", sock
);
1014 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
1016 /* Get the node reference for a call_rcu */
1017 node
= hashtable_lookup(ust_app_ht
,
1018 (void *)((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
1020 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
1024 hashtable_del(ust_app_ht
, &iter
);
1025 call_rcu(&node
->head
, delete_ust_app_rcu
);
1032 * Return traceable_app_count
1034 unsigned long ust_app_list_count(void)
1036 unsigned long count
;
1039 count
= hashtable_get_count(ust_app_ht
);
1046 * Fill events array with all events name of all registered apps.
1048 int ust_app_list_events(struct lttng_event
**events
)
1051 size_t nbmem
, count
= 0;
1052 struct cds_lfht_iter iter
;
1053 struct ust_app
*app
;
1054 struct lttng_event
*tmp
;
1056 nbmem
= UST_APP_EVENT_LIST_SIZE
;
1057 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
1059 PERROR("zmalloc ust app events");
1066 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1067 handle
= ustctl_tracepoint_list(app
->key
.sock
);
1069 ERR("UST app list events getting handle failed for app pid %d",
1074 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
1075 tmp
[count
].name
)) != -ENOENT
) {
1076 if (count
> nbmem
) {
1077 DBG2("Reallocating event list from %zu to %zu bytes", nbmem
,
1078 nbmem
+ UST_APP_EVENT_LIST_SIZE
);
1079 nbmem
+= UST_APP_EVENT_LIST_SIZE
;
1080 tmp
= realloc(tmp
, nbmem
);
1082 PERROR("realloc ust app events");
1088 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
1089 tmp
[count
].pid
= app
->key
.pid
;
1090 tmp
[count
].enabled
= -1;
1098 DBG2("UST app list events done (%zu events)", count
);
1107 * Free and clean all traceable apps of the global list.
1109 void ust_app_clean_list(void)
1112 struct cds_lfht_node
*node
;
1113 struct cds_lfht_iter iter
;
1114 struct ust_app
*app
;
1116 DBG2("UST app cleaning registered apps hash table");
1120 cds_lfht_for_each(ust_app_ht
, &iter
, node
) {
1121 app
= caa_container_of(node
, struct ust_app
, node
);
1123 ret
= hashtable_del(ust_app_ht
, &iter
);
1125 call_rcu(&node
->head
, delete_ust_app_rcu
);
1129 hashtable_destroy(ust_app_ht
);
1130 hashtable_destroy(ust_app_sock_key_map
);
1136 * Init UST app hash table.
1138 void ust_app_ht_alloc(void)
1140 ust_app_ht
= hashtable_new(0);
1141 ust_app_sock_key_map
= hashtable_new(0);
1145 * For a specific UST session, disable the channel for all registered apps.
1147 int ust_app_disable_channel_all(struct ltt_ust_session
*usess
,
1148 struct ltt_ust_channel
*uchan
)
1151 struct cds_lfht_iter iter
;
1152 struct ust_app
*app
;
1153 struct ust_app_session
*ua_sess
;
1155 if (usess
== NULL
|| uchan
== NULL
) {
1156 ERR("Disabling UST global channel with NULL values");
1161 DBG2("UST app disablling channel %s from global domain for session uid %d",
1162 uchan
->name
, usess
->uid
);
1166 /* For every registered applications */
1167 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1168 ua_sess
= lookup_session_by_app(usess
, app
);
1169 if (ua_sess
== NULL
) {
1173 /* Create channel onto application */
1174 ret
= disable_ust_app_channel(ua_sess
, uchan
, app
);
1176 /* XXX: We might want to report this error at some point... */
1188 * For a specific UST session, enable the channel for all registered apps.
1190 int ust_app_enable_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("Adding UST global channel to NULL values");
1204 DBG2("UST app enabling channel %s to 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 /* Enable channel onto application */
1217 ret
= enable_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, create the channel for all registered apps.
1233 int ust_app_create_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
;
1240 struct ust_app_channel
*ua_chan
;
1242 if (usess
== NULL
|| uchan
== NULL
) {
1243 ERR("Adding UST global channel to NULL values");
1248 DBG2("UST app adding channel %s to global domain for session uid %d",
1249 uchan
->name
, usess
->uid
);
1253 /* For every registered applications */
1254 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1255 /* Create session on the tracer side and add it to app session HT */
1256 ua_sess
= create_ust_app_session(usess
, app
);
1257 if (ua_sess
== NULL
) {
1261 /* Create channel onto application */
1262 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1263 if (ua_chan
== NULL
) {
1275 * For a specific UST session and UST channel, create the event for all
1278 int ust_app_create_event_all(struct ltt_ust_session
*usess
,
1279 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1282 struct cds_lfht_iter iter
;
1283 struct cds_lfht_node
*ua_chan_node
;
1284 struct ust_app
*app
;
1285 struct ust_app_session
*ua_sess
;
1286 struct ust_app_channel
*ua_chan
;
1287 struct ust_app_event
*ua_event
;
1289 DBG("UST app creating event %s for all apps for session uid %d",
1290 uevent
->attr
.name
, usess
->uid
);
1294 /* For all registered applications */
1295 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1296 struct cds_lfht_iter uiter
;
1298 /* Create session on the tracer side and add it to app session HT */
1299 ua_sess
= create_ust_app_session(usess
, app
);
1300 if (ua_sess
== NULL
) {
1304 /* Lookup channel in the ust app session */
1305 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1306 (void *)uchan
->name
, strlen(uchan
->name
),
1308 if (ua_chan_node
== NULL
) {
1309 ERR("Channel %s not found in session uid %d. Skipping",
1310 uchan
->name
, usess
->uid
);
1313 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1315 ua_event
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1316 if (ua_event
== NULL
) {
1327 * Start tracing for a specific UST session and app.
1329 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1332 struct cds_lfht_iter iter
;
1333 struct ust_app_session
*ua_sess
;
1334 struct ust_app_channel
*ua_chan
;
1335 struct ltt_ust_stream
*ustream
;
1338 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1342 ua_sess
= lookup_session_by_app(usess
, app
);
1343 if (ua_sess
== NULL
) {
1344 /* Only malloc can failed so something is really wrong */
1345 goto error_rcu_unlock
;
1348 /* Upon restart, we skip the setup, already done */
1349 if (ua_sess
->started
) {
1353 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1355 goto error_rcu_unlock
;
1358 /* For each channel */
1359 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1360 /* Create all streams */
1362 /* Create UST stream */
1363 ustream
= zmalloc(sizeof(*ustream
));
1364 if (ustream
== NULL
) {
1365 PERROR("zmalloc ust stream");
1366 goto error_rcu_unlock
;
1369 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1372 /* Got all streams */
1375 ustream
->handle
= ustream
->obj
->handle
;
1377 /* Order is important */
1378 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1379 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1380 ua_sess
->path
, ua_chan
->name
,
1381 ua_chan
->streams
.count
++);
1383 PERROR("asprintf UST create stream");
1386 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1391 switch (app
->bits_per_long
) {
1393 consumerd_fd
= ust_consumerd64_fd
;
1396 consumerd_fd
= ust_consumerd32_fd
;
1400 goto error_rcu_unlock
;
1403 /* Setup UST consumer socket and send fds to it */
1404 ret
= ust_consumer_send_session(consumerd_fd
, ua_sess
);
1406 goto error_rcu_unlock
;
1408 ua_sess
->started
= 1;
1411 /* This start the UST tracing */
1412 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1414 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1415 goto error_rcu_unlock
;
1420 /* Quiescent wait after starting trace */
1421 ustctl_wait_quiescent(app
->key
.sock
);
1431 * Stop tracing for a specific UST session and app.
1433 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1436 struct ust_app_session
*ua_sess
;
1438 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1442 ua_sess
= lookup_session_by_app(usess
, app
);
1443 if (ua_sess
== NULL
) {
1444 /* Only malloc can failed so something is really wrong */
1445 goto error_rcu_unlock
;
1448 #if 0 /* only useful when periodical flush will be supported */
1449 /* need to keep a handle on shm in session for this. */
1450 /* Flush all buffers before stopping */
1451 ret
= ustctl_flush_buffer(usess
->sock
, usess
->metadata
->obj
);
1453 ERR("UST metadata flush failed");
1456 cds_list_for_each_entry(ustchan
, &usess
->channels
.head
, list
) {
1457 ret
= ustctl_flush_buffer(usess
->sock
, ustchan
->obj
);
1459 ERR("UST flush buffer error");
1464 /* This inhibits UST tracing */
1465 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1467 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1468 goto error_rcu_unlock
;
1473 /* Quiescent wait after stopping trace */
1474 ustctl_wait_quiescent(app
->key
.sock
);
1484 * Destroy a specific UST session in apps.
1486 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1488 struct ust_app_session
*ua_sess
;
1489 struct lttng_ust_object_data obj
;
1490 struct cds_lfht_iter iter
;
1491 struct cds_lfht_node
*node
;
1493 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
1497 __lookup_session_by_app(usess
, app
, &iter
);
1498 node
= hashtable_iter_get_node(&iter
);
1500 /* Only malloc can failed so something is really wrong */
1501 goto error_rcu_unlock
;
1503 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
1504 hashtable_del(app
->sessions
, &iter
);
1505 delete_ust_app_session(app
->key
.sock
, ua_sess
);
1506 obj
.handle
= ua_sess
->handle
;
1509 obj
.memory_map_size
= 0;
1510 ustctl_release_object(app
->key
.sock
, &obj
);
1514 /* Quiescent wait after stopping trace */
1515 ustctl_wait_quiescent(app
->key
.sock
);
1525 * Start tracing for the UST session.
1527 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
1530 struct cds_lfht_iter iter
;
1531 struct ust_app
*app
;
1533 DBG("Starting all UST traces");
1537 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1538 ret
= ust_app_start_trace(usess
, app
);
1540 /* Continue to next apps even on error */
1551 * Start tracing for the UST session.
1553 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
1556 struct cds_lfht_iter iter
;
1557 struct ust_app
*app
;
1559 DBG("Stopping all UST traces");
1563 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1564 ret
= ust_app_stop_trace(usess
, app
);
1566 /* Continue to next apps even on error */
1577 * Destroy app UST session.
1579 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
1582 struct cds_lfht_iter iter
;
1583 struct ust_app
*app
;
1585 DBG("Destroy all UST traces");
1589 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1590 ret
= ust_app_destroy_trace(usess
, app
);
1592 /* Continue to next apps even on error */
1603 * Add channels/events from UST global domain to registered apps at sock.
1605 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
1608 struct cds_lfht_iter iter
;
1609 struct ust_app
*app
;
1610 struct ust_app_session
*ua_sess
;
1611 struct ust_app_channel
*ua_chan
;
1612 struct ust_app_event
*ua_event
;
1614 if (usess
== NULL
) {
1615 ERR("No UST session on global update. Returning");
1619 DBG2("UST app global update for app sock %d for session uid %d", sock
,
1624 app
= find_app_by_sock(sock
);
1626 ERR("Failed to update app sock %d", sock
);
1630 ua_sess
= create_ust_app_session(usess
, app
);
1631 if (ua_sess
== NULL
) {
1636 * We can iterate safely here over all UST app session sicne the create ust
1637 * app session above made a shadow copy of the UST global domain from the
1640 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1641 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1643 /* FIXME: Should we quit here or continue... */
1647 /* For each events */
1648 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
1649 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1651 /* FIXME: Should we quit here or continue... */
1657 if (usess
->start_trace
) {
1658 ret
= ust_app_start_trace(usess
, app
);
1663 DBG2("UST trace started for app pid %d", app
->key
.pid
);