2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
19 #include <sys/types.h>
21 #include <urcu/compiler.h>
24 #include <common/bytecode/bytecode.h>
25 #include <common/compat/errno.h>
26 #include <common/common.h>
27 #include <common/hashtable/utils.h>
28 #include <common/shm.h>
29 #include <lttng/lttng-error.h>
30 #include <lttng/event-rule/event-rule.h>
31 #include <lttng/event-rule/event-rule-internal.h>
32 #include <lttng/event-rule/tracepoint.h>
33 #include <lttng/condition/condition.h>
34 #include <lttng/condition/on-event-internal.h>
35 #include <lttng/condition/on-event.h>
36 #include <lttng/map/map.h>
37 #include <lttng/map/map-internal.h>
38 #include <lttng/map/map-query-internal.h>
39 #include <lttng/map-key.h>
40 #include <lttng/map-key-internal.h>
41 #include <lttng/trigger/trigger-internal.h>
42 #include <common/sessiond-comm/sessiond-comm.h>
44 #include "buffer-registry.h"
45 #include "condition-internal.h"
47 #include "health-sessiond.h"
49 #include "ust-consumer.h"
50 #include "lttng-ust-ctl.h"
51 #include "lttng-ust-error.h"
54 #include "lttng-sessiond.h"
55 #include "notification-thread-commands.h"
58 #include "event-notifier-error-accounting.h"
62 struct lttng_ht
*ust_app_ht
;
63 struct lttng_ht
*ust_app_ht_by_sock
;
64 struct lttng_ht
*ust_app_ht_by_notify_sock
;
67 int ust_app_flush_app_session(struct ust_app
*app
, struct ust_app_session
*ua_sess
);
69 /* Next available channel key. Access under next_channel_key_lock. */
70 static uint64_t _next_channel_key
;
71 static pthread_mutex_t next_channel_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
73 /* Next available map key. Access under next_map_key_lock. */
74 static uint64_t _next_map_key
;
75 static pthread_mutex_t next_map_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
77 /* Next available session ID. Access under next_session_id_lock. */
78 static uint64_t _next_session_id
;
79 static pthread_mutex_t next_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
82 * Return the incremented value of next_channel_key.
84 static uint64_t get_next_channel_key(void)
88 pthread_mutex_lock(&next_channel_key_lock
);
89 ret
= ++_next_channel_key
;
90 pthread_mutex_unlock(&next_channel_key_lock
);
95 * Return the incremented value of next_map_key.
97 static uint64_t get_next_map_key(void)
101 pthread_mutex_lock(&next_map_key_lock
);
102 ret
= ++_next_map_key
;
103 pthread_mutex_unlock(&next_map_key_lock
);
108 * Return the atomically incremented value of next_session_id.
110 static uint64_t get_next_session_id(void)
114 pthread_mutex_lock(&next_session_id_lock
);
115 ret
= ++_next_session_id
;
116 pthread_mutex_unlock(&next_session_id_lock
);
120 static void copy_channel_attr_to_ustctl(
121 struct ustctl_consumer_channel_attr
*attr
,
122 struct lttng_ust_channel_attr
*uattr
)
124 /* Copy event attributes since the layout is different. */
125 attr
->subbuf_size
= uattr
->subbuf_size
;
126 attr
->num_subbuf
= uattr
->num_subbuf
;
127 attr
->overwrite
= uattr
->overwrite
;
128 attr
->switch_timer_interval
= uattr
->switch_timer_interval
;
129 attr
->read_timer_interval
= uattr
->read_timer_interval
;
130 attr
->output
= uattr
->output
;
131 attr
->blocking_timeout
= uattr
->u
.s
.blocking_timeout
;
135 * Match function for the hash table lookup.
137 * It matches an ust app event based on three attributes which are the event
138 * name, the filter bytecode and the loglevel.
140 static int ht_match_ust_app_event(struct cds_lfht_node
*node
, const void *_key
)
142 struct ust_app_event
*event
;
143 const struct ust_app_ht_key
*key
;
144 int ev_loglevel_value
;
149 event
= caa_container_of(node
, struct ust_app_event
, node
.node
);
151 ev_loglevel_value
= event
->attr
.loglevel
;
154 * Match the 5 elements of the key:
155 * tracer token, name, filter, loglevel, exclusions
158 if (event
->attr
.token
!= key
->tracer_token
) {
163 if (strncmp(event
->attr
.name
, key
->name
, sizeof(event
->attr
.name
)) != 0) {
167 /* Event loglevel. */
168 if (ev_loglevel_value
!= key
->loglevel_type
) {
169 if (event
->attr
.loglevel_type
== LTTNG_UST_LOGLEVEL_ALL
170 && key
->loglevel_type
== 0 &&
171 ev_loglevel_value
== -1) {
173 * Match is accepted. This is because on event creation, the
174 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
175 * -1 are accepted for this loglevel type since 0 is the one set by
176 * the API when receiving an enable event.
183 /* One of the filters is NULL, fail. */
184 if ((key
->filter
&& !event
->filter
) || (!key
->filter
&& event
->filter
)) {
188 if (key
->filter
&& event
->filter
) {
189 /* Both filters exists, check length followed by the bytecode. */
190 if (event
->filter
->len
!= key
->filter
->len
||
191 memcmp(event
->filter
->data
, key
->filter
->data
,
192 event
->filter
->len
) != 0) {
197 /* One of the exclusions is NULL, fail. */
198 if ((key
->exclusion
&& !event
->exclusion
) || (!key
->exclusion
&& event
->exclusion
)) {
202 if (key
->exclusion
&& event
->exclusion
) {
203 /* Both exclusions exists, check count followed by the names. */
204 if (event
->exclusion
->count
!= key
->exclusion
->count
||
205 memcmp(event
->exclusion
->names
, key
->exclusion
->names
,
206 event
->exclusion
->count
* LTTNG_UST_SYM_NAME_LEN
) != 0) {
220 * Unique add of an ust app event in the given ht. This uses the custom
221 * ht_match_ust_app_event match function and the event name as hash.
223 static void add_unique_ust_app_event(struct lttng_ht
*events_ht
,
224 struct ust_app_event
*event
)
226 struct cds_lfht_node
*node_ptr
;
227 struct ust_app_ht_key key
;
232 key
.name
= event
->attr
.name
;
233 key
.filter
= event
->filter
;
234 key
.loglevel_type
= event
->attr
.loglevel
;
235 key
.exclusion
= event
->exclusion
;
236 key
.tracer_token
= event
->attr
.token
;
238 node_ptr
= cds_lfht_add_unique(events_ht
->ht
,
239 events_ht
->hash_fct(event
->node
.key
, lttng_ht_seed
),
240 ht_match_ust_app_event
, &key
, &event
->node
.node
);
241 assert(node_ptr
== &event
->node
.node
);
245 * Close the notify socket from the given RCU head object. This MUST be called
246 * through a call_rcu().
248 static void close_notify_sock_rcu(struct rcu_head
*head
)
251 struct ust_app_notify_sock_obj
*obj
=
252 caa_container_of(head
, struct ust_app_notify_sock_obj
, head
);
254 /* Must have a valid fd here. */
255 assert(obj
->fd
>= 0);
257 ret
= close(obj
->fd
);
259 ERR("close notify sock %d RCU", obj
->fd
);
261 lttng_fd_put(LTTNG_FD_APPS
, 1);
267 * Return the session registry according to the buffer type of the given
270 * A registry per UID object MUST exists before calling this function or else
271 * it assert() if not found. RCU read side lock must be acquired.
273 static struct ust_registry_session
*get_session_registry(
274 struct ust_app_session
*ua_sess
)
276 struct ust_registry_session
*registry
= NULL
;
280 switch (ua_sess
->buffer_type
) {
281 case LTTNG_BUFFER_PER_PID
:
283 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
287 registry
= reg_pid
->registry
->reg
.ust
;
290 case LTTNG_BUFFER_PER_UID
:
292 struct buffer_reg_uid
*reg_uid
= buffer_reg_uid_find(
293 ua_sess
->tracing_id
, ua_sess
->bits_per_long
,
294 lttng_credentials_get_uid(&ua_sess
->real_credentials
));
298 registry
= reg_uid
->registry
->reg
.ust
;
310 * Delete ust context safely. RCU read lock must be held before calling
314 void delete_ust_app_ctx(int sock
, struct ust_app_ctx
*ua_ctx
,
322 pthread_mutex_lock(&app
->sock_lock
);
323 ret
= ustctl_release_object(sock
, ua_ctx
->obj
);
324 pthread_mutex_unlock(&app
->sock_lock
);
325 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
326 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
327 sock
, ua_ctx
->obj
->handle
, ret
);
335 * Delete ust app event safely. RCU read lock must be held before calling
339 void delete_ust_app_event(int sock
, struct ust_app_event
*ua_event
,
346 free(ua_event
->filter
);
347 if (ua_event
->exclusion
!= NULL
)
348 free(ua_event
->exclusion
);
349 if (ua_event
->obj
!= NULL
) {
350 pthread_mutex_lock(&app
->sock_lock
);
351 ret
= ustctl_release_object(sock
, ua_event
->obj
);
352 pthread_mutex_unlock(&app
->sock_lock
);
353 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
354 ERR("UST app sock %d release event obj failed with ret %d",
363 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
364 * through a call_rcu().
367 void free_ust_app_event_notifier_rule_rcu(struct rcu_head
*head
)
369 struct ust_app_event_notifier_rule
*obj
= caa_container_of(
370 head
, struct ust_app_event_notifier_rule
, rcu_head
);
376 * Delete ust app event notifier rule safely.
378 static void delete_ust_app_event_notifier_rule(int sock
,
379 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
,
384 assert(ua_event_notifier_rule
);
386 if (ua_event_notifier_rule
->exclusion
!= NULL
) {
387 free(ua_event_notifier_rule
->exclusion
);
390 if (ua_event_notifier_rule
->obj
!= NULL
) {
391 pthread_mutex_lock(&app
->sock_lock
);
392 ret
= ustctl_release_object(sock
, ua_event_notifier_rule
->obj
);
393 pthread_mutex_unlock(&app
->sock_lock
);
394 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
395 ERR("Failed to release event notifier object: app = '%s' (ppid %d), ret = %d",
396 app
->name
, (int) app
->ppid
, ret
);
399 free(ua_event_notifier_rule
->obj
);
402 lttng_trigger_put(ua_event_notifier_rule
->trigger
);
403 call_rcu(&ua_event_notifier_rule
->rcu_head
,
404 free_ust_app_event_notifier_rule_rcu
);
408 * Release ust data object of the given stream.
410 * Return 0 on success or else a negative value.
412 static int release_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
420 pthread_mutex_lock(&app
->sock_lock
);
421 ret
= ustctl_release_object(sock
, stream
->obj
);
422 pthread_mutex_unlock(&app
->sock_lock
);
423 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
424 ERR("UST app sock %d release stream obj failed with ret %d",
427 lttng_fd_put(LTTNG_FD_APPS
, 2);
435 * Release ust data object of the given map_counter.
437 * Return 0 on success or else a negative value.
439 static int release_ust_app_map_counter(int sock
, struct ust_app_map_counter
*map_counter
,
446 if (map_counter
->obj
) {
447 pthread_mutex_lock(&app
->sock_lock
);
448 ret
= ustctl_release_object(sock
, map_counter
->obj
);
449 pthread_mutex_unlock(&app
->sock_lock
);
450 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
451 ERR("UST app sock %d release map_counter obj failed with ret %d",
454 lttng_fd_put(LTTNG_FD_APPS
, 2);
455 free(map_counter
->obj
);
462 * Delete ust app stream safely. RCU read lock must be held before calling
466 void delete_ust_app_stream(int sock
, struct ust_app_stream
*stream
,
471 (void) release_ust_app_stream(sock
, stream
, app
);
476 * Delete ust app map_counter safely. RCU read lock must be held before calling
480 void delete_ust_app_map_counter(int sock
, struct ust_app_map_counter
*map_counter
,
485 (void) release_ust_app_map_counter(sock
, map_counter
, app
);
490 * We need to execute ht_destroy outside of RCU read-side critical
491 * section and outside of call_rcu thread, so we postpone its execution
492 * using ht_cleanup_push. It is simpler than to change the semantic of
493 * the many callers of delete_ust_app_session().
496 void delete_ust_app_channel_rcu(struct rcu_head
*head
)
498 struct ust_app_channel
*ua_chan
=
499 caa_container_of(head
, struct ust_app_channel
, rcu_head
);
501 ht_cleanup_push(ua_chan
->ctx
);
502 ht_cleanup_push(ua_chan
->events
);
507 * We need to execute ht_destroy outside of RCU read-side critical
508 * section and outside of call_rcu thread, so we postpone its execution
509 * using ht_cleanup_push. It is simpler than to change the semantic of
510 * the many callers of delete_ust_app_session().
513 void delete_ust_app_map_rcu(struct rcu_head
*head
)
515 struct ust_app_map
*ua_map
=
516 caa_container_of(head
, struct ust_app_map
, rcu_head
);
518 ht_cleanup_push(ua_map
->events
);
523 * Extract the lost packet or discarded events counter when the channel is
524 * being deleted and store the value in the parent channel so we can
525 * access it from lttng list and at stop/destroy.
527 * The session list lock must be held by the caller.
530 void save_per_pid_lost_discarded_counters(struct ust_app_channel
*ua_chan
)
532 uint64_t discarded
= 0, lost
= 0;
533 struct ltt_session
*session
;
534 struct ltt_ust_channel
*uchan
;
536 if (ua_chan
->attr
.type
!= LTTNG_UST_CHAN_PER_CPU
) {
541 session
= session_find_by_id(ua_chan
->session
->tracing_id
);
542 if (!session
|| !session
->ust_session
) {
544 * Not finding the session is not an error because there are
545 * multiple ways the channels can be torn down.
547 * 1) The session daemon can initiate the destruction of the
548 * ust app session after receiving a destroy command or
549 * during its shutdown/teardown.
550 * 2) The application, since we are in per-pid tracing, is
551 * unregistering and tearing down its ust app session.
553 * Both paths are protected by the session list lock which
554 * ensures that the accounting of lost packets and discarded
555 * events is done exactly once. The session is then unpublished
556 * from the session list, resulting in this condition.
561 if (ua_chan
->attr
.overwrite
) {
562 consumer_get_lost_packets(ua_chan
->session
->tracing_id
,
563 ua_chan
->key
, session
->ust_session
->consumer
,
566 consumer_get_discarded_events(ua_chan
->session
->tracing_id
,
567 ua_chan
->key
, session
->ust_session
->consumer
,
570 uchan
= trace_ust_find_channel_by_name(
571 session
->ust_session
->domain_global
.channels
,
574 ERR("Missing UST channel to store discarded counters");
578 uchan
->per_pid_closed_app_discarded
+= discarded
;
579 uchan
->per_pid_closed_app_lost
+= lost
;
584 session_put(session
);
589 * Delete ust app channel safely. RCU read lock must be held before calling
592 * The session list lock must be held by the caller.
595 void delete_ust_app_channel(int sock
, struct ust_app_channel
*ua_chan
,
599 struct lttng_ht_iter iter
;
600 struct ust_app_event
*ua_event
;
601 struct ust_app_ctx
*ua_ctx
;
602 struct ust_app_stream
*stream
, *stmp
;
603 struct ust_registry_session
*registry
;
607 DBG3("UST app deleting channel %s", ua_chan
->name
);
610 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
611 cds_list_del(&stream
->list
);
612 delete_ust_app_stream(sock
, stream
, app
);
616 cds_lfht_for_each_entry(ua_chan
->ctx
->ht
, &iter
.iter
, ua_ctx
, node
.node
) {
617 cds_list_del(&ua_ctx
->list
);
618 ret
= lttng_ht_del(ua_chan
->ctx
, &iter
);
620 delete_ust_app_ctx(sock
, ua_ctx
, app
);
624 cds_lfht_for_each_entry(ua_chan
->events
->ht
, &iter
.iter
, ua_event
,
626 ret
= lttng_ht_del(ua_chan
->events
, &iter
);
628 delete_ust_app_event(sock
, ua_event
, app
);
631 if (ua_chan
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
632 /* Wipe and free registry from session registry. */
633 registry
= get_session_registry(ua_chan
->session
);
635 ust_registry_channel_del_free(registry
, ua_chan
->key
,
639 * A negative socket can be used by the caller when
640 * cleaning-up a ua_chan in an error path. Skip the
641 * accounting in this case.
644 save_per_pid_lost_discarded_counters(ua_chan
);
648 if (ua_chan
->obj
!= NULL
) {
649 /* Remove channel from application UST object descriptor. */
650 iter
.iter
.node
= &ua_chan
->ust_objd_node
.node
;
651 ret
= lttng_ht_del(app
->ust_chan_objd
, &iter
);
653 pthread_mutex_lock(&app
->sock_lock
);
654 ret
= ustctl_release_object(sock
, ua_chan
->obj
);
655 pthread_mutex_unlock(&app
->sock_lock
);
656 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
657 ERR("UST app sock %d release channel obj failed with ret %d",
660 lttng_fd_put(LTTNG_FD_APPS
, 1);
663 call_rcu(&ua_chan
->rcu_head
, delete_ust_app_channel_rcu
);
667 void copy_ust_app_map_values(int sock
, struct ust_app_map
*ua_map
,
670 struct ltt_ust_map_dead_pid_kv_values
*kv_pair_list
= ua_map
->dead_app_kv_values
;
671 struct ust_registry_session
*ust_reg_sess
;
672 struct ust_registry_map
*ust_reg_map
;
673 struct ust_registry_map_index_ht_entry
*map_index_entry
;
674 struct lttng_ht_iter key_iter
;
675 struct lttng_ht
*dead_app_kv_values
;
677 assert(app
->buffer_type
== LTTNG_BUFFER_PER_PID
);
678 ust_reg_sess
= get_session_registry(ua_map
->session
);
680 pthread_mutex_lock(&ust_reg_sess
->lock
);
681 ust_reg_map
= ust_registry_map_find(ust_reg_sess
, ua_map
->key
);
682 pthread_mutex_unlock(&ust_reg_sess
->lock
);
685 DBG("Aggregating dead map values");
687 pthread_mutex_lock(&kv_pair_list
->lock
);
689 if (app
->bits_per_long
== 32) {
690 dead_app_kv_values
= kv_pair_list
->dead_app_kv_values_32bits
;
692 dead_app_kv_values
= kv_pair_list
->dead_app_kv_values_64bits
;
695 /* Iterate over all the formated_key -> counter index */
696 cds_lfht_for_each_entry(ust_reg_map
->key_string_to_bucket_index_ht
->ht
,
697 &key_iter
.iter
, map_index_entry
, node
.node
) {
698 bool overflow
= 0, underflow
= 0;
699 int64_t local_value
= 0;
701 size_t dimension_indexes
[1] = {map_index_entry
->index
};
703 ret
= ustctl_counter_aggregate(ua_map
->map_handle
,
704 dimension_indexes
, &local_value
, &overflow
,
707 ERR("Error getting counter value from the tracer: key = '%s'",
708 map_index_entry
->formated_key
);
713 map_add_or_increment_map_values(dead_app_kv_values
,
714 map_index_entry
->formated_key
, local_value
,
715 underflow
, overflow
);
720 pthread_mutex_unlock(&kv_pair_list
->lock
);
724 * Delete ust app map safely. RCU read lock must be held before calling
727 * The session list lock must be held by the caller.
730 void delete_ust_app_map(int sock
, struct ust_app_map
*ua_map
,
734 struct lttng_ht_iter iter
;
735 struct ust_app_event
*ua_event
;
736 struct ust_app_map_counter
*map_counter
, *ctmp
;
737 struct ust_registry_session
*registry
;
741 DBG3("UST app deleting map %s", ua_map
->name
);
744 cds_list_for_each_entry_safe(map_counter
, ctmp
, &ua_map
->counters
.head
, list
) {
745 cds_list_del(&map_counter
->list
);
746 delete_ust_app_map_counter(sock
, map_counter
, app
);
750 cds_lfht_for_each_entry(ua_map
->events
->ht
, &iter
.iter
, ua_event
,
752 ret
= lttng_ht_del(ua_map
->events
, &iter
);
754 delete_ust_app_event(sock
, ua_event
, app
);
757 if (ua_map
->session
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
758 /* Wipe and free registry from session registry. */
759 registry
= get_session_registry(ua_map
->session
);
761 ust_registry_map_del_free(registry
, ua_map
->key
);
765 if (ua_map
->obj
!= NULL
) {
766 /* Remove map from application UST object descriptor. */
767 iter
.iter
.node
= &ua_map
->ust_objd_node
.node
;
768 ret
= lttng_ht_del(app
->ust_map_objd
, &iter
);
770 pthread_mutex_lock(&app
->sock_lock
);
771 ret
= ustctl_release_object(sock
, ua_map
->obj
);
772 pthread_mutex_unlock(&app
->sock_lock
);
773 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
774 ERR("UST app sock %d release map obj failed with ret %d",
777 lttng_fd_put(LTTNG_FD_APPS
, 1);
780 call_rcu(&ua_map
->rcu_head
, delete_ust_app_map_rcu
);
783 int ust_app_register_done(struct ust_app
*app
)
787 pthread_mutex_lock(&app
->sock_lock
);
788 ret
= ustctl_register_done(app
->sock
);
789 pthread_mutex_unlock(&app
->sock_lock
);
793 int ust_app_release_object(struct ust_app
*app
, struct lttng_ust_object_data
*data
)
798 pthread_mutex_lock(&app
->sock_lock
);
803 ret
= ustctl_release_object(sock
, data
);
805 pthread_mutex_unlock(&app
->sock_lock
);
811 * Push metadata to consumer socket.
813 * RCU read-side lock must be held to guarantee existance of socket.
814 * Must be called with the ust app session lock held.
815 * Must be called with the registry lock held.
817 * On success, return the len of metadata pushed or else a negative value.
818 * Returning a -EPIPE return value means we could not send the metadata,
819 * but it can be caused by recoverable errors (e.g. the application has
820 * terminated concurrently).
822 ssize_t
ust_app_push_metadata(struct ust_registry_session
*registry
,
823 struct consumer_socket
*socket
, int send_zero_data
)
826 char *metadata_str
= NULL
;
827 size_t len
, offset
, new_metadata_len_sent
;
829 uint64_t metadata_key
, metadata_version
;
834 metadata_key
= registry
->metadata_key
;
837 * Means that no metadata was assigned to the session. This can
838 * happens if no start has been done previously.
844 offset
= registry
->metadata_len_sent
;
845 len
= registry
->metadata_len
- registry
->metadata_len_sent
;
846 new_metadata_len_sent
= registry
->metadata_len
;
847 metadata_version
= registry
->metadata_version
;
849 DBG3("No metadata to push for metadata key %" PRIu64
,
850 registry
->metadata_key
);
852 if (send_zero_data
) {
853 DBG("No metadata to push");
859 /* Allocate only what we have to send. */
860 metadata_str
= zmalloc(len
);
862 PERROR("zmalloc ust app metadata string");
866 /* Copy what we haven't sent out. */
867 memcpy(metadata_str
, registry
->metadata
+ offset
, len
);
870 pthread_mutex_unlock(®istry
->lock
);
872 * We need to unlock the registry while we push metadata to
873 * break a circular dependency between the consumerd metadata
874 * lock and the sessiond registry lock. Indeed, pushing metadata
875 * to the consumerd awaits that it gets pushed all the way to
876 * relayd, but doing so requires grabbing the metadata lock. If
877 * a concurrent metadata request is being performed by
878 * consumerd, this can try to grab the registry lock on the
879 * sessiond while holding the metadata lock on the consumer
880 * daemon. Those push and pull schemes are performed on two
881 * different bidirectionnal communication sockets.
883 ret
= consumer_push_metadata(socket
, metadata_key
,
884 metadata_str
, len
, offset
, metadata_version
);
885 pthread_mutex_lock(®istry
->lock
);
888 * There is an acceptable race here between the registry
889 * metadata key assignment and the creation on the
890 * consumer. The session daemon can concurrently push
891 * metadata for this registry while being created on the
892 * consumer since the metadata key of the registry is
893 * assigned *before* it is setup to avoid the consumer
894 * to ask for metadata that could possibly be not found
895 * in the session daemon.
897 * The metadata will get pushed either by the session
898 * being stopped or the consumer requesting metadata if
899 * that race is triggered.
901 if (ret
== -LTTCOMM_CONSUMERD_CHANNEL_FAIL
) {
904 ERR("Error pushing metadata to consumer");
910 * Metadata may have been concurrently pushed, since
911 * we're not holding the registry lock while pushing to
912 * consumer. This is handled by the fact that we send
913 * the metadata content, size, and the offset at which
914 * that metadata belongs. This may arrive out of order
915 * on the consumer side, and the consumer is able to
916 * deal with overlapping fragments. The consumer
917 * supports overlapping fragments, which must be
918 * contiguous starting from offset 0. We keep the
919 * largest metadata_len_sent value of the concurrent
922 registry
->metadata_len_sent
=
923 max_t(size_t, registry
->metadata_len_sent
,
924 new_metadata_len_sent
);
933 * On error, flag the registry that the metadata is
934 * closed. We were unable to push anything and this
935 * means that either the consumer is not responding or
936 * the metadata cache has been destroyed on the
939 registry
->metadata_closed
= 1;
947 * For a given application and session, push metadata to consumer.
948 * Either sock or consumer is required : if sock is NULL, the default
949 * socket to send the metadata is retrieved from consumer, if sock
950 * is not NULL we use it to send the metadata.
951 * RCU read-side lock must be held while calling this function,
952 * therefore ensuring existance of registry. It also ensures existance
953 * of socket throughout this function.
955 * Return 0 on success else a negative error.
956 * Returning a -EPIPE return value means we could not send the metadata,
957 * but it can be caused by recoverable errors (e.g. the application has
958 * terminated concurrently).
960 static int push_metadata(struct ust_registry_session
*registry
,
961 struct consumer_output
*consumer
)
965 struct consumer_socket
*socket
;
970 pthread_mutex_lock(®istry
->lock
);
971 if (registry
->metadata_closed
) {
976 /* Get consumer socket to use to push the metadata.*/
977 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
984 ret
= ust_app_push_metadata(registry
, socket
, 0);
989 pthread_mutex_unlock(®istry
->lock
);
993 pthread_mutex_unlock(®istry
->lock
);
998 * Send to the consumer a close metadata command for the given session. Once
999 * done, the metadata channel is deleted and the session metadata pointer is
1000 * nullified. The session lock MUST be held unless the application is
1001 * in the destroy path.
1003 * Do not hold the registry lock while communicating with the consumerd, because
1004 * doing so causes inter-process deadlocks between consumerd and sessiond with
1005 * the metadata request notification.
1007 * Return 0 on success else a negative value.
1009 static int close_metadata(struct ust_registry_session
*registry
,
1010 struct consumer_output
*consumer
)
1013 struct consumer_socket
*socket
;
1014 uint64_t metadata_key
;
1015 bool registry_was_already_closed
;
1022 pthread_mutex_lock(®istry
->lock
);
1023 metadata_key
= registry
->metadata_key
;
1024 registry_was_already_closed
= registry
->metadata_closed
;
1025 if (metadata_key
!= 0) {
1027 * Metadata closed. Even on error this means that the consumer
1028 * is not responding or not found so either way a second close
1029 * should NOT be emit for this registry.
1031 registry
->metadata_closed
= 1;
1033 pthread_mutex_unlock(®istry
->lock
);
1035 if (metadata_key
== 0 || registry_was_already_closed
) {
1040 /* Get consumer socket to use to push the metadata.*/
1041 socket
= consumer_find_socket_by_bitness(registry
->bits_per_long
,
1048 ret
= consumer_close_metadata(socket
, metadata_key
);
1059 * We need to execute ht_destroy outside of RCU read-side critical
1060 * section and outside of call_rcu thread, so we postpone its execution
1061 * using ht_cleanup_push. It is simpler than to change the semantic of
1062 * the many callers of delete_ust_app_session().
1065 void delete_ust_app_session_rcu(struct rcu_head
*head
)
1067 struct ust_app_session
*ua_sess
=
1068 caa_container_of(head
, struct ust_app_session
, rcu_head
);
1070 ht_cleanup_push(ua_sess
->channels
);
1071 ht_cleanup_push(ua_sess
->maps
);
1076 * Delete ust app session safely. RCU read lock must be held before calling
1079 * The session list lock must be held by the caller.
1082 void delete_ust_app_session(int sock
, struct ust_app_session
*ua_sess
,
1083 struct ust_app
*app
)
1086 struct lttng_ht_iter iter
;
1087 struct ust_app_channel
*ua_chan
;
1088 struct ust_app_map
*ua_map
;
1089 struct ust_registry_session
*registry
;
1093 pthread_mutex_lock(&ua_sess
->lock
);
1095 assert(!ua_sess
->deleted
);
1096 ua_sess
->deleted
= true;
1098 registry
= get_session_registry(ua_sess
);
1099 /* Registry can be null on error path during initialization. */
1101 /* Push metadata for application before freeing the application. */
1102 (void) push_metadata(registry
, ua_sess
->consumer
);
1105 * Don't ask to close metadata for global per UID buffers. Close
1106 * metadata only on destroy trace session in this case. Also, the
1107 * previous push metadata could have flag the metadata registry to
1108 * close so don't send a close command if closed.
1110 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
1111 /* And ask to close it for this session registry. */
1112 (void) close_metadata(registry
, ua_sess
->consumer
);
1116 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
1118 ret
= lttng_ht_del(ua_sess
->channels
, &iter
);
1120 delete_ust_app_channel(sock
, ua_chan
, app
);
1123 cds_lfht_for_each_entry(ua_sess
->maps
->ht
, &iter
.iter
, ua_map
,
1125 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
1126 copy_ust_app_map_values(sock
, ua_map
, app
);
1128 ret
= lttng_ht_del(ua_sess
->maps
, &iter
);
1130 delete_ust_app_map(sock
, ua_map
, app
);
1133 /* In case of per PID, the registry is kept in the session. */
1134 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
1135 struct buffer_reg_pid
*reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
1138 * Registry can be null on error path during
1141 buffer_reg_pid_remove(reg_pid
);
1142 buffer_reg_pid_destroy(reg_pid
);
1146 if (ua_sess
->handle
!= -1) {
1147 pthread_mutex_lock(&app
->sock_lock
);
1148 ret
= ustctl_release_handle(sock
, ua_sess
->handle
);
1149 pthread_mutex_unlock(&app
->sock_lock
);
1150 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1151 ERR("UST app sock %d release session handle failed with ret %d",
1154 /* Remove session from application UST object descriptor. */
1155 iter
.iter
.node
= &ua_sess
->ust_objd_node
.node
;
1156 ret
= lttng_ht_del(app
->ust_sessions_objd
, &iter
);
1160 pthread_mutex_unlock(&ua_sess
->lock
);
1162 consumer_output_put(ua_sess
->consumer
);
1164 call_rcu(&ua_sess
->rcu_head
, delete_ust_app_session_rcu
);
1168 * Delete a traceable application structure from the global list. Never call
1169 * this function outside of a call_rcu call.
1171 * RCU read side lock should _NOT_ be held when calling this function.
1174 void delete_ust_app(struct ust_app
*app
)
1177 struct ust_app_session
*ua_sess
, *tmp_ua_sess
;
1178 struct lttng_ht_iter iter
;
1179 struct ust_app_event_notifier_rule
*event_notifier_rule
;
1180 bool event_notifier_write_fd_is_open
;
1183 * The session list lock must be held during this function to guarantee
1184 * the existence of ua_sess.
1186 session_lock_list();
1187 /* Delete ust app sessions info */
1192 cds_list_for_each_entry_safe(ua_sess
, tmp_ua_sess
, &app
->teardown_head
,
1194 /* Free every object in the session and the session. */
1196 delete_ust_app_session(sock
, ua_sess
, app
);
1200 /* Remove the event notifier rules associated with this app. */
1202 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
1203 &iter
.iter
, event_notifier_rule
, node
.node
) {
1204 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
, &iter
);
1207 delete_ust_app_event_notifier_rule(
1208 app
->sock
, event_notifier_rule
, app
);
1213 ht_cleanup_push(app
->sessions
);
1214 ht_cleanup_push(app
->ust_sessions_objd
);
1215 ht_cleanup_push(app
->ust_chan_objd
);
1216 ht_cleanup_push(app
->ust_map_objd
);
1217 ht_cleanup_push(app
->token_to_event_notifier_rule_ht
);
1220 * This could be NULL if the event notifier setup failed (e.g the app
1221 * was killed or the tracer does not support this feature).
1223 if (app
->event_notifier_group
.object
) {
1224 enum lttng_error_code ret_code
;
1225 enum event_notifier_error_accounting_status status
;
1227 const int event_notifier_read_fd
= lttng_pipe_get_readfd(
1228 app
->event_notifier_group
.event_pipe
);
1230 ret_code
= notification_thread_command_remove_tracer_event_source(
1231 notification_thread_handle
,
1232 event_notifier_read_fd
);
1233 if (ret_code
!= LTTNG_OK
) {
1234 ERR("Failed to remove application tracer event source from notification thread");
1237 status
= event_notifier_error_accounting_unregister_app(app
);
1238 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1239 ERR("Error unregistering app from event notifier error accounting");
1242 ustctl_release_object(sock
, app
->event_notifier_group
.object
);
1243 free(app
->event_notifier_group
.object
);
1246 event_notifier_write_fd_is_open
= lttng_pipe_is_write_open(
1247 app
->event_notifier_group
.event_pipe
);
1248 lttng_pipe_destroy(app
->event_notifier_group
.event_pipe
);
1250 * Release the file descriptors reserved for the event notifier pipe.
1251 * The app could be destroyed before the write end of the pipe could be
1252 * passed to the application (and closed). In that case, both file
1253 * descriptors must be released.
1255 lttng_fd_put(LTTNG_FD_APPS
, event_notifier_write_fd_is_open
? 2 : 1);
1258 * Wait until we have deleted the application from the sock hash table
1259 * before closing this socket, otherwise an application could re-use the
1260 * socket ID and race with the teardown, using the same hash table entry.
1262 * It's OK to leave the close in call_rcu. We want it to stay unique for
1263 * all RCU readers that could run concurrently with unregister app,
1264 * therefore we _need_ to only close that socket after a grace period. So
1265 * it should stay in this RCU callback.
1267 * This close() is a very important step of the synchronization model so
1268 * every modification to this function must be carefully reviewed.
1274 lttng_fd_put(LTTNG_FD_APPS
, 1);
1276 DBG2("UST app pid %d deleted", app
->pid
);
1278 session_unlock_list();
1282 * URCU intermediate call to delete an UST app.
1285 void delete_ust_app_rcu(struct rcu_head
*head
)
1287 struct lttng_ht_node_ulong
*node
=
1288 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
1289 struct ust_app
*app
=
1290 caa_container_of(node
, struct ust_app
, pid_n
);
1292 DBG3("Call RCU deleting app PID %d", app
->pid
);
1293 delete_ust_app(app
);
1297 * Delete the session from the application ht and delete the data structure by
1298 * freeing every object inside and releasing them.
1300 * The session list lock must be held by the caller.
1302 static void destroy_app_session(struct ust_app
*app
,
1303 struct ust_app_session
*ua_sess
)
1306 struct lttng_ht_iter iter
;
1311 iter
.iter
.node
= &ua_sess
->node
.node
;
1312 ret
= lttng_ht_del(app
->sessions
, &iter
);
1314 /* Already scheduled for teardown. */
1318 /* Once deleted, free the data structure. */
1319 delete_ust_app_session(app
->sock
, ua_sess
, app
);
1326 * Alloc new UST app session.
1329 struct ust_app_session
*alloc_ust_app_session(void)
1331 struct ust_app_session
*ua_sess
;
1333 /* Init most of the default value by allocating and zeroing */
1334 ua_sess
= zmalloc(sizeof(struct ust_app_session
));
1335 if (ua_sess
== NULL
) {
1340 ua_sess
->handle
= -1;
1341 ua_sess
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1342 ua_sess
->metadata_attr
.type
= LTTNG_UST_CHAN_METADATA
;
1343 ua_sess
->maps
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1344 pthread_mutex_init(&ua_sess
->lock
, NULL
);
1353 * Alloc new UST app channel.
1356 struct ust_app_channel
*alloc_ust_app_channel(const char *name
,
1357 struct ust_app_session
*ua_sess
,
1358 struct lttng_ust_channel_attr
*attr
)
1360 struct ust_app_channel
*ua_chan
;
1362 /* Init most of the default value by allocating and zeroing */
1363 ua_chan
= zmalloc(sizeof(struct ust_app_channel
));
1364 if (ua_chan
== NULL
) {
1369 /* Setup channel name */
1370 strncpy(ua_chan
->name
, name
, sizeof(ua_chan
->name
));
1371 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
1373 ua_chan
->enabled
= 1;
1374 ua_chan
->handle
= -1;
1375 ua_chan
->session
= ua_sess
;
1376 ua_chan
->key
= get_next_channel_key();
1377 ua_chan
->ctx
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1378 ua_chan
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1379 lttng_ht_node_init_str(&ua_chan
->node
, ua_chan
->name
);
1381 CDS_INIT_LIST_HEAD(&ua_chan
->streams
.head
);
1382 CDS_INIT_LIST_HEAD(&ua_chan
->ctx_list
);
1384 /* Copy attributes */
1386 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
1387 ua_chan
->attr
.subbuf_size
= attr
->subbuf_size
;
1388 ua_chan
->attr
.num_subbuf
= attr
->num_subbuf
;
1389 ua_chan
->attr
.overwrite
= attr
->overwrite
;
1390 ua_chan
->attr
.switch_timer_interval
= attr
->switch_timer_interval
;
1391 ua_chan
->attr
.read_timer_interval
= attr
->read_timer_interval
;
1392 ua_chan
->attr
.output
= attr
->output
;
1393 ua_chan
->attr
.blocking_timeout
= attr
->u
.s
.blocking_timeout
;
1395 /* By default, the channel is a per cpu channel. */
1396 ua_chan
->attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1398 DBG3("UST app channel %s allocated", ua_chan
->name
);
1407 * Alloc new UST app map.
1410 struct ust_app_map
*alloc_ust_app_map(const char *name
,
1411 struct ust_app_session
*ua_sess
)
1413 struct ust_app_map
*ua_map
;
1415 /* Init most of the default value by allocating and zeroing */
1416 ua_map
= zmalloc(sizeof(struct ust_app_map
));
1417 if (ua_map
== NULL
) {
1422 /* Setup map name */
1423 strncpy(ua_map
->name
, name
, sizeof(ua_map
->name
));
1424 ua_map
->name
[sizeof(ua_map
->name
) - 1] = '\0';
1426 ua_map
->enabled
= 1;
1427 ua_map
->handle
= -1;
1428 ua_map
->session
= ua_sess
;
1429 ua_map
->key
= get_next_map_key();
1430 ua_map
->events
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
1431 lttng_ht_node_init_str(&ua_map
->node
, ua_map
->name
);
1433 CDS_INIT_LIST_HEAD(&ua_map
->counters
.head
);
1435 DBG3("UST app map %s allocated", ua_map
->name
);
1444 * Allocate and initialize a UST app stream.
1446 * Return newly allocated stream pointer or NULL on error.
1448 struct ust_app_stream
*ust_app_alloc_stream(void)
1450 struct ust_app_stream
*stream
= NULL
;
1452 stream
= zmalloc(sizeof(*stream
));
1453 if (stream
== NULL
) {
1454 PERROR("zmalloc ust app stream");
1458 /* Zero could be a valid value for a handle so flag it to -1. */
1459 stream
->handle
= -1;
1466 * Allocate and initialize a UST app map_counter.
1468 * Return newly allocated map_counter pointer or NULL on error.
1470 struct ust_app_map_counter
*ust_app_alloc_map_counter(void)
1472 struct ust_app_map_counter
*map_counter
= NULL
;
1474 map_counter
= zmalloc(sizeof(*map_counter
));
1475 if (map_counter
== NULL
) {
1476 PERROR("zmalloc ust app map_counter");
1480 /* Zero could be a valid value for a handle so flag it to -1. */
1481 map_counter
->handle
= -1;
1488 * Alloc new UST app event.
1491 struct ust_app_event
*alloc_ust_app_event(char *name
,
1492 struct lttng_ust_event
*attr
)
1494 struct ust_app_event
*ua_event
;
1496 /* Init most of the default value by allocating and zeroing */
1497 ua_event
= zmalloc(sizeof(struct ust_app_event
));
1498 if (ua_event
== NULL
) {
1499 PERROR("Failed to allocate ust_app_event structure");
1503 ua_event
->enabled
= 1;
1504 strncpy(ua_event
->name
, name
, sizeof(ua_event
->name
));
1505 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
1506 lttng_ht_node_init_str(&ua_event
->node
, ua_event
->name
);
1508 /* Copy attributes */
1510 memcpy(&ua_event
->attr
, attr
, sizeof(ua_event
->attr
));
1513 DBG3("UST app event %s allocated", ua_event
->name
);
1523 * Allocate a new UST app event notifier rule.
1525 static struct ust_app_event_notifier_rule
*alloc_ust_app_event_notifier_rule(
1526 struct lttng_trigger
*trigger
)
1528 enum lttng_event_rule_generate_exclusions_status
1529 generate_exclusion_status
;
1530 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
1531 struct lttng_condition
*condition
= NULL
;
1532 const struct lttng_event_rule
*event_rule
= NULL
;
1534 ua_event_notifier_rule
= zmalloc(sizeof(struct ust_app_event_notifier_rule
));
1535 if (ua_event_notifier_rule
== NULL
) {
1536 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1540 ua_event_notifier_rule
->enabled
= 1;
1541 ua_event_notifier_rule
->token
= lttng_trigger_get_tracer_token(trigger
);
1542 lttng_ht_node_init_u64(&ua_event_notifier_rule
->node
,
1543 ua_event_notifier_rule
->token
);
1545 condition
= lttng_trigger_get_condition(trigger
);
1547 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_ON_EVENT
);
1549 assert(LTTNG_CONDITION_STATUS_OK
== lttng_condition_on_event_get_rule(condition
, &event_rule
));
1552 /* Acquire the event notifier's reference to the trigger. */
1553 lttng_trigger_get(trigger
);
1555 ua_event_notifier_rule
->trigger
= trigger
;
1556 ua_event_notifier_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
1557 generate_exclusion_status
= lttng_event_rule_generate_exclusions(
1558 event_rule
, &ua_event_notifier_rule
->exclusion
);
1559 switch (generate_exclusion_status
) {
1560 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK
:
1561 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
:
1564 /* Error occured. */
1565 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1566 goto error_put_trigger
;
1569 DBG3("UST app event notifier rule allocated: token = %" PRIu64
,
1570 ua_event_notifier_rule
->token
);
1572 return ua_event_notifier_rule
;
1575 lttng_trigger_put(trigger
);
1577 free(ua_event_notifier_rule
);
1582 * Alloc new UST app context.
1585 struct ust_app_ctx
*alloc_ust_app_ctx(struct lttng_ust_context_attr
*uctx
)
1587 struct ust_app_ctx
*ua_ctx
;
1589 ua_ctx
= zmalloc(sizeof(struct ust_app_ctx
));
1590 if (ua_ctx
== NULL
) {
1594 CDS_INIT_LIST_HEAD(&ua_ctx
->list
);
1597 memcpy(&ua_ctx
->ctx
, uctx
, sizeof(ua_ctx
->ctx
));
1598 if (uctx
->ctx
== LTTNG_UST_CONTEXT_APP_CONTEXT
) {
1599 char *provider_name
= NULL
, *ctx_name
= NULL
;
1601 provider_name
= strdup(uctx
->u
.app_ctx
.provider_name
);
1602 ctx_name
= strdup(uctx
->u
.app_ctx
.ctx_name
);
1603 if (!provider_name
|| !ctx_name
) {
1604 free(provider_name
);
1609 ua_ctx
->ctx
.u
.app_ctx
.provider_name
= provider_name
;
1610 ua_ctx
->ctx
.u
.app_ctx
.ctx_name
= ctx_name
;
1614 DBG3("UST app context %d allocated", ua_ctx
->ctx
.ctx
);
1622 * Create a liblttng-ust filter bytecode from given bytecode.
1624 * Return allocated filter or NULL on error.
1626 static struct lttng_ust_filter_bytecode
*
1627 create_ust_filter_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1629 struct lttng_ust_filter_bytecode
*filter
= NULL
;
1631 /* Copy filter bytecode. */
1632 filter
= zmalloc(sizeof(*filter
) + orig_f
->len
);
1634 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1638 assert(sizeof(struct lttng_bytecode
) ==
1639 sizeof(struct lttng_ust_filter_bytecode
));
1640 memcpy(filter
, orig_f
, sizeof(*filter
) + orig_f
->len
);
1646 * Create a liblttng-ust capture bytecode from given bytecode.
1648 * Return allocated filter or NULL on error.
1650 static struct lttng_ust_capture_bytecode
*
1651 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode
*orig_f
)
1653 struct lttng_ust_capture_bytecode
*capture
= NULL
;
1655 /* Copy capture bytecode. */
1656 capture
= zmalloc(sizeof(*capture
) + orig_f
->len
);
1658 PERROR("Failed to allocate lttng_ust_capture_bytecode: bytecode len = %" PRIu32
" bytes", orig_f
->len
);
1662 assert(sizeof(struct lttng_bytecode
) ==
1663 sizeof(struct lttng_ust_capture_bytecode
));
1664 memcpy(capture
, orig_f
, sizeof(*capture
) + orig_f
->len
);
1670 * Find an ust_app using the sock and return it. RCU read side lock must be
1671 * held before calling this helper function.
1673 struct ust_app
*ust_app_find_by_sock(int sock
)
1675 struct lttng_ht_node_ulong
*node
;
1676 struct lttng_ht_iter iter
;
1678 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &iter
);
1679 node
= lttng_ht_iter_get_node_ulong(&iter
);
1681 DBG2("UST app find by sock %d not found", sock
);
1685 return caa_container_of(node
, struct ust_app
, sock_n
);
1692 * Find an ust_app using the notify sock and return it. RCU read side lock must
1693 * be held before calling this helper function.
1695 static struct ust_app
*find_app_by_notify_sock(int sock
)
1697 struct lttng_ht_node_ulong
*node
;
1698 struct lttng_ht_iter iter
;
1700 lttng_ht_lookup(ust_app_ht_by_notify_sock
, (void *)((unsigned long) sock
),
1702 node
= lttng_ht_iter_get_node_ulong(&iter
);
1704 DBG2("UST app find by notify sock %d not found", sock
);
1708 return caa_container_of(node
, struct ust_app
, notify_sock_n
);
1715 * Lookup for an ust app event based on event name, filter bytecode and the
1718 * Return an ust_app_event object or NULL on error.
1720 static struct ust_app_event
*find_ust_app_event(struct lttng_ht
*ht
,
1721 const char *name
, const struct lttng_bytecode
*filter
,
1723 const struct lttng_event_exclusion
*exclusion
,
1724 uint64_t tracer_token
)
1726 struct lttng_ht_iter iter
;
1727 struct lttng_ht_node_str
*node
;
1728 struct ust_app_event
*event
= NULL
;
1729 struct ust_app_ht_key key
;
1734 /* Setup key for event lookup. */
1736 key
.filter
= filter
;
1737 key
.loglevel_type
= loglevel_value
;
1738 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1739 key
.exclusion
= exclusion
;
1740 key
.tracer_token
= tracer_token
;
1742 /* Lookup using the event name as hash and a custom match fct. */
1743 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) name
, lttng_ht_seed
),
1744 ht_match_ust_app_event
, &key
, &iter
.iter
);
1745 node
= lttng_ht_iter_get_node_str(&iter
);
1750 event
= caa_container_of(node
, struct ust_app_event
, node
);
1757 * Look-up an event notifier rule based on its token id.
1759 * Must be called with the RCU read lock held.
1760 * Return an ust_app_event_notifier_rule object or NULL on error.
1762 static struct ust_app_event_notifier_rule
*find_ust_app_event_notifier_rule(
1763 struct lttng_ht
*ht
, uint64_t token
)
1765 struct lttng_ht_iter iter
;
1766 struct lttng_ht_node_u64
*node
;
1767 struct ust_app_event_notifier_rule
*event_notifier_rule
= NULL
;
1771 lttng_ht_lookup(ht
, &token
, &iter
);
1772 node
= lttng_ht_iter_get_node_u64(&iter
);
1774 DBG2("UST app event notifier rule token not found: token = %" PRIu64
,
1779 event_notifier_rule
= caa_container_of(
1780 node
, struct ust_app_event_notifier_rule
, node
);
1782 return event_notifier_rule
;
1786 * Create the channel context on the tracer.
1788 * Called with UST app session lock held.
1791 int create_ust_channel_context(struct ust_app_channel
*ua_chan
,
1792 struct ust_app_ctx
*ua_ctx
, struct ust_app
*app
)
1796 health_code_update();
1798 pthread_mutex_lock(&app
->sock_lock
);
1799 ret
= ustctl_add_context(app
->sock
, &ua_ctx
->ctx
,
1800 ua_chan
->obj
, &ua_ctx
->obj
);
1801 pthread_mutex_unlock(&app
->sock_lock
);
1803 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1804 ERR("UST app create channel context failed for app (pid: %d) "
1805 "with ret %d", app
->pid
, ret
);
1808 * This is normal behavior, an application can die during the
1809 * creation process. Don't report an error so the execution can
1810 * continue normally.
1813 DBG3("UST app add context failed. Application is dead.");
1818 ua_ctx
->handle
= ua_ctx
->obj
->handle
;
1820 DBG2("UST app context handle %d created successfully for channel %s",
1821 ua_ctx
->handle
, ua_chan
->name
);
1824 health_code_update();
1829 * Set the filter on the tracer.
1831 static int set_ust_object_filter(struct ust_app
*app
,
1832 const struct lttng_bytecode
*bytecode
,
1833 struct lttng_ust_object_data
*ust_object
)
1836 struct lttng_ust_filter_bytecode
*ust_bytecode
= NULL
;
1838 health_code_update();
1840 ust_bytecode
= create_ust_filter_bytecode_from_bytecode(bytecode
);
1841 if (!ust_bytecode
) {
1842 ret
= -LTTNG_ERR_NOMEM
;
1845 pthread_mutex_lock(&app
->sock_lock
);
1846 ret
= ustctl_set_filter(app
->sock
, ust_bytecode
,
1848 pthread_mutex_unlock(&app
->sock_lock
);
1850 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1851 ERR("UST app set object filter failed: object = %p of app pid = %d, ret = %d",
1852 ust_object
, app
->pid
, ret
);
1855 * This is normal behavior, an application can die during the
1856 * creation process. Don't report an error so the execution can
1857 * continue normally.
1860 DBG3("Failed to set UST app object filter. Application is dead.");
1865 DBG2("UST filter successfully set: object = %p", ust_object
);
1868 health_code_update();
1874 * Set a capture bytecode for the passed object.
1875 * The sequence number enforces the ordering at runtime and on reception of
1876 * the captured payloads.
1878 static int set_ust_capture(struct ust_app
*app
,
1879 const struct lttng_bytecode
*bytecode
,
1880 unsigned int capture_seqnum
,
1881 struct lttng_ust_object_data
*ust_object
)
1884 struct lttng_ust_capture_bytecode
*ust_bytecode
= NULL
;
1886 health_code_update();
1888 ust_bytecode
= create_ust_capture_bytecode_from_bytecode(bytecode
);
1889 if (!ust_bytecode
) {
1890 ret
= -LTTNG_ERR_NOMEM
;
1895 * Set the sequence number to ensure the capture of fields is ordered.
1897 ust_bytecode
->seqnum
= capture_seqnum
;
1899 pthread_mutex_lock(&app
->sock_lock
);
1900 ret
= ustctl_set_capture(app
->sock
, ust_bytecode
,
1902 pthread_mutex_unlock(&app
->sock_lock
);
1904 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1905 ERR("UST app set object capture failed: object = %p of app pid = %d, ret = %d",
1906 ust_object
, app
->pid
, ret
);
1909 * This is normal behavior, an application can die during the
1910 * creation process. Don't report an error so the execution can
1911 * continue normally.
1914 DBG3("Failed to set UST app object capture. Application is dead.");
1920 DBG2("UST capture successfully set: object = %p", ust_object
);
1923 health_code_update();
1929 struct lttng_ust_event_exclusion
*create_ust_exclusion_from_exclusion(
1930 const struct lttng_event_exclusion
*exclusion
)
1932 struct lttng_ust_event_exclusion
*ust_exclusion
= NULL
;
1933 size_t exclusion_alloc_size
= sizeof(struct lttng_ust_event_exclusion
) +
1934 LTTNG_UST_SYM_NAME_LEN
* exclusion
->count
;
1936 ust_exclusion
= zmalloc(exclusion_alloc_size
);
1937 if (!ust_exclusion
) {
1942 assert(sizeof(struct lttng_event_exclusion
) ==
1943 sizeof(struct lttng_ust_event_exclusion
));
1944 memcpy(ust_exclusion
, exclusion
, exclusion_alloc_size
);
1946 return ust_exclusion
;
1950 * Set event exclusions on the tracer.
1952 static int set_ust_object_exclusions(struct ust_app
*app
,
1953 const struct lttng_event_exclusion
*exclusions
,
1954 struct lttng_ust_object_data
*ust_object
)
1957 struct lttng_ust_event_exclusion
*ust_exclusions
= NULL
;
1959 assert(exclusions
&& exclusions
->count
> 0);
1961 health_code_update();
1963 ust_exclusions
= create_ust_exclusion_from_exclusion(
1965 if (!ust_exclusions
) {
1966 ret
= -LTTNG_ERR_NOMEM
;
1969 pthread_mutex_lock(&app
->sock_lock
);
1970 ret
= ustctl_set_exclusion(app
->sock
, ust_exclusions
, ust_object
);
1971 pthread_mutex_unlock(&app
->sock_lock
);
1973 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
1974 ERR("Failed to set UST app exclusions for object %p of app (pid: %d) "
1975 "with ret %d", ust_object
, app
->pid
, ret
);
1978 * This is normal behavior, an application can die during the
1979 * creation process. Don't report an error so the execution can
1980 * continue normally.
1983 DBG3("Failed to set UST app object exclusions. Application is dead.");
1988 DBG2("UST exclusions set successfully for object %p", ust_object
);
1991 health_code_update();
1992 free(ust_exclusions
);
1997 * Disable the specified event on to UST tracer for the UST session.
1999 static int disable_ust_object(struct ust_app
*app
,
2000 struct lttng_ust_object_data
*object
)
2004 health_code_update();
2006 pthread_mutex_lock(&app
->sock_lock
);
2007 ret
= ustctl_disable(app
->sock
, object
);
2008 pthread_mutex_unlock(&app
->sock_lock
);
2010 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2011 ERR("Failed to disable UST app object %p app (pid: %d) with ret %d",
2012 object
, app
->pid
, ret
);
2015 * This is normal behavior, an application can die during the
2016 * creation process. Don't report an error so the execution can
2017 * continue normally.
2020 DBG3("Failed to disable UST app object. Application is dead.");
2025 DBG2("UST app object %p disabled successfully for app (pid: %d)",
2029 health_code_update();
2034 * Disable the specified channel on to UST tracer for the UST session.
2036 static int disable_ust_channel(struct ust_app
*app
,
2037 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
2041 health_code_update();
2043 pthread_mutex_lock(&app
->sock_lock
);
2044 ret
= ustctl_disable(app
->sock
, ua_chan
->obj
);
2045 pthread_mutex_unlock(&app
->sock_lock
);
2047 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2048 ERR("UST app channel %s disable failed for app (pid: %d) "
2049 "and session handle %d with ret %d",
2050 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
2053 * This is normal behavior, an application can die during the
2054 * creation process. Don't report an error so the execution can
2055 * continue normally.
2058 DBG3("UST app disable channel failed. Application is dead.");
2063 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
2064 ua_chan
->name
, app
->pid
);
2067 health_code_update();
2072 * Disable the specified map on to UST tracer for the UST session.
2074 static int disable_ust_map(struct ust_app
*app
,
2075 struct ust_app_session
*ua_sess
, struct ust_app_map
*ua_map
)
2079 health_code_update();
2081 pthread_mutex_lock(&app
->sock_lock
);
2082 ret
= ustctl_disable(app
->sock
, ua_map
->obj
);
2083 pthread_mutex_unlock(&app
->sock_lock
);
2085 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2086 ERR("UST app map %s disable failed for app (pid: %d) "
2087 "and session handle %d with ret %d",
2088 ua_map
->name
, app
->pid
, ua_sess
->handle
, ret
);
2091 * This is normal behavior, an application can die during the
2092 * creation process. Don't report an error so the execution can
2093 * continue normally.
2096 DBG3("UST app disable map failed. Application is dead.");
2101 DBG2("UST app map %s disabled successfully for app (pid: %d)",
2102 ua_map
->name
, app
->pid
);
2105 health_code_update();
2110 * Enable the specified channel on to UST tracer for the UST session.
2112 static int enable_ust_channel(struct ust_app
*app
,
2113 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
2117 health_code_update();
2119 pthread_mutex_lock(&app
->sock_lock
);
2120 ret
= ustctl_enable(app
->sock
, ua_chan
->obj
);
2121 pthread_mutex_unlock(&app
->sock_lock
);
2123 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2124 ERR("UST app channel %s enable failed for app (pid: %d) "
2125 "and session handle %d with ret %d",
2126 ua_chan
->name
, app
->pid
, ua_sess
->handle
, ret
);
2129 * This is normal behavior, an application can die during the
2130 * creation process. Don't report an error so the execution can
2131 * continue normally.
2134 DBG3("UST app enable channel failed. Application is dead.");
2139 ua_chan
->enabled
= 1;
2141 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
2142 ua_chan
->name
, app
->pid
);
2145 health_code_update();
2150 * Enable the specified map on to UST tracer for the UST session.
2152 static int enable_ust_map(struct ust_app
*app
,
2153 struct ust_app_session
*ua_sess
, struct ust_app_map
*ua_map
)
2157 health_code_update();
2159 pthread_mutex_lock(&app
->sock_lock
);
2160 ret
= ustctl_enable(app
->sock
, ua_map
->obj
);
2161 pthread_mutex_unlock(&app
->sock_lock
);
2163 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2164 ERR("UST app map %s enable failed for app (pid: %d) "
2165 "and session handle %d with ret %d",
2166 ua_map
->name
, app
->pid
, ua_sess
->handle
, ret
);
2169 * This is normal behavior, an application can die during the
2170 * creation process. Don't report an error so the execution can
2171 * continue normally.
2174 DBG3("UST app enable map failed. Application is dead.");
2179 ua_map
->enabled
= 1;
2181 DBG2("UST app map %s enabled successfully for app (pid: %d)",
2182 ua_map
->name
, app
->pid
);
2185 health_code_update();
2190 * Enable the specified event on to UST tracer for the UST session.
2192 static int enable_ust_object(
2193 struct ust_app
*app
, struct lttng_ust_object_data
*ust_object
)
2197 health_code_update();
2199 pthread_mutex_lock(&app
->sock_lock
);
2200 ret
= ustctl_enable(app
->sock
, ust_object
);
2201 pthread_mutex_unlock(&app
->sock_lock
);
2203 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2204 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
2205 ust_object
, app
->pid
, ret
);
2208 * This is normal behavior, an application can die during the
2209 * creation process. Don't report an error so the execution can
2210 * continue normally.
2213 DBG3("Failed to enable UST app object. Application is dead.");
2218 DBG2("UST app object %p enabled successfully for app (pid: %d)",
2219 ust_object
, app
->pid
);
2222 health_code_update();
2227 * Send channel and stream buffer to application.
2229 * Return 0 on success. On error, a negative value is returned.
2231 static int send_channel_pid_to_ust(struct ust_app
*app
,
2232 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
)
2235 struct ust_app_stream
*stream
, *stmp
;
2241 health_code_update();
2243 DBG("UST app sending channel %s to UST app sock %d", ua_chan
->name
,
2246 /* Send channel to the application. */
2247 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
2248 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2249 ret
= -ENOTCONN
; /* Caused by app exiting. */
2251 } else if (ret
< 0) {
2255 health_code_update();
2257 /* Send all streams to application. */
2258 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
2259 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, stream
);
2260 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
2261 ret
= -ENOTCONN
; /* Caused by app exiting. */
2263 } else if (ret
< 0) {
2266 /* We don't need the stream anymore once sent to the tracer. */
2267 cds_list_del(&stream
->list
);
2268 delete_ust_app_stream(-1, stream
, app
);
2270 /* Flag the channel that it is sent to the application. */
2271 ua_chan
->is_sent
= 1;
2274 health_code_update();
2279 * Send map and stream buffer to application.
2281 * Return 0 on success. On error, a negative value is returned.
2283 static int send_map_pid_to_ust(struct ust_app
*app
,
2284 struct ust_app_session
*ua_sess
, struct ust_app_map
*ua_map
)
2287 struct ust_app_map_counter
*counter
, *ctmp
;
2293 health_code_update();
2295 DBG("UST app sending map %s to UST app sock %d", ua_map
->name
,
2298 /* Send map to the application. */
2299 pthread_mutex_lock(&app
->sock_lock
);
2300 ret
= ustctl_send_counter_data_to_ust(app
->sock
,
2301 ua_sess
->handle
, ua_map
->obj
);
2302 pthread_mutex_unlock(&app
->sock_lock
);
2305 ua_map
->handle
= ua_map
->obj
->handle
;
2307 health_code_update();
2309 /* Send all streams to application. */
2310 cds_list_for_each_entry_safe(counter
, ctmp
, &ua_map
->counters
.head
, list
) {
2311 pthread_mutex_lock(&app
->sock_lock
);
2312 // Do send the per cpu counter here
2313 ret
= ustctl_send_counter_cpu_data_to_ust(app
->sock
,
2314 ua_map
->obj
, counter
->obj
);
2315 pthread_mutex_unlock(&app
->sock_lock
);
2318 /* We don't need the stream anymore once sent to the tracer. */
2319 cds_list_del(&counter
->list
);
2320 delete_ust_app_map_counter(-1, counter
, app
);
2322 /* Flag the map that it is sent to the application. */
2323 ua_map
->is_sent
= 1;
2325 health_code_update();
2330 * Create the specified event onto the UST tracer for a UST session.
2332 * Should be called with session mutex held.
2335 int create_ust_channel_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2336 struct ust_app_channel
*ua_chan
, struct ust_app_event
*ua_event
)
2340 health_code_update();
2342 /* Create UST event on tracer */
2343 pthread_mutex_lock(&app
->sock_lock
);
2344 ret
= ustctl_create_event(app
->sock
, &ua_event
->attr
, ua_chan
->obj
,
2346 pthread_mutex_unlock(&app
->sock_lock
);
2348 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2350 ERR("Error ustctl create event %s for app pid: %d with ret %d",
2351 ua_event
->attr
.name
, app
->pid
, ret
);
2354 * This is normal behavior, an application can die during the
2355 * creation process. Don't report an error so the execution can
2356 * continue normally.
2359 DBG3("UST app create event failed. Application is dead.");
2364 ua_event
->handle
= ua_event
->obj
->handle
;
2366 DBG2("UST app event %s created successfully for pid:%d object: %p",
2367 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
2369 health_code_update();
2371 /* Set filter if one is present. */
2372 if (ua_event
->filter
) {
2373 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
2379 /* Set exclusions for the event */
2380 if (ua_event
->exclusion
) {
2381 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
2387 /* If event not enabled, disable it on the tracer */
2388 if (ua_event
->enabled
) {
2390 * We now need to explicitly enable the event, since it
2391 * is now disabled at creation.
2393 ret
= enable_ust_object(app
, ua_event
->obj
);
2396 * If we hit an EPERM, something is wrong with our enable call. If
2397 * we get an EEXIST, there is a problem on the tracer side since we
2401 case -LTTNG_UST_ERR_PERM
:
2402 /* Code flow problem */
2404 case -LTTNG_UST_ERR_EXIST
:
2405 /* It's OK for our use case. */
2416 health_code_update();
2421 void add_key_token(struct lttng_ust_key_token
*ust_key_token
,
2422 const struct lttng_map_key_token
*key_token
)
2424 switch (key_token
->type
) {
2425 case LTTNG_MAP_KEY_TOKEN_TYPE_STRING
:
2427 const struct lttng_map_key_token_string
*str_token
;
2428 str_token
= (typeof(str_token
)) key_token
;
2430 ust_key_token
->type
= LTTNG_UST_KEY_TOKEN_STRING
;
2431 strncpy(ust_key_token
->arg
.string
, str_token
->string
,
2432 LTTNG_UST_KEY_TOKEN_STRING_LEN_MAX
);
2436 case LTTNG_MAP_KEY_TOKEN_TYPE_VARIABLE
:
2438 const struct lttng_map_key_token_variable
*var_token
;
2439 var_token
= (typeof(var_token
)) key_token
;
2440 switch (var_token
->type
) {
2441 case LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_EVENT_NAME
:
2442 ust_key_token
->type
= LTTNG_UST_KEY_TOKEN_EVENT_NAME
;
2444 case LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_PROVIDER_NAME
:
2445 ust_key_token
->type
= LTTNG_UST_KEY_TOKEN_PROVIDER_NAME
;
2459 * Create the specified event onto the UST tracer for a UST session.
2461 * Should be called with session mutex held.
2464 int create_ust_map_event(struct ust_app
*app
, struct ust_app_session
*ua_sess
,
2465 struct ust_app_map
*ua_map
, const struct lttng_map_key
*key
,
2466 struct ust_app_event
*ua_event
)
2469 unsigned int i
, key_token_count
;
2470 enum lttng_map_key_status status
;
2471 struct lttng_ust_counter_event counter_event
= {0};
2473 health_code_update();
2475 memcpy(&counter_event
.event
, &ua_event
->attr
, sizeof(struct lttng_ust_event
));
2477 status
= lttng_map_key_get_token_count(key
, &key_token_count
);
2478 if (status
!= LTTNG_MAP_KEY_STATUS_OK
) {
2479 ret
= LTTNG_ERR_UNK
;
2483 assert(key_token_count
> 0);
2485 counter_event
.key
.nr_dimensions
= 1;
2486 counter_event
.key
.key_dimensions
[0].nr_key_tokens
= key_token_count
;
2488 if (key_token_count
> LTTNG_UST_NR_KEY_TOKEN
) {
2489 ERR("Too many key tokens for UST tracer: token count = %u token count max =%u",
2490 key_token_count
, LTTNG_UST_NR_KEY_TOKEN
);
2491 ret
= LTTNG_ERR_INVALID
;
2495 for (i
= 0; i
< key_token_count
; i
++) {
2496 const struct lttng_map_key_token
*token
=
2497 lttng_map_key_get_token_at_index(key
, i
);
2499 add_key_token(&counter_event
.key
.key_dimensions
[0].key_tokens
[i
],
2503 /* Create UST event on tracer */
2504 pthread_mutex_lock(&app
->sock_lock
);
2505 ret
= ustctl_counter_create_event(app
->sock
, &counter_event
, ua_map
->obj
,
2507 pthread_mutex_unlock(&app
->sock_lock
);
2509 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2511 ERR("Error ustctl counter create event %s for app pid: %d with ret %d",
2512 ua_event
->attr
.name
, app
->pid
, ret
);
2515 * This is normal behavior, an application can die during the
2516 * creation process. Don't report an error so the execution can
2517 * continue normally.
2520 DBG3("UST app counter create event failed. Application is dead.");
2525 ua_event
->handle
= ua_event
->obj
->handle
;
2527 DBG2("UST app map event %s created successfully for pid:%d object: %p",
2528 ua_event
->attr
.name
, app
->pid
, ua_event
->obj
);
2530 health_code_update();
2532 /* Set filter if one is present. */
2533 if (ua_event
->filter
) {
2534 ret
= set_ust_object_filter(app
, ua_event
->filter
, ua_event
->obj
);
2540 /* Set exclusions for the event */
2541 if (ua_event
->exclusion
) {
2542 ret
= set_ust_object_exclusions(app
, ua_event
->exclusion
, ua_event
->obj
);
2548 /* If event not enabled, disable it on the tracer */
2549 if (ua_event
->enabled
) {
2551 * We now need to explicitly enable the event, since it
2552 * is now disabled at creation.
2554 ret
= enable_ust_object(app
, ua_event
->obj
);
2557 * If we hit an EPERM, something is wrong with our enable call. If
2558 * we get an EEXIST, there is a problem on the tracer side since we
2562 case -LTTNG_UST_ERR_PERM
:
2563 /* Code flow problem */
2565 case -LTTNG_UST_ERR_EXIST
:
2566 /* It's OK for our use case. */
2577 health_code_update();
2581 static int init_ust_event_from_event_rule(
2582 const struct lttng_event_rule
*rule
,
2583 struct lttng_ust_event
*event
)
2585 enum lttng_event_rule_status status
;
2586 enum lttng_ust_loglevel_type ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
2587 int loglevel
= -1, ret
= 0;
2588 const char *pattern
;
2590 /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported. */
2591 assert(lttng_event_rule_get_type(rule
) ==
2592 LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
2594 if (lttng_event_rule_targets_agent_domain(rule
)) {
2596 * Special event for agents
2597 * The actual meat of the event is in the filter that will be
2598 * attached later on.
2599 * Set the default values for the agent event.
2601 pattern
= event_get_default_agent_ust_name(
2602 lttng_event_rule_get_domain_type(rule
));
2604 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
2606 const struct lttng_log_level_rule
*log_level_rule
;
2608 status
= lttng_event_rule_tracepoint_get_pattern(rule
, &pattern
);
2609 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
2610 /* At this point, this is a fatal error. */
2614 status
= lttng_event_rule_tracepoint_get_log_level_rule(
2615 rule
, &log_level_rule
);
2616 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
2617 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_ALL
;
2618 } else if (status
== LTTNG_EVENT_RULE_STATUS_OK
) {
2619 enum lttng_log_level_rule_status llr_status
;
2621 switch (lttng_log_level_rule_get_type(log_level_rule
)) {
2622 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY
:
2623 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_SINGLE
;
2624 llr_status
= lttng_log_level_rule_exactly_get_level(log_level_rule
, &loglevel
);
2626 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS
:
2627 ust_loglevel_type
= LTTNG_UST_LOGLEVEL_RANGE
;
2628 llr_status
= lttng_log_level_rule_at_least_as_severe_as_get_level(log_level_rule
, &loglevel
);
2634 assert(llr_status
== LTTNG_LOG_LEVEL_RULE_STATUS_OK
);
2636 /* At this point this is a fatal error */
2641 event
->instrumentation
= LTTNG_UST_TRACEPOINT
;
2642 ret
= lttng_strncpy(event
->name
, pattern
,
2643 LTTNG_UST_SYM_NAME_LEN
- 1);
2645 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ",
2650 event
->loglevel_type
= ust_loglevel_type
;
2651 event
->loglevel
= loglevel
;
2657 * Create the specified event notifier against the user space tracer of a
2658 * given application.
2660 static int create_ust_event_notifier(struct ust_app
*app
,
2661 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
)
2664 struct lttng_ust_event_notifier event_notifier
= {0};
2665 const struct lttng_condition
*condition
= NULL
;
2666 const struct lttng_event_rule
*event_rule
= NULL
;
2667 unsigned int capture_bytecode_count
= 0, i
;
2668 enum lttng_condition_status cond_status
;
2670 health_code_update();
2671 assert(app
->event_notifier_group
.object
);
2673 condition
= lttng_trigger_get_const_condition(
2674 ua_event_notifier_rule
->trigger
);
2676 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_ON_EVENT
);
2678 lttng_condition_on_event_get_rule(condition
, &event_rule
);
2680 assert(lttng_event_rule_get_type(event_rule
) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT
);
2682 init_ust_event_from_event_rule(event_rule
, &event_notifier
.event
);
2683 event_notifier
.event
.token
= ua_event_notifier_rule
->token
;
2684 event_notifier
.error_counter_index
= ua_event_notifier_rule
->error_counter_index
;
2686 /* Create UST event notifier against the tracer. */
2687 pthread_mutex_lock(&app
->sock_lock
);
2688 ret
= ustctl_create_event_notifier(app
->sock
, &event_notifier
,
2689 app
->event_notifier_group
.object
,
2690 &ua_event_notifier_rule
->obj
);
2691 pthread_mutex_unlock(&app
->sock_lock
);
2693 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
2694 ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d",
2695 event_notifier
.event
.name
, app
->name
,
2699 * This is normal behavior, an application can die
2700 * during the creation process. Don't report an error so
2701 * the execution can continue normally.
2704 DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)",
2705 app
->name
, app
->ppid
);
2711 ua_event_notifier_rule
->handle
= ua_event_notifier_rule
->obj
->handle
;
2713 DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p",
2714 event_notifier
.event
.name
, app
->name
, app
->ppid
,
2715 ua_event_notifier_rule
->obj
);
2717 health_code_update();
2719 /* Set filter if one is present. */
2720 if (ua_event_notifier_rule
->filter
) {
2721 ret
= set_ust_object_filter(app
, ua_event_notifier_rule
->filter
,
2722 ua_event_notifier_rule
->obj
);
2728 /* Set exclusions for the event. */
2729 if (ua_event_notifier_rule
->exclusion
) {
2730 ret
= set_ust_object_exclusions(app
,
2731 ua_event_notifier_rule
->exclusion
,
2732 ua_event_notifier_rule
->obj
);
2738 /* Set the capture bytecodes. */
2739 cond_status
= lttng_condition_on_event_get_capture_descriptor_count(
2740 condition
, &capture_bytecode_count
);
2741 assert(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2743 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2744 const struct lttng_bytecode
*capture_bytecode
=
2745 lttng_condition_on_event_get_capture_bytecode_at_index(
2748 ret
= set_ust_capture(app
, capture_bytecode
, i
,
2749 ua_event_notifier_rule
->obj
);
2756 * We now need to explicitly enable the event, since it
2757 * is disabled at creation.
2759 ret
= enable_ust_object(app
, ua_event_notifier_rule
->obj
);
2762 * If we hit an EPERM, something is wrong with our enable call.
2763 * If we get an EEXIST, there is a problem on the tracer side
2764 * since we just created it.
2767 case -LTTNG_UST_ERR_PERM
:
2768 /* Code flow problem. */
2770 case -LTTNG_UST_ERR_EXIST
:
2771 /* It's OK for our use case. */
2781 ua_event_notifier_rule
->enabled
= true;
2784 health_code_update();
2789 * Copy data between an UST app event and a LTT event.
2791 static void shadow_copy_event(struct ust_app_event
*ua_event
,
2792 struct ltt_ust_event
*uevent
)
2794 size_t exclusion_alloc_size
;
2796 strncpy(ua_event
->name
, uevent
->attr
.name
, sizeof(ua_event
->name
));
2797 ua_event
->name
[sizeof(ua_event
->name
) - 1] = '\0';
2799 ua_event
->enabled
= uevent
->enabled
;
2801 /* Copy event attributes */
2802 memcpy(&ua_event
->attr
, &uevent
->attr
, sizeof(ua_event
->attr
));
2804 /* Copy filter bytecode */
2805 if (uevent
->filter
) {
2806 ua_event
->filter
= lttng_bytecode_copy(uevent
->filter
);
2807 /* Filter might be NULL here in case of ENONEM. */
2810 /* Copy exclusion data */
2811 if (uevent
->exclusion
) {
2812 exclusion_alloc_size
= sizeof(struct lttng_event_exclusion
) +
2813 LTTNG_UST_SYM_NAME_LEN
* uevent
->exclusion
->count
;
2814 ua_event
->exclusion
= zmalloc(exclusion_alloc_size
);
2815 if (ua_event
->exclusion
== NULL
) {
2818 memcpy(ua_event
->exclusion
, uevent
->exclusion
,
2819 exclusion_alloc_size
);
2825 * Copy data between an UST app channel and a LTT channel.
2827 static void shadow_copy_channel(struct ust_app_channel
*ua_chan
,
2828 struct ltt_ust_channel
*uchan
)
2830 DBG2("UST app shadow copy of channel %s started", ua_chan
->name
);
2832 strncpy(ua_chan
->name
, uchan
->name
, sizeof(ua_chan
->name
));
2833 ua_chan
->name
[sizeof(ua_chan
->name
) - 1] = '\0';
2835 ua_chan
->tracefile_size
= uchan
->tracefile_size
;
2836 ua_chan
->tracefile_count
= uchan
->tracefile_count
;
2838 /* Copy event attributes since the layout is different. */
2839 ua_chan
->attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2840 ua_chan
->attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2841 ua_chan
->attr
.overwrite
= uchan
->attr
.overwrite
;
2842 ua_chan
->attr
.switch_timer_interval
= uchan
->attr
.switch_timer_interval
;
2843 ua_chan
->attr
.read_timer_interval
= uchan
->attr
.read_timer_interval
;
2844 ua_chan
->monitor_timer_interval
= uchan
->monitor_timer_interval
;
2845 ua_chan
->attr
.output
= uchan
->attr
.output
;
2846 ua_chan
->attr
.blocking_timeout
= uchan
->attr
.u
.s
.blocking_timeout
;
2849 * Note that the attribute channel type is not set since the channel on the
2850 * tracing registry side does not have this information.
2853 ua_chan
->enabled
= uchan
->enabled
;
2854 ua_chan
->tracing_channel_id
= uchan
->id
;
2856 DBG3("UST app shadow copy of channel %s done", ua_chan
->name
);
2860 * Copy data between a UST app session and a regular LTT session.
2862 static void shadow_copy_session(struct ust_app_session
*ua_sess
,
2863 struct ltt_ust_session
*usess
, struct ust_app
*app
)
2865 struct tm
*timeinfo
;
2868 char tmp_shm_path
[PATH_MAX
];
2870 timeinfo
= localtime(&app
->registration_time
);
2871 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
2873 DBG2("Shadow copy of session handle %d", ua_sess
->handle
);
2875 ua_sess
->tracing_id
= usess
->id
;
2876 ua_sess
->id
= get_next_session_id();
2877 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.uid
, app
->uid
);
2878 LTTNG_OPTIONAL_SET(&ua_sess
->real_credentials
.gid
, app
->gid
);
2879 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.uid
, usess
->uid
);
2880 LTTNG_OPTIONAL_SET(&ua_sess
->effective_credentials
.gid
, usess
->gid
);
2881 ua_sess
->buffer_type
= usess
->buffer_type
;
2882 ua_sess
->bits_per_long
= app
->bits_per_long
;
2884 /* There is only one consumer object per session possible. */
2885 consumer_output_get(usess
->consumer
);
2886 ua_sess
->consumer
= usess
->consumer
;
2888 ua_sess
->output_traces
= usess
->output_traces
;
2889 ua_sess
->live_timer_interval
= usess
->live_timer_interval
;
2890 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
2891 &usess
->metadata_attr
);
2893 switch (ua_sess
->buffer_type
) {
2894 case LTTNG_BUFFER_PER_PID
:
2895 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2896 DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s", app
->name
, app
->pid
,
2899 case LTTNG_BUFFER_PER_UID
:
2900 ret
= snprintf(ua_sess
->path
, sizeof(ua_sess
->path
),
2901 DEFAULT_UST_TRACE_UID_PATH
,
2902 lttng_credentials_get_uid(&ua_sess
->real_credentials
),
2903 app
->bits_per_long
);
2910 PERROR("asprintf UST shadow copy session");
2915 strncpy(ua_sess
->root_shm_path
, usess
->root_shm_path
,
2916 sizeof(ua_sess
->root_shm_path
));
2917 ua_sess
->root_shm_path
[sizeof(ua_sess
->root_shm_path
) - 1] = '\0';
2918 strncpy(ua_sess
->shm_path
, usess
->shm_path
,
2919 sizeof(ua_sess
->shm_path
));
2920 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2921 if (ua_sess
->shm_path
[0]) {
2922 switch (ua_sess
->buffer_type
) {
2923 case LTTNG_BUFFER_PER_PID
:
2924 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2925 "/" DEFAULT_UST_TRACE_PID_PATH
"/%s-%d-%s",
2926 app
->name
, app
->pid
, datetime
);
2928 case LTTNG_BUFFER_PER_UID
:
2929 ret
= snprintf(tmp_shm_path
, sizeof(tmp_shm_path
),
2930 "/" DEFAULT_UST_TRACE_UID_PATH
,
2931 app
->uid
, app
->bits_per_long
);
2938 PERROR("sprintf UST shadow copy session");
2942 strncat(ua_sess
->shm_path
, tmp_shm_path
,
2943 sizeof(ua_sess
->shm_path
) - strlen(ua_sess
->shm_path
) - 1);
2944 ua_sess
->shm_path
[sizeof(ua_sess
->shm_path
) - 1] = '\0';
2949 consumer_output_put(ua_sess
->consumer
);
2953 * Lookup sesison wrapper.
2956 void __lookup_session_by_app(const struct ltt_ust_session
*usess
,
2957 struct ust_app
*app
, struct lttng_ht_iter
*iter
)
2959 /* Get right UST app session from app */
2960 lttng_ht_lookup(app
->sessions
, &usess
->id
, iter
);
2964 * Return ust app session from the app session hashtable using the UST session
2967 static struct ust_app_session
*lookup_session_by_app(
2968 const struct ltt_ust_session
*usess
, struct ust_app
*app
)
2970 struct lttng_ht_iter iter
;
2971 struct lttng_ht_node_u64
*node
;
2973 __lookup_session_by_app(usess
, app
, &iter
);
2974 node
= lttng_ht_iter_get_node_u64(&iter
);
2979 return caa_container_of(node
, struct ust_app_session
, node
);
2986 * Setup buffer registry per PID for the given session and application. If none
2987 * is found, a new one is created, added to the global registry and
2988 * initialized. If regp is valid, it's set with the newly created object.
2990 * Return 0 on success or else a negative value.
2992 static int setup_buffer_reg_pid(struct ust_app_session
*ua_sess
,
2993 struct ust_app
*app
, struct buffer_reg_pid
**regp
)
2996 struct buffer_reg_pid
*reg_pid
;
3003 reg_pid
= buffer_reg_pid_find(ua_sess
->id
);
3006 * This is the create channel path meaning that if there is NO
3007 * registry available, we have to create one for this session.
3009 ret
= buffer_reg_pid_create(ua_sess
->id
, ®_pid
,
3010 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
3018 /* Initialize registry. */
3019 ret
= ust_registry_session_init(®_pid
->registry
->reg
.ust
, app
,
3020 app
->bits_per_long
, app
->uint8_t_alignment
,
3021 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
3022 app
->uint64_t_alignment
, app
->long_alignment
,
3023 app
->byte_order
, app
->version
.major
, app
->version
.minor
,
3024 reg_pid
->root_shm_path
, reg_pid
->shm_path
,
3025 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
3026 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
3027 ua_sess
->tracing_id
,
3031 * reg_pid->registry->reg.ust is NULL upon error, so we need to
3032 * destroy the buffer registry, because it is always expected
3033 * that if the buffer registry can be found, its ust registry is
3036 buffer_reg_pid_destroy(reg_pid
);
3040 buffer_reg_pid_add(reg_pid
);
3042 DBG3("UST app buffer registry per PID created successfully");
3054 * Setup buffer registry per UID for the given session and application. If none
3055 * is found, a new one is created, added to the global registry and
3056 * initialized. If regp is valid, it's set with the newly created object.
3058 * Return 0 on success or else a negative value.
3060 static int setup_buffer_reg_uid(struct ltt_ust_session
*usess
,
3061 struct ust_app_session
*ua_sess
,
3062 struct ust_app
*app
, struct buffer_reg_uid
**regp
)
3065 struct buffer_reg_uid
*reg_uid
;
3072 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
3075 * This is the create channel path meaning that if there is NO
3076 * registry available, we have to create one for this session.
3078 ret
= buffer_reg_uid_create(usess
->id
, app
->bits_per_long
, app
->uid
,
3079 LTTNG_DOMAIN_UST
, ®_uid
,
3080 ua_sess
->root_shm_path
, ua_sess
->shm_path
);
3088 /* Initialize registry. */
3089 ret
= ust_registry_session_init(®_uid
->registry
->reg
.ust
, NULL
,
3090 app
->bits_per_long
, app
->uint8_t_alignment
,
3091 app
->uint16_t_alignment
, app
->uint32_t_alignment
,
3092 app
->uint64_t_alignment
, app
->long_alignment
,
3093 app
->byte_order
, app
->version
.major
,
3094 app
->version
.minor
, reg_uid
->root_shm_path
,
3095 reg_uid
->shm_path
, usess
->uid
, usess
->gid
,
3096 ua_sess
->tracing_id
, app
->uid
);
3099 * reg_uid->registry->reg.ust is NULL upon error, so we need to
3100 * destroy the buffer registry, because it is always expected
3101 * that if the buffer registry can be found, its ust registry is
3104 buffer_reg_uid_destroy(reg_uid
, NULL
);
3107 /* Add node to teardown list of the session. */
3108 cds_list_add(®_uid
->lnode
, &usess
->buffer_reg_uid_list
);
3110 buffer_reg_uid_add(reg_uid
);
3112 DBG3("UST app buffer registry per UID created successfully");
3123 * Create a session on the tracer side for the given app.
3125 * On success, ua_sess_ptr is populated with the session pointer or else left
3126 * untouched. If the session was created, is_created is set to 1. On error,
3127 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
3130 * Returns 0 on success or else a negative code which is either -ENOMEM or
3131 * -ENOTCONN which is the default code if the ustctl_create_session fails.
3133 static int find_or_create_ust_app_session(struct ltt_ust_session
*usess
,
3134 struct ust_app
*app
, struct ust_app_session
**ua_sess_ptr
,
3137 int ret
, created
= 0;
3138 struct ust_app_session
*ua_sess
;
3142 assert(ua_sess_ptr
);
3144 health_code_update();
3146 ua_sess
= lookup_session_by_app(usess
, app
);
3147 if (ua_sess
== NULL
) {
3148 DBG2("UST app pid: %d session id %" PRIu64
" not found, creating it",
3149 app
->pid
, usess
->id
);
3150 ua_sess
= alloc_ust_app_session();
3151 if (ua_sess
== NULL
) {
3152 /* Only malloc can failed so something is really wrong */
3156 shadow_copy_session(ua_sess
, usess
, app
);
3160 switch (usess
->buffer_type
) {
3161 case LTTNG_BUFFER_PER_PID
:
3162 /* Init local registry. */
3163 ret
= setup_buffer_reg_pid(ua_sess
, app
, NULL
);
3165 delete_ust_app_session(-1, ua_sess
, app
);
3169 case LTTNG_BUFFER_PER_UID
:
3170 /* Look for a global registry. If none exists, create one. */
3171 ret
= setup_buffer_reg_uid(usess
, ua_sess
, app
, NULL
);
3173 delete_ust_app_session(-1, ua_sess
, app
);
3183 health_code_update();
3185 if (ua_sess
->handle
== -1) {
3186 pthread_mutex_lock(&app
->sock_lock
);
3187 ret
= ustctl_create_session(app
->sock
);
3188 pthread_mutex_unlock(&app
->sock_lock
);
3190 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
3191 ERR("Creating session for app pid %d with ret %d",
3194 DBG("UST app creating session failed. Application is dead");
3196 * This is normal behavior, an application can die during the
3197 * creation process. Don't report an error so the execution can
3198 * continue normally. This will get flagged ENOTCONN and the
3199 * caller will handle it.
3203 delete_ust_app_session(-1, ua_sess
, app
);
3204 if (ret
!= -ENOMEM
) {
3206 * Tracer is probably gone or got an internal error so let's
3207 * behave like it will soon unregister or not usable.
3214 ua_sess
->handle
= ret
;
3216 /* Add ust app session to app's HT */
3217 lttng_ht_node_init_u64(&ua_sess
->node
,
3218 ua_sess
->tracing_id
);
3219 lttng_ht_add_unique_u64(app
->sessions
, &ua_sess
->node
);
3220 lttng_ht_node_init_ulong(&ua_sess
->ust_objd_node
, ua_sess
->handle
);
3221 lttng_ht_add_unique_ulong(app
->ust_sessions_objd
,
3222 &ua_sess
->ust_objd_node
);
3224 DBG2("UST app session created successfully with handle %d", ret
);
3227 *ua_sess_ptr
= ua_sess
;
3229 *is_created
= created
;
3232 /* Everything went well. */
3236 health_code_update();
3241 * Match function for a hash table lookup of ust_app_ctx.
3243 * It matches an ust app context based on the context type and, in the case
3244 * of perf counters, their name.
3246 static int ht_match_ust_app_ctx(struct cds_lfht_node
*node
, const void *_key
)
3248 struct ust_app_ctx
*ctx
;
3249 const struct lttng_ust_context_attr
*key
;
3254 ctx
= caa_container_of(node
, struct ust_app_ctx
, node
.node
);
3258 if (ctx
->ctx
.ctx
!= key
->ctx
) {
3263 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER
:
3264 if (strncmp(key
->u
.perf_counter
.name
,
3265 ctx
->ctx
.u
.perf_counter
.name
,
3266 sizeof(key
->u
.perf_counter
.name
))) {
3270 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
3271 if (strcmp(key
->u
.app_ctx
.provider_name
,
3272 ctx
->ctx
.u
.app_ctx
.provider_name
) ||
3273 strcmp(key
->u
.app_ctx
.ctx_name
,
3274 ctx
->ctx
.u
.app_ctx
.ctx_name
)) {
3290 * Lookup for an ust app context from an lttng_ust_context.
3292 * Must be called while holding RCU read side lock.
3293 * Return an ust_app_ctx object or NULL on error.
3296 struct ust_app_ctx
*find_ust_app_context(struct lttng_ht
*ht
,
3297 struct lttng_ust_context_attr
*uctx
)
3299 struct lttng_ht_iter iter
;
3300 struct lttng_ht_node_ulong
*node
;
3301 struct ust_app_ctx
*app_ctx
= NULL
;
3306 /* Lookup using the lttng_ust_context_type and a custom match fct. */
3307 cds_lfht_lookup(ht
->ht
, ht
->hash_fct((void *) uctx
->ctx
, lttng_ht_seed
),
3308 ht_match_ust_app_ctx
, uctx
, &iter
.iter
);
3309 node
= lttng_ht_iter_get_node_ulong(&iter
);
3314 app_ctx
= caa_container_of(node
, struct ust_app_ctx
, node
);
3321 * Create a context for the channel on the tracer.
3323 * Called with UST app session lock held and a RCU read side lock.
3326 int create_ust_app_channel_context(struct ust_app_channel
*ua_chan
,
3327 struct lttng_ust_context_attr
*uctx
,
3328 struct ust_app
*app
)
3331 struct ust_app_ctx
*ua_ctx
;
3333 DBG2("UST app adding context to channel %s", ua_chan
->name
);
3335 ua_ctx
= find_ust_app_context(ua_chan
->ctx
, uctx
);
3341 ua_ctx
= alloc_ust_app_ctx(uctx
);
3342 if (ua_ctx
== NULL
) {
3348 lttng_ht_node_init_ulong(&ua_ctx
->node
, (unsigned long) ua_ctx
->ctx
.ctx
);
3349 lttng_ht_add_ulong(ua_chan
->ctx
, &ua_ctx
->node
);
3350 cds_list_add_tail(&ua_ctx
->list
, &ua_chan
->ctx_list
);
3352 ret
= create_ust_channel_context(ua_chan
, ua_ctx
, app
);
3362 * Enable on the tracer side a ust app event for the session and channel.
3364 * Called with UST app session lock held.
3367 int enable_ust_app_event(struct ust_app_session
*ua_sess
,
3368 struct ust_app_event
*ua_event
, struct ust_app
*app
)
3372 ret
= enable_ust_object(app
, ua_event
->obj
);
3377 ua_event
->enabled
= 1;
3384 * Disable on the tracer side a ust app event for the session and channel.
3386 static int disable_ust_app_event(struct ust_app_session
*ua_sess
,
3387 struct ust_app_event
*ua_event
, struct ust_app
*app
)
3391 ret
= disable_ust_object(app
, ua_event
->obj
);
3396 ua_event
->enabled
= 0;
3403 * Lookup ust app channel for session and disable it on the tracer side.
3406 int disable_ust_app_channel(struct ust_app_session
*ua_sess
,
3407 struct ust_app_channel
*ua_chan
, struct ust_app
*app
)
3411 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
3416 ua_chan
->enabled
= 0;
3423 * Lookup ust app map for session and disable it on the tracer side.
3426 int disable_ust_app_map(struct ust_app_session
*ua_sess
,
3427 struct ust_app_map
*ua_map
, struct ust_app
*app
)
3431 ret
= disable_ust_map(app
, ua_sess
, ua_map
);
3436 ua_map
->enabled
= 0;
3443 * Lookup ust app channel for session and enable it on the tracer side. This
3444 * MUST be called with a RCU read side lock acquired.
3446 static int enable_ust_app_channel(struct ust_app_session
*ua_sess
,
3447 struct ltt_ust_channel
*uchan
, struct ust_app
*app
)
3450 struct lttng_ht_iter iter
;
3451 struct lttng_ht_node_str
*ua_chan_node
;
3452 struct ust_app_channel
*ua_chan
;
3454 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
3455 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
3456 if (ua_chan_node
== NULL
) {
3457 DBG2("Unable to find channel %s in ust session id %" PRIu64
,
3458 uchan
->name
, ua_sess
->tracing_id
);
3462 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
3464 ret
= enable_ust_channel(app
, ua_sess
, ua_chan
);
3474 * Lookup ust app map for session and enable it on the tracer side. This
3475 * MUST be called with a RCU read side lock acquired.
3477 static int enable_ust_app_map(struct ust_app_session
*ua_sess
,
3478 struct ltt_ust_map
*umap
, struct ust_app
*app
)
3481 struct lttng_ht_iter iter
;
3482 struct lttng_ht_node_str
*ua_map_node
;
3483 struct ust_app_map
*ua_map
;
3485 lttng_ht_lookup(ua_sess
->maps
, (void *)umap
->name
, &iter
);
3486 ua_map_node
= lttng_ht_iter_get_node_str(&iter
);
3487 if (ua_map_node
== NULL
) {
3488 DBG2("Unable to find map %s in ust session id %" PRIu64
,
3489 umap
->name
, ua_sess
->tracing_id
);
3493 ua_map
= caa_container_of(ua_map_node
, struct ust_app_map
, node
);
3495 ret
= enable_ust_map(app
, ua_sess
, ua_map
);
3505 * Ask the consumer to create a channel and get it if successful.
3507 * Called with UST app session lock held.
3509 * Return 0 on success or else a negative value.
3511 static int do_consumer_create_channel(struct ltt_ust_session
*usess
,
3512 struct ust_app_session
*ua_sess
, struct ust_app_channel
*ua_chan
,
3513 int bitness
, struct ust_registry_session
*registry
,
3514 uint64_t trace_archive_id
)
3517 unsigned int nb_fd
= 0;
3518 struct consumer_socket
*socket
;
3526 health_code_update();
3528 /* Get the right consumer socket for the application. */
3529 socket
= consumer_find_socket_by_bitness(bitness
, usess
->consumer
);
3535 health_code_update();
3537 /* Need one fd for the channel. */
3538 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3540 ERR("Exhausted number of available FD upon create channel");
3545 * Ask consumer to create channel. The consumer will return the number of
3546 * stream we have to expect.
3548 ret
= ust_consumer_ask_channel(ua_sess
, ua_chan
, usess
->consumer
, socket
,
3549 registry
, usess
->current_trace_chunk
);
3555 * Compute the number of fd needed before receiving them. It must be 2 per
3556 * stream (2 being the default value here).
3558 nb_fd
= DEFAULT_UST_STREAM_FD_NUM
* ua_chan
->expected_stream_count
;
3560 /* Reserve the amount of file descriptor we need. */
3561 ret
= lttng_fd_get(LTTNG_FD_APPS
, nb_fd
);
3563 ERR("Exhausted number of available FD upon create channel");
3564 goto error_fd_get_stream
;
3567 health_code_update();
3570 * Now get the channel from the consumer. This call wil populate the stream
3571 * list of that channel and set the ust objects.
3573 if (usess
->consumer
->enabled
) {
3574 ret
= ust_consumer_get_channel(socket
, ua_chan
);
3584 lttng_fd_put(LTTNG_FD_APPS
, nb_fd
);
3585 error_fd_get_stream
:
3587 * Initiate a destroy channel on the consumer since we had an error
3588 * handling it on our side. The return value is of no importance since we
3589 * already have a ret value set by the previous error that we need to
3592 (void) ust_consumer_destroy_channel(socket
, ua_chan
);
3594 lttng_fd_put(LTTNG_FD_APPS
, 1);
3596 health_code_update();
3601 static int create_map_object(struct ltt_ust_session
*usess
,
3602 struct ust_app_session
*ua_sess
, struct ust_app_map
*ua_map
)
3604 int i
, ret
, nr_counter_cpu
;
3605 struct ustctl_counter_dimension dimension
[1] = {0};
3606 struct ustctl_daemon_counter
*daemon_counter
;
3607 struct lttng_ust_object_data
**counter_cpus
;
3608 enum ustctl_counter_bitness bitness
;
3609 int *counter_cpu_fds
;
3614 assert(ua_map
->bucket_count
> 0);
3616 DBG("Creating UST map \"%s\"", ua_map
->name
);
3618 if (ua_map
->bitness
== LTTNG_MAP_BITNESS_32BITS
) {
3619 bitness
= USTCTL_COUNTER_BITNESS_32
;
3621 bitness
= USTCTL_COUNTER_BITNESS_64
;
3624 nr_counter_cpu
= ustctl_get_nr_cpu_per_counter();
3625 counter_cpu_fds
= zmalloc(nr_counter_cpu
* sizeof(*counter_cpu_fds
));
3626 if (!counter_cpu_fds
) {
3631 counter_cpus
= zmalloc(nr_counter_cpu
* sizeof(**counter_cpus
));
3632 if (!counter_cpus
) {
3637 /* Need one fd for each cpu counter of the map. */
3638 ret
= lttng_fd_get(LTTNG_FD_APPS
, nr_counter_cpu
);
3640 ERR("Exhausted number of available FD upon create map");
3641 goto free_cpu_counters
;
3644 for (i
= 0; i
< nr_counter_cpu
; i
++) {
3645 counter_cpu_fds
[i
] = shm_create_anonymous("ust-map-counter");
3646 if (counter_cpu_fds
[i
] < 0) {
3647 ERR("Error creating anonymous shared memory object");
3653 dimension
[0].size
= ua_map
->bucket_count
;
3654 dimension
[0].has_underflow
= false;
3655 dimension
[0].has_overflow
= false;
3657 daemon_counter
= ustctl_create_counter(1, dimension
, 0, -1,
3658 nr_counter_cpu
, counter_cpu_fds
,
3660 USTCTL_COUNTER_ARITHMETIC_MODULAR
,
3661 USTCTL_COUNTER_ALLOC_PER_CPU
,
3662 ua_map
->coalesce_hits
);
3663 assert(daemon_counter
);
3665 DBG("Created daemon counter succesfully");
3667 ua_map
->map_handle
= daemon_counter
;
3669 ret
= ustctl_create_counter_data(daemon_counter
, &ua_map
->obj
);
3671 DBG("Created counter data succesfully");
3673 for (i
= 0; i
< nr_counter_cpu
; i
++) {
3674 struct ust_app_map_counter
*counter
;
3676 /* Create UST counter */
3677 counter
= ust_app_alloc_map_counter();
3678 if (counter
== NULL
) {
3680 goto release_counters
;
3683 ret
= ustctl_create_counter_cpu_data(daemon_counter
, i
,
3686 ERR("Creating map counter cpu data");
3691 cds_list_add_tail(&counter
->list
, &ua_map
->counters
.head
);
3692 ua_map
->counters
.count
++;
3694 DBG2("UST app map counter %d created successfully",
3695 ua_map
->counters
.count
);
3708 free(counter_cpu_fds
);
3714 * Duplicate the ust data object of the ust app stream and save it in the
3715 * buffer registry stream.
3717 * Return 0 on success or else a negative value.
3719 static int duplicate_stream_object(struct buffer_reg_stream
*reg_stream
,
3720 struct ust_app_stream
*stream
)
3727 /* Duplicating a stream requires 2 new fds. Reserve them. */
3728 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3730 ERR("Exhausted number of available FD upon duplicate stream");
3734 /* Duplicate object for stream once the original is in the registry. */
3735 ret
= ustctl_duplicate_ust_object_data(&stream
->obj
,
3736 reg_stream
->obj
.ust
);
3738 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3739 reg_stream
->obj
.ust
, stream
->obj
, ret
);
3740 lttng_fd_put(LTTNG_FD_APPS
, 2);
3743 stream
->handle
= stream
->obj
->handle
;
3750 * Duplicate the ust data object of the ust app map_counter and save it in the
3751 * buffer registry map_counter.
3753 * Return 0 on success or else a negative value.
3755 static int duplicate_map_counter_object(struct buffer_reg_map_counter
*reg_map_counter
,
3756 struct ust_app_map_counter
*map_counter
)
3760 assert(reg_map_counter
);
3761 assert(map_counter
);
3763 /* Duplicating a map_counter requires 2 new fds. Reserve them. */
3764 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
3766 ERR("Exhausted number of available FD upon duplicate map_counter");
3770 /* Duplicate object for map_counter once the original is in the registry. */
3771 ret
= ustctl_duplicate_ust_object_data(&map_counter
->obj
,
3772 reg_map_counter
->obj
.ust
);
3774 ERR("Duplicate map_counter obj from %p to %p failed with ret %d",
3775 reg_map_counter
->obj
.ust
, map_counter
->obj
, ret
);
3776 lttng_fd_put(LTTNG_FD_APPS
, 2);
3779 map_counter
->handle
= map_counter
->obj
->handle
;
3786 * Duplicate the ust data object of the ust app. channel and save it in the
3787 * buffer registry channel.
3789 * Return 0 on success or else a negative value.
3791 static int duplicate_channel_object(struct buffer_reg_channel
*buf_reg_chan
,
3792 struct ust_app_channel
*ua_chan
)
3796 assert(buf_reg_chan
);
3799 /* Duplicating a channel requires 1 new fd. Reserve it. */
3800 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3802 ERR("Exhausted number of available FD upon duplicate channel");
3806 /* Duplicate object for stream once the original is in the registry. */
3807 ret
= ustctl_duplicate_ust_object_data(&ua_chan
->obj
, buf_reg_chan
->obj
.ust
);
3809 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3810 buf_reg_chan
->obj
.ust
, ua_chan
->obj
, ret
);
3813 ua_chan
->handle
= ua_chan
->obj
->handle
;
3818 lttng_fd_put(LTTNG_FD_APPS
, 1);
3824 * Duplicate the ust data object of the ust app. map and save it in the
3825 * buffer registry map.
3827 * Return 0 on success or else a negative value.
3829 static int duplicate_map_object(struct buffer_reg_map
*buf_reg_map
,
3830 struct ust_app_map
*ua_map
)
3834 assert(buf_reg_map
);
3837 /* Duplicating a map requires 1 new fd. Reserve it. */
3838 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
3840 ERR("Exhausted number of available FD upon duplicate map");
3844 /* Duplicate object for stream once the original is in the registry. */
3845 ret
= ustctl_duplicate_ust_object_data(&ua_map
->obj
, buf_reg_map
->obj
.ust
);
3847 ERR("Duplicate map obj from %p to %p failed with ret: %d",
3848 buf_reg_map
->obj
.ust
, ua_map
->obj
, ret
);
3851 ua_map
->handle
= ua_map
->obj
->handle
;
3856 lttng_fd_put(LTTNG_FD_APPS
, 1);
3862 * For a given channel buffer registry, setup all streams of the given ust
3863 * application channel.
3865 * Return 0 on success or else a negative value.
3867 static int setup_buffer_reg_streams(struct buffer_reg_channel
*buf_reg_chan
,
3868 struct ust_app_channel
*ua_chan
,
3869 struct ust_app
*app
)
3872 struct ust_app_stream
*stream
, *stmp
;
3874 assert(buf_reg_chan
);
3877 DBG2("UST app setup buffer registry stream");
3879 /* Send all streams to application. */
3880 cds_list_for_each_entry_safe(stream
, stmp
, &ua_chan
->streams
.head
, list
) {
3881 struct buffer_reg_stream
*reg_stream
;
3883 ret
= buffer_reg_stream_create(®_stream
);
3889 * Keep original pointer and nullify it in the stream so the delete
3890 * stream call does not release the object.
3892 reg_stream
->obj
.ust
= stream
->obj
;
3894 buffer_reg_stream_add(reg_stream
, buf_reg_chan
);
3896 /* We don't need the streams anymore. */
3897 cds_list_del(&stream
->list
);
3898 delete_ust_app_stream(-1, stream
, app
);
3906 * For a given map buffer registry, setup all counters of the given ust
3909 * Return 0 on success or else a negative value.
3911 static int setup_buffer_reg_map_counters(struct buffer_reg_map
*buf_reg_map
,
3912 struct ust_app_map
*ua_map
,
3913 struct ust_app
*app
)
3916 struct ust_app_map_counter
*counter
, *stmp
;
3918 assert(buf_reg_map
);
3921 DBG2("UST app setup buffer registry counter");
3923 /* Send all counters to application. */
3924 cds_list_for_each_entry_safe(counter
, stmp
, &ua_map
->counters
.head
, list
) {
3925 struct buffer_reg_map_counter
*reg_counter
;
3927 ret
= buffer_reg_map_counter_create(®_counter
);
3933 * Keep original pointer and nullify it in the counter so the delete
3934 * counter call does not release the object.
3936 reg_counter
->obj
.ust
= counter
->obj
;
3937 counter
->obj
= NULL
;
3938 buffer_reg_map_counter_add(reg_counter
, buf_reg_map
);
3940 /* We don't need the counters anymore. */
3941 cds_list_del(&counter
->list
);
3942 delete_ust_app_map_counter(-1, counter
, app
);
3950 * Create a buffer registry channel for the given session registry and
3951 * application channel object. If regp pointer is valid, it's set with the
3952 * created object. Important, the created object is NOT added to the session
3953 * registry hash table.
3955 * Return 0 on success else a negative value.
3957 static int create_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
3958 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
**regp
)
3961 struct buffer_reg_channel
*buf_reg_chan
= NULL
;
3966 DBG2("UST app creating buffer registry channel for %s", ua_chan
->name
);
3968 /* Create buffer registry channel. */
3969 ret
= buffer_reg_channel_create(ua_chan
->tracing_channel_id
, &buf_reg_chan
);
3973 assert(buf_reg_chan
);
3974 buf_reg_chan
->consumer_key
= ua_chan
->key
;
3975 buf_reg_chan
->subbuf_size
= ua_chan
->attr
.subbuf_size
;
3976 buf_reg_chan
->num_subbuf
= ua_chan
->attr
.num_subbuf
;
3978 /* Create and add a channel registry to session. */
3979 ret
= ust_registry_channel_add(reg_sess
->reg
.ust
,
3980 ua_chan
->tracing_channel_id
);
3984 buffer_reg_channel_add(reg_sess
, buf_reg_chan
);
3987 *regp
= buf_reg_chan
;
3993 /* Safe because the registry channel object was not added to any HT. */
3994 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
4000 * Create a buffer registry map for the given session registry and
4001 * application map object. If regp pointer is valid, it's set with the
4002 * created object. Important, the created object is NOT added to the session
4003 * registry hash table.
4005 * Return 0 on success else a negative value.
4007 static int create_buffer_reg_map(struct buffer_reg_session
*reg_sess
,
4008 struct ust_app_map
*ua_map
, struct buffer_reg_map
**regp
)
4011 struct buffer_reg_map
*buf_reg_map
= NULL
;
4016 DBG2("UST app creating buffer registry map for %s", ua_map
->name
);
4018 /* Create buffer registry map. */
4019 ret
= buffer_reg_map_create(ua_map
->tracing_map_id
, &buf_reg_map
);
4023 assert(buf_reg_map
);
4025 /* Create and add a map registry to session. */
4026 ret
= ust_registry_map_add(reg_sess
->reg
.ust
, ua_map
->tracing_map_id
);
4030 buffer_reg_map_add(reg_sess
, buf_reg_map
);
4033 *regp
= buf_reg_map
;
4039 /* Safe because the registry map object was not added to any HT. */
4040 buffer_reg_map_destroy(buf_reg_map
, LTTNG_DOMAIN_UST
);
4046 * Setup buffer registry channel for the given session registry and application
4047 * channel object. If regp pointer is valid, it's set with the created object.
4049 * Return 0 on success else a negative value.
4051 static int setup_buffer_reg_channel(struct buffer_reg_session
*reg_sess
,
4052 struct ust_app_channel
*ua_chan
, struct buffer_reg_channel
*buf_reg_chan
,
4053 struct ust_app
*app
)
4058 assert(buf_reg_chan
);
4060 assert(ua_chan
->obj
);
4062 DBG2("UST app setup buffer registry channel for %s", ua_chan
->name
);
4064 /* Setup all streams for the registry. */
4065 ret
= setup_buffer_reg_streams(buf_reg_chan
, ua_chan
, app
);
4070 buf_reg_chan
->obj
.ust
= ua_chan
->obj
;
4071 ua_chan
->obj
= NULL
;
4076 buffer_reg_channel_remove(reg_sess
, buf_reg_chan
);
4077 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
4082 * Setup buffer registry map for the given session registry and application
4083 * map object. If regp pointer is valid, it's set with the created object.
4085 * Return 0 on success else a negative value.
4087 static int setup_buffer_reg_map(struct buffer_reg_session
*reg_sess
,
4088 struct ust_app_map
*ua_map
, struct buffer_reg_map
*buf_reg_map
,
4089 struct ust_app
*app
)
4094 assert(buf_reg_map
);
4096 assert(ua_map
->obj
);
4098 DBG2("UST app setup buffer registry map for %s", ua_map
->name
);
4100 /* Setup all counters for the registry. */
4101 ret
= setup_buffer_reg_map_counters(buf_reg_map
, ua_map
, app
);
4106 buf_reg_map
->obj
.ust
= ua_map
->obj
;
4108 buf_reg_map
->daemon_counter
= ua_map
->map_handle
;
4109 ua_map
->map_handle
= NULL
;
4114 buffer_reg_map_remove(reg_sess
, buf_reg_map
);
4115 buffer_reg_map_destroy(buf_reg_map
, LTTNG_DOMAIN_UST
);
4120 * Send buffer registry channel to the application.
4122 * Return 0 on success else a negative value.
4124 static int send_channel_uid_to_ust(struct buffer_reg_channel
*buf_reg_chan
,
4125 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
4126 struct ust_app_channel
*ua_chan
)
4129 struct buffer_reg_stream
*reg_stream
;
4131 assert(buf_reg_chan
);
4136 DBG("UST app sending buffer registry channel to ust sock %d", app
->sock
);
4138 ret
= duplicate_channel_object(buf_reg_chan
, ua_chan
);
4143 /* Send channel to the application. */
4144 ret
= ust_consumer_send_channel_to_ust(app
, ua_sess
, ua_chan
);
4145 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4146 ret
= -ENOTCONN
; /* Caused by app exiting. */
4148 } else if (ret
< 0) {
4152 health_code_update();
4154 /* Send all streams to application. */
4155 pthread_mutex_lock(&buf_reg_chan
->stream_list_lock
);
4156 cds_list_for_each_entry(reg_stream
, &buf_reg_chan
->streams
, lnode
) {
4157 struct ust_app_stream stream
;
4159 ret
= duplicate_stream_object(reg_stream
, &stream
);
4161 goto error_stream_unlock
;
4164 ret
= ust_consumer_send_stream_to_ust(app
, ua_chan
, &stream
);
4166 (void) release_ust_app_stream(-1, &stream
, app
);
4167 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4168 ret
= -ENOTCONN
; /* Caused by app exiting. */
4170 goto error_stream_unlock
;
4174 * The return value is not important here. This function will output an
4177 (void) release_ust_app_stream(-1, &stream
, app
);
4179 ua_chan
->is_sent
= 1;
4181 error_stream_unlock
:
4182 pthread_mutex_unlock(&buf_reg_chan
->stream_list_lock
);
4188 * Send buffer registry map to the application.
4190 * Return 0 on success else a negative value.
4192 static int send_map_uid_to_ust(struct buffer_reg_map
*buf_reg_map
,
4193 struct ust_app
*app
, struct ust_app_session
*ua_sess
,
4194 struct ust_app_map
*ua_map
)
4197 struct buffer_reg_map_counter
*reg_map_counter
;
4199 assert(buf_reg_map
);
4204 DBG("UST app sending buffer registry map to ust sock %d", app
->sock
);
4206 ret
= duplicate_map_object(buf_reg_map
, ua_map
);
4211 /* Send map to the application. */
4213 pthread_mutex_lock(&app
->sock_lock
);
4214 ret
= ustctl_send_counter_data_to_ust(app
->sock
,
4215 ua_sess
->handle
, ua_map
->obj
);
4216 pthread_mutex_unlock(&app
->sock_lock
);
4219 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4220 ret
= -ENOTCONN
; /* Caused by app exiting. */
4222 goto error_map_counter_unlock
;
4225 ua_map
->handle
= ua_map
->obj
->handle
;
4227 health_code_update();
4229 /* Send all map_counters to application. */
4230 pthread_mutex_lock(&buf_reg_map
->counter_list_lock
);
4231 cds_list_for_each_entry(reg_map_counter
, &buf_reg_map
->counters
, lnode
) {
4232 struct ust_app_map_counter map_counter
;
4234 ret
= duplicate_map_counter_object(reg_map_counter
, &map_counter
);
4236 goto error_map_counter_unlock
;
4239 pthread_mutex_lock(&app
->sock_lock
);
4240 // Do send the per cpu counter here
4241 ret
= ustctl_send_counter_cpu_data_to_ust(app
->sock
,
4242 ua_map
->obj
, map_counter
.obj
);
4243 pthread_mutex_unlock(&app
->sock_lock
);
4246 (void) release_ust_app_map_counter(-1, &map_counter
, app
);
4247 if (ret
== -EPIPE
|| ret
== -LTTNG_UST_ERR_EXITING
) {
4248 ret
= -ENOTCONN
; /* Caused by app exiting. */
4250 goto error_map_counter_unlock
;
4254 * The return value is not important here. This function will
4255 * output an error if needed.
4257 (void) release_ust_app_map_counter(-1, &map_counter
, app
);
4259 ua_map
->is_sent
= 1;
4261 error_map_counter_unlock
:
4262 pthread_mutex_unlock(&buf_reg_map
->counter_list_lock
);
4268 * Create and send to the application the created buffers with per UID buffers.
4270 * This MUST be called with a RCU read side lock acquired.
4271 * The session list lock and the session's lock must be acquired.
4273 * Return 0 on success else a negative value.
4275 static int create_channel_per_uid(struct ust_app
*app
,
4276 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
4277 struct ust_app_channel
*ua_chan
)
4280 struct buffer_reg_uid
*reg_uid
;
4281 struct buffer_reg_channel
*buf_reg_chan
;
4282 struct ltt_session
*session
= NULL
;
4283 enum lttng_error_code notification_ret
;
4284 struct ust_registry_channel
*ust_reg_chan
;
4291 DBG("UST app creating channel %s with per UID buffers", ua_chan
->name
);
4293 reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
4295 * The session creation handles the creation of this global registry
4296 * object. If none can be find, there is a code flow problem or a
4301 buf_reg_chan
= buffer_reg_channel_find(ua_chan
->tracing_channel_id
,
4307 /* Create the buffer registry channel object. */
4308 ret
= create_buffer_reg_channel(reg_uid
->registry
, ua_chan
, &buf_reg_chan
);
4310 ERR("Error creating the UST channel \"%s\" registry instance",
4315 session
= session_find_by_id(ua_sess
->tracing_id
);
4317 assert(pthread_mutex_trylock(&session
->lock
));
4318 assert(session_trylock_list());
4321 * Create the buffers on the consumer side. This call populates the
4322 * ust app channel object with all streams and data object.
4324 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
4325 app
->bits_per_long
, reg_uid
->registry
->reg
.ust
,
4326 session
->most_recent_chunk_id
.value
);
4328 ERR("Error creating UST channel \"%s\" on the consumer daemon",
4332 * Let's remove the previously created buffer registry channel so
4333 * it's not visible anymore in the session registry.
4335 ust_registry_channel_del_free(reg_uid
->registry
->reg
.ust
,
4336 ua_chan
->tracing_channel_id
, false);
4337 buffer_reg_channel_remove(reg_uid
->registry
, buf_reg_chan
);
4338 buffer_reg_channel_destroy(buf_reg_chan
, LTTNG_DOMAIN_UST
);
4343 * Setup the streams and add it to the session registry.
4345 ret
= setup_buffer_reg_channel(reg_uid
->registry
,
4346 ua_chan
, buf_reg_chan
, app
);
4348 ERR("Error setting up UST channel \"%s\"", ua_chan
->name
);
4352 /* Notify the notification subsystem of the channel's creation. */
4353 pthread_mutex_lock(®_uid
->registry
->reg
.ust
->lock
);
4354 ust_reg_chan
= ust_registry_channel_find(reg_uid
->registry
->reg
.ust
,
4355 ua_chan
->tracing_channel_id
);
4356 assert(ust_reg_chan
);
4357 ust_reg_chan
->consumer_key
= ua_chan
->key
;
4358 ust_reg_chan
= NULL
;
4359 pthread_mutex_unlock(®_uid
->registry
->reg
.ust
->lock
);
4361 notification_ret
= notification_thread_command_add_channel(
4362 notification_thread_handle
, session
->name
,
4363 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
4364 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
4366 ua_chan
->key
, LTTNG_DOMAIN_UST
,
4367 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
4368 if (notification_ret
!= LTTNG_OK
) {
4369 ret
= - (int) notification_ret
;
4370 ERR("Failed to add channel to notification thread");
4375 /* Send buffers to the application. */
4376 ret
= send_channel_uid_to_ust(buf_reg_chan
, app
, ua_sess
, ua_chan
);
4378 if (ret
!= -ENOTCONN
) {
4379 ERR("Error sending channel to application");
4386 session_put(session
);
4392 * Create and send to the application the created buffers with per UID buffers.
4394 * This MUST be called with a RCU read side lock acquired.
4395 * The session list lock and the session's lock must be acquired.
4397 * Return 0 on success else a negative value.
4399 static int create_map_per_uid(struct ust_app
*app
,
4400 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
4401 struct ust_app_map
*ua_map
)
4404 struct buffer_reg_uid
*buffer_reg_uid
;
4405 struct buffer_reg_map
*buffer_reg_map
;
4406 struct ltt_session
*session
= NULL
;
4407 struct ust_registry_map
*ust_reg_map
;
4414 DBG("UST app creating map %s with per UID buffers", ua_map
->name
);
4416 buffer_reg_uid
= buffer_reg_uid_find(usess
->id
, app
->bits_per_long
, app
->uid
);
4418 * The session creation handles the creation of this global registry
4419 * object. If none can be find, there is a code flow problem or a
4422 assert(buffer_reg_uid
);
4424 buffer_reg_map
= buffer_reg_map_find(ua_map
->tracing_map_id
,
4426 if (buffer_reg_map
) {
4430 /* Create the buffer registry map object. */
4431 ret
= create_buffer_reg_map(buffer_reg_uid
->registry
, ua_map
,
4434 ERR("Error creating the UST map \"%s\" registry instance",
4439 session
= session_find_by_id(ua_sess
->tracing_id
);
4441 assert(pthread_mutex_trylock(&session
->lock
));
4442 assert(session_trylock_list());
4446 ret
= create_map_object(usess
, ua_sess
, ua_map
);
4449 ERR("Error creating UST map object: map_name = \"%s\"", ua_map
->name
);
4454 * Setup the streams and add it to the session registry.
4456 ret
= setup_buffer_reg_map(buffer_reg_uid
->registry
, ua_map
,
4457 buffer_reg_map
, app
);
4459 ERR("Error setting up UST map \"%s\"", ua_map
->name
);
4463 /* Notify the notification subsystem of the map's creation. */
4464 pthread_mutex_lock(&buffer_reg_uid
->registry
->reg
.ust
->lock
);
4465 ust_reg_map
= ust_registry_map_find(buffer_reg_uid
->registry
->reg
.ust
,
4466 ua_map
->tracing_map_id
);
4467 assert(ust_reg_map
);
4469 pthread_mutex_unlock(&buffer_reg_uid
->registry
->reg
.ust
->lock
);
4472 /* Send buffers to the application. */
4473 ret
= send_map_uid_to_ust(buffer_reg_map
, app
, ua_sess
, ua_map
);
4475 if (ret
!= -ENOTCONN
) {
4476 ERR("Error sending map to application");
4483 session_put(session
);
4488 //static int destroy_map_per_uid(struct ust_app *app,
4489 // struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
4490 // struct ust_app_map *ua_map)
4493 // struct buffer_reg_uid *buffer_reg_uid;
4494 // struct buffer_reg_map *buffer_reg_map;
4501 // DBG("UST app destroy map %s with per UID buffers", ua_map->name);
4503 // buffer_reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
4505 // * The session creation handles the creation of this global registry
4506 // * object. If none can be find, there is a code flow problem or a
4509 // assert(buffer_reg_uid);
4511 // buffer_reg_map = buffer_reg_map_find(ua_map->tracing_map_id,
4513 // if (!buffer_reg_map) {
4514 // ERR("Can't find map in buffer registry: map-name = '%s', uid = %d",
4515 // ua_map->name, app->uid);
4520 // buffer_reg_map_destroy(buffer_reg_map, LTTNG_DOMAIN_UST);
4528 * Create and send to the application the created buffers with per PID buffers.
4530 * Called with UST app session lock held.
4531 * The session list lock and the session's lock must be acquired.
4533 * Return 0 on success else a negative value.
4535 static int create_channel_per_pid(struct ust_app
*app
,
4536 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
4537 struct ust_app_channel
*ua_chan
)
4540 struct ust_registry_session
*registry
;
4541 enum lttng_error_code cmd_ret
;
4542 struct ltt_session
*session
= NULL
;
4543 uint64_t chan_reg_key
;
4544 struct ust_registry_channel
*ust_reg_chan
;
4551 DBG("UST app creating channel %s with per PID buffers", ua_chan
->name
);
4555 registry
= get_session_registry(ua_sess
);
4556 /* The UST app session lock is held, registry shall not be null. */
4559 /* Create and add a new channel registry to session. */
4560 ret
= ust_registry_channel_add(registry
, ua_chan
->key
);
4562 ERR("Error creating the UST channel \"%s\" registry instance",
4567 session
= session_find_by_id(ua_sess
->tracing_id
);
4570 assert(pthread_mutex_trylock(&session
->lock
));
4571 assert(session_trylock_list());
4573 /* Create and get channel on the consumer side. */
4574 ret
= do_consumer_create_channel(usess
, ua_sess
, ua_chan
,
4575 app
->bits_per_long
, registry
,
4576 session
->most_recent_chunk_id
.value
);
4578 ERR("Error creating UST channel \"%s\" on the consumer daemon",
4580 goto error_remove_from_registry
;
4583 ret
= send_channel_pid_to_ust(app
, ua_sess
, ua_chan
);
4585 if (ret
!= -ENOTCONN
) {
4586 ERR("Error sending channel to application");
4588 goto error_remove_from_registry
;
4591 chan_reg_key
= ua_chan
->key
;
4592 pthread_mutex_lock(®istry
->lock
);
4593 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
4594 assert(ust_reg_chan
);
4595 ust_reg_chan
->consumer_key
= ua_chan
->key
;
4596 pthread_mutex_unlock(®istry
->lock
);
4598 cmd_ret
= notification_thread_command_add_channel(
4599 notification_thread_handle
, session
->name
,
4600 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
4601 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
4603 ua_chan
->key
, LTTNG_DOMAIN_UST
,
4604 ua_chan
->attr
.subbuf_size
* ua_chan
->attr
.num_subbuf
);
4605 if (cmd_ret
!= LTTNG_OK
) {
4606 ret
= - (int) cmd_ret
;
4607 ERR("Failed to add channel to notification thread");
4608 goto error_remove_from_registry
;
4611 error_remove_from_registry
:
4613 ust_registry_channel_del_free(registry
, ua_chan
->key
, false);
4618 session_put(session
);
4624 * Create and send to the application the created buffers with per PID buffers.
4626 * Called with UST app session lock held.
4627 * The session list lock and the session's lock must be acquired.
4629 * Return 0 on success else a negative value.
4631 static int create_map_per_pid(struct ust_app
*app
,
4632 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
4633 struct ust_app_map
*ua_map
)
4636 struct ust_registry_session
*registry
;
4637 struct ltt_session
*session
= NULL
;
4638 uint64_t map_reg_key
;
4639 struct ust_registry_map
*ust_reg_map
;
4646 DBG("UST app creating map %s with per PID buffers", ua_map
->name
);
4650 registry
= get_session_registry(ua_sess
);
4651 /* The UST app session lock is held, registry shall not be null. */
4654 /* Create and add a new map registry to session. */
4655 ret
= ust_registry_map_add(registry
, ua_map
->key
);
4657 ERR("Error creating the UST map \"%s\" registry instance",
4662 session
= session_find_by_id(ua_sess
->tracing_id
);
4665 assert(pthread_mutex_trylock(&session
->lock
));
4666 assert(session_trylock_list());
4668 /* Create and get map. */
4669 ret
= create_map_object(usess
, ua_sess
, ua_map
);
4671 ERR("Error creating UST map object: map_name = \"%s\" ",
4673 goto error_remove_from_registry
;
4676 ret
= send_map_pid_to_ust(app
, ua_sess
, ua_map
);
4678 if (ret
!= -ENOTCONN
) {
4679 ERR("Error sending map to application");
4681 goto error_remove_from_registry
;
4684 map_reg_key
= ua_map
->key
;
4685 pthread_mutex_lock(®istry
->lock
);
4686 ust_reg_map
= ust_registry_map_find(registry
, map_reg_key
);
4687 assert(ust_reg_map
);
4688 pthread_mutex_unlock(®istry
->lock
);
4690 error_remove_from_registry
:
4692 ust_registry_map_del_free(registry
, ua_map
->key
);
4697 session_put(session
);
4703 * From an already allocated ust app channel, create the channel buffers if
4704 * needed and send them to the application. This MUST be called with a RCU read
4705 * side lock acquired.
4707 * Called with UST app session lock held.
4709 * Return 0 on success or else a negative value. Returns -ENOTCONN if
4710 * the application exited concurrently.
4712 static int ust_app_channel_send(struct ust_app
*app
,
4713 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
4714 struct ust_app_channel
*ua_chan
)
4720 assert(usess
->active
);
4724 /* Handle buffer type before sending the channel to the application. */
4725 switch (usess
->buffer_type
) {
4726 case LTTNG_BUFFER_PER_UID
:
4728 ret
= create_channel_per_uid(app
, usess
, ua_sess
, ua_chan
);
4734 case LTTNG_BUFFER_PER_PID
:
4736 ret
= create_channel_per_pid(app
, usess
, ua_sess
, ua_chan
);
4748 /* Initialize ust objd object using the received handle and add it. */
4749 lttng_ht_node_init_ulong(&ua_chan
->ust_objd_node
, ua_chan
->handle
);
4750 lttng_ht_add_unique_ulong(app
->ust_chan_objd
, &ua_chan
->ust_objd_node
);
4752 /* If channel is not enabled, disable it on the tracer */
4753 if (!ua_chan
->enabled
) {
4754 ret
= disable_ust_channel(app
, ua_sess
, ua_chan
);
4765 * From an already allocated ust app map, create the map buffers if
4766 * needed and send them to the application. This MUST be called with a RCU read
4767 * side lock acquired.
4769 * Called with UST app session lock held.
4771 * Return 0 on success or else a negative value. Returns -ENOTCONN if
4772 * the application exited concurrently.
4774 static int ust_app_map_send(struct ust_app
*app
,
4775 struct ltt_ust_session
*usess
, struct ust_app_session
*ua_sess
,
4776 struct ust_app_map
*ua_map
)
4782 assert(usess
->active
);
4786 /* Handle buffer type before sending the map to the application. */
4787 switch (usess
->buffer_type
) {
4788 case LTTNG_BUFFER_PER_UID
:
4790 ret
= create_map_per_uid(app
, usess
, ua_sess
, ua_map
);
4796 case LTTNG_BUFFER_PER_PID
:
4798 ret
= create_map_per_pid(app
, usess
, ua_sess
, ua_map
);
4810 /* Initialize ust objd object using the received handle and add it. */
4811 lttng_ht_node_init_ulong(&ua_map
->ust_objd_node
, ua_map
->handle
);
4812 lttng_ht_add_unique_ulong(app
->ust_map_objd
, &ua_map
->ust_objd_node
);
4814 /* If map is not enabled, disable it on the tracer */
4815 if (!ua_map
->enabled
) {
4816 ret
= disable_ust_map(app
, ua_sess
, ua_map
);
4827 * Create UST app channel and return it through ua_chanp if not NULL.
4829 * Called with UST app session lock and RCU read-side lock held.
4831 * Return 0 on success or else a negative value.
4833 static int ust_app_channel_allocate(struct ust_app_session
*ua_sess
,
4834 struct ltt_ust_channel
*uchan
,
4835 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
4836 struct ust_app_channel
**ua_chanp
)
4839 struct lttng_ht_iter iter
;
4840 struct lttng_ht_node_str
*ua_chan_node
;
4841 struct ust_app_channel
*ua_chan
;
4843 /* Lookup channel in the ust app session */
4844 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &iter
);
4845 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
4846 if (ua_chan_node
!= NULL
) {
4847 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
4851 ua_chan
= alloc_ust_app_channel(uchan
->name
, ua_sess
, &uchan
->attr
);
4852 if (ua_chan
== NULL
) {
4853 /* Only malloc can fail here */
4857 shadow_copy_channel(ua_chan
, uchan
);
4859 /* Set channel type. */
4860 ua_chan
->attr
.type
= type
;
4862 /* Only add the channel if successful on the tracer side. */
4863 lttng_ht_add_unique_str(ua_sess
->channels
, &ua_chan
->node
);
4866 *ua_chanp
= ua_chan
;
4869 /* Everything went well. */
4877 * Create UST app map and return it through ua_mapp if not NULL.
4879 * Called with UST app session lock and RCU read-side lock held.
4881 * Return 0 on success or else a negative value.
4883 static int ust_app_map_allocate(struct ust_app_session
*ua_sess
,
4884 struct ltt_ust_map
*umap
,
4885 enum lttng_ust_chan_type type
, struct ltt_ust_session
*usess
,
4886 struct ust_app_map
**ua_mapp
)
4889 struct lttng_ht_iter iter
;
4890 struct lttng_ht_node_str
*ua_map_node
;
4891 struct ust_app_map
*ua_map
;
4893 DBG("Allocating map id = %"PRIu64
, umap
->id
);
4895 /* Lookup map in the ust app session */
4896 lttng_ht_lookup(ua_sess
->maps
, (void *)umap
->name
, &iter
);
4897 ua_map_node
= lttng_ht_iter_get_node_str(&iter
);
4898 if (ua_map_node
!= NULL
) {
4899 ua_map
= caa_container_of(ua_map_node
, struct ust_app_map
, node
);
4903 ua_map
= alloc_ust_app_map(umap
->name
, ua_sess
);
4904 if (ua_map
== NULL
) {
4905 /* Only malloc can fail here */
4909 //shadow_copy_map(ua_map, umap);
4910 ua_map
->tracing_map_id
= umap
->id
;
4911 ua_map
->coalesce_hits
= umap
->coalesce_hits
;
4912 ua_map
->dead_app_kv_values
= &umap
->dead_app_kv_values
;
4913 ua_map
->bitness
= umap
->bitness
;
4916 //ua_map->attr.type = type;
4917 ua_map
->bucket_count
= umap
->bucket_count
;
4919 /* Only add the map if successful on the tracer side. */
4920 lttng_ht_add_unique_str(ua_sess
->maps
, &ua_map
->node
);
4926 /* Everything went well. */
4934 * Create UST app event and create it on the tracer side.
4936 * Must be called with the RCU read side lock held.
4937 * Called with ust app session mutex held.
4940 int create_ust_app_channel_event(struct ust_app_session
*ua_sess
,
4941 struct ust_app_channel
*ua_chan
, struct ltt_ust_event
*uevent
,
4942 struct ust_app
*app
)
4945 struct ust_app_event
*ua_event
;
4947 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
4948 if (ua_event
== NULL
) {
4949 /* Only failure mode of alloc_ust_app_event(). */
4953 shadow_copy_event(ua_event
, uevent
);
4955 /* Create it on the tracer side */
4956 ret
= create_ust_channel_event(app
, ua_sess
, ua_chan
, ua_event
);
4959 * Not found previously means that it does not exist on the
4960 * tracer. If the application reports that the event existed,
4961 * it means there is a bug in the sessiond or lttng-ust
4962 * (or corruption, etc.)
4964 if (ret
== -LTTNG_UST_ERR_EXIST
) {
4965 ERR("Tracer for application reported that an event being created already existed: "
4966 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
4968 app
->pid
, app
->ppid
, app
->uid
,
4974 add_unique_ust_app_event(ua_chan
->events
, ua_event
);
4976 DBG2("UST app create event completed: app = '%s' (ppid: %d)",
4977 app
->name
, app
->ppid
);
4983 /* Valid. Calling here is already in a read side lock */
4984 delete_ust_app_event(-1, ua_event
, app
);
4989 * Create UST app event and create it on the tracer side.
4991 * Must be called with the RCU read side lock held.
4992 * Called with ust app session mutex held.
4995 int create_ust_app_map_event(struct ust_app_session
*ua_sess
,
4996 struct ust_app_map
*ua_map
, struct ltt_ust_event
*uevent
,
4997 struct ust_app
*app
)
5000 uint64_t map_reg_key
;
5001 struct ust_app_event
*ua_event
;
5002 struct ust_registry_session
*registry
;
5004 ua_event
= alloc_ust_app_event(uevent
->attr
.name
, &uevent
->attr
);
5005 if (ua_event
== NULL
) {
5006 /* Only failure mode of alloc_ust_app_event(). */
5010 shadow_copy_event(ua_event
, uevent
);
5012 registry
= get_session_registry(ua_sess
);
5014 DBG("Application session is being torn down. Abort event notify");
5019 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
5020 map_reg_key
= ua_map
->tracing_map_id
;
5022 map_reg_key
= ua_map
->key
;
5025 pthread_mutex_lock(®istry
->lock
);
5026 ret
= ust_registry_map_add_token_key_mapping(registry
, map_reg_key
,
5027 uevent
->attr
.token
, uevent
->key
);
5029 pthread_mutex_unlock(®istry
->lock
);
5031 /* Create it on the tracer side */
5032 ret
= create_ust_map_event(app
, ua_sess
, ua_map
, uevent
->key
, ua_event
);
5035 * Not found previously means that it does not exist on the
5036 * tracer. If the application reports that the event existed,
5037 * it means there is a bug in the sessiond or lttng-ust
5038 * (or corruption, etc.)
5040 if (ret
== -LTTNG_UST_ERR_EXIST
) {
5041 ERR("Tracer for application reported that an event being created already existed: "
5042 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
5044 app
->pid
, app
->ppid
, app
->uid
,
5049 * FIXME: frdeso: remove key from tokey->key mapping.
5054 add_unique_ust_app_event(ua_map
->events
, ua_event
);
5056 DBG2("UST app create event completed: app = '%s', tracer token = %"PRIu64
" (ppid: %d)",
5057 app
->name
, uevent
->attr
.token
, app
->ppid
);
5063 /* Valid. Calling here is already in a read side lock */
5064 delete_ust_app_event(-1, ua_event
, app
);
5069 * Create UST app event notifier rule and create it on the tracer side.
5071 * Must be called with the RCU read side lock held.
5072 * Called with ust app session mutex held.
5075 int create_ust_app_event_notifier_rule(struct lttng_trigger
*trigger
,
5076 struct ust_app
*app
)
5079 struct ust_app_event_notifier_rule
*ua_event_notifier_rule
;
5081 ua_event_notifier_rule
= alloc_ust_app_event_notifier_rule(trigger
);
5082 if (ua_event_notifier_rule
== NULL
) {
5087 /* Create it on the tracer side. */
5088 ret
= create_ust_event_notifier(app
, ua_event_notifier_rule
);
5091 * Not found previously means that it does not exist on the
5092 * tracer. If the application reports that the event existed,
5093 * it means there is a bug in the sessiond or lttng-ust
5094 * (or corruption, etc.)
5096 if (ret
== -LTTNG_UST_ERR_EXIST
) {
5097 ERR("Tracer for application reported that an event notifier being created already exists: "
5098 "token = \"%" PRIu64
"\", pid = %d, ppid = %d, uid = %d, gid = %d",
5099 lttng_trigger_get_tracer_token(trigger
),
5100 app
->pid
, app
->ppid
, app
->uid
,
5106 lttng_ht_add_unique_u64(app
->token_to_event_notifier_rule_ht
,
5107 &ua_event_notifier_rule
->node
);
5109 DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64
,
5110 app
->name
, app
->ppid
, lttng_trigger_get_tracer_token(trigger
));
5115 /* The RCU read side lock is already being held by the caller. */
5116 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule
, app
);
5122 * Create UST metadata and open it on the tracer side.
5124 * Called with UST app session lock held and RCU read side lock.
5126 static int create_ust_app_metadata(struct ust_app_session
*ua_sess
,
5127 struct ust_app
*app
, struct consumer_output
*consumer
)
5130 struct ust_app_channel
*metadata
;
5131 struct consumer_socket
*socket
;
5132 struct ust_registry_session
*registry
;
5133 struct ltt_session
*session
= NULL
;
5139 registry
= get_session_registry(ua_sess
);
5140 /* The UST app session is held registry shall not be null. */
5143 pthread_mutex_lock(®istry
->lock
);
5145 /* Metadata already exists for this registry or it was closed previously */
5146 if (registry
->metadata_key
|| registry
->metadata_closed
) {
5151 /* Allocate UST metadata */
5152 metadata
= alloc_ust_app_channel(DEFAULT_METADATA_NAME
, ua_sess
, NULL
);
5154 /* malloc() failed */
5159 memcpy(&metadata
->attr
, &ua_sess
->metadata_attr
, sizeof(metadata
->attr
));
5161 /* Need one fd for the channel. */
5162 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
5164 ERR("Exhausted number of available FD upon create metadata");
5168 /* Get the right consumer socket for the application. */
5169 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
, consumer
);
5172 goto error_consumer
;
5176 * Keep metadata key so we can identify it on the consumer side. Assign it
5177 * to the registry *before* we ask the consumer so we avoid the race of the
5178 * consumer requesting the metadata and the ask_channel call on our side
5179 * did not returned yet.
5181 registry
->metadata_key
= metadata
->key
;
5183 session
= session_find_by_id(ua_sess
->tracing_id
);
5186 assert(pthread_mutex_trylock(&session
->lock
));
5187 assert(session_trylock_list());
5190 * Ask the metadata channel creation to the consumer. The metadata object
5191 * will be created by the consumer and kept their. However, the stream is
5192 * never added or monitored until we do a first push metadata to the
5195 ret
= ust_consumer_ask_channel(ua_sess
, metadata
, consumer
, socket
,
5196 registry
, session
->current_trace_chunk
);
5198 /* Nullify the metadata key so we don't try to close it later on. */
5199 registry
->metadata_key
= 0;
5200 goto error_consumer
;
5204 * The setup command will make the metadata stream be sent to the relayd,
5205 * if applicable, and the thread managing the metadatas. This is important
5206 * because after this point, if an error occurs, the only way the stream
5207 * can be deleted is to be monitored in the consumer.
5209 ret
= consumer_setup_metadata(socket
, metadata
->key
);
5211 /* Nullify the metadata key so we don't try to close it later on. */
5212 registry
->metadata_key
= 0;
5213 goto error_consumer
;
5216 DBG2("UST metadata with key %" PRIu64
" created for app pid %d",
5217 metadata
->key
, app
->pid
);
5220 lttng_fd_put(LTTNG_FD_APPS
, 1);
5221 delete_ust_app_channel(-1, metadata
, app
);
5223 pthread_mutex_unlock(®istry
->lock
);
5225 session_put(session
);
5231 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
5232 * acquired before calling this function.
5234 struct ust_app
*ust_app_find_by_pid(pid_t pid
)
5236 struct ust_app
*app
= NULL
;
5237 struct lttng_ht_node_ulong
*node
;
5238 struct lttng_ht_iter iter
;
5240 lttng_ht_lookup(ust_app_ht
, (void *)((unsigned long) pid
), &iter
);
5241 node
= lttng_ht_iter_get_node_ulong(&iter
);
5243 DBG2("UST app no found with pid %d", pid
);
5247 DBG2("Found UST app by pid %d", pid
);
5249 app
= caa_container_of(node
, struct ust_app
, pid_n
);
5256 * Allocate and init an UST app object using the registration information and
5257 * the command socket. This is called when the command socket connects to the
5260 * The object is returned on success or else NULL.
5262 struct ust_app
*ust_app_create(struct ust_register_msg
*msg
, int sock
)
5265 struct ust_app
*lta
= NULL
;
5266 struct lttng_pipe
*event_notifier_event_source_pipe
= NULL
;
5271 DBG3("UST app creating application for socket %d", sock
);
5273 if ((msg
->bits_per_long
== 64 &&
5274 (uatomic_read(&ust_consumerd64_fd
) == -EINVAL
))
5275 || (msg
->bits_per_long
== 32 &&
5276 (uatomic_read(&ust_consumerd32_fd
) == -EINVAL
))) {
5277 ERR("Registration failed: application \"%s\" (pid: %d) has "
5278 "%d-bit long, but no consumerd for this size is available.\n",
5279 msg
->name
, msg
->pid
, msg
->bits_per_long
);
5284 * Reserve the two file descriptors of the event source pipe. The write
5285 * end will be closed once it is passed to the application, at which
5286 * point a single 'put' will be performed.
5288 ret
= lttng_fd_get(LTTNG_FD_APPS
, 2);
5290 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s' (ppid: %d)",
5291 msg
->name
, (int) msg
->ppid
);
5295 event_notifier_event_source_pipe
= lttng_pipe_open(FD_CLOEXEC
);
5296 if (!event_notifier_event_source_pipe
) {
5297 PERROR("Failed to open application event source pipe: '%s' (ppid = %d)",
5298 msg
->name
, msg
->ppid
);
5302 lta
= zmalloc(sizeof(struct ust_app
));
5305 goto error_free_pipe
;
5308 lta
->event_notifier_group
.event_pipe
= event_notifier_event_source_pipe
;
5310 lta
->ppid
= msg
->ppid
;
5311 lta
->uid
= msg
->uid
;
5312 lta
->gid
= msg
->gid
;
5314 lta
->bits_per_long
= msg
->bits_per_long
;
5315 lta
->uint8_t_alignment
= msg
->uint8_t_alignment
;
5316 lta
->uint16_t_alignment
= msg
->uint16_t_alignment
;
5317 lta
->uint32_t_alignment
= msg
->uint32_t_alignment
;
5318 lta
->uint64_t_alignment
= msg
->uint64_t_alignment
;
5319 lta
->long_alignment
= msg
->long_alignment
;
5320 lta
->byte_order
= msg
->byte_order
;
5322 lta
->v_major
= msg
->major
;
5323 lta
->v_minor
= msg
->minor
;
5324 lta
->sessions
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
5325 lta
->ust_chan_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
5326 lta
->ust_map_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
5327 lta
->ust_sessions_objd
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
5328 lta
->notify_sock
= -1;
5329 lta
->token_to_event_notifier_rule_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
5331 /* Copy name and make sure it's NULL terminated. */
5332 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
5333 lta
->name
[UST_APP_PROCNAME_LEN
] = '\0';
5336 * Before this can be called, when receiving the registration information,
5337 * the application compatibility is checked. So, at this point, the
5338 * application can work with this session daemon.
5340 lta
->compatible
= 1;
5342 lta
->pid
= msg
->pid
;
5343 lttng_ht_node_init_ulong(<a
->pid_n
, (unsigned long) lta
->pid
);
5345 pthread_mutex_init(<a
->sock_lock
, NULL
);
5346 lttng_ht_node_init_ulong(<a
->sock_n
, (unsigned long) lta
->sock
);
5348 CDS_INIT_LIST_HEAD(<a
->teardown_head
);
5352 lttng_pipe_destroy(event_notifier_event_source_pipe
);
5353 lttng_fd_put(LTTNG_FD_APPS
, 2);
5359 * For a given application object, add it to every hash table.
5361 void ust_app_add(struct ust_app
*app
)
5364 assert(app
->notify_sock
>= 0);
5366 app
->registration_time
= time(NULL
);
5371 * On a re-registration, we want to kick out the previous registration of
5374 lttng_ht_add_replace_ulong(ust_app_ht
, &app
->pid_n
);
5377 * The socket _should_ be unique until _we_ call close. So, a add_unique
5378 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
5379 * already in the table.
5381 lttng_ht_add_unique_ulong(ust_app_ht_by_sock
, &app
->sock_n
);
5383 /* Add application to the notify socket hash table. */
5384 lttng_ht_node_init_ulong(&app
->notify_sock_n
, app
->notify_sock
);
5385 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock
, &app
->notify_sock_n
);
5387 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
5388 "notify_sock:%d (version %d.%d)", app
->pid
, app
->ppid
, app
->uid
,
5389 app
->gid
, app
->sock
, app
->name
, app
->notify_sock
, app
->v_major
,
5396 * Set the application version into the object.
5398 * Return 0 on success else a negative value either an errno code or a
5399 * LTTng-UST error code.
5401 int ust_app_version(struct ust_app
*app
)
5407 pthread_mutex_lock(&app
->sock_lock
);
5408 ret
= ustctl_tracer_version(app
->sock
, &app
->version
);
5409 pthread_mutex_unlock(&app
->sock_lock
);
5411 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
5412 ERR("UST app %d version failed with ret %d", app
->sock
, ret
);
5414 DBG3("UST app %d version failed. Application is dead", app
->sock
);
5422 * Setup the base event notifier group.
5424 * Return 0 on success else a negative value either an errno code or a
5425 * LTTng-UST error code.
5427 int ust_app_setup_event_notifier_group(struct ust_app
*app
)
5430 int event_pipe_write_fd
;
5431 struct lttng_ust_object_data
*event_notifier_group
= NULL
;
5432 enum lttng_error_code lttng_ret
;
5433 enum event_notifier_error_accounting_status event_notifier_error_accounting_status
;
5437 /* Get the write side of the pipe. */
5438 event_pipe_write_fd
= lttng_pipe_get_writefd(
5439 app
->event_notifier_group
.event_pipe
);
5441 pthread_mutex_lock(&app
->sock_lock
);
5442 ret
= ustctl_create_event_notifier_group(app
->sock
,
5443 event_pipe_write_fd
, &event_notifier_group
);
5444 pthread_mutex_unlock(&app
->sock_lock
);
5446 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
5447 ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d",
5448 ret
, app
->sock
, event_pipe_write_fd
);
5450 DBG("Failed to create application event notifier group (application is dead): app socket fd = %d",
5457 ret
= lttng_pipe_write_close(app
->event_notifier_group
.event_pipe
);
5459 ERR("Failed to close write end of the application's event source pipe: app = '%s' (ppid = %d)",
5460 app
->name
, app
->ppid
);
5465 * Release the file descriptor that was reserved for the write-end of
5468 lttng_fd_put(LTTNG_FD_APPS
, 1);
5470 lttng_ret
= notification_thread_command_add_tracer_event_source(
5471 notification_thread_handle
,
5472 lttng_pipe_get_readfd(app
->event_notifier_group
.event_pipe
),
5474 if (lttng_ret
!= LTTNG_OK
) {
5475 ERR("Failed to add tracer event source to notification thread");
5480 /* Assign handle only when the complete setup is valid. */
5481 app
->event_notifier_group
.object
= event_notifier_group
;
5483 event_notifier_error_accounting_status
= event_notifier_error_accounting_register_app(app
);
5484 if (event_notifier_error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
5485 ERR("Failed to setup event notifier error accounting for app");
5493 ustctl_release_object(app
->sock
, app
->event_notifier_group
.object
);
5494 free(app
->event_notifier_group
.object
);
5499 * Unregister app by removing it from the global traceable app list and freeing
5502 * The socket is already closed at this point so no close to sock.
5504 void ust_app_unregister(int sock
)
5506 struct ust_app
*lta
;
5507 struct lttng_ht_node_ulong
*node
;
5508 struct lttng_ht_iter ust_app_sock_iter
;
5509 struct lttng_ht_iter iter
;
5510 struct ust_app_session
*ua_sess
;
5515 /* Get the node reference for a call_rcu */
5516 lttng_ht_lookup(ust_app_ht_by_sock
, (void *)((unsigned long) sock
), &ust_app_sock_iter
);
5517 node
= lttng_ht_iter_get_node_ulong(&ust_app_sock_iter
);
5520 lta
= caa_container_of(node
, struct ust_app
, sock_n
);
5521 DBG("PID %d unregistering with sock %d", lta
->pid
, sock
);
5524 * For per-PID buffers, perform "push metadata" and flush all
5525 * application streams before removing app from hash tables,
5526 * ensuring proper behavior of data_pending check.
5527 * Remove sessions so they are not visible during deletion.
5529 cds_lfht_for_each_entry(lta
->sessions
->ht
, &iter
.iter
, ua_sess
,
5531 struct ust_registry_session
*registry
;
5533 ret
= lttng_ht_del(lta
->sessions
, &iter
);
5535 /* The session was already removed so scheduled for teardown. */
5539 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
5540 (void) ust_app_flush_app_session(lta
, ua_sess
);
5544 * Add session to list for teardown. This is safe since at this point we
5545 * are the only one using this list.
5547 pthread_mutex_lock(&ua_sess
->lock
);
5549 if (ua_sess
->deleted
) {
5550 pthread_mutex_unlock(&ua_sess
->lock
);
5555 * Normally, this is done in the delete session process which is
5556 * executed in the call rcu below. However, upon registration we can't
5557 * afford to wait for the grace period before pushing data or else the
5558 * data pending feature can race between the unregistration and stop
5559 * command where the data pending command is sent *before* the grace
5562 * The close metadata below nullifies the metadata pointer in the
5563 * session so the delete session will NOT push/close a second time.
5565 registry
= get_session_registry(ua_sess
);
5567 /* Push metadata for application before freeing the application. */
5568 (void) push_metadata(registry
, ua_sess
->consumer
);
5571 * Don't ask to close metadata for global per UID buffers. Close
5572 * metadata only on destroy trace session in this case. Also, the
5573 * previous push metadata could have flag the metadata registry to
5574 * close so don't send a close command if closed.
5576 if (ua_sess
->buffer_type
!= LTTNG_BUFFER_PER_UID
) {
5577 /* And ask to close it for this session registry. */
5578 (void) close_metadata(registry
, ua_sess
->consumer
);
5581 cds_list_add(&ua_sess
->teardown_node
, <a
->teardown_head
);
5583 pthread_mutex_unlock(&ua_sess
->lock
);
5586 /* Remove application from PID hash table */
5587 ret
= lttng_ht_del(ust_app_ht_by_sock
, &ust_app_sock_iter
);
5591 * Remove application from notify hash table. The thread handling the
5592 * notify socket could have deleted the node so ignore on error because
5593 * either way it's valid. The close of that socket is handled by the
5594 * apps_notify_thread.
5596 iter
.iter
.node
= <a
->notify_sock_n
.node
;
5597 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
5600 * Ignore return value since the node might have been removed before by an
5601 * add replace during app registration because the PID can be reassigned by
5604 iter
.iter
.node
= <a
->pid_n
.node
;
5605 ret
= lttng_ht_del(ust_app_ht
, &iter
);
5607 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
5612 call_rcu(<a
->pid_n
.head
, delete_ust_app_rcu
);
5619 * Fill events array with all events name of all registered apps.
5621 int ust_app_list_events(struct lttng_event
**events
)
5624 size_t nbmem
, count
= 0;
5625 struct lttng_ht_iter iter
;
5626 struct ust_app
*app
;
5627 struct lttng_event
*tmp_event
;
5629 nbmem
= UST_APP_EVENT_LIST_SIZE
;
5630 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event
));
5631 if (tmp_event
== NULL
) {
5632 PERROR("zmalloc ust app events");
5639 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5640 struct lttng_ust_tracepoint_iter uiter
;
5642 health_code_update();
5644 if (!app
->compatible
) {
5646 * TODO: In time, we should notice the caller of this error by
5647 * telling him that this is a version error.
5651 pthread_mutex_lock(&app
->sock_lock
);
5652 handle
= ustctl_tracepoint_list(app
->sock
);
5654 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
5655 ERR("UST app list events getting handle failed for app pid %d",
5658 pthread_mutex_unlock(&app
->sock_lock
);
5662 while ((ret
= ustctl_tracepoint_list_get(app
->sock
, handle
,
5663 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
5664 /* Handle ustctl error. */
5668 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
5669 ERR("UST app tp list get failed for app %d with ret %d",
5672 DBG3("UST app tp list get failed. Application is dead");
5674 * This is normal behavior, an application can die during the
5675 * creation process. Don't report an error so the execution can
5676 * continue normally. Continue normal execution.
5681 release_ret
= ustctl_release_handle(app
->sock
, handle
);
5682 if (release_ret
< 0 &&
5683 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
5684 release_ret
!= -EPIPE
) {
5685 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
5687 pthread_mutex_unlock(&app
->sock_lock
);
5691 health_code_update();
5692 if (count
>= nbmem
) {
5693 /* In case the realloc fails, we free the memory */
5694 struct lttng_event
*new_tmp_event
;
5697 new_nbmem
= nbmem
<< 1;
5698 DBG2("Reallocating event list from %zu to %zu entries",
5700 new_tmp_event
= realloc(tmp_event
,
5701 new_nbmem
* sizeof(struct lttng_event
));
5702 if (new_tmp_event
== NULL
) {
5705 PERROR("realloc ust app events");
5708 release_ret
= ustctl_release_handle(app
->sock
, handle
);
5709 if (release_ret
< 0 &&
5710 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
5711 release_ret
!= -EPIPE
) {
5712 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
5714 pthread_mutex_unlock(&app
->sock_lock
);
5717 /* Zero the new memory */
5718 memset(new_tmp_event
+ nbmem
, 0,
5719 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
5721 tmp_event
= new_tmp_event
;
5723 memcpy(tmp_event
[count
].name
, uiter
.name
, LTTNG_UST_SYM_NAME_LEN
);
5724 tmp_event
[count
].loglevel
= uiter
.loglevel
;
5725 tmp_event
[count
].type
= (enum lttng_event_type
) LTTNG_UST_TRACEPOINT
;
5726 tmp_event
[count
].pid
= app
->pid
;
5727 tmp_event
[count
].enabled
= -1;
5730 ret
= ustctl_release_handle(app
->sock
, handle
);
5731 pthread_mutex_unlock(&app
->sock_lock
);
5732 if (ret
< 0 && ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
5733 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
5738 *events
= tmp_event
;
5740 DBG2("UST app list events done (%zu events)", count
);
5745 health_code_update();
5750 * Fill events array with all events name of all registered apps.
5752 int ust_app_list_event_fields(struct lttng_event_field
**fields
)
5755 size_t nbmem
, count
= 0;
5756 struct lttng_ht_iter iter
;
5757 struct ust_app
*app
;
5758 struct lttng_event_field
*tmp_event
;
5760 nbmem
= UST_APP_EVENT_LIST_SIZE
;
5761 tmp_event
= zmalloc(nbmem
* sizeof(struct lttng_event_field
));
5762 if (tmp_event
== NULL
) {
5763 PERROR("zmalloc ust app event fields");
5770 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5771 struct lttng_ust_field_iter uiter
;
5773 health_code_update();
5775 if (!app
->compatible
) {
5777 * TODO: In time, we should notice the caller of this error by
5778 * telling him that this is a version error.
5782 pthread_mutex_lock(&app
->sock_lock
);
5783 handle
= ustctl_tracepoint_field_list(app
->sock
);
5785 if (handle
!= -EPIPE
&& handle
!= -LTTNG_UST_ERR_EXITING
) {
5786 ERR("UST app list field getting handle failed for app pid %d",
5789 pthread_mutex_unlock(&app
->sock_lock
);
5793 while ((ret
= ustctl_tracepoint_field_list_get(app
->sock
, handle
,
5794 &uiter
)) != -LTTNG_UST_ERR_NOENT
) {
5795 /* Handle ustctl error. */
5799 if (ret
!= -LTTNG_UST_ERR_EXITING
&& ret
!= -EPIPE
) {
5800 ERR("UST app tp list field failed for app %d with ret %d",
5803 DBG3("UST app tp list field failed. Application is dead");
5805 * This is normal behavior, an application can die during the
5806 * creation process. Don't report an error so the execution can
5807 * continue normally. Reset list and count for next app.
5812 release_ret
= ustctl_release_handle(app
->sock
, handle
);
5813 pthread_mutex_unlock(&app
->sock_lock
);
5814 if (release_ret
< 0 &&
5815 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
5816 release_ret
!= -EPIPE
) {
5817 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
5822 health_code_update();
5823 if (count
>= nbmem
) {
5824 /* In case the realloc fails, we free the memory */
5825 struct lttng_event_field
*new_tmp_event
;
5828 new_nbmem
= nbmem
<< 1;
5829 DBG2("Reallocating event field list from %zu to %zu entries",
5831 new_tmp_event
= realloc(tmp_event
,
5832 new_nbmem
* sizeof(struct lttng_event_field
));
5833 if (new_tmp_event
== NULL
) {
5836 PERROR("realloc ust app event fields");
5839 release_ret
= ustctl_release_handle(app
->sock
, handle
);
5840 pthread_mutex_unlock(&app
->sock_lock
);
5842 release_ret
!= -LTTNG_UST_ERR_EXITING
&&
5843 release_ret
!= -EPIPE
) {
5844 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, release_ret
);
5848 /* Zero the new memory */
5849 memset(new_tmp_event
+ nbmem
, 0,
5850 (new_nbmem
- nbmem
) * sizeof(struct lttng_event_field
));
5852 tmp_event
= new_tmp_event
;
5855 memcpy(tmp_event
[count
].field_name
, uiter
.field_name
, LTTNG_UST_SYM_NAME_LEN
);
5856 /* Mapping between these enums matches 1 to 1. */
5857 tmp_event
[count
].type
= (enum lttng_event_field_type
) uiter
.type
;
5858 tmp_event
[count
].nowrite
= uiter
.nowrite
;
5860 memcpy(tmp_event
[count
].event
.name
, uiter
.event_name
, LTTNG_UST_SYM_NAME_LEN
);
5861 tmp_event
[count
].event
.loglevel
= uiter
.loglevel
;
5862 tmp_event
[count
].event
.type
= LTTNG_EVENT_TRACEPOINT
;
5863 tmp_event
[count
].event
.pid
= app
->pid
;
5864 tmp_event
[count
].event
.enabled
= -1;
5867 ret
= ustctl_release_handle(app
->sock
, handle
);
5868 pthread_mutex_unlock(&app
->sock_lock
);
5870 ret
!= -LTTNG_UST_ERR_EXITING
&&
5872 ERR("Error releasing app handle for app %d with ret %d", app
->sock
, ret
);
5877 *fields
= tmp_event
;
5879 DBG2("UST app list event fields done (%zu events)", count
);
5884 health_code_update();
5889 * Free and clean all traceable apps of the global list.
5891 * Should _NOT_ be called with RCU read-side lock held.
5893 void ust_app_clean_list(void)
5896 struct ust_app
*app
;
5897 struct lttng_ht_iter iter
;
5899 DBG2("UST app cleaning registered apps hash table");
5903 /* Cleanup notify socket hash table */
5904 if (ust_app_ht_by_notify_sock
) {
5905 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock
->ht
, &iter
.iter
, app
,
5906 notify_sock_n
.node
) {
5908 * Assert that all notifiers are gone as all triggers
5909 * are unregistered prior to this clean-up.
5911 assert(lttng_ht_get_count(app
->token_to_event_notifier_rule_ht
) == 0);
5913 ust_app_notify_sock_unregister(app
->notify_sock
);
5918 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5919 ret
= lttng_ht_del(ust_app_ht
, &iter
);
5921 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
5925 /* Cleanup socket hash table */
5926 if (ust_app_ht_by_sock
) {
5927 cds_lfht_for_each_entry(ust_app_ht_by_sock
->ht
, &iter
.iter
, app
,
5929 ret
= lttng_ht_del(ust_app_ht_by_sock
, &iter
);
5936 /* Destroy is done only when the ht is empty */
5938 ht_cleanup_push(ust_app_ht
);
5940 if (ust_app_ht_by_sock
) {
5941 ht_cleanup_push(ust_app_ht_by_sock
);
5943 if (ust_app_ht_by_notify_sock
) {
5944 ht_cleanup_push(ust_app_ht_by_notify_sock
);
5949 * Init UST app hash table.
5951 int ust_app_ht_alloc(void)
5953 ust_app_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
5957 ust_app_ht_by_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
5958 if (!ust_app_ht_by_sock
) {
5961 ust_app_ht_by_notify_sock
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
5962 if (!ust_app_ht_by_notify_sock
) {
5969 * For a specific UST session, disable the channel for all registered apps.
5971 int ust_app_disable_channel_glb(struct ltt_ust_session
*usess
,
5972 struct ltt_ust_channel
*uchan
)
5975 struct lttng_ht_iter iter
;
5976 struct lttng_ht_node_str
*ua_chan_node
;
5977 struct ust_app
*app
;
5978 struct ust_app_session
*ua_sess
;
5979 struct ust_app_channel
*ua_chan
;
5981 assert(usess
->active
);
5982 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64
,
5983 uchan
->name
, usess
->id
);
5987 /* For every registered applications */
5988 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
5989 struct lttng_ht_iter uiter
;
5990 if (!app
->compatible
) {
5992 * TODO: In time, we should notice the caller of this error by
5993 * telling him that this is a version error.
5997 ua_sess
= lookup_session_by_app(usess
, app
);
5998 if (ua_sess
== NULL
) {
6003 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
6004 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6005 /* If the session if found for the app, the channel must be there */
6006 assert(ua_chan_node
);
6008 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6009 /* The channel must not be already disabled */
6010 assert(ua_chan
->enabled
== 1);
6012 /* Disable channel onto application */
6013 ret
= disable_ust_app_channel(ua_sess
, ua_chan
, app
);
6015 /* XXX: We might want to report this error at some point... */
6025 * For a specific UST session, disable the channel for all registered apps.
6027 int ust_app_disable_map_glb(struct ltt_ust_session
*usess
,
6028 struct ltt_ust_map
*umap
)
6031 struct lttng_ht_iter iter
;
6032 struct lttng_ht_node_str
*ua_map_node
;
6033 struct ust_app
*app
;
6034 struct ust_app_session
*ua_sess
;
6035 struct ust_app_map
*ua_map
;
6037 assert(usess
->active
);
6038 DBG2("UST app disabling map %s from global domain for session id %" PRIu64
,
6039 umap
->name
, usess
->id
);
6043 /* For every registered applications */
6044 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6045 struct lttng_ht_iter uiter
;
6046 if (!app
->compatible
) {
6048 * TODO: In time, we should notice the caller of this error by
6049 * telling him that this is a version error.
6053 ua_sess
= lookup_session_by_app(usess
, app
);
6054 if (ua_sess
== NULL
) {
6059 lttng_ht_lookup(ua_sess
->maps
, (void *)umap
->name
, &uiter
);
6060 ua_map_node
= lttng_ht_iter_get_node_str(&uiter
);
6061 /* If the session if found for the app, the map must be there */
6062 assert(ua_map_node
);
6064 ua_map
= caa_container_of(ua_map_node
, struct ust_app_map
, node
);
6065 /* The map must not be already disabled */
6066 assert(ua_map
->enabled
== 1);
6068 /* Disable map onto application */
6069 ret
= disable_ust_app_map(ua_sess
, ua_map
, app
);
6071 /* XXX: We might want to report this error at some point... */
6081 * For a specific UST session, enable the channel for all registered apps.
6083 int ust_app_enable_channel_glb(struct ltt_ust_session
*usess
,
6084 struct ltt_ust_channel
*uchan
)
6087 struct lttng_ht_iter iter
;
6088 struct ust_app
*app
;
6089 struct ust_app_session
*ua_sess
;
6091 assert(usess
->active
);
6092 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64
,
6093 uchan
->name
, usess
->id
);
6097 /* For every registered applications */
6098 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6099 if (!app
->compatible
) {
6101 * TODO: In time, we should notice the caller of this error by
6102 * telling him that this is a version error.
6106 ua_sess
= lookup_session_by_app(usess
, app
);
6107 if (ua_sess
== NULL
) {
6111 /* Enable channel onto application */
6112 ret
= enable_ust_app_channel(ua_sess
, uchan
, app
);
6114 /* XXX: We might want to report this error at some point... */
6124 * For a specific UST session, enable the map for all registered apps.
6126 int ust_app_enable_map_glb(struct ltt_ust_session
*usess
,
6127 struct ltt_ust_map
*umap
)
6130 struct lttng_ht_iter iter
;
6131 struct ust_app
*app
;
6132 struct ust_app_session
*ua_sess
;
6134 assert(usess
->active
);
6135 DBG2("UST app enabling map %s to global domain for session id %" PRIu64
,
6136 umap
->name
, usess
->id
);
6140 /* For every registered applications */
6141 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6142 if (!app
->compatible
) {
6144 * TODO: In time, we should notice the caller of this error by
6145 * telling him that this is a version error.
6149 ua_sess
= lookup_session_by_app(usess
, app
);
6150 if (ua_sess
== NULL
) {
6154 /* Enable map onto application */
6155 ret
= enable_ust_app_map(ua_sess
, umap
, app
);
6157 /* XXX: We might want to report this error at some point... */
6167 * Disable an event in a channel and for a specific session.
6169 int ust_app_disable_channel_event_glb(struct ltt_ust_session
*usess
,
6170 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
6173 struct lttng_ht_iter iter
, uiter
;
6174 struct lttng_ht_node_str
*ua_chan_node
;
6175 struct ust_app
*app
;
6176 struct ust_app_session
*ua_sess
;
6177 struct ust_app_channel
*ua_chan
;
6178 struct ust_app_event
*ua_event
;
6180 assert(usess
->active
);
6181 DBG("UST app disabling event %s for all apps in channel "
6182 "%s for session id %" PRIu64
,
6183 uevent
->attr
.name
, uchan
->name
, usess
->id
);
6187 /* For all registered applications */
6188 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6189 if (!app
->compatible
) {
6191 * TODO: In time, we should notice the caller of this error by
6192 * telling him that this is a version error.
6196 ua_sess
= lookup_session_by_app(usess
, app
);
6197 if (ua_sess
== NULL
) {
6202 /* Lookup channel in the ust app session */
6203 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
6204 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6205 if (ua_chan_node
== NULL
) {
6206 DBG2("Channel %s not found in session id %" PRIu64
" for app pid %d."
6207 "Skipping", uchan
->name
, usess
->id
, app
->pid
);
6210 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6212 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
6213 uevent
->filter
, uevent
->attr
.loglevel
,
6214 uevent
->exclusion
, uevent
->attr
.token
);
6215 if (ua_event
== NULL
) {
6216 DBG2("Event %s not found in channel %s for app pid %d."
6217 "Skipping", uevent
->attr
.name
, uchan
->name
, app
->pid
);
6221 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
6223 /* XXX: Report error someday... */
6233 * Disable an event in a map and for a specific session.
6235 int ust_app_disable_map_event_glb(struct ltt_ust_session
*usess
,
6236 struct ltt_ust_map
*umap
, struct ltt_ust_event
*uevent
)
6239 struct lttng_ht_iter iter
, uiter
;
6240 struct lttng_ht_node_str
*ua_map_node
;
6241 struct ust_app
*app
;
6242 struct ust_app_session
*ua_sess
;
6243 struct ust_app_map
*ua_map
;
6244 struct ust_app_event
*ua_event
;
6246 assert(usess
->active
);
6247 DBG("UST app disabling event %s for all apps in map "
6248 "%s for session id %" PRIu64
,
6249 uevent
->attr
.name
, umap
->name
, usess
->id
);
6253 /* For all registered applications */
6254 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6255 if (!app
->compatible
) {
6257 * TODO: In time, we should notice the caller of this error by
6258 * telling him that this is a version error.
6262 ua_sess
= lookup_session_by_app(usess
, app
);
6263 if (ua_sess
== NULL
) {
6268 /* Lookup map in the ust app session */
6269 lttng_ht_lookup(ua_sess
->maps
, (void *)umap
->name
, &uiter
);
6270 ua_map_node
= lttng_ht_iter_get_node_str(&uiter
);
6271 if (ua_map_node
== NULL
) {
6272 DBG2("map %s not found in session id %" PRIu64
" for app pid %d."
6273 "Skipping", umap
->name
, usess
->id
, app
->pid
);
6276 ua_map
= caa_container_of(ua_map_node
, struct ust_app_map
, node
);
6278 ua_event
= find_ust_app_event(ua_map
->events
, uevent
->attr
.name
,
6279 uevent
->filter
, uevent
->attr
.loglevel
,
6280 uevent
->exclusion
, uevent
->attr
.token
);
6281 if (ua_event
== NULL
) {
6282 DBG2("Event %s not found in map %s for app pid %d."
6283 "Skipping", uevent
->attr
.name
, umap
->name
, app
->pid
);
6287 ret
= disable_ust_app_event(ua_sess
, ua_event
, app
);
6289 /* XXX: Report error someday... */
6298 /* The ua_sess lock must be held by the caller. */
6300 int ust_app_channel_create(struct ltt_ust_session
*usess
,
6301 struct ust_app_session
*ua_sess
,
6302 struct ltt_ust_channel
*uchan
, struct ust_app
*app
,
6303 struct ust_app_channel
**_ua_chan
)
6306 struct ust_app_channel
*ua_chan
= NULL
;
6309 ASSERT_LOCKED(ua_sess
->lock
);
6311 if (!strncmp(uchan
->name
, DEFAULT_METADATA_NAME
,
6312 sizeof(uchan
->name
))) {
6313 copy_channel_attr_to_ustctl(&ua_sess
->metadata_attr
,
6317 struct ltt_ust_context
*uctx
= NULL
;
6320 * Create channel onto application and synchronize its
6323 ret
= ust_app_channel_allocate(ua_sess
, uchan
,
6324 LTTNG_UST_CHAN_PER_CPU
, usess
,
6330 ret
= ust_app_channel_send(app
, usess
,
6337 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
6338 ret
= create_ust_app_channel_context(ua_chan
,
6351 * The application's socket is not valid. Either a bad socket
6352 * or a timeout on it. We can't inform the caller that for a
6353 * specific app, the session failed so lets continue here.
6355 ret
= 0; /* Not an error. */
6363 if (ret
== 0 && _ua_chan
) {
6365 * Only return the application's channel on success. Note
6366 * that the channel can still be part of the application's
6367 * channel hashtable on error.
6369 *_ua_chan
= ua_chan
;
6374 /* The ua_sess lock must be held by the caller. */
6376 int ust_app_map_create(struct ltt_ust_session
*usess
,
6377 struct ust_app_session
*ua_sess
,
6378 struct ltt_ust_map
*umap
, struct ust_app
*app
,
6379 struct ust_app_map
**_ua_map
)
6382 struct ust_app_map
*ua_map
= NULL
;
6385 ASSERT_LOCKED(ua_sess
->lock
);
6388 * Create map onto application and synchronize its
6391 ret
= ust_app_map_allocate(ua_sess
, umap
,
6392 LTTNG_UST_CHAN_PER_CPU
, usess
,
6398 ret
= ust_app_map_send(app
, usess
, ua_sess
, ua_map
);
6408 * The application's socket is not valid. Either a bad socket
6409 * or a timeout on it. We can't inform the caller that for a
6410 * specific app, the session failed so lets continue here.
6412 ret
= 0; /* Not an error. */
6420 if (ret
== 0 && _ua_map
) {
6422 * Only return the application's map on success. Note
6423 * that the map can still be part of the application's
6424 * map hashtable on error.
6432 * Enable event for a specific session and channel on the tracer.
6434 int ust_app_enable_channel_event_glb(struct ltt_ust_session
*usess
,
6435 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
6438 struct lttng_ht_iter iter
, uiter
;
6439 struct lttng_ht_node_str
*ua_chan_node
;
6440 struct ust_app
*app
;
6441 struct ust_app_session
*ua_sess
;
6442 struct ust_app_channel
*ua_chan
;
6443 struct ust_app_event
*ua_event
;
6445 assert(usess
->active
);
6446 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
6447 uevent
->attr
.name
, usess
->id
);
6450 * NOTE: At this point, this function is called only if the session and
6451 * channel passed are already created for all apps. and enabled on the
6457 /* For all registered applications */
6458 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6459 if (!app
->compatible
) {
6461 * TODO: In time, we should notice the caller of this error by
6462 * telling him that this is a version error.
6466 ua_sess
= lookup_session_by_app(usess
, app
);
6468 /* The application has problem or is probably dead. */
6472 pthread_mutex_lock(&ua_sess
->lock
);
6474 if (ua_sess
->deleted
) {
6475 pthread_mutex_unlock(&ua_sess
->lock
);
6479 /* Lookup channel in the ust app session */
6480 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
6481 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6483 * It is possible that the channel cannot be found is
6484 * the channel/event creation occurs concurrently with
6485 * an application exit.
6487 if (!ua_chan_node
) {
6488 pthread_mutex_unlock(&ua_sess
->lock
);
6492 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6494 /* Get event node */
6495 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
6496 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
,
6497 uevent
->attr
.token
);
6498 if (ua_event
== NULL
) {
6499 DBG3("UST app enable event %s not found for app PID %d."
6500 "Skipping app", uevent
->attr
.name
, app
->pid
);
6504 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
6506 pthread_mutex_unlock(&ua_sess
->lock
);
6510 pthread_mutex_unlock(&ua_sess
->lock
);
6519 * Enable event for a specific session and map on the tracer.
6521 int ust_app_enable_map_event_glb(struct ltt_ust_session
*usess
,
6522 struct ltt_ust_map
*umap
, struct ltt_ust_event
*uevent
)
6525 struct lttng_ht_iter iter
, uiter
;
6526 struct lttng_ht_node_str
*ua_map_node
;
6527 struct ust_app
*app
;
6528 struct ust_app_session
*ua_sess
;
6529 struct ust_app_map
*ua_map
;
6530 struct ust_app_event
*ua_event
;
6532 assert(usess
->active
);
6533 DBG("UST app enabling event %s for all apps for session id %" PRIu64
,
6534 uevent
->attr
.name
, usess
->id
);
6537 * NOTE: At this point, this function is called only if the session and
6538 * map passed are already created for all apps. and enabled on the
6544 /* For all registered applications */
6545 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6546 if (!app
->compatible
) {
6548 * TODO: In time, we should notice the caller of this error by
6549 * telling him that this is a version error.
6553 ua_sess
= lookup_session_by_app(usess
, app
);
6555 /* The application has problem or is probably dead. */
6559 pthread_mutex_lock(&ua_sess
->lock
);
6561 if (ua_sess
->deleted
) {
6562 pthread_mutex_unlock(&ua_sess
->lock
);
6566 /* Lookup map in the ust app session */
6567 lttng_ht_lookup(ua_sess
->maps
, (void *)umap
->name
, &uiter
);
6568 ua_map_node
= lttng_ht_iter_get_node_str(&uiter
);
6570 * It is possible that the map cannot be found is
6571 * the map/event creation occurs concurrently with
6572 * an application exit.
6575 pthread_mutex_unlock(&ua_sess
->lock
);
6579 ua_map
= caa_container_of(ua_map_node
, struct ust_app_map
, node
);
6581 /* Get event node */
6582 ua_event
= find_ust_app_event(ua_map
->events
, uevent
->attr
.name
,
6583 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
,
6584 uevent
->attr
.token
);
6585 if (ua_event
== NULL
) {
6586 DBG3("UST app enable event %s not found for app PID %d."
6587 "Skipping app", uevent
->attr
.name
, app
->pid
);
6591 ret
= enable_ust_app_event(ua_sess
, ua_event
, app
);
6593 pthread_mutex_unlock(&ua_sess
->lock
);
6597 pthread_mutex_unlock(&ua_sess
->lock
);
6606 * For a specific existing UST session and UST channel, creates the event for
6607 * all registered apps.
6609 int ust_app_create_channel_event_glb(struct ltt_ust_session
*usess
,
6610 struct ltt_ust_channel
*uchan
, struct ltt_ust_event
*uevent
)
6613 struct lttng_ht_iter iter
, uiter
;
6614 struct lttng_ht_node_str
*ua_chan_node
;
6615 struct ust_app
*app
;
6616 struct ust_app_session
*ua_sess
;
6617 struct ust_app_channel
*ua_chan
;
6619 assert(usess
->active
);
6620 DBG("UST app creating event %s for all apps for session id %" PRIu64
,
6621 uevent
->attr
.name
, usess
->id
);
6625 /* For all registered applications */
6626 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
6627 if (!app
->compatible
) {
6629 * TODO: In time, we should notice the caller of this error by
6630 * telling him that this is a version error.
6634 ua_sess
= lookup_session_by_app(usess
, app
);
6636 /* The application has problem or is probably dead. */
6640 pthread_mutex_lock(&ua_sess
->lock
);
6642 if (ua_sess
->deleted
) {
6643 pthread_mutex_unlock(&ua_sess
->lock
);
6647 /* Lookup channel in the ust app session */
6648 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
6649 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
6650 /* If the channel is not found, there is a code flow error */
6651 assert(ua_chan_node
);
6653 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
6655 ret
= create_ust_app_channel_event(ua_sess
, ua_chan
, uevent
, app
);
6656 pthread_mutex_unlock(&ua_sess
->lock
);
6658 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
6659 /* Possible value at this point: -ENOMEM. If so, we stop! */
6662 DBG2("UST app event %s already exist on app PID %d",
6663 uevent
->attr
.name
, app
->pid
);
6673 int snapshot_key_values(struct ustctl_daemon_counter
*map_handle
,
6674 struct lttng_ht
*key_to_bucket_index_ht
, int cpu
,
6675 const char *key_filter
, struct lttng_ht
*values
)
6678 struct lttng_ht_iter key_iter
;
6679 struct ust_registry_map_index_ht_entry
*map_index_entry
;
6681 /* Iterate over all the formated_key -> counter index */
6682 cds_lfht_for_each_entry(key_to_bucket_index_ht
->ht
,
6683 &key_iter
.iter
, map_index_entry
, node
.node
) {
6684 bool overflow
= 0, underflow
= 0;
6685 int64_t local_value
= 0;
6686 size_t dimension_indexes
[1] = {map_index_entry
->index
};
6688 if (key_filter
&& strcmp(key_filter
,
6689 map_index_entry
->formated_key
) != 0) {
6693 ret
= ustctl_counter_read(map_handle
,
6694 dimension_indexes
, cpu
, &local_value
,
6695 &overflow
, &underflow
);
6697 ERR("Error getting counter value from the tracer: key = '%s'",
6698 map_index_entry
->formated_key
);
6703 map_add_or_increment_map_values(values
,
6704 map_index_entry
->formated_key
, local_value
,
6705 underflow
, overflow
);
6713 int ust_app_map_list_values_per_uid_with_bitness_and_cpu(
6714 const struct ltt_ust_session
*usess
,
6715 const struct ltt_ust_map
*umap
,
6716 uint32_t app_bitness
,
6718 const char *key_filter
,
6719 struct lttng_ht
*values
)
6722 struct lttng_ht_iter iter
;
6723 struct buffer_reg_uid
*buf_reg_uid
;
6724 struct buffer_reg_map
*buf_reg_map
;
6725 struct ust_registry_session
*ust_reg_sess
;
6726 struct lttng_ht_node_u64
*ust_reg_map_node
;
6727 struct ust_registry_map
*ust_reg_map
;
6729 buf_reg_uid
= buffer_reg_uid_find(usess
->id
, app_bitness
, usess
->uid
);
6732 * Buffer registry entry for uid not found. Probably no app for
6733 * this UID at the moment.
6735 DBG("No buffer registry entry found for uid: ust-sess-id = %"PRIu64
", bitness = %"PRIu32
", uid = %d",
6736 usess
->id
, app_bitness
, usess
->uid
);
6738 * Not an error. Leave the key value pair unchanged and return.
6744 buf_reg_map
= buffer_reg_map_find(umap
->id
, buf_reg_uid
);
6746 ERR("Error getting per-uid map buffer registry entry: map-id = %"PRIu64
,
6752 ust_reg_sess
= buf_reg_uid
->registry
->reg
.ust
;
6754 /* Get the ust_reg map object from the registry */
6755 // FIXME: frdeso: This can be changed to ust_registry_map_find() right?
6757 lttng_ht_lookup(ust_reg_sess
->maps
, (void *) &umap
->id
, &iter
);
6758 ust_reg_map_node
= lttng_ht_iter_get_node_u64(&iter
);
6759 if (!ust_reg_map_node
) {
6760 ERR("Error getting per-uid map buffer registry entry: map-id = %"PRIu64
,
6765 ust_reg_map
= caa_container_of(ust_reg_map_node
,
6766 struct ust_registry_map
, node
);
6768 ret
= snapshot_key_values(buf_reg_map
->daemon_counter
,
6769 ust_reg_map
->key_string_to_bucket_index_ht
,
6770 cpu
, key_filter
, values
);
6782 int ust_app_map_list_values_per_uid(const struct ltt_ust_session
*usess
,
6783 const struct ltt_ust_map
*umap
,
6784 const struct lttng_map_query
*query
,
6785 struct lttng_map_content
*map_content
)
6788 enum lttng_map_query_status map_query_status
;
6789 const char *key_filter
;
6790 struct lttng_ht
*values
= NULL
;
6791 bool sum_cpus
= lttng_map_query_get_config_sum_by_cpu(query
);
6792 enum lttng_map_query_config_buffer config_buffer
;
6793 enum lttng_map_query_config_cpu config_cpu
;
6796 map_query_status
= lttng_map_query_get_key_filter(query
, &key_filter
);
6797 if (map_query_status
== LTTNG_MAP_QUERY_STATUS_NONE
) {
6799 } else if (map_query_status
!= LTTNG_MAP_QUERY_STATUS_OK
) {
6804 config_cpu
= lttng_map_query_get_config_cpu(query
);
6805 if (config_cpu
== LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET
) {
6807 map_query_status
= lttng_map_query_get_cpu_count(query
, &count
);
6808 assert(map_query_status
== LTTNG_MAP_QUERY_STATUS_OK
);
6811 map_query_status
= lttng_map_query_get_cpu_at_index(query
, 0,
6813 assert(map_query_status
== LTTNG_MAP_QUERY_STATUS_OK
);
6816 config_buffer
= lttng_map_query_get_config_buffer(query
);
6817 if (config_buffer
== LTTNG_MAP_QUERY_CONFIG_BUFFER_UST_UID_SUBSET
) {
6821 map_query_status
= lttng_map_query_get_uid_count(query
, &count
);
6822 assert(map_query_status
== LTTNG_MAP_QUERY_STATUS_OK
);
6825 map_query_status
= lttng_map_query_get_uid_at_index(query
, 0,
6827 assert(map_query_status
== LTTNG_MAP_QUERY_STATUS_OK
);
6829 if (selected_uid
!= usess
->uid
) {
6836 values
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
6839 for (i
= 0; i
< umap
->nr_cpu
; i
++) {
6840 if (config_cpu
== LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET
) {
6841 if (selected_cpu
!= i
) {
6847 values
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
6850 ret
= ust_app_map_list_values_per_uid_with_bitness_and_cpu(
6851 usess
, umap
, 32, i
, key_filter
,
6857 ret
= ust_app_map_list_values_per_uid_with_bitness_and_cpu(
6858 usess
, umap
, 64, i
, key_filter
,
6864 ret
= map_new_content_section(map_content
,
6865 LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_UID
,
6866 sum_cpus
, usess
->uid
, i
, values
);
6871 lttng_ht_destroy(values
);
6876 ret
= map_new_content_section(map_content
,
6877 LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_UID
,
6878 sum_cpus
, usess
->uid
, 0, values
);
6882 lttng_ht_destroy(values
);
6890 int append_dead_app_kv(struct ltt_ust_map
*umap
,
6891 const char *key_filter
,
6892 struct lttng_map_content
*map_content
)
6895 struct lttng_ht
*dead_app_kv_ht
;
6896 struct map_kv_ht_entry
*kv_entry
;
6897 struct lttng_ht_iter key_iter
;
6899 struct lttng_ht
*values
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
6901 pthread_mutex_lock(&(umap
->dead_app_kv_values
.lock
));
6903 assert(umap
->dead_app_kv_values
.dead_app_kv_values_64bits
);
6904 dead_app_kv_ht
= umap
->dead_app_kv_values
.dead_app_kv_values_64bits
;
6906 cds_lfht_for_each_entry(dead_app_kv_ht
->ht
, &key_iter
.iter
, kv_entry
,
6908 if (key_filter
&& strcmp(key_filter
, kv_entry
->key
) != 0) {
6911 map_add_or_increment_map_values(values
, kv_entry
->key
,
6912 kv_entry
->value
, kv_entry
->has_underflowed
,
6913 kv_entry
->has_overflowed
);
6916 assert(umap
->dead_app_kv_values
.dead_app_kv_values_32bits
);
6918 dead_app_kv_ht
= umap
->dead_app_kv_values
.dead_app_kv_values_32bits
;
6919 cds_lfht_for_each_entry(dead_app_kv_ht
->ht
, &key_iter
.iter
, kv_entry
,
6921 if (key_filter
&& strcmp(key_filter
, kv_entry
->key
) != 0) {
6924 map_add_or_increment_map_values(values
, kv_entry
->key
,
6925 kv_entry
->value
, kv_entry
->has_underflowed
,
6926 kv_entry
->has_overflowed
);
6929 pthread_mutex_unlock(&umap
->dead_app_kv_values
.lock
);
6931 ret
= map_new_content_section(map_content
,
6932 LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID_AGGREGATED
,
6933 true, 0, 0, values
);
6935 lttng_ht_destroy(values
);
6937 ERR("Error appending deadapp kv");
6949 int ust_app_map_list_values_per_pid_with_bitness_and_cpu(
6950 const struct ltt_ust_session
*usess
,
6951 struct ust_app
*app
,
6952 struct ltt_ust_map
*umap
,
6953 uint32_t app_bitness
,
6955 const char *key_filter
,
6956 struct lttng_ht
*values
)
6960 struct lttng_ht_iter map_iter
;
6961 struct lttng_ht_node_str
*ua_map_node
;
6962 struct ust_app_map
*ua_map
;
6963 struct ust_app_session
*ua_sess
;
6964 struct ust_registry_session
*ust_reg_sess
;
6965 struct ust_registry_map
*ust_reg_map
;
6967 if (app
->bits_per_long
!= app_bitness
) {
6972 ua_sess
= lookup_session_by_app(usess
, app
);
6974 /* Session not associated with this app. */
6979 ust_reg_sess
= get_session_registry(ua_sess
);
6980 if (!ust_reg_sess
) {
6981 DBG("Application session is being torn down. Skip application.");
6986 /* Lookup map in the ust app session */
6987 lttng_ht_lookup(ua_sess
->maps
, (void *)umap
->name
, &map_iter
);
6988 ua_map_node
= lttng_ht_iter_get_node_str(&map_iter
);
6990 assert(ua_map_node
!= NULL
);
6991 ua_map
= caa_container_of(ua_map_node
, struct ust_app_map
, node
);
6993 pthread_mutex_lock(&ust_reg_sess
->lock
);
6994 ust_reg_map
= ust_registry_map_find(ust_reg_sess
, ua_map
->key
);
6995 pthread_mutex_unlock(&ust_reg_sess
->lock
);
6996 assert(ust_reg_map
);
6998 ret
= snapshot_key_values(ua_map
->map_handle
,
6999 ust_reg_map
->key_string_to_bucket_index_ht
,
7000 cpu
, key_filter
, values
);
7002 ERR("Error snapshoting the content of map");
7011 int ust_app_map_list_values_per_pid(const struct ltt_ust_session
*usess
,
7012 struct ltt_ust_map
*umap
,
7013 const struct lttng_map_query
*query
,
7014 struct lttng_map_content
*map_content
)
7016 enum lttng_map_query_status map_query_status
;
7017 const char *key_filter
;
7018 struct lttng_ht
*values
;
7019 bool sum_cpus
= lttng_map_query_get_config_sum_by_cpu(query
);
7020 bool sum_pids
= lttng_map_query_get_config_sum_by_pid(query
);
7021 enum lttng_map_query_config_cpu config_cpu
;
7022 int selected_cpu
, i
, ret
= 0;
7023 struct lttng_ht_iter app_iter
;
7024 struct ust_app
*app
;
7026 values
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
7028 map_query_status
= lttng_map_query_get_key_filter(query
, &key_filter
);
7029 if (map_query_status
== LTTNG_MAP_QUERY_STATUS_NONE
) {
7031 } else if (map_query_status
!= LTTNG_MAP_QUERY_STATUS_OK
) {
7036 config_cpu
= lttng_map_query_get_config_cpu(query
);
7037 if (config_cpu
== LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET
) {
7039 map_query_status
= lttng_map_query_get_cpu_count(query
, &count
);
7040 assert(map_query_status
== LTTNG_MAP_QUERY_STATUS_OK
);
7043 map_query_status
= lttng_map_query_get_cpu_at_index(query
, 0,
7045 assert(map_query_status
== LTTNG_MAP_QUERY_STATUS_OK
);
7048 /* Sum all cpus and pids on the same table. */
7049 if (sum_cpus
&& sum_pids
) {
7050 values
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
7053 if (!sum_cpus
&& sum_pids
) {
7054 /* Iterate over all currently registered apps. */
7055 for (i
= 0; i
< umap
->nr_cpu
; i
++) {
7056 values
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
7057 cds_lfht_for_each_entry(ust_app_ht
->ht
, &app_iter
.iter
, app
, pid_n
.node
) {
7058 ret
= ust_app_map_list_values_per_pid_with_bitness_and_cpu(
7059 usess
, app
, umap
, 32, i
, key_filter
, values
);
7063 ret
= ust_app_map_list_values_per_pid_with_bitness_and_cpu(
7064 usess
, app
, umap
, 64, i
, key_filter
, values
);
7069 ret
= map_new_content_section(map_content
,
7070 LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID
,
7071 sum_cpus
, app
->pid
, i
, values
);
7076 lttng_ht_destroy(values
);
7079 /* Iterate over all currently registered apps. */
7080 cds_lfht_for_each_entry(ust_app_ht
->ht
, &app_iter
.iter
, app
, pid_n
.node
) {
7082 if (sum_cpus
&& !sum_pids
) {
7083 values
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
7086 for (i
= 0; i
< umap
->nr_cpu
; i
++) {
7088 if (config_cpu
== LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET
) {
7089 if (selected_cpu
!= i
) {
7094 if (!sum_cpus
&& !sum_pids
) {
7095 values
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
7098 ret
= ust_app_map_list_values_per_pid_with_bitness_and_cpu(
7099 usess
, app
, umap
, 32, i
, key_filter
, values
);
7103 ret
= ust_app_map_list_values_per_pid_with_bitness_and_cpu(
7104 usess
, app
, umap
, 64, i
, key_filter
, values
);
7109 if (!sum_cpus
&& !sum_pids
) {
7110 ret
= map_new_content_section(map_content
,
7111 LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID
,
7112 sum_cpus
, app
->pid
, i
, values
);
7117 lttng_ht_destroy(values
);
7120 if (sum_cpus
&& !sum_pids
) {
7121 ret
= map_new_content_section(map_content
,
7122 LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID
,
7123 sum_cpus
, app
->pid
, i
, values
);
7128 lttng_ht_destroy(values
);
7133 if (sum_cpus
&& sum_pids
) {
7134 ret
= map_new_content_section(map_content
,
7135 LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID
,
7136 sum_cpus
, 0, 0, values
);
7140 lttng_ht_destroy(values
);
7143 /* Append dead app aggregated key-value pairs. */
7144 ret
= append_dead_app_kv(umap
, key_filter
, map_content
);
7146 ERR("Error appending values from dead apps map");
7154 int ust_app_map_list_values(const struct ltt_ust_session
*usess
,
7155 struct ltt_ust_map
*umap
,
7156 const struct lttng_map_query
*query
,
7157 struct lttng_map_content
**map_content
)
7160 struct lttng_map_content
*local_map_content
= NULL
;
7162 local_map_content
= lttng_map_content_create(usess
->buffer_type
);
7163 if (!local_map_content
) {
7164 ERR("Error creating a map content list");
7169 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
7170 ret
= ust_app_map_list_values_per_uid(usess
, umap
, query
,
7173 ERR("Error adding per-uid map value");
7178 ret
= ust_app_map_list_values_per_pid(usess
, umap
, query
,
7181 ERR("Error adding per-pid map value");
7186 *map_content
= local_map_content
;
7187 local_map_content
= NULL
;
7192 lttng_map_content_destroy(local_map_content
);
7197 * For a specific existing UST session and UST map, creates the event for
7198 * all registered apps.
7200 int ust_app_create_map_event_glb(struct ltt_ust_session
*usess
,
7201 struct ltt_ust_map
*umap
, struct ltt_ust_event
*uevent
)
7204 struct lttng_ht_iter iter
, uiter
;
7205 struct lttng_ht_node_str
*ua_map_node
;
7206 struct ust_app
*app
;
7207 struct ust_app_session
*ua_sess
;
7208 struct ust_app_map
*ua_map
;
7210 assert(usess
->active
);
7211 DBG("UST app creating event %s in map %s for all apps for session id %" PRIu64
,
7212 uevent
->attr
.name
, umap
->name
, usess
->id
);
7216 /* For all registered applications */
7217 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7218 if (!app
->compatible
) {
7220 * TODO: In time, we should notice the caller of this error by
7221 * telling him that this is a version error.
7225 ua_sess
= lookup_session_by_app(usess
, app
);
7227 /* The application has problem or is probably dead. */
7231 pthread_mutex_lock(&ua_sess
->lock
);
7233 if (ua_sess
->deleted
) {
7234 pthread_mutex_unlock(&ua_sess
->lock
);
7238 /* Lookup map in the ust app session */
7239 lttng_ht_lookup(ua_sess
->maps
, (void *)umap
->name
, &uiter
);
7240 ua_map_node
= lttng_ht_iter_get_node_str(&uiter
);
7241 /* If the map is not found, there is a code flow error */
7242 assert(ua_map_node
);
7244 ua_map
= caa_container_of(ua_map_node
, struct ust_app_map
, node
);
7246 ret
= create_ust_app_map_event(ua_sess
, ua_map
, uevent
, app
);
7248 pthread_mutex_unlock(&ua_sess
->lock
);
7250 if (ret
!= -LTTNG_UST_ERR_EXIST
) {
7251 /* Possible value at this point: -ENOMEM. If so, we stop! */
7254 DBG2("UST app event %s already exist on app PID %d",
7255 uevent
->attr
.name
, app
->pid
);
7265 * Start tracing for a specific UST session and app.
7267 * Called with UST app session lock held.
7271 int ust_app_start_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
7274 struct ust_app_session
*ua_sess
;
7276 DBG("Starting tracing for ust app pid %d", app
->pid
);
7280 if (!app
->compatible
) {
7284 ua_sess
= lookup_session_by_app(usess
, app
);
7285 if (ua_sess
== NULL
) {
7286 /* The session is in teardown process. Ignore and continue. */
7290 pthread_mutex_lock(&ua_sess
->lock
);
7292 if (ua_sess
->deleted
) {
7293 pthread_mutex_unlock(&ua_sess
->lock
);
7297 if (ua_sess
->enabled
) {
7298 pthread_mutex_unlock(&ua_sess
->lock
);
7302 /* Upon restart, we skip the setup, already done */
7303 if (ua_sess
->started
) {
7307 health_code_update();
7310 /* This starts the UST tracing */
7311 pthread_mutex_lock(&app
->sock_lock
);
7312 ret
= ustctl_start_session(app
->sock
, ua_sess
->handle
);
7313 pthread_mutex_unlock(&app
->sock_lock
);
7315 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
7316 ERR("Error starting tracing for app pid: %d (ret: %d)",
7319 DBG("UST app start session failed. Application is dead.");
7321 * This is normal behavior, an application can die during the
7322 * creation process. Don't report an error so the execution can
7323 * continue normally.
7325 pthread_mutex_unlock(&ua_sess
->lock
);
7331 /* Indicate that the session has been started once */
7332 ua_sess
->started
= 1;
7333 ua_sess
->enabled
= 1;
7335 pthread_mutex_unlock(&ua_sess
->lock
);
7337 health_code_update();
7339 /* Quiescent wait after starting trace */
7340 pthread_mutex_lock(&app
->sock_lock
);
7341 ret
= ustctl_wait_quiescent(app
->sock
);
7342 pthread_mutex_unlock(&app
->sock_lock
);
7343 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
7344 ERR("UST app wait quiescent failed for app pid %d ret %d",
7350 health_code_update();
7354 pthread_mutex_unlock(&ua_sess
->lock
);
7356 health_code_update();
7361 * Stop tracing for a specific UST session and app.
7364 int ust_app_stop_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
7367 struct ust_app_session
*ua_sess
;
7368 struct ust_registry_session
*registry
;
7370 DBG("Stopping tracing for ust app pid %d", app
->pid
);
7374 if (!app
->compatible
) {
7375 goto end_no_session
;
7378 ua_sess
= lookup_session_by_app(usess
, app
);
7379 if (ua_sess
== NULL
) {
7380 goto end_no_session
;
7383 pthread_mutex_lock(&ua_sess
->lock
);
7385 if (ua_sess
->deleted
) {
7386 pthread_mutex_unlock(&ua_sess
->lock
);
7387 goto end_no_session
;
7391 * If started = 0, it means that stop trace has been called for a session
7392 * that was never started. It's possible since we can have a fail start
7393 * from either the application manager thread or the command thread. Simply
7394 * indicate that this is a stop error.
7396 if (!ua_sess
->started
) {
7397 goto error_rcu_unlock
;
7400 health_code_update();
7402 /* This inhibits UST tracing */
7403 pthread_mutex_lock(&app
->sock_lock
);
7404 ret
= ustctl_stop_session(app
->sock
, ua_sess
->handle
);
7405 pthread_mutex_unlock(&app
->sock_lock
);
7407 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
7408 ERR("Error stopping tracing for app pid: %d (ret: %d)",
7411 DBG("UST app stop session failed. Application is dead.");
7413 * This is normal behavior, an application can die during the
7414 * creation process. Don't report an error so the execution can
7415 * continue normally.
7419 goto error_rcu_unlock
;
7422 health_code_update();
7423 ua_sess
->enabled
= 0;
7425 /* Quiescent wait after stopping trace */
7426 pthread_mutex_lock(&app
->sock_lock
);
7427 ret
= ustctl_wait_quiescent(app
->sock
);
7428 pthread_mutex_unlock(&app
->sock_lock
);
7429 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
7430 ERR("UST app wait quiescent failed for app pid %d ret %d",
7434 health_code_update();
7436 registry
= get_session_registry(ua_sess
);
7438 /* The UST app session is held registry shall not be null. */
7441 /* Push metadata for application before freeing the application. */
7442 (void) push_metadata(registry
, ua_sess
->consumer
);
7445 pthread_mutex_unlock(&ua_sess
->lock
);
7448 health_code_update();
7452 pthread_mutex_unlock(&ua_sess
->lock
);
7454 health_code_update();
7459 int ust_app_flush_app_session(struct ust_app
*app
,
7460 struct ust_app_session
*ua_sess
)
7462 int ret
, retval
= 0;
7463 struct lttng_ht_iter iter
;
7464 struct ust_app_channel
*ua_chan
;
7465 struct consumer_socket
*socket
;
7467 DBG("Flushing app session buffers for ust app pid %d", app
->pid
);
7471 if (!app
->compatible
) {
7472 goto end_not_compatible
;
7475 pthread_mutex_lock(&ua_sess
->lock
);
7477 if (ua_sess
->deleted
) {
7481 health_code_update();
7483 /* Flushing buffers */
7484 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7487 /* Flush buffers and push metadata. */
7488 switch (ua_sess
->buffer_type
) {
7489 case LTTNG_BUFFER_PER_PID
:
7490 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
, ua_chan
,
7492 health_code_update();
7493 ret
= consumer_flush_channel(socket
, ua_chan
->key
);
7495 ERR("Error flushing consumer channel");
7501 case LTTNG_BUFFER_PER_UID
:
7507 health_code_update();
7510 pthread_mutex_unlock(&ua_sess
->lock
);
7514 health_code_update();
7519 * Flush buffers for all applications for a specific UST session.
7520 * Called with UST session lock held.
7523 int ust_app_flush_session(struct ltt_ust_session
*usess
)
7528 DBG("Flushing session buffers for all ust apps");
7532 /* Flush buffers and push metadata. */
7533 switch (usess
->buffer_type
) {
7534 case LTTNG_BUFFER_PER_UID
:
7536 struct buffer_reg_uid
*reg
;
7537 struct lttng_ht_iter iter
;
7539 /* Flush all per UID buffers associated to that session. */
7540 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7541 struct ust_registry_session
*ust_session_reg
;
7542 struct buffer_reg_channel
*buf_reg_chan
;
7543 struct consumer_socket
*socket
;
7545 /* Get consumer socket to use to push the metadata.*/
7546 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
7549 /* Ignore request if no consumer is found for the session. */
7553 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
7554 buf_reg_chan
, node
.node
) {
7556 * The following call will print error values so the return
7557 * code is of little importance because whatever happens, we
7558 * have to try them all.
7560 (void) consumer_flush_channel(socket
, buf_reg_chan
->consumer_key
);
7563 ust_session_reg
= reg
->registry
->reg
.ust
;
7564 /* Push metadata. */
7565 (void) push_metadata(ust_session_reg
, usess
->consumer
);
7569 case LTTNG_BUFFER_PER_PID
:
7571 struct ust_app_session
*ua_sess
;
7572 struct lttng_ht_iter iter
;
7573 struct ust_app
*app
;
7575 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7576 ua_sess
= lookup_session_by_app(usess
, app
);
7577 if (ua_sess
== NULL
) {
7580 (void) ust_app_flush_app_session(app
, ua_sess
);
7591 health_code_update();
7596 int ust_app_clear_quiescent_app_session(struct ust_app
*app
,
7597 struct ust_app_session
*ua_sess
)
7600 struct lttng_ht_iter iter
;
7601 struct ust_app_channel
*ua_chan
;
7602 struct consumer_socket
*socket
;
7604 DBG("Clearing stream quiescent state for ust app pid %d", app
->pid
);
7608 if (!app
->compatible
) {
7609 goto end_not_compatible
;
7612 pthread_mutex_lock(&ua_sess
->lock
);
7614 if (ua_sess
->deleted
) {
7618 health_code_update();
7620 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
7623 ERR("Failed to find consumer (%" PRIu32
") socket",
7624 app
->bits_per_long
);
7629 /* Clear quiescent state. */
7630 switch (ua_sess
->buffer_type
) {
7631 case LTTNG_BUFFER_PER_PID
:
7632 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &iter
.iter
,
7633 ua_chan
, node
.node
) {
7634 health_code_update();
7635 ret
= consumer_clear_quiescent_channel(socket
,
7638 ERR("Error clearing quiescent state for consumer channel");
7644 case LTTNG_BUFFER_PER_UID
:
7651 health_code_update();
7654 pthread_mutex_unlock(&ua_sess
->lock
);
7658 health_code_update();
7663 * Clear quiescent state in each stream for all applications for a
7664 * specific UST session.
7665 * Called with UST session lock held.
7668 int ust_app_clear_quiescent_session(struct ltt_ust_session
*usess
)
7673 DBG("Clearing stream quiescent state for all ust apps");
7677 switch (usess
->buffer_type
) {
7678 case LTTNG_BUFFER_PER_UID
:
7680 struct lttng_ht_iter iter
;
7681 struct buffer_reg_uid
*reg
;
7684 * Clear quiescent for all per UID buffers associated to
7687 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
7688 struct consumer_socket
*socket
;
7689 struct buffer_reg_channel
*buf_reg_chan
;
7691 /* Get associated consumer socket.*/
7692 socket
= consumer_find_socket_by_bitness(
7693 reg
->bits_per_long
, usess
->consumer
);
7696 * Ignore request if no consumer is found for
7702 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
7703 &iter
.iter
, buf_reg_chan
, node
.node
) {
7705 * The following call will print error values so
7706 * the return code is of little importance
7707 * because whatever happens, we have to try them
7710 (void) consumer_clear_quiescent_channel(socket
,
7711 buf_reg_chan
->consumer_key
);
7716 case LTTNG_BUFFER_PER_PID
:
7718 struct ust_app_session
*ua_sess
;
7719 struct lttng_ht_iter iter
;
7720 struct ust_app
*app
;
7722 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
7724 ua_sess
= lookup_session_by_app(usess
, app
);
7725 if (ua_sess
== NULL
) {
7728 (void) ust_app_clear_quiescent_app_session(app
,
7740 health_code_update();
7745 * Destroy a specific UST session in apps.
7747 static int destroy_trace(struct ltt_ust_session
*usess
, struct ust_app
*app
)
7750 struct ust_app_session
*ua_sess
;
7751 struct lttng_ht_iter iter
;
7752 struct lttng_ht_node_u64
*node
;
7754 DBG("Destroy tracing for ust app pid %d", app
->pid
);
7758 if (!app
->compatible
) {
7762 __lookup_session_by_app(usess
, app
, &iter
);
7763 node
= lttng_ht_iter_get_node_u64(&iter
);
7765 /* Session is being or is deleted. */
7768 ua_sess
= caa_container_of(node
, struct ust_app_session
, node
);
7770 health_code_update();
7771 destroy_app_session(app
, ua_sess
);
7773 health_code_update();
7775 /* Quiescent wait after stopping trace */
7776 pthread_mutex_lock(&app
->sock_lock
);
7777 ret
= ustctl_wait_quiescent(app
->sock
);
7778 pthread_mutex_unlock(&app
->sock_lock
);
7779 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
7780 ERR("UST app wait quiescent failed for app pid %d ret %d",
7785 health_code_update();
7790 * Start tracing for the UST session.
7792 int ust_app_start_trace_all(struct ltt_ust_session
*usess
)
7794 struct lttng_ht_iter iter
;
7795 struct ust_app
*app
;
7797 DBG("Starting all UST traces");
7800 * Even though the start trace might fail, flag this session active so
7801 * other application coming in are started by default.
7808 * In a start-stop-start use-case, we need to clear the quiescent state
7809 * of each channel set by the prior stop command, thus ensuring that a
7810 * following stop or destroy is sure to grab a timestamp_end near those
7811 * operations, even if the packet is empty.
7813 (void) ust_app_clear_quiescent_session(usess
);
7815 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7816 ust_app_global_update(usess
, app
);
7825 * Start tracing for the UST session.
7826 * Called with UST session lock held.
7828 int ust_app_stop_trace_all(struct ltt_ust_session
*usess
)
7831 struct lttng_ht_iter iter
;
7832 struct ust_app
*app
;
7834 DBG("Stopping all UST traces");
7837 * Even though the stop trace might fail, flag this session inactive so
7838 * other application coming in are not started by default.
7844 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7845 ret
= ust_app_stop_trace(usess
, app
);
7847 /* Continue to next apps even on error */
7852 (void) ust_app_flush_session(usess
);
7860 * Destroy app UST session.
7862 int ust_app_destroy_trace_all(struct ltt_ust_session
*usess
)
7865 struct lttng_ht_iter iter
;
7866 struct ust_app
*app
;
7868 DBG("Destroy all UST traces");
7872 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
7873 ret
= destroy_trace(usess
, app
);
7875 /* Continue to next apps even on error */
7885 /* The ua_sess lock must be held by the caller. */
7887 int find_or_create_ust_app_channel(
7888 struct ltt_ust_session
*usess
,
7889 struct ust_app_session
*ua_sess
,
7890 struct ust_app
*app
,
7891 struct ltt_ust_channel
*uchan
,
7892 struct ust_app_channel
**ua_chan
)
7895 struct lttng_ht_iter iter
;
7896 struct lttng_ht_node_str
*ua_chan_node
;
7898 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &iter
);
7899 ua_chan_node
= lttng_ht_iter_get_node_str(&iter
);
7901 *ua_chan
= caa_container_of(ua_chan_node
,
7902 struct ust_app_channel
, node
);
7906 ret
= ust_app_channel_create(usess
, ua_sess
, uchan
, app
, ua_chan
);
7914 /* The ua_sess lock must be held by the caller. */
7916 int find_or_create_ust_app_map(
7917 struct ltt_ust_session
*usess
,
7918 struct ust_app_session
*ua_sess
,
7919 struct ust_app
*app
,
7920 struct ltt_ust_map
*umap
,
7921 struct ust_app_map
**ua_map
)
7924 struct lttng_ht_iter iter
;
7925 struct lttng_ht_node_str
*ua_map_node
;
7927 lttng_ht_lookup(ua_sess
->maps
, (void *) umap
->name
, &iter
);
7928 ua_map_node
= lttng_ht_iter_get_node_str(&iter
);
7930 *ua_map
= caa_container_of(ua_map_node
,
7931 struct ust_app_map
, node
);
7935 DBG("UST map id = %"PRIu64
" not found. Creating it.", umap
->id
);
7936 ret
= ust_app_map_create(usess
, ua_sess
, umap
, app
, ua_map
);
7945 int ust_app_channel_synchronize_event(struct ust_app_channel
*ua_chan
,
7946 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
7947 struct ust_app
*app
)
7950 struct ust_app_event
*ua_event
= NULL
;
7952 ua_event
= find_ust_app_event(ua_chan
->events
, uevent
->attr
.name
,
7953 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
,
7954 uevent
->attr
.token
);
7956 ret
= create_ust_app_channel_event(ua_sess
, ua_chan
, uevent
, app
);
7962 if (ua_event
->enabled
!= uevent
->enabled
) {
7963 ret
= uevent
->enabled
?
7964 enable_ust_app_event(ua_sess
, ua_event
, app
) :
7965 disable_ust_app_event(ua_sess
, ua_event
, app
);
7973 /* Called with RCU read-side lock held. */
7975 int ust_app_map_synchronize_event(struct ust_app_map
*ua_map
,
7976 struct ltt_ust_event
*uevent
, struct ust_app_session
*ua_sess
,
7977 struct ust_app
*app
)
7980 struct ust_app_event
*ua_event
= NULL
;
7982 ua_event
= find_ust_app_event(ua_map
->events
, uevent
->attr
.name
,
7983 uevent
->filter
, uevent
->attr
.loglevel
, uevent
->exclusion
,
7984 uevent
->attr
.token
);
7986 ret
= create_ust_app_map_event(ua_sess
, ua_map
, uevent
, app
);
7991 if (ua_event
->enabled
!= uevent
->enabled
) {
7992 ret
= uevent
->enabled
?
7993 enable_ust_app_event(ua_sess
, ua_event
, app
) :
7994 disable_ust_app_event(ua_sess
, ua_event
, app
);
8003 void ust_app_synchronize_event_notifier_rules(struct ust_app
*app
)
8006 enum lttng_error_code ret_code
;
8007 enum lttng_trigger_status t_status
;
8008 struct lttng_ht_iter app_trigger_iter
;
8009 struct lttng_triggers
*triggers
= NULL
;
8010 struct ust_app_event_notifier_rule
*event_notifier_rule
;
8011 unsigned int count
, i
;
8014 * Currrently, registering or unregistering a trigger with an
8015 * event rule condition causes a full synchronization of the event
8018 * The first step attempts to add an event notifier for all registered
8019 * triggers that apply to the user space tracers. Then, the
8020 * application's event notifiers rules are all checked against the list
8021 * of registered triggers. Any event notifier that doesn't have a
8022 * matching trigger can be assumed to have been disabled.
8024 * All of this is inefficient, but is put in place to get the feature
8025 * rolling as it is simpler at this moment. It will be optimized Soon™
8026 * to allow the state of enabled
8027 * event notifiers to be synchronized in a piece-wise way.
8030 /* Get all triggers using uid 0 (root) */
8031 ret_code
= notification_thread_command_list_triggers(
8032 notification_thread_handle
, 0, &triggers
);
8033 if (ret_code
!= LTTNG_OK
) {
8040 t_status
= lttng_triggers_get_count(triggers
, &count
);
8041 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
8046 for (i
= 0; i
< count
; i
++) {
8047 const struct lttng_condition
*condition
;
8048 const struct lttng_event_rule
*event_rule
;
8049 struct lttng_trigger
*trigger
;
8050 const struct ust_app_event_notifier_rule
*looked_up_event_notifier_rule
;
8051 enum lttng_condition_status condition_status
;
8054 trigger
= lttng_triggers_borrow_mutable_at_index(triggers
, i
);
8057 token
= lttng_trigger_get_tracer_token(trigger
);
8058 condition
= lttng_trigger_get_const_condition(trigger
);
8060 if (!lttng_trigger_needs_tracer_notifier(trigger
)) {
8064 condition_status
= lttng_condition_on_event_get_rule(condition
, &event_rule
);
8065 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
8067 if (lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
) {
8068 /* Skip kernel related triggers. */
8073 * Find or create the associated token event rule. The caller
8074 * holds the RCU read lock, so this is safe to call without
8075 * explicitly acquiring it here.
8077 looked_up_event_notifier_rule
= find_ust_app_event_notifier_rule(
8078 app
->token_to_event_notifier_rule_ht
, token
);
8079 if (!looked_up_event_notifier_rule
) {
8080 ret
= create_ust_app_event_notifier_rule(trigger
, app
);
8088 /* Remove all unknown event sources from the app. */
8089 cds_lfht_for_each_entry (app
->token_to_event_notifier_rule_ht
->ht
,
8090 &app_trigger_iter
.iter
, event_notifier_rule
,
8092 const uint64_t app_token
= event_notifier_rule
->token
;
8096 * Check if the app event trigger still exists on the
8097 * notification side.
8099 for (i
= 0; i
< count
; i
++) {
8100 uint64_t notification_thread_token
;
8101 const struct lttng_trigger
*trigger
=
8102 lttng_triggers_get_at_index(
8107 notification_thread_token
=
8108 lttng_trigger_get_tracer_token(trigger
);
8110 if (notification_thread_token
== app_token
) {
8122 * This trigger was unregistered, disable it on the tracer's
8125 ret
= lttng_ht_del(app
->token_to_event_notifier_rule_ht
,
8129 /* Callee logs errors. */
8130 (void) disable_ust_object(app
, event_notifier_rule
->obj
);
8132 delete_ust_app_event_notifier_rule(
8133 app
->sock
, event_notifier_rule
, app
);
8139 lttng_triggers_destroy(triggers
);
8144 * Called with RCU read-side lock held.
8147 void ust_app_synchronize_all_channels(struct ltt_ust_session
*usess
,
8148 struct ust_app_session
*ua_sess
,
8149 struct ust_app
*app
)
8152 struct cds_lfht_iter uchan_iter
;
8153 struct ltt_ust_channel
*uchan
;
8159 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &uchan_iter
,
8161 struct ust_app_channel
*ua_chan
;
8162 struct cds_lfht_iter uevent_iter
;
8163 struct ltt_ust_event
*uevent
;
8166 * Search for a matching ust_app_channel. If none is found,
8167 * create it. Creating the channel will cause the ua_chan
8168 * structure to be allocated, the channel buffers to be
8169 * allocated (if necessary) and sent to the application, and
8170 * all enabled contexts will be added to the channel.
8172 ret
= find_or_create_ust_app_channel(usess
, ua_sess
,
8173 app
, uchan
, &ua_chan
);
8175 /* Tracer is probably gone or ENOMEM. */
8180 /* ua_chan will be NULL for the metadata channel */
8184 cds_lfht_for_each_entry(uchan
->events
->ht
, &uevent_iter
, uevent
,
8186 ret
= ust_app_channel_synchronize_event(ua_chan
,
8187 uevent
, ua_sess
, app
);
8193 if (ua_chan
->enabled
!= uchan
->enabled
) {
8194 ret
= uchan
->enabled
?
8195 enable_ust_app_channel(ua_sess
, uchan
, app
) :
8196 disable_ust_app_channel(ua_sess
, ua_chan
, app
);
8207 * Called with RCU read-side lock held.
8210 void ust_app_synchronize_all_maps(struct ltt_ust_session
*usess
,
8211 struct ust_app_session
*ua_sess
,
8212 struct ust_app
*app
)
8215 struct cds_lfht_iter umap_iter
;
8216 struct ltt_ust_map
*umap
;
8222 cds_lfht_for_each_entry(usess
->domain_global
.maps
->ht
, &umap_iter
,
8224 struct ust_app_map
*ua_map
;
8225 struct cds_lfht_iter uevent_iter
;
8226 struct ltt_ust_event
*uevent
;
8228 DBG("Synchronizing UST map id = %"PRIu64
, umap
->id
);
8230 ret
= find_or_create_ust_app_map(usess
, ua_sess
,
8231 app
, umap
, &ua_map
);
8233 /* Tracer is probably gone or ENOMEM. */
8237 DBG("Synchronizing all events of UST map id = %"PRIu64
, umap
->id
);
8238 cds_lfht_for_each_entry(umap
->events
->ht
, &uevent_iter
, uevent
,
8240 ret
= ust_app_map_synchronize_event(ua_map
,
8241 uevent
, ua_sess
, app
);
8247 if (ua_map
->enabled
!= umap
->enabled
) {
8248 if (umap
->enabled
) {
8249 DBG("Map disabled on the tracer side but shouldn't");
8250 ret
= enable_ust_app_map(ua_sess
, umap
, app
);
8252 DBG("Map enabled on the tracer side but shouldn't");
8253 ret
= disable_ust_app_map(ua_sess
, ua_map
, app
);
8265 * The caller must ensure that the application is compatible and is tracked
8266 * by the process attribute trackers.
8269 void ust_app_synchronize(struct ltt_ust_session
*usess
,
8270 struct ust_app
*app
)
8273 struct ust_app_session
*ua_sess
= NULL
;
8276 * The application's configuration should only be synchronized for
8279 assert(usess
->active
);
8281 ret
= find_or_create_ust_app_session(usess
, app
, &ua_sess
, NULL
);
8283 /* Tracer is probably gone or ENOMEM. */
8291 pthread_mutex_lock(&ua_sess
->lock
);
8292 if (ua_sess
->deleted
) {
8293 pthread_mutex_unlock(&ua_sess
->lock
);
8296 ust_app_synchronize_all_channels(usess
, ua_sess
, app
);
8297 ust_app_synchronize_all_maps(usess
, ua_sess
, app
);
8300 * Create the metadata for the application. This returns gracefully if a
8301 * metadata was already set for the session.
8303 * The metadata channel must be created after the data channels as the
8304 * consumer daemon assumes this ordering. When interacting with a relay
8305 * daemon, the consumer will use this assumption to send the
8306 * "STREAMS_SENT" message to the relay daemon.
8308 ret
= create_ust_app_metadata(ua_sess
, app
, usess
->consumer
);
8316 pthread_mutex_unlock(&ua_sess
->lock
);
8317 /* Everything went well at this point. */
8322 pthread_mutex_unlock(&ua_sess
->lock
);
8325 destroy_app_session(app
, ua_sess
);
8331 void ust_app_global_destroy(struct ltt_ust_session
*usess
, struct ust_app
*app
)
8333 struct ust_app_session
*ua_sess
;
8335 ua_sess
= lookup_session_by_app(usess
, app
);
8336 if (ua_sess
== NULL
) {
8339 destroy_app_session(app
, ua_sess
);
8343 * Add channels/events from UST global domain to registered apps at sock.
8345 * Called with session lock held.
8346 * Called with RCU read-side lock held.
8348 void ust_app_global_update(struct ltt_ust_session
*usess
, struct ust_app
*app
)
8351 assert(usess
->active
);
8353 DBG2("UST app global update for app sock %d for session id %" PRIu64
,
8354 app
->sock
, usess
->id
);
8356 if (!app
->compatible
) {
8359 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
,
8361 trace_ust_id_tracker_lookup(
8362 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
,
8364 trace_ust_id_tracker_lookup(
8365 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
,
8368 * Synchronize the application's internal tracing configuration
8369 * and start tracing.
8371 ust_app_synchronize(usess
, app
);
8372 ust_app_start_trace(usess
, app
);
8374 ust_app_global_destroy(usess
, app
);
8379 * Add all event notifiers to an application.
8381 * Called with session lock held.
8382 * Called with RCU read-side lock held.
8384 void ust_app_global_update_event_notifier_rules(struct ust_app
*app
)
8386 DBG2("UST application global event notifier rules update: app = '%s' (ppid: %d)",
8387 app
->name
, app
->ppid
);
8389 if (!app
->compatible
) {
8393 if (app
->event_notifier_group
.object
== NULL
) {
8394 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s' (ppid: %d)",
8395 app
->name
, app
->ppid
);
8399 ust_app_synchronize_event_notifier_rules(app
);
8403 * Called with session lock held.
8405 void ust_app_global_update_all(struct ltt_ust_session
*usess
)
8407 struct lttng_ht_iter iter
;
8408 struct ust_app
*app
;
8411 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
8412 ust_app_global_update(usess
, app
);
8417 void ust_app_global_update_all_event_notifier_rules(void)
8419 struct lttng_ht_iter iter
;
8420 struct ust_app
*app
;
8423 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
8424 ust_app_global_update_event_notifier_rules(app
);
8430 void ust_app_update_event_notifier_error_count(struct lttng_trigger
*trigger
)
8432 uint64_t error_count
= 0;
8433 enum event_notifier_error_accounting_status status
;
8434 struct lttng_condition
*condition
= lttng_trigger_get_condition(trigger
);
8436 status
= event_notifier_error_accounting_get_count(trigger
, &error_count
);
8437 if (status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
8438 ERR("Error getting trigger error count.");
8441 lttng_condition_on_event_set_error_count(condition
, error_count
);
8445 * Add context to a specific channel for global UST domain.
8447 int ust_app_add_ctx_channel_glb(struct ltt_ust_session
*usess
,
8448 struct ltt_ust_channel
*uchan
, struct ltt_ust_context
*uctx
)
8451 struct lttng_ht_node_str
*ua_chan_node
;
8452 struct lttng_ht_iter iter
, uiter
;
8453 struct ust_app_channel
*ua_chan
= NULL
;
8454 struct ust_app_session
*ua_sess
;
8455 struct ust_app
*app
;
8457 assert(usess
->active
);
8460 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
8461 if (!app
->compatible
) {
8463 * TODO: In time, we should notice the caller of this error by
8464 * telling him that this is a version error.
8468 ua_sess
= lookup_session_by_app(usess
, app
);
8469 if (ua_sess
== NULL
) {
8473 pthread_mutex_lock(&ua_sess
->lock
);
8475 if (ua_sess
->deleted
) {
8476 pthread_mutex_unlock(&ua_sess
->lock
);
8480 /* Lookup channel in the ust app session */
8481 lttng_ht_lookup(ua_sess
->channels
, (void *)uchan
->name
, &uiter
);
8482 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
8483 if (ua_chan_node
== NULL
) {
8486 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
,
8488 ret
= create_ust_app_channel_context(ua_chan
, &uctx
->ctx
, app
);
8493 pthread_mutex_unlock(&ua_sess
->lock
);
8501 * Receive registration and populate the given msg structure.
8503 * On success return 0 else a negative value returned by the ustctl call.
8505 int ust_app_recv_registration(int sock
, struct ust_register_msg
*msg
)
8508 uint32_t pid
, ppid
, uid
, gid
;
8512 ret
= ustctl_recv_reg_msg(sock
, &msg
->type
, &msg
->major
, &msg
->minor
,
8513 &pid
, &ppid
, &uid
, &gid
,
8514 &msg
->bits_per_long
,
8515 &msg
->uint8_t_alignment
,
8516 &msg
->uint16_t_alignment
,
8517 &msg
->uint32_t_alignment
,
8518 &msg
->uint64_t_alignment
,
8519 &msg
->long_alignment
,
8526 case LTTNG_UST_ERR_EXITING
:
8527 DBG3("UST app recv reg message failed. Application died");
8529 case LTTNG_UST_ERR_UNSUP_MAJOR
:
8530 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
8531 msg
->major
, msg
->minor
, LTTNG_UST_ABI_MAJOR_VERSION
,
8532 LTTNG_UST_ABI_MINOR_VERSION
);
8535 ERR("UST app recv reg message failed with ret %d", ret
);
8540 msg
->pid
= (pid_t
) pid
;
8541 msg
->ppid
= (pid_t
) ppid
;
8542 msg
->uid
= (uid_t
) uid
;
8543 msg
->gid
= (gid_t
) gid
;
8550 * Return a ust app session object using the application object and the
8551 * session object descriptor has a key. If not found, NULL is returned.
8552 * A RCU read side lock MUST be acquired when calling this function.
8554 static struct ust_app_session
*find_session_by_objd(struct ust_app
*app
,
8557 struct lttng_ht_node_ulong
*node
;
8558 struct lttng_ht_iter iter
;
8559 struct ust_app_session
*ua_sess
= NULL
;
8563 lttng_ht_lookup(app
->ust_sessions_objd
, (void *)((unsigned long) objd
), &iter
);
8564 node
= lttng_ht_iter_get_node_ulong(&iter
);
8566 DBG2("UST app session find by objd %d not found", objd
);
8570 ua_sess
= caa_container_of(node
, struct ust_app_session
, ust_objd_node
);
8577 * Return a ust app channel object using the application object and the channel
8578 * object descriptor has a key. If not found, NULL is returned. A RCU read side
8579 * lock MUST be acquired before calling this function.
8581 static struct ust_app_channel
*find_channel_by_objd(struct ust_app
*app
,
8584 struct lttng_ht_node_ulong
*node
;
8585 struct lttng_ht_iter iter
;
8586 struct ust_app_channel
*ua_chan
= NULL
;
8590 lttng_ht_lookup(app
->ust_chan_objd
, (void *)((unsigned long) objd
), &iter
);
8591 node
= lttng_ht_iter_get_node_ulong(&iter
);
8593 DBG2("UST app channel find by objd %d not found", objd
);
8597 ua_chan
= caa_container_of(node
, struct ust_app_channel
, ust_objd_node
);
8604 * Return a ust app map object using the application object and the map
8605 * object descriptor has a key. If not found, NULL is returned. A RCU read side
8606 * lock MUST be acquired before calling this function.
8608 static struct ust_app_map
*find_map_by_objd(struct ust_app
*app
,
8611 struct lttng_ht_node_ulong
*node
;
8612 struct lttng_ht_iter iter
;
8613 struct ust_app_map
*ua_map
= NULL
;
8617 lttng_ht_lookup(app
->ust_map_objd
, (void *)((unsigned long) objd
), &iter
);
8618 node
= lttng_ht_iter_get_node_ulong(&iter
);
8620 DBG2("UST app map find by objd %d not found", objd
);
8624 ua_map
= caa_container_of(node
, struct ust_app_map
, ust_objd_node
);
8631 * Reply to a register channel notification from an application on the notify
8632 * socket. The channel metadata is also created.
8634 * The session UST registry lock is acquired in this function.
8636 * On success 0 is returned else a negative value.
8638 static int reply_ust_register_channel(int sock
, int cobjd
,
8639 size_t nr_fields
, struct ustctl_field
*fields
)
8641 int ret
, ret_code
= 0;
8643 uint64_t chan_reg_key
;
8644 enum ustctl_channel_header type
;
8645 struct ust_app
*app
;
8646 struct ust_app_channel
*ua_chan
;
8647 struct ust_app_session
*ua_sess
;
8648 struct ust_registry_session
*registry
;
8649 struct ust_registry_channel
*ust_reg_chan
;
8653 /* Lookup application. If not found, there is a code flow error. */
8654 app
= find_app_by_notify_sock(sock
);
8656 DBG("Application socket %d is being torn down. Abort event notify",
8659 goto error_rcu_unlock
;
8662 /* Lookup channel by UST object descriptor. */
8663 ua_chan
= find_channel_by_objd(app
, cobjd
);
8665 DBG("Application channel is being torn down. Abort event notify");
8667 goto error_rcu_unlock
;
8670 assert(ua_chan
->session
);
8671 ua_sess
= ua_chan
->session
;
8673 /* Get right session registry depending on the session buffer type. */
8674 registry
= get_session_registry(ua_sess
);
8676 DBG("Application session is being torn down. Abort event notify");
8678 goto error_rcu_unlock
;
8681 /* Depending on the buffer type, a different channel key is used. */
8682 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
8683 chan_reg_key
= ua_chan
->tracing_channel_id
;
8685 chan_reg_key
= ua_chan
->key
;
8688 pthread_mutex_lock(®istry
->lock
);
8690 ust_reg_chan
= ust_registry_channel_find(registry
, chan_reg_key
);
8691 assert(ust_reg_chan
);
8693 if (!ust_reg_chan
->register_done
) {
8695 * TODO: eventually use the registry event count for
8696 * this channel to better guess header type for per-pid
8699 type
= USTCTL_CHANNEL_HEADER_LARGE
;
8700 ust_reg_chan
->nr_ctx_fields
= nr_fields
;
8701 ust_reg_chan
->ctx_fields
= fields
;
8703 ust_reg_chan
->header_type
= type
;
8705 /* Get current already assigned values. */
8706 type
= ust_reg_chan
->header_type
;
8708 /* Channel id is set during the object creation. */
8709 chan_id
= ust_reg_chan
->chan_id
;
8711 /* Append to metadata */
8712 if (!ust_reg_chan
->metadata_dumped
) {
8713 ret_code
= ust_metadata_channel_statedump(registry
, ust_reg_chan
);
8715 ERR("Error appending channel metadata (errno = %d)", ret_code
);
8721 DBG3("UST app replying to register channel key %" PRIu64
8722 " with id %u, type: %d, ret: %d", chan_reg_key
, chan_id
, type
,
8725 ret
= ustctl_reply_register_channel(sock
, chan_id
, type
, ret_code
);
8727 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
8728 ERR("UST app reply channel failed with ret %d", ret
);
8730 DBG3("UST app reply channel failed. Application died");
8735 /* This channel registry registration is completed. */
8736 ust_reg_chan
->register_done
= 1;
8739 pthread_mutex_unlock(®istry
->lock
);
8746 static int add_event_ust_chan_registry(int sock
, struct ust_app
*ua
,
8747 struct ust_app_channel
*ua_chan
, int sobjd
, int cobjd
, char *name
,
8748 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
8749 int loglevel_value
, char *model_emf_uri
)
8752 uint32_t event_id
= 0;
8753 uint64_t chan_reg_key
;
8754 struct ust_app_session
*ua_sess
;
8755 struct ust_registry_session
*registry
;
8757 * The counter index is unused for channel events. It's only used for
8760 uint64_t counter_index
= 0;
8762 assert(ua_chan
->session
);
8763 ua_sess
= ua_chan
->session
;
8765 registry
= get_session_registry(ua_sess
);
8767 DBG("Application session is being torn down. Abort event notify");
8772 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
8773 chan_reg_key
= ua_chan
->tracing_channel_id
;
8775 chan_reg_key
= ua_chan
->key
;
8778 pthread_mutex_lock(®istry
->lock
);
8781 * From this point on, this call acquires the ownership of the sig, fields
8782 * and model_emf_uri meaning any free are done inside it if needed. These
8783 * three variables MUST NOT be read/write after this.
8785 ret_code
= ust_registry_chan_create_event(registry
, chan_reg_key
,
8786 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
8787 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
8791 model_emf_uri
= NULL
;
8794 * The return value is returned to ustctl so in case of an error, the
8795 * application can be notified. In case of an error, it's important not to
8796 * return a negative error or else the application will get closed.
8798 ret
= ustctl_reply_register_event(sock
, event_id
, counter_index
, ret_code
);
8800 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
8801 ERR("UST app reply event failed with ret %d", ret
);
8803 DBG3("UST app reply event failed. Application died");
8806 * No need to wipe the create event since the application socket will
8807 * get close on error hence cleaning up everything by itself.
8812 DBG3("UST registry event %s with id %" PRId32
" added successfully",
8816 pthread_mutex_unlock(®istry
->lock
);
8820 static int add_event_ust_map_registry(int sock
, struct ust_app
*ua
,
8821 struct ust_app_map
*ua_map
, int sobjd
, int cobjd
, char *name
,
8822 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
8823 int loglevel_value
, char *model_emf_uri
, uint64_t tracer_token
)
8826 uint64_t map_reg_key
, counter_index
;
8827 struct ust_app_session
*ua_sess
;
8828 struct ust_registry_session
*registry
;
8830 assert(ua_map
->session
);
8831 ua_sess
= ua_map
->session
;
8833 registry
= get_session_registry(ua_sess
);
8835 DBG("Application session is being torn down. Abort event notify");
8840 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
8841 map_reg_key
= ua_map
->tracing_map_id
;
8843 map_reg_key
= ua_map
->key
;
8846 pthread_mutex_lock(®istry
->lock
);
8849 * From this point on, this call acquires the ownership of the sig, fields
8850 * and model_emf_uri meaning any free are done inside it if needed. These
8851 * three variables MUST NOT be read/write after this.
8853 DBG("Registry_map_create_event on map=%"PRIu64
" with token=%"PRIu64
,
8854 map_reg_key
, tracer_token
);
8855 ret_code
= ust_registry_map_create_event(registry
, map_reg_key
,
8856 sobjd
, cobjd
, name
, sig
, nr_fields
, fields
,
8857 loglevel_value
, model_emf_uri
, ua_sess
->buffer_type
,
8858 tracer_token
, &counter_index
, ua
);
8863 model_emf_uri
= NULL
;
8866 * The return value is returned to ustctl so in case of an error, the
8867 * application can be notified. In case of an error, it's important not to
8868 * return a negative error or else the application will get closed.
8870 ret
= ustctl_reply_register_event(sock
, counter_index
, counter_index
,
8873 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
8874 ERR("UST app reply event failed with ret %d", ret
);
8876 DBG3("UST app reply event failed. Application died");
8879 * No need to wipe the create event since the application socket will
8880 * get close on error hence cleaning up everything by itself.
8885 DBG3("UST registry map event %s with counter index %" PRIu64
" added successfully",
8886 name
, counter_index
);
8889 pthread_mutex_unlock(®istry
->lock
);
8895 * Add event to the UST channel registry. When the event is added to the
8896 * registry, the metadata is also created. Once done, this replies to the
8897 * application with the appropriate error code.
8899 * The session UST registry lock is acquired in the function.
8901 * On success 0 is returned else a negative value.
8903 static int add_event_ust_registry(int sock
, int sobjd
, int cobjd
, char *name
,
8904 char *sig
, size_t nr_fields
, struct ustctl_field
*fields
,
8905 int loglevel_value
, char *model_emf_uri
, uint64_t tracer_token
)
8908 struct ust_app
*app
;
8909 struct ust_app_channel
*ua_chan
= NULL
;
8910 struct ust_app_map
*ua_map
= NULL
;
8914 /* Lookup application. If not found, there is a code flow error. */
8915 app
= find_app_by_notify_sock(sock
);
8917 DBG("Application socket %d is being torn down. Abort event notify",
8923 /* Lookup channel by UST object descriptor. */
8924 ua_chan
= find_channel_by_objd(app
, cobjd
);
8926 ret
= add_event_ust_chan_registry(sock
, app
, ua_chan
, sobjd
, cobjd
,
8927 name
, sig
, nr_fields
, fields
, loglevel_value
,
8930 ERR("Error adding channel event to registry: event_name = '%s'", name
);
8935 /* Lookup map by UST object descriptor. */
8936 ua_map
= find_map_by_objd(app
, cobjd
);
8938 ret
= add_event_ust_map_registry(sock
, app
, ua_map
, sobjd
, cobjd
,
8939 name
, sig
, nr_fields
, fields
, loglevel_value
,
8940 model_emf_uri
, tracer_token
);
8942 ERR("Error adding map event to registry: event_name = '%s'", name
);
8948 if (!ua_chan
&& !ua_map
) {
8949 DBG("Application event container is being torn down. Abort event notify");
8963 * Add enum to the UST session registry. Once done, this replies to the
8964 * application with the appropriate error code.
8966 * The session UST registry lock is acquired within this function.
8968 * On success 0 is returned else a negative value.
8970 static int add_enum_ust_registry(int sock
, int sobjd
, char *name
,
8971 struct ustctl_enum_entry
*entries
, size_t nr_entries
)
8973 int ret
= 0, ret_code
;
8974 struct ust_app
*app
;
8975 struct ust_app_session
*ua_sess
;
8976 struct ust_registry_session
*registry
;
8977 uint64_t enum_id
= -1ULL;
8981 /* Lookup application. If not found, there is a code flow error. */
8982 app
= find_app_by_notify_sock(sock
);
8984 /* Return an error since this is not an error */
8985 DBG("Application socket %d is being torn down. Aborting enum registration",
8988 goto error_rcu_unlock
;
8991 /* Lookup session by UST object descriptor. */
8992 ua_sess
= find_session_by_objd(app
, sobjd
);
8994 /* Return an error since this is not an error */
8995 DBG("Application session is being torn down (session not found). Aborting enum registration.");
8997 goto error_rcu_unlock
;
9000 registry
= get_session_registry(ua_sess
);
9002 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
9004 goto error_rcu_unlock
;
9007 pthread_mutex_lock(®istry
->lock
);
9010 * From this point on, the callee acquires the ownership of
9011 * entries. The variable entries MUST NOT be read/written after
9014 ret_code
= ust_registry_create_or_find_enum(registry
, sobjd
, name
,
9015 entries
, nr_entries
, &enum_id
);
9019 * The return value is returned to ustctl so in case of an error, the
9020 * application can be notified. In case of an error, it's important not to
9021 * return a negative error or else the application will get closed.
9023 ret
= ustctl_reply_register_enum(sock
, enum_id
, ret_code
);
9025 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
9026 ERR("UST app reply enum failed with ret %d", ret
);
9028 DBG3("UST app reply enum failed. Application died");
9031 * No need to wipe the create enum since the application socket will
9032 * get close on error hence cleaning up everything by itself.
9037 DBG3("UST registry enum %s added successfully or already found", name
);
9040 pthread_mutex_unlock(®istry
->lock
);
9047 * Handle application notification through the given notify socket.
9049 * Return 0 on success or else a negative value.
9051 int ust_app_recv_notify(int sock
)
9054 enum ustctl_notify_cmd cmd
;
9056 DBG3("UST app receiving notify from sock %d", sock
);
9058 ret
= ustctl_recv_notify(sock
, &cmd
);
9060 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
9061 ERR("UST app recv notify failed with ret %d", ret
);
9063 DBG3("UST app recv notify failed. Application died");
9069 case USTCTL_NOTIFY_CMD_EVENT
:
9071 int sobjd
, cobjd
, loglevel_value
;
9072 char name
[LTTNG_UST_SYM_NAME_LEN
], *sig
, *model_emf_uri
;
9074 uint64_t tracer_token
= 0;
9075 struct ustctl_field
*fields
;
9077 DBG2("UST app ustctl register event received");
9079 ret
= ustctl_recv_register_event(sock
, &sobjd
, &cobjd
, name
,
9080 &loglevel_value
, &sig
, &nr_fields
, &fields
,
9081 &model_emf_uri
, &tracer_token
);
9083 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
9084 ERR("UST app recv event failed with ret %d", ret
);
9086 DBG3("UST app recv event failed. Application died");
9092 * Add event to the UST registry coming from the notify socket. This
9093 * call will free if needed the sig, fields and model_emf_uri. This
9094 * code path loses the ownsership of these variables and transfer them
9095 * to the this function.
9097 ret
= add_event_ust_registry(sock
, sobjd
, cobjd
, name
, sig
, nr_fields
,
9098 fields
, loglevel_value
, model_emf_uri
, tracer_token
);
9105 case USTCTL_NOTIFY_CMD_CHANNEL
:
9109 struct ustctl_field
*fields
;
9111 DBG2("UST app ustctl register channel received");
9113 ret
= ustctl_recv_register_channel(sock
, &sobjd
, &cobjd
, &nr_fields
,
9116 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
9117 ERR("UST app recv channel failed with ret %d", ret
);
9119 DBG3("UST app recv channel failed. Application died");
9125 * The fields ownership are transfered to this function call meaning
9126 * that if needed it will be freed. After this, it's invalid to access
9127 * fields or clean it up.
9129 ret
= reply_ust_register_channel(sock
, cobjd
, nr_fields
,
9137 case USTCTL_NOTIFY_CMD_ENUM
:
9140 char name
[LTTNG_UST_SYM_NAME_LEN
];
9142 struct ustctl_enum_entry
*entries
;
9144 DBG2("UST app ustctl register enum received");
9146 ret
= ustctl_recv_register_enum(sock
, &sobjd
, name
,
9147 &entries
, &nr_entries
);
9149 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
9150 ERR("UST app recv enum failed with ret %d", ret
);
9152 DBG3("UST app recv enum failed. Application died");
9157 /* Callee assumes ownership of entries */
9158 ret
= add_enum_ust_registry(sock
, sobjd
, name
,
9159 entries
, nr_entries
);
9167 /* Should NEVER happen. */
9176 * Once the notify socket hangs up, this is called. First, it tries to find the
9177 * corresponding application. On failure, the call_rcu to close the socket is
9178 * executed. If an application is found, it tries to delete it from the notify
9179 * socket hash table. Whathever the result, it proceeds to the call_rcu.
9181 * Note that an object needs to be allocated here so on ENOMEM failure, the
9182 * call RCU is not done but the rest of the cleanup is.
9184 void ust_app_notify_sock_unregister(int sock
)
9187 struct lttng_ht_iter iter
;
9188 struct ust_app
*app
;
9189 struct ust_app_notify_sock_obj
*obj
;
9195 obj
= zmalloc(sizeof(*obj
));
9198 * An ENOMEM is kind of uncool. If this strikes we continue the
9199 * procedure but the call_rcu will not be called. In this case, we
9200 * accept the fd leak rather than possibly creating an unsynchronized
9201 * state between threads.
9203 * TODO: The notify object should be created once the notify socket is
9204 * registered and stored independantely from the ust app object. The
9205 * tricky part is to synchronize the teardown of the application and
9206 * this notify object. Let's keep that in mind so we can avoid this
9207 * kind of shenanigans with ENOMEM in the teardown path.
9214 DBG("UST app notify socket unregister %d", sock
);
9217 * Lookup application by notify socket. If this fails, this means that the
9218 * hash table delete has already been done by the application
9219 * unregistration process so we can safely close the notify socket in a
9222 app
= find_app_by_notify_sock(sock
);
9227 iter
.iter
.node
= &app
->notify_sock_n
.node
;
9230 * Whatever happens here either we fail or succeed, in both cases we have
9231 * to close the socket after a grace period to continue to the call RCU
9232 * here. If the deletion is successful, the application is not visible
9233 * anymore by other threads and is it fails it means that it was already
9234 * deleted from the hash table so either way we just have to close the
9237 (void) lttng_ht_del(ust_app_ht_by_notify_sock
, &iter
);
9243 * Close socket after a grace period to avoid for the socket to be reused
9244 * before the application object is freed creating potential race between
9245 * threads trying to add unique in the global hash table.
9248 call_rcu(&obj
->head
, close_notify_sock_rcu
);
9253 * Destroy a ust app data structure and free its memory.
9255 void ust_app_destroy(struct ust_app
*app
)
9261 call_rcu(&app
->pid_n
.head
, delete_ust_app_rcu
);
9265 * Take a snapshot for a given UST session. The snapshot is sent to the given
9268 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
9270 enum lttng_error_code
ust_app_snapshot_record(
9271 const struct ltt_ust_session
*usess
,
9272 const struct consumer_output
*output
, int wait
,
9273 uint64_t nb_packets_per_stream
)
9276 enum lttng_error_code status
= LTTNG_OK
;
9277 struct lttng_ht_iter iter
;
9278 struct ust_app
*app
;
9279 char *trace_path
= NULL
;
9286 switch (usess
->buffer_type
) {
9287 case LTTNG_BUFFER_PER_UID
:
9289 struct buffer_reg_uid
*reg
;
9291 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
9292 struct buffer_reg_channel
*buf_reg_chan
;
9293 struct consumer_socket
*socket
;
9294 char pathname
[PATH_MAX
];
9295 size_t consumer_path_offset
= 0;
9297 if (!reg
->registry
->reg
.ust
->metadata_key
) {
9298 /* Skip since no metadata is present */
9302 /* Get consumer socket to use to push the metadata.*/
9303 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
9306 status
= LTTNG_ERR_INVALID
;
9310 memset(pathname
, 0, sizeof(pathname
));
9311 ret
= snprintf(pathname
, sizeof(pathname
),
9312 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
,
9313 reg
->uid
, reg
->bits_per_long
);
9315 PERROR("snprintf snapshot path");
9316 status
= LTTNG_ERR_INVALID
;
9319 /* Free path allowed on previous iteration. */
9321 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
9322 &consumer_path_offset
);
9324 status
= LTTNG_ERR_INVALID
;
9327 /* Add the UST default trace dir to path. */
9328 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
9329 buf_reg_chan
, node
.node
) {
9330 status
= consumer_snapshot_channel(socket
,
9331 buf_reg_chan
->consumer_key
,
9332 output
, 0, usess
->uid
,
9333 usess
->gid
, &trace_path
[consumer_path_offset
], wait
,
9334 nb_packets_per_stream
);
9335 if (status
!= LTTNG_OK
) {
9339 status
= consumer_snapshot_channel(socket
,
9340 reg
->registry
->reg
.ust
->metadata_key
, output
, 1,
9341 usess
->uid
, usess
->gid
, &trace_path
[consumer_path_offset
],
9343 if (status
!= LTTNG_OK
) {
9349 case LTTNG_BUFFER_PER_PID
:
9351 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
9352 struct consumer_socket
*socket
;
9353 struct lttng_ht_iter chan_iter
;
9354 struct ust_app_channel
*ua_chan
;
9355 struct ust_app_session
*ua_sess
;
9356 struct ust_registry_session
*registry
;
9357 char pathname
[PATH_MAX
];
9358 size_t consumer_path_offset
= 0;
9360 ua_sess
= lookup_session_by_app(usess
, app
);
9362 /* Session not associated with this app. */
9366 /* Get the right consumer socket for the application. */
9367 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
9370 status
= LTTNG_ERR_INVALID
;
9374 /* Add the UST default trace dir to path. */
9375 memset(pathname
, 0, sizeof(pathname
));
9376 ret
= snprintf(pathname
, sizeof(pathname
), DEFAULT_UST_TRACE_DIR
"/%s",
9379 status
= LTTNG_ERR_INVALID
;
9380 PERROR("snprintf snapshot path");
9383 /* Free path allowed on previous iteration. */
9385 trace_path
= setup_channel_trace_path(usess
->consumer
, pathname
,
9386 &consumer_path_offset
);
9388 status
= LTTNG_ERR_INVALID
;
9391 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
9392 ua_chan
, node
.node
) {
9393 status
= consumer_snapshot_channel(socket
,
9394 ua_chan
->key
, output
, 0,
9395 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
9396 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
9397 &trace_path
[consumer_path_offset
], wait
,
9398 nb_packets_per_stream
);
9402 case LTTNG_ERR_CHAN_NOT_FOUND
:
9409 registry
= get_session_registry(ua_sess
);
9411 DBG("Application session is being torn down. Skip application.");
9414 status
= consumer_snapshot_channel(socket
,
9415 registry
->metadata_key
, output
, 1,
9416 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
9417 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
9418 &trace_path
[consumer_path_offset
], wait
, 0);
9422 case LTTNG_ERR_CHAN_NOT_FOUND
:
9442 * Return the size taken by one more packet per stream.
9444 uint64_t ust_app_get_size_one_more_packet_per_stream(
9445 const struct ltt_ust_session
*usess
, uint64_t cur_nr_packets
)
9447 uint64_t tot_size
= 0;
9448 struct ust_app
*app
;
9449 struct lttng_ht_iter iter
;
9453 switch (usess
->buffer_type
) {
9454 case LTTNG_BUFFER_PER_UID
:
9456 struct buffer_reg_uid
*reg
;
9458 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
9459 struct buffer_reg_channel
*buf_reg_chan
;
9462 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
9463 buf_reg_chan
, node
.node
) {
9464 if (cur_nr_packets
>= buf_reg_chan
->num_subbuf
) {
9466 * Don't take channel into account if we
9467 * already grab all its packets.
9471 tot_size
+= buf_reg_chan
->subbuf_size
* buf_reg_chan
->stream_count
;
9477 case LTTNG_BUFFER_PER_PID
:
9480 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
9481 struct ust_app_channel
*ua_chan
;
9482 struct ust_app_session
*ua_sess
;
9483 struct lttng_ht_iter chan_iter
;
9485 ua_sess
= lookup_session_by_app(usess
, app
);
9487 /* Session not associated with this app. */
9491 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
9492 ua_chan
, node
.node
) {
9493 if (cur_nr_packets
>= ua_chan
->attr
.num_subbuf
) {
9495 * Don't take channel into account if we
9496 * already grab all its packets.
9500 tot_size
+= ua_chan
->attr
.subbuf_size
* ua_chan
->streams
.count
;
9514 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
,
9515 struct cds_list_head
*buffer_reg_uid_list
,
9516 struct consumer_output
*consumer
, uint64_t uchan_id
,
9517 int overwrite
, uint64_t *discarded
, uint64_t *lost
)
9520 uint64_t consumer_chan_key
;
9525 ret
= buffer_reg_uid_consumer_channel_key(
9526 buffer_reg_uid_list
, uchan_id
, &consumer_chan_key
);
9534 ret
= consumer_get_lost_packets(ust_session_id
,
9535 consumer_chan_key
, consumer
, lost
);
9537 ret
= consumer_get_discarded_events(ust_session_id
,
9538 consumer_chan_key
, consumer
, discarded
);
9545 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session
*usess
,
9546 struct ltt_ust_channel
*uchan
,
9547 struct consumer_output
*consumer
, int overwrite
,
9548 uint64_t *discarded
, uint64_t *lost
)
9551 struct lttng_ht_iter iter
;
9552 struct lttng_ht_node_str
*ua_chan_node
;
9553 struct ust_app
*app
;
9554 struct ust_app_session
*ua_sess
;
9555 struct ust_app_channel
*ua_chan
;
9562 * Iterate over every registered applications. Sum counters for
9563 * all applications containing requested session and channel.
9565 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
9566 struct lttng_ht_iter uiter
;
9568 ua_sess
= lookup_session_by_app(usess
, app
);
9569 if (ua_sess
== NULL
) {
9574 lttng_ht_lookup(ua_sess
->channels
, (void *) uchan
->name
, &uiter
);
9575 ua_chan_node
= lttng_ht_iter_get_node_str(&uiter
);
9576 /* If the session is found for the app, the channel must be there */
9577 assert(ua_chan_node
);
9579 ua_chan
= caa_container_of(ua_chan_node
, struct ust_app_channel
, node
);
9584 ret
= consumer_get_lost_packets(usess
->id
, ua_chan
->key
,
9591 uint64_t _discarded
;
9593 ret
= consumer_get_discarded_events(usess
->id
,
9594 ua_chan
->key
, consumer
, &_discarded
);
9598 (*discarded
) += _discarded
;
9607 int ust_app_regenerate_statedump(struct ltt_ust_session
*usess
,
9608 struct ust_app
*app
)
9611 struct ust_app_session
*ua_sess
;
9613 DBG("Regenerating the metadata for ust app pid %d", app
->pid
);
9617 ua_sess
= lookup_session_by_app(usess
, app
);
9618 if (ua_sess
== NULL
) {
9619 /* The session is in teardown process. Ignore and continue. */
9623 pthread_mutex_lock(&ua_sess
->lock
);
9625 if (ua_sess
->deleted
) {
9629 pthread_mutex_lock(&app
->sock_lock
);
9630 ret
= ustctl_regenerate_statedump(app
->sock
, ua_sess
->handle
);
9631 pthread_mutex_unlock(&app
->sock_lock
);
9634 pthread_mutex_unlock(&ua_sess
->lock
);
9638 health_code_update();
9643 * Regenerate the statedump for each app in the session.
9645 int ust_app_regenerate_statedump_all(struct ltt_ust_session
*usess
)
9648 struct lttng_ht_iter iter
;
9649 struct ust_app
*app
;
9651 DBG("Regenerating the metadata for all UST apps");
9655 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
9656 if (!app
->compatible
) {
9660 ret
= ust_app_regenerate_statedump(usess
, app
);
9662 /* Continue to the next app even on error */
9673 * Rotate all the channels of a session.
9675 * Return LTTNG_OK on success or else an LTTng error code.
9677 enum lttng_error_code
ust_app_rotate_session(struct ltt_session
*session
)
9680 enum lttng_error_code cmd_ret
= LTTNG_OK
;
9681 struct lttng_ht_iter iter
;
9682 struct ust_app
*app
;
9683 struct ltt_ust_session
*usess
= session
->ust_session
;
9689 switch (usess
->buffer_type
) {
9690 case LTTNG_BUFFER_PER_UID
:
9692 struct buffer_reg_uid
*reg
;
9694 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
9695 struct buffer_reg_channel
*buf_reg_chan
;
9696 struct consumer_socket
*socket
;
9698 if (!reg
->registry
->reg
.ust
->metadata_key
) {
9699 /* Skip since no metadata is present */
9703 /* Get consumer socket to use to push the metadata.*/
9704 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
9707 cmd_ret
= LTTNG_ERR_INVALID
;
9711 /* Rotate the data channels. */
9712 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
9713 buf_reg_chan
, node
.node
) {
9714 ret
= consumer_rotate_channel(socket
,
9715 buf_reg_chan
->consumer_key
,
9716 usess
->uid
, usess
->gid
,
9718 /* is_metadata_channel */ false);
9720 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
9725 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
9727 ret
= consumer_rotate_channel(socket
,
9728 reg
->registry
->reg
.ust
->metadata_key
,
9729 usess
->uid
, usess
->gid
,
9731 /* is_metadata_channel */ true);
9733 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
9739 case LTTNG_BUFFER_PER_PID
:
9741 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
9742 struct consumer_socket
*socket
;
9743 struct lttng_ht_iter chan_iter
;
9744 struct ust_app_channel
*ua_chan
;
9745 struct ust_app_session
*ua_sess
;
9746 struct ust_registry_session
*registry
;
9748 ua_sess
= lookup_session_by_app(usess
, app
);
9750 /* Session not associated with this app. */
9754 /* Get the right consumer socket for the application. */
9755 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
9758 cmd_ret
= LTTNG_ERR_INVALID
;
9762 registry
= get_session_registry(ua_sess
);
9764 DBG("Application session is being torn down. Skip application.");
9768 /* Rotate the data channels. */
9769 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
9770 ua_chan
, node
.node
) {
9771 ret
= consumer_rotate_channel(socket
,
9773 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
9774 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
9776 /* is_metadata_channel */ false);
9778 /* Per-PID buffer and application going away. */
9779 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
9781 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
9786 /* Rotate the metadata channel. */
9787 (void) push_metadata(registry
, usess
->consumer
);
9788 ret
= consumer_rotate_channel(socket
,
9789 registry
->metadata_key
,
9790 lttng_credentials_get_uid(&ua_sess
->effective_credentials
),
9791 lttng_credentials_get_gid(&ua_sess
->effective_credentials
),
9793 /* is_metadata_channel */ true);
9795 /* Per-PID buffer and application going away. */
9796 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
)
9798 cmd_ret
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
9816 enum lttng_error_code
ust_app_create_channel_subdirectories(
9817 const struct ltt_ust_session
*usess
)
9819 enum lttng_error_code ret
= LTTNG_OK
;
9820 struct lttng_ht_iter iter
;
9821 enum lttng_trace_chunk_status chunk_status
;
9822 char *pathname_index
;
9825 assert(usess
->current_trace_chunk
);
9828 switch (usess
->buffer_type
) {
9829 case LTTNG_BUFFER_PER_UID
:
9831 struct buffer_reg_uid
*reg
;
9833 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
9834 fmt_ret
= asprintf(&pathname_index
,
9835 DEFAULT_UST_TRACE_DIR
"/" DEFAULT_UST_TRACE_UID_PATH
"/" DEFAULT_INDEX_DIR
,
9836 reg
->uid
, reg
->bits_per_long
);
9838 ERR("Failed to format channel index directory");
9839 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
9844 * Create the index subdirectory which will take care
9845 * of implicitly creating the channel's path.
9847 chunk_status
= lttng_trace_chunk_create_subdirectory(
9848 usess
->current_trace_chunk
,
9850 free(pathname_index
);
9851 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
9852 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
9858 case LTTNG_BUFFER_PER_PID
:
9860 struct ust_app
*app
;
9863 * Create the toplevel ust/ directory in case no apps are running.
9865 chunk_status
= lttng_trace_chunk_create_subdirectory(
9866 usess
->current_trace_chunk
,
9867 DEFAULT_UST_TRACE_DIR
);
9868 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
9869 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
9873 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
,
9875 struct ust_app_session
*ua_sess
;
9876 struct ust_registry_session
*registry
;
9878 ua_sess
= lookup_session_by_app(usess
, app
);
9880 /* Session not associated with this app. */
9884 registry
= get_session_registry(ua_sess
);
9886 DBG("Application session is being torn down. Skip application.");
9890 fmt_ret
= asprintf(&pathname_index
,
9891 DEFAULT_UST_TRACE_DIR
"/%s/" DEFAULT_INDEX_DIR
,
9894 ERR("Failed to format channel index directory");
9895 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
9899 * Create the index subdirectory which will take care
9900 * of implicitly creating the channel's path.
9902 chunk_status
= lttng_trace_chunk_create_subdirectory(
9903 usess
->current_trace_chunk
,
9905 free(pathname_index
);
9906 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
9907 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
9924 * Clear all the channels of a session.
9926 * Return LTTNG_OK on success or else an LTTng error code.
9929 enum lttng_error_code
ust_app_clear_session_channels(struct ltt_session
*session
)
9932 enum lttng_error_code cmd_ret
= LTTNG_OK
;
9933 struct lttng_ht_iter iter
;
9934 struct ust_app
*app
;
9935 struct ltt_ust_session
*usess
= session
->ust_session
;
9941 if (usess
->active
) {
9942 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
9943 cmd_ret
= LTTNG_ERR_FATAL
;
9947 switch (usess
->buffer_type
) {
9948 case LTTNG_BUFFER_PER_UID
:
9950 struct buffer_reg_uid
*reg
;
9952 cds_list_for_each_entry(reg
, &usess
->buffer_reg_uid_list
, lnode
) {
9953 struct buffer_reg_channel
*buf_reg_chan
;
9954 struct consumer_socket
*socket
;
9956 /* Get consumer socket to use to push the metadata.*/
9957 socket
= consumer_find_socket_by_bitness(reg
->bits_per_long
,
9960 cmd_ret
= LTTNG_ERR_INVALID
;
9964 /* Clear the data channels. */
9965 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
, &iter
.iter
,
9966 buf_reg_chan
, node
.node
) {
9967 ret
= consumer_clear_channel(socket
,
9968 buf_reg_chan
->consumer_key
);
9974 (void) push_metadata(reg
->registry
->reg
.ust
, usess
->consumer
);
9977 * Clear the metadata channel.
9978 * Metadata channel is not cleared per se but we still need to
9979 * perform a rotation operation on it behind the scene.
9981 ret
= consumer_clear_channel(socket
,
9982 reg
->registry
->reg
.ust
->metadata_key
);
9989 case LTTNG_BUFFER_PER_PID
:
9991 cds_lfht_for_each_entry(ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
9992 struct consumer_socket
*socket
;
9993 struct lttng_ht_iter chan_iter
;
9994 struct ust_app_channel
*ua_chan
;
9995 struct ust_app_session
*ua_sess
;
9996 struct ust_registry_session
*registry
;
9998 ua_sess
= lookup_session_by_app(usess
, app
);
10000 /* Session not associated with this app. */
10004 /* Get the right consumer socket for the application. */
10005 socket
= consumer_find_socket_by_bitness(app
->bits_per_long
,
10008 cmd_ret
= LTTNG_ERR_INVALID
;
10012 registry
= get_session_registry(ua_sess
);
10014 DBG("Application session is being torn down. Skip application.");
10018 /* Clear the data channels. */
10019 cds_lfht_for_each_entry(ua_sess
->channels
->ht
, &chan_iter
.iter
,
10020 ua_chan
, node
.node
) {
10021 ret
= consumer_clear_channel(socket
, ua_chan
->key
);
10023 /* Per-PID buffer and application going away. */
10024 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
10031 (void) push_metadata(registry
, usess
->consumer
);
10034 * Clear the metadata channel.
10035 * Metadata channel is not cleared per se but we still need to
10036 * perform rotation operation on it behind the scene.
10038 ret
= consumer_clear_channel(socket
, registry
->metadata_key
);
10040 /* Per-PID buffer and application going away. */
10041 if (ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
10054 cmd_ret
= LTTNG_OK
;
10059 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
10060 cmd_ret
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
10063 cmd_ret
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
10073 enum lttng_error_code
ust_app_clear_session_maps_per_uid(
10074 struct ltt_ust_session
*usess
, struct ltt_ust_map
*umap
,
10075 uint32_t app_bitness
)
10077 struct lttng_ht_iter iter
;
10078 struct buffer_reg_uid
*buf_reg_uid
;
10079 struct buffer_reg_map
*buf_reg_map
;
10080 struct ust_registry_session
*ust_reg_sess
;
10081 struct lttng_ht_node_u64
*ust_reg_map_node
;
10082 struct ust_registry_map
*ust_reg_map
;
10083 struct ust_registry_map_index_ht_entry
*map_index_entry
;
10084 enum lttng_error_code status
;
10086 buf_reg_uid
= buffer_reg_uid_find(usess
->id
, app_bitness
, usess
->uid
);
10087 if (!buf_reg_uid
) {
10089 * Buffer registry entry for uid not found. Probably no app for
10090 * this UID at the moment.
10092 DBG("No buffer registry entry found for uid: ust-sess-id = %"PRIu64
", bitness = %"PRIu32
", uid = %d",
10093 usess
->id
, app_bitness
, usess
->uid
);
10095 * Not an error. Leave the key value pair unchanged and return.
10101 buf_reg_map
= buffer_reg_map_find(umap
->id
, buf_reg_uid
);
10102 if (!buf_reg_uid
) {
10103 ERR("Error getting per-uid map buffer registry entry: map-id = %"PRIu64
,
10105 status
= LTTNG_ERR_UNK
;
10109 ust_reg_sess
= buf_reg_uid
->registry
->reg
.ust
;
10111 /* Get the ust_reg map object from the registry */
10112 // FIXME: frdeso: This can be changed to ust_registry_map_find() right?
10114 lttng_ht_lookup(ust_reg_sess
->maps
, (void *) &umap
->id
, &iter
);
10115 ust_reg_map_node
= lttng_ht_iter_get_node_u64(&iter
);
10116 if (!ust_reg_map_node
) {
10117 ERR("Error getting per-uid map buffer registry entry: map-id = %"PRIu64
,
10119 status
= LTTNG_ERR_UNK
;
10122 ust_reg_map
= caa_container_of(ust_reg_map_node
,
10123 struct ust_registry_map
, node
);
10125 cds_lfht_for_each_entry(ust_reg_map
->key_string_to_bucket_index_ht
->ht
,
10126 &iter
.iter
, map_index_entry
, node
.node
) {
10128 size_t dimension_indexes
[1] = {map_index_entry
->index
};
10130 ret
= ustctl_counter_clear(buf_reg_map
->daemon_counter
, dimension_indexes
);
10132 ERR("clearing counter index %"PRIu64
, map_index_entry
->index
);
10133 //fixme: frdeso: convert ust errors to tools errors
10134 status
= LTTNG_ERR_UNK
;
10146 enum lttng_error_code
ust_app_clear_session_maps_per_pid(
10147 struct ltt_ust_session
*usess
, struct ltt_ust_map
*umap
,
10148 uint32_t app_bitness
)
10150 struct lttng_ht_iter app_iter
;
10151 enum lttng_error_code status
;
10152 struct ust_app
*app
;
10153 struct map_kv_ht_entry
*kv_entry
;
10154 struct lttng_ht_iter iter
;
10156 cds_lfht_for_each_entry(ust_app_ht
->ht
, &app_iter
.iter
, app
, pid_n
.node
) {
10157 struct lttng_ht_iter map_iter
, key_iter
;
10158 struct lttng_ht_node_str
*ua_map_node
;
10159 struct ust_app_map
*ua_map
;
10160 struct ust_app_session
*ua_sess
;
10161 struct ust_registry_session
*ust_reg_sess
;
10162 struct ust_registry_map
*ust_reg_map
;
10163 struct ust_registry_map_index_ht_entry
*map_index_entry
;
10165 if (app
->bits_per_long
!= app_bitness
) {
10169 ua_sess
= lookup_session_by_app(usess
, app
);
10171 /* Session not associated with this app. */
10175 ust_reg_sess
= get_session_registry(ua_sess
);
10176 if (!ust_reg_sess
) {
10177 DBG("Application session is being torn down. Skip application.");
10181 /* Lookup map in the ust app session */
10182 lttng_ht_lookup(ua_sess
->maps
, (void *)umap
->name
, &map_iter
);
10183 ua_map_node
= lttng_ht_iter_get_node_str(&map_iter
);
10185 assert(ua_map_node
!= NULL
);
10186 ua_map
= caa_container_of(ua_map_node
, struct ust_app_map
, node
);
10188 pthread_mutex_lock(&ust_reg_sess
->lock
);
10189 ust_reg_map
= ust_registry_map_find(ust_reg_sess
, ua_map
->key
);
10190 pthread_mutex_unlock(&ust_reg_sess
->lock
);
10191 assert(ust_reg_map
);
10193 /* Iterate over all the formated_key -> counter index */
10194 cds_lfht_for_each_entry(ust_reg_map
->key_string_to_bucket_index_ht
->ht
,
10195 &key_iter
.iter
, map_index_entry
, node
.node
) {
10198 size_t dimension_indexes
[1] = {map_index_entry
->index
};
10200 ret
= ustctl_counter_clear(ua_map
->map_handle
,
10201 dimension_indexes
);
10203 ERR("clearing counter index %"PRIu64
, map_index_entry
->index
);
10204 //fixme: frdeso: convert ust errors to tools errors
10205 status
= LTTNG_ERR_UNK
;
10212 * Emptying the dead app key values.
10214 pthread_mutex_lock(&umap
->dead_app_kv_values
.lock
);
10216 if (app_bitness
== 32) {
10217 cds_lfht_for_each_entry(umap
->dead_app_kv_values
.dead_app_kv_values_32bits
->ht
,
10218 &iter
.iter
, kv_entry
, node
.node
) {
10219 kv_entry
->value
= 0;
10223 cds_lfht_for_each_entry(umap
->dead_app_kv_values
.dead_app_kv_values_64bits
->ht
,
10224 &iter
.iter
, kv_entry
, node
.node
) {
10225 kv_entry
->value
= 0;
10229 pthread_mutex_unlock(&umap
->dead_app_kv_values
.lock
);
10237 enum lttng_error_code
ust_app_clear_session_maps(struct ltt_session
*session
)
10239 struct ltt_ust_session
*usess
= session
->ust_session
;
10240 enum lttng_error_code status
;
10241 struct lttng_ht_iter iter
;
10242 struct ltt_ust_map
*umap
;
10244 cds_lfht_for_each_entry(usess
->domain_global
.maps
->ht
, &iter
.iter
,
10247 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
10248 status
= ust_app_clear_session_maps_per_uid(session
->ust_session
,
10250 assert(status
== LTTNG_OK
);
10252 status
= ust_app_clear_session_maps_per_uid(session
->ust_session
,
10254 assert(status
== LTTNG_OK
);
10255 //fixme:frdeso:error handling
10257 status
= ust_app_clear_session_maps_per_pid(session
->ust_session
,
10259 assert(status
== LTTNG_OK
);
10261 status
= ust_app_clear_session_maps_per_pid(session
->ust_session
,
10263 assert(status
== LTTNG_OK
);
10264 //fixme:frdeso:error handling
10274 enum lttng_error_code
ust_app_clear_session(struct ltt_session
*session
)
10276 enum lttng_error_code cmd_ret
;
10279 cmd_ret
= ust_app_clear_session_channels(session
);
10280 if (cmd_ret
!= LTTNG_OK
) {
10281 ERR("Clearing session's channels");
10285 cmd_ret
= ust_app_clear_session_maps(session
);
10286 if (cmd_ret
!= LTTNG_OK
) {
10287 ERR("Clearing session's maps");
10295 * This function skips the metadata channel as the begin/end timestamps of a
10296 * metadata packet are useless.
10298 * Moreover, opening a packet after a "clear" will cause problems for live
10299 * sessions as it will introduce padding that was not part of the first trace
10300 * chunk. The relay daemon expects the content of the metadata stream of
10301 * successive metadata trace chunks to be strict supersets of one another.
10303 * For example, flushing a packet at the beginning of the metadata stream of
10304 * a trace chunk resulting from a "clear" session command will cause the
10305 * size of the metadata stream of the new trace chunk to not match the size of
10306 * the metadata stream of the original chunk. This will confuse the relay
10307 * daemon as the same "offset" in a metadata stream will no longer point
10308 * to the same content.
10310 enum lttng_error_code
ust_app_open_packets(struct ltt_session
*session
)
10312 enum lttng_error_code ret
= LTTNG_OK
;
10313 struct lttng_ht_iter iter
;
10314 struct ltt_ust_session
*usess
= session
->ust_session
;
10320 switch (usess
->buffer_type
) {
10321 case LTTNG_BUFFER_PER_UID
:
10323 struct buffer_reg_uid
*reg
;
10325 cds_list_for_each_entry (
10326 reg
, &usess
->buffer_reg_uid_list
, lnode
) {
10327 struct buffer_reg_channel
*buf_reg_chan
;
10328 struct consumer_socket
*socket
;
10330 socket
= consumer_find_socket_by_bitness(
10331 reg
->bits_per_long
, usess
->consumer
);
10333 ret
= LTTNG_ERR_FATAL
;
10337 cds_lfht_for_each_entry(reg
->registry
->channels
->ht
,
10338 &iter
.iter
, buf_reg_chan
, node
.node
) {
10339 const int open_ret
=
10340 consumer_open_channel_packets(
10342 buf_reg_chan
->consumer_key
);
10344 if (open_ret
< 0) {
10345 ret
= LTTNG_ERR_UNK
;
10352 case LTTNG_BUFFER_PER_PID
:
10354 struct ust_app
*app
;
10356 cds_lfht_for_each_entry (
10357 ust_app_ht
->ht
, &iter
.iter
, app
, pid_n
.node
) {
10358 struct consumer_socket
*socket
;
10359 struct lttng_ht_iter chan_iter
;
10360 struct ust_app_channel
*ua_chan
;
10361 struct ust_app_session
*ua_sess
;
10362 struct ust_registry_session
*registry
;
10364 ua_sess
= lookup_session_by_app(usess
, app
);
10366 /* Session not associated with this app. */
10370 /* Get the right consumer socket for the application. */
10371 socket
= consumer_find_socket_by_bitness(
10372 app
->bits_per_long
, usess
->consumer
);
10374 ret
= LTTNG_ERR_FATAL
;
10378 registry
= get_session_registry(ua_sess
);
10380 DBG("Application session is being torn down. Skip application.");
10384 cds_lfht_for_each_entry(ua_sess
->channels
->ht
,
10385 &chan_iter
.iter
, ua_chan
, node
.node
) {
10386 const int open_ret
=
10387 consumer_open_channel_packets(
10391 if (open_ret
< 0) {
10393 * Per-PID buffer and application going
10396 if (open_ret
== -LTTNG_ERR_CHAN_NOT_FOUND
) {
10400 ret
= LTTNG_ERR_UNK
;