2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <sys/types.h>
30 #include <lttng-share.h>
32 #include "hashtable.h"
34 #include "ust-consumer.h"
38 * Delete ust app event safely. RCU read lock must be held before calling
41 static void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
43 /* TODO : remove context */
44 //struct ust_app_ctx *ltctx;
45 //cds_lfht_for_each_entry(lte->ctx, &iter, ltctx, node) {
46 // delete_ust_app_ctx(sock, ltctx);
49 ustctl_release_object(sock
, ua_event
->obj
);
54 * Delete ust app stream safely. RCU read lock must be held before calling
57 static void delete_ust_app_stream(int sock
, struct ltt_ust_stream
*stream
)
60 //stream is used for passing to consumer.
61 //send_channel_streams is responsible for freeing the streams.
62 //note that this will not play well with flight recorder mode:
63 //we might need a criterion to discard the streams.
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 delete_ust_app_stream(sock
, stream
);
81 /* TODO : remove channel context */
82 //cds_lfht_for_each_entry(ltc->ctx, &iter, ltctx, node) {
83 // hashtable_del(ltc->ctx, &iter);
84 // delete_ust_app_ctx(sock, ltctx);
86 //ret = hashtable_destroy(ltc->ctx);
88 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
89 hashtable_del(ua_chan
->events
, &iter
);
90 delete_ust_app_event(sock
, ua_event
);
93 ret
= hashtable_destroy(ua_chan
->events
);
95 ERR("UST app destroy session hashtable failed");
104 * Delete ust app session safely. RCU read lock must be held before calling
107 static void delete_ust_app_session(int sock
,
108 struct ust_app_session
*ua_sess
)
111 struct cds_lfht_iter iter
;
112 struct ust_app_channel
*ua_chan
;
114 if (ua_sess
->metadata
) {
116 * We do NOT release the stream object and metadata object since they
117 * are release when fds are sent to the consumer.
121 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
122 hashtable_del(ua_sess
->channels
, &iter
);
123 delete_ust_app_channel(sock
, ua_chan
);
126 ret
= hashtable_destroy(ua_sess
->channels
);
128 ERR("UST app destroy session hashtable failed");
137 * Delete a traceable application structure from the global list.
139 static void delete_ust_app(struct ust_app
*lta
)
142 struct cds_lfht_node
*node
;
143 struct cds_lfht_iter iter
;
144 struct ust_app_session
*lts
;
148 /* Remove from apps hash table */
149 node
= hashtable_lookup(ust_app_ht
,
150 (void *) ((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
152 ERR("UST app pid %d not found in hash table", lta
->key
.pid
);
155 ret
= hashtable_del(ust_app_ht
, &iter
);
157 ERR("UST app unable to delete app %d from hash table",
160 DBG2("UST app pid %d deleted", lta
->key
.pid
);
164 /* Remove from key hash table */
165 node
= hashtable_lookup(ust_app_sock_key_map
,
166 (void *) ((unsigned long) lta
->key
.sock
), sizeof(void *), &iter
);
168 ERR("UST app key %d not found in key hash table", lta
->key
.sock
);
171 ret
= hashtable_del(ust_app_sock_key_map
, &iter
);
173 ERR("UST app unable to delete app sock %d from key hash table",
176 DBG2("UST app pair sock %d key %d deleted",
177 lta
->key
.sock
, lta
->key
.pid
);
181 /* Socket is already closed at this point */
183 /* Delete ust app sessions info */
184 if (lta
->sock_closed
) {
188 cds_lfht_for_each_entry(lta
->sessions
, &iter
, lts
, node
) {
189 hashtable_del(lta
->sessions
, &iter
);
190 delete_ust_app_session(lta
->key
.sock
, lts
);
193 ret
= hashtable_destroy(lta
->sessions
);
195 ERR("UST app destroy session hashtable failed");
199 if (lta
->key
.sock
>= 0) {
200 close(lta
->key
.sock
);
209 * URCU intermediate call to delete an UST app.
211 static void delete_ust_app_rcu(struct rcu_head
*head
)
213 struct cds_lfht_node
*node
=
214 caa_container_of(head
, struct cds_lfht_node
, head
);
215 struct ust_app
*app
=
216 caa_container_of(node
, struct ust_app
, node
);
222 * Find an ust_app using the sock and return it. RCU read side lock must be
223 * held before calling this helper function.
225 static struct ust_app
*find_app_by_sock(int sock
)
227 struct cds_lfht_node
*node
;
228 struct ust_app_key
*key
;
229 struct cds_lfht_iter iter
;
231 node
= hashtable_lookup(ust_app_sock_key_map
,
232 (void *)((unsigned long) sock
), sizeof(void *), &iter
);
234 DBG2("UST app find by sock %d key not found", sock
);
238 key
= caa_container_of(node
, struct ust_app_key
, node
);
240 node
= hashtable_lookup(ust_app_ht
,
241 (void *)((unsigned long) key
->pid
), sizeof(void *), &iter
);
243 DBG2("UST app find by sock %d not found", sock
);
246 return caa_container_of(node
, struct ust_app
, node
);
253 * Return pointer to traceable apps list.
255 struct cds_lfht
*ust_app_get_ht(void)
261 * Return ust app pointer or NULL if not found.
263 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
265 struct cds_lfht_node
*node
;
266 struct cds_lfht_iter iter
;
269 node
= hashtable_lookup(ust_app_ht
,
270 (void *)((unsigned long) pid
), sizeof(void *), &iter
);
272 DBG2("UST app no found with pid %d", pid
);
277 DBG2("Found UST app by pid %d", pid
);
279 return caa_container_of(node
, struct ust_app
, node
);
287 * Using pid and uid (of the app), allocate a new ust_app struct and
288 * add it to the global traceable app list.
290 * On success, return 0, else return malloc ENOMEM.
292 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
296 lta
= zmalloc(sizeof(struct ust_app
));
304 lta
->key
.pid
= msg
->pid
;
305 lta
->ppid
= msg
->ppid
;
306 lta
->v_major
= msg
->major
;
307 lta
->v_minor
= msg
->minor
;
308 lta
->key
.sock
= sock
;
309 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
310 lta
->name
[16] = '\0';
311 hashtable_node_init(<a
->node
, (void *)((unsigned long)lta
->key
.pid
),
314 /* Session hashtable */
315 lta
->sessions
= hashtable_new(0);
317 /* Set sock key map */
318 hashtable_node_init(<a
->key
.node
, (void *)((unsigned long)lta
->key
.sock
),
322 hashtable_add_unique(ust_app_ht
, <a
->node
);
323 hashtable_add_unique(ust_app_sock_key_map
, <a
->key
.node
);
326 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
327 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
328 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
334 * Unregister app by removing it from the global traceable app list and freeing
337 * The socket is already closed at this point so no close to sock.
339 void ust_app_unregister(int sock
)
342 struct cds_lfht_node
*node
;
343 struct cds_lfht_iter iter
;
346 lta
= find_app_by_sock(sock
);
348 ERR("Unregister app sock %d not found!", sock
);
352 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
354 /* Get the node reference for a call_rcu */
355 node
= hashtable_lookup(ust_app_ht
,
356 (void *)((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
358 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
362 /* We got called because the socket was closed on the remote end. */
364 /* Using a flag because we still need "sock" as a key. */
365 lta
->sock_closed
= 1;
366 call_rcu(&node
->head
, delete_ust_app_rcu
);
373 * Return traceable_app_count
375 unsigned long ust_app_list_count(void)
380 count
= hashtable_get_count(ust_app_ht
);
387 * Fill events array with all events name of all registered apps.
389 int ust_app_list_events(struct lttng_event
**events
)
392 size_t nbmem
, count
= 0;
393 struct cds_lfht_iter iter
;
395 struct lttng_event
*tmp
;
397 nbmem
= UST_APP_EVENT_LIST_SIZE
;
398 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
400 PERROR("zmalloc ust app events");
407 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
408 handle
= ustctl_tracepoint_list(app
->key
.sock
);
410 ERR("UST app list events getting handle failed for app pid %d",
415 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
416 tmp
[count
].name
)) != -ENOENT
) {
418 DBG2("Reallocating event list from %zu to %zu bytes", nbmem
,
419 nbmem
+ UST_APP_EVENT_LIST_SIZE
);
420 nbmem
+= UST_APP_EVENT_LIST_SIZE
;
421 tmp
= realloc(tmp
, nbmem
);
423 PERROR("realloc ust app events");
429 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
430 tmp
[count
].pid
= app
->key
.pid
;
438 DBG2("UST app list events done (%zu events)", count
);
447 * Free and clean all traceable apps of the global list.
449 void ust_app_clean_list(void)
452 struct cds_lfht_node
*node
;
453 struct cds_lfht_iter iter
;
455 DBG2("UST app clean hash table");
459 hashtable_get_first(ust_app_ht
, &iter
);
460 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
461 ret
= hashtable_del(ust_app_ht
, &iter
);
463 call_rcu(&node
->head
, delete_ust_app_rcu
);
465 hashtable_get_next(ust_app_ht
, &iter
);
472 * Init UST app hash table.
474 void ust_app_ht_alloc(void)
476 ust_app_ht
= hashtable_new(0);
477 ust_app_sock_key_map
= hashtable_new(0);
481 * Alloc new UST app session.
483 static struct ust_app_session
*alloc_ust_app_session(void)
485 struct ust_app_session
*ua_sess
;
487 /* Init most of the default value by allocating and zeroing */
488 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
489 if (ua_sess
== NULL
) {
494 ua_sess
->handle
= -1;
495 ua_sess
->channels
= hashtable_new_str(0);
504 * Alloc new UST app channel.
506 static struct ust_app_channel
*alloc_ust_app_channel(char *name
)
508 struct ust_app_channel
*ua_chan
;
510 /* Init most of the default value by allocating and zeroing */
511 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
512 if (ua_chan
== NULL
) {
517 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
518 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
519 ua_chan
->handle
= -1;
520 ua_chan
->ctx
= hashtable_new(0);
521 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
522 ua_chan
->events
= hashtable_new_str(0);
523 hashtable_node_init(&ua_chan
->node
, (void *) ua_chan
->name
,
524 strlen(ua_chan
->name
));
526 DBG3("UST app channel %s allocated", ua_chan
->name
);
535 * Alloc new UST app event.
537 static struct ust_app_event
*alloc_ust_app_event(char *name
)
539 struct ust_app_event
*ua_event
;
541 /* Init most of the default value by allocating and zeroing */
542 ua_event
= zmalloc(sizeof(struct ust_app_event
));
543 if (ua_event
== NULL
) {
548 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
549 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
550 ua_event
->ctx
= hashtable_new(0);
551 hashtable_node_init(&ua_event
->node
, (void *) ua_event
->name
,
552 strlen(ua_event
->name
));
554 DBG3("UST app event %s allocated", ua_event
->name
);
562 static void shadow_copy_event(struct ust_app_event
*ua_event
,
563 struct ltt_ust_event
*uevent
)
565 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
566 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
568 /* TODO: support copy context */
571 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
572 struct ltt_ust_channel
*uchan
)
574 struct cds_lfht_iter iter
;
575 struct cds_lfht_node
*node
, *ua_event_node
;
576 struct ltt_ust_event
*uevent
;
577 struct ust_app_event
*ua_event
;
579 DBG2("Shadow copy of UST app channel %s", ua_chan
->name
);
581 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
582 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
584 /* TODO: support copy context */
586 /* Copy all events from ltt ust channel to ust app channel */
587 hashtable_get_first(uchan
->events
, &iter
);
588 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
589 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
591 ua_event_node
= hashtable_lookup(ua_chan
->events
,
592 (void *) uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
593 if (ua_event_node
== NULL
) {
594 DBG2("UST event %s not found on shadow copy channel",
596 ua_event
= alloc_ust_app_event(uevent
->attr
.name
);
597 if (ua_event
== NULL
) {
600 shadow_copy_event(ua_event
, uevent
);
601 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
605 /* Get next UST events */
606 hashtable_get_next(uchan
->events
, &iter
);
609 DBG3("Shadow copy channel done");
612 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
613 struct ltt_ust_session
*usess
)
615 struct cds_lfht_node
*node
, *ua_chan_node
;
616 struct cds_lfht_iter iter
;
617 struct ltt_ust_channel
*uchan
;
618 struct ust_app_channel
*ua_chan
;
620 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
622 ua_sess
->uid
= usess
->uid
;
624 /* TODO: support all UST domain */
626 /* Iterate over all channels in global domain. */
627 hashtable_get_first(usess
->domain_global
.channels
, &iter
);
628 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
629 uchan
= caa_container_of(node
, struct ltt_ust_channel
, node
);
631 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
632 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
633 if (ua_chan_node
== NULL
) {
634 DBG2("Channel %s not found on shadow session copy, creating it",
636 ua_chan
= alloc_ust_app_channel(uchan
->name
);
637 if (ua_chan
== NULL
) {
638 /* malloc failed... continuing */
642 shadow_copy_channel(ua_chan
, uchan
);
643 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
647 /* Next item in hash table */
648 hashtable_get_next(usess
->domain_global
.channels
, &iter
);
653 * Return ust app session from the app session hashtable using the UST session
656 static struct ust_app_session
*lookup_session_by_app(
657 struct ltt_ust_session
*usess
, struct ust_app
*app
)
659 struct cds_lfht_iter iter
;
660 struct cds_lfht_node
*node
;
662 /* Get right UST app session from app */
663 node
= hashtable_lookup(app
->sessions
,
664 (void *) ((unsigned long) usess
->uid
), sizeof(void *), &iter
);
669 return caa_container_of(node
, struct ust_app_session
, node
);
676 * Create a UST session onto the tracer of app and add it the session
679 * Return ust app session or NULL on error.
681 static struct ust_app_session
*create_ust_app_session(
682 struct ltt_ust_session
*usess
, struct ust_app
*app
)
685 struct ust_app_session
*ua_sess
;
687 ua_sess
= lookup_session_by_app(usess
, app
);
688 if (ua_sess
== NULL
) {
689 DBG2("UST app pid: %d session uid %d not found, creating it",
690 app
->key
.pid
, usess
->uid
);
691 ua_sess
= alloc_ust_app_session();
692 if (ua_sess
== NULL
) {
693 /* Only malloc can failed so something is really wrong */
696 shadow_copy_session(ua_sess
, usess
);
699 if (ua_sess
->handle
== -1) {
700 ret
= ustctl_create_session(app
->key
.sock
);
702 ERR("Error creating session for app pid %d, sock %d",
703 app
->key
.pid
, app
->key
.sock
);
704 /* TODO: free() ua_sess */
708 DBG2("UST app ustctl create session handle %d", ret
);
709 ua_sess
->handle
= ret
;
711 /* Add ust app session to app's HT */
712 hashtable_node_init(&ua_sess
->node
,
713 (void *)((unsigned long) ua_sess
->uid
), sizeof(void *));
714 hashtable_add_unique(app
->sessions
, &ua_sess
->node
);
716 DBG2("UST app session created successfully with handle %d", ret
);
725 static struct ust_app_channel
*create_ust_app_channel(
726 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
730 struct cds_lfht_iter iter
;
731 struct cds_lfht_node
*ua_chan_node
;
732 struct ust_app_channel
*ua_chan
;
734 /* Lookup channel in the ust app session */
735 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
736 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
737 if (ua_chan_node
== NULL
) {
738 DBG2("Unable to find channel %s in ust session uid %u",
739 uchan
->name
, ua_sess
->uid
);
740 ua_chan
= alloc_ust_app_channel(uchan
->name
);
741 if (ua_chan
== NULL
) {
744 shadow_copy_channel(ua_chan
, uchan
);
745 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
747 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
750 /* TODO: remove cast and use lttng-ust-abi.h */
751 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
752 (struct lttng_ust_channel_attr
*)&uchan
->attr
, &ua_chan
->obj
);
754 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
755 "and session handle %d with ret %d",
756 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
757 ua_sess
->handle
, ret
);
761 ua_chan
->handle
= ua_chan
->obj
->handle
;
762 ua_chan
->attr
.shm_fd
= ua_chan
->obj
->shm_fd
;
763 ua_chan
->attr
.wait_fd
= ua_chan
->obj
->wait_fd
;
764 ua_chan
->attr
.memory_map_size
= ua_chan
->obj
->memory_map_size
;
766 DBG2("Channel %s UST create successfully for pid:%d and sock:%d",
767 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
775 static struct ust_app_event
*create_ust_app_event(
776 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
777 struct ltt_ust_event
*uevent
, struct ust_app
*app
)
780 struct cds_lfht_iter iter
;
781 struct cds_lfht_node
*ua_event_node
;
782 struct ust_app_event
*ua_event
;
785 ua_event_node
= hashtable_lookup(ua_chan
->events
,
786 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
787 if (ua_event_node
== NULL
) {
788 DBG2("UST app event %s not found, creating it", uevent
->attr
.name
);
789 /* Does not exist so create one */
790 ua_event
= alloc_ust_app_event(uevent
->attr
.name
);
791 if (ua_event
== NULL
) {
792 /* Only malloc can failed so something is really wrong */
795 shadow_copy_event(ua_event
, uevent
);
797 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
799 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
802 /* Create UST event on tracer */
803 ret
= ustctl_create_event(app
->key
.sock
, &uevent
->attr
, ua_chan
->obj
,
806 ERR("Error ustctl create event %s for app pid: %d with ret %d",
807 uevent
->attr
.name
, app
->key
.pid
, ret
);
808 /* TODO: free() ua_event */
811 ua_event
->handle
= ua_event
->obj
->handle
;
812 ua_event
->enabled
= 1;
815 DBG2("Event %s UST create successfully for pid:%d", uevent
->attr
.name
,
824 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
825 char *pathname
, struct ust_app
*app
)
828 struct lttng_ust_channel_attr uattr
;
830 if (ua_sess
->metadata
== NULL
) {
831 /* Allocate UST metadata */
832 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
833 if (ua_sess
->metadata
== NULL
) {
834 ERR("UST app session %d creating metadata failed",
839 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
840 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
841 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
842 uattr
.switch_timer_interval
=
843 ua_sess
->metadata
->attr
.switch_timer_interval
;
844 uattr
.read_timer_interval
=
845 ua_sess
->metadata
->attr
.read_timer_interval
;
846 uattr
.output
= ua_sess
->metadata
->attr
.output
;
848 /* UST tracer metadata creation */
849 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
850 &ua_sess
->metadata
->obj
);
852 ERR("UST app open metadata failed for app pid:%d",
857 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
860 /* Open UST metadata stream */
861 if (ua_sess
->metadata
->stream_obj
== NULL
) {
862 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
863 &ua_sess
->metadata
->stream_obj
);
865 ERR("UST create metadata stream failed");
869 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
, "%s/%s-%d",
870 pathname
, app
->name
, app
->key
.pid
);
872 PERROR("asprintf UST create stream");
876 ret
= mkdir(ua_sess
->metadata
->pathname
, S_IRWXU
| S_IRWXG
);
878 PERROR("mkdir UST metadata");
882 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
, "%s/%s-%d/metadata",
883 pathname
, app
->name
, app
->key
.pid
);
885 PERROR("asprintf UST create stream");
889 DBG2("UST metadata stream object created for app pid %d",
900 * Add channel to all ust app session.
902 int ust_app_add_channel_all(struct ltt_ust_session
*usess
,
903 struct ltt_ust_channel
*uchan
)
906 struct cds_lfht_iter iter
;
907 struct cds_lfht_node
*node
;
909 struct ust_app_session
*ua_sess
;
910 struct ust_app_channel
*ua_chan
;
912 if (usess
== NULL
|| uchan
== NULL
) {
913 ERR("Adding UST global channel to NULL values");
918 DBG2("UST app adding channel %s to global domain for session uid %d",
919 uchan
->name
, usess
->uid
);
923 /* For every UST applications registered */
924 hashtable_get_first(ust_app_ht
, &iter
);
925 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
926 app
= caa_container_of(node
, struct ust_app
, node
);
928 /* Create session on the tracer side and add it to app session HT */
929 ua_sess
= create_ust_app_session(usess
, app
);
930 if (ua_sess
== NULL
) {
934 /* Create channel onto application */
935 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
936 if (ua_chan
== NULL
) {
941 /* Next applications */
942 hashtable_get_next(ust_app_ht
, &iter
);
950 int ust_app_add_event_all(struct ltt_ust_session
*usess
,
951 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
954 struct cds_lfht_iter iter
;
955 struct cds_lfht_node
*node
, *ua_chan_node
;
957 struct ust_app_session
*ua_sess
;
958 struct ust_app_channel
*ua_chan
;
959 struct ust_app_event
*ua_event
;
961 DBG2("UST app adding event %s to global domain for session uid %d",
962 uevent
->attr
.name
, usess
->uid
);
966 /* For all registered applications */
967 hashtable_get_first(ust_app_ht
, &iter
);
968 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
969 app
= caa_container_of(node
, struct ust_app
, node
);
971 /* Create session on the tracer side and add it to app session HT */
972 ua_sess
= create_ust_app_session(usess
, app
);
973 if (ua_sess
== NULL
) {
977 /* Lookup channel in the ust app session */
978 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
979 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
980 if (ua_chan_node
== NULL
) {
981 ERR("Channel %s not found in session uid %d. Skipping",
982 uchan
->name
, usess
->uid
);
985 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
987 ua_event
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
988 if (ua_event
== NULL
) {
993 /* Next applications */
994 hashtable_get_next(ust_app_ht
, &iter
);
1001 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1004 struct cds_lfht_iter iter
;
1005 struct cds_lfht_node
*node
;
1006 struct ust_app_session
*ua_sess
;
1007 struct ust_app_channel
*ua_chan
;
1009 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1013 ua_sess
= lookup_session_by_app(usess
, app
);
1014 if (ua_sess
== NULL
) {
1015 /* Only malloc can failed so something is really wrong */
1016 goto error_rcu_unlock
;
1019 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1021 goto error_rcu_unlock
;
1024 /* For each channel */
1025 hashtable_get_first(ua_sess
->channels
, &iter
);
1026 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
1027 ua_chan
= caa_container_of(node
, struct ust_app_channel
, node
);
1029 /* Create all streams */
1031 struct ltt_ust_stream
*ustream
;
1033 ustream
= zmalloc(sizeof(*ustream
));
1034 if (ustream
== NULL
) {
1035 PERROR("zmalloc ust stream");
1036 goto error_rcu_unlock
;
1039 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1042 /* Got all streams */
1045 ustream
->handle
= ustream
->obj
->handle
;
1047 /* Order is important */
1048 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1049 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s-%d/%s_%u",
1050 usess
->pathname
, app
->name
, app
->key
.pid
,
1051 ua_chan
->name
, ua_chan
->streams
.count
++);
1053 PERROR("asprintf UST create stream");
1056 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1060 /* Next applications */
1061 hashtable_get_next(ua_sess
->channels
, &iter
);
1064 /* Setup UST consumer socket and send fds to it */
1065 ret
= ust_consumer_send_session(usess
->consumer_fd
, ua_sess
);
1067 goto error_rcu_unlock
;
1070 /* This start the UST tracing */
1071 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1073 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1074 goto error_rcu_unlock
;
1078 /* Quiescent wait after starting trace */
1079 ustctl_wait_quiescent(app
->key
.sock
);
1088 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
1091 struct cds_lfht_iter iter
;
1092 struct cds_lfht_node
*node
;
1093 struct ust_app
*app
;
1095 DBG("Starting all UST traces");
1098 hashtable_get_first(ust_app_ht
, &iter
);
1099 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
1100 app
= caa_container_of(node
, struct ust_app
, node
);
1102 ret
= ust_app_start_trace(usess
, app
);
1108 /* Next applications */
1109 hashtable_get_next(ust_app_ht
, &iter
);
1116 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
1119 struct cds_lfht_iter iter
;
1120 struct cds_lfht_node
*node
;
1121 struct ust_app
*app
;
1122 struct ust_app_session
*ua_sess
;
1123 struct ust_app_channel
*ua_chan
;
1124 struct ust_app_event
*ua_event
;
1125 struct ltt_ust_channel
*uchan
;
1126 struct ltt_ust_event
*uevent
;
1130 if (usess
== NULL
) {
1131 DBG2("No UST session on global update. Returning");
1135 DBG2("UST app global update for app sock %d for session uid %d", sock
,
1138 app
= find_app_by_sock(sock
);
1140 ERR("Failed to update app sock %d", sock
);
1144 ua_sess
= create_ust_app_session(usess
, app
);
1145 if (ua_sess
== NULL
) {
1149 hashtable_get_first(usess
->domain_global
.channels
, &iter
);
1150 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
1151 uchan
= caa_container_of(node
, struct ltt_ust_channel
, node
);
1153 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1154 if (ua_chan
== NULL
) {
1158 hashtable_get_first(uchan
->events
, &iter
);
1159 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
1160 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
1162 ua_event
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1163 if (ua_event
== NULL
) {
1168 hashtable_get_next(uchan
->events
, &iter
);
1172 /* Next item in hash table */
1173 hashtable_get_next(usess
->domain_global
.channels
, &iter
);
1176 if (usess
->start_trace
) {
1177 ret
= ust_app_start_trace(usess
, app
);
1182 DBG2("UST trace started for app pid %d", app
->key
.pid
);