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 "../hashtable/hash.h"
36 #include "ust-consumer.h"
39 * Delete a traceable application structure from the global list.
41 static void delete_ust_app(struct ust_app
*lta
)
44 struct cds_lfht_node
*node
;
45 struct cds_lfht_iter iter
;
49 /* TODO: clean session hashtable */
53 /* Remove from apps hash table */
54 node
= hashtable_lookup(ust_app_ht
,
55 (void *) ((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
57 ERR("UST app pid %d not found in hash table", lta
->key
.pid
);
59 ret
= hashtable_del(ust_app_ht
, &iter
);
61 ERR("UST app unable to delete app %d from hash table",
64 DBG2("UST app pid %d deleted", lta
->key
.pid
);
68 /* Remove from key hash table */
69 node
= hashtable_lookup(ust_app_sock_key_map
,
70 (void *) ((unsigned long) lta
->key
.sock
), sizeof(void *), &iter
);
72 ERR("UST app key %d not found in key hash table", lta
->key
.sock
);
74 ret
= hashtable_del(ust_app_sock_key_map
, &iter
);
76 ERR("UST app unable to delete app sock %d from key hash table",
79 DBG2("UST app pair sock %d key %d deleted",
80 lta
->key
.sock
, lta
->key
.pid
);
90 * URCU intermediate call to delete an UST app.
92 static void delete_ust_app_rcu(struct rcu_head
*head
)
94 struct cds_lfht_node
*node
=
95 caa_container_of(head
, struct cds_lfht_node
, head
);
97 caa_container_of(node
, struct ust_app
, node
);
103 * Find an ust_app using the sock and return it. RCU read side lock must be
104 * held before calling this helper function.
106 static struct ust_app
*find_app_by_sock(int sock
)
108 struct cds_lfht_node
*node
;
109 struct ust_app_key
*key
;
110 struct cds_lfht_iter iter
;
112 node
= hashtable_lookup(ust_app_sock_key_map
,
113 (void *)((unsigned long) sock
), sizeof(void *), &iter
);
115 DBG2("UST app find by sock %d key not found", sock
);
119 key
= caa_container_of(node
, struct ust_app_key
, node
);
121 node
= hashtable_lookup(ust_app_ht
,
122 (void *)((unsigned long) key
->pid
), sizeof(void *), &iter
);
124 DBG2("UST app find by sock %d not found", sock
);
128 return caa_container_of(node
, struct ust_app
, node
);
135 * Return pointer to traceable apps list.
137 struct cds_lfht
*ust_app_get_ht(void)
143 * Return ust app pointer or NULL if not found.
145 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
147 struct cds_lfht_node
*node
;
148 struct cds_lfht_iter iter
;
151 node
= hashtable_lookup(ust_app_ht
,
152 (void *)((unsigned long) pid
), sizeof(void *), &iter
);
154 DBG2("UST app no found with pid %d", pid
);
159 DBG2("Found UST app by pid %d", pid
);
161 return caa_container_of(node
, struct ust_app
, node
);
169 * Using pid and uid (of the app), allocate a new ust_app struct and
170 * add it to the global traceable app list.
172 * On success, return 0, else return malloc ENOMEM.
174 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
178 lta
= malloc(sizeof(struct ust_app
));
186 lta
->key
.pid
= msg
->pid
;
187 lta
->ppid
= msg
->ppid
;
188 lta
->v_major
= msg
->major
;
189 lta
->v_minor
= msg
->minor
;
190 lta
->key
.sock
= sock
;
191 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
192 lta
->name
[16] = '\0';
193 hashtable_node_init(<a
->node
, (void *)((unsigned long)lta
->key
.pid
),
196 /* Session hashtable */
197 lta
->sessions
= hashtable_new(0);
199 /* Set sock key map */
200 hashtable_node_init(<a
->key
.node
, (void *)((unsigned long)lta
->key
.sock
),
204 hashtable_add_unique(ust_app_ht
, <a
->node
);
205 hashtable_add_unique(ust_app_sock_key_map
, <a
->key
.node
);
208 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
209 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
210 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
216 * Unregister app by removing it from the global traceable app list and freeing
219 * The socket is already closed at this point so no close to sock.
221 void ust_app_unregister(int sock
)
224 struct cds_lfht_node
*node
;
225 struct cds_lfht_iter iter
;
228 lta
= find_app_by_sock(sock
);
230 ERR("Unregister app sock %d not found!", sock
);
234 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
236 /* Get the node reference for a call_rcu */
237 node
= hashtable_lookup(ust_app_ht
,
238 (void *)((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
240 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
244 call_rcu(&node
->head
, delete_ust_app_rcu
);
252 * Return traceable_app_count
254 unsigned long ust_app_list_count(void)
259 count
= hashtable_get_count(ust_app_ht
);
266 * Fill events array with all events name of all registered apps.
268 int ust_app_list_events(struct lttng_event
**events
)
271 size_t nbmem
, count
= 0;
272 struct cds_lfht_iter iter
;
274 struct lttng_event
*tmp
;
276 nbmem
= UST_APP_EVENT_LIST_SIZE
;
277 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
279 PERROR("zmalloc ust app events");
286 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
287 handle
= ustctl_tracepoint_list(app
->key
.sock
);
289 ERR("UST app list events getting handle failed for app pid %d",
294 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
295 tmp
[count
].name
)) != -ENOENT
) {
297 DBG2("Reallocating event list from %zu to %zu bytes", nbmem
,
298 nbmem
+ UST_APP_EVENT_LIST_SIZE
);
299 nbmem
+= UST_APP_EVENT_LIST_SIZE
;
300 tmp
= realloc(tmp
, nbmem
);
302 PERROR("realloc ust app events");
308 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
309 tmp
[count
].pid
= app
->key
.pid
;
317 DBG2("UST app list events done (%zu events)", count
);
326 * Free and clean all traceable apps of the global list.
328 void ust_app_clean_list(void)
331 struct cds_lfht_node
*node
;
332 struct cds_lfht_iter iter
;
334 DBG2("UST app clean hash table");
338 hashtable_get_first(ust_app_ht
, &iter
);
339 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
340 ret
= hashtable_del(ust_app_ht
, &iter
);
342 call_rcu(&node
->head
, delete_ust_app_rcu
);
344 hashtable_get_next(ust_app_ht
, &iter
);
351 * Init UST app hash table.
353 void ust_app_ht_alloc(void)
355 ust_app_ht
= hashtable_new(0);
356 ust_app_sock_key_map
= hashtable_new(0);
360 * Alloc new UST app session.
362 static struct ust_app_session
*alloc_ust_app_session(void)
364 struct ust_app_session
*ua_sess
;
366 /* Init most of the default value by allocating and zeroing */
367 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
368 if (ua_sess
== NULL
) {
373 ua_sess
->handle
= -1;
374 ua_sess
->channels
= hashtable_new_str(0);
383 * Alloc new UST app channel.
385 static struct ust_app_channel
*alloc_ust_app_channel(char *name
)
387 struct ust_app_channel
*ua_chan
;
389 /* Init most of the default value by allocating and zeroing */
390 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
391 if (ua_chan
== NULL
) {
396 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
397 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
398 ua_chan
->handle
= -1;
399 ua_chan
->ctx
= hashtable_new(0);
400 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
401 ua_chan
->events
= hashtable_new_str(0);
402 hashtable_node_init(&ua_chan
->node
, (void *) ua_chan
->name
,
403 strlen(ua_chan
->name
));
405 DBG3("UST app channel %s allocated", ua_chan
->name
);
414 * Alloc new UST app event.
416 static struct ust_app_event
*alloc_ust_app_event(char *name
)
418 struct ust_app_event
*ua_event
;
420 /* Init most of the default value by allocating and zeroing */
421 ua_event
= zmalloc(sizeof(struct ust_app_event
));
422 if (ua_event
== NULL
) {
427 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
428 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
429 ua_event
->ctx
= hashtable_new(0);
430 hashtable_node_init(&ua_event
->node
, (void *) ua_event
->name
,
431 strlen(ua_event
->name
));
433 DBG3("UST app event %s allocated", ua_event
->name
);
441 static void shadow_copy_event(struct ust_app_event
*ua_event
,
442 struct ltt_ust_event
*uevent
)
444 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
445 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
447 /* TODO: support copy context */
450 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
451 struct ltt_ust_channel
*uchan
)
453 struct cds_lfht_iter iter
;
454 struct cds_lfht_node
*node
, *ua_event_node
;
455 struct ltt_ust_event
*uevent
;
456 struct ust_app_event
*ua_event
;
458 DBG2("Shadow copy of UST app channel %s", ua_chan
->name
);
460 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
461 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
463 /* TODO: support copy context */
465 /* Copy all events from ltt ust channel to ust app channel */
466 hashtable_get_first(uchan
->events
, &iter
);
467 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
468 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
470 ua_event_node
= hashtable_lookup(ua_chan
->events
,
471 (void *) uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
472 if (ua_event_node
== NULL
) {
473 DBG2("UST event %s not found on shadow copy channel",
475 ua_event
= alloc_ust_app_event(uevent
->attr
.name
);
476 if (ua_event
== NULL
) {
479 shadow_copy_event(ua_event
, uevent
);
480 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
484 /* Get next UST events */
485 hashtable_get_next(uchan
->events
, &iter
);
488 DBG3("Shadow copy channel done");
491 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
492 struct ltt_ust_session
*usess
)
494 struct cds_lfht_node
*node
, *ua_chan_node
;
495 struct cds_lfht_iter iter
;
496 struct ltt_ust_channel
*uchan
;
497 struct ust_app_channel
*ua_chan
;
499 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
501 ua_sess
->uid
= usess
->uid
;
503 /* TODO: support all UST domain */
505 /* Iterate over all channels in global domain. */
506 hashtable_get_first(usess
->domain_global
.channels
, &iter
);
507 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
508 uchan
= caa_container_of(node
, struct ltt_ust_channel
, node
);
510 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
511 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
512 if (ua_chan_node
== NULL
) {
513 DBG2("Channel %s not found on shadow session copy, creating it",
515 ua_chan
= alloc_ust_app_channel(uchan
->name
);
516 if (ua_chan
== NULL
) {
517 /* malloc failed... continuing */
521 shadow_copy_channel(ua_chan
, uchan
);
522 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
526 /* Next item in hash table */
527 hashtable_get_next(usess
->domain_global
.channels
, &iter
);
532 * Return ust app session from the app session hashtable using the UST session
535 static struct ust_app_session
*lookup_session_by_app(
536 struct ltt_ust_session
*usess
, struct ust_app
*app
)
538 struct cds_lfht_iter iter
;
539 struct cds_lfht_node
*node
;
541 /* Get right UST app session from app */
542 node
= hashtable_lookup(app
->sessions
,
543 (void *) ((unsigned long) usess
->uid
), sizeof(void *), &iter
);
548 return caa_container_of(node
, struct ust_app_session
, node
);
555 * Create a UST session onto the tracer of app and add it the session
558 * Return ust app session or NULL on error.
560 static struct ust_app_session
*create_ust_app_session(
561 struct ltt_ust_session
*usess
, struct ust_app
*app
)
564 struct ust_app_session
*ua_sess
;
566 ua_sess
= lookup_session_by_app(usess
, app
);
567 if (ua_sess
== NULL
) {
568 DBG2("UST app pid: %d session uid %d not found, creating it",
569 app
->key
.pid
, usess
->uid
);
570 ua_sess
= alloc_ust_app_session();
571 if (ua_sess
== NULL
) {
572 /* Only malloc can failed so something is really wrong */
575 shadow_copy_session(ua_sess
, usess
);
578 if (ua_sess
->handle
== -1) {
579 ret
= ustctl_create_session(app
->key
.sock
);
581 ERR("Error creating session for app pid %d, sock %d",
582 app
->key
.pid
, app
->key
.sock
);
583 /* TODO: free() ua_sess */
587 DBG2("UST app ustctl create session handle %d", ret
);
588 ua_sess
->handle
= ret
;
590 /* Add ust app session to app's HT */
591 hashtable_node_init(&ua_sess
->node
,
592 (void *)((unsigned long) ua_sess
->uid
), sizeof(void *));
593 hashtable_add_unique(app
->sessions
, &ua_sess
->node
);
595 DBG2("UST app session created successfully with handle %d", ret
);
604 static struct ust_app_channel
*create_ust_app_channel(
605 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
609 struct cds_lfht_iter iter
;
610 struct cds_lfht_node
*ua_chan_node
;
611 struct ust_app_channel
*ua_chan
;
613 /* Lookup channel in the ust app session */
614 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
615 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
616 if (ua_chan_node
== NULL
) {
617 DBG2("Unable to find channel %s in ust session uid %u",
618 uchan
->name
, ua_sess
->uid
);
619 ua_chan
= alloc_ust_app_channel(uchan
->name
);
620 if (ua_chan
== NULL
) {
623 shadow_copy_channel(ua_chan
, uchan
);
624 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
626 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
629 /* TODO: remove cast and use lttng-ust-abi.h */
630 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
631 (struct lttng_ust_channel_attr
*)&uchan
->attr
, &ua_chan
->obj
);
633 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
634 "and session handle %d with ret %d",
635 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
636 ua_sess
->handle
, ret
);
640 ua_chan
->handle
= ua_chan
->obj
->handle
;
641 ua_chan
->attr
.shm_fd
= ua_chan
->obj
->shm_fd
;
642 ua_chan
->attr
.wait_fd
= ua_chan
->obj
->wait_fd
;
643 ua_chan
->attr
.memory_map_size
= ua_chan
->obj
->memory_map_size
;
645 DBG2("Channel %s UST create successfully for pid:%d and sock:%d",
646 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
654 static struct ust_app_event
*create_ust_app_event(
655 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
656 struct ltt_ust_event
*uevent
, struct ust_app
*app
)
659 struct cds_lfht_iter iter
;
660 struct cds_lfht_node
*ua_event_node
;
661 struct ust_app_event
*ua_event
;
664 ua_event_node
= hashtable_lookup(ua_chan
->events
,
665 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
666 if (ua_event_node
== NULL
) {
667 DBG2("UST app event %s not found, creating it", uevent
->attr
.name
);
668 /* Does not exist so create one */
669 ua_event
= alloc_ust_app_event(uevent
->attr
.name
);
670 if (ua_event
== NULL
) {
671 /* Only malloc can failed so something is really wrong */
674 shadow_copy_event(ua_event
, uevent
);
676 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
678 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
681 /* Create UST event on tracer */
682 ret
= ustctl_create_event(app
->key
.sock
, &uevent
->attr
, ua_chan
->obj
,
685 ERR("Error ustctl create event %s for app pid: %d with ret %d",
686 uevent
->attr
.name
, app
->key
.pid
, ret
);
687 /* TODO: free() ua_event */
690 ua_event
->handle
= ua_event
->obj
->handle
;
691 ua_event
->enabled
= 1;
694 DBG2("Event %s UST create successfully for pid:%d", uevent
->attr
.name
,
703 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
704 char *pathname
, struct ust_app
*app
)
707 struct lttng_ust_channel_attr uattr
;
709 if (ua_sess
->metadata
== NULL
) {
710 /* Allocate UST metadata */
711 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
712 if (ua_sess
->metadata
== NULL
) {
713 ERR("UST app session %d creating metadata failed",
718 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
719 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
720 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
721 uattr
.switch_timer_interval
=
722 ua_sess
->metadata
->attr
.switch_timer_interval
;
723 uattr
.read_timer_interval
=
724 ua_sess
->metadata
->attr
.read_timer_interval
;
725 uattr
.output
= ua_sess
->metadata
->attr
.output
;
727 /* UST tracer metadata creation */
728 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
729 &ua_sess
->metadata
->obj
);
731 ERR("UST app open metadata failed for app pid:%d",
736 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
739 /* Open UST metadata stream */
740 if (ua_sess
->metadata
->stream_obj
== NULL
) {
741 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
742 &ua_sess
->metadata
->stream_obj
);
744 ERR("UST create metadata stream failed");
748 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
, "%s/%s-%d",
749 pathname
, app
->name
, app
->key
.pid
);
751 PERROR("asprintf UST create stream");
755 ret
= mkdir(ua_sess
->metadata
->pathname
, S_IRWXU
| S_IRWXG
);
757 PERROR("mkdir UST metadata");
761 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
, "%s/%s-%d/metadata",
762 pathname
, app
->name
, app
->key
.pid
);
764 PERROR("asprintf UST create stream");
768 DBG2("UST metadata stream object created for app pid %d",
779 * Add channel to all ust app session.
781 int ust_app_add_channel_all(struct ltt_ust_session
*usess
,
782 struct ltt_ust_channel
*uchan
)
785 struct cds_lfht_iter iter
;
786 struct cds_lfht_node
*node
;
788 struct ust_app_session
*ua_sess
;
789 struct ust_app_channel
*ua_chan
;
791 if (usess
== NULL
|| uchan
== NULL
) {
792 ERR("Adding UST global channel to NULL values");
797 DBG2("UST app adding channel %s to global domain for session uid %d",
798 uchan
->name
, usess
->uid
);
802 /* For every UST applications registered */
803 hashtable_get_first(ust_app_ht
, &iter
);
804 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
805 app
= caa_container_of(node
, struct ust_app
, node
);
807 /* Create session on the tracer side and add it to app session HT */
808 ua_sess
= create_ust_app_session(usess
, app
);
809 if (ua_sess
== NULL
) {
813 /* Create channel onto application */
814 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
815 if (ua_chan
== NULL
) {
820 /* Next applications */
821 hashtable_get_next(ust_app_ht
, &iter
);
829 int ust_app_add_event_all(struct ltt_ust_session
*usess
,
830 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
833 struct cds_lfht_iter iter
;
834 struct cds_lfht_node
*node
, *ua_chan_node
;
836 struct ust_app_session
*ua_sess
;
837 struct ust_app_channel
*ua_chan
;
838 struct ust_app_event
*ua_event
;
840 DBG2("UST app adding event %s to global domain for session uid %d",
841 uevent
->attr
.name
, usess
->uid
);
845 /* For all registered applications */
846 hashtable_get_first(ust_app_ht
, &iter
);
847 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
848 app
= caa_container_of(node
, struct ust_app
, node
);
850 /* Create session on the tracer side and add it to app session HT */
851 ua_sess
= create_ust_app_session(usess
, app
);
852 if (ua_sess
== NULL
) {
856 /* Lookup channel in the ust app session */
857 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
858 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
859 if (ua_chan_node
== NULL
) {
860 ERR("Channel %s not found in session uid %d. Skipping",
861 uchan
->name
, usess
->uid
);
864 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
866 ua_event
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
867 if (ua_event
== NULL
) {
872 /* Next applications */
873 hashtable_get_next(ust_app_ht
, &iter
);
880 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
883 struct cds_lfht_iter iter
;
884 struct cds_lfht_node
*node
;
885 struct ust_app_session
*ua_sess
;
886 struct ust_app_channel
*ua_chan
;
888 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
892 ua_sess
= lookup_session_by_app(usess
, app
);
893 if (ua_sess
== NULL
) {
894 /* Only malloc can failed so something is really wrong */
895 goto error_rcu_unlock
;
898 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
900 goto error_rcu_unlock
;
903 /* For each channel */
904 hashtable_get_first(ua_sess
->channels
, &iter
);
905 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
906 ua_chan
= caa_container_of(node
, struct ust_app_channel
, node
);
908 /* Create all streams */
910 struct ltt_ust_stream
*ustream
;
912 ustream
= zmalloc(sizeof(*ustream
));
913 if (ustream
== NULL
) {
914 PERROR("zmalloc ust stream");
915 goto error_rcu_unlock
;
918 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
921 /* Got all streams */
924 ustream
->handle
= ustream
->obj
->handle
;
926 /* Order is important */
927 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
928 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s-%d/%s_%u",
929 usess
->pathname
, app
->name
, app
->key
.pid
,
930 ua_chan
->name
, ua_chan
->streams
.count
++);
932 PERROR("asprintf UST create stream");
935 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
939 /* Next applications */
940 hashtable_get_next(ua_sess
->channels
, &iter
);
943 /* Setup UST consumer socket and send fds to it */
944 ret
= ust_consumer_send_session(usess
->consumer_fd
, ua_sess
);
946 goto error_rcu_unlock
;
949 /* This start the UST tracing */
950 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
952 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
953 goto error_rcu_unlock
;
957 /* Quiescent wait after starting trace */
958 ustctl_wait_quiescent(app
->key
.sock
);
967 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
970 struct cds_lfht_iter iter
;
971 struct cds_lfht_node
*node
;
974 DBG("Starting all UST traces");
977 hashtable_get_first(ust_app_ht
, &iter
);
978 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
979 app
= caa_container_of(node
, struct ust_app
, node
);
981 ret
= ust_app_start_trace(usess
, app
);
987 /* Next applications */
988 hashtable_get_next(ust_app_ht
, &iter
);
995 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
998 struct cds_lfht_iter iter
;
999 struct cds_lfht_node
*node
;
1000 struct ust_app
*app
;
1001 struct ust_app_session
*ua_sess
;
1002 struct ust_app_channel
*ua_chan
;
1003 struct ust_app_event
*ua_event
;
1004 struct ltt_ust_channel
*uchan
;
1005 struct ltt_ust_event
*uevent
;
1009 if (usess
== NULL
) {
1010 DBG2("No UST session on global update. Returning");
1014 DBG2("UST app global update for app sock %d for session uid %d", sock
,
1017 app
= find_app_by_sock(sock
);
1019 ERR("Failed to update app sock %d", sock
);
1023 ua_sess
= create_ust_app_session(usess
, app
);
1024 if (ua_sess
== NULL
) {
1028 hashtable_get_first(usess
->domain_global
.channels
, &iter
);
1029 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
1030 uchan
= caa_container_of(node
, struct ltt_ust_channel
, node
);
1032 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1033 if (ua_chan
== NULL
) {
1037 hashtable_get_first(uchan
->events
, &iter
);
1038 while ((node
= hashtable_iter_get_node(&iter
)) != NULL
) {
1039 uevent
= caa_container_of(node
, struct ltt_ust_event
, node
);
1041 ua_event
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1042 if (ua_event
== NULL
) {
1047 hashtable_get_next(uchan
->events
, &iter
);
1051 /* Next item in hash table */
1052 hashtable_get_next(usess
->domain_global
.channels
, &iter
);
1055 if (usess
->start_trace
) {
1056 ret
= ust_app_start_trace(usess
, app
);
1061 DBG2("UST trace started for app pid %d", app
->key
.pid
);