2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
29 #include <urcu/compiler.h>
30 #include <lttng/ust-error.h>
33 #include <common/common.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
36 #include "buffer-registry.h"
38 #include "health-sessiond.h"
40 #include "ust-consumer.h"
45 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
47 /* Next available channel key. Access under next_channel_key_lock. */
48 static uint64_t _next_channel_key
;
49 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
51 /* Next available session ID. Access under next_session_id_lock. */
52 static uint64_t _next_session_id
;
53 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
56 * Return the incremented value of next_channel_key.
58 static uint64_t get_next_channel_key(void)
62 pthread_mutex_lock(&next_channel_key_lock
);
63 ret
= ++_next_channel_key
;
64 pthread_mutex_unlock(&next_channel_key_lock
);
69 * Return the atomically incremented value of next_session_id.
71 static uint64_t get_next_session_id(void)
75 pthread_mutex_lock(&next_session_id_lock
);
76 ret
= ++_next_session_id
;
77 pthread_mutex_unlock(&next_session_id_lock
);
81 static void copy_channel_attr_to_ustctl(
82 struct ustctl_consumer_channel_attr
*attr
,
83 struct lttng_ust_channel_attr
*uattr
)
85 /* Copy event attributes since the layout is different. */
86 attr
->subbuf_size
= uattr
->subbuf_size
;
87 attr
->num_subbuf
= uattr
->num_subbuf
;
88 attr
->overwrite
= uattr
->overwrite
;
89 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
90 attr
->read_timer_interval
= uattr
->read_timer_interval
;
91 attr
->output
= uattr
->output
;
95 * Match function for the hash table lookup.
97 * It matches an ust app event based on three attributes which are the event
98 * name, the filter bytecode and the loglevel.
100 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
102 struct ust_app_event
*event
;
103 const struct ust_app_ht_key
*key
;
108 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
111 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
114 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
118 /* Event loglevel. */
119 if (event
->attr
.loglevel
!= key
->loglevel
) {
120 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
121 && key
->loglevel
== 0 && event
->attr
.loglevel
== -1) {
123 * Match is accepted. This is because on event creation, the
124 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
125 * -1 are accepted for this loglevel type since 0 is the one set by
126 * the API when receiving an enable event.
133 /* One of the filters is NULL, fail. */
134 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
138 if (key
->filter
&& event
->filter
) {
139 /* Both filters exists, check length followed by the bytecode. */
140 if (event
->filter
->len
!= key
->filter
->len
||
141 memcmp(event
->filter
->data
, key
->filter
->data
,
142 event
->filter
->len
) != 0) {
147 /* One of the exclusions is NULL, fail. */
148 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
152 if (key
->exclusion
&& event
->exclusion
) {
153 /* Both exclusions exists, check count followed by the names. */
154 if (event
->exclusion
->count
!= key
->exclusion
->count
||
155 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
156 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
170 * Unique add of an ust app event in the given ht. This uses the custom
171 * ht_match_ust_app_event match function and the event name as hash.
173 static void add_unique_ust_app_event(struct ust_app_channel
*ua_chan
,
174 struct ust_app_event
*event
)
176 struct cds_lfht_node
*node_ptr
;
177 struct ust_app_ht_key key
;
181 assert(ua_chan
->events
);
184 ht
= ua_chan
->events
;
185 key
.name
= event
->attr
.name
;
186 key
.filter
= event
->filter
;
187 key
.loglevel
= event
->attr
.loglevel
;
188 key
.exclusion
= event
->exclusion
;
190 node_ptr
= cds_lfht_add_unique(ht
->ht
,
191 ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
192 ht_match_ust_app_event
, &key
, &event
->node
.node
);
193 assert(node_ptr
== &event
->node
.node
);
197 * Close the notify socket from the given RCU head object. This MUST be called
198 * through a call_rcu().
200 static void close_notify_sock_rcu(struct rcu_head
*head
)
203 struct ust_app_notify_sock_obj
*obj
=
204 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
206 /* Must have a valid fd here. */
207 assert(obj
->fd
>= 0);
209 ret
= close(obj
->fd
);
211 ERR("close notify sock %d RCU", obj
->fd
);
213 lttng_fd_put(LTTNG_FD_APPS
, 1);
219 * Return the session registry according to the buffer type of the given
222 * A registry per UID object MUST exists before calling this function or else
223 * it assert() if not found. RCU read side lock must be acquired.
225 static struct ust_registry_session
*get_session_registry(
226 struct ust_app_session
*ua_sess
)
228 struct ust_registry_session
*registry
= NULL
;
232 switch (ua_sess
->buffer_type
) {
233 case LTTNG_BUFFER_PER_PID
:
235 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
239 registry
= reg_pid
->registry
->reg
.ust
;
242 case LTTNG_BUFFER_PER_UID
:
244 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
245 ua_sess
->tracing_id
, ua_sess
->bits_per_long
, ua_sess
->uid
);
249 registry
= reg_uid
->registry
->reg
.ust
;
261 * Delete ust context safely. RCU read lock must be held before calling
265 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
)
272 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
273 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
274 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
275 sock
, ua_ctx
->obj
->handle
, ret
);
283 * Delete ust app event safely. RCU read lock must be held before calling
287 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
)
293 free(ua_event
->filter
);
294 if (ua_event
->exclusion
!= NULL
)
295 free(ua_event
->exclusion
);
296 if (ua_event
->obj
!= NULL
) {
297 ret
= ustctl_release_object(sock
, ua_event
->obj
);
298 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
299 ERR("UST app sock %d release event obj failed with ret %d",
308 * Release ust data object of the given stream.
310 * Return 0 on success or else a negative value.
312 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
319 ret
= ustctl_release_object(sock
, stream
->obj
);
320 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
321 ERR("UST app sock %d release stream obj failed with ret %d",
324 lttng_fd_put(LTTNG_FD_APPS
, 2);
332 * Delete ust app stream safely. RCU read lock must be held before calling
336 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
)
340 (void) release_ust_app_stream(sock
, stream
);
345 * We need to execute ht_destroy outside of RCU read-side critical
346 * section and outside of call_rcu thread, so we postpone its execution
347 * using ht_cleanup_push. It is simpler than to change the semantic of
348 * the many callers of delete_ust_app_session().
351 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
353 struct ust_app_channel
*ua_chan
=
354 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
356 ht_cleanup_push(ua_chan
->ctx
);
357 ht_cleanup_push(ua_chan
->events
);
362 * Delete ust app channel safely. RCU read lock must be held before calling
366 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
370 struct lttng_ht_iter iter
;
371 struct ust_app_event
*ua_event
;
372 struct ust_app_ctx
*ua_ctx
;
373 struct ust_app_stream
*stream
, *stmp
;
374 struct ust_registry_session
*registry
;
378 DBG3("UST app deleting channel %s", ua_chan
->name
);
381 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
382 cds_list_del(&stream
->list
);
383 delete_ust_app_stream(sock
, stream
);
387 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
388 cds_list_del(&ua_ctx
->list
);
389 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
391 delete_ust_app_ctx(sock
, ua_ctx
);
395 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
397 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
399 delete_ust_app_event(sock
, ua_event
);
402 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
403 /* Wipe and free registry from session registry. */
404 registry
= get_session_registry(ua_chan
->session
);
406 ust_registry_channel_del_free(registry
, ua_chan
->key
);
410 if (ua_chan
->obj
!= NULL
) {
411 /* Remove channel from application UST object descriptor. */
412 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
413 ret
= lttng_ht_del(app
->ust_objd
, &iter
);
415 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
416 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
417 ERR("UST app sock %d release channel obj failed with ret %d",
420 lttng_fd_put(LTTNG_FD_APPS
, 1);
423 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
427 * Push metadata to consumer socket.
429 * RCU read-side lock must be held to guarantee existance of socket.
430 * Must be called with the ust app session lock held.
431 * Must be called with the registry lock held.
433 * On success, return the len of metadata pushed or else a negative value.
434 * Returning a -EPIPE return value means we could not send the metadata,
435 * but it can be caused by recoverable errors (e.g. the application has
436 * terminated concurrently).
438 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
439 struct consumer_socket
*socket
, int send_zero_data
)
442 char *metadata_str
= NULL
;
443 size_t len
, offset
, new_metadata_len_sent
;
445 uint64_t metadata_key
;
450 metadata_key
= registry
->metadata_key
;
453 * Means that no metadata was assigned to the session. This can
454 * happens if no start has been done previously.
461 * On a push metadata error either the consumer is dead or the
462 * metadata channel has been destroyed because its endpoint
463 * might have died (e.g: relayd), or because the application has
464 * exited. If so, the metadata closed flag is set to 1 so we
465 * deny pushing metadata again which is not valid anymore on the
468 if (registry
->metadata_closed
) {
472 offset
= registry
->metadata_len_sent
;
473 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
474 new_metadata_len_sent
= registry
->metadata_len
;
476 DBG3("No metadata to push for metadata key %" PRIu64
,
477 registry
->metadata_key
);
479 if (send_zero_data
) {
480 DBG("No metadata to push");
486 /* Allocate only what we have to send. */
487 metadata_str
= zmalloc(len
);
489 PERROR("zmalloc ust app metadata string");
493 /* Copy what we haven't sent out. */
494 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
497 pthread_mutex_unlock(®istry
->lock
);
499 * We need to unlock the registry while we push metadata to
500 * break a circular dependency between the consumerd metadata
501 * lock and the sessiond registry lock. Indeed, pushing metadata
502 * to the consumerd awaits that it gets pushed all the way to
503 * relayd, but doing so requires grabbing the metadata lock. If
504 * a concurrent metadata request is being performed by
505 * consumerd, this can try to grab the registry lock on the
506 * sessiond while holding the metadata lock on the consumer
507 * daemon. Those push and pull schemes are performed on two
508 * different bidirectionnal communication sockets.
510 ret
= consumer_push_metadata(socket
, metadata_key
,
511 metadata_str
, len
, offset
);
512 pthread_mutex_lock(®istry
->lock
);
515 * There is an acceptable race here between the registry
516 * metadata key assignment and the creation on the
517 * consumer. The session daemon can concurrently push
518 * metadata for this registry while being created on the
519 * consumer since the metadata key of the registry is
520 * assigned *before* it is setup to avoid the consumer
521 * to ask for metadata that could possibly be not found
522 * in the session daemon.
524 * The metadata will get pushed either by the session
525 * being stopped or the consumer requesting metadata if
526 * that race is triggered.
528 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
531 ERR("Error pushing metadata to consumer");
537 * Metadata may have been concurrently pushed, since
538 * we're not holding the registry lock while pushing to
539 * consumer. This is handled by the fact that we send
540 * the metadata content, size, and the offset at which
541 * that metadata belongs. This may arrive out of order
542 * on the consumer side, and the consumer is able to
543 * deal with overlapping fragments. The consumer
544 * supports overlapping fragments, which must be
545 * contiguous starting from offset 0. We keep the
546 * largest metadata_len_sent value of the concurrent
549 registry
->metadata_len_sent
=
550 max_t(size_t, registry
->metadata_len_sent
,
551 new_metadata_len_sent
);
560 * On error, flag the registry that the metadata is
561 * closed. We were unable to push anything and this
562 * means that either the consumer is not responding or
563 * the metadata cache has been destroyed on the
566 registry
->metadata_closed
= 1;
574 * For a given application and session, push metadata to consumer.
575 * Either sock or consumer is required : if sock is NULL, the default
576 * socket to send the metadata is retrieved from consumer, if sock
577 * is not NULL we use it to send the metadata.
578 * RCU read-side lock must be held while calling this function,
579 * therefore ensuring existance of registry. It also ensures existance
580 * of socket throughout this function.
582 * Return 0 on success else a negative error.
583 * Returning a -EPIPE return value means we could not send the metadata,
584 * but it can be caused by recoverable errors (e.g. the application has
585 * terminated concurrently).
587 static int push_metadata(struct ust_registry_session
*registry
,
588 struct consumer_output
*consumer
)
592 struct consumer_socket
*socket
;
597 pthread_mutex_lock(®istry
->lock
);
598 if (registry
->metadata_closed
) {
603 /* Get consumer socket to use to push the metadata.*/
604 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
611 ret
= ust_app_push_metadata(registry
, socket
, 0);
616 pthread_mutex_unlock(®istry
->lock
);
620 pthread_mutex_unlock(®istry
->lock
);
625 * Send to the consumer a close metadata command for the given session. Once
626 * done, the metadata channel is deleted and the session metadata pointer is
627 * nullified. The session lock MUST be held unless the application is
628 * in the destroy path.
630 * Return 0 on success else a negative value.
632 static int close_metadata(struct ust_registry_session
*registry
,
633 struct consumer_output
*consumer
)
636 struct consumer_socket
*socket
;
643 pthread_mutex_lock(®istry
->lock
);
645 if (!registry
->metadata_key
|| registry
->metadata_closed
) {
650 /* Get consumer socket to use to push the metadata.*/
651 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
658 ret
= consumer_close_metadata(socket
, registry
->metadata_key
);
665 * Metadata closed. Even on error this means that the consumer is not
666 * responding or not found so either way a second close should NOT be emit
669 registry
->metadata_closed
= 1;
671 pthread_mutex_unlock(®istry
->lock
);
677 * We need to execute ht_destroy outside of RCU read-side critical
678 * section and outside of call_rcu thread, so we postpone its execution
679 * using ht_cleanup_push. It is simpler than to change the semantic of
680 * the many callers of delete_ust_app_session().
683 void delete_ust_app_session_rcu(struct rcu_head
*head
)
685 struct ust_app_session
*ua_sess
=
686 caa_container_of(head
, struct ust_app_session
, rcu_head
);
688 ht_cleanup_push(ua_sess
->channels
);
693 * Delete ust app session safely. RCU read lock must be held before calling
697 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
701 struct lttng_ht_iter iter
;
702 struct ust_app_channel
*ua_chan
;
703 struct ust_registry_session
*registry
;
707 pthread_mutex_lock(&ua_sess
->lock
);
709 assert(!ua_sess
->deleted
);
710 ua_sess
->deleted
= true;
712 registry
= get_session_registry(ua_sess
);
714 /* Push metadata for application before freeing the application. */
715 (void) push_metadata(registry
, ua_sess
->consumer
);
718 * Don't ask to close metadata for global per UID buffers. Close
719 * metadata only on destroy trace session in this case. Also, the
720 * previous push metadata could have flag the metadata registry to
721 * close so don't send a close command if closed.
723 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
724 /* And ask to close it for this session registry. */
725 (void) close_metadata(registry
, ua_sess
->consumer
);
729 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
731 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
733 delete_ust_app_channel(sock
, ua_chan
, app
);
736 /* In case of per PID, the registry is kept in the session. */
737 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
738 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
740 buffer_reg_pid_remove(reg_pid
);
741 buffer_reg_pid_destroy(reg_pid
);
745 if (ua_sess
->handle
!= -1) {
746 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
747 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
748 ERR("UST app sock %d release session handle failed with ret %d",
752 pthread_mutex_unlock(&ua_sess
->lock
);
754 consumer_output_put(ua_sess
->consumer
);
756 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
760 * Delete a traceable application structure from the global list. Never call
761 * this function outside of a call_rcu call.
763 * RCU read side lock should _NOT_ be held when calling this function.
766 void delete_ust_app(struct ust_app
*app
)
769 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
771 /* Delete ust app sessions info */
776 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
778 /* Free every object in the session and the session. */
780 delete_ust_app_session(sock
, ua_sess
, app
);
784 ht_cleanup_push(app
->sessions
);
785 ht_cleanup_push(app
->ust_objd
);
788 * Wait until we have deleted the application from the sock hash table
789 * before closing this socket, otherwise an application could re-use the
790 * socket ID and race with the teardown, using the same hash table entry.
792 * It's OK to leave the close in call_rcu. We want it to stay unique for
793 * all RCU readers that could run concurrently with unregister app,
794 * therefore we _need_ to only close that socket after a grace period. So
795 * it should stay in this RCU callback.
797 * This close() is a very important step of the synchronization model so
798 * every modification to this function must be carefully reviewed.
804 lttng_fd_put(LTTNG_FD_APPS
, 1);
806 DBG2("UST app pid %d deleted", app
->pid
);
811 * URCU intermediate call to delete an UST app.
814 void delete_ust_app_rcu(struct rcu_head
*head
)
816 struct lttng_ht_node_ulong
*node
=
817 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
818 struct ust_app
*app
=
819 caa_container_of(node
, struct ust_app
, pid_n
);
821 DBG3("Call RCU deleting app PID %d", app
->pid
);
826 * Delete the session from the application ht and delete the data structure by
827 * freeing every object inside and releasing them.
829 static void destroy_app_session(struct ust_app
*app
,
830 struct ust_app_session
*ua_sess
)
833 struct lttng_ht_iter iter
;
838 iter
.iter
.node
= &ua_sess
->node
.node
;
839 ret
= lttng_ht_del(app
->sessions
, &iter
);
841 /* Already scheduled for teardown. */
845 /* Once deleted, free the data structure. */
846 delete_ust_app_session(app
->sock
, ua_sess
, app
);
853 * Alloc new UST app session.
856 struct ust_app_session
*alloc_ust_app_session(struct ust_app
*app
)
858 struct ust_app_session
*ua_sess
;
860 /* Init most of the default value by allocating and zeroing */
861 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
862 if (ua_sess
== NULL
) {
867 ua_sess
->handle
= -1;
868 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
869 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
870 pthread_mutex_init(&ua_sess
->lock
, NULL
);
879 * Alloc new UST app channel.
882 struct ust_app_channel
*alloc_ust_app_channel(char *name
,
883 struct ust_app_session
*ua_sess
,
884 struct lttng_ust_channel_attr
*attr
)
886 struct ust_app_channel
*ua_chan
;
888 /* Init most of the default value by allocating and zeroing */
889 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
890 if (ua_chan
== NULL
) {
895 /* Setup channel name */
896 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
897 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
899 ua_chan
->enabled
= 1;
900 ua_chan
->handle
= -1;
901 ua_chan
->session
= ua_sess
;
902 ua_chan
->key
= get_next_channel_key();
903 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
904 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
905 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
907 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
908 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
910 /* Copy attributes */
912 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
913 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
914 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
915 ua_chan
->attr
.overwrite
= attr
->overwrite
;
916 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
917 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
918 ua_chan
->attr
.output
= attr
->output
;
920 /* By default, the channel is a per cpu channel. */
921 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
923 DBG3("UST app channel %s allocated", ua_chan
->name
);
932 * Allocate and initialize a UST app stream.
934 * Return newly allocated stream pointer or NULL on error.
936 struct ust_app_stream
*ust_app_alloc_stream(void)
938 struct ust_app_stream
*stream
= NULL
;
940 stream
= zmalloc(sizeof(*stream
));
941 if (stream
== NULL
) {
942 PERROR("zmalloc ust app stream");
946 /* Zero could be a valid value for a handle so flag it to -1. */
954 * Alloc new UST app event.
957 struct ust_app_event
*alloc_ust_app_event(char *name
,
958 struct lttng_ust_event
*attr
)
960 struct ust_app_event
*ua_event
;
962 /* Init most of the default value by allocating and zeroing */
963 ua_event
= zmalloc(sizeof(struct ust_app_event
));
964 if (ua_event
== NULL
) {
969 ua_event
->enabled
= 1;
970 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
971 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
972 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
974 /* Copy attributes */
976 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
979 DBG3("UST app event %s allocated", ua_event
->name
);
988 * Alloc new UST app context.
991 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context
*uctx
)
993 struct ust_app_ctx
*ua_ctx
;
995 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
996 if (ua_ctx
== NULL
) {
1000 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1003 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1006 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1013 * Allocate a filter and copy the given original filter.
1015 * Return allocated filter or NULL on error.
1017 static struct lttng_filter_bytecode
*copy_filter_bytecode(
1018 struct lttng_filter_bytecode
*orig_f
)
1020 struct lttng_filter_bytecode
*filter
= NULL
;
1022 /* Copy filter bytecode */
1023 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1025 PERROR("zmalloc alloc filter bytecode");
1029 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1036 * Create a liblttng-ust filter bytecode from given bytecode.
1038 * Return allocated filter or NULL on error.
1040 static struct lttng_ust_filter_bytecode
*create_ust_bytecode_from_bytecode(
1041 struct lttng_filter_bytecode
*orig_f
)
1043 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1045 /* Copy filter bytecode */
1046 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1048 PERROR("zmalloc alloc ust filter bytecode");
1052 assert(sizeof(struct lttng_filter_bytecode
) ==
1053 sizeof(struct lttng_ust_filter_bytecode
));
1054 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1060 * Find an ust_app using the sock and return it. RCU read side lock must be
1061 * held before calling this helper function.
1063 struct ust_app
*ust_app_find_by_sock(int sock
)
1065 struct lttng_ht_node_ulong
*node
;
1066 struct lttng_ht_iter iter
;
1068 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1069 node
= lttng_ht_iter_get_node_ulong(&iter
);
1071 DBG2("UST app find by sock %d not found", sock
);
1075 return caa_container_of(node
, struct ust_app
, sock_n
);
1082 * Find an ust_app using the notify sock and return it. RCU read side lock must
1083 * be held before calling this helper function.
1085 static struct ust_app
*find_app_by_notify_sock(int sock
)
1087 struct lttng_ht_node_ulong
*node
;
1088 struct lttng_ht_iter iter
;
1090 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1092 node
= lttng_ht_iter_get_node_ulong(&iter
);
1094 DBG2("UST app find by notify sock %d not found", sock
);
1098 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1105 * Lookup for an ust app event based on event name, filter bytecode and the
1108 * Return an ust_app_event object or NULL on error.
1110 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1111 char *name
, struct lttng_filter_bytecode
*filter
, int loglevel
,
1112 const struct lttng_event_exclusion
*exclusion
)
1114 struct lttng_ht_iter iter
;
1115 struct lttng_ht_node_str
*node
;
1116 struct ust_app_event
*event
= NULL
;
1117 struct ust_app_ht_key key
;
1122 /* Setup key for event lookup. */
1124 key
.filter
= filter
;
1125 key
.loglevel
= loglevel
;
1126 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1127 key
.exclusion
= exclusion
;
1129 /* Lookup using the event name as hash and a custom match fct. */
1130 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1131 ht_match_ust_app_event
, &key
, &iter
.iter
);
1132 node
= lttng_ht_iter_get_node_str(&iter
);
1137 event
= caa_container_of(node
, struct ust_app_event
, node
);
1144 * Create the channel context on the tracer.
1146 * Called with UST app session lock held.
1149 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1150 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1154 health_code_update();
1156 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1157 ua_chan
->obj
, &ua_ctx
->obj
);
1159 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1160 ERR("UST app create channel context failed for app (pid: %d) "
1161 "with ret %d", app
->pid
, ret
);
1164 * This is normal behavior, an application can die during the
1165 * creation process. Don't report an error so the execution can
1166 * continue normally.
1169 DBG3("UST app disable event failed. Application is dead.");
1174 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1176 DBG2("UST app context handle %d created successfully for channel %s",
1177 ua_ctx
->handle
, ua_chan
->name
);
1180 health_code_update();
1185 * Set the filter on the tracer.
1188 int set_ust_event_filter(struct ust_app_event
*ua_event
,
1189 struct ust_app
*app
)
1192 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1194 health_code_update();
1196 if (!ua_event
->filter
) {
1201 ust_bytecode
= create_ust_bytecode_from_bytecode(ua_event
->filter
);
1202 if (!ust_bytecode
) {
1203 ret
= -LTTNG_ERR_NOMEM
;
1206 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1209 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1210 ERR("UST app event %s filter failed for app (pid: %d) "
1211 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1214 * This is normal behavior, an application can die during the
1215 * creation process. Don't report an error so the execution can
1216 * continue normally.
1219 DBG3("UST app filter event failed. Application is dead.");
1224 DBG2("UST filter set successfully for event %s", ua_event
->name
);
1227 health_code_update();
1233 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1234 struct lttng_event_exclusion
*exclusion
)
1236 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1237 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1238 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1240 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1241 if (!ust_exclusion
) {
1246 assert(sizeof(struct lttng_event_exclusion
) ==
1247 sizeof(struct lttng_ust_event_exclusion
));
1248 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1250 return ust_exclusion
;
1254 * Set event exclusions on the tracer.
1257 int set_ust_event_exclusion(struct ust_app_event
*ua_event
,
1258 struct ust_app
*app
)
1261 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1263 health_code_update();
1265 if (!ua_event
->exclusion
|| !ua_event
->exclusion
->count
) {
1270 ust_exclusion
= create_ust_exclusion_from_exclusion(
1271 ua_event
->exclusion
);
1272 if (!ust_exclusion
) {
1273 ret
= -LTTNG_ERR_NOMEM
;
1276 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusion
, ua_event
->obj
);
1278 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1279 ERR("UST app event %s exclusions failed for app (pid: %d) "
1280 "with ret %d", ua_event
->attr
.name
, app
->pid
, ret
);
1283 * This is normal behavior, an application can die during the
1284 * creation process. Don't report an error so the execution can
1285 * continue normally.
1288 DBG3("UST app event exclusion failed. Application is dead.");
1293 DBG2("UST exclusion set successfully for event %s", ua_event
->name
);
1296 health_code_update();
1297 free(ust_exclusion
);
1302 * Disable the specified event on to UST tracer for the UST session.
1304 static int disable_ust_event(struct ust_app
*app
,
1305 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1309 health_code_update();
1311 ret
= ustctl_disable(app
->sock
, ua_event
->obj
);
1313 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1314 ERR("UST app event %s disable failed for app (pid: %d) "
1315 "and session handle %d with ret %d",
1316 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1319 * This is normal behavior, an application can die during the
1320 * creation process. Don't report an error so the execution can
1321 * continue normally.
1324 DBG3("UST app disable event failed. Application is dead.");
1329 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1330 ua_event
->attr
.name
, app
->pid
);
1333 health_code_update();
1338 * Disable the specified channel on to UST tracer for the UST session.
1340 static int disable_ust_channel(struct ust_app
*app
,
1341 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1345 health_code_update();
1347 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
1349 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1350 ERR("UST app channel %s disable failed for app (pid: %d) "
1351 "and session handle %d with ret %d",
1352 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1355 * This is normal behavior, an application can die during the
1356 * creation process. Don't report an error so the execution can
1357 * continue normally.
1360 DBG3("UST app disable channel failed. Application is dead.");
1365 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1366 ua_chan
->name
, app
->pid
);
1369 health_code_update();
1374 * Enable the specified channel on to UST tracer for the UST session.
1376 static int enable_ust_channel(struct ust_app
*app
,
1377 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1381 health_code_update();
1383 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
1385 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1386 ERR("UST app channel %s enable failed for app (pid: %d) "
1387 "and session handle %d with ret %d",
1388 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
1391 * This is normal behavior, an application can die during the
1392 * creation process. Don't report an error so the execution can
1393 * continue normally.
1396 DBG3("UST app enable channel failed. Application is dead.");
1401 ua_chan
->enabled
= 1;
1403 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1404 ua_chan
->name
, app
->pid
);
1407 health_code_update();
1412 * Enable the specified event on to UST tracer for the UST session.
1414 static int enable_ust_event(struct ust_app
*app
,
1415 struct ust_app_session
*ua_sess
, struct ust_app_event
*ua_event
)
1419 health_code_update();
1421 ret
= ustctl_enable(app
->sock
, ua_event
->obj
);
1423 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1424 ERR("UST app event %s enable failed for app (pid: %d) "
1425 "and session handle %d with ret %d",
1426 ua_event
->attr
.name
, app
->pid
, ua_sess
->handle
, ret
);
1429 * This is normal behavior, an application can die during the
1430 * creation process. Don't report an error so the execution can
1431 * continue normally.
1434 DBG3("UST app enable event failed. Application is dead.");
1439 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1440 ua_event
->attr
.name
, app
->pid
);
1443 health_code_update();
1448 * Send channel and stream buffer to application.
1450 * Return 0 on success. On error, a negative value is returned.
1452 static int send_channel_pid_to_ust(struct ust_app
*app
,
1453 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
1456 struct ust_app_stream
*stream
, *stmp
;
1462 health_code_update();
1464 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
1467 /* Send channel to the application. */
1468 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
1469 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1470 ret
= -ENOTCONN
; /* Caused by app exiting. */
1472 } else if (ret
< 0) {
1476 health_code_update();
1478 /* Send all streams to application. */
1479 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
1480 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
1481 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
1482 ret
= -ENOTCONN
; /* Caused by app exiting. */
1484 } else if (ret
< 0) {
1487 /* We don't need the stream anymore once sent to the tracer. */
1488 cds_list_del(&stream
->list
);
1489 delete_ust_app_stream(-1, stream
);
1491 /* Flag the channel that it is sent to the application. */
1492 ua_chan
->is_sent
= 1;
1495 health_code_update();
1500 * Create the specified event onto the UST tracer for a UST session.
1502 * Should be called with session mutex held.
1505 int create_ust_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
1506 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
1510 health_code_update();
1512 /* Create UST event on tracer */
1513 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
1516 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1517 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1518 ua_event
->attr
.name
, app
->pid
, ret
);
1521 * This is normal behavior, an application can die during the
1522 * creation process. Don't report an error so the execution can
1523 * continue normally.
1526 DBG3("UST app create event failed. Application is dead.");
1531 ua_event
->handle
= ua_event
->obj
->handle
;
1533 DBG2("UST app event %s created successfully for pid:%d",
1534 ua_event
->attr
.name
, app
->pid
);
1536 health_code_update();
1538 /* Set filter if one is present. */
1539 if (ua_event
->filter
) {
1540 ret
= set_ust_event_filter(ua_event
, app
);
1546 /* Set exclusions for the event */
1547 if (ua_event
->exclusion
) {
1548 ret
= set_ust_event_exclusion(ua_event
, app
);
1554 /* If event not enabled, disable it on the tracer */
1555 if (ua_event
->enabled
) {
1557 * We now need to explicitly enable the event, since it
1558 * is now disabled at creation.
1560 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
1563 * If we hit an EPERM, something is wrong with our enable call. If
1564 * we get an EEXIST, there is a problem on the tracer side since we
1568 case -LTTNG_UST_ERR_PERM
:
1569 /* Code flow problem */
1571 case -LTTNG_UST_ERR_EXIST
:
1572 /* It's OK for our use case. */
1583 health_code_update();
1588 * Copy data between an UST app event and a LTT event.
1590 static void shadow_copy_event(struct ust_app_event
*ua_event
,
1591 struct ltt_ust_event
*uevent
)
1593 size_t exclusion_alloc_size
;
1595 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
1596 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1598 ua_event
->enabled
= uevent
->enabled
;
1600 /* Copy event attributes */
1601 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
1603 /* Copy filter bytecode */
1604 if (uevent
->filter
) {
1605 ua_event
->filter
= copy_filter_bytecode(uevent
->filter
);
1606 /* Filter might be NULL here in case of ENONEM. */
1609 /* Copy exclusion data */
1610 if (uevent
->exclusion
) {
1611 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
1612 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
1613 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
1614 if (ua_event
->exclusion
== NULL
) {
1617 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
1618 exclusion_alloc_size
);
1624 * Copy data between an UST app channel and a LTT channel.
1626 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
1627 struct ltt_ust_channel
*uchan
)
1629 struct lttng_ht_iter iter
;
1630 struct ltt_ust_event
*uevent
;
1631 struct ltt_ust_context
*uctx
;
1632 struct ust_app_event
*ua_event
;
1633 struct ust_app_ctx
*ua_ctx
;
1635 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
1637 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
1638 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1640 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
1641 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
1643 /* Copy event attributes since the layout is different. */
1644 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
1645 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
1646 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
1647 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
1648 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
1649 ua_chan
->attr
.output
= uchan
->attr
.output
;
1651 * Note that the attribute channel type is not set since the channel on the
1652 * tracing registry side does not have this information.
1655 ua_chan
->enabled
= uchan
->enabled
;
1656 ua_chan
->tracing_channel_id
= uchan
->id
;
1658 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
1659 ua_ctx
= alloc_ust_app_ctx(&uctx
->ctx
);
1660 if (ua_ctx
== NULL
) {
1663 lttng_ht_node_init_ulong(&ua_ctx
->node
,
1664 (unsigned long) ua_ctx
->ctx
.ctx
);
1665 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
1666 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
1669 /* Copy all events from ltt ust channel to ust app channel */
1670 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
1671 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
1672 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
1673 if (ua_event
== NULL
) {
1674 DBG2("UST event %s not found on shadow copy channel",
1676 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
1677 if (ua_event
== NULL
) {
1680 shadow_copy_event(ua_event
, uevent
);
1681 add_unique_ust_app_event(ua_chan
, ua_event
);
1685 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
1689 * Copy data between a UST app session and a regular LTT session.
1691 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
1692 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1694 struct lttng_ht_node_str
*ua_chan_node
;
1695 struct lttng_ht_iter iter
;
1696 struct ltt_ust_channel
*uchan
;
1697 struct ust_app_channel
*ua_chan
;
1699 struct tm
*timeinfo
;
1702 char tmp_shm_path
[PATH_MAX
];
1704 /* Get date and time for unique app path */
1706 timeinfo
= localtime(&rawtime
);
1707 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
1709 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
1711 ua_sess
->tracing_id
= usess
->id
;
1712 ua_sess
->id
= get_next_session_id();
1713 ua_sess
->uid
= app
->uid
;
1714 ua_sess
->gid
= app
->gid
;
1715 ua_sess
->euid
= usess
->uid
;
1716 ua_sess
->egid
= usess
->gid
;
1717 ua_sess
->buffer_type
= usess
->buffer_type
;
1718 ua_sess
->bits_per_long
= app
->bits_per_long
;
1720 /* There is only one consumer object per session possible. */
1721 consumer_output_get(usess
->consumer
);
1722 ua_sess
->consumer
= usess
->consumer
;
1724 ua_sess
->output_traces
= usess
->output_traces
;
1725 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
1726 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
1727 &usess
->metadata_attr
);
1729 switch (ua_sess
->buffer_type
) {
1730 case LTTNG_BUFFER_PER_PID
:
1731 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1732 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
1735 case LTTNG_BUFFER_PER_UID
:
1736 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
1737 DEFAULT_UST_TRACE_UID_PATH
, ua_sess
->uid
, app
->bits_per_long
);
1744 PERROR("asprintf UST shadow copy session");
1749 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
1750 sizeof(ua_sess
->root_shm_path
));
1751 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
1752 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
1753 sizeof(ua_sess
->shm_path
));
1754 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1755 if (ua_sess
->shm_path
[0]) {
1756 switch (ua_sess
->buffer_type
) {
1757 case LTTNG_BUFFER_PER_PID
:
1758 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1759 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
1760 app
->name
, app
->pid
, datetime
);
1762 case LTTNG_BUFFER_PER_UID
:
1763 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
1764 DEFAULT_UST_TRACE_UID_PATH
,
1765 app
->uid
, app
->bits_per_long
);
1772 PERROR("sprintf UST shadow copy session");
1776 strncat(ua_sess
->shm_path
, tmp_shm_path
,
1777 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
1778 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
1781 /* Iterate over all channels in global domain. */
1782 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
1784 struct lttng_ht_iter uiter
;
1786 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
1787 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
1788 if (ua_chan_node
!= NULL
) {
1789 /* Session exist. Contiuing. */
1793 DBG2("Channel %s not found on shadow session copy, creating it",
1795 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
1796 if (ua_chan
== NULL
) {
1797 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1800 shadow_copy_channel(ua_chan
, uchan
);
1802 * The concept of metadata channel does not exist on the tracing
1803 * registry side of the session daemon so this can only be a per CPU
1804 * channel and not metadata.
1806 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1808 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
1813 consumer_output_put(ua_sess
->consumer
);
1817 * Lookup sesison wrapper.
1820 void __lookup_session_by_app(struct ltt_ust_session
*usess
,
1821 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
1823 /* Get right UST app session from app */
1824 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
1828 * Return ust app session from the app session hashtable using the UST session
1831 static struct ust_app_session
*lookup_session_by_app(
1832 struct ltt_ust_session
*usess
, struct ust_app
*app
)
1834 struct lttng_ht_iter iter
;
1835 struct lttng_ht_node_u64
*node
;
1837 __lookup_session_by_app(usess
, app
, &iter
);
1838 node
= lttng_ht_iter_get_node_u64(&iter
);
1843 return caa_container_of(node
, struct ust_app_session
, node
);
1850 * Setup buffer registry per PID for the given session and application. If none
1851 * is found, a new one is created, added to the global registry and
1852 * initialized. If regp is valid, it's set with the newly created object.
1854 * Return 0 on success or else a negative value.
1856 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
1857 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
1860 struct buffer_reg_pid
*reg_pid
;
1867 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1870 * This is the create channel path meaning that if there is NO
1871 * registry available, we have to create one for this session.
1873 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
1874 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1882 /* Initialize registry. */
1883 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
1884 app
->bits_per_long
, app
->uint8_t_alignment
,
1885 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1886 app
->uint64_t_alignment
, app
->long_alignment
,
1887 app
->byte_order
, app
->version
.major
,
1888 app
->version
.minor
, reg_pid
->root_shm_path
,
1890 ua_sess
->euid
, ua_sess
->egid
);
1893 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1894 * destroy the buffer registry, because it is always expected
1895 * that if the buffer registry can be found, its ust registry is
1898 buffer_reg_pid_destroy(reg_pid
);
1902 buffer_reg_pid_add(reg_pid
);
1904 DBG3("UST app buffer registry per PID created successfully");
1916 * Setup buffer registry per UID for the given session and application. If none
1917 * is found, a new one is created, added to the global registry and
1918 * initialized. If regp is valid, it's set with the newly created object.
1920 * Return 0 on success or else a negative value.
1922 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
1923 struct ust_app_session
*ua_sess
,
1924 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
1927 struct buffer_reg_uid
*reg_uid
;
1934 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
1937 * This is the create channel path meaning that if there is NO
1938 * registry available, we have to create one for this session.
1940 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
1941 LTTNG_DOMAIN_UST
, ®_uid
,
1942 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
1950 /* Initialize registry. */
1951 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
1952 app
->bits_per_long
, app
->uint8_t_alignment
,
1953 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
1954 app
->uint64_t_alignment
, app
->long_alignment
,
1955 app
->byte_order
, app
->version
.major
,
1956 app
->version
.minor
, reg_uid
->root_shm_path
,
1957 reg_uid
->shm_path
, usess
->uid
, usess
->gid
);
1960 * reg_uid->registry->reg.ust is NULL upon error, so we need to
1961 * destroy the buffer registry, because it is always expected
1962 * that if the buffer registry can be found, its ust registry is
1965 buffer_reg_uid_destroy(reg_uid
, NULL
);
1968 /* Add node to teardown list of the session. */
1969 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
1971 buffer_reg_uid_add(reg_uid
);
1973 DBG3("UST app buffer registry per UID created successfully");
1984 * Create a session on the tracer side for the given app.
1986 * On success, ua_sess_ptr is populated with the session pointer or else left
1987 * untouched. If the session was created, is_created is set to 1. On error,
1988 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1991 * Returns 0 on success or else a negative code which is either -ENOMEM or
1992 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1994 static int create_ust_app_session(struct ltt_ust_session
*usess
,
1995 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
1998 int ret
, created
= 0;
1999 struct ust_app_session
*ua_sess
;
2003 assert(ua_sess_ptr
);
2005 health_code_update();
2007 ua_sess
= lookup_session_by_app(usess
, app
);
2008 if (ua_sess
== NULL
) {
2009 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
2010 app
->pid
, usess
->id
);
2011 ua_sess
= alloc_ust_app_session(app
);
2012 if (ua_sess
== NULL
) {
2013 /* Only malloc can failed so something is really wrong */
2017 shadow_copy_session(ua_sess
, usess
, app
);
2021 switch (usess
->buffer_type
) {
2022 case LTTNG_BUFFER_PER_PID
:
2023 /* Init local registry. */
2024 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
2026 delete_ust_app_session(-1, ua_sess
, app
);
2030 case LTTNG_BUFFER_PER_UID
:
2031 /* Look for a global registry. If none exists, create one. */
2032 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
2034 delete_ust_app_session(-1, ua_sess
, app
);
2044 health_code_update();
2046 if (ua_sess
->handle
== -1) {
2047 ret
= ustctl_create_session(app
->sock
);
2049 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2050 ERR("Creating session for app pid %d with ret %d",
2053 DBG("UST app creating session failed. Application is dead");
2055 * This is normal behavior, an application can die during the
2056 * creation process. Don't report an error so the execution can
2057 * continue normally. This will get flagged ENOTCONN and the
2058 * caller will handle it.
2062 delete_ust_app_session(-1, ua_sess
, app
);
2063 if (ret
!= -ENOMEM
) {
2065 * Tracer is probably gone or got an internal error so let's
2066 * behave like it will soon unregister or not usable.
2073 ua_sess
->handle
= ret
;
2075 /* Add ust app session to app's HT */
2076 lttng_ht_node_init_u64(&ua_sess
->node
,
2077 ua_sess
->tracing_id
);
2078 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
2080 DBG2("UST app session created successfully with handle %d", ret
);
2083 *ua_sess_ptr
= ua_sess
;
2085 *is_created
= created
;
2088 /* Everything went well. */
2092 health_code_update();
2097 * Match function for a hash table lookup of ust_app_ctx.
2099 * It matches an ust app context based on the context type and, in the case
2100 * of perf counters, their name.
2102 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
2104 struct ust_app_ctx
*ctx
;
2105 const struct lttng_ust_context
*key
;
2110 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
2114 if (ctx
->ctx
.ctx
!= key
->ctx
) {
2118 /* Check the name in the case of perf thread counters. */
2119 if (key
->ctx
== LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
) {
2120 if (strncmp(key
->u
.perf_counter
.name
,
2121 ctx
->ctx
.u
.perf_counter
.name
,
2122 sizeof(key
->u
.perf_counter
.name
))) {
2135 * Lookup for an ust app context from an lttng_ust_context.
2137 * Must be called while holding RCU read side lock.
2138 * Return an ust_app_ctx object or NULL on error.
2141 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
2142 struct lttng_ust_context
*uctx
)
2144 struct lttng_ht_iter iter
;
2145 struct lttng_ht_node_ulong
*node
;
2146 struct ust_app_ctx
*app_ctx
= NULL
;
2151 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2152 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
2153 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
2154 node
= lttng_ht_iter_get_node_ulong(&iter
);
2159 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
2166 * Create a context for the channel on the tracer.
2168 * Called with UST app session lock held and a RCU read side lock.
2171 int create_ust_app_channel_context(struct ust_app_session
*ua_sess
,
2172 struct ust_app_channel
*ua_chan
, struct lttng_ust_context
*uctx
,
2173 struct ust_app
*app
)
2176 struct ust_app_ctx
*ua_ctx
;
2178 DBG2("UST app adding context to channel %s", ua_chan
->name
);
2180 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
2186 ua_ctx
= alloc_ust_app_ctx(uctx
);
2187 if (ua_ctx
== NULL
) {
2193 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
2194 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
2195 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
2197 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
2207 * Enable on the tracer side a ust app event for the session and channel.
2209 * Called with UST app session lock held.
2212 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
2213 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2217 ret
= enable_ust_event(app
, ua_sess
, ua_event
);
2222 ua_event
->enabled
= 1;
2229 * Disable on the tracer side a ust app event for the session and channel.
2231 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
2232 struct ust_app_event
*ua_event
, struct ust_app
*app
)
2236 ret
= disable_ust_event(app
, ua_sess
, ua_event
);
2241 ua_event
->enabled
= 0;
2248 * Lookup ust app channel for session and disable it on the tracer side.
2251 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
2252 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
2256 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2261 ua_chan
->enabled
= 0;
2268 * Lookup ust app channel for session and enable it on the tracer side. This
2269 * MUST be called with a RCU read side lock acquired.
2271 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
2272 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
2275 struct lttng_ht_iter iter
;
2276 struct lttng_ht_node_str
*ua_chan_node
;
2277 struct ust_app_channel
*ua_chan
;
2279 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2280 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2281 if (ua_chan_node
== NULL
) {
2282 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
2283 uchan
->name
, ua_sess
->tracing_id
);
2287 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2289 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
2299 * Ask the consumer to create a channel and get it if successful.
2301 * Return 0 on success or else a negative value.
2303 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
2304 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
2305 int bitness
, struct ust_registry_session
*registry
)
2308 unsigned int nb_fd
= 0;
2309 struct consumer_socket
*socket
;
2317 health_code_update();
2319 /* Get the right consumer socket for the application. */
2320 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
2326 health_code_update();
2328 /* Need one fd for the channel. */
2329 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2331 ERR("Exhausted number of available FD upon create channel");
2336 * Ask consumer to create channel. The consumer will return the number of
2337 * stream we have to expect.
2339 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
2346 * Compute the number of fd needed before receiving them. It must be 2 per
2347 * stream (2 being the default value here).
2349 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
2351 /* Reserve the amount of file descriptor we need. */
2352 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
2354 ERR("Exhausted number of available FD upon create channel");
2355 goto error_fd_get_stream
;
2358 health_code_update();
2361 * Now get the channel from the consumer. This call wil populate the stream
2362 * list of that channel and set the ust objects.
2364 if (usess
->consumer
->enabled
) {
2365 ret
= ust_consumer_get_channel(socket
, ua_chan
);
2375 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
2376 error_fd_get_stream
:
2378 * Initiate a destroy channel on the consumer since we had an error
2379 * handling it on our side. The return value is of no importance since we
2380 * already have a ret value set by the previous error that we need to
2383 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
2385 lttng_fd_put(LTTNG_FD_APPS
, 1);
2387 health_code_update();
2393 * Duplicate the ust data object of the ust app stream and save it in the
2394 * buffer registry stream.
2396 * Return 0 on success or else a negative value.
2398 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
2399 struct ust_app_stream
*stream
)
2406 /* Reserve the amount of file descriptor we need. */
2407 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
2409 ERR("Exhausted number of available FD upon duplicate stream");
2413 /* Duplicate object for stream once the original is in the registry. */
2414 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
2415 reg_stream
->obj
.ust
);
2417 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2418 reg_stream
->obj
.ust
, stream
->obj
, ret
);
2419 lttng_fd_put(LTTNG_FD_APPS
, 2);
2422 stream
->handle
= stream
->obj
->handle
;
2429 * Duplicate the ust data object of the ust app. channel and save it in the
2430 * buffer registry channel.
2432 * Return 0 on success or else a negative value.
2434 static int duplicate_channel_object(struct buffer_reg_channel
*reg_chan
,
2435 struct ust_app_channel
*ua_chan
)
2442 /* Need two fds for the channel. */
2443 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
2445 ERR("Exhausted number of available FD upon duplicate channel");
2449 /* Duplicate object for stream once the original is in the registry. */
2450 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, reg_chan
->obj
.ust
);
2452 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2453 reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
2456 ua_chan
->handle
= ua_chan
->obj
->handle
;
2461 lttng_fd_put(LTTNG_FD_APPS
, 1);
2467 * For a given channel buffer registry, setup all streams of the given ust
2468 * application channel.
2470 * Return 0 on success or else a negative value.
2472 static int setup_buffer_reg_streams(struct buffer_reg_channel
*reg_chan
,
2473 struct ust_app_channel
*ua_chan
)
2476 struct ust_app_stream
*stream
, *stmp
;
2481 DBG2("UST app setup buffer registry stream");
2483 /* Send all streams to application. */
2484 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2485 struct buffer_reg_stream
*reg_stream
;
2487 ret
= buffer_reg_stream_create(®_stream
);
2493 * Keep original pointer and nullify it in the stream so the delete
2494 * stream call does not release the object.
2496 reg_stream
->obj
.ust
= stream
->obj
;
2498 buffer_reg_stream_add(reg_stream
, reg_chan
);
2500 /* We don't need the streams anymore. */
2501 cds_list_del(&stream
->list
);
2502 delete_ust_app_stream(-1, stream
);
2510 * Create a buffer registry channel for the given session registry and
2511 * application channel object. If regp pointer is valid, it's set with the
2512 * created object. Important, the created object is NOT added to the session
2513 * registry hash table.
2515 * Return 0 on success else a negative value.
2517 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2518 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
2521 struct buffer_reg_channel
*reg_chan
= NULL
;
2526 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
2528 /* Create buffer registry channel. */
2529 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, ®_chan
);
2534 reg_chan
->consumer_key
= ua_chan
->key
;
2535 reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
2536 reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
2538 /* Create and add a channel registry to session. */
2539 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
2540 ua_chan
->tracing_channel_id
);
2544 buffer_reg_channel_add(reg_sess
, reg_chan
);
2553 /* Safe because the registry channel object was not added to any HT. */
2554 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2560 * Setup buffer registry channel for the given session registry and application
2561 * channel object. If regp pointer is valid, it's set with the created object.
2563 * Return 0 on success else a negative value.
2565 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
2566 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*reg_chan
)
2573 assert(ua_chan
->obj
);
2575 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
2577 /* Setup all streams for the registry. */
2578 ret
= setup_buffer_reg_streams(reg_chan
, ua_chan
);
2583 reg_chan
->obj
.ust
= ua_chan
->obj
;
2584 ua_chan
->obj
= NULL
;
2589 buffer_reg_channel_remove(reg_sess
, reg_chan
);
2590 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2595 * Send buffer registry channel to the application.
2597 * Return 0 on success else a negative value.
2599 static int send_channel_uid_to_ust(struct buffer_reg_channel
*reg_chan
,
2600 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2601 struct ust_app_channel
*ua_chan
)
2604 struct buffer_reg_stream
*reg_stream
;
2611 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
2613 ret
= duplicate_channel_object(reg_chan
, ua_chan
);
2618 /* Send channel to the application. */
2619 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2620 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2621 ret
= -ENOTCONN
; /* Caused by app exiting. */
2623 } else if (ret
< 0) {
2627 health_code_update();
2629 /* Send all streams to application. */
2630 pthread_mutex_lock(®_chan
->stream_list_lock
);
2631 cds_list_for_each_entry(reg_stream
, ®_chan
->streams
, lnode
) {
2632 struct ust_app_stream stream
;
2634 ret
= duplicate_stream_object(reg_stream
, &stream
);
2636 goto error_stream_unlock
;
2639 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
2641 (void) release_ust_app_stream(-1, &stream
);
2642 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2643 ret
= -ENOTCONN
; /* Caused by app exiting. */
2644 goto error_stream_unlock
;
2645 } else if (ret
< 0) {
2646 goto error_stream_unlock
;
2648 goto error_stream_unlock
;
2652 * The return value is not important here. This function will output an
2655 (void) release_ust_app_stream(-1, &stream
);
2657 ua_chan
->is_sent
= 1;
2659 error_stream_unlock
:
2660 pthread_mutex_unlock(®_chan
->stream_list_lock
);
2666 * Create and send to the application the created buffers with per UID buffers.
2668 * Return 0 on success else a negative value.
2670 static int create_channel_per_uid(struct ust_app
*app
,
2671 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2672 struct ust_app_channel
*ua_chan
)
2675 struct buffer_reg_uid
*reg_uid
;
2676 struct buffer_reg_channel
*reg_chan
;
2683 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
2685 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
2687 * The session creation handles the creation of this global registry
2688 * object. If none can be find, there is a code flow problem or a
2693 reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
2696 /* Create the buffer registry channel object. */
2697 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, ®_chan
);
2699 ERR("Error creating the UST channel \"%s\" registry instance",
2706 * Create the buffers on the consumer side. This call populates the
2707 * ust app channel object with all streams and data object.
2709 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2710 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
);
2712 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2716 * Let's remove the previously created buffer registry channel so
2717 * it's not visible anymore in the session registry.
2719 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
2720 ua_chan
->tracing_channel_id
);
2721 buffer_reg_channel_remove(reg_uid
->registry
, reg_chan
);
2722 buffer_reg_channel_destroy(reg_chan
, LTTNG_DOMAIN_UST
);
2727 * Setup the streams and add it to the session registry.
2729 ret
= setup_buffer_reg_channel(reg_uid
->registry
, ua_chan
, reg_chan
);
2731 ERR("Error setting up UST channel \"%s\"",
2738 /* Send buffers to the application. */
2739 ret
= send_channel_uid_to_ust(reg_chan
, app
, ua_sess
, ua_chan
);
2741 if (ret
!= -ENOTCONN
) {
2742 ERR("Error sending channel to application");
2752 * Create and send to the application the created buffers with per PID buffers.
2754 * Return 0 on success else a negative value.
2756 static int create_channel_per_pid(struct ust_app
*app
,
2757 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2758 struct ust_app_channel
*ua_chan
)
2761 struct ust_registry_session
*registry
;
2768 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
2772 registry
= get_session_registry(ua_sess
);
2775 /* Create and add a new channel registry to session. */
2776 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
2778 ERR("Error creating the UST channel \"%s\" registry instance",
2783 /* Create and get channel on the consumer side. */
2784 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
2785 app
->bits_per_long
, registry
);
2787 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2792 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
2794 if (ret
!= -ENOTCONN
) {
2795 ERR("Error sending channel to application");
2806 * From an already allocated ust app channel, create the channel buffers if
2807 * need and send it to the application. This MUST be called with a RCU read
2808 * side lock acquired.
2810 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2811 * the application exited concurrently.
2813 static int do_create_channel(struct ust_app
*app
,
2814 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
2815 struct ust_app_channel
*ua_chan
)
2824 /* Handle buffer type before sending the channel to the application. */
2825 switch (usess
->buffer_type
) {
2826 case LTTNG_BUFFER_PER_UID
:
2828 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
2834 case LTTNG_BUFFER_PER_PID
:
2836 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
2848 /* Initialize ust objd object using the received handle and add it. */
2849 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
2850 lttng_ht_add_unique_ulong(app
->ust_objd
, &ua_chan
->ust_objd_node
);
2852 /* If channel is not enabled, disable it on the tracer */
2853 if (!ua_chan
->enabled
) {
2854 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
2865 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2866 * newly created channel if not NULL.
2868 * Called with UST app session lock and RCU read-side lock held.
2870 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2871 * the application exited concurrently.
2873 static int create_ust_app_channel(struct ust_app_session
*ua_sess
,
2874 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
2875 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
2876 struct ust_app_channel
**ua_chanp
)
2879 struct lttng_ht_iter iter
;
2880 struct lttng_ht_node_str
*ua_chan_node
;
2881 struct ust_app_channel
*ua_chan
;
2883 /* Lookup channel in the ust app session */
2884 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
2885 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
2886 if (ua_chan_node
!= NULL
) {
2887 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
2891 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
2892 if (ua_chan
== NULL
) {
2893 /* Only malloc can fail here */
2897 shadow_copy_channel(ua_chan
, uchan
);
2899 /* Set channel type. */
2900 ua_chan
->attr
.type
= type
;
2902 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
2907 DBG2("UST app create channel %s for PID %d completed", ua_chan
->name
,
2910 /* Only add the channel if successful on the tracer side. */
2911 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
2915 *ua_chanp
= ua_chan
;
2918 /* Everything went well. */
2922 delete_ust_app_channel(ua_chan
->is_sent
? app
->sock
: -1, ua_chan
, app
);
2928 * Create UST app event and create it on the tracer side.
2930 * Called with ust app session mutex held.
2933 int create_ust_app_event(struct ust_app_session
*ua_sess
,
2934 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
2935 struct ust_app
*app
)
2938 struct ust_app_event
*ua_event
;
2940 /* Get event node */
2941 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
2942 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
2943 if (ua_event
!= NULL
) {
2948 /* Does not exist so create one */
2949 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
2950 if (ua_event
== NULL
) {
2951 /* Only malloc can failed so something is really wrong */
2955 shadow_copy_event(ua_event
, uevent
);
2957 /* Create it on the tracer side */
2958 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
2960 /* Not found previously means that it does not exist on the tracer */
2961 assert(ret
!= -LTTNG_UST_ERR_EXIST
);
2965 add_unique_ust_app_event(ua_chan
, ua_event
);
2967 DBG2("UST app create event %s for PID %d completed", ua_event
->name
,
2974 /* Valid. Calling here is already in a read side lock */
2975 delete_ust_app_event(-1, ua_event
);
2980 * Create UST metadata and open it on the tracer side.
2982 * Called with UST app session lock held and RCU read side lock.
2984 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
2985 struct ust_app
*app
, struct consumer_output
*consumer
)
2988 struct ust_app_channel
*metadata
;
2989 struct consumer_socket
*socket
;
2990 struct ust_registry_session
*registry
;
2996 registry
= get_session_registry(ua_sess
);
2999 pthread_mutex_lock(®istry
->lock
);
3001 /* Metadata already exists for this registry or it was closed previously */
3002 if (registry
->metadata_key
|| registry
->metadata_closed
) {
3007 /* Allocate UST metadata */
3008 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
3010 /* malloc() failed */
3015 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
3017 /* Need one fd for the channel. */
3018 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3020 ERR("Exhausted number of available FD upon create metadata");
3024 /* Get the right consumer socket for the application. */
3025 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
3028 goto error_consumer
;
3032 * Keep metadata key so we can identify it on the consumer side. Assign it
3033 * to the registry *before* we ask the consumer so we avoid the race of the
3034 * consumer requesting the metadata and the ask_channel call on our side
3035 * did not returned yet.
3037 registry
->metadata_key
= metadata
->key
;
3040 * Ask the metadata channel creation to the consumer. The metadata object
3041 * will be created by the consumer and kept their. However, the stream is
3042 * never added or monitored until we do a first push metadata to the
3045 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
3048 /* Nullify the metadata key so we don't try to close it later on. */
3049 registry
->metadata_key
= 0;
3050 goto error_consumer
;
3054 * The setup command will make the metadata stream be sent to the relayd,
3055 * if applicable, and the thread managing the metadatas. This is important
3056 * because after this point, if an error occurs, the only way the stream
3057 * can be deleted is to be monitored in the consumer.
3059 ret
= consumer_setup_metadata(socket
, metadata
->key
);
3061 /* Nullify the metadata key so we don't try to close it later on. */
3062 registry
->metadata_key
= 0;
3063 goto error_consumer
;
3066 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
3067 metadata
->key
, app
->pid
);
3070 lttng_fd_put(LTTNG_FD_APPS
, 1);
3071 delete_ust_app_channel(-1, metadata
, app
);
3073 pthread_mutex_unlock(®istry
->lock
);
3078 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3079 * acquired before calling this function.
3081 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
3083 struct ust_app
*app
= NULL
;
3084 struct lttng_ht_node_ulong
*node
;
3085 struct lttng_ht_iter iter
;
3087 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
3088 node
= lttng_ht_iter_get_node_ulong(&iter
);
3090 DBG2("UST app no found with pid %d", pid
);
3094 DBG2("Found UST app by pid %d", pid
);
3096 app
= caa_container_of(node
, struct ust_app
, pid_n
);
3103 * Allocate and init an UST app object using the registration information and
3104 * the command socket. This is called when the command socket connects to the
3107 * The object is returned on success or else NULL.
3109 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
3111 struct ust_app
*lta
= NULL
;
3116 DBG3("UST app creating application for socket %d", sock
);
3118 if ((msg
->bits_per_long
== 64 &&
3119 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
3120 || (msg
->bits_per_long
== 32 &&
3121 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
3122 ERR("Registration failed: application \"%s\" (pid: %d) has "
3123 "%d-bit long, but no consumerd for this size is available.\n",
3124 msg
->name
, msg
->pid
, msg
->bits_per_long
);
3128 lta
= zmalloc(sizeof(struct ust_app
));
3134 lta
->ppid
= msg
->ppid
;
3135 lta
->uid
= msg
->uid
;
3136 lta
->gid
= msg
->gid
;
3138 lta
->bits_per_long
= msg
->bits_per_long
;
3139 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
3140 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
3141 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
3142 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
3143 lta
->long_alignment
= msg
->long_alignment
;
3144 lta
->byte_order
= msg
->byte_order
;
3146 lta
->v_major
= msg
->major
;
3147 lta
->v_minor
= msg
->minor
;
3148 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3149 lta
->ust_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3150 lta
->notify_sock
= -1;
3152 /* Copy name and make sure it's NULL terminated. */
3153 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
3154 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
3157 * Before this can be called, when receiving the registration information,
3158 * the application compatibility is checked. So, at this point, the
3159 * application can work with this session daemon.
3161 lta
->compatible
= 1;
3163 lta
->pid
= msg
->pid
;
3164 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
3166 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
3168 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
3175 * For a given application object, add it to every hash table.
3177 void ust_app_add(struct ust_app
*app
)
3180 assert(app
->notify_sock
>= 0);
3185 * On a re-registration, we want to kick out the previous registration of
3188 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
3191 * The socket _should_ be unique until _we_ call close. So, a add_unique
3192 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3193 * already in the table.
3195 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
3197 /* Add application to the notify socket hash table. */
3198 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
3199 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
3201 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3202 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
3203 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
3210 * Set the application version into the object.
3212 * Return 0 on success else a negative value either an errno code or a
3213 * LTTng-UST error code.
3215 int ust_app_version(struct ust_app
*app
)
3221 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
3223 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3224 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
3226 DBG3("UST app %d version failed. Application is dead", app
->sock
);
3234 * Unregister app by removing it from the global traceable app list and freeing
3237 * The socket is already closed at this point so no close to sock.
3239 void ust_app_unregister(int sock
)
3241 struct ust_app
*lta
;
3242 struct lttng_ht_node_ulong
*node
;
3243 struct lttng_ht_iter ust_app_sock_iter
;
3244 struct lttng_ht_iter iter
;
3245 struct ust_app_session
*ua_sess
;
3250 /* Get the node reference for a call_rcu */
3251 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
3252 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
3255 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
3256 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
3259 * For per-PID buffers, perform "push metadata" and flush all
3260 * application streams before removing app from hash tables,
3261 * ensuring proper behavior of data_pending check.
3262 * Remove sessions so they are not visible during deletion.
3264 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
3266 struct ust_registry_session
*registry
;
3268 ret
= lttng_ht_del(lta
->sessions
, &iter
);
3270 /* The session was already removed so scheduled for teardown. */
3274 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
3275 (void) ust_app_flush_app_session(lta
, ua_sess
);
3279 * Add session to list for teardown. This is safe since at this point we
3280 * are the only one using this list.
3282 pthread_mutex_lock(&ua_sess
->lock
);
3284 if (ua_sess
->deleted
) {
3285 pthread_mutex_unlock(&ua_sess
->lock
);
3290 * Normally, this is done in the delete session process which is
3291 * executed in the call rcu below. However, upon registration we can't
3292 * afford to wait for the grace period before pushing data or else the
3293 * data pending feature can race between the unregistration and stop
3294 * command where the data pending command is sent *before* the grace
3297 * The close metadata below nullifies the metadata pointer in the
3298 * session so the delete session will NOT push/close a second time.
3300 registry
= get_session_registry(ua_sess
);
3302 /* Push metadata for application before freeing the application. */
3303 (void) push_metadata(registry
, ua_sess
->consumer
);
3306 * Don't ask to close metadata for global per UID buffers. Close
3307 * metadata only on destroy trace session in this case. Also, the
3308 * previous push metadata could have flag the metadata registry to
3309 * close so don't send a close command if closed.
3311 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
3312 /* And ask to close it for this session registry. */
3313 (void) close_metadata(registry
, ua_sess
->consumer
);
3316 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
3318 pthread_mutex_unlock(&ua_sess
->lock
);
3321 /* Remove application from PID hash table */
3322 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
3326 * Remove application from notify hash table. The thread handling the
3327 * notify socket could have deleted the node so ignore on error because
3328 * either way it's valid. The close of that socket is handled by the other
3331 iter
.iter
.node
= <a
->notify_sock_n
.node
;
3332 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3335 * Ignore return value since the node might have been removed before by an
3336 * add replace during app registration because the PID can be reassigned by
3339 iter
.iter
.node
= <a
->pid_n
.node
;
3340 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3342 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3347 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
3354 * Fill events array with all events name of all registered apps.
3356 int ust_app_list_events(struct lttng_event
**events
)
3359 size_t nbmem
, count
= 0;
3360 struct lttng_ht_iter iter
;
3361 struct ust_app
*app
;
3362 struct lttng_event
*tmp_event
;
3364 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3365 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
3366 if (tmp_event
== NULL
) {
3367 PERROR("zmalloc ust app events");
3374 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3375 struct lttng_ust_tracepoint_iter uiter
;
3377 health_code_update();
3379 if (!app
->compatible
) {
3381 * TODO: In time, we should notice the caller of this error by
3382 * telling him that this is a version error.
3386 handle
= ustctl_tracepoint_list(app
->sock
);
3388 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3389 ERR("UST app list events getting handle failed for app pid %d",
3395 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
3396 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3397 /* Handle ustctl error. */
3399 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3400 ERR("UST app tp list get failed for app %d with ret %d",
3403 DBG3("UST app tp list get failed. Application is dead");
3405 * This is normal behavior, an application can die during the
3406 * creation process. Don't report an error so the execution can
3407 * continue normally. Continue normal execution.
3415 health_code_update();
3416 if (count
>= nbmem
) {
3417 /* In case the realloc fails, we free the memory */
3418 struct lttng_event
*new_tmp_event
;
3421 new_nbmem
= nbmem
<< 1;
3422 DBG2("Reallocating event list from %zu to %zu entries",
3424 new_tmp_event
= realloc(tmp_event
,
3425 new_nbmem
* sizeof(struct lttng_event
));
3426 if (new_tmp_event
== NULL
) {
3427 PERROR("realloc ust app events");
3432 /* Zero the new memory */
3433 memset(new_tmp_event
+ nbmem
, 0,
3434 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
3436 tmp_event
= new_tmp_event
;
3438 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
3439 tmp_event
[count
].loglevel
= uiter
.loglevel
;
3440 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
3441 tmp_event
[count
].pid
= app
->pid
;
3442 tmp_event
[count
].enabled
= -1;
3448 *events
= tmp_event
;
3450 DBG2("UST app list events done (%zu events)", count
);
3455 health_code_update();
3460 * Fill events array with all events name of all registered apps.
3462 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
3465 size_t nbmem
, count
= 0;
3466 struct lttng_ht_iter iter
;
3467 struct ust_app
*app
;
3468 struct lttng_event_field
*tmp_event
;
3470 nbmem
= UST_APP_EVENT_LIST_SIZE
;
3471 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
3472 if (tmp_event
== NULL
) {
3473 PERROR("zmalloc ust app event fields");
3480 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3481 struct lttng_ust_field_iter uiter
;
3483 health_code_update();
3485 if (!app
->compatible
) {
3487 * TODO: In time, we should notice the caller of this error by
3488 * telling him that this is a version error.
3492 handle
= ustctl_tracepoint_field_list(app
->sock
);
3494 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
3495 ERR("UST app list field getting handle failed for app pid %d",
3501 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
3502 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
3503 /* Handle ustctl error. */
3505 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
3506 ERR("UST app tp list field failed for app %d with ret %d",
3509 DBG3("UST app tp list field failed. Application is dead");
3511 * This is normal behavior, an application can die during the
3512 * creation process. Don't report an error so the execution can
3513 * continue normally. Reset list and count for next app.
3521 health_code_update();
3522 if (count
>= nbmem
) {
3523 /* In case the realloc fails, we free the memory */
3524 struct lttng_event_field
*new_tmp_event
;
3527 new_nbmem
= nbmem
<< 1;
3528 DBG2("Reallocating event field list from %zu to %zu entries",
3530 new_tmp_event
= realloc(tmp_event
,
3531 new_nbmem
* sizeof(struct lttng_event_field
));
3532 if (new_tmp_event
== NULL
) {
3533 PERROR("realloc ust app event fields");
3538 /* Zero the new memory */
3539 memset(new_tmp_event
+ nbmem
, 0,
3540 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
3542 tmp_event
= new_tmp_event
;
3545 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
3546 /* Mapping between these enums matches 1 to 1. */
3547 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
3548 tmp_event
[count
].nowrite
= uiter
.nowrite
;
3550 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
3551 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
3552 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
3553 tmp_event
[count
].event
.pid
= app
->pid
;
3554 tmp_event
[count
].event
.enabled
= -1;
3560 *fields
= tmp_event
;
3562 DBG2("UST app list event fields done (%zu events)", count
);
3567 health_code_update();
3572 * Free and clean all traceable apps of the global list.
3574 * Should _NOT_ be called with RCU read-side lock held.
3576 void ust_app_clean_list(void)
3579 struct ust_app
*app
;
3580 struct lttng_ht_iter iter
;
3582 DBG2("UST app cleaning registered apps hash table");
3587 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3588 ret
= lttng_ht_del(ust_app_ht
, &iter
);
3590 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
3594 /* Cleanup socket hash table */
3595 if (ust_app_ht_by_sock
) {
3596 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
3598 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
3603 /* Cleanup notify socket hash table */
3604 if (ust_app_ht_by_notify_sock
) {
3605 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
3606 notify_sock_n
.node
) {
3607 ret
= lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
3613 /* Destroy is done only when the ht is empty */
3615 ht_cleanup_push(ust_app_ht
);
3617 if (ust_app_ht_by_sock
) {
3618 ht_cleanup_push(ust_app_ht_by_sock
);
3620 if (ust_app_ht_by_notify_sock
) {
3621 ht_cleanup_push(ust_app_ht_by_notify_sock
);
3626 * Init UST app hash table.
3628 int ust_app_ht_alloc(void)
3630 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3634 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3635 if (!ust_app_ht_by_sock
) {
3638 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
3639 if (!ust_app_ht_by_notify_sock
) {
3646 * For a specific UST session, disable the channel for all registered apps.
3648 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
3649 struct ltt_ust_channel
*uchan
)
3652 struct lttng_ht_iter iter
;
3653 struct lttng_ht_node_str
*ua_chan_node
;
3654 struct ust_app
*app
;
3655 struct ust_app_session
*ua_sess
;
3656 struct ust_app_channel
*ua_chan
;
3658 if (usess
== NULL
|| uchan
== NULL
) {
3659 ERR("Disabling UST global channel with NULL values");
3664 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
3665 uchan
->name
, usess
->id
);
3669 /* For every registered applications */
3670 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3671 struct lttng_ht_iter uiter
;
3672 if (!app
->compatible
) {
3674 * TODO: In time, we should notice the caller of this error by
3675 * telling him that this is a version error.
3679 ua_sess
= lookup_session_by_app(usess
, app
);
3680 if (ua_sess
== NULL
) {
3685 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3686 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3687 /* If the session if found for the app, the channel must be there */
3688 assert(ua_chan_node
);
3690 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3691 /* The channel must not be already disabled */
3692 assert(ua_chan
->enabled
== 1);
3694 /* Disable channel onto application */
3695 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
3697 /* XXX: We might want to report this error at some point... */
3709 * For a specific UST session, enable the channel for all registered apps.
3711 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
3712 struct ltt_ust_channel
*uchan
)
3715 struct lttng_ht_iter iter
;
3716 struct ust_app
*app
;
3717 struct ust_app_session
*ua_sess
;
3719 if (usess
== NULL
|| uchan
== NULL
) {
3720 ERR("Adding UST global channel to NULL values");
3725 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
3726 uchan
->name
, usess
->id
);
3730 /* For every registered applications */
3731 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3732 if (!app
->compatible
) {
3734 * TODO: In time, we should notice the caller of this error by
3735 * telling him that this is a version error.
3739 ua_sess
= lookup_session_by_app(usess
, app
);
3740 if (ua_sess
== NULL
) {
3744 /* Enable channel onto application */
3745 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
3747 /* XXX: We might want to report this error at some point... */
3759 * Disable an event in a channel and for a specific session.
3761 int ust_app_disable_event_glb(struct ltt_ust_session
*usess
,
3762 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3765 struct lttng_ht_iter iter
, uiter
;
3766 struct lttng_ht_node_str
*ua_chan_node
, *ua_event_node
;
3767 struct ust_app
*app
;
3768 struct ust_app_session
*ua_sess
;
3769 struct ust_app_channel
*ua_chan
;
3770 struct ust_app_event
*ua_event
;
3772 DBG("UST app disabling event %s for all apps in channel "
3773 "%s for session id %" PRIu64
,
3774 uevent
->attr
.name
, uchan
->name
, usess
->id
);
3778 /* For all registered applications */
3779 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3780 if (!app
->compatible
) {
3782 * TODO: In time, we should notice the caller of this error by
3783 * telling him that this is a version error.
3787 ua_sess
= lookup_session_by_app(usess
, app
);
3788 if (ua_sess
== NULL
) {
3793 /* Lookup channel in the ust app session */
3794 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3795 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3796 if (ua_chan_node
== NULL
) {
3797 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
3798 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
3801 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3803 lttng_ht_lookup(ua_chan
->events
, (void *)uevent
->attr
.name
, &uiter
);
3804 ua_event_node
= lttng_ht_iter_get_node_str(&uiter
);
3805 if (ua_event_node
== NULL
) {
3806 DBG2("Event %s not found in channel %s for app pid %d."
3807 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
3810 ua_event
= caa_container_of(ua_event_node
, struct ust_app_event
, node
);
3812 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
3814 /* XXX: Report error someday... */
3825 * For a specific UST session, create the channel for all registered apps.
3827 int ust_app_create_channel_glb(struct ltt_ust_session
*usess
,
3828 struct ltt_ust_channel
*uchan
)
3830 int ret
= 0, created
;
3831 struct lttng_ht_iter iter
;
3832 struct ust_app
*app
;
3833 struct ust_app_session
*ua_sess
= NULL
;
3835 /* Very wrong code flow */
3839 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64
,
3840 uchan
->name
, usess
->id
);
3844 /* For every registered applications */
3845 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3846 if (!app
->compatible
) {
3848 * TODO: In time, we should notice the caller of this error by
3849 * telling him that this is a version error.
3853 if (!trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
3859 * Create session on the tracer side and add it to app session HT. Note
3860 * that if session exist, it will simply return a pointer to the ust
3863 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &created
);
3868 * The application's socket is not valid. Either a bad socket
3869 * or a timeout on it. We can't inform the caller that for a
3870 * specific app, the session failed so lets continue here.
3872 ret
= 0; /* Not an error. */
3876 goto error_rcu_unlock
;
3881 pthread_mutex_lock(&ua_sess
->lock
);
3883 if (ua_sess
->deleted
) {
3884 pthread_mutex_unlock(&ua_sess
->lock
);
3888 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
3889 sizeof(uchan
->name
))) {
3890 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
, &uchan
->attr
);
3893 /* Create channel onto application. We don't need the chan ref. */
3894 ret
= create_ust_app_channel(ua_sess
, uchan
, app
,
3895 LTTNG_UST_CHAN_PER_CPU
, usess
, NULL
);
3897 pthread_mutex_unlock(&ua_sess
->lock
);
3899 /* Cleanup the created session if it's the case. */
3901 destroy_app_session(app
, ua_sess
);
3906 * The application's socket is not valid. Either a bad socket
3907 * or a timeout on it. We can't inform the caller that for a
3908 * specific app, the session failed so lets continue here.
3910 ret
= 0; /* Not an error. */
3914 goto error_rcu_unlock
;
3925 * Enable event for a specific session and channel on the tracer.
3927 int ust_app_enable_event_glb(struct ltt_ust_session
*usess
,
3928 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
3931 struct lttng_ht_iter iter
, uiter
;
3932 struct lttng_ht_node_str
*ua_chan_node
;
3933 struct ust_app
*app
;
3934 struct ust_app_session
*ua_sess
;
3935 struct ust_app_channel
*ua_chan
;
3936 struct ust_app_event
*ua_event
;
3938 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
3939 uevent
->attr
.name
, usess
->id
);
3942 * NOTE: At this point, this function is called only if the session and
3943 * channel passed are already created for all apps. and enabled on the
3949 /* For all registered applications */
3950 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
3951 if (!app
->compatible
) {
3953 * TODO: In time, we should notice the caller of this error by
3954 * telling him that this is a version error.
3958 ua_sess
= lookup_session_by_app(usess
, app
);
3960 /* The application has problem or is probably dead. */
3964 pthread_mutex_lock(&ua_sess
->lock
);
3966 if (ua_sess
->deleted
) {
3967 pthread_mutex_unlock(&ua_sess
->lock
);
3971 /* Lookup channel in the ust app session */
3972 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
3973 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
3975 * It is possible that the channel cannot be found is
3976 * the channel/event creation occurs concurrently with
3977 * an application exit.
3979 if (!ua_chan_node
) {
3980 pthread_mutex_unlock(&ua_sess
->lock
);
3984 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3986 /* Get event node */
3987 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
3988 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
3989 if (ua_event
== NULL
) {
3990 DBG3("UST app enable event %s not found for app PID %d."
3991 "Skipping app", uevent
->attr
.name
, app
->pid
);
3995 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
3997 pthread_mutex_unlock(&ua_sess
->lock
);
4001 pthread_mutex_unlock(&ua_sess
->lock
);
4010 * For a specific existing UST session and UST channel, creates the event for
4011 * all registered apps.
4013 int ust_app_create_event_glb(struct ltt_ust_session
*usess
,
4014 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
4017 struct lttng_ht_iter iter
, uiter
;
4018 struct lttng_ht_node_str
*ua_chan_node
;
4019 struct ust_app
*app
;
4020 struct ust_app_session
*ua_sess
;
4021 struct ust_app_channel
*ua_chan
;
4023 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
4024 uevent
->attr
.name
, usess
->id
);
4028 /* For all registered applications */
4029 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4030 if (!app
->compatible
) {
4032 * TODO: In time, we should notice the caller of this error by
4033 * telling him that this is a version error.
4037 ua_sess
= lookup_session_by_app(usess
, app
);
4039 /* The application has problem or is probably dead. */
4043 pthread_mutex_lock(&ua_sess
->lock
);
4045 if (ua_sess
->deleted
) {
4046 pthread_mutex_unlock(&ua_sess
->lock
);
4050 /* Lookup channel in the ust app session */
4051 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4052 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4053 /* If the channel is not found, there is a code flow error */
4054 assert(ua_chan_node
);
4056 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4058 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4059 pthread_mutex_unlock(&ua_sess
->lock
);
4061 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
4062 /* Possible value at this point: -ENOMEM. If so, we stop! */
4065 DBG2("UST app event %s already exist on app PID %d",
4066 uevent
->attr
.name
, app
->pid
);
4077 * Start tracing for a specific UST session and app.
4080 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4083 struct ust_app_session
*ua_sess
;
4085 DBG("Starting tracing for ust app pid %d", app
->pid
);
4089 if (!app
->compatible
) {
4093 ua_sess
= lookup_session_by_app(usess
, app
);
4094 if (ua_sess
== NULL
) {
4095 /* The session is in teardown process. Ignore and continue. */
4099 pthread_mutex_lock(&ua_sess
->lock
);
4101 if (ua_sess
->deleted
) {
4102 pthread_mutex_unlock(&ua_sess
->lock
);
4106 /* Upon restart, we skip the setup, already done */
4107 if (ua_sess
->started
) {
4111 /* Create directories if consumer is LOCAL and has a path defined. */
4112 if (usess
->consumer
->type
== CONSUMER_DST_LOCAL
&&
4113 strlen(usess
->consumer
->dst
.trace_path
) > 0) {
4114 ret
= run_as_mkdir_recursive(usess
->consumer
->dst
.trace_path
,
4115 S_IRWXU
| S_IRWXG
, ua_sess
->euid
, ua_sess
->egid
);
4117 if (errno
!= EEXIST
) {
4118 ERR("Trace directory creation error");
4125 * Create the metadata for the application. This returns gracefully if a
4126 * metadata was already set for the session.
4128 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
4133 health_code_update();
4136 /* This start the UST tracing */
4137 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
4139 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4140 ERR("Error starting tracing for app pid: %d (ret: %d)",
4143 DBG("UST app start session failed. Application is dead.");
4145 * This is normal behavior, an application can die during the
4146 * creation process. Don't report an error so the execution can
4147 * continue normally.
4149 pthread_mutex_unlock(&ua_sess
->lock
);
4155 /* Indicate that the session has been started once */
4156 ua_sess
->started
= 1;
4158 pthread_mutex_unlock(&ua_sess
->lock
);
4160 health_code_update();
4162 /* Quiescent wait after starting trace */
4163 ret
= ustctl_wait_quiescent(app
->sock
);
4164 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4165 ERR("UST app wait quiescent failed for app pid %d ret %d",
4171 health_code_update();
4175 pthread_mutex_unlock(&ua_sess
->lock
);
4177 health_code_update();
4182 * Stop tracing for a specific UST session and app.
4185 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4188 struct ust_app_session
*ua_sess
;
4189 struct ust_registry_session
*registry
;
4191 DBG("Stopping tracing for ust app pid %d", app
->pid
);
4195 if (!app
->compatible
) {
4196 goto end_no_session
;
4199 ua_sess
= lookup_session_by_app(usess
, app
);
4200 if (ua_sess
== NULL
) {
4201 goto end_no_session
;
4204 pthread_mutex_lock(&ua_sess
->lock
);
4206 if (ua_sess
->deleted
) {
4207 pthread_mutex_unlock(&ua_sess
->lock
);
4208 goto end_no_session
;
4212 * If started = 0, it means that stop trace has been called for a session
4213 * that was never started. It's possible since we can have a fail start
4214 * from either the application manager thread or the command thread. Simply
4215 * indicate that this is a stop error.
4217 if (!ua_sess
->started
) {
4218 goto error_rcu_unlock
;
4221 health_code_update();
4223 /* This inhibits UST tracing */
4224 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
4226 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4227 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4230 DBG("UST app stop session failed. Application is dead.");
4232 * This is normal behavior, an application can die during the
4233 * creation process. Don't report an error so the execution can
4234 * continue normally.
4238 goto error_rcu_unlock
;
4241 health_code_update();
4243 /* Quiescent wait after stopping trace */
4244 ret
= ustctl_wait_quiescent(app
->sock
);
4245 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4246 ERR("UST app wait quiescent failed for app pid %d ret %d",
4250 health_code_update();
4252 registry
= get_session_registry(ua_sess
);
4255 /* Push metadata for application before freeing the application. */
4256 (void) push_metadata(registry
, ua_sess
->consumer
);
4259 pthread_mutex_unlock(&ua_sess
->lock
);
4262 health_code_update();
4266 pthread_mutex_unlock(&ua_sess
->lock
);
4268 health_code_update();
4273 int ust_app_flush_app_session(struct ust_app
*app
,
4274 struct ust_app_session
*ua_sess
)
4276 int ret
, retval
= 0;
4277 struct lttng_ht_iter iter
;
4278 struct ust_app_channel
*ua_chan
;
4279 struct consumer_socket
*socket
;
4281 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
4285 if (!app
->compatible
) {
4286 goto end_not_compatible
;
4289 pthread_mutex_lock(&ua_sess
->lock
);
4291 if (ua_sess
->deleted
) {
4295 health_code_update();
4297 /* Flushing buffers */
4298 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
4301 /* Flush buffers and push metadata. */
4302 switch (ua_sess
->buffer_type
) {
4303 case LTTNG_BUFFER_PER_PID
:
4304 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4306 health_code_update();
4307 assert(ua_chan
->is_sent
);
4308 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
4310 ERR("Error flushing consumer channel");
4316 case LTTNG_BUFFER_PER_UID
:
4322 health_code_update();
4325 pthread_mutex_unlock(&ua_sess
->lock
);
4329 health_code_update();
4334 * Flush buffers for all applications for a specific UST session.
4335 * Called with UST session lock held.
4338 int ust_app_flush_session(struct ltt_ust_session
*usess
)
4343 DBG("Flushing session buffers for all ust apps");
4347 /* Flush buffers and push metadata. */
4348 switch (usess
->buffer_type
) {
4349 case LTTNG_BUFFER_PER_UID
:
4351 struct buffer_reg_uid
*reg
;
4352 struct lttng_ht_iter iter
;
4354 /* Flush all per UID buffers associated to that session. */
4355 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4356 struct ust_registry_session
*ust_session_reg
;
4357 struct buffer_reg_channel
*reg_chan
;
4358 struct consumer_socket
*socket
;
4360 /* Get consumer socket to use to push the metadata.*/
4361 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
4364 /* Ignore request if no consumer is found for the session. */
4368 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
4369 reg_chan
, node
.node
) {
4371 * The following call will print error values so the return
4372 * code is of little importance because whatever happens, we
4373 * have to try them all.
4375 (void) consumer_flush_channel(socket
, reg_chan
->consumer_key
);
4378 ust_session_reg
= reg
->registry
->reg
.ust
;
4379 /* Push metadata. */
4380 (void) push_metadata(ust_session_reg
, usess
->consumer
);
4384 case LTTNG_BUFFER_PER_PID
:
4386 struct ust_app_session
*ua_sess
;
4387 struct lttng_ht_iter iter
;
4388 struct ust_app
*app
;
4390 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4391 ua_sess
= lookup_session_by_app(usess
, app
);
4392 if (ua_sess
== NULL
) {
4395 (void) ust_app_flush_app_session(app
, ua_sess
);
4406 health_code_update();
4411 * Destroy a specific UST session in apps.
4413 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4416 struct ust_app_session
*ua_sess
;
4417 struct lttng_ht_iter iter
;
4418 struct lttng_ht_node_u64
*node
;
4420 DBG("Destroy tracing for ust app pid %d", app
->pid
);
4424 if (!app
->compatible
) {
4428 __lookup_session_by_app(usess
, app
, &iter
);
4429 node
= lttng_ht_iter_get_node_u64(&iter
);
4431 /* Session is being or is deleted. */
4434 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
4436 health_code_update();
4437 destroy_app_session(app
, ua_sess
);
4439 health_code_update();
4441 /* Quiescent wait after stopping trace */
4442 ret
= ustctl_wait_quiescent(app
->sock
);
4443 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
4444 ERR("UST app wait quiescent failed for app pid %d ret %d",
4449 health_code_update();
4454 * Start tracing for the UST session.
4456 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
4459 struct lttng_ht_iter iter
;
4460 struct ust_app
*app
;
4462 DBG("Starting all UST traces");
4466 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4467 ret
= ust_app_start_trace(usess
, app
);
4469 /* Continue to next apps even on error */
4480 * Start tracing for the UST session.
4481 * Called with UST session lock held.
4483 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
4486 struct lttng_ht_iter iter
;
4487 struct ust_app
*app
;
4489 DBG("Stopping all UST traces");
4493 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4494 ret
= ust_app_stop_trace(usess
, app
);
4496 /* Continue to next apps even on error */
4501 (void) ust_app_flush_session(usess
);
4509 * Destroy app UST session.
4511 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
4514 struct lttng_ht_iter iter
;
4515 struct ust_app
*app
;
4517 DBG("Destroy all UST traces");
4521 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4522 ret
= destroy_trace(usess
, app
);
4524 /* Continue to next apps even on error */
4535 void ust_app_global_create(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4538 struct lttng_ht_iter iter
, uiter
;
4539 struct ust_app_session
*ua_sess
= NULL
;
4540 struct ust_app_channel
*ua_chan
;
4541 struct ust_app_event
*ua_event
;
4542 struct ust_app_ctx
*ua_ctx
;
4545 ret
= create_ust_app_session(usess
, app
, &ua_sess
, &is_created
);
4547 /* Tracer is probably gone or ENOMEM. */
4551 /* App session already created. */
4556 pthread_mutex_lock(&ua_sess
->lock
);
4558 if (ua_sess
->deleted
) {
4559 pthread_mutex_unlock(&ua_sess
->lock
);
4564 * We can iterate safely here over all UST app session since the create ust
4565 * app session above made a shadow copy of the UST global domain from the
4568 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
4570 ret
= do_create_channel(app
, usess
, ua_sess
, ua_chan
);
4571 if (ret
< 0 && ret
!= -ENOTCONN
) {
4573 * Stop everything. On error, the application
4574 * failed, no more file descriptor are available
4575 * or ENOMEM so stopping here is the only thing
4576 * we can do for now. The only exception is
4577 * -ENOTCONN, which indicates that the application
4584 * Add context using the list so they are enabled in the same order the
4587 cds_list_for_each_entry(ua_ctx
, &ua_chan
->ctx_list
, list
) {
4588 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
4595 /* For each events */
4596 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &uiter
.iter
, ua_event
,
4598 ret
= create_ust_event(app
, ua_sess
, ua_chan
, ua_event
);
4605 pthread_mutex_unlock(&ua_sess
->lock
);
4607 if (usess
->active
) {
4608 ret
= ust_app_start_trace(usess
, app
);
4613 DBG2("UST trace started for app pid %d", app
->pid
);
4616 /* Everything went well at this point. */
4620 pthread_mutex_unlock(&ua_sess
->lock
);
4623 destroy_app_session(app
, ua_sess
);
4629 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4631 struct ust_app_session
*ua_sess
;
4633 ua_sess
= lookup_session_by_app(usess
, app
);
4634 if (ua_sess
== NULL
) {
4637 destroy_app_session(app
, ua_sess
);
4641 * Add channels/events from UST global domain to registered apps at sock.
4643 * Called with session lock held.
4644 * Called with RCU read-side lock held.
4646 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
4650 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
4651 app
->sock
, usess
->id
);
4653 if (!app
->compatible
) {
4657 if (trace_ust_pid_tracker_lookup(usess
, app
->pid
)) {
4658 ust_app_global_create(usess
, app
);
4660 ust_app_global_destroy(usess
, app
);
4665 * Called with session lock held.
4667 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
4669 struct lttng_ht_iter iter
;
4670 struct ust_app
*app
;
4673 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4674 ust_app_global_update(usess
, app
);
4680 * Add context to a specific channel for global UST domain.
4682 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
4683 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
4686 struct lttng_ht_node_str
*ua_chan_node
;
4687 struct lttng_ht_iter iter
, uiter
;
4688 struct ust_app_channel
*ua_chan
= NULL
;
4689 struct ust_app_session
*ua_sess
;
4690 struct ust_app
*app
;
4694 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4695 if (!app
->compatible
) {
4697 * TODO: In time, we should notice the caller of this error by
4698 * telling him that this is a version error.
4702 ua_sess
= lookup_session_by_app(usess
, app
);
4703 if (ua_sess
== NULL
) {
4707 pthread_mutex_lock(&ua_sess
->lock
);
4709 if (ua_sess
->deleted
) {
4710 pthread_mutex_unlock(&ua_sess
->lock
);
4714 /* Lookup channel in the ust app session */
4715 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
4716 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
4717 if (ua_chan_node
== NULL
) {
4720 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
4722 ret
= create_ust_app_channel_context(ua_sess
, ua_chan
, &uctx
->ctx
, app
);
4727 pthread_mutex_unlock(&ua_sess
->lock
);
4735 * Enable event for a channel from a UST session for a specific PID.
4737 int ust_app_enable_event_pid(struct ltt_ust_session
*usess
,
4738 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
, pid_t pid
)
4741 struct lttng_ht_iter iter
;
4742 struct lttng_ht_node_str
*ua_chan_node
;
4743 struct ust_app
*app
;
4744 struct ust_app_session
*ua_sess
;
4745 struct ust_app_channel
*ua_chan
;
4746 struct ust_app_event
*ua_event
;
4748 DBG("UST app enabling event %s for PID %d", uevent
->attr
.name
, pid
);
4752 app
= ust_app_find_by_pid(pid
);
4754 ERR("UST app enable event per PID %d not found", pid
);
4759 if (!app
->compatible
) {
4764 ua_sess
= lookup_session_by_app(usess
, app
);
4766 /* The application has problem or is probably dead. */
4771 pthread_mutex_lock(&ua_sess
->lock
);
4773 if (ua_sess
->deleted
) {
4778 /* Lookup channel in the ust app session */
4779 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4780 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4781 /* If the channel is not found, there is a code flow error */
4782 assert(ua_chan_node
);
4784 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4786 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
4787 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
);
4788 if (ua_event
== NULL
) {
4789 ret
= create_ust_app_event(ua_sess
, ua_chan
, uevent
, app
);
4794 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
4801 pthread_mutex_unlock(&ua_sess
->lock
);
4808 * Calibrate registered applications.
4810 int ust_app_calibrate_glb(struct lttng_ust_calibrate
*calibrate
)
4813 struct lttng_ht_iter iter
;
4814 struct ust_app
*app
;
4818 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
4819 if (!app
->compatible
) {
4821 * TODO: In time, we should notice the caller of this error by
4822 * telling him that this is a version error.
4827 health_code_update();
4829 ret
= ustctl_calibrate(app
->sock
, calibrate
);
4833 /* Means that it's not implemented on the tracer side. */
4837 DBG2("Calibrate app PID %d returned with error %d",
4844 DBG("UST app global domain calibration finished");
4848 health_code_update();
4854 * Receive registration and populate the given msg structure.
4856 * On success return 0 else a negative value returned by the ustctl call.
4858 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
4861 uint32_t pid
, ppid
, uid
, gid
;
4865 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
4866 &pid
, &ppid
, &uid
, &gid
,
4867 &msg
->bits_per_long
,
4868 &msg
->uint8_t_alignment
,
4869 &msg
->uint16_t_alignment
,
4870 &msg
->uint32_t_alignment
,
4871 &msg
->uint64_t_alignment
,
4872 &msg
->long_alignment
,
4879 case LTTNG_UST_ERR_EXITING
:
4880 DBG3("UST app recv reg message failed. Application died");
4882 case LTTNG_UST_ERR_UNSUP_MAJOR
:
4883 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
4884 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
4885 LTTNG_UST_ABI_MINOR_VERSION
);
4888 ERR("UST app recv reg message failed with ret %d", ret
);
4893 msg
->pid
= (pid_t
) pid
;
4894 msg
->ppid
= (pid_t
) ppid
;
4895 msg
->uid
= (uid_t
) uid
;
4896 msg
->gid
= (gid_t
) gid
;
4903 * Return a ust app channel object using the application object and the channel
4904 * object descriptor has a key. If not found, NULL is returned. A RCU read side
4905 * lock MUST be acquired before calling this function.
4907 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
4910 struct lttng_ht_node_ulong
*node
;
4911 struct lttng_ht_iter iter
;
4912 struct ust_app_channel
*ua_chan
= NULL
;
4916 lttng_ht_lookup(app
->ust_objd
, (void *)((unsigned long) objd
), &iter
);
4917 node
= lttng_ht_iter_get_node_ulong(&iter
);
4919 DBG2("UST app channel find by objd %d not found", objd
);
4923 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
4930 * Reply to a register channel notification from an application on the notify
4931 * socket. The channel metadata is also created.
4933 * The session UST registry lock is acquired in this function.
4935 * On success 0 is returned else a negative value.
4937 static int reply_ust_register_channel(int sock
, int sobjd
, int cobjd
,
4938 size_t nr_fields
, struct ustctl_field
*fields
)
4940 int ret
, ret_code
= 0;
4941 uint32_t chan_id
, reg_count
;
4942 uint64_t chan_reg_key
;
4943 enum ustctl_channel_header type
;
4944 struct ust_app
*app
;
4945 struct ust_app_channel
*ua_chan
;
4946 struct ust_app_session
*ua_sess
;
4947 struct ust_registry_session
*registry
;
4948 struct ust_registry_channel
*chan_reg
;
4952 /* Lookup application. If not found, there is a code flow error. */
4953 app
= find_app_by_notify_sock(sock
);
4955 DBG("Application socket %d is being teardown. Abort event notify",
4959 goto error_rcu_unlock
;
4962 /* Lookup channel by UST object descriptor. */
4963 ua_chan
= find_channel_by_objd(app
, cobjd
);
4965 DBG("Application channel is being teardown. Abort event notify");
4968 goto error_rcu_unlock
;
4971 assert(ua_chan
->session
);
4972 ua_sess
= ua_chan
->session
;
4974 /* Get right session registry depending on the session buffer type. */
4975 registry
= get_session_registry(ua_sess
);
4978 /* Depending on the buffer type, a different channel key is used. */
4979 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
4980 chan_reg_key
= ua_chan
->tracing_channel_id
;
4982 chan_reg_key
= ua_chan
->key
;
4985 pthread_mutex_lock(®istry
->lock
);
4987 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
4990 if (!chan_reg
->register_done
) {
4991 reg_count
= ust_registry_get_event_count(chan_reg
);
4992 if (reg_count
< 31) {
4993 type
= USTCTL_CHANNEL_HEADER_COMPACT
;
4995 type
= USTCTL_CHANNEL_HEADER_LARGE
;
4998 chan_reg
->nr_ctx_fields
= nr_fields
;
4999 chan_reg
->ctx_fields
= fields
;
5000 chan_reg
->header_type
= type
;
5002 /* Get current already assigned values. */
5003 type
= chan_reg
->header_type
;
5005 /* Set to NULL so the error path does not do a double free. */
5008 /* Channel id is set during the object creation. */
5009 chan_id
= chan_reg
->chan_id
;
5011 /* Append to metadata */
5012 if (!chan_reg
->metadata_dumped
) {
5013 ret_code
= ust_metadata_channel_statedump(registry
, chan_reg
);
5015 ERR("Error appending channel metadata (errno = %d)", ret_code
);
5021 DBG3("UST app replying to register channel key %" PRIu64
5022 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
5025 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
5027 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5028 ERR("UST app reply channel failed with ret %d", ret
);
5030 DBG3("UST app reply channel failed. Application died");
5035 /* This channel registry registration is completed. */
5036 chan_reg
->register_done
= 1;
5039 pthread_mutex_unlock(®istry
->lock
);
5049 * Add event to the UST channel registry. When the event is added to the
5050 * registry, the metadata is also created. Once done, this replies to the
5051 * application with the appropriate error code.
5053 * The session UST registry lock is acquired in the function.
5055 * On success 0 is returned else a negative value.
5057 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
5058 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
, int loglevel
,
5059 char *model_emf_uri
)
5062 uint32_t event_id
= 0;
5063 uint64_t chan_reg_key
;
5064 struct ust_app
*app
;
5065 struct ust_app_channel
*ua_chan
;
5066 struct ust_app_session
*ua_sess
;
5067 struct ust_registry_session
*registry
;
5071 /* Lookup application. If not found, there is a code flow error. */
5072 app
= find_app_by_notify_sock(sock
);
5074 DBG("Application socket %d is being teardown. Abort event notify",
5079 free(model_emf_uri
);
5080 goto error_rcu_unlock
;
5083 /* Lookup channel by UST object descriptor. */
5084 ua_chan
= find_channel_by_objd(app
, cobjd
);
5086 DBG("Application channel is being teardown. Abort event notify");
5090 free(model_emf_uri
);
5091 goto error_rcu_unlock
;
5094 assert(ua_chan
->session
);
5095 ua_sess
= ua_chan
->session
;
5097 registry
= get_session_registry(ua_sess
);
5100 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5101 chan_reg_key
= ua_chan
->tracing_channel_id
;
5103 chan_reg_key
= ua_chan
->key
;
5106 pthread_mutex_lock(®istry
->lock
);
5109 * From this point on, this call acquires the ownership of the sig, fields
5110 * and model_emf_uri meaning any free are done inside it if needed. These
5111 * three variables MUST NOT be read/write after this.
5113 ret_code
= ust_registry_create_event(registry
, chan_reg_key
,
5114 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
, loglevel
,
5115 model_emf_uri
, ua_sess
->buffer_type
, &event_id
,
5119 * The return value is returned to ustctl so in case of an error, the
5120 * application can be notified. In case of an error, it's important not to
5121 * return a negative error or else the application will get closed.
5123 ret
= ustctl_reply_register_event(sock
, event_id
, ret_code
);
5125 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5126 ERR("UST app reply event failed with ret %d", ret
);
5128 DBG3("UST app reply event failed. Application died");
5131 * No need to wipe the create event since the application socket will
5132 * get close on error hence cleaning up everything by itself.
5137 DBG3("UST registry event %s with id %" PRId32
" added successfully",
5141 pthread_mutex_unlock(®istry
->lock
);
5148 * Handle application notification through the given notify socket.
5150 * Return 0 on success or else a negative value.
5152 int ust_app_recv_notify(int sock
)
5155 enum ustctl_notify_cmd cmd
;
5157 DBG3("UST app receiving notify from sock %d", sock
);
5159 ret
= ustctl_recv_notify(sock
, &cmd
);
5161 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5162 ERR("UST app recv notify failed with ret %d", ret
);
5164 DBG3("UST app recv notify failed. Application died");
5170 case USTCTL_NOTIFY_CMD_EVENT
:
5172 int sobjd
, cobjd
, loglevel
;
5173 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
5175 struct ustctl_field
*fields
;
5177 DBG2("UST app ustctl register event received");
5179 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
, &loglevel
,
5180 &sig
, &nr_fields
, &fields
, &model_emf_uri
);
5182 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5183 ERR("UST app recv event failed with ret %d", ret
);
5185 DBG3("UST app recv event failed. Application died");
5191 * Add event to the UST registry coming from the notify socket. This
5192 * call will free if needed the sig, fields and model_emf_uri. This
5193 * code path loses the ownsership of these variables and transfer them
5194 * to the this function.
5196 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
5197 fields
, loglevel
, model_emf_uri
);
5204 case USTCTL_NOTIFY_CMD_CHANNEL
:
5208 struct ustctl_field
*fields
;
5210 DBG2("UST app ustctl register channel received");
5212 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
5215 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
5216 ERR("UST app recv channel failed with ret %d", ret
);
5218 DBG3("UST app recv channel failed. Application died");
5224 * The fields ownership are transfered to this function call meaning
5225 * that if needed it will be freed. After this, it's invalid to access
5226 * fields or clean it up.
5228 ret
= reply_ust_register_channel(sock
, sobjd
, cobjd
, nr_fields
,
5237 /* Should NEVER happen. */
5246 * Once the notify socket hangs up, this is called. First, it tries to find the
5247 * corresponding application. On failure, the call_rcu to close the socket is
5248 * executed. If an application is found, it tries to delete it from the notify
5249 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5251 * Note that an object needs to be allocated here so on ENOMEM failure, the
5252 * call RCU is not done but the rest of the cleanup is.
5254 void ust_app_notify_sock_unregister(int sock
)
5257 struct lttng_ht_iter iter
;
5258 struct ust_app
*app
;
5259 struct ust_app_notify_sock_obj
*obj
;
5265 obj
= zmalloc(sizeof(*obj
));
5268 * An ENOMEM is kind of uncool. If this strikes we continue the
5269 * procedure but the call_rcu will not be called. In this case, we
5270 * accept the fd leak rather than possibly creating an unsynchronized
5271 * state between threads.
5273 * TODO: The notify object should be created once the notify socket is
5274 * registered and stored independantely from the ust app object. The
5275 * tricky part is to synchronize the teardown of the application and
5276 * this notify object. Let's keep that in mind so we can avoid this
5277 * kind of shenanigans with ENOMEM in the teardown path.
5284 DBG("UST app notify socket unregister %d", sock
);
5287 * Lookup application by notify socket. If this fails, this means that the
5288 * hash table delete has already been done by the application
5289 * unregistration process so we can safely close the notify socket in a
5292 app
= find_app_by_notify_sock(sock
);
5297 iter
.iter
.node
= &app
->notify_sock_n
.node
;
5300 * Whatever happens here either we fail or succeed, in both cases we have
5301 * to close the socket after a grace period to continue to the call RCU
5302 * here. If the deletion is successful, the application is not visible
5303 * anymore by other threads and is it fails it means that it was already
5304 * deleted from the hash table so either way we just have to close the
5307 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5313 * Close socket after a grace period to avoid for the socket to be reused
5314 * before the application object is freed creating potential race between
5315 * threads trying to add unique in the global hash table.
5318 call_rcu(&obj
->head
, close_notify_sock_rcu
);
5323 * Destroy a ust app data structure and free its memory.
5325 void ust_app_destroy(struct ust_app
*app
)
5331 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5335 * Take a snapshot for a given UST session. The snapshot is sent to the given
5338 * Return 0 on success or else a negative value.
5340 int ust_app_snapshot_record(struct ltt_ust_session
*usess
,
5341 struct snapshot_output
*output
, int wait
,
5342 uint64_t nb_packets_per_stream
)
5345 unsigned int snapshot_done
= 0;
5346 struct lttng_ht_iter iter
;
5347 struct ust_app
*app
;
5348 char pathname
[PATH_MAX
];
5355 switch (usess
->buffer_type
) {
5356 case LTTNG_BUFFER_PER_UID
:
5358 struct buffer_reg_uid
*reg
;
5360 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5361 struct buffer_reg_channel
*reg_chan
;
5362 struct consumer_socket
*socket
;
5364 /* Get consumer socket to use to push the metadata.*/
5365 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
5372 memset(pathname
, 0, sizeof(pathname
));
5373 ret
= snprintf(pathname
, sizeof(pathname
),
5374 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
5375 reg
->uid
, reg
->bits_per_long
);
5377 PERROR("snprintf snapshot path");
5381 /* Add the UST default trace dir to path. */
5382 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5383 reg_chan
, node
.node
) {
5384 ret
= consumer_snapshot_channel(socket
, reg_chan
->consumer_key
,
5385 output
, 0, usess
->uid
, usess
->gid
, pathname
, wait
,
5386 nb_packets_per_stream
);
5391 ret
= consumer_snapshot_channel(socket
,
5392 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
5393 usess
->uid
, usess
->gid
, pathname
, wait
, 0);
5401 case LTTNG_BUFFER_PER_PID
:
5403 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5404 struct consumer_socket
*socket
;
5405 struct lttng_ht_iter chan_iter
;
5406 struct ust_app_channel
*ua_chan
;
5407 struct ust_app_session
*ua_sess
;
5408 struct ust_registry_session
*registry
;
5410 ua_sess
= lookup_session_by_app(usess
, app
);
5412 /* Session not associated with this app. */
5416 /* Get the right consumer socket for the application. */
5417 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
5424 /* Add the UST default trace dir to path. */
5425 memset(pathname
, 0, sizeof(pathname
));
5426 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
5429 PERROR("snprintf snapshot path");
5433 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5434 ua_chan
, node
.node
) {
5435 ret
= consumer_snapshot_channel(socket
, ua_chan
->key
, output
,
5436 0, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
,
5437 nb_packets_per_stream
);
5443 registry
= get_session_registry(ua_sess
);
5445 ret
= consumer_snapshot_channel(socket
, registry
->metadata_key
, output
,
5446 1, ua_sess
->euid
, ua_sess
->egid
, pathname
, wait
, 0);
5459 if (!snapshot_done
) {
5461 * If no snapshot was made and we are not in the error path, this means
5462 * that there are no buffers thus no (prior) application to snapshot
5463 * data from so we have simply NO data.
5474 * Return the size taken by one more packet per stream.
5476 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session
*usess
,
5477 uint64_t cur_nr_packets
)
5479 uint64_t tot_size
= 0;
5480 struct ust_app
*app
;
5481 struct lttng_ht_iter iter
;
5485 switch (usess
->buffer_type
) {
5486 case LTTNG_BUFFER_PER_UID
:
5488 struct buffer_reg_uid
*reg
;
5490 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
5491 struct buffer_reg_channel
*reg_chan
;
5494 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
5495 reg_chan
, node
.node
) {
5496 if (cur_nr_packets
>= reg_chan
->num_subbuf
) {
5498 * Don't take channel into account if we
5499 * already grab all its packets.
5503 tot_size
+= reg_chan
->subbuf_size
* reg_chan
->stream_count
;
5509 case LTTNG_BUFFER_PER_PID
:
5512 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5513 struct ust_app_channel
*ua_chan
;
5514 struct ust_app_session
*ua_sess
;
5515 struct lttng_ht_iter chan_iter
;
5517 ua_sess
= lookup_session_by_app(usess
, app
);
5519 /* Session not associated with this app. */
5523 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
5524 ua_chan
, node
.node
) {
5525 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
5527 * Don't take channel into account if we
5528 * already grab all its packets.
5532 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;