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 * Open metadata onto the UST tracer for a UST session.
250 static int open_ust_metadata(struct ust_app
*app
,
251 struct ust_app_session
*ua_sess
)
254 struct lttng_ust_channel_attr uattr
;
256 uattr
.overwrite
= ua_sess
->metadata
->attr
.overwrite
;
257 uattr
.subbuf_size
= ua_sess
->metadata
->attr
.subbuf_size
;
258 uattr
.num_subbuf
= ua_sess
->metadata
->attr
.num_subbuf
;
259 uattr
.switch_timer_interval
=
260 ua_sess
->metadata
->attr
.switch_timer_interval
;
261 uattr
.read_timer_interval
=
262 ua_sess
->metadata
->attr
.read_timer_interval
;
263 uattr
.output
= ua_sess
->metadata
->attr
.output
;
265 /* UST tracer metadata creation */
266 ret
= ustctl_open_metadata(app
->key
.sock
, ua_sess
->handle
, &uattr
,
267 &ua_sess
->metadata
->obj
);
269 ERR("UST app open metadata failed for app pid:%d",
279 * Create stream onto the UST tracer for a UST session.
281 static int create_ust_stream(struct ust_app
*app
,
282 struct ust_app_session
*ua_sess
)
286 ret
= ustctl_create_stream(app
->key
.sock
, ua_sess
->metadata
->obj
,
287 &ua_sess
->metadata
->stream_obj
);
289 ERR("UST create metadata stream failed");
298 * Create the specified channel onto the UST tracer for a UST session.
300 static int create_ust_channel(struct ust_app
*app
,
301 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
305 /* TODO: remove cast and use lttng-ust-abi.h */
306 ret
= ustctl_create_channel(app
->key
.sock
, ua_sess
->handle
,
307 (struct lttng_ust_channel_attr
*)&ua_chan
->attr
, &ua_chan
->obj
);
309 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
310 "and session handle %d with ret %d",
311 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
,
312 ua_sess
->handle
, ret
);
316 ua_chan
->handle
= ua_chan
->obj
->handle
;
317 ua_chan
->attr
.shm_fd
= ua_chan
->obj
->shm_fd
;
318 ua_chan
->attr
.wait_fd
= ua_chan
->obj
->wait_fd
;
319 ua_chan
->attr
.memory_map_size
= ua_chan
->obj
->memory_map_size
;
321 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
322 ua_chan
->name
, app
->key
.pid
, app
->key
.sock
);
329 * Create the specified event onto the UST tracer for a UST session.
331 static int create_ust_event(struct ust_app
*app
,
332 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
333 struct ust_app_event
*ua_event
)
337 /* Create UST event on tracer */
338 ret
= ustctl_create_event(app
->key
.sock
, &ua_event
->attr
, ua_chan
->obj
,
341 ERR("Error ustctl create event %s for app pid: %d with ret %d",
342 ua_event
->attr
.name
, app
->key
.pid
, ret
);
346 ua_event
->handle
= ua_event
->obj
->handle
;
347 ua_event
->enabled
= 1;
349 DBG2("UST app event %s created successfully for pid:%d",
350 ua_event
->attr
.name
, app
->key
.pid
);
357 * Alloc new UST app session.
359 static struct ust_app_session
*alloc_ust_app_session(void)
361 struct ust_app_session
*ua_sess
;
363 /* Init most of the default value by allocating and zeroing */
364 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
365 if (ua_sess
== NULL
) {
370 ua_sess
->handle
= -1;
371 ua_sess
->channels
= hashtable_new_str(0);
380 * Alloc new UST app channel.
382 static struct ust_app_channel
*alloc_ust_app_channel(char *name
,
383 struct lttng_ust_channel
*attr
)
385 struct ust_app_channel
*ua_chan
;
387 /* Init most of the default value by allocating and zeroing */
388 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
389 if (ua_chan
== NULL
) {
394 /* Setup channel name */
395 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
396 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
398 ua_chan
->handle
= -1;
399 ua_chan
->ctx
= hashtable_new(0);
400 ua_chan
->events
= hashtable_new_str(0);
401 hashtable_node_init(&ua_chan
->node
, (void *) ua_chan
->name
,
402 strlen(ua_chan
->name
));
404 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
406 /* Copy attributes */
408 memcpy(&ua_chan
->attr
, attr
, sizeof(ua_chan
->attr
));
411 DBG3("UST app channel %s allocated", ua_chan
->name
);
420 * Alloc new UST app event.
422 static struct ust_app_event
*alloc_ust_app_event(char *name
,
423 struct lttng_ust_event
*attr
)
425 struct ust_app_event
*ua_event
;
427 /* Init most of the default value by allocating and zeroing */
428 ua_event
= zmalloc(sizeof(struct ust_app_event
));
429 if (ua_event
== NULL
) {
434 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
435 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
436 ua_event
->ctx
= hashtable_new(0);
437 hashtable_node_init(&ua_event
->node
, (void *) ua_event
->name
,
438 strlen(ua_event
->name
));
440 /* Copy attributes */
442 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
445 DBG3("UST app event %s allocated", ua_event
->name
);
454 * Copy data between an UST app event and a LTT event.
456 static void shadow_copy_event(struct ust_app_event
*ua_event
,
457 struct ltt_ust_event
*uevent
)
459 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
460 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
462 /* Copy event attributes */
463 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
465 /* TODO: support copy context */
469 * Copy data between an UST app channel and a LTT channel.
471 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
472 struct ltt_ust_channel
*uchan
)
474 struct cds_lfht_iter iter
;
475 struct cds_lfht_node
*ua_event_node
;
476 struct ltt_ust_event
*uevent
;
477 struct ust_app_event
*ua_event
;
479 DBG2("Shadow copy of UST app channel %s", ua_chan
->name
);
481 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
482 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
483 /* Copy event attributes */
484 memcpy(&ua_chan
->attr
, &uchan
->attr
, sizeof(ua_chan
->attr
));
486 /* TODO: support copy context */
488 /* Copy all events from ltt ust channel to ust app channel */
489 cds_lfht_for_each_entry(uchan
->events
, &iter
, uevent
, node
) {
490 struct cds_lfht_iter uiter
;
492 ua_event_node
= hashtable_lookup(ua_chan
->events
,
493 (void *) uevent
->attr
.name
, strlen(uevent
->attr
.name
),
495 if (ua_event_node
== NULL
) {
496 DBG2("UST event %s not found on shadow copy channel",
498 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
499 if (ua_event
== NULL
) {
502 shadow_copy_event(ua_event
, uevent
);
503 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
507 DBG3("Shadow copy channel done");
511 * Copy data between a UST app session and a regular LTT session.
513 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
514 struct ltt_ust_session
*usess
,
517 struct cds_lfht_node
*ua_chan_node
;
518 struct cds_lfht_iter iter
;
519 struct ltt_ust_channel
*uchan
;
520 struct ust_app_channel
*ua_chan
;
526 /* Get date and time for unique app path */
528 timeinfo
= localtime(&rawtime
);
529 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
531 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
533 ua_sess
->uid
= usess
->uid
;
535 ret
= snprintf(ua_sess
->path
, PATH_MAX
,
537 usess
->pathname
, app
->name
, app
->key
.pid
,
540 PERROR("asprintf UST shadow copy session");
541 /* TODO: We cannot return an error from here.. */
545 /* TODO: support all UST domain */
547 /* Iterate over all channels in global domain. */
548 cds_lfht_for_each_entry(usess
->domain_global
.channels
, &iter
,
550 struct cds_lfht_iter uiter
;
552 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
553 (void *)uchan
->name
, strlen(uchan
->name
),
555 if (ua_chan_node
!= NULL
) {
559 DBG2("Channel %s not found on shadow session copy, creating it",
561 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
562 if (ua_chan
== NULL
) {
563 /* malloc failed... continuing */
567 shadow_copy_channel(ua_chan
, uchan
);
568 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
573 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
574 struct ust_app
*app
, struct cds_lfht_iter
*iter
)
576 /* Get right UST app session from app */
577 (void) hashtable_lookup(app
->sessions
,
578 (void *) ((unsigned long) usess
->uid
), sizeof(void *),
583 * Return ust app session from the app session hashtable using the UST session
586 static struct ust_app_session
*lookup_session_by_app(
587 struct ltt_ust_session
*usess
, struct ust_app
*app
)
589 struct cds_lfht_iter iter
;
590 struct cds_lfht_node
*node
;
592 __lookup_session_by_app(usess
, app
, &iter
);
593 node
= hashtable_iter_get_node(&iter
);
598 return caa_container_of(node
, struct ust_app_session
, node
);
605 * Create a UST session onto the tracer of app and add it the session
608 * Return ust app session or NULL on error.
610 static struct ust_app_session
*create_ust_app_session(
611 struct ltt_ust_session
*usess
, struct ust_app
*app
)
614 struct ust_app_session
*ua_sess
;
616 ua_sess
= lookup_session_by_app(usess
, app
);
617 if (ua_sess
== NULL
) {
618 DBG2("UST app pid: %d session uid %d not found, creating it",
619 app
->key
.pid
, usess
->uid
);
620 ua_sess
= alloc_ust_app_session();
621 if (ua_sess
== NULL
) {
622 /* Only malloc can failed so something is really wrong */
625 shadow_copy_session(ua_sess
, usess
, app
);
628 if (ua_sess
->handle
== -1) {
629 ret
= ustctl_create_session(app
->key
.sock
);
631 ERR("Error creating session for app pid %d, sock %d",
632 app
->key
.pid
, app
->key
.sock
);
633 /* TODO: free() ua_sess */
637 DBG2("UST app ustctl create session handle %d", ret
);
638 ua_sess
->handle
= ret
;
640 /* Add ust app session to app's HT */
641 hashtable_node_init(&ua_sess
->node
,
642 (void *)((unsigned long) ua_sess
->uid
), sizeof(void *));
643 hashtable_add_unique(app
->sessions
, &ua_sess
->node
);
645 DBG2("UST app session created successfully with handle %d", ret
);
655 * Create UST app channel and create it on the tracer.
657 static struct ust_app_channel
*create_ust_app_channel(
658 struct ust_app_session
*ua_sess
, struct ltt_ust_channel
*uchan
,
662 struct cds_lfht_iter iter
;
663 struct cds_lfht_node
*ua_chan_node
;
664 struct ust_app_channel
*ua_chan
;
666 /* Lookup channel in the ust app session */
667 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
668 (void *)uchan
->name
, strlen(uchan
->name
), &iter
);
669 if (ua_chan_node
== NULL
) {
670 DBG2("Unable to find channel %s in ust session uid %u",
671 uchan
->name
, ua_sess
->uid
);
672 ua_chan
= alloc_ust_app_channel(uchan
->name
, &uchan
->attr
);
673 if (ua_chan
== NULL
) {
676 shadow_copy_channel(ua_chan
, uchan
);
678 hashtable_add_unique(ua_sess
->channels
, &ua_chan
->node
);
680 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
683 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
695 * Create UST app event and create it on the tracer side.
697 static struct ust_app_event
*create_ust_app_event(
698 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
699 struct ltt_ust_event
*uevent
, struct ust_app
*app
)
702 struct cds_lfht_iter iter
;
703 struct cds_lfht_node
*ua_event_node
;
704 struct ust_app_event
*ua_event
;
707 ua_event_node
= hashtable_lookup(ua_chan
->events
,
708 (void *)uevent
->attr
.name
, strlen(uevent
->attr
.name
), &iter
);
709 if (ua_event_node
== NULL
) {
710 DBG2("UST app event %s not found, creating it", uevent
->attr
.name
);
711 /* Does not exist so create one */
712 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
713 if (ua_event
== NULL
) {
714 /* Only malloc can failed so something is really wrong */
717 shadow_copy_event(ua_event
, uevent
);
719 hashtable_add_unique(ua_chan
->events
, &ua_event
->node
);
721 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
724 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
736 * Create UST metadata and open it on the tracer side.
738 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
739 char *pathname
, struct ust_app
*app
)
743 if (ua_sess
->metadata
== NULL
) {
744 /* Allocate UST metadata */
745 ua_sess
->metadata
= trace_ust_create_metadata(pathname
);
746 if (ua_sess
->metadata
== NULL
) {
747 ERR("UST app session %d creating metadata failed",
752 ret
= open_ust_metadata(app
, ua_sess
);
757 DBG2("UST metadata opened for app pid %d", app
->key
.pid
);
760 /* Open UST metadata stream */
761 if (ua_sess
->metadata
->stream_obj
== NULL
) {
762 ret
= create_ust_stream(app
, ua_sess
);
767 ret
= mkdir(ua_sess
->path
, S_IRWXU
| S_IRWXG
);
769 PERROR("mkdir UST metadata");
773 ret
= snprintf(ua_sess
->metadata
->pathname
, PATH_MAX
,
774 "%s/metadata", ua_sess
->path
);
776 PERROR("asprintf UST create stream");
780 DBG2("UST metadata stream object created for app pid %d",
783 ERR("Attempting to create stream without metadata opened");
794 * Return pointer to traceable apps list.
796 struct cds_lfht
*ust_app_get_ht(void)
802 * Return ust app pointer or NULL if not found.
804 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
806 struct cds_lfht_node
*node
;
807 struct cds_lfht_iter iter
;
810 node
= hashtable_lookup(ust_app_ht
,
811 (void *)((unsigned long) pid
), sizeof(void *), &iter
);
813 DBG2("UST app no found with pid %d", pid
);
818 DBG2("Found UST app by pid %d", pid
);
820 return caa_container_of(node
, struct ust_app
, node
);
828 * Using pid and uid (of the app), allocate a new ust_app struct and
829 * add it to the global traceable app list.
831 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
832 * bitness is not supported.
834 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
839 * Currently support only tracing of application which share the
840 * same bitness as the consumer. Eventually implement dispatch
841 * to specific compat32 consumer.
843 if (msg
->bits_per_long
!= CAA_BITS_PER_LONG
) {
844 ERR("Registration failed: application \"%s\" (pid: %d) has "
845 "%d-bit long, but only "
846 "%d-bit lttng-consumerd is available.\n",
847 msg
->name
, msg
->pid
, msg
->bits_per_long
,
853 lta
= zmalloc(sizeof(struct ust_app
));
859 lta
->ppid
= msg
->ppid
;
862 lta
->v_major
= msg
->major
;
863 lta
->v_minor
= msg
->minor
;
864 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
865 lta
->name
[16] = '\0';
866 lta
->sessions
= hashtable_new(0);
869 lta
->key
.pid
= msg
->pid
;
870 hashtable_node_init(<a
->node
, (void *)((unsigned long)lta
->key
.pid
),
872 lta
->key
.sock
= sock
;
873 hashtable_node_init(<a
->key
.node
, (void *)((unsigned long)lta
->key
.sock
),
877 hashtable_add_unique(ust_app_sock_key_map
, <a
->key
.node
);
878 hashtable_add_unique(ust_app_ht
, <a
->node
);
881 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
882 " (version %d.%d)", lta
->key
.pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
883 lta
->key
.sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
889 * Unregister app by removing it from the global traceable app list and freeing
892 * The socket is already closed at this point so no close to sock.
894 void ust_app_unregister(int sock
)
897 struct cds_lfht_node
*node
;
898 struct cds_lfht_iter iter
;
901 lta
= find_app_by_sock(sock
);
903 ERR("Unregister app sock %d not found!", sock
);
907 DBG("PID %d unregistering with sock %d", lta
->key
.pid
, sock
);
909 /* Get the node reference for a call_rcu */
910 node
= hashtable_lookup(ust_app_ht
,
911 (void *)((unsigned long) lta
->key
.pid
), sizeof(void *), &iter
);
913 ERR("Unable to find app sock %d by pid %d", sock
, lta
->key
.pid
);
917 hashtable_del(ust_app_ht
, &iter
);
918 call_rcu(&node
->head
, delete_ust_app_rcu
);
925 * Return traceable_app_count
927 unsigned long ust_app_list_count(void)
932 count
= hashtable_get_count(ust_app_ht
);
939 * Fill events array with all events name of all registered apps.
941 int ust_app_list_events(struct lttng_event
**events
)
944 size_t nbmem
, count
= 0;
945 struct cds_lfht_iter iter
;
947 struct lttng_event
*tmp
;
949 nbmem
= UST_APP_EVENT_LIST_SIZE
;
950 tmp
= zmalloc(nbmem
* sizeof(struct lttng_event
));
952 PERROR("zmalloc ust app events");
959 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
960 handle
= ustctl_tracepoint_list(app
->key
.sock
);
962 ERR("UST app list events getting handle failed for app pid %d",
967 while ((ret
= ustctl_tracepoint_list_get(app
->key
.sock
, handle
,
968 tmp
[count
].name
)) != -ENOENT
) {
970 DBG2("Reallocating event list from %zu to %zu bytes", nbmem
,
971 nbmem
+ UST_APP_EVENT_LIST_SIZE
);
972 nbmem
+= UST_APP_EVENT_LIST_SIZE
;
973 tmp
= realloc(tmp
, nbmem
);
975 PERROR("realloc ust app events");
981 tmp
[count
].type
= LTTNG_UST_TRACEPOINT
;
982 tmp
[count
].pid
= app
->key
.pid
;
983 tmp
[count
].enabled
= -1;
991 DBG2("UST app list events done (%zu events)", count
);
1000 * Free and clean all traceable apps of the global list.
1002 void ust_app_clean_list(void)
1005 struct cds_lfht_node
*node
;
1006 struct cds_lfht_iter iter
;
1007 struct ust_app
*app
;
1009 DBG2("UST app cleaning registered apps hash table");
1013 cds_lfht_for_each(ust_app_ht
, &iter
, node
) {
1014 app
= caa_container_of(node
, struct ust_app
, node
);
1016 ret
= hashtable_del(ust_app_ht
, &iter
);
1018 call_rcu(&node
->head
, delete_ust_app_rcu
);
1022 hashtable_destroy(ust_app_ht
);
1023 hashtable_destroy(ust_app_sock_key_map
);
1029 * Init UST app hash table.
1031 void ust_app_ht_alloc(void)
1033 ust_app_ht
= hashtable_new(0);
1034 ust_app_sock_key_map
= hashtable_new(0);
1038 * For a specific UST session, create the channel for all registered apps.
1040 int ust_app_create_channel_all(struct ltt_ust_session
*usess
,
1041 struct ltt_ust_channel
*uchan
)
1044 struct cds_lfht_iter iter
;
1045 struct ust_app
*app
;
1046 struct ust_app_session
*ua_sess
;
1047 struct ust_app_channel
*ua_chan
;
1049 if (usess
== NULL
|| uchan
== NULL
) {
1050 ERR("Adding UST global channel to NULL values");
1055 DBG2("UST app adding channel %s to global domain for session uid %d",
1056 uchan
->name
, usess
->uid
);
1060 /* For every registered applications */
1061 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1062 /* Create session on the tracer side and add it to app session HT */
1063 ua_sess
= create_ust_app_session(usess
, app
);
1064 if (ua_sess
== NULL
) {
1068 /* Create channel onto application */
1069 ua_chan
= create_ust_app_channel(ua_sess
, uchan
, app
);
1070 if (ua_chan
== NULL
) {
1082 * For a specific UST session and UST channel, create the event for all
1085 int ust_app_create_event_all(struct ltt_ust_session
*usess
,
1086 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
1089 struct cds_lfht_iter iter
;
1090 struct cds_lfht_node
*ua_chan_node
;
1091 struct ust_app
*app
;
1092 struct ust_app_session
*ua_sess
;
1093 struct ust_app_channel
*ua_chan
;
1094 struct ust_app_event
*ua_event
;
1096 DBG("UST app creating event %s for all apps for session uid %d",
1097 uevent
->attr
.name
, usess
->uid
);
1101 /* For all registered applications */
1102 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1103 struct cds_lfht_iter uiter
;
1105 /* Create session on the tracer side and add it to app session HT */
1106 ua_sess
= create_ust_app_session(usess
, app
);
1107 if (ua_sess
== NULL
) {
1111 /* Lookup channel in the ust app session */
1112 ua_chan_node
= hashtable_lookup(ua_sess
->channels
,
1113 (void *)uchan
->name
, strlen(uchan
->name
),
1115 if (ua_chan_node
== NULL
) {
1116 ERR("Channel %s not found in session uid %d. Skipping",
1117 uchan
->name
, usess
->uid
);
1120 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
1122 ua_event
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
1123 if (ua_event
== NULL
) {
1134 * Start tracing for a specific UST session and app.
1136 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1139 struct cds_lfht_iter iter
;
1140 struct ust_app_session
*ua_sess
;
1141 struct ust_app_channel
*ua_chan
;
1142 struct ltt_ust_stream
*ustream
;
1144 DBG("Starting tracing for ust app pid %d", app
->key
.pid
);
1148 ua_sess
= lookup_session_by_app(usess
, app
);
1149 if (ua_sess
== NULL
) {
1150 /* Only malloc can failed so something is really wrong */
1151 goto error_rcu_unlock
;
1154 /* upon restart, we skip the setup, already done */
1155 if (ua_sess
->started
)
1158 ret
= create_ust_app_metadata(ua_sess
, usess
->pathname
, app
);
1160 goto error_rcu_unlock
;
1163 /* For each channel */
1164 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1165 /* Create all streams */
1167 /* Create UST stream */
1168 ustream
= zmalloc(sizeof(*ustream
));
1169 if (ustream
== NULL
) {
1170 PERROR("zmalloc ust stream");
1171 goto error_rcu_unlock
;
1174 ret
= ustctl_create_stream(app
->key
.sock
, ua_chan
->obj
,
1177 /* Got all streams */
1180 ustream
->handle
= ustream
->obj
->handle
;
1182 /* Order is important */
1183 cds_list_add_tail(&ustream
->list
, &ua_chan
->streams
.head
);
1184 ret
= snprintf(ustream
->pathname
, PATH_MAX
, "%s/%s_%u",
1185 ua_sess
->path
, ua_chan
->name
,
1186 ua_chan
->streams
.count
++);
1188 PERROR("asprintf UST create stream");
1191 DBG2("UST stream %d ready at %s", ua_chan
->streams
.count
,
1196 /* Setup UST consumer socket and send fds to it */
1197 ret
= ust_consumer_send_session(ust_consumer_fd
, ua_sess
);
1199 goto error_rcu_unlock
;
1201 ua_sess
->started
= 1;
1204 /* This start the UST tracing */
1205 ret
= ustctl_start_session(app
->key
.sock
, ua_sess
->handle
);
1207 ERR("Error starting tracing for app pid: %d", app
->key
.pid
);
1208 goto error_rcu_unlock
;
1213 /* Quiescent wait after starting trace */
1214 ustctl_wait_quiescent(app
->key
.sock
);
1224 * Stop tracing for a specific UST session and app.
1226 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1229 struct ust_app_session
*ua_sess
;
1231 DBG("Stopping tracing for ust app pid %d", app
->key
.pid
);
1235 ua_sess
= lookup_session_by_app(usess
, app
);
1236 if (ua_sess
== NULL
) {
1237 /* Only malloc can failed so something is really wrong */
1238 goto error_rcu_unlock
;
1241 #if 0 /* only useful when periodical flush will be supported */
1242 /* need to keep a handle on shm in session for this. */
1243 /* Flush all buffers before stopping */
1244 ret
= ustctl_flush_buffer(usess
->sock
, usess
->metadata
->obj
);
1246 ERR("UST metadata flush failed");
1249 cds_list_for_each_entry(ustchan
, &usess
->channels
.head
, list
) {
1250 ret
= ustctl_flush_buffer(usess
->sock
, ustchan
->obj
);
1252 ERR("UST flush buffer error");
1257 /* This inhibits UST tracing */
1258 ret
= ustctl_stop_session(app
->key
.sock
, ua_sess
->handle
);
1260 ERR("Error stopping tracing for app pid: %d", app
->key
.pid
);
1261 goto error_rcu_unlock
;
1266 /* Quiescent wait after stopping trace */
1267 ustctl_wait_quiescent(app
->key
.sock
);
1277 * Destroy a specific UST session in apps.
1279 int ust_app_destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
1281 struct ust_app_session
*ua_sess
;
1282 struct lttng_ust_object_data obj
;
1283 struct cds_lfht_iter iter
;
1284 struct cds_lfht_node
*node
;
1286 DBG("Destroy tracing for ust app pid %d", app
->key
.pid
);
1290 __lookup_session_by_app(usess
, app
, &iter
);
1291 node
= hashtable_iter_get_node(&iter
);
1293 /* Only malloc can failed so something is really wrong */
1294 goto error_rcu_unlock
;
1296 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
1297 hashtable_del(app
->sessions
, &iter
);
1298 delete_ust_app_session(app
->key
.sock
, ua_sess
);
1299 obj
.handle
= ua_sess
->handle
;
1302 obj
.memory_map_size
= 0;
1303 ustctl_release_object(app
->key
.sock
, &obj
);
1307 /* Quiescent wait after stopping trace */
1308 ustctl_wait_quiescent(app
->key
.sock
);
1318 * Start tracing for the UST session.
1320 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
1323 struct cds_lfht_iter iter
;
1324 struct ust_app
*app
;
1326 DBG("Starting all UST traces");
1330 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1331 ret
= ust_app_start_trace(usess
, app
);
1333 /* Continue to next apps even on error */
1344 * Start tracing for the UST session.
1346 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
1349 struct cds_lfht_iter iter
;
1350 struct ust_app
*app
;
1352 DBG("Stopping all UST traces");
1356 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1357 ret
= ust_app_stop_trace(usess
, app
);
1359 /* Continue to next apps even on error */
1370 * Destroy app UST session.
1372 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
1375 struct cds_lfht_iter iter
;
1376 struct ust_app
*app
;
1378 DBG("Destroy all UST traces");
1382 cds_lfht_for_each_entry(ust_app_ht
, &iter
, app
, node
) {
1383 ret
= ust_app_destroy_trace(usess
, app
);
1385 /* Continue to next apps even on error */
1396 * Add channels/events from UST global domain to registered apps at sock.
1398 void ust_app_global_update(struct ltt_ust_session
*usess
, int sock
)
1401 struct cds_lfht_iter iter
;
1402 struct ust_app
*app
;
1403 struct ust_app_session
*ua_sess
;
1404 struct ust_app_channel
*ua_chan
;
1405 struct ust_app_event
*ua_event
;
1407 if (usess
== NULL
) {
1408 ERR("No UST session on global update. Returning");
1412 DBG2("UST app global update for app sock %d for session uid %d", sock
,
1417 app
= find_app_by_sock(sock
);
1419 ERR("Failed to update app sock %d", sock
);
1423 ua_sess
= create_ust_app_session(usess
, app
);
1424 if (ua_sess
== NULL
) {
1429 * We can iterate safely here over all UST app session sicne the create ust
1430 * app session above made a shadow copy of the UST global domain from the
1433 cds_lfht_for_each_entry(ua_sess
->channels
, &iter
, ua_chan
, node
) {
1434 ret
= create_ust_channel(app
, ua_sess
, ua_chan
);
1436 /* FIXME: Should we quit here or continue... */
1440 /* For each events */
1441 cds_lfht_for_each_entry(ua_chan
->events
, &iter
, ua_event
, node
) {
1442 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
1444 /* FIXME: Should we quit here or continue... */
1450 if (usess
->start_trace
) {
1451 ret
= ust_app_start_trace(usess
, app
);
1456 DBG2("UST trace started for app pid %d", app
->key
.pid
);