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. Never call
138 * this function outside of a call_rcu call.
140 static void delete_ust_app(struct ust_app
*app
)
143 struct cds_lfht_node
*node
;
144 struct cds_lfht_iter iter
;
145 struct ust_app_session
*ua_sess
;
149 /* Remove from key hash table */
150 node
= hashtable_lookup(ust_app_sock_key_map
,
151 (void *) ((unsigned long) app
->key
.sock
), sizeof(void *), &iter
);
153 /* Not suppose to happen */
154 ERR("UST app key %d not found in key hash table", app
->key
.sock
);
158 ret
= hashtable_del(ust_app_sock_key_map
, &iter
);
160 ERR("UST app unable to delete app sock %d from key hash table",
163 DBG2("UST app pair sock %d key %d deleted",
164 app
->key
.sock
, app
->key
.pid
);
167 /* Socket is already closed at this point */
169 /* Delete ust app sessions info */
170 if (app
->sock_closed
) {
174 cds_lfht_for_each_entry(app
->sessions
, &iter
, ua_sess
, node
) {
175 hashtable_del(app
->sessions
, &iter
);
176 delete_ust_app_session(app
->key
.sock
, ua_sess
);
179 ret
= hashtable_destroy(app
->sessions
);
181 ERR("UST app destroy session hashtable failed");
185 if (!app
->sock_closed
) {
186 close(app
->key
.sock
);
189 DBG2("UST app pid %d deleted", app
->key
.pid
);
196 * URCU intermediate call to delete an UST app.
198 static void delete_ust_app_rcu(struct rcu_head
*head
)
200 struct cds_lfht_node
*node
=
201 caa_container_of(head
, struct cds_lfht_node
, head
);
202 struct ust_app
*app
=
203 caa_container_of(node
, struct ust_app
, node
);
209 * Find an ust_app using the sock and return it. RCU read side lock must be
210 * held before calling this helper function.
212 static struct ust_app
*find_app_by_sock(int sock
)
214 struct cds_lfht_node
*node
;
215 struct ust_app_key
*key
;
216 struct cds_lfht_iter iter
;
218 node
= hashtable_lookup(ust_app_sock_key_map
,
219 (void *)((unsigned long) sock
), sizeof(void *), &iter
);
221 DBG2("UST app find by sock %d key not found", sock
);
225 key
= caa_container_of(node
, struct ust_app_key
, node
);
227 node
= hashtable_lookup(ust_app_ht
,
228 (void *)((unsigned long) key
->pid
), sizeof(void *), &iter
);
230 DBG2("UST app find by sock %d not found", sock
);
233 return caa_container_of(node
, struct ust_app
, node
);
240 * Open metadata onto the UST tracer for a UST session.
242 static int open_ust_metadata(struct ust_app
*app
,
243 struct ust_app_session
*ua_sess
)
246 struct lttng_ust_channel_attr uattr
;
248 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
249 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
250 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
251 uattr
.switch_timer_interval
=
252 ua_sess
->metadata
->attr
.switch_timer_interval
;
253 uattr
.read_timer_interval
=
254 ua_sess
->metadata
->attr
.read_timer_interval
;
255 uattr
.output
= ua_sess
->metadata
->attr
.output
;
257 /* UST tracer metadata creation */
258 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
259 &ua_sess
->metadata
->obj
);
261 ERR("UST app open metadata failed for app pid:%d",
271 * Create stream onto the UST tracer for a UST session.
273 static int create_ust_stream(struct ust_app
*app
,
274 struct ust_app_session
*ua_sess
)
278 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
279 &ua_sess
->metadata
->stream_obj
);
281 ERR("UST create metadata stream failed");
290 * Create the specified channel onto the UST tracer for a UST session.
292 static int create_ust_channel(struct ust_app
*app
,
293 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
297 /* TODO: remove cast and use lttng-ust-abi.h */
298 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
299 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
301 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
302 "and session handle %d with ret %d",
303 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
304 ua_sess
->handle
, ret
);
308 ua_chan
->handle
= ua_chan
->obj
->handle
;
309 ua_chan
->attr
.shm_fd
= ua_chan
->obj
->shm_fd
;
310 ua_chan
->attr
.wait_fd
= ua_chan
->obj
->wait_fd
;
311 ua_chan
->attr
.memory_map_size
= ua_chan
->obj
->memory_map_size
;
313 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
314 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
321 * Create the specified event onto the UST tracer for a UST session.
323 static int create_ust_event(struct ust_app
*app
,
324 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
325 struct ust_app_event
*ua_event
)
329 /* Create UST event on tracer */
330 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
333 ERR("Error ustctl create event %s for app pid: %d with ret %d",
334 ua_event
->attr
.name
, app
->key
.pid
, ret
);
338 ua_event
->handle
= ua_event
->obj
->handle
;
339 ua_event
->enabled
= 1;
341 DBG2("UST app event %s created successfully for pid:%d",
342 ua_event
->attr
.name
, app
->key
.pid
);
349 * Alloc new UST app session.
351 static struct ust_app_session
*alloc_ust_app_session(void)
353 struct ust_app_session
*ua_sess
;
355 /* Init most of the default value by allocating and zeroing */
356 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
357 if (ua_sess
== NULL
) {
362 ua_sess
->handle
= -1;
363 ua_sess
->channels
= hashtable_new_str(0);
372 * Alloc new UST app channel.
374 static struct ust_app_channel
*alloc_ust_app_channel(char *name
,
375 struct lttng_ust_channel
*attr
)
377 struct ust_app_channel
*ua_chan
;
379 /* Init most of the default value by allocating and zeroing */
380 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
381 if (ua_chan
== NULL
) {
386 /* Setup channel name */
387 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
388 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
390 ua_chan
->handle
= -1;
391 ua_chan
->ctx
= hashtable_new(0);
392 ua_chan
->events
= hashtable_new_str(0);
393 hashtable_node_init(&ua_chan
->node
, (void *) ua_chan
->name
,
394 strlen(ua_chan
->name
));
396 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
398 /* Copy attributes */
400 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
403 DBG3("UST app channel %s allocated", ua_chan
->name
);
412 * Alloc new UST app event.
414 static struct ust_app_event
*alloc_ust_app_event(char *name
,
415 struct lttng_ust_event
*attr
)
417 struct ust_app_event
*ua_event
;
419 /* Init most of the default value by allocating and zeroing */
420 ua_event
= zmalloc(sizeof(struct ust_app_event
));
421 if (ua_event
== NULL
) {
426 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
427 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
428 ua_event
->ctx
= hashtable_new(0);
429 hashtable_node_init(&ua_event
->node
, (void *) ua_event
->name
,
430 strlen(ua_event
->name
));
432 /* Copy attributes */
434 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
437 DBG3("UST app event %s allocated", ua_event
->name
);
446 * Copy data between an UST app event and a LTT event.
448 static void shadow_copy_event(struct ust_app_event
*ua_event
,
449 struct ltt_ust_event
*uevent
)
451 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
452 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
454 /* Copy event attributes */
455 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
457 /* TODO: support copy context */
461 * Copy data between an UST app channel and a LTT channel.
463 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
464 struct ltt_ust_channel
*uchan
)
466 struct cds_lfht_iter iter
;
467 struct cds_lfht_node
*ua_event_node
;
468 struct ltt_ust_event
*uevent
;
469 struct ust_app_event
*ua_event
;
471 DBG2("Shadow copy of UST app channel %s", ua_chan
->name
);
473 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
474 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
475 /* Copy event attributes */
476 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
478 /* TODO: support copy context */
480 /* Copy all events from ltt ust channel to ust app channel */
481 cds_lfht_for_each_entry(uchan
->events
, &iter
, uevent
, node
) {
482 ua_event_node
= hashtable_lookup(ua_chan
->events
,
483 (void *) uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
484 if (ua_event_node
== NULL
) {
485 DBG2("UST event %s not found on shadow copy channel",
487 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
488 if (ua_event
== NULL
) {
491 shadow_copy_event(ua_event
, uevent
);
492 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
496 DBG3("Shadow copy channel done");
500 * Copy data between a UST app session and a regular LTT session.
502 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
503 struct ltt_ust_session
*usess
)
505 struct cds_lfht_node
*ua_chan_node
;
506 struct cds_lfht_iter iter
;
507 struct ltt_ust_channel
*uchan
;
508 struct ust_app_channel
*ua_chan
;
510 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
512 ua_sess
->uid
= usess
->uid
;
514 /* TODO: support all UST domain */
516 /* Iterate over all channels in global domain. */
517 cds_lfht_for_each_entry(usess
->domain_global
.channels
, &iter
,
519 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
520 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
521 if (ua_chan_node
!= NULL
) {
525 DBG2("Channel %s not found on shadow session copy, creating it",
527 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
528 if (ua_chan
== NULL
) {
529 /* malloc failed... continuing */
533 shadow_copy_channel(ua_chan
, uchan
);
534 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
539 * Return ust app session from the app session hashtable using the UST session
542 static struct ust_app_session
*lookup_session_by_app(
543 struct ltt_ust_session
*usess
, struct ust_app
*app
)
545 struct cds_lfht_iter iter
;
546 struct cds_lfht_node
*node
;
548 /* Get right UST app session from app */
549 node
= hashtable_lookup(app
->sessions
,
550 (void *) ((unsigned long) usess
->uid
), sizeof(void *), &iter
);
555 return caa_container_of(node
, struct ust_app_session
, node
);
562 * Create a UST session onto the tracer of app and add it the session
565 * Return ust app session or NULL on error.
567 static struct ust_app_session
*create_ust_app_session(
568 struct ltt_ust_session
*usess
, struct ust_app
*app
)
571 struct ust_app_session
*ua_sess
;
573 ua_sess
= lookup_session_by_app(usess
, app
);
574 if (ua_sess
== NULL
) {
575 DBG2("UST app pid: %d session uid %d not found, creating it",
576 app
->key
.pid
, usess
->uid
);
577 ua_sess
= alloc_ust_app_session();
578 if (ua_sess
== NULL
) {
579 /* Only malloc can failed so something is really wrong */
582 shadow_copy_session(ua_sess
, usess
);
585 if (ua_sess
->handle
== -1) {
586 ret
= ustctl_create_session(app
->key
.sock
);
588 ERR("Error creating session for app pid %d, sock %d",
589 app
->key
.pid
, app
->key
.sock
);
590 /* TODO: free() ua_sess */
594 DBG2("UST app ustctl create session handle %d", ret
);
595 ua_sess
->handle
= ret
;
597 /* Add ust app session to app's HT */
598 hashtable_node_init(&ua_sess
->node
,
599 (void *)((unsigned long) ua_sess
->uid
), sizeof(void *));
600 hashtable_add_unique(app
->sessions
, &ua_sess
->node
);
602 DBG2("UST app session created successfully with handle %d", ret
);
612 * Create UST app channel and create it on the tracer.
614 static struct ust_app_channel
*create_ust_app_channel(
615 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
619 struct cds_lfht_iter iter
;
620 struct cds_lfht_node
*ua_chan_node
;
621 struct ust_app_channel
*ua_chan
;
623 /* Lookup channel in the ust app session */
624 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
625 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
626 if (ua_chan_node
== NULL
) {
627 DBG2("Unable to find channel %s in ust session uid %u",
628 uchan
->name
, ua_sess
->uid
);
629 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
630 if (ua_chan
== NULL
) {
633 shadow_copy_channel(ua_chan
, uchan
);
635 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
637 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
640 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
652 * Create UST app event and create it on the tracer side.
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
, &uevent
->attr
);
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 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
693 * Create UST metadata and open it on the tracer side.
695 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
696 char *pathname
, struct ust_app
*app
)
700 if (ua_sess
->metadata
== NULL
) {
701 /* Allocate UST metadata */
702 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
703 if (ua_sess
->metadata
== NULL
) {
704 ERR("UST app session %d creating metadata failed",
709 ret
= open_ust_metadata(app
, ua_sess
);
714 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
717 /* Open UST metadata stream */
718 if (ua_sess
->metadata
->stream_obj
== NULL
) {
719 ret
= create_ust_stream(app
, ua_sess
);
724 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
, "%s/%s-%d",
725 pathname
, app
->name
, app
->key
.pid
);
727 PERROR("asprintf UST create stream");
731 ret
= mkdir(ua_sess
->metadata
->pathname
, S_IRWXU
| S_IRWXG
);
733 PERROR("mkdir UST metadata");
737 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
, "%s/%s-%d/metadata",
738 pathname
, app
->name
, app
->key
.pid
);
740 PERROR("asprintf UST create stream");
744 DBG2("UST metadata stream object created for app pid %d",
747 ERR("Attempting to create stream without metadata opened");
758 * Return pointer to traceable apps list.
760 struct cds_lfht
*ust_app_get_ht(void)
766 * Return ust app pointer or NULL if not found.
768 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
770 struct cds_lfht_node
*node
;
771 struct cds_lfht_iter iter
;
774 node
= hashtable_lookup(ust_app_ht
,
775 (void *)((unsigned long) pid
), sizeof(void *), &iter
);
777 DBG2("UST app no found with pid %d", pid
);
782 DBG2("Found UST app by pid %d", pid
);
784 return caa_container_of(node
, struct ust_app
, node
);
792 * Using pid and uid (of the app), allocate a new ust_app struct and
793 * add it to the global traceable app list.
795 * On success, return 0, else return malloc ENOMEM.
797 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
801 lta
= zmalloc(sizeof(struct ust_app
));
807 lta
->ppid
= msg
->ppid
;
810 lta
->v_major
= msg
->major
;
811 lta
->v_minor
= msg
->minor
;
812 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
813 lta
->name
[16] = '\0';
814 lta
->sessions
= hashtable_new(0);
817 lta
->key
.pid
= msg
->pid
;
818 hashtable_node_init(<a
->node
, (void *)((unsigned long)lta
->key
.pid
),
820 lta
->key
.sock
= sock
;
821 hashtable_node_init(<a
->key
.node
, (void *)((unsigned long)lta
->key
.sock
),
825 hashtable_add_unique(ust_app_sock_key_map
, <a
->key
.node
);
826 hashtable_add_unique(ust_app_ht
, <a
->node
);
829 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
830 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
831 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
837 * Unregister app by removing it from the global traceable app list and freeing
840 * The socket is already closed at this point so no close to sock.
842 void ust_app_unregister(int sock
)
845 struct cds_lfht_node
*node
;
846 struct cds_lfht_iter iter
;
849 lta
= find_app_by_sock(sock
);
851 ERR("Unregister app sock %d not found!", sock
);
855 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
857 /* Get the node reference for a call_rcu */
858 node
= hashtable_lookup(ust_app_ht
,
859 (void *)((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
861 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
865 /* We got called because the socket was closed on the remote end. */
867 /* Using a flag because we still need "sock" as a key. */
868 lta
->sock_closed
= 1;
869 hashtable_del(ust_app_ht
, &iter
);
870 call_rcu(&node
->head
, delete_ust_app_rcu
);
877 * Return traceable_app_count
879 unsigned long ust_app_list_count(void)
884 count
= hashtable_get_count(ust_app_ht
);
891 * Fill events array with all events name of all registered apps.
893 int ust_app_list_events(struct lttng_event
**events
)
896 size_t nbmem
, count
= 0;
897 struct cds_lfht_iter iter
;
899 struct lttng_event
*tmp
;
901 nbmem
= UST_APP_EVENT_LIST_SIZE
;
902 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
904 PERROR("zmalloc ust app events");
911 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
912 handle
= ustctl_tracepoint_list(app
->key
.sock
);
914 ERR("UST app list events getting handle failed for app pid %d",
919 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
920 tmp
[count
].name
)) != -ENOENT
) {
922 DBG2("Reallocating event list from %zu to %zu bytes", nbmem
,
923 nbmem
+ UST_APP_EVENT_LIST_SIZE
);
924 nbmem
+= UST_APP_EVENT_LIST_SIZE
;
925 tmp
= realloc(tmp
, nbmem
);
927 PERROR("realloc ust app events");
933 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
934 tmp
[count
].pid
= app
->key
.pid
;
935 tmp
[count
].enabled
= -1;
943 DBG2("UST app list events done (%zu events)", count
);
952 * Free and clean all traceable apps of the global list.
954 void ust_app_clean_list(void)
957 struct cds_lfht_node
*node
;
958 struct cds_lfht_iter iter
;
961 DBG2("UST app cleaning registered apps hash table");
965 cds_lfht_for_each(ust_app_ht
, &iter
, node
) {
966 app
= caa_container_of(node
, struct ust_app
, node
);
967 close(app
->key
.sock
);
968 app
->sock_closed
= 1;
970 ret
= hashtable_del(ust_app_ht
, &iter
);
972 call_rcu(&node
->head
, delete_ust_app_rcu
);
976 hashtable_destroy(ust_app_ht
);
977 hashtable_destroy(ust_app_sock_key_map
);
983 * Init UST app hash table.
985 void ust_app_ht_alloc(void)
987 ust_app_ht
= hashtable_new(0);
988 ust_app_sock_key_map
= hashtable_new(0);
992 * For a specific UST session, create the channel for all registered apps.
994 int ust_app_create_channel_all(struct ltt_ust_session
*usess
,
995 struct ltt_ust_channel
*uchan
)
998 struct cds_lfht_iter iter
;
1000 struct ust_app_session
*ua_sess
;
1001 struct ust_app_channel
*ua_chan
;
1003 if (usess
== NULL
|| uchan
== NULL
) {
1004 ERR("Adding UST global channel to NULL values");
1009 DBG2("UST app adding channel %s to global domain for session uid %d",
1010 uchan
->name
, usess
->uid
);
1014 /* For every registered applications */
1015 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1016 /* Create session on the tracer side and add it to app session HT */
1017 ua_sess
= create_ust_app_session(usess
, app
);
1018 if (ua_sess
== NULL
) {
1022 /* Create channel onto application */
1023 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1024 if (ua_chan
== NULL
) {
1036 * For a specific UST session and UST channel, create the event for all
1039 int ust_app_create_event_all(struct ltt_ust_session
*usess
,
1040 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1043 struct cds_lfht_iter iter
;
1044 struct cds_lfht_node
*ua_chan_node
;
1045 struct ust_app
*app
;
1046 struct ust_app_session
*ua_sess
;
1047 struct ust_app_channel
*ua_chan
;
1048 struct ust_app_event
*ua_event
;
1050 DBG("UST app creating event %s for all apps for session uid %d",
1051 uevent
->attr
.name
, usess
->uid
);
1055 /* For all registered applications */
1056 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1057 /* Create session on the tracer side and add it to app session HT */
1058 ua_sess
= create_ust_app_session(usess
, app
);
1059 if (ua_sess
== NULL
) {
1063 /* Lookup channel in the ust app session */
1064 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1065 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
1066 if (ua_chan_node
== NULL
) {
1067 ERR("Channel %s not found in session uid %d. Skipping",
1068 uchan
->name
, usess
->uid
);
1071 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1073 ua_event
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1074 if (ua_event
== NULL
) {
1085 * Start tracing for a specific UST session and app.
1087 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1090 struct cds_lfht_iter iter
;
1091 struct ust_app_session
*ua_sess
;
1092 struct ust_app_channel
*ua_chan
;
1093 struct ltt_ust_stream
*ustream
;
1095 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1099 ua_sess
= lookup_session_by_app(usess
, app
);
1100 if (ua_sess
== NULL
) {
1101 /* Only malloc can failed so something is really wrong */
1102 goto error_rcu_unlock
;
1105 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1107 goto error_rcu_unlock
;
1110 /* For each channel */
1111 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1112 /* Create all streams */
1114 /* Create UST stream */
1115 ustream
= zmalloc(sizeof(*ustream
));
1116 if (ustream
== NULL
) {
1117 PERROR("zmalloc ust stream");
1118 goto error_rcu_unlock
;
1121 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1124 /* Got all streams */
1127 ustream
->handle
= ustream
->obj
->handle
;
1129 /* Order is important */
1130 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1131 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s-%d/%s_%u",
1132 usess
->pathname
, app
->name
, app
->key
.pid
,
1133 ua_chan
->name
, ua_chan
->streams
.count
++);
1135 PERROR("asprintf UST create stream");
1138 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1143 /* Setup UST consumer socket and send fds to it */
1144 ret
= ust_consumer_send_session(ust_consumer_fd
, ua_sess
);
1146 goto error_rcu_unlock
;
1149 /* This start the UST tracing */
1150 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1152 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1153 goto error_rcu_unlock
;
1158 /* Quiescent wait after starting trace */
1159 ustctl_wait_quiescent(app
->key
.sock
);
1169 * Start tracing for the UST session.
1171 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
1174 struct cds_lfht_iter iter
;
1175 struct ust_app
*app
;
1177 DBG("Starting all UST traces");
1181 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1182 ret
= ust_app_start_trace(usess
, app
);
1184 /* Continue to next apps even on error */
1195 * Add channels/events from UST global domain to registered apps at sock.
1197 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
1200 struct cds_lfht_iter iter
;
1201 struct ust_app
*app
;
1202 struct ust_app_session
*ua_sess
;
1203 struct ust_app_channel
*ua_chan
;
1204 struct ust_app_event
*ua_event
;
1206 if (usess
== NULL
) {
1207 ERR("No UST session on global update. Returning");
1211 DBG2("UST app global update for app sock %d for session uid %d", sock
,
1216 app
= find_app_by_sock(sock
);
1218 ERR("Failed to update app sock %d", sock
);
1222 ua_sess
= create_ust_app_session(usess
, app
);
1223 if (ua_sess
== NULL
) {
1228 * We can iterate safely here over all UST app session sicne the create ust
1229 * app session above made a shadow copy of the UST global domain from the
1232 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1233 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1235 /* FIXME: Should we quit here or continue... */
1239 /* For each events */
1240 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
1241 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1243 /* FIXME: Should we quit here or continue... */
1249 if (usess
->start_trace
) {
1250 ret
= ust_app_start_trace(usess
, app
);
1255 DBG2("UST trace started for app pid %d", app
->key
.pid
);